现象:
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/BatchNorm/beta] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/BatchNorm/beta/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/BatchNorm/gamma] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/BatchNorm/gamma/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/BatchNorm/moving_mean] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/BatchNorm/moving_variance] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/weights] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/weights/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv2/BatchNorm/beta] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv2/BatchNorm/beta/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv2/BatchNorm/gamma] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv2/BatchNorm/gamma/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv2/BatchNorm/moving_mean] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv2/BatchNorm/moving_variance] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv2/weights] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv2/weights/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv3/BatchNorm/beta] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv3/BatchNorm/beta/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv3/BatchNorm/gamma] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv3/BatchNorm/gamma/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv3/BatchNorm/moving_mean] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv3/BatchNorm/moving_variance] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv3/weights] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv3/weights/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/shortcut/BatchNorm/beta] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/shortcut/BatchNorm/beta/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/shortcut/BatchNorm/gamma] is not available in checkpoint
这个报的warning是你要的变量在 checkpoint里面找不到!!!!!!!!!!!
那checkpoint里面是什么?????????????那就去看咯:
然后就吧checkpoint的信息打印出来:
import tensorflow as tf
from tensorflow.python.tools.inspect_checkpoint import print_tensors_in_checkpoint_file
latest_ckp = tf.train.latest_checkpoint('./') # checkpoint 所在的路径是当前文件夹(./),如果是其他,请改过来
print_tensors_in_checkpoint_file(latest_ckp, all_tensors=True, tensor_name='')
然后发现:
tensor_name: FeatureExtractor/resnet_v1_50/block4/unit_2/bottleneck_v1/conv3/BatchNorm/beta
[-1.2371869 -1.3236059 -1.2312554 ... -1.3017322 -1.2117504 -1.4911506]
tensor_name: FeatureExtractor/resnet_v1_50/block4/unit_2/bottleneck_v1/conv3/BatchNorm/gamma
[1.4568403 1.4048293 1.0624585 ... 1.1037463 1.0324904 1.0967498]
tensor_name: FeatureExtractor/resnet_v1_50/block4/unit_2/bottleneck_v1/conv3/BatchNorm/moving_mean
[-0.08689928 0.00798311 -0.02823892 ... -0.04347016 -0.07710622
-0.04719209]
tensor_name: FeatureExtractor/resnet_v1_50/block4/unit_2/bottleneck_v1/conv3/BatchNorm/moving_variance
[0.00452107 0.00487971 0.00305854 ... 0.00261809 0.00344088 0.00306313]
tensor_name: FeatureExtractor/resnet_v1_50/block4/unit_2/bottleneck_v1/conv3/weights
[[[[ 1.2295754e-03 -9.4967345e-03 -6.6470391e-05 ... -9.7579239e-03
1.1851139e-02 -1.0630138e-02]
[-4.5038795e-04 -3.9205588e-03 -6.4933673e-03 ... 9.1390898e-03
-1.1232623e-02 -9.8358802e-03]
[ 1.3918030e-02 -7.1829297e-03 3.0942420e-03 ... -6.6203251e-03
原来是少了FeatureExtractor
,这时候就去找restore var 的code
var_name = (
re.split('^' + self._extract_features_scope + '/',
var_name)[-1])
他把这个给干掉了,所以:
_extract_features_scope
就是:FeatureExtractor
发现
问题是,tensorflow object detection 在model zoom新下载的模型命名方式改变了,如果使用旧的代码加载新的模型就会出现这个问题,解决方案是research/object_detection/meta_architectures/ssd_meta_arch.py
var_name = (
re.split('^' + self._extract_features_scope + '/',
var_name)[-1])
改为:
var_name = (
re.split('^' + '/',
var_name)[-1])
同样的其他模型也是这样。
但是,后面还是出现:
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/BatchNorm/beta/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/BatchNorm/gamma/Momentum] is not available in checkpoint
WARNING:root:Variable [resnet_v1_50/block1/unit_1/bottleneck_v1/conv1/weights/Momentum] is not available in checkpoint
仔细看,你会发现,这只是Momentum造成的,是优化器中的参数,不是模型参数,Momentum是动量的意思,优化器使用这个是为了
避免收敛到局部极值。這個不影響的。如果不想報錯可以:
if fine_tune_checkpoint_type == 'classification':
var_name = (
re.split('^' + '/',
var_name)[-1])
# var_name = (
# re.split('^' + self._extract_features_scope + '/',
# var_name)[-1])
if 'Momentum' in var_name:
continue
variables_to_restore[var_name] = variable
加一個:
if 'Momentum' in var_name:
continue
如果是Moment 變量就不加載。
不过,如果对这套代码不了解的,建议update最新的代码。