-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added CLIP module and redesigned tokenizer apis (#81)
* merged clip tokenizer Signed-off-by: BAAI-OpenPlatform <[email protected]> * Update inference_clip.py * Update auto_loader.py * Update glm_10b_en_tokenizer.py * swinv1v2 Signed-off-by: zhaohu xing <[email protected]> * updated the version Signed-off-by: Anhforth <[email protected]> * updated the requirement packages list Signed-off-by: Anhforth <[email protected]> * fixed some issues Signed-off-by: BAAI-OpenPlatform <[email protected]> * fixed some issues Signed-off-by: BAAI-OpenPlatform <[email protected]> * tried to fix the data directory not found error Signed-off-by: BAAI-OpenPlatform <[email protected]> * fixed issues in running glm_seq2seq Signed-off-by: BAAI-OpenPlatform <[email protected]> * Update test_glm_seq2seq.py * Update setup.py Signed-off-by: Anhforth <[email protected]> Signed-off-by: ZhaodongYan1 <[email protected]> Signed-off-by: zhaohu xing <[email protected]> Signed-off-by: shunxing1234 <[email protected]> Signed-off-by: BAAI-OpenPlatform <[email protected]> Co-authored-by: Anhforth <[email protected]> Co-authored-by: zhaohu xing <[email protected]> Co-authored-by: Zhaodong Yan <[email protected]> Co-authored-by: ZhaodongYan1 <[email protected]> Co-authored-by: Zac Liu <[email protected]> Co-authored-by: zhaohu xing <[email protected]> Co-authored-by: jongjyh <[email protected]> Co-authored-by: wchh-2000 <[email protected]> Co-authored-by: xuanricheng <[email protected]> Co-authored-by: shunxing1234 <[email protected]>
- Loading branch information
1 parent
693f9a4
commit dee25b7
Showing
88 changed files
with
4,599 additions
and
610 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
title filepath | ||
a very typical bus station 0.jpg | ||
the jetty : different types of plants to establish a variety of ecosystems . 1.jpg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"train_micro_batch_size_per_gpu": 64, | ||
"gradient_accumulation_steps": 1, | ||
"steps_per_print": 100, | ||
"gradient_clipping": 1.0, | ||
"zero_optimization": { | ||
"stage": 2, | ||
"contiguous_gradients": false, | ||
"overlap_comm": true, | ||
"reduce_scatter": true, | ||
"reduce_bucket_size": 5e7, | ||
"allgather_bucket_size": 5e7, | ||
"cpu_offload": true | ||
}, | ||
"scheduler": { | ||
"type": "WarmupLR", | ||
"params": { | ||
"warmup_min_lr": 0, | ||
"warmup_max_lr": 1e-5, | ||
"warmup_num_steps": 2000 | ||
} | ||
}, | ||
"zero_allow_untested_optimizer": true, | ||
"fp16": { | ||
"enabled": true, | ||
"loss_scale": 0, | ||
"loss_scale_window": 1000, | ||
"hysteresis": 2, | ||
"min_loss_scale": 1 | ||
}, | ||
"optimizer": { | ||
"type": "Adam", | ||
"params": { | ||
"lr": 1e-5, | ||
"weight_decay": 0.1, | ||
"betas": [ | ||
0.9, | ||
0.98 | ||
], | ||
"eps": 1e-6 | ||
} | ||
}, | ||
"activation_checkpointing": { | ||
"partition_activations": true, | ||
"contiguous_memory_optimization": false | ||
}, | ||
"wall_clock_breakdown": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
127.0.0.1 slots=2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import torch | ||
from PIL import Image | ||
from flagai.auto_model.auto_loader import AutoLoader | ||
from flagai.data.dataset.mm.clip_dataset import clip_transform | ||
|
||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
|
||
loader = AutoLoader(task_name="txt_img_matching", #contrastive learning | ||
model_name="clip-base-p32-224") | ||
|
||
model = loader.get_model() | ||
model.eval() | ||
model.to(device) | ||
tokenizer = loader.get_tokenizer() | ||
transform = clip_transform(img_size=model.image_size) | ||
|
||
def inference(): | ||
image = Image.open("./CLIP.png") | ||
image = transform(image).unsqueeze(0).to(device) | ||
text = tokenizer.tokenize_as_tensor(["a diagram", "a dog", "a cat"]).to(device) | ||
|
||
with torch.no_grad(): | ||
image_features = model.encode_image(image) | ||
text_features = model.encode_text(text) | ||
text_probs = (image_features @ text_features.T).softmax(dim=-1) | ||
|
||
print(text_probs.cpu().numpy()[0].tolist()) | ||
|
||
if __name__=="__main__": | ||
inference() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import torch | ||
from flagai.data.dataset.mm.clip_dataset import CsvDataset, clip_transform, collate_fn | ||
from flagai.trainer import Trainer | ||
from flagai.auto_model.auto_loader import AutoLoader | ||
|
||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
# cd examples/clip | ||
data_path = "./data/pairs.csv" | ||
img_dir = "./data/img" | ||
|
||
trainer = Trainer(env_type="pytorch", | ||
epochs=5, | ||
pytorch_device=device, | ||
batch_size=64, | ||
lr=1e-4, | ||
log_interval=10, | ||
) | ||
|
||
loader = AutoLoader(task_name="txt_img_matching",#contrastive learning | ||
model_name="clip-base-p32-224", | ||
) | ||
model = loader.get_model() | ||
tokenizer = loader.get_tokenizer() | ||
|
||
transform = clip_transform(img_size=model.image_size) | ||
train_dataset = CsvDataset(data_path, | ||
img_dir, | ||
transform, | ||
tokenizer) | ||
|
||
optimizer = torch.optim.Adam(model.parameters(), lr=1e-5) | ||
trainer.train(model, | ||
optimizer=optimizer, | ||
train_dataset=train_dataset, | ||
collate_fn=collate_fn) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import torch | ||
from flagai.data.dataset.mm.clip_dataset import CsvDataset, clip_transform, collate_fn | ||
from flagai.trainer import Trainer | ||
from flagai.auto_model.auto_loader import AutoLoader | ||
|
||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
# cd examples/clip | ||
data_path = "./data/pairs.csv"#"/mnt/datasets/multimodal/ConceptualCaptions/Train_GCC-training_output.csv" | ||
img_dir = "./data/img"#"/mnt/datasets/multimodal/ConceptualCaptions" | ||
|
||
trainer = Trainer( | ||
env_type="deepspeed", | ||
experiment_name="clip", | ||
batch_size=64, | ||
num_gpus=2, | ||
fp16=True, | ||
gradient_accumulation_steps=1, | ||
lr=1e-4, | ||
weight_decay=1e-5, | ||
epochs=5, | ||
log_interval=1, | ||
load_dir=None, | ||
pytorch_device=device, | ||
save_dir="clip_deepspeed", | ||
save_interval=1000, | ||
num_checkpoints=1, | ||
hostfile="./deepspeed/hostfile", | ||
training_script=__file__, | ||
deepspeed_config="./deepspeed.json" | ||
) | ||
loader = AutoLoader(task_name="txt_img_matching",#contrastive learning | ||
model_name="clip-base-p32-224", | ||
) | ||
model = loader.get_model() | ||
tokenizer = loader.get_tokenizer() | ||
|
||
transform = clip_transform(img_size=model.image_size) | ||
train_dataset = CsvDataset(data_path, | ||
img_dir, | ||
transform, | ||
tokenizer) | ||
|
||
optimizer = torch.optim.Adam(model.parameters(), lr=1e-5) | ||
trainer.train(model, | ||
optimizer=optimizer, | ||
train_dataset=train_dataset, | ||
collate_fn=collate_fn) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.