Interpretation of weight file based on tensorflow

Time:2021-11-30

1. Interpret the tensorflow weight file through the tf.train.newcheckpointreader function.

2.reader.get_ variable_ to_ shape_ Map () can get the tensor name in the weight file.

3.reader.get_ Tensor (key) can get the weight value of the corresponding tensor.

Interpretation of weight file based on tensorflow

import tensorflow as tf
cpktFileName = r'.\models\resnet_v2_152.ckpt'
reader = tf.train.NewCheckpointReader(cpktFileName)
for key in sorted(reader.get_variable_to_shape_map()):
   
    if key.endswith('weights') or key.endswith('biases'):
        keySplits = key.split(r'/')
        print(key)
        print(reader.get_tensor(key))
        

resnet_ v2_ 152 interpretation of tensor name

First, each tensor name is represented by RESNET_ v2_ Start with 152

Second, the second paragraph of tensor name is block. There are four blocks in total. Related to network architecture.

Third, the third field is unit, and the number of units in each block is different. Related to network architecture.

Fourth, except for the flat layer after the group, the fourth field is bottleneck_ v2

Fifth, the fifth field is’ conv1 ‘,’ conv2 ‘,’ conv3 ‘,’ shortcut ‘

Sixth, the sixth field is’ weights’ or ‘bias’

Interpretation of weight file based on tensorflow

Supplement: tensorflow model call, weight view

Take the tensorpack description of VC version

Model call

Checkpoint, graph and model will be generated each time

1. Among them, if there is already a checkpoint in the folder and it is written that the last model is automatically used, you can continue training on the basis of the last one, otherwise it will be regenerated and the previous model cannot be called, even if it already exists

2. Each run will regenerate the graph, even if the last one already exists, so calling the last model has nothing to do with whether there is a graph in the folder

Weight variable view

import numpy as np

File content

Chekpoint – records the latest saved checkpoint file and a list of other checkpoint files. During information, you can specify which model to use by modifying this file

Interpretation of weight file based on tensorflow

The mymodel.meta file saves the graph structure. The meta file is a Pb (protocol buffer) format file, including variables, Op, sets, etc.

The CKPT file is a binary file that holds all the weights, bias, gradients and other variables. Before tensorflow 0.11, it was saved in a. CKPT file. After 0.11, it is saved through two files, such as:

MyModel.data-00000-of-00001
MyModel.index

The above is personal experience. I hope I can give you a reference, and I hope you can give me more support.

Recommended Today

Game case | application evolution and practice of service mesh in happy games

author Chen Zhiwei, Tencent level 12 background expert engineer, is now responsible for the public background technology research and development and team management of happy game studio. Rich experience in micro service distributed architecture and game background operation and maintenance research and development. preface The background of happy game studio is a distributed micro service […]