Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

ImageEmbedder Docs: Fix print and remove embedding_dim usage #665

Merged

Conversation

tszumowski
Copy link
Contributor

@tszumowski tszumowski commented Aug 16, 2021

What does this PR do?

  • Fix the prints in the examples so that they don't error, and so they print the desired format
  • Remove embedding_dim usage in examples since it shouldn't be used unless fine tuning is included

Fixes #656

Before submitting

  • Was this discussed/approved via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests? [not needed for typos/docs]
  • Did you verify new and existing tests pass locally with your changes?
  • If you made a notable change (that affects users), did you update the CHANGELOG?

PR review

  • Is this pull request ready for review? (if not, please submit in draft mode)

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃

@tszumowski
Copy link
Contributor Author

Test: Validate image_embedder.py Example

$ python3 flash_examples/image_embedder.py 

/home/szumowskit1/.venv/flash001/lib/python3.7/site-packages/urllib3/connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'pl-flash-data.s3.amazonaws.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  InsecureRequestWarning,
data/hymenoptera_data.zip: 67335KB [00:00, 69727.30KB/s]                                                                          
/home/szumowskit1/workspace/lightning-flash/flash/core/model.py:397: LightningDeprecationWarning: The `LightningModule.datamodule` property is deprecated in v1.3 and will be removed in v1.5. Access the datamodule through using `self.trainer.datamodule` instead.
  if self.datamodule is not None and getattr(self.datamodule, "data_pipeline", None) is not None:
/home/szumowskit1/.venv/flash001/lib/python3.7/site-packages/torch/nn/functional.py:3613: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode)
/home/szumowskit1/.venv/flash001/lib/python3.7/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  /pytorch/c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
tensor([0.0847, 0.1147, 1.3101,  ..., 1.0288, 0.6192, 0.5854])
(flash001) szumowskit1@pytorch-1-9-20210810-141644:~/workspace/lightning-flash$  cd /home/szumowskit1/workspace/lightning-flash ; /usr/bin/env /home/szumowskit1/.venv/flash001/bin/python /home/szumowskit1/.vscode-server/extensions/ms-python.python-2021.8.1105858891/pythonFiles/lib/python/debugpy/launcher 43433 -- /home/szumowskit1/workspace/lightning-flash/flash_examples/image_embedder.py 
/home/szumowskit1/.venv/flash001/lib/python3.7/site-packages/urllib3/connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'pl-flash-data.s3.amazonaws.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  InsecureRequestWarning,
/home/szumowskit1/workspace/lightning-flash/flash/core/model.py:397: LightningDeprecationWarning: The `LightningModule.datamodule` property is deprecated in v1.3 and will be removed in v1.5. Access the datamodule through using `self.trainer.datamodule` instead.
  if self.datamodule is not None and getattr(self.datamodule, "data_pipeline", None) is not None:
/home/szumowskit1/.venv/flash001/lib/python3.7/site-packages/torch/nn/functional.py:3613: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode)
/home/szumowskit1/.venv/flash001/lib/python3.7/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  /pytorch/c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
[tensor([0.0847, 0.1147, 1.3101,  ..., 1.0288, 0.6192, 0.5854])]

The last line shows it still runs end to end.

@codecov
Copy link

codecov bot commented Aug 16, 2021

Codecov Report

Merging #665 (34ab4a9) into master (c40f384) will increase coverage by 1.37%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #665      +/-   ##
==========================================
+ Coverage   87.98%   89.36%   +1.37%     
==========================================
  Files         185      185              
  Lines        9663     9663              
==========================================
+ Hits         8502     8635     +133     
+ Misses       1161     1028     -133     
Flag Coverage Δ
unittests 89.36% <ø> (+1.37%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
flash/text/seq2seq/translation/model.py 76.19% <0.00%> (+4.76%) ⬆️
flash/text/seq2seq/summarization/model.py 80.00% <0.00%> (+5.00%) ⬆️
flash/text/seq2seq/core/model.py 75.36% <0.00%> (+8.69%) ⬆️
flash/text/seq2seq/core/data.py 89.87% <0.00%> (+21.51%) ⬆️
flash/text/classification/model.py 93.18% <0.00%> (+22.72%) ⬆️
flash/text/seq2seq/core/metrics.py 97.22% <0.00%> (+23.14%) ⬆️
flash/text/classification/data.py 91.01% <0.00%> (+28.08%) ⬆️
flash/text/classification/cli.py 100.00% <0.00%> (+42.85%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c40f384...34ab4a9. Read the comment docs.

@tszumowski
Copy link
Contributor Author

Test: Validate README Example


Executed README code:

from flash.core.data.utils import download_data
from flash.image import ImageEmbedder

# 1. Download the data
download_data("https://pl-flash-data.s3.amazonaws.com/hymenoptera_data.zip", "data/")

# 2. Create an ImageEmbedder with resnet50 trained on imagenet.
embedder = ImageEmbedder(backbone="resnet50")

# 3. Generate an embedding from an image path.
embeddings = embedder.predict("data/hymenoptera_data/predict/153783656_85f9c3ac70.jpg")

# 4. Print embeddings shape
print(embeddings[0].shape)

Output:

/home/szumowskit1/.venv/flash001/lib/python3.7/site-packages/urllib3/connectionpool.py:1020: InsecureRequestWarning: Unverified HTTPS request is being made to host 'pl-flash-data.s3.amazonaws.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
  InsecureRequestWarning,
/home/szumowskit1/workspace/lightning-flash/flash/core/model.py:397: LightningDeprecationWarning: The `LightningModule.datamodule` property is deprecated in v1.3 and will be removed in v1.5. Access the datamodule through using `self.trainer.datamodule` instead.
  if self.datamodule is not None and getattr(self.datamodule, "data_pipeline", None) is not None:
/home/szumowskit1/.venv/flash001/lib/python3.7/site-packages/torch/nn/functional.py:3613: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode)
/home/szumowskit1/.venv/flash001/lib/python3.7/site-packages/torch/nn/functional.py:718: UserWarning: Named tensors and all their associated APIs are an experimental feature and subject to change. Please do not use them for anything important until they are released as stable. (Triggered internally at  /pytorch/c10/core/TensorImpl.h:1156.)
  return torch.max_pool2d(input, kernel_size, stride, padding, dilation, ceil_mode)
torch.Size([2048])

Last line shows it working.
Prior to this, running without the change in this PR gives:

Traceback (most recent call last):
  File "/home/szumowskit1/workspace/lightning-flash/tmp.py", line 14, in <module>
    print(embeddings.shape)
AttributeError: 'list' object has no attribute 'shape'

@tszumowski tszumowski marked this pull request as ready for review August 16, 2021 16:14
@ananyahjha93 ananyahjha93 enabled auto-merge (squash) August 16, 2021 18:15
@Borda Borda added bug / fix Something isn't working documentation Improvements or additions to documentation labels Aug 16, 2021
Copy link
Collaborator

@ethanwharris ethanwharris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! LGTM 😃

@ananyahjha93 ananyahjha93 merged commit 2f07c63 into Lightning-Universe:master Aug 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug / fix Something isn't working documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ImageEmbedder default behavior is not a flattened output
4 participants