From dev-return-5281-archive-asf-public=cust-asf.ponee.io@singa.apache.org Sat May 16 07:53:40 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id 0719D180626 for ; Sat, 16 May 2020 09:53:39 +0200 (CEST) Received: (qmail 24586 invoked by uid 500); 16 May 2020 07:53:39 -0000 Mailing-List: contact dev-help@singa.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@singa.apache.org Delivered-To: mailing list dev@singa.apache.org Received: (qmail 24576 invoked by uid 99); 16 May 2020 07:53:39 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 16 May 2020 07:53:39 +0000 From: =?utf-8?q?GitBox?= To: dev@singa.apache.org Subject: =?utf-8?q?=5BGitHub=5D_=5Bsinga=5D_joddiy_edited_a_comment_on_issue_=23691?= =?utf-8?q?=3A_Add_save_and_load_method_for_Module_class?= Message-ID: <158961561919.19379.2565562713412659284.asfpy@gitbox.apache.org> Date: Sat, 16 May 2020 07:53:39 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit References: In-Reply-To: joddiy edited a comment on issue #691: URL: https://github.com/apache/singa/issues/691#issuecomment-629604531 ``` # 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 super.compile(self) # 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: layer.set_weight(self.weights[input_name]) class MyModel(SONNXModel): def __init__(self, onnx): super.__init__(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: users@infra.apache.org