繁体   English   中英

TensorFlow 数据集的意外尺寸

[英]Unexpected dimensions with TensorFlow dataset

我正在尝试在 MNIST 数据集上使用 InceptionV3 进行迁移学习。

计划是读取 MNIST 数据集,调整图像大小,然后使用它们进行训练,如下所示:

import numpy as np
import os
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

import tensorflow.compat.v2 as tf
import tensorflow.compat.v1 as tfv1
from tensorflow.python.keras.applications import InceptionV3

tfv1.enable_v2_behavior()

print(tf.version.VERSION)

img_size = 299

def preprocess_tf_image(image, label):
  image = tf.image.grayscale_to_rgb(image)
  image = tf.image.resize(image, [img_size, img_size])
  return image, label

#Acquire MNIST data
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
#Convert data to [0,1] range
x_train, x_test = x_train / 255.0, x_test / 255.0

#Add extra dimension to images so that they can be converted to RGB
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test  = x_test.reshape (x_test.shape[0],  28, 28, 1)

x_train = tf.data.Dataset.from_tensor_slices((x_train, y_train))
x_test  = tf.data.Dataset.from_tensor_slices((x_test, y_test))

#Convert images to RGB space and resize
x_train = x_train.map(preprocess_tf_image)
x_test  = x_test.map(preprocess_tf_image)

img_shape = (img_size, img_size, 3)

#Get trained model, but leave off the head
base_model = InceptionV3(input_shape = img_shape, weights='imagenet', include_top=False)
base_model.trainable = False

#Make a model with a new head
model = tf.keras.Sequential([
  base_model,
  tf.keras.layers.GlobalAveragePooling2D(),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

#Compile model
model.compile(
    optimizer='adam', #tf.keras.optimizers.RMSprop(lr=BASE_LEARNING_RATE),
    loss='binary_crossentropy',
    metrics=['accuracy']
)

model.fit(x_train, epochs=5)

model.evaluate(x_test)

但是,当我运行它时,事情在model.fit()处停止并出现错误:

ValueError:检查输入时出错:预期 inception_v3_input 有 4 个维度,但得到了形状为 (299、299、3) 的数组

这是怎么回事?

map应用到数据集后,响应没有关于批量大小的信息,您必须调用batch function 来添加它:

x_train = x_train.batch(batch_size = BATCH_SIZE) # adds batch size dimension to train dataset

x_test = x_test.batch(batch_size = BATCH_SIZE) # idem for test.

之后,我可以使用 Google 的 Colab 全面训练和评估 model,您可以 在此处查看

暂无
暂无

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

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