简体   繁体   English

Keras 应用程序 - VGG16 在 imagenet 上的精度低

[英]Keras Applications - VGG16 low Accuracy on imagenet

I'm trying to replicate the performance of VGG-16 mentioned here: https://github.com/keras-team/keras-applications我正在尝试复制此处提到的 VGG-16 的性能: https://github.com/keras-team/keras-applications

But when I run the model on the imagenet dataset from tensorflow datasets, I get a lower top5 accuracy of 0.866.但是,当我在 tensorflow 数据集的 imagenet 数据集上运行 model 时,我得到的 top5 精度较低,为 0.866。

This is my code:这是我的代码:

import tensorflow_datasets as tfds
import tensorflow as tf
from tensorflow.keras import applications
import tensorflow.keras.applications.vgg16 as vgg16

def scale16(image, label):
  i = image
  i = tf.cast(i, tf.float32)
  i = tf.image.resize(i, (224,224))
  i = vgg16.preprocess_input(i)
  return (i, label)

def batch_set(dataset, batch_size):
    return dataset.map(scale16) \
                  .shuffle(1000) \
                  .batch(batch_size) \
                  .prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

def create_batched_datasets(map_fn, data_dir = "/content", batch_size = 64):
    datasets, info = tfds.load(name="imagenet2012", 
                            with_info=True, 
                            as_supervised=True, 
                            download=False, 
                            data_dir=data_dir
                            )
    train = batch_set(datasets['train'], batch_size)
    val = batch_set(datasets['validation'], batch_size)
    return train, val, info


train, test_dataset, info = create_batched_datasets(scale16)

model = vgg16.VGG16(weights='imagenet', include_top=True)

model.compile('sgd', 'categorical_crossentropy', 
              ['sparse_categorical_accuracy','sparse_top_k_categorical_accuracy'])

model.evaluate(test_dataset)

What am i missing?我错过了什么? I'm running the code on google colab.我在 google colab 上运行代码。

The code does not preprocess the images correctly.该代码未正确预处理图像。 the tf.image.resize() method scales the image down. tf.image.resize() 方法缩小图像。 But according to the keras website, the 224x224x3 images should be created by a center crop.但根据 keras 网站,224x224x3 图像应该由中心裁剪创建。 Changing the scale16() method solves the problem:更改 scale16() 方法可以解决问题:


def resize_image(image, shape = (224,224)):
  target_width = shape[0]
  target_height = shape[1]
  initial_width = tf.shape(image)[0]
  initial_height = tf.shape(image)[1]
  im = image
  ratio = 0
  if(initial_width < initial_height):
    ratio = tf.cast(256 / initial_width, tf.float32)
    h = tf.cast(initial_height, tf.float32) * ratio
    im = tf.image.resize(im, (256, h), method="bicubic")
  else:
    ratio = tf.cast(256 / initial_height, tf.float32)
    w = tf.cast(initial_width, tf.float32) * ratio
    im = tf.image.resize(im, (w, 256), method="bicubic")
  width = tf.shape(im)[0]
  height = tf.shape(im)[1]
  startx = width//2 - (target_width//2)
  starty = height//2 - (target_height//2)
  im = tf.image.crop_to_bounding_box(im, startx, starty, target_width, target_height)
  return im

def scale16(image, label):
  i = image
  i = tf.cast(i, tf.float32)
  i = resize_image(i, (224,224))
  i = vgg16.preprocess_input(i)
  return (i, label)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM