在用深度网络训练时,大部分网络都要求输入为3通道,而有时现有的数据为单通道的灰度图,并且尺寸也不符合网络输入,可用下面的函数转换,以minist数据集为例。
import numpy as np
from keras.datasets import mnist
from keras.utils import to_categorical
image_size = 224
def load_mnist(image_size):
(x_train,y_train),(x_test,y_test) = mnist.load_data()
train_image = [cv2.cvtColor(cv2.resize(img,(image_size,image_size)),cv2.COLOR_GRAY2BGR) for img in x_train]
test_image = [cv2.cvtColor(cv2.resize(img,(image_size,image_size)),cv2.COLOR_GRAY2BGR) for img in x_test]
train_image = np.asarray(train_image)
test_image = np.asarray(test_image)
train_label = to_categorical(y_train)
test_label = to_categorical(y_test)
print('finish loading data!')
return train_image, train_label, test_image, test_label