-
Notifications
You must be signed in to change notification settings - Fork 9
/
batch_detect_pores.py
116 lines (95 loc) · 3.38 KB
/
batch_detect_pores.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
import tensorflow as tf
import argparse
import os
import utils
from models import detection
FLAGS = None
def batch_detect(load_path, save_path, detect_fn):
'''
Detects pores in all images in directory load_path
using detect_fn and saves corresponding detections
in save_path.
Args:
load_path: path to load image from.
save_path: path to save detections. Will be created
if non existent.
detect_fn: function that receives an image and
returns an array of shape [N, 2] of detections.
'''
# load images from 'load_path'
images, names = utils.load_images_with_names(load_path)
# create 'save_path' directory tree
if not os.path.exists(save_path):
os.makedirs(save_path)
# detect in each image and save it
for image, name in zip(images, names):
# detect pores
detections = detect_fn(image)
# save results
filename = os.path.join(save_path, '{}.txt'.format(name))
utils.save_dets_txt(detections, filename)
def main():
half_patch_size = FLAGS.patch_size // 2
with tf.Graph().as_default():
image_pl, _ = utils.placeholder_inputs()
print('Building graph...')
net = detection.Net(image_pl, training=False)
print('Done')
with tf.Session() as sess:
print('Restoring model in {}...'.format(FLAGS.model_dir_path))
utils.restore_model(sess, FLAGS.model_dir_path)
print('Done')
# capture arguments in lambda function
def detect_pores(image):
return utils.detect_pores(image, image_pl, net.predictions,
half_patch_size, FLAGS.prob_thr,
FLAGS.inter_thr, sess)
# batch detect in dbi training
print('Detecting pores in PolyU-HRF DBI Training images...')
load_path = os.path.join(FLAGS.polyu_dir_path, 'DBI', 'Training')
save_path = os.path.join(FLAGS.results_dir_path, 'DBI', 'Training')
batch_detect(load_path, save_path, detect_pores)
print('Done')
# batch detect in dbi test
print('Detecting pores in PolyU-HRF DBI Test images...')
load_path = os.path.join(FLAGS.polyu_dir_path, 'DBI', 'Test')
save_path = os.path.join(FLAGS.results_dir_path, 'DBI', 'Test')
batch_detect(load_path, save_path, detect_pores)
print('Done')
# batch detect in dbii
print('Detecting pores in PolyU-HRF DBII images...')
load_path = os.path.join(FLAGS.polyu_dir_path, 'DBII')
save_path = os.path.join(FLAGS.results_dir_path, 'DBII')
batch_detect(load_path, save_path, detect_pores)
print('Done')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--polyu_dir_path',
required=True,
type=str,
help='path to PolyU-HRF dataset')
parser.add_argument(
'--model_dir_path',
type=str,
required=True,
help='path from which to restore trained model')
parser.add_argument(
'--patch_size', type=int, default=17, help='pore patch size')
parser.add_argument(
'--results_dir_path',
type=str,
default='result',
help='path to folder in which results should be saved')
parser.add_argument(
'--prob_thr',
type=float,
default=0.9,
help='probability threshold to filter detections')
parser.add_argument(
'--inter_thr',
type=float,
default=0.1,
help='nms intersection threshold')
FLAGS = parser.parse_args()
main()