-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinference.py
More file actions
269 lines (216 loc) · 8.3 KB
/
inference.py
File metadata and controls
269 lines (216 loc) · 8.3 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import os
import argparse
from pathlib import Path
from typing import List, Any, Optional, Dict
import torch
from PIL import Image
from src.config import (
LLM_NAME, VIT_NAME, ASPECT_LABELS, NUM_ASPECTS, IMAGE_SIZE, MAX_TEXT_LEN,
DATA_DIR, OUTPUT_DIR,
)
from src.aspect_model import MultimodalACSAModel
from transformers import AutoTokenizer, AutoProcessor
SENTIMENT_LABELS = ["Irrelative", "Negative", "Neutral", "Positive"]
_siglip_processor = None
def _get_siglip_processor():
global _siglip_processor
if _siglip_processor is None:
_siglip_processor = AutoProcessor.from_pretrained(VIT_NAME)
return _siglip_processor
def parse_args():
parser = argparse.ArgumentParser(description="Inference with MultimodalSentimentModel")
parser.add_argument("--checkpoint", type=str, default=None,
help="Path to single checkpoint .pt file (or outputs dir for best_checkpoint.pt)")
parser.add_argument("--data_dir", type=str, default=DATA_DIR)
parser.add_argument("--device", type=str, default="cuda" if torch.cuda.is_available() else "cpu")
parser.add_argument("--use_lora", action="store_true", default=True)
parser.add_argument("--no_lora", dest="use_lora", action="store_false")
return parser.parse_args()
def load_model(checkpoint_path: Optional[str], device: str = "cuda") -> MultimodalACSAModel:
"""
Load trained MultimodalSentimentModel model (single model, all 6 aspects).
If checkpoint_path is None, returns model with random weights (for testing).
"""
from training import load_model_weights
model = MultimodalACSAModel(use_lora=True).to(device)
if checkpoint_path:
ckpt = Path(checkpoint_path)
if ckpt.is_dir():
ckpt = ckpt / "best_checkpoint.pt"
if ckpt.exists():
load_model_weights(model, str(ckpt), device=device)
print(f"Loaded weights from {ckpt}")
else:
print(f"Warning: checkpoint not found at {ckpt}, using random weights")
return model
def preprocess_image(
image_path: str,
size: int = IMAGE_SIZE,
) -> torch.Tensor:
"""
Load and preprocess a single image using SigLIP processor.
Returns [3, H, W] tensor normalized by the processor.
"""
processor = _get_siglip_processor()
img = Image.open(image_path).convert("RGB")
inputs = processor(images=img, return_tensors="pt")
pixel_values = inputs["pixel_values"][0] # [3, H, W]
return pixel_values
def preprocess_images(
image_paths: List[str],
max_images: int = 7,
size: int = IMAGE_SIZE,
) -> torch.Tensor:
"""
Load and stack multiple images.
Returns [max_images, 3, H, W] tensor.
"""
images = []
for path in image_paths[:max_images]:
try:
img_tensor = preprocess_image(path, size=size)
images.append(img_tensor)
except Exception:
pass
while len(images) < max_images:
images.append(torch.zeros(3, size, size))
return torch.stack(images)
def predict_all_aspects(
model: MultimodalACSAModel,
tokenizer,
comment: str,
image_paths: List[str],
roi_data: Optional[List[Dict[str, Any]]] = None,
device: str = "cuda",
) -> Dict[str, Any]:
"""
Predict sentiments for all 6 aspects in a single forward pass.
roi_data: list of roi dicts per image, same format as dataset.
If None, RoI encoder will return zeros.
"""
model.eval()
pixel_values = preprocess_images(image_paths).to(device)
aspect_labels = {i: 0 for i in range(NUM_ASPECTS)}
# Default roi_data: empty boxes for each image, wrapped as per-sample list
if roi_data is None:
roi_data = [[{"boxes": [], "labels": []} for _ in image_paths[:7]]]
encodings = tokenizer(
comment,
return_tensors="pt",
padding=True,
truncation=True,
max_length=MAX_TEXT_LEN,
)
input_ids = encodings["input_ids"].to(device)
attention_mask = encodings["attention_mask"].to(device)
with torch.no_grad():
logits = model.inference(
input_ids=input_ids,
attention_mask=attention_mask,
pixel_values=pixel_values.unsqueeze(0),
roi_data=roi_data,
aspect_labels=[aspect_labels],
)
results = {}
for asp_idx, aspect_name in enumerate(ASPECT_LABELS):
probs = logits[asp_idx].cpu().tolist()
pred_id = int(torch.argmax(logits[asp_idx]).item())
results[aspect_name] = {
"prediction": SENTIMENT_LABELS[pred_id],
"prediction_id": pred_id,
"confidence": probs[pred_id],
"probabilities": dict(zip(SENTIMENT_LABELS, probs)),
}
return results
def predict_batch(
model: MultimodalACSAModel,
tokenizer,
comments: List[str],
image_paths_list: List[List[str]],
device: str = "cuda",
batch_size: int = 8,
) -> List[Dict[str, Any]]:
"""
Predict sentiments for a batch of samples (all 6 aspects each).
"""
model.eval()
results = []
for i in range(0, len(comments), batch_size):
batch_comments = comments[i : i + batch_size]
batch_images = image_paths_list[i : i + batch_size]
B = len(batch_comments)
pixel_values_list = []
for paths in batch_images:
pv = preprocess_images(paths)
pixel_values_list.append(pv)
pixel_values = torch.stack(pixel_values_list).to(device)
aspect_labels = [{j: 0 for j in range(NUM_ASPECTS)} for _ in range(B)]
roi_data = [[{"boxes": [], "labels": []} for _ in range(len(paths))] for paths in batch_images]
encodings = tokenizer(
batch_comments,
return_tensors="pt",
padding=True,
truncation=True,
max_length=MAX_TEXT_LEN,
)
input_ids = encodings["input_ids"].to(device)
attention_mask = encodings["attention_mask"].to(device)
with torch.no_grad():
logits = model.inference(
input_ids=input_ids,
attention_mask=attention_mask,
pixel_values=pixel_values,
roi_data=roi_data,
aspect_labels=aspect_labels,
)
logits_per_sample = logits.view(B, NUM_ASPECTS, -1)
for b, comment in enumerate(batch_comments):
sample_result = {}
for asp_idx, aspect_name in enumerate(ASPECT_LABELS):
probs = logits_per_sample[b, asp_idx].cpu().tolist()
pred_id = int(torch.argmax(logits_per_sample[b, asp_idx]).item())
sample_result[aspect_name] = {
"prediction": SENTIMENT_LABELS[pred_id],
"prediction_id": pred_id,
"confidence": probs[pred_id],
"probabilities": dict(zip(SENTIMENT_LABELS, probs)),
}
results.append(sample_result)
return results
def main():
args = parse_args()
device = torch.device(args.device)
tokenizer = AutoTokenizer.from_pretrained(LLM_NAME, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token
print("MultimodalACSAModel Inference (encode-once, aspect-loop)")
print(f"Device: {device}")
print("Loading model...")
model = load_model(args.checkpoint, device)
print("\nEnter a review and image path to get predictions for all 6 aspects.")
print("Type 'quit' to exit.\n")
while True:
try:
comment = input("Review text: ").strip()
if comment.lower() in ["quit", "exit", "q"]:
break
if not comment:
continue
image_path = input("Image path (or press Enter for no image): ").strip()
image_paths = [image_path] if image_path else []
results = predict_all_aspects(
model=model,
tokenizer=tokenizer,
comment=comment,
image_paths=image_paths,
roi_data=None,
device=str(device),
)
print("\nPredictions:")
for aspect, result in results.items():
print(f" {aspect}: {result['prediction']} "
f"(confidence: {result['confidence']:.3f})")
except (KeyboardInterrupt, EOFError):
break
print("\nDone.")
if __name__ == "__main__":
main()