【问题标题】:Resnet50 achieves lower top1 accuracy than Mobilenet on imagenetResnet50 在 imagenet 上实现的 top1 精度低于 Mobilenet
【发布时间】: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


    【解决方案1】:

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

    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,然后裁剪图像的中心部分。

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2020-02-03
    • 2020-09-06
    • 1970-01-01
    • 2021-02-19
    • 2021-04-16
    • 2018-02-21
    • 2019-03-18
    • 1970-01-01
    • 2019-05-24
    相关资源
    最近更新 更多