Python TFLearn CNN预测总是错误的类

时间:2018-10-15 21:11:36

标签: python tensorflow classification conv-neural-network tflearn

基于示例herehere,我正在尝试使用TFLearn构建皮肤分类器。数据集为here

CNN火车脚本:

# Load the data set
X, Y = image_preloader("train_data", image_shape=(15, 15), mode='folder', categorical_labels=True, normalize=True)

# Shuffle the data
X, Y = shuffle(X, Y)

# Make sure the data is normalized
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()

# Create extra synthetic training data by flipping, rotating and blurring the
# images on our data set.
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
img_aug.add_random_blur(sigma_max=3.)

# Define our network architecture:

# Input is a 15x15 image with 3 color channels (red, green and blue)
network = input_data(shape=[None, 15, 15, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)

# Step 1: Convolution
network = conv_2d(network, 32, 3, activation='relu')

# Step 2: Max pooling
network = max_pool_2d(network, 2)

# Step 3: Convolution again
network = conv_2d(network, 64, 3, activation='relu')

# Step 4: Convolution yet again
network = conv_2d(network, 64, 3, activation='relu')

# Step 5: Max pooling again
network = max_pool_2d(network, 2)

# Step 6: Fully-connected 512 node neural network
network = fully_connected(network, 512, activation='relu')

# Step 7: Dropout - throw away some data randomly during training to prevent over-fitting
network = dropout(network, 0.4)

# Step 8: Fully-connected neural network with two outputs (0 = isn't skin, 1 = is skin) to make the final prediction
network = fully_connected(network, 2, activation='softmax')

# Tell tflearn how we want to train the network
network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

# Wrap the network in a model object
model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='logs/skin-classifier.tfl.ckpt')

# Train it! We'll do 100 training passes and monitor it as it goes.
model.fit(X, Y, n_epoch=100, shuffle=True, validation_set=0.33, 
    show_metric=True, batch_size=96, snapshot_epoch=True, run_id='skin-classifier')

# Save model when training is complete to a file
model.save("skin-classifier.tfl")

print("Network trained and saved as skin-classifier.tfl!")

我将数据集分为两个文件夹“ train_data”和“ test_data”,每个文件夹带有两个子文件夹“ NOT_SKIN_15”和“ SKIN_15”(按此顺序,我想这很重要)。培训进行得比较顺利:

  

培训步骤:4200 |总损失:0.14197 |时间:4.776s   |亚当|时代:100 |损失:0.14197-acc:0.9507 | val_loss:0.06002-val_acc 0.9802-迭代:3994/3994

但是在那之后,当我尝试计算一些指标以评估模型时,对于处理过的测试数据,按照以下confusion matrix进行操作,我获得了〜0.01的准确性:

36     1828
1117      1

似乎在某些时候类被“交换”了。如果模型“认为”是皮肤,则模型输出0,而不是皮肤则输出1。但是在训练阶段(我猜),非皮肤为0,皮肤为1。为什么会这样?

评估脚本:

# Load the data set
X, Y = image_preloader("test_data", image_shape=(15, 15), mode='folder', categorical_labels=True, normalize=True)

# Shuffle the data
X, Y = shuffle(X, Y)

# Same network definition as before
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
img_aug.add_random_blur(sigma_max=3.)

network = input_data(shape=[None, 15, 15, 3],
                     data_preprocessing=img_prep,
                     data_augmentation=img_aug)
network = conv_2d(network, 32, 3, activation='relu')
network = max_pool_2d(network, 2)
network = conv_2d(network, 64, 3, activation='relu')
network = conv_2d(network, 64, 3, activation='relu')
network = max_pool_2d(network, 2)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.4)
network = fully_connected(network, 2, activation='softmax')
network = regression(network, optimizer='adam',
                     loss='categorical_crossentropy',
                     learning_rate=0.001)

model = tflearn.DNN(network, tensorboard_verbose=0, checkpoint_path='skin-classifier.tfl.ckpt')
model.load("skin-classifier.tfl")

Y_predicted = model.predict_label(X)

cnf_matrix = confusion_matrix(Y.argmax(axis=1), Y_predicted.argmax(axis=1))
print("Confusion matrix: ")
print(cnf_matrix)

0 个答案:

没有答案