Python:在CNN中使用分类十字奖杯对多个obj进行分类的问题

2024-05-16 22:55:30 发布

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

我正在使用github的A.I.算法样本来研究图像识别和处理。导入4个dota hero类的600个jpeg图像,并将其大小调整为64*64像素。400个图像是该代码的训练集,200个图像是测试集。问题是python程序输出给我:这个dota II英雄是sven,也给我选项五:这是一个非dota英雄,其中要测试的图像是mirana的图像。以下是我的部分代码:

classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dense(units = 4, activation = 'softmax'))

# Compiling the CNN
 classifier.compile(optimizer = 'adam', loss =                         
'categorical_crossentropy', metrics = ['accuracy'])

# Part 2 - Fitting the CNN to the images
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/dota_training_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical')
test_set = test_datagen.flow_from_directory('dataset/dota_test_set',
target_size = (64, 64),
batch_size = 32,
class_mode = 'categorical')
classifier.fit_generator(training_set,
steps_per_epoch = 1000,
epochs = 3,
validation_data = test_set,
validation_steps = 500)

# Part 3 - Making new predictions
import numpy as np
from keras.preprocessing import image
test_image = image.load_img('dataset/single_prediction/test_2.jpg', target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1.0:
    prediction = 'sven'
    print("This is a Sven.")
if result[0][0] == 2.0:
    prediction = 'mirana'
    print("This is a mirana.")
if result[0][0] == 3.0:
    prediction = 'Wraith_King'
    print("This is a Wraith King.")
if result[0][0] == 4.0:
    prediction = 'Phantom_Lancer'
    print("This is a Phantom Lancer.")
else:
    prediction = 'non_dota hero'
    print("This is a non dota hero.")

我怀疑这是最后一部分如果和其他问题。我如何能够纠正我的问题,以便程序能够提供正确的答案。多谢各位


Tags: fromtest图像imagesizeistrainingresult
1条回答
网友
1楼 · 发布于 2024-05-16 22:55:30

else块只是最后一个ifelse

if result[0][0] == 4.0:
    prediction = 'Phantom_Lancer'
    print("This is a Phantom Lancer.")
else:
    prediction = 'non_dota hero'
    print("This is a non dota hero.")

将另一个ifs更改为elifs

if result[0][0] == 1.0:
    prediction = 'sven'
    print("This is a Sven.")
elif result[0][0] == 2.0:
    prediction = 'mirana'
    print("This is a mirana.")
elif result[0][0] == 3.0:
    prediction = 'Wraith_King'
    print("This is a Wraith King.")
elif result[0][0] == 4.0:
    prediction = 'Phantom_Lancer'
    print("This is a Phantom Lancer.")
else:
    prediction = 'non_dota hero'
    print("This is a non dota hero.")

相关问题 更多 >