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

标签 python tensorflow keras google-colaboratory tensorflow-datasets

我正在尝试复制此处提到的 VGG-16 的性能: https://github.com/keras-team/keras-applications

但是当我在 tensorflow 数据集的 imagenet 数据集上运行模型时,我得到的 top5 准确度较低,为 0.866。

这是我的代码:

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)

我错过了什么?我正在 google colab 上运行代码。

最佳答案

代码未正确预处理图像。 tf.image.resize() 方法缩小图像。但根据 keras 网站,224x224x3 图像应该由中心裁剪创建。更改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)

关于python - Keras 应用程序 - imagenet 上的 VGG16 低精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58248297/

相关文章:

python - 如何使用 MySQLdb 在 django 中创建回滚按钮?

python - Numba、Jitclass w/nopython 模式和字典

tensorflow - 使用 Bazel 安装 Tensorflow 时调用ProcessError

tensorflow - 如何使用 tensorflow tf.metrics.mean_iou?

python - 如何将 one-hot 编码转换为整数?

python - LSTM:了解时间步长、样本和特征,尤其是在 reshape 和 input_shape 中的使用

python - 普通类中的抽象方法

python - 相当于 Django ORM 中的 group_concat

python - 带有 tensorflow-gpu 的 Keras 完全卡住了 PC

python - 当使用 metrics= ['accuracy' ] 时,Keras 使用了什么精度函数?