-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
143 lines (113 loc) · 3.9 KB
/
main.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import argparse
import neural_network as nn
import neural_network.supply as attr
from neural_network import Config
from neural_network.board import Chart, FileInput
from neural_network.core.padding import Padding
from neural_network.core.image_processor import ImageProcessor
from neural_network.console import Commands
IMAGE_SIZE = (50, 50)
IMAGE_CHANNELS = 3
BATCH_SIZE = 75
EPOCHS = 25
def create_configuration():
config = Config()
config.set_processor(
ImageProcessor(
base_dir="./data/breast-histopathology-images",
image_size=IMAGE_SIZE,
batch_size=BATCH_SIZE,
split_ratios=(0.75, 0.15, 0.10),
shuffle=True,
augmentation=True,
augmentation_params={
'rotation': 30,
'zoom': 0.2,
'horizontal_flip': 0.5,
'vertical_flip': 0.5,
'brightness': 0.5,
'contrast': 0.5,
'shear': 0.3
}
)
)
config.driver('cpu')
config.set_global_optimizer(attr.Adam(learning_rate=0.001))
config.with_cache(path='./data/cache/model.pkl')
config.padding_type(Padding.SAME)
config.loss_function(attr.CrossEntropyLoss())
# ---- Convolutional Layers ----
kernel = config.add_kernel(number=32, shape=(3, 3), stride=1)
kernel.initializer(attr.He())
kernel.activation(attr.Relu())
kernel.batch_normalization()
kernel.max_pooling(shape=(2, 2), stride=2)
kernel = config.add_kernel(number=64, shape=(3, 3), stride=1)
kernel.initializer(attr.He())
kernel.activation(attr.Relu())
kernel.batch_normalization()
kernel.max_pooling(shape=(3, 3), stride=2)
kernel = config.add_kernel(number=128, shape=(3, 3), stride=1)
kernel.initializer(attr.He())
kernel.activation(attr.Relu())
kernel.batch_normalization()
kernel.max_pooling(shape=(3, 3), stride=2)
kernel = config.add_kernel(number=128, shape=(3, 3), stride=1)
kernel.initializer(attr.He())
kernel.activation(attr.Relu())
kernel.batch_normalization()
kernel.max_pooling(shape=(3, 3), stride=2)
config.flatten()
dense = config.dense()
layer1 = dense.add_layer(size=128)
layer1.dropout(0.3)
layer1.initializer(attr.He())
layer1.activation(attr.Relu())
# Output Layer
output = dense.add_layer(size=2)
output.activation(attr.Softmax())
output.initializer(attr.Xavier())
return config
def create_app(config: Config) -> nn.App:
return nn.app.App(
model=config.new_model(),
board=FileInput(
title="Breast Cancer Recognition",
img_resize=IMAGE_SIZE,
)
)
def train_model(app: nn.App, plot: bool):
app.model().set_training_mode()
app.model().get_trainer().train(
epochs=EPOCHS,
plot=Chart().plot_metrics if plot else None,
scheduler=attr.ReduceLROnPlateau(factor=0.2, patience=2, min_lr=1e-7)
)
def validate_model(app: nn.App):
app.draw()
app.model().set_test_mode()
app.board().set_handler(handler=app.predict_image)
app.board().set_labels(path_json="./labels/breast_cancer.json")
app.loop()
def test_model(app: nn.App, plot: bool):
app.model().set_test_mode()
app.model().get_tester().test(plot=Chart().plot_metrics if plot else None)
def main():
commands = Commands(argparse.ArgumentParser(description="Train or test the model"))
commands.register()
args = commands.get_args()
config = create_configuration()
if args.clear_cache:
config.restore_initialization_cache()
if args.no_cache:
config.with_no_cache()
app = create_app(config)
modes = {
"train": lambda: train_model(app, args.plot),
"validate": lambda: validate_model(app),
"test": lambda: test_model(app, args.plot),
}
action = modes.get(args.mode)
action()
if __name__ == "__main__":
main()