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compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
share/python-wheels/
|
| 24 |
+
*.egg-info/
|
| 25 |
+
.installed.cfg
|
| 26 |
+
*.egg
|
| 27 |
+
MANIFEST
|
| 28 |
+
|
| 29 |
+
# PyInstaller
|
| 30 |
+
# Usually these files are written by a python script from a template
|
| 31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 32 |
+
*.manifest
|
| 33 |
+
*.spec
|
| 34 |
+
|
| 35 |
+
# Installer logs
|
| 36 |
+
pip-log.txt
|
| 37 |
+
pip-delete-this-directory.txt
|
| 38 |
+
|
| 39 |
+
# Unit test / coverage reports
|
| 40 |
+
htmlcov/
|
| 41 |
+
.tox/
|
| 42 |
+
.nox/
|
| 43 |
+
.coverage
|
| 44 |
+
.coverage.*
|
| 45 |
+
.cache
|
| 46 |
+
nosetests.xml
|
| 47 |
+
coverage.xml
|
| 48 |
+
*.cover
|
| 49 |
+
*.py,cover
|
| 50 |
+
.hypothesis/
|
| 51 |
+
.pytest_cache/
|
| 52 |
+
cover/
|
| 53 |
+
|
| 54 |
+
# Translations
|
| 55 |
+
*.mo
|
| 56 |
+
*.pot
|
| 57 |
+
|
| 58 |
+
# Django stuff:
|
| 59 |
+
*.log
|
| 60 |
+
local_settings.py
|
| 61 |
+
db.sqlite3
|
| 62 |
+
db.sqlite3-journal
|
| 63 |
+
|
| 64 |
+
# Flask stuff:
|
| 65 |
+
instance/
|
| 66 |
+
.webassets-cache
|
| 67 |
+
|
| 68 |
+
# Scrapy stuff:
|
| 69 |
+
.scrapy
|
| 70 |
+
|
| 71 |
+
# Sphinx documentation
|
| 72 |
+
docs/_build/
|
| 73 |
+
|
| 74 |
+
# PyBuilder
|
| 75 |
+
.pybuilder/
|
| 76 |
+
target/
|
| 77 |
+
|
| 78 |
+
# Jupyter Notebook
|
| 79 |
+
.ipynb_checkpoints
|
| 80 |
+
|
| 81 |
+
# IPython
|
| 82 |
+
profile_default/
|
| 83 |
+
ipython_config.py
|
| 84 |
+
|
| 85 |
+
# pyenv
|
| 86 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 88 |
+
# .python-version
|
| 89 |
+
|
| 90 |
+
# pipenv
|
| 91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 94 |
+
# install all needed dependencies.
|
| 95 |
+
#Pipfile.lock
|
| 96 |
+
|
| 97 |
+
# poetry
|
| 98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 100 |
+
# commonly ignored for libraries.
|
| 101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 102 |
+
#poetry.lock
|
| 103 |
+
|
| 104 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
| 105 |
+
__pypackages__/
|
| 106 |
+
|
| 107 |
+
# Celery stuff
|
| 108 |
+
celerybeat-schedule
|
| 109 |
+
celerybeat.pid
|
| 110 |
+
|
| 111 |
+
# SageMath parsed files
|
| 112 |
+
*.sage.py
|
| 113 |
+
|
| 114 |
+
# Environments
|
| 115 |
+
.env
|
| 116 |
+
.venv
|
| 117 |
+
env/
|
| 118 |
+
venv/
|
| 119 |
+
ENV/
|
| 120 |
+
env.bak/
|
| 121 |
+
venv.bak/
|
| 122 |
+
|
| 123 |
+
# Spyder project settings
|
| 124 |
+
.spyderproject
|
| 125 |
+
.spyproject
|
| 126 |
+
|
| 127 |
+
# Rope project settings
|
| 128 |
+
.ropeproject
|
| 129 |
+
|
| 130 |
+
# mkdocs documentation
|
| 131 |
+
/site
|
| 132 |
+
|
| 133 |
+
# mypy
|
| 134 |
+
.mypy_cache/
|
| 135 |
+
.dmypy.json
|
| 136 |
+
dmypy.json
|
| 137 |
+
|
| 138 |
+
# Pyre type checker
|
| 139 |
+
.pyre/
|
| 140 |
+
|
| 141 |
+
# pytype static type analyzer
|
| 142 |
+
.pytype/
|
| 143 |
+
|
| 144 |
+
# Cython debug symbols
|
| 145 |
+
cython_debug/
|
| 146 |
+
|
| 147 |
+
# PyCharm
|
| 148 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 149 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 150 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 151 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 152 |
+
#.idea/
|
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
import requests
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from transformers import (
|
| 7 |
+
AutoModelForImageClassification,
|
| 8 |
+
AutoFeatureExtractor,
|
| 9 |
+
AutoConfig,
|
| 10 |
+
)
|
| 11 |
+
from torchcam.methods import GradCAM
|
| 12 |
+
from torchcam.utils import overlay_mask
|
| 13 |
+
import matplotlib.pyplot as plt
|
| 14 |
+
from torchvision.transforms.functional import to_pil_image
|
| 15 |
+
from torchcam import methods
|
| 16 |
+
|
| 17 |
+
# TODO I have an error with those
|
| 18 |
+
# CAM_METHODS = ["CAM", "GradCAM", "GradCAMpp", "SmoothGradCAMpp", "ScoreCAM", "SSCAM", "ISCAM", "XGradCAM", "LayerCAM"]
|
| 19 |
+
CAM_METHODS = ["CAM", "GradCAM", "GradCAMpp", "LayerCAM"]
|
| 20 |
+
|
| 21 |
+
SUPPORTED_MODELS = ["convnext"]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def main():
|
| 25 |
+
# Wide mode
|
| 26 |
+
st.set_page_config(layout="wide")
|
| 27 |
+
|
| 28 |
+
# Designing the interface
|
| 29 |
+
st.title("TorchCAM 📸 and Transformers 🤗")
|
| 30 |
+
st.header("Class activation explorer")
|
| 31 |
+
# For newline
|
| 32 |
+
st.write("\n")
|
| 33 |
+
st.write("`torch-cam`: https://github.com/frgfm/torch-cam")
|
| 34 |
+
st.write("`transformers`: https://github.com/huggingface/transformers")
|
| 35 |
+
st.write("Upload an image, select your CAM method and hit the Compute Cam button!")
|
| 36 |
+
|
| 37 |
+
# For newline
|
| 38 |
+
st.write("\n")
|
| 39 |
+
# Set the columns
|
| 40 |
+
cols = st.columns((1, 1))
|
| 41 |
+
cols[0].header("Input image")
|
| 42 |
+
cols[1].header("Overlayed CAM")
|
| 43 |
+
# Sidebar
|
| 44 |
+
# File selection
|
| 45 |
+
st.sidebar.title("Input selection")
|
| 46 |
+
# Disabling warning
|
| 47 |
+
st.set_option("deprecation.showfileUploaderEncoding", False)
|
| 48 |
+
# Choose your own image
|
| 49 |
+
uploaded_file = st.sidebar.file_uploader(
|
| 50 |
+
"Upload files", type=["png", "jpeg", "jpg"]
|
| 51 |
+
)
|
| 52 |
+
if uploaded_file is not None:
|
| 53 |
+
img = Image.open(BytesIO(uploaded_file.read()), mode="r").convert("RGB")
|
| 54 |
+
else:
|
| 55 |
+
r = requests.get(
|
| 56 |
+
"https://i.insider.com/5df126b679d7570ad2044f3e?width=700&format=jpeg&auto=webp"
|
| 57 |
+
)
|
| 58 |
+
img = Image.open(BytesIO(r.content))
|
| 59 |
+
cols[0].image(img, use_column_width=True)
|
| 60 |
+
|
| 61 |
+
model_name = st.sidebar.text_input("Model name", "facebook/convnext-tiny-224")
|
| 62 |
+
|
| 63 |
+
if model_name is not None:
|
| 64 |
+
with st.spinner("Loading model..."):
|
| 65 |
+
config = AutoConfig.from_pretrained(model_name)
|
| 66 |
+
model_type = config.model_type
|
| 67 |
+
if model_type not in SUPPORTED_MODELS:
|
| 68 |
+
st.warning(
|
| 69 |
+
f"{model_type} not in supported models: {','.join(SUPPORTED_MODELS)}"
|
| 70 |
+
)
|
| 71 |
+
else:
|
| 72 |
+
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)
|
| 73 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 74 |
+
|
| 75 |
+
cam_method = st.sidebar.selectbox("CAM method", CAM_METHODS)
|
| 76 |
+
if cam_method is not None:
|
| 77 |
+
cam_extractor = methods.__dict__[cam_method](
|
| 78 |
+
model, target_layer=model.convnext.encoder.stages[-1].layers[-1]
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# label choices
|
| 82 |
+
class_choices = [
|
| 83 |
+
f"{idx + 1} - {class_name}" for idx, class_name in model.config.id2label.items()
|
| 84 |
+
]
|
| 85 |
+
class_selection = st.sidebar.selectbox(
|
| 86 |
+
"Class selection", ["Predicted class (argmax)"] + class_choices
|
| 87 |
+
)
|
| 88 |
+
# for newline
|
| 89 |
+
st.sidebar.write("\n")
|
| 90 |
+
|
| 91 |
+
if st.sidebar.button("Compute CAM"):
|
| 92 |
+
# compute cam
|
| 93 |
+
if img is None:
|
| 94 |
+
st.sidebar.error("Please upload an image first")
|
| 95 |
+
else:
|
| 96 |
+
with st.spinner("Analyzing..."):
|
| 97 |
+
# Set your CAM extractor
|
| 98 |
+
cam_extractor = GradCAM(
|
| 99 |
+
model, target_layer=model.convnext.encoder.stages[-1].layers[-1]
|
| 100 |
+
)
|
| 101 |
+
inputs = feature_extractor(img, return_tensors="pt")
|
| 102 |
+
logits = model(**inputs).logits
|
| 103 |
+
# select the target class
|
| 104 |
+
if class_selection == "Predicted class (argmax)":
|
| 105 |
+
class_idx = logits.squeeze(0).argmax().item()
|
| 106 |
+
else:
|
| 107 |
+
class_idx = model.config.label2id[
|
| 108 |
+
class_selection.rpartition(" - ")[-1]
|
| 109 |
+
]
|
| 110 |
+
print(class_idx)
|
| 111 |
+
# run the cam extractor
|
| 112 |
+
cams = cam_extractor(class_idx, logits)
|
| 113 |
+
cam = cams[0] if len(cams) == 1 else cam_extractor.fuse_cams(cams)
|
| 114 |
+
# resize + overlay
|
| 115 |
+
result = overlay_mask(img, to_pil_image(cam, mode="F"), alpha=0.5)
|
| 116 |
+
# display it
|
| 117 |
+
fig, ax = plt.subplots()
|
| 118 |
+
result = overlay_mask(img, to_pil_image(cam, mode="F"), alpha=0.5)
|
| 119 |
+
ax.imshow(result)
|
| 120 |
+
ax.axis("off")
|
| 121 |
+
cols[1].pyplot(fig)
|
| 122 |
+
if class_selection == "Predicted class (argmax)":
|
| 123 |
+
# show the predicted class
|
| 124 |
+
st.markdown(
|
| 125 |
+
f"<p style='text align: center'> Predicted class is {config.id2label[class_idx]}</p>",
|
| 126 |
+
unsafe_allow_html=True,
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torchcam
|
| 2 |
+
git+https://github.com/huggingface/transformers.git
|
| 3 |
+
streamlit==0.86.2
|
| 4 |
+
torchvision
|