我正在尝试使用TensorFlow C-API运行已从Keras / TF模型保存的LeNet的实现,但是在设置输入时遇到了一致的问题。相关的代码段是:
// Load the image with openCV
CvMat * img = cvLoadImageM(argv[1], CV_LOAD_IMAGE_COLOR );
// Create an Tensor from the image
int64_t dims4[]={1,1,28,28};
TF_Tensor * imgTensor = TF_NewTensor(TF_FLOAT,dims4,4,img,28*28*sizeof(float),NULL,NULL);
TF_Operation* init_op2 = TF_GraphOperationByName(graph, "conv2d_1_input");
TF_Operation* targets[] = {init_op2};
// Build up the inputs
TF_Output inp = {
init_op2,
0
};
TF_Output * inputs[] = {&inp};
TF_Tensor * input_values[] = {imgTensor};
printf("\nBefore\n");
TF_SessionRun(session, NULL,
&inp, inputvalues, 1, // inputs
NULL, NULL, 0, // outputs
&init_op2, 1, // targets
NULL,
status);
printf("After\n");
printf("Status %d %s\n", TF_GetCode(status), TF_Message(status));
但是,无论如何,我尝试建立输入张量,但得到错误状态和消息:
Status 3 You must feed a value for placeholder tensor 'conv2d_1_input' with dtype float and shape [?,1,28,28]
[[Node: conv2d_1_input = Placeholder[_output_shapes=[[?,1,28,28]], dtype=DT_FLOAT, shape=[?,1,28,28], _device=...]()]]
对我做错了什么建议吗?
在对的调用中TF_SessionRun
,您还提供了conv2d_1_input
作为“目标”的操作。该错误消息可以得到改善,但基本上是在抱怨您要会话执行占位符操作,该操作不能执行-这是不可能的(请参阅文档中的注释tf.placeholder
)。
您是否应该从呼叫中索取不同的target
或output
张量,TF_SessionRun
例如:
TF_Output out = { TF_GraphOperationByName(graph, "<name_of_output_tensor>"), 0 };
TF_Tensor* outputvalues = NULL;
TF_SessionRun(session, NULL,
&inp, inputvalues, 1, // inputs
&out, &outputvalues, 1, // outputs
NULL, 0, // targets
NULL, status);
希望能有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句