[英]How can i format my images from filepath to the same way as mnist.load_data() in python?
How can I format my images from file path to the same way as mnist.load_data()
? 如何将文件路径中的图像格式化为与mnist.load_data()
相同的方式? I'm currently getting my images from a driectory/file path, how can I format these images the same way as mnist
uses for mnist.load_data()
? 我目前从driectory /文件路径获取图像,我如何格式化这些图像的方式与mnist
用于mnist.load_data()
方式相同?
The keras.datasets.mnist.load_data
actually just loads a preprocessed pickle file . keras.datasets.mnist.load_data
实际上只加载一个预处理的pickle文件 。 If you check the data type of X_train
& X_test
they are just a numpy
2D array of float representing images pixel value (0-255). 如果检查的数据类型X_train
& X_test
它们只是numpy
浮子表示图像的像素值(0-255)的2D阵列。 While y_train
& y_test
are just numpy
1D array representing the classes/labels (0-9). 虽然y_train
和y_test
只是代表类/标签(0-9)的numpy
1D数组。
So the first way to imitate that functionality is read you images using image processing library (eg. opencv ) into numpy array & finally split them using sklearn : 因此,模仿该功能的第一种方法是使用图像处理库(例如opencv )将图像读入numpy数组,最后使用sklearn将它们拆分 :
import numpy as np
import cv2
from sklearn.model_selection import train_test_split
X = []
y = []
# convert color image to 2D array (grayscale) & rescale
data = cv2.imread('zero.jpg',0) / 255.0
label = 0 # label/class of the image
X.append(data)
y.append(label)
# loop trough all images ...
# split for training & testing
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
Another way that you can try is using keras ImageDataGenerator.flow_from_directory(color_mode='grayscale') . 您可以尝试的另一种方法是使用keras ImageDataGenerator.flow_from_directory(color_mode ='grayscale') 。 The output is an ImageDataGenerator
object that can be passed to keras model.fit_generator()
function. 输出是一个ImageDataGenerator
对象,可以传递给keras model.fit_generator()
函数。 In order to make use this function, you should arrange your dataset into train & test directories where each of them contains subdirectories representing classes of the images inside them. 为了使用此功能,您应该将数据集安排到列车和测试目录中,其中每个目录都包含表示其中图像类的子目录。 Please find detailed explanation in here . 请在此处找到详细说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.