我有10000张手写数字的BMP图像。如果我想将这些数据馈送到神经网络中,我应该怎么做?对于MNIST数据集,我只需要编写
(X_train, y_train), (X_test, y_test) = mnist.load_data()
我正在使用Python的Keras库。我该如何创建这样的数据集?
我有10000张手写数字的BMP图像。如果我想将这些数据馈送到神经网络中,我应该怎么做?对于MNIST数据集,我只需要编写
(X_train, y_train), (X_test, y_test) = mnist.load_data()
我正在使用Python的Keras库。我该如何创建这样的数据集?
如果所有图像都可以放入内存,则可以编写一个函数将它们加载并堆叠到numpy数组中,或者使用Keras ImageDataGenerator(https://keras.io/preprocessing/image/),该类包括一个名为flow_from_directory
的函数。你可以在这里找到一个示例:https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d。
imagePaths = sorted(list(paths.list_images(args["testset"])))
# loop over the input images
for imagePath in imagePaths:
# load the image, pre-process it, and store it in the data list
image = cv2.imread(imagePath)
image = cv2.resize(image, (IMAGE_DIMS[1], IMAGE_DIMS[0]))
image = img_to_array(image)
data.append(image)
# extract the class label from the image path and update the
# labels list
data = np.array(data, dtype="float") / 255.0
tf.keras.datasets.mnist.load_data(path="mnist.npz")
#generate and save file
from PIL import Image
import os
import numpy as np
path_to_files = "./images/"
vectorized_images = []
for _, file in enumerate(os.listdir(path_to_files)):
image = Image.open(path_to_files + file)
image_array = np.array(image)
vectorized_images.append(image_array)
# save as DataX or any other name. But the same element name is to be used while loading it back.
np.savez("./mnistlikedataset.npz",DataX=vectorized_images)
np.savez("./mnistlikedataset.npz",DataX=vectorized_images_x,DataY=vectorized_images_Y)
#load and use file
import numpy as np
path = "./mnistlikedataset.npz"
with np.load(path) as data:
#load DataX as train_data
train_data = data['DataX']
print(train_data)
with np.load(path) as data:
train_data = data['DataX']
print(train_data)
test_data = data['DataY']
print(test_data)
import numpy as np
def save_data():
[images, labels] = read_data()
outshape = len(images[0])
npimages = np.empty((0, outshape), dtype=np.int32)
nplabels = np.empty((0,), dtype=np.int32)
for i in range(len(labels)):
label = labels[i]
npimages = np.append(npimages, [images[i]], axis=0)
nplabels = np.append(nplabels, y)
np.save('images', npimages)
np.save('labels', nplabels)
def read_data():
return [np.load('images.npy'), np.load('labels.npy')]