I have implemented maskrcnn code and installed it using the github repo maskrcnn_benchmark.
I am using https://github.com/facebookresearch/maskrcnn-benchmark this repository.
Problem:
-
I have used the pre-trained weights e2e_mask_rcnn_R_101_FPN_1x_caffe2.yaml
-
I am using flask application and a detection model to detect a person in the scene.
-
I pass 2 images for a particular request, first being a front pose and second is the side pose of the person
-
For a particular request maskrcnn is taking 600-800 MB of memory on my local machine
-
For another request memory starts to load from 600-800 MB and increases thereafter.
-
If I pause the request for sometime, it gets back to normal and starts from scratch
-
On the server, it is not doing the same though, memory keeps increasing subsequently
-
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.
-
But, again if I pass a different image it starts increasing for the maskrcnn process.
Code:
Here are the code snippets that I am using (You can check out run_maskrcnn)
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 > self.confidence_threshold).squeeze(1)
predictions = predictions[keep]
scores = predictions.get_field("scores")
_, idx = scores.sort(0, descending=True)
return predictions[idx]