Pytorch transforms.RandomRotation() does not work on Google Colab - python

Normally i was working on letter&digit recognition on my computer and I wanted to move my project to Colab but unfortunately there was an error (you can see the error below).
after some debugging i found which line is giving me error.
transforms.RandomRotation(degrees=(90, -90))
below i wrote simple abstract code to show this error.This code does not work on colab but it works fine at my own computer environment.Problem might be about the different versions of pytorch library i have version 1.3.1 on my computer and colab uses version 1.4.0.
import torch
import torchvision
from torchvision import datasets, transforms
import matplotlib.pyplot as plt
transformOpt = transforms.Compose([
transforms.RandomRotation(degrees=(90, -90)),
transforms.ToTensor()
])
train_set = datasets.MNIST(
root='', train=True, transform=transformOpt, download=True)
test_set = datasets.MNIST(
root='', train=False, transform=transformOpt, download=True)
train_loader = torch.utils.data.DataLoader(
dataset=train_set,
batch_size=100,
shuffle=True)
test_loader = torch.utils.data.DataLoader(
dataset=test_set,
batch_size=100,
shuffle=False)
images, labels = next(iter(train_loader))
plt.imshow(images[0].view(28, 28), cmap="gray")
plt.show()
The full error I got when I execute this sample code above on Google Colab.
TypeError Traceback (most recent call last)
<ipython-input-1-8409db422154> in <module>()
24 shuffle=False)
25
---> 26 images, labels = next(iter(train_loader))
27 plt.imshow(images[0].view(28, 28), cmap="gray")
28 plt.show()
10 frames
/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in __next__(self)
343
344 def __next__(self):
--> 345 data = self._next_data()
346 self._num_yielded += 1
347 if self._dataset_kind == _DatasetKind.Iterable and \
/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in _next_data(self)
383 def _next_data(self):
384 index = self._next_index() # may raise StopIteration
--> 385 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
386 if self._pin_memory:
387 data = _utils.pin_memory.pin_memory(data)
/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
---> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in <listcomp>(.0)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
---> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
/usr/local/lib/python3.6/dist-packages/torchvision/datasets/mnist.py in __getitem__(self, index)
95
96 if self.transform is not None:
---> 97 img = self.transform(img)
98
99 if self.target_transform is not None:
/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py in __call__(self, img)
68 def __call__(self, img):
69 for t in self.transforms:
---> 70 img = t(img)
71 return img
72
/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py in __call__(self, img) 1001 angle = self.get_params(self.degrees) 1002
-> 1003 return F.rotate(img, angle, self.resample, self.expand, self.center, self.fill) 1004 1005 def
__repr__(self):
/usr/local/lib/python3.6/dist-packages/torchvision/transforms/functional.py in rotate(img, angle, resample, expand, center, fill)
727 fill = tuple([fill] * 3)
728
--> 729 return img.rotate(angle, resample, expand, center, fillcolor=fill)
730
731
/usr/local/lib/python3.6/dist-packages/PIL/Image.py in rotate(self, angle, resample, expand, center, translate, fillcolor) 2003 w, h = nw, nh 2004
-> 2005 return self.transform((w, h), AFFINE, matrix, resample, fillcolor=fillcolor) 2006 2007 def save(self, fp, format=None, **params):
/usr/local/lib/python3.6/dist-packages/PIL/Image.py in transform(self, size, method, data, resample, fill, fillcolor) 2297 raise ValueError("missing method data") 2298
-> 2299 im = new(self.mode, size, fillcolor) 2300 if method == MESH: 2301 # list of quads
/usr/local/lib/python3.6/dist-packages/PIL/Image.py in new(mode, size, color) 2503 im.palette = ImagePalette.ImagePalette() 2504 color = im.palette.getcolor(color)
-> 2505 return im._new(core.fill(mode, size, color)) 2506 2507
TypeError: function takes exactly 1 argument (3 given)

You're absolutely correct. torchvision 0.5 has a bug in RandomRotation() in the fill argument probably due to incompatible Pillow version. This issue has now been fixed (PR#1760) and will be resolved in the next release.
Temporarily, you add fill=(0,) to RandomRotation transform to fix it.
transforms.RandomRotation(degrees=(90, -90), fill=(0,))

Related

Custom transformation on Subset of CIFAR10 - PyTorch

I am trying to create a custom transformation to part of the CIFAR10 data set which superimposing of an image over the dataset. I was able to download the data and divide it into subsets. Using the following code:
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
traindata = datasets.CIFAR10('./data', train=True, download=True,
transform= transform_train)
partitions = 5
traindata_split = torch.utils.data.random_split(traindata, [int(traindata.data.shape[0] / partitions) for _ in range(partitions)])
then I wanted to modify part of the splits so I created the following class and functions to use as as follows:
class MyDataset(Dataset): # https://discuss.pytorch.org/t/torch-utils-data-dataset-random-split/32209/3
def __init__(self, subset, transform=None):
self.subset = subset
self.transform = transform
def __getitem__(self, index):
x, y = self.subset[index]
if self.transform:
x = self.transform(x)
return x, y
def __len__(self):
return len(self.subset)
and
class ImageSuperImpose(object):
""" Image input as PIL and output as PIL
To be used as part of torchvision.transforms
Args: p, a threshold value to control image thinning
"""
def __init__(self, p=0):
self.p = p
def __call__(self, image):
img = cv2.imread('img.jpg')
img = img('float32')/255
imgSm = cv2.resize(img,(32,32))
np_arr = image.cpu().detach().numpy().T
sample = cv2.addWeighted(np_arr, 1, imgSm, 1, 0)
sample = sample.T
t = torch.from_numpy(sample)
return sample
transform_train2 = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
ImagePoisoning(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
datasetA = MyDataset(
traindata_split[0], transform= transform_train2
)
test_loader = torch.utils.data.DataLoader(datasetA, batch_size=128, shuffle=True)
But when I tried to train the model on the subset I got the following error:
RuntimeError: The size of tensor a (32) must match the size of tensor b (3) at non-singleton dimension 0
** UPDATE**
Here is the full given error
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-20-7428084b03be> in <module>()
----> 1 train(model, opt, test_loader, 3)
9 frames
<ipython-input-14-fcb03e1d7685> in client_update(client_model, optimizer, train_loader, epoch)
5 client_model.train()
6 for e in range(epoch):
----> 7 for batch_idx, (data, target) in enumerate(train_loader):
8 data, target = data.to(device), target.to(device)
9 optimizer.zero_grad()
/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in __next__(self)
433 if self._sampler_iter is None:
434 self._reset()
--> 435 data = self._next_data()
436 self._num_yielded += 1
437 if self._dataset_kind == _DatasetKind.Iterable and \
/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in _next_data(self)
473 def _next_data(self):
474 index = self._next_index() # may raise StopIteration
--> 475 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
476 if self._pin_memory:
477 data = _utils.pin_memory.pin_memory(data)
/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
---> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in <listcomp>(.0)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
---> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
<ipython-input-7-1bde43acaff0> in __getitem__(self, index)
7 x, y = self.subset[index]
8 if self.transform:
----> 9 x = self.transform(x)
10 return x, y
11
/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py in __call__(self, img)
65 def __call__(self, img):
66 for t in self.transforms:
---> 67 img = t(img)
68 return img
69
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
--> 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py in forward(self, tensor)
224 Tensor: Normalized Tensor image.
225 """
--> 226 return F.normalize(tensor, self.mean, self.std, self.inplace)
227
228 def __repr__(self):
/usr/local/lib/python3.6/dist-packages/torchvision/transforms/functional.py in normalize(tensor, mean, std, inplace)
282 if std.ndim == 1:
283 std = std.view(-1, 1, 1)
--> 284 tensor.sub_(mean).div_(std)
285 return tensor
286
RuntimeError: The size of tensor a (32) must match the size of tensor b (3) at non-singleton dimension 0

ValueError: tile cannot extend outside image pytorch

I am loading NumPy arrays as images in PyTorch while training the model, it’s giving me this error, I tried everything but couldn’t figure out pls help…, I am training a classifier model.......................................................................................................................................................................................
ValueError Traceback (most recent call last)
<ipython-input-12-b4d3f7be01c1> in <module>
1 # training
----> 2 trained_model = train(n_epochs, np.Inf, loaders, model, optimizer, criterion)
<ipython-input-10-b4d180a2c041> in train(n_epochs, valid_loss_min_input, loaders, model, optimizer, criterion, device, checkpoint_path, best_model_path)
29 ###################
30 model.train()
---> 31 for batch_idx, (data, target) in enumerate(loaders['train']):
32 # move to gpu
33 data, target = data.to(device), target.to(device)
G:\anaconda3\envs\data_env\lib\site-packages\torch\utils\data\dataloader.py in __next__(self)
343
344 def __next__(self):
--> 345 data = self._next_data()
346 self._num_yielded += 1
347 if self._dataset_kind == _DatasetKind.Iterable and \
G:\anaconda3\envs\data_env\lib\site-packages\torch\utils\data\dataloader.py in _next_data(self)
383 def _next_data(self):
384 index = self._next_index() # may raise StopIteration
--> 385 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
386 if self._pin_memory:
387 data = _utils.pin_memory.pin_memory(data)
G:\anaconda3\envs\data_env\lib\site-packages\torch\utils\data\_utils\fetch.py in fetch(self, possibly_batched_index)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
---> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
G:\anaconda3\envs\data_env\lib\site-packages\torch\utils\data\_utils\fetch.py in <listcomp>(.0)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
---> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
G:\anaconda3\envs\data_env\lib\site-packages\torchvision\datasets\folder.py in __getitem__(self, index)
135 sample = self.loader(path)
136 if self.transform is not None:
--> 137 sample = self.transform(sample)
138 if self.target_transform is not None:
139 target = self.target_transform(target)
G:\anaconda3\envs\data_env\lib\site-packages\torchvision\transforms\transforms.py in __call__(self, img)
59 def __call__(self, img):
60 for t in self.transforms:
---> 61 img = t(img)
62 return img
63
<ipython-input-3-88cdee8f0d6c> in __call__(self, img)
7 im = np.asarray(img)
8 im = detect(im)
----> 9 img = Image.fromarray(im)
10 img = img.resize(size=(128, 128))
11 return img
G:\anaconda3\envs\data_env\lib\site-packages\PIL\Image.py in fromarray(obj, mode)
2768 obj = obj.tostring()
2769
-> 2770 return frombuffer(mode, size, obj, "raw", rawmode, 0, 1)
2771
2772
G:\anaconda3\envs\data_env\lib\site-packages\PIL\Image.py in frombuffer(mode, size, data, decoder_name, *args)
2708 return im
2709
-> 2710 return frombytes(mode, size, data, decoder_name, args)
2711
2712
G:\anaconda3\envs\data_env\lib\site-packages\PIL\Image.py in frombytes(mode, size, data, decoder_name, *args)
2648
2649 im = new(mode, size)
-> 2650 im.frombytes(data, decoder_name, args)
2651 return im
2652
G:\anaconda3\envs\data_env\lib\site-packages\PIL\Image.py in frombytes(self, data, decoder_name, *args)
795 # unpack data
796 d = _getdecoder(self.mode, decoder_name, args)
--> 797 d.setimage(self.im)
798 s = d.decode(data)
799
ValueError: tile cannot extend outside the image
In the custom function, I am trying to preprocess the image and then calling it
It works fine when I test out the code loading a small batch for display

Pytorch custom Dataset class giving wrong output

I am trying to use this class I built for a dataset but it saying that it should be a PIL or ndarray. Im not quite sure whats wrong with it. Here is the class that I am using
class RotateDataset(Dataset):
def __init__(self, image_list, size,transform = None):
self.image_list = image_list
self.size = size
self.transform = transform
def __len__(self):
return len(self.image_list)
def __getitem__(self, idx):
img = cv2.imread(self.image_list[idx])
image_height, image_width = img.shape[:2]
print("ID: ", idx)
if idx % 2 == 0:
label = 0 # Set label
# chose negative or positive rotation
rotation_degree = random.randrange(35, 50, 1)
posnegrot = np.random.randint(2)
if posnegrot == 0:
#positive rotation
#rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), rotation_degree, 1)
#img = cv2.warpAffine(img, rotation_matrix, (num_cols, num_rows))
img = rotate_image(img, rotation_degree)
img = crop_around_center(img, *largest_rotated_rect(image_width,
image_height,
math.radians(rotation_degree)))
else:
# Negative rotation
rotation_degree = -rotation_degree
img = crop_around_center(img, *largest_rotated_rect(image_width,
image_height,
math.radians(rotation_degree)))
else:
label = 1
img = cv2.resize(img, self.size, cv2.INTER_AREA)
return self.transform(img), self.transform(label)
The error that it is giving me is
TypeError: pic should be PIL Image or ndarray. Got class 'int'
It should give me a img (tensor) and a label (tensor)
but I dont think it is doing it correctly.
TypeError Traceback (most recent call last)
<ipython-input-34-f47943b2600c> in <module>
2 train_loss = 0.0
3 net.train()
----> 4 for image, label in enumerate(train_loader):
5 if train_on_gpu:
6 image, label = image.cuda(), label.cuda()
~\Anaconda3\envs\TF2\lib\site-packages\torch\utils\data\dataloader.py in __next__(self)
343
344 def __next__(self):
--> 345 data = self._next_data()
346 self._num_yielded += 1
347 if self._dataset_kind == _DatasetKind.Iterable and \
~\Anaconda3\envs\TF2\lib\site-packages\torch\utils\data\dataloader.py in _next_data(self)
383 def _next_data(self):
384 index = self._next_index() # may raise StopIteration
--> 385 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
386 if self._pin_memory:
387 data = _utils.pin_memory.pin_memory(data)
~\Anaconda3\envs\TF2\lib\site-packages\torch\utils\data\_utils\fetch.py in fetch(self, possibly_batched_index)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
---> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
~\Anaconda3\envs\TF2\lib\site-packages\torch\utils\data\_utils\fetch.py in <listcomp>(.0)
42 def fetch(self, possibly_batched_index):
43 if self.auto_collation:
---> 44 data = [self.dataset[idx] for idx in possibly_batched_index]
45 else:
46 data = self.dataset[possibly_batched_index]
<ipython-input-28-6c77357ff619> in __getitem__(self, idx)
35 label = 1
36 img = cv2.resize(img, self.size, cv2.INTER_AREA)
---> 37 return self.transform(img), self.transform(label)
~\Anaconda3\envs\TF2\lib\site-packages\torchvision\transforms\transforms.py in __call__(self, pic)
99 Tensor: Converted image.
100 """
--> 101 return F.to_tensor(pic)
102
103 def __repr__(self):
~\Anaconda3\envs\TF2\lib\site-packages\torchvision\transforms\functional.py in to_tensor(pic)
53 """
54 if not(_is_pil_image(pic) or _is_numpy(pic)):
---> 55 raise TypeError('pic should be PIL Image or ndarray. Got {}'.format(type(pic)))
56
57 if _is_numpy(pic) and not _is_numpy_image(pic):
TypeError: pic should be PIL Image or ndarray. Got <class 'int'>
As discussed in the comments, the problem was applying transform on label as well. The label should instead simply be written as tensor:
return self.transform(img), torch.tensor(label)

Pytorch double image input : broken pipe with dataloader

I am developing a neural network in python with pytorch in order to classify a dataset of pairs of images. So I wanted to return as an output the 2 images and the ground truth but whenever I try to use the data loader I get the error "Broken pipe".
I want it to work as a classifier so I'm using this link (pytorch classifier CIFAR10) as an example
Here is my code :
###Defining class
class continuousImgDataset(Dataset):
def __init__(self, tabDataset, transform=None):
"""
Args:
tabDataset : Contains the imported data like : Item 1 = [im1,im2,ground-truth]
transform (callable, optional): Optional transform to be applied
on a sample.
"""
self.tabPairImg = tabDataset
self.transform = transform
def __len__(self):
return len(self.tabPairImg)
def __getitem__(self, idx):
img1 = self.tabPairImg[idx][0]
img2 = self.tabPairImg[idx][1]
label = self.tabPairImg[idx][2]
if self.transform:
img1 = self.transform(img1)
img2 = self.transform(img2)
return img1, img2,label
transform = transforms.Compose(
[transforms.Scale((32,32)),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
###Make the training and testing dataset
custom_dataset = continuousImgDataset(pairImg,transform)
train_dataset = continuousImgDataset(pairImg[: int(len(custom_dataset) * .90)],transform)
test_dataset = continuousImgDataset(pairImg[int(len(custom_dataset) * .90) : int(len(custom_dataset))],transform)
print(len(custom_dataset.tabPairImg)) #Output :22620
print(len(train_dataset.tabPairImg)) #Output : 20358
print(len(test_dataset.tabPairImg)) #Output : 2262
### Loaders
trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=4,
shuffle=True, num_workers=2)
testloader = torch.utils.data.DataLoader(test_dataset, batch_size=4,
shuffle=False, num_workers=2)
classes = ('joint','disjoint')
### Image show and Error
"""From the official website but a little modified to have 2 imgs"""
def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.show()
# get some random training images
dataiter = iter(trainloader) #ERROR HERE : Broken pipe
images1,images2, labels = dataiter.next()
# show images
imshow(torchvision.utils.make_grid(images1))
imshow(torchvision.utils.make_grid(images2))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(2)))
I may not have fully understand what the dataloader was expecting in order to iterate on it. Any help would be appreciated :)
EDIT : Here is the full error :
BrokenPipeError Traceback (most recent call last)
<ipython-input-58-314e85cc8fbb> in <module>
7
8 # get some random training images
----> 9 dataiter = iter(trainloader)
10 images1,images2, labels = dataiter.next()
11
C:\ProgramData\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py in __iter__(self)
191
192 def __iter__(self):
--> 193 return _DataLoaderIter(self)
194
195 def __len__(self):
C:\ProgramData\Anaconda3\lib\site-packages\torch\utils\data\dataloader.py in __init__(self, loader)
467 # before it starts, and __del__ tries to join but will get:
468 # AssertionError: can only join a started process.
--> 469 w.start()
470 self.index_queues.append(index_queue)
471 self.workers.append(w)
C:\ProgramData\Anaconda3\lib\multiprocessing\process.py in start(self)
110 'daemonic processes are not allowed to have children'
111 _cleanup()
--> 112 self._popen = self._Popen(self)
113 self._sentinel = self._popen.sentinel
114 # Avoid a refcycle if the target function holds an indirect
C:\ProgramData\Anaconda3\lib\multiprocessing\context.py in _Popen(process_obj)
221 #staticmethod
222 def _Popen(process_obj):
--> 223 return _default_context.get_context().Process._Popen(process_obj)
224
225 class DefaultContext(BaseContext):
C:\ProgramData\Anaconda3\lib\multiprocessing\context.py in _Popen(process_obj)
320 def _Popen(process_obj):
321 from .popen_spawn_win32 import Popen
--> 322 return Popen(process_obj)
323
324 class SpawnContext(BaseContext):
C:\ProgramData\Anaconda3\lib\multiprocessing\popen_spawn_win32.py in __init__(self, process_obj)
87 try:
88 reduction.dump(prep_data, to_child)
---> 89 reduction.dump(process_obj, to_child)
90 finally:
91 set_spawning_popen(None)
C:\ProgramData\Anaconda3\lib\multiprocessing\reduction.py in dump(obj, file, protocol)
58 def dump(obj, file, protocol=None):
59 '''Replacement for pickle.dump() using ForkingPickler.'''
---> 60 ForkingPickler(file, protocol).dump(obj)
61
62 #
BrokenPipeError: [Errno 32] Broken pipe

How to resolve Type error - Lime interpret CNN results

I am using keras with Tesorflow backend (using python 2.7) and I built two dense layer on top of pretrained vgg16 model with four classes. And I get a pretty decent results on my validation set. Now I want to use lime to interpret my result.
I import the lime package and transform one of my images following the lime github repo https://github.com/marcotcr/lime/blob/master/doc/notebooks/Tutorial%20-%20Image%20Classification%20Keras.ipynb. My path_list contains one photo.
import lime
from lime import lime_image
def transform_img_fn(path_list):
out = []
for img_path in path_list:
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img) / 255
x = np.expand_dims(x, axis=0)
out.append(x)
return np.vstack(out)
check_image = transform_img_fn(path_list)
Then
check_image[0].shape
OUTPUT: (3, 224, 224)
predictions[0]
OUTPUT: array([9.67346e-01, 3.00240e-03, 2.96037e-02, 4.79915e-05], dtype=float32)
explainer = lime_image.LimeImageExplainer()
explanation = explainer.explain_instance(check_image[0], model_top.predict, hide_color=0, num_samples=100)
I get this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-115-e286e97e849d> in <module>()
----> 1 explanation = explainer.explain_instance(check_image[0], model_top.predict, hide_color=0, num_samples=100)
/home/ec2-user/anaconda2/envs/env1/lib/python2.7/site-packages/lime/lime_image.pyc in explain_instance(self, image, classifier_fn, labels, hide_color, top_labels, num_features, num_samples, batch_size, segmentation_fn, distance_metric, model_regressor, random_seed)
165 segmentation_fn = SegmentationAlgorithm('quickshift', kernel_size=4,
166 max_dist=200, ratio=0.2,
--> 167 random_seed=random_seed)
168 try:
169 segments = segmentation_fn(image)
/home/ec2-user/anaconda2/envs/env1/lib/python2.7/site-packages/lime/wrappers/scikit_image.pyc in __init__(self, algo_type, **target_params)
103 if (self.algo_type == 'quickshift'):
104 BaseWrapper.__init__(self, quickshift, **target_params)
--> 105 kwargs = self.filter_params(quickshift)
106 self.set_params(**kwargs)
107 elif (self.algo_type == 'felzenszwalb'):
/home/ec2-user/anaconda2/envs/env1/lib/python2.7/site-packages/lime/wrappers/scikit_image.pyc in filter_params(self, fn, override)
82 result = {}
83 for name, value in self.target_params.items():
---> 84 if has_arg(fn, name):
85 result.update({name: value})
86 result.update(override)
/home/ec2-user/anaconda2/envs/env1/lib/python2.7/site-packages/lime/utils/generic_utils.pyc in has_arg(fn, arg_name)
19 else:
20 try:
---> 21 arg_spec = inspect.getargspec(fn.__call__)
22 except AttributeError:
23 return False
/home/ec2-user/anaconda2/envs/env1/lib/python2.7/inspect.pyc in getargspec(func)
813 func = func.im_func
814 if not isfunction(func):
--> 815 raise TypeError('{!r} is not a Python function'.format(func))
816 args, varargs, varkw = getargs(func.func_code)
817 return ArgSpec(args, varargs, varkw, func.func_defaults)
TypeError: <method-wrapper '__call__' of builtin_function_or_method object at 0x7fea20ea4e60> is not a Python function
Based on the documentation, "classifier_fn: function that takes a list of images and returns a matrix of prediction probabilities". I replaced this argument with model_top.predict. I can get all of my predictions if I call predictions = model_top.predict(validation_data, batch_size=32)
Any help would be appreciated.

Categories

Resources