如何解决,tensorflow.python.framework.errors_impl.InvalidArgumentError?

How to solve, tensorflow.python.framework.errors_impl.InvalidArgumentError?

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)

当 运行 以上代码 semantic segmentation 我得到异常发生:

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

您的问题来自最后一层的大小(为避免这些错误,始终希望对 N_IMAGESWIDTHHEIGHT 使用 python 常量, N_CHANNELSN_CLASSES):

图片class化

您应该为每张图片指定一个标签。尝试切换 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)) 
#...

用于语义分割

确保您的 classifier(网络的最后一层)大小相应。在这种情况下,每个像素需要 1 class:

#...
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]))
#...

这是您能得到的最简单的方法。相反,您可以设置多个反卷积层作为 class 化器,或者您甚至可以翻转 arch 架构并使用它来生成 class 化结果。正交地,您可以对标签执行 one_hot 编码,从而将它们扩展 N_CLASSES 倍,有效地乘以最后一层中的神经元数量。