Miscellaneous

  • Comparison:

    • Checkpoint(*.ckpt)

      • Contains three files normally: model.ckpt.index model.ckpt.meta model.ckpt.data-00000-of-00001

      • Saved by tf.trainer.Saver().save(), only save the tf.Variables(), not including graph model, thus impossible to recover a whole graph with ckpt only.

      • Use saver.restore(session, checkpoint_path) to restore.

    • GraphDef(*.pb)

      • Contains serialized protobuf data and compute graph, but not Variable data.

      • Can only recover compute graph, need checkpoint to inject data.

      • FrozenGraphDef is slightly different, since it will convert all Variable to constant values(which can be loaded from checkpoint), usually used as pre-train model.

    • SavedModel

      • Combination of checkpoint and GraphDef, plus SignatureDef of input and output variables.

Reference: TensorFlow 到底有几种模型格式?

checkpoint

  • Print parameters in checkpoint

  1. Using internal tool -- inspect_checkpoint:

    python -m tensorflow.python.tools.inspect_checkpoint --file_name=model.ckpt "$@"
  2. Using tf.compat.v1.NewCheckpointReader(ckpt_path):

    ckpt_path = "./model.ckpt"
    reader = tf.compat.v1.train.NewCheckpointReader(ckpt_path)
    var_to_shape_map = reader.get_variable_to_shape_map()
    for key in var_to_shape_map:
        print(f"{key}: {reader.get_tensor(key)}")

Reference: 如何打印出TensorFlow保存的checkpoint里的参数名

saved_model

pb

keras h5

Keras save its model in h5 format, so we need to know how to read h5 in python if we only want to search for some specific parameters.

Reference: 模型复现之HDF5文件

Last updated

Was this helpful?