openvinotoolkit / anomalib

An anomaly detection library comprising state-of-the-art algorithms and features such as experiment management, hyper-parameter optimization, and edge inference.
https://anomalib.readthedocs.io/en/latest/
Apache License 2.0
3.76k stars 668 forks source link

[Bug]: could not find a writer for the specified extension in function 'cv::imwrite_' #1432

Closed archocron closed 1 year ago

archocron commented 1 year ago

Describe the bug

I just follow all the steps to make the environment working and when i start the inference script i get the following problem:

(anomalib_env) C:\python\anomalib>python tools/inference/openvino_inference.py --weights results/padim/mvtec/bottle/run/weights/openvino/model.bin --metadata results/padim/mvtec/bottle/run/weights/openvino/metadata.json --input datasets/MVTec/bottle/test/broken_large/000.png --output results/padim/mvtec/bottle/images Traceback (most recent call last): File "C:\python\anomalib\tools\inference\openvino_inference.py", line 98, in infer(args) File "C:\python\anomalib\tools\inference\openvino_inference.py", line 89, in infer visualizer.save(file_path=file_path, image=output) File "C:\python\anomalib\src\anomalib\post_processing\visualizer.py", line 240, in save cv2.imwrite(str(filepath), image) cv2.error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:696: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'cv::imwrite'

Dataset

MVTec

Model

PADiM

Steps to reproduce the behavior

I have Anaconda and i install anomalib using github instructions for the "local install" and also i have installed all the requeriments. Everything works until i try to make the inference using OpenVINO with the following command:

python tools/inference/openvino_inference.py --weights results/padim/mvtec/bottle/run/weights/openvino/model.bin --metadata results/padim/mvtec/bottle/run/weights/openvino/metadata.json --input datasets/MVTec/bottle/test/broken_large/000.png --output results/padim/mvtec/bottle/images

And I get:

Traceback (most recent call last): File "C:\python\anomalib\tools\inference\openvino_inference.py", line 98, in infer(args) File "C:\python\anomalib\tools\inference\openvino_inference.py", line 89, in infer visualizer.save(file_path=file_path, image=output) File "C:\python\anomalib\src\anomalib\post_processing\visualizer.py", line 240, in save cv2.imwrite(str(filepath), image) cv2.error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:696: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'cv::imwrite'

OS information

OS information:

Expected behavior

I expect to have the inference results for the given image

Screenshots

No response

Pip/GitHub

GitHub

What version/branch did you use?

main

Configuration YAML

dataset:
  name: mvtec
  format: mvtec
  path: ./datasets/MVTec
  category: bottle
  task: segmentation
  train_batch_size: 32
  eval_batch_size: 32
  num_workers: 8
  image_size: 256 # dimensions to which images are resized (mandatory)
  center_crop: null # dimensions to which images are center-cropped after resizing (optional)
  normalization: imagenet # data distribution to which the images will be normalized: [none, imagenet]
  transform_config:
    train: null
    eval: null
  test_split_mode: from_dir # options: [from_dir, synthetic]
  test_split_ratio: 0.2 # fraction of train images held out testing (usage depends on test_split_mode)
  val_split_mode: same_as_test # options: [same_as_test, from_test, synthetic]
  val_split_ratio: 0.5 # fraction of train/test images held out for validation (usage depends on val_split_mode)
  tiling:
    apply: false
    tile_size: null
    stride: null
    remove_border_count: 0
    use_random_tiling: False
    random_tile_count: 16

model:
  name: padim
  backbone: resnet18
  pre_trained: true
  layers:
    - layer1
    - layer2
    - layer3
  normalization_method: min_max # options: [none, min_max, cdf]

metrics:
  image:
    - F1Score
    - AUROC
  pixel:
    - F1Score
    - AUROC
  threshold:
    method: adaptive #options: [adaptive, manual]
    manual_image: null
    manual_pixel: null

visualization:
  show_images: False # show images on the screen
  save_images: True # save images to the file system
  log_images: True # log images to the available loggers (if any)
  image_save_path: null # path to which images will be saved
  mode: full # options: ["full", "simple"]

project:
  seed: 42
  path: ./results

logging:
  logger: [] # options: [comet, tensorboard, wandb, csv] or combinations.
  log_graph: false # Logs the model graph to respective logger.

optimization:
  export_mode: openvino # options: torch, onnx, openvino

# PL Trainer Args. Don't add extra parameter here.
trainer:
  enable_checkpointing: true
  default_root_dir: null
  gradient_clip_val: 0
  gradient_clip_algorithm: norm
  num_nodes: 1
  devices: 1
  enable_progress_bar: true
  overfit_batches: 0.0
  track_grad_norm: -1
  check_val_every_n_epoch: 1 # Don't validate before extracting features.
  fast_dev_run: false
  accumulate_grad_batches: 1
  max_epochs: 1
  min_epochs: null
  max_steps: -1
  min_steps: null
  max_time: null
  limit_train_batches: 1.0
  limit_val_batches: 1.0
  limit_test_batches: 1.0
  limit_predict_batches: 1.0
  val_check_interval: 1.0 # Don't validate before extracting features.
  log_every_n_steps: 50
  accelerator: auto # <"cpu", "gpu", "tpu", "ipu", "hpu", "auto">
  strategy: null
  sync_batchnorm: false
  precision: 32
  enable_model_summary: true
  num_sanity_val_steps: 0
  profiler: null
  benchmark: false
  deterministic: false
  reload_dataloaders_every_n_epochs: 0
  auto_lr_find: false
  replace_sampler_ddp: true
  detect_anomaly: false
  auto_scale_batch_size: false
  plugins: null
  move_metrics_to_cpu: false
  multiple_trainloader_mode: max_size_cycle

Logs

(anomalib_env) C:\python\anomalib>python tools/inference/openvino_inference.py --weights results/padim/mvtec/bottle/run/weights/openvino/model.bin --metadata results/padim/mvtec/bottle/run/weights/openvino/metadata.json --input datasets/MVTec/bottle/test/broken_large/000.png --output results/padim/mvtec/bottle/images
Traceback (most recent call last):
  File "C:\python\anomalib\tools\inference\openvino_inference.py", line 98, in <module>
    infer(args)
  File "C:\python\anomalib\tools\inference\openvino_inference.py", line 89, in infer
    visualizer.save(file_path=file_path, image=output)
  File "C:\python\anomalib\src\anomalib\post_processing\visualizer.py", line 240, in save
    cv2.imwrite(str(file_path), image)
cv2.error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:696: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'cv::imwrite_'

Code of Conduct

archocron commented 1 year ago

Solved, i forgot to put the filename in the output parameter. It should look like this:

(anomalib_env) C:\python\anomalib>python tools/inference/openvino_inference.py --weights results/padim/mvtec/bottle/run/weights/openvino/model.bin --metadata results/padim/mvtec/bottle/run/weights/openvino/metadata.json --input datasets/MVTec/bottle/test/broken_large/000.png --output results/padim/mvtec/bottle/images/IMAGE.PNG

WKUANEI commented 7 months ago

I downloaded the latest version of anomalib and found this error File "E:\aisoftware\Miniconda3\envs\anomalib\lib\site-packages\anomalib\data\utils\image.py", line 456, in save_image cv2.imwrite(str(filepath), image) cv2.error: opencv (4.5.5) D:\Build\ opencv \opencv-4.5.5\modules\imgcodecs\src\loadsave.cpp:730: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'cv::imwrite' I know that there is no suffix in the file name here, I read your answer, you need to add IMAGE.PNG in the output, but which code should I add to it? Do I need to modify the source code? I modified the code file_path = directly in the python file E:\aisoftware\Miniconda3\envs\anomalib\lib\site-packages\anomalib\data\utils\image.py os.path.join(str(file_path), 'IMAGE.PNG') cv2.imwrite(str(file_path), image) gets some output E:\systemD\pythonProject1\anomalib\datasets\MVTec\bottle\test\broken_large\000.png E:\systemD\pythonProject1\anomalib\results\Padim\MVTec\bottle\latest\images E:\systemD\pythonProject1\anomalib\results\Padim\MVTec\bottle\latest\images\IMAGE.PNG E:\systemD\pythonProject1\anomalib\datasets\MVTec\bottle\test\broken_large\001.png E:\systemD\pythonProject1\anomalib\results\Padim\MVTec\bottle\latest\images E:\systemD\pythonProject1\anomalib\results\Padim\MVTec\bottle\latest\images\IMAGE.PNG E:\systemD\pythonProject1\anomalib\datasets\MVTec\bottle\test\broken_large\002.png How should I modify the code correctly if the image cannot be saved in the corresponding directory? Create your own directory? I put os.path.join(str(file_path), 'IMAGE.PNG') on the code that created the directory, and I can run this project, but I don't know if I changed the library source code will affect the future code. Does the code you publish on jupyter need to be modified?Give some advice