简体   繁体   English

我如何从filepath格式化我的图像到与python中的mnist.load_data()相同的方式?

[英]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_trainX_test它们只是numpy浮子表示图像的像素值(0-255)的2D阵列。 While y_train & y_test are just numpy 1D array representing the classes/labels (0-9). 虽然y_trainy_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.

相关问题 如何 select 从 mnist.load_data() 获得所需的训练样本 - How to select a desired training sample from mnist.load_data() 如何预处理我的图像,以便 SVM 可以像处理 MNIST 数据集一样处理它 - How can I preprocess my image so it can be processed by a SVM in the same way it processes the MNIST dataset 如何将 mnist 数据转换为 RGB 格式? - How can i convert mnist data to RGB format? 为什么我的代码无法从Google加载MNIST数据集? - why my code can not load MNIST dataset from google? 为什么我不能使用 gensim 下载器从特定文件路径加载? - Why can't I load from a specific filepath with gensim downloader? 无法在Windows上使用python-mnist软件包加载MNIST数据 - Cannot load MNIST data with the python-mnist package on Windows 如何在python中调整图像的大小以降低MNIST时尚数据等灰度低分辨率? - How can I resize an image in python to gray scale low resolution like MNIST fashion data? 如何调整我的神经网络以避免过度拟合 mnist 数据集? - How can I tune my neural network to avoid overfitting the mnist data set? 如何将 Keras MNIST 数据集与我自己的 MNIST 图像相结合? - How to combine Keras MNIST dataset with my own MNIST images? 如何将CIFAR数据集转换为与MNIST相同的格式 - How to convert CIFAR dataset into the same format as MNIST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM