docstr: Creates a :class:`~flash.image.instance_segmentation.data.InstanceSegmentationData` object from the given
  data folders and annotation files in the `COCO JSON format <https://cocodataset.org/#format-data>`_.
  
  For help understanding and using the COCO format, take a look at this tutorial: `Create COCO annotations from
  scratch <COCO>`_.
  
  To learn how to customize the transforms applied for each stage, read our
  :ref:`customizing transforms guide <customizing_transforms>`.
  
  Args:
      train_folder: The folder containing images to use when training.
      train_ann_file: The COCO format annotation file to use when training.
      val_folder: The folder containing images to use when validating.
      val_ann_file: The COCO format annotation file to use when validating.
      test_folder: The folder containing images to use when testing.
      test_ann_file: The COCO 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.instance_segmentation.data.InstanceSegmentationData`.
  
  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 = {"annotations": [
      ...     {"area": 50, "bbox": [10, 20, 5, 10], "category_id": 1, "id": 1, "image_id": 1, "iscrowd": 0,
      ...     "segmentation": [[10, 20, 15, 20, 15, 30, 10, 30]]},
      ...     {"area": 100, "bbox": [20, 30, 10, 10], "category_id": 2, "id": 2, "image_id": 2, "iscrowd": 0,
      ...     "segmentation": [[20, 30, 30, 30, 30, 40, 20, 40]]},
      ...     {"area": 125, "bbox": [10, 20, 5, 25], "category_id": 1, "id": 3, "image_id": 3, "iscrowd": 0,
      ...     "segmentation": [[10, 20, 15, 20, 15, 45, 10, 45]]},
      ... ], "categories": [
      ...     {"id": 1, "name": "cat", "supercategory": "annimal"},
      ...     {"id": 2, "name": "dog", "supercategory": "annimal"},
      ... ], "images": [
      ...     {"file_name": "image_1.png", "height": 64, "width": 64, "id": 1},
      ...     {"file_name": "image_2.png", "height": 64, "width": 64, "id": 2},
      ...     {"file_name": "image_3.png", "height": 64, "width": 64, "id": 3},
      ... ]}
      >>> 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::
  
      {
          "annotations": [
              {"area": 50, "bbox": [10, 20, 5, 10], "category_id": 1, "id": 1, "image_id": 1, "iscrowd": 0,
              "segmentation": [[10, 20, 15, 20, 15, 30, 10, 30]]},
              {"area": 100, "bbox": [20, 30, 10, 10], "category_id": 2, "id": 2, "image_id": 2, "iscrowd": 0,
              "segmentation": [[20, 30, 30, 30, 30, 40, 20, 40]]},
              {"area": 125, "bbox": [10, 20, 5, 25], "category_id": 1, "id": 3, "image_id": 3, "iscrowd": 0,
              "segmentation": [[10, 20, 15, 20, 15, 45, 10, 45]]}
          ], "categories": [
              {"id": 1, "name": "cat", "supercategory": "annimal"},
              {"id": 2, "name": "dog", "supercategory": "annimal"}
          ], "images": [
              {"file_name": "image_1.png", "height": 64, "width": 64, "id": 1},
              {"file_name": "image_2.png", "height": 64, "width": 64, "id": 2},
              {"file_name": "image_3.png", "height": 64, "width": 64, "id": 3}
          ]
      }
  
  .. doctest::
  
      >>> from flash import Trainer
      >>> from flash.image import InstanceSegmentation, InstanceSegmentationData
      >>> datamodule = InstanceSegmentationData.from_coco(
      ...     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 = InstanceSegmentation(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")
  
  .. _COCO: https://www.immersivelimit.com/tutorials/create-coco-annotations-from-scratch
docstr_tok: ["Creates","a",":","class",":","`","~flash.image.instance_segmentation.data.InstanceSegmentationData","`","object","from","the","given","data","folders","and","annotation","files","in","the","`","COCO","JSON","format","<","https",":","\/\/cocodataset.org\/","#","format-data",">","`","_",".","For","help","understanding","and","using","the","COCO","format",",","take","a","look","at","this","tutorial",":","`","Create","COCO","annotations","from","scratch","<","COCO",">","`","_",".","To","learn","how","to","customize","the","transforms","applied","for","each","stage",",","read","our",":","ref",":","`","customizing","transforms","guide","<","customizing_transforms",">","`",".","Args",":","train_folder",":","The","folder","containing","images","to","use","when","training",".","train_ann_file",":","The","COCO","format","annotation","file","to","use","when","training",".","val_folder",":","The","folder","containing","images","to","use","when","validating",".","val_ann_file",":","The","COCO","format","annotation","file","to","use","when","validating",".","test_folder",":","The","folder","containing","images","to","use","when","testing",".","test_ann_file",":","The","COCO","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.instance_segmentation.data.InstanceSegmentationData","`",".","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","=","{","``","annotations","''",":","[","...","{","``","area","''",":","50",",","``","bbox","''",":","[","10",",","20",",","5",",","10","]",",","``","category_id","''",":","1",",","``","id","''",":","1",",","``","image_id","''",":","1",",","``","iscrowd","''",":","0",",","...","``","segmentation","''",":","[","[","10",",","20",",","15",",","20",",","15",",","30",",","10",",","30","]","]","}",",","...","{","``","area","''",":","100",",","``","bbox","''",":","[","20",",","30",",","10",",","10","]",",","``","category_id","''",":","2",",","``","id","''",":","2",",","``","image_id","''",":","2",",","``","iscrowd","''",":","0",",","...","``","segmentation","''",":","[","[","20",",","30",",","30",",","30",",","30",",","40",",","20",",","40","]","]","}",",","...","{","``","area","''",":","125",",","``","bbox","''",":","[","10",",","20",",","5",",","25","]",",","``","category_id","''",":","1",",","``","id","''",":","3",",","``","image_id","''",":","3",",","``","iscrowd","''",":","0",",","...","``","segmentation","''",":","[","[","10",",","20",",","15",",","20",",","15",",","45",",","10",",","45","]","]","}",",","...","]",",","``","categories","''",":","[","...","{","``","id","''",":","1",",","``","name","''",":","``","cat","''",",","``","supercategory","''",":","``","annimal","''","}",",","...","{","``","id","''",":","2",",","``","name","''",":","``","dog","''",",","``","supercategory","''",":","``","annimal","''","}",",","...","]",",","``","images","''",":","[","...","{","``","file_name","''",":","``","image_1.png","''",",","``","height","''",":","64",",","``","width","''",":","64",",","``","id","''",":","1","}",",","...","{","``","file_name","''",":","``","image_2.png","''",",","``","height","''",":","64",",","``","width","''",":","64",",","``","id","''",":","2","}",",","...","{","``","file_name","''",":","``","image_3.png","''",",","``","height","''",":","64",",","``","width","''",":","64",",","``","id","''",":","3","}",",","...","]","}",">",">",">","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",":",":","{","``","annotations","''",":","[","{","``","area","''",":","50",",","``","bbox","''",":","[","10",",","20",",","5",",","10","]",",","``","category_id","''",":","1",",","``","id","''",":","1",",","``","image_id","''",":","1",",","``","iscrowd","''",":","0",",","``","segmentation","''",":","[","[","10",",","20",",","15",",","20",",","15",",","30",",","10",",","30","]","]","}",",","{","``","area","''",":","100",",","``","bbox","''",":","[","20",",","30",",","10",",","10","]",",","``","category_id","''",":","2",",","``","id","''",":","2",",","``","image_id","''",":","2",",","``","iscrowd","''",":","0",",","``","segmentation","''",":","[","[","20",",","30",",","30",",","30",",","30",",","40",",","20",",","40","]","]","}",",","{","``","area","''",":","125",",","``","bbox","''",":","[","10",",","20",",","5",",","25","]",",","``","category_id","''",":","1",",","``","id","''",":","3",",","``","image_id","''",":","3",",","``","iscrowd","''",":","0",",","``","segmentation","''",":","[","[","10",",","20",",","15",",","20",",","15",",","45",",","10",",","45","]","]","}","]",",","``","categories","''",":","[","{","``","id","''",":","1",",","``","name","''",":","``","cat","''",",","``","supercategory","''",":","``","annimal","''","}",",","{","``","id","''",":","2",",","``","name","''",":","``","dog","''",",","``","supercategory","''",":","``","annimal","''","}","]",",","``","images","''",":","[","{","``","file_name","''",":","``","image_1.png","''",",","``","height","''",":","64",",","``","width","''",":","64",",","``","id","''",":","1","}",",","{","``","file_name","''",":","``","image_2.png","''",",","``","height","''",":","64",",","``","width","''",":","64",",","``","id","''",":","2","}",",","{","``","file_name","''",":","``","image_3.png","''",",","``","height","''",":","64",",","``","width","''",":","64",",","``","id","''",":","3","}","]","}","..","doctest",":",":",">",">",">","from","flash","import","Trainer",">",">",">","from","flash.image","import","InstanceSegmentation",",","InstanceSegmentationData",">",">",">","datamodule","=","InstanceSegmentationData.from_coco","(","...","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","=","InstanceSegmentation","(","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","''",")","..","_COCO",":","https",":","\/\/www.immersivelimit.com\/tutorials\/create-coco-annotations-from-scratch"]
code: def from_coco(
      cls,
      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,
      input_cls: Type[Input] = IceVisionInput,
      transform: INPUT_TRANSFORM_TYPE = IceVisionInputTransform,
      transform_kwargs: Optional[Dict] = None,
      **data_module_kwargs: Any,
  ):
      """Creates a :class:`~flash.image.instance_segmentation.data.InstanceSegmentationData` object from the given
      data folders and annotation files in the `COCO JSON format <https://cocodataset.org/#format-data>`_.
  
      For help understanding and using the COCO format, take a look at this tutorial: `Create COCO annotations from
      scratch <COCO>`_.
  
      To learn how to customize the transforms applied for each stage, read our
      :ref:`customizing transforms guide <customizing_transforms>`.
  
      Args:
          train_folder: The folder containing images to use when training.
          train_ann_file: The COCO format annotation file to use when training.
          val_folder: The folder containing images to use when validating.
          val_ann_file: The COCO format annotation file to use when validating.
          test_folder: The folder containing images to use when testing.
          test_ann_file: The COCO 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.instance_segmentation.data.InstanceSegmentationData`.
  
      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 = {"annotations": [
          ...     {"area": 50, "bbox": [10, 20, 5, 10], "category_id": 1, "id": 1, "image_id": 1, "iscrowd": 0,
          ...     "segmentation": [[10, 20, 15, 20, 15, 30, 10, 30]]},
          ...     {"area": 100, "bbox": [20, 30, 10, 10], "category_id": 2, "id": 2, "image_id": 2, "iscrowd": 0,
          ...     "segmentation": [[20, 30, 30, 30, 30, 40, 20, 40]]},
          ...     {"area": 125, "bbox": [10, 20, 5, 25], "category_id": 1, "id": 3, "image_id": 3, "iscrowd": 0,
          ...     "segmentation": [[10, 20, 15, 20, 15, 45, 10, 45]]},
          ... ], "categories": [
          ...     {"id": 1, "name": "cat", "supercategory": "annimal"},
          ...     {"id": 2, "name": "dog", "supercategory": "annimal"},
          ... ], "images": [
          ...     {"file_name": "image_1.png", "height": 64, "width": 64, "id": 1},
          ...     {"file_name": "image_2.png", "height": 64, "width": 64, "id": 2},
          ...     {"file_name": "image_3.png", "height": 64, "width": 64, "id": 3},
          ... ]}
          >>> 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::
  
          {
              "annotations": [
                  {"area": 50, "bbox": [10, 20, 5, 10], "category_id": 1, "id": 1, "image_id": 1, "iscrowd": 0,
                  "segmentation": [[10, 20, 15, 20, 15, 30, 10, 30]]},
                  {"area": 100, "bbox": [20, 30, 10, 10], "category_id": 2, "id": 2, "image_id": 2, "iscrowd": 0,
                  "segmentation": [[20, 30, 30, 30, 30, 40, 20, 40]]},
                  {"area": 125, "bbox": [10, 20, 5, 25], "category_id": 1, "id": 3, "image_id": 3, "iscrowd": 0,
                  "segmentation": [[10, 20, 15, 20, 15, 45, 10, 45]]}
              ], "categories": [
                  {"id": 1, "name": "cat", "supercategory": "annimal"},
                  {"id": 2, "name": "dog", "supercategory": "annimal"}
              ], "images": [
                  {"file_name": "image_1.png", "height": 64, "width": 64, "id": 1},
                  {"file_name": "image_2.png", "height": 64, "width": 64, "id": 2},
                  {"file_name": "image_3.png", "height": 64, "width": 64, "id": 3}
              ]
          }
  
      .. doctest::
  
          >>> from flash import Trainer
          >>> from flash.image import InstanceSegmentation, InstanceSegmentationData
          >>> datamodule = InstanceSegmentationData.from_coco(
          ...     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 = InstanceSegmentation(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")
  
      .. _COCO: https://www.immersivelimit.com/tutorials/create-coco-annotations-from-scratch
      """
      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=COCOMaskParser,
          input_cls=input_cls,
          transform=transform,
          transform_kwargs=transform_kwargs,
          **data_module_kwargs,
      )
  
imports: from functools import partial
  from typing import Any, Callable, Dict, List, Optional, Type, Union
  from torch import tensor
  from flash.core.data.data_module import DataModule
  from flash.core.data.io.input import DataKeys, Input
  from flash.core.data.io.output_transform import OutputTransform
  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 _ICEVISION_AVAILABLE, _TOPIC_IMAGE_AVAILABLE, _TORCHVISION_AVAILABLE, _TORCHVISION_GREATER_EQUAL_0_9
  from flash.core.utilities.stages import RunningStage
  from flash.core.utilities.types import INPUT_TRANSFORM_TYPE
label_desc: Use image node_id 2 for calling the InstanceSegmentationData obj's underlying member method code with example usage: obj.from_coco(cls, train_folder, train_ann_file, val_folder, val_ann_file, test_folder, test_ann_file, predict_folder, input_cls, transform, transform_kwargs) and returns: cls