Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

初步实现文生图单路径代码 #48

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

forXuyx
Copy link

@forXuyx forXuyx commented Mar 13, 2025

感谢你杰出的工作,我觉得他完全可以扩展成为一个视觉理解与生成的统一模型(mini-deepseek-janus),这个项目将更有教育与学习的意义,因此我在这里尝试复现deepseek janus的结构!
以下是janus所采用的架构:
1741863331414
可以看到Janus采用了一个双路路径,这也是Janus的创新点,对于生成与理解两个任务,分开编码图像比较好。

想要复现Janus,可以分解为以下几个要素,且目前已实现部分要素:

  • 训练好的text_tokenizer(✅)
  • 训练好的image_tokenizer(LlamaGen预训练提供的vocab_size为16384,与我们的初衷”mini“背道而驰,这里我已经开始着手训练一个新的image_tokenizer,vocab_size为6400,与text_tokenizer相匹配)
  • 联合Dataset构建(image understanding dataset✅ image generation dataset✅ 但是尚未整合)
  • 联合训练模型构建(image understanding✅ image generation✅ 但是尚未整合)
  • 优秀的数据集准备(非常重要)
  • Janus的训练细节(非常重要)

本次为图像理解与生成统一模型的第一次提交,我粗糙地实现了文生图训练过程,流程图如下:
演示文稿1
我新增及修改的文件如下:

  • dataset/get_pretrain_t2i_data.py (该脚本是基于pretrain_data.jsonl获得符合t2idataset的pretrain_t2i_data.jsonl,这里我目前的选择是和语言模型共用一套预训练的文生图的数据)
  • model/dataset.py (新增文生图dataset的实现)
  • model/model_t2i.py (新增文生图模型)
  • model/model_vq.py (新增vqgan模型的代码,该代码直接来源于LlamaGen,用于加载image_tokenizer)
  • model/vq_demo.py (新增vq_demo,是用于测试是否能正确token化image,同样来源于LlamaGen)
  • train_pretrain_t2i.py (新增文生图的预训练代码)

以上是我目前所做的工作,我相信这是一个良好的开端,希望可以加大和您的交流,共同完善mini-janus的复现!

@forXuyx
Copy link
Author

forXuyx commented Mar 13, 2025

有一项疏忽了,由于目前尚未训练好minimind专属的image_tokenizer,在我的代码中使用的是LlamaGen所开源的vq_ds16_t2i.pt,可在LlamaGen的github页面找到,请下载后放置于model/minimind_img_tokenizer/vq_ds16_t2i.pt,然后就可以执行pretrain了

Copy link
Author

@forXuyx forXuyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

昨天实现中有误,具体错误在于我在计算loss时选取了全部的序列,参照llamagen的代码应该是将预测图片token的logits与实际图片的token做交叉熵,现已修改

Copy link
Author

@forXuyx forXuyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在进行文生图预训练时,我发现可训练的参数变少了,但是训练时长反而变长了,考虑到可能是vqgan进行encode时花费的大量时间,我这里采用了llamagen里面所采取到的策略,提前处理图像编码,因此我新增了一个脚本以及对应的生成jsonl文件脚本的逻辑。
此外一个较大的问题是,利用llamagen所提供的训练vqgan的代码,可能需要很长的时间训练(按照repo里面的40epoch),我估计得训上一个星期......此外,他们提供的预训练模型在rFID,SSIM以及PSNR上都非常优秀(可用model/vq_demo.py测试重构效果),所以目前暂时先利用开源的权重实现文生图的一整套流程。

Copy link
Author

@forXuyx forXuyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

训练速度提高了三倍左右,我在后续会打包处理过的数据到百度云

@forXuyx
Copy link
Author

forXuyx commented Mar 15, 2025

我找到了一个不错的预训练数据集,COCO-CN,有20K+优秀的图像文本对,用于文生图的预训练

Copy link
Author

@forXuyx forXuyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我处理了一下COCO-CN数据作为文生图的预训练数据并上传到了百度网盘:
链接: https://pan.baidu.com/s/11ibqcw1eHcrG9oNFjsBtjA 提取码: s9c7
在训练前请先预提取图片的embedding以及token,具体脚本位于:
dataset/get_image_token.py
这个过程大概会持续5分钟,接下来就可以预训练模型了,基础模型在A100-40G上进行训练,大概三分钟左右!

Copy link
Author

@forXuyx forXuyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前可以生成图像了,但是一股异味....我觉得可能的原因有是vqgan的codebook与text_tokenizer的词表大小不适配?
此外我是不是想的复杂了,其实对于janus来说,图像其实就相当于vlm或者是llm中的‘回答’?
不过这样看来,训练一个词表大小一致的vqgan越来越有必要了!
另外我扩充了Flicker30k-CNA到预训练数据集中,目前有20多万的文本对:
https://pan.baidu.com/s/1BvdVYpyWdh8NdGPi1ugcYQ 提取码: vrkx

TODO:

  • 训练一个6400词表大小的vqgan
  • 按照vlm的流程重写一遍文生图试试

@forXuyx forXuyx force-pushed the 新增图像理解与生成统一模型 branch from 678479c to cfac34c Compare March 17, 2025 11:52
Copy link
Author

@forXuyx forXuyx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我修改了训练代码的流程,大致的过程将与vlm保持一致,为后续的联合训练做铺垫,另外我正在基于t2i的数据训练vqgan,计划在一块a100 80g上训练三天,目前的代码的loss降的不是很理想,我用的是没有训练完的vqgan,codabookusage大概在73%,不够优秀

@forXuyx
Copy link
Author

forXuyx commented Mar 20, 2025

生成的图像还是很不理想......目前我也不知道是为啥o(╥﹏╥)o
更新了一下现在的存储链接:
数据:https://pan.baidu.com/s/1SUAx50zl95jCEPiGZMQCXw 提取码: 93pd
image_tokenizer: https://pan.baidu.com/s/1gZ8apSE8M9koLsXC3rUPRw 提取码: 3x7t

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant