-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathseg_utils.py
61 lines (51 loc) · 1.45 KB
/
seg_utils.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
#!/usr/bin/env python
"""Utility functions for segmentation tasks."""
from PIL import Image
import numpy
def get_image(image_path, force_mode=None):
"""
Get a numpy array of an image so that one can access values[y][x].
Parameters
----------
image_path : str
force_mode : {None, 'L', 'RGB', 'RGBA', ...}
Returns
-------
numpy array
"""
image = Image.open(image_path, 'r')
if force_mode is not None:
image = image.convert(mode=force_mode)
width, height = image.size
pixel_values = list(image.getdata())
if image.mode == 'RGB':
channels = 3
elif image.mode == 'RGBA':
image = Image.open(image_path).convert('RGB')
pixel_values = list(image.getdata())
channels = 3
elif image.mode == 'L':
channels = 1
else:
print("Unknown mode: %s" % image.mode)
print("image_path: %s" % image_path)
return None
pixel_values = numpy.array(pixel_values).reshape((height, width, channels))
return pixel_values
def get_class_weight(hypes):
"""
Get the weight of classes. The default value is 1.
Parameters
----------
hypes : dict
Returns
-------
dict
Class weights, mapping class indices to floats
"""
class_weights = {}
for i, cl in enumerate(hypes['classes']):
class_weights[i] = 1.0
if 'weight' in cl:
class_weights[i] = float(cl['weight'])
return class_weights