Create train_script.py
Browse files- train_script.py +102 -0
train_script.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
|
| 5 |
+
from sentence_transformers import (
|
| 6 |
+
SentenceTransformer,
|
| 7 |
+
SentenceTransformerModelCardData,
|
| 8 |
+
SentenceTransformerTrainer,
|
| 9 |
+
SentenceTransformerTrainingArguments,
|
| 10 |
+
)
|
| 11 |
+
from sentence_transformers.evaluation import InformationRetrievalEvaluator
|
| 12 |
+
from sentence_transformers.losses import MultipleNegativesRankingLoss
|
| 13 |
+
from sentence_transformers.training_args import BatchSamplers
|
| 14 |
+
import logging
|
| 15 |
+
|
| 16 |
+
logging.basicConfig(format="%(asctime)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S", level=logging.INFO)
|
| 17 |
+
logging.getLogger("httpx").setLevel(logging.WARNING)
|
| 18 |
+
|
| 19 |
+
# 1. Load a model to finetune with 2. (Optional) model card data
|
| 20 |
+
model = SentenceTransformer(
|
| 21 |
+
"google/siglip-base-patch16-512",
|
| 22 |
+
model_card_data=SentenceTransformerModelCardData(
|
| 23 |
+
language="en",
|
| 24 |
+
license="apache-2.0",
|
| 25 |
+
model_name="Google SigLIP (512x512 resolution) model trained on COCO Captions",
|
| 26 |
+
),
|
| 27 |
+
tokenizer_kwargs={"do_convert_rgb": True}
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# 3. Load a dataset to finetune on
|
| 31 |
+
dataset = load_dataset("jxie/coco_captions")
|
| 32 |
+
train_dataset = dataset["train"].select(range(10_000))
|
| 33 |
+
eval_dataset = dataset["validation"].select(range(1_000))
|
| 34 |
+
test_dataset = dataset["test"].select(range(1_000))
|
| 35 |
+
|
| 36 |
+
# 4. Define a loss function
|
| 37 |
+
loss = MultipleNegativesRankingLoss(model)
|
| 38 |
+
|
| 39 |
+
# 5. (Optional) Specify training arguments
|
| 40 |
+
run_name = "google-siglip-base-coco"
|
| 41 |
+
args = SentenceTransformerTrainingArguments(
|
| 42 |
+
# Required parameter:
|
| 43 |
+
output_dir=f"models/{run_name}",
|
| 44 |
+
# Optional training parameters:
|
| 45 |
+
num_train_epochs=1,
|
| 46 |
+
per_device_train_batch_size=16,
|
| 47 |
+
per_device_eval_batch_size=16,
|
| 48 |
+
learning_rate=2e-5,
|
| 49 |
+
warmup_ratio=0.1,
|
| 50 |
+
fp16=False, # Set to False if you get an error that your GPU can't run on FP16
|
| 51 |
+
bf16=True, # Set to True if you have a GPU that supports BF16
|
| 52 |
+
batch_sampler=BatchSamplers.NO_DUPLICATES, # MultipleNegativesRankingLoss benefits from no duplicate samples in a batch
|
| 53 |
+
# Optional tracking/debugging parameters:
|
| 54 |
+
eval_strategy="steps",
|
| 55 |
+
eval_steps=0.1,
|
| 56 |
+
save_strategy="steps",
|
| 57 |
+
save_steps=0.1,
|
| 58 |
+
save_total_limit=2,
|
| 59 |
+
logging_steps=0.01,
|
| 60 |
+
run_name=run_name, # Will be used in W&B if `wandb` is installed
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# 6. (Optional) Create an evaluator & evaluate the base model
|
| 64 |
+
eval_queries = {qid: sample["caption"] for qid, sample in enumerate(eval_dataset)}
|
| 65 |
+
eval_corpus = {sample["cocoid"]: sample["image"] for sample in eval_dataset}
|
| 66 |
+
eval_relevant_docs = {qid: [sample["cocoid"]] for qid, sample in enumerate(eval_dataset)}
|
| 67 |
+
eval_evaluator = InformationRetrievalEvaluator(
|
| 68 |
+
queries=eval_queries,
|
| 69 |
+
corpus=eval_corpus,
|
| 70 |
+
relevant_docs=eval_relevant_docs,
|
| 71 |
+
name="coco-eval",
|
| 72 |
+
)
|
| 73 |
+
eval_evaluator(model)
|
| 74 |
+
|
| 75 |
+
# 7. Create a trainer & train
|
| 76 |
+
trainer = SentenceTransformerTrainer(
|
| 77 |
+
model=model,
|
| 78 |
+
args=args,
|
| 79 |
+
train_dataset=train_dataset.select_columns(["image", "caption"]),
|
| 80 |
+
eval_dataset=eval_dataset.select_columns(["image", "caption"]),
|
| 81 |
+
loss=loss,
|
| 82 |
+
evaluator=eval_evaluator,
|
| 83 |
+
)
|
| 84 |
+
trainer.train()
|
| 85 |
+
|
| 86 |
+
# (Optional) Evaluate the trained model on the test set
|
| 87 |
+
test_queries = {qid: sample["caption"] for qid, sample in enumerate(test_dataset)}
|
| 88 |
+
test_corpus = {sample["cocoid"]: sample["image"] for sample in test_dataset}
|
| 89 |
+
test_relevant_docs = {qid: [sample["cocoid"]] for qid, sample in enumerate(test_dataset)}
|
| 90 |
+
test_evaluator = InformationRetrievalEvaluator(
|
| 91 |
+
queries=test_queries,
|
| 92 |
+
corpus=test_corpus,
|
| 93 |
+
relevant_docs=test_relevant_docs,
|
| 94 |
+
name="coco-test",
|
| 95 |
+
)
|
| 96 |
+
test_evaluator(model)
|
| 97 |
+
|
| 98 |
+
# 8. Save the trained model
|
| 99 |
+
model.save_pretrained(f"models/{run_name}/final")
|
| 100 |
+
|
| 101 |
+
# 9. (Optional) Push it to the Hugging Face Hub
|
| 102 |
+
model.push_to_hub(run_name, private=True)
|