Commit 355d07ec authored by Anna Petrovicheva's avatar Anna Petrovicheva

Network initialized successfully

parent b65a84fa
......@@ -500,7 +500,7 @@ layer {
type: "constant"
value: 0
}
# dilation: 6
dilation: 6
}
}
layer {
......@@ -750,7 +750,7 @@ layer {
}
layer {
name: "conv4_3_norm"
type: "Normalize"
type: "NormalizeBBox"
bottom: "conv4_3"
top: "conv4_3_norm"
normalize_bbox_param {
......
......@@ -9,48 +9,46 @@ using namespace cv::dnn;
#include <cstdlib>
using namespace std;
static const string fcnType = "fcn8s";
static void colorizeSegmentation(dnn::Blob& score,
const vector<cv::Vec3b>& colors,
cv::Mat& segm)
{
const int rows = score.rows();
const int cols = score.cols();
const int chns = score.channels();
cv::Mat maxCl(rows, cols, CV_8UC1);
cv::Mat maxVal(rows, cols, CV_32FC1);
for (int ch = 0; ch < chns; ch++)
{
for (int row = 0; row < rows; row++)
{
const float* ptrScore = score.ptrf(0, ch, row);
uchar* ptrMaxCl = maxCl.ptr<uchar>(row);
float* ptrMaxVal = maxVal.ptr<float>(row);
for (int col = 0; col < cols; col++)
{
if (ptrScore[col] > ptrMaxVal[col])
{
ptrMaxVal[col] = ptrScore[col];
ptrMaxCl[col] = ch;
}
}
}
}
segm.create(rows, cols, CV_8UC3);
for (int row = 0; row < rows; row++)
{
const uchar* ptrMaxCl = maxCl.ptr<uchar>(row);
cv::Vec3b* ptrSegm = segm.ptr<cv::Vec3b>(row);
for (int col = 0; col < cols; col++)
{
ptrSegm[col] = colors[ptrMaxCl[col]];
}
}
}
//static void colorizeSegmentation(dnn::Blob& score,
// const vector<cv::Vec3b>& colors,
// cv::Mat& segm)
//{
// const int rows = score.rows();
// const int cols = score.cols();
// const int chns = score.channels();
// cv::Mat maxCl(rows, cols, CV_8UC1);
// cv::Mat maxVal(rows, cols, CV_32FC1);
// for (int ch = 0; ch < chns; ch++)
// {
// for (int row = 0; row < rows; row++)
// {
// const float* ptrScore = score.ptrf(0, ch, row);
// uchar* ptrMaxCl = maxCl.ptr<uchar>(row);
// float* ptrMaxVal = maxVal.ptr<float>(row);
// for (int col = 0; col < cols; col++)
// {
// if (ptrScore[col] > ptrMaxVal[col])
// {
// ptrMaxVal[col] = ptrScore[col];
// ptrMaxCl[col] = ch;
// }
// }
// }
// }
// segm.create(rows, cols, CV_8UC3);
// for (int row = 0; row < rows; row++)
// {
// const uchar* ptrMaxCl = maxCl.ptr<uchar>(row);
// cv::Vec3b* ptrSegm = segm.ptr<cv::Vec3b>(row);
// for (int col = 0; col < cols; col++)
// {
// ptrSegm[col] = colors[ptrMaxCl[col]];
// }
// }
//}
const char* about = "This sample uses Single-Shot Detector to detect objects "
"from camera\n"; // TODO: link
......@@ -111,6 +109,7 @@ int main(int argc, char** argv)
return 0;
}
size_t i = 0;
for (;; )
{
Mat frame;
......@@ -118,12 +117,15 @@ int main(int argc, char** argv)
if (frame.empty())
break;
//! [Prepare blob]
resize(frame, frame, Size(300, 300)); //SSD accepts 300x300 RGB-images
dnn::Blob inputBlob = dnn::Blob(frame); //Convert Mat to dnn::Blob image
//! [Prepare blob]
std::ostringstream stream;
stream << "folder/" << i << ".jpg";
imwrite(stream.str(), frame);
//! [Set input blob]
net.setBlob(".data", inputBlob); //set the network input
//! [Set input blob]
......@@ -132,20 +134,20 @@ int main(int argc, char** argv)
net.forward(); //compute output
//! [Make forward pass]
//! [Gather output]
dnn::Blob detection = net.getBlob("detection_out");
// //! [Gather output]
// dnn::Blob detection = net.getBlob("detection_out");
// cv::Mat colorize;
// colorizeSegmentation(score, colors, colorize);
// cv::Mat show;
// cv::addWeighted(img, 0.4, colorize, 0.6, 0.0, show);
// cv::imshow("show", show);
// cv::waitKey(0);
// return 0;
// // cv::Mat colorize;
// // colorizeSegmentation(score, colors, colorize);
// // cv::Mat show;
// // cv::addWeighted(img, 0.4, colorize, 0.6, 0.0, show);
// // cv::imshow("show", show);
// // cv::waitKey(0);
// // return 0;
imshow("frame", frame);
if (waitKey(1) == 27)
break; // stop capturing by pressing ESC
// imshow("frame", frame);
// if (waitKey(1) == 27)
// break; // stop capturing by pressing ESC
}
camera.release();
......
......@@ -404,7 +404,7 @@ message ParamSpec {
// NOTE
// Update the next available ID when you add a new LayerParameter field.
//
// LayerParameter next available layer-specific ID: 138 (last added: detection_output_param)
// LayerParameter next available layer-specific ID: 142 (last added: detection_output_param)
message LayerParameter {
optional string name = 1; // the layer name
optional string type = 2; // the layer type
......
......@@ -55,6 +55,11 @@
#include "layers/split_layer.hpp"
#include "layers/crop_layer.hpp"
#include "layers/eltwise_layer.hpp"
#include "layers/flatten_layer.hpp"
#include "layers/permute_layer.hpp"
#include "layers/prior_box_layer.hpp"
#include "layers/detection_output_layer.hpp"
#include "layers/normalize_bbox_layer.hpp"
namespace cv
{
......@@ -82,7 +87,7 @@ void initModule()
REG_RUNTIME_LAYER_CLASS(Softmax, SoftMaxLayer)
REG_RUNTIME_LAYER_CLASS(Split, SplitLayer)
REG_RUNTIME_LAYER_CLASS(Reshape, ReshapeLayer)
REG_STATIC_LAYER_FUNC(Flatten, createFlattenLayer)
// REG_STATIC_LAYER_FUNC(Flatten, createFlattenLayer)
REG_RUNTIME_LAYER_CLASS(Pooling, PoolingLayer)
REG_RUNTIME_LAYER_CLASS(MVN, MVNLayer)
REG_RUNTIME_LAYER_CLASS(LRN, LRNLayer)
......@@ -103,6 +108,12 @@ void initModule()
REG_RUNTIME_LAYER_CLASS(Crop, CropLayer)
REG_RUNTIME_LAYER_CLASS(Eltwise, EltwiseLayer)
REG_RUNTIME_LAYER_CLASS(Permute, PermuteLayer)
REG_RUNTIME_LAYER_CLASS(Flatten, FlattenLayer)
REG_RUNTIME_LAYER_CLASS(PriorBox, PriorBoxLayer)
REG_RUNTIME_LAYER_CLASS(DetectionOutput, DetectionOutputLayer)
REG_RUNTIME_LAYER_CLASS(NormalizeBBox, NormalizeBBoxLayer)
init.status = true;
}
......
......@@ -190,8 +190,9 @@ namespace dnn
inpW = inpBlob.cols();
inpCn = inpBlob.channels();
outH = (inpH + 2 * padH - kerH) / strideH + 1;
outW = (inpW + 2 * padW - kerW) / strideW + 1;
outH = (inpH + 2 * padH - (dilationH * (kerH - 1) + 1)) / strideH + 1;
outW = (inpW + 2 * padW - (dilationW * (kerW - 1) + 1)) / strideW + 1;
outCn = numOutput;
topH = outH; topW = outW; topCn = outCn;
......
......@@ -79,65 +79,78 @@ bool SortScorePairDescend(const std::pair<float, T>& pair1,
const std::string DetectionOutputLayer::_layerName = std::string("DetectionOutput");
DictValue DetectionOutputLayer::getParameterDict(const LayerParams &params,
const std::string &parameterName)
bool DetectionOutputLayer::getParameterDict(const LayerParams &params,
const std::string &parameterName,
DictValue& result)
{
if (!params.has(parameterName))
{
std::string message = _layerName;
message += " layer parameter does not contain ";
message += parameterName;
message += " index.";
CV_Error(Error::StsBadArg, message);
return false;
}
DictValue parameter = params.get(parameterName);
if(parameter.size() != 1)
{
std::string message = parameterName;
message += " field in ";
message += _layerName;
message += " layer parameter is required";
CV_Error(Error::StsBadArg, message);
}
return parameter;
result = params.get(parameterName);
return true;
}
template<typename T>
T DetectionOutputLayer::getParameter(const LayerParams &params,
const std::string &parameterName,
const size_t &idx)
const std::string &parameterName,
const size_t &idx,
const bool required,
const T& defaultValue)
{
DictValue dictValue;
bool success = getParameterDict(params, parameterName, dictValue);
if(!success)
{
if(required)
{
std::string message = _layerName;
message += " layer parameter does not contain ";
message += parameterName;
message += " parameter.";
CV_Error(Error::StsBadArg, message);
}
else
{
return defaultValue;
}
}
return dictValue.get<T>(idx);
}
void DetectionOutputLayer::getCodeType(LayerParams &params)
{
return getParameterDict(params, parameterName).get<T>(idx);
String codeTypeString = params.get<String>("code_type").toLowerCase();
if (codeTypeString == "corner")
_codeType = caffe::PriorBoxParameter_CodeType_CORNER;
else if (codeTypeString == "center_size")
_codeType = caffe::PriorBoxParameter_CodeType_CENTER_SIZE;
else
_codeType = caffe::PriorBoxParameter_CodeType_CORNER;
}
DetectionOutputLayer::DetectionOutputLayer(LayerParams &params) : Layer(params)
{
_numClasses = getParameter<int>(params, "num_classes");
_shareLocation = getParameter<int>(params, "share_location");
_numClasses = getParameter<unsigned>(params, "num_classes");
_shareLocation = getParameter<bool>(params, "share_location");
_numLocClasses = _shareLocation ? 1 : _numClasses;
_backgroundLabelId = getParameter<int>(params, "background_label_id");
_codeType = static_cast<CodeType>(getParameter<int>(params, "code_type"));
_varianceEncodedInTarget = getParameter<int>(params, "variance_encoded_in_target");
_varianceEncodedInTarget = getParameter<bool>(params, "variance_encoded_in_target", 0, false, false);
_keepTopK = getParameter<int>(params, "keep_top_k");
_confidenceThreshold = params.has("confidence_threshold") ?
getParameter<int>(params, "confidence_threshold") : -FLT_MAX;
_confidenceThreshold = getParameter<float>(params, "confidence_threshold", 0, false, -FLT_MAX);
_topK = getParameter<int>(params, "top_k", 0, false, -1);
getCodeType(params);
// Parameters used in nms.
_nmsThreshold = getParameter<float>(params, "nms_threshold");
CV_Assert(_nmsThreshold > 0.);
_topK = -1;
if (params.has("top_k"))
{
_topK = getParameter<int>(params, "top_k");
}
}
void DetectionOutputLayer::checkInputs(const std::vector<Blob*> &inputs)
{
for (size_t i = 0; i < inputs.size(); i++)
for (size_t i = 1; i < inputs.size(); i++)
{
for (size_t j = 0; j < _numAxes; j++)
{
......
......@@ -51,7 +51,7 @@ namespace dnn
{
class DetectionOutputLayer : public Layer
{
int _numClasses;
unsigned _numClasses;
bool _shareLocation;
int _numLocClasses;
......@@ -79,13 +79,18 @@ public:
void forward(std::vector<Blob*> &inputs, std::vector<Blob> &outputs);
void checkInputs(const std::vector<Blob*> &inputs);
void getCodeType(LayerParams &params);
template<typename T>
T getParameter(const LayerParams &params, const std::string &parameterName,
const size_t &idx = 0);
DictValue getParameterDict(const LayerParams &params,
const std::string &parameterName);
T getParameter(const LayerParams &params,
const std::string &parameterName,
const size_t &idx = 0,
const bool required = true,
const T& defaultValue = T());
bool getParameterDict(const LayerParams &params,
const std::string &parameterName,
DictValue& result);
typedef std::map<int, std::vector<caffe::NormalizedBBox> > LabelBBox;
......
......@@ -52,54 +52,56 @@ namespace dnn
const std::string FlattenLayer::_layerName = std::string("Flatten");
DictValue FlattenLayer::getParameterDict(const LayerParams &params,
const std::string &parameterName)
bool FlattenLayer::getParameterDict(const LayerParams &params,
const std::string &parameterName,
DictValue& result)
{
if (!params.has(parameterName))
{
std::string message = _layerName;
message += " layer parameter does not contain ";
message += parameterName;
message += " index.";
CV_Error(Error::StsBadArg, message);
return false;
}
DictValue parameter = params.get(parameterName);
if(parameter.size() != 1)
{
std::string message = parameterName;
message += " field in ";
message += _layerName;
message += " layer parameter is required";
CV_Error(Error::StsBadArg, message);
}
return parameter;
result = params.get(parameterName);
return true;
}
template<typename T>
T FlattenLayer::getParameter(const LayerParams &params,
const std::string &parameterName,
const size_t &idx)
const std::string &parameterName,
const size_t &idx,
const bool required,
const T& defaultValue)
{
return getParameterDict(params, parameterName).get<T>(idx);
DictValue dictValue;
bool success = getParameterDict(params, parameterName, dictValue);
if(!success)
{
if(required)
{
std::string message = _layerName;
message += " layer parameter does not contain ";
message += parameterName;
message += " parameter.";
CV_Error(Error::StsBadArg, message);
}
else
{
return defaultValue;
}
}
return dictValue.get<T>(idx);
}
FlattenLayer::FlattenLayer(LayerParams &params) : Layer(params)
{
_startAxis = getParameter<int>(params, "start_axis");
_endAxis = getParameter<int>(params, "end_axis");
if(_endAxis <= 0)
{
_endAxis += _numAxes;
}
_startAxis = getParameter<int>(params, "axis");
_endAxis = getParameter<int>(params, "end_axis", 0, false, -1);
}
void FlattenLayer::checkInputs(const std::vector<Blob*> &inputs)
{
CV_Assert(inputs.size() > 0);
for (size_t i = 0; i < inputs.size(); i++)
for (size_t i = 1; i < inputs.size(); i++)
{
for (size_t j = 0; j < _numAxes; j++)
{
......@@ -112,29 +114,31 @@ void FlattenLayer::allocate(const std::vector<Blob*> &inputs, std::vector<Blob>
{
checkInputs(inputs);
_numAxes = inputs[0]->shape().dims();
if(_endAxis <= 0)
{
_endAxis += _numAxes;
}
CV_Assert(_startAxis >= 0);
CV_Assert(_endAxis >= _startAxis && _endAxis < (int)_numAxes);
size_t flattenedDimensionSize = 1;
for (int i = _startAxis; i <= _endAxis; i++)
{
flattenedDimensionSize *= inputs[0]->shape()[i];
}
BlobShape outputShape;
size_t interval = _endAxis - _startAxis;
// imitate flexible number of axes: make first dimension sizes = 1
for (size_t i = 0; i < interval; i++)
{
outputShape[i] = 1;
}
for (int i = interval; i < _endAxis; i++)
std::vector<int> outputShape;
for (int i = 0; i < _startAxis; i++)
{
outputShape[i] = inputs[0]->shape()[i - interval];
outputShape.push_back(inputs[0]->shape()[i]);
}
outputShape[_endAxis] = flattenedDimensionSize;
outputShape.push_back(flattenedDimensionSize);
for (size_t i = _endAxis + 1; i < _numAxes; i++)
{
outputShape[i] = inputs[0]->shape()[i];
outputShape.push_back(inputs[0]->shape()[i]);
}
CV_Assert(outputShape.size() <= 4);
for (size_t i = 0; i < inputs.size(); i++)
{
......
......@@ -49,10 +49,10 @@ namespace dnn
{
class FlattenLayer : public Layer
{
size_t _startAxis;
int _startAxis;
int _endAxis;
static const size_t _numAxes = 4;
size_t _numAxes;
static const std::string _layerName;
public:
......@@ -63,11 +63,14 @@ public:
void checkInputs(const std::vector<Blob*> &inputs);
template<typename T>
T getParameter(const LayerParams &params, const std::string &parameterName,
const size_t &idx = 0);
T getParameter(const LayerParams &params,
const std::string &parameterName,
const size_t &idx = 0,
const bool required = true,
const T& defaultValue = T());
DictValue getParameterDict(const LayerParams &params,
const std::string &parameterName);
bool getParameterDict(const LayerParams &params,
const std::string &parameterName, DictValue &result);
};
}
}
......
......@@ -72,20 +72,21 @@ bool getParameter(LayerParams &params, const std::string& nameBase, const std::s
else
{
if (params.has(nameAll_))
{
parameterH = parameterW = params.get<int>(nameAll_);
return true;
}
else
{
if(hasDefault)
{
parameterH = parameterW = params.get<int>(nameAll_, defaultValue);
parameterH = parameterW = defaultValue;
return true;
}
else
{
parameterH = parameterW = params.get<int>(nameAll_);
return false;
}
return true;
}
else
{
return false;
}
}
}
......
......@@ -54,43 +54,49 @@ namespace dnn
const std::string NormalizeBBoxLayer::_layerName = std::string("NormalizeBBox");
DictValue NormalizeBBoxLayer::getParameterDict(const LayerParams &params,
const std::string &parameterName)
bool NormalizeBBoxLayer::getParameterDict(const LayerParams &params,
const std::string &parameterName,
DictValue& result)
{
if (!params.has(parameterName))
{
std::string message = _layerName;
message += " layer parameter does not contain ";
message += parameterName;
message += " index.";
CV_Error(Error::StsBadArg, message);
return false;
}
DictValue parameter = params.get(parameterName);
if(parameter.size() != 1)
{
std::string message = parameterName;
message += " field in ";
message += _layerName;
message += " layer parameter is required";
CV_Error(Error::StsBadArg, message);
}
return parameter;
result = params.get(parameterName);
return true;
}
template<typename T>
T NormalizeBBoxLayer::getParameter(const LayerParams &params,
const std::string &parameterName,
const size_t &idx)
const size_t &idx,
const bool required,
const T& defaultValue)
{
return getParameterDict(params, parameterName).get<T>(idx);
DictValue dictValue;
bool success = getParameterDict(params, parameterName, dictValue);
if(!success)
{
if(required)
{
std::string message = _layerName;
message += " layer parameter does not contain ";
message += parameterName;
message += " parameter.";
CV_Error(Error::StsBadArg, message);
}
else
{
return defaultValue;
}
}
return dictValue.get<T>(idx);
}
NormalizeBBoxLayer::NormalizeBBoxLayer(LayerParams &params) : Layer(params)
{
_eps = getParameter<float>(params, "eps");
_eps = getParameter<float>(params, "eps", 0, false, 1e-10);
_across_spatial = getParameter<bool>(params, "across_spatial");
_channel_shared = getParameter<bool>(params, "channel_shared");
}
......@@ -98,7 +104,7 @@ NormalizeBBoxLayer::NormalizeBBoxLayer(LayerParams &params) : Layer(params)
void NormalizeBBoxLayer::checkInputs(const std::vector<Blob*> &inputs)
{
CV_Assert(inputs.size() > 0);
for (size_t i = 0; i < inputs.size(); i++)
for (size_t i = 1; i < inputs.size(); i++)
{
for (size_t j = 0; j < _numAxes; j++)
{
......
......@@ -82,11 +82,15 @@ public:
void checkInputs(const std::vector<Blob*> &inputs);
template<typename T>
T getParameter(const LayerParams &params, const std::string &parameterName,
const size_t &idx = 0);
T getParameter(const LayerParams &params,
const std::string &parameterName,
const size_t &idx = 0,
const bool required = true,
const T& defaultValue = T());
DictValue getParameterDict(const LayerParams &params,
const std::string &parameterName);
bool getParameterDict(const LayerParams &params,
const std::string &parameterName,
DictValue& result);
};
}
}
......
......@@ -53,42 +53,52 @@ namespace dnn
const std::string PriorBoxLayer::_layerName = std::string("PriorBox");
DictValue PriorBoxLayer::getParameterDict(const LayerParams &params,
const std::string &parameterName)
bool PriorBoxLayer::getParameterDict(const LayerParams &params,
const std::string &parameterName,
DictValue& result)
{
if (!params.has(parameterName))
{
std::string message = _layerName;
message += " layer parameter does not contain ";
message += parameterName;
message += " index.";
CV_Error(Error::StsBadArg, message);
return false;
}
DictValue parameter = params.get(parameterName);
if(parameter.size() != 1)
{
std::string message = parameterName;
message += " field in ";
message += _layerName;
message += " layer parameter is required";
CV_Error(Error::StsBadArg, message);
}
return parameter;
result = params.get(parameterName);
return true;
}
template<typename T>
T PriorBoxLayer::getParameter(const LayerParams &params,
const std::string &parameterName,
const size_t &idx)
const size_t &idx,
const bool required,
const T& defaultValue)
{
return getParameterDict(params, parameterName).get<T>(idx);
DictValue dictValue;
bool success = getParameterDict(params, parameterName, dictValue);
if(!success)
{
if(required)
{
std::string message = _layerName;
message += " layer parameter does not contain ";
message += parameterName;
message += " parameter.";
CV_Error(Error::StsBadArg, message);
}
else
{
return defaultValue;
}
}
return dictValue.get<T>(idx);
}
void PriorBoxLayer::getAspectRatios(const LayerParams &params)
{
DictValue aspectRatioParameter = getParameterDict(params, "aspect_ratio");
DictValue aspectRatioParameter;
bool aspectRatioRetieved = getParameterDict(params, "aspect_ratio", aspectRatioParameter);
CV_Assert(aspectRatioRetieved);
for (int i = 0; i < aspectRatioParameter.size(); ++i)
{
float aspectRatio = aspectRatioParameter.get<float>(i);
......@@ -115,7 +125,10 @@ void PriorBoxLayer::getAspectRatios(const LayerParams &params)
void PriorBoxLayer::getVariance(const LayerParams &params)
{
DictValue varianceParameter = getParameterDict(params, "variance");
DictValue varianceParameter;
bool varianceParameterRetrieved = getParameterDict(params, "variance", varianceParameter);
CV_Assert(varianceParameterRetrieved);
int varianceSize = varianceParameter.size();
if (varianceSize > 1)
{
......@@ -190,12 +203,9 @@ void PriorBoxLayer::allocate(const std::vector<Blob*> &inputs, std::vector<Blob>
// 2 channels. First channel stores the mean of each prior coordinate.
// Second channel stores the variance of each prior coordinate.
size_t outChannels = 2;
size_t outHeight = _layerHeight;
size_t outWidth = _layerWidth * _numPriors * 4;
_outChannelSize = _layerHeight * _layerWidth * _numPriors * 4;
outputs[0].create(BlobShape(outNum, outChannels, outHeight, outWidth));
outputs[0].create(BlobShape(outNum, outChannels, _outChannelSize));
}
void PriorBoxLayer::forward(std::vector<Blob*> &inputs, std::vector<Blob> &outputs)
......
......@@ -83,11 +83,15 @@ public:
void forward(std::vector<Blob*> &inputs, std::vector<Blob> &outputs);
template<typename T>
T getParameter(const LayerParams &params, const std::string &parameterName,
const size_t &idx = 0);
T getParameter(const LayerParams &params,
const std::string &parameterName,
const size_t &idx = 0,
const bool required = true,
const T& defaultValue = T());
DictValue getParameterDict(const LayerParams &params,
const std::string &parameterName);
bool getParameterDict(const LayerParams &params,
const std::string &parameterName,
DictValue& result);
void getAspectRatios(const LayerParams &params);
void getVariance(const LayerParams &params);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment