当前位置:网站首页>[paddleseg source code reading] add boundary IOU calculation in paddleseg validation (1) -- val.py file details tips

[paddleseg source code reading] add boundary IOU calculation in paddleseg validation (1) -- val.py file details tips

2022-07-07 18:08:00 Master Fuwen

The last one was tossed boundary IoU Calculation method of , This article talks about PaddleSeg How to add Boundary IoU do Val, Training and infer Almost. ,hxdm Step on the pit yourself


First of all, the whole thing PaddleSeg Lite version :

.travis.yml
.style.yapf
.pre-commit-config.yaml
.gitignore
.copyright.hook
LICENSE
README.md
README_CN.md
requirements.txt

export.py  #  Delete it or not , This is used to export static graph model files 
setup.py #  This is used to install  PaddleSeg  Of , Delete it 
benchmark
deploy
docs
EISeg #  An efficient and intelligent interactive segmentation and annotation software based on propeller 
slim
test_tipc #  Integrated certification of propeller training and propulsion  (Training and Inference Pipeline Certification(TIPC))  Information and testing tools 
tests

If it's just running training and reasoning , The above can be deleted directly

contrib  Just some other functions , such as  matting、PPHumanSeg  And so on. , Can be deleted 
configs  Well , That's the pile of configuration files , Understand everything 
tools, There are some commonly used tools , Label , Annotation format conversion and so on , After that, I don't want to write wheels , Just look here ( This is a treasure chest )

 Insert picture description here

That's my PaddleSeg Lite version ,

val.py file

There's nothing to see , Just two sentences of code

#  Directly here   Data class   Instantiation 
val_dataset = cfg.val_dataset
#  Directly here  model  Instantiation 
model = cfg.model

Why instantiate directly here ? Obviously, it assigns an attribute , In fact, single-step debugging , You can see , He actually added a decorator to the attribute @property

@property
def val_dataset(self) -> paddle.io.Dataset:
    _val_dataset = self.val_dataset_config
    if not _val_dataset:
        return None
    return self._load_object(_val_dataset)
@property
def model(self) -> paddle.nn.Layer:
    model_cfg = self.dic.get('model').copy()
    if not model_cfg:
        raise RuntimeError('No model specified in the configuration file.')
    if not 'num_classes' in model_cfg:
        ......

    if not self._model:
        self._model = self._load_object(model_cfg)
    return self._model

Did you see? , There is one self._load_object function , All classes are loaded through this method ( Instantiation ) Of

Take a look at this method ( I hope the old irons can read it by themselves ):

def _load_object(self, cfg: dict) -> Any:
    cfg = cfg.copy()
    if 'type' not in cfg:
        raise RuntimeError('No object information in {}.'.format(cfg))

    component = self._load_component(cfg.pop('type'))
	
	#  The upper boundary is to get the class name 
	# -----------------  This is the dividing line  -----------------
	#  Below the dividing line is the parameter dictionary 
	
    params = {
    }
    for key, val in cfg.items():
        if self._is_meta_type(val):
            params[key] = self._load_object(val)
        elif isinstance(val, list):
            params[key] = [
                self._load_object(item)
                if self._is_meta_type(item) else item for item in val
            ]
        else:
            params[key] = val
	
	#  take config After taking out the classes and parameters in , stay return This step returns the object 
    return component(**params)

There's another one inside _is_meta_type Method , Guess the meaning , Is it Can be instantiated Judge

Click this method to see :

def _is_meta_type(self, item: Any) -> bool:
	return isinstance(item, dict) and 'type' in item

If it's a dictionary , And there are type attribute , stay PaddleSeg Of Config In this architecture , Think that this class can be instantiated


OK, Now keep recursing to see the source code , Now let's jump back , have a look sellf._load_object in self._load_component How to get that kind of name

def _load_component(self, com_name: str) -> Any:
    com_list = [
        manager.MODELS, manager.BACKBONES, manager.DATASETS,
        manager.TRANSFORMS, manager.LOSSES
    ]

    for com in com_list:
        if com_name in com.components_dict:
            return com[com_name]
    else:
        raise RuntimeError(
            'The specified component was not found {}.'.format(com_name))

See here , I believe if you have read my blog , Then there must be a feeling of sudden enlightenment
[PaddleSeg Source code reading ] PaddleSeg Custom data class

The end of this blog :

manager.MODELS
manager.BACKBONES
manager.DATASETS
manager.TRANSFORMS
manager.LOSSES

These components are shown , I think so ,PaddleSeg The commonly used components are abstracted , Directly abstracted as Model class ,Backbone Backbone network , Dataset class , picture Transform class and Loss function class

Abstract as a class , You need to add the corresponding decorator , For example, add custom classes , Need this decorator :

@manager.DATASETS.add_component

Look at that English .add_component It means adding components , So in _load_component In the source code ,

for com in com_list:
    if com_name in com.components_dict:
        return com[com_name]

Go and see com_name Whether in manager.MODELSmanager.BACKBONESmanager.DATASETSmanager.TRANSFORMS and manager.LOSSES Among these components

If none of them are in , be raise This is wrong :

raise RuntimeError(
    'The specified component was not found {}.'.format(com_name))

“ This component was not found ”


Here's another line , image metrics Those indicators ( such as IoU, acc And so on. ), They're just functions , Not class , So you don't need to use it .add_component To register

( This sentence is for later use Boundary IoU Pave the way )

Almost , This blog starts with these two sentences :

val_dataset = cfg.val_dataset
model = cfg.model

Led to so many

To sum up for yourselves , Send it to the comment area

原网站

版权声明
本文为[Master Fuwen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071602237975.html