Add preprocessing warps for separate parameters

This commit is contained in:
Dmitry Kurtaev 2019-08-07 14:51:41 +03:00
parent 174b4ce29d
commit a9839af903
3 changed files with 36 additions and 19 deletions

View File

@ -999,9 +999,14 @@ CV__DNN_INLINE_NS_BEGIN
* Model creates net from file with trained weights and config, * Model creates net from file with trained weights and config,
* sets preprocessing input and runs forward pass. * sets preprocessing input and runs forward pass.
*/ */
class CV_EXPORTS_W Model : public Net class CV_EXPORTS_W_SIMPLE Model : public Net
{ {
public: public:
/**
* @brief Default constructor.
*/
Model();
/** /**
* @brief Create model from deep learning network represented in one of the supported formats. * @brief Create model from deep learning network represented in one of the supported formats.
* An order of @p model and @p config arguments does not matter. * An order of @p model and @p config arguments does not matter.
@ -1020,7 +1025,7 @@ CV__DNN_INLINE_NS_BEGIN
* @param[in] size New input size. * @param[in] size New input size.
* @note If shape of the new blob less than 0, then frame size not change. * @note If shape of the new blob less than 0, then frame size not change.
*/ */
Model& setInputSize(const Size& size); CV_WRAP Model& setInputSize(const Size& size);
/** @brief Set input size for frame. /** @brief Set input size for frame.
* @param[in] width New input width. * @param[in] width New input width.
@ -1028,27 +1033,27 @@ CV__DNN_INLINE_NS_BEGIN
* @note If shape of the new blob less than 0, * @note If shape of the new blob less than 0,
* then frame size not change. * then frame size not change.
*/ */
Model& setInputSize(int width, int height); CV_WRAP Model& setInputSize(int width, int height);
/** @brief Set mean value for frame. /** @brief Set mean value for frame.
* @param[in] mean Scalar with mean values which are subtracted from channels. * @param[in] mean Scalar with mean values which are subtracted from channels.
*/ */
Model& setInputMean(const Scalar& mean); CV_WRAP Model& setInputMean(const Scalar& mean);
/** @brief Set scalefactor value for frame. /** @brief Set scalefactor value for frame.
* @param[in] scale Multiplier for frame values. * @param[in] scale Multiplier for frame values.
*/ */
Model& setInputScale(double scale); CV_WRAP Model& setInputScale(double scale);
/** @brief Set flag crop for frame. /** @brief Set flag crop for frame.
* @param[in] crop Flag which indicates whether image will be cropped after resize or not. * @param[in] crop Flag which indicates whether image will be cropped after resize or not.
*/ */
Model& setInputCrop(bool crop); CV_WRAP Model& setInputCrop(bool crop);
/** @brief Set flag swapRB for frame. /** @brief Set flag swapRB for frame.
* @param[in] swapRB Flag which indicates that swap first and last channels. * @param[in] swapRB Flag which indicates that swap first and last channels.
*/ */
Model& setInputSwapRB(bool swapRB); CV_WRAP Model& setInputSwapRB(bool swapRB);
/** @brief Set preprocessing parameters for frame. /** @brief Set preprocessing parameters for frame.
* @param[in] size New input size. * @param[in] size New input size.
@ -1078,7 +1083,7 @@ CV__DNN_INLINE_NS_BEGIN
* ClassificationModel creates net from file with trained weights and config, * ClassificationModel creates net from file with trained weights and config,
* sets preprocessing input, runs forward pass and return top-1 prediction. * sets preprocessing input, runs forward pass and return top-1 prediction.
*/ */
class CV_EXPORTS_W ClassificationModel : public Model class CV_EXPORTS_W_SIMPLE ClassificationModel : public Model
{ {
public: public:
/** /**
@ -1111,7 +1116,7 @@ CV__DNN_INLINE_NS_BEGIN
* sets preprocessing input, runs forward pass and return result detections. * sets preprocessing input, runs forward pass and return result detections.
* For DetectionModel SSD, Faster R-CNN, YOLO topologies are supported. * For DetectionModel SSD, Faster R-CNN, YOLO topologies are supported.
*/ */
class CV_EXPORTS_W DetectionModel : public Model class CV_EXPORTS_W_SIMPLE DetectionModel : public Model
{ {
public: public:
/** /**

View File

@ -138,10 +138,7 @@ class dnn_test(NewOpenCVTests):
config = self.find_dnn_file("dnn/MobileNetSSD_deploy.prototxt") config = self.find_dnn_file("dnn/MobileNetSSD_deploy.prototxt")
frame = cv.imread(img_path) frame = cv.imread(img_path)
model = cv.dnn_DetectionModel(weights, config) model = cv.dnn_DetectionModel(weights, config)
size = (300, 300) model.setInputParams(size=(300, 300), mean=(127.5, 127.5, 127.5), scale=1.0/127.5)
mean = (127.5, 127.5, 127.5)
scale = 1.0 / 127.5
model.setInputParams(size=size, mean=mean, scale=scale)
iouDiff = 0.05 iouDiff = 0.05
confThreshold = 0.0001 confThreshold = 0.0001
@ -164,6 +161,21 @@ class dnn_test(NewOpenCVTests):
cv.rectangle(frame, list(box), (0, 255, 0)) cv.rectangle(frame, list(box), (0, 255, 0))
def test_classification_model(self):
img_path = self.find_dnn_file("dnn/googlenet_0.png")
weights = self.find_dnn_file("dnn/squeezenet_v1.1.caffemodel")
config = self.find_dnn_file("dnn/squeezenet_v1.1.prototxt")
ref = np.load(self.find_dnn_file("dnn/squeezenet_v1.1_prob.npy"))
frame = cv.imread(img_path)
model = cv.dnn_ClassificationModel(config, weights)
model.setInputSize(227, 227)
model.setInputCrop(True)
out = model.predict(frame)
normAssert(self, out, ref)
def test_face_detection(self): def test_face_detection(self):
testdata_required = bool(os.environ.get('OPENCV_DNN_TEST_REQUIRE_TESTDATA', False)) testdata_required = bool(os.environ.get('OPENCV_DNN_TEST_REQUIRE_TESTDATA', False))
proto = self.find_dnn_file('dnn/opencv_face_detector.prototxt', required=testdata_required) proto = self.find_dnn_file('dnn/opencv_face_detector.prototxt', required=testdata_required)

View File

@ -23,7 +23,7 @@ struct Model::Impl
Mat blob; Mat blob;
std::vector<String> outNames; std::vector<String> outNames;
void predict(Net& net, const Mat& frame, std::vector<Mat>& outs) void predict(Net& net, const Mat& frame, OutputArrayOfArrays outs)
{ {
if (size.empty()) if (size.empty())
CV_Error(Error::StsBadSize, "Input size not specified"); CV_Error(Error::StsBadSize, "Input size not specified");
@ -41,16 +41,18 @@ struct Model::Impl
} }
}; };
Model::Model() : impl(new Impl) {}
Model::Model(const String& model, const String& config) Model::Model(const String& model, const String& config)
: Net(readNet(model, config)), impl(new Impl) : Net(readNet(model, config)), impl(new Impl)
{ {
impl->outNames = getUnconnectedOutLayersNames(); impl->outNames = getUnconnectedOutLayersNames();
}; }
Model::Model(const Net& network) : Net(network), impl(new Impl) Model::Model(const Net& network) : Net(network), impl(new Impl)
{ {
impl->outNames = getUnconnectedOutLayersNames(); impl->outNames = getUnconnectedOutLayersNames();
}; }
Model& Model::setInputSize(const Size& size) Model& Model::setInputSize(const Size& size)
{ {
@ -100,9 +102,7 @@ void Model::setInputParams(double scale, const Size& size, const Scalar& mean,
void Model::predict(InputArray frame, OutputArrayOfArrays outs) void Model::predict(InputArray frame, OutputArrayOfArrays outs)
{ {
std::vector<Mat> outputs; impl->predict(*this, frame.getMat(), outs);
outs.getMatVector(outputs);
impl->predict(*this, frame.getMat(), outputs);
} }
ClassificationModel::ClassificationModel(const String& model, const String& config) ClassificationModel::ClassificationModel(const String& model, const String& config)