Keras: Error when checking input

元气小坏坏 提交于 2021-01-28 06:36:51

问题


I am using Keras autoencodes with Theano backend. And want to make autoencode for 720x1080 RGB images. This is my code

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))

But it gives me an error

ValueError: Error when checking input: expected input_7 to have shape (None, 720, 1080, 3) but got array with shape (995, 720, 1280, 3)


回答1:


Input error:

As simple as:

  • You defined your input as (720,1080,3)
  • You're trying to trian your model with data in the form (720,1280,3)

One of them is wrong, and I think it's a typo in the input:

#change 1080 for 1280
input_img = Input(shape=(720, 1280, 3))

Output error (target):

Now, your target data is shaped like (720,1280,3), and your last layer outputs (720,1280,1)

A simple fix is:

decoded = Conv2D(3, (3, 3), data_format="channels_last", activation='sigmoid', padding='same')(x)

Using the encoder:

After training that model, you can create submodels for using only the encoder or the decoder:

encoderModel = Model(input_img, decoded)    

decoderInput = Input((shape of the encoder output))    
decoderModel = Model(decoderInput,decoded))

These two models will share the exact same weights of the entire model, training one model will affect all three models.

For using them without training, you can use model.predict(data), which will give you the results without training.



来源:https://stackoverflow.com/questions/43944981/keras-error-when-checking-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!