# `mobile\_exporter <https://github.com/caffe2/caffe2/blob/master/caffe2/python/predictor/mobile_exporter.py>`__
# to generate the two model protobufs that can run on mobile. The first is
# used to initialize the network with the correct weights, and the second
# actual runs executes the model. We will continue to use the small
# super-resolution model for the rest of this tutorial.
#

# extract the workspace and the model proto from the internal representation
c2_workspace = prepared_backend.workspace
c2_model = prepared_backend.predict_net

# Now import the caffe2 mobile exporter
from caffe2.python.predictor import mobile_exporter

# call the Export to get the predict_net, init_net. These nets are needed for running things on mobile
init_net, predict_net = mobile_exporter.Export(c2_workspace, c2_model, c2_model.external_input)

# Let's also save the init_net and predict_net to a file that we will later use for running them on mobile
with open('init_net.pb', "wb") as fopen:
    fopen.write(init_net.SerializeToString())
with open('predict_net.pb', "wb") as fopen:
    fopen.write(predict_net.SerializeToString())


######################################################################
# ``init_net`` has the model parameters and the model input embedded in it
# and ``predict_net`` will be used to guide the ``init_net`` execution at
# run-time. In this tutorial, we will use the ``init_net`` and
# ``predict_net`` generated above and run them in both normal Caffe2
# backend and mobile and verify that the output high-resolution cat image
# produced in both runs is the same.
Exemplo n.º 2
0
# the inputs/outputs of the model are manually specified.
pe_meta = pe.PredictorExportMeta(
    predict_net=deploy_model.net.Proto(),
    parameters=[str(b) for b in deploy_model.params],
    inputs=["data"],
    outputs=["softmax"],
)

# save the model to a file. Use minidb as the file format
pe.save_to_db("minidb", os.path.join(root_folder, "mnist_model.minidb"),
              pe_meta)
print("The deploy model is saved to: " + root_folder + "/mnist_model.minidb")

workspace.RunNetOnce(deploy_model.param_init_net)

init_net, predict_net = mobile_exporter.Export(workspace, deploy_model.net,
                                               deploy_model.params)
with open("init_net.pb", "wb") as f:
    f.write(init_net.SerializeToString())
with open("predict_net.pb", "wb") as f:
    f.write(predict_net.SerializeToString())
with open("predict_net.pbtxt", "wb") as f:
    f.write(str(deploy_model.net.Proto()))

# Now we can load the model back and run the prediction to verify it works.

# we retrieve the last input data out and use it in our prediction test
# before we scratch the workspace
blob = workspace.FetchBlob("data")

# reset the workspace, to make sure the model is actually loaded
workspace.ResetWorkspace(root_folder)
print(onnx.helper.printable_graph(model.graph))

import caffe2.python.onnx.backend as backend
import numpy as np

rep = backend.prepare(model, device="CUDA:0")  # or "CPU"
#rep = backend.prepare(model, device="CPU")
# For the Caffe2 backend:
#     rep.predict_net is the Caffe2 protobuf for the network
#     rep.workspace is the Caffe2 workspace for the network
#       (see the class caffe2.python.onnx.backend.Workspace)
outputs = rep.run(
    np.random.randn(1, Global.img_chn, Global.img_h,
                    Global.img_w).astype(np.float32))
# To run networks with more than one input, pass a tuple
# rather than a single numpy ndarray.
print(outputs[0])

c2_workspace = rep.workspace
c2_graph = rep.predict_net

from caffe2.python.predictor import mobile_exporter

init_net, predict_net = mobile_exporter.Export(c2_workspace, c2_graph,
                                               c2_graph.external_input)

with io.open(Global.initnet, 'wb') as f:
    f.write(init_net.SerializeToString())
with open(Global.prednet, 'wb') as f:
    f.write(predict_net.SerializeToString())
Exemplo n.º 4
0
def main(args):
    # User defined keyword arguments
    kwargs = {"order": "NCHW"}
    kwargs.update(dict(args.kwargs))

    model = ModelHelper(name=args.benchmark_name)

    op_type = args.operator  # assumes a brew type op name
    input_name = args.input_name
    output_name = args.output_name

    iters = int(args.iters)
    for i in range(iters):
        input_blob_name = input_name + (str(i) if i > 0 and args.chain else '')
        output_blob_name = output_name + str(i + 1)
        add_op = getattr(brew, op_type)
        add_op(model, input_blob_name, output_blob_name, **kwargs)
        if args.chain:
            input_name, output_name = output_name, input_name

    workspace.RunNetOnce(model.param_init_net)
    extra_init_net_ops = []

    def make_blob_on_context(blob_name, blob_data, context):
        if context.upper() != "CPU":
            blob_name_modified = "{}_CPU".format(blob_name)
        else:  # CPU case is simple
            blob_name_modified = blob_name

        fill_op = core.CreateOperator(
            "GivenTensorFill", [], [blob_name_modified],
            arg=[
                utils.MakeArgument("shape", blob_data.shape),
                utils.MakeArgument("values", blob_data)
            ])
        extra_init_net_ops.append(fill_op)

        # We need to create CPU blobs and add some copy operations in
        # the init_net
        if context.upper() == "OPENGL":
            copy_op = core.CreateOperator("CopyToOpenGL", [blob_name_modified],
                                          [blob_name])
            extra_init_net_ops.append(copy_op)

    for unparsed_blob in args.blob:
        name, unparsed_dims = unparsed_blob.split('=')
        dims = [int(d) for d in unparsed_dims.split(',')]
        np_input = np.random.rand(*dims).astype(np.float32)
        make_blob_on_context(name, np_input, args.context)

    init_net, predict_net = mobile_exporter.Export(workspace, model.net,
                                                   model.params)
    init_net.op.extend(extra_init_net_ops)

    # Handle manual rewrite
    if args.context.upper() == "OPENGL":
        old_ops = [op for op in predict_net.op]
        del predict_net.op[:]
        for op in old_ops:
            op.type = 'OpenGL{}'.format(op.type)
        predict_net.op.extend(old_ops)

    if args.debug:
        print("init_net:")
        for op in init_net.op:
            print(" ", op.type, op.input, "-->", op.output)
        print("predict_net:")
        for op in predict_net.op:
            print(" ", op.type, op.input, "-->", op.output)

    with open(args.predict_net, 'wb') as f:
        f.write(predict_net.SerializeToString())
    with open(args.init_net, 'wb') as f:
        f.write(init_net.SerializeToString())
Exemplo n.º 5
0
    def test_mobile_exporter_datatypes(self):
        model = ModelHelper(name="mobile_exporter_test_model")
        model.Copy("data_int", "out")
        model.params.append("data_int")
        model.Copy("data_obj", "out_obj")
        model.params.append("data_obj")

        # Create our mobile exportable networks
        workspace.RunNetOnce(model.param_init_net)
        np_data_int = np.random.randint(100,
                                        size=(1, 1, 28, 28),
                                        dtype=np.int32)
        workspace.FeedBlob("data_int", np_data_int)
        np_data_obj = np.array(['aa', 'bb']).astype(np.dtype('O'))
        workspace.FeedBlob("data_obj", np_data_obj)

        init_net, predict_net = mobile_exporter.Export(workspace, model.net,
                                                       model.params)

        workspace.CreateNet(model.net)
        workspace.RunNet(model.net)
        ref_out = workspace.FetchBlob("out")
        ref_out_obj = workspace.FetchBlob("out_obj")

        # Clear the workspace
        workspace.ResetWorkspace()

        # Populate the workspace with data
        workspace.RunNetOnce(init_net)

        # Overwrite the old net
        workspace.CreateNet(predict_net, True)
        workspace.RunNet(predict_net.name)
        manual_run_out = workspace.FetchBlob("out")
        manual_run_out_obj = workspace.FetchBlob("out_obj")
        np.testing.assert_allclose(ref_out,
                                   manual_run_out,
                                   atol=1e-10,
                                   rtol=1e-10)
        np.testing.assert_equal(ref_out_obj, manual_run_out_obj)

        # Clear the workspace
        workspace.ResetWorkspace()

        # Predictor interface test (simulates writing to disk)
        predictor = workspace.Predictor(init_net.SerializeToString(),
                                        predict_net.SerializeToString())

        # Output is a vector of outputs.
        predictor_out = predictor.run([])
        assert len(predictor_out) == 2
        predictor_out_int = predictor_out[1]
        predictor_out_obj = predictor_out[0]
        # The order in predictor_out is non-deterministic. Use type of the entry
        # to figure out what to compare it to.
        if isinstance(predictor_out[1][0], bytes):
            predictor_out_int = predictor_out[0]
            predictor_out_obj = predictor_out[1]
        np.testing.assert_allclose(ref_out,
                                   predictor_out_int,
                                   atol=1e-10,
                                   rtol=1e-10)
        np.testing.assert_equal(ref_out_obj, predictor_out_obj)
Exemplo n.º 6
0
def main():
    root_path = '/home/osboxes/zementis/scalogram/fault_diagnosis'
    data_path = os.path.join(root_path, 'data')
    labels_path = os.path.join(data_path, 'labels.txt')
    labels_to_classes_map = get_labels_to_classes_map(labels_path)
    fault_types_path = {
        'baseLine': os.path.join(data_path, 'raw_signals', 'baseLine'),
        'rollingDefect': os.path.join(data_path, 'raw_signals',
                                      'rollingDefect'),
        'innerRace': os.path.join(data_path, 'raw_signals', 'innerRace'),
        'outerRace': os.path.join(data_path, 'raw_signals', 'outerRace')
    }
    sub_signal_len = 400
    # The sample rate is 12 kHz and the approximate motor speed is 1797 RPM. Therefore, there are approximately 401
    # sample points per revolution (12000 / (1797 / 60)).
    for fault_type, fault_dir_path in fault_types_path.items():
        sub_signals = get_sub_signals(fault_dir_path, sub_signal_len)
        sub_signal_idx = 0
        for sub_signal in sub_signals:
            sub_signal_idx += 1
            scalo = get_scalogram(sub_signal)
            scaled_scalo = get_scaled_data(
                scalo)  # scales an array to have values between 0.0 and 1.0
            img_obj = PIL.Image.fromarray(scaled_scalo)
            img_f_name = str(sub_signal_idx) + '_' + fault_type + '.tiff'
            img_obj.save(os.path.join(data_path, img_f_name))
            if sub_signal_idx == 50:
                break  # stop after creating 50 images in each class
    # Create txt files mapping image names to classes
    img_to_class_paths = create_img_to_class_files(data_path,
                                                   labels_to_classes_map)
    # Create lmdb files
    lmdb_paths = write_lmdb_files(data_path, img_to_class_paths)
    model_files_path = os.path.join(root_path, 'model_files')
    if not os.path.isdir(model_files_path):
        os.makedirs(model_files_path)
    workspace.ResetWorkspace(model_files_path)
    unique_timestamp = str(
        datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
    checkpoint_dir = os.path.join(model_files_path, unique_timestamp)
    os.makedirs(checkpoint_dir)
    print("Checkpoint output location: ", checkpoint_dir)

    # Dataset specific params
    image_width = sub_signal_len
    image_height = sub_signal_len
    image_channels = 1
    num_classes = 4
    init_net_out_fname = 'init_net.pb'
    predict_net_out_fname = 'predict_net.pb'

    # Training params
    n_iters = 600  # total training iterations
    batch_size = 10  # batch size for training
    n_val_images = 30  # total number of validation images
    validation_interval = 50  # validate every <validation_interval> training iterations
    n_checkpoint_iters = 200  # output checkpoint db every <checkpoint_iters> iterations

    # TRAINING MODEL
    train_model = model_helper.ModelHelper(name="train_net")
    data, label = add_input(train_model,
                            batch_size=batch_size,
                            db=lmdb_paths['train'],
                            db_type='lmdb')
    softmax = add_cnn_model_1(train_model, data, num_classes, image_height,
                              image_width, image_channels)
    add_optmzer_lossfunc(train_model, softmax, label)
    add_check_points(train_model,
                     unique_timestamp,
                     n_checkpoint_iters,
                     db_type="lmdb")

    # VALIDATION MODEL
    # Initialize with ModelHelper class without re-initializing params
    val_model = model_helper.ModelHelper(name="val_net", init_params=False)
    data, label = add_input(val_model,
                            batch_size=n_val_images,
                            db=lmdb_paths['val'],
                            db_type='lmdb')
    softmax = add_cnn_model_1(val_model, data, num_classes, image_height,
                              image_width, image_channels)
    add_accuracy(val_model, softmax, label)

    # DEPLOY MODEL
    # Initialize with ModelHelper class without re-initializing params
    deploy_model = model_helper.ModelHelper(name="deploy_net",
                                            init_params=False)
    # Add model definition, expect input blob called "data"
    add_cnn_model_1(deploy_model, "data", num_classes, image_height,
                    image_width, image_channels)
    print("Training, Validation, and Deploy models all defined!")

    # Initialize and create the training network
    workspace.RunNetOnce(train_model.param_init_net)
    workspace.CreateNet(train_model.net, overwrite=True)
    # Initialize and create validation network
    workspace.RunNetOnce(val_model.param_init_net)
    workspace.CreateNet(val_model.net, overwrite=True)
    # Placeholder to track loss and validation accuracy
    training_loss = np.zeros(int(math.ceil(n_iters / validation_interval)))
    val_accuracy = np.zeros(int(math.ceil(n_iters / validation_interval)))
    val_count = 0
    val_iter_list = np.zeros(int(math.ceil(n_iters / validation_interval)))

    # run the network (forward & backward pass)
    for i in range(n_iters):
        workspace.RunNet(train_model.net)
        # Validate every <validation_interval> training iterations
        if (i % validation_interval) == 0:
            print("Training iter: ", i)
            training_loss[val_count] = workspace.FetchBlob('loss')
            workspace.RunNet(val_model.net)
            val_accuracy[val_count] = workspace.FetchBlob('accuracy')
            print("Loss: ", str(training_loss[val_count]))
            print("Validation accuracy: ", str(val_accuracy[val_count]) + "\n")
            val_iter_list[val_count] = i
            val_count += 1

    fig = pyplot.figure()
    fig.add_subplot(111)
    pyplot.title("Training Loss and Validation Accuracy")
    pyplot.plot(val_iter_list, training_loss, 'b')
    pyplot.plot(val_iter_list, val_accuracy, 'r')
    pyplot.xlabel("Training iteration")
    pyplot.legend(('Training Loss', 'Validation Accuracy'), loc='upper right')
    pyplot.savefig("loss_and_accuracy.png")
    pyplot.close()

    # Save trained model
    workspace.RunNetOnce(deploy_model.param_init_net)
    workspace.CreateNet(deploy_model.net, overwrite=True)
    init_net, predict_net = mobile_exporter.Export(workspace, deploy_model.net,
                                                   deploy_model.params)
    init_net_out_path = os.path.join(checkpoint_dir, init_net_out_fname)
    predict_net_out_path = os.path.join(checkpoint_dir, predict_net_out_fname)
    with open(init_net_out_path, 'wb') as f:
        f.write(init_net.SerializeToString())
    with open(predict_net_out_path, 'wb') as f:
        f.write(predict_net.SerializeToString())
    print("Model saved as " + init_net_out_path + " and " +
          predict_net_out_path)
Exemplo n.º 7
0
            data_parallel_model.GetLearningRateBlobNames(train_model)[0])

        values = [
            e + 1,
            lr,
            loss_sum / batch_num,
            correct / batch_num,
            test_res['loss'],
            test_res['accuracy'],
            time_ep,
        ]
        table = tabulate.tabulate([values],
                                  columns,
                                  tablefmt='simple',
                                  floatfmt='8.4f')
        if e % 25 == 0:
            table = table.split('\n')
            table = '\n'.join([table[1]] + table)
        else:
            table = table.split('\n')[2]
        print(table)

    checkpoint_params = data_parallel_model.GetCheckpointParams(train_model)

    init_net, _ = mobile_exporter.Export(workspace, deploy_model.net,
                                         checkpoint_params)
    with open("predict_net.pb", 'wb') as f:
        f.write(deploy_model.net._net.SerializeToString())
    with open("init_net.pb", 'wb') as f:
        f.write(init_net.SerializeToString())