def augmentation(self, idx=None, do_aug=True, relative=True, resize=True, for_one_image=None, show_img=False): """Create augmentation images from kitti. :param idx: the index of kitti images , index is in the shape of [1, 3, 555, 1033...] :param do_aug: if do_aug is False , then do nothing about images,and labels, but corp to fixed size. :param relative: the relative location of bounding boxes :return: return a np array of images, and return a list about labs are in the shape of [[class, left, top, button, right],..] """ if resize: resize = random.choice( self.cfg.TRAIN.MULTI_SIZE_RATIO) * self.cfg.TRAIN.IMG_SIZE images, labels = self._augmenting(idx, do_aug, relative, resize=resize, image_for_aug=for_one_image) if labels is not None: # if labels is not none ,then check the label in labels,whether the label is none. for i, label in enumerate(labels): time = 0 total_time = 50 while not label: # check every label, whether there is a label is empty. print('no label at NO.%s, repeating %d/%d' % (idx[i], time, total_time)) time += 1 re_aug_idx = [idx[i]] if time < total_time else [ random.choice(idx) ] image, label = self._augmenting( re_aug_idx, do_aug, relative, resize=resize, image_for_aug=for_one_image) label = label[0] if label: images[i] = image[0] labels[i] = label if show_img: _show_img(images, labels, show_img=True, cfg=self.cfg) return images, labels
def show_tracker_OTB(img_path, gt_path): img_list = glob.glob(img_path + '*.jpg') lines = open(gt_path).readlines() for img, line in zip(img_list, lines): img = cv2.imread(img) box = line.strip().split(',') box[0] = int(box[0]) box[1] = int(box[1]) box[2] = box[0] + int(box[2]) box[3] = box[1] + int(box[3]) boxes = ['OTB_GT'] boxes.append(box) _show_img(img, [boxes], show_time=200)
def test_backbone(self, test_picture_path): """Test.""" # prepare paramertas img_raw = cv2.imread(test_picture_path) if img_raw is None: print('ERROR:no such a image') if self.cfg.TEST.DO_AUG: img_aug, _ = self.dataaug.augmentation(for_one_image=img_raw) img_aug = img_aug[0] elif self.cfg.TEST.RESIZE: img_aug = cv2.resize(img_raw, (int( self.cfg.TRAIN.IMG_SIZE[1]), int(self.cfg.TRAIN.IMG_SIZE[0]))) else: img_aug = img_raw img_in = torch.from_numpy(img_aug).unsqueeze(0).type( torch.FloatTensor).to(self.cfg.TRAIN.DEVICE) img_raw = torch.from_numpy(img_raw).unsqueeze(0).type( torch.FloatTensor) img_in = img_in.permute([ 0, 3, 1, 2, ]) img_in = img_in / 127.5 - 1. predict = self.Model.forward(input_x=img_in, is_training=False) labels_pre = self.parsepredict._parse_predict(predict) return _show_img(img_raw, labels_pre, img_in=img_in[0], pic_path=test_picture_path, cfg=self.cfg)
def test_backbone(self, test_picture_path): """Test.""" # prepare paramertas if self.args.checkpoint: model_path = self.args.checkpoint else: model_path = self.cfg.PATH.TEST_WEIGHT_PATH self.Model.load_state_dict(torch.load(model_path)) self.Model = self.Model.cuda() self.Model.eval() img_raw = cv2.imread(test_picture_path) if img_raw is None: print('ERROR:no such a image') if self.cfg.TEST.DO_AUG: img_aug, _ = self.dataaug.augmentation(for_one_image=img_raw, do_aug=self.cfg.TEST.DO_AUG, resize=self.cfg.TEST.RESIZE) img_aug = img_aug[0] elif self.cfg.TEST.RESIZE: img_aug = cv2.resize(img_raw, (int( self.cfg.TRAIN.IMG_SIZE[1]), int(self.cfg.TRAIN.IMG_SIZE[0]))) else: img_aug = img_raw img_in = torch.from_numpy(img_aug).unsqueeze(0).type( torch.FloatTensor).cuda() img_raw = torch.from_numpy(img_raw).unsqueeze(0).type( torch.FloatTensor) predict = self.Model.forward(img_in) labels_pre = self.parsepredict._parse_predict(predict) return _show_img(img_raw, labels_pre, img_in=img_in[0], pic_path=test_picture_path, cfg=self.cfg)
def test_set(self): set_path = '../tmp/checkpoint/apollo_lg_1_test_set' test_set = torch.load(set_path) do_aug = False resize = True for img_idx in test_set: if os.path.isfile(self.cfg.IMGPATH + '%06d.png' % img_idx): file_name = '%06d.png' % img_idx else: file_name = '%06d.jpg' % img_idx imagepath = self.cfg.IMGPATH + file_name # label_path = cfg.LABPATH + '%06d.txt' % img_idx image1 = self.test_backbone(imagepath, do_aug, resize) # imgs, labels = self.augmentation([img_idx], do_aug=False, resize=False) image2 = _show_img([img_idx], do_aug, resize) image_cat = np.vstack((image1, image2)) cv2.imshow('img', image_cat) cv2.waitKey()
def _show_imgaug(self, idx, do_aug=True, resize=True, relative=False, show_img=True): """ Show the images with data augmentation. :param idx: idx of images from kitti. :return: show the images. """ images, labels = self.augmentation(idx, do_aug=do_aug, relative=relative, resize=resize) images = torch.Tensor(images) LOG.debug(labels) return _show_img(images, labels, show_img=show_img, cfg=self.cfg)
def get_data_by_idx(self, idx_store, index_from, index_to, is_training): ''' :param idx_store: :param index_from: :param index_to: :return: imags: torch.Float32, relative labels:[[cls, x1, y1, x2, y2],[...],...] ''' data = (None, None) if self.one_test: if self.one_name: idx = self.one_name else: idx = idx_store[1:2] else: idx = idx_store[index_from:index_to] if not idx: print('error, no IDX in loader_img.py') exit() imgs, labels = self._read_datas(idx) do_aug = self.cfg.TRAIN.DO_AUG if is_training else self.cfg.TEST.DO_AUG if do_aug: imgs, labels = self.dataaug.augmentation((imgs, labels)) if self.cfg.TRAIN.RESIZE: size = random.choice( self.cfg.TRAIN.MULTI_SIZE_RATIO) * self.cfg.TRAIN.IMG_SIZE resized_imgs = [] resized_labels = [] for i, img_i in enumerate(imgs): img_i_size = img_i.shape resized_imgs.append(cv2.resize(img_i, (size[1], size[0]))) labs_i = labels[i] if self.cfg.TRAIN.RELATIVE_LABELS: label_i = [[ lab[0], lab[1] / img_i_size[1], lab[2] / img_i_size[0], lab[3] / img_i_size[1], lab[4] / img_i_size[0] ] for lab in labs_i] else: label_i = [[ lab[0], lab[1] / img_i_size[1] * size[1], lab[2] / img_i_size[0] * size[0], lab[3] / img_i_size[1] * size[1], lab[4] / img_i_size[0] * size[0] ] for lab in labs_i] resized_labels.append(label_i) imgs = resized_imgs labels = resized_labels if self.cfg.TRAIN.SHOW_INPUT: _show_img(imgs, labels, show_img=True, cfg=self.cfg) imgs = torch.Tensor(np.array(imgs)) imgs = imgs.permute([ 0, 3, 1, 2, ]) imgs = imgs / 127.5 - 1. imgs = imgs.to(self.cfg.TRAIN.DEVICE) data = (imgs, labels) # return data