Tensorflow C API实现卷积计算

原创文章,转载请注明: 转载自慢慢的回味

本文链接地址: Tensorflow C API实现卷积计算

一个基于C API实现的进行卷积计算的示例。

回目录

测试程序

一个卷积Operation测试代码:

//============================================================================
// Name        : TensorflowTest.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
 
#include <iostream>
#include <tensorflow/c/c_api.h>
#include <tensorflow/c/c_test_util.h>
 
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <memory>
#include <vector>
#include <string.h>
 
using namespace std;
 
static void FloatDeallocator(void* data, size_t, void* arg) {
	delete[] static_cast<float*>(data);
}
 
int main() {
	cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	cout << "Hello from TensorFlow C library version" << TF_Version() << endl;
 
	TF_Status* s = TF_NewStatus();
	TF_Graph* graph = TF_NewGraph();
 
	int64_t imageDims[] = { 1, 3, 6, 1 };
	float* imageVals = new float[18] { 3.0, 2.0, 1.0, -1.0, -2.0, -3.0,  //
			4.0, 3.0, 2.0, -2.0, -3.0, -4.0,  //
			5.0, 4.0, 3.0, -3.0, -4.0, -5.0  //
			};
	TF_Tensor* image = TF_NewTensor(TF_FLOAT, imageDims, 4, imageVals,
			sizeof(float) * 18, &FloatDeallocator, nullptr);
 
	float* filterVals = new float[4] { 1, 2,  //
			3, 4,  //
			};
	int64_t filterDims[] = { 2, 2, 1, 1 };
	TF_Tensor* filter = TF_NewTensor(TF_FLOAT, filterDims, 4, filterVals,
			sizeof(float) * 4, &FloatDeallocator, nullptr);
 
	TF_OperationDescription* desc = TF_NewOperation(graph, "Conv2D", "conv_2d");
	TF_Operation* imageInput = Const(image, graph, s, "imageInput");
	TF_Operation* filterInput = Const(filter, graph, s, "filterInput");
	//TF_Output add_inputs[] = { {imageInput,0}, {filterInput,0}};
	TF_AddInput(desc, { imageInput, 0 });
	TF_AddInput(desc, { filterInput, 0 });
	TF_SetAttrType(desc, "T", TF_FLOAT);
	int64_t strideDim[] = { 1, 3, 1, 1 };
	TF_SetAttrIntList(desc, "strides", strideDim, 4);
	TF_SetAttrString(desc, "padding", "VALID", 5);
	TF_Operation *op = TF_FinishOperation(desc, s);
	cout << TF_Message(s);
	cout << TF_Message(s);
 
	TF_SessionOptions* opts = TF_NewSessionOptions();
	TF_Session* sess = TF_NewSession(graph, opts, s);
	TF_DeleteSessionOptions(opts);
 
	TF_Output feeds[] = { };
	TF_Output fetches[] = { TF_Output { op, 0 } };
 
	const char* handle = nullptr;
	TF_SessionPRunSetup(sess, feeds, TF_ARRAYSIZE(feeds), fetches,
			TF_ARRAYSIZE(fetches), NULL, 0, &handle, s);
	cout << TF_Message(s);
 
	// Feed A and fetch A + 2.
	TF_Output feeds1[] = { };
	TF_Output fetches1[] = { TF_Output { op, 0 } };
	TF_Tensor* feedValues1[] = { };
	TF_Tensor* fetchValues1[1];
	TF_SessionPRun(sess, handle, feeds1, feedValues1, 0, fetches1, fetchValues1,
			1, NULL, 0, s);
	cout << TF_Message(s);
	cout << *(static_cast<int*>(TF_TensorData(fetchValues1[0]))) << endl;
	float* data = static_cast<float*>(TF_TensorData(fetchValues1[0]));
	size_t size = TF_TensorByteSize(fetchValues1[0]) / sizeof(float);
	for (size_t i = 0; i < size; i++) {
		cout << data[i] << endl;
	}
	// Clean up.
	TF_DeletePRunHandle(handle);
	TF_DeleteSession(sess, s);
	cout << TF_Message(s);
	TF_DeleteGraph(graph);
	TF_DeleteStatus(s);
	return 0;
}

本作品采用知识共享署名 4.0 国际许可协议进行许可。

发表回复