Upload README.md
Browse files
README.md
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# How to use the model?
|
2 |
+
|
3 |
+
In order to use the model use can use the class in model.py like the example below:
|
4 |
+
|
5 |
+
```Python
|
6 |
+
from model import Net
|
7 |
+
import torch
|
8 |
+
import torchvision
|
9 |
+
import torch.nn as nn
|
10 |
+
from torchvision import transforms
|
11 |
+
import torch.nn.functional as F
|
12 |
+
from PIL import Image
|
13 |
+
from transformers import AutoTokenizer, AutoModel
|
14 |
+
|
15 |
+
model = Net()
|
16 |
+
# If you use model on cpu you need the map_location part
|
17 |
+
model.load_state_dict(torch.load("clip_model.pt", map_location=torch.device('cpu')))
|
18 |
+
model.eval()
|
19 |
+
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained("dbmdz/distilbert-base-turkish-cased")
|
21 |
+
|
22 |
+
transform=transforms.Compose(
|
23 |
+
[
|
24 |
+
transforms.Resize((224, 224)),
|
25 |
+
transforms.ToTensor(),
|
26 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
27 |
+
],
|
28 |
+
)
|
29 |
+
|
30 |
+
def predict(img,text_vec):
|
31 |
+
input = transform(img).unsqueeze(0)
|
32 |
+
token_list = tokenizer(text_vec,padding = True)
|
33 |
+
|
34 |
+
text = torch.Tensor(token_list["input_ids"]).long()
|
35 |
+
mask = torch.Tensor(token_list["attention_mask"]).long()
|
36 |
+
|
37 |
+
|
38 |
+
image_vec, text_vec = model(input, text , mask)
|
39 |
+
print(F.softmax(torch.matmul(image_vec,text_vec.T),dim=1))
|
40 |
+
|
41 |
+
img = Image.open("dog.png") # A dog image
|
42 |
+
|
43 |
+
text_vec = ["Çimenler içinde bir köpek.","Bir köpek.","Çimenler içinde bir kuş."] # Descriptions
|
44 |
+
predict(img,text_vec) # Probabilities for each description
|
45 |
+
|
46 |
+
```
|
47 |
+
|
48 |
+
|