Skip to content

Commit 2f73e93

Browse files
committed
refactor(dto): example service_create+ncnn
The Service Create call works, but most parameters doesn't. Supported parameters are only those declared in the DTO files. To not update all input/output connector code. This PR just convert the DTO into APIData to pass it to connector. /!\ But beware if a parameters is not present in the DTO it will be ignored and will not be present in the generated APIData passed to the connectors. /!\ That's why we need first to fill the InputConnector and OutputConnector with all possible option from all type and all backends. cmake .. -D USE_HTTP_SERVER_OATPP=ON -D USE_HTTP_SERVER=OFF -D USE_NCNN=ON -D USE_CAFFE=OFF -DUSE_COMMAND_LINE=OFF
1 parent 5c0f343 commit 2f73e93

20 files changed

+482
-149
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ if (NOT EXISTS ${CMAKE_BINARY_DIR}/src)
8787
COMMAND bash -c "mkdir ${CMAKE_BINARY_DIR}/src")
8888
endif()
8989

90-
set(CMAKE_CXX_FLAGS "-g -O2 -Wall -Wextra -fopenmp -fPIC -std=c++14 -DUSE_OPENCV -DUSE_LMDB")
90+
set(CMAKE_CXX_FLAGS "-g -O2 -Wall -Wextra -fopenmp -fPIC -std=c++14 -DUSE_OPENCV -DUSE_LMDB -fmax-errors=4")
9191

9292
if(WARNING)
9393
string(APPEND CMAKE_CXX_FLAGS " -Werror")

src/apidata.h

+14
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,20 @@ namespace dd
298298
.getPtr();
299299
}
300300

301+
template <typename T>
302+
inline static APIData fromDTO(const oatpp::Object<T> &dto)
303+
{
304+
std::shared_ptr<oatpp::data::mapping::ObjectMapper> object_mapper
305+
= oatpp::parser::json::mapping::ObjectMapper::createShared();
306+
307+
oatpp::String json = object_mapper->writeToString(dto);
308+
APIData ad;
309+
rapidjson::Document d;
310+
d.Parse<rapidjson::kParseNanAndInfFlag>(json->c_str());
311+
ad.fromRapidJson(d);
312+
return ad;
313+
}
314+
301315
public:
302316
/**
303317
* \brief render Mustache template based on this APIData object

src/backends/ncnn/ncnnlib.cc

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include "net.h"
3131
#include <iostream>
3232

33+
#include "dto/mllib.hpp"
34+
3335
namespace dd
3436
{
3537
template <class TInputConnectorStrategy, class TOutputConnectorStrategy,
@@ -88,13 +90,11 @@ namespace dd
8890
template <class TInputConnectorStrategy, class TOutputConnectorStrategy,
8991
class TMLModel>
9092
void NCNNLib<TInputConnectorStrategy, TOutputConnectorStrategy,
91-
TMLModel>::init_mllib(const APIData &ad)
93+
TMLModel>::init_mllib(const oatpp::Object<DTO::MLLib> &init_dto)
9294
{
93-
_init_dto = ad.createSharedDTO<NcnnInitDto>();
95+
_init_dto = init_dto;
9496

95-
bool use_fp32 = (ad.has("datatype")
96-
&& ad.get("datatype").get<std::string>()
97-
== "fp32"); // default is fp16
97+
bool use_fp32 = (_init_dto->datatype == "fp32");
9898
_net->opt.use_fp16_packed = !use_fp32;
9999
_net->opt.use_fp16_storage = !use_fp32;
100100
_net->opt.use_fp16_arithmetic = !use_fp32;

src/backends/ncnn/ncnnlib.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "apidata.h"
2626
#include "utils/utils.hpp"
2727

28-
#include "dto/ncnn.hpp"
28+
#include "dto/mllib.hpp"
2929

3030
// NCNN
3131
#include "net.h"
@@ -44,7 +44,7 @@ namespace dd
4444
~NCNNLib();
4545

4646
/*- from mllib -*/
47-
void init_mllib(const APIData &ad);
47+
void init_mllib(const oatpp::Object<DTO::MLLib> &init_dto);
4848

4949
void clear_mllib(const APIData &ad);
5050

@@ -59,7 +59,7 @@ namespace dd
5959
bool _timeserie = false;
6060

6161
private:
62-
std::shared_ptr<NcnnInitDto> _init_dto;
62+
oatpp::Object<DTO::MLLib> _init_dto;
6363
static ncnn::UnlockedPoolAllocator _blob_pool_allocator;
6464
static ncnn::PoolAllocator _workspace_pool_allocator;
6565

src/backends/ncnn/ncnnmodel.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "dd_spdlog.h"
2626
#include "mlmodel.h"
2727
#include "apidata.h"
28+
#include "dto/model.hpp"
29+
#include "dto/service_create.hpp"
2830

2931
namespace dd
3032
{
@@ -34,12 +36,13 @@ namespace dd
3436
NCNNModel() : MLModel()
3537
{
3638
}
37-
NCNNModel(const APIData &ad, APIData &adg,
39+
NCNNModel(const oatpp::Object<DTO::Model> &model_dto,
40+
const oatpp::Object<DTO::ServiceCreate> &service_dto,
3841
const std::shared_ptr<spdlog::logger> &logger)
39-
: MLModel(ad, adg, logger)
42+
: MLModel(model_dto, service_dto, logger)
4043
{
41-
if (ad.has("repository"))
42-
this->_repo = ad.get("repository").get<std::string>();
44+
if (model_dto->repository)
45+
this->_repo = model_dto->repository->std_str();
4346
read_from_repository(spdlog::get("api"));
4447
read_corresp_file();
4548
}

src/dto/img_connector.hpp src/dto/input_connector.hpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>.
2020
*/
2121

22-
#ifndef HTTP_DTO_IMG_CONNECTOR_HPP
23-
#define HTTP_DTO_IMG_CONNECTOR_HPP
22+
#ifndef DTO_INPUT_CONNECTOR_HPP
23+
#define DTO_INPUT_CONNECTOR_HPP
2424

2525
#include "dd_config.h"
2626
#include "oatpp/core/Types.hpp"
@@ -32,10 +32,13 @@ namespace dd
3232
{
3333
#include OATPP_CODEGEN_BEGIN(DTO)
3434

35-
class ImgInputConnectorParameters : public oatpp::DTO
35+
class InputConnector : public oatpp::DTO
3636
{
37-
DTO_INIT(ImgInputConnectorParameters, DTO /* extends */)
37+
DTO_INIT(InputConnector, DTO /* extends */)
38+
// Connector type
39+
DTO_FIELD(String, connector);
3840

41+
// IMG Input Connector
3942
DTO_FIELD(Int32, width);
4043
DTO_FIELD(Int32, height);
4144
DTO_FIELD(Int32, crop_width);

src/dto/mllib.hpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* DeepDetect
3+
* Copyright (c) 2021 Jolibrain SASU
4+
* Author: Mehdi Abaakouk <[email protected]>
5+
*
6+
* This file is part of deepdetect.
7+
*
8+
* deepdetect is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* deepdetect is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
#ifndef DTO_MLLIB_H
23+
#define DTO_MLLIB_H
24+
25+
#include "dd_config.h"
26+
#include "utils/utils.hpp"
27+
#include "oatpp/core/Types.hpp"
28+
#include "oatpp/core/macro/codegen.hpp"
29+
30+
namespace dd
31+
{
32+
namespace DTO
33+
{
34+
#include OATPP_CODEGEN_BEGIN(DTO) ///< Begin DTO codegen section
35+
36+
class MLLib : public oatpp::DTO
37+
{
38+
DTO_INIT(MLLib, DTO /* extends */)
39+
40+
// NCNN Options
41+
DTO_FIELD_INFO(nclasses)
42+
{
43+
info->description = "number of output classes (`supervised` service "
44+
"type), classification only";
45+
};
46+
DTO_FIELD(Int32, nclasses) = 0;
47+
48+
DTO_FIELD_INFO(threads)
49+
{
50+
info->description = "number of threads";
51+
};
52+
DTO_FIELD(Int32, threads) = dd::dd_utils::my_hardware_concurrency();
53+
54+
DTO_FIELD_INFO(lightmode)
55+
{
56+
info->description = "enable light mode";
57+
};
58+
DTO_FIELD(Boolean, lightmode) = true;
59+
60+
DTO_FIELD_INFO(inputBlob)
61+
{
62+
info->description = "network input blob name";
63+
};
64+
DTO_FIELD(String, inputBlob) = "data";
65+
66+
DTO_FIELD_INFO(outputBlob)
67+
{
68+
info->description = "network output blob name (default depends on "
69+
"network type(ie prob or "
70+
"rnn_pred or probs or detection_out)";
71+
};
72+
DTO_FIELD(String, outputBlob);
73+
74+
DTO_FIELD_INFO(datatype)
75+
{
76+
info->description = "fp16 or fp32";
77+
};
78+
79+
DTO_FIELD(String, datatype) = "fp16";
80+
};
81+
#include OATPP_CODEGEN_END(DTO) ///< End DTO codegen section
82+
83+
}
84+
}
85+
#endif

src/dto/model.hpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* DeepDetect
3+
* Copyright (c) 2021 Jolibrain SASU
4+
* Author: Mehdi Abaakouk <[email protected]>
5+
*
6+
* This file is part of deepdetect.
7+
*
8+
* deepdetect is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* deepdetect is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
#ifndef DTO_MODEL
23+
#define DTO_MODEL
24+
25+
#include "oatpp/core/Types.hpp"
26+
#include "oatpp/core/macro/codegen.hpp"
27+
28+
namespace dd
29+
{
30+
namespace DTO
31+
{
32+
#include OATPP_CODEGEN_BEGIN(DTO) ///< Begin DTO codegen section
33+
34+
35+
class Model: public oatpp::DTO
36+
{
37+
DTO_INIT(Model, DTO /* extends */)
38+
DTO_FIELD(String, repository);
39+
DTO_FIELD(String, init);
40+
DTO_FIELD(Boolean, create_repository) = false;
41+
DTO_FIELD(Boolean, index_preload) = false;
42+
};
43+
44+
#include OATPP_CODEGEN_END(DTO) ///< End DTO codegen section
45+
46+
}
47+
}
48+
49+
#endif

src/dto/output_connector.hpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* DeepDetect
3+
* Copyright (c) 2021 Jolibrain SASU
4+
* Author: Mehdi Abaakouk <[email protected]>
5+
*
6+
* This file is part of deepdetect.
7+
*
8+
* deepdetect is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* deepdetect is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
#ifndef DTO_OUTPUT_CONNECTOR_H
23+
#define DTO_OUTPUT_CONNECTOR_H
24+
25+
#include "oatpp/core/Types.hpp"
26+
#include "oatpp/core/macro/codegen.hpp"
27+
28+
namespace dd
29+
{
30+
namespace DTO
31+
{
32+
#include OATPP_CODEGEN_BEGIN(DTO) ///< Begin DTO codegen section
33+
34+
class OutputConnector: public oatpp::DTO
35+
{
36+
DTO_INIT(OutputConnector, DTO /* extends */)
37+
DTO_FIELD(Boolean, store_config);
38+
};
39+
40+
#include OATPP_CODEGEN_END(DTO) ///< End DTO codegen section
41+
}
42+
}
43+
44+
#endif

src/dto/parameters.hpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* DeepDetect
3+
* Copyright (c) 2021 Jolibrain SASU
4+
* Author: Mehdi Abaakouk <[email protected]>
5+
*
6+
* This file is part of deepdetect.
7+
*
8+
* deepdetect is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* deepdetect is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with deepdetect. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
#ifndef DTO_PARAMETERS_H
23+
#define DTO_PARAMETERS_H
24+
25+
#include "oatpp/core/Types.hpp"
26+
#include "oatpp/core/macro/codegen.hpp"
27+
#include "dto/mllib.hpp"
28+
#include "dto/input_connector.hpp"
29+
#include "dto/output_connector.hpp"
30+
31+
namespace dd
32+
{
33+
namespace DTO
34+
{
35+
#include OATPP_CODEGEN_BEGIN(DTO) ///< Begin DTO codegen section
36+
37+
class Parameters: public oatpp::DTO
38+
{
39+
DTO_INIT(Parameters, DTO /* extends */)
40+
DTO_FIELD(Object<InputConnector>, input) = InputConnector::createShared();
41+
DTO_FIELD(Object<MLLib>, mllib) = MLLib::createShared();
42+
DTO_FIELD(Object<OutputConnector>, output) = OutputConnector::createShared();
43+
};
44+
45+
#include OATPP_CODEGEN_END(DTO) ///< End DTO codegen section
46+
}
47+
}
48+
49+
#endif

0 commit comments

Comments
 (0)