Beispiel #1
0
def generate():
  test_dir = os.path.dirname(__file__)
  graph = tf.Graph()
  with graph.as_default():
    tf.constant(np.arange(10), dtype=tf.float32, name="x")

  with tf.Session(graph=graph) as sess:
    save_consts(sess, test_dir)
    save_graph(graph, "test_const", test_dir)
Beispiel #2
0
def generate():
  test_dir = os.path.dirname(__file__)
  graph = tf.Graph()
  with graph.as_default():
    x = tf.placeholder(tf.float32, name="x")
    one = tf.constant(1.0, dtype=tf.float32, name="one")
    tf.add(x, one, name="y")

  with tf.Session(graph=graph) as sess:
    save_consts(sess, test_dir)
    save_graph(graph, "test_placeholder", test_dir)
Beispiel #3
0
def generate():
  test_dir = os.path.dirname(__file__)
  graph = tf.Graph()
  with graph.as_default():
    x = tf.constant(0.5 * np.random.randn(5, 5), name="x", dtype=tf.float32)
    relu = tf.nn.relu(x, name="relu")

  with tf.Session(graph=graph) as sess:
    save_consts(sess, test_dir)
    save_graph(graph, "test_relu", test_dir)
    np_relu = relu.eval()
    save_idx(np_relu, os.path.join(test_dir, "output_relu.idx"))
Beispiel #4
0
def generate():
  test_dir = os.path.dirname(__file__)
  graph = tf.Graph()
  with graph.as_default():
    x = tf.constant(np.random.rand(5, 3), dtype=tf.float32, name="x")
    arg_max = tf.argmax(x, axis=1, name='argmax')

  with tf.Session(graph=graph) as sess:
    save_consts(sess, test_dir)
    save_graph(graph, "test_argmax", test_dir)
    np_argmax = arg_max.eval()
    save_idx(np_argmax, os.path.join(test_dir, "output_argmax.idx"))
Beispiel #5
0
def generate():
    test_dir = os.path.dirname(__file__)
    graph = tf.Graph()
    with graph.as_default():
        x = tf.constant(np.random.rand(2, 2, 2), dtype=tf.float32, name="x")
        max_x3_1 = tf.reduce_max(x, axis=2, name="max_x3_1")

    with tf.Session(graph=graph) as sess:
        save_consts(sess, test_dir)
        save_graph(graph, "test_max_3_1", test_dir)
        np_max_x3_1 = max_x3_1.eval()
        save_idx(np_max_x3_1, os.path.join(test_dir, "output_max_x3_1.idx"))
Beispiel #6
0
def generate():
  test_dir = os.path.dirname(__file__)
  graph = tf.Graph()
  with graph.as_default():
    x = tf.constant(np.random.rand(2, 2, 2), dtype=tf.float32, name="x")
    min_x = tf.reduce_min(x, axis=1, name="min_x_2")

  with tf.Session(graph=graph) as sess:
    save_consts(sess, test_dir)
    save_graph(graph, "test_min_2", test_dir)
    np_min_x = min_x.eval()
    save_idx(np_min_x, os.path.join(test_dir, "min_2/output_min_x2.idx"))
Beispiel #7
0
def generate():
    test_dir = os.path.dirname(__file__)
    graph = tf.Graph()
    with graph.as_default():
        x = tf.constant(np.random.randn(10), dtype=tf.float32, name='x')
        output_x = tf.reshape(x, [5, -1], name="output_x")

    with tf.Session(graph=graph) as sess:
        save_consts(sess, test_dir)
        save_graph(graph, 'test_reshape_5', test_dir)
        np_output = output_x.eval()
        save_idx(np_output, os.path.join(test_dir, 'output_x.idx'))
Beispiel #8
0
def generate():
    test_dir = os.path.dirname(__file__)
    graph = tf.Graph()
    with graph.as_default():
        x = tf.constant(np.array([1, 2, 3, 4]), dtype=tf.float32, name="x")
        y = tf.constant(np.array([1, 1, 1, 1]), dtype=tf.float32, name="y")
        z = tf.add(x, y, name="z")

    with tf.Session(graph=graph) as sess:
        save_consts(sess, test_dir)
        save_graph(graph, "test_add", test_dir)
        np_z = z.eval()
        save_idx(np_z, os.path.join(test_dir, "output_z.idx"))
Beispiel #9
0
def generate():
  """ test 1 for min op (fixed with hotfix/64)
  """
  test_dir = os.path.dirname(__file__)
  graph = tf.Graph()
  with graph.as_default():
    x = tf.constant(np.random.rand(2, 2, 2), dtype=tf.float32, name="x")
    min_x = tf.reduce_min(x, name="min_x_1")

  with tf.Session(graph=graph) as sess:
    save_consts(sess, test_dir)
    save_graph(graph, "test_min_1", test_dir)
    np_min_x = min_x.eval()
    save_idx(np_min_x, os.path.join(test_dir, "output_min_x1.idx"))
Beispiel #10
0
def generate():
    test_dir = os.path.dirname(__file__)
    graph = tf.Graph()
    with graph.as_default():
        w = tf.constant(np.arange(1, 10).reshape((3, 3)),
                        dtype=tf.float32,
                        name="w")
        x = tf.constant(np.ones((3, 1)) / 3, dtype=tf.float32, name="x")
        z = tf.matmul(w, x, name='z')

    with tf.Session(graph=graph) as sess:
        save_consts(sess, test_dir)
        save_graph(graph, "test_matmul", test_dir)
        np_z = z.eval()
        save_idx(np_z, os.path.join(test_dir, "output_z.idx"))
Beispiel #11
0
def generate():
    """flatten (fail, since we did not have QuantizedReshape in uTensor)
  """
    test_dir = os.path.dirname(__file__)
    graph = tf.Graph()
    with graph.as_default():
        x = tf.constant(np.arange(10).reshape(2, 5),
                        dtype=tf.float32,
                        name='x')
        output_x = tf.reshape(x, shape=[-1], name='x_reshape')

    with tf.Session(graph=graph) as sess:
        save_consts(sess, test_dir)
        save_graph(graph, 'test_reshape_1', test_dir)
        np_output = output_x.eval()
        save_idx(np_output, os.path.join(test_dir, 'output_x.idx'))
Beispiel #12
0
def generate():
  """
  VALID padding
  small floats
  """
  test_dir = os.path.abspath(os.path.dirname(__file__))
  graph = tf.Graph()
  with graph.as_default():
    input_data = np.arange(0, 1.0, 1.0 / (3 * 10 * 10 * 5)).reshape((3, 10, 10, 5))
    x = tf.constant(input_data, dtype=tf.float32, name="x")
    pool1 = tf.nn.max_pool(x, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
                           padding='VALID', name='pool1')

  with tf.Session(graph=graph) as sess:
    save_consts(sess, test_dir)
    save_graph(graph, 'test_max_pool_1', test_dir)
    np_output = pool1.eval()
    save_idx(np_output, os.path.join(test_dir, 'output_pool.idx'))
Beispiel #13
0
def generate():
    """the reshape op will be used in this case since tensorflow will flatten 
  the input tensor and find the min/max value for quantized matmul
  """
    test_dir = os.path.dirname(__file__)
    graph = tf.Graph()
    with graph.as_default():
        x = tf.placeholder(tf.float32, shape=[None, 5], name='x')
        w = tf.constant(0.5 * np.random.randn(5, 3),
                        dtype=tf.float32,
                        name='w')
        y = tf.matmul(x, w, name='y')

    np_x = 0.5 * np.random.randn(1, 5).astype(np.float32)
    with tf.Session(graph=graph) as sess:
        save_consts(sess, test_dir)
        save_graph(graph, 'test_reshape_3', test_dir)
        np_output = y.eval(feed_dict={'x:0': np_x})
        save_idx(np_x, os.path.join(test_dir, 'input_x.idx'))
        save_idx(np_output, os.path.join(test_dir, 'output_y.idx'))
Beispiel #14
0
def generate():
  """
  add test 3 (broadcasting)
  """
  test_dir = os.path.dirname(__file__)

  np.random.seed(1234)
  graph = tf.Graph()
  with graph.as_default():
    x = tf.constant(np.random.randn(3, 3), 
                    dtype=tf.float32,
                    name='x')
    b = tf.constant(1, dtype=tf.float32, name='b')
    z = tf.add(b, x, name='z')

  with tf.Session(graph=graph) as sess:
    save_consts(sess, test_dir)
    save_graph(graph, "test_add_3", test_dir)
    np_z = z.eval()
    save_idx(np_z, os.path.join(test_dir, "output_z.idx"))
Beispiel #15
0
def generate():
  test_dir = os.path.dirname(__file__)
  np.random.seed(1234)
  graph = tf.Graph()
  with graph.as_default():
    x = tf.constant(np.random.random((3, 5, 5, 3)),
                    dtype=tf.float32,
                    name="x")
    w_filter = tf.constant(np.random.random((3, 3, 3, 2)), 
                           dtype=tf.float32,
                           name="w_filter")
    out_conv = tf.nn.conv2d(x, w_filter, 
                            strides=[1, 2, 2, 1], 
                            padding='VALID', 
                            name="out_conv")
  
  with tf.Session(graph=graph) as sess:
    save_consts(sess, test_dir)
    save_graph(graph, "test_conv", test_dir)
    np_out = sess.run(out_conv)
    save_idx(np_out, os.path.join(test_dir, "output_conv.idx"))
Beispiel #16
0
def generate():
    test_dir = os.path.dirname(__file__)
    X = np.stack([0.5 * np.random.randn(50), 0.1 * np.random.randn(50)],
                 axis=1)
    bias = 0.5
    W = np.array([0.29, 0.18])
    Y = X.dot(W) + bias + np.random.randn(50) * 0.01

    train_graph = tf.Graph()
    with train_graph.as_default():
        tf_x = tf.constant(X, dtype=tf.float32, name="X")
        tf_y = tf.constant(Y, dtype=tf.float32, name="Y")

        tf_w = tf.Variable(np.random.randn(2, 1), dtype=tf.float32, name="W")
        tf_b = tf.Variable(0, dtype=tf.float32, name="b")
        tf_yhat = tf.add(tf.matmul(tf_x, tf_w), tf_b, name="yhat")
        loss = tf.reduce_mean(tf.pow(tf_yhat - tf_y, 2), name="loss")

        train_op = tf.train.AdamOptimizer(0.001).minimize(loss,
                                                          name="train_op")

    with tf.Session(graph=train_graph) as sess:
        tf.global_variables_initializer().run()
        for step in range(1, 10001):
            _, l = sess.run([train_op, loss])
            if step % 1000 == 0:
                print("step:", step)
                print("loss:", l)
        const_graphdef = graph_util.convert_variables_to_constants(
            sess, train_graph.as_graph_def(), ["yhat"])
        graph = tf.Graph()
        with graph.as_default():
            tf.import_graph_def(const_graphdef, name='')
        with tf.Session(graph=graph) as sess:
            save_consts(sess, test_dir)
            save_graph(graph, "test_linreg", test_dir)
            pred = graph.get_tensor_by_name("yhat:0")
            np_pred = pred.eval()
            save_idx(np_pred, os.path.join(test_dir, "output_yhat.idx"))
Beispiel #17
0
def generate():
    """
  VALID padding
  large floats
  """
    test_dir = os.path.abspath(os.path.dirname(__file__))
    graph = tf.Graph()
    with graph.as_default():
        x = tf.constant(np.arange(3 * 4 * 4 * 5).reshape((3, 4, 4, 5)),
                        dtype=tf.float32,
                        name='x')
        pool = tf.nn.max_pool(x,
                              ksize=[1, 3, 3, 1],
                              strides=[1, 2, 2, 1],
                              padding='VALID',
                              name='pool2')

    with tf.Session(graph=graph) as sess:
        save_consts(sess, test_dir)
        save_graph(graph, 'test_max_pool_2', test_dir)
        out_pool = pool.eval()
        save_idx(out_pool, os.path.join(test_dir, 'output_max_pool_2.idx'))
Beispiel #18
0
def generate():
    """Test SAME paddding

  expecting small error
  """
    test_dir = os.path.abspath(os.path.dirname(__file__))
    graph = tf.Graph()
    np.random.seed(3690)
    with graph.as_default():
        input_data = np.random.randint(0, 256, (3, 4, 4, 5), dtype=np.uint8)
        x = tf.constant(input_data, dtype=tf.float32, name="x")
        pool5 = tf.nn.max_pool(x,
                               ksize=[1, 3, 3, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME',
                               name='pool5')

    with tf.Session(graph=graph) as sess:
        save_consts(sess, test_dir)
        save_graph(graph, 'test_max_pool_5', test_dir)
        np_output = pool5.eval()
        save_idx(np_output, os.path.join(test_dir, 'output_max_pool_5.idx'))
Beispiel #19
0
def generate():
    """Test SAME paddding

  pad_top == 0
  pad_left == 0
  larger floats
  """
    test_dir = os.path.abspath(os.path.dirname(__file__))
    graph = tf.Graph()
    with graph.as_default():
        input_data = np.arange(0, 1, 1 / (3 * 4 * 4 * 5)).reshape((3, 4, 4, 5))
        x = tf.constant(input_data, dtype=tf.float32, name="x")
        pool3 = tf.nn.max_pool(x,
                               ksize=[1, 3, 3, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME',
                               name='pool3')

    with tf.Session(graph=graph) as sess:
        save_consts(sess, test_dir)
        save_graph(graph, 'test_max_pool_3', test_dir)
        np_output = pool3.eval()
        save_idx(np_output, os.path.join(test_dir, 'output_max_pool_3.idx'))
Beispiel #20
0
def generate():
    """Test SAME paddding

  pad_left != 0
  pad_top != 0
  """
    test_dir = os.path.abspath(os.path.dirname(__file__))
    graph = tf.Graph()
    np.random.seed(3690)
    with graph.as_default():
        input_data = np.random.randint(0, 256, (3, 13, 13, 5))
        x = tf.constant(input_data, dtype=tf.float32, name="x")
        pool6 = tf.nn.max_pool(x,
                               ksize=[1, 7, 7, 1],
                               strides=[1, 3, 3, 1],
                               padding='SAME',
                               name='pool6')

    with tf.Session(graph=graph) as sess:
        save_consts(sess, test_dir)
        save_graph(graph, 'test_max_pool_6', test_dir)
        np_output = pool6.eval()
        save_idx(np_output, os.path.join(test_dir, 'output_max_pool_6.idx'))