-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_and_test.py
200 lines (144 loc) · 4.86 KB
/
train_and_test.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import os
import numpy as np
import tensorflow_datasets as tfds
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from akida_models import mobilenet_edge_imagenet_pretrained
from cnn2snn import convert
from akida import Model, FullyConnected
MODEL_FBZ = "models/edge_learning_example.fbz"
HORSES = [
"img/horse1.jpeg", # The model will learn this image of a horse in Step1
"img/horse2.jpeg",
"img/horse3.jpeg",
"img/horse4.jpeg",
"img/horse5.jpeg", # The script will test this image in Step2
"img/horse6.jpeg",
]
DOGS = [
"img/dog1.jpeg", # The model will learn this image of a dog in Step1
"img/dog2.jpg",
"img/dog3.jpeg",
"img/dog4.jpeg",
"img/dog5.jpeg", # The script will test this image in Step2
"img/dog6.jpeg",
]
BATS = [
"img/bat1.jpg", # The model will learn this image of a bat in Step3
"img/bat2.jpg",
"img/bat3.jpg", # The model will learn this image of a bat in Step3
"img/bat4.jpg",
"img/bat5.jpg",
"img/bat6.jpg", # The script will test this image of a bat in Step4
]
LABELS = {0: "unknown", 1: "Horse", 2: "Dog", 3: "Bat"}
NUM_CLASSES = 10
NUM_NEURONS_PER_CLASS = 1
NUM_WEIGHTS = 350
TARGET_WIDTH = 224
TARGET_HEIGHT = 224
"""
Change this step as you progress through this tutorial
"""
STEP = 1
"""
Step 1:
- Initialise a pretrained ImageNet model
- Remove the last layer of the ImageNet network
- Add a FullyConnected Akida layer as the last layer of the network
- Learn a 'Dog'
"""
if STEP == 1:
ds, ds_info = tfds.load("coil100:2.*.*", split="train", with_info=True)
model_keras = mobilenet_edge_imagenet_pretrained()
# # Convert it to akida
model_ak = convert(model_keras, input_scaling=(128, 128))
# remove the last layer of network, replace with Akida learning layer
model_ak.pop_layer()
layer_fc = FullyConnected(
name="akida_edge_layer",
num_neurons=NUM_CLASSES * NUM_NEURONS_PER_CLASS,
activations_enabled=False,
)
model_ak.add(layer_fc)
model_ak.compile(
num_weights=NUM_WEIGHTS, num_classes=NUM_CLASSES, learning_competition=0.1
)
# Learn a horse
image = load_img(
HORSES[0], target_size=(TARGET_WIDTH, TARGET_HEIGHT), color_mode="rgb"
)
input_arr = img_to_array(image)
input_arr = np.array([input_arr], dtype="uint8")
model_ak.fit(input_arr, 1)
# Learn a dog
image = load_img(
DOGS[2], target_size=(TARGET_WIDTH, TARGET_HEIGHT), color_mode="rgb"
)
input_arr = img_to_array(image)
input_arr = np.array([input_arr], dtype="uint8")
model_ak.fit(input_arr, 2)
# output the saved akida model
model_file = os.path.join("", MODEL_FBZ)
model_ak.save(model_file)
"""
Step 2:
- Test the network by trying to identify a HORSE and a DOG
* Spend some time understanding where you may need more than 1 shot, eg. dogs are weird looking
"""
if STEP == 2:
model_ak = Model(filename=MODEL_FBZ)
# image to test
image = load_img(
HORSES[3], target_size=(TARGET_WIDTH, TARGET_HEIGHT), color_mode="rgb"
)
input_arr = img_to_array(image)
input_arr = np.array([input_arr], dtype="uint8")
predictions = model_ak.predict(input_arr, num_classes=NUM_CLASSES)
print("should be class 1 (horse)", predictions[0])
# image to test
image = load_img(
DOGS[5], target_size=(TARGET_WIDTH, TARGET_HEIGHT), color_mode="rgb"
)
input_arr = img_to_array(image)
input_arr = np.array([input_arr], dtype="uint8")
predictions = model_ak.predict(input_arr, num_classes=NUM_CLASSES)
print("should be class 2 (dog)", predictions[0])
"""
Step 3:
- Learn a new class, BAT, on output neuron 3
- Add another BAT image to strengthen the classification
- Save the updated Akida model
"""
if STEP == 3:
model_ak = Model(filename=MODEL_FBZ)
# Learn a bat
image = load_img(
BATS[0], target_size=(TARGET_WIDTH, TARGET_HEIGHT), color_mode="rgb"
)
input_arr = img_to_array(image)
input_arr = np.array([input_arr], dtype="uint8")
model_ak.fit(input_arr, 3)
# Learn another bat
image = load_img(
BATS[2], target_size=(TARGET_WIDTH, TARGET_HEIGHT), color_mode="rgb"
)
input_arr = img_to_array(image)
input_arr = np.array([input_arr], dtype="uint8")
model_ak.fit(input_arr, 3)
# output the saved akida model
model_file = os.path.join("", MODEL_FBZ)
model_ak.save(model_file)
"""
Step 4:
- Identify the BAT in random images using the LABELS dict
"""
if STEP == 4:
model_ak = Model(filename=MODEL_FBZ)
# recognise a bat
image = load_img(
BATS[5], target_size=(TARGET_WIDTH, TARGET_HEIGHT), color_mode="rgb"
)
input_arr = img_to_array(image)
input_arr = np.array([input_arr], dtype="uint8")
predictions = model_ak.predict(input_arr, num_classes=NUM_CLASSES)
print("the creature is a:", LABELS[predictions[0]])