super-gradients icon indicating copy to clipboard operation
super-gradients copied to clipboard

AttributeError: 'Grayscale' object has no attribute 'apply_to_sample'AttributeError: 'Grayscale' object has no attribute 'apply_to_sample'

Open hlacikd opened this issue 1 year ago • 1 comments

💡 I need to convert my dataset to grayscale and train yolo-nas using grascale images

I have appended to "standard yolo-nas transforms" {"Grayscale": {"num_output_channels": 1}}, which is according to documentation https://docs.deci.ai/super-gradients/latest/docstring/common/object_names.html#latest.src.super_gradients.common.object_names.Transforms among supported transformes (inherited from torchvision).

Therefore train_transforms and val_transforms looks like this

train_transforms = [
    {"DetectionMosaic": {"input_dim": (640, 640), "prob": 1.0}},
    {
        "DetectionRandomAffine": {
            "degrees": 0.0,
            "translate": 0.1,
            "scales": [0.5, 1.5],
            "shear": 0.0,
            "target_size": (640, 640),
            "filter_box_candidates": False,
            "wh_thr": 2,
            "area_thr": 0.1,
            "ar_thr": 20,
            "border_value": 128,
        }
    },
    {"DetectionHSV": {"prob": 1.0, "hgain": 5, "sgain": 30, "vgain": 30}},
    {"DetectionHorizontalFlip": {"prob": 0.5}},
    {"DetectionPaddedRescale": {"input_dim": (640, 640)}},
    {"Grayscale": {"num_output_channels": 1}},
    {"DetectionStandardize": {"max_value": 255.0}},
    {"DetectionTargetsFormatTransform": {"input_dim": (640, 640), "output_format": "LABEL_CXCYWH"}},
]

val_transforms = [
    {"DetectionPaddedRescale": {"input_dim": (640, 640), "pad_value": 114}},
    {"Grayscale": {"num_output_channels": 1}},
    {"DetectionStandardize": {"max_value": 255.0}},
    {"DetectionTargetsFormatTransform": {"input_dim": (640, 640), "output_format": "LABEL_CXCYWH"}},
]

after aplying them to my dataset and either trying to plot them using plot

train_ds.plot(max_samples_per_plot=12, plot_transformed_data=True)

or running training train I get the following error

AttributeError: 'Grayscale' object has no attribute 'apply_to_sample'

Please help me to solve this .

Versions

No response

hlacikd avatar Mar 11 '24 21:03 hlacikd

While it is true that torchvision transforms are supported, that is not how you use them for Object Detection.

You can:

  1. Re-implement the GrayScale as a [AbstractDetectionTransform](https://docs.deci.ai/super-gradients/latest/docstring/training/transforms.html#latest.src.super_gradients.training.transforms.detection.abstract_detection_transform.AbstractDetectionTransform()
  2. Just use Albumentations ToGray transform (I think this option is preferable and easier), see our Albumentations integration tutorial [here](Using Albumentations with SG

shaydeci avatar Apr 15 '24 09:04 shaydeci