Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
4d925c6
Add new branch.
zmgong Apr 4, 2025
47577ad
Merge commit '7fb8e020f139aceee889f206a42e006b618a38bc' into before_m…
Apr 15, 2025
4e967fb
fix(config): Add missing config
Apr 15, 2025
51b4e4e
fix(training): Fix new barcodeBERT tokenizer
Apr 15, 2025
907efb5
Initial attempt for just change .logits.softmax(dim=-1).mean(dim=1) t…
zmgong Apr 16, 2025
9963b3d
Add a temporary config for testing the softmax fix.
zmgong Apr 16, 2025
31b3f0f
Add a temporary config for testing the softmax fix with a smaller bat…
zmgong Apr 16, 2025
00a492e
Merge commit '51b4e4eb62bffaf5c76fb524664b7b5e878ebc71' into before_m…
Apr 17, 2025
3916b2b
refactor(dataset): Move tokenizer to dataset.py
Apr 17, 2025
6974b88
feat(dataset): Support batch for new barcodebert tokenizer
Apr 17, 2025
6d64867
Merge commit '6fd7556946d5e07c7bb7801aab459c2e37d8a891' into before_m…
Apr 17, 2025
710f57f
style(dataset): Remove comment
Apr 17, 2025
950a4a1
fix(dataset): Fixed the problem of tokenizer loading data too slowly
Apr 17, 2025
7c0d919
style(dataset): Delete unused tokenizers and format the code
Apr 18, 2025
736e499
style(training): Format the code
Apr 18, 2025
fac118b
fix(inference): Add missing library
Apr 18, 2025
462a599
Update the font size of some figure plotting scripts.
zmgong Apr 21, 2025
779da4f
update the model names to more proper names in multiple config files.
zmgong Apr 21, 2025
5802ac8
Remove the finished tod comment.
zmgong Apr 21, 2025
d831530
fix(dataset): Correct typo
Apr 21, 2025
b8ef8ce
Merge commit '5802ac80745c2417661ebc29641ea7498c6a8460' into before_m…
Apr 23, 2025
462ab50
refactor(config): Update config for barcodeBERT comparison
Apr 23, 2025
bba13d3
accept 'bert-small' as valid language model name
zmgong Apr 23, 2025
8609a6b
Same for previous commit, now for the language tokenizer init, we als…
zmgong Apr 23, 2025
a045f8f
Fix a tiny bug. Now the dataloader is not looking for pre-train_model…
zmgong Apr 23, 2025
08d9215
accept bert-small and bert_small
zmgong Apr 23, 2025
8c5e83d
stop using pre-train-model to define model name. Will do a further re…
zmgong Apr 23, 2025
5e7f148
do not tokenize when not language model.
zmgong Apr 23, 2025
0ca8898
None leadd to error during collecting, replace none with empty list
zmgong Apr 23, 2025
8031f16
None leadd to error during collecting, replace none with empty tensor
zmgong Apr 23, 2025
cdc34df
Change the model name that support for language encoder.
zmgong Apr 27, 2025
e19dabf
Change the epoch number for the final experiments to 50, as what we d…
zmgong Apr 27, 2025
7cfb016
Update the code to use the newer dna tokenizer with attention mask an…
zmgong May 4, 2025
163f845
Remove the unused model file for the language.
zmgong May 4, 2025
08940c2
dna_input_batch can also be a dictionary now.
zmgong May 4, 2025
86beb64
Change the input ids, token type ids and attention mask to tensor.
zmgong May 4, 2025
516738d
modify the debug flag function. Now it only test for 1 training step …
zmgong May 4, 2025
b0ab6ed
Fix a small bug that accidentally disabled the language tokenizer.
zmgong May 4, 2025
8b38955
update the un-align baseline config.
zmgong May 4, 2025
2ebe1d9
Quick check for stride value
zmgong May 4, 2025
660e877
Remove the test code.
zmgong May 4, 2025
989dc64
Fix a very very weird bug for the tokenizer...
zmgong May 4, 2025
4ba9a9f
Change the way to generate tokens to use in get item.
zmgong May 4, 2025
a2e29c4
Change back the debug method. Stop just testing one step.
zmgong May 4, 2025
6f1a704
Update the config for image text alignment for BIOSCAN-1M
zmgong May 4, 2025
6b592cb
Set the function of debug flag back to normal
zmgong May 4, 2025
d5fcb57
Change the kmer tokenization a bit. Now any token with N in it will b…
zmgong May 4, 2025
080a775
Merge branch '33-update-the-softmax-issue-with-dna-encoder' into befo…
May 5, 2025
e2acf1c
fix: Resolve tensor resizing error in data loading
May 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions bioscanclip/config/global_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ insect_data:
species_to_other: ${insect_data.dir}/specie_to_other_labels.json
save_ckpt: true
bioscan_bert_checkpoint: ${project_root_path}/ckpt/BarcodeBERT/5_mer/model_41.pth
bioscan_bert_checkpoint_trained_with_canada_1_5_m: ${project_root_path}/ckpt/BarcodeBERT/new_checkpoints/trained_with_canada_1_5M/CANADA-1.5M-BEST_k4_4_4_w1_m0_r0_wd.pt
bioscan_bert_checkpoint_trained_with_bioscan_5_m: ${project_root_path}/ckpt/BarcodeBERT/new_checkpoints/trained_with_5m/BIOSCAN-5M-BEST_k4_6_6_w1_m0_r0.pt
model_output_dir: ${project_root_path}/ckpt/bioscan_clip

inference_and_eval_setting:
Expand Down Expand Up @@ -65,3 +67,8 @@ general_fine_tune_setting:
hf_repo_id: bioscan-ml/clibd

default_seed: 42

barcodebert_setting:
old_model_setting:
k: 5
max_len: 660
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
batch_size: 200
epochs: 30
wandb_project_name: BIOSCAN-CLIP_softmax_issue
using_train_seen_for_pre_train: true
dataset: bioscan_1m

image:
input_type: image
model: vit
dna:
input_type: sequence
model: barcode_bert
language:
input_type: sequence
model: bert_small

model_output_name: image_dna_text_4gpu_softmax_issue
evaluation_period: 1
ckpt_path: ${project_root_path}/ckpt/bioscan_clip/image_dna_text_4gpu_softmax_issue/best.pth
output_dim: 768
port: 29531

disable_lora: true
lr_scheduler: one_cycle
lr_config:
lr: 1e-6
max_lr: 5e-5


all_gather: true
loss_setup:
gather_with_grad: true
use_horovod: false
local_loss: false
fix_temperature: false
amp: true

random_seed: false

eval_skip_epoch: 23

default_seed: 42

fine_tuning_set:
batch_size: 150
epochs: 15
fine_tune_model_output_dir: ${model_output_dir}/${model_config.model_output_name}/supervise_fine_tune_ckpt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
batch_size: 500
epochs: 30
epochs: 50
labels_for_driven_positive_and_negative_pairs:
wandb_project_name: BIOSCAN-CLIP
using_train_seen_for_pre_train: true
dataset: bioscan_1m

image:
input_type: image
model: lora_vit
model: vit
dna:
input_type: sequence
model: lora_barcode_bert
model: barcode_bert

model_output_name: image_dna_4gpu
evaluation_period: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
batch_size: 500
epochs: 30
epochs: 50
labels_for_driven_positive_and_negative_pairs:
wandb_project_name: BIOSCAN-CLIP
using_train_seen_for_pre_train: true
dataset: bioscan_1m

image:
input_type: image
model: lora_vit
model: vit
dna:
input_type: sequence
model: lora_barcode_bert
model: barcode_bert
language:
input_type: sequence
model: lora_bert
model: bert_small

load_ckpt: false
model_output_name: no_align_baseline
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
batch_size: 500
epochs: 30
epochs: 50
wandb_project_name: BIOSCAN-CLIP
using_train_seen_for_pre_train: true
dataset: bioscan_1m
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
batch_size: 500
epochs: 30
epochs: 50
labels_for_driven_positive_and_negative_pairs:
wandb_project_name: BIOSCAN-CLIP
using_train_seen_for_pre_train: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
batch_size: 500
epochs: 30
epochs: 50
labels_for_driven_positive_and_negative_pairs:
wandb_project_name: BIOSCAN-CLIP
using_train_seen_for_pre_train: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
batch_size: 500
epochs: 30
labels_for_driven_positive_and_negative_pairs:
epochs: 50
wandb_project_name: BIOSCAN-CLIP
using_train_seen_for_pre_train: true
dataset: bioscan_1m

image:
input_type: image
model: lora_vit
model: vit
language:
input_type: sequence
model: lora_bert
model: bert_small

model_output_name: image_text_4gpu
evaluation_period: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
batch_size: 300
batch_size: 200
epochs: 15
labels_for_driven_positive_and_negative_pairs:
wandb_project_name: BIOSCAN-CLIP-5M
Expand All @@ -16,7 +16,7 @@ language:
pre_train_model: prajjwal1/bert-small

model_output_name: image_dna_text_4gpu
evaluation_period: 5
evaluation_period: 3

output_dim: 768
port: 29531
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
batch_size: 200
epochs: 10
epochs: 15
labels_for_driven_positive_and_negative_pairs:
wandb_project_name: BIOSCAN-CLIP-5M
using_train_seen_for_pre_train: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
batch_size: 200
epochs: 10
epochs: 15
labels_for_driven_positive_and_negative_pairs:
wandb_project_name: BIOSCAN-CLIP-5M
using_train_seen_for_pre_train: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ dataset: bioscan_5m

image:
input_type: image
pre_train_model: vit_base_patch16_224
model: vit
dna:
input_type: sequence
pre_train_model: barcode_bert
model: barcode_bert

model_output_name: image_dna_4gpu
evaluation_period: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ dataset: bioscan_5m

image:
input_type: image
pre_train_model: vit_base_patch16_224
model: vit
dna:
input_type: sequence
pre_train_model: barcode_bert
model: barcode_bert
language:
input_type: sequence
pre_train_model: prajjwal1/bert-small
model: bert_small

model_output_name: image_dna_text_4gpu
evaluation_period: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ dataset: bioscan_5m

image:
input_type: image
pre_train_model: vit_base_patch16_224
model: vit
language:
input_type: sequence
pre_train_model: prajjwal1/bert-small
model: bert_small

model_output_name: image_text_4gpu
evaluation_period: 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ dataset: bioscan_5m

image:
input_type: image
pre_train_model: vit_base_patch16_224
model: vit_base_patch16_224
dna:
input_type: sequence
pre_train_model: barcode_bert
model: barcode_bert
language:
input_type: sequence
pre_train_model: prajjwal1/bert-small
model: prajjwal1/bert-small

model_output_name: image_dna_text_4gpu-testing_tokenizer
evaluation_period: 1
Expand Down
2 changes: 2 additions & 0 deletions bioscanclip/epoch/fine_tuning_epoch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import torch
import numpy as np

# TODO: these functions either need to be updated or removed.

def label_batch_to_species_idx(label_batch, unique_species_for_seen):
species_list = label_batch['species']
target = torch.tensor([unique_species_for_seen.index(species) for species in species_list])
Expand Down
27 changes: 8 additions & 19 deletions bioscanclip/epoch/inference_epoch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import torch.nn.functional as F
import torch
from transformers import AutoTokenizer

import matplotlib as plt
import seaborn as sns
from sklearn.metrics import confusion_matrix

def convert_label_dict_to_list_of_dict(label_batch):
order = label_batch['order']
Expand Down Expand Up @@ -51,7 +53,6 @@ def get_feature_and_label(dataloader, model, device, for_open_clip=False, multi_
label_list = []
file_name_list =[]

tokenizer = AutoTokenizer.from_pretrained("bioscan-ml/BarcodeBERT", trust_remote_code=True) # Load tokenizer
pbar = tqdm(enumerate(dataloader), total=len(dataloader))
model.eval()
with torch.no_grad():
Expand All @@ -60,28 +61,16 @@ def get_feature_and_label(dataloader, model, device, for_open_clip=False, multi_
processid_batch, image_input_batch, dna_input_batch, input_ids, token_type_ids, attention_mask, label_batch = batch

if for_open_clip:
language_input = input_ids
else:
language_input = {'input_ids': input_ids.to(device), 'token_type_ids': token_type_ids.to(device),
'attention_mask': attention_mask.to(device)}

if isinstance(dna_input_batch, torch.Tensor):
dna_input_batch = dna_input_batch.to(device)
language_input_batch = input_ids
else:
# Tokenizing DNA sequences
tokenized_dna_sequences = []
for dna_seq in dna_input_batch:
tokenized_output = tokenizer(dna_seq, padding='max_length', truncation=True, max_length=133, return_tensors="pt")
input_seq = tokenized_output["input_ids"]
tokenized_dna_sequences.append(input_seq)
# Convert DNA tokenized sequences into tensors
dna_input_batch = torch.stack(tokenized_dna_sequences).squeeze(1).to(device)
language_input_batch = {'input_ids': input_ids.to(device), 'token_type_ids': token_type_ids.to(device),
'attention_mask': attention_mask.to(device)}

# Forward pass through model
image_output, dna_output, language_output, logit_scale, logit_bias = model(
image_input_batch.to(device),
dna_input_batch, # Passing tokenized DNA sequences
language_input
dna_input_batch,
language_input_batch
)

# Normalizing and storing outputs
Expand Down
13 changes: 2 additions & 11 deletions bioscanclip/epoch/train_epoch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def train_epoch(activate_wandb, total_epochs, epoch, dataloader, model, optimize
pbar = enumerate(dataloader)
epoch_loss = 0.0
total_step = len(dataloader)
tokenizer = AutoTokenizer.from_pretrained("bioscan-ml/BarcodeBERT", trust_remote_code=True)
model.train()
stop_flag = False
for step, batch in pbar:
Expand All @@ -27,17 +26,8 @@ def train_epoch(activate_wandb, total_epochs, epoch, dataloader, model, optimize
'attention_mask': attention_mask.to(device)}
optimizer.zero_grad()
image_input_batch = image_input_batch.to(device)
dna_input_batch = dna_input_batch

if isinstance(dna_input_batch, torch.Tensor):
dna_input_batch = dna_input_batch.to(device)
# if dna_input_batch is not a tensor, tokenize it
else:
tokenized_dna_sequences = []
for dna_seq in dna_input_batch:
tokenized_output = tokenizer(dna_seq, padding='max_length', truncation=True, max_length=133, return_tensors="pt")
input_seq = tokenized_output["input_ids"]
tokenized_dna_sequences.append(input_seq)
dna_input_batch = torch.stack(tokenized_dna_sequences).squeeze(1).to(device)

if enable_autocast:
with torch.autocast(device_type='cuda', dtype=torch.bfloat16):
Expand Down Expand Up @@ -78,4 +68,5 @@ def train_epoch(activate_wandb, total_epochs, epoch, dataloader, model, optimize

if activate_wandb:
wandb.log({"loss": loss.item(), "step": step + epoch * len(dataloader), "learning_rate": current_lr})

print(f'Epoch [{epoch}/{total_epochs}], Loss: {epoch_loss / len(dataloader)}')
Loading