Создание прогнозов с помощью пользовательского CNN с использованием ввода tfrecord - PullRequest
0 голосов
/ 06 июня 2018

Моя цель - классифицировать изображения по десяти категориям.У меня есть файл tfrecord в качестве ввода.Вы можете скачать его здесь (30 МБ).Я изменил код в соответствии с ответом:

import tensorflow as tf
import tensorflow.contrib.slim as slim
import numpy as np

def my_cnn(images, num_classes, is_training):  # is_training is not used...
    with slim.arg_scope([slim.max_pool2d], kernel_size=[3, 3], stride=2):
        net = slim.conv2d(images, 64, [5, 5])
        net = slim.max_pool2d(net)
        net = slim.conv2d(net, 64, [5, 5])
        net = slim.max_pool2d(net)
        net = slim.flatten(net)
        net = slim.fully_connected(net, 192)
        net = slim.fully_connected(net, num_classes, activation_fn=None)       
        return net

data_path = 'train-some.tfrecords' 

with tf.Graph().as_default():
    batch_size, height, width, channels = 10, 224, 224, 3  
    feature = {'train/image': tf.FixedLenFeature([], tf.string),
               'train/label': tf.FixedLenFeature([], tf.int64)}
    filename_queue = tf.train.string_input_producer([data_path], num_epochs=1)
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(serialized_example, features=feature)
    image = tf.decode_raw(features['train/image'], tf.float32)
    label = tf.cast(features['train/label'], tf.int32)
    image = tf.reshape(image, [224, 224, 3])
    images, labels = tf.train.shuffle_batch([image, label], batch_size, capacity=30, num_threads=1, min_after_dequeue=10)

    num_classes = 10
    logits = my_cnn(images, num_classes, is_training=True)
    probabilities = tf.nn.softmax(logits)


with tf.Session() as sess:
    init_op = [tf.global_variables_initializer(), tf.local_variables_initializer()]
    # Run the init_op, evaluate the model outputs and print the results:
    sess.run(init_op)
    #probabilities = sess.run(probabilities)

    # Create a coordinator, launch the queue runner threads.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    try:
        while not coord.should_stop():
            while True:
                prob = sess.run(probabilities)
                print('Probabilities Shape:')
                print(prob.shape) 

    except tf.errors.OutOfRangeError:
        # When done, ask the threads to stop.
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()
        # Wait for threads to finish.
    coord.join(threads)

    # Save the model
    saver = tf.train.Saver()
    saver.save(sess, './slim_model/custom_model')

К сожалению, у меня все еще есть сообщения об ошибках:

ValueError: Tensor Tensor ("Softmax: 0", shape = (10, 10), dtype = float32) не является элементом этого графа.

ValueError: Аргумент Fetch нельзя интерпретировать как Tensor.(Тензор Тензор («Softmax: 0», shape = (10, 10), dtype = float32) не является элементом этого графа.)

1 Ответ

0 голосов
/ 10 июня 2018

Проблема связана с вашей тренировкой.Вам нужно запустить очереди, используя tf.train.start_queue_runners, который будет запускать несколько потоков для обработки и постановки в очередь примеров.Создайте Coordinator и попросите обработчика очереди запустить свои потоки с координатором.

Проверьте изменения кода:


with tf.Session() as sess:
    init_op = [tf.global_variables_initializer(), tf.local_variables_initializer()]
    # Run the init_op, evaluate the model outputs and print the results:
    sess.run(init_op)
    #probabilities = sess.run(probabilities)

    # Create a coordinator, launch the queue runner threads.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord)
    try:
        while not coord.should_stop():
            while True:
                prob = sess.run(probabilities)
                print('Probabilities Shape:')
                print(prob.shape) 

    except tf.errors.OutOfRangeError:
        # When done, ask the threads to stop.
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()
        # Wait for threads to finish.
    coord.join(threads)

    # Save the model
    saver = tf.train.Saver()
    saver.save(sess, './slim_model/custom_model'

Вывод:

 Probabilities Shape:
 (10, 10)
 Probabilities Shape:
 (10, 10)
 Probabilities Shape:
 (10, 10)
 Probabilities Shape:
 (10, 10)
 Probabilities Shape:
 (10, 10)
Done training -- epoch limit reached

Код с вышеуказанными исправлениями наряду с сохранением и восстановлением модели можетможно скачать с здесь .

...