Skip to content

Commit

Permalink
Update QformerMoE & DataProcess 1123
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzi25 committed Nov 23, 2023
1 parent 4e054b6 commit 4b98969
Show file tree
Hide file tree
Showing 86 changed files with 9,102 additions and 225 deletions.
1 change: 1 addition & 0 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def gradio_answer(chatbot, chat_state, img_list, num_beams, temperature):
max_new_tokens=300,
max_length=2000)[0]
chatbot[-1][1] = llm_message
print(llm_message)
return chatbot, chat_state, img_list


Expand Down
92 changes: 92 additions & 0 deletions evaluate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
Copyright (c) 2022, salesforce.com, inc.
All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
"""

import argparse
import random

import numpy as np
import torch
import torch.backends.cudnn as cudnn

import minigpt4.tasks as tasks
from minigpt4.common.config import Config
from minigpt4.common.dist_utils import get_rank, init_distributed_mode
from minigpt4.common.logger import setup_logger
from minigpt4.common.optims import (
LinearWarmupCosineLRScheduler,
LinearWarmupStepLRScheduler,
)
from minigpt4.common.utils import now

# imports modules for registration
from minigpt4.datasets.builders import *
from minigpt4.models import *
from minigpt4.processors import *
from minigpt4.runners.runner_base import RunnerBase
from minigpt4.tasks import *


def parse_args():
parser = argparse.ArgumentParser(description="Training")

parser.add_argument("--cfg-path", required=True, help="path to configuration file.")
parser.add_argument(
"--options",
nargs="+",
help="override some settings in the used config, the key-value pair "
"in xxx=yyy format will be merged into config file (deprecate), "
"change to --cfg-options instead.",
)

args = parser.parse_args()
# if 'LOCAL_RANK' not in os.environ:
# os.environ['LOCAL_RANK'] = str(args.local_rank)

return args


def setup_seeds(config):
seed = config.run_cfg.seed + get_rank()

random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)

cudnn.benchmark = False
cudnn.deterministic = True


def main():
# allow auto-dl completes on main process without timeout when using NCCL backend.
# os.environ["NCCL_BLOCKING_WAIT"] = "1"

# set before init_distributed_mode() to ensure the same job_id shared across all ranks.
job_id = now()

cfg = Config(parse_args())

init_distributed_mode(cfg.run_cfg)

setup_seeds(cfg)

# set after init_distributed_mode() to only log on master.
setup_logger()

cfg.pretty_print()

task = tasks.setup_task(cfg)
datasets = task.build_datasets(cfg)
model = task.build_model(cfg)

runner = RunnerBase(
cfg=cfg, job_id=job_id, task=task, model=model, datasets=datasets
)
runner.evaluate(skip_reload=True)


if __name__ == "__main__":
main()
30 changes: 29 additions & 1 deletion minigpt4/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, args):
runner_config = self.build_runner_config(config)
model_config = self.build_model_config(config, **user_config)
dataset_config = self.build_dataset_config(config)
evaluation_dataset_config = self.build_evaluation_dataset_config(config)

# Validate the user-provided runner configuration
# model and dataset configuration are supposed to be validated by the respective classes
Expand All @@ -37,7 +38,7 @@ def __init__(self, args):

# Override the default configuration with user options.
self.config = OmegaConf.merge(
runner_config, model_config, dataset_config, user_config
runner_config, model_config, dataset_config, evaluation_dataset_config, user_config
)

def _validate_runner_config(self, runner_config):
Expand Down Expand Up @@ -111,6 +112,29 @@ def build_dataset_config(config):

return dataset_config

@staticmethod
def build_evaluation_dataset_config(config):
# from Minigpt-v2
datasets = config.get("evaluation_datasets", None)
# if datasets is None:
# raise KeyError(
# "Expecting 'datasets' as the root key for dataset configuration."
# )

dataset_config = OmegaConf.create()

if datasets is not None:
for dataset_name in datasets:
builder_cls = registry.get_builder_class(dataset_name)

# hierarchy override, customized config > default config
dataset_config = OmegaConf.merge(
dataset_config,
{"evaluation_datasets": {dataset_name: config["evaluation_datasets"][dataset_name]}},
)

return dataset_config

def _convert_to_dot_list(self, opts):
if opts is None:
opts = []
Expand All @@ -136,6 +160,10 @@ def run_cfg(self):
def datasets_cfg(self):
return self.config.datasets

@property
def evaluation_datasets_cfg(self):
return self.config.evaluation_datasets

@property
def model_cfg(self):
return self.config.model
Expand Down
76 changes: 76 additions & 0 deletions minigpt4/common/eval_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import argparse
import numpy as np
from nltk.translate.bleu_score import sentence_bleu

from minigpt4.common.registry import registry
from minigpt4.common.config import Config

# imports modules for registration
from minigpt4.datasets.builders import *
from minigpt4.models import *
from minigpt4.processors import *
from minigpt4.runners import *
from minigpt4.tasks import *



def eval_parser():
parser = argparse.ArgumentParser(description="Demo")
parser.add_argument("--cfg-path", required=True, help="path to configuration file.")
parser.add_argument("--name", type=str, default='A2', help="evaluation name")
parser.add_argument("--ckpt", type=str, help="path to configuration file.")
parser.add_argument("--eval_opt", type=str, default='all', help="path to configuration file.")
parser.add_argument("--max_new_tokens", type=int, default=10, help="max number of generated tokens")
parser.add_argument("--batch_size", type=int, default=32)
parser.add_argument("--lora_r", type=int, default=64, help="lora rank of the model")
parser.add_argument("--lora_alpha", type=int, default=16, help="lora alpha")
parser.add_argument(
"--options",
nargs="+",
help="override some settings in the used config, the key-value pair "
"in xxx=yyy format will be merged into config file (deprecate), "
"change to --cfg-options instead.",
)
return parser


def prepare_texts(texts, conv_temp):
convs = [conv_temp.copy() for _ in range(len(texts))]
[conv.append_message(
conv.roles[0], '<Img><ImageHere></Img> {}'.format(text)) for conv, text in zip(convs, texts)]
[conv.append_message(conv.roles[1], None) for conv in convs]
texts = [conv.get_prompt() for conv in convs]
return texts


def init_model(args):
print('Initialization Model')
cfg = Config(args)
# cfg.model_cfg.ckpt = args.ckpt
# cfg.model_cfg.lora_r = args.lora_r
# cfg.model_cfg.lora_alpha = args.lora_alpha

model_config = cfg.model_cfg
model_cls = registry.get_model_class(model_config.arch)
model = model_cls.from_config(model_config).to('cuda:0')

# import pudb; pudb.set_trace()
key = list(cfg.datasets_cfg.keys())[0]
vis_processor_cfg = cfg.datasets_cfg.get(key).vis_processor.train
vis_processor = registry.get_processor_class(vis_processor_cfg.name).from_config(vis_processor_cfg)
print('Initialization Finished')
return model, vis_processor

def computeIoU(bbox1, bbox2):
x1, y1, x2, y2 = bbox1
x3, y3, x4, y4 = bbox2
intersection_x1 = max(x1, x3)
intersection_y1 = max(y1, y3)
intersection_x2 = min(x2, x4)
intersection_y2 = min(y2, y4)
intersection_area = max(0, intersection_x2 - intersection_x1 + 1) * max(0, intersection_y2 - intersection_y1 + 1)
bbox1_area = (x2 - x1 + 1) * (y2 - y1 + 1)
bbox2_area = (x4 - x3 + 1) * (y4 - y3 + 1)
union_area = bbox1_area + bbox2_area - intersection_area
iou = intersection_area / union_area
return iou
10 changes: 8 additions & 2 deletions minigpt4/common/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
Copyright (c) 2022, salesforce.com, inc.
All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
For full license text, see the LICENSE_Lavis file in the repo root or https://opensource.org/licenses/BSD-3-Clause
For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
"""

import datetime
import logging
import time
from collections import defaultdict, deque
from torch.utils.tensorboard import SummaryWriter

import torch
import torch.distributed as dist
Expand Down Expand Up @@ -80,16 +81,21 @@ def __str__(self):


class MetricLogger(object):
def __init__(self, delimiter="\t"):
def __init__(self, delimiter="\t",writer: SummaryWriter=None):
self.meters = defaultdict(SmoothedValue)
self.delimiter = delimiter
self.writer = writer

def update(self, **kwargs):
for k, v in kwargs.items():
if isinstance(v, torch.Tensor):
v = v.item()
assert isinstance(v, (float, int))
self.meters[k].update(v)

def update_writer(self, it):
for name, meter in self.meters.items():
self.writer.add_scalar(name, meter, )

def __getattr__(self, attr):
if attr in self.meters:
Expand Down
8 changes: 8 additions & 0 deletions minigpt4/common/vqa_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""
Copyright (c) 2022, salesforce.com, inc.
All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
"""

__author__ = "aagrawal"
Loading

0 comments on commit 4b98969

Please sign in to comment.