tensorflow加载resnet v2 chekpoint方法

版本:
python = 3.6
tensorflow-gpu = 1.11

代码及模型下载地址:https://github.com/tensorflow/models/tree/master/research/slim

问题:在使用tf.train.Saver(tf.global_variables())加载预训练模型时,出现加载的chekpoint中的graph key和代码构建的网络模型不对应的情况,显示缺失一个biases。错误如下:

NotFoundError (see above for traceback): Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

Tensor name "resnet_v2_152/block1/unit_1/bottleneck_v2/conv1/biases" not found in checkpoint files resnet_v2_152.ckpt

问题原因:使用了错误的scope

解决方法:在初始化resnet时,使用resnet_arg_scope()(参数可选)

import tensorflow as tf
# resnet_v2是官方代码
import resnet_v2

slim = tf.contrib.slim

def main():
	
	# 输入图片大小为224*224
	batch_size = 1
	height, width = 224, 224
    image = tf.random_uniform((batch_size, height, width, 3))
	
	# 错误方法:
	# net, end_points = resnet_v2.resnet_v2_152(inputs, is_training=False)
    # 正确方法
    with slim.arg_scope(resnet_v2.resnet_arg_scope()):
        net, end_points = resnet_v2.resnet_v2_152(inputs=image, is_training=False, num_classes=1001)
    # print(net)

    init = tf.global_variables_initializer()
    saver = tf.train.Saver(tf.global_variables())
    checkpoint_path = 'resnet_v2_152.ckpt'
    with tf.Session() as sess:
        sess.run(init)
        # 加载模型
        saver.restore(sess, checkpoint_path)
        
main()

解决问题参考:https://github.com/tensorflow/models/issues/2527
https://github.com/tensorflow/tensorflow/issues/4249

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下为使用TensorFlow搭建ResNet50V2的步骤: 1. 导入必要的库 ```python import tensorflow as tf from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, BatchNormalization, Activation, Dense, Flatten, Add, ZeroPadding2D, AveragePooling2D from tensorflow.keras.models import Model from tensorflow.keras import backend as K ``` 2. 定义卷积块函数 ```python def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)): """ 定义ResNet中的卷积块 """ filters1, filters2, filters3 = filters if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 # 定义卷积块名称 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' # 第一层卷积 x = Conv2D(filters1, (1, 1), strides=strides, name=conv_name_base + '2a')(input_tensor) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x) x = Activation('relu')(x) # 第二层卷积 x = Conv2D(filters2, kernel_size, padding='same', name=conv_name_base + '2b')(x) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x) x = Activation('relu')(x) # 第三层卷积 x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x) # shortcut shortcut = Conv2D(filters3, (1, 1), strides=strides, name=conv_name_base + '1')(input_tensor) shortcut = BatchNormalization(axis=bn_axis, name=bn_name_base + '1')(shortcut) x = Add()([x, shortcut]) x = Activation('relu')(x) return x ``` 3. 定义恒等块函数 ```python def identity_block(input_tensor, kernel_size, filters, stage, block): """ 定义ResNet中的恒等块 """ filters1, filters2, filters3 = filters if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 # 定义恒等块名称 conv_name_base = 'res' + str(stage) + block + '_branch' bn_name_base = 'bn' + str(stage) + block + '_branch' # 第一层卷积 x = Conv2D(filters1, (1, 1), name=conv_name_base + '2a')(input_tensor) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x) x = Activation('relu')(x) # 第二层卷积 x = Conv2D(filters2, kernel_size, padding='same', name=conv_name_base + '2b')(x) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x) x = Activation('relu')(x) # 第三层卷积 x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x) x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x) x = Add()([x, input_tensor]) x = Activation('relu')(x) return x ``` 4. 定义ResNet50V2模型 ```python def ResNet50V2(input_shape=(224, 224, 3), classes=1000): """ 定义ResNet50V2模型 """ if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 # 输入层 img_input = Input(shape=input_shape) # 第一层卷积 x = ZeroPadding2D(padding=(3, 3))(img_input) x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) # 卷积块 x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') # 卷积块 x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') # 卷积块 x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') # 卷积块 x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') # 平均池化层 x = AveragePooling2D((7, 7), name='avg_pool')(x) # 全连接层 x = Flatten()(x) x = Dense(classes, activation='softmax', name='fc1000')(x) # 构建模型 model = Model(img_input, x, name='resnet50v2') return model ``` 5. 编译模型并训练 ```python model = ResNet50V2(input_shape=(224, 224, 3), classes=1000) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, epochs=10, batch_size=32) ``` 以上就是使用TensorFlow搭建ResNet50V2的步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值