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: 'ResNet' object has no attribute 'grad_checkpointing' #8

Open
edwardchalstrey1 opened this issue Jan 5, 2023 · 2 comments

Comments

@edwardchalstrey1
Copy link
Contributor

On trying to run the notebook I got the following error:

Screenshot 2023-01-05 at 16 45 42

Full error:

========================================
Slicing mr_tmp_1672936332/orig_image.png
----------------------------------------
[INFO] added 'infer_test' dataloader with 8217 elements.
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[7], line 1
----> 1 model.predict(plant_flower[1], slice_size=25)

File /opt/anaconda3/envs/plant-pheno/lib/python3.8/site-packages/scivision/io/wrapper.py:19, in PretrainedModel.predict(self, X, *args, **kwargs)
     17 """Run the prediction."""
     18 # note, we may need to pass other kwargs to the prediction here
---> 19 return self._plumbing(X, **kwargs)

File /opt/anaconda3/envs/plant-pheno/lib/python3.8/site-packages/scivision/io/autoplumber.py:79, in AutoPlumber.__call__(self, X, **kwargs)
     77 # optionally, we can also pass on other keyword arguments to the model
     78 model_kwargs.update(kwargs)
---> 79 return self._fn(*model_args, **model_kwargs)

File /opt/anaconda3/envs/plant-pheno/lib/python3.8/site-packages/mapreader-plant-scivision/model.py:192, in MapReader_model.predict(self, path2images, slice_size, plot_output, **slice_kwds)
    183 def predict(self, 
    184             path2images: str,
    185             slice_size: int=100,
    186             plot_output: bool=True,
    187             **slice_kwds
    188             ):
    190     self.load_image(path2images, slice_size=slice_size, **slice_kwds)
--> 192     return self.inference(plot_output=plot_output)

File /opt/anaconda3/envs/plant-pheno/lib/python3.8/site-packages/mapreader-plant-scivision/model.py:145, in MapReader_model.inference(self, plot_output)
    138 self.pretrained_model.add2dataloader(patches2infer_dataset, 
    139                                      set_name=self.infer_name, 
    140                                      batch_size=self.batch_size, 
    141                                      shuffle=False, 
    142                                      num_workers=0)
    144 # ---- INFERENCE
--> 145 self.pretrained_model.inference(set_name=self.infer_name)
    147 patches2infer['pred'] = self.pretrained_model.pred_label
    148 patches2infer['conf'] = np.max(np.array(self.pretrained_model.pred_conf), axis=1)

File /opt/anaconda3/envs/plant-pheno/lib/python3.8/site-packages/mapreader/train/classifier.py:363, in classifier.inference(self, set_name, verbosity_level, print_info_batch_freq)
    361 def inference(self, set_name="infer", verbosity_level=0, print_info_batch_freq: int=5):
    362     """Model inference on dataset: set_name"""
--> 363     self.train(phases=[set_name], 
    364                num_epochs=1, 
    365                save_model_dir=None,
    366                verbosity_level=verbosity_level,
    367                tensorboard_path=None,
    368                tmp_file_save_freq=2,
    369                remove_after_load=False,
    370                print_info_batch_freq=print_info_batch_freq)

File /opt/anaconda3/envs/plant-pheno/lib/python3.8/site-packages/mapreader/train/classifier.py:400, in classifier.train(self, phases, num_epochs, save_model_dir, verbosity_level, tensorboard_path, tmp_file_save_freq, remove_after_load, print_info_batch_freq)
    393 """Wrapper function for train_core method to capture exceptions. Supported exceptions so far:
    394 - KeyboardInterrupt
    395 
    396 Refer to train_core for more information.
    397 """
    399 try:
--> 400     self.train_core(phases, 
    401                     num_epochs, 
    402                     save_model_dir, 
    403                     verbosity_level, 
    404                     tensorboard_path,
    405                     tmp_file_save_freq,
    406                     print_info_batch_freq=print_info_batch_freq)
    407 except KeyboardInterrupt:
    408     print("KeyboardInterrupted...Exiting...")

File /opt/anaconda3/envs/plant-pheno/lib/python3.8/site-packages/mapreader/train/classifier.py:551, in classifier.train_core(self, phases, num_epochs, save_model_dir, verbosity_level, tensorboard_path, tmp_file_save_freq, print_info_batch_freq)
    545     running_loss += loss.item() * inputs.size(0)
    547     # TQDM
    548     # batch_loop.set_postfix(loss=loss.data)
    549     # batch_loop.refresh()
    550 else:
--> 551     outputs = self.model(inputs)
    552     _, pred_label = torch.max(outputs, dim=1)
    554 running_pred_conf.extend(
    555     torch.nn.functional.softmax(outputs, dim=1).cpu().tolist()
    556 )

File /opt/anaconda3/envs/plant-pheno/lib/python3.8/site-packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs)
   1106 # If we don't have any hooks, we want to skip the rest of the logic in
   1107 # this function, and just call forward.
   1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
   1109         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110     return forward_call(*input, **kwargs)
   1111 # Do not call functions when jit is used
   1112 full_backward_hooks, non_full_backward_hooks = [], []

File /opt/anaconda3/envs/plant-pheno/lib/python3.8/site-packages/timm/models/resnet.py:730, in ResNet.forward(self, x)
    729 def forward(self, x):
--> 730     x = self.forward_features(x)
    731     x = self.forward_head(x)
    732     return x

File /opt/anaconda3/envs/plant-pheno/lib/python3.8/site-packages/timm/models/resnet.py:714, in ResNet.forward_features(self, x)
    711 x = self.act1(x)
    712 x = self.maxpool(x)
--> 714 if self.grad_checkpointing and not torch.jit.is_scripting():
    715     x = checkpoint_seq([self.layer1, self.layer2, self.layer3, self.layer4], x, flatten=True)
    716 else:

File /opt/anaconda3/envs/plant-pheno/lib/python3.8/site-packages/torch/nn/modules/module.py:1185, in Module.__getattr__(self, name)
   1183     if name in modules:
   1184         return modules[name]
-> 1185 raise AttributeError("'{}' object has no attribute '{}'".format(
   1186     type(self).__name__, name))

AttributeError: 'ResNet' object has no attribute 'grad_checkpointing'
@IFenton
Copy link
Contributor

IFenton commented Jan 31, 2023

This problem relates to the timm package - the latest version, 0.6.12 causes the error; it runs fine if v 0.5.4 is used. However, just pinning this version in requirement.txt of this notebook doesn't solve the problem, as during load_pretrained_model the mapreader-plant-scivision package updates timm to the latest version.

So to solve this, I think that either the mapreader-plant-scivision model needs to pin the earlier version of timm in some way (maybe the requirements file), or it needs to be updated to handle the problem. @evangeline-corcoran do you have a preference for which solution is better?

@IFenton
Copy link
Contributor

IFenton commented Mar 1, 2023

Pinning the timm package to 0.5.4. in the mapreader-plant-scivision model seems to have fixed this issue for Mac / Linux. However, the binder is still crashing on model.predict, and some Windows machines are still not working properly.

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

2 participants