Skip to content

Commit

Permalink
[Fix] mmcv.utils -> mmengine.utils (#1295)
Browse files Browse the repository at this point in the history
* [Fix] mmcv.utils -> mmengine.utils

* mmcv -> mmengine
  • Loading branch information
gaotongxiao authored Aug 22, 2022
1 parent 7ac7f66 commit b0b6dad
Show file tree
Hide file tree
Showing 50 changed files with 196 additions and 162 deletions.
4 changes: 2 additions & 2 deletions mmocr/evaluation/metrics/recog_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from difflib import SequenceMatcher
from typing import Dict, Optional, Sequence, Union

import mmcv
import mmengine
from mmengine.evaluator import BaseMetric
from rapidfuzz.distance import Levenshtein

Expand Down Expand Up @@ -45,7 +45,7 @@ def __init__(self,
self.valid_symbol = re.compile(valid_symbol)
if isinstance(mode, str):
mode = [mode]
assert mmcv.is_seq_of(mode, str)
assert mmengine.is_seq_of(mode, str)
assert set(mode).issubset(
{'exact', 'ignore_case', 'ignore_case_symbol'})
self.mode = set(mode)
Expand Down
2 changes: 1 addition & 1 deletion mmocr/models/common/backbones/unet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import torch.nn as nn
import torch.utils.checkpoint as cp
from mmcv.cnn import ConvModule, build_norm_layer
from mmcv.utils.parrots_wrapper import _BatchNorm
from mmengine.model import BaseModule
from mmengine.utils.parrots_wrapper import _BatchNorm

from mmocr.registry import MODELS

Expand Down
6 changes: 3 additions & 3 deletions mmocr/models/textdet/postprocessors/base_postprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from functools import partial
from typing import Dict, List, Optional, Sequence, Tuple, Union

import mmcv
import mmengine
import numpy as np
from torch import Tensor

Expand Down Expand Up @@ -138,10 +138,10 @@ def split_results(
is a tensor, or a list of N lists of tensors if
``pred_results`` is a list of tensors.
"""
assert isinstance(pred_results, Tensor) or mmcv.is_seq_of(
assert isinstance(pred_results, Tensor) or mmengine.is_seq_of(
pred_results, Tensor)

if mmcv.is_seq_of(pred_results, Tensor):
if mmengine.is_seq_of(pred_results, Tensor):
for i in range(1, len(pred_results)):
assert pred_results[0].shape[0] == pred_results[i].shape[0], \
'The first dimension of all tensors should be the same'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import warnings
from typing import Dict, Optional, Sequence, Tuple, Union

import mmcv
import mmengine
import torch
from mmengine.data import LabelData

Expand Down Expand Up @@ -48,7 +48,7 @@ def __init__(self,
'end': self.dictionary.end_idx,
'unknown': self.dictionary.unknown_idx,
}
if not mmcv.is_list_of(ignore_chars, str):
if not mmengine.is_list_of(ignore_chars, str):
raise TypeError('ignore_chars must be list of str')
ignore_indexes = list()
for ignore_char in ignore_chars:
Expand Down
11 changes: 4 additions & 7 deletions mmocr/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Copyright (c) OpenMMLab. All rights reserved.
from mmcv.utils import Registry, build_from_cfg

from .bbox_utils import (bbox2poly, bbox_center_distance, bbox_diag_distance,
bezier2polygon, is_on_same_line, rescale_bboxes,
stitch_boxes_into_lines)
Expand Down Expand Up @@ -28,11 +26,10 @@
RecForwardResults, RecSampleList)

__all__ = [
'Registry', 'build_from_cfg', 'collect_env', 'is_3dlist', 'is_type_list',
'is_none_or_type', 'equal_len', 'is_2dlist', 'valid_boundary',
'list_to_file', 'list_from_file', 'is_on_same_line',
'stitch_boxes_into_lines', 'StringStripper', 'bezier2polygon',
'sort_points', 'dump_ocr_data', 'recog_anno_to_imginfo',
'collect_env', 'is_3dlist', 'is_type_list', 'is_none_or_type', 'equal_len',
'is_2dlist', 'valid_boundary', 'list_to_file', 'list_from_file',
'is_on_same_line', 'stitch_boxes_into_lines', 'StringStripper',
'bezier2polygon', 'sort_points', 'dump_ocr_data', 'recog_anno_to_imginfo',
'rescale_polygons', 'rescale_polygon', 'rescale_bboxes', 'bbox2poly',
'crop_polygon', 'is_poly_inside_rect', 'poly2bbox', 'poly_intersection',
'poly_iou', 'poly_make_valid', 'poly_union', 'poly2shapely',
Expand Down
4 changes: 2 additions & 2 deletions mmocr/utils/collect_env.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) OpenMMLab. All rights reserved.
from mmcv.utils import collect_env as collect_base_env
from mmcv.utils import get_git_hash
from mmengine.utils import collect_env as collect_base_env
from mmengine.utils import get_git_hash

import mmocr

Expand Down
4 changes: 2 additions & 2 deletions mmocr/utils/fileio.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) OpenMMLab. All rights reserved.
import os

import mmcv
import mmengine


def list_to_file(filename, lines):
Expand All @@ -11,7 +11,7 @@ def list_to_file(filename, lines):
filename (str): The output filename. It will be created/overwritten.
lines (list(str)): Data to be written.
"""
mmcv.mkdir_or_exist(os.path.dirname(filename))
mmengine.mkdir_or_exist(os.path.dirname(filename))
with open(filename, 'w', encoding='utf-8') as fw:
for line in lines:
fw.write(f'{line}\n')
Expand Down
2 changes: 1 addition & 1 deletion mmocr/utils/img_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) OpenMMLab. All rights reserved.
import cv2
import numpy as np
from mmcv.utils import is_seq_of
from mmengine.utils import is_seq_of
from shapely.geometry import LineString, Point

from .bbox_utils import bbox_jitter
Expand Down
4 changes: 2 additions & 2 deletions tools/analysis_tools/browse_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import argparse
import os.path as osp

import mmcv
import mmengine
from mmengine import Config, DictAction

from mmocr.registry import DATASETS, VISUALIZERS
Expand Down Expand Up @@ -50,7 +50,7 @@ def main():
dataset = DATASETS.build(cfg.train_dataloader.dataset)
visualizer = VISUALIZERS.build(cfg.visualizer)

progress_bar = mmcv.ProgressBar(len(dataset))
progress_bar = mmengine.ProgressBar(len(dataset))
for item in dataset:
img = item['inputs'].permute(1, 2, 0).numpy()
data_sample = item['data_sample'].numpy()
Expand Down
11 changes: 5 additions & 6 deletions tools/dataset_converters/common/curvedsyntext_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import os.path as osp
from functools import partial

import mmcv
import mmengine
import numpy as np

Expand Down Expand Up @@ -84,14 +83,14 @@ def convert_annotations(data,
start_img_id=start_img_id,
start_ann_id=start_ann_id)
if nproc > 1:
data['annotations'] = mmcv.track_parallel_progress(
data['annotations'] = mmengine.track_parallel_progress(
modify_annotation_with_params, data['annotations'], nproc=nproc)
data['images'] = mmcv.track_parallel_progress(
data['images'] = mmengine.track_parallel_progress(
modify_image_info_with_params, data['images'], nproc=nproc)
else:
data['annotations'] = mmcv.track_progress(
data['annotations'] = mmengine.track_progress(
modify_annotation_with_params, data['annotations'])
data['images'] = mmcv.track_progress(
data['images'] = mmengine.track_progress(
modify_image_info_with_params,
data['images'],
)
Expand All @@ -103,7 +102,7 @@ def main():
args = parse_args()
root_path = args.root_path
out_dir = args.out_dir if args.out_dir else root_path
mmcv.mkdir_or_exist(out_dir)
mmengine.mkdir_or_exist(out_dir)

anns = mmengine.load(osp.join(root_path, 'train1.json'))
data1 = convert_annotations(anns, 'syntext_word_eng', args.num_sample,
Expand Down
12 changes: 6 additions & 6 deletions tools/dataset_converters/common/labelme_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def parse_labelme_json(json_file,
src_img = mmcv.imread(img_full_path)
img_basename = osp.splitext(img_file)[0]
sub_dir = osp.join(out_dir, 'crops', img_basename)
mmcv.mkdir_or_exist(sub_dir)
mmengine.mkdir_or_exist(sub_dir)

det_line_json_list = []
recog_crop_line_str_list = []
Expand Down Expand Up @@ -143,7 +143,7 @@ def process(json_dir,
nproc=1,
recog_format='jsonl',
warp=False):
mmcv.mkdir_or_exist(out_dir)
mmengine.mkdir_or_exist(out_dir)

json_file_list = glob.glob(osp.join(json_dir, '*.json'))

Expand All @@ -156,10 +156,10 @@ def process(json_dir,
warp_flag=warp)

if nproc <= 1:
total_results = mmcv.track_progress(parse_labelme_json_func,
json_file_list)
total_results = mmengine.track_progress(parse_labelme_json_func,
json_file_list)
else:
total_results = mmcv.track_parallel_progress(
total_results = mmengine.track_parallel_progress(
parse_labelme_json_func,
json_file_list,
keep_order=True,
Expand All @@ -174,7 +174,7 @@ def process(json_dir,
total_recog_crop_line_str.extend(res[1])
total_recog_warp_line_str.extend(res[2])

mmcv.mkdir_or_exist(out_dir)
mmengine.mkdir_or_exist(out_dir)
det_out_file = osp.join(out_dir, 'instances_training.txt')
list_to_file(det_out_file, total_det_line_json_list)

Expand Down
4 changes: 2 additions & 2 deletions tools/dataset_converters/kie/closeset_to_openset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
from functools import partial

import mmcv
import mmengine

from mmocr.utils import list_from_file, list_to_file

Expand Down Expand Up @@ -89,7 +89,7 @@ def process(closeset_file, openset_file, merge_bg_others=False, n_proc=10):

convert_func = partial(convert, merge_bg_others=merge_bg_others)

openset_lines = mmcv.track_parallel_progress(
openset_lines = mmengine.track_parallel_progress(
convert_func, closeset_lines, nproc=n_proc)

list_to_file(openset_file, openset_lines)
Expand Down
7 changes: 4 additions & 3 deletions tools/dataset_converters/textdet/bid_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os.path as osp

import mmcv
import mmengine

from mmocr.utils import dump_ocr_data

Expand Down Expand Up @@ -50,10 +51,10 @@ def collect_annotations(files, nproc=1):
assert isinstance(nproc, int)

if nproc > 1:
images = mmcv.track_parallel_progress(
images = mmengine.track_parallel_progress(
load_img_info, files, nproc=nproc)
else:
images = mmcv.track_progress(load_img_info, files)
images = mmengine.track_progress(load_img_info, files)

return images

Expand Down Expand Up @@ -164,7 +165,7 @@ def parse_args():
def main():
args = parse_args()
root_path = args.root_path
with mmcv.Timer(print_tmpl='It takes {}s to convert BID annotation'):
with mmengine.Timer(print_tmpl='It takes {}s to convert BID annotation'):
files = collect_files(
osp.join(root_path, 'imgs'), osp.join(root_path, 'annotations'))
image_infos = collect_annotations(files, nproc=args.nproc)
Expand Down
10 changes: 6 additions & 4 deletions tools/dataset_converters/textdet/ctw1500_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from functools import partial

import mmcv
import mmengine
import numpy as np
from shapely.geometry import Polygon

Expand Down Expand Up @@ -72,10 +73,10 @@ def collect_annotations(files, split, nproc=1):

load_img_info_with_split = partial(load_img_info, split=split)
if nproc > 1:
images = mmcv.track_parallel_progress(
images = mmengine.track_parallel_progress(
load_img_info_with_split, files, nproc=nproc)
else:
images = mmcv.track_progress(load_img_info_with_split, files)
images = mmengine.track_progress(load_img_info_with_split, files)

return images

Expand Down Expand Up @@ -208,7 +209,7 @@ def main():
args = parse_args()
root_path = args.root_path
out_dir = args.out_dir if args.out_dir else root_path
mmcv.mkdir_or_exist(out_dir)
mmengine.mkdir_or_exist(out_dir)

img_dir = osp.join(root_path, 'imgs')
gt_dir = osp.join(root_path, 'annotations')
Expand All @@ -220,7 +221,8 @@ def main():

for split, json_name in set_name.items():
print(f'Converting {split} into {json_name}')
with mmcv.Timer(print_tmpl='It takes {}s to convert icdar annotation'):
with mmengine.Timer(
print_tmpl='It takes {}s to convert icdar annotation'):
files = collect_files(
osp.join(img_dir, split), osp.join(gt_dir, split), split)
image_infos = collect_annotations(files, split, nproc=args.nproc)
Expand Down
7 changes: 4 additions & 3 deletions tools/dataset_converters/textdet/detext_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os.path as osp

import mmcv
import mmengine
import numpy as np

from mmocr.utils import dump_ocr_data
Expand Down Expand Up @@ -50,10 +51,10 @@ def collect_annotations(files, nproc=1):
assert isinstance(nproc, int)

if nproc > 1:
images = mmcv.track_parallel_progress(
images = mmengine.track_parallel_progress(
load_img_info, files, nproc=nproc)
else:
images = mmcv.track_progress(load_img_info, files)
images = mmengine.track_progress(load_img_info, files)

return images

Expand Down Expand Up @@ -146,7 +147,7 @@ def main():

for split in ['training', 'val']:
print(f'Processing {split} set...')
with mmcv.Timer(
with mmengine.Timer(
print_tmpl='It takes {}s to convert DeText annotation'):
files = collect_files(
osp.join(root_path, 'imgs', split),
Expand Down
7 changes: 4 additions & 3 deletions tools/dataset_converters/textdet/funsd_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ def collect_annotations(files, nproc=1):
assert isinstance(nproc, int)

if nproc > 1:
images = mmcv.track_parallel_progress(
images = mmengine.track_parallel_progress(
load_img_info, files, nproc=nproc)
else:
images = mmcv.track_progress(load_img_info, files)
images = mmengine.track_progress(load_img_info, files)

return images

Expand Down Expand Up @@ -144,7 +144,8 @@ def main():

for split in ['training', 'test']:
print(f'Processing {split} set...')
with mmcv.Timer(print_tmpl='It takes {}s to convert FUNSD annotation'):
with mmengine.Timer(
print_tmpl='It takes {}s to convert FUNSD annotation'):
files = collect_files(
osp.join(root_path, 'imgs'),
osp.join(root_path, 'annotations', split))
Expand Down
7 changes: 4 additions & 3 deletions tools/dataset_converters/textdet/ic11_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os.path as osp

import mmcv
import mmengine
from PIL import Image

from mmocr.utils import dump_ocr_data
Expand Down Expand Up @@ -68,10 +69,10 @@ def collect_annotations(files, nproc=1):
assert isinstance(nproc, int)

if nproc > 1:
images = mmcv.track_parallel_progress(
images = mmengine.track_parallel_progress(
load_img_info, files, nproc=nproc)
else:
images = mmcv.track_progress(load_img_info, files)
images = mmengine.track_progress(load_img_info, files)

return images

Expand Down Expand Up @@ -158,7 +159,7 @@ def main():

for split in ['training', 'test']:
print(f'Processing {split} set...')
with mmcv.Timer(print_tmpl='It takes {}s to convert annotation'):
with mmengine.Timer(print_tmpl='It takes {}s to convert annotation'):
files = collect_files(
osp.join(root_path, 'imgs', split),
osp.join(root_path, 'annotations', split))
Expand Down
Loading

0 comments on commit b0b6dad

Please sign in to comment.