Memory leak while running maskrcnn_benchmark

ankitsharma07: <p>I have implemented maskrcnn code and installed it using the github repo maskrcnn_benchmark.</p>
<p>I am using <a href="https://github.com/facebookresearch/maskrcnn-benchmark" rel="nofollow noopener">https://github.com/facebookresearch/maskrcnn-benchmark</a> this repository.</p>
<p>Problem:</p>
<ul>
<li>
<p>I have used the pre-trained weights e2e_mask_rcnn_R_101_FPN_1x_caffe2.yaml</p>
</li>
<li>
<p>I am using flask application and a detection model to detect a person in the scene.</p>
</li>
<li>
<p>I pass 2 images for a particular request, first being a front pose and second is the side pose of the person</p>
</li>
<li>
<p>For a particular request maskrcnn is taking 600-800 MB of memory on my local machine</p>
</li>
<li>
<p>For another request memory starts to load from 600-800 MB and increases thereafter.</p>
</li>
<li>
<p>If I pause the request for sometime, it gets back to normal and starts from scratch</p>
</li>
<li>
<p>On the server, it is not doing the same though, memory keeps increasing subsequently</p>
</li>
<li>
<p>But, if I keep sending same front and side image memory increases till 2000 MB and then processes happen without the increment in the memory.</p>
</li>
<li>
<p>But, again if I pass a different image it starts increasing for the maskrcnn process.</p>
</li>
</ul>
<p>Code:</p>
<p>Here are the code snippets that I am using (You can check out run_maskrcnn)</p>
<pre><code class="lang-auto">def process_maskrcnn(self,front,side):
        # try:
        self.obj_maskrcnn.loader()
        frontrcnnmask,frontmask = self.obj_maskrcnn.run_maskrcnn(front,'f')
        sidercnnmask,sidemask = self.obj_maskrcnn.run_maskrcnn(side,'s')
        cv2.imwrite("frontmaskrcnn.jpg",frontrcnnmask)
        cv2.imwrite("sidemaskrcnn.jpg",sidercnnmask)
        gc.collect()
        return True,frontrcnnmask,sidercnnmask,frontmask,sidemask

 def run_maskrcnn(self, image,pose):
        if pose == 'f':
            predictions = self.compute_prediction(image)
            top_predictions = self.select_top_predictions(predictions)
            result = image.copy()
            maskimg,maskimgall = self.overlay_mask(result, top_predictions)
            # kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
            # maskimg = cv2.dilate(maskimg, kernel)
            # maskimgall = cv2.dilate(maskimgall, kernel)
            if cfg.debug == 'True':
                cv2.imwrite(self.outputdir + '/front.jpg', maskimg)
            return maskimg, maskimgall
        if pose == 's':
            predictions = self.compute_prediction(image)
            top_predictions = self.select_top_predictions(predictions)
            result = image.copy()
            maskimg, maskimgall = self.overlay_mask(result, top_predictions)
            kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15))
            maskimg = cv2.dilate(maskimg, kernel)
            maskimgall = cv2.dilate(maskimgall, kernel)
            if cfg.debug == 'True':
                cv2.imwrite(self.outputdir + '/side.jpg', maskimg)
            return maskimg, maskimgall


 def compute_prediction(self, original_image):
        image = self.transforms(original_image)
        image_list = to_image_list(image, self.cfg.DATALOADER.SIZE_DIVISIBILITY)
        image_list = image_list.to(self.device)
        with torch.no_grad():
            predictions = self.model(image_list)
        predictions = [o.to(self.cpu_device) for o in predictions]
        prediction = predictions[0]
        height, width = original_image.shape[:-1]
        prediction = prediction.resize((width, height))
        if prediction.has_field("mask"):
            masks = prediction.get_field("mask")
            masks = self.masker([masks], [prediction])[0]
            prediction.add_field("mask", masks)


def select_top_predictions(self, predictions):
        scores = predictions.get_field("scores")
        keep = torch.nonzero(scores &gt; self.confidence_threshold).squeeze(1)
        predictions = predictions[keep]
        scores = predictions.get_field("scores")
        _, idx = scores.sort(0, descending=True)
        return predictions[idx]

</code></pre>