50 int sz[4] = {1, 1, 1, 1};
51 std::copy(blob.size.p, blob.size.p + blob.dims, sz);
52 blob = blob.reshape(0, 4, sz);
102 "MxArray unable to convert to vector<cv::dnn::Net::LayerId>");
120 CV_Assert(dict.isStruct() && dict.numel()==1);
121 for (
int i = 0; i < dict.nfields(); ++i) {
122 string key(dict.fieldname(i));
123 const MxArray val(dict.at(key));
125 if (val.numel() == 1)
126 params.
set(key, val.toString());
129 params.set(key, DictValue::arrayString(
v.begin(),
v.size()));
132 else if (val.isFloat()) {
133 if (val.numel() == 1)
134 params.set(key, val.toDouble());
137 params.set(key, DictValue::arrayReal(
v.begin(),
v.size()));
141 if (val.numel() == 1)
142 params.set(key, val.toInt());
145 params.set(key, DictValue::arrayInt(
v.begin(),
v.size()));
152 params.blobs.reserve(blobs.
size());
156 if (arr.
isField(
"name")) params.name = arr.
at(
"name").toString();
157 if (arr.
isField(
"type")) params.type = arr.
at(
"type").toString();
167 const char *fields[] = {
"blobs",
"name",
"type"};
182 const char *fields[] = {
"blobs",
"name",
"type"};
184 for (
mwIndex i = 0; i < layers.size(); ++i) {
185 s.
set(
"blobs", layers[i]->blobs, i);
186 s.
set(
"name", layers[i]->name, i);
187 s.
set(
"type", layers[i]->
type, i);
222 if (
type ==
"Caffe") {
224 string prototxt(first->toString()); ++first;
225 string caffeModel(len==2 ? first->toString() :
string());
228 else if (
type ==
"Tensorflow") {
230 string model(first->toString()); ++first;
231 string config(len==2 ? first->toString() :
string());
234 else if (
type ==
"Torch") {
236 string filename(first->toString()); ++first;
237 bool isBinary = (len==2 ? first->toBool() :
true);
240 else if (
type ==
"Darknet") {
242 string cfgFile(first->toString()); ++first;
243 string darknetModel(len==2 ? first->toString() :
string());
248 "Unrecognized network type %s",
type.c_str());
249 return makePtr<Net>(net);
267 int id = rhs[0].toInt();
268 string method(rhs[1].toString());
271 if (method ==
"new") {
281 else if (method ==
"readTorchBlob") {
282 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
283 bool isBinary =
true;
284 for (
int i=3; i<nrhs; i+=2) {
285 string key(rhs[i].toString());
286 if (key ==
"IsBinary")
287 isBinary = rhs[i+1].toBool();
290 "Unrecognized option %s", key.
c_str());
292 string filename(rhs[2].toString());
297 else if (method ==
"blobFromImages") {
298 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
299 double scalefactor = 1.0;
304 for (
int i=3; i<nrhs; i+=2) {
305 string key(rhs[i].toString());
306 if (key ==
"ScaleFactor")
307 scalefactor = rhs[i+1].toDouble();
308 else if (key ==
"Size")
309 size = rhs[i+1].toSize();
310 else if (key ==
"Mean")
311 mean = rhs[i+1].toScalar();
312 else if (key ==
"SwapRB")
313 swapRB = rhs[i+1].toBool();
314 else if (key ==
"Crop")
315 crop = rhs[i+1].toBool();
318 "Unrecognized option %s", key.
c_str());
321 if (rhs[2].isCell()) {
339 else if (method ==
"imagesFromBlob") {
347 else if (method ==
"shrinkCaffeModel") {
348 nargchk(nrhs>=4 && (nrhs%2)==0 && nlhs==0);
350 for (
int i=4; i<nrhs; i+=2) {
351 string key(rhs[i].toString());
352 if (key ==
"LayersTypes")
353 layersTypes = rhs[i+1].toVector<
string>();
356 "Unrecognized option %s", key.
c_str());
358 string src(rhs[2].toString()),
359 dst(rhs[3].toString());
364 else if (method ==
"NMSBoxes") {
365 nargchk(nrhs>=6 && (nrhs%2)==0 && nlhs<=1);
368 for (
int i=6; i<nrhs; i+=2) {
369 string key(rhs[i].toString());
371 eta = rhs[i+1].toFloat();
372 else if (key ==
"TopK")
373 top_k = rhs[i+1].toInt();
376 "Unrecognized option %s", key.
c_str());
380 float score_threshold = rhs[4].toFloat();
381 float nms_threshold = rhs[5].toFloat();
383 NMSBoxes(bboxes, scores, score_threshold, nms_threshold, indices,
393 if (method ==
"delete") {
398 else if (method ==
"empty") {
402 else if (method ==
"addLayer") {
404 string name(rhs[2].toString()),
405 type(rhs[3].toString());
410 else if (method ==
"addLayerToPrev") {
412 string name(rhs[2].toString()),
413 type(rhs[3].toString());
418 else if (method ==
"getLayerId") {
420 string layer(rhs[2].toString());
424 else if (method ==
"getLayerNames") {
428 else if (method ==
"getLayer") {
433 else if (method ==
"getLayerInputs") {
438 else if (method ==
"deleteLayer") {
442 else if (method ==
"connect") {
443 nargchk((nrhs==4 || nrhs==6) && nlhs==0);
445 string outPin(rhs[2].toString()),
446 inpPin(rhs[3].toString());
450 int outLayerId(rhs[2].toInt()),
451 outNum(rhs[3].toInt()),
452 inpLayerId(rhs[4].toInt()),
453 inpNum(rhs[5].toInt());
454 obj->
connect(outLayerId, outNum, inpLayerId, inpNum);
457 else if (method ==
"setInputsNames") {
463 else if (method ==
"forward") {
464 nargchk((nrhs==2 || nrhs==3) && nlhs<=1);
465 if (nrhs == 2 || rhs[2].isChar()) {
468 outputName = rhs[2].toString();
469 MatND outputBlob = obj->
forward(outputName);
477 plhs[0] =
MxArray(outputBlobs);
480 else if (method ==
"forwardAndRetrieve") {
481 nargchk((nrhs==2 || nrhs==3) && nlhs<=1);
482 if (nrhs == 2 || rhs[2].isChar()) {
485 outputName = rhs[2].toString();
487 obj->
forward(outputBlobs, outputName);
488 plhs[0] =
MxArray(outputBlobs);
495 plhs[0] =
MxArray(outputBlobs);
498 else if (method ==
"setHalideScheduler") {
502 else if (method ==
"setPreferableBackend") {
506 else if (method ==
"setPreferableTarget") {
510 else if (method ==
"setInput") {
511 nargchk((nrhs==3 || nrhs==4) && nlhs==0);
514 obj->
setInput(blob, rhs[3].toString());
518 else if (method ==
"setParam") {
521 int numParam = rhs[3].toInt();
523 obj->
setParam(layer, numParam, blob);
525 else if (method ==
"getParam") {
526 nargchk((nrhs==3 || nrhs==4) && nlhs<=1);
528 int numParam = (nrhs > 3) ? rhs[3].toInt() : 0;
530 MatND blob = obj->
getParam(layer, numParam);
533 else if (method ==
"appendParam") {
538 if (!layer.
empty()) {
540 layer->
blobs.push_back(blob);
543 else if (method ==
"getUnconnectedOutLayers") {
547 else if (method ==
"getLayerTypes") {
551 plhs[0] =
MxArray(layersTypes);
553 else if (method ==
"getLayersCount") {
555 string layerType(rhs[2].toString());
559 else if (method ==
"enableFusion") {
563 else if (method ==
"getPerfProfile") {
578 "Unrecognized operation %s",method.
c_str());
Mat readTorchBlob(const String &filename, bool isBinary=true)
Ptr< Net > readNetFrom(const string &type, vector< MxArray >::const_iterator first, vector< MxArray >::const_iterator last)
Create an instance of Net using options in arguments.
int toInt() const
Convert MxArray to int.
const ConstMap< int, string > TargetsInvMap
Computation target devices for option processing.
MxArray toMxArray(int64_t i)
MxArray constructor from 64-bit integer.
int addLayer(const String &name, const String &type, LayerParams ¶ms)
Scalar mean(InputArray src, InputArray mask=noArray())
mwSize numel() const
Number of elements in an array.
T at(mwIndex index) const
Template for numeric array element accessor.
void getLayerTypes(std::vector< String > &layersTypes) const
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
Net::LayerId MxArrayToLayerId(const MxArray &arr)
Convert MxArray to cv::dnn::Net::LayerId.
std::string toString() const
Convert MxArray to std::string.
void NMSBoxes(const std::vector< Rect > &bboxes, const std::vector< float > &scores, const float score_threshold, const float nms_threshold, std::vector< int > &indices, const float eta=1.f, const int top_k=0)
void setPreferableBackend(int backendId)
void connect(String outPin, String inpPin)
bool isChar() const
Determine whether input is string array.
Mat blobFromImages(InputArrayOfArrays images, double scalefactor=1.0, Size size=Size(), const Scalar &mean=Scalar(), bool swapRB=true, bool crop=true)
void setPreferableTarget(int targetId)
LayerParams MxArrayToLayerParams(const MxArray &arr)
Convert MxArray to cv::dnn::LayerParams.
MxArray toStruct(const vector< Ptr< Layer > > &layers)
Convert std::vector<cv::Ptr<cv::dnn::Layer>> to struct array.
const ConstMap< string, int > BackendsMap
Computation backends for option processing.
struct mxArray_tag mxArray
Forward declaration for mxArray.
void set(mwIndex index, const T &value)
Template for numeric array element write accessor.
void setInputsNames(const std::vector< String > &inputBlobNames)
Mat getParam(LayerId layer, int numParam=0)
Net readNetFromCaffe(const String &prototxt, const String &caffeModel=String())
bool isFloat() const
Determine whether array represents data as floating-point numbers, both single and double precision...
Net readNetFromTorch(const String &model, bool isBinary=true)
void setInput(InputArray blob, const String &name="")
int getLayersCount(const String &layerType) const
std::vector< Ptr< Layer > > getLayerInputs(LayerId layerId)
void setParam(LayerId layer, int numParam, const Mat &blob)
Mat blobFromImage(InputArray image, double scalefactor=1.0, const Size &size=Size(), const Scalar &mean=Scalar(), bool swapRB=true, bool crop=true)
int64 getPerfProfile(std::vector< double > &timings)
Net readNetFromTensorflow(const String &model, const String &config=String())
bool isNumeric() const
Determine whether array is numeric.
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
void enableFusion(bool fusion)
vector< Net::LayerId > MxArrayToVectorLayerId(const MxArray &arr)
Convert MxArray to std::vector<cv::dnn::Net::LayerId>
#define mxCreateNumericMatrix
bool isCell() const
Determine whether input is cell array.
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
int last_id
Last object id to allocate.
bool isField(const std::string &fieldName) const
Determine whether a struct array has a specified field.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/output arguments number check.
std::vector< String > getLayerNames() const
static MxArray Struct(const char **fields=NULL, int nfields=0, mwSize m=1, mwSize n=1)
Create a new struct array.
bool isStruct() const
Determine whether input is structure array.
void shrinkCaffeModel(const String &src, const String &dst, const std::vector< String > &layersTypes=std::vector< String >())
std::vector< T > toVector() const
Convert MxArray to std::vector<T> of primitive types.
cv::MatND toMatND(int depth=CV_USRTYPE1, bool transpose=true) const
Convert MxArray to a single-channel cv::Mat.
Global constant definitions.
void setHalideScheduler(const String &scheduler)
void deleteLayer(LayerId layer)
const ConstMap< string, int > TargetsMap
Computation target devices for option processing.
void imagesFromBlob(const cv::Mat &blob_, OutputArrayOfArrays images_)
Ptr< Layer > getLayer(LayerId layerId)
int addLayerToPrev(const String &name, const String &type, LayerParams ¶ms)
Mat forward(const String &outputName=String())
double toDouble() const
Convert MxArray to double.
int getLayerId(const String &layer)
DNN_BACKEND_INFERENCE_ENGINE
Net readNetFromDarknet(const String &cfgFile, const String &darknetModel=String())
bool isNull() const
Determine whether the array is initialized or not.
Identifies a numeric mxArray whose data is stored as the type specified in the MATLAB Primitive Types...
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
std::map wrapper with one-line initialization and lookup method.
map< int, Ptr< Net > > obj_
Object container.
MatND MxArrayToBlob(const MxArray &arr)
Create 4-dimensional blob from MATLAB array.
std::vector< int > getUnconnectedOutLayers() const
Identifies an mxArray with no imaginary components.