python - NotImplementedError:预训练图形输出 -> 新层
问题描述
我正在努力将预训练图形的一些输出输入到 Tensorflow 中的一些附加层中。这是我的一些代码的演练:
首先,我定义了一个新的 tf.Graph(),并加载到预训练的模型中。
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile('./mobilenetssd/frozen_inference_graph.pb', 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
获取加载图的输入/输出张量,定义占位符,添加一些操作。
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
output_matrix = detection_graph.get_tensor_by_name('concat:0')
labels = tf.placeholder(tf.float32, [None, 1])
# Adding operations
outmat_sq = tf.squeeze(output_matrix)
logits_max = tf.squeeze(tf.math.reduce_max(outmat_sq, reduction_indices=[0]))
logits_mean = tf.squeeze(tf.math.reduce_mean(outmat_sq, reduction_indices=[0]))
logodds = tf.concat([logits_max, logits_mean], 0)
logodds = tf.expand_dims(logodds, 0)
logodds.set_shape([None, 1204])
定义新层,设置优化器来训练新层。
hidden = tf.contrib.layers.fully_connected(inputs=logodds, num_outputs=500, activation_fn=tf.nn.tanh)
out = tf.contrib.layers.fully_connected(inputs=hidden, num_outputs=1, activation_fn=tf.nn.sigmoid)
# Define Loss, Training, and Accuracy
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out, labels=labels))
training_step = tf.train.AdamOptimizer(1e-6).minimize(loss, var_list=[hidden, out])
correct_prediction = tf.equal(tf.round(out), labels)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
运行此代码后,我收到NotImplementedError:('Trying to update a Tensor ', tf.Tensor 'fully_connected/Tanh:0' shape=(?, 500) dtype=float32)错误。这似乎是将模型的两个部分“链接”在一起的问题。我是否需要将第一个图的输出传递到一些 tf.Variable 中,然后将其传递到后续层?另外,我使用的是 TF 1.10。
对此的任何见解将不胜感激!
解决方案
推荐阅读
- bash - 我有一个 bash 文件,我想对其进行编辑和更改
- javascript - 获取输入事件以在 Android 上触发(使用 Nuxt)
- python - pyecharts 散点图更改工具提示文本
- docker - 获取失败:在 ubuntu 上安装 apache Web 服务器时出错
- javascript - 我们可以为入站集成重置 streamwriter 数据吗?
- python - 点击后 Dash DropDown 关闭
- python - 获取数组数组,需要数字数组。想法?
- react-native - handleChange 不是函数,React Native Picker
- wordpress - 如何在 Wordpress 中创建唯一的用户仪表板
- mongodb - 如何在 MongoDB 聚合管道中获得 10 万条记录限制