Models: InvalidArgumentError: logits and labels must be broadcastable: logits_size=[64,48] labels_size=[32,48] [[node softmax_cross_entropy_loss/xentropy (defined at <ipython-input-51-b7b564720801>:112) = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](softmax_cross_entropy_loss/xentropy/Reshape, softmax_cross_entropy_loss/xentropy/Reshape_1)]]

Created on 12 Jan 2019  路  11Comments  路  Source: tensorflow/models

batch_size = 32
total_batch = len(train_batches)
num_epochs = 50

tf.reset_default_graph()
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 48], name="truth")

Set the weights for the network

xavier = tf.contrib.layers.xavier_initializer_conv2d()
conv1_weights = tf.get_variable(name="c1", initializer=xavier, shape=[3, 3, 1, 32])
conv1_biases = tf.Variable(tf.zeros([32]))
conv2_weights = tf.get_variable(name="c2", initializer=xavier, shape=[3, 3, 32, 32])
conv2_biases = tf.Variable(tf.zeros([32]))
conv3_weights = tf.get_variable(name="c3", initializer=xavier, shape=[3, 3, 32, 32])
conv3_biases = tf.Variable(tf.zeros([32]))
conv4_weights = tf.get_variable(name="c4", initializer=xavier, shape=[3, 3, 32, 32])
conv4_biases = tf.Variable(tf.zeros([32]))
conv5_weights = tf.get_variable(name="c5", initializer=xavier, shape=[3, 3, 32, 32])
conv5_biases = tf.Variable(tf.zeros([32]))

conv6_weights = tf.get_variable(name="c6", initializer=xavier, shape=[3, 3, 32, 64])
conv6_biases = tf.Variable(tf.zeros([64]))
conv7_weights = tf.get_variable(name="c7", initializer=xavier, shape=[3, 3, 64, 64])
conv7_biases = tf.Variable(tf.zeros([64]))
conv8_weights = tf.get_variable(name="c8", initializer=xavier, shape=[3, 3, 64, 64])
conv8_biases = tf.Variable(tf.zeros([64]))
conv9_weights = tf.get_variable(name="c9", initializer=xavier, shape=[3, 3, 64, 64])
conv9_biases = tf.Variable(tf.zeros([64]))

conv10_weights = tf.get_variable(name="c10", initializer=xavier, shape=[3, 3, 64, 128])
conv10_biases = tf.Variable(tf.zeros([128]))
conv11_weights = tf.get_variable(name="c11", initializer=xavier, shape=[3, 3, 128, 128])
conv11_biases = tf.Variable(tf.zeros([128]))
conv12_weights = tf.get_variable(name="c12", initializer=xavier, shape=[3, 3, 128, 128])
conv12_biases = tf.Variable(tf.zeros([128]))
conv13_weights = tf.get_variable(name="c13", initializer=xavier, shape=[3, 3, 128, 128])
conv13_biases = tf.Variable(tf.zeros([128]))

conv14_weights = tf.get_variable(name="c14", initializer=xavier, shape=[3, 3, 128, 256])
conv14_biases = tf.Variable(tf.zeros([256]))
conv15_weights = tf.get_variable(name="c15", initializer=xavier, shape=[3, 3, 256, 256])
conv15_biases = tf.Variable(tf.zeros([256]))
conv16_weights = tf.get_variable(name="c16", initializer=xavier, shape=[3, 3, 256, 256])
conv16_biases = tf.Variable(tf.zeros([256]))

fc1_weights = tf.Variable(tf.truncated_normal([1024, 200], stddev=0.1))
fc1_biases = tf.Variable(tf.zeros([200]))
fc2_weights = tf.Variable(tf.truncated_normal([200, 100], stddev=0.1))
fc2_biases = tf.Variable(tf.zeros([100]))

out_weights = tf.Variable(tf.truncated_normal([100, 48], stddev=0.1))
out_biases = tf.Variable(tf.zeros([48]))

Stack the Layers

reshaped_input = tf.reshape(x, [-1, 28, 28, 1], name="absolute_input")

Stack 1

conv1 = tf.nn.conv2d(reshaped_input, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))
conv2 = tf.nn.conv2d(relu1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
conv3 = tf.nn.conv2d(relu2, conv3_weights, strides=[1, 1, 1, 1], padding='SAME')
relu3 = tf.nn.relu(tf.nn.bias_add(conv3, conv3_biases))
conv4 = tf.nn.conv2d(relu3, conv4_weights, strides=[1, 1, 1, 1], padding='SAME')
relu4 = tf.nn.relu(tf.nn.bias_add(conv4, conv4_biases))
conv5 = tf.nn.conv2d(relu4, conv5_weights, strides=[1, 1, 1, 1], padding='SAME')
relu5 = tf.nn.relu(tf.nn.bias_add(conv5, conv5_biases))
pool1 = tf.nn.max_pool(relu5, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
print(pool1.shape)

Stack 2

conv6 = tf.nn.conv2d(pool1, conv6_weights, strides=[1, 1, 1, 1], padding='SAME')
relu6 = tf.nn.relu(tf.nn.bias_add(conv6, conv6_biases))
conv7 = tf.nn.conv2d(relu6, conv7_weights, strides=[1, 1, 1, 1], padding='SAME')
relu7 = tf.nn.relu(tf.nn.bias_add(conv7, conv7_biases))
conv8 = tf.nn.conv2d(relu7, conv8_weights, strides=[1, 1, 1, 1], padding='SAME')
relu8 = tf.nn.relu(tf.nn.bias_add(conv8, conv8_biases))
conv9 = tf.nn.conv2d(relu8, conv9_weights, strides=[1, 1, 1, 1], padding='SAME')
relu9 = tf.nn.relu(tf.nn.bias_add(conv9, conv9_biases))
pool2 = tf.nn.max_pool(relu9, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
print(pool2.shape)

Stack 3

conv10 = tf.nn.conv2d(pool2, conv10_weights, strides=[1, 1, 1, 1], padding='SAME')
relu10 = tf.nn.relu(tf.nn.bias_add(conv10, conv10_biases))
conv11 = tf.nn.conv2d(relu10, conv11_weights, strides=[1, 1, 1, 1], padding='SAME')
relu11 = tf.nn.relu(tf.nn.bias_add(conv11, conv11_biases))
conv12 = tf.nn.conv2d(relu11, conv12_weights, strides=[1, 1, 1, 1], padding='SAME')
relu12 = tf.nn.relu(tf.nn.bias_add(conv12, conv12_biases))
conv13 = tf.nn.conv2d(relu12, conv13_weights, strides=[1, 1, 1, 1], padding='SAME')
relu13 = tf.nn.relu(tf.nn.bias_add(conv13, conv13_biases))
pool3 = tf.nn.max_pool(relu13, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
print(pool3.shape)

Stack 4

conv14 = tf.nn.conv2d(pool3, conv14_weights, strides=[1, 1, 1, 1], padding='SAME')
relu14 = tf.nn.relu(tf.nn.bias_add(conv14, conv14_biases))
conv15 = tf.nn.conv2d(relu14, conv15_weights, strides=[1, 1, 1, 1], padding='SAME')
relu15 = tf.nn.relu(tf.nn.bias_add(conv15, conv15_biases))
conv16 = tf.nn.conv2d(relu15, conv16_weights, strides=[1, 1, 1, 1], padding='SAME')
relu16 = tf.nn.relu(tf.nn.bias_add(conv16, conv16_biases))
pool4 = tf.nn.max_pool(relu16, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
print(pool4.shape)

layer 4

pool_shape = pool4.get_shape().as_list()
reshaped = tf.reshape(pool3, [-1, pool_shape[1] * pool_shape[2] * pool_shape[3]])
fc1 = tf.nn.relu(tf.add(tf.matmul(reshaped, fc1_weights), fc1_biases))
fc2 = tf.nn.relu(tf.add(tf.matmul(fc1, fc2_weights), fc2_biases))
print(fc2.shape)
y = tf.add(tf.matmul(fc2, out_weights), out_biases, name="absolute_output")
print(y.shape,y_.shape)

Define loss and optimizer

cross_entropy = tf.reduce_mean(tf.losses.softmax_cross_entropy(y_,y))

train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cross_entropy)

Train the model

sess = tf.InteractiveSession()
tf.initialize_all_variables().run()
for i in tqdm(range(num_epochs)):
for i in range(total_batch):
batch_x, batch_y =train_batches[i]
print("batch",batch_x.shape,batch_y.shape)
sess.run(train_step, feed_dict={x: batch_x, y_: batch_y})

Test trained model

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
test_acc = []
train_acc = []
for i in tqdm(range(total_batch)):
batch_x, batch_y = train_batches[i]
test_acc.append(sess.run(accuracy, feed_dict={x: batch_x, y_: batch_y}))
for i in tqdm(range(len(test_batches))):
batch_x, batch_y = test_batches[i]
train_acc.append(sess.run(accuracy, feed_dict={x: batch_x, y_: batch_y}))
print(np.mean(train_acc), np.mean(test_acc))

I searched on Google but I didn't find any solution. It's saying logits have size [64,48] and labels have size [32,48]

My batch size is 32 and number of classes is 48

Most helpful comment

@MagnusFelinto @OneManArmy93
This happened to me as well, but in regards to the shape of the labels, instead of the batch. Basically, my final classification was defined with 4 outputs.
But then I changed my dataset, to have only 3 labels, and tried to run the script again.

I just had to change my last layer of the model, from:

tf.keras.layers.Dense(4, activation="softmax")

to:

tf.keras.layers.Dense(3, activation="softmax")

If you are using, Keras Sequence Generator, don't forget to take a look into it as well when defining the returned labels etc..

All 11 comments

batch_size = 32
total_batch = len(train_batches)
num_epochs = 50

tf.reset_default_graph()
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 48], name="truth")

Set the weights for the network

xavier = tf.contrib.layers.xavier_initializer_conv2d()
conv1_weights = tf.get_variable(name="c1", initializer=xavier, shape=[3, 3, 1, 32])
conv1_biases = tf.Variable(tf.zeros([32]))
conv2_weights = tf.get_variable(name="c2", initializer=xavier, shape=[3, 3, 32, 32])
conv2_biases = tf.Variable(tf.zeros([32]))
conv3_weights = tf.get_variable(name="c3", initializer=xavier, shape=[3, 3, 32, 32])
conv3_biases = tf.Variable(tf.zeros([32]))
conv4_weights = tf.get_variable(name="c4", initializer=xavier, shape=[3, 3, 32, 32])
conv4_biases = tf.Variable(tf.zeros([32]))
conv5_weights = tf.get_variable(name="c5", initializer=xavier, shape=[3, 3, 32, 32])
conv5_biases = tf.Variable(tf.zeros([32]))

conv6_weights = tf.get_variable(name="c6", initializer=xavier, shape=[3, 3, 32, 64])
conv6_biases = tf.Variable(tf.zeros([64]))
conv7_weights = tf.get_variable(name="c7", initializer=xavier, shape=[3, 3, 64, 64])
conv7_biases = tf.Variable(tf.zeros([64]))
conv8_weights = tf.get_variable(name="c8", initializer=xavier, shape=[3, 3, 64, 64])
conv8_biases = tf.Variable(tf.zeros([64]))
conv9_weights = tf.get_variable(name="c9", initializer=xavier, shape=[3, 3, 64, 64])
conv9_biases = tf.Variable(tf.zeros([64]))

conv10_weights = tf.get_variable(name="c10", initializer=xavier, shape=[3, 3, 64, 128])
conv10_biases = tf.Variable(tf.zeros([128]))
conv11_weights = tf.get_variable(name="c11", initializer=xavier, shape=[3, 3, 128, 128])
conv11_biases = tf.Variable(tf.zeros([128]))
conv12_weights = tf.get_variable(name="c12", initializer=xavier, shape=[3, 3, 128, 128])
conv12_biases = tf.Variable(tf.zeros([128]))
conv13_weights = tf.get_variable(name="c13", initializer=xavier, shape=[3, 3, 128, 128])
conv13_biases = tf.Variable(tf.zeros([128]))

conv14_weights = tf.get_variable(name="c14", initializer=xavier, shape=[3, 3, 128, 256])
conv14_biases = tf.Variable(tf.zeros([256]))
conv15_weights = tf.get_variable(name="c15", initializer=xavier, shape=[3, 3, 256, 256])
conv15_biases = tf.Variable(tf.zeros([256]))
conv16_weights = tf.get_variable(name="c16", initializer=xavier, shape=[3, 3, 256, 256])
conv16_biases = tf.Variable(tf.zeros([256]))

fc1_weights = tf.Variable(tf.truncated_normal([1024, 200], stddev=0.1))
fc1_biases = tf.Variable(tf.zeros([200]))
fc2_weights = tf.Variable(tf.truncated_normal([200, 100], stddev=0.1))
fc2_biases = tf.Variable(tf.zeros([100]))

out_weights = tf.Variable(tf.truncated_normal([100, 48], stddev=0.1))
out_biases = tf.Variable(tf.zeros([48]))

Stack the Layers

reshaped_input = tf.reshape(x, [-1, 28, 28, 1], name="absolute_input")

Stack 1

conv1 = tf.nn.conv2d(reshaped_input, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_biases))
conv2 = tf.nn.conv2d(relu1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME')
relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_biases))
conv3 = tf.nn.conv2d(relu2, conv3_weights, strides=[1, 1, 1, 1], padding='SAME')
relu3 = tf.nn.relu(tf.nn.bias_add(conv3, conv3_biases))
conv4 = tf.nn.conv2d(relu3, conv4_weights, strides=[1, 1, 1, 1], padding='SAME')
relu4 = tf.nn.relu(tf.nn.bias_add(conv4, conv4_biases))
conv5 = tf.nn.conv2d(relu4, conv5_weights, strides=[1, 1, 1, 1], padding='SAME')
relu5 = tf.nn.relu(tf.nn.bias_add(conv5, conv5_biases))
pool1 = tf.nn.max_pool(relu5, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
print(pool1.shape)

Stack 2

conv6 = tf.nn.conv2d(pool1, conv6_weights, strides=[1, 1, 1, 1], padding='SAME')
relu6 = tf.nn.relu(tf.nn.bias_add(conv6, conv6_biases))
conv7 = tf.nn.conv2d(relu6, conv7_weights, strides=[1, 1, 1, 1], padding='SAME')
relu7 = tf.nn.relu(tf.nn.bias_add(conv7, conv7_biases))
conv8 = tf.nn.conv2d(relu7, conv8_weights, strides=[1, 1, 1, 1], padding='SAME')
relu8 = tf.nn.relu(tf.nn.bias_add(conv8, conv8_biases))
conv9 = tf.nn.conv2d(relu8, conv9_weights, strides=[1, 1, 1, 1], padding='SAME')
relu9 = tf.nn.relu(tf.nn.bias_add(conv9, conv9_biases))
pool2 = tf.nn.max_pool(relu9, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
print(pool2.shape)

Stack 3

conv10 = tf.nn.conv2d(pool2, conv10_weights, strides=[1, 1, 1, 1], padding='SAME')
relu10 = tf.nn.relu(tf.nn.bias_add(conv10, conv10_biases))
conv11 = tf.nn.conv2d(relu10, conv11_weights, strides=[1, 1, 1, 1], padding='SAME')
relu11 = tf.nn.relu(tf.nn.bias_add(conv11, conv11_biases))
conv12 = tf.nn.conv2d(relu11, conv12_weights, strides=[1, 1, 1, 1], padding='SAME')
relu12 = tf.nn.relu(tf.nn.bias_add(conv12, conv12_biases))
conv13 = tf.nn.conv2d(relu12, conv13_weights, strides=[1, 1, 1, 1], padding='SAME')
relu13 = tf.nn.relu(tf.nn.bias_add(conv13, conv13_biases))
pool3 = tf.nn.max_pool(relu13, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
print(pool3.shape)

Stack 4

conv14 = tf.nn.conv2d(pool3, conv14_weights, strides=[1, 1, 1, 1], padding='SAME')
relu14 = tf.nn.relu(tf.nn.bias_add(conv14, conv14_biases))
conv15 = tf.nn.conv2d(relu14, conv15_weights, strides=[1, 1, 1, 1], padding='SAME')
relu15 = tf.nn.relu(tf.nn.bias_add(conv15, conv15_biases))
conv16 = tf.nn.conv2d(relu15, conv16_weights, strides=[1, 1, 1, 1], padding='SAME')
relu16 = tf.nn.relu(tf.nn.bias_add(conv16, conv16_biases))
pool4 = tf.nn.max_pool(relu16, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
print(pool4.shape)

layer 4

pool_shape = pool4.get_shape().as_list()
reshaped = tf.reshape(pool3, [-1, pool_shape[1] * pool_shape[2] * pool_shape[3]])
fc1 = tf.nn.relu(tf.add(tf.matmul(reshaped, fc1_weights), fc1_biases))
fc2 = tf.nn.relu(tf.add(tf.matmul(fc1, fc2_weights), fc2_biases))
print(fc2.shape)
y = tf.add(tf.matmul(fc2, out_weights), out_biases, name="absolute_output")
print(y.shape,y_.shape)

Define loss and optimizer

cross_entropy = tf.reduce_mean(tf.losses.softmax_cross_entropy(y_,y))

train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cross_entropy)

Train the model

sess = tf.InteractiveSession()
tf.initialize_all_variables().run()
for i in tqdm(range(num_epochs)):
for i in range(total_batch):
batch_x, batch_y =train_batches[i]
print("batch",batch_x.shape,batch_y.shape)
sess.run(train_step, feed_dict={x: batch_x, y_: batch_y})

Test trained model

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
test_acc = []
train_acc = []
for i in tqdm(range(total_batch)):
batch_x, batch_y = train_batches[i]
test_acc.append(sess.run(accuracy, feed_dict={x: batch_x, y_: batch_y}))
for i in tqdm(range(len(test_batches))):
batch_x, batch_y = test_batches[i]
train_acc.append(sess.run(accuracy, feed_dict={x: batch_x, y_: batch_y}))
print(np.mean(train_acc), np.mean(test_acc))

I searched on Google but I didn't find any solution. It's saying logits have size [64,48] and labels have size [32,48]

My batch size is 32 and number of classes is 48

Sorry! I found the error. The problem is at flattening final pool layer. By mistake I flattened the last before pool layer and passed it through fully connected layer.

Can u elaborate where u found the error. I have the same problem

@ramadevsaiteja will you elaborate more?? I also have the same error.

@ramadevsaiteja Im encountering the same issue. Can you please elaborate what is the error and how you fixed it? Thank you.

Estou com mesmo algu茅m saber soluciona lo ?

@MagnusFelinto @OneManArmy93
This happened to me as well, but in regards to the shape of the labels, instead of the batch. Basically, my final classification was defined with 4 outputs.
But then I changed my dataset, to have only 3 labels, and tried to run the script again.

I just had to change my last layer of the model, from:

tf.keras.layers.Dense(4, activation="softmax")

to:

tf.keras.layers.Dense(3, activation="softmax")

If you are using, Keras Sequence Generator, don't forget to take a look into it as well when defining the returned labels etc..

@MagnusFelinto @OneManArmy93
This happened to me as well, but in regards to the shape of the labels, instead of the batch. Basically, my final classification was defined with 4 outputs.
But then I changed my dataset, to have only 3 labels, and tried to run the script again.

I just had to change my last layer of the model, from:

tf.keras.layers.Dense(4, activation="softmax")

to:

tf.keras.layers.Dense(3, activation="softmax")

If you are using, Keras Sequence Generator, don't forget to take a look into it as well when defining the returned labels etc..

That does it!!

@MagnusFelinto @OneManArmy93
This happened to me as well, but in regards to the shape of the labels, instead of the batch. Basically, my final classification was defined with 4 outputs.
But then I changed my dataset, to have only 3 labels, and tried to run the script again.

I just had to change my last layer of the model, from:

tf.keras.layers.Dense(4, activation="softmax")

to:

tf.keras.layers.Dense(3, activation="softmax")

If you are using, Keras Sequence Generator, don't forget to take a look into it as well when defining the returned labels etc..

Thank you.

it got resolved when the number of classes in train and test were different while using image generator. It got fixed when i matched the train and test number of class

Why was the final classification defined with four outputs initially ?

Was this page helpful?
0 / 5 - 0 ratings