AttributeError: 'OrderedDict' object has no attribute 'parameters'


#1

Hi, I am new to Pytorch and I am trying to extract features from pretrained models and dump into pickle, but ran into an Error.

vgg = models.vgg16_bn(pretrained=True)
model = nn.Sequential(*(vgg.features[i] for i in xrange(29)))
torch.save(model, 'model.pth')
def extract_features(root, files, transform, batch_size, shuffle, num_workers, model):

    dataloader = get_loader(root, files, transform, batch_size, shuffle, num_workers)
    model = model.cuda()
    model.eval()

    features = []
    imnames = []
    n_iters = len(dataloader)
    for i, (images, names) in enumerate(dataloader):
        images = Variable(images).cuda()
        feas = model(images).cpu() # The error comes after this line
        features.append(feas.data)
        imnames.extend(names)

        if (i+1)%100 == 0:
            print 'iter [%d/%d] finsihed.'%(i, n_iters)

    return torch.cat(features, 0), imnames

File “./utils/extract_feature.py”, line 38, in extract_features
feas = model(images).cpu()
File “$HOME/.local/lib/python2.7/site-packages/torch/nn/modules/module.py”, line 357, in call
result = self.forward(*input, **kwargs)
File “$HOME/.local/lib/python2.7/site-packages/torch/nn/parallel/data_parallel.py”, line 72, in forward
replicas = self.replicate(self.module, self.device_ids[:len(inputs)])
File “$HOME/.local/lib/python2.7/site-packages/torch/nn/parallel/data_parallel.py”, line 77, in replicate
return replicate(module, device_ids)
File “$HOME/.local/lib/python2.7/site-packages/torch/nn/parallel/replicate.py”, line 10, in replicate
params = list(network.parameters())
AttributeError: ‘OrderedDict’ object has no attribute ‘parameters’

Could you please light up the problem? Many thanks!