python - TensorFlow 数据集的意外维度

标签 python tensorflow machine-learning tensorflow-datasets mnist

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

计划是读取 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: Error when checking input: expected inception_v3_input to have 4 dimensions, but got array with shape (299, 299, 3)

发生什么事了?

最佳答案

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

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 全面训练和评估模型,您可以查看 here .

关于python - TensorFlow 数据集的意外维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58142459/

相关文章:

machine-learning - 如何加载检查点文件并使用略有不同的图形结构继续训练

python - 恢复keras seq2seq模型

python - 使用 tensorflow 和 inception-v3 的边界框

matlab: fminunc 返回 'User objective function returned complex; trying a new point...'

c++ - Opencv 3 支持 vector 机训练

python - python SWIG对象比较

python - 迭代 Django 中的相关对象

python - 在python中合并列表对象

python - 使用 Openpyxl 读取时索引超出范围

python - Jupyter Notebook 和 Colab 因运行随机森林模型而不断崩溃