如何开发卷积神经网络来区分具有相似特征的图像?

时间:2018-06-05 15:07:07

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

我目前正在使用张量流后端在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  

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

1 个答案:

答案 0 :(得分:0)

如果您的区别特征主要是颜色,您可以预处理以帮助神经网络。在这种情况下,您可以将RGB转换为Hue Saturation Value(HSV),并使用例如Hue通道,该通道将包含有关像素颜色的信息并忽略阴影等。这里有一个post和您可以将preprocessing_function用作ImageDataGenerator