尝试使用我自己的数据而不是cifar10生成Keras模型

时间:2018-05-17 12:31:51

标签: python-3.x tensorflow keras

我遵循了这个例子: https://www.pyimagesearch.com/2017/10/30/how-to-multi-gpu-training-with-keras-python-and-deep-learning/

并遇到以下问题(第51行):

((trainX, trainY), (testX, testY)) = cifar10.load_data()

因为我想根据自己的数据进行训练 有没有简单的方法来生成这种输出而不深入挖掘cifar的实现? 我很确定这是人们已经做过的事情,但我找不到样本/教程/示例

谢谢..

1 个答案:

答案 0 :(得分:1)

假设您的图片为.jpg格式,而您的标签为csv格式,名为label.csv,并将其分为2个文件夹train foldertest folder

然后,您可以执行以下操作以获取x_train

import cv2 #library for reading images
import numpy as np
import glob #library for reading files in a folder
x_train= []
for file in glob.glob("train/*.jpg"):
    im = cv2.imread(file) #reading each image from the folder
    x_train.append(im)
x_train = np.array(x_train)

您可以执行以下操作来获取y_train

import csv
y_train= []
with open('train/label.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        y_train.append([int(row[0])]) #converting the string to int (otherwise the csv data will be read as string)
y_train = np.array(y_train)

您可以对测试文件夹执行相同操作,只需更改参数和参数的名称。