简体   繁体   English

为深度学习模型调整图像大小的正确方法

[英]Proper way of resizing image for Deep Learning models

I'm a beginner in Deep Learning & Tensorflow. During the preprocessing part, I'm stucking again & again on that part where I have to resize the image with specific dimension for some specific NN architecture.我是深度学习和 Tensorflow 的初学者。在预处理部分,我一次又一次地停留在我必须为某些特定的 NN 体系结构调整图像的特定尺寸的部分。 I googled and tried different methods but in vain.我用谷歌搜索并尝试了不同的方法,但没有成功。

For eg., I did following to resize image to 227 x 227 for AlexNet:例如,我按照以下步骤将 AlexNet 的图像大小调整为 227 x 227:

height = 227
width = 227
dim = (width, height)

x_train = np.array([cv2.resize(img, dim) for img in x_train[:,:,:]])
x_valid = np.array([cv2.resize(img, dim) for img in x_valid[:,:,:]])

x_train = tf.expand_dims(x_train, axis=-1)
x_valid = tf.expand_dims(x_valid, axis=-1)

I'm trying to resize the images with cv2 but after expanding, the dimensions come out to be:我正在尝试使用 cv2 调整图像的大小,但在展开后,尺寸变为:

(227, 227, 1)

whereas I want them to be:而我希望他们是:

(227, 227, 3)

So, is there any better way to do this?那么,有没有更好的方法来做到这一点?

The following line in your script is causing the problem脚本中的以下行导致了问题

x_train = np.array([cv2.resize(img, dim) for img in x_train[:,:,:]])

Change it to改成

x_train = np.array([cv2.resize(img, dim) for img in x_train])

One option for fasting do this can be creating a dataset with tf.data.Dataset then writing a function for resizing images with tf.image.resize like below:禁食的一种选择是使用tf.data.Dataset创建数据集,然后编写 function 以使用tf.image.resize调整图像大小,如下所示:

import tensorflow as tf
import matplotlib.pyplot as plt

(X_train, y_train), (X_test, y_test) = tf.keras.datasets.cifar10.load_data()

train_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))
test_dataset  = tf.data.Dataset.from_tensor_slices((X_test, y_test))


HEIGHT = 227
WIDTH = 227

def resize_preprocess(image, label):
    image = tf.image.resize(image, (HEIGHT, WIDTH)) / 255.0
    return image, label


train_dataset = train_dataset.map(resize_preprocess, num_parallel_calls=tf.data.AUTOTUNE)
test_dataset  = test_dataset.map(resize_preprocess, num_parallel_calls=tf.data.AUTOTUNE)


for image, label in train_dataset.take(1):
    print(image.shape)
    plt.imshow(image), plt.axis('off')
    plt.show()

Output: Output:

(227, 227, 3)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在python深度学习中构建图像推荐系统的最佳模型 - Best models to build Image recommendation System in python deep learning 用于图像训练的深度学习 - Deep learning for image training 有没有办法使用 AWS S3 上的数据在本地运行深度学习 model? - Is there a way to run a Deep Learning model locally with the data on AWS S3? 这个深度学习图像分类问题的目标应该是什么 - what should be the target in this deep learning image classification problem 我们是否可以使用TensorflowLite在Raspberry Pi3上运行多个深度学习模型以进行推理 - Can we run multiple deep learning models on Raspberry Pi3 with TensorflowLite for inferenc 在 PyTorch 中加载用于推理的迁移学习模型的正确方法是什么? - What is the proper way to load a transfer learning model for inference in PyTorch? 通过 REST 框架在 Django 模型中进行健壮搜索的正确方法 - Proper way to do a robust search in Django models via REST framework 使用 mlflow 提供在线学习模型 - Serve online learning models with mlflow 使用深度学习的小物体检测 - Small object detection using deep learning Keras中具有不同数据类型的深度学习模型 - Deep Learning model with Different data types in Keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM