一、介绍
AnomalyGPT 是第一个基于大型视觉语言模型 (LVLM) 的工业异常检测 (IAD) 方法,无需手动指定阈值即可检测工业图像中的异常。现有的 IAD 方法只能提供异常分数,需要手动设置阈值,而现有的 LVLM 无法检测图像中的异常。AnomalyGPT 不仅可以指示异常的存在和位置,还可以提供有关图像的信息
二、环境配置
(1)安装python环境
打开git bash 克隆项目到本地
git clone https://github.com/CASIA-IVA-Lab/AnomalyGPT.git
如果需要安装到指定位置,例如D盘,不需要先创建本地文件夹
git clone https://github.com/CASIA-IVA-Lab/AnomalyGPT.git D:/AnomalyGPT
之后打开Anaconda Prompt创建环境
conda create --prefix=D:/yourEnvFile/AnormalyGPT python=3.10
安装好之后进行激活
activate D:/yourEnvFile/AnormalyGPT
之后找到我们git下来的那个AnomalyGPT文件夹中的requirements.txt

将torch的三个包打注释,由于我们安装的cuda版本不一样,所以我们应该根据自己的CUDA的版本去安装Pytorch,还有deepseed安装的时候也报错,也先把deepseed注释掉,
之后在anaconda prompt中输入
pip install -r D:\AnomalyGPT\requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
之后安装deepspeed(0.3.16)
pip install deepspeed==0.3.16 -i https://pypi.tuna.tsinghua.edu.cn/simple
根据自己安装的cuda版本来装torch,打开cmd输入
nvcc -V

之后我需要安装的是cuda12.1版本,再去Pytorch官网找pip指令(清华镜像源安装总是安装cpu版本,我也不知道这个是为啥,同时提醒一下,这里pytorch版本不要太高,要不然会和deepspeed0.3.16冲突),找到了在anaconda prompt输入
pip install torch==2.5.0 torchvision==0.20.0 torchaudio==2.5.0 --index-url https://download.pytorch.org/whl/cu121
到这里,环境就配置完成了
(2)准备ImageBind检查点
可以从https://dl.fbaipublicfiles.com/imagebind/imagebind_huge.pth下载pth文件,然后放在D:AnomalyGPT\pretrained_ckpt\imagebind_ckpt(也是./pretrained_ckpt/imagebind_ckpt文件夹中)

(3)准备Vicuna检查点
根据这篇大佬的文章的3.2合并模型,大佬将模型放在了下面的百度网盘上。
通过网盘分享的文件:LLaMA
链接: https://pan.baidu.com/s/1syklVFou4r252PxcCaZY7w 提取码: 5ffx 。只下载7B和tokenizer.model,然后把model放在7B文件夹。
下载7B的文件和tokenizer.model

之后转换成huggingface的格式
首先安装protobuf
pip install protobuf==3.20 -i https://pypi.tuna.tsinghua.edu.cn/simple
转换模型,可以参考官方文档https://huggingface.co/docs/transformers/main/model_doc/llama

在运行下面代码之后,发现有找不到文件,因此把刚刚下载的.model文件也复制一份导llama文件下

这里src是当前虚拟环境,因此进行修改
python D:/AnomalyGPT/Lib/site-packages/transformers/models/llama/convert_llama_weights_to_hf.py --input_dir LLaMA --model_size 7B --output_dir LLaMA/7Bhuggingface
等待安装好了之后

接下来使用git指令
git clone https://huggingface.co/lmsys/vicuna-7b-delta-v0 F:/liyi/AnomalyGPT/vicuna-7b-delta
用服务器git的时候报了443,后面还是手动下载了

下载完放到创建一个名为vicuna-7b-delta文件夹,并将下载文件放入到这个文件夹中

之后合并两个的权重,但是需要再安装一个fschat
pip install fschat==0.1.10 -i https://pypi.tuna.tsinghua.edu.cn/simple
下载好后,合并模型
python -m fastchat.model.apply_delta --base LLaMA/7Bhuggingface --target pretrained_ckpt/vicuna_ckpt/7b_v0 --delta LLaMA/vicuna-7b-delta
(4)准备 AnomalyGPT 的 Delta 权重
GitHub作者使用 PandaGPT 的预训练参数来初始化模型。可以在下表中获得使用不同策略训练的 PandaGPT 的权重。在我们的实验和在线演示中,我们使用 Vicuna-7B, openllmplayground/pandagpt_7b_max_len_1024 并且由于计算资源的限制。如果切换到 Vicuna-13B,预期会有更好的结果。
| 基本语言模型 | 最大序列长度 | Huggingface Delta Weights 地址 |
| Vicuna-7B (version 0) | 512 | openllmplayground/pandagpt_7b_max_len_512 |
| Vicuna-7B (version 0) | 1024 | openllmplayground/pandagpt_7b_max_len_1024 |
| Vicuna-13B (version 0) | 256 | openllmplayground/pandagpt_13b_max_len_256 |
| Vicuna-13B (version 0) | 400 | openllmplayground/pandagpt_13b_max_len_400 |
我选择的是7B序列长度为1024的权重
请将下载的 7B delta weights 文件 (pytorch_model.pt) 放在 AnomalyGPT/pretrained_pretrained_ckpt/pandagpt_ckpt/7b文件夹中,这里,7b的文件夹要自己创建

之后,您可以从下表下载 AnomalyGPT 权重
| Setup and Datasets | Weights Address |
| Unsupervised on MVTec-AD | AnomalyGPT/train_mvtec 异常GPT/train_mvtec |
| Unsupervised on VisA | AnomalyGPT/train_visa 异常GPT/train_visa |
| Supervised on MVTec-AD, VisA, MVTec-LOCO-AD and CrackForest | AnomalyGPT/train_supervised异常GPT/train_supervised |
下载之后,把这三个权重放在AnomalyGPT/code/ckpt目录下(ckpt要先创建,再创建三个对应名字的文件夹)

(5)部署demo
cd D:/Anomaly/code
python web_demo.py

后续有时间再微调一个,根据GitHub网址介绍再说,今天的摸鱼到此为止
参考资料:
详细部署AnomalyGPT工业缺陷检测大模型(Ubuntu)-优快云博客[2308.15366] AnomalyGPT: Detecting Industrial Anomalies Using Large Vision-Language Models详细部署AnomalyGPT工业缺陷检测大模型(Ubuntu)-优快云博客
3356





