我正在尝试构建一个简单的程序,以使用C ++上的Caffe2库进行推理。我从网上举了一些例子,代码看起来像这样:
#include <iostream>
#include "caffe2/caffe2.hpp"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h"
using namespace caffe2;
using namespace std;
NetDef m_Init_net, m_Predict_net;
Workspace m_Workspace;
map<string, DeviceOption> m_BlobOptionMap;
void readNetwork(const string& proto_file, const string trained_file) {
CAFFE_ENFORCE(ReadProtoFromFile(proto_file, &m_Init_net));
CAFFE_ENFORCE(ReadProtoFromFile(trained_file, &m_Predict_net));
}
bool enableCPU() {
m_Predict_net.mutable_device_option()->set_device_type((int)CPU);
m_Init_net.mutable_device_option()->set_device_type((int)CPU);
for (int i = 0; i<m_Predict_net.op_size(); ++i) {
m_Predict_net.mutable_op(i)->mutable_device_option()->set_device_type((int)CPU);
}
for (int i = 0; i<m_Init_net.op_size(); ++i) {
m_Init_net.mutable_op(i)->mutable_device_option()->set_device_type((int)CPU);
}
}
void initializeNetwork() {
enableCPU();
CAFFE_ENFORCE(m_Workspace.RunNetOnce(m_Init_net));
CAFFE_ENFORCE(m_Workspace.CreateNet(m_Predict_net));
}
bool setInput(float* input, vector<TIndex> vInputDims, string sBlobName) {
if (!m_Workspace.HasBlob(sBlobName)) {
cerr << "The workspace doesn't have the blob " + sBlobName << endl;
return false;
}
cerr << "Success finding blob " + sBlobName << endl;
auto _tensor = m_Workspace.GetBlob(sBlobName)->GetMutableTensor(CPU);
_tensor->Resize(vInputDims);
cerr << "Dimensions set" << endl;
auto tensor_ = m_Workspace.GetBlob(sBlobName)->GetMutableTensor(CPU);
CPUContext context(m_BlobOptionMap[sBlobName]);
context.SwitchToDevice();
context.template CopyBytes< CPUContext, CPUContext>(tensor_->size() * sizeof(float), static_cast<void*>(input), tensor_->raw_mutable_data());
context.FinishDeviceComputation();
cerr << "Input set" << endl;
return true;
}
bool run() {
bool result = m_Workspace.RunNet(m_Predict_net.name());
return result;
}
TensorCPU getTensorByName(string sBlobName) {
return TensorCPU(m_Workspace.GetBlob(sBlobName)->Get<TensorCPU>());
}
vector<float> getBlobContentByName(std::string name) {
TensorCPU cpudata = getTensorByName(name); // need a instance to save
const float *data = cpudata.data<float>();
const int size = cpudata.size();
return std::vector<float>(data, data + size);
}
int main() {
srand(time(NULL));
string s1, s2;
cin >> s1 >> s2;
readNetwork(s1, s2);
initializeNetwork();
getchar();
float* input = new float[227*227*3];
for (int i = 0; i < 227 * 227 * 3; i++) input[i] = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);;
vector<TIndex> inputshape({1,3,224,224});
setInput(input, inputshape, "data");
vector<float> result = getBlobContentByName("softmaxout");
sort(result.begin(), result.end());
for (int i = 999; i > 990; i--) cout << result[i] << endl;
return 0;
}
“ caffe2 / caffe2.hpp”包含用于编译此代码的所有必需的include文件。另外,我还包括了protobuf库和静态库。
不幸的是,它显示以下错误:
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) protected: __cdecl c10::intrusive_ptr_target::intrusive_ptr_target(void)" (__imp_??0intrusive_ptr_target@c10@@IEAA@XZ)
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __cdecl at::BaseContext::BaseContext(void)" (__imp_??0BaseContext@at@@QEAA@XZ)
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: void __cdecl at::BaseContext::SwitchToDevice(void)" (__imp_?SwitchToDevice@BaseContext@at@@QEAAXXZ)
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static class caffe2::TypeIdentifier __cdecl caffe2::TypeMeta::Id<class caffe2::Tensor>(void)" (__imp_??$Id@VTensor@caffe2@@@TypeMeta@caffe2@@SA?AVTypeIdentifier@1@XZ)
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static class caffe2::TypeIdentifier __cdecl caffe2::TypeMeta::Id<float>(void)" (__imp_??$Id@M@TypeMeta@caffe2@@SA?AVTypeIdentifier@1@XZ)
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: void (__cdecl*__cdecl caffe2::TypeMeta::dtor(void)const )(void *,unsigned __int64)" (__imp_?dtor@TypeMeta@caffe2@@QEBAP6AXPEAX_K@ZXZ)
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: void (__cdecl*__cdecl caffe2::TypeMeta::ctor(void)const )(void *,unsigned __int64)" (__imp_?ctor@TypeMeta@caffe2@@QEBAP6AXPEAX_K@ZXZ)
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: unsigned __int64 const & __cdecl caffe2::TypeMeta::itemsize(void)const " (__imp_?itemsize@TypeMeta@caffe2@@QEBAAEB_KXZ)
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: class caffe2::TypeIdentifier const & __cdecl caffe2::TypeMeta::id(void)const " (__imp_?id@TypeMeta@caffe2@@QEBAAEBVTypeIdentifier@2@XZ)
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) private: __cdecl caffe2::TypeMeta::TypeMeta(class caffe2::TypeIdentifier,unsigned __int64,void (__cdecl*)(void *,unsigned __int64),void (__cdecl*)(void const *,void *,unsigned __int64),void (__cdecl*)(void *,unsigned __int64))" (__imp_??0TypeMeta@caffe2@@AEAA@VTypeIdentifier@1@_KP6AXPEAX1@ZP6AXPEBX21@Z3@Z)
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __cdecl caffe2::TypeMeta::TypeMeta(void)" (__imp_??0TypeMeta@caffe2@@QEAA@XZ)
1>Source.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static class caffe2::TypeIdentifier __cdecl caffe2::TypeIdentifier::uninitialized(void)" (__imp_?uninitialized@TypeIdentifier@caffe2@@SA?AV12@XZ)
1>D:\UserData\z003yjnk\Documents\Visual Studio 2015\Projects\Project1\x64\Release\Project1.exe : fatal error LNK1120: 12 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
似乎缺少一个.lib文件。有什么想法吗?
谢谢。