gpt4 book ai didi

python - 如何使用 cnn 和稀疏_分类_交叉熵构建多类分类器

转载 作者:行者123 更新时间:2023-11-30 08:37:33 24 4
gpt4 key购买 nike

我是机器学习的初学者,正在研究多类分类问题,其中我预测动物猫、狗和马这三类,并使用如下值(狗=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]

最佳答案

您没有提供我要求的信息,但我按照代码操作,效果很好:

# 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.]

所以如果您想更好地检查,请提供更多信息和反馈。

关于python - 如何使用 cnn 和稀疏_分类_交叉熵构建多类分类器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59522643/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com