Python 使用 Keras,如何输入 X_train 图像(超过一千张图像)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40466303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:34:37  来源:igfitidea点击:

Using Keras, how can I input an X_train of images (more than a thousand images)?

pythonmachine-learningtensorflowkerasconv-neural-network

提问by Zahra Sorour

My application is accident-avoidance car systems using Machine Learning (Convolutional Neural Networks). My images are 200x100 JPG images and the output is an array of 4 elements: the car would move left, right, stop or move forward. So the output will let one element be 1(according to the correct action that should be taken) and the 3 other elements will be 0.

我的应用是使用机器学习(卷积神经网络)的事故避免汽车系统。我的图像是 200x100 JPG 图像,输出是 4 个元素的数组:汽车会向左、向右、停止或向前移动。因此,输出将让一个元素为1(根据应采取的正确操作),而其他 3 个元素将为0.

I want to train my machine now in order to help it input any image and decide on the action independently. Here's my code:

我现在想训练我的机器,以帮助它输入任何图像并独立决定动作。这是我的代码:

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.optimizers import SGD

import numpy as np

model = Sequential()

model.add(Convolution2D(16, 1, 1, border_mode='valid', dim_ordering='tf', input_shape=(200, 150, 1)))
model.add(Activation('relu'))
model.add(Convolution2D(16, 1, 1))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25)) #Cannot take float values

model.add(Convolution2D(32, 1, 1, border_mode='valid'))
model.add(Activation('relu'))
model.add(Convolution2D(32, 1, 1))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
# Note: Keras does automatic shape inference.
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(10))
model.add(Activation('softmax'))

model.fit(X_train, Y_train, batch_size=32, nb_epoch=1)

How can I input my images (I have them on my PC)? And how can I specify the Y-train?

如何输入我的图像(我的电脑上有它们)?以及如何指定 Y-train?

回答by dhinckley

This Keras blog post, Building powerful image classification models using very little data, is an excellent tutorial for training a model on images stored in directories. It also introduces the ImageDataGeneratorclass, which has the member function flow_from_directoryreferenced in @isaac-moore's answer. flow from directorycan be used train on images, where the directory structure is used to deduce the value of Y_train.

这篇 Keras 博客文章使用很少的数据构建强大的图像分类模型是一个很好的教程,用于在目录中存储的图像上训练模型。它还介绍了ImageDataGenerator类,该类具有flow_from_directory@isaac-moore 的答案中引用的成员函数。 flow from directory可以在图像上使用训练,其中目录结构用于推导 的值Y_train

The three python scripts that accompany the tutorial blog post can be found at the links below:

可以在以下链接中找到教程博客文章附带的三个 Python 脚本:

  1. classifier_from_little_data_script_1.py
  2. classifier_from_little_data_script_2.py
  3. classifier_from_little_data_script_3.py
  1. 分类器_from_little_data_script_1.py
  2. category_from_little_data_script_2.py
  3. 分类器_from_little_data_script_3.py

(Of course, these links are in the blog post itself, but the links are not centrally located.) Note that scripts 2 and 3 build on the output of the previous. Also, note that additional files will need to be downloaded from Kaggleand Github.

(当然,这些链接在博客文章中,但链接不在中心位置。)请注意,脚本 2 和 3 建立在前一个输出的基础上。另请注意,需要从KaggleGithub下载其他文件。

回答by isaac-moore

Create a folder for train and in the folder, create separate folders for the classes of images.

为火车创建一个文件夹,并在该文件夹中为图像类别创建单独的文件夹。

Access the images using

使用图像访问

  train_generator = train_datagen.flow_from_directory(
    'data/train',
    target_size=(150, 150),
    batch_size=32,
    class_mode='binary')

In reference to keras.io

参考keras.io

回答by Rob

In this repository you have an example:

在此存储库中,您有一个示例:

https://github.com/ZFTurbo/KAGGLE_DISTRACTED_DRIVER/blob/master/run_keras_simple.py

https://github.com/ZFTurbo/KAGGLE_DISTRACTED_DRIVER/blob/master/run_keras_simple.py

They have different folders, in every folder there is a different class of image. They load the images using OpenCV and they build arrays that contains the class of every image.

它们有不同的文件夹,在每个文件夹中都有不同类别的图像。他们使用 OpenCV 加载图像并构建包含每个图像类的数组。