NVIDIA-AI-IOT / jetracer

An autonomous AI racecar using NVIDIA Jetson Nano
MIT License
1.06k stars 319 forks source link

interactive_regression.ipynb, 'DenseNet' object has no attribute 'num_features' #138

Closed chentyra closed 1 year ago

chentyra commented 1 year ago

I tried to run interactive_regression.ipynb using Densenet. I get the following error message

---------------------------------------------------------------------------
ModuleAttributeError                      Traceback (most recent call last)
<ipython-input-4-08299e61f775> in <module>
     24 # DENSENET 121
     25 model = torchvision.models.densenet121(pretrained=True)
---> 26 model.classifier = torch.nn.Linear(model.num_features, output_dim)
     27 
     28 model = model.to(device)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __getattr__(self, name)
    770                 return modules[name]
    771         raise ModuleAttributeError("'{}' object has no attribute '{}'".format(
--> 772             type(self).__name__, name))
    773 
    774     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:

ModuleAttributeError: 'DenseNet' object has no attribute 'num_features'
---------------------------------------------------------------------------

when I run this cell:

import torch
import torchvision

device = torch.device('cuda')
output_dim = 2 * len(dataset.categories)  # x, y coordinate for each category

# ALEXNET
#model = torchvision.models.alexnet(pretrained=True)
#model.classifier[-1] = torch.nn.Linear(4096, output_dim)

# SQUEEZENET 
#model = torchvision.models.squeezenet1_1(pretrained=True)
#model.classifier[1] = torch.nn.Conv2d(512, output_dim, kernel_size=1)
#model.num_classes = len(dataset.categories)

# RESNET 18
#model = torchvision.models.resnet18(pretrained=True)
#model.fc = torch.nn.Linear(512, output_dim)

# RESNET 34
#model = torchvision.models.resnet34(pretrained=True)
#model.fc = torch.nn.Linear(512, output_dim)

# DENSENET 121
model = torchvision.models.densenet121(pretrained=True)
model.classifier = torch.nn.Linear(model.num_features, output_dim)

model = model.to(device)

model_save_button = ipywidgets.Button(description='save model')
model_load_button = ipywidgets.Button(description='load model')
model_path_widget = ipywidgets.Text(description='model path', value='road_following_model.pth')

def load_model(c):
    model.load_state_dict(torch.load(model_path_widget.value))
model_load_button.on_click(load_model)

def save_model(c):
    torch.save(model.state_dict(), model_path_widget.value)
model_save_button.on_click(save_model)

model_widget = ipywidgets.VBox([
    model_path_widget,
    ipywidgets.HBox([model_load_button, model_save_button])
])

display(model_widget)
chentyra commented 1 year ago

Densenet121's model, as reported in the file describing net(densenet.py in the folder models of torchvision), does not have any attribute called num_feature( maybe init_num_features). I resolved the problem in two ways:

I removed the istruction that defines the classifier. I indicated hardcoded model.num_features. The chosen value was 1024.