Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Padim model returns fixed size (256, 256) and not image size #2532

Closed
1 task done
vmiller987 opened this issue Jan 21, 2025 · 5 comments
Closed
1 task done

Comments

@vmiller987
Copy link

Describe the bug

This is for the main branch.

BUG: My images are supposed to be (2048, 110) in size, but the model is returning predictions as (256, 256). Resizing to (2048, 110) just skews/distorts the image.

I'm trying to adapt usage code from v1.2 to v2 so I can attempt to look into the Export functionality (OpenVino/ONNX).

Due to the bug reported here: #2510
I can't continue with the current v2 beta release.

I saw that it was fixed in #2508 merge request to main.

I have cloned main so I can use this version of the code.

## This section is for prediction ##
transform = transforms.Compose(
    [
        transforms.Resize((2048, 110)),  # (h, w)
    ]
)

predict_dataset = PredictDataset(
    # I took 10 images identical in shape to predict on
    path="/path/to/dataset/shape_2048x110_small/",
    image_size=(2048, 110),
)

model = Padim()

# This shows the shape torch.Size([3, 2048, 110])
for idx in range(3):  # Inspect the first few samples
    image = predict_dataset[idx].image
    print(f"Image {idx} Shape in Dataset: {image.shape}")

ckpt_callback = ModelCheckpoint(
    dirpath="/path/to/checkpoints/",
    filename="bursters",
    monitor="AUROC",
    mode="max",
    save_top_k=1,
)

engine = Engine(
   default_root_dir="/path/to/models/",
    callbacks=[ckpt_callback],
)


predictions = engine.predict(
    dataset=predict_dataset,
    model=model,
    return_predictions=True,
    ckpt_path="/path/to/checkpoints/bursters.ckpt",
)

for prediction in predictions:
    # Extract attributes
    anomaly_map = prediction.anomaly_map.cpu().numpy().squeeze()
    pred_mask = prediction.pred_mask.cpu().numpy().squeeze()
    image_path = prediction.image_path[0]
    image = np.array(Image.open(image_path))
    image_name = Path(image_path).stem

    print(f"anomaly_shape: {anomaly_map.shape}")
    print(f"anomaly_type: {type(anomaly_map)}")
    print(f"image_shape: {image.shape}")
    print(f"image_type: {type(image)}")
anomaly_shape: (256, 256)
anomaly_type: <class 'numpy.ndarray'>
image_shape: (2048, 110)
image_type: <class 'numpy.ndarray'>

In engine.py I have added a print statement to check before the return self.trainer.predict(model, dataloaders, datamodule, return_predictions, ckpt_path) line which shows the images are the correct size at this point.

Shape in DataLoader: torch.Size([1, 3, 2048, 110])

I have also added a check at the start of the forward pass in Padim:

torch.Size([1, 3, 256, 256])

Which shows it's resized before it gets to the model.

Dataset

Other (please specify in the text field below)

Model

PADiM

Steps to reproduce the behavior

I setup my environment using uv as it's much faster than normal pip.

  • uv venv .venv --python 3.11
  • source .venv/bin/activate
  • uv pip install pip
  • git clone https://github.com/openvinotoolkit/anomalib.git
  • cd anomalib/
  • git checkout main
  • uv pip install -e .[full]

OS information

OS information:

  • OS: RHEL 9.5
  • Python version: 3.11
  • Anomalib version: 2 - main branch
  • PyTorch version: 2.5.1
  • CUDA/cuDNN version: 12.6
  • GPU models and configuration: 1x RTX 4090 (haven't tested your mutli-GPU yet)
  • Any other relevant information: I'm using a custom dataset of images around (height: 2048, width: 110). They vary slightly, but are close to this size.
    • Structure:
      • /path/to/dataset/
        • good_all/
        • defect/

Expected behavior

In v1.2, predictions were returned at the correct size.

Screenshots

No response

Pip/GitHub

GitHub

What version/branch did you use?

main

Configuration YAML

None

Logs

None

Code of Conduct

  • I agree to follow this project's Code of Conduct
@vmiller987
Copy link
Author

vmiller987 commented Jan 21, 2025

I've realized it's the additional default PreProcessor() to Padim that is forcing a resize to (256, 256).

However, when I disable the preprocessor I end up with OOM issues during validation that weren't there in v1.2.

@vmiller987
Copy link
Author

In v1.2 Padim's forward pass accepts my (2048, 110) shape.

So something's changed in v2 (main) that causes Padim to OOM on GPU's now unless it's forced down in size.

@vmiller987
Copy link
Author

I'm just going to say it's a skill issue on my part and it's just trying to learn and work with v2 beta pre-documentation.

Look forward to the updates, keep up the good work.

@samet-akcay
Copy link
Contributor

it is likely that when you disable the preprocessor the transforms are not applied, and hence your input is not resized to a smaller inputs, which causes OOM issues.

If you want to read more about preprocessing, you could follow this link
https://anomalib.readthedocs.io/en/v2.0.0-beta.1/markdown/guides/how_to/models/pre_processor.html

@vmiller987
Copy link
Author

it is likely that when you disable the preprocessor the transforms are not applied, and hence your input is not resized to a smaller inputs, which causes OOM issues.

If you want to read more about preprocessing, you could follow this link https://anomalib.readthedocs.io/en/v2.0.0-beta.1/markdown/guides/how_to/models/pre_processor.html

Thank you. I was originally looking through the code base. I probably got mixed up between v1.2 and v2 beta when I was looking for the docs.

I went the lazy route and just cut my dataset in half to avoid the OOM issues. This allows me to finally work on my assigned task for investigating the Anomalib exporter.

I was able to get OpenVINO working, but ONNX isn't exporting/inferring correctly. We originally wanted TorchScript but I'm going to learn OpenVINO and ONNX and push my work in that direction. This is today's problem and not relevant to the above issue though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants