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

六眼飞鱼酱① 提交于 2020-01-12 02:37:52

问题


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)

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


回答1:


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.

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

(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.




回答2:


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




回答3:


In this repository you have an example:

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 buld an array that contains the class of every image.



来源:https://stackoverflow.com/questions/40466303/using-keras-how-can-i-input-an-x-train-of-images-more-than-a-thousand-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!