-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraining code.txt
More file actions
55 lines (43 loc) · 1.68 KB
/
Copy pathTraining code.txt
File metadata and controls
55 lines (43 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
# Step 1: Initialising the CNN
model = Sequential()
# Step 2: Convolution
model.add(Conv2D(32, (3, 3), input_shape = (50, 50, 3), activation = 'relu'))
# Step 3: Pooling
model.add(MaxPooling2D(pool_size = (2, 2)))
# Step 4: Second convolutional layer
model.add(Conv2D(32, (3, 3), activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2, 2)))
# Step 5: Flattening
model.add(Flatten())
# Step 6: Full connection
model.add(Dense(units = 128, activation = 'relu'))
model.add(Dense(units = 64, activation = 'relu'))
model.add(Dense(units = 32, activation = 'relu'))
model.add(Dense(units = 1, activation = 'sigmoid'))
# Step 7: Compiling the CNN
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Step 8: ImageDataGenerator
fromkeras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip = True)
# Step 9: Load the training Set
training_set = train_datagen.flow_from_directory('./Dataset/Train',
target_size = (50, 50),
batch_size = 32,
class_mode = 'binary')
# Step 10: Classifier Training
model.fit_generator(training_set,steps_per_epoch = 4000,epochs = 6,
validation_steps = 2000)
# Step 11: Convert the Model to json
model_json = model.to_json()
with open("./model.json","w") as json_file:
json_file.write(model_json)
# Step 12: Save the weights in a seperatefilemodel.save_weights("./model.h5")
print("Classifier trained Successfully!")