AttributeError                            Traceback (most recent call last)
 in 
45
46 start = datetime.datetime.now()
—> 47 inp, out, lab, pred, paths = predict_test_paths(model, dataloaders=dataloaders)
48 end = datetime.datetime.now()
49
 in predict_test_paths(model, dataloaders, device_type)
126                 labels = labels.to(device)
127
–> 128                 outputs = model(inputs)
129                 _, preds = torch.max(outputs, 1)
130
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
530             result = self._slow_forward(*input, **kwargs)
531         else:
–> 532             result = self.forward(*input, **kwargs)
533         for hook in self._forward_hooks.values():
534             hook_result = hook(self, input, result)
~/anaconda3/lib/python3.7/site-packages/torch/nn/parallel/data_parallel.py in forward(self, *inputs, **kwargs)
138     def forward(self, *inputs, **kwargs):
139         if not self.device_ids:
–> 140             return self.module(*inputs, **kwargs)
141
142         for t in chain(self.module.parameters(), self.module.buffers()):
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
530             result = self._slow_forward(*input, **kwargs)
531         else:
–> 532             result = self.forward(*input, **kwargs)
533         for hook in self._forward_hooks.values():
534             hook_result = hook(self, input, result)
 in forward(self, x)
44
45     def forward(self, x):
—> 46         x = self.basemodel(x)
47         x = F.relu(self.dropout1(self.feedforward1(x)))
48         x = F.relu(self.dropout2(self.feedforward2(x)))
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
530             result = self._slow_forward(*input, **kwargs)
531         else:
–> 532             result = self.forward(*input, **kwargs)
533         for hook in self._forward_hooks.values():
534             hook_result = hook(self, input, result)
~/anaconda3/lib/python3.7/site-packages/torchvision/models/resnet.py in forward(self, x)
214
215     def forward(self, x):
–> 216         return self._forward_impl(x)
217
218
~/anaconda3/lib/python3.7/site-packages/torchvision/models/resnet.py in _forward_impl(self, x)
207         x = self.layer4(x)
208
–> 209         x = self.avgpool(x)
210         x = torch.flatten(x, 1)
211         x = self.fc(x)
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in call(self, *input, **kwargs)
530             result = self._slow_forward(*input, **kwargs)
531         else:
–> 532             result = self.forward(*input, **kwargs)
533         for hook in self._forward_hooks.values():
534             hook_result = hook(self, input, result)
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/pooling.py in forward(self, input)
552     def forward(self, input):
553         return F.avg_pool2d(input, self.kernel_size, self.stride,
–> 554                             self.padding, self.ceil_mode, self.count_include_pad, self.divisor_override)
555
556
~/anaconda3/lib/python3.7/site-packages/torch/nn/modules/module.py in getattr(self, name)
574                 return modules[name]
575         raise AttributeError("’{}’ object has no attribute ‘{}’".format(
–> 576             type(self).name, name))
577
578     def setattr(self, name, value):
AttributeError: ‘AvgPool2d’ object has no attribute ‘divisor_override’
v = '/home/lalit/notebooks/Lalit/organic_non_organic/resnet50False30dec_organic_non_organic50.pth'
torch.cuda.empty_cache()

# use_cuda = torch.cuda.is_available()
# DEVICE = torch.device('cuda' if use_cuda else 'cpu') 
# var = torch.load(v)# 'cpu' in this case
# cpu_model = var
# cpu_model.load_state_dict(torch.load(v, map_location=DEVICE))

# var=cpu_model
var = torch.load(v, map_location=torch.device('cpu'))
model = var['model']
# model.avgpool = nn.AvgPool2d(kernel_size=7, stride=1, padding=0)

model = nn.DataParallel(model.module)
model.avgpool = nn.AvgPool2d(kernel_size=7, stride=1, padding=0)
data_dir= fixed_path 

MyImageFolder(data_dir)
batch_size = 32
shuffle = False
num_workers = 4

data_transforms = {'test': transforms.Compose([transforms.Resize((224, 224)),
                                              transforms.ToTensor(),
                                              transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])}

#replace MyImageFolder with datasets.ImageFolder to ignore image paths
image_datasets = {x: MyImageFolder(data_dir, data_transforms[x])
                 for x in ['test']}

dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], sampler= None,
                                             batch_size=batch_size, shuffle=shuffle, num_workers=num_workers)
              for x in ['test']}

dataset_sizes = {x: len(image_datasets[x])
               for x in ['test']}

class_names = ['non_organic','organic']

device = torch.device("cuda:2" if torch.cuda.is_available() else "cpu")

start = datetime.datetime.now()
inp, out, lab, pred, paths = predict_test_paths(model, dataloaders=dataloaders)
end = datetime.datetime.now()



supcs = [p.split('/')[-1].split('.')[0] for p in paths]
finaldf = pd.DataFrame({'path':paths, 'supc':supcs, 'flag_organic':pred},
            columns=['supc', 'path', 'flag_organic'])


noncompdf= finaldf[finaldf['flag_organic'] == 0][['supc','flag_organic']]
finaldf = finaldf[['supc','flag_organic']]