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

Apply data augmentation on MVTec #542

Closed
astewzxc opened this issue Sep 6, 2022 · 4 comments
Closed

Apply data augmentation on MVTec #542

astewzxc opened this issue Sep 6, 2022 · 4 comments
Assignees

Comments

@astewzxc
Copy link

astewzxc commented Sep 6, 2022

I tried to apply transforms on MVTec dataset according to Add transform_config to the main config.yaml file.
But I got an error:
RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[32, 224, 224, 3] to have 3 channels, but got 224 channels instead

I'm not sure if I done right. I created a transform pipeline using albumentations serialization augmentation pipeline, saved it as 'transform.json' and put the file into anomalib fold.
Then change the 'null' at transform_config of config.yaml to 'transform.json'

I've tried patchcore and cflow, neither worked
I assume that the channels of the images changed, but not sure why this happened and how to fix it.

@djdameln
Copy link
Contributor

djdameln commented Sep 6, 2022

Anomalib models expect their inputs to be of shape [Batch x Channel x Height x Weight]. It seems that one of your transforms has changed the order of the dimensions of the input tensor. Would you mind sharing your transform.json so we could have a closer look?

@djdameln djdameln self-assigned this Sep 6, 2022
@astewzxc
Copy link
Author

astewzxc commented Sep 6, 2022

{"__version__": "1.2.1", "transform": {"__class_fullname__": "Compose", "p": 1.0, "transforms": [{"__class_fullname__": "RGBShift", "always_apply": false, "p": 0.5, "r_shift_limit": [-20, 20], "g_shift_limit": [-20, 20], "b_shift_limit": [-20, 20]}], "bbox_params": null, "keypoint_params": null, "additional_targets": {}}}

These are all contents of the file. I applied only RGBShift, nothing else.

@djdameln
Copy link
Contributor

djdameln commented Sep 6, 2022

I see. When using a custom transform configuration, the transforms specified in the .json file replace the default transforms that are used when no custom configuration is passed. These default transforms consist of Resize (resizing to specified image size), Normalize (normalizing to ImageNet stats), and ToTensorV2 (converting the inputs to a tensor with the appropriate dimension ordering). The ToTensorV2 transform is always required, because it prepares the images to be properly handled by the first layer of the PyTorch model.

Looking at it now, I feel our design regarding custom transform configurations is probably not very intuitive and should be improved, or at least better documented. Ideally Anomalib should automatically append mandatory transforms such as ToTensorV2 when these are not present in the user provided .json file. For now however, it is necessary to always include the ToTensorV2 transform in your transforms json.

In your case, your problem would be solved by adding both the ToFloat and the ToTensorV2 transforms to your .json file. The ToFloat transform is needed to make sure that the elements of the input tensor are of the right data type. By default this is accounted for by the Normalize transform. Please try the following config to which I've added the ToFloat and ToTensorV2 transforms, and let us know if it addresses your issue:

{
  "__version__": "1.2.1",
  "transform": {
    "__class_fullname__": "Compose",
    "p": 1.0,
    "transforms": [
      {
        "__class_fullname__": "RGBShift",
        "always_apply": false,
        "p": 0.5,
        "r_shift_limit": [
          -20,
          20
        ],
        "g_shift_limit": [
          -20,
          20
        ],
        "b_shift_limit": [
          -20,
          20
        ]
      },
      {
        "__class_fullname__": "ToFloat",
        "always_apply": true,
        "p": 1.0,
        "max_value": null
      },
      {
        "__class_fullname__": "ToTensorV2",
        "always_apply": true,
        "p": 1.0,
        "transpose_mask": false
      }
    ],
    "bbox_params": null,
    "keypoint_params": null,
    "additional_targets": {}
  }
}

@astewzxc
Copy link
Author

astewzxc commented Sep 8, 2022

It works, thanks for the help!

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