TensorFlow训练CNN上的自定义图像 [英] TensorFlow Training CNN on Custom Images

查看:110
本文介绍了TensorFlow训练CNN上的自定义图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有tensorflow教程都做得很好,但是,它们都使用了开箱即用的经过预处理的可下载数据集.他们关于MNIST的教程就是一个很好的例子.
对于一个学校项目,我和其他4个人已被分配对CNN进行PNG图像形式的数据训练.它只是一个包含150张图像的目录.标签包含在图像文件名中.

All the tensorflow tutorials do a great job, however, they all use preprocessed downloadable datasets that work out of the box. Their tutorial on MNIST is the perfect example.
For a school project, 4 others and I have been assigned to train a CNN on supplied data in the form of PNG images. It's just a directory with 150 images. The labels are contained in the image file names.

代码现在的位置,我们遇到了一个错误,下面将对此进行介绍.

The way the codes sits now we are getting an error which I will include below.

我们遵循此处找到的MNIST代码:

We followed the MNIST code found here: https://github.com/tensorflow/tensorflow/blob/r1.3/tensorflow/examples/tutorials/layers/cnn_mnist.py

因此,我们可以肯定地确定我们的问题在于如何处理图像数据. 我们一直在努力使它工作大约3天. (我们已经解决了许多错误,这只是最新的).

So we are fairly certain our problem is in how we have processed the image data. We have been trying to get this to work for roughly 3 days. (Many errors that we have worked through, this is just the latest).

任何帮助或反馈将不胜感激! 另外,如果有人对此有疑问,请发表评论.

Any help or feedback would be greatly appreciated! Also, if anybody has questions about this please comment.

import os

import tensorflow as tf
import numpy as np
#from PIL import Image


# a function
def cnn_model_fn(features,labels,mode):
    """Model function for CNN."""
    # Input Layer
    input_layer = tf.reshape(features['x'], [-1, 128, 128, 3])

    # Convolutional Layer #1
    conv_1 = tf.layers.conv2d(
        inputs=input_layer,
        filters=64,
        kernel_size=[7, 7],
        strides=2,
        padding="same",
        activation=tf.nn.relu)
    conv_2 = tf.layers.conv2d(
        inputs=conv_1,
        filters=128,
        kernel_size=[5, 5],
        padding="same",
        strides = 2,
        activation=tf.nn.relu)
    max_pool_1 = tf.layers.max_pooling2d(
        inputs = conv_2,
        pool_size = 3,
        strides = 1
    )
    conv_3 = tf.layers.conv2d(
        inputs=max_pool_1,
        filters=96,
        kernel_size=[3, 3],
        activation=tf.nn.relu
    )
    max_pool_2 = tf.layers.max_pooling2d(
        inputs = conv_3,
        pool_size = 2,
        strides = 1
    )
    dropout_1 = tf.layers.dropout(
        inputs = max_pool_2,
        rate=0.5
    )
    fully_connected_1 = tf.contrib.layers.fully_connected(
        inputs = dropout_1,
        num_outputs = 1024,

    )
    dropout_2 = tf.layers.dropout(
        inputs = fully_connected_1,
        rate=0.5
    )
    fully_connected_2 = tf.contrib.layers.fully_connected(
        inputs = dropout_2,
        num_outputs = 1024,

    )
    fully_connected_3 = tf.contrib.layers.fully_connected(
        inputs = fully_connected_2,
        num_outputs = 15,

    )
    softmax_layer = tf.contrib.layers.softmax(
        logits = fully_connected_3
    )
#------------------------------------------------------------------------MAIN--------------------------------------------------------------------------------------------------


def getLabels():

    imagelabels_arr = []

    image_files = os.listdir("../assets/CNN-Data/")

    for image in image_files:
        imagelabels_arr.append(image.split('.')[len(image.split('.'))-2])

    return imagelabels_arr


def getTrainImages():

    filenames = []

    image_files = os.listdir("../assets/CNN-Data/")

    for image in image_files:
        filenames.append(image)

    filename_queue = tf.train.string_input_producer(filenames)

    reader = tf.WholeFileReader()
    filename, content = reader.read(filename_queue)
    image = tf.image.decode_png(content, channels=3)
    images = np.asarray(image)
    image = tf.cast(image, tf.float64)
    resize_image = tf.image.resize_images(image, (128, 128))

   # image_batch = tf.train.batch([resize_image], batch_size=10)

    print(resize_image)
    return resize_image


with tf.Session() as sess:

    sess.run(tf.initialize_all_variables())

    classifier = tf.estimator.Estimator(
        model_fn=cnn_model_fn, model_dir="./test")

    train_input_fn = tf.estimator.inputs.numpy_input_fn(
          x={'x':np.array(getTrainImages())},
          y=np.array(getLabels()),
          batch_size=10,
          num_epochs=None,
          shuffle=True)

    classifier.train(
          input_fn=train_input_fn,
          steps=20,
          )

错误:

Traceback (most recent call last):
  File "CNN.py", line 134, in <module>
    steps=20,
  File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\estimator.py", line 241, in train
    loss = self._train_model(input_fn=input_fn, hooks=hooks)
  File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\estimator.py", line 628, in _train_model
    input_fn, model_fn_lib.ModeKeys.TRAIN)
  File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\estimator.py", line 499, in _get_features_and_labels_from_input_fn
    result = self._call_input_fn(input_fn, mode)
  File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\estimator.py", line 585, in _call_input_fn
    return input_fn(**kwargs)
  File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\inputs\numpy_io.py", line 109, in input_fn
    if len(set(v.shape[0] for v in ordered_dict_x.values())) != 1:
  File "C:\Users\Tyler\Desktop\tensorFlowPratice\flowenv\lib\site-packages\tensorflow\python\estimator\inputs\numpy_io.py", line 109, in <genexpr>
    if len(set(v.shape[0] for v in ordered_dict_x.values())) != 1:
IndexError: tuple index out of range

推荐答案

classifier.train函数需要numpy数组,但不需要张量.因此,您需要通过在会话中进行评估来转换example_batch, label batch,而不是使用np.array()函数包装它们. (说明)

classifier.train function expects numpy arrays, but not tensors. Hence you need to convert example_batch, label batch by evaluating them with a session, but not wrapping them using np.array() function. (Explanation)

sess.run(tf.initialize_all_variables())

tf.train.start_queue_runners(sess)

classifier = tf.estimator.Estimator(
    model_fn=cnn_model_fn, model_dir="./test")

train_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={'x':getTrainImages().eval()},
      y=getLabels().eval(),
      batch_size=10,
      num_epochs=None,
      shuffle=True)

classifier.train(
      input_fn=train_input_fn,
      steps=20,
      )

这篇关于TensorFlow训练CNN上的自定义图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆