[英]Error when checking target: expected to have shape (256, 256, 1) but got array with shape (256, 256, 3)
[英]ValueError: Error when checking target: expected conv2d_120 to have shape (256, 256, 16) but got array with shape (256, 256, 3)
我的输入是 256x256 rgb 图像,我的自动编码器 output 想要是 256x256 rgb(但只有黑白颜色),输入和输出如下例如输入图像outputimage这是我的代码
train_data = np.empty((train_N,256,256,3))
train_labels = np.empty((trainL_N,256,256,3))
test_data = np.empty((test_N,256,256,3))
test_labels = np.empty((testL_N,256,256,3))
def loadIMG(imagePath , number, Array):
while number >0:
img = cv2.imread(imagePath[number-1])
img = cv2.resize(img,(256,256),interpolation=cv2.INTER_AREA)
img_ndarray=np.asarray(img,dtype='float64')
Array[number-1] = img_ndarray
number = number - 1
loadIMG(imagePath1,train_N,train_data)
loadIMG(imagePath2,trainL_N,train_labels)
loadIMG(imagePath3,test_N,test_data)
loadIMG(imagePath4,testL_N,test_labels)
def train_model():
global history
input_img= Input(shape=(256, 256, 3))
#大小 = 256*256
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
#大小 = 127*127
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
#大小 = 62*62
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
#大小 = 30*30
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
#大小 = 14*14
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
#大小 = 6*6
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
#大小 = 2*2
#這邊直接再次maxpooling 來達到1*1
encoded = MaxPooling2D((2, 2), padding='same', name='encoder')(x) #大小 = 1*1
x = UpSampling2D((2, 2))(encoded)
#大小2*2
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
#大小6*6
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
#大小14*14
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
#大小30*30
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
#大小62*62
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
#大小127*127
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(16, (3, 3), activation='softmax', padding='same')(x)
autoencoder = Model(input_img, decoded)
print(autoencoder.summary())
autoencoder.compile(optimizer='adam', loss='categorical_crossentropy',metrics=[tf.keras.metrics.CategoricalAccuracy()])
history = autoencoder.fit(train_data, train_labels,
epochs=20,
batch_size=24,
shuffle=True,
validation_data=(test_data, test_labels),
callbacks=[TensorBoard(log_dir='/tmp/autoencoder', histogram_freq=0, write_graph=False)])
autoencoder.save('autoencoder.h5')
这是 model 摘要model 摘要
如何使 (256,256,16) 变回 (256,256,3)? 我读过其他类似的问题,但没有找到关于我的情况的解决方案
如果您希望 output 成为灰度图像,您需要像这样更改 model 中的最后一层:
解码 = Conv2D(1, (3, 3), activation='softmax', padding='same')(x)
如果你想让你的 output 形状为 (256,256,3) 那么只需使用 3 作为过滤器的数量。
虽然不确定您要在这里解决什么问题。 缺少这里的上下文..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.