warpctc: replace BasicLSTM with DYNAMICRNN

maskrcnn: add export and disable save_graphs
This commit is contained in:
gengdongjie 2020-09-29 16:39:42 +08:00
parent f0e9f6137b
commit 3aad1b5dfe
8 changed files with 176 additions and 39 deletions

View File

@ -0,0 +1,47 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""export checkpoint file into air models"""
import argparse
import numpy as np
from mindspore import Tensor, context
from mindspore.train.serialization import load_checkpoint, load_param_into_net, export
from src.maskrcnn.mask_rcnn_r50 import Mask_Rcnn_Resnet50
from src.config import config
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='maskrcnn_export')
parser.add_argument('--ckpt_file', type=str, default='', help='maskrcnn ckpt file.')
parser.add_argument('--output_file', type=str, default='', help='maskrcnn output air name.')
args_opt = parser.parse_args()
net = Mask_Rcnn_Resnet50(config=config)
param_dict = load_checkpoint(args_opt.ckpt_file)
load_param_into_net(net, param_dict)
net.set_train(False)
bs = config.test_batch_size
img = Tensor(np.zeros([bs, 3, 768, 1280], np.float16))
img_metas = Tensor(np.zeros([bs, 4], np.float16))
gt_bboxes = Tensor(np.zeros([bs, 128, 4], np.float16))
gt_labels = Tensor(np.zeros([bs, 128], np.int32))
gt_num = Tensor(np.zeros([bs, 128], np.bool))
gt_mask = Tensor(np.zeros([bs, 128], np.bool))
export(net, img, img_metas, gt_bboxes, gt_labels, gt_num, gt_mask, file_name=args_opt.output_file,
file_format="AIR")

View File

@ -16,13 +16,11 @@
import numpy as np
import mindspore.nn as nn
from mindspore import context
from mindspore.ops import operations as P
from mindspore.common.tensor import Tensor
from mindspore.common import dtype as mstype
from mindspore.common.initializer import initializer
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=True)
def bias_init_zeros(shape):
"""Bias init method."""

View File

@ -19,10 +19,6 @@ import mindspore.nn as nn
import mindspore.common.dtype as mstype
from mindspore.ops import operations as P
from mindspore import Tensor
from mindspore import context
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=True)
class Proposal(nn.Cell):

View File

@ -19,10 +19,6 @@ import mindspore.nn as nn
from mindspore.ops import operations as P
from mindspore.common.tensor import Tensor
from mindspore.ops import functional as F
from mindspore import context
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend", save_graphs=True)
def weight_init_ones(shape):

View File

@ -0,0 +1,59 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""
##############export checkpoint file into air and onnx models#################
python export.py
"""
import argparse
import numpy as np
from mindspore import Tensor
from mindspore.train.serialization import load_checkpoint, load_param_into_net, export
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='resnet export')
parser.add_argument('--network_dataset', type=str, default='resnet50_cifar10', choices=['resnet50_cifar10',
'resnet50_imagenet2012',
'resnet101_imagenet2012',
"se-resnet50_imagenet2012"],
help='network and dataset name.')
parser.add_argument('--ckpt_file', type=str, default='', help='resnet ckpt file.')
parser.add_argument('--output_file', type=str, default='', help='resnet output air name.')
args_opt = parser.parse_args()
if args_opt.network_dataset == 'resnet50_cifar10':
from src.config import config1 as config
from src.resnet import resnet50 as resnet
elif args_opt.network_dataset == 'resnet50_imagenet2012':
from src.config import config2 as config
from src.resnet import resnet50 as resnet
elif args_opt.network_dataset == 'resnet101_imagenet2012':
from src.config import config3 as config
from src.resnet import resnet101 as resnet
elif args_opt.network_dataset == 'se-resnet50_imagenet2012':
from src.config import config4 as config
from src.resnet import se_resnet50 as resnet
else:
raise ValueError("network and dataset is not support.")
net = resnet(config.class_num)
assert args_opt.ckpt_file is not None, "checkpoint_path is None."
param_dict = load_checkpoint(args_opt.ckpt_file)
load_param_into_net(net, param_dict)
input_arr = Tensor(np.zeros([1, 3, 224, 224], np.float32))
export(net, input_arr, file_name=args_opt.output_file, file_format="AIR")

View File

@ -0,0 +1,43 @@
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""export checkpoint file into air models"""
import argparse
import numpy as np
from mindspore import Tensor, context
from mindspore.train.serialization import load_checkpoint, load_param_into_net, export
from src.warpctc import StackedRNN
from src.config import config
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='warpctc_export')
parser.add_argument('--ckpt_file', type=str, default='', help='warpctc ckpt file.')
parser.add_argument('--output_file', type=str, default='', help='warpctc output air name.')
args_opt = parser.parse_args()
captcha_width = config.captcha_width
captcha_height = config.captcha_height
batch_size = config.batch_size
hidden_size = config.hidden_size
net = StackedRNN(captcha_height * 3, batch_size, hidden_size)
param_dict = load_checkpoint(args_opt.ckpt_file)
load_param_into_net(net, param_dict)
net.set_train(False)
image = Tensor(np.zeros([batch_size, 3, captcha_height, captcha_width], np.float16))
export(net, image, file_name=args_opt.output_file, file_format="AIR")

View File

@ -32,6 +32,7 @@ class StackedRNN(nn.Cell):
batch_size(int): batch size of input data, default is 64
hidden_size(int): the hidden size in LSTM layers, default is 512
"""
def __init__(self, input_size, batch_size=64, hidden_size=512):
super(StackedRNN, self).__init__()
self.batch_size = batch_size
@ -40,22 +41,25 @@ class StackedRNN(nn.Cell):
self.reshape = P.Reshape()
self.cast = P.Cast()
k = (1 / hidden_size) ** 0.5
self.h1 = Tensor(np.zeros(shape=(batch_size, hidden_size)).astype(np.float16))
self.c1 = Tensor(np.zeros(shape=(batch_size, hidden_size)).astype(np.float16))
self.w1 = Parameter(np.random.uniform(-k, k, (4 * hidden_size, input_size + hidden_size, 1, 1))
.astype(np.float16), name="w1")
self.w2 = Parameter(np.random.uniform(-k, k, (4 * hidden_size, hidden_size + hidden_size, 1, 1))
.astype(np.float16), name="w2")
self.b1 = Parameter(np.random.uniform(-k, k, (4 * hidden_size, 1, 1, 1)).astype(np.float16), name="b1")
self.b2 = Parameter(np.random.uniform(-k, k, (4 * hidden_size, 1, 1, 1)).astype(np.float16), name="b2")
self.h2 = Tensor(np.zeros(shape=(batch_size, hidden_size)).astype(np.float16))
self.c2 = Tensor(np.zeros(shape=(batch_size, hidden_size)).astype(np.float16))
self.rnn1 = P.DynamicRNN(forget_bias=0.0)
self.rnn2 = P.DynamicRNN(forget_bias=0.0)
self.basic_lstm_cell = P.BasicLSTMCell(keep_prob=1.0, forget_bias=0.0, state_is_tuple=True, activation="tanh")
self.w1 = Parameter(np.random.uniform(-k, k, (input_size + hidden_size, 4 * hidden_size)).astype(np.float16),
name="w1")
self.w2 = Parameter(np.random.uniform(-k, k, (hidden_size + hidden_size, 4 * hidden_size)).astype(np.float16),
name="w2")
self.b1 = Parameter(np.random.uniform(-k, k, (4 * hidden_size)).astype(np.float16), name="b1")
self.b2 = Parameter(np.random.uniform(-k, k, (4 * hidden_size)).astype(np.float16), name="b2")
self.h1 = Tensor(np.zeros(shape=(1, batch_size, hidden_size)).astype(np.float16))
self.h2 = Tensor(np.zeros(shape=(1, batch_size, hidden_size)).astype(np.float16))
self.c1 = Tensor(np.zeros(shape=(1, batch_size, hidden_size)).astype(np.float16))
self.c2 = Tensor(np.zeros(shape=(1, batch_size, hidden_size)).astype(np.float16))
self.fc_weight = np.random.random((self.num_classes, hidden_size)).astype(np.float32)
self.fc_bias = np.random.random((self.num_classes)).astype(np.float32)
self.fc_bias = np.random.random(self.num_classes).astype(np.float32)
self.fc = nn.Dense(in_channels=hidden_size, out_channels=self.num_classes, weight_init=Tensor(self.fc_weight),
bias_init=Tensor(self.fc_bias))
@ -64,29 +68,22 @@ class StackedRNN(nn.Cell):
self.expand_dims = P.ExpandDims()
self.concat = P.Concat()
self.transpose = P.Transpose()
self.squeeze = P.Squeeze(axis=0)
def construct(self, x):
x = self.cast(x, mstype.float16)
x = self.transpose(x, (3, 0, 2, 1))
x = self.reshape(x, (-1, self.batch_size, self.input_size))
h1 = self.h1
c1 = self.c1
h2 = self.h2
c2 = self.c2
c1, h1, _, _, _, _, _ = self.basic_lstm_cell(x[0, :, :], h1, c1, self.w1, self.b1)
c2, h2, _, _, _, _, _ = self.basic_lstm_cell(h1, h2, c2, self.w2, self.b2)
h2_after_fc = self.fc(h2)
output = self.expand_dims(h2_after_fc, 0)
for i in range(1, F.shape(x)[0]):
c1, h1, _, _, _, _, _ = self.basic_lstm_cell(x[i, :, :], h1, c1, self.w1, self.b1)
c2, h2, _, _, _, _, _ = self.basic_lstm_cell(h1, h2, c2, self.w2, self.b2)
h2_after_fc = self.fc(h2)
h2_after_fc = self.expand_dims(h2_after_fc, 0)
output = self.concat((output, h2_after_fc))
y1, _, _, _, _, _, _, _ = self.rnn1(x, self.w1, self.b1, None, self.h1, self.c1)
y2, _, _, _, _, _, _, _ = self.rnn2(y1, self.w2, self.b2, None, self.h2, self.c2)
output = ()
for i in range(F.shape(x)[0]):
y2_after_fc = self.fc(self.squeeze(y2[i:i + 1:1]))
y2_after_fc = self.expand_dims(y2_after_fc, 0)
output += (y2_after_fc,)
output = self.concat(output)
return output
@ -101,6 +98,7 @@ class StackedRNNForGPU(nn.Cell):
hidden_size(int): the hidden size in LSTM layers, default is 512
num_layer(int): the number of layer of LSTM.
"""
def __init__(self, input_size, batch_size=64, hidden_size=512, num_layer=2):
super(StackedRNNForGPU, self).__init__()
self.batch_size = batch_size

View File

@ -42,7 +42,7 @@ parser.add_argument('--platform', type=str, default='Ascend', choices=['Ascend',
parser.set_defaults(run_distribute=False)
args_opt = parser.parse_args()
context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.platform, save_graphs=False)
context.set_context(mode=context.GRAPH_MODE, device_target=args_opt.platform)
if args_opt.platform == 'Ascend':
device_id = int(os.getenv('DEVICE_ID'))
context.set_context(device_id=device_id)