예제 #1
0
    def compare_with_pytorch(self,
                             pytorch_model,
                             input_shape_with_batch,
                             compare_result=True,
                             rtol=1e-6,
                             atol=1e-6):
        input_shape_with_batch = to_list(input_shape_with_batch)
        onnx_path = self.dump_pytorch_to_onnx(pytorch_model,
                                              input_shape_with_batch)
        onnx_model = onnx.load(onnx_path)
        # TODO: we only consider single  output for now.
        input_data_with_batch = [
            np.random.uniform(0, 1, shape).astype(np.float32)
            for shape in input_shape_with_batch
        ]
        # coutput = caffe2.python.onnx.backend.run_model(onnx_model, input_data_with_batch)[0]

        pytorch_out = pytorch_model.forward(
            self._convert_ndarray_to_tensor(input_data_with_batch))
        zmodel = OnnxLoader(onnx_model.graph).to_keras()
        zoutput = zmodel.forward(input_data_with_batch[0] if len(
            input_data_with_batch) == 1 else input_data_with_batch)
        if compare_result:
            self.assert_allclose(pytorch_out.detach().numpy(), zoutput, rtol,
                                 atol)
예제 #2
0
 def compare_with_pytorch(self, pytorch_model, input_shape_with_batch):
     onnx_path = self.dump_pytorch_to_onnx(pytorch_model, input_shape_with_batch)
     onnx_model = onnx.load(onnx_path)
     # TODO: we only consider single input and output for now.
     input_data_with_batch = np.random.uniform(0, 1, input_shape_with_batch).astype(np.float32)
     # coutput = caffe2.python.onnx.backend.run_model(onnx_model, input_data_with_batch)[0]
     pytorch_out = pytorch_model.forward(torch.from_numpy(input_data_with_batch))
     zmodel = OnnxLoader(onnx_model.graph).to_keras()
     zoutput = zmodel.forward(input_data_with_batch)
     self.assert_allclose(pytorch_out.detach().numpy(), zoutput)