Ejemplo n.º 1
0
label_path = download_testdata(label_map_url, label_map, module='data')

######################################################################
# Import model
# ------------
# Creates tensorflow graph definition from protobuf file.

with tf_compat_v1.gfile.GFile(model_path, 'rb') as f:
    graph_def = tf_compat_v1.GraphDef()
    graph_def.ParseFromString(f.read())
    graph = tf.import_graph_def(graph_def, name='')
    # Call the utility to import the graph definition into default graph.
    graph_def = tf_testing.ProcessGraphDefParam(graph_def)
    # Add shapes to the graph.
    with tf_compat_v1.Session() as sess:
        graph_def = tf_testing.AddShapesToGraphDef(sess, 'softmax')

######################################################################
# Decode image
# ------------
# .. note::
#
#   tensorflow frontend import doesn't support preprocessing ops like JpegDecode.
#   JpegDecode is bypassed (just return source node).
#   Hence we supply decoded frame to TVM instead.
#

from PIL import Image

image = Image.open(img_path).resize((299, 299))
Ejemplo n.º 2
0
from PIL import Image
try:
    tf_compat_v1 = tf.compat.v1
except ImportError:
    tf_compat_v1 = tf

# Load Model
MODEL_PATH = "./model/yolov3_visdrone.pb"
with tf_compat_v1.gfile.GFile(MODEL_PATH, "rb") as f:
    graph_def = tf_compat_v1.GraphDef()
    graph_def.ParseFromString(f.read())
    graph = tf.import_graph_def(graph_def, name="")
    # Call the utility to import the graph definition into default graph.
    graph_def = tf_testing.ProcessGraphDefParam(graph_def)
    with tf_compat_v1.Session() as sess:
        graph_def = tf_testing.AddShapesToGraphDef(sess,
                                                   "pred_multi_scale/concat")

IMAGE_PATH = "./images/road.jpg"
image = Image.open(IMAGE_PATH, 'r').resize((416, 416))
x = np.array(image)
x = x[np.newaxis, ...]
shape_dict = {"input/input_data": x.shape}
mod, params = relay.frontend.from_tensorflow(graph_def,
                                             layout=None,
                                             shape=shape_dict)
print("Tensorflow protobuf imported to relay frontend.")

target = 'llvm'
target_host = 'llvm'
with tvm.transform.PassContext(opt_level=1):
    lib = relay.build(mod,
Ejemplo n.º 3
0
                  "",
                  model_path,
                  as_text=False)
""" 
    Create the graph in TVM and compile it 
"""
with tf.io.gfile.GFile(model_path, "rb") as f:
    graph_def = tf.compat.v1.GraphDef()
    graph_def.ParseFromString(f.read())
    graph = tf.import_graph_def(graph_def, name="")

    graph_def = tf_testing.ProcessGraphDefParam(graph_def)

    # Add shapes to the graph.
    with tf.Session() as sess:
        graph_def = tf_testing.AddShapesToGraphDef(sess, "output")

# Import TF graph definition to Relay frontend
shape_dict = {"X": input_shape}
mod, parameters = relay.frontend.from_tensorflow(graph_def,
                                                 layout=layout,
                                                 shape=shape_dict)

# Compile the graph
with relay.build_config(opt_level=3):
    graph, lib, params = relay.build_module.build(mod,
                                                  target=target,
                                                  params=parameters)
""" Execute and evaluate the graph """
ctx = tvm.cpu()
data_tvm = tvm.nd.array((np.random.uniform(size=input_shape)).astype(dtype))
Ejemplo n.º 4
0
x = CustomLayer(10, name='MyLayer')(x)
w = tf.Variable(tf.zeros([10, 8]))
b = tf.Variable(tf.zeros([8]))
x = tf.nn.sigmoid(tf.matmul(x, w) + b, name='custom_layer')

outputs = tf.layers.dense(x, 5, activation='softmax', name='Outputs')

losses = tf.keras.losses.categorical_crossentropy(ph_ytrue, outputs)

# model = keras.models.Model(inputs, outputs)
# model.compile('sgd', loss=tf.keras.losses.CategoricalCrossentropy())
# model.summary()

sess = tf.Session()
sess.run(tf.global_variables_initializer())

graph_def = tf_testing.AddShapesToGraphDef(sess, 'Outputs/Softmax')

mod, params = relay.frontend.from_tensorflow(graph_def, layout=None)
with relay.build_config(opt_level=3):
    graph, lib, params = relay.build(mod, target='llvm', params=params)

ctx = tvm.cpu()
e = relay.create_executor(mod=mod, ctx=ctx)
m = tvm.contrib.graph_runtime.create(graph, lib, ctx)

x = tvm.nd.array(np.ones([1, 10]).astype(np.float32))
m.set_input(**params)
m.run(**{'Input_ph': x})
out = m.get_output(0)
print(out)
Ejemplo n.º 5
0
# model_path = "/home/lesliefang/tvm/test_script/mobilenet_v1/mobilenet_v1_1.0_224_frozen-with-shapes.pb"
# model_path = "/root/.tvm_test_data/tf/InceptionV1/classify_image_graph_def-with_shapes.pb"

with tf_compat_v1.gfile.GFile(model_path, 'rb') as f:
    graph_def = tf_compat_v1.GraphDef()
    graph_def.ParseFromString(f.read())

    graph = tf_compat_v1.import_graph_def(graph_def, name='')

    # Call the utility to import the graph definition into default graph.
    graph_def = tf_testing.ProcessGraphDefParam(graph_def)

    # Add shapes to the graph.
    with tf_compat_v1.Session() as sess:
        graph_def = tf_testing.AddShapesToGraphDef(sess, OUTPUTS)
        #graph_def = tf_testing.AddShapesToGraphDef(sess, 'MobilenetV1/Predictions/Reshape_1')
        #graph_def = tf_testing.AddShapesToGraphDef(sess, 'softmax')

shape_dict = {INPUTS: (1, 784)}
#shape_dict = None

mod, params = relay.frontend.from_tensorflow(graph_def,
                                             layout=layout,
                                             shape=shape_dict)

print("Tensorflow protobuf imported to relay frontend.")

with tvm.transform.PassContext(opt_level=3):
    graph, lib, params = relay.build(mod,
                                     target=target,
Ejemplo n.º 6
0
def export_module(opts):
    # Target settings
    layout = "NCHW"

    # Download required files
    from tvm.contrib.download import download_testdata
    model_path = download_testdata(model_url, model_file_name, module=['tf', 'keyword_spotting'])
    label_path = download_testdata(label_url, label_name, module=['data'])

    # Import model
    with tf_compat_v1.gfile.GFile(model_path, 'rb') as f:
        graph_def = tf_compat_v1.GraphDef()
        graph_def.ParseFromString(f.read())
        graph = tf.import_graph_def(graph_def, name='')
        graph_def = tf_testing.ProcessGraphDefParam(graph_def)
        with tf_compat_v1.Session() as sess:
            graph_def = tf_testing.AddShapesToGraphDef(sess, 'labels_softmax')

    build_dir = opts.out_dir
    if not os.path.exists(build_dir):
        os.makedirs(build_dir)

    ##save original TF graph
    if DEBUG_LOG:
        with open(os.path.join(build_dir, f'{model_name}_graph_original.log'), 'w') as orig_file:
            orig_file.write(str(graph_def))

    ##remove pre-processing nodes and fix begining
    nodes = []
    ##add first op
    input_dim0 = 1
    input_dim1 = 49
    input_dim2 = 10
    new_input = graph_def.node.add()
    new_input.op = 'Placeholder'
    new_input.name = 'Mfcc'
    new_input.attr["dtype"].CopyFrom(attr_value_pb2.AttrValue(
            type=dtypes.float32.as_datatype_enum))
                
    nodes.append(new_input)

    removed_count = 0
    for ii, node in enumerate(graph_def.node, start=0):
        if node.op == 'DecodeWav' \
        or node.op == 'AudioSpectrogram' \
        or node.op == 'Mfcc' \
        or node.op == 'Placeholder' \
        or node.op == 'wav_data':
            removed_count += 1
            pass
        else:
            nodes.append(node) 
    print(f'NUM of layers removed: {removed_count}')

    new_graph = tf_compat_v1.GraphDef()
    new_graph.node.extend(nodes)
    ##log new graph
    if DEBUG_LOG:
        with open(os.path.join(build_dir, f'{model_name}_graph_new.log'), 'w') as new_graph_log:
            new_graph_log.write(str(new_graph))

    ##get mod and params with new graph
    shape_dict = {'Mfcc': (1, 49, 10)}
    mod, params = relay.frontend.from_tensorflow(new_graph,
                                                layout=layout,
                                                shape=shape_dict)

    if DEBUG_LOG:
        with open(os.path.join(build_dir, f'{model_name}_mod.log'), 'w') as mod_file:
            mod_file.write(str(mod))
        with open(os.path.join(build_dir, f'{model_name}_param.log'), 'w') as param_log:
            param_log.write(str(params))

    #quantization
    if opts.quantize:
        if not opts.global_scale:
            raise RuntimeError('Global Scale is not valid!')
        global_scale = float(opts.global_scale)
        print('INFO: Quantizing...')
        print(f'INFO: Global Scale: {global_scale}')
        with relay.quantize.qconfig(calibrate_mode='global_scale', 
                                    global_scale=global_scale,
                                    skip_conv_layers=[0]):
            mod = relay.quantize.quantize(mod, params)

        if DEBUG_LOG:
            with open(os.path.join(build_dir, f'{model_name}_mod_quantized.log'), 'w') as mod_log:
                mod_log.write(str(mod))

    #save module
    if opts.quantize:
        file_path = f'{build_dir}/module_gs_{global_scale}.pickle'
        with open(file_path, 'wb') as h1:
            pickle.dump(mod, h1, protocol=pickle.HIGHEST_PROTOCOL)
            print(f'INFO: {file_path} saved!')
        with open(f'{build_dir}/module_gs_{global_scale}.txt', 'w') as f:
            f.write(mod.astext())
    else:
        file_path = f'{build_dir}/module.pickle'
        with open(file_path, 'wb') as h1:
            pickle.dump(mod, h1, protocol=pickle.HIGHEST_PROTOCOL)
            print(f'INFO: {file_path} saved!')
        param_path = f'{build_dir}/params.bin'
        with open(param_path, 'wb') as f_params:
            f_params.write(relay.save_param_dict(params))
            print(f'INFO: {param_path} saved!')
        with open(f'{build_dir}/module.txt', 'w') as f:
            f.write(mod.astext())
    return mod, params