【问题标题】:How to develop a convolutional neural network to differentiate images with similar features?如何开发卷积神经网络来区分具有相似特征的图像?
【发布时间】:2018-11-15 03:12:19
【问题描述】:

我目前正在使用 tensorflow 后端在 keras 框架中开发卷积神经网络,用于区分通过或失败的指标。两者之间的区别(确定是通过还是失败)在于管内的颜色变化很小。然而,当我在图像上训练卷积神经网络时(每张大约 1500 张图片),无论图像如何,网络似乎总是预测通过。我的猜测是,这是由于两者的巨大相似之处,但我不确定为什么它无法将这种颜色变化检测为差异化特征。

我目前用于构建分类器的代码如下所示,以提供分类器可能在何处构建此类偏差的参考。

# Imports from Keras Library to build Network
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Activation
from keras.callbacks import ModelCheckpoint
from keras.layers import BatchNormalization
# Initialising the CNN as a sequential network
classifier = Sequential()

# Addition of convultional layer
classifier.add(Conv2D(32, kernel_size=(3, 3), input_shape = (356, 356, 3)))
# Adding a dropout to prevent overstabilization on certain nodes

# Adding a second/third/fourth convolutional/pooling/dropout layer
classifier.add(BatchNormalization())
classifier.add(Activation("relu"))
classifier.add(Conv2D(32, (3, 3)))
classifier.add(BatchNormalization())
classifier.add(Activation("relu"))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Dropout(0.25))

classifier.add(Conv2D(32, (3, 3)))
classifier.add(BatchNormalization())
classifier.add(Activation("relu"))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Dropout(0.25))
classifier.add(Conv2D(64, (3, 3)))
classifier.add(BatchNormalization())
classifier.add(Activation("relu"))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Dropout(0.25))

# Flattening Layer
classifier.add(Flatten())
# Full connection using dense layers
classifier.add(Dense(units = 128))
classifier.add(BatchNormalization())
classifier.add(Activation("relu"))  
classifier.add(Dense(units = 2, activation = 'softmax'))

# Compiling the CNN
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
classifier.summary()

# Fitting the CNN to the images

from keras.preprocessing.image import ImageDataGenerator

# Taining image generator (causes variation in how images may appear when trained upon)
train_datagen = ImageDataGenerator(rescale = 1./255,
                                   shear_range = 0.4,
                                   zoom_range = 0.4,
                                   horizontal_flip = True)

test_datagen = ImageDataGenerator(rescale = 1./255)

# Creation of training set
training_set = train_datagen.flow_from_directory('dataset/TrainingSet',
                                                 target_size = (356, 356),
                                                 batch_size = 32,
                                                 class_mode = 'categorical',
                                                 shuffle = True)

# Creation of test set
test_set = test_datagen.flow_from_directory('dataset/TestSet',
                                            target_size = (356, 356),
                                            batch_size = 32,
                                            class_mode = 'categorical',
                                            shuffle = True)

caller = ModelCheckpoint('/Users/anishkhanna/Documents/Work/BI Test/BI Models/part3.weights.{epoch:02d}-{val_loss:.2f}.hdf5', monitor='val_loss', verbose=0, save_best_only=False, save_weights_only=False, mode='auto', period=1)
# Training the model based on above set
# Can also be improved with more images
classifier.fit_generator(training_set,
                         steps_per_epoch = 200,
                         epochs = 200,
                         validation_data = test_set,
                         validation_steps = 15,
                         shuffle = True,
                         callbacks = [caller])

# Creates a HDF5 file to save the imformation of the model so it can be used later without retraining
classifier.save('BI_Test_Classifier_model.h5')

# Deletes the existing model
del classifier  

如果我可以对模型进行一些改进或对它提出建议,将不胜感激。

【问题讨论】:

    标签: python machine-learning keras computer-vision convolutional-neural-network


    【解决方案1】:

    如果您的显着特征主要是颜色,您可以进行预处理以帮助神经网络。在这种情况下,您可以将 RGB 转换为 Hue Saturation Value (HSV),然后仅使用 Hue 通道,该通道将包含有关像素颜色的信息并忽略阴影等。这是一个 post,您可以使用它如preprocessing_functionImageDataGenerator

    【讨论】:

    • 我已经实现了这个功能,但我不确定我应该如何只将色调通道或色调和价值通道输入模型。
    • 有很多posts 覆盖分渠道,研究一下。
    猜你喜欢
    • 2010-11-21
    • 2014-08-09
    • 2021-12-16
    • 2020-12-02
    • 2016-03-10
    • 2015-10-11
    • 2021-05-15
    • 2016-04-16
    • 2017-08-21
    相关资源
    最近更新 更多