gpt4 book ai didi

python - 根据现有检查点定义 TensorFlow 网络键名称

转载 作者:太空宇宙 更新时间:2023-11-03 14:27:48 24 4
gpt4 key购买 nike

我使用 Nvidia DIGITS 训练了 LeNet-gray-28x28 图像检测 Tensorflow 模型,得到了我期望的结果。现在,我必须对 DIGITS 之外的一些图像进行分类,并且我想使用我训练过的模型。

所以我得到了 DIGITS 使用的 LeNet 模型,并创建了一个类来使用它:

import tensorflow as tf
import tensorflow.contrib.slim as slim
import tflearn
from tflearn.layers.core import input_data


class LeNetModel():

def gray28(self, nclasses):
x = input_data(shape=[None, 28, 28, 1])
# scale (divide by MNIST std)
# x = x * 0.0125
with slim.arg_scope([slim.conv2d, slim.fully_connected],
weights_initializer=tf.contrib.layers.xavier_initializer(),
weights_regularizer=slim.l2_regularizer(0.0005)):
model = slim.conv2d(x, 20, [5, 5], padding='VALID', scope='conv1')
model = slim.max_pool2d(model, [2, 2], padding='VALID', scope='pool1')
model = slim.conv2d(model, 50, [5, 5], padding='VALID', scope='conv2')
model = slim.max_pool2d(model, [2, 2], padding='VALID', scope='pool2')
model = slim.flatten(model)
model = slim.fully_connected(model, 500, scope='fc1')
model = slim.dropout(model, 0.5, is_training=False, scope='do1')
model = slim.fully_connected(model, nclasses, activation_fn=None, scope='fc2')

return tflearn.DNN(model)

我从 DIGITS 下载了我的模型,并使用(在另一个文件中)实例化它:

self.ballmodel = LeNetModel().gray28(2)
self.ballmodel.load("src/perftrack/prototype/models/ball/snapshot_5.ckpt")

但是,当我启动脚本时,我遇到了这些异常:

2017-11-26 14:55:50.330524: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key conv1/biases not found in checkpoint
2017-11-26 14:55:50.330948: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key Global_Step not found in checkpoint
2017-11-26 14:55:50.331270: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key is_training not found in checkpoint
2017-11-26 14:55:50.331564: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key conv2/weights not found in checkpoint
2017-11-26 14:55:50.332823: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key conv1/weights not found in checkpoint
2017-11-26 14:55:50.332891: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key conv2/biases not found in checkpoint
2017-11-26 14:55:50.333620: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key fc2/weights not found in checkpoint
2017-11-26 14:55:50.334021: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key fc1/weights not found in checkpoint
2017-11-26 14:55:50.334173: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key fc1/biases not found in checkpoint
2017-11-26 14:55:50.334431: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key fc2/biases not found in checkpoint
...
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Key conv1/biases not found in checkpoint
[[Node: save_1/RestoreV2_1 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save_1/Const_0_0, save_1/RestoreV2_1/tensor_names, save_1/RestoreV2_1/shape_and_slices)]]
[[Node: save_1/RestoreV2_1/_19 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/gpu:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_38_save_1/RestoreV2_1", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

所以我使用https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/inspect_checkpoint.py脚本来检查我的检查点包含的键名称,我得到如下内容:

model/conv1/biases
model/conv2/weights
...

所以我重写了我的网络,手动添加模型/前缀:

                model = slim.conv2d(x, 20, [5, 5], padding='VALID', scope='model/conv1')
model = slim.max_pool2d(model, [2, 2], padding='VALID', scope='model/pool1')
model = slim.conv2d(model, 50, [5, 5], padding='VALID', scope='model/conv2')
model = slim.max_pool2d(model, [2, 2], padding='VALID', scope='model/pool2')
model = slim.flatten(model)
model = slim.fully_connected(model, 500, scope='model/fc1')
model = slim.dropout(model, 0.5, is_training=False, scope='model/do1')
model = slim.fully_connected(model, nclasses,

它修复了一些缺少按键的警告,但是:

  • 我感觉这不是解决问题的正确方法
  • 我无法修复两个键:
    1. Global_Step(我的检查点中有一个 global_step 键)
    2. is_training(我不知道它是什么)

所以我的问题是:如何重新定义网络中的这些键名称以匹配我在检查点中找到的键名称?

最佳答案

因为我的问题主要是由于我对 TensorFlow 理解不好,所以我查阅了官方文档,找到了一些答案。

首先,我结合了 contrib/slim 和 contrib/tflearn 的使用,即使可能,它也不是真正相关。所以我只使用 slim 重写网络:

import tensorflow as tf
import tensorflow.contrib.slim as slim


class LeNetModel():

def gray28(self, nclasses):
# x = input_data(shape=[None, 28, 28, 1])
x = tf.placeholder(tf.float32, shape=[1, 28, 28], name="x")
rs = tf.reshape(x, shape=[-1, 28, 28, 1])
# scale (divide by MNIST std)
# x = x * 0.0125
with slim.arg_scope([slim.conv2d, slim.fully_connected],
weights_initializer=tf.contrib.layers.xavier_initializer(),
weights_regularizer=slim.l2_regularizer(0.0005)):
model = slim.conv2d(rs, 20, [5, 5], padding='VALID', scope='conv1')
model = slim.max_pool2d(model, [2, 2], padding='VALID', scope='pool1')
model = slim.conv2d(model, 50, [5, 5], padding='VALID', scope='conv2')
model = slim.max_pool2d(model, [2, 2], padding='VALID', scope='pool2')
model = slim.flatten(model)
model = slim.fully_connected(model, 500, scope='fc1')
model = slim.dropout(model, 0.5, is_training=True, scope='do1')
model = slim.fully_connected(model, nclasses, activation_fn=None, scope='fc2')

return x, model

我返回 x 占位符和模型,并使用它来加载 DIGITS 预训练模型(检查点):

import tensorflow as tf
import tensorflow.contrib.slim as slim
import cv2
from models.lenet import LeNetModel

# Helper function to load/resize images
def image(path):
img = cv2.imread(path, 0)
return cv2.resize(img, dsize=(28,28))

# Define a function that adds the model/ prefix to all variables :
def name_in_checkpoint(var):
return 'model/' + var.op.name

#Instantiate the model
x, model = LeNetModel().gray28(2)

# Define the variables to restore :
# Exclude the "is_training" that I don't care about
variables_to_restore = slim.get_variables_to_restore(exclude=["is_training"])
# Rename the other variables with the function name_in_checkpoint
variables_to_restore = {name_in_checkpoint(var):var for var in variables_to_restore}

# Create a Saver to restore the checkpoint, given the variables
restorer = tf.train.Saver(variables_to_restore)

#Launch a session to restore the checkpoint and try to infer some images :
with tf.Session() as sess:
# Restore variables from disk.
restorer.restore(sess, "src/prototype/models/snapshot_5.ckpt")
print("Model restored.")
print(sess.run(model, feed_dict={x:[image("/home/damien/Vidéos/1/positives/img/1-img143.jpg")]}))
print(sess.run(model, feed_dict={x:[image("/home/damien/Vidéos/0/positives/img/1-img1.jpg")]}))

而且它有效!

关于python - 根据现有检查点定义 TensorFlow 网络键名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47506607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com