我的输入是256x256 rgb图像,我的自动编码器输出希望是256x256rgb(但只有黑白色(,输入和输出如下所示输入图像输出估计这是我的代码
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')
这是型号摘要模型摘要
如何使(256256,16(回到(256256,3(?我读过其他类似的问题,但没有找到解决我的情况的方法
如果您希望输出为灰度图像,则需要更改模型中的最后一层,如下所示:
解码=Conv2D(1,(3,3(,激活="softmax",填充="me"((x(
如果你想让你的输出形状为(256256,3(,那么只需使用3作为过滤器的数量。
但不确定你想在这里解决什么。此处缺少上下文。。