Deep-Learning icon indicating copy to clipboard operation
Deep-Learning copied to clipboard

AttributeError: 'function' object has no attribute 'named_parameters'

Open chsaiujwal opened this issue 1 year ago • 4 comments

I tried to pip install fastai==2.4 but couldn't as colab couldn't find 2.4 version of fastai. so i just installed pip install fastai. whenever i try to run the trained model, i get the error

AttributeError Traceback (most recent call last) in <cell line: 1>() ----> 1 net_G = build_res_unet(n_input=1, n_output=2, size=256) 2 net_G.load_state_dict(torch.load("res18-unet.pt", map_location=device)) 3 model = MainModel(net_G=net_G) 4 train_model(model, train_dl, 20)

3 frames /usr/local/lib/python3.10/dist-packages/fastai/vision/learner.py in _get_first_layer(m) 32 "Access first layer of a model" 33 c,p,n = m,None,None # child, parent, name ---> 34 for n in next(m.named_parameters())[0].split('.')[:-1]: 35 p,c=c,getattr(c,n) 36 return c,p,n

AttributeError: 'function' object has no attribute 'named_parameters'

please let me know how to fix it. Thanks.

chsaiujwal avatar Jul 19 '23 17:07 chsaiujwal

Have you tried installing the 2.4 version using directly the .whl file?

moein-shariatnia avatar Jul 22 '23 07:07 moein-shariatnia

how do i install directly from the .whl file?

chsaiujwal avatar Jul 22 '23 11:07 chsaiujwal

If you make the following modifications to the function build_res_unet, it should work properly.

def build_res_unet(n_input=1, n_output=2, size=256):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    body = create_body(resnet18(), pretrained=True, n_in=n_input, cut=-2)
    net_G = DynamicUnet(body, n_output, (size, size)).to(device)
    return net_G

ibrahimth avatar Jan 06 '24 17:01 ibrahimth

If you make the following modifications to the function build_res_unet, it should work properly.

def build_res_unet(n_input=1, n_output=2, size=256):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    body = create_body(resnet18(), pretrained=True, n_in=n_input, cut=-2)
    net_G = DynamicUnet(body, n_output, (size, size)).to(device)
    return net_G

Change the import a bit like, from torchvision.models.resnet import resnet18

soumyamindfire avatar Jun 11 '24 12:06 soumyamindfire