Download MedEvalKit/models/Med_Flamingo/src/utils.py from Master-Shi/ExplainableVQA: direct link, hf CLI and curl.
- Browser
- Download file 1.39 kB
-
https://huggingface.co/datasets/Master-Shi/ExplainableVQA/resolve/main/MedEvalKit/models/Med_Flamingo/src/utils.py
- Command line
-
hf download hf://datasets/Master-Shi/ExplainableVQA/MedEvalKit/models/Med_Flamingo/src/utils.py
-
curl -L -o utils.py https://huggingface.co/datasets/Master-Shi/ExplainableVQA/resolve/main/MedEvalKit/models/Med_Flamingo/src/utils.py
1.39 kB
| import torch | |
| from abc import ABC, abstractmethod | |
| class AbstractProcessor(ABC): | |
| """ | |
| Abstract class for processors to show what methods they need to implement. | |
| Processors handle text encoding and image preprocessing. | |
| """ | |
| def encode_text(self, prompt): | |
| pass | |
| def preprocess_images(self, images: list): | |
| pass | |
| class FlamingoProcessor(AbstractProcessor): | |
| """ | |
| Processor class for Flamingo. | |
| """ | |
| def __init__(self, tokenizer, vision_processor): | |
| """ | |
| OF does not use same vision processor, image_processor only transforms single image | |
| """ | |
| self.tokenizer = tokenizer | |
| self.vision_processor = vision_processor | |
| def encode_text(self, prompt): | |
| self.tokenizer.padding_side = "left" | |
| # For generation padding tokens should be on the left | |
| return self.tokenizer([prompt], | |
| return_tensors="pt", | |
| ) | |
| def preprocess_images(self, images: list): | |
| vision_x = [self.vision_processor(im).unsqueeze(0) for im in images] | |
| vision_x = torch.cat(vision_x, dim=0) | |
| return vision_x | |
| def clean_generation(response): | |
| """ | |
| for some reason, the open-flamingo based model slightly changes the input prompt (e.g. prepends <unk>, an adds some spaces) | |
| """ | |
| return response.replace('<unk> ', '').strip() |