예제 #1
0
def accimage_loader(path):
    try:
        import accimage
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)
예제 #2
0
    def __getitem__(self, idx):
        if self.image_num_limit is not None and len(self.image_paths[idx]) > self.image_num_limit:
            image_path_list = sorted(sample(self.image_paths[idx], self.image_num_limit))
        else:
            image_path_list = self.image_paths[idx]
        image_list = [accimage.Image(x) for x in image_path_list]
        if self.transform:
            image_list = [self.transform(img) for img in image_list]
        if self.channel_first:
            image_tensor = torch.stack(image_list, dim=1)
        else:
            image_tensor = torch.stack(image_list, dim=0)
        labels = self.labels_list[idx]

        if self.transform_3d:
            image_tensor = self.transform_3d(image_tensor)

        if self.channel_first:
            channel_num, image_num, image_height, image_width = image_tensor.size()
            if self.image_num_limit is not None:
                input_tensor = torch.zeros(channel_num, self.image_num_limit, image_height, image_width)
            else:
                input_tensor = torch.zeros(channel_num, self.max_image_num, image_height, image_width)
            input_tensor[:, :image_num, :, :] = image_tensor
        else:
            image_num, channel_num, image_height, image_width = image_tensor.size()
            if self.image_num_limit is not None:
                input_tensor = torch.zeros(self.image_num_limit, channel_num, image_height, image_width)
            else:
                input_tensor = torch.zeros(self.max_image_num, channel_num, image_height, image_width)
            input_tensor[:image_num, :, :, :] = image_tensor

        return input_tensor, labels
예제 #3
0
    def __getitem__(self, idx):
        image_path_list = self.image_paths[idx]
        image_list = [accimage.Image(x) for x in image_path_list]
        if self.transform:
            image_list = [self.transform(img) for img in image_list]
        labels = self.labels_list[idx]

        return image_list, labels
예제 #4
0
 def accimage_loader(self, path):
     """Accimage image loader."""
     import accimage
     try:
         return accimage.Image(path)
     except IOError:
         # Potentially a decoding problem, fall back to PIL.Image
         return self.pil_loader(path)
예제 #5
0
def accimage_loader(path, *, n_channel):
    import accimage
    try:
        img = accimage.Image(path)
        return img.convert('RGB') if n_channel == 3 else img.convert('L')
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path, n_channel=n_channel)
예제 #6
0
    def __getitem__(self, idx):
        image = accimage.Image(self.image_path[idx])
        labels = self.new_labels_list[idx]

        if self.transform:
            image = self.transform(image)

        return image, labels
예제 #7
0
def accimage_loader(path):
    torchvision.set_image_backend('accimage')
    import accimage
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)
예제 #8
0
def accimage_loader(path):
    # accimge:高性能图像加载和增强程序模拟的程序。
    import accimage
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)
예제 #9
0
    def test_accimage_to_tensor(self):
        trans = transforms.ToTensor()

        expected_output = trans(Image.open(GRACE_HOPPER).convert('RGB'))
        output = trans(accimage.Image(GRACE_HOPPER))

        self.assertEqual(expected_output.size(), output.size())
        assert np.allclose(output.numpy(), expected_output.numpy())
예제 #10
0
def accimage_loader(path):
    try:
        import accimage
        return accimage.Image(path)
    except ModuleNotFoundError:
        # Potentially a decoding problem, fall back to PIL.Image
        torchvision.set_image_backend('PIL')
        return pil_loader(path)
예제 #11
0
def accimage_loader(path: str) -> Any:
    import accimage

    try:
        return accimage.Image(path)
    except OSError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)
def accimage_loader(path):
    print("can't find acc image loader")
    import accimage
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)
예제 #13
0
파일: data.py 프로젝트: HNoodles/trojanzoo
 def zip_loader(self, path: str) -> Image.Image:
     f = io.BytesIO(self.root_zip.read(path))
     if get_image_backend() == 'accimage':
         try:
             import accimage  # type: ignore
             return accimage.Image(f)
         except IOError:
             pass  # fall through to PIL
     return Image.open(f).convert('RGB')
예제 #14
0
파일: data_list.py 프로젝트: tl32rodan/MDD
def accimage_loader(path, root_folder=None):
    import accimage
    if not (root_folder is None):
        path = osp.join(root_folder, path)
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)
예제 #15
0
def pil_loader(path):
    # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    with open(path, 'rb') as f:
        try:
            img = Image.open(f)
            return img.convert('RGB')
        except:
            import accimage
            return accimage.Image(path)
예제 #16
0
 def tar_loader(self, path: str) -> Image.Image:
     f = self.root_data.extract(path)
     if get_image_backend() == 'accimage':
         try:
             import accimage  # type: ignore
             return accimage.Image(f)
         except IOError:
             pass  # fall through to PIL
     return Image.open(f).convert('RGB')
예제 #17
0
파일: test.py 프로젝트: xvdp/accimage
def test_cropping():
    image = accimage.Image("chicago.jpg")

    image.crop(box=(50, 50, 150, 150))
    if SAVE_IMAGES:
        save_image('test_cropping.jpg', image)

    assert image.width == 100
    assert image.height == 100
예제 #18
0
def accimage_loader(path):
    """Accimage loader for accelebrating loading image.
    """
    try:
        import accimage
        return accimage.Image(path)
    except IOError:
        # Potentionally a decoding problem, fall back to PIL.image
        return pil_loader(path)
예제 #19
0
 def zip_loader(self, path) -> Any:
     f = self.root_zip[path]
     if get_image_backend() == 'accimage':
         try:
             import accimage  # type: ignore
             return accimage.Image(f)
         except IOError:
             pass  # fall through to PIL
     return Image.open(f).convert('RGB')
예제 #20
0
파일: test.py 프로젝트: xvdp/accimage
def test_resizing():
    image = accimage.Image("chicago.jpg")

    image.resize(size=(200, 200))
    if SAVE_IMAGES:
        save_image('test_resizing.jpg', image)

    assert image.width == 200
    assert image.height == 200
예제 #21
0
 def Sample_Image(imgs_path, sl):
     frams = []
     for a in sl:
         img = transform(
             image_to_np(
                 accimage.Image(os.path.join(imgs_path,
                                             "%06d.jpg" % a))))
         frams.append(
             self.transform(img).view(3, sample_size, sample_size, 1))
     return torch.cat(frams, dim=3).type(torch.FloatTensor)
예제 #22
0
def accimage_loader(path):
    import accimage
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        print('acc_loader', IOError)
        try:
            return pil_loader(path)
        except IOError:
            print('pil_loader', IOError)
예제 #23
0
    def test_accimage_crop(self):
        trans = transforms.Compose([
            transforms.CenterCrop(256),
            transforms.ToTensor(),
        ])

        expected_output = trans(Image.open(GRACE_HOPPER).convert('RGB'))
        output = trans(accimage.Image(GRACE_HOPPER))

        self.assertEqual(expected_output.size(), output.size())
        assert np.allclose(output.numpy(), expected_output.numpy())
예제 #24
0
def accimage_loader(path):
    '''
    ---------------------------------------------------------------------------
    code reference: https://github.com/pytorch/vision/blob/master/torchvision/datasets/folder.py
    '''

    import accimage
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)
예제 #25
0
def accimage_loader(path):
    """
    compared with PIL, accimage loader eliminates useless function within class, so that it is faster than PIL
    :param path: image path
    :return: image data
    """
    try:
        import accimage
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)
예제 #26
0
def pil_loader(path):
    # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    with open(path, 'rb') as f:
        img = Image.open(f)
        return img.convert('RGB')

    import accimage
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)
예제 #27
0
def accimage_loader(path):
    '''
    Ref:
    https://pytorch.org/docs/stable/_modules/torchvision/datasets/folder.html#ImageFolder
    accimage is an accelerated Image loader and preprocessor leveraging Intel IPP.
    accimage is available on conda-forge.
    '''
    import accimage
    try:
        return accimage.Image(path)
    except IOError:
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)
예제 #28
0
def accimage_loader(path):
    # Tries to use accimage with PIL fallback.

    is_accimage_avail = importlib.find_loader('accimage')
    if is_accimage_avail:
        import accimage
    else:
        return pil_loader(path)
    try:
        return accimage.Image(path)
    except IOError as e:
        print("WARN: Exception in accimage_loader: {}".format(e))
        return pil_loader(path)
예제 #29
0
파일: test.py 프로젝트: xvdp/accimage
def test_flipping():
    image = accimage.Image("chicago.jpg")
    original_image_np = image_to_np(image)

    FLIP_LEFT_RIGHT = 0
    image.transpose(FLIP_LEFT_RIGHT)
    if SAVE_IMAGES:
        save_image('test_flipping.jpg', image)

    new_image_np = image_to_np(image)
    assert image.width == 1920
    assert image.height == 931
    np.testing.assert_array_equal(new_image_np[:, ::-1, :], original_image_np)
예제 #30
0
def accimage_loader(path):
    import accimage
    try:
        start = time.time()
        img = accimage.Image(path)
        end = time.time()
        logger.debug('accimage decode {:.3f} ms'.format(1000 * (end - start)))

        return img
    except IOError:
        logger.warn('accimage failed to decode {}'.format(path))
        # Potentially a decoding problem, fall back to PIL.Image
        return pil_loader(path)