【问题标题】:reshaping array keras model to suit minimum size of pre_trained_model重塑数组 keras 模型以适应 pre_trained_model 的最小大小
【发布时间】:2021-12-30 21:48:18
【问题描述】:

我正在尝试对 inceptionv3 算法使用迁移学习进行一些图像分类。我有 48x48 灰度图像,但 inception v3 模型需要的最小输入尺寸为 75x5(见下文)

pre_trained_model = InceptionV3(input_shape = (48,48,3),
                           include_top  = False,
                           weights      = 'imagenet')

ValueError: Input size must be at least 75x75; Received: input_shape=(48, 48, 3)

如果我尝试将数组重塑为大于 75x75 的尺寸,我会收到以下错误,这是有道理的

train_images = train_image_array.reshape((train_image_array.shape[0], 76, 76, 1))
ValueError: cannot reshape array of size 66145536 into shape (28709,76,76,1)

如何将我的数组填充为大于 75x75 的东西?我觉得这应该很容易,但我似乎找不到解决方案。我填充数组的代码如下:

image_array = np.zeros(shape=(len(data), 48, 48))
image_label = np.array(list(map(int, data['emotion'])))

for i, row in enumerate(data.index):
    image = np.fromstring(data.loc[row, 'pixels'], dtype=int, sep=' ')
    image = np.reshape(image, (48, 48))
    image_array[i] = image
    
return image_array, image_label

【问题讨论】:

  • Reshape 和 resize 不一样,你想调整图片的大小,而不是 reshape。
  • @Dr.史努比如果我用image = np.resize(image, (150,150)) 调整我的图像大小,然后尝试重塑train_images = train_image_array.reshape((train_image_array.shape[0], 150, 150, 1)) 我仍然得到与数组大小相同的错误 ValueError: cannot reshape array of size 66145536 into shape (28709,150,150,1)
  • 您似乎只调整了一张图像的大小,而不是您的整个图像数据集?

标签: python tensorflow keras


【解决方案1】:

正如已经指出的,在将数据集提供给 Inception 模型之前,您只需使用某种调整大小的方法来调整您的大小:

import tensorflow as tf
import numpy as np

model = tf.keras.applications.InceptionV3(input_shape = (75, 75, 3),
                           include_top = False,
                           weights = 'imagenet')
samples = 20
classes = 10
images = np.random.rand(samples, 48, 48, 3)
labels = np.random.randint(5, size=(samples, classes))
dataset = tf.data.Dataset.from_tensor_slices((images, labels)).batch(5)

def resize_images(images, labels):
  return tf.image.resize(images, [75, 75], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR), labels

dataset = dataset.map(resize_images)

for x, y in dataset:
  print(x.shape, y.shape)
(5, 75, 75, 3) (5, 10)
(5, 75, 75, 3) (5, 10)
(5, 75, 75, 3) (5, 10)
(5, 75, 75, 3) (5, 10)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 2019-12-16
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多