-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from disktnk/feature/where
Add Where op converter
- Loading branch information
Showing
5 changed files
with
42 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,40 @@ | ||
import chainer | ||
|
||
from onnx_chainer import onnx_helper | ||
|
||
|
||
class Context(object): | ||
"""Context of converter | ||
This context shares names during exporting. | ||
Attributes: | ||
name_list (dict): list of being exported as ONNX node name keyed by | ||
instance ID. When the target variable is ``chainer.Variable`` or | ||
``chainer.Parameter``, instance ID of ``ndarray`` held by the | ||
variable is also put as key, because some functions like | ||
``F.where`` internally unwrap variable. | ||
""" | ||
|
||
def __init__(self, model): | ||
self.name_list = dict() | ||
for name, param in model.namedparams(): | ||
onnx_name = onnx_helper.cleanse_param_name(name) | ||
self.name_list[str(id(param))] = onnx_name | ||
self.set_name(param, onnx_name) | ||
|
||
def get_name(self, variable): | ||
str_id = str(id(variable)) | ||
str_id = id(variable) | ||
if str_id in self.name_list: | ||
return self.name_list[str_id] | ||
else: | ||
new_name = 'v{}'.format(len(self.name_list)) | ||
self.name_list[str_id] = new_name | ||
self.set_name(variable, new_name) | ||
return new_name | ||
|
||
def set_name(self, variable, name): | ||
str_id = str(id(variable)) | ||
str_id = id(variable) | ||
self.name_list[str_id] = name | ||
if isinstance(variable, (chainer.Variable, chainer.Parameter)): | ||
array_id = id(variable.array) | ||
self.name_list[array_id] = name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
'Squeeze', | ||
'Tile', | ||
'Transpose', | ||
'Where', | ||
|
||
# Connection | ||
'Convolution2DFunction', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters