使用Keras,如何输入图像的X_train(一千多个图像)? [英] Using Keras, how can I input an X_train of images (more than a thousand images)?

查看:72
本文介绍了使用Keras,如何输入图像的X_train(一千多个图像)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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)

如何输入图像(我的计算机上有图像)?以及如何指定Y轴?

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

推荐答案

此Keras博客文章

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 ImageDataGenerator class, which has the member function flow_from_directory referenced in @isaac-moore's answer. flow from directory can be used train on images, where the directory structure is used to deduce the value of Y_train.

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

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

  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. classifier_from_little_data_script_1.py
  2. classifier_from_little_data_script_2.py
  3. classifier_from_little_data_script_3.py

(当然,这些链接位于博客文章本身中,但这些链接并不位于中央.)请注意,脚本2和3建立在前一个脚本的输出上.另外,请注意,还需要从 Kaggle Github .

(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 Kaggle and Github.

这篇关于使用Keras,如何输入图像的X_train(一千多个图像)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆