Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimally support accimage.Image #15

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions test/preprocess-bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import torch.utils.data
import torchvision.transforms as transforms
import torchvision.datasets as datasets
try:
import accimage
print("Using accimage.Image")
except ImportError:
print("Using PIL.Image")


parser = argparse.ArgumentParser(description='PyTorch ImageNet Training')
Expand Down
13 changes: 12 additions & 1 deletion torchvision/datasets/folder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import torch.utils.data as data

from PIL import Image
try:
import accimage
except ImportError:
accimage = None
import os
import os.path

Expand Down Expand Up @@ -47,7 +51,14 @@ def __init__(self, root, transform=None, target_transform=None):

def __getitem__(self, index):
path, target = self.imgs[index]
img = Image.open(os.path.join(self.root, path)).convert('RGB')
if accimage is None:
img = Image.open(os.path.join(self.root, path)).convert('RGB')
else:
try:
img = accimage.Image(os.path.join(self.root, path))
except IOError:
# Potentially a decoding problem, fall back to PIL.Image
img = Image.open(os.path.join(self.root, path)).convert('RGB')
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
Expand Down
10 changes: 9 additions & 1 deletion torchvision/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import math
import random
from PIL import Image, ImageOps
try:
import accimage
except ImportError:
accimage = None
import numpy as np
import numbers
import types
Expand All @@ -28,7 +32,11 @@ class ToTensor(object):
""" Converts a PIL.Image (RGB) or numpy.ndarray (H x W x C) in the range [0, 255]
to a torch.FloatTensor of shape (C x H x W) in the range [0.0, 1.0] """
def __call__(self, pic):
if isinstance(pic, np.ndarray):
if accimage is not None and isinstance(pic, accimage.Image):
nppic = np.empty([pic.channels, pic.height, pic.width])
pic.copyto(nppic)
img = torch.from_numpy(nppic)
elif isinstance(pic, np.ndarray):
# handle numpy array
img = torch.from_numpy(pic)
else:
Expand Down