Predict w/ restored model from 'model.ckpt.data-00000-of-00001', 'model.ckpt.index', 'model.ckpt.meta'

Aperta
#982 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
1-2 giorni
Idoneità per principianti
15/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
python, tensorflow

Direzione di ricerca

The issue asks how to restore a TensorFlow v1 checkpoint (model.ckpt.meta, .index, .data-00000-of-00001) and run inference, and it never names the input or output tensors. Start with the restore_ckpt function and the import_meta_graph call in the snippet, then inspect the graph to find the tensor names. Done means a documented way to load the EfficientNet-lite0 checkpoint and get predictions, which the thread has not yet settled.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Hello.
I tried to restore mobilenet model and efficientnet-lite which are not frozen model, so that I can use model to inference, retrain and/or transfer learning.

So I tried below using efficientnet-lite first.

import tensorflow.compat.v1 as tf
import os
import time

tf.compat.v1.disable_eager_execution()

def restore_ckpt(sess, model_dir, export_ckpt=None):

    """Restore variables from a given checkpoint.

    Args:
    sess: a tf session for restoring or exporting models.
    ckpt_path: the path of the checkpoint. Can be a file path or a folder path.
    export_ckpt: whether to export the restored model.
    """
    files = os.listdir(model_dir)

    if any(file for file in files if '.ckpt.' in file):

        if tf.io.gfile.isdir(model_dir):
            ckpt_path = tf.train.latest_checkpoint(model_dir)

        for file in files:
            if 'ckpt.meta' in file:
                meta = file
                meta_path = os.path.join(model_dir, meta)

        saver = tf.train.import_meta_graph(meta_path)

        # Restore all variables from ckpt.
        start_time = time.time()
        saver.restore(sess, ckpt_path)
        end_time = time.time()

        elapsed_time = end_time - start_time
        print('Restoring model took {} seconds'.format(elapsed_time))

    else:
        raise ValueError("ckpt do not exist")

for img_idx, image in enumerate(image_list):
  img = cv2.imread(input_dir + image)


  # preprocessing
  img = crop_image(img)
  input_data = resize_img(img, input_details[0]['shape'][1], input_details[0]['shape'][2], interpolation)
  input_data = preprocess_input(input_data, mode)

  input_tensor = tf.convert_to_tensor(input_data)

  ckpt_path = '/content/efficientnet-lite0/efficientnet-lite0/'
  with tf.Session() as sess:
        restore_ckpt(sess, ckpt_path, export_ckpt=None)
        graph = tf.get_default_graph()
        X = graph.get_tensor_by_name(INPUT_TENSOR_NAME)
        y = graph.get_tensor_by_name(OUTPUT_TENSOR_NAME)

        output_data = sess.run(y, feed_dict={X:input_data})

To execute 'sess.run()',as far as I know, input and output tensor name should be given.
I tried below to see the tensor name.
However, I do not know which one is INPUT_TENSOR_NAME, or OUPUT_TENSOR_NAME, because it is not a network I created .

all_tensors = [tensor for op in tf.get_default_graph().get_operations() for tensor in op.values()]

Then, I tried to use tensorboard, but the graph keeps disappearing

trained_checkpoint_prefix = '/content/efficientnet-lite0/efficientnet-lite0/model.ckpt'

export_dir = os.path.join('export_dir', '0')
!rm -rf {export_dir}

graph = tf.Graph()
with tf.compat.v1.Session(graph=graph) as sess:
    # Restore from checkpoint
    loader = tf.compat.v1.train.import_meta_graph(trained_checkpoint_prefix + '.meta')
    loader.restore(sess, trained_checkpoint_prefix)

    # Export checkpoint to SavedModel
    builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_dir)
    builder.add_meta_graph_and_variables(sess,
                                         [tf.compat.v1.saved_model.TRAINING, tf.compat.v1.saved_model.SERVING],
                                         strip_default_attrs=True)
    builder.save()   

tf.enable_eager_execution()

%load_ext tensorboard
%tensorboard --logdir=./output/

Is there any other method to restore and run models from model.ckpt.data-00000-of-00001', 'model.ckpt.index', 'model.ckpt.meta' ?

Lingua principale
Jupyter Notebook
Stelle
5.3k
Fork
1.7k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di tensorflow/tpu

Tutte le issue di tensorflow/tpu

Issue simili

Altre issue su Machine Learning

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.