回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我是机器学习的初学者,正在研究一个多类分类的问题,我预测猫、狗和马的三类动物,并使用如下值(狗=0,猫=1,马=2)。我是用<code>sparse_categorical_crossentropy</code>做的,得到了95%的准确率,但在预测时,我的模型给出了3个不同的结果。那么,是代码有什么问题还是我做得不对?
下面是我的全部代码和输出:</p>
<pre><code># 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)
</code></pre>
<p><strong>输出:(600,200,200,3)(600,)</strong></p>
<p>这是我的型号代码:</p>
<pre><code># 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()
</code></pre>
<p>下面的代码是用模型预测图像:</p>
<pre><code># 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()
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>[0.4865459 0.35041294 0.16304109]
</code></pre>