Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix node names ending with _fwd not being able to find weights #795

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions mmdnn/conversion/mxnet/mxnet_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,14 @@ def rename_FullyConnected(self, source_node):

# weights
if self.weight_loaded:
if source_node.name.endswith("_fwd"):
node_prefix = source_node.name[:-4]
else:
node_prefix = source_node.name
if self.data_format == 'NM':
self.set_weight(source_node.name, "weights", self.weight_data.get(source_node.name + "_weight").asnumpy().transpose((1, 0)))
self.set_weight(source_node.name, "weights", self.weight_data.get(node_prefix + "_weight").asnumpy().transpose((1, 0)))
else:
weight = self.weight_data.get(source_node.name + "_weight").asnumpy().transpose((1, 0))
weight = self.weight_data.get(node_prefix + "_weight").asnumpy().transpose((1, 0))
original_shape = weight.shape

channel_first_list = self.trace_shape(source_node, IR_node)
Expand All @@ -417,7 +421,7 @@ def rename_FullyConnected(self, source_node):
self.set_weight(source_node.name, "weights", weight)

if IR_node.attr["use_bias"].b:
self.set_weight(source_node.name, "bias", self.weight_data.get(source_node.name + "_bias").asnumpy())
self.set_weight(source_node.name, "bias", self.weight_data.get(node_prefix + "_bias").asnumpy())

if not self.data_format == 'NM':
# print("Warning: Layer [{}] has changed model data format from [{}] to [NM]".format(source_node.name, self.data_format))
Expand Down Expand Up @@ -502,15 +506,19 @@ def rename_Convolution(self, source_node):

# weights
if self.weight_loaded:
weight = self.weight_data.get(source_node.name + "_weight").asnumpy()
if source_node.name.endswith("_fwd"):
node_prefix = source_node.name[:-4]
else:
node_prefix = source_node.name
weight = self.weight_data.get(node_prefix + "_weight").asnumpy()
if not layout in MXNetParser.channels_last:
weight = MXNetParser.transpose(weight, dim)
if IR_node.op == "DepthwiseConv":
weight = weight.transpose(0, 1, 3, 2)
self.set_weight(source_node.name, "weights", weight)

if IR_node.attr["use_bias"].b:
self.set_weight(source_node.name, "bias", self.weight_data.get(source_node.name + "_bias").asnumpy())
self.set_weight(source_node.name, "bias", self.weight_data.get(node_prefix + "_bias").asnumpy())


def rename_Activation(self, source_node):
Expand All @@ -537,19 +545,29 @@ def rename_BatchNorm(self, source_node):

# weights
if self.weight_loaded:
if source_node.name.endswith("_fwd"):
node_prefix = source_node.name[:-4]
else:
node_prefix = source_node.name
# gamma
if IR_node.attr["scale"].b:
self.set_weight(source_node.name, "scale", self.weight_data.get(source_node.name + "_gamma").asnumpy())
self.set_weight(source_node.name, "scale", self.weight_data.get(node_prefix + "_gamma").asnumpy())

# beta
if IR_node.attr["bias"].b:
self.set_weight(source_node.name, "bias", self.weight_data.get(source_node.name + "_beta").asnumpy())
self.set_weight(source_node.name, "bias", self.weight_data.get(node_prefix + "_beta").asnumpy())

# mean
self.set_weight(source_node.name, "mean", self.weight_data.get(source_node.name + "_moving_mean").asnumpy())
try:
self.set_weight(source_node.name, "mean", self.weight_data.get(node_prefix + "_moving_mean").asnumpy())
except AttributeError:
self.set_weight(source_node.name, "mean", self.weight_data.get(node_prefix + "_running_mean").asnumpy())

# var
self.set_weight(source_node.name, "var", self.weight_data.get(source_node.name + "_moving_var").asnumpy())
try:
self.set_weight(source_node.name, "var", self.weight_data.get(node_prefix + "_moving_var").asnumpy())
except AttributeError:
self.set_weight(source_node.name, "var", self.weight_data.get(node_prefix + "_running_var").asnumpy())


def rename_Pooling(self, source_node):
Expand Down