def __init__(self, data_dir, file_list, label_list=None, transforms=None, num_workers='auto', buffer_size=100, parallel_method='process', shuffle=False): super(ChangeDetDataset, self).__init__(transforms=transforms, num_workers=num_workers, buffer_size=buffer_size, parallel_method=parallel_method, shuffle=shuffle) self.file_list = list() self.labels = list() self._epoch = 0 if label_list is not None: with open(label_list, encoding=get_encoding(label_list)) as f: for line in f: item = line.strip() self.labels.append(item) with open(file_list, encoding=get_encoding(file_list)) as f: for line in f: items = line.strip().split() if len(items) > 3: raise Exception( "A space is defined as the separator, but it exists in image or label name {}." .format(line)) items[0] = path_normalization(items[0]) items[1] = path_normalization(items[1]) items[2] = path_normalization(items[2]) full_path_im1 = osp.join(data_dir, items[0]) full_path_im2 = osp.join(data_dir, items[1]) full_path_label = osp.join(data_dir, items[2]) if not osp.exists(full_path_im1): raise IOError('The image file {} is not exist!'.format( full_path_im1)) if not osp.exists(full_path_im2): raise IOError('The image file {} is not exist!'.format( full_path_im2)) if not osp.exists(full_path_label): raise IOError('The image file {} is not exist!'.format( full_path_label)) self.file_list.append( [full_path_im1, full_path_im2, full_path_label]) self.num_samples = len(self.file_list) logging.info("{} samples in file {}".format(len(self.file_list), file_list))
def generate_images_field(self, json_info, image_id): image = {} image["height"] = json_info["size"]["height"] image["width"] = json_info["size"]["width"] image["id"] = image_id + 1 json_info["path"] = path_normalization(json_info["path"]) image["file_name"] = osp.split(json_info["path"])[-1] return image
def generate_images_field(self, img_path, image_id): image = {} img = cv2.imread(img_path) image["height"] = img.shape[0] image["width"] = img.shape[1] image["id"] = image_id + 1 img_path = path_normalization(img_path) image["file_name"] = osp.split(img_path)[-1] return image
def generate_images_field(self, json_info, image_file, image_id): image = {} image["height"] = json_info["imageHeight"] image["width"] = json_info["imageWidth"] image["id"] = image_id + 1 json_img_path = path_normalization(json_info["imagePath"]) json_info["imagePath"] = osp.join( osp.split(json_img_path)[0], image_file) image["file_name"] = osp.split(json_info["imagePath"])[-1] return image
def __init__(self, data_dir, file_list, label_list, transforms=None, num_workers='auto', buffer_size=100, parallel_method='process', shuffle=False): super(VOCDetection, self).__init__(transforms=transforms, num_workers=num_workers, buffer_size=buffer_size, parallel_method=parallel_method, shuffle=shuffle) self.file_list = list() self.labels = list() self._epoch = 0 annotations = {} annotations['images'] = [] annotations['categories'] = [] annotations['annotations'] = [] cname2cid = {} label_id = 1 with open(label_list, encoding=get_encoding(label_list)) as fr: for line in fr.readlines(): cname2cid[line.strip()] = label_id label_id += 1 self.labels.append(line.strip()) logging.info("Starting to read file list from dataset...") for k, v in cname2cid.items(): annotations['categories'].append({ 'supercategory': 'component', 'id': v, 'name': k }) from pycocotools.mask import decode ct = 0 ann_ct = 0 with open(file_list, encoding=get_encoding(file_list)) as f: for line in f: img_file, json_file = [osp.join(data_dir, x) \ for x in line.strip().split()[:2]] img_file = path_normalization(img_file) json_file = path_normalization(json_file) if not is_pic(img_file): continue if not osp.isfile(json_file): continue if not osp.exists(img_file): raise IOError( 'The image file {} is not exist!'.format(img_file)) with open(json_file, mode='r', \ encoding=get_encoding(json_file)) as j: json_info = json.load(j) im_id = np.array([ct]) im = cv2.imread(img_file) im_w = im.shape[1] im_h = im.shape[0] objs = json_info['labels'] gt_bbox = np.zeros((len(objs), 4), dtype=np.float32) gt_class = np.zeros((len(objs), 1), dtype=np.int32) gt_score = np.ones((len(objs), 1), dtype=np.float32) is_crowd = np.zeros((len(objs), 1), dtype=np.int32) difficult = np.zeros((len(objs), 1), dtype=np.int32) gt_poly = [None] * len(objs) for i, obj in enumerate(objs): cname = obj['name'] gt_class[i][0] = cname2cid[cname] x1 = max(0, obj['x1']) y1 = max(0, obj['y1']) x2 = min(im_w - 1, obj['x2']) y2 = min(im_h - 1, obj['y2']) gt_bbox[i] = [x1, y1, x2, y2] is_crowd[i][0] = 0 if 'mask' in obj: mask_dict = {} mask_dict['size'] = [im_h, im_w] mask_dict['counts'] = obj['mask'].encode() mask = decode(mask_dict) gt_poly[i] = self.mask2polygon(mask) annotations['annotations'].append({ 'iscrowd': 0, 'image_id': int(im_id[0]), 'bbox': [x1, y1, x2 - x1 + 1, y2 - y1 + 1], 'area': float((x2 - x1 + 1) * (y2 - y1 + 1)), 'segmentation': [[x1, y1, x1, y2, x2, y2, x2, y1]] if gt_poly[i] is None else gt_poly[i], 'category_id': cname2cid[cname], 'id': ann_ct, 'difficult': 0 }) ann_ct += 1 im_info = { 'im_id': im_id, 'image_shape': np.array([im_h, im_w]).astype('int32'), } label_info = { 'is_crowd': is_crowd, 'gt_class': gt_class, 'gt_bbox': gt_bbox, 'gt_score': gt_score, 'difficult': difficult } if None not in gt_poly: label_info['gt_poly'] = gt_poly voc_rec = (im_info, label_info) if len(objs) != 0: self.file_list.append([img_file, voc_rec]) ct += 1 annotations['images'].append({ 'height': im_h, 'width': im_w, 'id': int(im_id[0]), 'file_name': osp.split(img_file)[1] }) if not len(self.file_list) > 0: raise Exception('not found any voc record in %s' % (file_list)) logging.info("{} samples in file {}".format(len(self.file_list), file_list)) self.num_samples = len(self.file_list) # matplotlib.use() must be called *before* pylab, matplotlib.pyplot, # or matplotlib.backends is imported for the first time # pycocotools import matplotlib import matplotlib matplotlib.use('Agg') from pycocotools.coco import COCO self.coco_gt = COCO() self.coco_gt.dataset = annotations self.coco_gt.createIndex()