-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_hog_dt_5_fold.py
233 lines (182 loc) · 6.79 KB
/
main_hog_dt_5_fold.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in
import numpy as np # linear algebra
import json
from matplotlib import pyplot as plt
from skimage import color
from skimage.feature import hog
from sklearn import tree
from sklearn.metrics import classification_report, accuracy_score
import pickle
from gzip_pickle import *
from sklearn.model_selection import StratifiedKFold
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory
# from subprocess import check_output
# print(check_output(["ls", "input"]).decode("utf8"))
# Any results you write to the current directory are saved as output.
try:
pickle_in = open("dataset.pickle", "rb")
dataset = pickle.load(pickle_in)
pickle_in.detach()
except:
f = open(r'input/shipsnet.json')
dataset = json.load(f)
f.close()
pickle_out = open("dataset.pickle", "wb")
pickle.dump(dataset, pickle_out)
pickle_out.close()
# {data:[{label, locations, scene_ids}]}
print(dataset["scene_ids"][0])
# try
# assert 1 == 0
# break
# exit()
dataset.keys()
data = np.array(dataset['data']).astype('uint8')
labels = np.array(dataset['labels']).reshape(len(dataset['labels']), 1)
# Define Positive and Negative Class
try:
img_pos_idx = load_pickle('img_pos_idx_dev.pickle.gz')
img_neg_idx = load_pickle('img_neg_idx_dev.pickle.gz')
except:
img_pos_idx = []
img_neg_idx = []
for i in range(labels.shape[0]):
img_neg_idx.append(i) if labels[i] == 0 else img_pos_idx.append(i)
# Limit data size
save_pickle(img_pos_idx[:100], 'img_pos_idx_dev.pickle.gz')
save_pickle(img_neg_idx[:300], 'img_neg_idx_dev.pickle.gz')
img_pos_idx = img_pos_idx[:100]
img_neg_idx = img_neg_idx[:300]
img_pos = data[img_pos_idx]
img_neg = data[img_neg_idx]
# print(len(img_pos_idx))
# print(len(img_neg_idx))
# print(img_pos_idx[:4])
# print(img_neg_idx[:4])
# print(len(img_pos_idx))
# print(len(img_neg_idx))
# exit()
img_length = 80
data = data.reshape(-1, 3, img_length, img_length).transpose([0, 2, 3, 1])
data.shape
img_length = 80
data = data.reshape(-1, 3, img_length, img_length).transpose([0, 2, 3, 1])
data.shape
# plt.imshow(data[5])
data_gray = [color.rgb2gray(i) for i in data]
# plt.imshow(data_gray[5])
ppc = 16
hog_images = []
hog_features = []
for image in data_gray:
fd, hog_image = hog(image, orientations=16, pixels_per_cell=(
ppc, ppc), cells_per_block=(4, 4), block_norm='L2', visualize=True)
hog_images.append(hog_image)
hog_features.append(fd)
# plt.imshow(hog_images[51])
# labels = np.array(dataset['labels']).reshape(len(dataset['labels']), 1)
clf = tree.DecisionTreeClassifier(random_state=17)
hog_features = np.array(hog_features)
data_frame = np.hstack((hog_features, labels))
np.random.shuffle(data_frame)
# Stratified K-Fold Cross Validation
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=17)
skf.get_n_splits(hog_features, labels)
print(skf)
i = 0
classification_reports = []
precision_0 = []
precision_1 = []
recall_0 = []
recall_1 = []
f_score_0 = []
f_score_1 = []
weighted_avg_f1_score = []
train_test_index = []
for train_index, test_index in skf.split(hog_features, labels):
train_test_index.append((train_index, test_index))
print("Fold-", (i+1))
print("TRAIN index:", train_index, " Size:", len(train_index))
print("TEST index:", test_index, " Size: ", len(test_index))
x_train, x_test = hog_features[train_index], hog_features[test_index]
y_train, y_test = labels[train_index], labels[test_index]
# print("Labels:", labels[train_index])
clf.fit(x_train, y_train)
print("Finish Training")
y_pred = clf.predict(x_test)
y_pred_training = clf.predict(x_train)
print("Finish Predict")
print("Accuracy: "+str(accuracy_score(y_test, y_pred)))
print("Accuracy Training: "+str(accuracy_score(y_train, y_pred_training)))
print('\n')
print(classification_report(y_test, y_pred))
report = classification_report(y_test, y_pred, output_dict=True)
classification_reports.append(report)
f_score_0.append(report["0"]["f1-score"])
f_score_1.append(report["1"]["f1-score"])
weighted_avg_f1_score.append(report["weighted avg"]["f1-score"])
i += 1
print("Final Report")
# print(classification_reports)
# print("f_score_0", f_score_0)
# print("f_score_1", f_score_1)
# print("weighted_avg_f1_score", weighted_avg_f1_score)
# print("Fold Average F-Score Class 0", sum(f_score_0)/len(f_score_0))
# print("Fold Average F-Score Class 1", sum(f_score_1)/len(f_score_1))
# print("Fold Average weighted_avg_f1_score", sum(f_score_1)/len(f_score_1))
percentage = 80
partition = int(len(hog_features)*percentage/100)
print("Partition:"+str(partition))
x_train, x_test = data_frame[:partition, :-1], data_frame[partition:, :-1]
y_train, y_test = data_frame[:partition, -
1:].ravel(), data_frame[partition:, -1:].ravel()
clf.fit(x_train, y_train)
print("Finish Training")
y_pred = clf.predict(x_test)
y_pred_training = clf.predict(x_train)
print("Finish Predict")
print("Accuracy: "+str(accuracy_score(y_test, y_pred)))
print("Accuracy Training: "+str(accuracy_score(y_train, y_pred_training)))
print('\n')
print(classification_report(y_test, y_pred))
scene = PIL.Image.open('Indonesia_res_1080/makasar_1.jpg')
tensor = np.array(scene).astype('uint8')
width, height = scene.size
STEP_SIZE = 20
fig = plt.figure(figsize=(16, 32))
ax = fig.add_subplot(3, 1, 1)
ax.imshow(tensor)
plt.show()
ships = {}
for row in range(0, height, STEP_SIZE):
for col in range(0, width, STEP_SIZE):
area = tensor[row:row+img_length, col:col+img_length, 0:3]
if area.shape != (80, 80, 3):
continue
area = color.rgb2gray(area)
fd, hog_image = hog(area, orientations=16,
pixels_per_cell=(ppc, ppc),
cells_per_block=(4, 4),
block_norm='L2', visualize=True)
# print("HOG-Pred")
# fig = plt.figure(figsize=(16,32))
# ax = fig.add_subplot(3, 1, 1)
# ax.imshow(hog_image)
# plt.show()
hog_features = None
hog_features = fd.reshape(1, len(fd))
prediction = clf.predict(hog_features)
if prediction == 1:
print(f"found ship at [{row},{col}] with class {prediction}")
ships[row, col] = prediction
fig = plt.figure(figsize=(16, 32))
ax = fig.add_subplot(3, 1, 1)
ax.imshow(tensor)
for ship in ships:
row, col = ship
ax.add_patch(patches.Rectangle((col, row), 80,
80, edgecolor='r', facecolor='none'))
plt.show()