Create readme.md
Browse files
readme.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
### Model Summary
|
| 2 |
+
|
| 3 |
+
The checkpoint aligns with our pixel-linguist-all setting in the paper. The model is initialized from our monolingual model, and is trained on parallel data (205000 steps) <-> AllNLI (2600 steps), going back and forth for three rounds. This model is the last round checkpoint. We recommend using it with A100 GPU, aligning with training.
|
| 4 |
+
|
| 5 |
+
### Downstream Use
|
| 6 |
+
|
| 7 |
+
Semantic Textual Similarity, Information Retrieval, Reasoning Retrieval
|
| 8 |
+
|
| 9 |
+
### Out-of-Scope Use
|
| 10 |
+
|
| 11 |
+
The model might not be optimal for further fine-tuning to do other tasks (such as classification), as it's trained to do representation tasks with similarity matching.
|
| 12 |
+
|
| 13 |
+
### Training Data
|
| 14 |
+
|
| 15 |
+
Please refer to the paper for the exact process.
|
| 16 |
+
|
| 17 |
+
## Inference
|
| 18 |
+
Encoding with our PixelLinguist class is very straightforward, just like using a SentenceTransformer class.
|
| 19 |
+
|
| 20 |
+
```python
|
| 21 |
+
model_name = "AnonymousPage/checkpoint-all"
|
| 22 |
+
model = PixelLinguist(model_name)
|
| 23 |
+
texts = ["I love you","I like you"]
|
| 24 |
+
embeddings = model.encode(texts)
|
| 25 |
+
print(outputs[0] @ outputs[1].T) # just use dot product because the embeddings are normalized automatically in the model class.
|
| 26 |
+
#tensor(0.9217)
|
| 27 |
+
```
|
| 28 |
+
|
| 29 |
+
To use the PixelLinguist class: First install the package following our Github Repo. Then define our PixelLinguist Class as follow.
|
| 30 |
+
|
| 31 |
+
```python
|
| 32 |
+
import torch
|
| 33 |
+
from PIL import Image
|
| 34 |
+
from pixel import (
|
| 35 |
+
AutoConfig,
|
| 36 |
+
PangoCairoTextRenderer,
|
| 37 |
+
PIXELForSequenceClassification,
|
| 38 |
+
PIXELForRepresentation,
|
| 39 |
+
PoolingMode,
|
| 40 |
+
get_attention_mask,
|
| 41 |
+
get_transforms,
|
| 42 |
+
glue_strip_spaces,
|
| 43 |
+
resize_model_embeddings,
|
| 44 |
+
)
|
| 45 |
+
from tqdm import tqdm
|
| 46 |
+
class PixelLinguist:
|
| 47 |
+
def __init__(self, model_name, batch_size = 16, max_seq_length = 64,
|
| 48 |
+
device=None, pooling = "mean", keep_mlp = False):
|
| 49 |
+
if device is not None:
|
| 50 |
+
self.device = device
|
| 51 |
+
else:
|
| 52 |
+
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 53 |
+
self.config = AutoConfig.from_pretrained(model_name, num_labels=0)
|
| 54 |
+
self.batch_size = batch_size
|
| 55 |
+
if keep_mlp == True:
|
| 56 |
+
self.model = PIXELForSequenceClassification.from_pretrained(
|
| 57 |
+
model_name,
|
| 58 |
+
config=self.config,
|
| 59 |
+
pooling_mode=PoolingMode.from_string(pooling),
|
| 60 |
+
add_layer_norm=True
|
| 61 |
+
).to(self.device)
|
| 62 |
+
else:
|
| 63 |
+
self.model = PIXELForRepresentation.from_pretrained(
|
| 64 |
+
model_name,
|
| 65 |
+
config=self.config,
|
| 66 |
+
pooling_mode=PoolingMode.from_string(pooling),
|
| 67 |
+
add_layer_norm=True
|
| 68 |
+
).to(self.device)
|
| 69 |
+
self.processor = PangoCairoTextRenderer.from_pretrained(model_name, rgb=False)
|
| 70 |
+
self.processor.max_seq_length = max_seq_length
|
| 71 |
+
resize_model_embeddings(self.model, self.processor.max_seq_length)
|
| 72 |
+
self.transforms = get_transforms(do_resize=True, size=(self.processor.pixels_per_patch, self.processor.pixels_per_patch * self.processor.max_seq_length))
|
| 73 |
+
def preprocess(self, texts):
|
| 74 |
+
encodings = [self.processor(text=glue_strip_spaces(a)) for a in texts]
|
| 75 |
+
pixel_values = torch.stack([self.transforms(Image.fromarray(e.pixel_values)) for e in encodings])
|
| 76 |
+
attention_mask = torch.stack([get_attention_mask(e.num_text_patches, seq_length=self.processor.max_seq_length) for e in encodings])
|
| 77 |
+
return {'pixel_values': pixel_values, 'attention_mask': attention_mask}
|
| 78 |
+
def encode(self, texts, **kwargs):
|
| 79 |
+
all_outputs = []
|
| 80 |
+
for i in tqdm(range(0, len(texts), self.batch_size)):
|
| 81 |
+
batch_texts = texts[i:i+batch_size]
|
| 82 |
+
inputs = self.preprocess(batch_texts)
|
| 83 |
+
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
| 84 |
+
with torch.no_grad():
|
| 85 |
+
outputs = self.model(**inputs).logits.detach().cpu()
|
| 86 |
+
all_outputs.append(outputs)
|
| 87 |
+
return torch.cat(all_outputs, dim=0)
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
### Evaluation
|
| 91 |
+
|
| 92 |
+
For STS evaluation (see Github repo):
|
| 93 |
+
```
|
| 94 |
+
python tools/evaluation_sts_all.py
|
| 95 |
+
```
|
| 96 |
+
For BEIR information retrieval evaluation (see Github repo):
|
| 97 |
+
```
|
| 98 |
+
python tools/evaluation_retrieval.py
|
| 99 |
+
```
|