究竟应该如何导出python模型以便在c ++中使用?
我正在尝试做类似于本教程的内容:https: //www.tensorflow.org/versions/r0.8/tutorials/image_recognition/index.html
我试图在c ++ API中导入我自己的TF模型,而不是从一开始.我调整了输入大小和路径,但奇怪的错误不断出现.我花了一整天阅读堆栈溢出和其他论坛但无济于事.
我尝试了两种导出图形的方法.
方法1:元图.
...loading inputs, setting up the model, etc....
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
for i in range(num_steps):
x_batch, y_batch = batch(50)
if i%10 == 0:
train_accuracy = accuracy.eval(feed_dict={
x:x_batch, y_: y_batch, keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: x_batch, y_: y_batch, keep_prob: 0.5})
print("test accuracy %g"%accuracy.eval(feed_dict={
x: features_test, y_: labels_test, keep_prob: 1.0}))
saver = tf.train.Saver(tf.all_variables())
checkpoint =
'/home/sander/tensorflow/tensorflow/examples/cat_face/data/model.ckpt'
saver.save(sess, checkpoint)
tf.train.export_meta_graph(filename=
'/home/sander/tensorflow/tensorflow/examples/cat_face/data/cat_graph.pb',
meta_info_def=None,
graph_def=sess.graph_def,
saver_def=saver.restore(sess, checkpoint),
collection_list=None, as_text=False)
Run Code Online (Sandbox Code Playgroud)
尝试运行该程序时,方法1会产生以下错误: …
这个问题与这个问题有关: 从Python导出Tensorflow图以便在C++中使用
我正在尝试将Tensorflow模型从Python导出到C++.问题是,我的神经网络以占位符开始接收输入,这需要一个feed_dict.我找不到任何c ++ API来为我的模型提供feed_dict.我能做什么?
如果没有用于提供feed_dicts的API,我应该如何更改我的模型,以便可以在没有占位符的情况下为c ++目的训练和导出它?
循环遍历列表时,您可以使用列表的当前项.例如,如果要将某些项目替换为其他项目,则可以使用:
a=['a','b','c','d','e']
b=[]
for i in a:
if i=='b':
b.append('replacement')
else:
b.append(i)
print b
['a', 'replacement', 'c', 'd', 'e']
Run Code Online (Sandbox Code Playgroud)
但是,我希望替换某些值不是基于索引i,而是基于索引i+1.我已经尝试了很多年,我似乎无法使它工作.我想要这样的东西:
c=['a','b','c','d','e']
d=[]
for i in c:
if i+1=='b':
d.append('replacement')
else:
d.append(i)
print d
d=['replacement','b','c','d','e']
Run Code Online (Sandbox Code Playgroud)
有没有办法实现这个目标?
我训练了一个由2个组件组成的计算机视觉分类器:数据的内核PCA转换和SVM二进制分类模型。
这些模型是使用SKlearn在Python中训练的,但是我想将它们用于c ++和后来的Java中的实际计算机视觉任务。将模型导出到其他环境的最佳方法是什么?有什么聪明的方法可以做到这一点,还是我只需要用一种新的语言手工编码所有参数?
我刚接触scikit学习,而且我正撞在墙上.我已经使用了现实世界和测试数据,并且scikit算法在预测任何事情时都没有达到机会水平以上.我尝试过knn,决策树,svc和天真的贝叶斯.
基本上,我制作了一个由0和1列组成的测试数据集,所有0都具有介于0和0.5之间的特征,所有1都具有介于0.5和1之间的特征值.这应该非常简单并且给出接近100%的准确度.但是,没有一种算法在机会级别之上执行.准确率从45%到55%不等.我已经尝试为每个算法调整一大堆参数,但注意到有帮助.我认为我的实施存在根本性的错误.
请帮帮我.这是我的代码:
from sklearn.cross_validation import train_test_split
from sklearn import preprocessing
from sklearn.preprocessing import OneHotEncoder
from sklearn.metrics import accuracy_score
import sklearn
import pandas
import numpy as np
df=pandas.read_excel('Test.xlsx')
# Make data into np arrays
y = np.array(df[1])
y=y.astype(float)
y=y.reshape(399)
x = np.array(df[2])
x=x.astype(float)
x=x.reshape(399, 1)
# Creating training and test data
labels_train, labels_test = train_test_split(y)
features_train, features_test = train_test_split(x)
#####################################################################
# PERCEPTRON
#####################################################################
from sklearn import linear_model
perceptron=linear_model.Perceptron()
perceptron.fit(features_train, labels_train)
perc_pred=perceptron.predict(features_test)
print sklearn.metrics.accuracy_score(labels_test, perc_pred, normalize=True, sample_weight=None)
print 'perceptron'
#####################################################################
# …Run Code Online (Sandbox Code Playgroud) python ×4
c++ ×3
scikit-learn ×2
tensorflow ×2
algorithm ×1
export ×1
for-loop ×1
indexing ×1
java ×1
list ×1