mirror of
https://github.com/opencv/opencv.git
synced 2026-07-30 15:53:03 +04:00
add loading TensorFlow/Caffe net from memory buffer
add a corresponding test
This commit is contained in:
@@ -55,6 +55,24 @@ static std::string _tf(TString filename)
|
||||
return (getOpenCVExtraDir() + "/dnn/") + filename;
|
||||
}
|
||||
|
||||
TEST(Test_Caffe, memory_read)
|
||||
{
|
||||
const string proto = findDataFile("dnn/bvlc_googlenet.prototxt", false);
|
||||
const string model = findDataFile("dnn/bvlc_googlenet.caffemodel", false);
|
||||
|
||||
string dataProto;
|
||||
ASSERT_TRUE(readFileInMemory(proto, dataProto));
|
||||
string dataModel;
|
||||
ASSERT_TRUE(readFileInMemory(model, dataModel));
|
||||
|
||||
Net net = readNetFromCaffe(dataProto.c_str(), dataProto.size());
|
||||
ASSERT_FALSE(net.empty());
|
||||
|
||||
Net net2 = readNetFromCaffe(dataProto.c_str(), dataProto.size(),
|
||||
dataModel.c_str(), dataModel.size());
|
||||
ASSERT_FALSE(net2.empty());
|
||||
}
|
||||
|
||||
TEST(Test_Caffe, read_gtsrb)
|
||||
{
|
||||
Net net = readNetFromCaffe(_tf("gtsrb.prototxt"));
|
||||
@@ -67,13 +85,26 @@ TEST(Test_Caffe, read_googlenet)
|
||||
ASSERT_FALSE(net.empty());
|
||||
}
|
||||
|
||||
TEST(Reproducibility_AlexNet, Accuracy)
|
||||
typedef testing::TestWithParam<tuple<bool> > Reproducibility_AlexNet;
|
||||
TEST_P(Reproducibility_AlexNet, Accuracy)
|
||||
{
|
||||
bool readFromMemory = get<0>(GetParam());
|
||||
Net net;
|
||||
{
|
||||
const string proto = findDataFile("dnn/bvlc_alexnet.prototxt", false);
|
||||
const string model = findDataFile("dnn/bvlc_alexnet.caffemodel", false);
|
||||
net = readNetFromCaffe(proto, model);
|
||||
if (readFromMemory)
|
||||
{
|
||||
string dataProto;
|
||||
ASSERT_TRUE(readFileInMemory(proto, dataProto));
|
||||
string dataModel;
|
||||
ASSERT_TRUE(readFileInMemory(model, dataModel));
|
||||
|
||||
net = readNetFromCaffe(dataProto.c_str(), dataProto.size(),
|
||||
dataModel.c_str(), dataModel.size());
|
||||
}
|
||||
else
|
||||
net = readNetFromCaffe(proto, model);
|
||||
ASSERT_FALSE(net.empty());
|
||||
}
|
||||
|
||||
@@ -86,6 +117,8 @@ TEST(Reproducibility_AlexNet, Accuracy)
|
||||
normAssert(ref, out);
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_CASE_P(Test_Caffe, Reproducibility_AlexNet, testing::Values(true, false));
|
||||
|
||||
#if !defined(_WIN32) || defined(_WIN64)
|
||||
TEST(Reproducibility_FCN, Accuracy)
|
||||
{
|
||||
|
||||
@@ -57,4 +57,23 @@ inline void normAssert(cv::InputArray ref, cv::InputArray test, const char *comm
|
||||
EXPECT_LE(normInf, lInf) << comment;
|
||||
}
|
||||
|
||||
inline bool readFileInMemory(const std::string& filename, std::string& content)
|
||||
{
|
||||
std::ios::openmode mode = std::ios::in | std::ios::binary;
|
||||
std::ifstream ifs(filename.c_str(), mode);
|
||||
if (!ifs.is_open())
|
||||
return false;
|
||||
|
||||
content.clear();
|
||||
|
||||
ifs.seekg(0, std::ios::end);
|
||||
content.reserve(ifs.tellg());
|
||||
ifs.seekg(0, std::ios::beg);
|
||||
|
||||
content.assign((std::istreambuf_iterator<char>(ifs)),
|
||||
std::istreambuf_iterator<char>());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -75,14 +75,32 @@ static std::string path(const std::string& file)
|
||||
}
|
||||
|
||||
static void runTensorFlowNet(const std::string& prefix, bool hasText = false,
|
||||
double l1 = 1e-5, double lInf = 1e-4)
|
||||
double l1 = 1e-5, double lInf = 1e-4,
|
||||
bool memoryLoad = false)
|
||||
{
|
||||
std::string netPath = path(prefix + "_net.pb");
|
||||
std::string netConfig = (hasText ? path(prefix + "_net.pbtxt") : "");
|
||||
std::string inpPath = path(prefix + "_in.npy");
|
||||
std::string outPath = path(prefix + "_out.npy");
|
||||
|
||||
Net net = readNetFromTensorflow(netPath, netConfig);
|
||||
Net net;
|
||||
if (memoryLoad)
|
||||
{
|
||||
// Load files into a memory buffers
|
||||
string dataModel;
|
||||
ASSERT_TRUE(readFileInMemory(netPath, dataModel));
|
||||
|
||||
string dataConfig;
|
||||
if (hasText)
|
||||
ASSERT_TRUE(readFileInMemory(netConfig, dataConfig));
|
||||
|
||||
net = readNetFromTensorflow(dataModel.c_str(), dataModel.size(),
|
||||
dataConfig.c_str(), dataConfig.size());
|
||||
}
|
||||
else
|
||||
net = readNetFromTensorflow(netPath, netConfig);
|
||||
|
||||
ASSERT_FALSE(net.empty());
|
||||
|
||||
cv::Mat input = blobFromNPY(inpPath);
|
||||
cv::Mat target = blobFromNPY(outPath);
|
||||
@@ -216,4 +234,15 @@ TEST(Test_TensorFlow, resize_nearest_neighbor)
|
||||
runTensorFlowNet("resize_nearest_neighbor");
|
||||
}
|
||||
|
||||
TEST(Test_TensorFlow, memory_read)
|
||||
{
|
||||
double l1 = 1e-5;
|
||||
double lInf = 1e-4;
|
||||
runTensorFlowNet("lstm", true, l1, lInf, true);
|
||||
|
||||
runTensorFlowNet("batch_norm", false, l1, lInf, true);
|
||||
runTensorFlowNet("fused_batch_norm", false, l1, lInf, true);
|
||||
runTensorFlowNet("batch_norm_text", true, l1, lInf, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user