-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_class.py
152 lines (135 loc) · 4.99 KB
/
model_class.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
144
145
146
147
148
149
150
151
152
# Convolutional neural network model class.
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Conv2D, AveragePooling2D, Dense, Dropout, Flatten, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import sys
sys.path.insert(1, '../../')
from dev_modules.vcs_params import params_dataset
from dev_modules.vcs_params import params_model
import cv2
import numpy as np
from src.Lite_handle import Lite_handler
class fp_CNN_MCU():
"""
Floating point CNN for IoT and MCU devices.
"""
def __init__(self):
"""
Construct net.
"""
self.model_classes = ['Usable', 'Defective']
self.threshold_decision = params_model.THRESHOLD_DECISION
self.fp_model = Sequential()
# 128.
self.fp_model.add(Conv2D(
filters=params_model.L0_N_FILTER,
kernel_size=(params_model.L0_KERNEL_SIZE,
params_model.L0_KERNEL_SIZE),
strides=1,
activation='relu', padding='same',
input_shape=params_dataset.IMAGE_SIZE + (1,)))
self.fp_model.add(AveragePooling2D(
pool_size=(params_model.POOL_KERNEL_SIZE,
params_model.POOL_KERNEL_SIZE),
strides=params_model.POOL_STRIDE))
# 64
self.fp_model.add(Conv2D(
filters=params_model.L1_N_FILTER,
kernel_size=(params_model.L1_KERNEL_SIZE,
params_model.L1_KERNEL_SIZE),
strides=1,
activation='relu', padding='same'))
self.fp_model.add(AveragePooling2D(
pool_size=(params_model.POOL_KERNEL_SIZE,
params_model.POOL_KERNEL_SIZE),
strides=params_model.POOL_STRIDE))
# 32
self.fp_model.add(Conv2D(
filters=params_model.L2_N_FILTER,
kernel_size=(params_model.L2_KERNEL_SIZE,
params_model.L2_KERNEL_SIZE),
strides=1,
activation='relu', padding='same'))
self.fp_model.add(AveragePooling2D(
pool_size=(params_model.POOL_KERNEL_SIZE,
params_model.POOL_KERNEL_SIZE),
strides=params_model.POOL_STRIDE))
self.fp_model.add(Dropout(.05))
# 16
self.fp_model.add(Conv2D(
filters=params_model.L3_N_FILTER,
kernel_size=(params_model.L3_KERNEL_SIZE,
params_model.L3_KERNEL_SIZE),
strides=1,
activation='relu', padding='same'))
self.fp_model.add(AveragePooling2D(
pool_size=(params_model.POOL_KERNEL_SIZE,
params_model.POOL_KERNEL_SIZE),
strides=params_model.POOL_STRIDE))
self.fp_model.add(Dropout(.05))
# 8
self.fp_model.add(Conv2D(
filters=params_model.L4_N_FILTER,
kernel_size=(params_model.L4_KERNEL_SIZE,
params_model.L4_KERNEL_SIZE),
strides=1,
activation='relu', padding='same'))
self.fp_model.add(Dropout(.05))
# Output
self.fp_model.add(Flatten())
self.fp_model.add(Dense(1, activation='sigmoid'))
def get_model(self) -> Sequential:
"""
Return the fp model structure.
"""
return self.fp_model
def load_model(self, path: str) -> tf.keras.models.Sequential:
"""
Load trained model.
"""
self.loaded_model = tf.keras.models.load_model(path)
return self.loaded_model
def fp_predict(self, x_input: object) -> tf.Tensor:
"""
Abstraction to inference in floating point model.
"""
return self.loaded_model.predict(x_input)
class qt_CNN_MCU():
"""
Quantized CNN for IoT and MCU devices.
"""
def __init__(self):
"""
Init lite handler class.
"""
self.lite_h = Lite_handler()
def load_qt_model(self,
model_path: str) -> bytes:
"""
Load quantized model in "model_path".
Return quantized model loaded.
"""
with open(model_path, 'rb') as model_file:
self.qt_model = model_file.read()
self.lite_h.load_model(self.qt_model)
return self.qt_model
def get_model(self,
fp_model: Sequential,
dataset: ImageDataGenerator,
savedir: str) -> bytes:
"""
Return the qt model build.
"""
self.qt_model = self.lite_h.build_quantized_model(fp_model, dataset, savedir)
return self.qt_model
def qt_predict(self, dataset: ImageDataGenerator) -> tf.Tensor:
"""
Abstraction to inference in quantized model.
"""
y_pred = list()
for file in dataset.filepaths:
sample = cv2.imread(file, cv2.IMREAD_GRAYSCALE).reshape(-1, 128, 128, 1)
y_pred.append(self.lite_h.predict_tflite(sample)[0])
y_pred = np.array(y_pred)
return y_pred