Skip to content

Instantly share code, notes, and snippets.

@stephenlb
Last active August 30, 2024 20:09
Show Gist options
  • Save stephenlb/51c523323b134c13ce87986c4074489b to your computer and use it in GitHub Desktop.
Save stephenlb/51c523323b134c13ce87986c4074489b to your computer and use it in GitHub Desktop.
Blip2 transformer with BetterTransformer HuggingFace Optimum πŸš€
# YouTube Video https://youtu.be/SDCzXr-mBcw
import torch
import requests
from PIL import Image
from transformers import Blip2Processor, Blip2ForConditionalGeneration
from optimum.bettertransformer import BetterTransformer
# Load the processor and model
processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-xl")
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-flan-t5-xl", load_in_8bit=True, device_map="auto")
# Convert the model to use BetterTransformers for optimized inference
model = BetterTransformer.transform(model)
# Load the image
img_url = 'https://storage.googleapis.com/sfr-vision-language-research/BLIP/demo.jpg'
raw_image = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
# Define the question and process inputs
question = "how many dogs are in the picture?"
inputs = processor(raw_image, question, return_tensors="pt").to("cuda", torch.float16)
# Generate the output
out = model.generate(**inputs, max_new_tokens=20)
print()
print()
print("answer: ")
print(processor.decode(out[0], skip_special_tokens=True).strip())
print()
print()
accelerate==0.33.0
aiohttp==3.9.3
aiosignal==1.3.1
attrs==19.3.0
Automat==0.8.0
bitsandbytes==0.43.3
blinker==1.4
cbor2==5.6.1
certifi==2019.11.28
chardet==3.0.4
charset-normalizer==3.3.2
Click==7.0
cloud-init==23.3.3
cmake==3.29.5
colorama==0.4.3
coloredlogs==15.0.1
command-not-found==0.3
configobj==5.0.6
constantly==15.1.0
contourpy==1.2.1
cryptography==2.8
cupshelpers==1.0
cycler==0.12.1
datasets==2.21.0
dbus-python==1.2.16
defer==1.0.6
devscripts===2.20.2ubuntu2
dill==0.3.8
distro==1.4.0
distro-info==0.23+ubuntu1.1
ec2-hibinit-agent==1.0.0
entrypoints==0.3
filelock==3.14.0
fonttools==4.53.0
frozenlist==1.4.1
fsspec==2024.6.0
gpg==1.13.1
hibagent==1.0.1
httplib2==0.14.0
huggingface-hub==0.24.6
humanfriendly==10.0
hyperlink==19.0.0
idna==2.8
importlib-metadata==1.5.0
incremental==16.10.1
Jinja2==2.10.1
jsonpatch==1.22
jsonpointer==2.0
jsonschema==3.2.0
keyring==18.0.1
kiwisolver==1.4.5
language-selector==0.1
launchpadlib==1.10.13
lazr.restfulclient==0.14.2
lazr.uri==1.0.3
lit==18.1.7
macaroonbakery==1.3.1
MarkupSafe==1.1.0
matplotlib==3.9.0
more-itertools==4.2.0
mpmath==1.3.0
multidict==6.0.5
multiprocess==0.70.16
netifaces==0.10.4
networkx==3.3
numpy==1.26.4
nvidia-cublas-cu11==11.10.3.66
nvidia-cublas-cu12==12.1.3.1
nvidia-cuda-cupti-cu11==11.7.101
nvidia-cuda-cupti-cu12==12.1.105
nvidia-cuda-nvrtc-cu11==11.7.99
nvidia-cuda-nvrtc-cu12==12.1.105
nvidia-cuda-runtime-cu11==11.7.99
nvidia-cuda-runtime-cu12==12.1.105
nvidia-cudnn-cu11==8.5.0.96
nvidia-cudnn-cu12==8.9.2.26
nvidia-cufft-cu11==10.9.0.58
nvidia-cufft-cu12==11.0.2.54
nvidia-curand-cu11==10.2.10.91
nvidia-curand-cu12==10.3.2.106
nvidia-cusolver-cu11==11.4.0.1
nvidia-cusolver-cu12==11.4.5.107
nvidia-cusparse-cu11==11.7.4.91
nvidia-cusparse-cu12==12.1.0.106
nvidia-nccl-cu11==2.14.3
nvidia-nccl-cu12==2.20.5
nvidia-nvjitlink-cu12==12.5.40
nvidia-nvtx-cu11==11.7.91
nvidia-nvtx-cu12==12.1.105
oauthlib==3.1.0
opencv-python==4.10.0.82
optimum==1.21.4
packaging==24.1
pandas==2.2.2
pexpect==4.6.0
pillow==10.3.0
protobuf==3.6.1
psutil==5.9.8
pubnub==7.3.2
py-cpuinfo==9.0.0
pyarrow==17.0.0
pyasn1==0.4.2
pyasn1-modules==0.2.1
pycairo==1.16.2
pycryptodomex==3.20.0
pycups==1.9.73
PyGObject==3.36.0
PyHamcrest==1.9.0
PyJWT==1.7.1
pymacaroons==0.13.0
PyNaCl==1.3.0
pyOpenSSL==19.0.0
pyparsing==3.1.2
pyRFC3339==1.1
pyrsistent==0.15.5
pyserial==3.4
python-apt==2.0.1+ubuntu0.20.4.1
python-dateutil==2.9.0.post0
python-debian==0.1.36+ubuntu1.1
python-magic==0.4.16
pytz==2024.1
pyxdg==0.26
PyYAML==5.3.1
regex==2024.7.24
requests==2.32.3
requests-unixsocket==0.2.0
safetensors==0.4.4
scipy==1.13.1
seaborn==0.13.2
SecretStorage==2.3.1
sentencepiece==0.2.0
service-identity==18.1.0
simplejson==3.16.0
six==1.14.0
sos==4.5.6
ssh-import-id==5.10
sympy==1.12.1
systemd-python==234
tokenizers==0.19.1
torch==2.0.1
torchaudio==2.0.2
torchvision==0.15.2
tqdm==4.66.4
transformers==4.43.4
triton==2.0.0
Twisted==18.9.0
typing_extensions==4.12.2
tzdata==2024.1
ubuntu-advantage-tools==8001
ufw==0.36
ultralytics==8.2.30
ultralytics-thop==0.2.8
unattended-upgrades==0.1
unidiff==0.5.5
urllib3==1.25.8
wadllib==1.3.3
xxhash==3.5.0
yarl==1.9.4
zipp==1.0.0
zope.interface==4.7.1
@stephenlb
Copy link
Author

stephenlb commented Aug 30, 2024

Blip2 BetterTransformers

YouTube Video: https://youtu.be/SDCzXr-mBcw

pip install -r requirements.txt
python blip2-bettertransformer.py

output

answer:
one

@stephenlb
Copy link
Author

Screenshot 2024-08-30 at 11 08 25β€―AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment