-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model02.py
118 lines (86 loc) · 2.39 KB
/
Model02.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
## Imports
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers
from tensorflow.keras import models
from tensorflow.keras import regularizers
## Data Generators for train/test/val
train_data_generator = ImageDataGenerator(
rescale=1./255
)
test_data_generator = ImageDataGenerator(
rescale=1./255
)
validation_data_generator = ImageDataGenerator(
rescale=1./255
)
## File import
train_data = train_data_generator.flow_from_directory(
"data/training",
target_size = (150, 150),
batch_size = 176,
class_mode = "binary",
color_mode = "grayscale",
shuffle = True,
seed=1984)
test_data = test_data_generator.flow_from_directory(
"data/testing",
target_size = (150, 150),
batch_size = 22,
class_mode = "binary",
color_mode = "grayscale",
shuffle = True,
seed = 1963
)
validation_data = validation_data_generator.flow_from_directory(
"data/validation",
target_size = (150, 150),
batch_size = 22,
class_mode = "binary",
color_mode = "grayscale",
shuffle = True,
seed = 1989
)
## Task 3 Constructing the Network
model = models.Sequential()
# Input Layer
model.add(layers.Conv2D(
32,
(3, 3),
activation="relu",
input_shape=(150, 150, 1),
data_format="channels_last"))
# Feature Extraction
model.add(layers.Dense(64, activation="relu"))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation="relu"))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation="relu", kernel_regularizer=regularizers.l2(l2=1e-4)))
# Classification
model.add(layers.Flatten())
model.add(layers.Dense(64, activation="relu"))
model.add(layers.Dense(1, activation="sigmoid"))
# Compile Model
model.compile(
optimizer='Adam',
loss='BinaryCrossentropy',
metrics=['accuracy'])
## Fit Model
model_history = model.fit(
train_data,
steps_per_epoch = 10,
epochs = 10,
validation_data = validation_data,
validation_steps = 10
)
model.evaluate(test_data)
## Save Model
model.save("model02.h5")
## Evaluate Current model
model.evaluate(test_data)
## Evaluate Load Model
#model_load = keras.models.load_model('j_model.h5')
#model_load.evaluate(test_data)
#model_load.evaluate(validation_data)