Exemplo n.º 1
0
 def __init__(self, cfg):
     self.cfg = cfg
     self.dataaug = Dataaug(cfg)
     self.one_test = cfg.TEST.ONE_TEST
     self.one_name = cfg.TEST.ONE_NAME
     self.train_batch_num = 100
     self.test_batch_num = 1
     self.show_augimg = cfg.TRAIN.SHOW_AUG
Exemplo n.º 2
0
 def __init__(self, cfg, args):
     super(Test_OBD, self).__init__(cfg, args)
     self.dataaug = Dataaug(cfg)
     self.parsepredict = ParsePredict(cfg)
     self.apolloclass2num = dict(
         zip(self.cfg.TRAIN.CLASSES, range(len(self.cfg.TRAIN.CLASSES))))
     self.DataLoader = DataLoaderDict[cfg.BELONGS](cfg)
     self.SCORE = get_score_class(self.cfg.BELONGS)(self.cfg)
     self.SCORE.init_parameters()
Exemplo n.º 3
0
 def __init__(self, cfg):
     self.cfg = cfg
     self.one_test = cfg.TEST.ONE_TEST
     self.one_name = cfg.TEST.ONE_NAME
     self.train_batch_num = 100
     self.test_batch_num = 1
     self.resize_input2output = False
     if self.cfg.TRAIN.INPUT_AUG:
         self.Data_aug = Dataaug(self.cfg)
     if self.cfg.TRAIN.MODEL in ['cbdnet', 'srcnn', 'vdsr']:
         self.resize_input2output = True
Exemplo n.º 4
0
 def __init__(self, cfg):
     self.cfg = cfg
     self.dataaug = Dataaug(cfg)
     self.one_test = cfg.TEST.ONE_TEST
     self.one_name = cfg.TEST.ONE_NAME
     self.train_batch_num = 100
     self.test_batch_num = 1
     self.class_name = _get_class_names(cfg.PATH.CLASSES_PATH)
     self.print_path = self.cfg.TRAIN.SHOW_TRAIN_NAMES
     self.cls2idx = dict(
         zip(self.cfg.TRAIN.CLASSES, range(len(self.cfg.TRAIN.CLASSES))))
     self.a = 0
Exemplo n.º 5
0
class DataLoader:
    def __init__(self, cfg):
        self.cfg = cfg
        self.dataaug = Dataaug(cfg)
        self.one_test = cfg.TEST.ONE_TEST
        self.one_name = cfg.TEST.ONE_NAME
        self.train_batch_num = 100
        self.test_batch_num = 1
        self.show_augimg = cfg.TRAIN.SHOW_AUG

    def get_data_by_idx(self, idx_store, index_from, index_to):
        '''
        :param idx_store:
        :param index_from:
        :param index_to:
        :return: imags: torch.Float32, relative labels:[[cls, x1, y1, x2, y2],[...],...]
        '''
        data = (None, None)
        idx = idx_store[index_from:index_to]
        if idx:
            if self.one_test:
                idx = self.one_name
                do_aug = False
            else:
                do_aug = self.cfg.TRAIN.DO_AUG
            imgs, labels = self.dataaug.augmentation(
                idx,
                do_aug=do_aug,
                resize=self.cfg.TRAIN.RESIZE,
                relative=self.cfg.TRAIN.RELATIVE_LABELS,
                show_img=self.show_augimg)
            imgs = torch.Tensor(np.array(imgs))
            if _is_use_cuda(self.cfg.TRAIN.GPU_NUM):
                imgs = imgs.cuda(self.cfg.TRAIN.GPU_NUM)
            data = (imgs, labels)  #
        return data
Exemplo n.º 6
0
class DataLoader:
    def __init__(self, cfg):
        self.cfg = cfg
        self.one_test = cfg.TEST.ONE_TEST
        self.one_name = cfg.TEST.ONE_NAME
        self.train_batch_num = 100
        self.test_batch_num = 1
        self.resize_input2output = False
        if self.cfg.TRAIN.INPUT_AUG:
            self.Data_aug = Dataaug(self.cfg)
        if self.cfg.TRAIN.MODEL in ['cbdnet', 'srcnn', 'vdsr']:
            self.resize_input2output = True

    def get_data_by_idx(self,
                        idx_store,
                        index_from,
                        index_to,
                        is_training=True):
        '''
        :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()
        if idx:
            # processes = mp.Pool(4)
            # imgs, labels = processes.apply_async(self._prepare_data, (idx))
            imgs, labels = self._prepare_data(idx, is_training)
            imgs = imgs.permute([0, 3, 1, 2])
            imgs = imgs.to(self.cfg.TRAIN.DEVICE)
            labels = labels.to(self.cfg.TRAIN.DEVICE)
            data = (imgs, labels)  #
        return data

    def _prepare_data(self, idx, is_training=False):
        input_imgs = []
        target_imgs = []
        for id in idx:
            target = cv2.imread(id[2])  # no norse image or HR image
            if self.cfg.TRAIN.TARGET_PREDEEL:
                target = self._target_predeal(img=target, filename=id[0])

            input = target if id[1] in ['None', '', ' ', 'none'
                                        ] else cv2.imread(id[1])
            if self.cfg.TRAIN.INPUT_PREDEEL and is_training:
                input = self._input_predeal(img=input, filename=id[0])

            if self.cfg.TRAIN.SHOW_INPUT:
                cv2.imshow('img', input)
                cv2.waitKey(self.cfg.TRAIN.SHOW_INPUT)
            input = torch.from_numpy(
                np.asarray(
                    (input - self.cfg.TRAIN.PIXCELS_NORM[0]) * 1.0 /
                    self.cfg.TRAIN.PIXCELS_NORM[1])).type(torch.FloatTensor)
            target = torch.from_numpy(
                np.asarray(
                    (target - self.cfg.TRAIN.PIXCELS_NORM[0]) * 1.0 /
                    self.cfg.TRAIN.PIXCELS_NORM[1])).type(torch.FloatTensor)
            input_imgs.append(input)
            target_imgs.append(target)

        input_imgs = torch.stack(input_imgs)
        target_imgs = torch.stack(target_imgs)

        return input_imgs, target_imgs

    def _input_predeal(self, **kwargs):
        img = kwargs['img']
        filename = kwargs['filename']
        img = cv2.resize(
            img, (self.cfg.TRAIN.IMG_SIZE[0] // self.cfg.TRAIN.UPSCALE_FACTOR,
                  self.cfg.TRAIN.IMG_SIZE[1] // self.cfg.TRAIN.UPSCALE_FACTOR))
        if self.resize_input2output:
            img = cv2.resize(
                img, (self.cfg.TRAIN.IMG_SIZE[0], self.cfg.TRAIN.IMG_SIZE[1]))
        # add the augmentation ...
        img, _ = self.Data_aug.augmentation(for_one_image=[img])
        img = img[0]

        return img

    def _target_predeal(self, **kwargs):
        img = kwargs['img']
        filename = kwargs['filename']
        # img = _crop_licience_plante(img, filename)

        try:
            img = cv2.resize(
                img, (self.cfg.TRAIN.IMG_SIZE[0], self.cfg.TRAIN.IMG_SIZE[1]))
        except:
            print(filename)
        else:
            pass
        return img
Exemplo n.º 7
0
class Test_OBD(Test_Base):
    def __init__(self, cfg, args):
        super(Test_OBD, self).__init__(cfg, args)
        self.dataaug = Dataaug(cfg)
        self.parsepredict = ParsePredict(cfg)
        self.apolloclass2num = dict(
            zip(self.cfg.TRAIN.CLASSES, range(len(self.cfg.TRAIN.CLASSES))))
        self.DataLoader = DataLoaderDict[cfg.BELONGS](cfg)
        self.SCORE = get_score_class(self.cfg.BELONGS)(self.cfg)
        self.SCORE.init_parameters()

    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 score(self, txt_info, pre_path):
        pre_path_list = glob.glob(pre_path + '/*.*')
        lines = open(txt_info, 'r').readlines()
        gt_labels = []
        pre_labels = []
        for line in lines:
            tmp = line.split(";")
            gt_name = tmp[0].strip()
            for pre_path_i in pre_path_list:
                if gt_name == os.path.basename(pre_path_i).split('.')[0]:
                    gt_path = tmp[2].strip()
                    gt_labels.append(self.DataLoader._read_line(gt_path))
                    pre_labels.append(
                        self.DataLoader._read_line(pre_path_i,
                                                   predicted_line=True))
                    break

        self.SCORE.cal_score(pre_labels, gt_labels, from_net=False)
        return self.SCORE.score_out()
Exemplo n.º 8
0
 def __init__(self, cfg, args):
     self.cfg = cfg
     self.args = args
     self.Model = ModelDict[cfg.TRAIN.MODEL](cfg)
     self.dataaug = Dataaug(cfg)
     self.parsepredict = ParsePredict(cfg)
Exemplo n.º 9
0
class Test_img:
    def __init__(self, cfg, args):
        self.cfg = cfg
        self.args = args
        self.Model = ModelDict[cfg.TRAIN.MODEL](cfg)
        self.dataaug = Dataaug(cfg)
        self.parsepredict = ParsePredict(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_run(self, file_s):
        """
        Test images in the file_s.

        :param file_s:
        :return:
        """
        if os.path.isfile(file_s):
            if file_s.split('.')[1] == 'txt':  # .txt
                lines = open(file_s, 'r').readlines()
                FILES = []
                for line in lines:
                    tmp = line.split(";")
                    FILES.append(tmp[1])
            else:  # xx.jpg
                FILES = [file_s]
        else:
            if os.path.isdir(file_s):
                FILES = os.listdir(file_s)
            elif isinstance(list, file_s):
                FILES = file_s

        _len = len(FILES)
        for i, file in enumerate(FILES):
            print('testing {}...{}/{}'.format(file, i + 1, _len))
            img_path = file
            self.test_backbone(img_path)

    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 calculate_f1_score(self, cfg):
        print('calculating the F1score...')
        f1sore = F1Score(cfg)
        pre_labels, gt_labels = f1sore.get_labels_txt(cfg.pre_path,
                                                      cfg.lab_path)
        f1sore.cal_tp_fp(pre_labels, gt_labels, from_net=False)
        f1_sore, prec, rec = f1sore.f1_score()
        print('f1_sore: {}\nprec: {}\nrec: {}'.format(f1_sore, prec, rec))
Exemplo n.º 10
0
class DataLoader:
    def __init__(self, cfg):
        self.cfg = cfg
        self.dataaug = Dataaug(cfg)
        self.one_test = cfg.TEST.ONE_TEST
        self.one_name = cfg.TEST.ONE_NAME
        self.train_batch_num = 100
        self.test_batch_num = 1
        self.class_name = _get_class_names(cfg.PATH.CLASSES_PATH)
        self.print_path = self.cfg.TRAIN.SHOW_TRAIN_NAMES
        self.cls2idx = dict(
            zip(self.cfg.TRAIN.CLASSES, range(len(self.cfg.TRAIN.CLASSES))))
        self.a = 0

    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

    def _read_line(self, path, predicted_line=False,
                   pass_obj=[
                       'DontCare',
                   ]):
        """
        Parse the labels from file.

        :param pass_obj: pass the labels in the list.
                        e.g. pass_obj=['Others','Pedestrian', 'DontCare']
        :param path: the path of file that need to parse.
        :return:lists of the classes and the key points.
        """
        bbs = []
        if os.path.basename(path).split(
                '.')[-1] == 'txt' and not predicted_line:
            file_open = open(path, 'r')
            for line in file_open.readlines():
                tmps = line.strip().split(' ')
                if tmps[0] not in self.class_name:
                    continue
                if self.class_name[tmps[0]] in pass_obj:
                    continue
                box_x1 = float(tmps[4])
                box_y1 = float(tmps[5])
                box_x2 = float(tmps[6])
                box_y2 = float(tmps[7])
                if not self._is_finedata([box_x1, box_y1, box_x2, box_y2]):
                    continue
                bbs.append([
                    self.cls2idx[self.class_name[tmps[0]]], box_x1, box_y1,
                    box_x2, box_y2
                ])
        elif os.path.basename(path).split(
                '.')[-1] == 'xml' and not predicted_line:
            tree = ET.parse(path)
            root = tree.getroot()
            for obj in root.findall('object'):
                cls_name = obj.find('name').text
                if cls_name not in self.class_name:
                    continue
                if self.class_name[cls_name] in pass_obj:
                    continue
                bbox = obj.find('bndbox')
                box_x1 = float(bbox.find('xmin').text)
                box_y1 = float(bbox.find('ymin').text)
                box_x2 = float(bbox.find('xmax').text)
                box_y2 = float(bbox.find('ymax').text)
                if not self._is_finedata([box_x1, box_y1, box_x2, box_y2]):
                    continue
                bbs.append([
                    self.cls2idx[self.class_name[cls_name]], box_x1, box_y1,
                    box_x2, box_y2
                ])

        elif os.path.basename(path).split('.')[-1] == 'txt' and predicted_line:
            f_path = open(path, 'r')
            print(path)
            for line in f_path.readlines():
                tmp = line.split()
                cls_name = tmp[0]
                bbs.append([
                    float(tmp[1]), self.cls2idx[self.class_name[cls_name]],
                    [
                        float(tmp[2]),
                        float(tmp[3]),
                        float(tmp[4]),
                        float(tmp[5])
                    ]
                ])
        return bbs

    def _read_datas(self, idx, image=None):
        '''
        Read images and labels depend on the idx.
        :param image: if there is a image ,the just return the image.
        :return: images, labels, image_size
        '''
        images = []
        labels = []
        if image is None:
            idx_remove = idx.copy()
            for id in idx:
                if self.print_path:
                    print(id[1], '<--->', id[2])
                if not (os.path.isfile(id[1]) and os.path.isfile(id[2])):
                    print('ERROR, NO SUCH A FILE.', id[1], '<--->', id[2])
                    exit()
                # labels come first.
                label = self._read_line(id[2])
                while not label:  # whether the label is empty?
                    if id in idx_remove:  # if img_idx has been removed then label is empty
                        idx_remove.remove(id)
                    if not idx_remove: break
                    id = random.choice(idx_remove)
                    print('warning: no label...instead it of ', id[1])
                    label = self._read_line(id[2])
                labels.append(label)
                # then add the images.
                img = cv2.imread(id[1])
                if img is not None:
                    images.append(img)
                else:
                    print('imread img is NONE.')
        else:
            images.append(image)

        if labels == [[]]:
            labels = None
        return images, labels

    def _is_finedata(self, xyxy):
        x1, y1, x2, y2 = xyxy
        for point in xyxy:
            if point < 0: return False
        if x2 - x1 <= 0: return False
        if y2 - y1 <= 0: return False
        return True