簡體   English   中英

評估一張圖像的CNN模型

[英]Evaluating CNN model for one image

我是tensorflow的新手,我正在嘗試它,所以如果你們中的一個人能夠幫助我,我將非常感謝。 因此,我創建了一個模型CNN並對其進行了訓練,以將其分為兩類,例如FLOWERSOTHERS ,我認為我的工作做得很好,但是如果您有任何想法如何改進此模型請告訴我。

但是我的問題是,在訓練了該模型之后,如何使用它對一個特定的圖像進行分類? 如果可能的話,我不想使用下巴。 有人可以給我一些建議或示例嗎?

我的代碼:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import time
import math
import numpy as np
from PIL import Image
import tensorflow as tf
import os

# Basic model parameters as external flags.
flags = tf.flags
FLAGS = flags.FLAGS
flags.DEFINE_float('learning_rate', 1e-4, 'Initial learning rate.')
flags.DEFINE_integer('max_steps', 10000, 'Number of steps to run trainer.')
flags.DEFINE_integer('hidden1', 256, 'Number of units in hidden layer 1.')
flags.DEFINE_integer('hidden2', 64, 'Number of units in hidden layer 2.')
flags.DEFINE_integer('batch_size', 32, 'Batch size.  '
                                       'Must divide evenly into the dataset sizes.')
flags.DEFINE_string('train_dir', "ModelData/data", 'Directory to put the training data.')
flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data '
                                         'for unit testing.')
NUM_CLASSES = 2
IMAGE_SIZE = 200
CHANNELS = 3
IMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE * CHANNELS


# starter_learning_rate = 0.1


def inference(images, hidden1_units, hidden2_units):
    # Hidden 1
    with tf.name_scope('hidden1'):
        weights = tf.Variable(
            tf.truncated_normal([IMAGE_PIXELS, hidden1_units], stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))),
            name='weights')
        biases = tf.Variable(tf.zeros([hidden1_units]), name='biases')
        hidden1 = tf.nn.relu(tf.matmul(images, weights) + biases)
    # Hidden 2
    with tf.name_scope('hidden2'):
        weights = tf.Variable(
            tf.truncated_normal([hidden1_units, hidden2_units], stddev=1.0 / math.sqrt(float(hidden1_units))),
            name='weights')
        biases = tf.Variable(tf.zeros([hidden2_units]), name='biases')
        hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases)
    # Linear
    with tf.name_scope('softmax_linear'):
        weights = tf.Variable(
            tf.truncated_normal([hidden2_units, NUM_CLASSES], stddev=1.0 / math.sqrt(float(hidden2_units))),
            name='weights')
        biases = tf.Variable(tf.zeros([NUM_CLASSES]), name='biases')
        logits = tf.matmul(hidden2, weights) + biases
    return logits


def cal_loss(logits, labels):
    labels = tf.to_int64(labels)
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels, name='xentropy')
    loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
    return loss


def training(loss, learning_rate):
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    global_step = tf.Variable(0, name='global_step', trainable=False)
    train_op = optimizer.minimize(loss, global_step=global_step)
    return train_op


def evaluation(logits, labels):
    correct = tf.nn.in_top_k(logits, labels, 1)
    return tf.reduce_sum(tf.cast(correct, tf.int32))


def placeholder_inputs(batch_size):
    images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, IMAGE_PIXELS))
    labels_placeholder = tf.placeholder(tf.int32, shape=batch_size)
    return images_placeholder, labels_placeholder


def fill_feed_dict(images_feed, labels_feed, images_pl, labels_pl):
    feed_dict = {
        images_pl: images_feed,
        labels_pl: labels_feed,
    }
    return feed_dict


def do_eval(sess, eval_correct, images_placeholder, labels_placeholder, data_set):
    # And run one epoch of eval.
    true_count = 0  # Counts the number of correct predictions.
    steps_per_epoch = 32 // FLAGS.batch_size
    num_examples = steps_per_epoch * FLAGS.batch_size
    for step in range(steps_per_epoch):
        feed_dict = fill_feed_dict(train_images, train_labels, images_placeholder, labels_placeholder)
        true_count += sess.run(eval_correct, feed_dict=feed_dict)
    precision = true_count / num_examples
    print('  Num examples: %d  Num correct: %d  Precision @ 1: %0.04f' % (num_examples, true_count, precision))


# Get the sets of images and labels for training, validation, and
def init_training_data_set(dir):
    train_images = []
    train_labels = []

    def GetFoldersList():
        mylist = []
        filelist = os.listdir(dir)
        for name in filelist:
            if os.path.isdir(os.path.join(dir, name)):
                mylist.append(name)
        return mylist

    def ReadImagesFromFolder(folder):
        fin_dir = os.path.join(dir, folder)
        images_name = os.listdir(fin_dir)
        images = []
        for img_name in images_name:
            img_location = os.path.join(dir, folder)
            final_loc = os.path.join(img_location, img_name)
            try:
                hash_folder = int(folder.split("_")[0])
                images.append((np.array(Image.open(final_loc).convert('RGB')), hash_folder))
            except:
                pass
        return images

    folders = GetFoldersList()
    for folder in folders:
        for imgs in ReadImagesFromFolder(folder):
            train_images.append(imgs[0])
            train_labels.append(imgs[1])
    return train_images, train_labels


train_images, train_labels = init_training_data_set(os.path.join("FetchData", "Image"))
train_images = np.array(train_images)
train_images = train_images.reshape(len(train_images), IMAGE_PIXELS)

train_labels = np.array(train_labels)


def restore_model_last_version(saver, sess):
    def get_biggest_index(folder):
        import re
        index_vals = []
        for file in os.listdir(folder):
            split_data = file.split(".")
            extension = split_data[len(split_data) - 1]
            if extension == "meta":
                index = int(re.findall(r"\d+", file)[0])
                index_vals.append(index)
        index_vals.sort(reverse=True)
        if index_vals:
            return index_vals[0]
        else:
            return ""

    real_path = os.path.abspath(os.path.split(FLAGS.train_dir)[0])
    index = get_biggest_index(real_path)
    isdir = os.path.isdir(real_path)
    is_empty = True
    if isdir:
        if os.listdir(real_path):
            is_empty = False

    if not is_empty:
        saver.restore(sess, FLAGS.train_dir + "-" + str(index))


def run_training():
    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
        # Generate placeholders for the images and labels.
        images_placeholder, labels_placeholder = placeholder_inputs(len(train_images))

        # Build a Graph that computes predictions from the inference model.
        logits = inference(images_placeholder, FLAGS.hidden1, FLAGS.hidden2)

        # Add to the Graph the Ops for loss calculation.
        loss = cal_loss(logits, labels_placeholder)

        # Add to the Graph the Ops that calculate and apply gradients.
        train_op = training(loss, FLAGS.learning_rate)

        # Add the Op to compare the logits to the labels during evaluation.
        eval_correct = evaluation(logits, labels_placeholder)

        # Create a saver for writing training checkpoints.
        saver = tf.train.Saver(save_relative_paths=True)

        # Create a session for running Ops on the Graph.
        # sess = tf.Session()

        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        config.gpu_options.per_process_gpu_memory_fraction = 0.9
        # gpu_options = tf.GPUOptions(allow_growth=True)
        # sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
        sess = tf.Session(config=config)

        # Run the Op to initialize the variables.
        # init = train_op.g
        init = tf.global_variables_initializer()
        sess.run(init)

        restore_model_last_version(saver, sess)

        # And then after everything is built, start the training loop.
        for step in range(FLAGS.max_steps):
            start_time = time.time()
            feed_dict = fill_feed_dict(train_images, train_labels, images_placeholder, labels_placeholder)
            _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)
            duration = time.time() - start_time

            if (step) % 1000 == 0 or (step + 1) == FLAGS.max_steps:
                print("Current step is: " + str(step))
                print("Current los value: " + str(loss_value))
                print("Current duration: " + str(duration))
                print("\n")

                saver.save(sess, save_path=FLAGS.train_dir, global_step=step)
                print('Training Data Eval:')
                do_eval(sess, eval_correct, images_placeholder, labels_placeholder, train_images)


def main(_):
    run_training()


if __name__ == '__main__':
    tf.app.run()

因此,如果有人可以幫助我並且知道如何僅對一張照片進行評估,請幫助我。

謝謝 :)

Tensorflow中的幾乎每個操作都希望您傳遞批處理輸入,以充分利用現代GPU的並行化能力。

現在,如果要推斷單個圖像,則只需要將此圖像視為大小為1的批處理即可。這是快速代碼段:

# Load image 
img = np.array(Image.open(your_path).convert('RGB'))

# Expand dimensions to simulate a batch of size 1
img = np.expand_dims(img, 0)

...
# Get prediction
pred = sess.run(tf.nn.softmax(logits), {images_placeholder: img})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM