singa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <>
Subject [GitHub] [singa] joddiy edited a comment on issue #691: Add save and load method for Module class
Date Sat, 16 May 2020 07:52:48 GMT

joddiy edited a comment on issue #691:

   # handle ONNX 
   def to_onnx(model):
       return a onnx model 
   class SONNXModel(Module):
       def __init__(self, onnx_model):
           singa_rep = sonnx.prepare(onnx_model, device=dev, batchsize=1)
           for layer_name, layer in singa_rep.layers:
           self.__dict__[layer_name] = layer
           # store weights here as numpy
           for weith_name, weight in singa_rep.weights:
               self.weights[weith_name] = weight
           # store layer info such as input and output name(only weights)
           for layer_name, layer_info in singa_rep.layer_infos:
               self.layer_infos[layer_name] = layer_info
       def forward(self, aux_output):
           # run forward according to onnx graph 
           return the last output + aux_output
       def compile(self)
           # init weights
           # set weights' value
       for layer_name, layer in self.__dict__:
           input_info, output_info = self.layer_infos[layer_name]
           for input_name in input_info:
   class MyModel(SONNXModel):
        def __init__(self, onnx):
             self.layer1 = Conv()
             self.layer2 = Conv()
        def forward(self, x):
              x1, x2 = super.forward(x, aux_output)
              x = self.layer1.forward(x2)
              return self.layer2.forward(x1) + x
         def train_one_batch(self, x, y):
              y_ = self.forward(x)
   How about this one, we pareses onnx by `soon.prepare`(Backend), it returns a `singa_rep`(BackendRep),
and the  singa_rep contains the layers, weights and input_output_info, we store the layers
in self.__dict__. When we compile the model, first we call super() to init the params, then
we set its value from the onnx loaded weights.

This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:

View raw message