我正在使用带有Theano后端的Keras自动编码。并希望为720x1080 RGB图像制作自动编码。 这是我的代码
from keras.datasets import mnist
import numpy as np
from keras.layers import Input, LSTM, RepeatVector, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from PIL import Image
x_train = []
x_train_noisy = []
for i in range(5,1000):
image = Image.open('data/trailerframes/frame' + str(i) + '.jpg', 'r')
x_train.append(np.array(image))
image = Image.open('data/trailerframes_avg/frame' + str(i) + '.jpg', 'r')
x_train_noisy.append(np.array(image))
x_train = np.array(x_train)
x_train = x_train.astype('float32') / 255.
x_train_noisy = np.array(x_train_noisy)
x_train_noisy = x_train_noisy.astype('float32') / 255.
input_img = Input(shape=(720, 1080, 3))
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), data_format="channels_last", activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), data_format="channels_last", activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), data_format="channels_last", activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(x_train_noisy, x_train,
epochs=10,
batch_size=128,
shuffle=True,
validation_data=(x_train_noisy, x_train))
但它给了我一个错误
ValueError:检查输入时出错:预期input_7有形状(无,720,1080,3)但是有形状的数组(995,720,1280,3)
答案 0 :(得分:1)
输入错误:
简单如下:
其中一个是错的,我认为这是输入中的拼写错误:
#change 1080 for 1280
input_img = Input(shape=(720, 1280, 3))
输出错误(目标):
现在,您的目标数据形状像(720,1280,3),最后一层输出(720,1280,1)
一个简单的解决方法是:
decoded = Conv2D(3, (3, 3), data_format="channels_last", activation='sigmoid', padding='same')(x)
使用编码器:
训练该模型后,您可以创建仅使用编码器或解码器的子模型:
encoderModel = Model(input_img, decoded)
decoderInput = Input((shape of the encoder output))
decoderModel = Model(decoderInput,decoded))
这两个模型将分享整个模型的完全相同的权重,训练一个模型将影响所有三个模型。
如果在未经过培训的情况下使用它们,您可以使用model.predict(data)
,无需培训即可获得结果。