# Miscellaneous

## Checkpoint related

* 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 到底有几种模型格式？](https://cloud.tencent.com/developer/article/1009979)

### checkpoint

* Print parameters in checkpoint

1. Using internal tool -- `inspect_checkpoint`:

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

   ```python
   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里的参数名](https://www.codelast.com/%E5%8E%9F%E5%88%9B-%E5%A6%82%E4%BD%95%E6%89%93%E5%8D%B0%E5%87%BAtensorflow%E4%BF%9D%E5%AD%98%E7%9A%84checkpoint%E9%87%8C%E7%9A%84%E5%8F%82%E6%95%B0%E5%90%8D/)

### 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.

```python
# import related lib
import h5py

# init a file object with only-read permission
f = h5py.File(path_to_h5, 'r')

# list its keys
list(f.keys())

# h5 file can be indexed by keywords
list(f['model_weights'].keys()) 

# indexed into deeper layer
f['model_weights']['dense_73']
# Out: <HDF5 group "/model_weights/dense_73" (1 members)>
# Group means it has children leaves, can be indexed deeper by key

f['model_weights']['dense_73']['dense_73']['kernel:0']
# Out: <HDF5 dataset "kernel:0": shape (768, 22), type "<f4">
# dataset means it can be read now

# dataset can be read as list, numpy, etc.
list(f['model_weights']['dense_73']['dense_73']['bias:0'])

# dataset also has shape
f['model_weights']['dense_73']['dense_73']['kernel:0'].shape
# Out: (768, 22)
```

Reference: [模型复现之HDF5文件](https://blog.csdn.net/qq_33866063/article/details/115788312)
