我决定尝试训练自动编码器重新着色灰度图像。这种方法可能有点幼稚,但我想玩它,看看它有多好(或坏),并检查我如何改进它。
然而,它意外地抛出了以下错误:
File "colorise0.py", line 63, in <module>
validation_data=(val_g_generator, val_c_generator)
File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 2183, in fit_generator
val_x, val_y, val_sample_weight)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1483, in _standardize_user_data
exception_prefix='input')
File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 76, in _standardize_input_data
data = [np.expand_dims(x, 1) if x is not None and x.ndim == 1 else x for x in data]
AttributeError: 'DirectoryIterator' object has no attribute 'ndim'
我的代码是:
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.layers import Conv2DTranspose as DeConv2D
from keras.models import Model
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
img_width, img_height = 150, 150
batch_size=32
train_data_dir = './train/'
validation_data_dir = './validation/'
input_shape = (img_width, img_height,3)
train_datagen = ImageDataGenerator(rescale = 1./255)
test_datagen = ImageDataGenerator(rescale = 1./255)
train_c_generator= train_datagen.flow_from_directory(
train_data_dir+'colored',
target_size=(img_width, img_height),
batch_size=batch_size
)
train_g_generator= train_datagen.flow_from_directory(
train_data_dir+'grey',
target_size=(img_width, img_height),
batch_size=batch_size
)
val_c_generator= test_datagen.flow_from_directory(
validation_data_dir+'colored',
target_size=(img_width, img_height),
batch_size=batch_size
)
val_g_generator= test_datagen.flow_from_directory(
validation_data_dir+'grey',
target_size=(img_width, img_height),
batch_size=batch_size
)
input_img=Input(shape=(img_width,img_height,3))
x=Conv2D(32,(3,3), activation='relu', padding='same')(input_img)
x=Conv2D(32,(3,3), activation='relu', padding='same')(x)
x=Conv2D(32,(3,3), activation='relu', padding='same')(x)
x=Conv2D(32,(3,3), activation='relu', padding='same')(x)
y=DeConv2D(32,(3,3), activation='relu',padding='same')(x)
y=DeConv2D(32,(3,3), activation='relu',padding='same')(y)
y=DeConv2D(32,(3,3), activation='relu',padding='same')(y)
decoded=DeConv2D(3,(3,3), padding='same')(y)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit_generator(
train_g_generator, train_c_generator,
epochs=50,
validation_data=(val_g_generator, val_c_generator)
)
鉴于错误消息,我认为错误可能源于调用两个生成器(一个提供灰色输入图像,第二个提供原始彩色图像,作为目标)。
我该如何解决这个问题?
非常感谢!
答案 0 :(得分:0)
fit_generator只需要一个生成器,而不是两个。生成器输出一个元组(X,y),因此输入和目标都由一个生成器处理。
对于您的特定用例,我认为您需要制作自定义生成器。