代码之家  ›  专栏  ›  技术社区  ›  Adam

这些图片是否太“嘈杂”以至于CNN无法正确分类?

  •  0
  • Adam  · 技术社区  · 6 年前

    我试图建立一个图像分类器,以确定两种类型的图像之间的财产网站。我已经将数据集分为两类:[属性,房间]。我希望能够区分图像是某个酒店的外部还是酒店内部的房间。

    我似乎总是希望在训练中得到合理的结果,但是当我对一些真实的样本进行测试时,它通常会将所有的图像分类为一个类别。

    下面您可以看到我使用的模型:

    train_datagen = ImageDataGenerator(
        rescale=1./255,
        width_shift_range=0.1,
        height_shift_range=0.1,
        rotation_range=10,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='nearest'
    ) # set validation split
    
    validate_datagen = ImageDataGenerator(rescale=1./255)
    
    IMG_HEIGHT = IMG_WIDTH = 128
    model = tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(32, (11,11), activation='relu', input_shape=(IMG_HEIGHT, IMG_WIDTH, 3), padding='same'),
        tf.keras.layers.MaxPooling2D(11, 11),
        # tf.keras.layers.Dropout(0.5),
        # Second convolutional layer
        tf.keras.layers.Conv2D(64, (11, 11), padding='same', activation='relu'),
        tf.keras.layers.MaxPooling2D(11, 11),
        # tf.keras.layers.Dropout(0.5),
        # Flattening
        tf.keras.layers.Flatten(),
        # Full connection
        tf.keras.layers.Dense(128, activation='relu'),
        tf.keras.layers.Dropout(0.5),
        tf.keras.layers.Dense(1, activation='sigmoid')
    ])
    
    from tensorflow.keras.optimizers import RMSprop
    
    model.compile(
        optimizer=RMSprop(lr=0.001),
        loss='binary_crossentropy',
        metrics=['accuracy']
    )
    
    # now train the model
    history = model.fit_generator(
        train_generator,
        validation_data=validation_generator,
        steps_per_epoch=75, #100
        epochs=5, # 15, or 20, and 100 steps per epoch
        validation_steps=50,
        verbose=1
    )
    
    # Predict image
    def load_image(img_path, show=False):
      test_image = image.load_img(img_path, target_size=(IMG_HEIGHT, IMG_WIDTH))
      test_image = image.img_to_array(test_image)
      test_image /= 255.
      test_image = np.expand_dims(test_image, axis = 0)
      return test_image
    
    def predict_image(img_path, show=False):
      loaded_img = load_image(img_path, show)
      pred = model.predict(loaded_img)
      return 'property' if pred[0][0] == 0.0 else 'room'
    
    print('Prediction is...')
    print(predict_image('path/to/my/img')
    

    有人能提出可能的原因吗?我尝试过使用不同的epoch和批处理大小,进一步增强图像,更改Conv2D和池层大小,但似乎没有任何帮助。

    我可能没有足够的数据,或者他们是坏的图像开始?这是我第一次进入ML,所以如果有任何问题看起来很明显,我深表歉意。

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    0 回复  |  直到 6 年前
        1
  •  2
  •   Dr. Snoopy    6 年前

    您没有正确地对分类器的输出进行后处理,它在[0,1]中输出一个概率,其中 values < 0.5 与第一类相对应,以及 values >= 0.5 第二节课。你应该相应地修改代码。

        2
  •  -1
  •   Krishna Kankipati    6 年前

    尝试数据增强:它将图像增强为一些随机变换,如随机旋转、随机缩放、随机水平翻转、宽度偏移和高度偏移。并尝试实施批量标准化。