Skip to content

Commit

Permalink
feat: add predict from video
Browse files Browse the repository at this point in the history
  • Loading branch information
Bycob authored and mergify[bot] committed Jan 26, 2022
1 parent 07644b4 commit 02872eb
Show file tree
Hide file tree
Showing 34 changed files with 1,395 additions and 217 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -573,9 +573,9 @@ if (USE_TF)
endif() # USE_TF

# OpenCV
set(OPENCV_MODULES core imgproc highgui imgcodecs)
set(OPENCV_MODULES core imgproc highgui imgcodecs videoio)
if (USE_CUDA_CV)
list(APPEND OPENCV_MODULES cudaimgproc cudaarithm cudawarping)
list(APPEND OPENCV_MODULES cudaimgproc cudaarithm cudawarping cudacodec)
endif()

if (USE_OPENCV_VERSION STREQUAL "")
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ set(ddetect_SOURCES deepdetect.h deepdetect.cc mllibstrategy.h mlmodel.h
csvinputfileconn.cc csvtsinputfileconn.h csvtsinputfileconn.cc
svminputfileconn.h svminputfileconn.cc txtinputfileconn.h
txtinputfileconn.cc apidata.h apidata.cc chain_actions.h chain_actions.cc
service_stats.h service_stats.cc chain.h chain.cc ext/rmustache/mustache.h ext/rmustache/mustache.cc
service_stats.h service_stats.cc chain.h chain.cc resources.cc ext/rmustache/mustache.h ext/rmustache/mustache.cc
utils/oatpp.cc dto/ddtypes.cc utils/db.cpp utils/db_lmdb.cpp ${CMAKE_BINARY_DIR}/src/caffe.pb.cc)

if (USE_JSON_API)
Expand Down
36 changes: 36 additions & 0 deletions src/allconnectors.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#ifdef USE_CAFFE
#include "caffeinputconns.h"
#endif

#ifdef USE_TF
#include "backends/tf/tfinputconns.h"
#endif

#ifdef USE_DLIB
#include "backends/dlib/dlibinputconns.h"
#endif

#ifdef USE_NCNN
#include "backends/ncnn/ncnninputconns.h"
#endif

#ifdef USE_CAFFE2
#include "backends/caffe2/caffe2inputconns.h"
#endif

#ifdef USE_TENSORRT
#include "backends/tensorrt/tensorrtinputconns.h"
#endif

#ifdef USE_TORCH
#include "backends/torch/torchinputconns.h"
#endif

#ifdef USE_XGBOOST
#include "backends/xgb/xgbinputconns.h"
#endif

#ifdef USE_TSNE
#include "backends/tsne/tsneinputconns.h"
#endif
2 changes: 1 addition & 1 deletion src/backends/caffe/caffelib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

#include "caffelib.h"
#include "imginputfileconn.h"
#include "allconnectors.hpp"
#include "outputconnectorstrategy.h"
#include "generators/net_caffe.h"
#include "generators/net_caffe_convnet.h"
Expand Down
10 changes: 10 additions & 0 deletions src/backends/torch/native/native_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ namespace dd
return nullptr;
}

template <>
NativeModule *NativeFactory::from_template<VideoTorchInputFileConn>(
const std::string tdef, const APIData &template_params,
const VideoTorchInputFileConn &inputc,
const std::shared_ptr<spdlog::logger> &logger)
{
return from_template<ImgTorchInputFileConn>(tdef, template_params, inputc,
logger);
}

template NativeModule *
NativeFactory::from_template(const std::string tdef,
const APIData &template_params,
Expand Down
10 changes: 5 additions & 5 deletions src/backends/torch/torchdataset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ namespace dd
if (!_lfiles.empty()) // prefetch batch from file list
{
ImgTorchInputFileConn *inputc
= reinterpret_cast<ImgTorchInputFileConn *>(_inputc);
= dynamic_cast<ImgTorchInputFileConn *>(_inputc);
bool first_iter = true;

for (int64_t id : ids)
Expand Down Expand Up @@ -526,7 +526,7 @@ namespace dd
else
{
ImgTorchInputFileConn *inputc
= reinterpret_cast<ImgTorchInputFileConn *>(_inputc);
= dynamic_cast<ImgTorchInputFileConn *>(_inputc);

cv::Mat bgr, bw_target;
read_image_from_db(datas, targets, bgr, t, bw_target,
Expand Down Expand Up @@ -652,7 +652,7 @@ namespace dd
const bool &target)
{
ImgTorchInputFileConn *inputc
= reinterpret_cast<ImgTorchInputFileConn *>(_inputc);
= dynamic_cast<ImgTorchInputFileConn *>(_inputc);

DDImg dimg;
inputc->copy_parameters_to(dimg);
Expand Down Expand Up @@ -730,7 +730,7 @@ namespace dd
{
// read image before reading bboxes to get the size of the image
ImgTorchInputFileConn *inputc
= reinterpret_cast<ImgTorchInputFileConn *>(_inputc);
= dynamic_cast<ImgTorchInputFileConn *>(_inputc);

DDImg dimg;
inputc->copy_parameters_to(dimg);
Expand Down Expand Up @@ -798,7 +798,7 @@ namespace dd
const bool &target)
{
ImgTorchInputFileConn *inputc
= reinterpret_cast<ImgTorchInputFileConn *>(_inputc);
= dynamic_cast<ImgTorchInputFileConn *>(_inputc);

std::vector<int64_t> sizes{ height, width, bgr.channels() };
at::TensorOptions options(at::ScalarType::Byte);
Expand Down
25 changes: 24 additions & 1 deletion src/backends/torch/torchinputconns.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "imginputfileconn.h"
#include "txtinputfileconn.h"
#include "csvtsinputfileconn.h"
#include "videoinputfileconn.h"
#include "torchdataset.h"
#include "torchutils.h"

Expand Down Expand Up @@ -207,7 +208,7 @@ namespace dd
/**
* \brief image connector to torch backend
*/
class ImgTorchInputFileConn : public ImgInputFileConn,
class ImgTorchInputFileConn : virtual public ImgInputFileConn,
public TorchInputInterface
{
public:
Expand Down Expand Up @@ -336,6 +337,28 @@ namespace dd
bool _supports_bw = true;
};

class VideoTorchInputFileConn : public ImgTorchInputFileConn,
public VideoInputFileConn
{
public:
VideoTorchInputFileConn()
: ImgInputFileConn(), ImgTorchInputFileConn(), VideoInputFileConn()
{
}

/**
* \brief copy constructor
*/
VideoTorchInputFileConn(const VideoTorchInputFileConn &i)
: ImgInputFileConn(i), ImgTorchInputFileConn(i), VideoInputFileConn(i)
{
}

~VideoTorchInputFileConn()
{
}
};

/**
* \brief txt input connector
*/
Expand Down
9 changes: 7 additions & 2 deletions src/backends/torch/torchlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1812,9 +1812,12 @@ namespace dd

if (ad.has("chain") && ad.get("chain").get<bool>())
{
if (typeid(inputc) == typeid(ImgTorchInputFileConn))
if (std::is_same<TInputConnectorStrategy,
ImgTorchInputFileConn>::value)
{
auto *img_ic = reinterpret_cast<ImgTorchInputFileConn *>(&inputc);
// can't do reinterpret_cast because virtual inheritance
InputConnectorStrategy *inputc_base = &inputc;
auto *img_ic = dynamic_cast<ImgTorchInputFileConn *>(inputc_base);
APIData chain_input;
if (!img_ic->_orig_images.empty())
chain_input.add("imgs", img_ic->_orig_images);
Expand Down Expand Up @@ -2218,6 +2221,8 @@ namespace dd
}

template class TorchLib<ImgTorchInputFileConn, SupervisedOutput, TorchModel>;
template class TorchLib<VideoTorchInputFileConn, SupervisedOutput,
TorchModel>;
template class TorchLib<TxtTorchInputFileConn, SupervisedOutput, TorchModel>;
template class TorchLib<CSVTSTorchInputFileConn, SupervisedOutput,
TorchModel>;
Expand Down
15 changes: 15 additions & 0 deletions src/backends/torch/torchmodule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,21 @@ namespace dd
const ImgTorchInputFileConn &inputc, const TorchModel &tmodel,
const torch::Device &device, const APIData &ad);

template void TorchModule::post_transform(
const std::string tmpl, const APIData &template_params,
const VideoTorchInputFileConn &inputc, const TorchModel &tmodel,
const torch::Device &device);

template void TorchModule::post_transform_train(
const std::string tmpl, const APIData &template_params,
const VideoTorchInputFileConn &inputc, const TorchModel &tmodel,
const torch::Device &device);

template void TorchModule::post_transform_predict(
const std::string tmpl, const APIData &template_params,
const VideoTorchInputFileConn &inputc, const TorchModel &tmodel,
const torch::Device &device, const APIData &ad);

template void TorchModule::post_transform(
const std::string tmpl, const APIData &template_params,
const TxtTorchInputFileConn &inputc, const TorchModel &tmodel,
Expand Down
2 changes: 1 addition & 1 deletion src/backends/tsne/tsnelib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

#include "tsnelib.h"
#include "csvinputfileconn.h"
#include "allconnectors.hpp"
#include "outputconnectorstrategy.h"
#include <thread>
#include "utils/utils.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/backends/xgb/xgblib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/

#include "xgblib.h"
#include "csvinputfileconn.h"
#include "allconnectors.hpp"
#include "outputconnectorstrategy.h"
#include <iomanip>
#include <iostream>
Expand Down
8 changes: 0 additions & 8 deletions src/csvinputfileconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -964,12 +964,4 @@ namespace dd
};
}

#ifdef USE_XGBOOST
#include "backends/xgb/xgbinputconns.h"
#endif

#ifdef USE_TSNE
#include "backends/tsne/tsneinputconns.h"
#endif

#endif
5 changes: 2 additions & 3 deletions src/dto/chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,11 @@ namespace dd
DTO_FIELD(Float64, time);
};

class ChainResponse : public oatpp::DTO
class ChainResponse : public GenericResponse
{
DTO_INIT(ChainResponse, DTO)
DTO_INIT(ChainResponse, GenericResponse)

DTO_FIELD(String, dd_msg);
DTO_FIELD(Object<Status>, status);
DTO_FIELD(Object<ChainHead>, head);
DTO_FIELD(Object<ChainBody>, body);
};
Expand Down
Loading

0 comments on commit 02872eb

Please sign in to comment.