如何利用cnn和稀疏分类交叉熵构造多类分类器

2024-03-28 13:59:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我是机器学习的初学者,正在研究一个多类分类的问题,我预测猫、狗和马的三类动物,并使用如下值(狗=0,猫=1,马=2)。我是用sparse_categorical_crossentropy做的,得到了95%的准确率,但在预测时,我的模型给出了3个不同的结果。那么,是代码有什么问题还是我做得不对? 下面是我的全部代码和输出:

# load dogs vs cats vs horse dataset, reshape and save to a new file
from os import listdir
from numpy import asarray
from numpy import save
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
# define location of dataset
folder = 'train/'
photos, labels = list(), list()
# enumerate files in the directory
for file in listdir(folder):
    # determine class
    output = 0.0
    if file.startswith('cat'):
        output = 1.0
    if file.startswith('horse'):
        output = 2.0
    # load image
    photo = load_img(folder + file, target_size=(200, 200))
    # convert to numpy array
    photo = img_to_array(photo)
    # store
    photos.append(photo)
    labels.append(output)
# convert to a numpy arrays
photos = asarray(photos)
labels = asarray(labels)
print(photos.shape, labels.shape)
# save the reshaped photos
save('animals_photos.npy', photos)
save('animals_labels.npy', labels)

输出:(600,200,200,3)(600,)

这是我的型号代码:

# save the final model to file
from keras.applications.vgg16 import VGG16
from keras.models import Model
from keras.layers import Dense
from keras.layers import Flatten
from keras.optimizers import SGD
from keras.preprocessing.image import ImageDataGenerator

# define cnn model
def define_model():
    # load model
    model = VGG16(include_top=False, input_shape=(224, 224, 3))

    # mark loaded layers as not trainable
    for layer in model.layers:
        layer.trainable = False
    # add new classifier layers
    flat1 = Flatten()(model.layers[-1].output)
    class1 = Dense(128, activation='relu', kernel_initializer='he_uniform')(flat1)
    output = Dense(3, activation='softmax')(class1)
    # define new model
    model = Model(inputs=model.inputs, outputs=output)  
    # compile model
    opt = SGD(lr=0.001, momentum=0.9)
    model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
    return model

# run the test harness for evaluating a model
def run_test_harness():
# define model
    model = define_model()
    # create data generator
    datagen = ImageDataGenerator(featurewise_center=True)
    # specify imagenet mean values for centering
    datagen.mean = [123.68, 116.779, 103.939]
    # prepare iterator
    train_it = datagen.flow_from_directory('dataset_animal/',
        class_mode='sparse', batch_size=64, target_size=(224, 224))
    # fit model
    model.fit_generator(train_it, steps_per_epoch=len(train_it), epochs=10, verbose=1)
    # save model
    model.save('animal_model_sparse.h5')

# entry point, run the test harness
run_test_harness()

下面的代码是用模型预测图像:

# make a prediction for a new image.
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model
from numpy import argmax
# load and prepare the image
def load_image(filename):
    # load the image
    img = load_img(filename, target_size=(224, 224))
    # convert to array
    img = img_to_array(img)
    # reshape into a single sample with 3 channels
    img = img.reshape(1, 224, 224, 3)
    # center pixel data
    img = img.astype('float32')
    img = img - [123.68, 116.779, 103.939]
    return img

# load an image and predict the class
def run_example():
    # load the image
    img = load_image('train\horse.90.jpg')
    # load model
    model = load_model('animal_model_sparse.h5')
    # predict the class
    result = model.predict(img)
    print(result[0])

# entry point, run the example
run_example()

输出:

[0.4865459  0.35041294 0.16304109]

Tags: thetorunfromimageimportimgoutput
2条回答

你没有提供我要求的信息,但我遵循了代码,它只是工作得很好:

# make a prediction for a new image.
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model
from numpy import argmax
# load and prepare the image
def load_image(filename):
    # load the image
    img = load_img(filename, target_size=(224, 224))
    # convert to array
    img = img_to_array(img)
    # reshape into a single sample with 3 channels
    img = img.reshape(1, 224, 224, 3)
    # center pixel data
    img = img.astype('float32')
    img = img - [123.68, 116.779, 103.939]
    return img

# load an image and predict the class
def run_example():
    # load the image
    img = load_image('D:/___COMMON/Project/Test_area/VGG16-classifier_animals/train/horse-90.jpg')
    # load model    
    model = load_model('animal_model_sparse.h5')
    # predict the class
    result = model.predict(img)
    print(result[0])

# entry point, run the example
run_example()

输出:

[0. 0. 1.]

所以如果你想更好的检查,请提供更多的信息和反馈。你知道吗

不,没错。正如一条评论所说,“你的模型已经预测了0.4865459的概率,图像属于类别0(狗),0.35041294的概率为1(猫),0.16304109的概率为2(马)。”

要把这个预测变成可以解释的东西,你应该使用np.argmax最大值()检索最高概率类的索引,如下所示:

# load an image and predict the class
def run_example():
    # load the image
    img = load_image('train\horse.90.jpg')
    # load model
    model = load_model('animal_model_sparse.h5')
    # predict the class
    result = np.argmax(model.predict(img)[0])
    print('Predict class {}'.format(result))

相关问题 更多 >