自动编码器不匹配尺寸误差

时间:2017-06-17 19:54:47

标签: keras autoencoder

我正在尝试使用Conv Autoencoder重建图像,但是我得到了与尺寸相关的错误,你能找到解决方案吗,谢谢,

基本上我想在重建相同的输入数据时测试模型,然后如果模型工作正常,我需要将图像建模到地图上,

在这种情况下,我可以将数据从image_data更改为map_data,如下面的代码所示:

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K
from keras.callbacks import TensorBoard
import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.utils import np_utils
import matplotlib.pyplot as plt
import matplotlib
import os
from PIL import Image
from numpy import *
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
from keras.preprocessing.image import ImageDataGenerator,array_to_img, img_to_array, load_img

image_data='C:/Users/user_PC/Desktop/Image2Map/Samples'
map_data='C:/Users/user_PC/Desktop/Image2Map/Samples'

K.set_image_dim_ordering('tf')

input_img = Input(batch_shape=(1024, 106, 106,3))


x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)


x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')



train_datagen = ImageDataGenerator(
        rescale=1. / 255)

test_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    image_data,
    target_size=(106, 106),
    batch_size=4,
    class_mode=None)

validation_generator = test_datagen.flow_from_directory(
    image_data,
    target_size=(106, 106),
    batch_size=4,
    class_mode=None)

imgs = np.concatenate([train_generator.next()[0] for i in range(1024)])

autoencoder.fit_generator(generator=(imgs,imgs),
    samples_per_epoch=1024 // 4,
    epochs=10,
    validation_data=(imgs,imgs),
    validation_steps=1024 // 4)



decoded_imgs = autoencoder.predict(image_data)

n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
    # display original
    ax = plt.subplot(2, n, i)
    plt.imshow(image_data[i].reshape(106, 106))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display reconstruction
    ax = plt.subplot(2, n, i + n)
    plt.imshow(decoded_imgs[i].reshape(106, 106))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
plt.show()

1 个答案:

答案 0 :(得分:0)

将class_mode从'None'更改为'input'。它应该工作。阅读更多here