我一直在研究图像分类。我已经获得了相当准确的分类,但是我似乎无法获得所有类之间分配的概率的正确输出。
我将softmax作为具有正确数量的输出类的最后一个输出层,并且使用了predict()方法。据我有限的理解,这应该工作。但是,它不能预测正确的分类,但是我不知道它的自信程度。
示例输出: [0,0,0,1,0]
示例预期输出: [.025,.01,.2,.75,.015]
import os
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPool2D
from keras.layers import Flatten
from keras.layers import Dense, Dropout
"""
import tensorflow as tf
from keras import backend as K
num_cores = 6
config = tf.ConfigProto(intra_op_parallelism_threads=num_cores,
inter_op_parallelism_threads=num_cores)
session = tf.Session(config=config)
K.set_session(session)
"""
#from keras.models import load_model
#Got 25/26 w/ this one.
a_type = "relu"
d_type = "softmax"
i_shape = 512 #512 is the one to use
b_size = 32
e_poch = 15
output = 29
#====================================================================
# CNN
#====================================================================
classifier = Sequential()
#====================================================================
classifier.add(Conv2D(filters = 32, kernel_size = (3,3),
input_shape = (i_shape,i_shape,1),
activation = a_type))
classifier.add(MaxPool2D(pool_size = (2,2), strides = 2))
classifier.add(Dropout(.20))
#====================================================================
classifier.add(Conv2D(filters = 64, kernel_size = (3,3),
input_shape = (i_shape,i_shape,1),
activation = a_type))
classifier.add(MaxPool2D(pool_size = (2,2), strides = 2))
classifier.add(Dropout(.20))
#====================================================================
classifier.add(Conv2D(filters = 128, kernel_size = (3,3),
input_shape = (i_shape,i_shape,1),
activation = a_type))
classifier.add(MaxPool2D(pool_size = (2,2), strides = 2))
classifier.add(Dropout(.20))
classifier.add(Flatten())
#====================================================================
#====================================================================
#this was relu
classifier.add(Dense(units = 128, activation = a_type))
classifier.add(Dropout(.25))
classifier.add(Dense(units = 64, activation = a_type))
classifier.add(Dense(units = output, activation = d_type))
#print(classifier.summary())
classifier.compile(optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics= ['accuracy'])
#====================================================================
#====================================================================
# Image Processing
#====================================================================
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255)
#shear_range=0.2,
#zoom_range=0.2,
#horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory('dataset/training_set',
target_size=(i_shape, i_shape), #match to input_shape
color_mode='grayscale',
batch_size=b_size,
class_mode='categorical')
test_set = test_datagen.flow_from_directory('dataset/test_set',
target_size=(i_shape, i_shape),
color_mode='grayscale',
batch_size=b_size,
class_mode='categorical')
#====================================================================
test = classifier.fit_generator(training_set,
steps_per_epoch=(6230/b_size),
epochs=e_poch,
validation_data=test_set,
validation_steps=(699/b_size))
#====================================================================
# Save the model and weights
#====================================================================
classifier.save(a_type+'_'+d_type+'_'+str(output)+'_'+str(i_shape)+'_'+str(b_size)+'_'+str(e_poch)+'.h5')
#This will load the model
#class2 = load_model('path to .h5 file')
#classifier = load_model(a_type+'_'+d_type+str(i_shape)+'_'+str(b_size)+'_'+str(e_poch)+'.h5')
#classifier = load_model('256_32_50.h5')
#====================================================================
# Predict a new outcome
#====================================================================
import numpy as np
from keras.preprocessing import image
result_p = []
for root, dirs, files in os.walk(os.path.abspath("./dataset/single_prediction/")):
for file in files:
path = os.path.join(root, file)
test_image = image.load_img(path,
target_size = (i_shape, i_shape),
color_mode='grayscale')
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result_p.append(classifier.predict(test_image, batch_size=b_size))
#result = classifier.predict_proba(test_image)
result = classifier.predict_classes(test_image)
print(path)
print(result)
"""
test_image = image.load_img('dataset/single_prediction/VB.AT/1.png',
target_size = (i_shape, i_shape),
color_mode='grayscale')
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
"""
print(training_set.class_indices)
#print(result_p)