Пример #1
0
    def _create_train_test(self, force_augmentation, kwargs):
        slide_window_params = {
            'displace': kwargs['displace'],
            'size_x': kwargs['size_x'],
            'size_y': kwargs['size_y'],
            'people_thr': kwargs['people_thr']
        }
        noise_params = {'augment_noise': kwargs['augment_noise']}
        light_params = {
            'augment_light': kwargs['augment_light'],
            'bright': kwargs['bright'],
            'contrast': kwargs['contrast']
        }

        #shanghaiTech part A
        self.augmented_dir_partA = osp.join(self.ori_dir_partA,
                                            self.signature())
        augment_data_A = False
        if osp.exists(self.augmented_dir_partA):
            print("'{}' already exists".format(self.augmented_dir_partA))
            if force_augmentation:
                augment_data_A = True
                print("augmenting data anyway")
            else:
                augment_data_A = False
                print("will not augmenting data")
        else:
            augment_data_A = True
            os.makedirs(self.augmented_dir_partA)

        aug_dir_partA_img = osp.join(self.augmented_dir_partA, "train_img")
        aug_dir_partA_den = osp.join(self.augmented_dir_partA, "train_den")
        aug_dir_partA_lab = osp.join(self.augmented_dir_partA, "train_lab")
        test_dir_partA_img = osp.join(self.augmented_dir_partA, 'test_img')
        test_dir_partA_den = osp.join(self.augmented_dir_partA, 'test_den')
        test_dir_partA_lab = osp.join(self.augmented_dir_partA, 'test_lab')
        mkdir_if_missing(aug_dir_partA_img)
        mkdir_if_missing(aug_dir_partA_den)
        mkdir_if_missing(aug_dir_partA_lab)
        mkdir_if_missing(test_dir_partA_img)
        mkdir_if_missing(test_dir_partA_den)
        mkdir_if_missing(test_dir_partA_lab)

        kwargs['name'] = 'shanghai-partA'
        part_A_train_test = train_test_unit(aug_dir_partA_img,
                                            aug_dir_partA_den,
                                            test_dir_partA_img,
                                            test_dir_partA_den, kwargs.copy())
        self.train_test_set.append(part_A_train_test)

        if augment_data_A:
            ori_img_paths = [
                osp.join(self.ori_dir_partA_train_img, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partA_train_img))
            ]
            ori_lab_paths = [
                osp.join(self.ori_dir_partA_train_lab, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partA_train_lab))
            ]
            ori_den_paths = [
                osp.join(self.ori_dir_partA_train_den, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partA_train_den))
            ]
            augment(ori_img_paths, ori_lab_paths, ori_den_paths,
                    aug_dir_partA_img, aug_dir_partA_lab, aug_dir_partA_den,
                    slide_window_params, noise_params, light_params)

            test_img_paths = [
                osp.join(self.ori_dir_partA_test_img,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partA_test_img))
            ]
            test_lab_paths = [
                osp.join(self.ori_dir_partA_test_lab,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partA_test_lab))
            ]
            test_den_paths = [
                osp.join(self.ori_dir_partA_test_den,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partA_test_den))
            ]
            copy_to_directory(test_img_paths, test_dir_partA_img)
            copy_to_directory(test_lab_paths, test_dir_partA_lab)
            copy_to_directory(test_den_paths, test_dir_partA_den)

        #shanghaiTech part B
        self.augmented_dir_partB = osp.join(self.ori_dir_partB,
                                            self.signature())
        augment_data_B = False
        if osp.exists(self.augmented_dir_partB):
            print("'{}' already exists".format(self.augmented_dir_partB))
            if force_augmentation:
                augment_data_B = True
                print("augmenting data anyway")
            else:
                augment_data_B = False
                print("will not augmenting data")
        else:
            augment_data_B = True
            os.makedirs(self.augmented_dir_partB)

        aug_dir_partB_img = osp.join(self.augmented_dir_partB, "train_img")
        aug_dir_partB_den = osp.join(self.augmented_dir_partB, "train_den")
        aug_dir_partB_lab = osp.join(self.augmented_dir_partB, "train_lab")
        test_dir_partB_img = osp.join(self.augmented_dir_partB, 'test_img')
        test_dir_partB_den = osp.join(self.augmented_dir_partB, 'test_den')
        test_dir_partB_lab = osp.join(self.augmented_dir_partB, 'test_lab')
        mkdir_if_missing(aug_dir_partB_img)
        mkdir_if_missing(aug_dir_partB_den)
        mkdir_if_missing(aug_dir_partB_lab)
        mkdir_if_missing(test_dir_partB_img)
        mkdir_if_missing(test_dir_partB_den)
        mkdir_if_missing(test_dir_partB_lab)

        kwargs['name'] = 'shanghai-partB'
        part_B_train_test = train_test_unit(aug_dir_partB_img,
                                            aug_dir_partB_den,
                                            test_dir_partB_img,
                                            test_dir_partB_den, kwargs.copy())
        self.train_test_set.append(part_B_train_test)

        if augment_data_B:
            ori_img_paths = [
                osp.join(self.ori_dir_partB_train_img, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partB_train_img))
            ]
            ori_lab_paths = [
                osp.join(self.ori_dir_partB_train_lab, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partB_train_lab))
            ]
            ori_den_paths = [
                osp.join(self.ori_dir_partB_train_den, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partB_train_den))
            ]
            augment(ori_img_paths, ori_lab_paths, ori_den_paths,
                    aug_dir_partB_img, aug_dir_partB_lab, aug_dir_partB_den,
                    slide_window_params, noise_params, light_params)

            test_img_paths = [
                osp.join(self.ori_dir_partB_test_img,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partB_test_img))
            ]
            test_lab_paths = [
                osp.join(self.ori_dir_partB_test_lab,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partB_test_lab))
            ]
            test_den_paths = [
                osp.join(self.ori_dir_partB_test_den,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partB_test_den))
            ]
            copy_to_directory(test_img_paths, test_dir_partB_img)
            copy_to_directory(test_lab_paths, test_dir_partB_lab)
            copy_to_directory(test_den_paths, test_dir_partB_den)
Пример #2
0
    def _create_train_test(self, force_augmentation, **kwargs):
        slide_window_params = {
            'displace': kwargs['displace'],
            'size_x': kwargs['size_x'],
            'size_y': kwargs['size_y'],
            'people_thr': kwargs['people_thr']
        }
        noise_params = {'augment_noise': kwargs['augment_noise']}
        light_params = {
            'augment_light': kwargs['augment_light'],
            'bright': kwargs['bright'],
            'contrast': kwargs['contrast']
        }

        self.augmented_dir = osp.join(self.root, self.signature())

        # check if augmented data exists
        augment_data = False
        if osp.exists(self.augmented_dir):
            print("'{}' already exists".format(self.augmented_dir))

            if force_augmentation:
                augment_data = True
                print("augmenting data anyway, since force_augmentantion=True")

            else:
                augment_data = False
                print("skipping augmentation, since force_augmentantion=False")

        else:
            augment_data = True
            os.makedirs(self.augmented_dir)

        #create new directories for augmentations
        aug_dir_train_img = osp.join(self.augmented_dir, "train_img")
        aug_dir_train_den = osp.join(self.augmented_dir, "train_den")
        aug_dir_train_lab = osp.join(self.augmented_dir, "train_lab")
        test_dir_img = osp.join(self.augmented_dir, "test_img")
        test_dir_den = osp.join(self.augmented_dir, "test_den")
        test_dir_lab = osp.join(self.augmented_dir, "test_lab")
        val_dir_img = osp.join(self.augmented_dir, "val_img")
        val_dir_den = osp.join(self.augmented_dir, "val_den")
        val_dir_lab = osp.join(self.augmented_dir, "val_lab")
        mkdir_if_missing(aug_dir_train_img)
        mkdir_if_missing(aug_dir_train_den)
        mkdir_if_missing(aug_dir_train_lab)
        mkdir_if_missing(test_dir_img)
        mkdir_if_missing(test_dir_den)
        mkdir_if_missing(test_dir_lab)
        mkdir_if_missing(val_dir_img)
        mkdir_if_missing(val_dir_den)
        mkdir_if_missing(val_dir_lab)

        kwargs['name'] = 'jhu_crowd'
        kwargs["val"] = True
        kwargs["val_dir_img"] = val_dir_img
        kwargs["val_dir_den"] = val_dir_den
        train_test = train_test_unit(aug_dir_train_img, aug_dir_train_den,
                                     test_dir_img, test_dir_den, kwargs.copy())

        if augment_data and not (kwargs["skip_dir_check"]):
            ori_img_paths = [
                osp.join(self.ori_train_img, file_name)
                for file_name in sorted(os.listdir(self.ori_train_img))
            ]
            ori_lab_paths = [
                osp.join(self.ori_train_lab, file_name)
                for file_name in sorted(os.listdir(self.ori_train_lab))
            ]
            ori_den_paths = [
                osp.join(self.ori_train_den, file_name)
                for file_name in sorted(os.listdir(self.ori_train_den))
            ]
            augment(ori_img_paths, ori_lab_paths, ori_den_paths,
                    aug_dir_train_img, aug_dir_train_lab, aug_dir_train_den,
                    slide_window_params, noise_params, light_params)

            test_img_paths = [
                osp.join(self.ori_test_img, file_name)
                for file_name in sorted(os.listdir(self.ori_test_img))
            ]
            test_lab_paths = [
                osp.join(self.ori_test_lab, file_name)
                for file_name in sorted(os.listdir(self.ori_test_lab))
            ]
            test_den_paths = [
                osp.join(self.ori_test_den, file_name)
                for file_name in sorted(os.listdir(self.ori_test_den))
            ]
            copy_to_directory(test_img_paths, test_dir_img)
            copy_to_directory(test_lab_paths, test_dir_lab)
            copy_to_directory(test_den_paths, test_dir_den)

            val_img_paths = [
                osp.join(self.ori_val_img, file_name)
                for file_name in sorted(os.listdir(self.ori_val_img))
            ]
            val_lab_paths = [
                osp.join(self.ori_val_lab, file_name)
                for file_name in sorted(os.listdir(self.ori_val_lab))
            ]
            val_den_paths = [
                osp.join(self.ori_val_den, file_name)
                for file_name in sorted(os.listdir(self.ori_val_den))
            ]
            copy_to_directory(val_img_paths, val_dir_img)
            copy_to_directory(val_lab_paths, val_dir_lab)
            copy_to_directory(val_den_paths, val_dir_den)

        self.train_test_set.append(train_test)
Пример #3
0
    def _create_train_test(self, force_augmentation, kwargs):
        slide_window_params = {
            'displace': kwargs['displace'],
            'size_x': kwargs['size_x'],
            'size_y': kwargs['size_y'],
            'people_thr': kwargs['people_thr']
        }
        noise_params = {'augment_noise': kwargs['augment_noise']}
        light_params = {
            'augment_light': kwargs['augment_light'],
            'bright': kwargs['bright'],
            'contrast': kwargs['contrast']
        }

        file_names = os.listdir(self.ori_dir_img)
        file_names.sort()
        img_names = []
        img_ids = []
        for file_name in file_names:
            file_extention = file_name.split('.')[-1]
            file_id = file_name[:len(file_name) - len(file_extention)]
            if file_extention != 'png' and file_extention != 'jpg':
                continue
            img_names.append(file_name)
            img_ids.append(file_id)
        if len(img_names) != 50:
            raise RuntimeError(
                "UCF_CC_50 dataset expects 50 images, {} found".format(
                    len(img_names)))

        self.augmented_dir = osp.join(self.root, self.signature())
        augment_data = False
        if osp.exists(self.augmented_dir):
            print("'{}' already exists".format(self.augmented_dir))
            if force_augmentation:
                augment_data = True
                print("augmenting data anyway")
            else:
                augment_data = False
                print("will not augmenting data")
        else:
            augment_data = True
            os.makedirs(self.augmented_dir)

        #using 5 fold cross validation protocol
        for fold in range(5):
            fold_dir = osp.join(self.augmented_dir, 'fold{}'.format(fold + 1))
            aug_train_dir_img = osp.join(fold_dir, 'train_img')
            aug_train_dir_den = osp.join(fold_dir, 'train_den')
            aug_train_dir_lab = osp.join(fold_dir, 'train_lab')
            fold_test_dir_img = osp.join(fold_dir, 'test_img')
            fold_test_dir_den = osp.join(fold_dir, 'test_den')
            fold_test_dir_lab = osp.join(fold_dir, 'test_lab')

            mkdir_if_missing(aug_train_dir_img)
            mkdir_if_missing(aug_train_dir_den)
            mkdir_if_missing(aug_train_dir_lab)
            mkdir_if_missing(fold_test_dir_img)
            mkdir_if_missing(fold_test_dir_den)
            mkdir_if_missing(fold_test_dir_lab)

            kwargs['name'] = 'ucf-fold{}'.format(fold + 1)
            train_test = train_test_unit(aug_train_dir_img, aug_train_dir_den,
                                         fold_test_dir_img, fold_test_dir_den,
                                         kwargs.copy())
            self.train_test_set.append(train_test)

            if augment_data:
                test_img = img_names[fold * 10:(fold + 1) * 10]
                test_ids = img_ids[fold * 10:(fold + 1) * 10]
                test_den_paths = [
                    osp.join(self.ori_dir_den, img_id + 'npy')
                    for img_id in test_ids
                ]
                test_lab_paths = [
                    osp.join(self.ori_dir_lab, img_id + 'json')
                    for img_id in test_ids
                ]
                test_img_paths = [
                    osp.join(self.ori_dir_img, img) for img in test_img
                ]

                train_img = sorted(list(set(img_names) - set(test_img)))
                train_ids = sorted(list(set(img_ids) - set(test_ids)))
                train_den_paths = [
                    osp.join(self.ori_dir_den, img_id + 'npy')
                    for img_id in train_ids
                ]
                train_lab_paths = [
                    osp.join(self.ori_dir_lab, img_id + 'json')
                    for img_id in train_ids
                ]
                train_img_paths = [
                    osp.join(self.ori_dir_img, img) for img in train_img
                ]

                #augment train data
                print("Augmenting {}".format(kwargs['name']))
                augment(train_img_paths, train_lab_paths, train_den_paths,
                        aug_train_dir_img, aug_train_dir_lab,
                        aug_train_dir_den, slide_window_params, noise_params,
                        light_params)
                copy_to_directory(test_den_paths, fold_test_dir_den)
                copy_to_directory(test_lab_paths, fold_test_dir_lab)
                copy_to_directory(test_img_paths, fold_test_dir_img)
Пример #4
0
    def _create_train_test(self, force_augmentation, kwargs):
        slide_window_params = {
            'displace': kwargs['displace'],
            'size_x': kwargs['size_x'],
            'size_y': kwargs['size_y'],
            'people_thr': kwargs['people_thr']
        }
        noise_params = {'augment_noise': kwargs['augment_noise']}
        light_params = {
            'augment_light': kwargs['augment_light'],
            'bright': kwargs['bright'],
            'contrast': kwargs['contrast']
        }
        downscale = kwargs['downscale']
        ten_crop = kwargs['ten_crop_mode']

        if downscale and ten_crop:
            raise Exception(
                'five crop and dowscale mode cannot be used at the same time, choose one.'
            )

        #shanghaiTech part A
        self.augmented_dir_partA = osp.join(self.ori_dir_partA,
                                            self.signature())
        augment_data_A = False
        if osp.exists(self.augmented_dir_partA):
            print("'{}' already exists".format(self.augmented_dir_partA))
            if force_augmentation:
                augment_data_A = True
                print("augmenting data anyway")
            else:
                augment_data_A = False
                print("will not augmenting data")
        else:
            augment_data_A = True
            os.makedirs(self.augmented_dir_partA)

        aug_dir_partA_img = osp.join(self.augmented_dir_partA, "train_img")
        aug_dir_partA_den = osp.join(self.augmented_dir_partA, "train_den")
        aug_dir_partA_lab = osp.join(self.augmented_dir_partA, "train_lab")
        test_dir_partA_img = osp.join(self.augmented_dir_partA, 'test_img')
        test_dir_partA_den = osp.join(self.augmented_dir_partA, 'test_den')
        test_dir_partA_lab = osp.join(self.augmented_dir_partA, 'test_lab')
        mkdir_if_missing(aug_dir_partA_img)
        mkdir_if_missing(aug_dir_partA_den)
        mkdir_if_missing(aug_dir_partA_lab)
        mkdir_if_missing(test_dir_partA_img)
        mkdir_if_missing(test_dir_partA_den)
        mkdir_if_missing(test_dir_partA_lab)

        kwargs['name'] = 'shanghai-partA'
        kwargs["val"] = False
        part_A_train_test = train_test_unit(aug_dir_partA_img,
                                            aug_dir_partA_den,
                                            test_dir_partA_img,
                                            test_dir_partA_den, kwargs.copy())
        self.train_test_set.append(part_A_train_test)

        if augment_data_A:
            ori_img_paths = [
                osp.join(self.ori_dir_partA_train_img, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partA_train_img))
            ]
            ori_lab_paths = [
                osp.join(self.ori_dir_partA_train_lab, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partA_train_lab))
            ]
            ori_den_paths = [
                osp.join(self.ori_dir_partA_train_den, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partA_train_den))
            ]
            augment(ori_img_paths,
                    ori_lab_paths,
                    ori_den_paths,
                    aug_dir_partA_img,
                    aug_dir_partA_lab,
                    aug_dir_partA_den,
                    slide_window_params,
                    noise_params,
                    light_params,
                    downscale_img=downscale,
                    ten_crop=ten_crop)

            test_img_paths = [
                osp.join(self.ori_dir_partA_test_img,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partA_test_img))
            ]
            test_lab_paths = [
                osp.join(self.ori_dir_partA_test_lab,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partA_test_lab))
            ]
            test_den_paths = [
                osp.join(self.ori_dir_partA_test_den,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partA_test_den))
            ]
            if downscale:
                downscale_test_images(test_img_paths, test_dir_partA_img,
                                      test_lab_paths, test_dir_partA_lab,
                                      test_den_paths, test_dir_partA_den,
                                      slide_window_params['size_x'],
                                      slide_window_params['size_y'])
            elif ten_crop:
                resize_test_img_ten_crop_mode(
                    test_img_paths, test_dir_partA_img, test_lab_paths,
                    test_dir_partA_lab, test_den_paths, test_dir_partA_den)
            else:
                copy_to_directory(test_img_paths, test_dir_partA_img)
                copy_to_directory(test_lab_paths, test_dir_partA_lab)
                copy_to_directory(test_den_paths, test_dir_partA_den)

        #shanghaiTech part B
        self.augmented_dir_partB = osp.join(self.ori_dir_partB,
                                            self.signature())
        augment_data_B = False
        if osp.exists(self.augmented_dir_partB):
            print("'{}' already exists".format(self.augmented_dir_partB))
            if force_augmentation:
                augment_data_B = True
                print("augmenting data anyway")
            else:
                augment_data_B = False
                print("will not augmenting data")
        else:
            augment_data_B = True
            os.makedirs(self.augmented_dir_partB)

        aug_dir_partB_img = osp.join(self.augmented_dir_partB, "train_img")
        aug_dir_partB_den = osp.join(self.augmented_dir_partB, "train_den")
        aug_dir_partB_lab = osp.join(self.augmented_dir_partB, "train_lab")
        test_dir_partB_img = osp.join(self.augmented_dir_partB, 'test_img')
        test_dir_partB_den = osp.join(self.augmented_dir_partB, 'test_den')
        test_dir_partB_lab = osp.join(self.augmented_dir_partB, 'test_lab')
        mkdir_if_missing(aug_dir_partB_img)
        mkdir_if_missing(aug_dir_partB_den)
        mkdir_if_missing(aug_dir_partB_lab)
        mkdir_if_missing(test_dir_partB_img)
        mkdir_if_missing(test_dir_partB_den)
        mkdir_if_missing(test_dir_partB_lab)

        kwargs['name'] = 'shanghai-partB'
        kwargs["val"] = False
        part_B_train_test = train_test_unit(aug_dir_partB_img,
                                            aug_dir_partB_den,
                                            test_dir_partB_img,
                                            test_dir_partB_den, kwargs.copy())
        self.train_test_set.append(part_B_train_test)

        if augment_data_B:
            ori_img_paths = [
                osp.join(self.ori_dir_partB_train_img, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partB_train_img))
            ]
            ori_lab_paths = [
                osp.join(self.ori_dir_partB_train_lab, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partB_train_lab))
            ]
            ori_den_paths = [
                osp.join(self.ori_dir_partB_train_den, file_name)
                for file_name in sorted(
                    os.listdir(self.ori_dir_partB_train_den))
            ]
            augment(ori_img_paths,
                    ori_lab_paths,
                    ori_den_paths,
                    aug_dir_partB_img,
                    aug_dir_partB_lab,
                    aug_dir_partB_den,
                    slide_window_params,
                    noise_params,
                    light_params,
                    downscale_img=downscale,
                    ten_crop=ten_crop)

            test_img_paths = [
                osp.join(self.ori_dir_partB_test_img,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partB_test_img))
            ]
            test_lab_paths = [
                osp.join(self.ori_dir_partB_test_lab,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partB_test_lab))
            ]
            test_den_paths = [
                osp.join(self.ori_dir_partB_test_den,
                         file_name) for file_name in sorted(
                             os.listdir(self.ori_dir_partB_test_den))
            ]
            if downscale:
                downscale_test_images(test_img_paths, test_dir_partB_img,
                                      test_lab_paths, test_dir_partB_lab,
                                      test_den_paths, test_dir_partB_den,
                                      slide_window_params['size_x'],
                                      slide_window_params['size_y'])
            elif ten_crop:
                resize_test_img_ten_crop_mode(
                    test_img_paths, test_dir_partB_img, test_lab_paths,
                    test_dir_partB_lab, test_den_paths, test_dir_partB_den)
            else:
                copy_to_directory(test_img_paths, test_dir_partB_img)
                copy_to_directory(test_lab_paths, test_dir_partB_lab)
                copy_to_directory(test_den_paths, test_dir_partB_den)