【问题标题】:How to build an autoencoder in tensorflow using own dataset images?如何使用自己的数据集图像在 tensorflow 中构建自动编码器?
【发布时间】:2020-03-10 19:35:04
【问题描述】:

我是 Tensorflow 的初学者,我想为图像创建一个简单的 auencoder,我尝试了一些我在网上找到的示例,但所有这些都在 Mnist 数据集上工作,这使得处理这些图像变得容易,但我想为我自己的数据集图像创建一个自动编码器。 我的问题是:如何使用我自己的数据集图像在 tensorflow 中创建一个简单的自动编码器(因为我需要一些步骤来加载我的图像和预处理..)?(我需要使用自己的数据集的自动编码器模型的完整示例)

【问题讨论】:

    标签: python image tensorflow autoencoder


    【解决方案1】:

    我认为这个答案会有所帮助。我想你可以理解,每个问题都没有通用的解决方案。你需要尝试不同的架构、不同的超参数组合和不同的图像处理技术来训练你的网络。

    1. 用于数据预处理和将图像加载到网络 您可以使用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)

    1. 构建卷积自动编码器模型并适应生成器

    这取决于用例,您需要尝试不同的层和损失函数以及不同的架构来达到所需的阈值。 例如,从简单的架构开始。

    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)
    
    1. 预测重构图像 为测试集(testGene)创建另一个生成器
      restored = model.predict_generator(testGene, steps=testGene.n/batch_size)
    
    1. 找不同

      现在您已经为给定的订单重新构建了图像。

      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
      

    【讨论】:

    • 感谢您的帮助。但是我怎样才能得到输入图像和重构图像之间的差异呢?
    猜你喜欢
    • 2022-01-25
    • 2018-05-03
    • 1970-01-01
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 2017-08-27
    相关资源
    最近更新 更多