class_docstr: The ``ObjectDetectionData`` class is a :class:`~flash.core.data.data_module.DataModule` with a set of
  classmethods for loading data for object detection.
class_docstr_tok: ["The","``","ObjectDetectionData","``","class","is","a",":","class",":","`","~flash.core.data.data_module.DataModule","`","with","a","set","of","classmethods","for","loading","data","for","object","detection","."]
docstr: Creates a :class:`~flash.image.detection.data.ObjectDetectionData` object from the given data folders and
  annotation files in the VIA (`VGG Image Annotator <https://www.robots.ox.ac.uk/~vgg/software/via/>`_) `JSON
  format <https://gitlab.com/vgg/via/-/blob/via-3.x.y/via-3.x.y/CodeDoc.md#structure-of-via-project- json-file>`_.
  
  To learn how to customize the transforms applied for each stage, read our
  :ref:`customizing transforms guide <customizing_transforms>`.
  
  Args:
      labels: A list of class labels. Not that the list should not include a label for the background class which
          will be added automatically as class zero (additional labels will be sorted).
      label_field: The field within ``region_attributes`` which corresponds to the region label.
      train_folder: The folder containing images to use when training.
      train_ann_file: The VIA format annotation file to use when training.
      val_folder: The folder containing images to use when validating.
      val_ann_file: The VIA format annotation file to use when validating.
      test_folder: The folder containing images to use when testing.
      test_ann_file: The VIA format annotation file to use when testing.
      predict_folder: The folder containing images to use when predicting.
      input_cls: The :class:`~flash.core.data.io.input.Input` type to use for loading the data.
      transform: The :class:`~flash.core.data.io.input_transform.InputTransform` type to use.
      transform_kwargs: Dict of keyword arguments to be provided when instantiating the transforms.
      data_module_kwargs: Additional keyword arguments to provide to the
        :class:`~flash.core.data.data_module.DataModule` constructor.
  
  Returns:
      The constructed :class:`~flash.image.detection.data.ObjectDetectionData`.
  
  Examples
  ________
  
  .. testsetup::
  
      >>> import os
      >>> import json
      >>> import numpy as np
      >>> from PIL import Image
      >>> rand_image = Image.fromarray(np.random.randint(0, 255, (64, 64, 3), dtype="uint8"))
      >>> os.makedirs("train_folder", exist_ok=True)
      >>> os.makedirs("predict_folder", exist_ok=True)
      >>> _ = [rand_image.save(os.path.join("train_folder", f"image_{i}.png")) for i in range(1, 4)]
      >>> _ = [rand_image.save(os.path.join("predict_folder", f"predict_image_{i}.png")) for i in range(1, 4)]
      >>> annotations = {
      ...     f"image_{i+1}.png": {
      ...         "filename": f"image_{i+1}.png",
      ...         "regions": [{
      ...                 "shape_attributes": {"name": "rect", "x": 10, "y": 20, "width": 5, "height": 10},
      ...                 "region_attributes": {"label": lb},
      ...         }]
      ...     } for i, lb in enumerate(["cat", "dog", "cat"])
      ... }
      >>> with open("train_annotations.json", "w") as annotation_file:
      ...     json.dump(annotations, annotation_file)
  
  The folder ``train_folder`` has the following contents:
  
  .. code-block::
  
      train_folder
      ├── image_1.png
      ├── image_2.png
      ├── image_3.png
      ...
  
  The file ``train_annotations.json`` contains the following:
  
  .. code-block::
  
      {
          "image_1.png": {
              "filename": "image_1.png",
              "regions": [{
                  "shape_attributes": {"name": "rect", "x": 10, "y": 20, "width": 5, "height": 10},
                  "region_attributes": {"label": "cat"}
              }]
          }, "image_2.png": {
              "filename": "image_2.png",
              "regions": [{
                  "shape_attributes": {"name": "rect", "x": 20, "y": 30, "width": 10, "height": 10},
                  "region_attributes": {"label": "dog"}}
          ]}, "image_3.png": {
              "filename": "image_3.png",
              "regions": [{
                  "shape_attributes": {"name": "rect", "x": 10, "y": 20, "width": 5, "height": 25},
                  "region_attributes": {"label": "cat"}
              }]
          }
      }
  
  .. doctest::
  
      >>> from flash import Trainer
      >>> from flash.image import ObjectDetector, ObjectDetectionData
      >>> datamodule = ObjectDetectionData.from_via(
      ...     ["cat", "dog"],
      ...     train_folder="train_folder",
      ...     train_ann_file="train_annotations.json",
      ...     predict_folder="predict_folder",
      ...     transform_kwargs=dict(image_size=(128, 128)),
      ...     batch_size=2,
      ... )
      >>> datamodule.num_classes
      3
      >>> datamodule.labels
      ['background', 'cat', 'dog']
      >>> model = ObjectDetector(num_classes=datamodule.num_classes)
      >>> trainer = Trainer(fast_dev_run=True)
      >>> trainer.fit(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
      Training...
      >>> trainer.predict(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
      Predicting...
  
  .. testcleanup::
  
      >>> import shutil
      >>> shutil.rmtree("train_folder")
      >>> shutil.rmtree("predict_folder")
      >>> os.remove("train_annotations.json")
docstr_tok: ["Creates","a",":","class",":","`","~flash.image.detection.data.ObjectDetectionData","`","object","from","the","given","data","folders","and","annotation","files","in","the","VIA","(","`","VGG","Image","Annotator","<","https",":","\/\/www.robots.ox.ac.uk\/~vgg\/software\/via\/",">","`","_",")","`","JSON","format","<","https",":","\/\/gitlab.com\/vgg\/via\/-\/blob\/via-3.x.y\/via-3.x.y\/CodeDoc.md","#","structure-of-via-project-","json-file",">","`","_",".","To","learn","how","to","customize","the","transforms","applied","for","each","stage",",","read","our",":","ref",":","`","customizing","transforms","guide","<","customizing_transforms",">","`",".","Args",":","labels",":","A","list","of","class","labels",".","Not","that","the","list","should","not","include","a","label","for","the","background","class","which","will","be","added","automatically","as","class","zero","(","additional","labels","will","be","sorted",")",".","label_field",":","The","field","within","``","region_attributes","``","which","corresponds","to","the","region","label",".","train_folder",":","The","folder","containing","images","to","use","when","training",".","train_ann_file",":","The","VIA","format","annotation","file","to","use","when","training",".","val_folder",":","The","folder","containing","images","to","use","when","validating",".","val_ann_file",":","The","VIA","format","annotation","file","to","use","when","validating",".","test_folder",":","The","folder","containing","images","to","use","when","testing",".","test_ann_file",":","The","VIA","format","annotation","file","to","use","when","testing",".","predict_folder",":","The","folder","containing","images","to","use","when","predicting",".","input_cls",":","The",":","class",":","`","~flash.core.data.io.input.Input","`","type","to","use","for","loading","the","data",".","transform",":","The",":","class",":","`","~flash.core.data.io.input_transform.InputTransform","`","type","to","use",".","transform_kwargs",":","Dict","of","keyword","arguments","to","be","provided","when","instantiating","the","transforms",".","data_module_kwargs",":","Additional","keyword","arguments","to","provide","to","the",":","class",":","`","~flash.core.data.data_module.DataModule","`","constructor",".","Returns",":","The","constructed",":","class",":","`","~flash.image.detection.data.ObjectDetectionData","`",".","Examples","________","..","testsetup",":",":",">",">",">","import","os",">",">",">","import","json",">",">",">","import","numpy","as","np",">",">",">","from","PIL","import","Image",">",">",">","rand_image","=","Image.fromarray","(","np.random.randint","(","0",",","255",",","(","64",",","64",",","3",")",",","dtype=","''","uint8","''",")",")",">",">",">","os.makedirs","(","``","train_folder","''",",","exist_ok=True",")",">",">",">","os.makedirs","(","``","predict_folder","''",",","exist_ok=True",")",">",">",">","_","=","[","rand_image.save","(","os.path.join","(","``","train_folder","''",",","f","''","image_","{","i","}",".png","''",")",")","for","i","in","range","(","1",",","4",")","]",">",">",">","_","=","[","rand_image.save","(","os.path.join","(","``","predict_folder","''",",","f","''","predict_image_","{","i","}",".png","''",")",")","for","i","in","range","(","1",",","4",")","]",">",">",">","annotations","=","{","...","f","''","image_","{","i+1","}",".png","''",":","{","...","``","filename","''",":","f","''","image_","{","i+1","}",".png","''",",","...","``","regions","''",":","[","{","...","``","shape_attributes","''",":","{","``","name","''",":","``","rect","''",",","``","x","''",":","10",",","``","y","''",":","20",",","``","width","''",":","5",",","``","height","''",":","10","}",",","...","``","region_attributes","''",":","{","``","label","''",":","lb","}",",","...","}","]","...","}","for","i",",","lb","in","enumerate","(","[","``","cat","''",",","``","dog","''",",","``","cat","''","]",")","...","}",">",">",">","with","open","(","``","train_annotations.json","''",",","``","w","''",")","as","annotation_file",":","...","json.dump","(","annotations",",","annotation_file",")","The","folder","``","train_folder","``","has","the","following","contents",":","..","code-block",":",":","train_folder","\u251c\u2500\u2500","image_1.png","\u251c\u2500\u2500","image_2.png","\u251c\u2500\u2500","image_3.png","...","The","file","``","train_annotations.json","``","contains","the","following",":","..","code-block",":",":","{","``","image_1.png","''",":","{","``","filename","''",":","``","image_1.png","''",",","``","regions","''",":","[","{","``","shape_attributes","''",":","{","``","name","''",":","``","rect","''",",","``","x","''",":","10",",","``","y","''",":","20",",","``","width","''",":","5",",","``","height","''",":","10","}",",","``","region_attributes","''",":","{","``","label","''",":","``","cat","''","}","}","]","}",",","``","image_2.png","''",":","{","``","filename","''",":","``","image_2.png","''",",","``","regions","''",":","[","{","``","shape_attributes","''",":","{","``","name","''",":","``","rect","''",",","``","x","''",":","20",",","``","y","''",":","30",",","``","width","''",":","10",",","``","height","''",":","10","}",",","``","region_attributes","''",":","{","``","label","''",":","``","dog","''","}","}","]","}",",","``","image_3.png","''",":","{","``","filename","''",":","``","image_3.png","''",",","``","regions","''",":","[","{","``","shape_attributes","''",":","{","``","name","''",":","``","rect","''",",","``","x","''",":","10",",","``","y","''",":","20",",","``","width","''",":","5",",","``","height","''",":","25","}",",","``","region_attributes","''",":","{","``","label","''",":","``","cat","''","}","}","]","}","}","..","doctest",":",":",">",">",">","from","flash","import","Trainer",">",">",">","from","flash.image","import","ObjectDetector",",","ObjectDetectionData",">",">",">","datamodule","=","ObjectDetectionData.from_via","(","...","[","``","cat","''",",","``","dog","''","]",",","...","train_folder=","''","train_folder","''",",","...","train_ann_file=","''","train_annotations.json","''",",","...","predict_folder=","''","predict_folder","''",",","...","transform_kwargs=dict","(","image_size=","(","128",",","128",")",")",",","...","batch_size=2",",","...",")",">",">",">","datamodule.num_classes","3",">",">",">","datamodule.labels","[","'background","'",",","'cat","'",",","'dog","'","]",">",">",">","model","=","ObjectDetector","(","num_classes=datamodule.num_classes",")",">",">",">","trainer","=","Trainer","(","fast_dev_run=True",")",">",">",">","trainer.fit","(","model",",","datamodule=datamodule",")","#","doctest",":","+ELLIPSIS","+NORMALIZE_WHITESPACE","Training","...",">",">",">","trainer.predict","(","model",",","datamodule=datamodule",")","#","doctest",":","+ELLIPSIS","+NORMALIZE_WHITESPACE","Predicting","...","..","testcleanup",":",":",">",">",">","import","shutil",">",">",">","shutil.rmtree","(","``","train_folder","''",")",">",">",">","shutil.rmtree","(","``","predict_folder","''",")",">",">",">","os.remove","(","``","train_annotations.json","''",")"]
code: def from_via(
      cls,
      labels: List[str],
      label_field: str = "label",
      train_folder: Optional[str] = None,
      train_ann_file: Optional[str] = None,
      val_folder: Optional[str] = None,
      val_ann_file: Optional[str] = None,
      test_folder: Optional[str] = None,
      test_ann_file: Optional[str] = None,
      predict_folder: Optional[str] = None,
      transform: INPUT_TRANSFORM_TYPE = IceVisionInputTransform,
      input_cls: Type[Input] = IceVisionInput,
      transform_kwargs: Optional[Dict] = None,
      **data_module_kwargs: Any,
  ) -> "ObjectDetectionData":
      """Creates a :class:`~flash.image.detection.data.ObjectDetectionData` object from the given data folders and
      annotation files in the VIA (`VGG Image Annotator <https://www.robots.ox.ac.uk/~vgg/software/via/>`_) `JSON
      format <https://gitlab.com/vgg/via/-/blob/via-3.x.y/via-3.x.y/CodeDoc.md#structure-of-via-project- json-file>`_.
  
      To learn how to customize the transforms applied for each stage, read our
      :ref:`customizing transforms guide <customizing_transforms>`.
  
      Args:
          labels: A list of class labels. Not that the list should not include a label for the background class which
              will be added automatically as class zero (additional labels will be sorted).
          label_field: The field within ``region_attributes`` which corresponds to the region label.
          train_folder: The folder containing images to use when training.
          train_ann_file: The VIA format annotation file to use when training.
          val_folder: The folder containing images to use when validating.
          val_ann_file: The VIA format annotation file to use when validating.
          test_folder: The folder containing images to use when testing.
          test_ann_file: The VIA format annotation file to use when testing.
          predict_folder: The folder containing images to use when predicting.
          input_cls: The :class:`~flash.core.data.io.input.Input` type to use for loading the data.
          transform: The :class:`~flash.core.data.io.input_transform.InputTransform` type to use.
          transform_kwargs: Dict of keyword arguments to be provided when instantiating the transforms.
          data_module_kwargs: Additional keyword arguments to provide to the
            :class:`~flash.core.data.data_module.DataModule` constructor.
  
      Returns:
          The constructed :class:`~flash.image.detection.data.ObjectDetectionData`.
  
      Examples
      ________
  
      .. testsetup::
  
          >>> import os
          >>> import json
          >>> import numpy as np
          >>> from PIL import Image
          >>> rand_image = Image.fromarray(np.random.randint(0, 255, (64, 64, 3), dtype="uint8"))
          >>> os.makedirs("train_folder", exist_ok=True)
          >>> os.makedirs("predict_folder", exist_ok=True)
          >>> _ = [rand_image.save(os.path.join("train_folder", f"image_{i}.png")) for i in range(1, 4)]
          >>> _ = [rand_image.save(os.path.join("predict_folder", f"predict_image_{i}.png")) for i in range(1, 4)]
          >>> annotations = {
          ...     f"image_{i+1}.png": {
          ...         "filename": f"image_{i+1}.png",
          ...         "regions": [{
          ...                 "shape_attributes": {"name": "rect", "x": 10, "y": 20, "width": 5, "height": 10},
          ...                 "region_attributes": {"label": lb},
          ...         }]
          ...     } for i, lb in enumerate(["cat", "dog", "cat"])
          ... }
          >>> with open("train_annotations.json", "w") as annotation_file:
          ...     json.dump(annotations, annotation_file)
  
      The folder ``train_folder`` has the following contents:
  
      .. code-block::
  
          train_folder
          ├── image_1.png
          ├── image_2.png
          ├── image_3.png
          ...
  
      The file ``train_annotations.json`` contains the following:
  
      .. code-block::
  
          {
              "image_1.png": {
                  "filename": "image_1.png",
                  "regions": [{
                      "shape_attributes": {"name": "rect", "x": 10, "y": 20, "width": 5, "height": 10},
                      "region_attributes": {"label": "cat"}
                  }]
              }, "image_2.png": {
                  "filename": "image_2.png",
                  "regions": [{
                      "shape_attributes": {"name": "rect", "x": 20, "y": 30, "width": 10, "height": 10},
                      "region_attributes": {"label": "dog"}}
              ]}, "image_3.png": {
                  "filename": "image_3.png",
                  "regions": [{
                      "shape_attributes": {"name": "rect", "x": 10, "y": 20, "width": 5, "height": 25},
                      "region_attributes": {"label": "cat"}
                  }]
              }
          }
  
      .. doctest::
  
          >>> from flash import Trainer
          >>> from flash.image import ObjectDetector, ObjectDetectionData
          >>> datamodule = ObjectDetectionData.from_via(
          ...     ["cat", "dog"],
          ...     train_folder="train_folder",
          ...     train_ann_file="train_annotations.json",
          ...     predict_folder="predict_folder",
          ...     transform_kwargs=dict(image_size=(128, 128)),
          ...     batch_size=2,
          ... )
          >>> datamodule.num_classes
          3
          >>> datamodule.labels
          ['background', 'cat', 'dog']
          >>> model = ObjectDetector(num_classes=datamodule.num_classes)
          >>> trainer = Trainer(fast_dev_run=True)
          >>> trainer.fit(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
          Training...
          >>> trainer.predict(model, datamodule=datamodule)  # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
          Predicting...
  
      .. testcleanup::
  
          >>> import shutil
          >>> shutil.rmtree("train_folder")
          >>> shutil.rmtree("predict_folder")
          >>> os.remove("train_annotations.json")
      """
      return cls.from_icedata(
          train_folder=train_folder,
          train_ann_file=train_ann_file,
          val_folder=val_folder,
          val_ann_file=val_ann_file,
          test_folder=test_folder,
          test_ann_file=test_ann_file,
          predict_folder=predict_folder,
          parser=partial(
              VIABBoxParser,
              class_map=ClassMap(list(sorted_alphanumeric(labels))),
              label_field=label_field,
          ),
          input_cls=input_cls,
          transform=transform,
          transform_kwargs=transform_kwargs,
          **data_module_kwargs,
      )
  
imports: from functools import partial
  from typing import Any, Callable, Collection, Dict, List, Optional, Sequence, Type, Union
  import numpy
  import torch
  from flash.core.data.data_module import DataModule
  from flash.core.data.io.input import Input
  from flash.core.data.utilities.classification import TargetFormatter
  from flash.core.data.utilities.sort import sorted_alphanumeric
  from flash.core.integrations.icevision.data import IceVisionInput
  from flash.core.integrations.icevision.transforms import IceVisionInputTransform
  from flash.core.utilities.imports import _FIFTYONE_AVAILABLE, _ICEVISION_AVAILABLE, _TOPIC_IMAGE_AVAILABLE, Image, requires
  from flash.core.utilities.stages import RunningStage
  from flash.core.utilities.types import INPUT_TRANSFORM_TYPE
  from flash.image.detection.input import ObjectDetectionFiftyOneInput, ObjectDetectionFilesInput, ObjectDetectionImageInput, ObjectDetectionNumpyInput, ObjectDetectionTensorInput
label_desc: Use image node_id 8 for calling the ObjectDetectionData obj's underlying member method code with example usage: obj.from_via(cls, labels, label_field, train_folder, train_ann_file, val_folder, val_ann_file, test_folder, test_ann_file, predict_folder, transform, input_cls, transform_kwargs) and returns: cls