Cleaned caffe dependencies

pull/265/head
Vitaliy Lyudvichenko 10 years ago
parent df5eec6844
commit f28effb490
  1. 4
      modules/dnn/src/caffe/caffe_importer.cpp
  2. 98
      modules/dnn/src/caffe/caffe_io.cpp
  3. 19
      modules/dnn/src/caffe/caffe_io.hpp
  4. 40
      modules/dnn/src/caffe/common.hpp
  5. 12
      modules/dnn/src/caffe/glog_emulator.hpp
  6. 70
      modules/dnn/src/caffe/util/io.cpp
  7. 146
      modules/dnn/src/caffe/util/io.hpp
  8. 66
      modules/dnn/src/caffe/util/upgrade_proto.hpp
  9. 71
      modules/dnn/src/layers/mvn_layer.cpp
  10. 4
      modules/dnn/testdata/dnn/layers/run.py

@ -11,7 +11,7 @@ using namespace cv::dnn;
#include <google/protobuf/message.h>
#include <google/protobuf/text_format.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include "caffe/util/upgrade_proto.hpp"
#include "caffe_io.hpp"
using ::google::protobuf::RepeatedField;
using ::google::protobuf::RepeatedPtrField;
@ -31,7 +31,7 @@ namespace
CaffeImporter(const char *pototxt, const char *caffeModel)
{
ReadNetParamsFromTextFileOrDie(std::string(pototxt), &net);
ReadNetParamsFromTextFileOrDie(pototxt, &net);
if (caffeModel && caffeModel[0])
ReadNetParamsFromBinaryFileOrDie(caffeModel, &netBinary);

@ -3,15 +3,72 @@
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <opencv2/core.hpp>
#include <map>
#include <string>
#include <fstream>
#include <vector>
#include "caffe/common.hpp"
#include "caffe.pb.h"
#include "caffe/util/io.hpp"
#include "caffe/util/upgrade_proto.hpp"
#include "caffe_io.hpp"
#include "glog_emulator.hpp"
namespace cv {
namespace dnn {
using std::string;
using std::map;
using namespace caffe;
using namespace ::google::protobuf;
using namespace ::google::protobuf::io;
// Return true iff the net is not the current version.
bool NetNeedsUpgrade(const NetParameter& net_param);
// Return true iff any layer contains parameters specified using
// deprecated V0LayerParameter.
bool NetNeedsV0ToV1Upgrade(const NetParameter& net_param);
// Perform all necessary transformations to upgrade a V0NetParameter into a
// NetParameter (including upgrading padding layers and LayerParameters).
bool UpgradeV0Net(const NetParameter& v0_net_param, NetParameter* net_param);
// Upgrade NetParameter with padding layers to pad-aware conv layers.
// For any padding layer, remove it and put its pad parameter in any layers
// taking its top blob as input.
// Error if any of these above layers are not-conv layers.
void UpgradeV0PaddingLayers(const NetParameter& param,
NetParameter* param_upgraded_pad);
// Upgrade a single V0LayerConnection to the V1LayerParameter format.
bool UpgradeV0LayerParameter(const V1LayerParameter& v0_layer_connection,
V1LayerParameter* layer_param);
V1LayerParameter_LayerType UpgradeV0LayerType(const string& type);
// Return true iff any layer contains deprecated data transformation parameters.
bool NetNeedsDataUpgrade(const NetParameter& net_param);
// Perform all necessary transformations to upgrade old transformation fields
// into a TransformationParameter.
void UpgradeNetDataTransformation(NetParameter* net_param);
// Return true iff the Net contains any layers specified as V1LayerParameters.
bool NetNeedsV1ToV2Upgrade(const NetParameter& net_param);
// Perform all necessary transformations to upgrade a NetParameter with
// deprecated V1LayerParameters.
bool UpgradeV1Net(const NetParameter& v1_net_param, NetParameter* net_param);
bool UpgradeV1LayerParameter(const V1LayerParameter& v1_layer_param,
LayerParameter* layer_param);
const char* UpgradeV1LayerType(const V1LayerParameter_LayerType type);
// Check for deprecations and upgrade the NetParameter as needed.
bool UpgradeNetAsNeeded(const string& param_file, NetParameter* param);
namespace caffe {
bool NetNeedsUpgrade(const NetParameter& net_param) {
return NetNeedsV0ToV1Upgrade(net_param) || NetNeedsV1ToV2Upgrade(net_param);
@ -924,19 +981,46 @@ const char* UpgradeV1LayerType(const V1LayerParameter_LayerType type) {
}
}
void ReadNetParamsFromTextFileOrDie(const string& param_file,
const int kProtoReadBytesLimit = INT_MAX; // Max size of 2 GB minus 1 byte.
bool ReadProtoFromTextFile(const char* filename, Message* proto) {
std::ifstream fs(filename, std::ifstream::in);
CHECK(fs.is_open()) << "Can't open \"" << filename << "\"";
IstreamInputStream input(&fs);
bool success = google::protobuf::TextFormat::Parse(&input, proto);
fs.close();
return success;
}
bool ReadProtoFromBinaryFile(const char* filename, Message* proto) {
std::ifstream fs(filename, std::ifstream::in | std::ifstream::binary);
CHECK(fs.is_open()) << "Can't open \"" << filename << "\"";
ZeroCopyInputStream* raw_input = new IstreamInputStream(&fs);
CodedInputStream* coded_input = new CodedInputStream(raw_input);
coded_input->SetTotalBytesLimit(kProtoReadBytesLimit, 536870912);
bool success = proto->ParseFromCodedStream(coded_input);
delete coded_input;
delete raw_input;
fs.close();
return success;
}
void ReadNetParamsFromTextFileOrDie(const char* param_file,
NetParameter* param) {
CHECK(ReadProtoFromTextFile(param_file, param))
<< "Failed to parse NetParameter file: " << param_file;
UpgradeNetAsNeeded(param_file, param);
}
void ReadNetParamsFromBinaryFileOrDie(const string& param_file,
void ReadNetParamsFromBinaryFileOrDie(const char* param_file,
NetParameter* param) {
CHECK(ReadProtoFromBinaryFile(param_file, param))
<< "Failed to parse NetParameter file: " << param_file;
UpgradeNetAsNeeded(param_file, param);
}
} // namespace caffe
}
}
#endif

@ -0,0 +1,19 @@
#ifndef __OPENCV_DNN_CAFFE_IO_HPP__
#define __OPENCV_DNN_CAFFE_IO_HPP__
#if HAVE_PROTOBUF
#include "caffe.pb.h"
namespace cv {
namespace dnn {
// Read parameters from a file into a NetParameter proto message.
void ReadNetParamsFromTextFileOrDie(const char* param_file,
caffe::NetParameter* param);
void ReadNetParamsFromBinaryFileOrDie(const char* param_file,
caffe::NetParameter* param);
}
}
#endif
#endif

@ -1,40 +0,0 @@
#ifndef CAFFE_COMMON_HPP_
#define CAFFE_COMMON_HPP_
#include <opencv2/core.hpp>
#include <iostream>
#include "glog_emulator.hpp"
//#include <gflags/gflags.h>
//#include <glog/logging.h>
#include <climits>
#include <cmath>
#include <fstream> // NOLINT(readability/streams)
#include <iostream> // NOLINT(readability/streams)
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <utility> // pair
#include <vector>
namespace caffe {
// Common functions and classes from std that caffe often uses.
using std::fstream;
using std::ios;
using std::isnan;
using std::isinf;
using std::iterator;
using std::make_pair;
using std::map;
using std::ostringstream;
using std::pair;
using std::set;
using std::string;
using std::stringstream;
using std::vector;
} // namespace caffe
#endif // CAFFE_COMMON_HPP_

@ -1,6 +1,6 @@
#ifndef __OPENCV_DNN_CAFFE_GLOG_EMULATOR__
#define __OPENCV_DNN_CAFFE_GLOG_EMULATOR__
#include <stdlib.h>
#ifndef __OPENCV_DNN_CAFFE_GLOG_EMULATOR_HPP__
#define __OPENCV_DNN_CAFFE_GLOG_EMULATOR_HPP__
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <opencv2/core.hpp>
@ -44,10 +44,10 @@ public:
}
else if (!cond_str && strcmp(type, "CHECK"))
{
if (!strcmp(type, "INFO"))
std::cout << stream.str();
if (!std::strcmp(type, "INFO"))
std::cout << stream.str() << std::endl;
else
std::cerr << stream.str();
std::cerr << stream.str() << std::endl;
}
}
};

@ -1,70 +0,0 @@
#if HAVE_PROTOBUF
#include "io.hpp"
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <opencv2/core.hpp>
//#include <stdint.h>
//
//#include <algorithm>
#include <fstream> // NOLINT(readability/streams)
//#include <string>
//#include <vector>
//
//#include "caffe/common.hpp"
//#include "caffe.pb.h"
//#include "caffe/util/io.hpp"
//
const int kProtoReadBytesLimit = INT_MAX; // Max size of 2 GB minus 1 byte.
namespace caffe {
using google::protobuf::io::FileInputStream;
using google::protobuf::io::FileOutputStream;
using google::protobuf::io::ZeroCopyInputStream;
using google::protobuf::io::CodedInputStream;
using google::protobuf::io::ZeroCopyOutputStream;
using google::protobuf::io::CodedOutputStream;
using google::protobuf::io::IstreamInputStream;
using google::protobuf::Message;
bool ReadProtoFromTextFile(const char* filename, Message* proto) {
std::ifstream fs(filename, std::ifstream::in);
CHECK(fs.is_open()) << "Can't open \"" << filename << "\"";
IstreamInputStream input(&fs);
bool success = google::protobuf::TextFormat::Parse(&input, proto);
fs.close();
return success;
}
//
//void WriteProtoToTextFile(const Message& proto, const char* filename) {
// int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
// FileOutputStream* output = new FileOutputStream(fd);
// CHECK(google::protobuf::TextFormat::Print(proto, output));
// delete output;
// close(fd);
//}
//
bool ReadProtoFromBinaryFile(const char* filename, Message* proto) {
std::ifstream fs(filename, std::ifstream::in | std::ifstream::binary);
CHECK(fs.is_open()) << "Can't open \"" << filename << "\"";
ZeroCopyInputStream* raw_input = new IstreamInputStream(&fs);
CodedInputStream* coded_input = new CodedInputStream(raw_input);
coded_input->SetTotalBytesLimit(kProtoReadBytesLimit, 536870912);
bool success = proto->ParseFromCodedStream(coded_input);
delete coded_input;
delete raw_input;
fs.close();
return success;
}
//
//void WriteProtoToBinaryFile(const Message& proto, const char* filename) {
// fstream output(filename, ios::out | ios::trunc | ios::binary);
// CHECK(proto.SerializeToOstream(&output));
//}
} // namespace caffe
#endif

@ -1,146 +0,0 @@
#ifndef CAFFE_UTIL_IO_H_
#define CAFFE_UTIL_IO_H_
#if HAVE_PROTOBUF
//instead of GLOG
#include "../glog_emulator.hpp"
//#include <unistd.h>
#include <string>
#include <stdio.h>
#include <google/protobuf/message.h>
#include "caffe/common.hpp"
#include "caffe.pb.h"
namespace caffe {
using ::google::protobuf::Message;
//inline void MakeTempFilename(string* temp_filename) {
// temp_filename->clear();
// *temp_filename = "/tmp/caffe_test.XXXXXX";
// char* temp_filename_cstr = new char[temp_filename->size() + 1];
// // NOLINT_NEXT_LINE(runtime/printf)
// strcpy(temp_filename_cstr, temp_filename->c_str());
// int fd = mkstemp(temp_filename_cstr);
// CHECK_GE(fd, 0) << "Failed to open a temporary file at: " << *temp_filename;
// close(fd);
// *temp_filename = temp_filename_cstr;
// delete[] temp_filename_cstr;
//}
//
//inline void MakeTempDir(string* temp_dirname) {
// temp_dirname->clear();
// *temp_dirname = "/tmp/caffe_test.XXXXXX";
// char* temp_dirname_cstr = new char[temp_dirname->size() + 1];
// // NOLINT_NEXT_LINE(runtime/printf)
// strcpy(temp_dirname_cstr, temp_dirname->c_str());
// char* mkdtemp_result = mkdtemp(temp_dirname_cstr);
// CHECK(mkdtemp_result != NULL)
// << "Failed to create a temporary directory at: " << *temp_dirname;
// *temp_dirname = temp_dirname_cstr;
// delete[] temp_dirname_cstr;
//}
bool ReadProtoFromTextFile(const char* filename, Message* proto);
inline bool ReadProtoFromTextFile(const string& filename, Message* proto) {
return ReadProtoFromTextFile(filename.c_str(), proto);
}
inline void ReadProtoFromTextFileOrDie(const char* filename, Message* proto) {
CHECK(ReadProtoFromTextFile(filename, proto));
}
inline void ReadProtoFromTextFileOrDie(const string& filename, Message* proto) {
ReadProtoFromTextFileOrDie(filename.c_str(), proto);
}
void WriteProtoToTextFile(const Message& proto, const char* filename);
inline void WriteProtoToTextFile(const Message& proto, const string& filename) {
WriteProtoToTextFile(proto, filename.c_str());
}
bool ReadProtoFromBinaryFile(const char* filename, Message* proto);
inline bool ReadProtoFromBinaryFile(const string& filename, Message* proto) {
return ReadProtoFromBinaryFile(filename.c_str(), proto);
}
inline void ReadProtoFromBinaryFileOrDie(const char* filename, Message* proto) {
CHECK(ReadProtoFromBinaryFile(filename, proto));
}
inline void ReadProtoFromBinaryFileOrDie(const string& filename,
Message* proto) {
ReadProtoFromBinaryFileOrDie(filename.c_str(), proto);
}
void WriteProtoToBinaryFile(const Message& proto, const char* filename);
inline void WriteProtoToBinaryFile(
const Message& proto, const string& filename) {
WriteProtoToBinaryFile(proto, filename.c_str());
}
bool ReadFileToDatum(const string& filename, const int label, Datum* datum);
inline bool ReadFileToDatum(const string& filename, Datum* datum) {
return ReadFileToDatum(filename, -1, datum);
}
bool ReadImageToDatum(const string& filename, const int label,
const int height, const int width, const bool is_color,
const std::string & encoding, Datum* datum);
inline bool ReadImageToDatum(const string& filename, const int label,
const int height, const int width, const bool is_color, Datum* datum) {
return ReadImageToDatum(filename, label, height, width, is_color,
"", datum);
}
inline bool ReadImageToDatum(const string& filename, const int label,
const int height, const int width, Datum* datum) {
return ReadImageToDatum(filename, label, height, width, true, datum);
}
inline bool ReadImageToDatum(const string& filename, const int label,
const bool is_color, Datum* datum) {
return ReadImageToDatum(filename, label, 0, 0, is_color, datum);
}
inline bool ReadImageToDatum(const string& filename, const int label,
Datum* datum) {
return ReadImageToDatum(filename, label, 0, 0, true, datum);
}
inline bool ReadImageToDatum(const string& filename, const int label,
const std::string & encoding, Datum* datum) {
return ReadImageToDatum(filename, label, 0, 0, true, encoding, datum);
}
bool DecodeDatumNative(Datum* datum);
bool DecodeDatum(Datum* datum, bool is_color);
cv::Mat ReadImageToCVMat(const string& filename,
const int height, const int width, const bool is_color);
cv::Mat ReadImageToCVMat(const string& filename,
const int height, const int width);
cv::Mat ReadImageToCVMat(const string& filename,
const bool is_color);
cv::Mat ReadImageToCVMat(const string& filename);
cv::Mat DecodeDatumToCVMatNative(const Datum& datum);
cv::Mat DecodeDatumToCVMat(const Datum& datum, bool is_color);
void CVMatToDatum(const cv::Mat& cv_img, Datum* datum);
} // namespace caffe
#endif
#endif // CAFFE_UTIL_IO_H_

@ -1,66 +0,0 @@
#ifndef CAFFE_UTIL_UPGRADE_PROTO_H_
#define CAFFE_UTIL_UPGRADE_PROTO_H_
#if HAVE_PROTOBUF
#include <string>
#include "caffe/common.hpp"
#include "caffe.pb.h"
namespace caffe {
// Return true iff the net is not the current version.
bool NetNeedsUpgrade(const NetParameter& net_param);
// Return true iff any layer contains parameters specified using
// deprecated V0LayerParameter.
bool NetNeedsV0ToV1Upgrade(const NetParameter& net_param);
// Perform all necessary transformations to upgrade a V0NetParameter into a
// NetParameter (including upgrading padding layers and LayerParameters).
bool UpgradeV0Net(const NetParameter& v0_net_param, NetParameter* net_param);
// Upgrade NetParameter with padding layers to pad-aware conv layers.
// For any padding layer, remove it and put its pad parameter in any layers
// taking its top blob as input.
// Error if any of these above layers are not-conv layers.
void UpgradeV0PaddingLayers(const NetParameter& param,
NetParameter* param_upgraded_pad);
// Upgrade a single V0LayerConnection to the V1LayerParameter format.
bool UpgradeV0LayerParameter(const V1LayerParameter& v0_layer_connection,
V1LayerParameter* layer_param);
V1LayerParameter_LayerType UpgradeV0LayerType(const string& type);
// Return true iff any layer contains deprecated data transformation parameters.
bool NetNeedsDataUpgrade(const NetParameter& net_param);
// Perform all necessary transformations to upgrade old transformation fields
// into a TransformationParameter.
void UpgradeNetDataTransformation(NetParameter* net_param);
// Return true iff the Net contains any layers specified as V1LayerParameters.
bool NetNeedsV1ToV2Upgrade(const NetParameter& net_param);
// Perform all necessary transformations to upgrade a NetParameter with
// deprecated V1LayerParameters.
bool UpgradeV1Net(const NetParameter& v1_net_param, NetParameter* net_param);
bool UpgradeV1LayerParameter(const V1LayerParameter& v1_layer_param,
LayerParameter* layer_param);
const char* UpgradeV1LayerType(const V1LayerParameter_LayerType type);
// Check for deprecations and upgrade the NetParameter as needed.
bool UpgradeNetAsNeeded(const string& param_file, NetParameter* param);
// Read parameters from a file into a NetParameter proto message.
void ReadNetParamsFromTextFileOrDie(const string& param_file,
NetParameter* param);
void ReadNetParamsFromBinaryFileOrDie(const string& param_file,
NetParameter* param);
} // namespace caffe
#endif
#endif // CAFFE_UTIL_UPGRADE_PROTO_H_

@ -0,0 +1,71 @@
#include "../precomp.hpp"
#include "layers_common.hpp"
namespace cv
{
namespace dnn
{
class MVNLayer : public Layer
{
double eps;
bool acrossChannels, normalizeVariance;
public:
MVNLayer(LayerParams &params);
void allocate(const std::vector<Blob*> &inputs, std::vector<Blob> &outputs);
void forward(std::vector<Blob*> &inputs, std::vector<Blob> &outputs);
};
REGISTER_LAYER_CLASS(MVN, MVNLayer)
MVNLayer::MVNLayer(LayerParams &params)
{
eps = params.get<double>("eps", 1e-9);
acrossChannels = params.get<bool>("across_channels", false);
normalizeVariance = params.get<bool>("normalize_variance", true);
}
void MVNLayer::allocate(const std::vector<Blob *> &inputs, std::vector<Blob> &outputs)
{
outputs.resize(inputs.size());
for (size_t i = 0; i < inputs.size(); i++)
{
CV_Assert(!acrossChannels || inputs[i]->dims() >= 2);
outputs[i].create(inputs[i]->shape(), inputs[i]->type());
}
}
void MVNLayer::forward(std::vector<Blob *> &inputs, std::vector<Blob> &outputs)
{
for (size_t inpIdx = 0; inpIdx < inputs.size(); inpIdx++)
{
Blob &inpBlob = *inputs[inpIdx];
Blob &outBlob = outputs[inpIdx];
int workSize[2];
int splitDim = (acrossChannels) ? 1 : 2;
workSize[0] = inpBlob.total(0, splitDim);
workSize[1] = inpBlob.total(splitDim);
Mat inpMat = inpBlob.getMatRef().reshape(1, 2, workSize);
Mat outMat = outBlob.getMatRef().reshape(1, 2, workSize);
Scalar mean, dev;
for (int i = 0; i < workSize[0]; i++)
{
Mat inpRow = inpMat.row(i);
Mat outRow = outMat.row(i);
cv::meanStdDev(inpRow, mean, (normalizeVariance) ? dev : noArray());
double alpha = (normalizeVariance) ? 1/(eps + dev[0]) : 1;
inpRow.convertTo(outRow, outRow.type(), alpha, -mean[0] * alpha);
}
}
}
}
}

@ -5,10 +5,6 @@ import sys, os, glob
CAFFE_ROOT = "/home/vitaliy/opencv/caffe/"
sys.path.insert(0, CAFFE_ROOT + 'python')
CV2_DIR = "/home/vitaliy/opencv/build-opencv-qt/lib"
sys.path.insert(0, CV2_DIR)
import numpy as np
import caffe
import cv2

Loading…
Cancel
Save