Example #1
0
def test_avg_pool():
    save_dir = "../tfProfiling/avg_pool"
    image_input = ImageInput(seed=713,
                             batch_size=16,
                             image_h=224,
                             image_w=224,
                             image_c=64)
    in_node = image_input.get_placeholder("image", data_type=tf.float32)
    dummy_var = tf.Variable(tf.zeros(
        [2]))  # saver barfs if there are no variables in the graph??
    avg_pool_output = tf.layers.average_pooling2d(inputs=in_node,
                                                  pool_size=[2, 2],
                                                  strides=2,
                                                  padding="same")
    out_node = tf.identity(avg_pool_output, name="output")

    placeholders = [in_node]
    predictions = [out_node]
    tfp = TensorFlowPersistor(save_dir=save_dir, verbose=False)
    predictions_after_freeze = tfp \
        .set_placeholders(placeholders) \
        .set_output_tensors(predictions) \
        .set_test_data(image_input.get_test_data()) \
        .build_save_frozen_graph(skip_intermediate=True)
    print(predictions_after_freeze[0].shape)
Example #2
0
def test_pool_1():
    # [batch, in_height, in_width, in_channels].
    image_input = ImageInput(seed=713,
                             batch_size=1,
                             image_h=4,
                             image_w=4,
                             image_c=2)
    in_node = image_input.get_placeholder("image", data_type=tf.float32)
    dummy_var = tf.Variable(tf.random_uniform(
        [3, 2]))  # saver barfs without a variable
    constr = NNImageOps(in_node)
    constr.set_filter_hw_inout(2, 2, 2, 2)
    constr.set_kernel_hw(2, 2)
    constr.set_stride_hw(1, 1)
    in1 = constr.execute("max_pool")
    out_node = tf.identity(in1, name="output")  # calc required dims by hand

    placeholders = [in_node]
    predictions = [out_node]

    tfp = TensorFlowPersistor(save_dir="pool_1")
    predictions_after_freeze = tfp \
        .set_placeholders(placeholders) \
        .set_output_tensors(predictions) \
        .set_test_data(image_input.get_test_data()) \
        .build_save_frozen_graph()
    print(predictions_after_freeze[0].shape)
Example #3
0
def test_conv_2():
    image_input = ImageInput(seed=713,
                             batch_size=4,
                             image_h=64,
                             image_w=64,
                             image_c=4)
    in_node = image_input.get_placeholder("image", data_type=tf.float32)
    # in_channels must match between input and filter.
    # filter shape is [filter_height, filter_width, in_channels, out_channels]
    filter_one = tf.Variable(tf.random_uniform([4, 5, image_input.image_c, 2]),
                             name="filter1")
    atrous_one = tf.nn.atrous_conv2d(in_node,
                                     filters=filter_one,
                                     rate=8,
                                     padding='SAME',
                                     name="atrous_one")
    filter_two = tf.Variable(tf.random_uniform([31, 31, 2, 1]), name="filter2")
    atrous_two = tf.nn.atrous_conv2d(atrous_one,
                                     filters=filter_two,
                                     rate=2,
                                     padding='VALID')
    out_node = tf.identity(atrous_two, name="output")

    placeholders = [in_node]
    predictions = [out_node]

    tfp = TensorFlowPersistor(save_dir="conv_2")
    predictions_after_freeze = tfp \
        .set_placeholders(placeholders) \
        .set_output_tensors(predictions) \
        .set_test_data(image_input.get_test_data()) \
        .build_save_frozen_graph()
    print(predictions_after_freeze[0].shape)
Example #4
0
def test_conv_4():
    image_input = ImageInput(seed=713,
                             batch_size=3,
                             image_h=4,
                             image_w=4,
                             image_c=5)
    in_node = image_input.get_placeholder("image", data_type=tf.float32)
    constr = NNImageOps(in_node)
    constr.set_filter_size([7, 7, 16, 5])
    constr.set_output_shape([3, 8, 8, 16])
    constr.set_stride_size([1, 2, 2, 1])
    in1 = constr.execute("conv2d_transpose")  # size is (3, 8, 8, 16)
    constr.set_image(in1)
    constr.set_filter_size([2, 2, 32, 16])
    constr.set_output_shape([3, 8, 8, 32])
    constr.set_rate(2)
    in02 = constr.execute("atrous_conv2d_transpose")  # size is (3,8,8,32)
    constr.set_image(in02)
    constr.set_filter_size([2, 2, 32, 2])
    constr.set_pointwise_filter([1, 1, 64, 8])
    constr.set_stride_size([1, 1, 1, 1])
    constr.set_rate([1, 2])
    in2 = constr.execute("separable_conv2d")  # (3, 8, 8, 8)
    constr.set_image(in2)
    constr.set_filter_size([2, 2, 8, 4])
    constr.set_stride_size([1, 1, 1, 1])
    constr.set_rate([3, 2])
    in3 = constr.execute("depthwise_conv2d")  # size (3,8,8,32)
    in33 = tf.space_to_batch_nd(in3, [3, 3],
                                paddings=[[1, 0], [0, 1]])  # size (27,3,3,32)
    constr.set_image(tf.reshape(in33, [27 * 2, 12, 12]))
    constr.set_filter_size([2, 12, 2])
    constr.set_stride_size(1)
    in4 = constr.execute("conv1d")  # size (54,12,2)
    out_node = tf.identity(in4, name="output")
    placeholders = [in_node]
    predictions = [out_node]

    tfp = TensorFlowPersistor(save_dir="conv_4")
    predictions_after_freeze = tfp \
        .set_placeholders(placeholders) \
        .set_output_tensors(predictions) \
        .set_test_data(image_input.get_test_data()) \
        .build_save_frozen_graph()
    print(predictions_after_freeze[0].shape)

    if __name__ == "main":
        test_conv_4()
Example #5
0
def test_conv_0():
    image_input = ImageInput(seed=713,
                             batch_size=4,
                             image_h=28,
                             image_w=28,
                             image_c=3)
    in_node = image_input.get_placeholder("image", data_type=tf.float32)
    constr = NNImageOps(in_node)
    constr.set_filter_hw_inout(h=5, w=5, in_ch=3, out_ch=3)
    constr.set_kernel_hw(3, 3)
    constr.set_stride_hw(3, 3)

    in1 = constr.execute("conv2d")
    constr.set_image(in1)

    in2 = constr.execute("avg_pool")
    constr.set_image(in2)

    in3 = constr.execute("conv2d")
    constr.set_image(in3)

    in4 = constr.execute("max_pool")

    in5 = constr.flatten_convolution(in4)

    out_node = tf.matmul(in5,
                         tf.Variable(tf.random_uniform([3, 2])),
                         name="output")  # calc required dims by hand

    placeholders = [in_node]
    predictions = [out_node]

    tfp = TensorFlowPersistor(save_dir="conv_0")
    predictions_after_freeze = tfp \
        .set_placeholders(placeholders) \
        .set_output_tensors(predictions) \
        .set_test_data(image_input.get_test_data()) \
        .build_save_frozen_graph()
    print(predictions_after_freeze[0].shape)
Example #6
0
def test_conv_3():
    # [batch, in_height, in_width, in_channels].
    image_input = ImageInput(seed=713, batch_size=4, image_h=128, image_w=128, image_c=4)
    in_node = image_input.get_placeholder("image", data_type=tf.float32)
    # [filter_height, filter_width, in_channels, out_channels]. in_channels must match between input and filter.
    filter_one = tf.Variable(tf.random_uniform([4, 5, image_input.image_c]), name="filter1")
    dilation_one = tf.nn.dilation2d(in_node, filter=filter_one, strides=[1, 2, 3, 1], rates=[1, 5, 7, 1],
                                    padding='SAME',
                                    name="dilation_one")
    filter_two = tf.Variable(tf.random_uniform([11, 7, 4]), name="filter2")
    dilation_two = tf.nn.dilation2d(dilation_one, filter=filter_two, strides=[1, 3, 2, 1], rates=[1, 2, 3, 1],
                                    padding='VALID',
                                    name="output")
    out_node = tf.identity(dilation_two, name="output")
    placeholders = [in_node]
    predictions = [out_node]

    tfp = TensorFlowPersistor(save_dir="conv_3")
    predictions_after_freeze = tfp \
        .set_placeholders(placeholders) \
        .set_output_tensors(predictions) \
        .set_test_data(image_input.get_test_data()) \
        .build_save_frozen_graph()
    print(predictions_after_freeze[0].shape)
Example #7
0
def test_conv2d():
    save_dir = "../tfProfiling/conv2d"
    image_input = ImageInput(seed=713,
                             batch_size=16,
                             image_h=256,
                             image_w=256,
                             image_c=3)
    in_node = image_input.get_placeholder("image", data_type=tf.float32)
    conv_2d_output = tf.layers.conv2d(inputs=in_node,
                                      filters=64,
                                      kernel_size=[5, 5],
                                      padding="same",
                                      activation=tf.nn.relu)
    out_node = tf.identity(conv_2d_output, name="output")

    placeholders = [in_node]
    predictions = [out_node]
    tfp = TensorFlowPersistor(save_dir=save_dir, verbose=False)
    predictions_after_freeze = tfp \
        .set_placeholders(placeholders) \
        .set_output_tensors(predictions) \
        .set_test_data(image_input.get_test_data()) \
        .build_save_frozen_graph(skip_intermediate=True)
    print(predictions_after_freeze[0].shape)