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

Updating the tutorial file #907

Merged
merged 12 commits into from
Aug 23, 2024
Merged

Updating the tutorial file #907

merged 12 commits into from
Aug 23, 2024

Conversation

ytzfhqs
Copy link
Contributor

@ytzfhqs ytzfhqs commented Aug 17, 2024

Hi, I noticed that the tutorial files have not been updated for a long time, including version dependencies on pytorch-Lightning and albumentations in the file examples/binary_segmentation_intro.ipynb:

...
def training_epoch_end(self, outputs):
    return self.shared_epoch_end(outputs, "train")
...

Unfortunately pytorch-Lightning has deprecated this approach in v2.0.0, so I've fixed this section according to the migration guide and kept the original as much as possible structure of the code as much as possible.

in the file examples/cars segmentation (camvid).ipynb:

...
loss = smp.utils.losses.DiceLoss()
metrics = [
    smp.utils.metrics.IoU(threshold=0.5),
]

optimizer = torch.optim.Adam([ 
    dict(params=model.parameters(), lr=0.0001),
])
...

Due to a version update of segmentation-models-pytorch, this writeup is no longer supported, so I rewrote it based on the structure of the pytorch-Lightning code in the file examples/binary_segmentation_intro.ipynb, and additionally in the image enhancement using albumentations:

...
albu.OneOf(
    [
        albu.IAASharpen(p=1),
        albu.Blur(blur_limit=3, p=1),
        albu.MotionBlur(blur_limit=3, p=1),
    ],
    p=0.9,
),
....

albumentations has been deprecated in newer versions of official documentation. So I fixed it using albu.Sharpen()

Overall, the new tutorial files run perfectly with the latest versions of pytorch-Lightning and albumentations. Here's a link to my notebook running the tutorial files on kaggle, binary_segmentation_intro, cars segmentation (camvid)

@qubvel
Copy link
Collaborator

qubvel commented Aug 17, 2024

Hi @ytzfhqs,

Thanks for opening PR! Overall, it looks great to me!
Just a few notes:

  • in binary_segmentation_intro can you please save the section regarding uploading the model to the HF Hub, it was removed in your notebook
  • in the cars segmentation (camvid), please, rename the model to CamVid model instead of PetModel and make sure that all comments are in English

Thanks once again for this contribution! I will be happy to merge it after the update!

@ytzfhqs
Copy link
Contributor Author

ytzfhqs commented Aug 17, 2024

@qubvel ,The cars segmentation (camvid) has been changed as requested.

However, binary_segmentation_intro will report an error when uploading the model to the HF Hub:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[19], line 4
      2 smp_model = model.model
      3 # if push_to_hub=True, model will be saved to repository with this name
----> 4 commit_info = smp_model.save_pretrained(
      5     save_directory="qubvel-hf/oxford-pet-segmentation",  
      6     push_to_hub=True,      # optional, push model to HF Hub
      7     metrics=test_metrics,  # optional, save metrics in model card
      8     dataset="Oxford Pet",  # optional, save dataset name in model card
      9 )

File /opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1695, in Module.__getattr__(self, name)
   1693     if name in modules:
   1694         return modules[name]
-> 1695 raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")

AttributeError: 'FPN' object has no attribute 'save_pretrained'

I tried the example in docs/save_load.rst on colab, but still get the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[<ipython-input-2-17ebc9899347>](https://localhost:8080/#) in <cell line: 6>()
      4 
      5 # After training your model, save it to a directory
----> 6 model.save_pretrained('./my_model')

[/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py](https://localhost:8080/#) in __getattr__(self, name)
   1707             if name in modules:
   1708                 return modules[name]
-> 1709         raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
   1710 
   1711     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:

AttributeError: 'Unet' object has no attribute 'save_pretrained'

colad link, I can't fix the bug.Can you give me some advice?

@qubvel
Copy link
Collaborator

qubvel commented Aug 17, 2024

You have to install the library from the source and paste this installation on the top of the script (the pypi package is not released yet)

pip install -U git+https://github.com/qubvel-org/segmentation_models.pytorch

@ytzfhqs
Copy link
Contributor Author

ytzfhqs commented Aug 18, 2024

Thanks for the suggestion, I solved the model saving problem, but it was running:

commit_info = smp_model.save_pretrained(
    save_directory=ytzfhqs/oxford-pet-segmentation’,
    push_to_hub=True, # optional, push model to HF Hub
    metrics=test_metrics, # optional, save metrics in model card
    dataset=Oxford Pet’, # optional, save dataset name in model card
)

An error occurs:

---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-9-63eccb09cc45> in <cell line: 2>()
      1 smp_model = model
----> 2 commit_info = smp_model.save_pretrained(
      3 save_directory=ytzfhqs/oxford-pet-segmentation’,
      4metrics=test_metrics, # optional, save metrics in model card
      5 push_to_hub=True, # optional, push model to HF Hub
      6 dataset=Oxford Pet’, # optional, save dataset name in model card

/usr/local/lib/python3.10/dist-packages/segmentation_models_pytorch/base/hub_mixin.py in save_pretrained(self, save_directory, *args, * *kwargs)
    121 # delete the additional attributes
    122 self._del_attrs([‘save_directory’, ‘metrics’, ‘dataset’])
--> 123 self._hub_mixin_config.pop(‘_model_class’)
    124 
    125 return result

KeyError: ‘_model_class

I checked the segmentation_models_pytorch/base/hub_mixin.py file and the problem is:

...
self._hub_mixin_config.pop(‘_model_class’)
...

It looks like the self._hub_mixin_config variable was changed when the try branch was executed:

...
result = super().save_pretrained(save_directory, *args, **kwargs)
...

Then I checked the huggingface_hub\hub_mixin.py file in the huggingface_hub package and the key code is on line 417:

...
    if config is None.
       config = self._hub_mixin_config
...

The above overrides the self._hub_mixin_config variable, and the _model_class key is no longer present, so it is being executed:

...
self._hub_mixin_config.pop(‘_model_class’)
...

An error occurs, and because of the limitations of my current device I can't make further refinements, so I simply comment out the segmentation_models_pytorch/base/hub_mixin.py file in:

...
self._hub_mixin_config.pop(‘_model_class’)
...

Then Save model to HF Hub works fine!

You can check out cars segmentation (camvid) at kaggle,binary_segmentation_intro at github.

@qubvel
Copy link
Collaborator

qubvel commented Aug 23, 2024

Thanks for the investigation of this HF hub problem. Would you like to make a small PR to fix the mixin issue? Then we will be able to merge these PRs too

for example we can add None to pop to avoid error:

self._hub_mixin_config.pop(‘_model_class’, None)

@ytzfhqs
Copy link
Contributor Author

ytzfhqs commented Aug 23, 2024

Sure, very happy to help, I've proposed a new PR.

I would recommend releasing the new version in the pypi package as soon as possible for support pushing the model to the HF hub. otherwise users may need to use pip+git when running the examples/binary_segmentation_intro.ipynb tutorial file to installation of segmentation_model, whereas the examples/cars segmentation (camvid).ipynb tutorial file is sufficient to simply use pip, which may be somewhat misleading.

I will update the installation section of the examples/binary_segmentation_intro.ipynb file if and when a new pypi package is released.

@qubvel
Copy link
Collaborator

qubvel commented Aug 23, 2024

Thanks for the update, I think it's in good shape to merge!

May I ask you to update also a few more things for consistency in CamVid notebook:

  • import albumentations as albu -> import albumentations as A and then replace it across the whole notebook
  • # 训练集增强策略 - rewrite in english

Thanks for iterating!

import albumentations as albu -> import albumentations as A
# 训练集增强策略 -> # training set images augmentation
@ytzfhqs
Copy link
Contributor Author

ytzfhqs commented Aug 23, 2024

Already iterated as required.

@qubvel
Copy link
Collaborator

qubvel commented Aug 23, 2024

Ok, thanks a lot for the update! looks good to me!

@qubvel qubvel merged commit 85bb28e into qubvel-org:main Aug 23, 2024
2 checks passed
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

Successfully merging this pull request may close these issues.

2 participants