-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
36 lines (28 loc) · 880 Bytes
/
inference.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from src.tokenizer import loadTokenizer
from src.transforms import Transforms
import matplotlib.pyplot as plt
from src.misc import loadImage
from hazm import Normalizer
from src.nn import TRnet
import torch
import sys
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model, metadata = TRnet.from_pretrained('model-new.pth')
model = model.to(device)
tokenizer = loadTokenizer(metadata.vocab_path)
if len(sys.argv) == 2:
_, image_file_pth = sys.argv
image = Transforms((32, 512))(
loadImage(
image_file_pth
)
)
plt.imshow(image[0], cmap = 'gray')
plt.savefig('image_sample.png')
generatd = model.read_image(
pixel_values = image[None, ...]
)
gen_text = tokenizer.decode(generatd[0], ignore_special = True)
normalizer = Normalizer()
norm_gen = normalizer.normalize(gen_text)
print(f"Prediction:\n\t{norm_gen}")