a b/RefineNet & SESNet/utils/pascal_voc.py
1
import os
2
3
4
def pascal_segmentation_lut():
5
    """Return look-up table with number and correspondng class names
6
    for PASCAL VOC segmentation dataset. Two special classes are: 0 -
7
    background and 255 - ambigious region. All others are numerated from
8
    1 to 20.
9
    
10
    Returns
11
    -------
12
    classes_lut : dict
13
        look-up table with number and correspondng class names
14
    """
15
16
    classes_lut = dict([(0, 'background'), (255, 'teeth')])
17
18
    return classes_lut
19
20
21
def get_pascal_segmentation_images_lists_txts(pascal_root):
22
    segmentation_images_lists_relative_folder = 'ImageSets/Segmentation'
23
    
24
    segmentation_images_lists_folder = os.path.join(pascal_root,
25
                                                    segmentation_images_lists_relative_folder)
26
    
27
    pascal_train_list_filename = os.path.join(segmentation_images_lists_folder,
28
                                              'train.txt')
29
30
    pascal_validation_list_filename = os.path.join(segmentation_images_lists_folder,
31
                                                   'val.txt')
32
    
33
    pascal_trainval_list_filname = os.path.join(segmentation_images_lists_folder,
34
                                                'trainval.txt')
35
    
36
    return [
37
            pascal_train_list_filename,
38
            pascal_validation_list_filename,
39
            pascal_trainval_list_filname
40
           ]
41
42
43
def readlines_with_strip(filename):
44
    # Get raw filnames from the file
45
    with open(filename, 'r') as f:
46
        lines = f.readlines()
47
48
    # Clean filenames from whitespaces and newline symbols
49
    clean_lines = list(map(lambda x: x.strip(), lines))
50
    
51
    return clean_lines
52
53
54
def readlines_with_strip_array_version(filenames_array):
55
    multiple_files_clean_lines = list(map(readlines_with_strip, filenames_array))
56
    
57
    return multiple_files_clean_lines
58
59
60
def add_full_path_and_extention_to_filenames(filenames_array, full_path, extention):
61
    full_filenames = list(map(lambda x: os.path.join(full_path, x) + '.' + extention, filenames_array))
62
    
63
    return full_filenames
64
65
66
def add_full_path_and_extention_to_filenames_array_version(filenames_array_array, full_path, extention):
67
    result = list(map(lambda x: add_full_path_and_extention_to_filenames(x, full_path, extention),
68
                 filenames_array_array))
69
    
70
    return result
71
72
73
def get_pascal_segmentation_image_annotation_filenames_pairs(pascal_root):
74
    
75
    pascal_relative_images_folder = 'JPEGImages'
76
    pascal_relative_class_annotations_folder = 'SegmentationClass'
77
    
78
    images_extention = 'jpg'
79
    annotations_extention = 'png'
80
    
81
    pascal_images_folder = os.path.join(pascal_root, pascal_relative_images_folder)
82
    pascal_class_annotations_folder = os.path.join(pascal_root, pascal_relative_class_annotations_folder)
83
    
84
    pascal_images_lists_txts = get_pascal_segmentation_images_lists_txts(pascal_root)
85
    
86
    pascal_image_names = readlines_with_strip_array_version(pascal_images_lists_txts)
87
    
88
    images_full_names = add_full_path_and_extention_to_filenames_array_version(pascal_image_names,
89
                                                                               pascal_images_folder,
90
                                                                               images_extention)
91
    
92
    annotations_full_names = add_full_path_and_extention_to_filenames_array_version(pascal_image_names,
93
                                                                                    pascal_class_annotations_folder,
94
                                                                                    annotations_extention)
95
    
96
97
    temp = zip(images_full_names, annotations_full_names)
98
99
    image_annotation_filename_pairs = list(map(lambda x: zip(*x), temp))
100
    
101
    return image_annotation_filename_pairs
102
103
104
def get_pascal_selected_image_annotation_filenames_pairs(pascal_root, selected_names):
105
    """Returns (image, annotation) filenames pairs from PASCAL VOC segmentation dataset for selected names.
106
    The function accepts the selected file names from PASCAL VOC segmentation dataset
107
    and returns image, annotation pairs with fullpath and extention for those names.
108
    Parameters
109
    ----------
110
    pascal_root : string
111
        Path to the PASCAL VOC dataset root that is usually named 'VOC2012'
112
        after being extracted from tar file.
113
    selected_names : array of strings
114
        Selected filenames from PASCAL VOC that can be read from txt files that
115
        come with dataset.
116
    Returns
117
    -------
118
    image_annotation_pairs : 
119
        Array with filename pairs with fullnames.
120
    """
121
    pascal_relative_images_folder = 'JPEGImages'
122
    pascal_relative_class_annotations_folder = 'SegmentationClass'
123
    
124
    images_extention = 'jpg'
125
    annotations_extention = 'png'
126
    
127
    pascal_images_folder = os.path.join(pascal_root, pascal_relative_images_folder)
128
    pascal_class_annotations_folder = os.path.join(pascal_root, pascal_relative_class_annotations_folder)
129
    
130
    images_full_names = add_full_path_and_extention_to_filenames(selected_names,
131
                                                                 pascal_images_folder,
132
                                                                 images_extention)
133
    
134
    annotations_full_names = add_full_path_and_extention_to_filenames(selected_names,
135
                                                                      pascal_class_annotations_folder,
136
                                                                      annotations_extention)
137
    
138
    image_annotation_pairs = zip(images_full_names, 
139
                                 annotations_full_names)
140
    
141
    return image_annotation_pairs
142
143
def get_augmented_pascal_image_annotation_filename_pairs(pascal_root):
144
145
    pascal_txts = get_pascal_segmentation_images_lists_txts(pascal_root=pascal_root)
146
147
    pascal_name_lists = readlines_with_strip_array_version(pascal_txts)
148
149
    pascal_train_name_set, pascal_val_name_set, _ = list(map(lambda x: set(x), pascal_name_lists))
150
151
    all_pascal = pascal_train_name_set | pascal_val_name_set
152
153
    everything = all_pascal
154
    validation = pascal_val_name_set
155
156
    # The rest of the dataset is for training
157
    train = everything - validation
158
159
160
    # The rest of the data will be loaded from pascal
161
    train_from_pascal = train
162
163
164
    train_from_pascal_image_annotation_pairs = \
165
    get_pascal_selected_image_annotation_filenames_pairs(pascal_root,
166
                                                         list(train_from_pascal))
167
168
    overall_train_image_annotation_filename_pairs = train_from_pascal_image_annotation_pairs
169
170
    overall_val_image_annotation_filename_pairs = \
171
    get_pascal_selected_image_annotation_filenames_pairs(pascal_root,
172
                                                         validation)
173
174
    return overall_train_image_annotation_filename_pairs, overall_val_image_annotation_filename_pairs