cnulab / RealNet

Offical implementation of "RealNet: A Feature Selection Network with Realistic Synthetic Anomaly for Anomaly Detection (CVPR 2024)"
MIT License
232 stars 14 forks source link

异常图像生成 #44

Open 122y0nx opened 4 months ago

122y0nx commented 4 months ago

您好,异常图像是怎么生成的呢?我运行一遍代码生成的结果是mvtec数据集自带的text图像,没有生成新的。具体那个py文件是生成异常的呢?

cnulab commented 4 months ago

你好!

$ python -m torch.distributed.launch --nproc_per_node=1  sample.py --dataset MVTec-AD

这个命令是生成异常图像的,你按照主页引导执行到这一步即可,包括下载数据集、下载扩散模型checkpoint等.

122y0nx commented 4 months ago

您好,感谢回答,请问生成后的图像保存在哪个文件夹下呢 我想把mvtec中的数据集项不需要的删除,在训练diffusion model的时候出现这个错误AssertionError: ./data/MVTec-AD/mvtec/screw/train/good/232.png ERROR:torch.distributed.elastic.multiprocessing.api:failed (exitcode: 1) local_rank: 0 (pid: 219111) of binary: /root/miniconda3/bin/python 怎么修改呢

cnulab commented 4 months ago
  1. 生成的异常图像默认在 data/MVTec-AD/sdas 下。
  2. 把完整的异常信息截图发出来看一下
122y0nx commented 4 months ago

![Uploading 微信图片_20240710174340.png…]()

122y0nx commented 4 months ago

就是我想把mvtec里的类型删除几个,删除完现在运行不了$ python -m torch.distributed.launch --nproc_per_node=4 train_diffusion.py --dataset MVTec-AD 怎么办,我应该调整哪里

cnulab commented 4 months ago
  1. 首先修改utils/categories.py,把Categories和CategoriesDict全改了。
  2. 修改experiments/MVTec-AD/diffusion.yaml和experiments/MVTec-AD/classifier.yaml中的class_number,class_number是你修改以后总的类别个数。
122y0nx commented 4 months ago

非常感谢您的解答!但是我修改后还是会出现AssertionError: ./data/MVTec-AD/mvtec/capsule/train/good/098.png错误,我重新把删除的数据集上传后会出现KeyError: 'capsule'这样的错误,还是运行不了麻烦您再次解答一下,感谢

cnulab commented 4 months ago

这个AssertionError是在哪里产生的?哪个py的哪一行?

122y0nx commented 4 months ago

[2024-07-10 18:52:22,066][train_diffusion.py][line: 124][ INFO] train_loader len is 1210 Traceback (most recent call last): File "train_diffusion.py", line 311, in main() File "train_diffusion.py", line 158, in main train_one_epoch( File "train_diffusion.py", line 206, in train_one_epoch for i, input in enumerate(train_loader): File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 521, in next data = self._next_data() File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 561, in _next_data data = self._dataset_fetcher.fetch(index) # may raise StopIteration File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch data = [self.dataset[idx] for idx in possibly_batched_index] File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in data = [self.dataset[idx] for idx in possibly_batched_index] File "/root/autodl-tmp/RealNet-main/datasets/sdas_dataset.py", line 104, in getitem input['class_id']= self.class_name_dict[input['clsname']]

cnulab commented 4 months ago

data/MVTec-AD/samples/train_uni.json 这个文件也需要修改,把里面你删掉的类别项全都去掉

122y0nx commented 4 months ago

非常抱歉,我以按照您说的在data/MVTec-AD/samples/train_uni.json 进行修改,但是依旧是这样的错误AssertionError: ./data/MVTec-AD/mvtec/screw/train/good/232.png,

[2024-07-11 09:13:29,860][train_diffusion.py][line: 124][ INFO] train_loader len is 1210 Traceback (most recent call last): File "train_diffusion.py", line 311, in main() File "train_diffusion.py", line 158, in main train_one_epoch( File "train_diffusion.py", line 206, in train_one_epoch for i, input in enumerate(train_loader): File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 521, in next data = self._next_data() File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/dataloader.py", line 561, in _next_data data = self._dataset_fetcher.fetch(index) # may raise StopIteration File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch data = [self.dataset[idx] for idx in possibly_batched_index] File "/root/miniconda3/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py", line 44, in data = [self.dataset[idx] for idx in possibly_batched_index] File "/root/autodl-tmp/RealNet-main/datasets/sdas_dataset.py", line 90, in getitem image = self.image_reader(meta["filename"]) File "/root/autodl-tmp/RealNet-main/datasets/image_reader.py", line 17, in call assert os.path.exists(filename), filename AssertionError: ./data/MVTec-AD/mvtec/screw/train/good/232.png ERROR:torch.distributed.elastic.multiprocessing.api:failed (exitcode: 1) local_rank: 0 (pid: 2363) of binary: /root/miniconda3/bin/python 为什么呢?感谢解答

cnulab commented 4 months ago

这个错误是文件./data/MVTec-AD/mvtec/screw/train/good/232.png没有找到,你检查下该路径下是否有图像。

122y0nx commented 4 months ago

您好我想问一下,我生成了sdas文件,但是我最终的结果还是依据mvtec数据集中的text的异常图片,是我的步骤错误了吗,还是什么原因

cnulab commented 4 months ago

训练的时候既要用到生成的sdas,也要用到原始的正常图像,mvtec中的test会在模型的评估中用到。

122y0nx commented 4 months ago

就是模型评估的结果里只有text文件夹里的图片是吗,没有我生成的异常照片进行异常定位的结果。

122y0nx commented 4 months ago

AssertionError: ./data/MVTec-AD/mvtec/screw/train/good/232.png 您好我重新开了一个服务器全部重新上传文件,上传的数据集只有我需要的,其他文件内容按照您说的修改后还是出现这个错误

cnulab commented 4 months ago

这个问题就是路径 ./data/MVTec-AD/mvtec/screw/train/good/232.png 上的文件没有找到,你确认一下该路径下真的有文件吗?

122y0nx commented 4 months ago

我是不需要这个文件没有上传,但是他一直报这个错误

122y0nx commented 4 months ago

您好,麻烦您帮我解答一下!我想只用几个mvtec中的数据集,只上传了那几个数据集文件夹,我现在已经把您上述需要修改的部分都修改了,但是就是跑不通

cnulab commented 4 months ago

你确定在 train_uni.json 中删除了与 screw 相关的全部内容吗?如果删除了就不会出现在数据加载中啊。

122y0nx commented 4 months ago

非常感谢,我没有看清楚您的回答,谢谢,现在可以运行了

122y0nx commented 3 months ago

您好我想问一下生成的异常图像只是保存在sdas文件夹下的吗?有没有类似mvtec每一类图形的每一类异常的生成。有点不太明白,sdas的文件只是用于训练模型吗,最终验证模型性能只是使用官方mvtec文件里的一场图片吗?

122y0nx commented 3 months ago

SIA合成的局部异常图像的结果是在哪保存啊