easycv.utils package¶
Submodules¶
easycv.utils.alias_multinomial module¶
easycv.utils.bbox_util module¶
- easycv.utils.bbox_util.batched_cxcywh2xyxy_with_shape(bboxes, shape)[source]¶
reverse of xyxy2xywh_with_shape transform normalized points [[x_center, y_center, box_w, box_h],…] to standard [[x1, y1, x2, y2],…] :param bboxes: np.array or tensor like [[x_center, y_center, box_w, box_h],…],
all value is normalized
- Parameters
shape – img shape: [h, w]
return: np.array or tensor like [[x1, y1, x2, y2],…]
- easycv.utils.bbox_util.bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-09)[source]¶
- easycv.utils.bbox_util.box_iou(box1, box2)[source]¶
Return intersection-over-union (Jaccard index) of boxes. Both sets of boxes are expected to be in (x1, y1, x2, y2) format. :param box1: :type box1: Tensor[N, 4] :param box2: :type box2: Tensor[M, 4]
- Returns
- the NxM matrix containing the pairwise
IoU values for every element in boxes1 and boxes2
- Return type
iou (Tensor[N, M])
- easycv.utils.bbox_util.box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1)[source]¶
Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
easycv.utils.checkpoint module¶
- easycv.utils.checkpoint.load_checkpoint(model, filename, map_location='cpu', strict=False, logger=None)[source]¶
Load checkpoint from a file or URI.
- Parameters
model (Module) – Module to load checkpoint.
filename (str) – Accept local filepath, URL,
torchvision://xxx
,open-mmlab://xxx
. Please refer todocs/model_zoo.md
for details.map_location (str) – Same as
torch.load()
.strict (bool) – Whether to allow different params for the model and checkpoint.
logger (
logging.Logger
or None) – The logger for error message.
- Returns
The loaded checkpoint.
- Return type
dict or OrderedDict
- easycv.utils.checkpoint.save_checkpoint(model, filename, optimizer=None, meta=None)[source]¶
Save checkpoint to file.
The checkpoint will have 3 fields:
meta
,state_dict
andoptimizer
. By defaultmeta
will contain version and time info.- Parameters
model (Module) – Module whose params are to be saved.
filename (str) – Checkpoint filename.
optimizer (
Optimizer
, optional) – Optimizer to be saved.meta (dict, optional) – Metadata to be saved in checkpoint.
easycv.utils.collect module¶
- easycv.utils.collect.nondist_forward_collect(func, data_loader, length)[source]¶
Forward and collect network outputs.
This function performs forward propagation and collects outputs. It can be used to collect results, features, losses, etc.
- Parameters
func (function) – The function to process data. The output must be a dictionary of CPU tensors.
length (int) – Expected length of output arrays.
- Returns
The concatenated outputs.
- Return type
results_all (dict(np.ndarray))
- easycv.utils.collect.dist_forward_collect(func, data_loader, rank, length, ret_rank=- 1)[source]¶
Forward and collect network outputs in a distributed manner.
This function performs forward propagation and collects outputs. It can be used to collect results, features, losses, etc.
- Parameters
func (function) – The function to process data. The output must be a dictionary of CPU tensors.
rank (int) – This process id.
length (int) – Expected length of output arrays.
ret_rank (int) – The process that returns. Other processes will return None.
- Returns
The concatenated outputs.
- Return type
results_all (dict(np.ndarray))
easycv.utils.config_tools module¶
- easycv.utils.config_tools.check_base_cfg_path(base_cfg_name='configs/base.py', ori_filename=None)[source]¶
- easycv.utils.config_tools.config_dict_edit(ori_cfg_dict, cfg_dict, reg, dict_mem_helper)[source]¶
edit ${configs.variables} in config dict to solve dependicies in config
ori_cfg_dict: to find the true value of ${configs.variables} cfg_dict: for find leafs of dict by recursive reg: Regular expression pattern for find all ${configs.variables} in leafs of dict dict_mem_helper: to store the true value of ${configs.variables} which have been found
easycv.utils.constant module¶
easycv.utils.dist_utils module¶
- easycv.utils.dist_utils.obj2tensor(pyobj, device='cuda')[source]¶
Serialize picklable python object to tensor.
- easycv.utils.dist_utils.all_reduce_dict(py_dict, op='sum', group=None, to_float=True)[source]¶
Apply all reduce function for python dict object.
The code is modified from https://github.com/Megvii- BaseDetection/YOLOX/blob/main/yolox/utils/allreduce_norm.py.
NOTE: make sure that py_dict in different ranks has the same keys and the values should be in the same shape.
- Parameters
py_dict (dict) – Dict to be applied all reduce op.
op (str) – Operator, could be ‘sum’ or ‘mean’. Default: ‘sum’
group (
torch.distributed.group
, optional) – Distributed group, Default: None.to_float (bool) – Whether to convert all values of dict to float. Default: True.
- Returns
reduced python dict object.
- Return type
OrderedDict
easycv.utils.eval_utils module¶
- easycv.utils.eval_utils.generate_best_metric_name(evaluate_type, dataset_name, metric_names)[source]¶
Generate best metric name for different evaluator / different dataset / different metric_names evaluate_type: str dataset_name: None or str metric_names: None str or list[str] or tuple(str)
- Returns
list[str]
easycv.utils.flops_counter module¶
- easycv.utils.flops_counter.get_model_info(model, input_size, model_config, logger)[source]¶
get_model_info, check model parameters and Gflops
- easycv.utils.flops_counter.get_model_complexity_info(model, input_res, print_per_layer_stat=True, as_strings=True, input_constructor=None, ost=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]¶
- easycv.utils.flops_counter.params_to_string(params_num)[source]¶
converting number to string
- Parameters
params_num (float) – number
- Returns str
number
>>> params_to_string(1e9) '1000.0 M' >>> params_to_string(2e5) '200.0 k' >>> params_to_string(3e-9) '3e-09'
- easycv.utils.flops_counter.print_model_with_flops(model, units='GMac', precision=3, ost=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)[source]¶
- easycv.utils.flops_counter.compute_average_flops_cost(self)[source]¶
A method that will be available after add_flops_counting_methods() is called on a desired net object. Returns current mean flops consumption per image.
- easycv.utils.flops_counter.start_flops_count(self)[source]¶
A method that will be available after add_flops_counting_methods() is called on a desired net object. Activates the computation of mean flops consumption per image. Call it before you run the network.
- easycv.utils.flops_counter.stop_flops_count(self)[source]¶
A method that will be available after add_flops_counting_methods() is called on a desired net object. Stops computing the mean flops consumption per image. Call whenever you want to pause the computation.
easycv.utils.gather module¶
easycv.utils.json_utils module¶
Utilities for dealing with writing json strings.
json_utils wraps json.dump and json.dumps so that they can be used to safely control the precision of floats when writing to json strings or files.
- class easycv.utils.json_utils.MyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]¶
Bases:
json.encoder.JSONEncoder
- default(o)[source]¶
Implement this method in a subclass such that it returns a serializable object for
o
, or calls the base implementation (to raise aTypeError
).For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
- easycv.utils.json_utils.dump(obj, fid, float_digits=- 1, **params)[source]¶
Wrapper of json.dump that allows specifying the float precision used.
- Parameters
obj – The object to dump.
fid – The file id to write to.
float_digits – The number of digits of precision when writing floats out.
**params – Additional parameters to pass to json.dumps.
- easycv.utils.json_utils.dumps(obj, float_digits=- 1, **params)[source]¶
Wrapper of json.dumps that allows specifying the float precision used.
- Parameters
obj – The object to dump.
float_digits – The number of digits of precision when writing floats out.
**params – Additional parameters to pass to json.dumps.
- Returns
JSON string representation of obj.
- Return type
output
- easycv.utils.json_utils.compat_dumps(data, float_digits=- 1)[source]¶
handle json dumps chinese and numpy data :param data python data structure: :param float_digits: The number of digits of precision when writing floats out.
- Returns
- json str, in python2 , the str is encoded with utf8
in python3, the str is unicode type(python3 str)
- easycv.utils.json_utils.PrettyParams(**params)[source]¶
Returns parameters for use with Dump and Dumps to output pretty json.
- Example usage:
`json_str = json_utils.Dumps(obj, **json_utils.PrettyParams())`
```json_str = json_utils.Dumps(obj, **json_utils.PrettyParams(allow_nans=False))```
- Parameters
**params – Additional params to pass to json.dump or json.dumps.
- Returns
- Parameters that are compatible with json_utils.Dump and
json_utils.Dumps.
- Return type
params
easycv.utils.logger module¶
- easycv.utils.logger.get_root_logger(log_file=None, log_level=20)[source]¶
Get the root logger.
The logger will be initialized if it has not been initialized. By default a StreamHandler will be added. If log_file is specified, a FileHandler will also be added. The name of the root logger is the top-level package name, e.g., “easycv”.
- Parameters
log_file (str | None) – The log filename. If specified, a FileHandler will be added to the root logger.
log_level (int) – The root logger level. Note that only the process of rank 0 is affected, while other processes will set the level to “Error” and be silent most of the time.
- Returns
The root logger.
- Return type
logging.Logger
- easycv.utils.logger.print_log(msg, logger=None, level=20)[source]¶
Print a log message.
- Parameters
msg (str) – The message to be logged.
logger (logging.Logger | str | None) – The logger to be used. Some special loggers are: - “root”: the root logger obtained with get_root_logger(). - “silent”: no message will be printed. - None: The print() method will be used to print log messages.
level (int) – Logging level. Only available when logger is a Logger object or “root”.
easycv.utils.metric_distance module¶
easycv.utils.misc module¶
easycv.utils.preprocess_function module¶
- easycv.utils.preprocess_function.bninceptionPre(image, mean=[104, 117, 128], std=[1, 1, 1])[source]¶
- Parameters
image – pytorch Image tensor from PIL (range 0~1), bgr format
mean – norm mean
std – norm val
- Returns
A image norm in 0~255, rgb format
- easycv.utils.preprocess_function.randomErasing(image, probability=0.5, sl=0.02, sh=0.2, r1=0.3, mean=[0.4914, 0.4822, 0.4465])[source]¶
easycv.utils.profiling module¶
easycv.utils.py_util module¶
easycv.utils.registry module¶
- class easycv.utils.registry.Registry(name)[source]¶
Bases:
object
- property name¶
- property module_dict¶
- easycv.utils.registry.build_from_cfg(cfg, registry, default_args=None)[source]¶
Build a module from config dict.
- Parameters
cfg (dict) – Config dict. It should at least contain the key “type”.
registry (
Registry
) – The registry to search the type from.default_args (dict, optional) – Default initialization arguments.
- Returns
The constructed object.
- Return type
obj
easycv.utils.test_util module¶
Contains functions which are convenient for unit testing.
- easycv.utils.test_util.replace_data_for_test(cfg)[source]¶
replace real data with test data
- Parameters
cfg – Config object