-
Notifications
You must be signed in to change notification settings - Fork 0
/
active_learner.py
163 lines (130 loc) · 4.35 KB
/
active_learner.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
import numpy as np
import matplotlib.pyplot as plt
from modAL.models import ActiveLearner
from tensorflow.keras.wrappers.scikit_learn import KerasClassifier
from keras.preprocessing.image import save_img
import os
from query import Query
class ActiveLearner(ActiveLearner):
def __init__(
self,
build_fn,
query_strategy: Query,
X_training,
y_training,
**fit_kwargs,
) -> None:
'''
initiate Active learning instance with Deep Learning classifier,
AL technic, X and Y training data
'''
super().__init__(
KerasClassifier(build_fn),
query_strategy,
X_training,
y_training,
**fit_kwargs,
)
def loop(
self,
X_test,
y_test,
X_unlabeled,
accuracy_goal,
):
# accuracy of model with initialize images
model_accuracy = self.score(X_test, y_test, verbose=0)
print("\nAccuracy after query {n}: {acc:0.4f}".format(n=0, acc=model_accuracy))
accuracy_values = [model_accuracy]
i=1
while (model_accuracy<accuracy_goal):
query_idx, _ = self.query(X_unlabeled)
new_y = [self.label(X_unlabeled, indice) for indice in query_idx]
self.train(X_unlabeled[query_idx], new_y)
model_accuracy = self.score(X_test, y_test, verbose=0)
accuracy_values.append(model_accuracy)
self.save(X_unlabeled[query_idx], new_y)
X_unlabeled = np.delete(X_unlabeled, query_idx, axis=0)
print(
"\nAccuracy after query {n}: {acc:0.4f}".format(
n=i, acc=model_accuracy
)
)
i=i+1
return accuracy_values
def train(
self,
images,
labels,
epochs=10,
batch_size=32,
):
self.teach(
images,
labels,
epochs=epochs,
batch_size=batch_size,
)
def label(
self,
images,
indice,
):
# labelling images
plt.imshow(images[indice])
plt.show()
print(
"Informativa = 0 / Nao Informativa =1 ? 2 para rever os frames anteriores"
)
new_y = int(input())
# verificar se pretende ver as 10 anteriores e guarda a anotação após visualizar as anteriores
if new_y == 2:
self.get_previous_frames(images, indice)
new_y = self.label(images, indice)
return new_y
def show_images(
self,
images,
cols=1,
titles=None,
):
assert (titles is None) or (len(images) == len(titles))
n_images = len(images)
if titles is None:
titles = ["Image (%d)" % i for i in range(1, n_images + 1)]
fig = plt.figure()
for n, (image, title) in enumerate(zip(images, titles)):
a = fig.add_subplot(cols, np.ceil(n_images / float(cols)), n + 1)
if image.ndim == 2:
plt.gray()
plt.imshow(image)
a.set_title(title)
fig.set_size_inches(np.array(fig.get_size_inches()) * n_images)
plt.show()
def get_previous_frames(
self,
X_unlabeled,
indice,
):
if indice > 10:
imagens = []
for imagem in zip(range(indice - 10, indice)):
imagens.append(X_unlabeled[imagem])
self.show_images(imagens)
label = int(input())
return label
def save(self,images,labels):
self.estimator.model.save_weights("model.h5")
list = os.listdir('images/informativa') # dir is your directory path
number_files = len(list)
list = os.listdir('images/nao_informativa') # dir is your directory path
number_files += len(list)
if not os.path.exists('images/informativa'):
os.makedirs('images/informativa')
if not os.path.exists('images/nao_informativa'):
os.makedirs('images/nao_informativa')
for x,y in zip(images,labels):
number_files += 1
if(y==0) : file_path=('images/informativa/' + str(number_files) + '.png')
if(y==1) : file_path=('images/nao_informativa/' + str(number_files) + '.png')
save_img(file_path, x)