]> rtime.felk.cvut.cz Git - hercules2020/kcf.git/commitdiff
Do not allocate and free temporary matrices for every frame
authorMichal Sojka <michal.sojka@cvut.cz>
Thu, 11 Oct 2018 23:04:25 +0000 (01:04 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Thu, 11 Oct 2018 23:04:25 +0000 (01:04 +0200)
This speeds up the CUDA version, because pinned memory allocation is
implicit synchronization point. By allocating the variables only once,
we reduce the number of synchronization points.

src/kcf.cpp
src/kcf.h

index 7135a022d917f6595fc43117d4a1074f187f1f8f..69c8a9ae0b4ac6830e12890b32d8d4476aeb561f 100644 (file)
@@ -68,14 +68,11 @@ void KCF_Tracker::train(cv::Mat input_rgb, cv::Mat input_gray, double interp_fac
     TRACE("");
 
     // obtain a sub-window for training
-    // TODO: Move Mats outside from here
-    MatScaleFeats patch_feats(1, p_num_of_feats, feature_size);
-    MatScaleFeats temp(1, p_num_of_feats, feature_size);
     get_features(input_rgb, input_gray, p_current_center.x, p_current_center.y,
                  p_windows_size.width, p_windows_size.height,
-                 p_current_scale).copyTo(patch_feats.scale(0));
-    DEBUG_PRINT(patch_feats);
-    fft.forward_window(patch_feats, model->xf, temp);
+                 p_current_scale).copyTo(model->patch_feats.scale(0));
+    DEBUG_PRINT(model->patch_feats);
+    fft.forward_window(model->patch_feats, model->xf, model->temp);
     DEBUG_PRINTM(model->xf);
     model->model_xf = model->model_xf * (1. - interp_factor) + model->xf * interp_factor;
     DEBUG_PRINTM(model->model_xf);
@@ -190,7 +187,7 @@ void KCF_Tracker::init(cv::Mat &img, const cv::Rect &bbox, int fit_size_x, int f
     }
 #endif
 
-    model.reset(new Model(Fft::freq_size(feature_size), p_num_of_feats));
+    model.reset(new Model(feature_size, p_num_of_feats));
 
 #ifndef BIG_BATCH
     for (auto scale: p_scales)
index 99719e9bf086927baa5d74d1f97412e73e662644..349083b03aeb80ec2761858a6a20ca111da1d9e0 100644 (file)
--- a/src/kcf.h
+++ b/src/kcf.h
@@ -119,6 +119,7 @@ private:
     Kcf_Tracker_Private &d;
 
     class Model {
+        cv::Size feature_size;
         uint height, width, n_feats;
     public:
         ComplexMat yf {height, width, 1};
@@ -128,7 +129,17 @@ private:
         ComplexMat model_xf {height, width, n_feats};
         ComplexMat xf {height, width, n_feats};
 
-        Model(cv::Size freq_size, uint _n_feats) : height(freq_size.height), width(freq_size.width), n_feats(_n_feats) {}
+        // Temporary variables for trainig
+        MatScaleFeats patch_feats{1, n_feats, feature_size};
+        MatScaleFeats temp{1, n_feats, feature_size};
+
+
+
+        Model(cv::Size feature_size, uint _n_feats)
+            : feature_size(feature_size)
+            , height(Fft::freq_size(feature_size).height)
+            , width(Fft::freq_size(feature_size).width)
+            , n_feats(_n_feats) {}
     };
 
     std::unique_ptr<Model> model;
@@ -151,7 +162,6 @@ private:
         MatScales k;
     };
 
-
     //helping functions
     void scale_track(ThreadCtx &vars, cv::Mat &input_rgb, cv::Mat &input_gray);
     cv::Mat get_subwindow(const cv::Mat &input, int cx, int cy, int size_x, int size_y) const;