ImportError: cannot import name 'ShieldGemmaForImageClassification' from 'transformers'
I followed the usage part on the model card page, but the import error occurred.
I founded that transformers v4.49.0-Gemma-3 branch does NOT contain the class ShieldGemmaForImageClassification
. What's the correct transformers version for this model?
@Renu11
Thank you! But I did install that version of the transformers by following the instructions on the Model Card. I also checked the source code and found ShieldGemmaForImageClassification
does not exist.
$ pip install git+https://github.com/huggingface/transformers@v4.49.0-Gemma-3
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: http://pypiproxy.xxx:8080/simple
Collecting git+https://github.com/huggingface/transformers@v4.49.0-Gemma-3
Cloning https://github.com/huggingface/transformers (to revision v4.49.0-Gemma-3) to /tmp/pip-req-build-7bx5foet
Running command git clone --filter=blob:none --quiet https://github.com/huggingface/transformers /tmp/pip-req-build-7bx5foet
Running command git checkout -q 1c0f782fe5f983727ff245c4c1b3906f9b99eec2
Resolved https://github.com/huggingface/transformers to commit 1c0f782fe5f983727ff245c4c1b3906f9b99eec2
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: filelock in /home/jovyan/.local/lib/python3.10/site-packages (from transformers==4.50.0.dev0) (3.13.4)
Requirement already satisfied: huggingface-hub<1.0,>=0.26.0 in /home/jovyan/.local/lib/python3.10/site-packages (from transformers==4.50.0.dev0) (0.29.3)
Requirement already satisfied: numpy>=1.17 in /home/jovyan/.local/lib/python3.10/site-packages (from transformers==4.50.0.dev0) (1.22.4)
Requirement already satisfied: packaging>=20.0 in /home/jovyan/.local/lib/python3.10/site-packages (from transformers==4.50.0.dev0) (24.0)
Requirement already satisfied: pyyaml>=5.1 in /home/jovyan/.local/lib/python3.10/site-packages (from transformers==4.50.0.dev0) (6.0.1)
Requirement already satisfied: regex!=2019.12.17 in /home/jovyan/.local/lib/python3.10/site-packages (from transformers==4.50.0.dev0) (2024.11.6)
Requirement already satisfied: requests in /home/jovyan/.local/lib/python3.10/site-packages (from transformers==4.50.0.dev0) (2.31.0)
Requirement already satisfied: tokenizers<0.22,>=0.21 in /home/jovyan/.local/lib/python3.10/site-packages (from transformers==4.50.0.dev0) (0.21.0)
Requirement already satisfied: safetensors>=0.4.1 in /home/jovyan/.local/lib/python3.10/site-packages (from transformers==4.50.0.dev0) (0.5.2)
Requirement already satisfied: tqdm>=4.27 in /opt/conda/lib/python3.10/site-packages (from transformers==4.50.0.dev0) (4.64.1)
Requirement already satisfied: fsspec>=2023.5.0 in /home/jovyan/.local/lib/python3.10/site-packages (from huggingface-hub<1.0,>=0.26.0->transformers==4.50.0.dev0) (2024.12.0)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /home/jovyan/.local/lib/python3.10/site-packages (from huggingface-hub<1.0,>=0.26.0->transformers==4.50.0.dev0) (4.12.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/jovyan/.local/lib/python3.10/site-packages (from requests->transformers==4.50.0.dev0) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /home/jovyan/.local/lib/python3.10/site-packages (from requests->transformers==4.50.0.dev0) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in /home/jovyan/.local/lib/python3.10/site-packages (from requests->transformers==4.50.0.dev0) (1.26.11)
Requirement already satisfied: certifi>=2017.4.17 in /home/jovyan/.local/lib/python3.10/site-packages (from requests->transformers==4.50.0.dev0) (2024.2.2)
Building wheels for collected packages: transformers
Building wheel for transformers (pyproject.toml) ... done
Created wheel for transformers: filename=transformers-4.50.0.dev0-py3-none-any.whl size=10936429 sha256=d192084136fb29c56ac7366435089fab7699866f1ffcefdeb3ae35702bb11dc3
Stored in directory: /tmp/pip-ephem-wheel-cache-lyy5dd1u/wheels/c1/95/d1/7d800368ab25942bf88f88fe1716597fcc8d757043c9b48a2f
Successfully built transformers
Installing collected packages: transformers
Successfully installed transformers-4.50.0.dev0
And then still getting
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
Cell In[1], line 3
1 # pip install accelerate
----> 3 from transformers import AutoProcessor, ShieldGemmaForImageClassification
4 from PIL import Image
5 import requests
ImportError: cannot import name 'ShieldGemmaForImageClassification' from 'transformers'
the same question, I have installed dev transformers by git+https://github.com/huggingface/transformers@v4.49.0-Gemma-3, but it doesn't work
Thank you for bringing this issue to our attention. We are currently investigating it.
Hi @BITDDD ! I'm the ShieldGemma technical lead and the problem you're having is because the model class is still being reviewed and integrated into Transformers. We had intended to land both PRs at the same time but ran into some process hiccups yesterday. It's expected to land later today. Follow along here: https://github.com/huggingface/transformers/pull/36678
I was curious, so I immediately accepted the PR and installed it.
pip install git+https://github.com/RyanMullins/transformers.git@shieldgemma2
It still didn't work as described in the Model Card.
After reading the PR, I found that it's ShieldGemma2ForImageClassification, not ShieldGemmaForImageClassification
Everything is working well now—thanks for the PR!