This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
experiment.py
113 lines (88 loc) · 3.42 KB
/
experiment.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
from cnnlevelset.pascalvoc_util import PascalVOC
from cnnlevelset.localizer import Localizer
from cnnlevelset import config as cfg
from collections import defaultdict
import tensorflow as tf
import keras.backend as K
import numpy as np
import matplotlib.pyplot as plt
import sys
import time
tf.python.control_flow_ops = tf
pascal = PascalVOC(cfg.PASCAL_PATH)
X_img_test, X_test, y_test, y_seg = pascal.get_test_data(10000, False)
cls_y_test = y_test[:, :, 0]
N = float(X_img_test.shape[0])
localizer = Localizer(model_path=cfg.MODEL_PATH)
start = time.time()
cls_preds, bbox_preds = localizer.predict(X_test)
end = time.time()
print('CNN time: {:.4f}'.format(end - start))
print('Average: {:.4f}'.format((end - start) / N))
cls_acc = np.mean(np.argmax(cls_preds, axis=1) == np.argmax(cls_y_test, axis=1))
print(cls_acc)
K.clear_session()
from cnnlevelset.segmenter import *
if len(sys.argv) > 1 and sys.argv[1] == 'show':
show = True
else:
show = False
bbox_res, border_res, cnn_res = defaultdict(list), defaultdict(list), defaultdict(list)
i = 0
for img, y, cls_pred, bbox_pred, ys in zip(X_img_test, y_test, cls_preds, bbox_preds, y_seg):
if show:
label = pascal.idx2label[np.argmax(cls_pred)]
print(label)
img = img.reshape(224, 224, 3)
plt.imshow(pascal.draw_bbox(img, bbox_pred))
plt.show()
phi = phi_from_bbox(img, bbox_pred)
levelset_segment_theano(img, phi=phi, sigma=5, v=1, alpha=100000, n_iter=80, print_after=80)
input()
else:
start = time.time()
phi = phi_from_bbox(img, bbox_pred)
mask = (phi < 0)
end = time.time()
bbox_res['time'].append(end - start)
bbox_res['accuracy'].append(pascal.segmentation_accuracy(mask, ys))
p, r, f1 = pascal.segmentation_prec_rec_f1(mask, ys)
bbox_res['precision'].append(p)
bbox_res['recall'].append(r)
bbox_res['f1'].append(f1)
start = time.time()
phi = default_phi(img)
mask = levelset_segment_theano(img, phi=phi, sigma=5, v=1, alpha=100000, n_iter=80)
end = time.time()
border_res['time'].append(end - start)
border_res['accuracy'].append(pascal.segmentation_accuracy(mask, ys))
p, r, f1 = pascal.segmentation_prec_rec_f1(mask, ys)
border_res['precision'].append(p)
border_res['recall'].append(r)
border_res['f1'].append(f1)
start = time.time()
phi = phi_from_bbox(img, bbox_pred)
mask = levelset_segment_theano(img, phi=phi, sigma=5, v=1, alpha=100000, n_iter=80)
end = time.time()
cnn_res['time'].append(end - start)
cnn_res['accuracy'].append(pascal.segmentation_accuracy(mask, ys))
p, r, f1 = pascal.segmentation_prec_rec_f1(mask, ys)
cnn_res['precision'].append(p)
cnn_res['recall'].append(r)
cnn_res['f1'].append(f1)
i += 1
print(i)
if not show:
for metric in ['accuracy', 'precision', 'recall', 'f1']:
print(metric)
print('----------------')
print('Bbox: {}'.format(np.mean(bbox_res[metric])))
print('Border: {}'.format(np.mean(border_res[metric])))
print('CNN: {}'.format(np.mean(cnn_res[metric])))
print()
print('Time')
print('---------------------')
print('Bbox: {}'.format(np.mean(bbox_res['time'])))
print('Border: {}'.format(np.mean(border_res['time'])))
print('CNN: {}'.format(np.mean(cnn_res['time'])))
print()