我认为这个答案会有所帮助。我想你可以理解,每个问题都没有通用的解决方案。你需要尝试不同的架构、不同的超参数组合和不同的图像处理技术来训练你的网络。
- 用于数据预处理和将图像加载到网络
您可以使用keras image processing,ImageGenerator 类进行图像增强并将图像加载到网络。
首先您创建具有所需配置的 ImageGenerator。
datagen = ImageDataGenerator(width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False,
rescale=1/255)
第二次通过 ImageGenerator 从目录加载图片
trainGene = datagen.flow_from_directory(train_path,
color_mode="grayscale", #use grayscale images
target_size=(image_height,image_width), #image size
shuffle=True,
class_mode="input",
batch_size=batch_size,
save_to_dir=None)
您可以创建验证数据生成器并从目录加载验证数据集。例如验证(valGene)
- 构建卷积自动编码器模型并适应生成器
这取决于用例,您需要尝试不同的层和损失函数以及不同的架构来达到所需的阈值。
例如,从简单的架构开始。
Layer (type) Output Shape Param #
=================================================================
input0 (InputLayer) (None, 64, 32, 1) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 64, 32, 32) 320
_________________________________________________________________
activation_1 (Activation) (None, 64, 32, 32) 0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 32, 16, 32) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 16384) 0
_________________________________________________________________
dense_1 (Dense) (None, 16) 262160
_________________________________________________________________
dense_2 (Dense) (None, 16384) 278528
_________________________________________________________________
reshape_1 (Reshape) (None, 32, 16, 32) 0
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 64, 32, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 64, 32, 32) 9248
_________________________________________________________________
activation_2 (Activation) (None, 64, 32, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 64, 32, 1) 289
_________________________________________________________________
activation_3 (Activation) (None, 64, 32, 1) 0
model.fit_generator(trainGene,
steps_per_epoch=trainGene.n/batch_size,
validation_data=valGene,
validation_steps=valGene.n/batch_size,
epochs=epochs, # number of epochs
verbose=True)
- 预测重构图像
为测试集(testGene)创建另一个生成器
restored = model.predict_generator(testGene, steps=testGene.n/batch_size)
-
找不同
现在您已经为给定的订单重新构建了图像。
difference = reconstructed_image - original_image
例如,
如果你想得到每张图片的均方误差
RMSE = np.sqrt(np.square(restored - x_test)/dim)
#x_test is your original images that used to predict
你可以像这样通过testGene获取x_test
x_test = np.zeros((0, image_height, image_width, image_channels), dtype=float)
for x, _ in testGene:
x_test = np.r_[x_test, x]
if testGene.total_batches_seen > testGene.n/batch_size:
break