我使用的是CIFAR 100数据集。我使用InceptionResNetV 2来构建我的模型。我使用数据增强训练了25个epoch。即使这个数据集有32 x32的图像,我也将它们放大到75 x75。结果如下
第一节第一节第一节第一节第一次
之后我将权重保存在一个h5文件中,以便以后使用。现在,我加载模型如下:
from sklearn.metrics import classification_report
import tensorflow as tf, numpy as np
from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.preprocessing.image import ImageDataGenerator
from keras.utils import to_categorical
from keras.layers import Dense, GlobalAveragePooling2D, Dropout
from keras.models import Model, Sequential
tf.keras.backend.clear_session()
(x_train, y_train), (x_test, y_test)=tf.keras.datasets.cifar100.load_data(label_mode='fine')
print(x_train.shape, y_train.shape, x_test.shape, y_test.shape)
#We found names of the classes For learning and visual reason
labels = ['apple', 'aquarium_fish', 'baby', 'bear', 'beaver', 'bed', 'bee', 'beetle', 'bicycle', 'bottle', 'bowl', 'boy', 'bridge', 'bus', 'butterfly',
'camel', 'can', 'castle', 'caterpillar', 'cattle', 'chair', 'chimpanzee', 'clock', 'cloud', 'cockroach', 'couch', 'crab', 'crocodile', 'cup',
'dinosaur', 'dolphin', 'elephant', 'flatfish', 'forest', 'fox', 'girl', 'hamster', 'house', 'kangaroo', 'computer_keyboard',
'lamp', 'lawn_mower', 'leopard', 'lion', 'lizard', 'lobster', 'man', 'maple_tree', 'motorcycle', 'mountain', 'mouse', 'mushroom',
'oak_tree', 'orange', 'orchid', 'otter', 'palm_tree', 'pear', 'pickup_truck', 'pine_tree', 'plain', 'plate', 'poppy', 'porcupine', 'possum',
'rabbit', 'raccoon', 'ray', 'road', 'rocket', 'rose',
'sea', 'seal', 'shark', 'shrew', 'skunk', 'skyscraper', 'snail', 'snake', 'spider', 'squirrel', 'streetcar', 'sunflower', 'sweet_pepper',
'table', 'tank', 'telephone', 'television', 'tiger', 'tractor', 'train', 'trout', 'tulip', 'turtle',
'wardrobe', 'whale', 'willow_tree', 'wolf', 'woman', 'worm']
train_datagen = ImageDataGenerator(
rescale=1.0/255.0,
height_shift_range=0.2,
width_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
rotation_range=40,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest',
)
test_datagen = ImageDataGenerator(
# Your Code Here
rescale=1.0/255.0)
# Convert labels to one hot encoding matrix
print(y_train.shape, y_test.shape)
train_y = to_categorical(y_train, 100)
test_y = to_categorical(y_test, 100)
print(train_y.shape, test_y.shape)
def build_IncV4_model(input_shape, n_classes):
base4 = InceptionResNetV2(input_shape=input_shape,
weights="imagenet",
include_top=False,
classes=n_classes)
optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4)
model = Sequential()
model.add(base4)
model.add(GlobalAveragePooling2D())
model.add(Dropout(0.5))
model.add(Dense(n_classes, activation='softmax'))
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
return model
mymodel = build_IncV4_model(input_shape=(75, 75, 3), n_classes=len(labels))
mymodel.summary()# Evaluate model on validation data
mymodel.load_weights('./cifar100_incv4_weights.h5')
mymodel.evaluate(test_datagen.flow(tf.image.resize(x_test, [75, 75]),test_y))
evaluate函数的结果是:[1.1647623777389526,0.7148000001907349].当我尝试使用sklearn创建分类报告时,问题出现了。所有指标都是0。对于73类,它只显示1,并且我得到了这个警告“UndefinedMetricWarning:精度和F-score定义不明确,在没有预测样本的标签中设置为0.0。使用zero_division
参数控制此行为。_warn_prf(average,modifier,msg_start,len(result))"。我不确定是否正确应用了一个热编码。
data = mymodel.predict(tf.image.resize(x_test, [75, 75]))
pred = np.argmax(data, axis=1)
print(classification_report(y_test, pred, labels=list(range(len(labels)))))
提前感谢您的所有评论。
1条答案
按热度按时间kmb7vmvb1#
你的pred如何?另一件事,我认为在这里你应该在预测之前划分x_test / 255.。因为在ImageDataGenerator中你可以重新缩放。
试试这个: