Reputation: 53
I'm using Tensorflow for a short time. Here is my problem : I load AlexNet weights to do fine tuning on it, so i give batch of size 50. So i defined :
# Graph input
x = tf.placeholder(tf.float32, [50, 227, 227, 3])
y = tf.placeholder(tf.float32, [None, 40])
I give a batch of 50 images and want to get 40 output classes.
Then I defined my model
class Model:
@staticmethod
def alexnet(_X, _dropout):
# Layer 1 (conv-relu-pool-lrn)
conv1 = conv(_X, 11, 11, 96, 4, 4, padding='VALID', name='conv1')
conv1 = max_pool(conv1, 3, 3, 2, 2, padding='VALID', name='pool1')
norm1 = lrn(conv1, 2, 2e-05, 0.75, name='norm1')
# Layer 2 (conv-relu-pool-lrn)
conv2 = conv(norm1, 5, 5, 256, 1, 1, group=2, name='conv2')
conv2 = max_pool(conv2, 3, 3, 2, 2, padding='VALID', name='pool2')
norm2 = lrn(conv2, 2, 2e-05, 0.75, name='norm2')
# Layer 3 (conv-relu)
conv3 = conv(norm2, 3, 3, 384, 1, 1, name='conv3')
# Layer 4 (conv-relu)
conv4 = conv(conv3, 3, 3, 384, 1, 1, group=2, name='conv4')
# Layer 5 (conv-relu-pool)
conv5 = conv(conv4, 3, 3, 256, 1, 1, group=2, name='conv5')
pool5 = max_pool(conv5, 3, 3, 2, 2, padding='VALID', name='pool5')
# Layer 6 (fc-relu-drop)
fc6 = tf.reshape(pool5, [-1, 6*6*256])
fc6 = fc(fc6, 6*6*256, 4096, name='fc6')
fc6 = dropout(fc6, _dropout)
# Layer 7 (fc-relu-drop)
fc7 = fc(fc6, 4096, 4096, name='fc7')
fc7 = dropout(fc7, _dropout)
# Layer 8 (fc-prob)
fc8 = fc(fc7, 4096, 40, relu=False, name='fc8')
return fc8 # fc8 and fc7 (for transfer-learning)
and create it
keep_var = tf.placeholder(tf.float32)
# Model
pred = Model.alexnet(x, keep_var)
I can do the training, it works well, but at the end, i want to give only one image, but the x placeholder and y placeholder are defined for 50 images so it raises an error. Here is my code after the training to give only one image :
x_test = tf.placeholder(tf.float32, [1, 227, 227, 3])
y_test = tf.placeholder(tf.float32, [None, 40])
img = loaded_img_train[0][:][:][:] # Only one image
label = loaded_lab_train[0][:] # Only one label
prediction = sess.run(pred, feed_dict={x_test: [img], y_test: [label], keep_var: 1.})
And it raises me this error :
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [50,227,227,3]
[[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[50,227,227,3], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
I can't figured out how to feed the input size I want.
My exercise is directly inspired from the flower recognition with cnn
Thanks a lot for your help ! Guillaume
Upvotes: 0
Views: 20359
Reputation: 390
Instead of setting the first dimension of the shape to a fixed size, you can use for the first dimension of the shape a variable size by setting None instead of a number. Tensorflow is able to calculate the batch size by the input size and the fixed sizes of the other dimensions of the shape.
For the placeholder y, you have made that correct:
y = tf.placeholder(tf.float32, [None, 40])
For the placeholder x, you have to set:
x = tf.placeholder(tf.float32, [None, 227, 227, 3])
Upvotes: 5