Skip to content

Commit 2b8ca2d

Browse files
authored
Add files via upload
0 parents  commit 2b8ca2d

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

cnn.py

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Convolutional Neural Network
2+
3+
# Installing Theano
4+
# pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
5+
6+
# Installing Tensorflow
7+
# Install Tensorflow from the website: https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html
8+
9+
# Installing Keras
10+
# pip install --upgrade keras
11+
12+
# Part 1 - Building the CNN
13+
14+
# Importing the Keras libraries and packages
15+
from keras.models import Sequential
16+
from keras.layers import Convolution2D
17+
from keras.layers import MaxPooling2D
18+
from keras.layers import Flatten
19+
from keras.layers import Dense
20+
21+
# Initialising the CNN
22+
classifier = Sequential()
23+
24+
# Step 1 - Convolution
25+
classifier.add(Convolution2D(32, 3, 3, input_shape = (64, 64, 3), activation = 'relu'))
26+
27+
# Step 2 - Pooling
28+
classifier.add(MaxPooling2D(pool_size = (2, 2)))
29+
30+
# Adding a second convolutional layer
31+
classifier.add(Convolution2D(32, 3, 3, activation = 'relu'))
32+
classifier.add(MaxPooling2D(pool_size = (2, 2)))
33+
34+
# Step 3 - Flattening
35+
classifier.add(Flatten())
36+
37+
# Step 4 - Full connection
38+
classifier.add(Dense(output_dim = 128, activation = 'relu'))
39+
classifier.add(Dense(output_dim = 1, activation = 'sigmoid'))
40+
41+
# Compiling the CNN
42+
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
43+
44+
# Part 2 - Fitting the CNN to the images
45+
46+
from keras.preprocessing.image import ImageDataGenerator
47+
48+
train_datagen = ImageDataGenerator(rescale = 1./255,
49+
shear_range = 0.2,
50+
zoom_range = 0.2,
51+
horizontal_flip = True)
52+
53+
test_datagen = ImageDataGenerator(rescale = 1./255)
54+
55+
training_set = train_datagen.flow_from_directory('test_dataset/training_set',
56+
target_size = (64, 64),
57+
batch_size = 32,
58+
class_mode = 'binary')
59+
60+
test_set = test_datagen.flow_from_directory('test_dataset/test_set',
61+
target_size = (64, 64),
62+
batch_size = 32,
63+
class_mode = 'binary')
64+
65+
classifier.fit_generator(training_set,
66+
samples_per_epoch = 8000,
67+
nb_epoch = 25,
68+
validation_data = test_set,
69+
nb_val_samples = 2000)

0 commit comments

Comments
 (0)