GithubHelp home page GithubHelp logo

patchcore_anomaly_detection's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

patchcore_anomaly_detection's Issues

Hardware

Hi,
How long can be the estimated training time by an NVIDIA® GeForce® RTX 3060 Ti™ 8 ?
With 1000 images
Thank you

heatmap accuracy?

Thank you for sharing this implementation,
I found that the roc_auc_scores are really high,
however, the generated heatmaps are not accurate at all?
are these results really reliable?

ValueError: cannot reshape array of size 1024 into shape (28,28)

When I run the test phase of this project(mvtec/carpet),I got this error:

train.py", line 333, in test_step
anomaly_map = score_patches[:,0].reshape((28,28))
ValueError: cannot reshape array of size 1024 into shape (28,28)
Testing: 0%| | 0/117 [00:09<?, ?it/s]
Any ideas? thank you !

Parameter: coreset_sampling_ratio 0.01 happened error

Hello, I have try to run your code by your sample cmd

python train.py --phase train or test --dataset_path .../mvtec_anomaly_detection --category carpet --project_root_path path/to/save/results --coreset_sampling_ratio 0.01 --n_neighbors 9

But I found coreset_sampling_ratio 0.01 will make error so I try use default parameters 0.001 and this is correctly work.

Error code

Error report is :
Traceback (most recent call last): File "train.py", line 441, in <module> trainer.fit(model) File "/home/neaf-3090/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 458, in fit self._run(model) File "/home/neaf-3090/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 756, in _run self.dispatch() File "/home/neaf-3090/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 797, in dispatch self.accelerator.start_training(self) File "/home/neaf-3090/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pytorch_lightning/accelerators/accelerator.py", line 96, in start_training self.training_type_plugin.start_training(trainer) File "/home/neaf-3090/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pytorch_lightning/plugins/training_type/training_type_plugin.py", line 144, in start_training self._results = trainer.run_stage() File "/home/neaf-3090/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 807, in run_stage return self.run_train() File "/home/neaf-3090/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 869, in run_train self.train_loop.run_training_epoch() File "/home/neaf-3090/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pytorch_lightning/trainer/training_loop.py", line 556, in run_training_epoch self.on_train_epoch_end(epoch_output) File "/home/neaf-3090/.pyenv/versions/3.6.8/lib/python3.6/site-packages/pytorch_lightning/trainer/training_loop.py", line 596, in on_train_epoch_end training_epoch_end_output = model.training_epoch_end(processed_epoch_output) File "train.py", line 345, in training_epoch_end selected_idx = selector.select_batch(model=self.randomprojector, already_selected=[], N=int(total_embeddings.shape[0]*args.coreset_sampling_ratio)) ValueError: invalid literal for int() with base 10: '0.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.0

Look like the problem is happened in line 345:
selected_idx = selector.select_batch(model=self.randomprojector, already_selected=[], N=int(total_embeddings.shape[0]*args.coreset_sampling_ratio))

System and python interpreter

  • Ubuntu 20
  • Python 3.8
  • pip 21.2.4
  • torch 1.9.0
  • torchvision 0.10

Can you tell me why coreset_sampling_ratio set 0.01 will make this bug ?

thx~

Possible error at final anomaly map

hey there,
first of all thanks for the code!

regarding the final anomaly map computed:
shouldn't the finale rescaling of the scores be done to all patch features? and not only to the highest one?
thus creating a different anomaly map for the whole image?

any thoughts?
thanks!

Poor performance

Hi.

Thank you for providing this repository.
So I was trying to reproduce the result obtained in the paper[Cable category]
Left[Input] / Middle[ Prediction] / Right [GT]
image

Note that, I have applied a 0.8 threshold on the anomaly map after the normalization.

anomaly_map_norm[anomaly_map_norm< 0.8] = 0

However, I noticed for the 'Good' samples. the result become weird.

image

Minor argparse problem

I think it's better to specify types of arguments so that argments are cast correctry

from

def get_args():
    parser = argparse.ArgumentParser(description='ANOMALYDETECTION')
    parser.add_argument('--phase', choices=['train','test'], default='train')
    parser.add_argument('--dataset_path', default=r'/home/changwoo/hdd/datasets/mvtec_anomaly_detection') # 'D:\Dataset\mvtec_anomaly_detection')#
    parser.add_argument('--category', default='carpet')
    parser.add_argument('--num_epochs', default=1)
    parser.add_argument('--batch_size', default=32)
    parser.add_argument('--load_size', default=256) # 256
    parser.add_argument('--input_size', default=224)
    parser.add_argument('--coreset_sampling_ratio', default=0.001)
    parser.add_argument('--project_root_path', default=r'/home/changwoo/hdd/project_results/patchcore/test') # 'D:\Project_Train_Results\mvtec_anomaly_detection\210624\test') #
    parser.add_argument('--save_src_code', default=True)
    parser.add_argument('--save_anomaly_map', default=True)
    parser.add_argument('--n_neighbors', type=int, default=9)
    args = parser.parse_args()
    return args

to

def get_args():
    parser = argparse.ArgumentParser(description='ANOMALYDETECTION')
    parser.add_argument('--phase', choices=['train','test'], default='train')
    parser.add_argument('--dataset_path', default=r'/home/changwoo/hdd/datasets/mvtec_anomaly_detection') # 'D:\Dataset\mvtec_anomaly_detection')#
    parser.add_argument('--category', default='carpet')
    parser.add_argument('--num_epochs', type=int, default=1)
    parser.add_argument('--batch_size', type=int, default=32)
    parser.add_argument('--load_size', type=int, default=256) # 256
    parser.add_argument('--input_size', type=int, default=224)
    parser.add_argument('--coreset_sampling_ratio', type=float, default=0.001)
    parser.add_argument('--project_root_path', default=r'/home/changwoo/hdd/project_results/patchcore/test') # 'D:\Project_Train_Results\mvtec_anomaly_detection\210624\test') #
    parser.add_argument('--save_src_code', default=True)
    parser.add_argument('--save_anomaly_map', default=True)
    parser.add_argument('--n_neighbors', type=int, default=9)
    args = parser.parse_args()
    return args

Learning questions from a green beginner

Hello, predecessor hcw-00, I am a new student entering the field of Industrial anomaly detection. I don't know how to study. I have encountered some problems.

  1. When I read papers, I have a lot of concept that I don't understand.
  2. Sometimes I try to rewrite the code, I also find it hard to do.
    Can you recommend some books or videos for me to learn? Or should I just keep reading papers and codes?

train error:ValueError: invalid literal for int() with base 10:

Epoch 0: 100%|█████████████████████████████████████████████████████Epoch 0: 100%|████████████████████████████████████████████████████████████████| 62/62 [00:20<00:00, 2.98it/s, v_num=6]Traceback (most recent call last):
File "train.py", line 440, in
trainer.fit(model)
File "F:\ProgramData\Anaconda3\envs\patchcore\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 696, in fit
self._call_and_handle_interrupt(
File "F:\ProgramData\Anaconda3\envs\patchcore\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 650, in _call_and_handle_interrupt
return trainer_fn(*args, **kwargs)
File "F:\ProgramData\Anaconda3\envs\patchcore\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 737, in _fit_impl
results = self._run(model, ckpt_path=self.ckpt_path)
File "F:\ProgramData\Anaconda3\envs\patchcore\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1168, in _run
results = self._run_stage()
File "F:\ProgramData\Anaconda3\envs\patchcore\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1254, in _run_stage
return self._run_train()
File "F:\ProgramData\Anaconda3\envs\patchcore\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1285, in _run_train
self.fit_loop.run()
File "F:\ProgramData\Anaconda3\envs\patchcore\lib\site-packages\pytorch_lightning\loops\loop.py", line 201, in run
self.on_advance_end()
File "F:\ProgramData\Anaconda3\envs\patchcore\lib\site-packages\pytorch_lightning\loops\fit_loop.py", line 286, in on_advance_end
epoch_end_outputs = self.trainer._call_lightning_module_hook("training_epoch_end", epoch_end_outputs)
File "F:\ProgramData\Anaconda3\envs\patchcore\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1552, in _call_lightning_module_hook
output = fn(*args, **kwargs)
File "train.py", line 354, in training_epoch_end
selected_idx = selector.select_batch(model=self.randomprojector, already_selected=[], N=int(total_embeddings.shape[0]*args.coreset_sampling_ratio))
ValueError: invalid literal for int() with base 10: '0.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.010.0

num_samples should be a positive integer value, but got num_samples=0

I ran the code and got many errors but handled to solve them somehow! here is a problem that I have. I only want to train the model on the bottle dataset and it is already in the dataset address. the original dataset has broken_large, broken_small, contaminated, and good but I do not know how to feed those 4 groups as a train/test dataset.

I also have got an error while training only on good images.
I will appreciate it if you could help me

$ python train.py --phase train --dataset_path C:\\projects\\interview\\mvtec --category bottle --project_root_path C:\projects\interview\PatchCore_anomaly_detection\results --coreset_sampling_ratio 0.01 --n_neighbors 9 C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torchvision\models\detection\anchor_utils.py:63: UserWarning: Failed to initialize NumPy: module compiled against API version 0x10 but this version of numpy is 0xf (Triggered internally at ..\torch\csrc\utils\tensor_numpy.cpp:68.) device: torch.device = torch.device("cpu"), GPU available: False, used: False TPU available: False, using: 0 TPU cores IPU available: False, using: 0 IPUs Using cache found in C:\Users\user/.cache\torch\hub\pytorch_vision_v0.9.0 C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torchvision\models\_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and will be removed in 0.15, please use 'weights' instead. warnings.warn( C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torchvision\models\_utils.py:223: UserWarning: Arguments other than a weight enum or Nonefor 'weights' are deprecated since 0.13 and will be removed in 0.15. The current behavior is equivalent to passingweights=Wide_ResNet50_2_Weights.IMAGENET1K_V1. You can also use weights=Wide_ResNet50_2_Weights.DEFAULTto get the most up-to-date weights. warnings.warn(msg) C:\projects\interview\PatchCore_anomaly_detection_2\train.py:259: DeprecationWarning: ANTIALIAS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.LANCZOS instead. transforms.Resize((args.load_size, args.load_size), Image.ANTIALIAS), C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torchvision\transforms\transforms.py:332: UserWarning: Argument 'interpolation' of type int is deprecated since 0.13 and will be removed in 0.15. Please use InterpolationMode enum. warnings.warn( C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\optimizers.py:37: UserWarning:LightningModule.configure_optimizersreturnedNone`, this fit will run with no optimizer
rank_zero_warn(

| Name | Type | Params

0 | model | ResNet | 68.9 M
1 | criterion | MSELoss | 0
2 | inv_normalize | Normalize | 0

0 Trainable params
68.9 M Non-trainable params
68.9 M Total params
275.533 Total estimated model params size (MB)
Traceback (most recent call last):
File "C:\projects\interview\PatchCore_anomaly_detection_2\train.py", line 437, in
trainer.fit(model)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\trainer.py", line 740, in fit
self._call_and_handle_interrupt(
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\trainer.py", line 685, in _call_and_handle_interrupt
return trainer_fn(*args, **kwargs)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\trainer.py", line 777, in _fit_impl
self._run(model, ckpt_path=ckpt_path)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\trainer.py", line 1199, in _run
self._dispatch()
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\trainer.py", line 1279, in _dispatch
self.training_type_plugin.start_training(self)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\plugins\training_type\training_type_plugin.py", line 202, in start_training
self._results = trainer.run_stage()
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\trainer.py", line 1289, in run_stage
return self._run_train()
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\trainer.py", line 1319, in _run_train
self.fit_loop.run()
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\loops\base.py", line 140, in run
self.on_run_start(*args, **kwargs)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\loops\fit_loop.py", line 197, in on_run_start
self.trainer.reset_train_val_dataloaders(self.trainer.lightning_module)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\data_loading.py", line 595, in reset_train_val_dataloaders
self.reset_train_dataloader(model=model)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\data_loading.py", line 365, in reset_train_dataloader
self.train_dataloader = self.request_dataloader(RunningStage.TRAINING, model=model)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\data_loading.py", line 611, in request_dataloader
dataloader = source.dataloader()
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\connectors\data_connector.py", line 296, in dataloader
return self.instance.trainer.call_hook(self.name, pl_module=self.instance)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pytorch_lightning\trainer\trainer.py", line 1501, in call_hook
output = model_fx(*args, **kwargs)
File "C:\projects\interview\PatchCore_anomaly_detection_2\train.py", line 304, in train_dataloader
train_loader = DataLoader(image_datasets, batch_size=args.batch_size, shuffle=True, num_workers=0) #, pin_memory=True)
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torch\utils\data\dataloader.py", line 347, in init
sampler = RandomSampler(dataset, generator=generator) # type: ignore[arg-type]
File "C:\Users\user\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torch\utils\data\sampler.py", line 107, in init
raise ValueError("num_samples should be a positive integer "
ValueError: num_samples should be a positive integer value, but got num_samples=0
`

Attribute Error while running test

There is an issue while running the test phase with the given code according to the instructions provided. Train phase runs without any issues but test phase runs into error.
python train.py --phase test --dataset_path mvt_data --category capsule --project_root_path ./

Error:

File "train.py", line 442, in <module>
    trainer.test(model)
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 911, in test
    return self._call_and_handle_interrupt(self._test_impl, model, dataloaders, ckpt_path, verbose, datamodule)
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 685, in _call_and_handle_interrupt
    return trainer_fn(*args, **kwargs)
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 954, in _test_impl
    results = self._run(model, ckpt_path=self.tested_ckpt_path)
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 1199, in _run
    self._dispatch()
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 1275, in _dispatch
    self.training_type_plugin.start_evaluating(self)
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/plugins/training_type/training_type_plugin.py", line 206, in start_evaluating
    self._results = trainer.run_stage()
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 1286, in run_stage
    return self._run_evaluate()
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 1334, in _run_evaluate
    eval_loop_results = self._evaluation_loop.run()
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/loops/base.py", line 140, in run
    self.on_run_start(*args, **kwargs)
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/loops/dataloader/evaluation_loop.py", line 96, in on_run_start
    self._on_evaluation_start()
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/loops/dataloader/evaluation_loop.py", line 178, in _on_evaluation_start
    self.trainer.call_hook("on_test_start", *args, **kwargs)
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/pytorch_lightning/trainer/trainer.py", line 1501, in call_hook
    output = model_fx(*args, **kwargs)
  File "train.py", line 329, in on_test_start
    self.index = faiss.read_index(os.path.join(self.embedding_dir_path,'index.faiss'))
  File "/home/shreevijay/anaconda3/envs/patch_unoff/lib/python3.6/site-packages/torch/nn/modules/module.py", line 948, in __getattr__
    type(self).__name__, name))
AttributeError: 'STPM' object has no attribute 'embedding_dir_path'`

using pip install for faiss is not recommanded

I have tried pip version of faiss for my own experiments,
faiss-cpu and faiss-gpu,
and I found my results are unstable,
after searching on google,
facebookresearch/faiss#2273
pip is not a supported way of installing
in my user experience, I found that faiss(pip version) is very unstable,
and also hard to debug, sometimes my program hangs when running faiss,
anomalib provides an alternative way to do nearest neighbor search.

Using layer4

Thanks for this great implementation.
In the paper the authors claim in "Evaluation on other benchmarks":

As the detection context is much closer to
that of natural image data available in ImageNet and images
are larger, we make use of deeper network feature maps at
hierarchy levels 3 and 4, but otherwise do not perform any
hyperparameter tuning for PatchCore.

I tried to simply swap layer2 and 3 to 3 and 4 and had to adapt the reshaping later on.
But the results were a complete failure. The heatmap is always blank.

Has someone tried to use layer4 successfully?

when i tested some images by the code, the usage is getting bigger

when i tested some images by the code, the usage is getting bigger。 I find the problem is caused by the line code(features = self(x)):
def test_step(self, batch, batch_idx): # Nearest Neighbour Search
self.embedding_coreset = pickle.load(open(os.path.join(self.embedding_dir_path, 'embedding.pickle'), 'rb'))
x, gt, label, file_name, x_type = batch
# extract embedding
features = self(x)
embeddings = []
for feature in features:
m = torch.nn.AvgPool2d(3, 1, 1)
embeddings.append(m(feature))
I used torch.no_gard() and torch.cuda.empty_cache(), but it is unuseful. why?

During the test, the GPU usage is large.

Does the GPU occupy a small amount during training and a large amount during testing? Does the distance matrix take up a large number of GPUs?The following functions:

def distance_matrix(x, y=None, p=2): # pairwise distance of vectors
y = x if type(y) == type(None) else y
n = x.size(0)
m = y.size(0)
d = x.size(1)
x = x.unsqueeze(1).expand(n, m, d)
y = y.unsqueeze(0).expand(n, m, d)
dist = torch.pow(x - y, p).sum(2)
return dist

How to apply SparseRandomProjector to large Image dataset?

Hello.

I want to apply this model to large image dataset. (I have over 10,000 images)

But RAM memory issue arise.

https://github.com/hcw-00/PatchCore_anomaly_detection/blob/main/sampling_methods/kcenter_greedy.py#L95

self.features = model.transform(self.X)

I think this code puts all the data embedding into RAM memory and apply SparseRandomProjector, which seems to put a lot of

pressure on RAM memory.(I'm just novice, so this may be wrong.)

Does anyone know how to solve this problem?

One idea i have is to split the data in half and apply the SparseRandomProjector to each of them, but I think it might cause problems

because SparseRandomProjector determines the dimensionality of embeddings based on Johnson-Lindenstrauss lemma.

According to sklearn document(https://scikit-learn.org/stable/modules/generated/sklearn.random_projection.SparseRandomProjection.html), n_components can be automatically adjusted according to the number of samples in the dataset.

wondering if the coreset sampling is only random sampling now

If I understand correctly, the kCenterGreedy.select_batch_ function is merely doing random sampling now. The main reason is that self.already_selected is not updated within the range(N) loop, therefore line 'ind = np.random.choice(np.arange(self.n_obs))' is always executed. To fix it, put self.already_selected = new_batch into the range(N) loop.

Please point it out if I misunderstood anything!

for _ in range(N):
if not self.already_selected:
# Initialize centers with a randomly selected datapoint
ind = np.random.choice(np.arange(self.n_obs))
else:
ind = np.argmax(self.min_distances)
# New examples should not be in already selected since those points
# should have min_distance of zero to a cluster center.
assert ind not in already_selected

  self.update_distances([ind], only_new=True, reset_dist=False)
  new_batch.append(ind)
  print('Maximum distance from cluster centers is %0.2f' % max(self.min_distances))

  self.already_selected = new_batch#already_selected

About Licensing

Hey,

thanks for your awesome work here.

Can you please add a license file, so we can use your code?

Best regards

Trivial use of AdaptiveAvgPool2d

m = torch.nn.AdaptiveAvgPool2d(feature[0].shape[-2:])

AdaptiveAvgPool is applied to each feature map on the same scale due to feature[0].shape[-2:]
This means that nothing happens to the feature maps.

If you want to rescale the second feature maps to the first, I guess you could use features[0].shape[-2:]

Furthermore, even if you would use the suggestion above, you still don't apply any aggregation function f_agg to the first feature maps (as described in the paper). Shouldn't you use AvgPool2d in order to actually aggregate information from nearby patches?

low quality anomaly_map for type "good"

Hi, I'm confusing about the low quality anomaly_map for type "good" and high AUROC scores for both image and pixel level.
How to calculate the AUROC scores from the anomaly_map? Thanks~
Such as examples of "bottle" shown below.
good_014
good_014_amap
good_014_amap_on_img

Error in test mode after faiss implementation

Has anybody tried the code in the test mode after faiss implementation?
I believe that the line below needs to be added to the on_test_start method.

self.embedding_dir_path, self.sample_path, self.source_code_save_path = prep_dirs(self.logger.log_dir)

Otherwise, you'll get the error below:

AttributeError: 'STPM' object has no attribute 'embedding_dir_path'

And that makes sense because the attributes are not assigned yet, instead they are assigned after this point and also in on_train_start.

train speed get more and more slowly

Hi, I am trying to train the model in dataset, however I find that the speed is getting more and more slowly. I noticed that the embedding size is getting bigger, so whether we can change it? Thanks.
image

[QnA] memory usage

Hello, is this patch-core algorithm more improved compared to the calculate mahalnobis distance memory usage used by the existing PaDiM and semiorthogonal? Thank you

how test my own dataset without groundtrouth?

As described in title, the data from real world without GT_mask, but almost every algorithms need gt_mask to get roc_auc_score. Do you try to decide the threshold of anomaly score which higher than the threshold will be thought as anomaly ? Other than obtained the optim threshold from GT ? The problem confiused me all the time.

Memory Explosion

Hi I run the train.py under following env:

  1. Ubuntu 16.04, docker env: pytorch 1.8.1 with GPU support
  2. Machine Memory: 32G, swap area size: 12GB; GPU: GF RTX 2080 Ti 12 GB
  3. Num of epochs:50, batch_size:1, load_size: 256, input_size:224, with other parameters default/unchanged
  4. Trainning dataset consists of 150 non-defect images with 1300*2300 resolution
  5. Test dataset consists of 100 defect and non-defect images, with ground truth images where a pure black for non-defect images and write musk for the defect areas for defect images

Problem: After epoch 11, the machine memory (not GPU memory) takes up to 40GB, and at epoch 15, the usage of machine memory would be over 50GB, then the system would kill the training process.

Any ideas why this happen, and how to fix?

Thanks for your work. But it's a bit strange here in the coreset sampling part

for _ in range(N):
if self.already_selected is None:
# Initialize centers with a randomly selected datapoint
ind = np.random.choice(np.arange(self.n_obs))
else:
ind = np.argmax(self.min_distances)
# New examples should not be in already selected since those points
# should have min_distance of zero to a cluster center.
assert ind not in already_selected
self.update_distances([ind], only_new=True, reset_dist=False)
new_batch.append(ind)
print('Maximum distance from cluster centers is %0.2f'
% max(self.min_distances))
self.already_selected = already_selected

I think the wanted logic here should be initialing a centre point by random choice (the first if-branch) and all the entire centre points are chosen by argmax the distance (the second if-branch).
But for the "self.already_selected" is initialized as an empty list in line 49(not None), so we'll never get into the first if-branch. As for the centre point initializing process, the expression "np.argmax(self.min_distances)" will return 0 for "np.argmax(None)" will return 0.
So, as a result, the program selects index-0-feature as algorithm initialization everytime instead of random selecting one as a common practice.

Confusion matrix show error classifficating all the nominal (no defectives) sample

Hello.

I noticed that the model always says "this is image has a deffect" when testing. You can see a commented part of the code where the autor calculates the confusion matrix.
This is what i get for the category hazelnut

Total pixel-level auc-roc score :
0.8770563697278964
Total image-level auc-roc score :
0.7642857142857142
test_epoch_end
[[ 0 40]
 [ 0 70]]
false positive
['005', '001', '025', '029', '036', '002', '033', '018', '004', '015', '024', '035', '010', '021', '039', '019', '022', '031', '032', '003', '012', '020', '014', '006', '023', '000', '009', '008', '037', '007', '038', '034', '030', '017', '013', '011', '027', '028', '016', '026']

Note that the 40 means that the model inferenced 40 samples as deffected object instead of 'good'.

My args:

parser = argparse.ArgumentParser(description='ANOMALYDETECTION')
    parser.add_argument('--phase', type=str, choices=['train','test'], default='train')
    parser.add_argument('--dataset_path', type=str, default='../../datasets/mvtec_anomaly_detection/') # 'D:\Dataset\mvtec_anomaly_detection')#
    parser.add_argument('--category', type=str, default='hazelnut')
    parser.add_argument('--num_epochs', type=int, default=1)
    parser.add_argument('--batch_size', type=int, default=32)
    parser.add_argument('--load_size', type=int, default=256) # 256
    parser.add_argument('--input_size', type=int, default=224)
    parser.add_argument('--coreset_sampling_ratio', type=float, default=0.005)
    parser.add_argument('--project_root_path', type=str, default='test') # 'D:\Project_Train_Results\mvtec_anomaly_detection\210624\test') #
    parser.add_argument('--save_src_code', type=bool, default=True)
    parser.add_argument('--save_anomaly_map', type=bool, default=True)
    parser.add_argument('--n_neighbors', type=int, default=9)
    args = parser.parse_args()

I am testing with the last version of Pytorch (v11). There is something wrong?

test

Hello, I get an error when I run test. What is the reason?

image

too much time for "corest sampling"

Thank you for your work!!

I found that the training time is very long, especially during the "corest subsampling" step.

When you are training, how long does it take for a category?

image

It's been almost an hour without any progress...

C++ implementation

Not an issue but has anyone ever tried to implement a C++ version of PatchCore ?
If not, what would be the best approach if you have a C++ production environment.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.