Problem during openVINO inference
Dear Anomalib Team,
I am using Anomalib to detect anomalies in my dataset, which contains maps of displacements. I have followed the getting_started file from the notebooks section and encountered an issue during the inference phase using OpenVINO.
Here is a brief overview of my process:
Dataset and DataModule: I used the Folder class to create the datamodule with the following code:
datamodule = Folder(
name="abejas",
root=Path.cwd() / "mapas/datos_noviembre",
normal_dir="normales",
abnormal_dir="anomalas_rural",
task=TaskType.CLASSIFICATION,
)
datamodule.prepare_data() # Create train/val/test/predict dataloaders
datamodule.setup() # Split the data into train/val/test/prediction sets.
Model Creation: I created a basic Padim model:
model_padim_final = Padim()
Training the Model: I used the following code to train the model:
engine = Engine(task=TaskType.CLASSIFICATION)
engine.fit(model=model_padim_final, datamodule=datamodule)
The training process completed successfully.
Testing the Model: The testing phase also yielded good results:
test_padim = engine.test(
model=model_padim_final,
datamodule=datamodule,
ckpt_path=engine.trainer.checkpoint_callback.best_model_path,
)
Results:
Copiar código ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┃ Test metric ┃ DataLoader 0 ┃ ┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩ │ image_AUROC │ 0.9957906007766724 │ │ image_F1Score │ 0.9931600689888 │ └───────────────────────────┴──────────────────────────┘
Exporting the Model to OpenVINO: I used the following code to export the model to OpenVINO:
engine.export(model=model_padim_final, export_type=ExportType.OPENVINO)
output_path = Path(engine.trainer.default_root_dir)
openvino_model_path = output_path / "weights" / "openvino" / "model.bin"
metadata = output_path / "weights" / "openvino" / "metadata.json"
inferencer = OpenVINOInferencer(
path=openvino_model_path, # Path to the OpenVINO IR model.
metadata=metadata, # Path to the metadata file.
device="CPU", # Run on an Intel CPU.
)
Inference Issue: When I use the inferencer to predict a new image from the test data, the prediction always indicates a 100% probability of the image being abnormal. I used the following code for inference:
i, data = next(enumerate(datamodule.test_dataloader()))
images_test = data['image']
image_numpy = images_test[0].numpy()
predictions = inferencer.predict(image=image_numpy)
print(predictions.pred_score, predictions.pred_label)`
Results of Prediction:
1.0 LabelName.ABNORMAL
Dataset Verification: I have verified the number of images in each class to ensure the Folder class accesses the folders correctly:
Number of normal images: 2327 Number of abnormal images: 727
Image Shape Verification: I have checked the shape of the images at various stages:
Before using the Folder class: (768, 1366, 4) In the Folder class: torch.Size([3, 768, 1366]) In the test datamodule: torch.Size([3, 768, 1366]) Transposed for OpenVINO inference: (1366, 768, 3)
Installation: I installed Anomalib from source using the following commands in a new conda environment:
!git clone https://github.com/openvinotoolkit/anomalib.git
%cd anomalib
%pip install .
%anomalib install -v
Despite achieving good results during testing with various models (Dfm, PathCore, Padim), the inference using OpenVINO consistently fails, indicating the problem is likely not with the model itself. I have tested with many images and used various methods for loading the images (from the Folder datamodule and directly from my original folder), but the issue persists.
I dont know if the problem is related with the dataset or even if the model is overfiiting.
I have attached a file with an example from my dataset in case the problem is related to the images.
I would appreciate any guidance or suggestions you could provide to resolve the inference issue with OpenVINO.
Thank you for your assistance.
Best regards, Alex
I got this problem too. I checked the read_image() function and I found that they divide the image by 255. After I divided the image(in np.array format) by 255, this problem was solved,
I got this problem too. I checked the read_image() function and I found that they divide the image by 255. After I divided the image(in np.array format) by 255, this problem was solved,
@him192021 Hi, could you please elaborate on exactly what steps you take to correctly infer an image?
@haimat
something like:
image = Image.open(path).convert("RGB")
image=np.array(image) / 255.0
In my case, the result was fine when using anomalib.data.utils.read_image. However, the result was always 100% defective when I use cv2/PIL to read the image. Then I printed the heat map and found that the image becomes strange. It seems that the OpenVINOInferencer.predict expect the input between 0 and 1.
@him192021 Thanks for your response!
Thanks for the replies @him192021 and @haimat.
In my case it still doesn't work despite rescaling the images between [0,1] so that the inference with OPENVINO works correctly.
In this image you can see how I am reading the images (normal and anomalous) with the function read_image of anomalib.data.utils.
When doing inference of the normal image, it still comes out abnormal 100% in spite of having images scaled between [0,1].
It is probably a problem with my data as they are quite complex to analyse by neural networks.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
This issue was closed because it has been stalled for 14 days with no activity.