def __getitem__(self, idx, transform=True): item = self.dataset[idx] #jst.py call function __getitem__ img = cv2.imread(item['path']) label = item['label'] if transform: line_strings = self.lane_to_linestrings( item['old_anno']['lanes'] ) # get from origin, to prevent the lane shape be resized twice line_strings = LineStringsOnImage( line_strings, shape=img.shape) # lane and image resolution 1280*720 img, line_strings = self.transform( image=img, line_strings=line_strings) # lane and image resolution 640*360 #print(img.shape, line_strings.shape) line_strings.clip_out_of_image_() new_anno = { 'path': item['path'], 'lanes': self.linestrings_to_lanes(line_strings) } new_anno['categories'] = item[ 'categories'] #categories has been fed into 'label' label = self.transform_annotation(new_anno, img_wh=(self.img_w, self.img_h))['label'] img = img / 255. if self.normalize: img = (img - IMAGENET_MEAN) / IMAGENET_STD img = self.to_tensor(img.astype(np.float32)) return (img, label, idx)
def __getitem__(self, idx, transform=False): item = self._annotations[idx] img = cv2.imread(item['path']) label = item['label'] if transform: line_strings = self.lane_to_linestrings(item['old_anno']['lanes']) line_strings = LineStringsOnImage(line_strings, shape=img.shape) img, line_strings = self.transform(image=img, line_strings=line_strings) line_strings.clip_out_of_image_() new_anno = { 'path': item['path'], 'lanes': self.linestrings_to_lanes(line_strings) } new_anno['categories'] = item['categories'] label = self._transform_annotation(new_anno, img_wh=(self.img_w, self.img_h))['label'] img = img / 255. if self.normalize: img = (img - IMAGENET_MEAN) / IMAGENET_STD img = self.to_tensor(img.astype(np.float32)) return (img, label, idx)
def test_augment_line_strings__kernel_size_is_two__no_keep_size(self): from imgaug.augmentables.lines import LineString, LineStringsOnImage ls = [LineString([(1.5, 1.5), (5.5, 1.5), (5.5, 5.5)])] lsoi = LineStringsOnImage(ls, shape=(6, 6, 3)) expected = LineStringsOnImage([ LineString([(1.5/2, 1.5/2), (5.5/2, 1.5/2), (5.5/2, 5.5/2)]) ], shape=(3, 3, 3)) self._test_augment_cbaoi__kernel_size_is_two__no_keep_size( lsoi, expected, "augment_line_strings")
def test_line_strings_alignment(self): from imgaug.augmentables.lines import LineString, LineStringsOnImage lss = [LineString([(10, 10), (30, 10), (30, 30)])] lsoi = LineStringsOnImage(lss, shape=(40, 40, 1)) lsoi_empty = LineStringsOnImage([], shape=(40, 40, 1)) self._test_cbaoi_alignment(lsoi, lsoi_empty, [[(10 / 2, 10 / 2), (30 / 2, 10 / 2), (30 / 2, 30 / 2)]], [[(10, 10), (30, 10), (30, 30)]], "augment_line_strings")
def __getitem__(self, idx): item = self.dataset[idx] img_org = cv2.imread(item['path']) line_strings_org = self.lane_to_linestrings(item['old_anno']['lanes']) line_strings_org = LineStringsOnImage(line_strings_org, shape=img_org.shape) for i in range(30): img, line_strings = self.transform(image=img_org.copy(), line_strings=line_strings_org) line_strings.clip_out_of_image_() new_anno = { 'path': item['path'], 'lanes': self.linestrings_to_lanes(line_strings) } try: label = self.transform_annotation(new_anno, img_wh=(self.img_w, self.img_h))['label'] break except: if (i + 1) == 30: self.logger.critical( 'Transform annotation failed 30 times :(') exit() img = img / 255. if self.normalize: img = (img - IMAGENET_MEAN) / IMAGENET_STD img = self.to_tensor(img.astype(np.float32)) return (img, label, idx)
def _lane_argue(*, image, lane_src): lines_tuple = [[(float(pt['x']), float(pt['y'])) for pt in line_spec] for line_spec in lane_src['Lines']] lss = [ia_LineString(line_tuple_spec) for line_tuple_spec in lines_tuple] lsoi = LineStringsOnImage(lss, shape=image.shape) color_shift = iaa.OneOf([ iaa.GaussianBlur(sigma=(0.5, 1.5)), iaa.LinearContrast((1.5, 1.5), per_channel=False), iaa.Multiply((0.8, 1.2), per_channel=0.2), iaa.AdditiveGaussianNoise(loc=0, scale=(0.0, 0.1 * 255), per_channel=0.5), iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB, children=iaa.WithChannels(0, iaa.Multiply((0.7, 1.3)))), iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB, children=iaa.WithChannels(1, iaa.Multiply((0.1, 2)))), iaa.WithColorspace(to_colorspace=iaa.CSPACE_HSV, from_colorspace=iaa.CSPACE_RGB, children=iaa.WithChannels(2, iaa.Multiply((0.5, 1.5)))), ]) posion_shift = iaa.SomeOf(4, [ iaa.Fliplr(), iaa.Crop(percent=([0, 0.2], [0, 0.15], [0, 0], [0, 0.15]), keep_size=True), iaa.TranslateX(px=(-16, 16)), iaa.ShearX(shear=(-15, 15)), iaa.Rotate(rotate=(-15, 15)) ]) aug = iaa.Sequential([ iaa.Sometimes(p=0.6, then_list=color_shift), iaa.Sometimes(p=0.6, then_list=posion_shift) ], random_order=True) batch = ia.Batch(images=[image], line_strings=[lsoi]) batch_aug = list(aug.augment_batches([batch]))[0] # augment_batches returns a generator image_aug = batch_aug.images_aug[0] lsoi_aug = batch_aug.line_strings_aug[0] lane_aug = [[dict(x=kpt.x, y=kpt.y) for kpt in shapely_line.to_keypoints()] for shapely_line in lsoi_aug] return image_aug, dict(Lines=lane_aug)
def _test_augment_line_strings__kernel_size_differs(self, shape, shape_exp): from imgaug.augmentables.lines import LineString, LineStringsOnImage ls = [LineString([(1.5, 5.5), (5.5, 1.5), (5.5, 5.5)])] lsoi = LineStringsOnImage(ls, shape=shape) aug = self.augmenter( (iap.Deterministic(3), iap.Deterministic(2)), keep_size=False) lsoi_aug = aug.augment_line_strings(lsoi) expected = LineStringsOnImage( [LineString([ ((1.5/shape[1])*shape_exp[1], (5.5/shape[0])*shape_exp[0]), ((5.5/shape[1])*shape_exp[1], (1.5/shape[0])*shape_exp[0]), ((5.5/shape[1])*shape_exp[1], (5.5/shape[0])*shape_exp[0]) ])], shape=shape_exp) assert_cbaois_equal(lsoi_aug, expected)
def dealFile(img_path, img_savedir, label_path, label_savedir): basename = os.path.basename(img_path).split(".")[0] img_savesubdir = img_savedir label_savesubdir = label_savedir if (not os.path.exists(img_savesubdir)): os.makedirs(img_savesubdir) if (not os.path.exists(label_savesubdir)): os.makedirs(label_savesubdir) save_imgname = basename + ".jpg" save_labelname = basename + ".txt" image = imageio.imread(img_path) lines_vec = [] lines_tem = load_line(label_path) lines_svec = [] for idx, line in enumerate(lines_tem): line_pts = [] p1 = (line[1], line[2]) p2 = (line[3], line[4]) line_pts.append(p1) line_pts.append(p2) line_s = LineString(line_pts, label="{}".format(int(line[0]))) lines_svec.append(line_s) if (len(lines_svec) < 25): return lsoi = LineStringsOnImage(lines_svec, shape=image.shape) imageio.imwrite(os.path.join(img_savesubdir, save_imgname), image) saveLineString(os.path.join(label_savesubdir, save_labelname), lsoi) for t in range(10): save_aug_imgname = save_imgname.split(".")[0] + "_" + str(t) + ".jpg" save_aug_labelname = save_labelname.split(".")[0] + "_" + str( t) + ".txt" image_aug, lsoi_aug = seq(image=image, line_strings=lsoi) lsoi_aug = lsoi_aug.remove_out_of_image() lsoi_aug = lsoi_aug.clip_out_of_image() line_len = len(lsoi_aug.line_strings) if (line_len < 25): continue # ia.imshow(lsoi_aug.draw_on_image(image_aug, size=3)) imageio.imwrite(os.path.join(img_savesubdir, save_aug_imgname), image_aug) saveLineString(os.path.join(label_savesubdir, save_aug_labelname), lsoi_aug) img_len = len(os.listdir(img_savesubdir)) if (img_len < 2): shutil.rmtree(img_savesubdir) shutil.rmtree(label_savesubdir)
def test_empty_line_strings(self): from imgaug.augmentables.lines import LineStringsOnImage cbaoi = LineStringsOnImage([], shape=(5, 6, 3)) self._test_empty_cbaoi(cbaoi, "augment_line_strings")
def test_augment_line_strings__kernel_size_is_two__keep_size(self): from imgaug.augmentables.lines import LineString, LineStringsOnImage ls = [LineString([(0, 0), (2, 0), (2, 2)])] lsoi = LineStringsOnImage(ls, shape=(6, 6, 3)) self._test_augment_cbaoi__kernel_size_is_two__keep_size( lsoi, "augment_line_strings")
def _test_augment_line_strings__kernel_size_is_noop(self, kernel_size): from imgaug.augmentables.lines import LineString, LineStringsOnImage ls = [LineString([(1, 1), (2, 1), (2, 2)])] lsoi = LineStringsOnImage(ls, shape=(6, 6, 3)) self._test_augment_cbaoi__kernel_size_is_noop(kernel_size, lsoi, "augment_line_strings")
def kp_detection(db, k_ind): data_rng = setup_configurations.data_rng batch_size = setup_configurations.batch_size input_size = db.configs["input_size"] lighting = db.configs["lighting"] rand_color = db.configs["rand_color"] images = np.zeros((batch_size, 3, input_size[0], input_size[1]), dtype=np.float32) # b, 3, H, W masks = np.zeros((batch_size, 1, input_size[0], input_size[1]), dtype=np.float32) # b, 1, H, W gt_lanes = [] db_size = db.db_inds.size # 3268 | 2782 for b_ind in range(batch_size): if k_ind == 0: db.shuffle_inds() db_ind = db.db_inds[k_ind] k_ind = (k_ind + 1) % db_size # reading ground truth item = db.detections(db_ind) # all in the raw coordinate img = cv2.imread(item['path']) mask = np.ones((1, img.shape[0], img.shape[1], 1), dtype=np.bool) label = item['label'] transform = True if transform: line_strings = db.lane_to_linestrings(item['old_anno']['lanes']) line_strings = LineStringsOnImage(line_strings, shape=img.shape) img, line_strings, mask = db.transform(image=img, line_strings=line_strings, segmentation_maps=mask) line_strings.clip_out_of_image_() new_anno = { 'path': item['path'], 'lanes': db.linestrings_to_lanes(line_strings) } new_anno['categories'] = item['categories'] label = db._transform_annotation(new_anno, img_wh=(input_size[1], input_size[0]))['label'] # clip polys tgt_ids = label[:, 0] label = label[tgt_ids > 0] # make lower the same label[:, 1][label[:, 1] < 0] = 1 label[:, 1][...] = np.min(label[:, 1]) label = np.stack([label] * batch_size, axis=0) gt_lanes.append(torch.from_numpy(label.astype(np.float32))) img = (img / 255.).astype(np.float32) if rand_color: color_jittering_(data_rng, img) if lighting: lighting_(data_rng, img, 0.1, db.eig_val, db.eig_vec) normalize_(img, db.mean, db.std) images[b_ind] = img.transpose((2, 0, 1)) masks[b_ind] = np.logical_not(mask[:, :, :, 0]) images = torch.from_numpy(images) masks = torch.from_numpy(masks) return {"xs": [images, masks], "ys": [images, *gt_lanes]}, k_ind