(model, image, inputs, outputs)
| 43 | |
| 44 | |
| 45 | def evaluate_from_pil(model, image, inputs, outputs): |
| 46 | if isinstance(model.image_processor, Compose) or hasattr(model.image_processor, "is_prismatic"): |
| 47 | # This is a standard `torchvision.transforms` object or custom PrismaticVLM wrapper |
| 48 | adv_pixel_values = model.image_processor(image).unsqueeze(0) |
| 49 | else: |
| 50 | # Assume `image_transform` is an HF ImageProcessor... |
| 51 | adv_pixel_values = model.image_processor(image, return_tensors="pt")["pixel_values"] |
| 52 | adv_pixel_values = adv_pixel_values.to(model.distributed_state.device) |
| 53 | |
| 54 | gen_texts = model.generate_answer(adv_pixel_values, inputs) |
| 55 | assert len(gen_texts) == len(outputs) |
| 56 | |
| 57 | acc = sum([gen_text == output for gen_text, output in zip(gen_texts, outputs)]) / len(outputs) |
| 58 | for output, gen_text in zip(outputs, gen_texts): |
| 59 | print("Generated text:", gen_text) |
| 60 | print("Target text:", output) |
| 61 | |
| 62 | print("Accuracy:", acc) |
| 63 | return acc, gen_texts |
| 64 | |
| 65 | |
| 66 | def resize_image(image, size): |
no test coverage detected