Pytorch double image input : broken pipe with dataloader - python

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

Related

pytorch_geometric 'GlobalStorage' object has no attribute 'edge_indexes'

I am working on Unsupervised Domain Adaptation and I'm working on a geometric learning approach to this problem. In this approach the a custom model uses the scores of a CNN model (in my case a pretrained ResNet50) and uses the class scores of the original ImageNet to build a graph and applies a GCN to the resulting graph. Here is the code of my forward pass
def forward(self, x):
features = self.cnn.forward(x)
scores = self.dsa.forward(x)
transposed_scores = torch.transpose(scores, 0, 1)
adjacency_matrix = torch.matmul(scores, transposed_scores)
sparse_adj_matrix = dense_to_sparse(adjacency_matrix)
edge_index, edge_attr = sparse_adj_matrix[0], sparse_adj_matrix[1]
graph = geometric_data(scores, edge_index=edge_index)
graph.to(self.device)
gcn_features = self.gcn(graph.x, graph.edge_indexes)
concat_features = torch.cat((features, gcn_features))
domain_classification = self.domain_alignment(concat_features)
pseudo_label = self.classifier(concat_features)
return domain_classification, pseudo_label
The problem is that when calling the forward method, I get a runtime error saying 'GlobalStorage' object has no attribute 'edge_indexes'. I'm working on Google Colab. What did I get wrong?
EDIT:
The full error traceback is:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/torch_geometric/data/storage.py in __getattr__(self, key)
61 try:
---> 62 return self[key]
63 except KeyError:
/usr/local/lib/python3.8/dist-packages/torch_geometric/data/storage.py in __getitem__(self, key)
84 def __getitem__(self, key: str) -> Any:
---> 85 return self._mapping[key]
86
KeyError: 'edge_indexes'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-32-7436986037c0> in <module>
7 uda_model.to(device)
8
----> 9 summary(uda_model, input_size=(3, 224, 224))
/usr/local/lib/python3.8/dist-packages/torchsummary/torchsummary.py in summary(model, input_size, batch_size, device)
70 # make a forward pass
71 # print(x.shape)
---> 72 model(*x)
73
74 # remove these hooks
/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1192 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1193 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1194 return forward_call(*input, **kwargs)
1195 # Do not call functions when jit is used
1196 full_backward_hooks, non_full_backward_hooks = [], []
<ipython-input-31-1c0d271d6797> in forward(self, x)
43 graph = geometric_data(scores, edge_index=edge_index)
44
---> 45 gcn_features = self.gcn(graph.x, graph.edge_indexes)
46 concat_features = torch.cat((features, gcn_features))
47 domain_classification = self.domain_alignment(concat_features)
/usr/local/lib/python3.8/dist-packages/torch_geometric/data/data.py in __getattr__(self, key)
426 "dataset, remove the 'processed/' directory in the dataset's "
427 "root folder and try again.")
--> 428 return getattr(self._store, key)
429
430 def __setattr__(self, key: str, value: Any):
/usr/local/lib/python3.8/dist-packages/torch_geometric/data/storage.py in __getattr__(self, key)
62 return self[key]
63 except KeyError:
---> 64 raise AttributeError(
65 f"'{self.__class__.__name__}' object has no attribute '{key}'")
66
AttributeError: 'GlobalStorage' object has no attribute 'edge_indexes'
For a minimum reproducible example:
import os
import random
import numpy as np
from PIL import Image
from skimage.metrics import structural_similarity as ssim
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader, random_split
from torchvision import transforms
from torch_geometric import nn as geometric_nn
from torch_geometric.data import Data as geometric_data
from torch_geometric.utils import dense_to_sparse
from torchsummary import summary
The code here was divided into different cells in Google Colab, here reported in different code sections
def get_transform():
transform = list()
transform.append(transforms.Resize((256, 256)))
transform.append(transforms.RandomCrop((224, 224)))
transform.append(transforms.RandomHorizontalFlip(0.5))
transform.append(transforms.RandomVerticalFlip(0.5))
transform.append(transforms.GaussianBlur((3, 3)))
transform.append(transforms.RandomGrayscale(0.5))
transform.append(transforms.ToTensor())
transform.append(transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]))
return transforms.Compose(transform)
class GCANDataset(Dataset):
def __init__(self, source_path, target_path, transform=None):
self.transform = transform
self.source_path = source_path
self.target_path = target_path
self.file_counter = 0
self.source_length = 0
for folder in os.listdir(source_path):
path = os.path.join(source_path, folder)
self.file_counter += len(os.listdir(path))
self.source_length += len(os.listdir(path))
for folder in os.listdir(target_path):
path = os.path.join(target_path, folder)
self.file_counter += len(os.listdir(path))
def __len__(self):
return self.file_counter
def __getitem__(self, index):
path_to_use = self.source_path
domain = 0
if index > self.source_length:
path_to_use = self.target_path
index -= self.source_length
domain = 1
label = ''
image = 0
for folder in os.listdir(path_to_use):
item_list = os.listdir(os.path.join(path_to_use, folder))
if index - len(item_list) < 0:
file_to_take = len(item_list) - index - 1
file_path = os.path.join(os.path.join(path_to_use, folder), item_list[file_to_take])
image = Image.open(file_path)
label = folder
if self.transform is not None:
image = self.transform(image)
return image, label, domain
class GCANModel(nn.Module):
def __init__(self, num_classes, gcn_in_channels=224, gcn_out_channels=150, device='cuda:0'):
super(GCANModel, self).__init__()
self.device = device
self.cnn = resnet50(weights=ResNet50_Weights.IMAGENET1K_V2)
features = self.cnn.fc.in_features
combined_features = features + gcn_out_channels
self.cnn = nn.Sequential(*list(self.cnn.children())[:-1])
self.dsa = resnet50(weights=ResNet50_Weights.IMAGENET1K_V2)
self.gcn = geometric_nn.GCNConv(in_channels=gcn_in_channels,
out_channels=gcn_out_channels)
self.domain_alignment = nn.Sequential(
nn.Linear(in_features=combined_features, out_features=1024),
nn.ReLU(),
nn.Linear(in_features=1024, out_features=1024),
nn.ReLU(),
nn.Linear(in_features=1024, out_features=1),
nn.Sigmoid()
)
self.classifier = nn.Sequential(
nn.Linear(in_features=combined_features, out_features=1024),
nn.Dropout(p=0.2),
nn.ReLU(),
nn.Linear(in_features=1024, out_features=1024),
nn.Dropout(p=0.2),
nn.ReLU(),
nn.Linear(in_features=1024, out_features=num_classes),
nn.Softmax()
)
def forward(self, x):
features = self.cnn.forward(x)
scores = self.dsa.forward(x)
transposed_scores = torch.transpose(scores, 0, 1)
adjacency_matrix = torch.matmul(scores, transposed_scores)
sparse_adj_matrix = dense_to_sparse(adjacency_matrix)
edge_index, edge_attr = sparse_adj_matrix[0], sparse_adj_matrix[1]
graph = geometric_data(scores, edge_index=edge_index)
gcn_features = self.gcn(graph.x, graph.edge_indexes)
concat_features = torch.cat((features, gcn_features))
domain_classification = self.domain_alignment(concat_features)
pseudo_label = self.classifier(concat_features)
return domain_classification, pseudo_label
real_world_path = '/content/Adaptiope/product_images'
products_path = '/content/Adaptiope/real_life'
device='cuda:0'
transform = get_transform()
uda_dataset = GCANDataset(source_path=real_world_path,
target_path=products_path, transform=transform)
uda_model = GCANModel(20)
uda_model.to(device)
summary(uda_model, input_size=(3, 224, 224))
The dataset is Adaptiope, which can be found here. The dataset is divided into 3 domains. The synthetic one is ignored, while on the others only 20 classes are considered. The classes mantained are:
backpack, bookcase, car jack, comb, crown, file cabinet, flat iron, game controller, glasses, helicopter, ice skates, letter tray, monitor, mug, network switch, over-ear headphones, pen, purse, stand mixer, stroller

Train Pytorch Autoencoder with custom dataset

I am new to Pytorch. I was able to build an autoencoder model and train it using the MINST dataset.
However, I need to train the model using a custom dataset.
I am getting the error 'ToTensor' object is not iterable when i try to train with the custom dataset.
Below is a code of my dataset class
class AutoEncoderDataSet(Dataset):
def __init__(self, in_dir, transform):
self._transforms = transform
self.img_paths = []
files = os.listdir(in_dir)
for file in files:
self.img_paths.append(os.path.join(in_dir, file))
def __getitem__(self, index):
img, img_trans = Image.open(self.img_paths[index]), Image.open(self.img_paths[index])
x, y = transform(img), transform(img_trans)
return x, y
def __len__(self):
return len(self.img_paths)
Here is how I am generating the dataloader
transform = transforms.Compose([torchvision.transforms.ToTensor()])
train_dataset = AutoEncoderDataSet('./datasets/train/', transform)
batch_size = 512
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True
When I try to train using the data generated with the custom dataset class, I am getting the error mentioned above.
Below is code for training the model
epochs = 2048
for epoch in range(epochs):
loss = 0
for batch_features, _ in train_loader:
# reshape mini-batch data to [N, 784] matrix
# load it to the active device
batch_features = batch_features.view(-1, 250*250).to(device)
# reset the gradients back to zero
# PyTorch accumulates gradients on subsequent backward passes
optimizer.zero_grad()
# compute ecoder output
outputs = model(batch_features)
# compute training reconstruction loss
train_loss = criterion(outputs, batch_features)
# compute accumulated gradients
train_loss.backward()
# perform parameter update based on current gradients
optimizer.step()
# add the mini-batch training loss to epoch loss
loss += train_loss.item()
# compute the epoch training loss
loss = loss / len(train_loader)
# display the epoch training loss
print("epoch : {}/{}, recon loss = {:.8f}".format(epoch + 1, epochs, loss))
And this is the error I am getting
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11164/1462449221.py in <module>
3 for epoch in range(epochs):
4 loss = 0
----> 5 for batch_features, _ in test_loader:
6 # reshape mini-batch data to [N, 784] matrix
7 # load it to the active device
~\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\utils\data\dataloader.py in __next__(self)
519 if self._sampler_iter is None:
520 self._reset()
--> 521 data = self._next_data()
522 self._num_yielded += 1
523 if self._dataset_kind == _DatasetKind.Iterable and \
~\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\utils\data\dataloader.py in _next_data(self)
559 def _next_data(self):
560 index = self._next_index() # may raise StopIteration
--> 561 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
562 if self._pin_memory:
563 data = _utils.pin_memory.pin_memory(data)
~\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\utils\data\_utils\fetch.py in fetch(self, possibly_batched_index)
47 def fetch(self, possibly_batched_index):
48 if self.auto_collation:
---> 49 data = [self.dataset[idx] for idx in possibly_batched_index]
50 else:
51 data = self.dataset[possibly_batched_index]
~\AppData\Local\Programs\Python\Python38\lib\site-packages\torch\utils\data\_utils\fetch.py in <listcomp>(.0)
47 def fetch(self, possibly_batched_index):
48 if self.auto_collation:
---> 49 data = [self.dataset[idx] for idx in possibly_batched_index]
50 else:
51 data = self.dataset[possibly_batched_index]
~\AppData\Local\Programs\Python\Python38\lib\site-packages\torchvision\datasets\folder.py in __getitem__(self, index)
232 sample = self.loader(path)
233 if self.transform is not None:
--> 234 sample = self.transform(sample)
235 if self.target_transform is not None:
236 target = self.target_transform(target)
~\AppData\Local\Programs\Python\Python38\lib\site-packages\torchvision\transforms\transforms.py in __call__(self, img)
58
59 def __call__(self, img):
---> 60 for t in self.transforms:
61 img = t(img)
62 return img
TypeError: 'ToTensor' object is not iterable
Any suggestions would be greatly appreciated.

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

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

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,))

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