Update app.py
Browse files
app.py
CHANGED
|
@@ -141,22 +141,21 @@ def load_model(model_path, lang_name):
|
|
| 141 |
new_state_dict[k] = v
|
| 142 |
|
| 143 |
# Create model with required parameters
|
| 144 |
-
# Default PARSeq parameters (standard configuration)
|
| 145 |
model = PARSeq(
|
| 146 |
-
num_tokens=len(charset_str),
|
| 147 |
-
max_label_length=100,
|
| 148 |
-
img_size=(32, 128),
|
| 149 |
-
patch_size=(4, 8),
|
| 150 |
-
embed_dim=384,
|
| 151 |
-
enc_num_heads=6,
|
| 152 |
-
enc_mlp_ratio=4,
|
| 153 |
-
enc_depth=12,
|
| 154 |
-
dec_num_heads=6,
|
| 155 |
-
dec_mlp_ratio=4,
|
| 156 |
-
dec_depth=4,
|
| 157 |
-
decode_ar=True,
|
| 158 |
-
refine_iters=1,
|
| 159 |
-
dropout=0.1
|
| 160 |
)
|
| 161 |
|
| 162 |
# Load weights
|
|
@@ -176,7 +175,7 @@ def load_model(model_path, lang_name):
|
|
| 176 |
return None, None, None
|
| 177 |
|
| 178 |
# =========================
|
| 179 |
-
# Inference
|
| 180 |
# =========================
|
| 181 |
def inference_image(model, image, device, tokenizer):
|
| 182 |
if image is None:
|
|
@@ -188,7 +187,18 @@ def inference_image(model, image, device, tokenizer):
|
|
| 188 |
img_tensor = transform(image).unsqueeze(0).to(device)
|
| 189 |
|
| 190 |
with torch.no_grad():
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
predicted_text = decode_prediction(logits, tokenizer)
|
| 193 |
|
| 194 |
probs = torch.softmax(logits, dim=-1)
|
|
|
|
| 141 |
new_state_dict[k] = v
|
| 142 |
|
| 143 |
# Create model with required parameters
|
|
|
|
| 144 |
model = PARSeq(
|
| 145 |
+
num_tokens=len(charset_str),
|
| 146 |
+
max_label_length=100,
|
| 147 |
+
img_size=(32, 128),
|
| 148 |
+
patch_size=(4, 8),
|
| 149 |
+
embed_dim=384,
|
| 150 |
+
enc_num_heads=6,
|
| 151 |
+
enc_mlp_ratio=4,
|
| 152 |
+
enc_depth=12,
|
| 153 |
+
dec_num_heads=6,
|
| 154 |
+
dec_mlp_ratio=4,
|
| 155 |
+
dec_depth=4,
|
| 156 |
+
decode_ar=True,
|
| 157 |
+
refine_iters=1,
|
| 158 |
+
dropout=0.1
|
| 159 |
)
|
| 160 |
|
| 161 |
# Load weights
|
|
|
|
| 175 |
return None, None, None
|
| 176 |
|
| 177 |
# =========================
|
| 178 |
+
# Inference - FIXED FORWARD METHOD
|
| 179 |
# =========================
|
| 180 |
def inference_image(model, image, device, tokenizer):
|
| 181 |
if image is None:
|
|
|
|
| 187 |
img_tensor = transform(image).unsqueeze(0).to(device)
|
| 188 |
|
| 189 |
with torch.no_grad():
|
| 190 |
+
# Try different forward signatures
|
| 191 |
+
try:
|
| 192 |
+
# Try with 'images' parameter
|
| 193 |
+
logits = model(images=img_tensor)
|
| 194 |
+
except TypeError:
|
| 195 |
+
try:
|
| 196 |
+
# Try with 'image' parameter
|
| 197 |
+
logits = model(image=img_tensor)
|
| 198 |
+
except TypeError:
|
| 199 |
+
# Try with just the tensor
|
| 200 |
+
logits = model(img_tensor)
|
| 201 |
+
|
| 202 |
predicted_text = decode_prediction(logits, tokenizer)
|
| 203 |
|
| 204 |
probs = torch.softmax(logits, dim=-1)
|