首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单自动编码器设计中的尺寸值误差

简单自动编码器设计中的尺寸值误差
EN

Stack Overflow用户
提问于 2018-10-13 06:13:33
回答 1查看 1.1K关注 0票数 0

嗨,我正在使用Keras库在Python3.5中试用一个简单的自动编码器。我面临的问题是- ValueError:在检查输入时出错:期望input_40具有二维,但得到了形状为(32、256、256、3)的数组。我的数据集非常小(60幅RGB图像的维数为- 256*256,需要验证的图像类型相同)。我对Python有点陌生。请帮帮忙。

代码语言:javascript
复制
import matplotlib.pyplot as plt
from keras.layers import Input, Dense
from keras.models import Model

#Declaring the model
encoding_dim = 32
input_img = Input(shape=(65536,))
encoded = Dense(encoding_dim, activation='relu')(input_img)
decoded = Dense(65536, activation='sigmoid')(encoded)
autoencoder = Model(input_img, decoded)
encoder = Model(input_img, encoded)
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
decoder = Model(encoded_input, decoder_layer(encoded_input))
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')


#Constructing a data generator iterator
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
test_datagen = ImageDataGenerator(rescale = 1./255)
training_set=
train_datagen.flow_from_directory('C:\\Users\\vlsi\\Desktop\\train',
batch_size = 32,
class_mode = 'binary')
test_set =     
test_datagen.flow_from_directory('C:\\Users\\vlsi\\Desktop\\validation',
batch_size = 32,
class_mode = 'binary')


#fitting data
autoencoder.fit_generator(training_set,
steps_per_epoch = 80,
epochs = 25,
validation_data = test_set,
validation_steps = 20)

import numpy as np from keras.preprocessing import image
test_image =            
image.load_img('C:\\Users\\vlsi\\Desktop\\validation\\validate\\apple1.jpg')
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)

#Displaying output
encoded_imgs = encoder.predict(test_image)
decoded_imgs = decoder.predict(encoded_imgs)
plt.imshow(decoded_imgs)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-13 09:19:24

问题在于:

代码语言:javascript
复制
input_img = Input(shape=(65536,))

您告诉Keras网络的输入将有65K维,这意味着形状向量(样本,65536),但是实际的输入有形状(样本,256,256,3)。任何简单的解决方案都是使用真正的输入形状,并对网络执行必要的整形:

代码语言:javascript
复制
input_img = Input(shape=((256, 256, 3))
flattened = Flatten()(input_img)
encoded = Dense(encoding_dim, activation='relu')(flattened)
decoded = Dense(256 * 256 * 3, activation='sigmoid')(encoded)
decoded = Reshape((256, 256, 3))(decoded)
autoencoder = Model(input_img, decoded)
encoder = Model(input_img, encoded)
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
decoder = Model(encoded_input, decoder_layer(encoded_input))

请注意,我首先添加了一个扁平层和一个重塑层,以使图像变平,然后将平坦的图像返回到形状(256、256、3)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52790068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档