-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcnn_train.py
93 lines (66 loc) · 2.33 KB
/
cnn_train.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""
Created on Tue dec 5 17:17:40 2023
@author: Mike
"""
#Part 1 : Building a CNN
#import Keras packages
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
import numpy as np
import scipy
# from keras.utils.vis_utils import plot_model
# Initializing the CNN
np.random.seed(1337)
classifier = Sequential()
classifier.add(Convolution2D(32, 3, 3, input_shape = (128, 128, 3), activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Convolution2D(16, 3, 3, activation = 'relu'))
classifier.add(MaxPooling2D(pool_size = (2, 2)))
# classifier.add(Convolution2D(8, 3, 3, activation = 'relu'))
# classifier.add(MaxPooling2D(pool_size = (2, 2)))
classifier.add(Flatten())
#hidden layer
classifier.add(Dense(units = 128, activation = 'relu'))
classifier.add(Dropout( 0.5))
classifier.add(Dropout(rate=0.5))
#output layer
classifier.add(Dense(units = 10, activation = 'softmax'))
classifier.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
print(classifier.summary())
#plot_model(classifier, show_shapes=True, to_file='PlantVillage_CNN.png')
#Part 2 - fitting the data set
from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
training_set = train_datagen.flow_from_directory(
'train',
target_size=(128, 128),
batch_size=64,
class_mode='categorical' )
label_map = (training_set.class_indices)
print(label_map)
test_set = test_datagen.flow_from_directory(
'val',
target_size=(128, 128),
batch_size=64,
class_mode='categorical')
classifier.fit(
training_set,
steps_per_epoch=20,
epochs=1000,
validation_data=test_set,
validation_steps=100)
# Evaluate the model on the test set
accuracy = classifier.evaluate_generator(test_set, steps=len(test_set))
print("Test Accuracy:", accuracy[1])
classifier.save_weights('keras_potato_trained_model_weights.h5')
print('Saved trained model as %s ' % 'keras_potato_trained_model_weights.h5')