2
import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split

np.random.seed(4213)

data = np.random.randint(low=1,high=29, size=(500, 160, 160, 10)) 
labels = np.random.randint(low=0,high=5, size=(500, 160, 160)) 
nclass = len(np.unique(labels))
print (nclass)

samples, width, height, nbands = data.shape


X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size=0.25, random_state=421)

print (X_train.shape)
print (y_train.shape)

arch = tf.keras.applications.VGG16(input_shape=[width, height, nbands],
                      include_top=False,
                      weights=None)

model = tf.keras.Sequential()
model.add(arch)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(nclass))

model.compile(optimizer = tf.keras.optimizers.Adam(0.0001),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(),
              metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
    

model.fit(X_train,
          y_train,                                 
          epochs=3,
          batch_size=32,
          verbose=2)


res = model.predict(X_test)
print(res.shape)

When running the above code for semantic segmentation I get Exception has occurred:

InvalidArgumentError
 Incompatible shapes: [32,160,160] vs. [32]
     [[node Equal (defined at c...:38) ]] [Op:__inference_train_function_1815]


tensorflow.python.framework.errors_impl.InvalidArgumentError
4
  • what line is generating the error? Commented Jul 28, 2020 at 10:51
  • model.fit , verbose=2 is generating the error Commented Jul 28, 2020 at 11:18
  • 2
    You should post the complete error message when dealing with errors in Stackoverflow Commented Jul 28, 2020 at 14:35
  • As mentioned in the posted answer, your data is for image segmentation, but your model has the structure for image classification. You should pick a model for that purpose, not VGG16. There are many papers out there discussing neural image segmentation (e.g. see here). Commented Jul 31, 2020 at 12:03

1 Answer 1

1
+50

Your issue comes from the size of the last layer (to avoid these mistakes it is always desirable to use python constants for N_IMAGES, WIDTH, HEIGHT, N_CHANNELS and N_CLASSES):

For image classification

You should assign one single label to each image. Try switching labels:

import tensorflow as tf
import numpy as np
from sklearn.model_selection import train_test_split

np.random.seed(4213)

N_IMAGES, WIDTH, HEIGHT, N_CHANNELS = (500, 160, 160, 10)
N_CLASSES  = 5

data = np.random.randint(low=1,high=29, size=(N_IMAGES, WIDTH, HEIGHT, N_CHANNELS)) 
labels = np.random.randint(low=0,high=N_CLASSES, size=(N_IMAGES)) 
#...

For semantic segmentation

Make sure your classifier (last layers of the network) is sized accordingly. In this case you need 1 class per pixel:

#...
model = tf.keras.Sequential()
model.add(arch)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(width * height))
model.add(tf.keras.layers.Reshape([width , height]))
#...

This is the simplest you can get. Instead, you can set up multiple deconvolution layers to act as classifier, or you can even flip the arch architecture over and use it to generate the classification results. Orthogohally, you can perform one_hot encoding on the labels and thus expand them by a factor of N_CLASSES, effectively multiplying the number of neurons in the last layer.

Sign up to request clarification or add additional context in comments.

1 Comment

That's an entirely different question! You should either close this question and ask a new one, or modify it accordingly. Do you have any references for semantic segmentation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.