Exemple #1
0
    def transforms_tr(self, sample):
        if sample['image'].shape[0] < 720 or sample['image'].shape[1] < 960:
            composed_transforms = transforms.Compose([
                tr.Resize((720, 960)),
                tr.ToTensor()])

        else:
            composed_transforms = transforms.Compose([
                tr.RandomCrop((720, 960)),
                tr.ToTensor()])
        return composed_transforms(sample)
    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()

        l_p_s = self.large_patch_size
        n_p_p_i = self.number_patch_per_image
        n_s_p_p_i = self.image_initial_size // self.patch_size
        p_s = self.patch_size
        l_p_s = self.large_patch_size

        image_index = idx // n_p_p_i
        patch_index = idx % n_p_p_i

        padding = (l_p_s - p_s) // 2

        y = ((patch_index % n_s_p_p_i) * p_s) + padding
        x = ((patch_index // n_s_p_p_i) * p_s) + padding

        image_sample = self._get_image_and_gt(image_index)
        image = image_sample["image"]
        groundtruth = image_sample["groundtruth"]

        small_image = F.crop(image, x - padding, y - padding, l_p_s, l_p_s)
        small_groundtruth = F.crop(groundtruth, x - padding, y - padding, l_p_s, l_p_s)

        sample = {"image": small_image, "groundtruth": small_groundtruth}

        transformation = transformations.ToTensor()
        sample = transformation(sample)

        return sample
Exemple #3
0
 def transforms_tr(self, sample):
     prob_augmentation = random.random()
     # if prob_augmentation > 0.5:
     #     augmentation_strength = int(np.random.uniform(10, 30))
     #     composed_transforms = transforms.Compose([
     #         tr.RandomCrop((720, 960)),
     #         tr.RandAugment(3, augmentation_strength),
     #         tr.ToTensor()])
     # else:
     composed_transforms = transforms.Compose(
         [tr.RandomCrop((720, 960)),
          tr.ToTensor()])
     return composed_transforms(sample)
    def _get_transforms(self):

        padding = (self.large_patch_size - self.patch_size) // 2

        im_size1 = self.image_initial_size + 2 * padding
        im_size2 = self.image_initial_size

        rot_padding = math.ceil(math.ceil((im_size1 * math.sqrt(2)) - im_size2) / 2)
        transforms_list = [
            transformations.Pad(padding, padding_mode="symmetric"),
            transformations.RandomHorizontalFlip(p=1),
            transformations.RandomVerticalFlip(p=1),
            transformations.Pad(rot_padding, padding_mode="symmetric"),
            transformations.RandomRotation(degrees=90),
            transformations.CenterCrop(self.image_initial_size + 2 * padding),
            transformations.ColorJitter(),
            transformations.ToTensor(),
            # TODO: Right params for normalize, can be the mean and std of imageNet
            transformations.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),
            transformations.ToPILImage(),
        ]

        return transforms.Compose(transforms_list)
Exemple #5
0
def prepare_data(csv_1="csv_files/annotations.csv",
                 csv_2="csv_files/images.csv",
                 data_dir="/mnt/data/data/summer_2018/resized_target_files/",
                 batch_size=256,
                 shuffle=None,
                 num_workers=4):
    # csv_annot : [id, image_id, annotation_user_id, annotation_time, annotation_value]
    csv_annots = pd.read_csv(csv_1)
    csv_annots = csv_annots[csv_annots["annotation_user_id"] > 6]
    # csv_images : [id, md5sum, file_name, file_path, source, annotated, reserved_user_id]
    csv_images = pd.read_csv(csv_2)
    image_names = list(
        map(lambda s: os.path.basename(s),
            glob.glob(os.path.join(data_dir, "*.jpg"))))
    our_images = csv_images[csv_images["file_name"].isin(image_names)]
    labeled_images = our_images.merge(
        csv_annots, on="id")[["file_name", "annotation_value"]]
    strip = lambda x: x.lstrip(" u'").rstrip("'")
    labeled_images["annotation_value"] = labeled_images[
        "annotation_value"].map(strip)
    # tester icin discard disari cikarildi
    #labeled_images = labeled_images[labeled_images["annotation_value"].isin(["receipt", "discard", "invoice", "slip", "inforeceipt", "fisandslip"])]

    labeled_images = labeled_images[labeled_images["annotation_value"].isin(
        ["receipt", "invoice", "slip", "inforeceipt", "fisandslip"])]
    classes = {}
    classes["receipt"] = len(
        labeled_images[labeled_images["annotation_value"] == "receipt"])
    classes["invoice"] = len(
        labeled_images[labeled_images["annotation_value"] == "invoice"])
    classes["slip"] = len(
        labeled_images[labeled_images["annotation_value"] == "slip"])
    classes["inforeceipt"] = len(
        labeled_images[labeled_images["annotation_value"] == "inforeceipt"])
    classes["fisandslip"] = len(
        labeled_images[labeled_images["annotation_value"] == "fisandslip"])
    classes["discard"] = len(
        labeled_images[labeled_images["annotation_value"] == "discard"])
    print(classes["receipt"], classes["invoice"], classes["slip"],
          classes["inforeceipt"], classes["fisandslip"])

    clean_labeled_images = labeled_images
    clean_labeled_images.to_csv("csv_files/loader.csv", index=None)

    img_name = clean_labeled_images.iloc[:, 0]
    img_label = clean_labeled_images.iloc[:, 1]

    label_map = {}
    for i, label in enumerate(
            pd.read_csv("csv_files/loader.csv")["annotation_value"].unique()):
        label_map[label] = np.array(i)

    clean = pd.read_csv("csv_files/loader.csv")

    train_size = int(len(clean) * 0.8)
    clean_train = clean[:train_size]

    clean_train.to_csv("csv_files/clean_train.csv", index=False)

    test_size = int((len(clean) - train_size) / 2)
    clean_test = clean[train_size:(train_size + test_size)]

    clean_test.to_csv("csv_files/clean_test.csv", index=False)
    val_size = len(clean) - train_size - test_size

    clean_val = clean[(train_size + test_size):]
    clean_val.to_csv("csv_files/clean_val.csv", index=False)

    new_transforms = [
        tr.RandomCrop(224),
        tr.ToTensor(),
    ]

    sample_train = NewDataset("csv_files/clean_train.csv",
                              data_dir,
                              label_map=label_map,
                              transform=transforms.Compose(new_transforms))

    train_loader = DataLoader(sample_train,
                              batch_size=batch_size,
                              shuffle=True,
                              num_workers=num_workers,
                              pin_memory=True)

    sample_test = NewDataset("csv_files/clean_test.csv",
                             data_dir,
                             label_map=label_map,
                             transform=transforms.Compose([
                                 tr.RandomCrop(224),
                                 tr.ToTensor(),
                             ]))

    test_loader = DataLoader(sample_test,
                             batch_size=batch_size,
                             shuffle=True,
                             num_workers=num_workers,
                             pin_memory=True)

    sample_val = NewDataset("csv_files/clean_val.csv",
                            data_dir,
                            label_map=label_map,
                            transform=transforms.Compose([
                                tr.RandomCrop(224),
                                tr.ToTensor(),
                            ]))

    val_loader = DataLoader(sample_val,
                            batch_size=batch_size,
                            shuffle=True,
                            num_workers=num_workers,
                            pin_memory=True)

    samples = {"train": train_loader, "test": test_loader, "val": val_loader}
    dataset_sizes = {"train": train_size, "test": test_size, "val": val_size}

    return (samples, dataset_sizes, label_map)
Exemple #6
0
 def transforms_valid(self, sample):
     composed_transforms = transforms.Compose(
         [tr.Resize((720, 960)), tr.ToTensor()])
     return composed_transforms(sample)