| 224 | |
| 225 | |
| 226 | def pascalvoc(settings, file_list, mode, batch_size, shuffle): |
| 227 | def reader(): |
| 228 | if mode == 'train' and shuffle: |
| 229 | np.random.shuffle(file_list) |
| 230 | batch_out = [] |
| 231 | cnt = 0 |
| 232 | for image in file_list: |
| 233 | image_path, label_path = image.split() |
| 234 | image_path = os.path.join(settings.data_dir, image_path) |
| 235 | label_path = os.path.join(settings.data_dir, label_path) |
| 236 | if not os.path.exists(image_path): |
| 237 | raise ValueError("%s is not exist, you should specify " |
| 238 | "data path correctly." % image_path) |
| 239 | im = Image.open(image_path) |
| 240 | if im.mode == 'L': |
| 241 | im = im.convert('RGB') |
| 242 | im_width, im_height = im.size |
| 243 | |
| 244 | # layout: label | xmin | ymin | xmax | ymax | difficult |
| 245 | bbox_labels = [] |
| 246 | root = xml.etree.ElementTree.parse(label_path).getroot() |
| 247 | for object in root.findall('object'): |
| 248 | bbox_sample = [] |
| 249 | # start from 1 |
| 250 | bbox_sample.append( |
| 251 | float(settings.label_list.index(object.find('name').text))) |
| 252 | bbox = object.find('bndbox') |
| 253 | difficult = float(object.find('difficult').text) |
| 254 | bbox_sample.append(float(bbox.find('xmin').text) / im_width) |
| 255 | bbox_sample.append(float(bbox.find('ymin').text) / im_height) |
| 256 | bbox_sample.append(float(bbox.find('xmax').text) / im_width) |
| 257 | bbox_sample.append(float(bbox.find('ymax').text) / im_height) |
| 258 | bbox_sample.append(difficult) |
| 259 | bbox_labels.append(bbox_sample) |
| 260 | im, sample_labels = preprocess(im, bbox_labels, mode, settings) |
| 261 | sample_labels = np.array(sample_labels) |
| 262 | if len(sample_labels) == 0: continue |
| 263 | im = im.astype('float32') |
| 264 | boxes = sample_labels[:, 1:5] |
| 265 | lbls = sample_labels[:, 0].astype('int32') |
| 266 | difficults = sample_labels[:, -1].astype('int32') |
| 267 | |
| 268 | batch_out.append((im, boxes, lbls, difficults)) |
| 269 | if len(batch_out) == batch_size: |
| 270 | yield batch_out |
| 271 | cnt += len(batch_out) |
| 272 | batch_out = [] |
| 273 | |
| 274 | if mode == 'test' and len(batch_out) > 1: |
| 275 | yield batch_out |
| 276 | cnt += len(batch_out) |
| 277 | batch_out = [] |
| 278 | |
| 279 | return reader |
| 280 | |
| 281 | |
| 282 | def train(settings, |