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

2024/10/10 14:23:18

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 SGDimport numpy as npmodel = 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 valuesmodel.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?

Answer

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.

https://en.xdnf.cn/q/69884.html

Related Q&A

Fastest way to merge two deques

Exist a faster way to merge two deques than this?# a, b are two deques. The maximum length # of a is greater than the current length # of a plus the current length of bwhile len(b):a.append(b.poplef…

Python cannot find shared library in cron

My Python script runs well in the shell. However when I cron it (under my own account) it gives me the following error:/usr/local/bin/python: error while loading shared libraries: libpython2.7.so.1.0: …

Multiple async unit tests fail, but running them one by one will pass

I have two unit tests, if I run them one by one, they pass. If I run them at class level, one pass and the other one fails at response = await ac.post( with the error message: RuntimeError: Event loop…

Pyusb on Windows 7 cannot find any devices

So I installed Pyusb 1.0.0-alpha-1 Under Windows, I cannot get any handles to usb devices.>>> import usb.core >>> print usb.core.find() NoneI do have 1 usb device plugged in(idVendor=…

How to speed up nested for loops in Python

I have the following Python 2.7 code:listOfLists = [] for l1_index, l1 in enumerate(L1):list = []for l2 in L2:for l3_index,l3 in enumerate(L3):if (L4[l2-1] == l3):value = L5[l2-1] * l1[l3_index]list.ap…

Folium Search Plugin No Results for FeatureGroup

Im trying to add search functionality to a map Im generating in Python with Folium. I see there is a handy Search plugin available and able to implement it successfully and get it added to the map. Unf…

writing dictionary of dictionaries to .csv file in a particular format

I am generating a dictionary out of multiple .csv files and it looks like this (example):dtDict = {AV-IM-1-13991730: {6/1/2014 0:10: 0.96,6/1/2014 0:15: 0.92,6/1/2014 0:20: 0.97},AV-IM-1-13991731: {6/1…

How to import SSL certificates for Firefox with Selenium [in Python]?

Trying to find a way to install a particular SSL certificate in Firefox with Selenium, using the Python WebDriver and FirefoxProfile. We need to use our own, custom certificate which is stored in the …

Cell assignment of a 2-dimensional Matrix in Python, without numpy

Below is my script, which basically creates a zero matrix of 12x8 filled with 0. Then I want to fill it in, one by one. So lets say column 2 row 0 needs to be 5. How do I do that? The example below sh…

Fill matplotlib subplots by column, not row

By default, matplotlib subplots are filled by row, not by column. To clarify, the commandsplt.subplot(nrows=3, ncols=2, idx=2) plt.subplot(nrows=3, ncols=2, idx=3)first plot into the upper right plot o…