Spaces:
Running
on
Zero
Running
on
Zero
File size: 17,710 Bytes
d59f323 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
from mmengine.hooks import (CheckpointHook, DistSamplerSeedHook, IterTimerHook,
LoggerHook, ParamSchedulerHook)
from mmengine.optim import AmpOptimWrapper, CosineAnnealingLR, LinearLR
from torch.optim import AdamW
from transformers import AutoTokenizer
from xtuner.dataset import ConcatDataset
from xtuner.dataset.samplers import LengthGroupedSampler
from xtuner.engine.hooks import DatasetInfoHook
from xtuner.engine.runner import TrainLoop
from xtuner.utils import PROMPT_TEMPLATE
from xtuner.dataset.map_fns import template_map_fn_factory
from third_parts.mmdet.models.losses import DiceLoss, CrossEntropyLoss
from peft import LoraConfig
from projects.llava_sam2.models.internvl import InternVL_Slowfast
from projects.llava_sam2.models import VideoLLaVASAMModel, SAM2TrainRunner, VideoLLaVASAMModel_zero3
from projects.llava_sam2.datasets import VideoReVOSDataset, VideoMeVISDataset, VideoRefYoutubeVOSDataset, video_lisa_collate_fn, VideoSAM2Dataset
from projects.llava_sam2.datasets import VideoChatUniViDataset
from projects.llava_sam2.datasets import RefCOCOgGCGDataset, OpenPsgGCGDataset, FlickrGCGDataset, GranDfGCGDataset, OspreyDataset, OspreyDescriptionDataset, OspreyShortDescriptionDataset
from projects.llava_sam2.datasets import LLaVADataset
from projects.llava_sam2.datasets import ReferSegmDataset
from projects.llava_sam2.models.preprocess.image_resize import DirectResize
#######################################################################
# PART 1 Settings #
#######################################################################
# Model
path = './pretrained/InternVL2_5-4B'
pretrained_pth = None
# Data
prompt_template = PROMPT_TEMPLATE.phi3_chat
max_length = 8192
# Scheduler & Optimizer
batch_size = 2 # per_device
accumulative_counts = 4
dataloader_num_workers = 4
max_epochs = 1
optim_type = AdamW
# official 1024 -> 4e-5
# lr = 1e-6
lr = 4e-5
betas = (0.9, 0.999)
weight_decay = 0.05
max_norm = 1 # grad clip
warmup_ratio = 0.05
# Save
save_steps = 1000
save_total_limit = 2 # Maximum checkpoints to keep (-1 means unlimited)
special_tokens = ['[SEG]', '<p>', '</p>', '<vp>', '</vp>']
tokenizer = dict(
type=AutoTokenizer.from_pretrained,
pretrained_model_name_or_path=path,
trust_remote_code=True,
padding_side='right')
extra_image_processor = dict(
type=DirectResize,
target_length=1024,
)
#######################################################################
# PART 2 Model & Tokenizer & Image Processor #
#######################################################################
model = dict(
type=VideoLLaVASAMModel_zero3,
special_tokens=special_tokens,
frozen_sam2_decoder=False,
mllm=dict(
type=InternVL_Slowfast,
model_path=path,
freeze_llm=True,
freeze_visual_encoder=True,
llm_lora=dict(
type=LoraConfig,
r=128,
lora_alpha=256,
lora_dropout=0.05,
bias='none',
task_type='CAUSAL_LM'),
special_tokens=special_tokens,
),
tokenizer=tokenizer,
grounding_encoder=dict(
type=SAM2TrainRunner,
),
loss_mask=dict(
type=CrossEntropyLoss,
use_sigmoid=True,
reduction='mean',
loss_weight=2.0),
loss_dice=dict(
type=DiceLoss,
use_sigmoid=True,
activate=True,
reduction='mean',
naive_dice=True,
eps=1.0,
loss_weight=0.5),
pretrained_pth=pretrained_pth,
loss_sample_points=True,
# loss_sample_points=False,
bs=batch_size,
)
#######################################################################
# PART 3 Dataset & Dataloader #
#######################################################################
VIDEO_DATAS = './data/video_datas/'
IMG_DATAS = './data/image_datas/'
############### video res
data_root_revos = './data/video_datas/revos/'
video_revos_image_folder = data_root_revos
video_revos_expression_file = data_root_revos + 'meta_expressions_train_.json'
video_revos_mask_file = data_root_revos + 'mask_dict.json'
data_root_mevis = './data/video_datas/mevis/train/'
video_mevis_image_folder = data_root_mevis + 'JPEGImages'
video_mevis_expression_file = data_root_mevis + 'meta_expressions.json'
video_mevis_mask_file = data_root_mevis + 'mask_dict.json'
data_root_refytvos = './data/video_datas/rvos/'
video_refytvos_image_folder = data_root_refytvos + 'train/JPEGImages/'
video_refytvos_expression_file = data_root_refytvos + 'meta_expressions/train/meta_expressions.json'
video_refytvos_mask_file = data_root_refytvos + 'mask_dict.pkl'
video_revos_dataset = dict(
type=VideoReVOSDataset,
image_folder=video_revos_image_folder,
expression_file=video_revos_expression_file,
mask_file=video_revos_mask_file,
tokenizer=tokenizer,
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
lazy=True,
repeats=10,
special_tokens=special_tokens,
extra_image_processor=extra_image_processor,
sampled_frames=5,
)
video_mevis_dataset = dict(
type=VideoMeVISDataset,
image_folder=video_mevis_image_folder,
expression_file=video_mevis_expression_file,
mask_file=video_mevis_mask_file,
tokenizer=tokenizer,
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
lazy=True,
repeats=4,
special_tokens=special_tokens,
extra_image_processor=extra_image_processor,
sampled_frames=5,
)
video_refytvos_dataset = dict(
type=VideoRefYoutubeVOSDataset,
image_folder=video_refytvos_image_folder,
expression_file=video_refytvos_expression_file,
mask_file=video_refytvos_mask_file,
tokenizer=tokenizer,
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
lazy=True,
repeats=4,
special_tokens=special_tokens,
extra_image_processor=extra_image_processor,
sampled_frames=5,
)
################### Video chat
data_root_video_chatunivi = VIDEO_DATAS + 'video_vlm/video_chat/'
video_chatunivi_image_folder = data_root_video_chatunivi + 'Activity_Videos/'
video_chatunivi_json_file = data_root_video_chatunivi+ 'video_chat.json'
video_qa_dataset = dict(
type=VideoChatUniViDataset,
image_folder=video_chatunivi_image_folder,
json_file=video_chatunivi_json_file,
tokenizer=tokenizer,
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
lazy=True,
repeats=1,
special_tokens=special_tokens,
extra_image_processor=extra_image_processor,
sampled_frames=5,
)
################## image chat
llava_vqa_dataset = dict(
type=LLaVADataset,
tokenizer=tokenizer,
data_path='data/llava_data/LLaVA-Instruct-150K/llava_v1_5_mix665k.json',
prompt_template=prompt_template,
special_tokens=special_tokens,
image_folder='data/llava_data/llava_images/',
)
################## image res
refcoco_segm_dataset=dict(
type=ReferSegmDataset,
tokenizer=tokenizer,
special_tokens=special_tokens,
extra_image_processor=extra_image_processor,
data_root='data/ref_seg/refcoco',
data_prefix=dict(img_path='coco2014/train2014/'),
ann_file='instances.json',
split_file='refs(unc).p',
prompt_template=prompt_template,
num_classes_per_sample=5,
max_length=max_length,
)
refcoco_plus_segm_dataset=dict(
type=ReferSegmDataset,
tokenizer=tokenizer,
special_tokens=special_tokens,
extra_image_processor=extra_image_processor,
data_root='data/ref_seg/refcoco+',
data_prefix=dict(img_path='coco2014/train2014/'),
ann_file='instances.json',
split_file='refs(unc).p',
prompt_template=prompt_template,
num_classes_per_sample=5,
max_length=max_length,
)
refcocog_segm_dataset=dict(
type=ReferSegmDataset,
tokenizer=tokenizer,
special_tokens=special_tokens,
extra_image_processor=extra_image_processor,
data_root='data/ref_seg/refcocog',
data_prefix=dict(img_path='coco2014/train2014/'),
ann_file='instances.json',
split_file='refs(umd).p',
prompt_template=prompt_template,
num_classes_per_sample=5,
max_length=max_length,
)
# image gcg datas
glamm_data_root = './data/glamm_data/'
refcocog_image_path = glamm_data_root + 'images/coco2014/train2014/'
refcocog_ann_file = glamm_data_root + 'annotations/RefCOCOg_GCG_train.json'
grandf_image_path = glamm_data_root + 'images/grandf/train/'
grandf_ann_file = glamm_data_root + 'annotations/GranDf_HA_GCG_train.json'
flickr_image_path = glamm_data_root + 'images/flickr30k/Flickr30K/'
flickr_ann_file = glamm_data_root + 'annotations/flickr_mergedGT_GCG_train.json'
psg_image_path = glamm_data_root + 'images/coco2017/'
psg_ann_file = glamm_data_root + 'annotations/OpenPsgGCG_train.json'
glamm_refcocog_dataset = dict(
type=RefCOCOgGCGDataset,
image_folder=refcocog_image_path,
data_path=refcocog_ann_file,
tokenizer=tokenizer,
max_length=max_length,
special_tokens=special_tokens,
template_map_fn=dict(type=template_map_fn_factory, template=prompt_template),
extra_image_processor=extra_image_processor,
lazy=True,
repeats=1,
)
glamm_grandf_dataset = dict(
type=GranDfGCGDataset,
data_path=grandf_ann_file,
image_folder=grandf_image_path,
tokenizer=tokenizer,
max_length=max_length,
special_tokens=special_tokens,
template_map_fn=dict(type=template_map_fn_factory, template=prompt_template),
extra_image_processor=extra_image_processor,
lazy=True,
repeats=10,
)
glamm_psg_dataset = dict(
type=OpenPsgGCGDataset,
data_path=psg_ann_file,
image_folder=psg_image_path,
tokenizer=tokenizer,
max_length=max_length,
special_tokens=special_tokens,
template_map_fn=dict(type=template_map_fn_factory, template=prompt_template),
extra_image_processor=extra_image_processor,
lazy=True,
repeats=1,
)
glamm_flickr_dataset = dict(
type=FlickrGCGDataset,
data_path=flickr_ann_file,
image_folder=flickr_image_path,
tokenizer=tokenizer,
max_length=max_length,
special_tokens=special_tokens,
template_map_fn=dict(type=template_map_fn_factory, template=prompt_template),
extra_image_processor=extra_image_processor,
lazy=True,
repeats=1,
)
# sam2 data
data_sam2_folder = VIDEO_DATAS + 'segmentation_datasets/sam_v_full/'
data_sam2_expression_file = './whole_pesudo_cap_v3/sam_v_final_v3.json'
video_sam2_dataset = dict(
type=VideoSAM2Dataset,
sam2_folder=data_sam2_folder,
expression_file=data_sam2_expression_file,
tokenizer=tokenizer,
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
lazy=True,
repeats=4,
special_tokens=special_tokens,
extra_image_processor=extra_image_processor,
sampled_frames=5,
select_number=5,
)
# osprey
data_osprey_file = VIDEO_DATAS + 'osprey-724k/Osprey-724K/osprey_conversation.json'
data_osprey_image_folders = [
IMG_DATAS+ 'coco/train2014/',
IMG_DATAS + 'coco/val2014/',
IMG_DATAS + 'coco/train2017/',
IMG_DATAS + 'coco/val2017/',
]
image_osprey_dataset = dict(
type=OspreyDataset,
image_folder=data_osprey_image_folders,
data_path=data_osprey_file,
tokenizer=tokenizer,
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
lazy=True,
repeats=1,
special_tokens=special_tokens,
)
data_osprey_detail_description_file = VIDEO_DATAS + 'osprey-724k/Osprey-724K/osprey_detail_description.json'
image_osprey_description_dataset = dict(
type=OspreyDescriptionDataset,
image_folder=data_osprey_image_folders,
data_path=data_osprey_detail_description_file,
tokenizer=tokenizer,
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
lazy=True,
repeats=1,
special_tokens=special_tokens,
)
data_osprey_short_file = VIDEO_DATAS + 'osprey-724k/Osprey-724K/osprey_short_form.json'
image_osprey_short_dataset = dict(
type=OspreyShortDescriptionDataset,
image_folder=data_osprey_image_folders,
data_path=data_osprey_short_file,
tokenizer=tokenizer,
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
lazy=True,
repeats=1,
special_tokens=special_tokens,
)
data_osprey_part_file = VIDEO_DATAS + 'osprey-724k/Osprey-724K/osprey_part_level.json'
image_osprey_part_dataset = dict(
type=OspreyDataset,
image_folder=data_osprey_image_folders,
data_path=data_osprey_part_file,
tokenizer=tokenizer,
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
lazy=True,
repeats=1,
special_tokens=special_tokens,
)
data_osprey_positive_neg_file = VIDEO_DATAS + 'osprey-724k/Osprey-724K/osprey_lvis_positive_negative.json'
image_osprey_positive_neg_dataset = dict(
type=OspreyDataset,
image_folder=data_osprey_image_folders,
data_path=data_osprey_positive_neg_file,
tokenizer=tokenizer,
template_map_fn=dict(
type=template_map_fn_factory, template=prompt_template),
max_length=max_length,
lazy=True,
repeats=1,
special_tokens=special_tokens,
)
train_dataset = dict(
type=ConcatDataset, datasets=[
# sem seg
# semantic_seg_ade20k_dataset,
# ref seg
refcoco_segm_dataset, refcoco_plus_segm_dataset, refcocog_segm_dataset,
refcoco_segm_dataset, refcoco_plus_segm_dataset, refcocog_segm_dataset,
refcoco_segm_dataset, refcoco_plus_segm_dataset, refcocog_segm_dataset,
refcoco_segm_dataset, refcoco_plus_segm_dataset, refcocog_segm_dataset,
# image qa
llava_vqa_dataset,
# video res
video_mevis_dataset, video_revos_dataset, video_refytvos_dataset,
# video chat
video_qa_dataset,
# sam2 pesudo
video_sam2_dataset,
# gcg data
glamm_psg_dataset,
glamm_grandf_dataset,
glamm_flickr_dataset,
glamm_refcocog_dataset,
# visual prompt
image_osprey_dataset, image_osprey_description_dataset,
image_osprey_part_dataset, image_osprey_short_dataset,
image_osprey_positive_neg_dataset,
]
)
train_dataloader = dict(
batch_size=batch_size,
num_workers=dataloader_num_workers,
dataset=train_dataset,
sampler=dict(
type=LengthGroupedSampler,
length_property='modality_length',
per_device_batch_size=batch_size * accumulative_counts),
collate_fn=dict(type=video_lisa_collate_fn)
)
#######################################################################
# PART 4 Scheduler & Optimizer #
#######################################################################
# optimizer
optim_wrapper = dict(
type=AmpOptimWrapper,
optimizer=dict(
type=optim_type, lr=lr, betas=betas, weight_decay=weight_decay),
clip_grad=dict(max_norm=max_norm, error_if_nonfinite=False),
accumulative_counts=accumulative_counts,
loss_scale='dynamic',
dtype='bfloat16'
)
# learning policy
# More information: https://github.com/open-mmlab/mmengine/blob/main/docs/en/tutorials/param_scheduler.md # noqa: E501
param_scheduler = [
dict(
type=LinearLR,
start_factor=1e-5,
by_epoch=True,
begin=0,
end=warmup_ratio * max_epochs,
convert_to_iter_based=True),
dict(
type=CosineAnnealingLR,
eta_min=0.0,
by_epoch=True,
begin=warmup_ratio * max_epochs,
end=max_epochs,
convert_to_iter_based=True)
]
# train, val, test setting
train_cfg = dict(type=TrainLoop, max_epochs=max_epochs)
#######################################################################
# PART 5 Runtime #
#######################################################################
# Log the dialogue periodically during the training process, optional
custom_hooks = [
# dict(type=DatasetInfoHook, tokenizer=tokenizer),
]
# configure default hooks
default_hooks = dict(
# record the time of every iteration.
timer=dict(type=IterTimerHook),
# print log every 10 iterations.
logger=dict(type=LoggerHook, log_metric_by_epoch=False, interval=10),
# enable the parameter scheduler.
param_scheduler=dict(type=ParamSchedulerHook),
# save checkpoint per `save_steps`.
checkpoint=dict(
type=CheckpointHook,
save_optimizer=False,
by_epoch=False,
interval=save_steps,
max_keep_ckpts=save_total_limit),
# set sampler seed in distributed evrionment.
sampler_seed=dict(type=DistSamplerSeedHook),
)
# configure environment
env_cfg = dict(
# whether to enable cudnn benchmark
cudnn_benchmark=False,
# set multi process parameters
mp_cfg=dict(mp_start_method='fork', opencv_num_threads=0),
# set distributed parameters
dist_cfg=dict(backend='nccl'),
)
# set visualizer
visualizer = None
# set log level
log_level = 'INFO'
# load from which checkpoint
load_from = None
# whether to resume training from the loaded checkpoint
resume = False
# Defaults to use random seed and disable `deterministic`
randomness = dict(seed=None, deterministic=False)
# set log processor
log_processor = dict(by_epoch=False)
|