Ejemplo n.º 1
0
def get_coco_dataset():
    """A dummy COCO dataset that includes only the 'classes' field."""
    ds = AttrDict()
    classes = [
        '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
        'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant',
        'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse',
        'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack',
        'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis',
        'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove',
        'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass',
        'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich',
        'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake',
        'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
        'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
        'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
        'scissors', 'teddy bear', 'hair drier', 'toothbrush'
    ]
    ds.classes = {i: name for i, name in enumerate(classes)}
    return ds
Ejemplo n.º 2
0
    def test_merge_cfg_from_cfg(self):
        # Test: merge from deepcopy
        s = 'dummy0'
        cfg2 = copy.deepcopy(cfg)
        cfg2.MODEL.TYPE = s
        core_config.merge_cfg_from_cfg(cfg2)
        assert cfg.MODEL.TYPE == s

        # Test: merge from yaml
        s = 'dummy1'
        cfg2 = core_config.load_cfg(envu.yaml_dump(cfg))
        cfg2.MODEL.TYPE = s
        core_config.merge_cfg_from_cfg(cfg2)
        assert cfg.MODEL.TYPE == s

        # Test: merge with a valid key
        s = 'dummy2'
        cfg2 = AttrDict()
        cfg2.MODEL = AttrDict()
        cfg2.MODEL.TYPE = s
        core_config.merge_cfg_from_cfg(cfg2)
        assert cfg.MODEL.TYPE == s

        # Test: merge with an invalid key
        s = 'dummy3'
        cfg2 = AttrDict()
        cfg2.FOO = AttrDict()
        cfg2.FOO.BAR = s
        with self.assertRaises(KeyError):
            core_config.merge_cfg_from_cfg(cfg2)

        # Test: merge with converted type
        cfg2 = AttrDict()
        cfg2.TRAIN = AttrDict()
        cfg2.TRAIN.SCALES = [1]
        core_config.merge_cfg_from_cfg(cfg2)
        assert type(cfg.TRAIN.SCALES) is tuple
        assert cfg.TRAIN.SCALES[0] == 1

        # Test: merge with invalid type
        cfg2 = AttrDict()
        cfg2.TRAIN = AttrDict()
        cfg2.TRAIN.SCALES = 1
        with self.assertRaises(ValueError):
            core_config.merge_cfg_from_cfg(cfg2)
Ejemplo n.º 3
0
def get_ivoc_dataset():
    """A dummy COCO dataset that includes only the 'classes' field."""
    ds = AttrDict()
    classes = ['__background__', 'people', 'bicycle', 'electric bicycle']
    ds.classes = {i: name for i, name in enumerate(classes)}
    return ds
Ejemplo n.º 4
0
    def inference(self, image_list):
        """Do an inference on the model with a set of inputs.

        # Arguments:
            image_list: The input image list

        Return the result of the inference.
        """
        image = image_list[0]
        image = self.linear_to_srgb(image)*255.
        imcpy = image.copy()

        # Initialize the model out of the configuration and weights files
        if not hasattr(self, 'model'):
            workspace.ResetWorkspace()
            # Reset to default config
            merge_cfg_from_cfg(self.default_cfg)
            # Load mask rcnn configuration file
            merge_cfg_from_file(self.cfg_file)
            assert_and_infer_cfg(cache_urls=False, make_immutable=False)
            self.model = infer_engine.initialize_model_from_cfg(self.weights)
            # Save mask rcnn full configuration file
            self.mrcnn_cfg = copy.deepcopy(AttrDict(cfg)) # cfg from detectron.core.config
        else:
            # There is a global config file for all detectron models (Densepose, Mask RCNN..)
            # Check if current global config file is correct for mask rcnn
            if not dict_equal(self.mrcnn_cfg, cfg):
                # Free memory of previous workspace
                workspace.ResetWorkspace()
                # Load mask rcnn configuration file
                merge_cfg_from_cfg(self.mrcnn_cfg)
                assert_and_infer_cfg(cache_urls=False, make_immutable=False)
                self.model = infer_engine.initialize_model_from_cfg(self.weights)

        with c2_utils.NamedCudaScope(0):
            cls_boxes, cls_segms, cls_keyps, _ = infer_engine.im_detect_all(
                self.model, image[:, :, ::-1], None
                )

        if self.binary_masks:
            res = vis_utils.vis_one_image_binary(
                imcpy,
                cls_boxes,
                cls_segms,
                thresh=self.thresh
                )
        else:
            res = vis_utils.vis_one_image_opencv(
                imcpy,
                cls_boxes,
                cls_segms,
                cls_keyps,
                thresh=self.thresh,
                show_box=self.show_box,
                show_class=self.show_class,
                dataset=self.dummy_coco_dataset,
                alpha=self.alpha,
                show_border=self.show_border,
                border_thick=self.border_thick,
                bbox_thick=self.bbox_thick,
                font_scale=self.font_scale
                )

        res = self.srgb_to_linear(res.astype(np.float32) / 255.)

        return [res]
Ejemplo n.º 5
0
    def test_merge_cfg_from_cfg(self):
        # Test: merge from deepcopy
        s = 'dummy0'
        cfg2 = copy.deepcopy(cfg)
        cfg2.MODEL.TYPE = s
        core_config.merge_cfg_from_cfg(cfg2)
        assert cfg.MODEL.TYPE == s

        # Test: merge from yaml
        s = 'dummy1'
        cfg2 = core_config.load_cfg(yaml.dump(cfg))
        cfg2.MODEL.TYPE = s
        core_config.merge_cfg_from_cfg(cfg2)
        assert cfg.MODEL.TYPE == s

        # Test: merge with a valid key
        s = 'dummy2'
        cfg2 = AttrDict()
        cfg2.MODEL = AttrDict()
        cfg2.MODEL.TYPE = s
        core_config.merge_cfg_from_cfg(cfg2)
        assert cfg.MODEL.TYPE == s

        # Test: merge with an invalid key
        s = 'dummy3'
        cfg2 = AttrDict()
        cfg2.FOO = AttrDict()
        cfg2.FOO.BAR = s
        with self.assertRaises(KeyError):
            core_config.merge_cfg_from_cfg(cfg2)

        # Test: merge with converted type
        cfg2 = AttrDict()
        cfg2.TRAIN = AttrDict()
        cfg2.TRAIN.SCALES = [1]
        core_config.merge_cfg_from_cfg(cfg2)
        assert type(cfg.TRAIN.SCALES) is tuple
        assert cfg.TRAIN.SCALES[0] == 1

        # Test: merge with invalid type
        cfg2 = AttrDict()
        cfg2.TRAIN = AttrDict()
        cfg2.TRAIN.SCALES = 1
        with self.assertRaises(ValueError):
            core_config.merge_cfg_from_cfg(cfg2)
Ejemplo n.º 6
0
    def test_immutability(self):
        # Top level immutable
        a = AttrDict()
        a.foo = 0
        a.immutable(True)
        with self.assertRaises(AttributeError):
            a.foo = 1
            a.bar = 1
        assert a.is_immutable()
        assert a.foo == 0
        a.immutable(False)
        assert not a.is_immutable()
        a.foo = 1
        assert a.foo == 1

        # Recursively immutable
        a.level1 = AttrDict()
        a.level1.foo = 0
        a.level1.level2 = AttrDict()
        a.level1.level2.foo = 0
        a.immutable(True)
        assert a.is_immutable()
        with self.assertRaises(AttributeError):
            a.level1.level2.foo = 1
            a.level1.bar = 1
        assert a.level1.level2.foo == 0

        # Serialize immutability state
        a.immutable(True)
        a2 = core_config.load_cfg(yaml.dump(a))
        assert a.is_immutable()
        assert a2.is_immutable()
Ejemplo n.º 7
0
def get_hanzi_dataset():
    ds = AttrDict()
    classes = ['__background__', 'hanzi']
    ds.classes = {i: name for i, name in enumerate(classes)}
    return ds
Ejemplo n.º 8
0
    def inference(self, image_list):
        """Do an inference of the DensePose model with a set of image inputs.

        # Arguments:
            image_list: The input image list
            
        Return the result of the inference.        
        """
        # Directly return image when no inference options
        if not (self.show_human_index or self.show_uv or self.show_border
                or self.show_grid):
            return [image_list[0]]

        image = image_list[0]
        image = self.linear_to_srgb(image) * 255.
        imcpy = image.copy()

        # Initialize the model out of the configuration and weights files
        if not hasattr(self, 'model'):
            workspace.ResetWorkspace()
            # Reset to default config
            merge_cfg_from_cfg(self.default_cfg)
            # Load densepose configuration file
            merge_cfg_from_file(self.cfg_file)
            assert_and_infer_cfg(cache_urls=False, make_immutable=False)
            self.model = infer_engine.initialize_model_from_cfg(self.weights)
            # Save densepose full configuration file
            self.densepose_cfg = copy.deepcopy(
                AttrDict(cfg))  #cfg from detectron.core.config
        else:
            # There is a global config file for all detectron models (Densepose, Mask RCNN..)
            # Check if current global config file is correct for densepose
            if not dict_equal(self.densepose_cfg, cfg):
                # Free memory of previous workspace
                workspace.ResetWorkspace()
                # Load densepose configuration file
                merge_cfg_from_cfg(self.densepose_cfg)
                assert_and_infer_cfg(cache_urls=False, make_immutable=False)
                self.model = infer_engine.initialize_model_from_cfg(
                    self.weights)

        # Compute the image inference
        with c2_utils.NamedCudaScope(0):
            # image in BGR format for inference
            cls_boxes, cls_segms, cls_keyps, cls_bodys = infer_engine.im_detect_all(
                self.model, image[:, :, ::-1], None)

        res = vis_utils.vis_densepose(
            imcpy,  # image in RGB format for visualization
            cls_boxes,
            cls_bodys,
            show_human_index=self.show_human_index,
            show_uv=self.show_uv,
            show_grid=self.show_grid,
            show_border=self.show_border,
            border_thick=self.border_thick,
            alpha=self.alpha)

        res = self.srgb_to_linear(res.astype(np.float32) / 255.)

        return [res]
Ejemplo n.º 9
0
def get_nucleus_dataset():
    ds = AttrDict()
    classes = ['__background__', 'diploidn']
    ds.classes = {i: name for i, name in enumerate(classes)}
    return ds
Ejemplo n.º 10
0
def get_ava_dataset():
    """A dummy COCO dataset that includes only the 'classes' field."""
    ds = AttrDict()
    classes = ['__background__', 'actor']
    ds.classes = {i: name for i, name in enumerate(classes)}
    return ds
Ejemplo n.º 11
0
def get_cones_dataset():
    """A cones dataset that includes only the 'classes' field."""
    ds = AttrDict()
    classes = ['__background__', 'Yellow', 'Blue', 'Orange']
    ds.classes = {i: name for i, name in enumerate(classes)}
    return ds
Ejemplo n.º 12
0
def get_theyes_dataset():
    ds = AttrDict()
    classes = ['__background__', 'bottom', 'shoe', 'bag', 'one_piece', 'tops']
    ds.classes = {i: name for i, name in enumerate(classes)}
    return ds
Ejemplo n.º 13
0
def get_coco_dataset():
    """A dummy COCO dataset that includes only the 'classes' field."""
    ds = AttrDict()
    # classes = [
    #     '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
    #     'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant',
    #     'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse',
    #     'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack',
    #     'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis',
    #     'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove',
    #     'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass',
    #     'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich',
    #     'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake',
    #     'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
    #     'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
    #     'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
    #     'scissors', 'teddy bear', 'hair drier', 'toothbrush'
    # ]
    # classes = ['__background__', 'lane']
    #
    base_classes = [
        '__background__', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
        'bus', 'train', 'truck', 'boat', 'traffic light', 'fire hydrant',
        'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse',
        'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack',
        'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis',
        'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove',
        'skateboard', 'surfboard', 'tennis racket', 'bottle', 'wine glass',
        'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich',
        'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake',
        'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet',
        'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
        'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book',
        'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
    ]
    classes = [
        '__background__',
        'guard rail',
        # 'car',
        'dashed',
        'solid',
        'solid solid',
        'dashed dashed',
        'dashed-solid',
        'solid-dashed',
        'yellow dashed',
        'yellow solid',
        'yellow solid solid',
        'yellow dashed dashed',
        'yellow dashed-solid',
        'yellow solid-dashed',
        'boundary',
        'fork_line',
        'fork_edge',
        'arrow_s',
        'arrow_r',
        'arrow_l',
        'arrow_lr',
        'arrow_inclined_r',
        'arrow_r_s',
        'arrow_l_s',
        'sidewalk',
        'handrail'
    ]
    base_classes.extend(classes[1:])
    classes = base_classes
    ds.classes = {i: name for i, name in enumerate(classes)}
    return ds
Ejemplo n.º 14
0
from ast import literal_eval
from future.utils import iteritems
from past.builtins import basestring
import copy
import logging
import numpy as np
import os
import os.path as osp
import yaml

from detectron.utils.collections import AttrDict
from detectron.utils.io import cache_url

logger = logging.getLogger(__name__)

__C = AttrDict()
# Consumers can get config by:
#   from detectron.core.config import cfg
cfg = __C

# Random note: avoid using '.ON' as a config key since yaml converts it to True;
# prefer 'ENABLED' instead

# ---------------------------------------------------------------------------- #
# Training options
# ---------------------------------------------------------------------------- #
__C.TRAIN = AttrDict()

# Initialize network with weights from this .pkl file
__C.TRAIN.WEIGHTS = b''
Ejemplo n.º 15
0
from detectron.core.config import merge_cfg_from_file
from detectron.core.config import merge_cfg_from_list
from detectron.core.config import assert_and_infer_cfg
import detectron.core.test_engine as infer_engine
import detectron.utils.c2 as c2_utils
import detectron.utils.io as io_utils
import detectron.utils.model_convert_utils as mutils
import detectron.datasets.dummy_datasets as dummy_datasets
import tools.convert_pkl_to_pb as convert_tools

#############################################################
# Supposed to be used for "seg-every-thing models" training #
# Set for compatibility but currenlty ignored               #
from detectron.utils.collections import AttrDict

cfg.MRCNN.BBOX2MASK = AttrDict()
cfg.MRCNN.BBOX2MASK.BBOX2MASK_ON = False
cfg.MRCNN.BBOX2MASK.TYPE = b''
cfg.MRCNN.BBOX2MASK.USE_PRETRAINED_EMBED = False
cfg.MRCNN.BBOX2MASK.PRETRAINED_EMBED_NAME = b''
cfg.MRCNN.BBOX2MASK.PRETRAINED_EMBED_DIM = -1
cfg.MRCNN.BBOX2MASK.STOP_DET_W_GRAD = True
cfg.MRCNN.BBOX2MASK.INCLUDE_CLS_SCORE = True
cfg.MRCNN.BBOX2MASK.INCLUDE_BBOX_PRED = False
cfg.MRCNN.BBOX2MASK.USE_LEAKYRELU = True
cfg.MRCNN.JOINT_FCN_MLP_HEAD = False
cfg.MRCNN.MLP_MASK_BRANCH_TYPE = b''
cfg.TRAIN.TRAIN_MASK_HEAD_ONLY = False
cfg.TRAIN.MRCNN_FILTER_LABELS = False
cfg.TRAIN.MRCNN_LABELS_TO_KEEP = ()
Ejemplo n.º 16
0
def merge_cfg_from_file(cfg_filename):
    """Load a yaml config file and merge it into the global config."""
    with open(cfg_filename, 'r') as f:
        yaml_cfg = AttrDict(load_cfg(f))
    _merge_a_into_b(yaml_cfg, __C)
Ejemplo n.º 17
0
    def test_immutability(self):
        # Top level immutable
        a = AttrDict()
        a.foo = 0
        a.immutable(True)
        with self.assertRaises(AttributeError):
            a.foo = 1
            a.bar = 1
        assert a.is_immutable()
        assert a.foo == 0
        a.immutable(False)
        assert not a.is_immutable()
        a.foo = 1
        assert a.foo == 1

        # Recursively immutable
        a.level1 = AttrDict()
        a.level1.foo = 0
        a.level1.level2 = AttrDict()
        a.level1.level2.foo = 0
        a.immutable(True)
        assert a.is_immutable()
        with self.assertRaises(AttributeError):
            a.level1.level2.foo = 1
            a.level1.bar = 1
        assert a.level1.level2.foo == 0

        # Serialize immutability state
        a.immutable(True)
        a2 = core_config.load_cfg(envu.yaml_dump(a))
        assert a.is_immutable()
        assert a2.is_immutable()
Ejemplo n.º 18
0
from ast import literal_eval
from future.utils import iteritems
from past.builtins import basestring
import copy
import logging
import numpy as np
import os
import os.path as osp
import yaml

from detectron.utils.collections import AttrDict
from detectron.utils.io import cache_url

logger = logging.getLogger(__name__)

__C = AttrDict()
# Consumers can get config by:
#   from detectron.core.config import cfg
cfg = __C


# Random note: avoid using '.ON' as a config key since yaml converts it to True;
# prefer 'ENABLED' instead

# ---------------------------------------------------------------------------- #
# Training options
# ---------------------------------------------------------------------------- #
__C.TRAIN = AttrDict()

# Initialize network with weights from this .pkl file
__C.TRAIN.WEIGHTS = b''
Ejemplo n.º 19
0
from __future__ import print_function
from __future__ import unicode_literals
from ast import literal_eval
from future.utils import iteritems
import copy, io, logging
import numpy as np
import os
import os.path as osp
import six

from detectron.utils.collections import AttrDict
from detectron.utils.io import cache_url

logger = logging.getLogger(__name__)  # 日志模块

__C = AttrDict()

cfg = __C

# Training options
__C.TRAIN = AttrDict()

__C.TRAIN.WEIGHTS = ''

__C.TRAIN.DATASETS = ()

__C.TRAIN.SCALES = (600, )

__C.TRAIN.MAX_SIZE = 1000

__C.TRAIN.IMS_PER_BATCH = 2