11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
-
15
14
"""
16
15
File: convert_voc2010.py
17
16
This file is based on https://www.cs.stanford.edu/~roozbeh/pascal-context/ to generate PASCAL-Context Dataset.
24
23
|--ImageSets
25
24
|
26
25
|--SegmentationClass
27
- |
26
+ |
28
27
|--JPEGImages
29
28
|
30
29
|--SegmentationObject
31
30
|
32
31
|--trainval_merged.json
33
32
"""
34
33
34
+ import argparse
35
35
import os
36
36
37
- import argparse
38
37
import tqdm
39
38
import numpy as np
40
39
from detail import Detail
41
40
from PIL import Image
41
+ from paddleseg .utils .download import _download_file
42
+
43
+ JSON_URL = 'https://codalabuser.blob.core.windows.net/public/trainval_merged.json'
42
44
43
45
44
46
def parse_args ():
45
47
parser = argparse .ArgumentParser (
46
- description =
47
- 'Generate PASCAL-Context dataset'
48
- )
48
+ description = 'Generate PASCAL-Context dataset' )
49
49
parser .add_argument (
50
- '--voc_path' ,
51
- dest = 'voc_path' ,
52
- help = 'pascal voc path' ,
53
- type = str )
50
+ '--voc_path' , dest = 'voc_path' , help = 'pascal voc path' , type = str )
54
51
parser .add_argument (
55
52
'--annotation_path' ,
56
53
dest = 'annotation_path' ,
@@ -66,17 +63,23 @@ def __init__(self, voc_path, annotation_path):
66
63
self .annotation_path = annotation_path
67
64
self .label_dir = os .path .join (self .voc_path , 'Context' )
68
65
self ._image_dir = os .path .join (self .voc_path , 'JPEGImages' )
69
- self .annFile = os .path .join (self .annotation_path , 'trainval_merged.json' )
70
-
66
+ self .annFile = os .path .join (self .annotation_path ,
67
+ 'trainval_merged.json' )
68
+
71
69
if not os .path .exists (self .annFile ):
72
- _download_file (url = JSON_URL , savepath = self .annotation_path , print_progress = True )
73
-
74
- self ._mapping = np .sort (np .array ([
75
- 0 , 2 , 259 , 260 , 415 , 324 , 9 , 258 , 144 , 18 , 19 , 22 ,
76
- 23 , 397 , 25 , 284 , 158 , 159 , 416 , 33 , 162 , 420 , 454 , 295 , 296 ,
77
- 427 , 44 , 45 , 46 , 308 , 59 , 440 , 445 , 31 , 232 , 65 , 354 , 424 ,
78
- 68 , 326 , 72 , 458 , 34 , 207 , 80 , 355 , 85 , 347 , 220 , 349 , 360 ,
79
- 98 , 187 , 104 , 105 , 366 , 189 , 368 , 113 , 115 ]))
70
+ _download_file (
71
+ url = JSON_URL ,
72
+ savepath = self .annotation_path ,
73
+ print_progress = True )
74
+
75
+ self ._mapping = np .sort (
76
+ np .array ([
77
+ 0 , 2 , 259 , 260 , 415 , 324 , 9 , 258 , 144 , 18 , 19 , 22 , 23 , 397 , 25 ,
78
+ 284 , 158 , 159 , 416 , 33 , 162 , 420 , 454 , 295 , 296 , 427 , 44 , 45 ,
79
+ 46 , 308 , 59 , 440 , 445 , 31 , 232 , 65 , 354 , 424 , 68 , 326 , 72 , 458 ,
80
+ 34 , 207 , 80 , 355 , 85 , 347 , 220 , 349 , 360 , 98 , 187 , 104 , 105 ,
81
+ 366 , 189 , 368 , 113 , 115
82
+ ]))
80
83
self ._key = np .array (range (len (self ._mapping ))).astype ('uint8' ) - 1
81
84
82
85
self .train_detail = Detail (self .annFile , self ._image_dir , 'train' )
@@ -94,12 +97,20 @@ def _class_to_index(self, mask, _mapping, _key):
94
97
assert (values [i ] in _mapping )
95
98
index = np .digitize (mask .ravel (), _mapping , right = True )
96
99
return _key [index ].reshape (mask .shape )
97
-
100
+
98
101
def save_mask (self , img_id , mode ):
99
102
if mode == 'train' :
100
- mask = Image .fromarray (self ._class_to_index (self .train_detail .getMask (img_id ), _mapping = self ._mapping , _key = self ._key ))
103
+ mask = Image .fromarray (
104
+ self ._class_to_index (
105
+ self .train_detail .getMask (img_id ),
106
+ _mapping = self ._mapping ,
107
+ _key = self ._key ))
101
108
elif mode == 'val' :
102
- mask = Image .fromarray (self ._class_to_index (self .val_detail .getMask (img_id ), _mapping = self ._mapping , _key = self ._key ))
109
+ mask = Image .fromarray (
110
+ self ._class_to_index (
111
+ self .val_detail .getMask (img_id ),
112
+ _mapping = self ._mapping ,
113
+ _key = self ._key ))
103
114
filename = img_id ['file_name' ]
104
115
basename , _ = os .path .splitext (filename )
105
116
if filename .endswith (".jpg" ):
@@ -109,27 +120,38 @@ def save_mask(self, img_id, mode):
109
120
110
121
def generate_label (self ):
111
122
112
- with open (os .path .join (self .voc_path , 'ImageSets/Segmentation/train_context.txt' ), 'w' ) as f :
123
+ with open (
124
+ os .path .join (self .voc_path ,
125
+ 'ImageSets/Segmentation/train_context.txt' ),
126
+ 'w' ) as f :
113
127
for img_id in tqdm .tqdm (self .train_ids , desc = 'train' ):
114
128
basename = self .save_mask (img_id , 'train' )
115
129
f .writelines ('' .join ([basename , '\n ' ]))
116
130
117
- with open (os .path .join (self .voc_path , 'ImageSets/Segmentation/val_context.txt' ), 'w' ) as f :
131
+ with open (
132
+ os .path .join (self .voc_path ,
133
+ 'ImageSets/Segmentation/val_context.txt' ),
134
+ 'w' ) as f :
118
135
for img_id in tqdm .tqdm (self .val_ids , desc = 'val' ):
119
136
basename = self .save_mask (img_id , 'val' )
120
137
f .writelines ('' .join ([basename , '\n ' ]))
121
-
122
- with open (os .path .join (self .voc_path , 'ImageSets/Segmentation/trainval_context.txt' ), 'w' ) as f :
138
+
139
+ with open (
140
+ os .path .join (self .voc_path ,
141
+ 'ImageSets/Segmentation/trainval_context.txt' ),
142
+ 'w' ) as f :
123
143
for img in tqdm .tqdm (os .listdir (self .label_dir ), desc = 'trainval' ):
124
144
if img .endswith ('.png' ):
125
145
basename = img .split ('.' , 1 )[0 ]
126
146
f .writelines ('' .join ([basename , '\n ' ]))
127
-
128
-
147
+
148
+
129
149
def main ():
130
150
args = parse_args ()
131
- generator = PascalContextGenerator (voc_path = args .voc_path , annotation_path = args .annotation_path )
151
+ generator = PascalContextGenerator (
152
+ voc_path = args .voc_path , annotation_path = args .annotation_path )
132
153
generator .generate_label ()
133
154
155
+
134
156
if __name__ == '__main__' :
135
157
main ()
0 commit comments