-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcenter.py
69 lines (57 loc) · 2.56 KB
/
center.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
import os
# Make TensorFlow log less verbose
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
# Do not consume all GPU at once
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"] = "True"
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator as data_augment
from keras.models import load_model
from keras.layers import Input
#data augmetation
data_generate_training = data_augment (rescale=1./255,
shear_range = 0.2,
zoom_range = 0.2,
fill_mode = "nearest",
horizontal_flip = True,
width_shift_range = 0.2,
height_shift_range = 0.2,
validation_split = 0.15)
# the path for the aggregator should be something like "/home/user/ddd/"
datadir= "REPLACE_WITH_THE_PATH_WHERE_YOU_EXTRACTED_THE_DDD_DATASET"
#data split and loading
traind = data_generate_training.flow_from_directory(datadir,
target_size = (227, 227),
seed = 123,
batch_size = 32,
subset = "training")
testd = data_generate_training.flow_from_directory(datadir,
target_size = (227, 227),
seed = 123,
batch_size = 32,
subset = "validation")
#Building Model
CNNmodel = keras.Sequential([
keras.layers.Conv2D(32, (3, 3), input_shape=(227, 227, 3), activation='relu'),
keras.layers.BatchNormalization(),
keras.layers.MaxPooling2D((2,2)),
keras.layers.Dropout(0.5),
keras.layers.Conv2D(64, (3, 3), activation='relu'),
keras.layers.BatchNormalization(),
keras.layers.MaxPooling2D((2,2)),
keras.layers.Dropout(0.5),
keras.layers.Conv2D(128, (3, 3), activation='relu'),
keras.layers.BatchNormalization(),
keras.layers.MaxPooling2D((2,2)),
keras.layers.Dropout(0.5),
keras.layers.Flatten(),
keras.layers.Dense(256, activation='relu'),
keras.layers.Dropout(0.5),
keras.layers.Dense(128, activation = 'relu', kernel_regularizer='l1'),
keras.layers.Dense(2, activation = 'sigmoid')
])
#compile model
CNNmodel.compile(optimizer='adam',
loss="binary_crossentropy",
metrics=['accuracy', keras.metrics.Precision(), keras.metrics.Recall()])
#train
history = CNNmodel.fit(traind, epochs = 100, validation_data = testd)