Skip to content

Commit 54014db

Browse files
authored
Python image classification
Image classification is a method to classify the images into their respective category classes using some method like : Training a small network from scratch Fine tuning the top layers of the model using VGG16 Let’s discuss how to train model from scratch and classify the data containing cars and planes. Train Data : Train data contains the 200 images of each cars and planes i.e. total their are 400 images in the training dataset Test Data : Test data contains 50 images of each cars and planes i.e. total their are 100 images in the test dataset Conv2D is the layer to convolve the image into multiple images Activation is the activation function. MaxPooling2D is used to max pool the value from the given size matrix and same is used for the next 2 layers. then, Flatten is used to flatten the dimensions of the image obtained after convolving it. Dense is used to make this a fully connected model and is the hidden layer. Dropout is used to avoid overfitting on the dataset. Dense is the output layer contains only one neuron which decide to which category image belongs.
1 parent f581e7e commit 54014db

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

‎python_image_classification‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
from keras.preprocessing.image import ImageDataGenerator
2+
from keras.models import Sequential
3+
from keras.layers import Conv2D, MaxPooling2D
4+
from keras.layers import Activation, Dropout, Flatten, Dense
5+
from keras import backend as K
6+
7+
8+
img_width, img_height = 224, 224
9+
10+
train_data_dir = 'v_data/train'
11+
validation_data_dir = 'v_data/test'
12+
nb_train_samples = 400
13+
nb_validation_samples = 100
14+
epochs = 10
15+
batch_size = 16
16+
17+
if K.image_data_format() == 'channels_first':
18+
input_shape = (3, img_width, img_height)
19+
else:
20+
input_shape = (img_width, img_height, 3)
21+
22+
model = Sequential()
23+
model.add(Conv2D(32, (2, 2), input_shape = input_shape))
24+
model.add(Activation('relu'))
25+
model.add(MaxPooling2D(pool_size =(2, 2)))
26+
27+
model.add(Conv2D(32, (2, 2)))
28+
model.add(Activation('relu'))
29+
model.add(MaxPooling2D(pool_size =(2, 2)))
30+
31+
model.add(Conv2D(64, (2, 2)))
32+
model.add(Activation('relu'))
33+
model.add(MaxPooling2D(pool_size =(2, 2)))
34+
35+
model.add(Flatten())
36+
model.add(Dense(64))
37+
model.add(Activation('relu'))
38+
model.add(Dropout(0.5))
39+
model.add(Dense(1))
40+
model.add(Activation('sigmoid'))
41+
42+
model.compile(loss ='binary_crossentropy',
43+
optimizer ='rmsprop',
44+
metrics =['accuracy'])
45+
46+
train_datagen = ImageDataGenerator(
47+
rescale = 1. / 255,
48+
shear_range = 0.2,
49+
zoom_range = 0.2,
50+
horizontal_flip = True)
51+
52+
test_datagen = ImageDataGenerator(rescale = 1. / 255)
53+
54+
train_generator = train_datagen.flow_from_directory(train_data_dir,
55+
target_size =(img_width, img_height),
56+
batch_size = batch_size, class_mode ='binary')
57+
58+
validation_generator = test_datagen.flow_from_directory(
59+
validation_data_dir,
60+
target_size =(img_width, img_height),
61+
batch_size = batch_size, class_mode ='binary')
62+
63+
model.fit_generator(train_generator,
64+
steps_per_epoch = nb_train_samples // batch_size,
65+
epochs = epochs, validation_data = validation_generator,
66+
validation_steps = nb_validation_samples // batch_size)
67+
68+
model.save_weights('model_saved.h5')

0 commit comments

Comments
 (0)