简体   繁体   English

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

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

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.我的图像是 200x100 JPG 图像,输出是 4 个元素的数组:汽车会向左、向右、停止或向前移动。 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 .因此,输出将使一个元素为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 型火车?

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.这篇 Keras 博客文章使用很少的数据构建强大的图像分类模型是一个很好的教程,用于在目录中存储的图像上训练模型。 It also introduces the ImageDataGenerator class, which has the member function flow_from_directory referenced in @isaac-moore's answer.它还引入了ImageDataGenerator类,该类具有 @isaac-moore 的答案中引用的成员函数flow_from_directory flow from directory can be used train on images, where the directory structure is used to deduce the value of Y_train . flow from directory可以在图像上使用 train ,其中目录结构用于推导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 分类器_from_little_data_script_1.py
  2. classifier_from_little_data_script_2.py category_from_little_data_script_2.py
  3. classifier_from_little_data_script_3.py 分类器_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. (当然,这些链接在博客文章中,但链接不在中心位置。)请注意,脚本 2 和 3 建立在前一个输出的基础上。 Also, note that additional files will need to be downloaded from Kaggle and Github .另请注意,需要从KaggleGithub下载其他文件。

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

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 加载图像并构建包含每个图像类的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在主数据集中找到 X_train 索引? - How can I find X_train indexes in the main dataset? 如何为 LSTM keras 重塑 X_train 和 y_train - How to reshape X_train and y_train for LSTM keras 更常见的是:X_train 还是 x_train? - What is more common: X_train or x_train? Keras fit_generator(datagen.flow(X_train))函数拍摄的图片少于X_train的图片吗? - Keras fit_generator(datagen.flow(X_train)) function takes less pictures than X_train have? 如何绘制超过24,000张图像并将其保存为视频? - How to plot more than 24 thousand images and save them as a video? 如何解决问题以重塑从 Python 中的 x_train 派生的图像中的过程? - How can I fix the issue to reshape process in image derived from x_train in Python? Keras中的x_train和x_test有什么区别? - What is the difference between x_train and x_test in Keras? Keras-使用y_preds和y_true而不是X_train训练模型 - Keras- training the model using y_preds and y_true and not X_train 我如何在Tensorflow中准备自己的数据集(数千张图片)? - How i can prepare my own dataset (thousand of images) in Tensorflow? 如何将 tf.data.Dataset 拆分为 x_train、y_train、x_test、y_test for keras - how to split up tf.data.Dataset into x_train, y_train, x_test, y_test for keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM