vision_papers / pages /2_Oneformer.py
merve's picture
merve HF Staff
Fix streamlit warning (#3)
595df7a verified
import streamlit as st
from streamlit_extras.switch_page_button import switch_page
translations = {
'en': {'title': 'OneFormer',
'original_tweet':
"""
[Original tweet](https://twitter.com/mervenoyann/status/1739707076501221608) (December 26, 2023)
""",
'tweet_1':
"""
OneFormer: one model to segment them all? 🤯
I was looking into paperswithcode leaderboards when I came across OneFormer for the first time so it was time to dig in!
""",
'tweet_2':
"""
OneFormer is a "truly universal" model for semantic, instance and panoptic segmentation tasks ⚔️
What makes is truly universal is that it's a single model that is trained only once and can be used across all tasks 👇
""",
'tweet_3':
"""
The enabler here is the text conditioning, i.e. the model is given a text query that states task type along with the appropriate input, and using contrastive loss, the model learns the difference between different task types 👇
""",
'tweet_4':
"""
Thanks to 🤗 Transformers, you can easily use the model!
I have drafted a [notebook](https://t.co/cBylk1Uv20) for you to try right away 😊
You can also check out the [Space](https://t.co/31GxlVo1W5) without checking out the code itself.
""",
'ressources':
"""
Ressources:
[OneFormer: One Transformer to Rule Universal Image Segmentation](https://arxiv.org/abs/2211.06220)
by Jitesh Jain, Jiachen Li, MangTik Chiu, Ali Hassani, Nikita Orlov, Humphrey Shi (2022)
[GitHub](https://github.com/SHI-Labs/OneFormer)
[Hugging Face documentation](https://huggingface.co/docs/transformers/model_doc/oneformer)"""
},
'fr': {
'title': 'OneFormer',
'original_tweet':
"""
[Tweet de base](https://twitter.com/mervenoyann/status/1739707076501221608) (en anglais) (26 décembre 2023)
""",
'tweet_1':
"""
OneFormer : un seul modèle pour tous les segmenter ? 🤯
Je regardais les classements de paperswithcode quand je suis tombée sur OneFormer pour la première fois. J'ai donc creusé les choses !
""",
'tweet_2':
"""
OneFormer est un modèle "véritablement universel" pour les tâches de segmentation sémantique, d'instance et panoptique ⚔️
Ce qui le rend vraiment universel, c'est qu'il s'agit d'un modèle unique qui n'est entraîné qu'une seule fois et qui peut être utilisé pour toutes les tâches 👇
""",
'tweet_3':
"""
Le catalyseur ici est le conditionnement du texte, c'est-à-dire que le modèle reçoit une requête textuelle indiquant le type de tâche ainsi que l'entrée appropriée, et en utilisant la perte contrastive, le modèle apprend la différence entre les différents types de tâches 👇 """,
'tweet_4':
"""
Grâce à 🤗 Transformers, vous pouvez facilement utiliser ce modèle !
J'ai rédigé un [notebook](https://t.co/cBylk1Uv20) que vous pouvez essayer sans attendre 😊
Vous pouvez également consulter le [Space](https://t.co/31GxlVo1W5) sans consulter le code lui-même.
""",
'ressources':
"""
Ressources :
[OneFormer: One Transformer to Rule Universal Image Segmentation](https://arxiv.org/abs/2211.06220)
de Jitesh Jain, Jiachen Li, MangTik Chiu, Ali Hassani, Nikita Orlov, Humphrey Shi (2022)
[GitHub](https://github.com/SHI-Labs/OneFormer)
[Documentation d'Hugging Face](https://huggingface.co/docs/transformers/model_doc/oneformer)
"""
}
}
def language_selector():
languages = {'EN': '🇬🇧', 'FR': '🇫🇷'}
selected_lang = st.selectbox('', options=list(languages.keys()), format_func=lambda x: languages[x], key='lang_selector')
return 'en' if selected_lang == 'EN' else 'fr'
left_column, right_column = st.columns([5, 1])
# Add a selector to the right column
with right_column:
lang = language_selector()
# Add a title to the left column
with left_column:
st.title(translations[lang]["title"])
st.success(translations[lang]["original_tweet"], icon="ℹ️")
st.markdown(""" """)
st.markdown(translations[lang]["tweet_1"], unsafe_allow_html=True)
st.markdown(""" """)
st.image("pages/OneFormer/image_1.jpeg", use_container_width=True)
st.markdown(""" """)
st.markdown(translations[lang]["tweet_2"])
st.markdown(""" """)
st.image("pages/OneFormer/image_2.jpeg", use_container_width=True)
st.markdown(""" """)
st.markdown(translations[lang]["tweet_3"])
st.markdown(""" """)
st.image("pages/OneFormer/image_3.jpeg", use_container_width=True)
st.markdown(""" """)
st.markdown(translations[lang]["tweet_4"])
st.markdown(""" """)
st.image("pages/OneFormer/image_4.jpeg", use_container_width=True)
st.markdown(""" """)
with st.expander ("Code"):
if lang == "en":
st.code("""
from transformers import OneFormerProcessor, OneFormerForUniversalSegmentation
# Loading a single model for all three tasks
processor = OneformerProcessor.from_pretrained("shi-Labs/oneformer_cityscapes_swin_large")
model = OneFormerForUniversalSegmentation.from_pretrained("shi-labs/oneformer_cityscapes_swin_large")
# To get panoptic and instance segmentation results, swap task_inputs with "panoptic" or "instance" and use the appropriate post processing method
semantic_inputs = processor(images=image, task_inputs=["semantic"], return_tensors="pt")
semantic_outputs = model(**semantic_inputs)
# pass through image_processor for postprocessing
predicted_semantic_map = processor.post_process_semantic_segmentation(semantic_outputs, target_sizes=[image.size[::-1]])[0]
""")
else:
st.code("""
from transformers import OneFormerProcessor, OneFormerForUniversalSegmentation
# Chargement d'un seul modèle pour les trois tâches
processor = OneformerProcessor.from_pretrained("shi-Labs/oneformer_cityscapes_swin_large")
model = OneFormerForUniversalSegmentation.from_pretrained("shi-labs/oneformer_cityscapes_swin_large")
# Pour avoir des résultats de segmentation panoptique ou par instance, remplacez task_inputs par "panoptic" ou "instance" et utilisez la méthode de post-traitement appropriée
semantic_inputs = processor(images=image, task_inputs=["semantic"], return_tensors="pt")
semantic_outputs = model(**semantic_inputs)
# passage par image_processor pour le post-traitement
predicted_semantic_map = processor.post_process_semantic_segmentation(semantic_outputs, target_sizes=[image.size[::-1]])[0]
""")
st.markdown(""" """)
st.info(translations[lang]["ressources"], icon="📚")
st.markdown(""" """)
st.markdown(""" """)
st.markdown(""" """)
col1, col2, col3= st.columns(3)
with col1:
if lang == "en":
if st.button('Previous paper', use_container_width=True):
switch_page("MobileSAM")
else:
if st.button('Papier précédent', use_container_width=True):
switch_page("MobileSAM")
with col2:
if lang == "en":
if st.button("Home", use_container_width=True):
switch_page("Home")
else:
if st.button("Accueil", use_container_width=True):
switch_page("Home")
with col3:
if lang == "en":
if st.button("Next paper", use_container_width=True):
switch_page("VITMAE")
else:
if st.button("Papier suivant", use_container_width=True):
switch_page("VITMAE")