训练后将数据输入张量流模型

时间:2017-09-18 19:14:38

标签: python-3.x machine-learning tensorflow

我已经制作了一个简单的图表,以便我可以测试恢复图表并输入数据用于评估目的。我使用CIFAR10作为我的测试集,模型我只在两个卷积层中构建,然后是完全连接的层。数据通过队列加载,由图形处理,并应用反向传播。

模型的代码:

# Libraries
# Standard Libraries
import os
import re
import sys

# Third Party Libraries
import numpy as np
import tensorflow as tf

# Custom Paths
PACKAGE_PARENT = ".."
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(),
                                              os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

# User Defined Libraries
# User Defined Libraries
from helper_scripts.synset_cifar import *

# Directories
tf.app.flags.DEFINE_string("data_dir", "~/Documents/CIFAR/data/",
                           "The directory containing the training images")
tf.app.flags.DEFINE_string("save_dir", "./save/", "Checkpoints save directory")

# Model Paramaters
tf.app.flags.DEFINE_integer("num_classes", 10, "The number of classes")
tf.app.flags.DEFINE_integer("batch_size", 64, "The batch size")
tf.app.flags.DEFINE_integer('num_epochs', 3, "The number of training steps")

FLAGS = tf.app.flags.FLAGS

def load_data(data_dir):
    data = []
    file_list = []

    for root, dirs, files in os.walk(data_dir, topdown=False):
        for file in files:
            label_name = re.search(r'(n\d+)', file).group(1)
            img_path = "{}{}/{}".format(data_dir, label_name, file)
            file_list.append(img_path)


    for img_fn in file_list:
        ext = os.path.splitext(img_fn)[1] # Gets the extensions of the files in the filelist
        if ext != '.png': 
            continue

        label_name = re.search(r'(n\d+)', img_fn).group(1) # Synset index

        fn = os.path.join(data_dir, img_fn)

        label_index = synset_map[label_name]["index"]

        data.append({
            "filename": fn,
            "label_name": label_name, #n\d+
            "label_index": label_index,
            "desc": synset[label_index],
        })

    return data

def decode_jpeg(image_buffer):
    # with tf.name_scope("decode_jpeg", values=[image_buffer]):
    image = tf.image.decode_jpeg(image_buffer, channels=3)
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    image = tf.cast(image, tf.float32)
    image.set_shape([32, 32, 3])

    return image

def distorted_inputs():
    data = load_data(FLAGS.data_dir)
    images_and_labels = []

    filenames = [d['filename'] for d in data]
    label_indexes_ = [d['label_index'] for d in data]

    label_indexes = tf.one_hot(label_indexes_, depth=FLAGS.num_classes, 
        on_value=1.0, off_value=0.0, axis=-1, dtype=tf.float32)

    # with tf.variable_scope('InputProducer'):
    filename, label_index = tf.train.slice_input_producer(
        [filenames, label_indexes], 
        num_epochs=FLAGS.num_epochs, 
        seed=22, 
        capacity=32, 
        shuffle=True)

    image_buffer = tf.read_file(filename)

    image = decode_jpeg(image_buffer)
    images_and_labels.append([image, label_index])

    images, label_index_batch = tf.train.batch_join(images_and_labels,
        batch_size=FLAGS.batch_size,
        capacity=2 * FLAGS.batch_size,
        dynamic_pad=False,
        allow_smaller_final_batch=True)

    return images, label_index_batch 

def weight_variable(shape):
    initial = tf.truncated_normal(shape, stddev=0.1)

    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1, shape=shape)
    return tf.Variable(initial)

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def conv_layer(input, shape):
    W = weight_variable(shape)
    b = bias_variable([shape[3]])

    return tf.nn.relu(conv2d(input, W) + b)

def max_pool_2x2(x):
    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

def full_layer(input, size):
    in_size = int(input.get_shape()[1])
    W = weight_variable([in_size, size])
    b = bias_variable([size])

    return tf.matmul(input, W) + b

def loss(labels, logits):
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
        labels=labels,
        logits=logits))

    return loss

def accuracy(labels, logits):
    correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    return accuracy

def inference(images):
    with tf.variable_scope("conv1x"):
        conv1 = conv_layer(images, shape=[5, 5, 3, 32])
        conv1_pool = max_pool_2x2(conv1)

    with tf.variable_scope("conv2x"):
        conv2 = conv_layer(conv1_pool, shape=[5, 5, 32, 64])
        conv2_pool = max_pool_2x2(conv2)

    with tf.variable_scope("fc_layer"):
        conv2_flat = tf.reshape(conv2_pool, [-1, 8 * 8 * 64])
        full_1 = tf.nn.relu(full_layer(conv2_flat, 1024))
        # full1_drop = tf.nn.dropout(full_1, keep_prob=keep_prob)
        y_conv = full_layer(full_1, 10)

    return y_conv

def train(logits, labels, length=300):
    with tf.variable_scope("loss"):
        loss_ = loss(labels, logits)
        accu_ = accuracy(logits, labels)

    global_step = tf.get_variable('global_step', [],
                                  initializer=tf.constant_initializer(0),
                                  trainable=False)

    optimizer = tf.train.AdamOptimizer(1e-3)
    train_op = optimizer.minimize(loss_, global_step=global_step)

    init_op = tf.group(tf.global_variables_initializer(), 
                   tf.local_variables_initializer())

    saver = tf.train.Saver(tf.global_variables(), max_to_keep=2)

    with tf.Session() as sess:
        writer = tf.summary.FileWriter("./graphs", sess.graph)

        sess.run(init_op)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        try:
            for i in range(length):
                if coord.should_stop():
                    break

                o = sess.run([loss_, train_op, accu_, global_step])
                print("Loss: {:05f}, Accuracy: {:04f}, Global Step: {:04d}".\
                    format(o[0], o[2], int(o[3]))) 

                # Save the model checkpoints periodically:
                if o[-1] > 1 and o[-1] % 100 == 0:
                    checkpoint_path = os.path.join(FLAGS.save_dir, "model_cifar.ckpt")
                    saver.save(sess, checkpoint_path, global_step=global_step)

        except Exception as e:
            print("Error: {}".format(e))

        finally:
            writer.close()
            coord.request_stop()
            coord.join(threads)

def main(*args, **kwargs):
    images, labels = distorted_inputs()
    y_conv = inference(images)
    train(y_conv, labels, length=400)

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

为了简单起见,模型似乎可以像人们期望的那样工作。现在,在恢复模型时,我已经看过了:

Tensorflow: restoring a graph and model then running evaluation on a single image

Tensorflow: how to save/restore a model?

其中第一个链接似乎是最有用的bigdata2s解决方案,但我无法使其正常工作。我的代码:

def forward():
    images = tf.placeholder(tf.float32, (1, 32, 32, 3), name='imgs')
    loc_test_img = "./images/test.png"
    img = mpimage.imread(loc_test_img)

    sess = tf.Session('', tf.Graph())

    with sess.graph.as_default() as graph:
        # Read meta graph and checkpoint to restore tf session
        saver = tf.train.import_meta_graph("./save/model_cifar.ckpt-301.meta")
        saver.restore(sess, "./save/model_cifar.ckpt-301")

        # images = tf.placeholder(tf.float32, (1, 32, 32, 3), name='imgs')

        # Read a single image from a file.
        img = np.expand_dims(img, axis=0)

        # Start the queue runners. If they are not started the program will hang
        # see e.g. https://www.tensorflow.org/programmers_guide/reading_data
        coord = tf.train.Coordinator()
        threads = []
        for qr in graph.get_collection(tf.GraphKeys.QUEUE_RUNNERS):
            threads.extend(qr.create_threads(sess, coord=coord, daemon=True,
                                             start=True))

        # In the graph created above, feed "is_training" and "imgs" 
        # placeholders. Feeding them will disconnect the path from queue 
        # runners to the graph and enable a path from the placeholder 
        # instead. The "img" placeholder will be fed with the image that
        # was read above.

        # o = sess.run(pred, feed_dict={'images': img})

        # Prints classifiction results
    sess.close()
    # print(logits)  

我已尝试过其他发布的解决方案,但仍然没有运气。

1 个答案:

答案 0 :(得分:1)

您是否尝试取消注释该行:

 o = sess.run(pred, feed_dict={'images': img})

对于培训或测试,必须执行Tensorflow会话才能获得输出(据我所知)。

然后您需要打印o

的值

还有一点,你定义了pred吗?经过简单的介绍后,无法在代码中看到它。与sess.run一样,需要定义您正在执行的变量。

您可能希望使用accuracyaccu_loss代替,这些已在已恢复的模型中定义且仍应存在。有了这些,您需要根据您的培训代码定义提供logits, labels作为feed_dict输入。

这具有与培训阶段中使用的指标相当的额外好处。