gpt4 book ai didi

python - 如何使用数据生成器训练自动编码器?

转载 作者:行者123 更新时间:2023-12-05 05:42:38 26 4
gpt4 key购买 nike

我正在尝试训练一个自动编码器,我想使用 Keras 的数据生成器 API 来提供数据。下面给出了代码片段。我已经尝试了 fit() 和 fit_generator(),但都没有用。

seed=24
batch_size= 8
image_height,image_width =32,32

img_data_gen_args = dict(rescale = 1/255.)
image_data_generator = ImageDataGenerator(**img_data_gen_args)

image_generator = image_data_generator.flow_from_directory(path,
seed=seed,
batch_size=batch_size,
class_mode='input')
encoder_input = keras.Input(shape=(image_height,image_width,1))
# encoder
x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoder_input)
x = MaxPooling2D((2, 2), padding='same')(x)
encoded = Conv2D(1, (3, 3), activation='relu', padding='same')(x)
encoder = Model(encoder_input, encoded)

# decoder
decoder_input= Input(shape=(8, 8, 1))
decoder = Conv2D(32, (3, 3), activation='relu', padding='same')(decoder_input)
x = UpSampling2D((2, 2))(decoder)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
decoder = Model(decoder_input, decoded)

# auto encoder
auto_input = Input(shape=(image_height,image_width, 1))
encoded = encoder(auto_input)
decoded = decoder(encoded)
autoencoder = Model(auto_input, decoded)
autoencoder.compile(optimizer="adam", loss="binary_crossentropy", metrics=['accuracy'])

num_train_imgs = len(os.listdir(path))
steps_per_epoch = num_train_imgs //batch_size

history = autoencoder.fit_generator(generator=image_generator,
validation_data=image_generator,
epochs=20)

# history = model.fit_generator(image_generator, validation_data=image_generator,
# steps_per_epoch=steps_per_epoch,
# validation_steps=steps_per_epoch, epochs=50)

#history = autoencoder.fit(
# image_generator,
# epochs=10,
# validation_data=image_generator,
#)

我收到以下错误

---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call last)
Input In [62], in <cell line: 1>()
----> 1 history = autoencoder.fit_generator(generator=image_generator,
2 validation_data=image_generator,
3 epochs=20)

最佳答案

解码器的输入形状必须是 (16, 16, 1) 才能与编码器的输出配合使用。此外,尝试在 flow_from_directory 中设置 color_mode='grayscale'。这是一个使用虚拟数据的工作示例:

# Create dummy data
import numpy
from PIL import Image

imarray = numpy.random.rand(32, 32, 3) * 255
im = Image.fromarray(imarray.astype('uint8')).convert('L')

im.save('/content/images/class1/image.png')
im.save('/content/images/class1/image1.png')
im.save('/content/images/class1/image2.png')

im.save('/content/images/class2/image3.png')
im.save('/content/images/class2/image4.png')
im.save('/content/images/class2/image5.png')

型号:

import tensorflow as tf

seed=24
batch_size= 8
image_height,image_width =32,32

img_data_gen_args = dict(rescale = 1/255.)


image_data_generator = tf.keras.preprocessing.image.ImageDataGenerator(**img_data_gen_args)
image_generator = image_data_generator.flow_from_directory('/content/images',
seed=seed, target_size=(image_height, image_width),
batch_size=batch_size,
class_mode='input', color_mode='grayscale')

encoder_input = tf.keras.layers.Input(shape=(image_height,image_width,1))
# encoder
x = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(encoder_input)
x = tf.keras.layers.MaxPooling2D((2, 2), padding='same')(x)
encoded = tf.keras.layers.Conv2D(1, (3, 3), activation='relu', padding='same')(x)
encoder = tf.keras.Model(encoder_input, encoded)

# decoder
decoder_input= tf.keras.layers.Input(shape=(16, 16, 1))
decoder = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', padding='same')(decoder_input)
x = tf.keras.layers.UpSampling2D((2, 2))(decoder)
decoded = tf.keras.layers.Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
decoder = tf.keras.Model(decoder_input, decoded)

# auto encoder
auto_input = tf.keras.layers.Input(shape=(image_height,image_width, 1))
encoded = encoder(auto_input)
decoded = decoder(encoded)
autoencoder = tf.keras.Model(auto_input, decoded)
autoencoder.compile(optimizer="adam", loss="binary_crossentropy", metrics=['accuracy'])

history = autoencoder.fit(image_generator, epochs=20)
Found 6 images belonging to 2 classes.
Epoch 1/20
1/1 [==============================] - 1s 544ms/step - loss: 0.6932 - accuracy: 0.0000e+00
Epoch 2/20
1/1 [==============================] - 0s 19ms/step - loss: 0.6931 - accuracy: 0.0000e+00
Epoch 3/20
...
...

关于python - 如何使用数据生成器训练自动编码器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71994928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com