Upload 5 files
Browse files- inference.py +43 -0
- model_adventurous.pth +3 -0
- model_balanced.pth +3 -0
- model_conservative.pth +3 -0
- tags_8034.json +0 -0
inference.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import math
|
3 |
+
import time
|
4 |
+
from PIL import Image
|
5 |
+
import torch
|
6 |
+
from torchvision.transforms import transforms
|
7 |
+
|
8 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
9 |
+
model = torch.load("path/to/your/model.pth")
|
10 |
+
model.to(device)
|
11 |
+
model.eval()
|
12 |
+
|
13 |
+
transform = transforms.Compose([
|
14 |
+
transforms.ToTensor(),
|
15 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
16 |
+
])
|
17 |
+
|
18 |
+
with open("tags_8034.json", "r") as f:
|
19 |
+
tags = json.load(f)
|
20 |
+
tags.append("placeholder0")
|
21 |
+
tags = sorted(tags)
|
22 |
+
|
23 |
+
image_path = "path/to/your/image.jpg"
|
24 |
+
start = time.time()
|
25 |
+
img = Image.open(image_path).convert('RGB')
|
26 |
+
aspect_ratio = img.width / img.height
|
27 |
+
new_height = math.sqrt(512 ** 2 / aspect_ratio)
|
28 |
+
new_width = aspect_ratio * new_height
|
29 |
+
img.resize((int(new_width), int(new_height)), Image.LANCZOS)
|
30 |
+
tensor = transform(img).unsqueeze(0).to(device)
|
31 |
+
with torch.no_grad():
|
32 |
+
out = model(tensor)
|
33 |
+
probabilities = torch.nn.functional.sigmoid(out[0])
|
34 |
+
|
35 |
+
indices = torch.where(probabilities > 0.3)[0]
|
36 |
+
values = probabilities[indices]
|
37 |
+
|
38 |
+
for i in range(indices.size(0)):
|
39 |
+
print(tags[indices[i]], values[i].item())
|
40 |
+
|
41 |
+
end = time.time()
|
42 |
+
print(f'Executed in {end - start} seconds')
|
43 |
+
print("\n\n", end="")
|
model_adventurous.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ff3bb8d813d7dbc6a017865aefa201feebaadad72f234d5deadc41a9ad163edd
|
3 |
+
size 254284770
|
model_balanced.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c04afa3cb5d9848f642f6ed25ac457d4adba5ea300d55c8c86e52dcfc7f2727a
|
3 |
+
size 254284770
|
model_conservative.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:56eca9dbaf3ce1fcb158076c4c47c47bcbff7acf6aac02ec398e4e3b1b7e3a31
|
3 |
+
size 254284770
|
tags_8034.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|