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

Python 如何开发卷积神经网络来区分具有相似特征的图像?,python,machine-learning,keras,computer-vision,convolutional-neural-network,Python,Machine Learning,Keras,Computer Vision,Convolutional Neural Network,我目前正在keras框架中开发一个卷积神经网络,使用tensorflow后端,用于区分通过或失败的指标。两者之间的差异(确定是通过还是失败)在于管内的微小颜色变化。然而,当我在图像上训练卷积神经网络(每个图像大约1500张)时,无论图像如何,网络似乎总是预测通过。我的猜测是,这是由于两者有着巨大的相似性,但我不确定为什么它不能检测出这种颜色变化作为一种区分特征 下面是我当前用于构建分类器的代码,以提供分类器可能在何处构建这种偏差的参考 # Imports from Keras Library t

我目前正在keras框架中开发一个卷积神经网络,使用tensorflow后端,用于区分通过或失败的指标。两者之间的差异(确定是通过还是失败)在于管内的微小颜色变化。然而,当我在图像上训练卷积神经网络(每个图像大约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  

如果对模型有一些改进或建议,我将不胜感激。

如果您的主要特征是颜色,您可以对神经网络进行预处理。在这种情况下,您可以将RGB转换为(HSV)例如,只需使用色调通道,它将包含有关像素颜色的信息,并忽略着色等。这里有一个示例,您可以将其用作
预处理功能。

我已经实现了该功能,但我不确定如何仅将色调通道或色调和值通道输入到模型。有许多模型涵盖了分裂渠道、研究。