我是深度學習和 Tensorflow 的初學者。在預處理部分,我一次又一次地停留在那個部分,我必須為某些特定的 NN 架構調整具有特定尺寸的影像大小。我用谷歌搜索并嘗試了不同的方法,但徒勞無功。
例如,我為 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)
我正在嘗試使用 cv2 調整影像大小,但在擴展后,尺寸變為:
(227, 227, 1)
而我希望它們是:
(227, 227, 3)
那么,有沒有更好的方法來做到這一點?
uj5u.com熱心網友回復:
禁食的一種選擇是創建一個資料集,tf.data.Dataset然后撰寫一個用于調整影像大小的函式,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()
輸出:
(227, 227, 3)

uj5u.com熱心網友回復:
腳本中的以下行導致了問題
x_train = np.array([cv2.resize(img, dim) for img in x_train[:,:,:]])
將其更改為
x_train = np.array([cv2.resize(img, dim) for img in x_train])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/462354.html
標籤:python-3.x 张量流 图像处理 深度学习 神经网络
上一篇:使用powershell將.bmp轉換為.png會導致記憶體泄漏
下一篇:了解alpha混合操作
