amazon-science / patchcore-inspection

Apache License 2.0
719 stars 146 forks source link

How to save segmentation images #15

Closed kodai2003 closed 2 years ago

kodai2003 commented 2 years ago

Thank you for sharing your code and your great work. As I am trying to reproduce your work, I have a question regarding saving segmentation images.

Whenever I try to save segmentation images by setting "--save_segmentation_images", I get the below error. (Without this setting, it runs fine)

By the way, I am only using MVTec 'bottle' for my dataset. Can you help with this error?


Traceback (most recent call last):
File "bin/run_patchcore.py", line 435, in main() File "../patchcore/lib/python3.8/site-packages/click/core.py", line 1130, in call return self.main(args, kwargs) File "../patchcore/lib/python3.8/site-packages/click/core.py", line 1055, in main rv = self.invoke(ctx) File "../patchcore/lib/python3.8/site-packages/click/core.py", line 1689, in invoke return _process_result(rv) File "../patchcore/lib/python3.8/site-packages/click/core.py", line 1626, in _process_result value = ctx.invoke(self._result_callback, value, ctx.params) File "../patchcore/lib/python3.8/site-packages/click/core.py", line 760, in invoke return __callback(args, **kwargs) File "bin/run_patchcore.py", line 166, in run patchcore.utils.plot_segmentation_images( File "../patchcore-inspection/src/patchcore/utils.py", line 51, in plot_segmentation_images image = image_transform(image) File "bin/run_patchcore.py", line 149, in image_transform dataloaders["testing"].dataset.transform_std AttributeError: 'MVTecDataset' object has no attribute 'transform_std'

tshh commented 2 years ago

i did this in run_patchcore.py line 149:
def image_transform(image): image = dataloaders["testing"].dataset.transform_img(image) return np.array(image).astype(np.uint8)

it runs and got segmentation images output.

kodai2003 commented 2 years ago

i did this in run_patchcore.py line 149: def image_transform(image): image = dataloaders["testing"].dataset.transform_img(image) return np.array(image).astype(np.uint8)

it runs and got segmentation images output.

bottle_test_broken_large_011

@tshh Thanks for help! With your suggestion, I get this image. However, this seems to be a bit different from the paper and does not show segmentation maps for anomalies. Any other suggestion on how I go about doing this?

tshh commented 2 years ago

turn out the image must have transform_std and transform_mean to have right pixel value. these two value locate at patchcore-inspection/src/patchcore/datasets/mvtec.py add these two value to run_patchcore.py line 149:

def image_transform(image): dataloaders["testing"].dataset.transform_std = [0.229, 0.224, 0.225] dataloaders["testing"].dataset.transform_mean = [0.485, 0.456, 0.406] in_std = np.array( dataloaders["testing"].dataset.transform_std ).reshape(-1, 1, 1) in_mean = np.array( dataloaders["testing"].dataset.transform_mean ).reshape(-1, 1, 1) image = dataloaders["testing"].dataset.transform_img(image) return np.clip( (image.numpy() in_std + in_mean) 255, 0, 255 ).astype(np.uint8)

kodai2003 commented 2 years ago

turn out the image must have transform_std and transform_mean to have right pixel value. these two value locate at patchcore-inspection/src/patchcore/datasets/mvtec.py add these two value to run_patchcore.py line 149:

def image_transform(image): dataloaders["testing"].dataset.transform_std = [0.229, 0.224, 0.225] dataloaders["testing"].dataset.transform_mean = [0.485, 0.456, 0.406] in_std = np.array( dataloaders["testing"].dataset.transform_std ).reshape(-1, 1, 1) in_mean = np.array( dataloaders["testing"].dataset.transform_mean ).reshape(-1, 1, 1) image = dataloaders["testing"].dataset.transform_img(image) return np.clip( (image.numpy() in_std + in_mean) 255, 0, 255 ).astype(np.uint8)

@tshh That does the job. Appreciated! Closing the issue.

hyfine commented 1 year ago

Add two member variables to MVTecDataset(patchcore-inspection/src/patchcore/datasets/mvtec.py) self.transform_std = IMAGENET_STD self.transform_mean = IMAGENET_MEAN

wyweather commented 7 months ago

After making these changes, can the run_patchcore.py process directly generate a predictive graph?? Or to run load_and_evaluate_patchcore?

wyweather commented 7 months ago

Is there a specific operational process for obtaining the predicted graph?