将MNIST数据集从(60000,28,28)调整大小为(60000,14,14)。

huangapple go评论68阅读模式
英文:

Resize MNIST dataset from (60000, 28, 28) to (60000, 14, 14)

问题

  1. 我对为什么要添加一个新的轴来改变我想要的形状感到困惑。

  2. 我想知道是否有另一种方法可以在不添加新轴的情况下进行调整大小操作。

英文:

I found the method of resizing the MNIST training dataset from (60000, 28, 28) to (60000, 14, 14).

This is the code and results:

import tensorflow as tf
import numpy as np

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
x_train, x_test = x_train[..., np.newaxis], x_test[..., np.newaxis]

x_train_small = tf.image.resize(x_train, (14,14)).numpy()
x_test_small = tf.image.resize(x_test, (14,14)).numpy()


print(x_train.shape)
print(x_test.shape)
print(x_train_small.shape)
print(x_test_small.shape)
​
>>>(60000, 28, 28, 1)
>>>(10000, 28, 28, 1)
>>>(60000, 14, 14, 1)
>>>(10000, 14, 14, 1)

  1. I'm confused about why it has to add a new axis to change the shape that I want.
  2. I would like to know whether there is another method to do the resize work without adding a new axis.

答案1

得分: 1

这些内容的翻译如下:

  • resize 的第一个参数是 images:"4-D 张量,形状为 [batch, height, width, channels] 或 3-D 张量,形状为 [height, width, channels]。"
  • 第二个参数是 size:"1-D int32 张量,包含 2 个元素:new_height, new_width。图像的新尺寸。"

结论:需要第四个维度,因为这是 tf.image.resize 期望的通道数量,不管如何。在这个维度上的尺寸为1,因为MNIST图像是灰度图像。

当然,你可以使用其他库来进行调整大小,但个人建议避免不必要的依赖,以保持代码的整洁。

英文:

This is all described in the docs:

  • The first argument of resize is images: "4-D Tensor of shape [batch, height, width, channels] or 3-D Tensor of shape [height, width, channels]."
  • The second is size: "A 1-D int32 Tensor of 2 elements: new_height, new_width. The new size for the images."

Conclusion: You need the fourth dimension because those are the channels which tf.image.resize expects no matter what. The size along that dimension is 1 because the MNIST image are grayscale.

Of course you could use a some other library to resize, but personally I would avoid unnecessary dependencies, just for the sake of cleanliness.

huangapple
  • 本文由 发表于 2023年2月6日 17:04:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75359243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定