Ejemplo n.º 1
0
def apply_dir(img_path,
              save_path,
              crop_size=(128, 128),
              scale=1,
              blur_algos=['clean'],
              noise_types=['clean'],
              noise_types2=['clean']):
    img_list = _get_paths_from_dir(img_dir)

    for path in img_list:
        rann = ''
        env = None
        img = util.read_img(env, path)
        import uuid
        rann = uuid.uuid4().hex

        img_crop = random_crop(img, crop_size)
        print(img_crop.shape)
        #"""
        cv2.imwrite(save_path + '/' + rann + 'mask_.png', img_crop * 255)
        #"""

        img_resize, _ = resize_img(img, crop_size)
        print(img_resize.shape)
        #"""
        cv2.imwrite(save_path + '/' + rann + 'resize_.png', img_resize * 255)
        #"""

        img_random_resize, _ = random_resize_img(img, crop_size)
        print(img_random_resize.shape)
        #"""
        cv2.imwrite(save_path + '/' + rann + 'random_resize_.png',
                    img_random_resize * 255)
        #"""

        img_cutout = cutout(img, img.shape[0] // 2)
        print(img_cutout.shape)
        #"""
        cv2.imwrite(save_path + '/' + rann + 'cutout_.png', img_cutout * 255)
        #"""

        img_erasing = random_erasing(img)
        print(img_erasing.shape)
        #"""
        cv2.imwrite(save_path + '/' + rann + 'erasing_.png', img_erasing * 255)
        #"""

        #scale = 4
        img_scale, interpol_algo = scale_img(img, scale)
        print(img_scale.shape)
        #"""
        cv2.imwrite(
            save_path + '/' + rann + 'scale_' + str(scale) + '_' +
            str(interpol_algo) + '_.png', img_scale * 255)
        #"""

        img_blur, blur_algo, blur_kernel_size = blur_img(img, blur_algos)
        print(img_blur.shape)
        #"""
        cv2.imwrite(
            save_path + '/' + rann + 'blur_' + str(blur_kernel_size) + '_' +
            str(blur_algo) + '_.png', img_blur * 255)
        #cv2.imwrite(save_path+'/blur__.png',img_blur*255)
        #"""

        img_noise, noise_algo = noise_img(img, noise_types)
        #img_noise, noise_algo = noise_img(img_scale, noise_types)
        print(img_noise.shape)
        #"""
        cv2.imwrite(
            save_path + '/' + rann + 'noise_' + str(noise_algo) + '_.png',
            img_noise * 255)
        #"""

        img_noise2, noise_algo2 = noise_img(img_noise, noise_types2)
        print(img_noise2.shape)
        #"""
        cv2.imwrite(
            save_path + '/' + rann + 'noise2_' + str(noise_algo2) + '_.png',
            img_noise2 * 255)
        #"""

        img_rrot, angle = random_rotate(img)
        print(img_rrot.shape)
        #"""
        cv2.imwrite(save_path + '/' + rann + 'rrot_' + str(angle) + '_.png',
                    img_rrot * 255)
        #"""

    print('Finished')
Ejemplo n.º 2
0
    def __getitem__(self, idx):
        args = self.args
        annot = self.annotations[idx]

        t_s = self._getT(annot["subject"]["bbox"], annot["object"]["bbox"])
        t_o = self._getT(annot["object"]["bbox"], annot["subject"]["bbox"])

        ys0, ys1, xs0, xs1 = annot["subject"]["bbox"]
        yo0, yo1, xo0, xo1 = annot["object"]["bbox"]

        datum = {
            "url": annot["url"],
            "_id": annot["_id"],
            "subject": {
                "name": annot["subject"]["name"],
                "embedding": phrase2vec(
                    annot["subject"]["name"], self.args.max_phrase_len, 300
                ),
                "bbox": np.asarray(
                    [
                        ys0 / annot["height"],
                        ys1 / annot["height"],
                        xs0 / annot["width"],
                        xs1 / annot["width"],
                    ],
                    dtype=np.float32,
                ),
                "t": np.asarray(t_s, dtype=np.float32),
            },
            "object": {
                "name": annot["object"]["name"],
                "embedding": phrase2vec(
                    annot["object"]["name"], self.args.max_phrase_len, 300
                ),
                "bbox": np.asarray(
                    [
                        yo0 / annot["height"],
                        yo1 / annot["height"],
                        xo0 / annot["width"],
                        xo1 / annot["width"],
                    ],
                    dtype=np.float32,
                ),
                "t": np.asarray(t_o, dtype=np.float32),
            },
            "label": np.asarray([[annot["label"]]], dtype=np.float32),
            "predicate": onehot(args.predicate_categories.index(annot["predicate"]), 9),
            "predicate_name": annot["predicate"],
        }

        if self.split == "test":
            del datum["label"]

        if self.load_image:
            img = read_img(annot["url"], self.args.imagepath)
            ih, iw = img.shape[:2]

            if "train" in self.split:
                t_bbox = transforms.Compose(
                    [
                        transforms.ToPILImage("RGB"),
                        transforms.Pad(4, padding_mode="edge"),
                        transforms.RandomResizedCrop(32, scale=(0.75, 0.85)),
                        transforms.ToTensor(),
                    ]
                )
            else:
                t_bbox = transforms.Compose(
                    [
                        transforms.ToPILImage("RGB"),
                        transforms.Pad(4, padding_mode="edge"),
                        transforms.CenterCrop(32),
                        transforms.ToTensor(),
                    ]
                )
            bbox_mask = np.stack(
                [
                    self._getDualMask(ih, iw, annot["subject"]["bbox"], 32).astype(
                        np.uint8
                    ),
                    self._getDualMask(ih, iw, annot["object"]["bbox"], 32).astype(
                        np.uint8
                    ),
                    np.zeros((32, 32), dtype=np.uint8),
                ],
                2,
            )
            bbox_mask = t_bbox(bbox_mask)[:2].float() / 255.0
            datum["bbox_mask"] = bbox_mask

            union_bbox = self.enlarge(
                self._getUnionBBox(
                    annot["subject"]["bbox"], annot["object"]["bbox"], ih, iw
                ),
                1.25,
                ih,
                iw,
            )
            if "train" in self.split:
                t_bboximg = transforms.Compose(
                    [
                        transforms.ToPILImage("RGB"),
                        transforms.RandomResizedCrop(224, scale=(0.75, 0.85)),
                        transforms.ColorJitter(0.1, 0.1, 0.1, 0.05),
                        transforms.ToTensor(),
                    ]
                )
            else:
                t_bboximg = transforms.Compose(
                    [
                        transforms.ToPILImage("RGB"),
                        transforms.CenterCrop(224),
                        transforms.ToTensor(),
                    ]
                )
            bbox_img = t_bboximg(self._getAppr(img, union_bbox))
            datum["bbox_img"] = bbox_img

            if "train" in self.split:
                t_fullimg = transforms.Compose(
                    [
                        transforms.ToPILImage("RGB"),
                        transforms.ColorJitter(0.1, 0.1, 0.1, 0.05),
                        transforms.ToTensor(),
                    ]
                )
            else:
                t_fullimg = transforms.Compose(
                    [transforms.ToPILImage("RGB"), transforms.ToTensor(),]
                )
            if self.args.model == "vipcnn":
                img_size = 400
            elif self.args.model == "pprfcn":
                img_size = 720
            else:
                img_size = 224
            datum["full_img"] = t_fullimg(self._getAppr(img, [0, ih, 0, iw], img_size))

        return datum
Ejemplo n.º 3
0
def single_image(img_path,
                 save_path,
                 crop_size=(128, 128),
                 scale=1,
                 blur_algos=['clean'],
                 noise_types=['clean'],
                 noise_types2=['clean']):
    env = None
    img = util.read_img(
        env, img_path
    )  #read image from path, opens with OpenCV, value ranges from 0 to 1
    print(img.shape)

    img_crop = random_crop(img, crop_size)
    print(img_crop.shape)
    #"""
    cv2.imwrite(save_path + '/crop_.png', img_crop * 255)
    #"""

    img_resize, _ = resize_img(img, crop_size)
    print(img_resize.shape)
    #"""
    cv2.imwrite(save_path + '/resize_.png', img_resize * 255)
    #"""

    img_random_resize, _ = random_resize_img(img, crop_size)
    print(img_random_resize.shape)
    #"""
    cv2.imwrite(save_path + '/random_resize_.png', img_random_resize * 255)
    #"""

    img_cutout = cutout(img, img.shape[0] // 2)
    print(img_cutout.shape)
    #"""
    cv2.imwrite(save_path + '/cutout_.png', img_cutout * 255)
    #"""

    img_erasing = random_erasing(img)
    print(img_erasing.shape)
    #"""
    cv2.imwrite(save_path + '/erasing_.png', img_erasing * 255)
    #"""

    #scale = 4
    img_scale, interpol_algo = scale_img(img, scale)
    print(img_scale.shape)
    #"""
    cv2.imwrite(
        save_path + '/scale_' + str(scale) + '_' + str(interpol_algo) +
        '_.png', img_scale * 255)
    #"""

    img_blur, blur_algo, blur_kernel_size = blur_img(img, blur_algos)
    print(img_blur.shape)
    #"""
    cv2.imwrite(
        save_path + '/blur_' + str(blur_kernel_size) + '_' + str(blur_algo) +
        '_.png', img_blur * 255)
    #"""

    img_noise, noise_algo = noise_img(img, noise_types)
    #img_noise, noise_algo = noise_img(img_scale, noise_types)
    print(img_noise.shape)
    #"""
    cv2.imwrite(save_path + '/noise_' + str(noise_algo) + '_.png',
                img_noise * 255)
    #"""

    img_noise2, noise_algo2 = noise_img(img_noise, noise_types2)
    print(img_noise2.shape)
    #"""
    cv2.imwrite(save_path + '/noise2_' + str(noise_algo2) + '_.png',
                img_noise2 * 255)
    #"""

    img_rrot, angle = random_rotate(img)
    print(img_rrot.shape)
    #"""
    cv2.imwrite(save_path + '/rrot_' + str(angle) + '_.png', img_rrot * 255)
    #"""

    print('Finished')
Ejemplo n.º 4
0
    def __getitem__(self, idx):
        args = self.args
        annot = self.annotations[idx]

        t_s = self._getT(annot['subject']['bbox'], annot['object']['bbox'])
        t_o = self._getT(annot['object']['bbox'], annot['subject']['bbox'])
        
        xs0, xs1, ys0, ys1 = annot['subject']['bbox']
        xo0, xo1, yo0, yo1 = annot['object']['bbox']


        datum = {'url': annot['url'],
                '_id': annot['_id'],
                'subject': {'name': annot['subject']['name'],
                            'embedding': phrase2vec(annot['subject']['name'], 
                                                    self.args.max_phrase_len, 300),
                            'bbox': np.asarray([xs0 / annot['height'], xs1 / annot['height'], 
                                                ys0 / annot['width'], ys1 / annot['width']], dtype=np.float32),
                            't': np.asarray(t_s, dtype=np.float32)},
                'object': {'name': annot['object']['name'],
                            'embedding': phrase2vec(annot['object']['name'],
                                                    self.args.max_phrase_len, 300),
                            'bbox': np.asarray([xo0 / annot['height'], xo1 / annot['height'], 
                                                yo0 / annot['width'], yo1 / annot['width']], dtype=np.float32),
                            't': np.asarray(t_o, dtype=np.float32)},
                'label': np.asarray([[annot['label']]], dtype=np.float32),
                'predicate': onehot(args.predicate_categories.index(annot['predicate']), 9),
                'predicate_name': annot['predicate'],
                }
    
        if self.split == 'test':
            del datum['label']

        if self.load_image:
            img = read_img(annot['url'], self.args.imagepath)
            ih, iw = img.shape[:2]

            if 'train' in self.split:
                t_bbox = transforms.Compose([
                transforms.ToPILImage('RGB'),
                transforms.Pad(4, padding_mode='edge'),
                transforms.RandomResizedCrop(32, scale=(0.75, 0.85)),
                transforms.ToTensor(),
                ])
            else:
                t_bbox = transforms.Compose([
                transforms.ToPILImage('RGB'),
                transforms.Pad(4, padding_mode='edge'),
                transforms.CenterCrop(32),
                transforms.ToTensor(),
                ])
            bbox_mask = np.stack([self._getDualMask(ih, iw, annot['subject']['bbox'], 32).astype(np.uint8), 
                                    self._getDualMask(ih, iw, annot['object']['bbox'], 32).astype(np.uint8), 
                                    np.zeros((32, 32), dtype=np.uint8)], 2)
            bbox_mask = t_bbox(bbox_mask)[:2].float() / 255.
            datum['bbox_mask'] = bbox_mask

            union_bbox = self.enlarge(self._getUnionBBox(annot['subject']['bbox'], annot['object']['bbox'], ih, iw), 1.25, ih, iw)
            if 'train' in self.split:
                t_bboximg = transforms.Compose([
                transforms.ToPILImage('RGB'),
                transforms.RandomResizedCrop(224, scale=(0.75, 0.85)),
                transforms.ColorJitter(0.1, 0.1, 0.1, 0.05),
                transforms.ToTensor(),
                ]) 
            else:
                t_bboximg = transforms.Compose([
                transforms.ToPILImage('RGB'),
                transforms.CenterCrop(224),
                transforms.ToTensor(),
                ])
            bbox_img = t_bboximg(self._getAppr(img, union_bbox))
            datum['bbox_img'] = bbox_img

            if 'train' in self.split:
                t_fullimg = transforms.Compose([
                transforms.ToPILImage('RGB'),
                transforms.ColorJitter(0.1, 0.1, 0.1, 0.05),
                transforms.ToTensor(),
                ])
            else:
                t_fullimg = transforms.Compose([
                transforms.ToPILImage('RGB'),
                transforms.ToTensor(),
                ])
            if self.args.model == 'vipcnn':
                img_size = 400
            elif self.args.model == 'pprfcn':
                img_size = 720
            else:
                img_size = 224  
            datum['full_img'] = t_fullimg(self._getAppr(img, [0, ih, 0, iw], img_size))

        return datum