Commit
•
ee1f1f1
1
Parent(s):
09eb282
update_model_init_fp16 (#12)
Browse files- update model init with float16 (8c55fe1ada8186cc1d35785756e0e63ef1e6827a)
Co-authored-by: Haiping Wu <haipingwu@users.noreply.huggingface.co>
- README.md +9 -5
- config.json +1 -1
README.md
CHANGED
@@ -27,7 +27,7 @@ Resources and Technical Documentation:
|
|
27 |
|
28 |
## How to Get Started with the Model
|
29 |
|
30 |
-
Use the code below to get started with the model.
|
31 |
|
32 |
```python
|
33 |
import requests
|
@@ -35,8 +35,10 @@ import requests
|
|
35 |
from PIL import Image
|
36 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
37 |
|
|
|
|
|
38 |
|
39 |
-
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
|
40 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
|
41 |
|
42 |
prompt = "<OD>"
|
@@ -44,7 +46,7 @@ prompt = "<OD>"
|
|
44 |
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
|
45 |
image = Image.open(requests.get(url, stream=True).raw)
|
46 |
|
47 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
48 |
|
49 |
generated_ids = model.generate(
|
50 |
input_ids=inputs["input_ids"],
|
@@ -77,8 +79,10 @@ import requests
|
|
77 |
from PIL import Image
|
78 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
79 |
|
|
|
|
|
80 |
|
81 |
-
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
|
82 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
|
83 |
|
84 |
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
|
@@ -89,7 +93,7 @@ def run_example(task_prompt, text_input=None):
|
|
89 |
prompt = task_prompt
|
90 |
else:
|
91 |
prompt = task_prompt + text_input
|
92 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
93 |
generated_ids = model.generate(
|
94 |
input_ids=inputs["input_ids"],
|
95 |
pixel_values=inputs["pixel_values"],
|
|
|
27 |
|
28 |
## How to Get Started with the Model
|
29 |
|
30 |
+
Use the code below to get started with the model. All models are trained with float16.
|
31 |
|
32 |
```python
|
33 |
import requests
|
|
|
35 |
from PIL import Image
|
36 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
37 |
|
38 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
39 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
40 |
|
41 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
|
42 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
|
43 |
|
44 |
prompt = "<OD>"
|
|
|
46 |
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
|
47 |
image = Image.open(requests.get(url, stream=True).raw)
|
48 |
|
49 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
|
50 |
|
51 |
generated_ids = model.generate(
|
52 |
input_ids=inputs["input_ids"],
|
|
|
79 |
from PIL import Image
|
80 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
81 |
|
82 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
83 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
84 |
|
85 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
|
86 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base", trust_remote_code=True)
|
87 |
|
88 |
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
|
|
|
93 |
prompt = task_prompt
|
94 |
else:
|
95 |
prompt = task_prompt + text_input
|
96 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
|
97 |
generated_ids = model.generate(
|
98 |
input_ids=inputs["input_ids"],
|
99 |
pixel_values=inputs["pixel_values"],
|
config.json
CHANGED
@@ -79,7 +79,7 @@
|
|
79 |
"image_feature_source": ["spatial_avg_pool", "temporal_avg_pool"]
|
80 |
},
|
81 |
"vocab_size": 51289,
|
82 |
-
"torch_dtype": "
|
83 |
"transformers_version": "4.41.0.dev0",
|
84 |
"is_encoder_decoder": true
|
85 |
}
|
|
|
79 |
"image_feature_source": ["spatial_avg_pool", "temporal_avg_pool"]
|
80 |
},
|
81 |
"vocab_size": 51289,
|
82 |
+
"torch_dtype": "float16",
|
83 |
"transformers_version": "4.41.0.dev0",
|
84 |
"is_encoder_decoder": true
|
85 |
}
|