评估一张图像的CNN模型

时间:2018-07-09 08:48:30

标签: python-3.x tensorflow tensorflow-estimator

我是tensorflow的新手,我正在尝试它,所以如果你们中的一个能够帮助我,我将非常感激。 因此,我创建了一个模型CNN,并对其进行了训练,以将图像分为2类,例如,花卉其他,我认为我的工作做得很好那但是如果您有任何想法我该如何改进这个模型,请告诉我。

但是我的问题是在训练了该模型之后,如何使用它对一个特定的图像进行分类?如果可能的话,我不想使用下巴。有人可以给我一些建议或示例吗?

我的代码:

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()

因此,如果有人可以帮助我并且知道如何仅对一张照片进行评估,请帮助我。

谢谢:)

1 个答案:

答案 0 :(得分:0)

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})