python - EfficientNet B0 模型在测试时无法预测正确的类别
问题描述
我使用训练数据集训练了一个有效的netB0 模型,该数据集包含大约 400 张军队人员、公众和军用车辆(特别是每个类别的坦克)的图像。在训练集上对其进行训练后,我得到了大约 98% 的准确率,在测试集上,我得到了相当不错的准确率。
测试集上的混淆矩阵为:[Army, general, vehicle]
[[52 35 7]
[ 5 93 15]
[ 5 5 86]]
但是当我尝试预测一个单独的图像时,它并不能很好地预测它。我尝试了 StackOverflow 的不同解决方案,但无法使任何工作。
import numpy as np
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import os
dataset_path = os.listdir('combined')
print (dataset_path) #what kinds of classes are in this dataset
print("Types of classes labels found: ", len(dataset_path))
class_labels = []
for item in dataset_path:
all_classes = os.listdir('combined' + '/' +item)
for room in all_classes:
class_labels.append((item, str('dataset_path' + '/' +item) + '/' + room))
print(class_labels)
df = pd.DataFrame(data=class_labels, columns=['Labels', 'image'])
print(df.head())
print(df.tail())
print("Total number of images in the dataset: ", len(df))
label_count = df['Labels'].value_counts()
print(label_count)
import cv2
path = 'combined/'
dataset_path = os.listdir('combined')
im_size = 224
images = []
labels = []
for i in dataset_path:
data_path = path + str(I)
filenames = [i for i in os.listdir(data_path) ]
print(data_path)
for f in filenames:
img = cv2.imread(data_path + '/' + f)
img = cv2.resize(img, (im_size, im_size))
images.append(img)
labels.append(i)
images = np.array(images)
print(images.shape)
images = images.astype('float32') / 255.0
images = preprocess_input(images)
print(images.shape)
from sklearn.preprocessing import LabelEncoder , OneHotEncoder
y=df['Labels'].values
print(y)
print(len(y))
print(list(set(y)))
y_labelencoder = LabelEncoder ()
y = y_labelencoder.fit_transform (y)
print(y)
print(list(set(y)))
y=y.reshape(-1,1)
from sklearn.compose import ColumnTransformer
ct = ColumnTransformer([('my_ohe', OneHotEncoder(), [0])], remainder='passthrough')
Y = ct.fit_transform(y) #.toarray()
print(Y[:5])
print(Y[35:])
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
images, Y = shuffle(images, Y, random_state=1)
train_x, test_x, train_y, test_y = train_test_split(images, Y, test_size=0.2, random_state=0)
from tensorflow.keras import layers
from tensorflow.keras.applications import EfficientNetB0
NUM_CLASSES = 3
IMG_SIZE = 224
size = (IMG_SIZE, IMG_SIZE)
inputs = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3))
outputs = EfficientNetB0(include_top=True, weights=None, classes=NUM_CLASSES)(inputs)
model = tf.keras.Model(inputs, outputs)
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )
model.summary()
hist = model.fit(train_x, train_y, epochs=30, verbose=2)
preds = model.evaluate(test_x, test_y)
print ("Loss = " + str(preds[0]))
print ("Test Accuracy = " + str(preds[1]))
from matplotlib.pyplot import imread
from matplotlib.pyplot import imshow
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.imagenet_utils import decode_predictions
from tensorflow.keras.applications.imagenet_utils import preprocess_input
img_path = '/content/combined/vehicles/al_khalid_l1.jpg'
img = cv2.imread(img_path)
img = cv2.resize(img, (224, 224))
img_array = image.img_to_array(img)
img_batch = np.expand_dims(img_array, axis=0)
img_preprocessed = preprocess_input(img_batch)
my_image = imread(img_path)
imshow(my_image)
preds=model.predict(img_preprocessed)
joined = preds[0]
joined[np.where(joined==np.max(joined)) ] = 1
joined[np.where(joined!=np.max(joined)) ] = 0
joined = list(map(int,joined))
print(joined)
label = ['vehicle', 'army', 'general']
zipped = zip(joined, label)
for i in list(zipped):
if i[0] == 1:
print(i[1])
预测结果为:[0, 1, 0] 军队
解决方案
推荐阅读
- javascript - 如何设置 Asp.net core 3.1 和 Reactjs 的混合结构
- c# - C# - 并行运行的多个任务可以附加到一个字符串吗?
- video - Xamarin Forms:如何捕获 webview 视频/音频播放时间?
- node.js - MongoDB自定义方法在其他两个数据被批准时更改一个数据
- javascript - 我如何获得机器人频道的频道 ID 和消息作者所在的频道
- security - 什么是木马-QT5?如何查看家庭酿造历史?
- javascript - 如何在Javascript中等待具有不同参数的同一函数的多次调用
- ubuntu - 在 x86-64 上启用分段
- c++ - 将向量存储和写入队列c ++
- java - 如何通过 JNI 热交换运行在 JVM 上的代码