im2txt& TensorFlow 1.4.1

时间:2018-01-04 06:56:56

标签: python tensorflow

有没有人在这里成功使用TensorFlow 1.4.1运行im2txt?

我正在使用此模型(https://drive.google.com/file/d/0B_qCJ40uBfjEWVItOTdyNUFOMzg/view

2018-01-04 00:46:59.268582: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Key lstm/basic_lstm_cell/kernel not found in checkpoint

然后我尝试了以下脚本来转换模型。该脚本生成了检查点,.meta,.data和.index。

OLD_CHECKPOINT_FILE = "/tmp/my_checkpoint/model.ckpt-3000000"
NEW_CHECKPOINT_FILE = "/tmp/my_converted_checkpoint/model.ckpt-3000000"

import tensorflow as tf
vars_to_rename = {
    "lstm/BasicLSTMCell/Linear/Matrix": "lstm/basic_lstm_cell/weights",
    "lstm/BasicLSTMCell/Linear/Bias": "lstm/basic_lstm_cell/biases",
}
new_checkpoint_vars = {}
reader = tf.train.NewCheckpointReader(OLD_CHECKPOINT_FILE)
for old_name in reader.get_variable_to_shape_map():
  if old_name in vars_to_rename:
    new_name = vars_to_rename[old_name]
  else:
    new_name = old_name
  new_checkpoint_vars[new_name] = tf.Variable(reader.get_tensor(old_name))

init = tf.global_variables_initializer()
saver = tf.train.Saver(new_checkpoint_vars)

with tf.Session() as sess:
  sess.run(init)
  print("save checkpoint")
  saver.save(sess, NEW_CHECKPOINT_FILE)

有人能告诉我如何使用这些文件与TensorFlow 1.4.1一起运行im2txt。 (实际上,我可以用tensorflow 0.12.1运行im2txt)

的Env python 3.5.2
Mac OS X版本10.12.6
TensorFlow 1.4.1

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

在MacOS 10.13上使用tf 1.4.1和python3.5获取与checkpoint文件相同的错误

原因:使用旧版本的tensorflow(python2)生成下载的检查点文件。 word_count.txt文件格式

来自https://github.com/KranthiGV/Pretrained-Show-and-Tell-model

的答案

的变化: 1.生成可由tf1.4.1

加载的ckp文件
OLD_CHECKPOINT_FILE = "model.ckpt-1000000"
NEW_CHECKPOINT_FILE = "model2.ckpt-1000000"

import tensorflow as tf
vars_to_rename = {
"lstm/basic_lstm_cell/weights": "lstm/basic_lstm_cell/kernel",
"lstm/basic_lstm_cell/biases": "lstm/basic_lstm_cell/bias",
}
new_checkpoint_vars = {}
reader = tf.train.NewCheckpointReader(OLD_CHECKPOINT_FILE)
for old_name in reader.get_variable_to_shape_map():
    if old_name in vars_to_rename:
        new_name = vars_to_rename[old_name]
else:
    new_name = old_name
new_checkpoint_vars[new_name] = 
tf.Variable(reader.get_tensor(old_name))`

init = tf.global_variables_initializer()
saver = tf.train.Saver(new_checkpoint_vars)

with tf.Session() as sess:
   sess.run(init)
   saver.save(sess, NEW_CHECKPOINT_FILE)
  1. python3文件读取问题,在im2txt / run_reference.py

    with tf.gfile.GFile(filename, "rb") as f:

  2. 从该链接下载的
  3. word_count.txt需要替换为此链接 https://github.com/siavash9000/im2txt_demo/tree/master/im2txt_pretrained

答案 1 :(得分:0)

春芳的解决方案对我有用,但我想分享另一种方法。

在最新版本的TensorFlow中,Google提供了一个“官方” checkpoint_convert.py实用程序来转换旧的RNN检查点:

    program linked_list;
type
    pointer_typ = ^record_2;
    record_2 = record
        data: integer;
        next: pointer_typ;
    end;
procedure Push_int(var p: pointer_typ);
    var
        temp: pointer_typ;
        x: integer;
    begin
        read(x);
        new(temp);
        temp^.data := x;
        temp^.next := p;
        p := temp;
        if Seekeof then
            exit
        else
            Push_int(p^.next)
    end;
var
    first: pointer_typ;
begin
    first := nil;
    Push_int(first);
end.