-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.py
More file actions
60 lines (47 loc) · 1.8 KB
/
utils.py
File metadata and controls
60 lines (47 loc) · 1.8 KB
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
import numpy as np
from skimage.transform import resize
import os
from settings import configs, tasks
def crop_center(img, cropx, cropy):
x,y,c = img.shape
startx = x//2-(cropx//2)
starty = y//2-(cropy//2)
return img[startx:startx+cropx,starty:starty+cropy]
def resize_img(img, target_size):
ratios = [float(target_size[i]) / img.shape[i] for i in range(len(target_size))]
larger_ratio = max(ratios)
new_shape = [float(larger_ratio) * img.shape[i] for i in range(len(target_size))]
img = resize(img, (int(np.round(new_shape[0])),
int(np.round(new_shape[1]))),
mode='reflect')
# crop
img = crop_center(img, target_size[0], target_size[1])
return img
def get_create_results_dir(config_name, base_results_dir):
results_dir = os.path.join(base_results_dir, config_name)
if not os.path.exists(results_dir): os.makedirs(results_dir)
return results_dir
def get_config_str(config):
config_str = ''
for k, v in sorted(config.items()):
if k != 'description':
config_str += ' {}: {}\n'.format(k, v)
return config_str
def get_config(FLAGS):
config = dict()
config.update(configs[FLAGS['config']])
if FLAGS.get('task', None) is None:
task_suffix = '__' + config['task']
else:
task_suffix = '__' + FLAGS['task']
config['task'] = FLAGS['task']
if FLAGS.get('model_type', None) is None:
prefix = config['model_type'] + '__'
else:
prefix = FLAGS['model_type'] + '__'
config['model_type'] = FLAGS['model_type']
config.update(tasks[config['task']])
name = prefix + FLAGS['config'] + task_suffix
config['_config_name'] = name
config['_config_name_original'] = FLAGS['config']
return name, config