Shortcuts

Optional Extras

Organize your transforms in transforms.py

If you have a lot of default transforms, it can be useful to put them all in a transforms.py file, to be referenced in your InputTransform. Here’s an example from image/classification/transforms.py which creates some default transforms given the desired image size:

def default_transforms(image_size: Tuple[int, int]) -> Dict[str, Callable]:
    """The default transforms for image classification: resize the image, convert the image and target to a tensor,
    collate the batch, and apply normalization."""
    if _KORNIA_AVAILABLE and os.getenv("FLASH_TESTING", "0") != "1":
        #  Better approach as all transforms are applied on tensor directly
        per_sample_transform = T.Compose(
            [
                ApplyToKeys(
                    DataKeys.INPUT,
                    T.Compose([T.ToTensor(), K.geometry.Resize(image_size)]),
                ),
                ApplyToKeys(DataKeys.TARGET, torch.as_tensor),
            ]
        )
        per_batch_transform_on_device = ApplyToKeys(
            DataKeys.INPUT,
            K.augmentation.Normalize(torch.tensor([0.485, 0.456, 0.406]), torch.tensor([0.229, 0.224, 0.225])),
        )
        return dict(
            per_sample_transform=per_sample_transform,
            collate=kornia_collate,
            per_batch_transform_on_device=per_batch_transform_on_device,
        )
    return dict(
        per_sample_transform=T.Compose(
            [
                ApplyToKeys(
                    DataKeys.INPUT,
                    T.Compose(
                        [T.ToTensor(), T.Resize(image_size), T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])]
                    ),
                ),
                ApplyToKeys(DataKeys.TARGET, torch.as_tensor),
            ]
        ),
        collate=kornia_collate,
    )

Here’s how we create our transforms in the ImageClassificationInputTransform:

    def default_transforms(self) -> Optional[Dict[str, Callable]]:
        return default_transforms(self.image_size)

Add outputs to your Task

We recommend that you do most of the heavy lifting in the OutputTransform. Specifically, it should include any formatting and transforms that should always be applied to the predictions. If you want to support different use cases that require different prediction formats, you should add some Output implementations in an output.py file.

Some good examples are in flash/core/classification.py. Here’s the ClassesOutput Output:

class ClassesOutput(PredsClassificationOutput):
    """A :class:`.Output` which applies an argmax to the model outputs (either logits or probabilities) and
    converts to a list.

    Args:
        multi_label: If true, treats outputs as multi label logits.
        threshold: The threshold to use for multi_label classification.
    """

    def __init__(self, multi_label: bool = False, threshold: float = 0.5):
        super().__init__(multi_label)

        self.threshold = threshold

    def transform(self, sample: Any) -> Union[int, List[int]]:
        sample = super().transform(sample)
        if self.multi_label:
            one_hot = (sample.sigmoid() > self.threshold).int().tolist()
            result = []
            for index, value in enumerate(one_hot):
                if value == 1:
                    result.append(index)
            return result
        return torch.argmax(sample, -1).tolist()

Alternatively, here’s the LogitsOutput Output:

class LogitsOutput(PredsClassificationOutput):
    """A :class:`.Output` which simply converts the model outputs (assumed to be logits) to a list."""

    def transform(self, sample: Any) -> Any:
        return super().transform(sample).tolist()

Take a look at Predictions (inference) to learn more.


Once you’ve added any optional extras, it’s time to create some examples showing your task in action!

Read the Docs v: latest
Versions
latest
stable
0.5.2
0.5.1
0.5.0
0.4.0
0.3.2
0.3.1
0.3.0
0.2.3
0.2.2
0.2.1
0.2.0
0.1.0post1
Downloads
html
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.