Esempio n. 1
0
def build_fake_model():
    try:
        graph = tf.Graph()
        graph_def = tf.GraphDef()
        with tf.Session() as sess:
            x = tf.placeholder(tf.float32, shape=(1, 3, 3, 1), name='x')
            y = tf.constant(np.random.random((2, 2, 1, 1)),
                            dtype=tf.float32,
                            name='y')
            relu_0 = tf.nn.relu(x, name='relu')
            conv = tf.nn.conv2d(input=relu_0,
                                filters=y,
                                strides=[1, 1, 1, 1],
                                padding='VALID',
                                name='conv')
            bias = tf.Variable(tf.ones([1], tf.float32))
            conv_add = tf.nn.bias_add(conv, bias, name='bias_add')
            relu = tf.nn.relu(conv_add)
            op = tf.identity(relu, name='identity')

            sess.run(tf.global_variables_initializer())
            from tensorflow.compat.v1.graph_util import convert_variables_to_constants
            constant_graph = convert_variables_to_constants(
                sess, sess.graph_def, ['identity'])

        graph_def.ParseFromString(constant_graph.SerializeToString())
        with graph.as_default():
            tf.import_graph_def(graph_def, name='')
    except:
        graph = tf.Graph()
        graph_def = tf.compat.v1.GraphDef()
        with tf.compat.v1.Session() as sess:
            x = tf.compat.v1.placeholder(tf.float32,
                                         shape=(1, 3, 3, 1),
                                         name='x')
            y = tf.compat.v1.constant(np.random.random((2, 2, 1, 1)),
                                      dtype=tf.float32,
                                      name='y')
            relu_0 = tf.nn.relu(x, name='relu')
            conv = tf.nn.conv2d(input=relu_0,
                                filters=y,
                                strides=[1, 1, 1, 1],
                                padding='VALID',
                                name='conv')
            bias = tf.Variable(tf.ones([1], tf.float32))
            conv_add = tf.nn.bias_add(conv, bias, name='bias_add')
            relu = tf.nn.relu(conv_add)
            op = tf.identity(relu, name='identity')

            sess.run(tf.compat.v1.global_variables_initializer())
            from tensorflow.compat.v1.graph_util import convert_variables_to_constants
            constant_graph = convert_variables_to_constants(
                sess, sess.graph_def, ['identity'])

        graph_def.ParseFromString(constant_graph.SerializeToString())
        with graph.as_default():
            tf.import_graph_def(graph_def, name='')
    return graph
def convert_to_pb(name_project):
    """
    Converts Keras model into Tensorflow .pb file.
    
    The string 'name_project' represents the name of the DeepPix Worflow project
    given by the user.
    """

    # Define paths.
    path_to_model = '/content/drive/My Drive/unser_project/models/{b}.hdf5'.format(
        b=name_project)
    path_output = '/content/drive/My Drive/unser_project/'

    # Load model.
    model = load_model(path_to_model)

    # Get node names.
    node_names = [node.op.name for node in model.outputs]

    # Get Keras session.
    session = K.get_session()

    # Convert Keras variables to Tensorflow constants.
    graph_to_constant = graph_util.convert_variables_to_constants(
        session, session.graph.as_graph_def(), node_names)

    # Write graph as .pb file.
    graph_io.write_graph(graph_to_constant,
                         path_output,
                         name_project + ".pb",
                         as_text=False)
Esempio n. 3
0
def keras_to_pb(model, output_filename, output_node_names):
    """
   This is the function to convert the Keras model to pb.

   Args:
      model: The Keras model.
      output_filename: The output .pb file name.
      output_node_names: The output nodes of the network. If None, then
      the function gets the last layer name as the output node.
   """

    # Get the names of the input and output nodes.
    in_name = model.layers[0].get_output_at(0).name.split(':')[0]

    if output_node_names is None:
        output_node_names = [
            model.layers[-1].get_output_at(0).name.split(':')[0]
        ]

    sess = K.get_session()

    # The TensorFlow freeze_graph expects a comma-separated string of output node names.
    output_node_names_tf = ','.join(output_node_names)

    frozen_graph_def = graph_util.convert_variables_to_constants(
        sess, sess.graph.as_graph_def(), output_node_names)

    sess.close()
    wkdir = ''
    tf.io.write_graph(frozen_graph_def, wkdir, output_filename, as_text=False)

    return in_name, output_node_names
def convert_to_pb(model,
                  path,
                  input_layer_name,
                  output_layer_name,
                  pbfilename,
                  verbose=False):

    model.load(path, weights_only=True)
    print("[INFO] Loaded CNN network weights from " + path + " ...")

    print("[INFO] Re-export model ...")
    del tf.get_collection_ref(tf.GraphKeys.TRAIN_OPS)[:]
    model.save("model-tmp.tfl")

    # taken from: https://stackoverflow.com/questions/34343259/is-there-an-example-on-how-to-generate-protobuf-files-holding-trained-tensorflow

    print("[INFO] Re-import model ...")

    input_checkpoint = "model-tmp.tfl"
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', True)
    sess = tf.Session()
    saver.restore(sess, input_checkpoint)

    # print out all layers to find name of output

    if (verbose):
        op = sess.graph.get_operations()
        [print(m.values()) for m in op][1]

    print("[INFO] Freeze model to " + pbfilename + " ...")

    # freeze and removes nodes which are not related to feedforward prediction

    minimal_graph = convert_variables_to_constants(sess,
                                                   sess.graph.as_graph_def(),
                                                   [output_layer_name])

    graph_def = optimize_for_inference_lib.optimize_for_inference(
        minimal_graph, [input_layer_name], [output_layer_name],
        tf.float32.as_datatype_enum)
    graph_def = TransformGraph(graph_def, [input_layer_name],
                               [output_layer_name],
                               ["sort_by_execution_order"])

    with tf.gfile.GFile(pbfilename, 'wb') as f:
        f.write(graph_def.SerializeToString())

    # write model to logs dir so we can visualize it as:
    # tensorboard --logdir="logs"

    if (verbose):
        writer = tf.summary.FileWriter('logs', graph_def)
        writer.close()

    # tidy up tmp files

    for f in glob.glob("model-tmp.tfl*"):
        os.remove(f)

    os.remove('checkpoint')
def save_models(sess, tf_model, w2id):
    import json
    import os
    from tensorflow.compat.v1.graph_util import convert_variables_to_constants
    graph = convert_variables_to_constants(sess, sess.graph_def,
                                           ["input", "prediction"])
    try:
        os.mkdir("{}".format(tf_model))
    except:
        pass
    logdir = "{}".format(tf_model)
    tf.io.write_graph(graph, logdir, "tf_model.pb", as_text=False)
    with open("{}/word2id.json".format(tf_model), 'w') as f:
        json.dump(w2id, f, ensure_ascii=False)
    print("{}".format(tf_model))
Esempio n. 6
0
def h5_to_pb(h5_model,output_dir,model_name,out_prefix = "output_",log_tensorboard = True):
    if osp.exists(output_dir) == False:
        os.mkdir(output_dir)
    out_nodes = []
    for i in range(len(h5_model.outputs)):
        out_nodes.append(out_prefix + str(i + 1))
        tf.identity(h5_model.output[i],out_prefix + str(i + 1))
    sess = K.get_session()
    from tensorflow.python.framework import graph_util,graph_io
    init_graph = sess.graph.as_graph_def()
    main_graph = graph_util.convert_variables_to_constants(sess,init_graph,out_nodes)
    graph_io.write_graph(main_graph,output_dir,name = model_name,as_text = False)
    if log_tensorboard:
        from tensorflow.python.tools import import_pb_to_tensorboard
        import_pb_to_tensorboard.import_to_tensorboard(osp.join(output_dir,model_name),output_dir)
Esempio n. 7
0
def save_model(model, output_dir):
    timestamp = int(time.time())
    if not os.path.isdir(output_dir):
        os.mkdir(output_dir)
    model.save('%s/%d.h5' %
               (output_dir,
                timestamp))  # save reloadable .h5 so we can restart training
    tmp_name = '/tmp/weights-%s.h5' % timestamp
    model.save_weights(tmp_name)

    # determine the number of filters and blocks in the model
    last_add = [l for l in model.layers if 'add' in l.name][-1]
    blocks = int(last_add.name.split('_')[-1]) + 1
    filters = int(last_add.input[0].shape[1])
    kernels = [
        int([l for l in model.layers
             if 'conv2d' in l.name][i].weights[0].shape[0]) for i in [0, -1]
    ]

    with tf.Graph().as_default():
        with tf.Session().as_default() as freeze_sess:
            # in new graph and session create an identical model, except with custom batch normalization
            # layers that can be frozen without any training ops.
            model2 = make_model(filters=filters,
                                blocks=blocks,
                                kernels=kernels,
                                freeze_batch_norm=True)
            # load weights into the new network and get a frozen graph def.
            model2.load_weights(tmp_name)
            os.remove(tmp_name)
            freeze_var_names = [v.name for v in model2.variables]
            output_node_names = ['input', 'value/Tanh', 'policy/Softmax']
            output_node_names += [v.op.name for v in model2.variables]
            input_graph_def = freeze_sess.graph.as_graph_def()
            frozen_graph_def = convert_variables_to_constants(
                freeze_sess, input_graph_def, output_node_names)
            # create a new graph and sesion containing the frozen graph and save
            with tf.Graph().as_default():
                with tf.Session().as_default() as save_sess:
                    tf.graph_util.import_graph_def(frozen_graph_def, name='')
                    builder = tf.saved_model.builder.SavedModelBuilder(
                        '%s/%d' % (output_dir, timestamp))
                    builder.add_meta_graph_and_variables(
                        save_sess, [tf.saved_model.tag_constants.SERVING])
                    builder.save(False)
def deploy_model(saved_model_dir, frozen_graph_save_path, output_node_names):
    g = tf.Graph()
    with tf.Session(graph=g) as sess:
        tf.keras.backend.set_learning_phase(0)
        tf.saved_model.loader.load(sess, ["serve"], saved_model_dir)
        with g.as_default():
            freeze_var_names = list(
                set(v.op.name for v in tf.global_variables()))
            graph_def = g.as_graph_def()
            for node in graph_def.node:
                node.device = ""
            frozen_graph_def = convert_variables_to_constants(
                sess, graph_def, output_node_names, freeze_var_names)
            tf.io.write_graph(frozen_graph_def,
                              name="model0",
                              logdir=frozen_graph_save_path,
                              as_text=False)
    del g
    return frozen_graph_def
Esempio n. 9
0
    last_model = os.listdir(checkpoint_dir)[-1]
    chosen_model = 'Epoch_439_model.hp5'
    # chosen model = last_model
    save_pb = True
    if save_pb:
        h5_path = checkpoint_dir + chosen_model
        model = tf.keras.models.load_model(h5_path, compile=False)
        # save pb
        with K.get_session() as sess:
            output_names = [out.op.name for out in model.outputs]
            input_graph_def = sess.graph.as_graph_def()
            for node in input_graph_def.node:
                node.device = ""
            graph = graph_util.remove_training_nodes(input_graph_def)
            graph_frozen = graph_util.convert_variables_to_constants(
                sess, graph, output_names)
            tf.io.write_graph(graph_frozen,
                              checkpoint_dir,
                              'model.pb',
                              as_text=False)
        logging.info("save pb successfully!")

    # Load Frozen graph
    pb_file = checkpoint_dir + 'model.pb'
    # Load a (frozen) Tensorflow model into memory.
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.compat.v1.GraphDef()
        with tf.io.gfile.GFile(pb_file, 'rb') as fid:
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
Esempio n. 10
0
def train_net(net, batch_size, epochs, train_db, val_db, summary_writer):
    '''
    Train your network.
    The model will be saved as .bp format in ./model dictionary.
    Input parameter:
        - net: your network
        - batch_size: the number of samples in one forward processing
        - epoch: train your network epoch time with your dataset
        - train_db: training dataset
        - val_db: validation dataset
        - summary_writer: summary object
    '''
    # create session  training from scratch!
    sess.run(tf.compat.v1.global_variables_initializer())
    # # restore session
    # saver.restore(sess, './ckpt_model/ckpt_model_valid_acc=0.6962.ckpt')

    train_samples = train_db.num_samples  # get number of samples
    train_images = train_db.images  # get training images
    train_labels = train_db.labels  # get training labels, noting that it is one_hot format

    print("=" * 50)
    print()
    print("Start training...\n")

    # start training
    global_step = 0  # record total step when training
    for epoch in range(epochs):
        total_loss = 0
        for offset in tqdm(range(0, train_samples, batch_size)):
            # "offset" is the start position of the index, "end" is the end position of the index.
            end = offset + batch_size
            batch_train_images, batch_train_labels = train_images[offset:end], train_labels[offset:end]
            # get images and labels according to the batch number

            batch_train_images = preprocess(batch_train_images, IMAGE_SIZE)

            _, loss, loss_summary = sess.run(
                [training_operation, loss_operation, merge_summary],
                feed_dict={
                    input: batch_train_images,
                    labels: batch_train_labels,
                    prob: 0.5  # the probability to discard elements
                })
            total_loss += loss

            # record summary
            # print(global_step)
            summary_writer.add_summary(loss_summary, global_step=global_step)
            global_step += 1

        validation_accuracy = test_net(net, batch_size, val_db)
        loss_avg = total_loss * batch_size / train_samples
        print(
            "EPOCH {:>3d}: Loss = {:.5f}, Validation Accuracy = {:.5f}".format(epoch + 1, loss_avg,
                                                                               validation_accuracy))

        # save model for every 10 epochs
        if ((epoch + 1) != 0) and ((epoch + 1) % 10 == 0):
            ckpt_path = os.path.join(dictionary, 'ckpt_model_valid_acc=%.4f.ckpt' % validation_accuracy)
            save_path = saver.save(sess, ckpt_path)  # add global_step=epoch to add 1 in the end of file name
            print("model has saved,saved in path: %s" % save_path)

    #### save model ####
    pbtxt_name = 'frozen_model.pbtxt'
    pbtxt_path = os.path.join(dictionary, pbtxt_name)
    frozen_model_path = os.path.join(dictionary, 'frozen_model.pb')
    # output_node = 'full_layer_03/linear'
    output_node = 'full_layer_03/linear'

    # ckpt_path = os.path.join(dictionary, 'ckpt_model_valid_acc=%.4f.ckpt' % validation_accuracy)
    # save_path = saver.save(sess, ckpt_path)
    #
    # # This will only save the graph but the variables will not be saved.
    # # You have to freeze your model first.
    # tf.train.write_graph(graph_or_graph_def=sess.graph_def, logdir=dictionary, name=pbtxt_name, as_text=True)
    # # Freeze graph
    # freeze_graph.freeze_graph(input_graph=pbtxt_path, input_saver='',
    #                           input_binary=False, input_checkpoint=ckpt_path, output_node_names=output_node,
    #                           restore_op_name='save/restore_all', filename_tensor_name='save/Const:0',
    #                           output_graph=frozen_model_path, clear_devices=True, initializer_nodes='')

    # save the final model
    if os.path.exists('./model') == False:
        os.makedirs('./model')
    output_graph_def = convert_variables_to_constants(
        sess, sess.graph_def,
        output_node_names=['output', 'loss_op', 'accuracy'])  # set saving node
    with tf.gfile.GFile('model/AlexNet_model.pb', mode='wb') as f:
        f.write(output_graph_def.SerializeToString())

    print("=" * 50)
    print()
    print("The model have been saved to ./model dictionary.")