Search code examples
pythontensorflowdeep-learningtflearn

ValueError: Cannot feed value of shape TensorFlow


I'm having some issues with TFLearn / TensorFlow. I have adjusted my np.reshape to the appropriate sizes but am crashing with the error:

This error happens at line 17 of training code:

ValueError: Cannot feed value of shape (48, 1) for Tensor 'TargetsData/Y:0', which has shape '(?, 2)'

Line for reference:

model.fit(X, Y, n_epoch=250, validation_set=(W,Z), show_metric=True)

My training code is the following:

import deepneuralnet as net
import numpy as np
from tflearn.data_utils import image_preloader
import os

model = net.model
train_path = os.path.abspath('train')
print(train_path)
X, Y = image_preloader(target_path=train_path, image_shape=(100, 100),
 mode='folder', grayscale=False, categorical_labels=True, normalize=True)
X = np.reshape(X, (-1, 100, 100, 3))

validate_path = os.path.abspath('validate')
W, Z = image_preloader(target_path=validate_path, image_shape=(100, 100),
 mode='folder', grayscale=False, categorical_labels=True, normalize=True)
W = np.reshape(W, (-1, 100, 100, 3))
model.fit(X, Y, n_epoch=250, validation_set=(W,Z), show_metric=True)
model.save('./ZtrainedNet/final-model.tfl')

And the neural net is:

import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.estimator import regression
from tflearn.metrics import Accuracy

acc = Accuracy()
network = input_data(shape=[None, 100, 100, 3])
# Conv layers ------------------------------------
network = conv_2d(network, 64, 3, strides=1, activation='relu')
network = max_pool_2d(network, 2, strides=2)
network = conv_2d(network, 64, 3, strides=1, activation='relu')
network = max_pool_2d(network, 2, strides=2)
network = conv_2d(network, 64, 3, strides=1, activation='relu')
network = conv_2d(network, 64, 3, strides=1, activation='relu')
network = conv_2d(network, 64, 3, strides=1, activation='relu')
network = max_pool_2d(network, 2, strides=2)
# Fully Connected Layers -------------------------
network = fully_connected(network, 1024, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 1024, activation='tanh')
network = dropout(network, 0.5)
network = fully_connected(network, 2, activation='softmax')
network = regression(network, optimizer='momentum',
 loss='categorical_crossentropy',
learning_rate=0.001, metric=acc)
model = tflearn.DNN(network)

My understanding is it has something to do with the softmax? I am unsure though.


Solution

  • Turns out that subfolders were messed up. The 2 corresponded to the number of subfolders I had, which I thought I set up correctly, but only had 1 subfolder inside "train."