Skip to content

Commit 7b4b755

Browse files
committed
add new pipeline
1 parent 7971ee0 commit 7b4b755

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2972
-2
lines changed

configs/cls/mobilenetv3/cls_mv3.yaml

+44
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,47 @@ eval:
142142
drop_remainder: False
143143
max_rowsize: 12
144144
num_workers: 8
145+
146+
predict:
147+
backend: MindSpore
148+
deive_target: Ascend
149+
device_id: 0
150+
max_device_memory: 8GB
151+
amp_level: O0
152+
mode: 0
153+
ckpt_load_path: /root/.mindspore/models/cls_mobilenetv3-92db9c58.ckpt
154+
dataset_sink_mode: False
155+
dataset:
156+
type: RecDataset
157+
dataset_root: dir/to/dataset
158+
data_dir: all_images
159+
label_file: val_cls_gt.txt
160+
sample_ratio: 1.0
161+
shuffle: False
162+
transform_pipeline:
163+
- DecodeImage:
164+
img_mode: BGR
165+
to_float32: False
166+
- Rotate90IfVertical:
167+
threshold: 2.0
168+
direction: counterclockwise
169+
- RecResizeImg:
170+
image_shape: [48, 192] # H, W
171+
padding: False # aspect ratio will be preserved if true.
172+
- NormalizeImage:
173+
bgr_to_rgb: True
174+
is_hwc: True
175+
mean : [127.0, 127.0, 127.0]
176+
std : [127.0, 127.0, 127.0]
177+
- ToCHWImage:
178+
# the order of the dataloader list, matching the network input and the input labels for the loss function, and optional data for debug/visaulize
179+
output_columns: ['image', 'label'] # TODO return text string padding w/ fixed length, and a scaler to indicate the length
180+
net_input_column_index: [0] # input indices for network forward func in output_columns
181+
label_column_index: [1] # input indices marked as label
182+
183+
loader:
184+
shuffle: False
185+
batch_size: 8
186+
drop_remainder: False
187+
max_rowsize: 12
188+
num_workers: 8

configs/det/dbnet/db_r50_icdar15.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,13 @@ eval:
157157
num_workers: 2
158158

159159
predict:
160-
ckpt_load_path: tmp_det/best.ckpt
160+
backend: MindSpore
161+
deive_target: Ascend
162+
device_id: 0
163+
max_device_memory: 8GB
164+
amp_level: O0
165+
mode: 0
166+
ckpt_load_path: /root/.mindspore/models/dbnet_resnet50-c3a4aa24.ckpt
161167
output_save_dir: ./output
162168
dataset_sink_mode: False
163169
dataset:

configs/rec/crnn/crnn_resnet34.yaml

+7-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,13 @@ eval:
150150
num_workers: 8
151151

152152
predict:
153-
ckpt_load_path: ./tmp_rec/best.ckpt
153+
backend: MindSpore
154+
deive_target: Ascend
155+
device_id: 0
156+
max_device_memory: 8GB
157+
amp_level: O3
158+
mode: 0
159+
ckpt_load_path: /root/.mindspore/models/crnn_resnet34-83f37f07.ckpt
154160
vis_font_path: tools/utils/simfang.ttf
155161
dataset_sink_mode: False
156162
dataset:
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .cls_infer_node import ClsInferNode
2+
from .cls_post_node import ClsPostNode
3+
from .cls_pre_node import ClsPreNode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import logging
2+
import os
3+
import time
4+
import sys
5+
import numpy as np
6+
import yaml
7+
from addict import Dict
8+
from typing import List
9+
10+
__dir__ = os.path.dirname(os.path.abspath(__file__))
11+
sys.path.insert(0, os.path.abspath(os.path.join(__dir__, "../../")))
12+
13+
from tools.infer.text.utils import get_ckpt_file
14+
from mindocr.data.transforms import create_transforms, run_transforms
15+
from mindocr.postprocess import build_postprocess
16+
from mindocr.infer.utils.model import MSModel, LiteModel
17+
18+
19+
algo_to_model_name = {
20+
"MV3": "cls_mobilenet_v3_small_100_model",
21+
}
22+
logger = logging.getLogger("mindocr")
23+
24+
class ClsPreprocess(object):
25+
def __init__(self, args):
26+
self.args = args
27+
with open(args.cls_model_name_or_config, "r") as f:
28+
self.yaml_cfg = Dict(yaml.safe_load(f))
29+
self.transforms = create_transforms(self.yaml_cfg.predict.dataset.transform_pipeline)
30+
31+
def __call__(self, img):
32+
print(img)
33+
data = {"image": img}
34+
data = run_transforms(data, self.transforms[1:])
35+
return data
36+
37+
38+
class ClsModelMS(MSModel):
39+
def __init__(self, args):
40+
self.args = args
41+
self.model_name = algo_to_model_name[args.cls_algorithm]
42+
self.config_path = args.cls_config_path
43+
self._init_model(self.model_name, self.config_path)
44+
45+
46+
class ClsModelLite(LiteModel):
47+
def __init__(self, args):
48+
self.args = args
49+
self.model_name = algo_to_model_name[args.cls_algorithm]
50+
self.config_path = args.cls_config_path
51+
self._init_model(self.model_name, self.config_path)
52+
53+
INFER_CLS_MAP = {"MindSporeLite": ClsModelLite, "MindSpore": ClsModelMS}
54+
55+
class ClsPostprocess(object):
56+
def __init__(self, args):
57+
self.args = args
58+
with open(args.cls_model_name_or_config, "r") as f:
59+
self.yaml_cfg = Dict(yaml.safe_load(f))
60+
self.postprocessor = build_postprocess(self.yaml_cfg.postprocess)
61+
62+
def __call__(self, pred):
63+
return self.postprocessor(pred)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os
2+
import time
3+
import sys
4+
import numpy as np
5+
import yaml
6+
from addict import Dict
7+
8+
__dir__ = os.path.dirname(os.path.abspath(__file__))
9+
sys.path.insert(0, os.path.abspath(os.path.join(__dir__, "../../")))
10+
11+
from pipeline.framework.module_base import ModuleBase
12+
from pipeline.tasks import TaskType
13+
from .classification import INFER_CLS_MAP
14+
15+
16+
class ClsInferNode(ModuleBase):
17+
def __init__(self, args, msg_queue, tqdm_info):
18+
super(ClsInferNode, self).__init__(args, msg_queue, tqdm_info)
19+
self.args = args
20+
self.cls_model = None
21+
self.task_type = self.args.task_type
22+
23+
def init_self_args(self):
24+
with open(self.args.cls_model_name_or_config, "r") as f:
25+
self.yaml_cfg = Dict(yaml.safe_load(f))
26+
self.batch_size = self.yaml_cfg.predict.loader.batch_size
27+
ClsModel = INFER_CLS_MAP[self.yaml_cfg.predict.backend]
28+
self.cls_model = ClsModel(self.args)
29+
super().init_self_args()
30+
31+
def process(self, input_data):
32+
"""
33+
Input:
34+
- input_data.data: [np.ndarray], shape:[3,w,h], e.g. [3,48,192]
35+
Output:
36+
- input_data.data: [np.ndarray], shape:[?,2]
37+
"""
38+
if input_data.skip:
39+
self.send_to_next_module(input_data)
40+
return
41+
42+
data = input_data.data
43+
data = [np.expand_dims(d, 0) for d in data if len(d.shape) == 3]
44+
data = np.concatenate(data, axis=0)
45+
46+
preds = []
47+
for batch_i in range(data.shape[0] // self.batch_size + 1):
48+
start_i = batch_i * self.batch_size
49+
end_i = (batch_i + 1) * self.batch_size
50+
d = data[start_i:end_i]
51+
if d.shape[0] == 0:
52+
continue
53+
pred = self.cls_model([d])
54+
preds.append(pred[0])
55+
preds = np.concatenate(preds, axis=0)
56+
input_data.data = {"pred": preds}
57+
self.send_to_next_module(input_data)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
import time
3+
import sys
4+
import numpy as np
5+
import yaml
6+
from addict import Dict
7+
8+
__dir__ = os.path.dirname(os.path.abspath(__file__))
9+
sys.path.insert(0, os.path.abspath(os.path.join(__dir__, "../../")))
10+
11+
import cv2
12+
import numpy as np
13+
14+
from pipeline.framework.module_base import ModuleBase
15+
from pipeline.tasks import TaskType
16+
from .classification import ClsPostprocess
17+
from tools.infer.text.utils import crop_text_region
18+
from pipeline.data_process.utils.cv_utils import crop_box_from_image
19+
20+
21+
class ClsPostNode(ModuleBase):
22+
def __init__(self, args, msg_queue, tqdm_info):
23+
super(ClsPostNode, self).__init__(args, msg_queue, tqdm_info)
24+
self.cls_postprocess = ClsPostprocess(args)
25+
self.task_type = self.args.task_type
26+
self.cls_thresh = 0.9
27+
28+
def init_self_args(self):
29+
super().init_self_args()
30+
31+
def process(self, input_data):
32+
"""
33+
Input:
34+
- input_data.data: [np.ndarray], shape:[?,2]
35+
Output:
36+
- input_data.sub_image_list: [np.ndarray], shape:[1,3,-1,-1], e.g. [1,3,48,192]
37+
- input_data.data = None
38+
or
39+
- input_data.infer_result = [(str, float)]
40+
"""
41+
if input_data.skip:
42+
self.send_to_next_module(input_data)
43+
return
44+
45+
data = input_data.data
46+
pred = data["pred"]
47+
output = self.cls_postprocess(pred)
48+
angles = output["angles"]
49+
scores = np.array(output["scores"]).tolist()
50+
51+
batch = input_data.sub_image_size
52+
if self.task_type.value == TaskType.DET_CLS_REC.value:
53+
sub_images = input_data.sub_image_list
54+
for i in range(batch):
55+
angle, score = angles[i], scores[i]
56+
if "180" == angle and score > self.cls_thresh:
57+
sub_images[i] = cv2.rotate(sub_images[i], cv2.ROTATE_180)
58+
input_data.sub_image_list = sub_images
59+
else:
60+
input_data.infer_result = [(angle, score) for angle, score in zip(angles, scores)]
61+
62+
input_data.data = None
63+
self.send_to_next_module(input_data)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import argparse
2+
import os
3+
import time
4+
import sys
5+
import numpy as np
6+
7+
__dir__ = os.path.dirname(os.path.abspath(__file__))
8+
sys.path.insert(0, os.path.abspath(os.path.join(__dir__, "../../")))
9+
10+
from pipeline.framework.module_base import ModuleBase
11+
from pipeline.tasks import TaskType
12+
from .classification import ClsPreprocess
13+
14+
15+
class ClsPreNode(ModuleBase):
16+
def __init__(self, args, msg_queue, tqdm_info):
17+
super(ClsPreNode, self).__init__(args, msg_queue, tqdm_info)
18+
self.cls_preprocesser = ClsPreprocess(args)
19+
self.task_type = self.args.task_type
20+
21+
def init_self_args(self):
22+
super().init_self_args()
23+
return {"batch_size": 1}
24+
25+
def process(self, input_data):
26+
if input_data.skip:
27+
self.send_to_next_module(input_data)
28+
return
29+
30+
if self.task_type.value == TaskType.REC.value:
31+
image = input_data.frame[0]
32+
data = [self.cls_preprocesser(image)["image"]]
33+
input_data.sub_image_size = 1
34+
input_data.data = data
35+
self.send_to_next_module(input_data)
36+
else:
37+
sub_image_list = input_data.sub_image_list
38+
data = [self.cls_preprocesser(split_image)["image"] for split_image in sub_image_list]
39+
input_data.data = data
40+
self.send_to_next_module(input_data)

mindocr/infer/common/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .collect_node import CollectNode
2+
from .decode_node import DecodeNode
3+
from .handout_node import HandoutNode

0 commit comments

Comments
 (0)