Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chsaiujwal opened this issue Jul 19, 2023 · 4 comments
Open

Comments

@chsaiujwal
Copy link

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.

@moein-shariatnia
Copy link
Owner

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

@chsaiujwal
Copy link
Author

how do i install directly from the .whl file?

@ibrahimth
Copy link

ibrahimth commented Jan 6, 2024

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

@soumyamindfire
Copy link

soumyamindfire commented Jun 11, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants