【发布时间】:2021-04-22 09:13:43
【问题描述】:
Keras 文档指出 resnet50 的 top1 准确度为 0.75,而 mobilenet 只有 0.7,这是有道理的,因为 mobilenet 更轻。
但是,在 imagenet 验证数据集 (2012) 上测试这两个模型的准确度为 mobilenet 为 0.695,resnet50 为 0.68。在这个数据集上,resnet 怎么会不如 mobilenet 准确呢?
下面是测试resnet50的代码。
预处理:
def prepare_image_resnet50(file):
img = image.load_img(file, target_size=(224, 224))
img_array = image.img_to_array(img)
img_array_expanded_dims = np.expand_dims(img_array, axis=0)
return keras.applications.resnet50.preprocess_input(img_array_expanded_dims)
推理:
predictions = []
for i in range(10000):
j = i + 1
num = fill_zeros(str(j))
img_name = fixed + num + '.JPEG'
image_t = prepare_image_resnet50('H:/Datasets/imagenet/2012/'+img_name)
tensor = tf.convert_to_tensor(image_t, dtype=tf.float32)
prediction = model.predict(image_t)
predictions.append(prediction[0])
测量准确度(使用 one-hot 编码标签):
def top1_accuracy(predictions, labels):
acc_sum = 0
num_of_samples = len(predictions)
for i in range(num_of_samples):
true_class_index = np.argmax(labels[i])
pred_class_index = np.argmax(predictions[i])
if true_class_index == pred_class_index:
acc_sum += 1
return acc_sum/num_of_samples
如果得到的准确度值有意义或者我的代码中是否有错误,请告诉我。
【问题讨论】:
标签: python tensorflow machine-learning keras deep-learning