问题描述
InvalidArgumentError:无法批处理组件0中具有不同形状的张量。第一个元素的形状为[224,224,3],而元素25的形状为[224,1]。
我已经调整了图像的形状,在这里可以看到。
def process_path(file_path=train_data):
image_file= tf.io.read_file(image_dir+file_path+'.jpg')
image_file=tf.image.decode_jpeg(image_file)
image_file=tf.image.convert_image_dtype(image_file,tf.float32)
image_file=tf.image.resize(image_file,[224,224])
return image_file
X_train = train_data.map(process_path)
然后我只合并标签和图像数据
train=tf.data.Dataset.zip((X_train,y_train))
train=train.shuffle(buffer_size=64).batch(32).prefetch(1)
base_res_model.fit(train,epochs=10,verbose=2)
解决方法
彩色图像具有3个通道,R,G,B。但是,灰度图像仅具有一个通道。列表的元素25是灰度图像,而之前的索引是颜色。一种解决方法是按如下方式将通道数传递到tf.image.decode_jpeg
中:
imagefile=tf.image.decode_jpeg(image_file,channels=3)