Resnet50在imagenet上的top1精度低于Mobilenet



Keras文档指出resnet50具有0.75的top1精度,而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])

测量精度(带有一个热编码标签(:

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

请让我知道结果的准确性值是否合理,或者我的代码中是否有错误。

一个更好的选择应该是经典的调整大小和中心裁剪:

img = image.load_img(file, target_size=(256, 256))
img = image.img_to_array(img)
img = tf.image.central_crop(image, 0.875) # 224/256
img = tf.image.resize(image, [224, 224])
img = np.expand_dims(img, axis=0)
return keras.applications.resnet50.preprocess_input(img)

ImageNet的典型预处理包括将大小调整为image_resolution+32,然后裁剪图像的中心部分。

最新更新