forked from LachubCz/pero-quality-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparing_classifier.py
178 lines (130 loc) · 6.17 KB
/
comparing_classifier.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
import os
import argparse
import cv2
import pickle
import numpy as np
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import func
from app.db import Base, User, Annotation, Crop, Page, Record, Set, RecordCrop
from networks import get_network, get_convolution_part
def get_args():
"""
method for parsing of arguments
"""
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--model_name", action="store", type=str, required=True)
args = parser.parse_args()
return args
if __name__ == "__main__":
engine = create_engine('sqlite:///database.sqlite3',
convert_unicode=True,
connect_args={'check_same_thread': False})
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base.query = db_session.query_property()
Base.metadata.create_all(bind=engine)
records_set_1 = Record.query.filter(Record.set_id == 1).all()
records_set_3 = Record.query.filter(Record.set_id == 3).all()
records_set_5 = Record.query.filter(Record.set_id == 5).all()
records_set_6 = Record.query.filter(Record.set_id == 6).all()
records_set_10 = Record.query.filter(Record.set_id == 10).all()
records_set_11 = Record.query.filter(Record.set_id == 11).all()
merged_records = records_set_1 + records_set_3 + records_set_5 + records_set_6 + records_set_10 + records_set_11
not_empty_records = []
labels = []
for i, item in enumerate(merged_records):
if len(item.annotations) > 0:
_01 = 0
_10 = 0
for e, elem in enumerate(item.annotations):
if elem.annotation == "01":
_01 += 1
elif elem.annotation == "10":
_10 += 1
labels.append([_01 / (_01 + _10), _10 / (_01 + _10)])
not_empty_records.append(item.id)
crops = []
for i, item in enumerate(not_empty_records):
record = RecordCrop.query.filter(RecordCrop.record_id == item).order_by(RecordCrop.order).all()
crops.append([record[0].crop_id, record[1].crop_id])
print(len(crops), len(labels))
tst = int(len(crops)/100)*10
trn_crops = crops[tst:]
trn_labels = labels[tst:]
tst_crops = crops[:tst]
tst_labels = labels[:tst]
print(len(trn_crops), len(tst_crops))
args = get_args()
classifier, conv, size = get_network(args.model_name)
path = './app/static/crops'
episodes = 1000
minibatch_size = 128
highest_val_acc = 0
for i in range(episodes):
indexes_trn = np.random.randint(low=0, high=len(trn_crops), size=minibatch_size)
indexes_tst = np.random.randint(low=0, high=len(tst_crops), size=int(minibatch_size/2))
image_batch_1_trn = []
image_batch_2_trn = []
for _, item in enumerate(indexes_trn):
image = cv2.imread(os.path.join(path, str(trn_crops[item][0])+".jpg"))
max_width = image.shape[1] - size
max_height = image.shape[0] - size
width = np.random.randint(max_width, size=1)
height = np.random.randint(max_height, size=1)
image = image[height[0]:height[0]+size, width[0]:width[0]+size]
order = np.random.randint(2, size=1)[0]
if order == 1:
image_batch_1_trn.append(1 - image/255.0)
else:
image_batch_1_trn.append(image/255.0)
########################################
image = cv2.imread(os.path.join(path, str(trn_crops[item][1])+".jpg"))
max_width = image.shape[1] - size
max_height = image.shape[0] - size
width = np.random.randint(max_width, size=1)
height = np.random.randint(max_height, size=1)
image = image[height[0]:height[0]+size, width[0]:width[0]+size]
order = np.random.randint(2, size=1)[0]
if order == 1:
image_batch_2_trn.append(1 - image/255.0)
else:
image_batch_2_trn.append(image/255.0)
image_batch_1_tst = []
image_batch_2_tst = []
for _, item in enumerate(indexes_tst):
image = cv2.imread(os.path.join(path, str(tst_crops[item][0])+".jpg"))
max_width = image.shape[1] - size
max_height = image.shape[0] - size
width = np.random.randint(max_width, size=1)
height = np.random.randint(max_height, size=1)
image = image[height[0]:height[0]+size, width[0]:width[0]+size]
order = np.random.randint(2, size=1)[0]
if order == 1:
image_batch_1_tst.append(1 - image/255.0)
else:
image_batch_1_tst.append(image/255.0)
########################################
image = cv2.imread(os.path.join(path, str(tst_crops[item][1])+".jpg"))
max_width = image.shape[1] - size
max_height = image.shape[0] - size
width = np.random.randint(max_width, size=1)
height = np.random.randint(max_height, size=1)
image = image[height[0]:height[0]+size, width[0]:width[0]+size]
order = np.random.randint(2, size=1)[0]
if order == 1:
image_batch_2_tst.append(1 - image/255.0)
else:
image_batch_2_tst.append(image/255.0)
labs_tst = [tst_labels[x] for x in indexes_tst]
for e, elem in enumerate(labs_tst):
labs_tst[e] = [round(elem[0])]
labs_trn = [trn_labels[x] for x in indexes_trn]
for e, elem in enumerate(labs_trn):
labs_trn[e] = [round(elem[0])]
hist = classifier.fit([np.array(image_batch_1_trn), np.array(image_batch_2_trn)], np.array(labs_trn),
epochs=5, verbose=1, validation_data=([np.array(image_batch_1_tst), np.array(image_batch_2_tst)], np.array(labs_tst)))
if hist.history["val_binary_accuracy"][-1] > highest_val_acc:
classifier.save_weights("comparing_model_{}.h5" .format(i))
highest_val_acc = hist.history["val_binary_accuracy"][-1]