From 20ee2f5d59624b9ac46b907b6dd75fbcc1378ce0 Mon Sep 17 00:00:00 2001 From: yuji nunome Date: Wed, 8 Dec 2021 15:46:23 +0900 Subject: [PATCH 01/11] =?UTF-8?q?faceboxes=E3=81=AEbatch=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- faceboxes_pytorch/faceboxes_face_detector.py | 82 +++++++++++++++++++- faceboxes_pytorch/utils/box_utils.py | 49 ++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/faceboxes_pytorch/faceboxes_face_detector.py b/faceboxes_pytorch/faceboxes_face_detector.py index 835cafc6..98adbf5b 100755 --- a/faceboxes_pytorch/faceboxes_face_detector.py +++ b/faceboxes_pytorch/faceboxes_face_detector.py @@ -4,7 +4,8 @@ from .data import cfg from .layers.functions.prior_box import PriorBox from .models.faceboxes import FaceBoxes -from .utils.box_utils import decode +from .utils.box_utils import decode, batch_decode, get_faceboxes_max_batch_size +from typing import List, Tuple class FaceBoxesFaceDetector(object): def __init__(self, use_gpu=False): @@ -90,3 +91,82 @@ def get_faceboxes(self, image, threshold=0.2): scores, boxes = zip(*scores_and_boxes) return scores, boxes + + + def get_batch_faceboxes(self, image_list:List[np.ndarray], *, batch_size=-1, threshold=0.2)->List[Tuple[List[float],List[np.ndarray]]]: + """ + image_list: List[np.ndarray] 同じサイズの画像のリスト + batch_size: image_listの中から何個処理するかを指定する. デフォルトの場合はgpuの空きメモリから算出する + threshold: faceboxesのconfの閾値 + + return: List[Tuple[confのリスト],[BoundingBoxのリスト]] + """ + + if len(image_list) == 0: + return [([],[])] + + im_height, im_width, im_ch = image_list[0].shape + + if batch_size == -1: + # batch_sizeが未指定の場合はgpuの空きメモリと画像1枚あたりの容量から許容枚数を算出し、安全マージンの7掛けした値をbatch_sizeとして使用する + torch.cuda.empty_cache() + batch_size = get_faceboxes_max_batch_size(width=im_width, height=im_height, ch=im_ch) + batch_size = int(batch_size * 0.7) + + resize = 1 + + results = [] + + priorbox = PriorBox(cfg, image_size=(im_height, im_width)) + priors = priorbox.forward() + priors = priors.to(self.device) + prior_data = priors.data + + for i in range(0, int(len(image_list) / batch_size) + 1): + images = image_list[batch_size * i: min(len(image_list), batch_size * (i+1))] + + im_height, im_width, im_ch = images[0].shape + scale = torch.Tensor([im_width, im_height, im_width, im_height]) + images = np.array(images).reshape(len(images), im_height, im_width, im_ch) + + imgs = torch.from_numpy(images) + imgs = imgs.to(self.device) + scale = scale.to(self.device) + + imgs = imgs.to(torch.float32) + imgs = torch.sub(imgs, torch.tensor((104,117,123)).to(self.device)) + imgs = imgs.permute(0,3,1,2) + + loc, conf = self.net(imgs) + + boxes = batch_decode(loc.data.squeeze(0), prior_data, cfg['variance']) + boxes = boxes * scale / resize + + boxes = boxes.cpu().numpy() + scores = conf.data.cpu().numpy()[:, :, 1] + + thres_tuple = torch.nonzero(torch.where(conf[:,:,1] > threshold, 1, 0), as_tuple=True) + image_inds = thres_tuple[0].data.cpu().numpy() + boxes_inds = thres_tuple[1].data.cpu().numpy() + + # ここgpu化しなくても処理時間が支配的ではないのでcpuでやる + result = [] + for i in range(0, len(images)): + inds = boxes_inds[np.where(image_inds == i)] + if len(inds) == 0: + result.append(([],[])) + continue + + scores_and_boxes = zip(scores[i][inds], boxes[i][inds]) + scores_and_boxes = sorted(scores_and_boxes, key=lambda x: -x[0]) + s, b = zip(*scores_and_boxes) + result.append((s,b)) + + results += result + + # 以降gpu上の画像は使わないので解放する + # 速度に影響なさそうなのでループごとに実行 + del imgs + torch.cuda.empty_cache() + + return results \ No newline at end of file diff --git a/faceboxes_pytorch/utils/box_utils.py b/faceboxes_pytorch/utils/box_utils.py index 4797f1d7..42ebc167 100755 --- a/faceboxes_pytorch/utils/box_utils.py +++ b/faceboxes_pytorch/utils/box_utils.py @@ -1,6 +1,20 @@ import torch import numpy as np +import subprocess + +DEFAULT_ATTRIBUTES = ( + 'index', + 'uuid', + 'name', + 'timestamp', + 'memory.total', + 'memory.free', + 'memory.used', + 'utilization.gpu', + 'utilization.memory' +) + def point_form(boxes): """ Convert prior_boxes to (xmin, ymin, xmax, ymax) @@ -274,3 +288,38 @@ def nms(boxes, scores, overlap=0.5, top_k=200): return keep, count +# https://qiita.com/tomotaka_ito/items/1da001c98b46ecf28ec7 +def get_gpu_info(nvidia_smi_path='nvidia-smi', keys=DEFAULT_ATTRIBUTES, no_units=True): + nu_opt = '' if not no_units else ',nounits' + cmd = '%s --query-gpu=%s --format=csv,noheader%s' % (nvidia_smi_path, ','.join(keys), nu_opt) + output = subprocess.check_output(cmd, shell=True) + lines = output.decode().split('\n') + lines = [ line.strip() for line in lines if line.strip() != '' ] + + return [ { k: v for k, v in zip(keys, line.split(', ')) } for line in lines ] + + +def get_faceboxes_max_batch_size(width=1080, height=1920, ch=3, print_status_flg=False): + gpu_info = get_gpu_info() + if print_status_flg: + print(gpu_info) + + # faceboxesは内部でfloat32に変換するため1ch4byte + image_bit = width * height * ch * 4 + image_mega_byte = image_bit / 1024 / 1024 + gpu_batch_sizes = [int(int(info['memory.free']) / image_mega_byte) for info in gpu_info] + + if print_status_flg: + print(f'cpu_batch_size: {cpu_batch_size}') + print(f'gpu_batch_sizes: {gpu_batch_sizes}') + + return min(gpu_batch_sizes) + + +def batch_decode(loc, priors, variances): + boxes = torch.cat(( + priors[:, :2] + loc[:, :, :2] * variances[0] * priors[:, 2:], + priors[:, 2:] * torch.exp(loc[:, :, 2:] * variances[1])), 2) + boxes[:, :, :2] -= boxes[:, :, 2:] / 2 + boxes[:, :, 2:] += boxes[:, :, :2] + return boxes From a75a25dfcb890777eb16810076d3995d92e6832b Mon Sep 17 00:00:00 2001 From: yuji nunome Date: Wed, 8 Dec 2021 16:42:53 +0900 Subject: [PATCH 02/11] =?UTF-8?q?batch=20size=E5=91=A8=E3=82=8A=E3=81=AE?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AB?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- faceboxes_pytorch/faceboxes_face_detector.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/faceboxes_pytorch/faceboxes_face_detector.py b/faceboxes_pytorch/faceboxes_face_detector.py index 98adbf5b..ed809af3 100755 --- a/faceboxes_pytorch/faceboxes_face_detector.py +++ b/faceboxes_pytorch/faceboxes_face_detector.py @@ -96,7 +96,7 @@ def get_faceboxes(self, image, threshold=0.2): def get_batch_faceboxes(self, image_list:List[np.ndarray], *, batch_size=-1, threshold=0.2)->List[Tuple[List[float],List[np.ndarray]]]: """ image_list: List[np.ndarray] 同じサイズの画像のリスト - batch_size: image_listの中から何個処理するかを指定する. デフォルトの場合はgpuの空きメモリから算出する + batch_size: image_listの中から何個処理するかを指定する. デフォルトの場合はgpuの空きメモリから算出する. 値は2以上にすること threshold: faceboxesのconfの閾値 return: List[Tuple[confのリスト],[BoundingBoxのリスト]] @@ -105,6 +105,9 @@ def get_batch_faceboxes(self, image_list:List[np.ndarray], *, batch_size=-1, thr if len(image_list) == 0: return [([],[])] + if (batch_size == 0) or (batch_size == 1): + raise ValueError("batch_size must be greater than or equal to 2.") + im_height, im_width, im_ch = image_list[0].shape if batch_size == -1: @@ -125,6 +128,14 @@ def get_batch_faceboxes(self, image_list:List[np.ndarray], *, batch_size=-1, thr for i in range(0, int(len(image_list) / batch_size) + 1): images = image_list[batch_size * i: min(len(image_list), batch_size * (i+1))] + if len(images) == 0: + break + + if len(images) == 1: + scores, boxes = self.get_faceboxes(images[0], threshold) + results.append((scores, boxes)) + break + im_height, im_width, im_ch = images[0].shape scale = torch.Tensor([im_width, im_height, im_width, im_height]) images = np.array(images).reshape(len(images), im_height, im_width, im_ch) @@ -169,4 +180,4 @@ def get_batch_faceboxes(self, image_list:List[np.ndarray], *, batch_size=-1, thr del imgs torch.cuda.empty_cache() - return results \ No newline at end of file + return results From 747aac1d65af4c19788d527c98d09d74e154b886 Mon Sep 17 00:00:00 2001 From: yuji nunome Date: Wed, 8 Dec 2021 16:52:47 +0900 Subject: [PATCH 03/11] =?UTF-8?q?=E4=BD=BF=E3=82=8F=E3=81=AA=E3=81=8F?= =?UTF-8?q?=E3=81=AA=E3=81=A3=E3=81=9F=E5=A4=89=E6=95=B0=E3=81=AE=E5=89=8A?= =?UTF-8?q?=E9=99=A4=E3=81=97=E5=BF=98=E3=82=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- faceboxes_pytorch/utils/box_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/faceboxes_pytorch/utils/box_utils.py b/faceboxes_pytorch/utils/box_utils.py index 42ebc167..067fda58 100755 --- a/faceboxes_pytorch/utils/box_utils.py +++ b/faceboxes_pytorch/utils/box_utils.py @@ -310,7 +310,6 @@ def get_faceboxes_max_batch_size(width=1080, height=1920, ch=3, print_status_flg gpu_batch_sizes = [int(int(info['memory.free']) / image_mega_byte) for info in gpu_info] if print_status_flg: - print(f'cpu_batch_size: {cpu_batch_size}') print(f'gpu_batch_sizes: {gpu_batch_sizes}') return min(gpu_batch_sizes) From 4541d29a1402fb40d77f3821fff68259c365870a Mon Sep 17 00:00:00 2001 From: yuji nunome Date: Thu, 9 Dec 2021 12:10:28 +0900 Subject: [PATCH 04/11] =?UTF-8?q?cpu=E3=81=A7=E5=AE=9F=E6=96=BD=E3=81=97?= =?UTF-8?q?=E3=81=A6=E3=81=84=E3=81=9Ffloat=E5=A4=89=E6=8F=9B=E3=81=A8subt?= =?UTF-8?q?ract=E3=82=92gpu=E3=81=A7=E5=AE=9F=E6=96=BD=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- faceboxes_pytorch/faceboxes_face_detector.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/faceboxes_pytorch/faceboxes_face_detector.py b/faceboxes_pytorch/faceboxes_face_detector.py index 835cafc6..c9c93fdb 100755 --- a/faceboxes_pytorch/faceboxes_face_detector.py +++ b/faceboxes_pytorch/faceboxes_face_detector.py @@ -58,15 +58,17 @@ def load_model(self, model, pretrained_path, load_to_cpu): def get_faceboxes(self, image, threshold=0.2): resize = 1 - img = np.float32(image) - im_height, im_width, _ = img.shape - scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]]) - img -= (104, 117, 123) - img = img.transpose(2, 0, 1) - img = torch.from_numpy(img).unsqueeze(0) + im_height, im_width, _ = image.shape + scale = torch.Tensor([im_width, im_height, im_width, im_height]) + + img = torch.from_numpy(image).unsqueeze(0) img = img.to(self.device) + img = img.to(torch.float32) scale = scale.to(self.device) + img = torch.sub(img, torch.tensor((104,117,123)).to(self.device)) + img = img.permute(0,3,1,2) + loc, conf = self.net(img) # forward pass priorbox = PriorBox(cfg, image_size=(im_height, im_width)) priors = priorbox.forward() From 4784a4714fc5b86bea7428db3a9593aacfc72fdb Mon Sep 17 00:00:00 2001 From: yuji nunome Date: Thu, 9 Dec 2021 13:24:42 +0900 Subject: [PATCH 05/11] Revert "Merge branch 'feature/optimize_1shot' into feature/batch_process" This reverts commit d7cb70bfd5feb4fc39dd13a7d12d739e7d595f2c, reversing changes made to 747aac1d65af4c19788d527c98d09d74e154b886. --- faceboxes_pytorch/faceboxes_face_detector.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/faceboxes_pytorch/faceboxes_face_detector.py b/faceboxes_pytorch/faceboxes_face_detector.py index 10ffc9a3..ed809af3 100755 --- a/faceboxes_pytorch/faceboxes_face_detector.py +++ b/faceboxes_pytorch/faceboxes_face_detector.py @@ -59,17 +59,15 @@ def load_model(self, model, pretrained_path, load_to_cpu): def get_faceboxes(self, image, threshold=0.2): resize = 1 - im_height, im_width, _ = image.shape - scale = torch.Tensor([im_width, im_height, im_width, im_height]) - - img = torch.from_numpy(image).unsqueeze(0) + img = np.float32(image) + im_height, im_width, _ = img.shape + scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]]) + img -= (104, 117, 123) + img = img.transpose(2, 0, 1) + img = torch.from_numpy(img).unsqueeze(0) img = img.to(self.device) - img = img.to(torch.float32) scale = scale.to(self.device) - img = torch.sub(img, torch.tensor((104,117,123)).to(self.device)) - img = img.permute(0,3,1,2) - loc, conf = self.net(img) # forward pass priorbox = PriorBox(cfg, image_size=(im_height, im_width)) priors = priorbox.forward() From db7d1f53219defa53b9c8adf4f0d6adce98dca24 Mon Sep 17 00:00:00 2001 From: yuji nunome Date: Fri, 10 Dec 2021 13:08:11 +0900 Subject: [PATCH 06/11] =?UTF-8?q?empty=5Fcache=E3=81=AF=E3=82=84=E3=82=89?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=81=BB=E3=81=86=E3=81=8C=E3=82=88=E3=81=95?= =?UTF-8?q?=E3=81=9D=E3=81=86=E3=81=AA=E3=81=AE=E3=81=A7=E9=99=A4=E5=8E=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- faceboxes_pytorch/faceboxes_face_detector.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/faceboxes_pytorch/faceboxes_face_detector.py b/faceboxes_pytorch/faceboxes_face_detector.py index ed809af3..dcf5cc66 100755 --- a/faceboxes_pytorch/faceboxes_face_detector.py +++ b/faceboxes_pytorch/faceboxes_face_detector.py @@ -116,6 +116,7 @@ def get_batch_faceboxes(self, image_list:List[np.ndarray], *, batch_size=-1, thr batch_size = get_faceboxes_max_batch_size(width=im_width, height=im_height, ch=im_ch) batch_size = int(batch_size * 0.7) + batch_size = min(len(image_list), batch_size) resize = 1 results = [] @@ -176,8 +177,6 @@ def get_batch_faceboxes(self, image_list:List[np.ndarray], *, batch_size=-1, thr results += result # 以降gpu上の画像は使わないので解放する - # 速度に影響なさそうなのでループごとに実行 del imgs - torch.cuda.empty_cache() return results From 58837c525b6ace2d6f7eb6f3242f8aeb7efdbf8e Mon Sep 17 00:00:00 2001 From: yuji nunome Date: Fri, 10 Dec 2021 14:52:07 +0900 Subject: [PATCH 07/11] =?UTF-8?q?faceboxes=E3=81=A7=E3=81=AF=E5=8B=BE?= =?UTF-8?q?=E9=85=8D=E8=A8=88=E7=AE=97=E3=82=92=E3=81=97=E3=81=AA=E3=81=84?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- faceboxes_pytorch/faceboxes_face_detector.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/faceboxes_pytorch/faceboxes_face_detector.py b/faceboxes_pytorch/faceboxes_face_detector.py index dcf5cc66..5c3d54d7 100755 --- a/faceboxes_pytorch/faceboxes_face_detector.py +++ b/faceboxes_pytorch/faceboxes_face_detector.py @@ -9,7 +9,6 @@ class FaceBoxesFaceDetector(object): def __init__(self, use_gpu=False): - torch.set_grad_enabled(False) # net and model self.net = FaceBoxes(phase='test', size=None, num_classes=2) # initialize detector weight_path = os.path.normpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'weights/FaceBoxes.pth')) @@ -56,6 +55,7 @@ def load_model(self, model, pretrained_path, load_to_cpu): model.load_state_dict(pretrained_dict, strict=False) return model + @torch.no_grad() def get_faceboxes(self, image, threshold=0.2): resize = 1 @@ -92,7 +92,7 @@ def get_faceboxes(self, image, threshold=0.2): return scores, boxes - + @torch.no_grad() def get_batch_faceboxes(self, image_list:List[np.ndarray], *, batch_size=-1, threshold=0.2)->List[Tuple[List[float],List[np.ndarray]]]: """ image_list: List[np.ndarray] 同じサイズの画像のリスト From d9b031349058ad6a69a815b7f2e0e34fd26f3bff Mon Sep 17 00:00:00 2001 From: yuji nunome Date: Fri, 10 Dec 2021 17:11:19 +0900 Subject: [PATCH 08/11] =?UTF-8?q?resize=E3=82=82=E3=81=82=E3=82=8F?= =?UTF-8?q?=E3=81=9B=E3=81=A6=E5=AE=9F=E8=A1=8C=E3=81=99=E3=82=8B=E9=96=A2?= =?UTF-8?q?=E6=95=B0=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- faceboxes_pytorch/faceboxes_face_detector.py | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/faceboxes_pytorch/faceboxes_face_detector.py b/faceboxes_pytorch/faceboxes_face_detector.py index 5c3d54d7..2d68de17 100755 --- a/faceboxes_pytorch/faceboxes_face_detector.py +++ b/faceboxes_pytorch/faceboxes_face_detector.py @@ -7,6 +7,8 @@ from .utils.box_utils import decode, batch_decode, get_faceboxes_max_batch_size from typing import List, Tuple +import cv2 + class FaceBoxesFaceDetector(object): def __init__(self, use_gpu=False): # net and model @@ -180,3 +182,24 @@ def get_batch_faceboxes(self, image_list:List[np.ndarray], *, batch_size=-1, thr del imgs return results + + @torch.no_grad() + def get_batch_faceboxes_with_resize(self, image_list:List[np.ndarray], *, resize_target_width=360, resize_target_height=640, + batch_size=-1, threshold=0.2)->List[Tuple[List[float],List[np.ndarray]]]: + + resize_target_resize_size = (resize_target_width, resize_target_height) + shapes = [i.shape for i in image_list] + resized_images = [cv2.resize(i, resize_target_resize_size) for i in image_list] + + results = self.get_batch_faceboxes(resized_images, batch_size=batch_size, threshold=threshold) + + new_results = [] + for shape, (confs, boxes) in zip(shapes, results): + h, w = shape[:2] + rh = h / resize_target_height + rw = w / resize_target_width + ratios = [rw, rh, rw, rh] + new_result = (confs, [b * ratios for b in boxes]) + new_results.append(new_result) + + return new_results From 62a20de058eaa85439daa9eb5a75ebb4da633b13 Mon Sep 17 00:00:00 2001 From: yuji nunome Date: Fri, 10 Dec 2021 17:47:08 +0900 Subject: [PATCH 09/11] =?UTF-8?q?=E3=82=B3=E3=83=A1=E3=83=B3=E3=83=88?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- faceboxes_pytorch/faceboxes_face_detector.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/faceboxes_pytorch/faceboxes_face_detector.py b/faceboxes_pytorch/faceboxes_face_detector.py index 2d68de17..47cdc1c1 100755 --- a/faceboxes_pytorch/faceboxes_face_detector.py +++ b/faceboxes_pytorch/faceboxes_face_detector.py @@ -186,6 +186,16 @@ def get_batch_faceboxes(self, image_list:List[np.ndarray], *, batch_size=-1, thr @torch.no_grad() def get_batch_faceboxes_with_resize(self, image_list:List[np.ndarray], *, resize_target_width=360, resize_target_height=640, batch_size=-1, threshold=0.2)->List[Tuple[List[float],List[np.ndarray]]]: + """ + image_list: List[np.ndarray] 画像のリスト 内部でresizeするため、同じ大きさである必要はない + resize_target_width: 内部でresizeするときのwidth + resize_target_height: 内部でresizeするときのheight + 上二つのデフォルト値はアスペクト比9:16にしている + batch_size: image_listの中から何個処理するかを指定する. デフォルトの場合はgpuの空きメモリから算出する.値は2以上にすること + threshold: faceboxesのconfの閾値 + + return: List[Tuple[confのリスト],[BoundingBoxのリスト]] + """ resize_target_resize_size = (resize_target_width, resize_target_height) shapes = [i.shape for i in image_list] From c57656af0ad190dfff3b76c53cdf76198c19196a Mon Sep 17 00:00:00 2001 From: yuji nunome Date: Mon, 14 Feb 2022 17:17:35 +0900 Subject: [PATCH 10/11] =?UTF-8?q?version=E3=82=92=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f12fd9e3..f6bad8ff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "faceboxes_pytorch" -version = "0.1.2" +version = "0.1.3" description = "" authors = ["Your Name "] From 93a3b82cbc94f54ad4ce34f2a06e2293999de349 Mon Sep 17 00:00:00 2001 From: yuji nunome Date: Thu, 17 Feb 2022 18:34:11 +0900 Subject: [PATCH 11/11] =?UTF-8?q?cpu=E3=81=A7get=5Fbatch=5Ffaceboxes?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=81=A8=E3=80=81=E5=AD=98=E5=9C=A8=E3=81=97?= =?UTF-8?q?=E3=81=AA=E3=81=84nvidiasmi=E3=82=92=E8=A6=8B=E3=81=AB=E8=A1=8C?= =?UTF-8?q?=E3=81=A3=E3=81=A6=E3=81=97=E3=81=BE=E3=81=86=E3=81=AE=E3=81=A7?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- faceboxes_pytorch/faceboxes_face_detector.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/faceboxes_pytorch/faceboxes_face_detector.py b/faceboxes_pytorch/faceboxes_face_detector.py index 47cdc1c1..3676d5b6 100755 --- a/faceboxes_pytorch/faceboxes_face_detector.py +++ b/faceboxes_pytorch/faceboxes_face_detector.py @@ -112,12 +112,15 @@ def get_batch_faceboxes(self, image_list:List[np.ndarray], *, batch_size=-1, thr im_height, im_width, im_ch = image_list[0].shape - if batch_size == -1: + if (batch_size == -1) and (torch.cuda.is_available()): # batch_sizeが未指定の場合はgpuの空きメモリと画像1枚あたりの容量から許容枚数を算出し、安全マージンの7掛けした値をbatch_sizeとして使用する torch.cuda.empty_cache() batch_size = get_faceboxes_max_batch_size(width=im_width, height=im_height, ch=im_ch) batch_size = int(batch_size * 0.7) + if (batch_size == -1) and (not torch.cuda.is_available()): + batch_size = len(image_list) + batch_size = min(len(image_list), batch_size) resize = 1