首页 > 解决方案 > Keras 输入图像大小

问题描述

我有一组大小为 200x600(WxH)的图像,但是当我使用 pyplot 显示它时,它们看起来像它们的大小为 600x200。

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="training",
    seed=123,
    image_size=(200, 600),
    batch_size=batch_size)

我不确定为什么会这样。当我将其更改为image_size=(600, 200)时,它看起来不错,但 tensorflowjs 抱怨形状。

let tensorImg = tf.browser.fromPixels(canvas).resizeNearestNeighbor([200, 600]).toFloat().expandDims();

有什么提示吗?

标签: pythontensorflowkeras

解决方案


我用我自己的数据集测试了它按预期工作。 下面的示例代码

batch_size = 32
img_height = 200
img_width = 600

train_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

val_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

for image_batch, labels_batch in train_ds:
  print(image_batch.shape)
  print(labels_batch.shape)
  break

输出:

(32, 200, 600, 3)
(32,)

推荐阅读