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