Skip to content

Commit fb94dee

Browse files
[cherry-pick] qinspector update seg to 2.8 (#3159)
* [feature Industry Inspect] * [add qinspector repo] * [update readme] * [del iinsepect] * [update readme] * Update mvtec.py * Update predict.py * Update train.py * Update val.py * Merge branch 'IInspection' of github.com:Sunting78/PaddleSeg into IInspection * Update train.py * [update readme] * [update post] * update read * update readme * update readme * update readme * update readme * update readme * update readme * update readme * update readme * update readme * update bbox * update predict * update args * Update README.md * [cherry-pick] qinspector * update quickstart * update quickstart * update image * update image * update data.md * [update v28seg] --------- Co-authored-by: zhaofefe <61047836+zhaoyantao-murray@users.noreply.github.com>
1 parent 2f60bed commit fb94dee

File tree

11 files changed

+236
-408
lines changed

11 files changed

+236
-408
lines changed

contrib/QualityInspector/configs/seg/_base_/mtile_dataset.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ iters: 80000
33

44
train_dataset:
55
type: Dataset
6+
num_classes: 6
67
dataset_root: ./
78
train_path: dataset/MT_dataset/train.txt
89
transforms:
@@ -22,6 +23,7 @@ train_dataset:
2223

2324
val_dataset:
2425
type: Dataset
26+
num_classes: 6
2527
dataset_root: ./
2628
val_path: dataset/MT_dataset/val.txt
2729
transforms:

contrib/QualityInspector/configs/seg/ocrnet/ocrnet_hrnetw18_RoI_defect_256x256_40k.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ iters: 1000
44
train_dataset:
55
type: Dataset
66
dataset_root: ./
7+
num_classes: 2
78
train_path: dataset/MT_dataset/RoI/train/RoI.txt
89
transforms:
910
- type: ResizeStepScaling
@@ -24,6 +25,7 @@ train_dataset:
2425
val_dataset:
2526
type: Dataset
2627
dataset_root: ./
28+
num_classes: 2
2729
val_path: dataset/MT_dataset/RoI/val/RoI.txt
2830
transforms:
2931
- type: ResizeByLong

contrib/QualityInspector/configs/seg/ocrnet/ocrnet_hrnetw18_defect_256x256_40k.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ iters: 40000
44

55
train_dataset:
66
type: Dataset
7+
num_classes: 6
78
dataset_root: ./
89
train_path: dataset/MT_dataset/train.txt
910
transforms:
@@ -24,6 +25,7 @@ train_dataset:
2425
mode: train
2526

2627
val_dataset:
28+
num_classes: 6
2729
type: Dataset
2830
dataset_root: ./
2931
val_path: dataset/MT_dataset/val.txt

contrib/QualityInspector/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ numpy<1.24
22
lap==0.4.0
33
paddlepaddle-gpu>=2.3.0
44
paddledet==2.6.0
5-
paddleseg==2.7.0
5+
paddleseg==2.8.0
66
visualdl>=2.2.0
77
scipy
88
opencv-python <= 4.6.0

contrib/QualityInspector/tools/end2end/eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def main():
326326
gt_data, img_to_pred_annos, post_modules, args.image_root,
327327
args.instance_level, args.iou_theshold)
328328

329-
if args.badcase:
329+
if args.save_badcase:
330330
show_badcase(gt_data, img_to_pred_annos, overkill_info,
331331
escape_info_image, escape_info_instance, args.image_root,
332332
args.output_dir)

contrib/QualityInspector/tools/seg/export.py

Lines changed: 60 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -18,119 +18,90 @@
1818
import paddle
1919
import yaml
2020

21-
from paddleseg.cvlibs import Config
22-
from paddleseg.utils import logger
21+
from paddleseg.cvlibs import Config, SegBuilder
22+
from paddleseg.utils import logger, utils
23+
from paddleseg.deploy.export import WrappedModel
2324

2425

2526
def parse_args():
26-
parser = argparse.ArgumentParser(description='Model export.')
27+
parser = argparse.ArgumentParser(description='Export Inference Model.')
28+
parser.add_argument("--config", help="The path of config file.", type=str)
2729
parser.add_argument(
28-
"--config", help="The config file.", type=str, required=True)
29-
parser.add_argument(
30-
'--model_path', help='The path of model for export', type=str)
30+
'--model_path',
31+
help='The path of trained weights for exporting inference model',
32+
type=str)
3133
parser.add_argument(
3234
'--save_dir',
33-
help='The directory for saving the exported model',
35+
help='The directory for saving the exported inference model',
3436
type=str,
3537
default='./output/inference_model')
36-
parser.add_argument(
37-
'--output_op',
38-
choices=['argmax', 'softmax', 'none'],
39-
default="argmax",
40-
help="Select which op to be appended to output result, default: argmax")
41-
parser.add_argument(
42-
'--without_argmax',
43-
help='Do not add the argmax operation at the end of the network. [Deprecated]',
44-
action='store_true')
45-
parser.add_argument(
46-
'--with_softmax',
47-
help='Add the softmax operation at the end of the network. [Deprecated]',
48-
action='store_true')
4938
parser.add_argument(
5039
"--input_shape",
5140
nargs='+',
52-
help="Export the model with fixed input shape, such as 1 3 1024 1024.",
41+
help="Export the model with fixed input shape, e.g., `--input_shape 1 3 1024 1024`.",
5342
type=int,
5443
default=None)
44+
parser.add_argument(
45+
'--output_op',
46+
choices=['argmax', 'softmax', 'none'],
47+
default="argmax",
48+
help="Select the op to be appended to the last of inference model, default: argmax."
49+
"In PaddleSeg, the output of trained model is logit (H*C*H*W). We can apply argmax and"
50+
"softmax op to the logit according the actual situation.")
5551

5652
return parser.parse_args()
5753

5854

59-
class SavedSegmentationNet(paddle.nn.Layer):
60-
def __init__(self, net, output_op):
61-
super().__init__()
62-
self.net = net
63-
self.output_op = output_op
64-
assert output_op in ['argmax', 'softmax'], \
65-
"output_op should in ['argmax', 'softmax']"
66-
67-
def forward(self, x):
68-
outs = self.net(x)
69-
70-
new_outs = []
71-
for out in outs:
72-
if self.output_op == 'argmax':
73-
out = paddle.argmax(out, axis=1, dtype='int32')
74-
elif self.output_op == 'softmax':
75-
out = paddle.nn.functional.softmax(out, axis=1)
76-
new_outs.append(out)
77-
return new_outs
78-
79-
8055
def main(args):
81-
os.environ['PADDLESEG_EXPORT_STAGE'] = 'True'
56+
assert args.config is not None, \
57+
'No configuration file specified, please set --config'
8258
cfg = Config(args.config)
83-
cfg.check_sync_info()
84-
net = cfg.model
59+
builder = SegBuilder(cfg)
60+
61+
utils.show_env_info()
62+
utils.show_cfg_info(cfg)
63+
os.environ['PADDLESEG_EXPORT_STAGE'] = 'True'
8564

65+
# save model
66+
model = builder.model
8667
if args.model_path is not None:
87-
para_state_dict = paddle.load(args.model_path)
88-
net.set_dict(para_state_dict)
89-
logger.info('Loaded trained params of model successfully.')
90-
91-
if args.input_shape is None:
92-
shape = [None, 3, None, None]
93-
else:
94-
shape = args.input_shape
95-
96-
output_op = args.output_op
97-
if args.without_argmax:
98-
logger.warning(
99-
'--without_argmax will be deprecated, please use --output_op')
100-
output_op = 'none'
101-
if args.with_softmax:
102-
logger.warning(
103-
'--with_softmax will be deprecated, please use --output_op')
104-
output_op = 'softmax'
105-
106-
new_net = net if output_op == 'none' else SavedSegmentationNet(net,
107-
output_op)
108-
new_net.eval()
109-
new_net = paddle.jit.to_static(
110-
new_net,
111-
input_spec=[paddle.static.InputSpec(
112-
shape=shape, dtype='float32')])
113-
114-
save_path = os.path.join(args.save_dir, 'model')
115-
paddle.jit.save(new_net, save_path)
68+
state_dict = paddle.load(args.model_path)
69+
model.set_dict(state_dict)
70+
logger.info('Loaded trained params successfully.')
71+
if args.output_op != 'none':
72+
model = WrappedModel(model, args.output_op)
73+
74+
shape = [None, 3, None, None] if args.input_shape is None \
75+
else args.input_shape
76+
input_spec = [paddle.static.InputSpec(shape=shape, dtype='float32')]
77+
model.eval()
78+
model = paddle.jit.to_static(model, input_spec=input_spec)
79+
paddle.jit.save(model, os.path.join(args.save_dir, 'model'))
80+
81+
# save deploy.yaml
82+
val_dataset_cfg = cfg.val_dataset_cfg
83+
assert val_dataset_cfg != {}, 'No val_dataset specified in the configuration file.'
84+
transforms = val_dataset_cfg.get('transforms', None)
85+
output_dtype = 'int32' if args.output_op == 'argmax' else 'float32'
86+
87+
# TODO add test config
88+
deploy_info = {
89+
'Deploy': {
90+
'model': 'model.pdmodel',
91+
'params': 'model.pdiparams',
92+
'transforms': transforms,
93+
'input_shape': shape,
94+
'output_op': args.output_op,
95+
'output_dtype': output_dtype
96+
}
97+
}
98+
msg = '\n---------------Deploy Information---------------\n'
99+
msg += str(yaml.dump(deploy_info))
100+
logger.info(msg)
116101

117102
yml_file = os.path.join(args.save_dir, 'deploy.yaml')
118103
with open(yml_file, 'w') as file:
119-
transforms = cfg.export_config.get('transforms', [{
120-
'type': 'Normalize'
121-
}])
122-
output_dtype = 'int32' if output_op == 'argmax' else 'float32'
123-
data = {
124-
'Deploy': {
125-
'model': 'model.pdmodel',
126-
'params': 'model.pdiparams',
127-
'transforms': transforms,
128-
'input_shape': shape,
129-
'output_op': output_op,
130-
'output_dtype': output_dtype
131-
}
132-
}
133-
yaml.dump(data, file)
104+
yaml.dump(deploy_info, file)
134105

135106
logger.info(f'The inference model is saved in {args.save_dir}')
136107

contrib/QualityInspector/tools/seg/model/analyze_model.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ def analyze(args):
140140
paddle.set_device('cpu')
141141

142142
cfg = Config(args.config)
143-
cfg.check_sync_info()
144143

145144
custom_ops = {paddle.nn.SyncBatchNorm: op_flops_funs.count_syncbn}
146145
inputs = paddle.randn(args.input_shape)

contrib/QualityInspector/tools/seg/model/export_onnx.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import onnx
3737
import onnxruntime
3838

39-
from paddleseg.cvlibs import Config
39+
from paddleseg.cvlibs import Config, SegBuilder
4040
from paddleseg.utils import logger, utils
4141

4242

@@ -80,9 +80,11 @@ def check_and_run_onnx(onnx_model_path, input_data):
8080

8181

8282
def export_onnx(args):
83+
assert args.config is not None, \
84+
'Please set --config path/to/yml'
8385
cfg = Config(args.config)
84-
cfg.check_sync_info()
85-
model = cfg.model
86+
builder = SegBuilder(cfg)
87+
model = builder.model
8688
if args.model_path is not None:
8789
utils.load_entire_model(model, args.model_path)
8890
logger.info('Loaded trained params of model successfully')

0 commit comments

Comments
 (0)