1
+ import numpy as np
2
+ from keras .models import Sequential
3
+ from keras .layers import Dense , Conv2D , MaxPooling2D , Flatten
4
+
5
+ # Initializing the CNN
6
+ classifier = Sequential ()
7
+
8
+ # Step 1 - Convolution
9
+ classifier .add (Conv2D (32 , (3 , 3 ), input_shape = (64 , 64 , 3 ), activation = 'relu' ))
10
+
11
+ # Step 2 - Pooling
12
+ classifier .add (MaxPooling2D (pool_size = (2 , 2 )))
13
+
14
+ # Adding a second convolutional layer
15
+ classifier .add (Conv2D (32 , (3 , 3 ), activation = 'relu' ))
16
+ classifier .add (MaxPooling2D (pool_size = (2 , 2 )))
17
+
18
+ # Step 3 - Flattening
19
+ classifier .add (Flatten ())
20
+
21
+ # Step 4 - Full connection
22
+ classifier .add (Dense (units = 128 , activation = 'relu' ))
23
+ classifier .add (Dense (units = 1 , activation = 'sigmoid' ))
24
+
25
+ # Compiling the CNN
26
+ classifier .compile (optimizer = 'adam' , loss = 'binary_crossentropy' , metrics = ['accuracy' ])
27
+
28
+ # Fitting the CNN to the images
29
+
30
+ from keras .preprocessing .image import ImageDataGenerator
31
+
32
+ train_datagen = ImageDataGenerator (rescale = 1. / 255 ,
33
+ shear_range = 0.2 ,
34
+ zoom_range = 0.2 ,
35
+ horizontal_flip = True )
36
+
37
+ test_datagen = ImageDataGenerator (rescale = 1. / 255 )
38
+
39
+ training_set = train_datagen .flow_from_directory ('dataset/training_set' ,
40
+ target_size = (64 , 64 ),
41
+ batch_size = 32 ,
42
+ class_mode = 'binary' )
43
+
44
+ test_set = test_datagen .flow_from_directory ('dataset/test_set' ,
45
+ target_size = (64 , 64 ),
46
+ batch_size = 32 ,
47
+ class_mode = 'binary' )
48
+
49
+ classifier .fit_generator (training_set ,
50
+ steps_per_epoch = 8000 ,
51
+ epochs = 25 ,
52
+ validation_data = test_set ,
53
+ validation_steps = 2000 )
0 commit comments