Create utils
Browse files
utils
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import open_clip
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
model, _, transform = open_clip.create_model_and_transforms(
|
6 |
+
model_name="coca_ViT-L-14",
|
7 |
+
pretrained="mscoco_finetuned_laion2B-s13B-b90k"
|
8 |
+
)
|
9 |
+
|
10 |
+
def get_captions(image):
|
11 |
+
im = transform(image).unsqueeze(0)
|
12 |
+
|
13 |
+
with torch.no_grad(), torch.cuda.amp.autocast():
|
14 |
+
generated = model.generate(im)
|
15 |
+
|
16 |
+
return open_clip.decode(generated[0]).split("<end_of_text>")[0].replace("<start_of_text>", "")
|