detail adjustment & formatting

pull/45/head
gongjy 2024-11-03 11:39:46 +08:00
parent c115169e88
commit 1fc98152c0
13 changed files with 23 additions and 34 deletions

1
.envrc
View File

@ -1 +0,0 @@
source activate minimind

9
.gitignore vendored
View File

@ -1,9 +0,0 @@
model/clip_model/clip-vit-base-patch32/
model/siglip_model/siglip-vit-base-patch16/
out/*.pth
full.json
trans_json.py
dataset/*
!dataset/eval_images/
!dataset/eval_multi_images/
minimind-v/model/__pycache__/*

View File

@ -79,7 +79,7 @@ if __name__ == "__main__":
# -------------------------- 问题和目录设置 ----------------------------------- # -------------------------- 问题和目录设置 -----------------------------------
if multi: if multi:
image_dir = './dataset/eval_multi_images/bird/' image_dir = './dataset/eval_multi_images/bird/'
prompt = "<image>\n<image>\nName all the differences between these two birds." prompt = f"{lm_config.image_special_token}\n{lm_config.image_special_token}\nName all the differences between these two birds."
else: else:
image_dir = './dataset/eval_images/' image_dir = './dataset/eval_images/'
prompt = lm_config.image_special_token + '\n这个图片描述的是什么内容?' prompt = lm_config.image_special_token + '\n这个图片描述的是什么内容?'

View File

@ -19,7 +19,7 @@ class LMConfig(PretrainedConfig):
dropout: float = 0.0, dropout: float = 0.0,
flash_attn: bool = True, flash_attn: bool = True,
image_special_token: str = '<' * 25 + '>' * 25, image_special_token: str = '<' * 25 + '>' * 25,
image_ids=[30] * 25 + [32] * 25, image_ids: List = [30] * 25 + [32] * 25,
#################################################### ####################################################
# Here are the specific configurations of MOE # Here are the specific configurations of MOE
# When use_moe is false, the following is invalid # When use_moe is false, the following is invalid

View File

@ -326,12 +326,12 @@ class Transformer(PreTrainedModel):
config_class = LMConfig config_class = LMConfig
last_loss: Optional[torch.Tensor] last_loss: Optional[torch.Tensor]
def __init__(self, params: LMConfig = None, vocab_size = 6400): def __init__(self, params: LMConfig = None):
super().__init__(params) super().__init__(params)
if not params: if not params:
params = LMConfig() params = LMConfig()
self.params = params self.params = params
self.vocab_size = vocab_size self.vocab_size = params.vocab_size
self.n_layers = params.n_layers self.n_layers = params.n_layers
# image的特殊占位符,对应每张图切分成M个token,和get_img_process中的数量对应 # image的特殊占位符,对应每张图切分成M个token,和get_img_process中的数量对应
self.image_ids = params.image_ids self.image_ids = params.image_ids
@ -373,10 +373,8 @@ class Transformer(PreTrainedModel):
def find_indices(tokens, image_ids): def find_indices(tokens, image_ids):
image_ids_tensor = torch.tensor(image_ids).to(tokens.device) image_ids_tensor = torch.tensor(image_ids).to(tokens.device)
len_image_ids = len(image_ids) len_image_ids = len(image_ids)
# .generate时,在初始化后直接跳过 # .generate时,在初始化后直接跳过
if len_image_ids > tokens.size(1): if len_image_ids > tokens.size(1):
# print(f"len_image_ids ({len_image_ids}) is greater than sequence length ({tokens.size(1)}), skipping.")
return None return None
# 使用view来创建一个视图,便于处理滑动窗口 # 使用view来创建一个视图,便于处理滑动窗口
@ -451,7 +449,6 @@ class Transformer(PreTrainedModel):
self.last_loss = None self.last_loss = None
self.OUT.__setitem__('logits', logits) self.OUT.__setitem__('logits', logits)
# self.OUT.__setitem__('last_loss', self.last_loss)
self.OUT.__setitem__('loss', self.last_loss) self.OUT.__setitem__('loss', self.last_loss)
return self.OUT return self.OUT

View File

@ -40,7 +40,8 @@ def get_img_embedding(batch_encoding, vision_model):
embeddings.append(output.last_hidden_state) embeddings.append(output.last_hidden_state)
# 从 BatchEncoding 中提取图像张量 # 从 BatchEncoding 中提取图像张量
if isinstance(batch_encoding, transformers.tokenization_utils_base.BatchEncoding) or isinstance(batch_encoding, transformers.feature_extraction_utils.BatchFeature): if (isinstance(batch_encoding, transformers.tokenization_utils_base.BatchEncoding)
or isinstance(batch_encoding, transformers.feature_extraction_utils.BatchFeature)):
image_tensor = batch_encoding['pixel_values'] image_tensor = batch_encoding['pixel_values']
else: else:
image_tensor = batch_encoding # torch.Size([32, 4, 3, 224, 224]) image_tensor = batch_encoding # torch.Size([32, 4, 3, 224, 224])
@ -66,5 +67,6 @@ def get_img_embedding(batch_encoding, vision_model):
hook.remove() hook.remove()
# 拼接所有特征向量成为一个张量 # 拼接所有特征向量成为一个张量
all_embeddings = torch.stack(embeddings, dim=0).squeeze() # torch.Size([32, 4, 50, 768]) or torch.Size([32, 2, 196, 768]) all_embeddings = torch.stack(embeddings, dim=0).squeeze()
# torch.Size([32, 4, 50, 768]) or torch.Size([32, 2, 196, 768])
return all_embeddings return all_embeddings