Я хочу реализовать авто-кодировщик для набора данных лиц с использованием Keras.Я использовал train_on_batch
, потому что набор данных слишком велик, но я столкнулся с этой проблемой:
for i in range(10):
batch_index = 0
while batch_index <= train_data.batch_index:
data = train_data.next()
result = train_result.next()
model.train_on_batch(data[0],result[0])
batch_index = batch_index + 1
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-54-d7d64e954a89> in <module>
4 data = train_data.next()
5 result = train_result.next()
----> 6 model.train_on_batch(data[0],result[0])
7 batch_index = batch_index + 1
~/.local/lib/python3.5/site-packages/keras/engine/training.py in train_on_batch(self, x, y, sample_weight, class_weight)
1209 x, y,
1210 sample_weight=sample_weight,
-> 1211 class_weight=class_weight)
1212 if self._uses_dynamic_learning_phase():
1213 ins = x + y + sample_weights + [1.]
~/.local/lib/python3.5/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
787 feed_output_shapes,
788 check_batch_axis=False, # Don't enforce the batch size.
--> 789 exception_prefix='target')
790
791 # Generate sample-wise weight values given the `sample_weight` and
~/.local/lib/python3.5/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
136 ': expected ' + names[i] + ' to have shape ' +
137 str(shape) + ' but got array with shape ' +
--> 138 str(data_shape))
139 return data
140
ValueError: Error when checking target: expected conv2d_transpose_21 to have shape (250, 250, 1) but got array with shape (250, 250, 3)
Моя модель такая же:
Input_Layer = keras.Input((250,250,3))
x = keras.layers.Conv2D(20,5,activation='relu')(Input_Layer)
x = keras.layers.MaxPooling2D(2)(x)
x = keras.layers.Conv2D(20,2,activation = 'relu')(x)
x = keras.layers.MaxPooling2D(2)(x)
encoded = x
x = keras.layers.UpSampling2D(2)(x)
x = keras.layers.Conv2DTranspose(20,2,activation='relu')(x)
x = keras.layers.UpSampling2D(2)(x)
x = keras.layers.Conv2DTranspose(20,5,activation= 'relu')(x)
model = keras.Model(input = Input_Layer ,output = x)
Я загружаю изображения, используя керасы ImageDataGenerator
какой груз:
train_data = trainGenerator.flow_from_directory('lfw',batch_size=67,target_size=(250, 250))
Found 13199 images belonging to 1 classes.
Вот весь код
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
import matplotlib.pyplot as plt
import numpy as np
import keras
def cutHalf(img):
for j in range(125):
for i in range(250):
img[i][j][0]=1
img[i][j][1]=1
img[i][j][2]=1
return img
img_width = 250
img_height = 250
train_datagen = ImageDataGenerator(rescale=1./255)
train_datagen2 = ImageDataGenerator(rescale=1./255,preprocessing_function=cutHalf)
train_generator = train_datagen.flow_from_directory(
'lfw',target_size=(img_width, img_height),
class_mode=None)
train_generator2 = train_datagen2.flow_from_directory(
'lfw',target_size=(img_width, img_height),
class_mode=None)
def fixed_generator(generator,generator2):
batch_index = 0
while batch_index <= generator.batch_index:
yield (generator.next(), generator2.next())
Input_Layer = keras.Input(shape=(img_width, img_height,3))
x = keras.layers.Conv2D(20,5,activation='relu')(Input_Layer)
x = keras.layers.MaxPooling2D(2)(x)
x = keras.layers.Conv2D(20,2,activation = 'relu')(x)
x = keras.layers.MaxPooling2D(2)(x)
encoded = x
x = keras.layers.UpSampling2D(2)(x)
x = keras.layers.Conv2DTranspose(20,2,activation='relu')(x)
x = keras.layers.UpSampling2D(2)(x)
x = keras.layers.Conv2DTranspose(20,5,activation= 'relu')(x)
model = keras.Model(input = Input_Layer ,output = x)
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit_generator(
fixed_generator(train_generator,train_generator2),
nb_epoch=20,
steps_per_epoch=50
)