Issues with shaping Tensorflow/TFLearn inputs/outputs for images
<p>To learn more about deep learning and computer vision, I'm working on a project to perform lane-detection on roads. I'm using TFLearn as a wrapper around Tensorflow.</p>

<p><strong>Background</strong></p>

<p>The training inputs are images of roads (each image represented as a 50x50 pixel 2D array, with each element being a luminance value from 0.0 to 1.0).</p>

<p>The training outputs are the same shape (50x50 array), but represent the marked lane area. Essentially, non-road pixels are 0, and road pixels are 1.</p>

<p>This is not a fixed-size image classification problem, but instead a problem of detecting road vs. non-road pixels from a picture.</p>

<p><strong>Problem</strong></p>

<p>I've not been able to successfully shape my inputs/outputs in a way that TFLearn/Tensorflow accepts, and I'm not sure why. Here is my sample code:</p>

<pre><code># X = An array of training inputs (of shape (50 x 50)).
# Y = An array of training outputs (of shape (50 x 50)).

# "None" equals the number of samples in my training set, 50 represents
# the size of the 2D image array, and 1 represents the single channel
# (grayscale) of the image.
network = input_data(shape=[None, 50, 50, 1])

network = conv_2d(network, 50, 50, activation='relu')

# Does the 50 argument represent the output shape? Should this be 2500?
network = fully_connected(network, 50, activation='softmax')

network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.001)

model = tflearn.DNN(network, tensorboard_verbose=1)

model.fit(X, Y, n_epoch=10, shuffle=True, validation_set=(X, Y), show_metric=True, batch_size=1)
</code></pre>

<p>The error I receive is on the <code>model.fit</code> call, with error:</p>

<p><code>ValueError: Cannot feed value of shape (1, 50, 50) for Tensor u'InputData/X:0', which has shape '(?, 50, 50, 1)'</code></p>

<p>I've tried reducing the sample input/output arrays to a 1D vector (with length 2500), but that leads to other errors.</p>

<p>I'm a bit lost with how to shape all this, any help would be greatly appreciated!</p>

<p>Have a look at the imageflow wrapper for tensorflow, which converts a numpy array containing multiple images into a .tfrecords file, which is the suggested format for using tensorflow <a href="https://github.com/HamedMP/ImageFlow" rel="nofollow">https://github.com/HamedMP/ImageFlow</a>.</p>

<p>You have to install it using </p>

<pre><code>$ pip install imageflow
</code></pre>

<p>Suppose your numpy array containing some 'k' images is <code>k_images</code> and the corresponding k labels (one-hot-encoded) are stored in <code>k_labels</code>, then creating a .tfrecords file with the name 'tfr_file.tfrecords' gets as simple as writing the line</p>

<pre><code>imageflow.convert_images(k_images, k_labels, 'tfr_file')
</code></pre>

<p>Alternatively, Google's Inception model contains a code to read images in a folder assuming each folder represents one label <a href="https://github.com/tensorflow/models/blob/master/inception/inception/data/build_image_data.py" rel="nofollow">https://github.com/tensorflow/models/blob/master/inception/inception/data/build_image_data.py</a></p>

python machine-learning computer-vision neural-network tensorflow