如何处理深度学习中的图像大小变化?

时间:2021-01-24 14:22:57

标签: tensorflow opencv machine-learning deep-learning conv-neural-network

我正在研究一个图像分类模型,该模型将图像分为 5 个类别。 我在一个文件夹中存储了 5000 个训练图像数据,但所有图像的高度和宽度都不同。 像这样 -

'631.jpg': {'width': 81, 'heigth': 25},
'8595.jpg': {'width': 1173, 'heigth': 769},
'284.jpg': {'width': 94, 'heigth': 75},
'5999.jpg': {'width': 4220, 'heigth': 1951}
  

谁能告诉我处理这类数据的技术?

1 个答案:

答案 0 :(得分:0)

tf.image.resize_with_crop_or_pad(image, desired_height, desired_width)

小于 desired_heightdesired_width 的图像将被填充,较大的图像将被集中裁剪。

import tensorflow as tf
import matplotlib.pyplot as plt

_, ((first, *rest), _) = tf.keras.datasets.cifar10.load_data()

modified = tf.image.resize_with_crop_or_pad(first[None, ...]/255, 48, 48)

plt.imshow(tf.squeeze(modified))
plt.show()

enter image description here