Ejemplo n.º 1
0
def export(path: str) -> None:
    torch_path = f"{path}.pth"
    onnx_path = f"{path}.onnx"
    tensorflow_path = f"{path}.pb"
    tensorflowjs_path = path

    model = LeNet5(10)
    model.load_state_dict(torch.load(torch_path))
    model = model.eval().cpu()

    torch.onnx.export(
        model,
        torch.zeros((1, 1, 28, 28)).float(),
        onnx_path,
        input_names=["img"],
        output_names=["pred"],
        do_constant_folding=True,
        export_params=True,
        opset_version=10,
        verbose=True,
    )

    model = onnx.load(onnx_path)
    prepare(model).export_graph(tensorflow_path)

    convert_tf_saved_model(tensorflow_path, tensorflowjs_path)
Ejemplo n.º 2
0
def test():
    onnx_model = onnx.load("resnet50.onnx")
    torch.random.manual_seed(0)
    input = torch.rand(1, 3, 224, 224).numpy()
    output = prepare(onnx_model).run(input)
    print("output:", output)

    tf_rep = prepare(onnx_model)
    print("tf_rep.inputs:", tf_rep.inputs)
    print("tf_rep.outputs:", tf_rep.outputs)
    tf_rep.export_graph("./resnet50.graph")
Ejemplo n.º 3
0
def convert(input_ops_dict, output_ops, input_model, output_model):
    '''Convert keras h5 to tensorflow pb
        Args:
            input_ops_dict: input ops dict including names and shapes
            output_ops: output op names
            input_model: input keras h5 model name
            output_model: output pb model name
    '''
    onnx_name = ".tmp.onnx"
    pb_name = ".tmp.pb"
    # keras --> onnx --> pb --> onnx --> pb

    # keras --> onnx
    model = tf.keras.models.load_model(input_model)
    onnx_model = keras2onnx.convert_keras(model, model.name, target_opset=8)
    keras2onnx.save_model(onnx_model, onnx_name)

    # onnx --> tf
    onnx_model = onnx.load(onnx_name)
    tf_rep = prepare(onnx_model, input_shape_dict=input_ops_dict)
    tf_rep.export_graph(pb_name)

    # tf --> onnx (fold constants)
    inputs = input_ops_dict.keys()
    inputs = [i + ":0" for i in inputs]
    outputs = output_ops
    graph_def, inputs, outputs = tf_loader.from_graphdef(
        pb_name, inputs, outputs)
    with tf.Graph().as_default() as tf_graph:
        tf.import_graph_def(graph_def, name="")

    g = tf2onnx.tfonnx.process_tf_graph(tf_graph,
                                        opset=8,
                                        input_names=inputs,
                                        output_names=outputs)
    onnx_graph = optimizer.optimize_graph(g)
    model_proto = onnx_graph.make_model("converted from %s" % pb_name)
    utils.save_protobuf(onnx_name, model_proto)

    # onnx --> tf
    onnx_model = onnx.load(onnx_name)
    tf_rep = prepare(onnx_model, input_shape_dict=input_ops_dict)
    tf_rep.export_graph(output_model)

    # remove tmp files
    if os.path.exists(onnx_name):
        os.remove(onnx_name)
    if os.path.exists(pb_name):
        os.remove(pb_name)
def detect_face(image):
    global cnt
    image = cv2.resize(image,(640,480))
    img = image.copy()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img_mean = np.array([127, 127, 127])
    img = (img - img_mean) / 128
    img = np.transpose(img, [2, 0, 1])
    img = np.expand_dims(img, axis=0)
    img = img.astype(np.float32)
    onnx_model = onnx.load('Copy of ultra_light_640.onnx')
    predictor = prepare(onnx_model)
    ort_session = ort.InferenceSession('Copy of ultra_light_640.onnx')
    input_name = ort_session.get_inputs()[0].name
    confidences, boxes = ort_session.run(None, {input_name: img})
    h,w,_ = image.shape
    boxes, labels, probs = predict(w, h, confidences, boxes, 0.7)
    cnt = 0
    for i in range(boxes.shape[0]):
            if probs[cnt]>0.99:
                box = boxes[i, :]
                x1, y1, x2, y2 = box
                crop_image = image[y1:y2,x1:x2]
                if crop_image.shape[0]<=0 or crop_image.shape[1]<=0 or len(crop_image)==0:
                      continue
                coords.append((x1, y1, x2, y2))
                image_list.append(crop_image)
            cnt = cnt + 1
    return coords, image_list
Ejemplo n.º 5
0
def conv_test():
    conv1_weights = tf.Variable(np.load("conv1w.npy").astype(np.float32))
    conv1_biases = tf.Variable(np.load("conv1b.npy").astype(np.float32))
    d = tf.placeholder(tf.float32, shape=[1, 28, 28, 1])
    sess = tf.Session()
    sess.run(tf.initialize_all_variables())
    conv = tf.nn.conv2d(d, conv1_weights, strides=[1, 1, 1, 1], padding='SAME')
    tf_model = tf.nn.bias_add(conv, conv1_biases)

    model = onnx.load("test_conv.onnx")
    tf_rep = prepare(model)
    npmodel = Testmodel(1)
    img = np.load("img.npy")
    for ele in img:
        data = ele.reshape([1, 28, 28, 1])
        tf_result = sess.run(tf_model, feed_dict={d: data})
        tf_result = np.array(tf_result)
        npresult = npmodel.infer(data)
        data = ele.reshape([1, 1, 28, 28])
        output = np.array(tf_rep.run(data))
        output = output[0]
        print(output.shape)
        output = np.swapaxes(output, 2, 3)
        output = np.swapaxes(output, 1, 3)

        print(output[0][0][1])
        print(tf_result[0][0][1])
        print(npresult[0][0][1])

        break
Ejemplo n.º 6
0
    def test_auto_cast(self):
        node_def = helper.make_node("Equal", ["a", "b"], ["Y"])
        graph_def = helper.make_graph(
            [node_def],
            name="test_auto_cast",
            inputs=[
                helper.make_tensor_value_info("a", TensorProto.UINT64,
                                              [None, None]),
                helper.make_tensor_value_info("b", TensorProto.UINT64,
                                              [None, None])
            ],
            outputs=[
                helper.make_tensor_value_info("Y", TensorProto.BOOL,
                                              [None, None])
            ])
        tf_rep = prepare(helper.make_model(graph_def), auto_cast=True)

        # random inputs with shape [5, 5]
        a = np.random.randint(low=0, high=10, size=(5, 5)).astype(np.uint64)
        b = np.random.randint(low=0, high=10, size=(5, 5)).astype(np.uint64)
        Y_ref = np.equal(a, b)

        # check the output from converter API against numpy's output
        output = tf_rep.run({"a": a, "b": b})
        np.testing.assert_almost_equal(output.Y, Y_ref)
Ejemplo n.º 7
0
def helper_test_onnx_tf(model, input_shape: List[int], tmpdir: str):
    """Ensure the model outputs do not change under onnx-tf transformation."""
    # run the model and cache the outputs
    test_spec: List[Tuple[np.ndarray, np.ndarray]] = []
    for i in range(5):
        rnd_input = np.random.rand(*input_shape).astype(np.float32)
        spec_output = model(torch.from_numpy(rnd_input))
        test_spec.append((rnd_input, spec_output))
    # convert model to tensorflow graph
    onnx_model_path = str(Path(tmpdir) / "model.onnx")
    torch.onnx.export(model, torch.from_numpy(test_spec[0][0]),
                      onnx_model_path)
    original_onnx_model = onnx.load(onnx_model_path)
    onnx_model = onnx.optimizer.optimize(original_onnx_model,
                                         passes=[
                                             'eliminate_identity',
                                             'eliminate_nop_transpose',
                                             'fuse_consecutive_transposes',
                                             'fuse_consecutive_squeezes',
                                             'fuse_add_bias_into_conv',
                                         ])
    onnx_model = onnx.utils.polish_model(onnx_model)
    tf.reset_default_graph()
    tf_rep = prepare(onnx_model)
    # test tf graph
    for (rnd_input, spec_output) in test_spec:
        tf_output = tf_rep.run({"0": rnd_input})
        assert tf_output._0 == approx(spec_output.data.numpy(),
                                      abs=1e-4,
                                      rel=1e-4)
def gen_pb():
    from trades.models.wideresnet import WideResNet
    import torch
    import onnx
    from onnx_tf.backend import prepare

    device = torch.device("cuda")
    model = WideResNet().to(device)
    model.load_state_dict(torch.load('./model_cifar_wrn.pt'))
    model.eval()

    dummy_input = torch.from_numpy(np.zeros(
        (64, 3, 32, 32), )).float().to(device)
    dummy_output = model(dummy_input)

    torch.onnx.export(model,
                      dummy_input,
                      './model_cifar_wrn.onnx',
                      input_names=['input'],
                      output_names=['output'])

    model_onnx = onnx.load('./model_cifar_wrn.onnx')

    tf_rep = prepare(model_onnx)

    # Print out tensors and placeholders in model (helpful during inference in TensorFlow)
    print(tf_rep.tensor_dict)

    # Export model as .pb file
    tf_rep.export_graph('./model_cifar_wrn.pb')
Ejemplo n.º 9
0
    def __init__(self, sess):
        # Preparing predictor
        onnx_model = onnx.load('ultra_light_640.onnx')
        self.predictor = prepare(onnx_model)
        self.ort_session = ort.InferenceSession('ultra_light_640.onnx')
        self.input_name = self.ort_session.get_inputs()[0].name
        logging.info("ultralight model loaded")

        self.shape_predictor = dlib.shape_predictor(
            'shape_predictor_5_face_landmarks.dat')

        self.f_a = face_utils.facealigner.FaceAligner(self.shape_predictor,
                                                      desiredFaceWidth=112,
                                                      desiredLeftEye=(0.3,
                                                                      0.3))
        self.threshold = 0.63
        with open("embeddings.pkl", "rb") as f:
            self.saved_embeds, self.names = pickle.load(f)
        logging.info("Loaded saved embeddings")

        self.sess = sess
        self.saver = tf.train.import_meta_graph('mfn.ckpt.meta')
        self.saver.restore(self.sess, 'mfn.ckpt')
        logging.info("tensorflow session restored")

        # Setting up tensors
        g = tf.get_default_graph()
        self.images_placeholder = g.get_tensor_by_name('input:0')
        self.embeddings = g.get_tensor_by_name('embeddings:0')
        self.phase_train_placeholder = g.get_tensor_by_name('phase_train:0')
        self.embedding_size = self.embeddings.get_shape()[1]
Ejemplo n.º 10
0
def convert(infile, outdir, **kwargs):
    """Convert pb.

  Args:
    infile: Input path.
    outdir: Output path.
    **kwargs: Other args for converting.

  Returns:
    None.
  """
    logging_level = kwargs.get("logging_level", "INFO")
    ext_data_dir = kwargs.get("extdatadir")

    common.logger.setLevel(logging_level)
    common.logger.handlers[0].setLevel(logging_level)
    common.logger.info("Start converting onnx pb to tf saved model")

    # load external data if the file directory is provided
    if ext_data_dir:
        onnx_model = onnx.load(infile, load_external_data=False)
        load_external_data_for_model(onnx_model, ext_data_dir)
    else:
        onnx_model = onnx.load(infile)

    tf_rep = backend.prepare(onnx_model, **kwargs)
    tf_rep.export_graph(outdir)
    common.logger.info("Converting completes successfully.")
Ejemplo n.º 11
0
def _export_via_onnx(model, inputs):
    from ipdb import set_trace
    set_trace()

    def _check_val(module):
        assert not module.training

    model.apply(_check_val)

    # Export the model to ONNX
    with torch.no_grad():
        with io.BytesIO() as f:
            torch.onnx.export(
                model,
                inputs,
                f,
                # verbose=True,  # NOTE: uncomment this for debugging
                export_params=True,
            )
            onnx_model = onnx.load_from_string(f.getvalue())
    # torch.onnx.export(model,  # model being run
    #                   inputs,  # model input (or a tuple for multiple inputs)
    #                   "reid_test.onnx",  # where to save the model (can be a file or file-like object)
    # export_params=True,  # store the trained parameter weights inside the model file
    # opset_version=10,  # the ONNX version to export the model to
    # do_constant_folding=True,  # whether to execute constant folding for optimization
    # input_names=['input'],  # the model's input names
    # output_names=['output'],  # the model's output names
    # dynamic_axes={'input': {0: 'batch_size'},  # variable lenght axes
    #               'output': {0: 'batch_size'}})
    # )

    # Apply ONNX's Optimization
    # all_passes = optimizer.get_available_passes()
    # passes = ["fuse_bn_into_conv"]
    # assert all(p in all_passes for p in passes)
    # onnx_model = optimizer.optimize(onnx_model, passes)

    # Convert ONNX Model to Tensorflow Model
    tf_rep = prepare(onnx_model,
                     strict=False)  # Import the ONNX model to Tensorflow
    print(tf_rep.inputs)  # Input nodes to the model
    print('-----')
    print(tf_rep.outputs)  # Output nodes from the model
    print('-----')
    # print(tf_rep.tensor_dict)  # All nodes in the model
    # """

    # install onnx-tensorflow from github,and tf_rep = prepare(onnx_model, strict=False)
    # Reference https://github.com/onnx/onnx-tensorflow/issues/167
    # tf_rep = prepare(onnx_model) # whthout strict=False leads to KeyError: 'pyfunc_0'

    # debug, here using the same input to check onnx and tf.
    # output_onnx_tf = tf_rep.run(to_numpy(img))
    # print('output_onnx_tf = {}'.format(output_onnx_tf))
    # onnx --> tf.graph.pb
    # tf_pb_path = 'reid_tf_graph.pb'
    # tf_rep.export_graph(tf_pb_path)

    return tf_rep
Ejemplo n.º 12
0
    def test_gru_savedmodel(self):
        input_size = 3
        hidden_size = 3
        weight_scale = 0.1
        custom_bias = 0.1
        number_of_gates = 3

        node_def = helper.make_node('GRU',
                                    inputs=['X', 'W', 'R', 'B'],
                                    outputs=['Y', 'Y_h'],
                                    hidden_size=hidden_size)
        graph_def = helper.make_graph(
            [node_def],
            name="gru_test",
            inputs=[
                helper.make_tensor_value_info("X", TensorProto.FLOAT,
                                              [1, 3, 3]),
                helper.make_tensor_value_info("W", TensorProto.FLOAT,
                                              [1, 9, 3]),
                helper.make_tensor_value_info("R", TensorProto.FLOAT,
                                              [1, 9, 3]),
                helper.make_tensor_value_info("B", TensorProto.FLOAT, [1, 18])
            ],
            outputs=[
                helper.make_tensor_value_info("Y", TensorProto.FLOAT,
                                              [1, 1, 3, 3]),
                helper.make_tensor_value_info("Y_h", TensorProto.FLOAT,
                                              [1, 3, 3])
            ])

        tf_rep = prepare(helper.make_model(graph_def))
        model_path = "gru_savedmodel"
        tf_rep.export_graph(model_path)

        # initializing Inputs
        X = np.array([[[1., 2., 3.], [4., 5., 6.], [7., 8.,
                                                    9.]]]).astype(np.float32)
        W = weight_scale * np.ones(
            (1, number_of_gates * hidden_size, input_size)).astype(np.float32)
        R = weight_scale * np.ones(
            (1, number_of_gates * hidden_size, hidden_size)).astype(np.float32)
        W_B = custom_bias * np.ones(
            (1, number_of_gates * hidden_size)).astype(np.float32)
        R_B = np.zeros((1, number_of_gates * hidden_size)).astype(np.float32)
        B = np.concatenate((W_B, R_B), axis=1)

        # use the ONNX reference implementation to get the expected output
        gru = GRU_Helper(X=X, W=W, R=R, B=B)
        _, Y_ref = gru.step()

        # load the savedmodel from file system
        m = tf.saved_model.load(model_path)

        # run the model
        tf_output = m(X=X, W=W, R=R, B=B)

        np.testing.assert_almost_equal(tf_output[1], Y_ref)

        # clean up saved model folder
        shutil.rmtree(model_path)
def pytorch2tensorflow():

    model_path = "model_path"

    # step 1 init pytorch
    cuda_ = 'cuda:{}'.format(0)
    device = torch.device(cuda_ if torch.cuda.is_available() else 'cpu')
    pytorch_model_path = os.path.join(model_path, "siamese.ckpt")
    model = ResNet18SiameseNetwork()
    model_params = torch.load(pytorch_model_path)
    siamese_state_dict = {}
    for k, v in model_params.items():
        siamese_state_dict[k.replace('module.', '')] = v
    model.load_state_dict(siamese_state_dict)
    model.eval()

    # step 2 pytorch to onnx
    dummy_input = Variable(torch.randn(1, 3, HEIGHT, WIDTH))  # nchw
    onnx_model_path = os.path.join(model_path, "siamese.onnx")
    torch.onnx.export(model, dummy_input, onnx_model_path, verbose=True)
    pytorch_feature = test_pytorch(model, test_img)
    onnx_feature = test_onnx(onnx_model_path, test_img)
    compare_feature(pytorch_feature, onnx_feature)
    print("pytorch to onnx ok!")
    #
    # step 3 onnx --> tf pb
    onnx_model = onnx.load(onnx_model_path)
    tf_pb_path = os.path.join(model_path, "siamese.pb")
    print("1")
    tf_rep = prepare(onnx_model, strict=False)
    print("2")
    tf_rep.export_graph(tf_pb_path)
    #tf_feature = test_tensorflow(tf_pb_path)
    #compare_feature(pytorch_feature, tf_feature)
    print("onnx to tensorflow ok!")
Ejemplo n.º 14
0
def convert_to_tensorflow(pytorch_model_path, model_class):
    """
    Converts a trained pytorch model to a tensorflow model using ONNX
    :param pytorch_model_path: Path to pytorch model
    """

    # Setup filenames
    fileroot = os.path.basename(pytorch_model_path).split('.')[0]
    dirname = os.path.dirname(pytorch_model_path)
    onnx_path = '{}.onnx'.format(os.path.join(dirname, fileroot))
    tf_path = '{}.pb'.format(os.path.join(dirname, fileroot))

    print("Saving onnx file to {}".format(onnx_path))
    print("Saving onnx file to {}".format(tf_path))

    print("Loading trained model...")
    trained_model = model_class()
    trained_model.load_state_dict(torch.load(pytorch_model_path))

    # Export the trained model to ONNX
    print("Exporting model to ONNX...")
    input_size = trained_model.get_input_size()
    dummy_input = Variable(torch.randn(input_size))
    torch.onnx.export(trained_model, dummy_input, onnx_path)

    # Create Tensorflow Representation
    print("Creating tensorflow representation...")
    onnx_model = onnx.load(onnx_path)
    tf_rep = prepare(onnx_model)

    print("Saving tensorflow graph...")
    tf_rep.export_graph(tf_path)
Ejemplo n.º 15
0
  def test_if_with_sequence(self):
    # S = [a]
    # if cond is True
    #   S = [a,b]
    # else
    #   S = [a,c]
    a = np.random.randn(2, 1, 2).astype(np.float32)
    b = np.random.randn(1, 1, 2).astype(np.float32)
    c = np.random.randn(3, 1, 2).astype(np.float32)
    seq_construct_node = helper.make_node('SequenceConstruct', ['a'], ['S'])
    seq_insert_node1 = helper.make_node('SequenceInsert', ['S', 'b'], ['Sb'])
    seq_insert_node2 = helper.make_node('SequenceInsert', ['S', 'c'], ['Sc'])

    a_in = helper.make_tensor_value_info('a', onnx.TensorProto.FLOAT, [2, 1, 2])
    b_in = helper.make_tensor_value_info('b', onnx.TensorProto.FLOAT, [1, 1, 2])
    c_in = helper.make_tensor_value_info('c', onnx.TensorProto.FLOAT, [3, 1, 2])
    cond_in = helper.make_tensor_value_info('cond', TensorProto.BOOL, [])
    s_in = helper.make_sequence_value_info('S', TensorProto.FLOAT,
                                           [None, None, None, None])

    sb_out = helper.make_sequence_value_info('Sb', TensorProto.FLOAT,
                                             [None, None, None, None])
    sc_out = helper.make_sequence_value_info('Sc', TensorProto.FLOAT,
                                             [None, None, None, None])
    s_final_out = helper.make_sequence_value_info('S_final', TensorProto.FLOAT,
                                                  [None, None, None, None])

    then_graph = helper.make_graph(nodes=[seq_insert_node1],
                                   name="then_graph",
                                   inputs=[s_in, b_in],
                                   outputs=[sb_out])
    else_graph = helper.make_graph(nodes=[seq_insert_node2],
                                   name="else_graph",
                                   inputs=[s_in, c_in],
                                   outputs=[sc_out])
    if_node = helper.make_node('If', ['cond'], ['S_final'],
                               then_branch=then_graph,
                               else_branch=else_graph)

    graph_def = helper.make_graph(nodes=[seq_construct_node, if_node],
                                  name='test_if',
                                  inputs=[a_in, b_in, c_in, cond_in],
                                  outputs=[s_final_out])
    tf_rep = prepare(helper.make_model(graph_def))
    output = tf_rep.run({
        'a': a,
        'b': b,
        'c': c,
        'cond': np.array(True, dtype=np.bool)
    })
    np.testing.assert_almost_equal(output['S_final'].values[:2], a)
    np.testing.assert_almost_equal(output['S_final'].values[2:], b)
    output = tf_rep.run({
        'a': a,
        'b': b,
        'c': c,
        'cond': np.array(False, dtype=np.bool)
    })
    np.testing.assert_almost_equal(output['S_final'].values[:2], a)
    np.testing.assert_almost_equal(output['S_final'].values[2:], c)
Ejemplo n.º 16
0
  def do_test_expected(self):
    tf_op = test_data[1]
    output_name = test_data[2]
    inputs = test_data[3]
    attrs = test_data[4]

    # Now construct input feed dict
    # keyed by input name
    onnx_feed_dict = {}
    # keyed by placeholder op
    tf_feed_dict = {}
    tf_param_list = []
    for idx, input_tensor in enumerate(inputs):
      if type(input_tensor) is np.ndarray:
        placeholder = tf.placeholder(input_tensor.dtype,
                                     shape=input_tensor.shape,
                                     name="in_" + str(idx))
        onnx_feed_dict["in_" + str(idx)] = input_tensor
        tf_feed_dict[placeholder] = input_tensor
        tf_param_list.append(placeholder)
      else:
        tf_param_list.append(input_tensor)
    test_op = tf_op(*tf_param_list, **attrs)
    tf_graph = test_op.graph.as_graph_def(add_shapes=True)
    # Construct onnx graph, run with backend.
    output_node = get_node_by_name(tf_graph.node, output_name)
    onnx_graph = convert_graph(tf_graph, output_node)
    backend_rep = prepare(helper.make_model(onnx_graph))
    backend_output = backend_rep.run(onnx_feed_dict)[output_name]

    with tf.Session() as sess:
      tf_output = sess.run(test_op, tf_feed_dict)
    tf.reset_default_graph()
    np.testing.assert_allclose(backend_output, tf_output)
Ejemplo n.º 17
0
def export_tf_proto(onnx_file, meta_file):
    """
    Exports the ONNX model to a Tensorflow Proto file.
    The exported file will have a .meta extension.
    :param onnx_file: string, Path to the .onnx model file
    :param meta_file: string, Path to the exported Tensorflow .meta file
    :return: tuple, input and output tensor dictionaries. Dictionaries have a
        {tensor_name: TF_Tensor_op} structure.
    """
    model = onnx.load(onnx_file)

    # Convert the ONNX model to a Tensorflow graph
    tf_rep = prepare(model, strict=False)

    global pb_output
    pb_output = tf_rep.run(input_np)

    output_keys = tf_rep.outputs
    input_keys = tf_rep.inputs

    tf_dict = tf_rep.tensor_dict
    input_tensor_names = {key: tf_dict[key] for key in input_keys}
    output_tensor_names = {key: tf_dict[key] for key in output_keys}

    tf_rep.export_graph(meta_file)
    log.info("Exported Tensorflow proto file to {}".format(meta_file))
    return input_tensor_names, output_tensor_names
Ejemplo n.º 18
0
  def test_initializer(self):
    X = np.array([[1, 2], [3, 4]]).astype(np.float32)
    Y = np.array([[1, 2], [3, 4]]).astype(np.float32)
    weight = np.array([[1, 0], [0, 1]])
    graph_def = helper.make_graph(
        [
            helper.make_node("Add", ["X", "Y"], ["Z0"]),
            helper.make_node("Cast", ["Z0"], ["Z"], to="float"),
            helper.make_node("Mul", ["Z", "weight"], ["W"]),
            helper.make_node("Tanh", ["W"], ["W1"]),
            helper.make_node("Sigmoid", ["W1"], ["W2"])
        ],
        name="test_initializer",
        inputs=[
            helper.make_tensor_value_info("X", TensorProto.FLOAT, (2, 2)),
            helper.make_tensor_value_info("Y", TensorProto.FLOAT, (2, 2)),
            helper.make_tensor_value_info("weight", TensorProto.FLOAT, (2, 2)),
        ],
        outputs=[
            helper.make_tensor_value_info("W2", TensorProto.FLOAT, (2, 2))
        ],
        initializer=[
            helper.make_tensor("weight", TensorProto.FLOAT, [2, 2],
                               weight.flatten().astype(float))
        ])

    def sigmoid(x):
      return 1 / (1 + np.exp(-x))

    W_ref = sigmoid(np.tanh((X + Y) * weight))
    tf_rep = prepare(helper.make_model(graph_def))
    output = tf_rep.run({"X": X, "Y": Y})
    np.testing.assert_almost_equal(output["W2"], W_ref)
Ejemplo n.º 19
0
    def OnnxEmit(original_framework, architecture_name, architecture_path,
                 weight_path, image_path):
        from mmdnn.conversion.onnx.onnx_emitter import OnnxEmitter

        original_framework = checkfrozen(original_framework)

        # IR to code
        converted_file = original_framework + '_onnx_' + architecture_name + "_converted"
        converted_file = converted_file.replace('.', '_')
        emitter = OnnxEmitter(architecture_path, weight_path)
        emitter.run(converted_file + '.py', None, 'test')
        del emitter
        del OnnxEmitter

        # import converted model
        from onnx_tf.backend import prepare
        model_converted = __import__(converted_file).KitModel(weight_path)
        tf_rep = prepare(model_converted)

        func = TestKit.preprocess_func[original_framework][architecture_name]
        img = func(image_path)
        input_data = np.expand_dims(img, 0)

        predict = tf_rep.run(input_data)[0]

        del prepare
        del model_converted
        del tf_rep
        os.remove(converted_file + '.py')

        return predict
Ejemplo n.º 20
0
def Main(queue, msg_queue):
    #region init
    # load the model, create runtime session & get input variable name
    onnx_path = 'model/ultra_light_640.onnx'
    onnx_model = onnx.load(onnx_path)
    predictor = prepare(onnx_model)
    ort_session = ort.InferenceSession(onnx_path)
    input_name = ort_session.get_inputs()[0].name

    import dlib
    shape_predictor = dlib.shape_predictor(
        'model/shape_predictor_68_face__landmarks.dat')
    fa = face_utils.facealigner.FaceAligner(shape_predictor,
                                            desiredFaceWidth=112,
                                            desiredLeftEye=(0.3, 0.3))
    #endregion

    try:
        with tf.Graph().as_default():
            with tf.Session() as sess:
                ip.In_Program_M(sess, ort_session, input_name, fa, queue,
                                msg_queue)

    except Exception as e:
        print(e)
Ejemplo n.º 21
0
    def initialize(self, model, model_as_file):

        if not HAS_TF_ONNX:
            raise Exception(
                'Tensorflow(version 1.13.1 or above), Onnx(version 1.5.0) and Onnx_tf(version 1.3.0) libraries are not installed. Install Tensorflow using "conda install tensorflow-gpu=1.13.1". Install onnx and onnx_tf using "pip install onnx onnx_tf".'
            )

        from arcgis.learn.models import SingleShotDetector

        if model_as_file:
            with open(model, 'r') as f:
                self.json_info = json.load(f)
        else:
            self.json_info = json.loads(model)

        model_path = self.json_info['ModelFile']
        anc_grids = self.json_info['Grids']
        anc_zooms = self.json_info['Zooms']
        anc_ratios = self.json_info['Ratios']
        self.batch_size = self.json_info['BatchSize']

        if model_as_file and not os.path.isabs(model_path):
            model_path = os.path.abspath(
                os.path.join(os.path.dirname(model), model_path))

        model_onnx = onnx.load(model_path)
        self.tf_rep_graph = prepare(model_onnx)

        self._anchors, self._grid_sizes = _create_anchors(
            anc_grids, anc_zooms, anc_ratios)
Ejemplo n.º 22
0
def main():
    """Main function"""
    
    # Option parser
    parser = argparse.ArgumentParser()
    parser.add_argument("--onnx", action="store", help="input onnx model")
    parser.add_argument("--imgs", action="store", 
                        help="Comma separated list of images")
    args = parser.parse_args()

    # Convert onnx to TF
    model = onnx.load(args.onnx)

    print("preparing model")
    tf_rep = prepare(model)

    # load mnist using PyTorch
    test_loader = torch.utils.data.DataLoader(
        datasets.MNIST('../data', train=False, transform=transforms.Compose([
                           transforms.ToTensor(),
                           transforms.Normalize((0.1307,), (0.3081,))
                       ])),
        batch_size=1000, shuffle=True)
   
    # run the model
    output = tf_rep.run(np.asarray(test_loader, dtype=np.float32)[np.newaxis, np.newaxis, :, :])
Ejemplo n.º 23
0
def covert_to_tensorflow_and_save(pytorch_model, path, args):
    if args['dataset'] == 'mnist':
        dummy_input = torch.randn(1, 1, 28, 28)
    elif args['dataset'] == 'cifar10':
        dummy_input = torch.randn(1, 1, 32, 32)
    elif args['dataset'] == 'physionet':
        dummy_input = torch.randn(1, 1, 12000)
    elif args['dataset'] == 'shhs':
        dummy_input = torch.randn(1, 1, 15000)

    if args['device'] == torch.device('cuda'):
        dummy_input = dummy_input.cuda()

    torch.onnx.export(pytorch_model, dummy_input, path + '.onnx')
    onnx_model = onnx.load(path + '.onnx')

    tf_model = prepare(onnx_model)
    tf_model.export_graph(path + '.pb')

    # graph_def_file = args['chkpnt_dir'] + "tf_model.pb"
    # input_arrays = ['0']
    # output_arrays = ['LogSoftmax']

    # converter = tf.lite.TFLiteConverter.from_frozen_graph(graph_def_file, input_arrays, output_arrays)
    # tflite_model = converter.convert()

    return tf_model
Ejemplo n.º 24
0
    def _convert_to_tf(self):
        onnx_output_dir = './onnx_output/model.onnx'

        if not os.path.exists('./onnx_output/'):
            os.makedirs('./onnx_output/')
        if not os.path.exists('./tf_model/'):
            os.makedirs('./tf_model/')

        dummy_input = torch.rand(self.__input_dims)

        # Export the onnx representation to the onnx_output_dir
        torch.onnx.export(self.__new_model,
                          dummy_input,
                          onnx_output_dir,
                          input_names=['input'],
                          output_names=['output'])

        # Load ONNX representation and convert to TensorFlow format
        model_onnx = onnx.load(onnx_output_dir)
        tf_rep = prepare(model_onnx, strict=False)

        # Get the output and input tensors from the tf_rep
        with tf.Session() as sess:
            sess.graph.as_default()
            tf.import_graph_def(tf_rep.graph.as_graph_def(), name='')

            self.input_tensor = sess.graph.get_tensor_by_name(
                tf_rep.tensor_dict['input'].name)
            self.output_tensor = sess.graph.get_tensor_by_name(
                tf_rep.tensor_dict['output'].name)

            self.tf_graph = sess.graph
Ejemplo n.º 25
0
def validate_onnx_model(platform, device_type, model_file,
                        input_file, mace_out_file,
                        input_names, input_shapes, input_data_formats,
                        output_names, output_shapes, output_data_formats,
                        validation_threshold, input_data_types,
                        backend, log_file):
    import onnx
    if backend == "tensorflow":
        from onnx_tf.backend import prepare
        print("valivate on onnx tensorflow backend.")
    elif backend == "caffe2" or backend == "pytorch":
        from caffe2.python.onnx.backend import prepare
        print("valivate on onnx caffe2 backend.")
    else:
        common.MaceLogger.error(
            VALIDATION_MODULE,
            "onnx backend framwork '" + backend + "' is invalid.")
    if not os.path.isfile(model_file):
        common.MaceLogger.error(
            VALIDATION_MODULE,
            "Input graph file '" + model_file + "' does not exist!")
    model = onnx.load(model_file)
    input_dict = {}
    for i in range(len(input_names)):
        input_value = load_data(common.formatted_file_name(input_file,
                                                           input_names[i]),
                                input_data_types[i])
        input_value = input_value.reshape(input_shapes[i])
        if input_data_formats[i] == common.DataFormat.NHWC and \
                len(input_shapes[i]) == 4:
            input_value = input_value.transpose((0, 3, 1, 2))
        input_dict[input_names[i]] = input_value
    onnx_outputs = []
    for i in range(len(output_names)):
        out_shape = output_shapes[i][:]
        if output_data_formats[i] == common.DataFormat.NHWC and\
                len(out_shape) == 4:
            out_shape[1], out_shape[2], out_shape[3] = \
                out_shape[3], out_shape[1], out_shape[2]
        onnx_outputs.append(
            onnx.helper.make_tensor_value_info(output_names[i],
                                               onnx.TensorProto.FLOAT,
                                               out_shape))
    model.graph.output.extend(onnx_outputs)
    rep = prepare(model)

    output_values = rep.run(input_dict)
    for i in range(len(output_names)):
        out_name = output_names[i]
        value = output_values[out_name].flatten()
        output_file_name = common.formatted_file_name(mace_out_file,
                                                      output_names[i])
        mace_out_value = load_data(output_file_name)
        if output_data_formats[i] == common.DataFormat.NHWC and \
                len(output_shapes[i]) == 4:
            mace_out_value = mace_out_value.reshape(output_shapes[i]) \
                .transpose((0, 3, 1, 2))
        compare_output(platform, device_type, output_names[i],
                       mace_out_value, value,
                       validation_threshold, log_file)
def onnx2tf():
    # https://ichi.pro/ko/pytorchleul-tensorflow-litelo-byeonhwanhaneun-naui-yeojeong-14449401839341
    # https://github.com/onnx/onnx-tensorflow

    TF_PATH = "C:/Users/mmclab1/Desktop/fakecheck/tf_model_adv"
    ONNX_PATH = "C:/Users/mmclab1/Desktop/fakecheck/onnx_model_adv.onnx"

    onnx_model = onnx.load(ONNX_PATH)  # load onnx model
    print('success loading onnx file')

    tf_rep = prepare(onnx_model)  # creating TensorflowRep object
    '''
    Process finished with exit code -1073741819 (0xC0000005)
    Error code 0xc0000005 means "access violation"
    -> Please check your MPI code for memory/stack/heap access issues. And make sure any array access has valid index.
    
    ==> 해결) 읽어들인 학습된 가중치(.pt)는 torch.save(model.state_dict(), PATH) 모델 객체의 state_dict가 저장되었기 때문.
    model.load_state_dict(best_model_wts) 전체모델 저장으로 바꿔서 수행
    
    ==> jupyter lab이나 colab으로 실행
    '''
    print('success creating tensorflowRep')

    tf_rep.export_graph(TF_PATH)  # export the model
    print('success converting onnx to tensorflow')
def convert_to_tf(onnx_path: str, tf_path: str):
    if not os.path.exists(tf_path):
        os.makedirs(tf_path)
    onnx_model = onnx.load(onnx_path)
    #onnx.checker.check_model(onnx_model)  # Checks signature
    tf_rep = prepare(onnx_model)  # Prepare TF representation
    tf_rep.export_graph(tf_path)
    return tf_path
Ejemplo n.º 28
0
 def build_model_from_onnx(self, filename):
     model = onnx.load(filename)
     tf_rep = prepare(model)
     print(tf_rep.predict_net.tensor_dict[
         tf_rep.predict_net.external_input[0]].name)
     print(tf_rep.predict_net.tensor_dict[
         tf_rep.predict_net.external_output[0]].name)
     return tf_rep
def main():
    onnx_path = "models/yolo_v3.onnx"
    pb_path = "models/yolo_v3.pb"

    onnx_model = onnx.load(onnx_path)
    tf_exp = prepare(onnx_model, device='CPU')
    print(tf_exp.tensor_dict)
    tf_exp.export_graph(pb_path)
def torch2tf(model):
    dummy_input = torch.randn(1, 1, 48, 48)
    torch.onnx.export(model, dummy_input, './models/onnx_model.onnx')

    onnx_model = onnx.load('./models/onnx_model.onnx')

    tf_model = prepare(onnx_model)
    tf_model.export_graph('./models/model_simple.pb')
def convert_onnx_to_model(onnx_input_path):
    """Reimplementation of the TensorFlow-onnx official tutorial convert the onnx file to specific: model

    Parameters
    -----------
    onnx_input_path : string
    the path where you save the onnx file.

    References
    -----------
    - `onnx-tf exporting tutorial <https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb>`__
    """
    model = onnx.load(onnx_input_path)
    tf_rep = prepare(model)
    # Image Path
    img = np.load("./assets/image.npz")
    output = tf_rep.run(img.reshape([1, 784]))
    print("The digit is classified as ", np.argmax(output))
Ejemplo n.º 32
0
# can be expressed using Gemm alternatively
node = oh.make_node('MatMul', ["pdf","cdfmat"], ["cdf"])

# ArgMax(Clip(cdf + (1 - rand), max=1))
# node_rand_sub = oh.make_node('Sub', ["one","r"], ["rr"])
add_node = oh.make_node('Add', ["cdf", "r"], ["temp"], broadcast=1)
clip_node = oh.make_node('Clip', ["temp"], ["cdf_clipped"], min=0.0, max=1.0)
topk_node = oh.make_node('ArgMax', ["cdf_clipped"], ["chosen_action"], axis=1, keepdims=0)

graph = oh.make_graph([one, one_int, pdf_temp, pdf_temp_2,
                       max_score, normalized_scores, # top_action_1, # top_action_2,
                       # one_hot_top_action_int, one_hot_top_action_float, 
                       one_hot_top_action,
                       exploit_prob, exploit_top_action,
                       pdf, # node_rand_sub, 
                       node, node_rand, add_node, clip_node, topk_node], 
                    'compute_graph', input_tensors, output_tensors, initializer_tensors)
model = oh.make_model(graph, producer_name='explore')

f = open("model1.onnx", "wb")
f.write(model.SerializeToString())
f.close()

tf_model = onnx.load('model1.onnx')

tf_rep = prepare(tf_model)

sample = tf_rep.run(np.asarray([[.3,.3,.4]]))
print(sample)