diff --git a/app.py b/app.py index dcc471b17926fd9fe322bf3ea6294373f209cf00..ea7dabf502a171b22b6ab9d3c38d273489f3dc5e 100644 --- a/app.py +++ b/app.py @@ -15,156 +15,156 @@ from PIL import Image import tqdm from modules.networks.faceshifter import FSGenerator -# from inference.alignment import norm_crop, norm_crop_with_M, paste_back -# from inference.utils import save, get_5_from_98, get_detector, get_lmk -# from inference.PIPNet.lib.tools import get_lmk_model, demo_image -# from inference.landmark_smooth import kalman_filter_landmark, savgol_filter_landmark +from inference.alignment import norm_crop, norm_crop_with_M, paste_back +from inference.utils import save, get_5_from_98, get_detector, get_lmk +from third_party.PIPNet.lib.tools import get_lmk_model, demo_image +from inference.landmark_smooth import kalman_filter_landmark, savgol_filter_landmark from inference.tricks import Trick -# make_abs_path = lambda fn: os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), fn)) -# -# -# fs_model_name = 'faceshifter' -# in_size = 512 -# -# mouth_net_param = { -# "use": True, -# "feature_dim": 128, -# "crop_param": (28, 56, 84, 112), -# "weight_path": "../../modules/third_party/arcface/weights/mouth_net_28_56_84_112.pth", -# } -# trick = Trick() -# -# T = transforms.Compose( -# [ -# transforms.ToTensor(), -# transforms.Normalize(0.5, 0.5), -# ] -# ) -# tensor2pil_transform = transforms.ToPILImage() -# -# -# def extract_generator(ckpt: str, pt: str): -# print(f'[extract_generator] loading ckpt...') -# from trainer.faceshifter.faceshifter_pl import FaceshifterPL512, FaceshifterPL -# import yaml -# with open(make_abs_path('../../trainer/faceshifter/config.yaml'), 'r') as f: -# config = yaml.load(f, Loader=yaml.FullLoader) -# config['mouth_net'] = mouth_net_param -# -# if in_size == 256: -# net = FaceshifterPL(n_layers=3, num_D=3, config=config) -# elif in_size == 512: -# net = FaceshifterPL512(n_layers=3, num_D=3, config=config, verbose=False) -# else: -# raise ValueError('Not supported in_size.') -# checkpoint = torch.load(ckpt, map_location="cpu", ) -# net.load_state_dict(checkpoint["state_dict"], strict=False) -# net.eval() -# -# G = net.generator -# torch.save(G.state_dict(), pt) -# print(f'[extract_generator] extracted from {ckpt}, pth saved to {pt}') -# -# -# ''' load model ''' -# if fs_model_name == 'faceshifter': -# # pt_path = make_abs_path("../ffplus/extracted_ckpt/G_mouth1_t38.pth") -# # pt_path = make_abs_path("../ffplus/extracted_ckpt/G_mouth1_t512_6.pth") -# # ckpt_path = "/apdcephfs/share_1290939/gavinyuan/out/triplet512_6/epoch=3-step=128999.ckpt" -# pt_path = make_abs_path("../ffplus/extracted_ckpt/G_mouth1_t512_4.pth") -# ckpt_path = "/apdcephfs/share_1290939/gavinyuan/out/triplet512_4/epoch=2-step=185999.ckpt" -# if not os.path.exists(pt_path) or 't512' in pt_path: -# extract_generator(ckpt_path, pt_path) -# fs_model = FSGenerator( -# make_abs_path("../../modules/third_party/arcface/weights/ms1mv3_arcface_r100_fp16/backbone.pth"), -# mouth_net_param=mouth_net_param, -# in_size=in_size, -# downup=in_size == 512, -# ) -# fs_model.load_state_dict(torch.load(pt_path, "cpu"), strict=True) -# fs_model.eval() -# -# @torch.no_grad() -# def infer_batch_to_img(i_s, i_t, post: bool = False): -# i_r = fs_model(i_s, i_t)[0] # x, id_vector, att -# -# if post: -# target_hair_mask = trick.get_any_mask(i_t, par=[0, 17]) -# target_hair_mask = trick.smooth_mask(target_hair_mask) -# i_r = target_hair_mask * i_t + (target_hair_mask * (-1) + 1) * i_r -# i_r = trick.finetune_mouth(i_s, i_t, i_r) if in_size == 256 else i_r -# -# img_r = trick.tensor_to_arr(i_r)[0] -# return img_r -# -# elif fs_model_name == 'simswap_triplet' or fs_model_name == 'simswap_vanilla': -# from modules.networks.simswap import Generator_Adain_Upsample -# sw_model = Generator_Adain_Upsample( -# input_nc=3, output_nc=3, latent_size=512, n_blocks=9, deep=False, -# mouth_net_param=mouth_net_param -# ) -# if fs_model_name == 'simswap_triplet': -# pt_path = make_abs_path("../ffplus/extracted_ckpt/G_mouth1_st5.pth") -# ckpt_path = make_abs_path("/apdcephfs/share_1290939/gavinyuan/out/" -# "simswap_triplet_5/epoch=12-step=782999.ckpt") -# elif fs_model_name == 'simswap_vanilla': -# pt_path = make_abs_path("../ffplus/extracted_ckpt/G_tmp_sv4_off.pth") -# ckpt_path = make_abs_path("/apdcephfs/share_1290939/gavinyuan/out/" -# "simswap_vanilla_4/epoch=694-step=1487999.ckpt") -# else: -# pt_path = None -# ckpt_path = None -# sw_model.load_state_dict(torch.load(pt_path, "cpu"), strict=False) -# sw_model.eval() -# fs_model = sw_model -# -# from trainer.simswap.simswap_pl import SimSwapPL -# import yaml -# with open(make_abs_path('../../trainer/simswap/config.yaml'), 'r') as f: -# config = yaml.load(f, Loader=yaml.FullLoader) -# config['mouth_net'] = mouth_net_param -# net = SimSwapPL(config=config, use_official_arc='off' in pt_path) -# -# checkpoint = torch.load(ckpt_path, map_location="cpu") -# net.load_state_dict(checkpoint["state_dict"], strict=False) -# net.eval() -# sw_mouth_net = net.mouth_net # maybe None -# sw_netArc = net.netArc -# fs_model = fs_model.cuda() -# sw_mouth_net = sw_mouth_net.cuda() if sw_mouth_net is not None else sw_mouth_net -# sw_netArc = sw_netArc.cuda() -# -# @torch.no_grad() -# def infer_batch_to_img(i_s, i_t, post: bool = False): -# i_r = fs_model(source=i_s, target=i_t, net_arc=sw_netArc, mouth_net=sw_mouth_net,) -# if post: -# target_hair_mask = trick.get_any_mask(i_t, par=[0, 17]) -# target_hair_mask = trick.smooth_mask(target_hair_mask) -# i_r = target_hair_mask * i_t + (target_hair_mask * (-1) + 1) * i_r -# i_r = i_r.clamp(-1, 1) -# i_r = trick.tensor_to_arr(i_r)[0] -# return i_r -# -# elif fs_model_name == 'simswap_official': -# from simswap.image_infer import SimSwapOfficialImageInfer -# fs_model = SimSwapOfficialImageInfer() -# pt_path = 'Simswap Official' -# mouth_net_param = { -# "use": False -# } -# -# @torch.no_grad() -# def infer_batch_to_img(i_s, i_t): -# i_r = fs_model.image_infer(source_tensor=i_s, target_tensor=i_t) -# i_r = i_r.clamp(-1, 1) -# return i_r -# -# else: -# raise ValueError('Not supported fs_model_name.') -# -# -# print(f'[demo] model loaded from {pt_path}') +make_abs_path = lambda fn: os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), fn)) + + +fs_model_name = 'faceshifter' +in_size = 256 + +mouth_net_param = { + "use": True, + "feature_dim": 128, + "crop_param": (28, 56, 84, 112), + "weight_path": "../../modules/third_party/arcface/weights/mouth_net_28_56_84_112.pth", +} +trick = Trick() + +T = transforms.Compose( + [ + transforms.ToTensor(), + transforms.Normalize(0.5, 0.5), + ] + ) +tensor2pil_transform = transforms.ToPILImage() + + +def extract_generator(ckpt: str, pt: str): + print(f'[extract_generator] loading ckpt...') + from trainer.faceshifter.faceshifter_pl import FaceshifterPL512, FaceshifterPL + import yaml + with open(make_abs_path('../../trainer/faceshifter/config.yaml'), 'r') as f: + config = yaml.load(f, Loader=yaml.FullLoader) + config['mouth_net'] = mouth_net_param + + if in_size == 256: + net = FaceshifterPL(n_layers=3, num_D=3, config=config) + elif in_size == 512: + net = FaceshifterPL512(n_layers=3, num_D=3, config=config, verbose=False) + else: + raise ValueError('Not supported in_size.') + checkpoint = torch.load(ckpt, map_location="cpu", ) + net.load_state_dict(checkpoint["state_dict"], strict=False) + net.eval() + + G = net.generator + torch.save(G.state_dict(), pt) + print(f'[extract_generator] extracted from {ckpt}, pth saved to {pt}') + + +''' load model ''' +if fs_model_name == 'faceshifter': + pt_path = make_abs_path("./weights/extracted/G_mouth1_t38.pth") + # pt_path = make_abs_path("../ffplus/extracted_ckpt/G_mouth1_t512_6.pth") + # ckpt_path = "/apdcephfs/share_1290939/gavinyuan/out/triplet512_6/epoch=3-step=128999.ckpt" + # pt_path = make_abs_path("../ffplus/extracted_ckpt/G_mouth1_t512_4.pth") + # ckpt_path = "/apdcephfs/share_1290939/gavinyuan/out/triplet512_4/epoch=2-step=185999.ckpt" + if not os.path.exists(pt_path) or 't512' in pt_path: + extract_generator(ckpt_path, pt_path) + fs_model = FSGenerator( + make_abs_path("./weights/arcface/ms1mv3_arcface_r100_fp16/backbone.pth"), + mouth_net_param=mouth_net_param, + in_size=in_size, + downup=in_size == 512, + ) + fs_model.load_state_dict(torch.load(pt_path, "cpu"), strict=True) + fs_model.eval() + + @torch.no_grad() + def infer_batch_to_img(i_s, i_t, post: bool = False): + i_r = fs_model(i_s, i_t)[0] # x, id_vector, att + + if post: + target_hair_mask = trick.get_any_mask(i_t, par=[0, 17]) + target_hair_mask = trick.smooth_mask(target_hair_mask) + i_r = target_hair_mask * i_t + (target_hair_mask * (-1) + 1) * i_r + i_r = trick.finetune_mouth(i_s, i_t, i_r) if in_size == 256 else i_r + + img_r = trick.tensor_to_arr(i_r)[0] + return img_r + +elif fs_model_name == 'simswap_triplet' or fs_model_name == 'simswap_vanilla': + from modules.networks.simswap import Generator_Adain_Upsample + sw_model = Generator_Adain_Upsample( + input_nc=3, output_nc=3, latent_size=512, n_blocks=9, deep=False, + mouth_net_param=mouth_net_param + ) + if fs_model_name == 'simswap_triplet': + pt_path = make_abs_path("../ffplus/extracted_ckpt/G_mouth1_st5.pth") + ckpt_path = make_abs_path("/apdcephfs/share_1290939/gavinyuan/out/" + "simswap_triplet_5/epoch=12-step=782999.ckpt") + elif fs_model_name == 'simswap_vanilla': + pt_path = make_abs_path("../ffplus/extracted_ckpt/G_tmp_sv4_off.pth") + ckpt_path = make_abs_path("/apdcephfs/share_1290939/gavinyuan/out/" + "simswap_vanilla_4/epoch=694-step=1487999.ckpt") + else: + pt_path = None + ckpt_path = None + sw_model.load_state_dict(torch.load(pt_path, "cpu"), strict=False) + sw_model.eval() + fs_model = sw_model + + from trainer.simswap.simswap_pl import SimSwapPL + import yaml + with open(make_abs_path('../../trainer/simswap/config.yaml'), 'r') as f: + config = yaml.load(f, Loader=yaml.FullLoader) + config['mouth_net'] = mouth_net_param + net = SimSwapPL(config=config, use_official_arc='off' in pt_path) + + checkpoint = torch.load(ckpt_path, map_location="cpu") + net.load_state_dict(checkpoint["state_dict"], strict=False) + net.eval() + sw_mouth_net = net.mouth_net # maybe None + sw_netArc = net.netArc + fs_model = fs_model.cuda() + sw_mouth_net = sw_mouth_net.cuda() if sw_mouth_net is not None else sw_mouth_net + sw_netArc = sw_netArc.cuda() + + @torch.no_grad() + def infer_batch_to_img(i_s, i_t, post: bool = False): + i_r = fs_model(source=i_s, target=i_t, net_arc=sw_netArc, mouth_net=sw_mouth_net,) + if post: + target_hair_mask = trick.get_any_mask(i_t, par=[0, 17]) + target_hair_mask = trick.smooth_mask(target_hair_mask) + i_r = target_hair_mask * i_t + (target_hair_mask * (-1) + 1) * i_r + i_r = i_r.clamp(-1, 1) + i_r = trick.tensor_to_arr(i_r)[0] + return i_r + +elif fs_model_name == 'simswap_official': + from simswap.image_infer import SimSwapOfficialImageInfer + fs_model = SimSwapOfficialImageInfer() + pt_path = 'Simswap Official' + mouth_net_param = { + "use": False + } + + @torch.no_grad() + def infer_batch_to_img(i_s, i_t): + i_r = fs_model.image_infer(source_tensor=i_s, target_tensor=i_t) + i_r = i_r.clamp(-1, 1) + return i_r + +else: + raise ValueError('Not supported fs_model_name.') + + +print(f'[demo] model loaded from {pt_path}') def swap_image( @@ -435,6 +435,7 @@ def swap_video_gr(img1, target_path, use_gpu=True, frames=9999999): if __name__ == "__main__": + use_gpu = torch.cuda.is_available() with gr.Blocks() as demo: gr.Markdown("SuperSwap") @@ -458,12 +459,12 @@ if __name__ == "__main__": video_button = gr.Button("换脸") image_button.click( swap_image_gr, - inputs=[image1_input, image2_input, use_post, use_gpen], + inputs=[image1_input, image2_input, use_post, use_gpen, use_gpu], outputs=image_output, ) video_button.click( swap_video_gr, - inputs=[image3_input, video_input], + inputs=[image3_input, video_input, use_gpu], outputs=video_output, ) diff --git a/inference/alignment.py b/inference/alignment.py new file mode 100644 index 0000000000000000000000000000000000000000..82b220785569e7a444899a8132f4e112e06eb288 --- /dev/null +++ b/inference/alignment.py @@ -0,0 +1,245 @@ +import cv2 +import numpy as np +from skimage import transform as trans + + +def get_center(points): + x = [p[0] for p in points] + y = [p[1] for p in points] + centroid = (sum(x) / len(points), sum(y) / len(points)) + return np.array([centroid]) + + +def extract_five_lmk(lmk): + x = lmk[..., :2] + left_eye = get_center(x[36:42]) + right_eye = get_center(x[42:48]) + nose = x[30:31] + left_mouth = x[48:49] + right_mouth = x[54:55] + x = np.concatenate([left_eye, right_eye, nose, left_mouth, right_mouth], axis=0) + return x + + +set1 = np.array( + [ + [41.125, 50.75], + [71.75, 49.4375], + [49.875, 73.0625], + [45.9375, 87.9375], + [70.4375, 87.9375], + ], + dtype=np.float32, +) + +arcface_src = np.array( + [ + [38.2946, 51.6963], + [73.5318, 51.5014], + [56.0252, 71.7366], + [41.5493, 92.3655], + [70.7299, 92.2041], + ], + dtype=np.float32, +) + + +ffhq = np.array( + [ + [192.98138, 239.94708], + [318.90277, 240.1936], + [256.63416, 314.01935], + [201.26117, 371.41043], + [313.08905, 371.15118], + ], + dtype=np.float32, +) + +mtcnn = np.array( + [ + [40.95041, 52.341854], + [70.90203, 52.17619], + [56.02142, 69.376114], + [43.716904, 86.910675], + [68.52042, 86.77348], + ], + dtype=np.float32, +) + +arcface_src = np.expand_dims(arcface_src, axis=0) +set1 = np.expand_dims(set1, axis=0) +ffhq = np.expand_dims(ffhq, axis=0) +mtcnn = np.expand_dims(mtcnn, axis=0) + + +# lmk is prediction; src is template +def estimate_norm(lmk, image_size=112, mode="set1"): + assert lmk.shape == (5, 2) + tform = trans.SimilarityTransform() + lmk_tran = np.insert(lmk, 2, values=np.ones(5), axis=1) + min_M = [] + min_index = [] + min_error = float("inf") + if mode == "arcface": + if image_size == 112: + src = arcface_src + else: + src = float(image_size) / 112 * arcface_src + elif mode == "set1": + if image_size == 112: + src = set1 + else: + src = float(image_size) / 112 * set1 + elif mode == "ffhq": + if image_size == 512: + src = ffhq + else: + src = float(image_size) / 512 * ffhq + elif mode == "mtcnn": + if image_size == 112: + src = mtcnn + else: + src = float(image_size) / 112 * mtcnn + else: + print("no mode like {}".format(mode)) + exit() + for i in np.arange(src.shape[0]): + tform.estimate(lmk, src[i]) + M = tform.params[0:2, :] + results = np.dot(M, lmk_tran.T) + results = results.T + error = np.sum(np.sqrt(np.sum((results - src[i]) ** 2, axis=1))) + # print(error) + if error < min_error: + min_error = error + min_M = M + min_index = i + return min_M, min_index + + +def estimate_norm_any(lmk_from, lmk_to, image_size=112): + tform = trans.SimilarityTransform() + lmk_tran = np.insert(lmk_from, 2, values=np.ones(5), axis=1) + min_M = [] + min_index = [] + min_error = float("inf") + src = lmk_to[np.newaxis, ...] + for i in np.arange(src.shape[0]): + tform.estimate(lmk_from, src[i]) + M = tform.params[0:2, :] + results = np.dot(M, lmk_tran.T) + results = results.T + error = np.sum(np.sqrt(np.sum((results - src[i]) ** 2, axis=1))) + # print(error) + if error < min_error: + min_error = error + min_M = M + min_index = i + return min_M, min_index + + +def norm_crop(img, landmark, image_size=112, mode="arcface", borderValue=0.0): + M, pose_index = estimate_norm(landmark, image_size, mode) + warped = cv2.warpAffine(img, M, (image_size, image_size), borderValue=borderValue) + return warped + + +def norm_crop_with_M(img, landmark, image_size=112, mode="arcface", borderValue=0.0): + M, pose_index = estimate_norm(landmark, image_size, mode) + warped = cv2.warpAffine(img, M, (image_size, image_size), borderValue=borderValue) + return warped, M + + +def square_crop(im, S): + if im.shape[0] > im.shape[1]: + height = S + width = int(float(im.shape[1]) / im.shape[0] * S) + scale = float(S) / im.shape[0] + else: + width = S + height = int(float(im.shape[0]) / im.shape[1] * S) + scale = float(S) / im.shape[1] + resized_im = cv2.resize(im, (width, height)) + det_im = np.zeros((S, S, 3), dtype=np.uint8) + det_im[: resized_im.shape[0], : resized_im.shape[1], :] = resized_im + return det_im, scale + + +def transform(data, center, output_size, scale, rotation): + scale_ratio = scale + rot = float(rotation) * np.pi / 180.0 + # translation = (output_size/2-center[0]*scale_ratio, output_size/2-center[1]*scale_ratio) + t1 = trans.SimilarityTransform(scale=scale_ratio) + cx = center[0] * scale_ratio + cy = center[1] * scale_ratio + t2 = trans.SimilarityTransform(translation=(-1 * cx, -1 * cy)) + t3 = trans.SimilarityTransform(rotation=rot) + t4 = trans.SimilarityTransform(translation=(output_size / 2, output_size / 2)) + t = t1 + t2 + t3 + t4 + M = t.params[0:2] + cropped = cv2.warpAffine(data, M, (output_size, output_size), borderValue=0.0) + return cropped, M + + +def trans_points2d(pts, M): + new_pts = np.zeros(shape=pts.shape, dtype=np.float32) + for i in range(pts.shape[0]): + pt = pts[i] + new_pt = np.array([pt[0], pt[1], 1.0], dtype=np.float32) + new_pt = np.dot(M, new_pt) + # print('new_pt', new_pt.shape, new_pt) + new_pts[i] = new_pt[0:2] + + return new_pts + + +def trans_points3d(pts, M): + scale = np.sqrt(M[0][0] * M[0][0] + M[0][1] * M[0][1]) + # print(scale) + new_pts = np.zeros(shape=pts.shape, dtype=np.float32) + for i in range(pts.shape[0]): + pt = pts[i] + new_pt = np.array([pt[0], pt[1], 1.0], dtype=np.float32) + new_pt = np.dot(M, new_pt) + # print('new_pt', new_pt.shape, new_pt) + new_pts[i][0:2] = new_pt[0:2] + new_pts[i][2] = pts[i][2] * scale + + return new_pts + + +def trans_points(pts, M): + if pts.shape[1] == 2: + return trans_points2d(pts, M) + else: + return trans_points3d(pts, M) + + +def paste_back(img, mat, ori_img): + mat_rev = np.zeros([2, 3]) + div1 = mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0] + mat_rev[0][0] = mat[1][1] / div1 + mat_rev[0][1] = -mat[0][1] / div1 + mat_rev[0][2] = -(mat[0][2] * mat[1][1] - mat[0][1] * mat[1][2]) / div1 + div2 = mat[0][1] * mat[1][0] - mat[0][0] * mat[1][1] + mat_rev[1][0] = mat[1][0] / div2 + mat_rev[1][1] = -mat[0][0] / div2 + mat_rev[1][2] = -(mat[0][2] * mat[1][0] - mat[0][0] * mat[1][2]) / div2 + + img_shape = (ori_img.shape[1], ori_img.shape[0]) + + img = cv2.warpAffine(img, mat_rev, img_shape) + img_white = np.full((256, 256), 255, dtype=float) + img_white = cv2.warpAffine(img_white, mat_rev, img_shape) + img_white[img_white > 20] = 255 + img_mask = img_white + kernel = np.ones((40, 40), np.uint8) + img_mask = cv2.erode(img_mask, kernel, iterations=2) + kernel_size = (20, 20) + blur_size = tuple(2 * j + 1 for j in kernel_size) + img_mask = cv2.GaussianBlur(img_mask, blur_size, 0) + img_mask /= 255 + img_mask = np.reshape(img_mask, [img_mask.shape[0], img_mask.shape[1], 1]) + ori_img = img_mask * img + (1 - img_mask) * ori_img + ori_img = ori_img.astype(np.uint8) + return ori_img diff --git a/inference/landmark_smooth.py b/inference/landmark_smooth.py new file mode 100644 index 0000000000000000000000000000000000000000..b5bda44989a353b1dc722e90a40efa1a254f70b2 --- /dev/null +++ b/inference/landmark_smooth.py @@ -0,0 +1,117 @@ +import cv2 +import numpy as np +from scipy.signal import savgol_filter + +def kalman_filter(inputs: np.array, + process_noise: float = 0.03, + measure_noise: float = 0.01, + ): + """ OpenCV - Kalman Filter + https://blog.csdn.net/angelfish91/article/details/61768575 + https://blog.csdn.net/qq_23981335/article/details/82968422 + """ + assert inputs.ndim == 2, "inputs should be 2-dim np.array" + + ''' + 它有3个输入参数, + dynam_params:状态空间的维数,这里为2; + measure_param:测量值的维数,这里也为2; + control_params:控制向量的维数,默认为0。由于这里该模型中并没有控制变量,因此也为0。 + ''' + kalman = cv2.KalmanFilter(2,2) + + kalman.measurementMatrix = np.array([[1,0],[0,1]],np.float32) + kalman.transitionMatrix = np.array([[1,0],[0,1]], np.float32) + kalman.processNoiseCov = np.array([[1,0],[0,1]], np.float32) * process_noise + kalman.measurementNoiseCov = np.array([[1,0],[0,1]], np.float32) * measure_noise + ''' + kalman.measurementNoiseCov为测量系统的协方差矩阵,方差越小,预测结果越接近测量值, + kalman.processNoiseCov为模型系统的噪声,噪声越大,预测结果越不稳定,越容易接近模型系统预测值,且单步变化越大, + 相反,若噪声小,则预测结果与上个计算结果相差不大。 + ''' + + kalman.statePre = np.array([[inputs[0][0]], + [inputs[0][1]]]) + + ''' + Kalman Filtering + ''' + outputs = np.zeros_like(inputs) + for i in range(len(inputs)): + mes = np.reshape(inputs[i,:],(2,1)) + + x = kalman.correct(mes) + + y = kalman.predict() + outputs[i] = np.squeeze(y) + # print (kalman.statePost[0],kalman.statePost[1]) + # print (kalman.statePre[0],kalman.statePre[1]) + # print ('measurement:\t',mes[0],mes[1]) + # print ('correct:\t',x[0],x[1]) + # print ('predict:\t',y[0],y[1]) + # print ('='*30) + + return outputs + + +def kalman_filter_landmark(landmarks: np.array, + process_noise: float = 0.03, + measure_noise: float = 0.01, + ): + """ Kalman Filter for Landmarks + :param process_noise: large means unstable and close to model predictions + :param measure_noise: small means close to measurement + """ + print('[Using Kalman Filter for Landmark Smoothing, process_noise=%f, measure_noise=%f]' % + (process_noise, measure_noise)) + + ''' + landmarks: (#frames, key, xy) + ''' + assert landmarks.ndim == 3, 'landmarks should be 3-dim np.array' + assert landmarks.dtype == 'float32', 'landmarks dtype should be float32' + + for s1 in range(landmarks.shape[1]): + landmarks[:, s1] = kalman_filter(landmarks[:, s1], + process_noise, + measure_noise) + return landmarks + + +def savgol_filter_landmark(landmarks: np.array, + window_length: int = 25, + poly_order: int = 2, + ): + """ Savgol Filter for Landmarks + https://blog.csdn.net/kaever/article/details/105520941 + """ + print('[Using Savgol Filter for Landmark Smoothing, window_length=%d, poly_order=%d]' % + (window_length, poly_order)) + + ''' + landmarks: (#frames, key, xy) + ''' + assert landmarks.ndim == 3, 'landmarks should be 3-dim np.array' + assert landmarks.dtype == 'float32', 'landmarks dtype should be float32' + assert window_length % 2 == 1, 'window_length should be odd' + + for s1 in range(landmarks.shape[1]): + for s2 in range(landmarks.shape[2]): + landmarks[:, s1, s2] = savgol_filter(landmarks[:, s1, s2], + window_length, + poly_order) + return landmarks + +if __name__ == '__main__': + + pos = np.array([ + [10, 50], + [12, 49], + [11, 52], + [13, 52.2], + [12.9, 50]], np.float32) + + print(pos) + pos_filtered = kalman_filter(pos) + print(pos) + print(pos_filtered) \ No newline at end of file diff --git a/inference/tricks.py b/inference/tricks.py index ed90a15ba68783fbdd6f792113edc10fa9f85e9c..7e7d3b63e3aabe255deb0f4e53ca9eba2652a325 100644 --- a/inference/tricks.py +++ b/inference/tricks.py @@ -74,7 +74,7 @@ class Trick(object): if not use_gpen: return img_np if self.gpen_model is None: - self.gpen_model = GPENImageInfer() + self.gpen_model = GPENImageInfer(device=global_device) img_np = self.gpen_model.image_infer(img_np) return img_np @@ -139,22 +139,22 @@ class SoftErosion(nn.Module): if torch.cuda.is_available(): - device = torch.device(0) + global_device = torch.device(0) else: - device = torch.device('cpu') + global_device = torch.device('cpu') vgg_mean = torch.tensor([[[0.485]], [[0.456]], [[0.406]]], - requires_grad=False, device=device) + requires_grad=False, device=global_device) vgg_std = torch.tensor([[[0.229]], [[0.224]], [[0.225]]], - requires_grad=False, device=device) + requires_grad=False, device=global_device) def load_bisenet(): bisenet_model = BiSeNet(n_classes=19) bisenet_model.load_state_dict( - torch.load(make_abs_path("../weights/79999_iter.pth",), map_location="cpu") + torch.load(make_abs_path("../weights/bisenet/79999_iter.pth",), map_location="cpu") ) bisenet_model.eval() - bisenet_model = bisenet_model.to(device) + bisenet_model = bisenet_model.to(global_device) - smooth_mask = SoftErosion(kernel_size=17, threshold=0.9, iterations=7).to(device) + smooth_mask = SoftErosion(kernel_size=17, threshold=0.9, iterations=7).to(global_device) print('[Global] bisenet loaded.') return bisenet_model, smooth_mask diff --git a/inference/utils.py b/inference/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..3ecb1783a16e62e65b55bc6a8921e2414cf5d8d8 --- /dev/null +++ b/inference/utils.py @@ -0,0 +1,133 @@ +import numpy as np +import cv2 +from PIL import Image +# from TDDFA_V2.FaceBoxes import FaceBoxes +# from TDDFA_V2.TDDFA import TDDFA + +def get_5_from_98(lmk): + lefteye = (lmk[60] + lmk[64] + lmk[96]) / 3 # lmk[96] + righteye = (lmk[68] + lmk[72] + lmk[97]) / 3 # lmk[97] + nose = lmk[54] + leftmouth = lmk[76] + rightmouth = lmk[82] + return np.array([lefteye, righteye, nose, leftmouth, rightmouth]) + + +def get_center(points): + x = [p[0] for p in points] + y = [p[1] for p in points] + centroid = (sum(x) / len(points), sum(y) / len(points)) + return np.array([centroid]) + + +def get_lmk(img, tddfa, face_boxes): + # 仅接受一个人的图像 + boxes = face_boxes(img) + n = len(boxes) + if n < 1: + return None + param_lst, roi_box_lst = tddfa(img, boxes) + ver_lst = tddfa.recon_vers(param_lst, roi_box_lst, dense_flag=False) + x = ver_lst[0].transpose(1, 0)[..., :2] + left_eye = get_center(x[36:42]) + right_eye = get_center(x[42:48]) + nose = x[30:31] + left_mouth = x[48:49] + right_mouth = x[54:55] + x = np.concatenate([left_eye, right_eye, nose, left_mouth, right_mouth], axis=0) + return x + + +def get_landmark_once(img, gpu_mode=False): + tddfa = TDDFA( + gpu_mode=gpu_mode, + arch="resnet", + checkpoint_fp="./TDDFA_V2/weights/resnet22.pth", + bfm_fp="TDDFA_V2/configs/bfm_noneck_v3.pkl", + size=120, + num_params=62, + ) + face_boxes = FaceBoxes() + boxes = face_boxes(img) + n = len(boxes) + if n < 1: + return None + param_lst, roi_box_lst = tddfa(img, boxes) + ver_lst = tddfa.recon_vers(param_lst, roi_box_lst, dense_flag=False) + x = ver_lst[0].transpose(1, 0)[..., :2] + left_eye = get_center(x[36:42]) + right_eye = get_center(x[42:48]) + nose = x[30:31] + left_mouth = x[48:49] + right_mouth = x[54:55] + x = np.concatenate([left_eye, right_eye, nose, left_mouth, right_mouth], axis=0) + return x + + +def get_detector(gpu_mode=False): + tddfa = TDDFA( + gpu_mode=gpu_mode, + arch="resnet", + checkpoint_fp="./TDDFA_V2/weights/resnet22.pth", + bfm_fp="TDDFA_V2/configs/bfm_noneck_v3.pkl", + size=120, + num_params=62, + ) + face_boxes = FaceBoxes() + return tddfa, face_boxes + + +def save(x, trick=None, use_post=False): + """ Paste img to ori_img """ + img, mat, ori_img, save_path, img_mask = x + if mat is None: + print('[Warning] mat is None.') + ori_img = ori_img.astype(np.uint8) + Image.fromarray(ori_img).save(save_path) + return + + H, W = img.shape[0], img.shape[1] # (256,256) or (512,512) + mat_rev = np.zeros([2, 3]) + div1 = mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0] + mat_rev[0][0] = mat[1][1] / div1 + mat_rev[0][1] = -mat[0][1] / div1 + mat_rev[0][2] = -(mat[0][2] * mat[1][1] - mat[0][1] * mat[1][2]) / div1 + div2 = mat[0][1] * mat[1][0] - mat[0][0] * mat[1][1] + mat_rev[1][0] = mat[1][0] / div2 + mat_rev[1][1] = -mat[0][0] / div2 + mat_rev[1][2] = -(mat[0][2] * mat[1][0] - mat[0][0] * mat[1][2]) / div2 + + img_shape = (ori_img.shape[1], ori_img.shape[0]) # (h,w) + + img = cv2.warpAffine(img, mat_rev, img_shape) + + if img_mask is None: + ''' hanbang version of paste masks ''' + img_white = np.full((H, W), 255, dtype=float) + img_white = cv2.warpAffine(img_white, mat_rev, img_shape) + img_white[img_white > 20] = 255 + img_mask = img_white + + kernel = np.ones((40, 40), np.uint8) + img_mask = cv2.erode(img_mask, kernel, iterations=2) + + kernel_size = (20, 20) + blur_size = tuple(2 * j + 1 for j in kernel_size) + img_mask = cv2.GaussianBlur(img_mask, blur_size, 0) + img_mask /= 255 + img_mask = np.reshape(img_mask, [img_mask.shape[0], img_mask.shape[1], 1]) + else: + ''' yuange version of paste masks ''' + img_mask = cv2.warpAffine(img_mask, mat_rev, img_shape) + img_mask = np.expand_dims(img_mask, axis=-1) + + ori_img = img_mask * img + (1 - img_mask) * ori_img + ori_img = ori_img.astype(np.uint8) + + if trick is not None: + ori_img = trick.gpen(ori_img, use_post) + + Image.fromarray(ori_img).save(save_path) + + # img_mask = np.array((img_mask * 255), dtype=np.uint8).squeeze() + # Image.fromarray(img_mask).save('img_mask.jpg') diff --git a/third_party/GPEN/infer_image.py b/third_party/GPEN/infer_image.py index 1660aa1f14ec9a62849002793940d38eda81c60f..953e9d3d97679fc631547b7cc2b36de64d67fb66 100644 --- a/third_party/GPEN/infer_image.py +++ b/third_party/GPEN/infer_image.py @@ -14,7 +14,7 @@ make_abs_path = lambda fn: os.path.abspath(os.path.join(os.path.dirname(os.path. class GPENImageInfer(object): - def __init__(self): + def __init__(self, device): super(GPENImageInfer, self).__init__() model = { @@ -32,6 +32,7 @@ class GPENImageInfer(object): model=model["name"], channel_multiplier=model["channel_multiplier"], narrow=model["narrow"], + device=device, ) self.faceenhancer = faceenhancer @@ -77,6 +78,7 @@ class GPENImageInfer(object): :return: out_batch: (N,RGB,H,W), in [-1,1] """ B, C, H, W = in_batch.shape + device = in_batch.device in_batch = ((in_batch + 1.) * 127.5).permute(0, 2, 3, 1) in_batch = in_batch.cpu().numpy().astype(np.uint8) # (N,H,W,RGB), in [0,255] @@ -89,7 +91,7 @@ class GPENImageInfer(object): out_batch[b_idx] = out_img[:, :, ::-1] if save_batch_idx is not None and b_idx == save_batch_idx: cv2.imwrite(os.path.join(save_folder, save_name), out_img) - out_batch = torch.FloatTensor(out_batch).cuda() + out_batch = torch.FloatTensor(out_batch).to(device) out_batch = out_batch / 127.5 - 1. # (N,H,W,RGB) out_batch = out_batch.permute(0, 3, 1, 2) # (N,RGB,H,W) out_batch = out_batch.clamp(-1, 1) diff --git a/third_party/PIPNet/FaceBoxesV2/detector.py b/third_party/PIPNet/FaceBoxesV2/detector.py new file mode 100644 index 0000000000000000000000000000000000000000..bb9c8fe988e05bb5d72103d6699ca8eac6de678f --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/detector.py @@ -0,0 +1,39 @@ +import cv2 + +class Detector(object): + def __init__(self, model_arch, model_weights): + self.model_arch = model_arch + self.model_weights = model_weights + + def detect(self, image, thresh): + raise NotImplementedError + + def crop(self, image, detections): + crops = [] + for det in detections: + xmin = max(det[2], 0) + ymin = max(det[3], 0) + width = det[4] + height = det[5] + xmax = min(xmin+width, image.shape[1]) + ymax = min(ymin+height, image.shape[0]) + cut = image[ymin:ymax, xmin:xmax,:] + crops.append(cut) + + return crops + + def draw(self, image, detections, im_scale=None): + if im_scale is not None: + image = cv2.resize(image, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR) + detections = [[det[0],det[1],int(det[2]*im_scale),int(det[3]*im_scale),int(det[4]*im_scale),int(det[5]*im_scale)] for det in detections] + + for det in detections: + xmin = det[2] + ymin = det[3] + width = det[4] + height = det[5] + xmax = xmin + width + ymax = ymin + height + cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2) + + return image diff --git a/third_party/PIPNet/FaceBoxesV2/faceboxes_detector.py b/third_party/PIPNet/FaceBoxesV2/faceboxes_detector.py new file mode 100644 index 0000000000000000000000000000000000000000..b953b85ce20424025e3127fe60f7f9021078da87 --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/faceboxes_detector.py @@ -0,0 +1,124 @@ +from third_party.PIPNet.FaceBoxesV2.detector import Detector +import cv2, os +import numpy as np +import torch +import torch.nn as nn +from third_party.PIPNet.FaceBoxesV2.utils.config import cfg +from third_party.PIPNet.FaceBoxesV2.utils.prior_box import PriorBox +from third_party.PIPNet.FaceBoxesV2.utils.nms_wrapper import nms +from third_party.PIPNet.FaceBoxesV2.utils.faceboxes import FaceBoxesV2 +from third_party.PIPNet.FaceBoxesV2.utils.box_utils import decode +import time + + +class FaceBoxesDetector(Detector): + def __init__(self, model_arch, model_weights, use_gpu, device): + super().__init__(model_arch, model_weights) + self.name = "FaceBoxesDetector" + self.net = FaceBoxesV2( + phase="test", size=None, num_classes=2 + ) # initialize detector + self.use_gpu = use_gpu + self.device = device + + state_dict = torch.load(self.model_weights, map_location=self.device) + # create new OrderedDict that does not contain `module.` + from collections import OrderedDict + + new_state_dict = OrderedDict() + for k, v in state_dict.items(): + name = k[7:] # remove `module.` + new_state_dict[name] = v + # load params + self.net.load_state_dict(new_state_dict) + self.net = self.net.to(self.device) + self.net.eval() + + def detect(self, image, thresh=0.6, im_scale=None): + # auto resize for large images + if im_scale is None: + height, width, _ = image.shape + if min(height, width) > 600: + im_scale = 600.0 / min(height, width) + else: + im_scale = 1 + image_scale = cv2.resize( + image, None, None, fx=im_scale, fy=im_scale, interpolation=cv2.INTER_LINEAR + ) + + scale = torch.Tensor( + [ + image_scale.shape[1], + image_scale.shape[0], + image_scale.shape[1], + image_scale.shape[0], + ] + ) + image_scale = ( + torch.from_numpy(image_scale.transpose(2, 0, 1)).to(self.device).int() + ) + mean_tmp = torch.IntTensor([104, 117, 123]).to(self.device) + mean_tmp = mean_tmp.unsqueeze(1).unsqueeze(2) + image_scale -= mean_tmp + image_scale = image_scale.float().unsqueeze(0) + scale = scale.to(self.device) + + with torch.no_grad(): + out = self.net(image_scale) + # priorbox = PriorBox(cfg, out[2], (image_scale.size()[2], image_scale.size()[3]), phase='test') + priorbox = PriorBox( + cfg, image_size=(image_scale.size()[2], image_scale.size()[3]) + ) + priors = priorbox.forward() + priors = priors.to(self.device) + loc, conf = out + prior_data = priors.data + boxes = decode(loc.data.squeeze(0), prior_data, cfg["variance"]) + boxes = boxes * scale + boxes = boxes.cpu().numpy() + scores = conf.data.cpu().numpy()[:, 1] + + # ignore low scores + inds = np.where(scores > thresh)[0] + boxes = boxes[inds] + scores = scores[inds] + + # keep top-K before NMS + order = scores.argsort()[::-1][:5000] + boxes = boxes[order] + scores = scores[order] + + # do NMS + dets = np.hstack((boxes, scores[:, np.newaxis])).astype( + np.float32, copy=False + ) + keep = nms(dets, 0.3) + dets = dets[keep, :] + + dets = dets[:750, :] + detections_scale = [] + for i in range(dets.shape[0]): + xmin = int(dets[i][0]) + ymin = int(dets[i][1]) + xmax = int(dets[i][2]) + ymax = int(dets[i][3]) + score = dets[i][4] + width = xmax - xmin + height = ymax - ymin + detections_scale.append(["face", score, xmin, ymin, width, height]) + + # adapt bboxes to the original image size + if len(detections_scale) > 0: + detections_scale = [ + [ + det[0], + det[1], + int(det[2] / im_scale), + int(det[3] / im_scale), + int(det[4] / im_scale), + int(det[5] / im_scale), + ] + for det in detections_scale + ] + + return detections_scale, im_scale diff --git a/third_party/PIPNet/FaceBoxesV2/utils/__init__.py b/third_party/PIPNet/FaceBoxesV2/utils/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/third_party/PIPNet/FaceBoxesV2/utils/box_utils.py b/third_party/PIPNet/FaceBoxesV2/utils/box_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..83e13402e6f1520cb670b6c2e26f67860cb8b1f8 --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/box_utils.py @@ -0,0 +1,276 @@ +import torch +import numpy as np + + +def point_form(boxes): + """ Convert prior_boxes to (xmin, ymin, xmax, ymax) + representation for comparison to point form ground truth data. + Args: + boxes: (tensor) center-size default boxes from priorbox layers. + Return: + boxes: (tensor) Converted xmin, ymin, xmax, ymax form of boxes. + """ + return torch.cat((boxes[:, :2] - boxes[:, 2:]/2, # xmin, ymin + boxes[:, :2] + boxes[:, 2:]/2), 1) # xmax, ymax + + +def center_size(boxes): + """ Convert prior_boxes to (cx, cy, w, h) + representation for comparison to center-size form ground truth data. + Args: + boxes: (tensor) point_form boxes + Return: + boxes: (tensor) Converted xmin, ymin, xmax, ymax form of boxes. + """ + return torch.cat((boxes[:, 2:] + boxes[:, :2])/2, # cx, cy + boxes[:, 2:] - boxes[:, :2], 1) # w, h + + +def intersect(box_a, box_b): + """ We resize both tensors to [A,B,2] without new malloc: + [A,2] -> [A,1,2] -> [A,B,2] + [B,2] -> [1,B,2] -> [A,B,2] + Then we compute the area of intersect between box_a and box_b. + Args: + box_a: (tensor) bounding boxes, Shape: [A,4]. + box_b: (tensor) bounding boxes, Shape: [B,4]. + Return: + (tensor) intersection area, Shape: [A,B]. + """ + A = box_a.size(0) + B = box_b.size(0) + max_xy = torch.min(box_a[:, 2:].unsqueeze(1).expand(A, B, 2), + box_b[:, 2:].unsqueeze(0).expand(A, B, 2)) + min_xy = torch.max(box_a[:, :2].unsqueeze(1).expand(A, B, 2), + box_b[:, :2].unsqueeze(0).expand(A, B, 2)) + inter = torch.clamp((max_xy - min_xy), min=0) + return inter[:, :, 0] * inter[:, :, 1] + + +def jaccard(box_a, box_b): + """Compute the jaccard overlap of two sets of boxes. The jaccard overlap + is simply the intersection over union of two boxes. Here we operate on + ground truth boxes and default boxes. + E.g.: + A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B) + Args: + box_a: (tensor) Ground truth bounding boxes, Shape: [num_objects,4] + box_b: (tensor) Prior boxes from priorbox layers, Shape: [num_priors,4] + Return: + jaccard overlap: (tensor) Shape: [box_a.size(0), box_b.size(0)] + """ + inter = intersect(box_a, box_b) + area_a = ((box_a[:, 2]-box_a[:, 0]) * + (box_a[:, 3]-box_a[:, 1])).unsqueeze(1).expand_as(inter) # [A,B] + area_b = ((box_b[:, 2]-box_b[:, 0]) * + (box_b[:, 3]-box_b[:, 1])).unsqueeze(0).expand_as(inter) # [A,B] + union = area_a + area_b - inter + return inter / union # [A,B] + + +def matrix_iou(a, b): + """ + return iou of a and b, numpy version for data augenmentation + """ + lt = np.maximum(a[:, np.newaxis, :2], b[:, :2]) + rb = np.minimum(a[:, np.newaxis, 2:], b[:, 2:]) + + area_i = np.prod(rb - lt, axis=2) * (lt < rb).all(axis=2) + area_a = np.prod(a[:, 2:] - a[:, :2], axis=1) + area_b = np.prod(b[:, 2:] - b[:, :2], axis=1) + return area_i / (area_a[:, np.newaxis] + area_b - area_i) + + +def matrix_iof(a, b): + """ + return iof of a and b, numpy version for data augenmentation + """ + lt = np.maximum(a[:, np.newaxis, :2], b[:, :2]) + rb = np.minimum(a[:, np.newaxis, 2:], b[:, 2:]) + + area_i = np.prod(rb - lt, axis=2) * (lt < rb).all(axis=2) + area_a = np.prod(a[:, 2:] - a[:, :2], axis=1) + return area_i / np.maximum(area_a[:, np.newaxis], 1) + + +def match(threshold, truths, priors, variances, labels, loc_t, conf_t, idx): + """Match each prior box with the ground truth box of the highest jaccard + overlap, encode the bounding boxes, then return the matched indices + corresponding to both confidence and location preds. + Args: + threshold: (float) The overlap threshold used when mathing boxes. + truths: (tensor) Ground truth boxes, Shape: [num_obj, num_priors]. + priors: (tensor) Prior boxes from priorbox layers, Shape: [n_priors,4]. + variances: (tensor) Variances corresponding to each prior coord, + Shape: [num_priors, 4]. + labels: (tensor) All the class labels for the image, Shape: [num_obj]. + loc_t: (tensor) Tensor to be filled w/ endcoded location targets. + conf_t: (tensor) Tensor to be filled w/ matched indices for conf preds. + idx: (int) current batch index + Return: + The matched indices corresponding to 1)location and 2)confidence preds. + """ + # jaccard index + overlaps = jaccard( + truths, + point_form(priors) + ) + # (Bipartite Matching) + # [1,num_objects] best prior for each ground truth + best_prior_overlap, best_prior_idx = overlaps.max(1, keepdim=True) + + # ignore hard gt + valid_gt_idx = best_prior_overlap[:, 0] >= 0.2 + best_prior_idx_filter = best_prior_idx[valid_gt_idx, :] + if best_prior_idx_filter.shape[0] <= 0: + loc_t[idx] = 0 + conf_t[idx] = 0 + return + + # [1,num_priors] best ground truth for each prior + best_truth_overlap, best_truth_idx = overlaps.max(0, keepdim=True) + best_truth_idx.squeeze_(0) + best_truth_overlap.squeeze_(0) + best_prior_idx.squeeze_(1) + best_prior_idx_filter.squeeze_(1) + best_prior_overlap.squeeze_(1) + best_truth_overlap.index_fill_(0, best_prior_idx_filter, 2) # ensure best prior + # NoTODO refactor: index best_prior_idx with long tensor + # ensure every gt matches with its prior of max overlap + for j in range(best_prior_idx.size(0)): + best_truth_idx[best_prior_idx[j]] = j + matches = truths[best_truth_idx] # Shape: [num_priors,4] + conf = labels[best_truth_idx] # Shape: [num_priors] + conf[best_truth_overlap < threshold] = 0 # label as background + loc = encode(matches, priors, variances) + loc_t[idx] = loc # [num_priors,4] encoded offsets to learn + conf_t[idx] = conf # [num_priors] top class label for each prior + + +def encode(matched, priors, variances): + """Encode the variances from the priorbox layers into the ground truth boxes + we have matched (based on jaccard overlap) with the prior boxes. + Args: + matched: (tensor) Coords of ground truth for each prior in point-form + Shape: [num_priors, 4]. + priors: (tensor) Prior boxes in center-offset form + Shape: [num_priors,4]. + variances: (list[float]) Variances of priorboxes + Return: + encoded boxes (tensor), Shape: [num_priors, 4] + """ + + # dist b/t match center and prior's center + g_cxcy = (matched[:, :2] + matched[:, 2:])/2 - priors[:, :2] + # encode variance + g_cxcy /= (variances[0] * priors[:, 2:]) + # match wh / prior wh + g_wh = (matched[:, 2:] - matched[:, :2]) / priors[:, 2:] + g_wh = torch.log(g_wh) / variances[1] + # return target for smooth_l1_loss + return torch.cat([g_cxcy, g_wh], 1) # [num_priors,4] + + +# Adapted from https://github.com/Hakuyume/chainer-ssd +def decode(loc, priors, variances): + """Decode locations from predictions using priors to undo + the encoding we did for offset regression at train time. + Args: + loc (tensor): location predictions for loc layers, + Shape: [num_priors,4] + priors (tensor): Prior boxes in center-offset form. + Shape: [num_priors,4]. + variances: (list[float]) Variances of priorboxes + Return: + decoded bounding box predictions + """ + + boxes = torch.cat(( + priors[:, :2] + loc[:, :2] * variances[0] * priors[:, 2:], + priors[:, 2:] * torch.exp(loc[:, 2:] * variances[1])), 1) + boxes[:, :2] -= boxes[:, 2:] / 2 + boxes[:, 2:] += boxes[:, :2] + return boxes + + +def log_sum_exp(x): + """Utility function for computing log_sum_exp while determining + This will be used to determine unaveraged confidence loss across + all examples in a batch. + Args: + x (Variable(tensor)): conf_preds from conf layers + """ + x_max = x.data.max() + return torch.log(torch.sum(torch.exp(x-x_max), 1, keepdim=True)) + x_max + + +# Original author: Francisco Massa: +# https://github.com/fmassa/object-detection.torch +# Ported to PyTorch by Max deGroot (02/01/2017) +def nms(boxes, scores, overlap=0.5, top_k=200): + """Apply non-maximum suppression at test time to avoid detecting too many + overlapping bounding boxes for a given object. + Args: + boxes: (tensor) The location preds for the img, Shape: [num_priors,4]. + scores: (tensor) The class predscores for the img, Shape:[num_priors]. + overlap: (float) The overlap thresh for suppressing unnecessary boxes. + top_k: (int) The Maximum number of box preds to consider. + Return: + The indices of the kept boxes with respect to num_priors. + """ + + keep = torch.Tensor(scores.size(0)).fill_(0).long() + if boxes.numel() == 0: + return keep + x1 = boxes[:, 0] + y1 = boxes[:, 1] + x2 = boxes[:, 2] + y2 = boxes[:, 3] + area = torch.mul(x2 - x1, y2 - y1) + v, idx = scores.sort(0) # sort in ascending order + # I = I[v >= 0.01] + idx = idx[-top_k:] # indices of the top-k largest vals + xx1 = boxes.new() + yy1 = boxes.new() + xx2 = boxes.new() + yy2 = boxes.new() + w = boxes.new() + h = boxes.new() + + # keep = torch.Tensor() + count = 0 + while idx.numel() > 0: + i = idx[-1] # index of current largest val + # keep.append(i) + keep[count] = i + count += 1 + if idx.size(0) == 1: + break + idx = idx[:-1] # remove kept element from view + # load bboxes of next highest vals + torch.index_select(x1, 0, idx, out=xx1) + torch.index_select(y1, 0, idx, out=yy1) + torch.index_select(x2, 0, idx, out=xx2) + torch.index_select(y2, 0, idx, out=yy2) + # store element-wise max with next highest score + xx1 = torch.clamp(xx1, min=x1[i]) + yy1 = torch.clamp(yy1, min=y1[i]) + xx2 = torch.clamp(xx2, max=x2[i]) + yy2 = torch.clamp(yy2, max=y2[i]) + w.resize_as_(xx2) + h.resize_as_(yy2) + w = xx2 - xx1 + h = yy2 - yy1 + # check sizes of xx1 and xx2.. after each iteration + w = torch.clamp(w, min=0.0) + h = torch.clamp(h, min=0.0) + inter = w*h + # IoU = i / (area(a) + area(b) - i) + rem_areas = torch.index_select(area, 0, idx) # load remaining areas) + union = (rem_areas - inter) + area[i] + IoU = inter/union # store result in iou + # keep only elements with an IoU <= overlap + idx = idx[IoU.le(overlap)] + return keep, count + + diff --git a/third_party/PIPNet/FaceBoxesV2/utils/build.py b/third_party/PIPNet/FaceBoxesV2/utils/build.py new file mode 100644 index 0000000000000000000000000000000000000000..b1d4fb495db46eb5eb0b311d9bfbd5e111d49c56 --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/build.py @@ -0,0 +1,57 @@ +# coding: utf-8 + +# -------------------------------------------------------- +# Fast R-CNN +# Copyright (c) 2015 Microsoft +# Licensed under The MIT License [see LICENSE for details] +# Written by Ross Girshick +# -------------------------------------------------------- + +import os +from os.path import join as pjoin +import numpy as np +from distutils.core import setup +from distutils.extension import Extension +from Cython.Distutils import build_ext + + +def find_in_path(name, path): + "Find a file in a search path" + # adapted fom http://code.activestate.com/recipes/52224-find-a-file-given-a-search-path/ + for dir in path.split(os.pathsep): + binpath = pjoin(dir, name) + if os.path.exists(binpath): + return os.path.abspath(binpath) + return None + + +# Obtain the numpy include directory. This logic works across numpy versions. +try: + numpy_include = np.get_include() +except AttributeError: + numpy_include = np.get_numpy_include() + + +# run the customize_compiler +class custom_build_ext(build_ext): + def build_extensions(self): + # customize_compiler_for_nvcc(self.compiler) + build_ext.build_extensions(self) + + +ext_modules = [ + Extension( + "nms.cpu_nms", + ["nms/cpu_nms.pyx"], + # extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]}, + extra_compile_args=["-Wno-cpp", "-Wno-unused-function"], + include_dirs=[numpy_include] + ) +] + +setup( + name='mot_utils', + ext_modules=ext_modules, + # inject our custom trigger + cmdclass={'build_ext': custom_build_ext}, +) diff --git a/third_party/PIPNet/FaceBoxesV2/utils/config.py b/third_party/PIPNet/FaceBoxesV2/utils/config.py new file mode 100644 index 0000000000000000000000000000000000000000..527c8b3754fbc6e1187e4539bf5075cd0cea4952 --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/config.py @@ -0,0 +1,14 @@ +# config.py + +cfg = { + 'name': 'FaceBoxes', + #'min_dim': 1024, + #'feature_maps': [[32, 32], [16, 16], [8, 8]], + # 'aspect_ratios': [[1], [1], [1]], + 'min_sizes': [[32, 64, 128], [256], [512]], + 'steps': [32, 64, 128], + 'variance': [0.1, 0.2], + 'clip': False, + 'loc_weight': 2.0, + 'gpu_train': True +} diff --git a/third_party/PIPNet/FaceBoxesV2/utils/faceboxes.py b/third_party/PIPNet/FaceBoxesV2/utils/faceboxes.py new file mode 100644 index 0000000000000000000000000000000000000000..4ae4d31a7dfde983da5459c169a30e5a11ccdd7a --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/faceboxes.py @@ -0,0 +1,239 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F + + +class BasicConv2d(nn.Module): + + def __init__(self, in_channels, out_channels, **kwargs): + super(BasicConv2d, self).__init__() + self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs) + self.bn = nn.BatchNorm2d(out_channels, eps=1e-5) + + def forward(self, x): + x = self.conv(x) + x = self.bn(x) + return F.relu(x, inplace=True) + + +class Inception(nn.Module): + + def __init__(self): + super(Inception, self).__init__() + self.branch1x1 = BasicConv2d(128, 32, kernel_size=1, padding=0) + self.branch1x1_2 = BasicConv2d(128, 32, kernel_size=1, padding=0) + self.branch3x3_reduce = BasicConv2d(128, 24, kernel_size=1, padding=0) + self.branch3x3 = BasicConv2d(24, 32, kernel_size=3, padding=1) + self.branch3x3_reduce_2 = BasicConv2d(128, 24, kernel_size=1, padding=0) + self.branch3x3_2 = BasicConv2d(24, 32, kernel_size=3, padding=1) + self.branch3x3_3 = BasicConv2d(32, 32, kernel_size=3, padding=1) + + def forward(self, x): + branch1x1 = self.branch1x1(x) + + branch1x1_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1) + branch1x1_2 = self.branch1x1_2(branch1x1_pool) + + branch3x3_reduce = self.branch3x3_reduce(x) + branch3x3 = self.branch3x3(branch3x3_reduce) + + branch3x3_reduce_2 = self.branch3x3_reduce_2(x) + branch3x3_2 = self.branch3x3_2(branch3x3_reduce_2) + branch3x3_3 = self.branch3x3_3(branch3x3_2) + + outputs = [branch1x1, branch1x1_2, branch3x3, branch3x3_3] + return torch.cat(outputs, 1) + + +class CRelu(nn.Module): + + def __init__(self, in_channels, out_channels, **kwargs): + super(CRelu, self).__init__() + self.conv = nn.Conv2d(in_channels, out_channels, bias=False, **kwargs) + self.bn = nn.BatchNorm2d(out_channels, eps=1e-5) + + def forward(self, x): + x = self.conv(x) + x = self.bn(x) + x = torch.cat([x, -x], 1) + x = F.relu(x, inplace=True) + return x + + +class FaceBoxes(nn.Module): + + def __init__(self, phase, size, num_classes): + super(FaceBoxes, self).__init__() + self.phase = phase + self.num_classes = num_classes + self.size = size + + self.conv1 = CRelu(3, 24, kernel_size=7, stride=4, padding=3) + self.conv2 = CRelu(48, 64, kernel_size=5, stride=2, padding=2) + + self.inception1 = Inception() + self.inception2 = Inception() + self.inception3 = Inception() + + self.conv3_1 = BasicConv2d(128, 128, kernel_size=1, stride=1, padding=0) + self.conv3_2 = BasicConv2d(128, 256, kernel_size=3, stride=2, padding=1) + + self.conv4_1 = BasicConv2d(256, 128, kernel_size=1, stride=1, padding=0) + self.conv4_2 = BasicConv2d(128, 256, kernel_size=3, stride=2, padding=1) + + self.loc, self.conf = self.multibox(self.num_classes) + + if self.phase == 'test': + self.softmax = nn.Softmax(dim=-1) + + if self.phase == 'train': + for m in self.modules(): + if isinstance(m, nn.Conv2d): + if m.bias is not None: + nn.init.xavier_normal_(m.weight.data) + m.bias.data.fill_(0.02) + else: + m.weight.data.normal_(0, 0.01) + elif isinstance(m, nn.BatchNorm2d): + m.weight.data.fill_(1) + m.bias.data.zero_() + + def multibox(self, num_classes): + loc_layers = [] + conf_layers = [] + loc_layers += [nn.Conv2d(128, 21 * 4, kernel_size=3, padding=1)] + conf_layers += [nn.Conv2d(128, 21 * num_classes, kernel_size=3, padding=1)] + loc_layers += [nn.Conv2d(256, 1 * 4, kernel_size=3, padding=1)] + conf_layers += [nn.Conv2d(256, 1 * num_classes, kernel_size=3, padding=1)] + loc_layers += [nn.Conv2d(256, 1 * 4, kernel_size=3, padding=1)] + conf_layers += [nn.Conv2d(256, 1 * num_classes, kernel_size=3, padding=1)] + return nn.Sequential(*loc_layers), nn.Sequential(*conf_layers) + + def forward(self, x): + + detection_sources = list() + loc = list() + conf = list() + + x = self.conv1(x) + x = F.max_pool2d(x, kernel_size=3, stride=2, padding=1) + x = self.conv2(x) + x = F.max_pool2d(x, kernel_size=3, stride=2, padding=1) + x = self.inception1(x) + x = self.inception2(x) + x = self.inception3(x) + detection_sources.append(x) + + x = self.conv3_1(x) + x = self.conv3_2(x) + detection_sources.append(x) + + x = self.conv4_1(x) + x = self.conv4_2(x) + detection_sources.append(x) + + for (x, l, c) in zip(detection_sources, self.loc, self.conf): + loc.append(l(x).permute(0, 2, 3, 1).contiguous()) + conf.append(c(x).permute(0, 2, 3, 1).contiguous()) + + loc = torch.cat([o.view(o.size(0), -1) for o in loc], 1) + conf = torch.cat([o.view(o.size(0), -1) for o in conf], 1) + + if self.phase == "test": + output = (loc.view(loc.size(0), -1, 4), + self.softmax(conf.view(conf.size(0), -1, self.num_classes))) + else: + output = (loc.view(loc.size(0), -1, 4), + conf.view(conf.size(0), -1, self.num_classes)) + + return output + +class FaceBoxesV2(nn.Module): + + def __init__(self, phase, size, num_classes): + super(FaceBoxesV2, self).__init__() + self.phase = phase + self.num_classes = num_classes + self.size = size + + self.conv1 = BasicConv2d(3, 8, kernel_size=3, stride=2, padding=1) + self.conv2 = BasicConv2d(8, 16, kernel_size=3, stride=2, padding=1) + self.conv3 = BasicConv2d(16, 32, kernel_size=3, stride=2, padding=1) + self.conv4 = BasicConv2d(32, 64, kernel_size=3, stride=2, padding=1) + self.conv5 = BasicConv2d(64, 128, kernel_size=3, stride=2, padding=1) + + self.inception1 = Inception() + self.inception2 = Inception() + self.inception3 = Inception() + + self.conv6_1 = BasicConv2d(128, 128, kernel_size=1, stride=1, padding=0) + self.conv6_2 = BasicConv2d(128, 256, kernel_size=3, stride=2, padding=1) + + self.conv7_1 = BasicConv2d(256, 128, kernel_size=1, stride=1, padding=0) + self.conv7_2 = BasicConv2d(128, 256, kernel_size=3, stride=2, padding=1) + + self.loc, self.conf = self.multibox(self.num_classes) + + if self.phase == 'test': + self.softmax = nn.Softmax(dim=-1) + + if self.phase == 'train': + for m in self.modules(): + if isinstance(m, nn.Conv2d): + if m.bias is not None: + nn.init.xavier_normal_(m.weight.data) + m.bias.data.fill_(0.02) + else: + m.weight.data.normal_(0, 0.01) + elif isinstance(m, nn.BatchNorm2d): + m.weight.data.fill_(1) + m.bias.data.zero_() + + def multibox(self, num_classes): + loc_layers = [] + conf_layers = [] + loc_layers += [nn.Conv2d(128, 21 * 4, kernel_size=3, padding=1)] + conf_layers += [nn.Conv2d(128, 21 * num_classes, kernel_size=3, padding=1)] + loc_layers += [nn.Conv2d(256, 1 * 4, kernel_size=3, padding=1)] + conf_layers += [nn.Conv2d(256, 1 * num_classes, kernel_size=3, padding=1)] + loc_layers += [nn.Conv2d(256, 1 * 4, kernel_size=3, padding=1)] + conf_layers += [nn.Conv2d(256, 1 * num_classes, kernel_size=3, padding=1)] + return nn.Sequential(*loc_layers), nn.Sequential(*conf_layers) + + def forward(self, x): + + sources = list() + loc = list() + conf = list() + + x = self.conv1(x) + x = self.conv2(x) + x = self.conv3(x) + x = self.conv4(x) + x = self.conv5(x) + x = self.inception1(x) + x = self.inception2(x) + x = self.inception3(x) + sources.append(x) + x = self.conv6_1(x) + x = self.conv6_2(x) + sources.append(x) + x = self.conv7_1(x) + x = self.conv7_2(x) + sources.append(x) + + for (x, l, c) in zip(sources, self.loc, self.conf): + loc.append(l(x).permute(0, 2, 3, 1).contiguous()) + conf.append(c(x).permute(0, 2, 3, 1).contiguous()) + + loc = torch.cat([o.view(o.size(0), -1) for o in loc], 1) + conf = torch.cat([o.view(o.size(0), -1) for o in conf], 1) + + if self.phase == "test": + output = (loc.view(loc.size(0), -1, 4), + self.softmax(conf.view(-1, self.num_classes))) + else: + output = (loc.view(loc.size(0), -1, 4), + conf.view(conf.size(0), -1, self.num_classes)) + + return output diff --git a/third_party/PIPNet/FaceBoxesV2/utils/make.sh b/third_party/PIPNet/FaceBoxesV2/utils/make.sh new file mode 100644 index 0000000000000000000000000000000000000000..9693ed462e516432e100cc743ae3a1417aa12c41 --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/make.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +python3 build.py build_ext --inplace + diff --git a/third_party/PIPNet/FaceBoxesV2/utils/nms/__init__.py b/third_party/PIPNet/FaceBoxesV2/utils/nms/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.c b/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.c new file mode 100644 index 0000000000000000000000000000000000000000..b97cdbdb569f25d576097d359355cad51baca901 --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.c @@ -0,0 +1,9830 @@ +/* Generated by Cython 0.29.24 */ + +#ifndef PY_SSIZE_T_CLEAN +#define PY_SSIZE_T_CLEAN +#endif /* PY_SSIZE_T_CLEAN */ +#include "Python.h" +#ifndef Py_PYTHON_H + #error Python headers needed to compile C extensions, please install development version of Python. +#elif PY_VERSION_HEX < 0x02060000 || (0x03000000 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x03030000) + #error Cython requires Python 2.6+ or Python 3.3+. +#else +#define CYTHON_ABI "0_29_24" +#define CYTHON_HEX_VERSION 0x001D18F0 +#define CYTHON_FUTURE_DIVISION 0 +#include +#ifndef offsetof + #define offsetof(type, member) ( (size_t) & ((type*)0) -> member ) +#endif +#if !defined(WIN32) && !defined(MS_WINDOWS) + #ifndef __stdcall + #define __stdcall + #endif + #ifndef __cdecl + #define __cdecl + #endif + #ifndef __fastcall + #define __fastcall + #endif +#endif +#ifndef DL_IMPORT + #define DL_IMPORT(t) t +#endif +#ifndef DL_EXPORT + #define DL_EXPORT(t) t +#endif +#define __PYX_COMMA , +#ifndef HAVE_LONG_LONG + #if PY_VERSION_HEX >= 0x02070000 + #define HAVE_LONG_LONG + #endif +#endif +#ifndef PY_LONG_LONG + #define PY_LONG_LONG LONG_LONG +#endif +#ifndef Py_HUGE_VAL + #define Py_HUGE_VAL HUGE_VAL +#endif +#ifdef PYPY_VERSION + #define CYTHON_COMPILING_IN_PYPY 1 + #define CYTHON_COMPILING_IN_PYSTON 0 + #define CYTHON_COMPILING_IN_CPYTHON 0 + #undef CYTHON_USE_TYPE_SLOTS + #define CYTHON_USE_TYPE_SLOTS 0 + #undef CYTHON_USE_PYTYPE_LOOKUP + #define CYTHON_USE_PYTYPE_LOOKUP 0 + #if PY_VERSION_HEX < 0x03050000 + #undef CYTHON_USE_ASYNC_SLOTS + #define CYTHON_USE_ASYNC_SLOTS 0 + #elif !defined(CYTHON_USE_ASYNC_SLOTS) + #define CYTHON_USE_ASYNC_SLOTS 1 + #endif + #undef CYTHON_USE_PYLIST_INTERNALS + #define CYTHON_USE_PYLIST_INTERNALS 0 + #undef CYTHON_USE_UNICODE_INTERNALS + #define CYTHON_USE_UNICODE_INTERNALS 0 + #undef CYTHON_USE_UNICODE_WRITER + #define CYTHON_USE_UNICODE_WRITER 0 + #undef CYTHON_USE_PYLONG_INTERNALS + #define CYTHON_USE_PYLONG_INTERNALS 0 + #undef CYTHON_AVOID_BORROWED_REFS + #define CYTHON_AVOID_BORROWED_REFS 1 + #undef CYTHON_ASSUME_SAFE_MACROS + #define CYTHON_ASSUME_SAFE_MACROS 0 + #undef CYTHON_UNPACK_METHODS + #define CYTHON_UNPACK_METHODS 0 + #undef CYTHON_FAST_THREAD_STATE + #define CYTHON_FAST_THREAD_STATE 0 + #undef CYTHON_FAST_PYCALL + #define CYTHON_FAST_PYCALL 0 + #undef CYTHON_PEP489_MULTI_PHASE_INIT + #define CYTHON_PEP489_MULTI_PHASE_INIT 0 + #undef CYTHON_USE_TP_FINALIZE + #define CYTHON_USE_TP_FINALIZE 0 + #undef CYTHON_USE_DICT_VERSIONS + #define CYTHON_USE_DICT_VERSIONS 0 + #undef CYTHON_USE_EXC_INFO_STACK + #define CYTHON_USE_EXC_INFO_STACK 0 +#elif defined(PYSTON_VERSION) + #define CYTHON_COMPILING_IN_PYPY 0 + #define CYTHON_COMPILING_IN_PYSTON 1 + #define CYTHON_COMPILING_IN_CPYTHON 0 + #ifndef CYTHON_USE_TYPE_SLOTS + #define CYTHON_USE_TYPE_SLOTS 1 + #endif + #undef CYTHON_USE_PYTYPE_LOOKUP + #define CYTHON_USE_PYTYPE_LOOKUP 0 + #undef CYTHON_USE_ASYNC_SLOTS + #define CYTHON_USE_ASYNC_SLOTS 0 + #undef CYTHON_USE_PYLIST_INTERNALS + #define CYTHON_USE_PYLIST_INTERNALS 0 + #ifndef CYTHON_USE_UNICODE_INTERNALS + #define CYTHON_USE_UNICODE_INTERNALS 1 + #endif + #undef CYTHON_USE_UNICODE_WRITER + #define CYTHON_USE_UNICODE_WRITER 0 + #undef CYTHON_USE_PYLONG_INTERNALS + #define CYTHON_USE_PYLONG_INTERNALS 0 + #ifndef CYTHON_AVOID_BORROWED_REFS + #define CYTHON_AVOID_BORROWED_REFS 0 + #endif + #ifndef CYTHON_ASSUME_SAFE_MACROS + #define CYTHON_ASSUME_SAFE_MACROS 1 + #endif + #ifndef CYTHON_UNPACK_METHODS + #define CYTHON_UNPACK_METHODS 1 + #endif + #undef CYTHON_FAST_THREAD_STATE + #define CYTHON_FAST_THREAD_STATE 0 + #undef CYTHON_FAST_PYCALL + #define CYTHON_FAST_PYCALL 0 + #undef CYTHON_PEP489_MULTI_PHASE_INIT + #define CYTHON_PEP489_MULTI_PHASE_INIT 0 + #undef CYTHON_USE_TP_FINALIZE + #define CYTHON_USE_TP_FINALIZE 0 + #undef CYTHON_USE_DICT_VERSIONS + #define CYTHON_USE_DICT_VERSIONS 0 + #undef CYTHON_USE_EXC_INFO_STACK + #define CYTHON_USE_EXC_INFO_STACK 0 +#else + #define CYTHON_COMPILING_IN_PYPY 0 + #define CYTHON_COMPILING_IN_PYSTON 0 + #define CYTHON_COMPILING_IN_CPYTHON 1 + #ifndef CYTHON_USE_TYPE_SLOTS + #define CYTHON_USE_TYPE_SLOTS 1 + #endif + #if PY_VERSION_HEX < 0x02070000 + #undef CYTHON_USE_PYTYPE_LOOKUP + #define CYTHON_USE_PYTYPE_LOOKUP 0 + #elif !defined(CYTHON_USE_PYTYPE_LOOKUP) + #define CYTHON_USE_PYTYPE_LOOKUP 1 + #endif + #if PY_MAJOR_VERSION < 3 + #undef CYTHON_USE_ASYNC_SLOTS + #define CYTHON_USE_ASYNC_SLOTS 0 + #elif !defined(CYTHON_USE_ASYNC_SLOTS) + #define CYTHON_USE_ASYNC_SLOTS 1 + #endif + #if PY_VERSION_HEX < 0x02070000 + #undef CYTHON_USE_PYLONG_INTERNALS + #define CYTHON_USE_PYLONG_INTERNALS 0 + #elif !defined(CYTHON_USE_PYLONG_INTERNALS) + #define CYTHON_USE_PYLONG_INTERNALS 1 + #endif + #ifndef CYTHON_USE_PYLIST_INTERNALS + #define CYTHON_USE_PYLIST_INTERNALS 1 + #endif + #ifndef CYTHON_USE_UNICODE_INTERNALS + #define CYTHON_USE_UNICODE_INTERNALS 1 + #endif + #if PY_VERSION_HEX < 0x030300F0 + #undef CYTHON_USE_UNICODE_WRITER + #define CYTHON_USE_UNICODE_WRITER 0 + #elif !defined(CYTHON_USE_UNICODE_WRITER) + #define CYTHON_USE_UNICODE_WRITER 1 + #endif + #ifndef CYTHON_AVOID_BORROWED_REFS + #define CYTHON_AVOID_BORROWED_REFS 0 + #endif + #ifndef CYTHON_ASSUME_SAFE_MACROS + #define CYTHON_ASSUME_SAFE_MACROS 1 + #endif + #ifndef CYTHON_UNPACK_METHODS + #define CYTHON_UNPACK_METHODS 1 + #endif + #ifndef CYTHON_FAST_THREAD_STATE + #define CYTHON_FAST_THREAD_STATE 1 + #endif + #ifndef CYTHON_FAST_PYCALL + #define CYTHON_FAST_PYCALL 1 + #endif + #ifndef CYTHON_PEP489_MULTI_PHASE_INIT + #define CYTHON_PEP489_MULTI_PHASE_INIT (PY_VERSION_HEX >= 0x03050000) + #endif + #ifndef CYTHON_USE_TP_FINALIZE + #define CYTHON_USE_TP_FINALIZE (PY_VERSION_HEX >= 0x030400a1) + #endif + #ifndef CYTHON_USE_DICT_VERSIONS + #define CYTHON_USE_DICT_VERSIONS (PY_VERSION_HEX >= 0x030600B1) + #endif + #ifndef CYTHON_USE_EXC_INFO_STACK + #define CYTHON_USE_EXC_INFO_STACK (PY_VERSION_HEX >= 0x030700A3) + #endif +#endif +#if !defined(CYTHON_FAST_PYCCALL) +#define CYTHON_FAST_PYCCALL (CYTHON_FAST_PYCALL && PY_VERSION_HEX >= 0x030600B1) +#endif +#if CYTHON_USE_PYLONG_INTERNALS + #include "longintrepr.h" + #undef SHIFT + #undef BASE + #undef MASK + #ifdef SIZEOF_VOID_P + enum { __pyx_check_sizeof_voidp = 1 / (int)(SIZEOF_VOID_P == sizeof(void*)) }; + #endif +#endif +#ifndef __has_attribute + #define __has_attribute(x) 0 +#endif +#ifndef __has_cpp_attribute + #define __has_cpp_attribute(x) 0 +#endif +#ifndef CYTHON_RESTRICT + #if defined(__GNUC__) + #define CYTHON_RESTRICT __restrict__ + #elif defined(_MSC_VER) && _MSC_VER >= 1400 + #define CYTHON_RESTRICT __restrict + #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + #define CYTHON_RESTRICT restrict + #else + #define CYTHON_RESTRICT + #endif +#endif +#ifndef CYTHON_UNUSED +# if defined(__GNUC__) +# if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) +# define CYTHON_UNUSED __attribute__ ((__unused__)) +# else +# define CYTHON_UNUSED +# endif +# elif defined(__ICC) || (defined(__INTEL_COMPILER) && !defined(_MSC_VER)) +# define CYTHON_UNUSED __attribute__ ((__unused__)) +# else +# define CYTHON_UNUSED +# endif +#endif +#ifndef CYTHON_MAYBE_UNUSED_VAR +# if defined(__cplusplus) + template void CYTHON_MAYBE_UNUSED_VAR( const T& ) { } +# else +# define CYTHON_MAYBE_UNUSED_VAR(x) (void)(x) +# endif +#endif +#ifndef CYTHON_NCP_UNUSED +# if CYTHON_COMPILING_IN_CPYTHON +# define CYTHON_NCP_UNUSED +# else +# define CYTHON_NCP_UNUSED CYTHON_UNUSED +# endif +#endif +#define __Pyx_void_to_None(void_result) ((void)(void_result), Py_INCREF(Py_None), Py_None) +#ifdef _MSC_VER + #ifndef _MSC_STDINT_H_ + #if _MSC_VER < 1300 + typedef unsigned char uint8_t; + typedef unsigned int uint32_t; + #else + typedef unsigned __int8 uint8_t; + typedef unsigned __int32 uint32_t; + #endif + #endif +#else + #include +#endif +#ifndef CYTHON_FALLTHROUGH + #if defined(__cplusplus) && __cplusplus >= 201103L + #if __has_cpp_attribute(fallthrough) + #define CYTHON_FALLTHROUGH [[fallthrough]] + #elif __has_cpp_attribute(clang::fallthrough) + #define CYTHON_FALLTHROUGH [[clang::fallthrough]] + #elif __has_cpp_attribute(gnu::fallthrough) + #define CYTHON_FALLTHROUGH [[gnu::fallthrough]] + #endif + #endif + #ifndef CYTHON_FALLTHROUGH + #if __has_attribute(fallthrough) + #define CYTHON_FALLTHROUGH __attribute__((fallthrough)) + #else + #define CYTHON_FALLTHROUGH + #endif + #endif + #if defined(__clang__ ) && defined(__apple_build_version__) + #if __apple_build_version__ < 7000000 + #undef CYTHON_FALLTHROUGH + #define CYTHON_FALLTHROUGH + #endif + #endif +#endif + +#ifndef CYTHON_INLINE + #if defined(__clang__) + #define CYTHON_INLINE __inline__ __attribute__ ((__unused__)) + #elif defined(__GNUC__) + #define CYTHON_INLINE __inline__ + #elif defined(_MSC_VER) + #define CYTHON_INLINE __inline + #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + #define CYTHON_INLINE inline + #else + #define CYTHON_INLINE + #endif +#endif + +#if CYTHON_COMPILING_IN_PYPY && PY_VERSION_HEX < 0x02070600 && !defined(Py_OptimizeFlag) + #define Py_OptimizeFlag 0 +#endif +#define __PYX_BUILD_PY_SSIZE_T "n" +#define CYTHON_FORMAT_SSIZE_T "z" +#if PY_MAJOR_VERSION < 3 + #define __Pyx_BUILTIN_MODULE_NAME "__builtin__" + #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\ + PyCode_New(a+k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) + #define __Pyx_DefaultClassType PyClass_Type +#else + #define __Pyx_BUILTIN_MODULE_NAME "builtins" +#if PY_VERSION_HEX >= 0x030800A4 && PY_VERSION_HEX < 0x030800B2 + #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\ + PyCode_New(a, 0, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) +#else + #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\ + PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) +#endif + #define __Pyx_DefaultClassType PyType_Type +#endif +#ifndef Py_TPFLAGS_CHECKTYPES + #define Py_TPFLAGS_CHECKTYPES 0 +#endif +#ifndef Py_TPFLAGS_HAVE_INDEX + #define Py_TPFLAGS_HAVE_INDEX 0 +#endif +#ifndef Py_TPFLAGS_HAVE_NEWBUFFER + #define Py_TPFLAGS_HAVE_NEWBUFFER 0 +#endif +#ifndef Py_TPFLAGS_HAVE_FINALIZE + #define Py_TPFLAGS_HAVE_FINALIZE 0 +#endif +#ifndef METH_STACKLESS + #define METH_STACKLESS 0 +#endif +#if PY_VERSION_HEX <= 0x030700A3 || !defined(METH_FASTCALL) + #ifndef METH_FASTCALL + #define METH_FASTCALL 0x80 + #endif + typedef PyObject *(*__Pyx_PyCFunctionFast) (PyObject *self, PyObject *const *args, Py_ssize_t nargs); + typedef PyObject *(*__Pyx_PyCFunctionFastWithKeywords) (PyObject *self, PyObject *const *args, + Py_ssize_t nargs, PyObject *kwnames); +#else + #define __Pyx_PyCFunctionFast _PyCFunctionFast + #define __Pyx_PyCFunctionFastWithKeywords _PyCFunctionFastWithKeywords +#endif +#if CYTHON_FAST_PYCCALL +#define __Pyx_PyFastCFunction_Check(func)\ + ((PyCFunction_Check(func) && (METH_FASTCALL == (PyCFunction_GET_FLAGS(func) & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_KEYWORDS | METH_STACKLESS))))) +#else +#define __Pyx_PyFastCFunction_Check(func) 0 +#endif +#if CYTHON_COMPILING_IN_PYPY && !defined(PyObject_Malloc) + #define PyObject_Malloc(s) PyMem_Malloc(s) + #define PyObject_Free(p) PyMem_Free(p) + #define PyObject_Realloc(p) PyMem_Realloc(p) +#endif +#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX < 0x030400A1 + #define PyMem_RawMalloc(n) PyMem_Malloc(n) + #define PyMem_RawRealloc(p, n) PyMem_Realloc(p, n) + #define PyMem_RawFree(p) PyMem_Free(p) +#endif +#if CYTHON_COMPILING_IN_PYSTON + #define __Pyx_PyCode_HasFreeVars(co) PyCode_HasFreeVars(co) + #define __Pyx_PyFrame_SetLineNumber(frame, lineno) PyFrame_SetLineNumber(frame, lineno) +#else + #define __Pyx_PyCode_HasFreeVars(co) (PyCode_GetNumFree(co) > 0) + #define __Pyx_PyFrame_SetLineNumber(frame, lineno) (frame)->f_lineno = (lineno) +#endif +#if !CYTHON_FAST_THREAD_STATE || PY_VERSION_HEX < 0x02070000 + #define __Pyx_PyThreadState_Current PyThreadState_GET() +#elif PY_VERSION_HEX >= 0x03060000 + #define __Pyx_PyThreadState_Current _PyThreadState_UncheckedGet() +#elif PY_VERSION_HEX >= 0x03000000 + #define __Pyx_PyThreadState_Current PyThreadState_GET() +#else + #define __Pyx_PyThreadState_Current _PyThreadState_Current +#endif +#if PY_VERSION_HEX < 0x030700A2 && !defined(PyThread_tss_create) && !defined(Py_tss_NEEDS_INIT) +#include "pythread.h" +#define Py_tss_NEEDS_INIT 0 +typedef int Py_tss_t; +static CYTHON_INLINE int PyThread_tss_create(Py_tss_t *key) { + *key = PyThread_create_key(); + return 0; +} +static CYTHON_INLINE Py_tss_t * PyThread_tss_alloc(void) { + Py_tss_t *key = (Py_tss_t *)PyObject_Malloc(sizeof(Py_tss_t)); + *key = Py_tss_NEEDS_INIT; + return key; +} +static CYTHON_INLINE void PyThread_tss_free(Py_tss_t *key) { + PyObject_Free(key); +} +static CYTHON_INLINE int PyThread_tss_is_created(Py_tss_t *key) { + return *key != Py_tss_NEEDS_INIT; +} +static CYTHON_INLINE void PyThread_tss_delete(Py_tss_t *key) { + PyThread_delete_key(*key); + *key = Py_tss_NEEDS_INIT; +} +static CYTHON_INLINE int PyThread_tss_set(Py_tss_t *key, void *value) { + return PyThread_set_key_value(*key, value); +} +static CYTHON_INLINE void * PyThread_tss_get(Py_tss_t *key) { + return PyThread_get_key_value(*key); +} +#endif +#if CYTHON_COMPILING_IN_CPYTHON || defined(_PyDict_NewPresized) +#define __Pyx_PyDict_NewPresized(n) ((n <= 8) ? PyDict_New() : _PyDict_NewPresized(n)) +#else +#define __Pyx_PyDict_NewPresized(n) PyDict_New() +#endif +#if PY_MAJOR_VERSION >= 3 || CYTHON_FUTURE_DIVISION + #define __Pyx_PyNumber_Divide(x,y) PyNumber_TrueDivide(x,y) + #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceTrueDivide(x,y) +#else + #define __Pyx_PyNumber_Divide(x,y) PyNumber_Divide(x,y) + #define __Pyx_PyNumber_InPlaceDivide(x,y) PyNumber_InPlaceDivide(x,y) +#endif +#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030500A1 && CYTHON_USE_UNICODE_INTERNALS +#define __Pyx_PyDict_GetItemStr(dict, name) _PyDict_GetItem_KnownHash(dict, name, ((PyASCIIObject *) name)->hash) +#else +#define __Pyx_PyDict_GetItemStr(dict, name) PyDict_GetItem(dict, name) +#endif +#if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND) + #define CYTHON_PEP393_ENABLED 1 + #if defined(PyUnicode_IS_READY) + #define __Pyx_PyUnicode_READY(op) (likely(PyUnicode_IS_READY(op)) ?\ + 0 : _PyUnicode_Ready((PyObject *)(op))) + #else + #define __Pyx_PyUnicode_READY(op) (0) + #endif + #define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_LENGTH(u) + #define __Pyx_PyUnicode_READ_CHAR(u, i) PyUnicode_READ_CHAR(u, i) + #define __Pyx_PyUnicode_MAX_CHAR_VALUE(u) PyUnicode_MAX_CHAR_VALUE(u) + #define __Pyx_PyUnicode_KIND(u) PyUnicode_KIND(u) + #define __Pyx_PyUnicode_DATA(u) PyUnicode_DATA(u) + #define __Pyx_PyUnicode_READ(k, d, i) PyUnicode_READ(k, d, i) + #define __Pyx_PyUnicode_WRITE(k, d, i, ch) PyUnicode_WRITE(k, d, i, ch) + #if defined(PyUnicode_IS_READY) && defined(PyUnicode_GET_SIZE) + #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x03090000 + #define __Pyx_PyUnicode_IS_TRUE(u) (0 != (likely(PyUnicode_IS_READY(u)) ? PyUnicode_GET_LENGTH(u) : ((PyCompactUnicodeObject *)(u))->wstr_length)) + #else + #define __Pyx_PyUnicode_IS_TRUE(u) (0 != (likely(PyUnicode_IS_READY(u)) ? PyUnicode_GET_LENGTH(u) : PyUnicode_GET_SIZE(u))) + #endif + #else + #define __Pyx_PyUnicode_IS_TRUE(u) (0 != PyUnicode_GET_LENGTH(u)) + #endif +#else + #define CYTHON_PEP393_ENABLED 0 + #define PyUnicode_1BYTE_KIND 1 + #define PyUnicode_2BYTE_KIND 2 + #define PyUnicode_4BYTE_KIND 4 + #define __Pyx_PyUnicode_READY(op) (0) + #define __Pyx_PyUnicode_GET_LENGTH(u) PyUnicode_GET_SIZE(u) + #define __Pyx_PyUnicode_READ_CHAR(u, i) ((Py_UCS4)(PyUnicode_AS_UNICODE(u)[i])) + #define __Pyx_PyUnicode_MAX_CHAR_VALUE(u) ((sizeof(Py_UNICODE) == 2) ? 65535 : 1114111) + #define __Pyx_PyUnicode_KIND(u) (sizeof(Py_UNICODE)) + #define __Pyx_PyUnicode_DATA(u) ((void*)PyUnicode_AS_UNICODE(u)) + #define __Pyx_PyUnicode_READ(k, d, i) ((void)(k), (Py_UCS4)(((Py_UNICODE*)d)[i])) + #define __Pyx_PyUnicode_WRITE(k, d, i, ch) (((void)(k)), ((Py_UNICODE*)d)[i] = ch) + #define __Pyx_PyUnicode_IS_TRUE(u) (0 != PyUnicode_GET_SIZE(u)) +#endif +#if CYTHON_COMPILING_IN_PYPY + #define __Pyx_PyUnicode_Concat(a, b) PyNumber_Add(a, b) + #define __Pyx_PyUnicode_ConcatSafe(a, b) PyNumber_Add(a, b) +#else + #define __Pyx_PyUnicode_Concat(a, b) PyUnicode_Concat(a, b) + #define __Pyx_PyUnicode_ConcatSafe(a, b) ((unlikely((a) == Py_None) || unlikely((b) == Py_None)) ?\ + PyNumber_Add(a, b) : __Pyx_PyUnicode_Concat(a, b)) +#endif +#if CYTHON_COMPILING_IN_PYPY && !defined(PyUnicode_Contains) + #define PyUnicode_Contains(u, s) PySequence_Contains(u, s) +#endif +#if CYTHON_COMPILING_IN_PYPY && !defined(PyByteArray_Check) + #define PyByteArray_Check(obj) PyObject_TypeCheck(obj, &PyByteArray_Type) +#endif +#if CYTHON_COMPILING_IN_PYPY && !defined(PyObject_Format) + #define PyObject_Format(obj, fmt) PyObject_CallMethod(obj, "__format__", "O", fmt) +#endif +#define __Pyx_PyString_FormatSafe(a, b) ((unlikely((a) == Py_None || (PyString_Check(b) && !PyString_CheckExact(b)))) ? PyNumber_Remainder(a, b) : __Pyx_PyString_Format(a, b)) +#define __Pyx_PyUnicode_FormatSafe(a, b) ((unlikely((a) == Py_None || (PyUnicode_Check(b) && !PyUnicode_CheckExact(b)))) ? PyNumber_Remainder(a, b) : PyUnicode_Format(a, b)) +#if PY_MAJOR_VERSION >= 3 + #define __Pyx_PyString_Format(a, b) PyUnicode_Format(a, b) +#else + #define __Pyx_PyString_Format(a, b) PyString_Format(a, b) +#endif +#if PY_MAJOR_VERSION < 3 && !defined(PyObject_ASCII) + #define PyObject_ASCII(o) PyObject_Repr(o) +#endif +#if PY_MAJOR_VERSION >= 3 + #define PyBaseString_Type PyUnicode_Type + #define PyStringObject PyUnicodeObject + #define PyString_Type PyUnicode_Type + #define PyString_Check PyUnicode_Check + #define PyString_CheckExact PyUnicode_CheckExact +#ifndef PyObject_Unicode + #define PyObject_Unicode PyObject_Str +#endif +#endif +#if PY_MAJOR_VERSION >= 3 + #define __Pyx_PyBaseString_Check(obj) PyUnicode_Check(obj) + #define __Pyx_PyBaseString_CheckExact(obj) PyUnicode_CheckExact(obj) +#else + #define __Pyx_PyBaseString_Check(obj) (PyString_Check(obj) || PyUnicode_Check(obj)) + #define __Pyx_PyBaseString_CheckExact(obj) (PyString_CheckExact(obj) || PyUnicode_CheckExact(obj)) +#endif +#ifndef PySet_CheckExact + #define PySet_CheckExact(obj) (Py_TYPE(obj) == &PySet_Type) +#endif +#if PY_VERSION_HEX >= 0x030900A4 + #define __Pyx_SET_REFCNT(obj, refcnt) Py_SET_REFCNT(obj, refcnt) + #define __Pyx_SET_SIZE(obj, size) Py_SET_SIZE(obj, size) +#else + #define __Pyx_SET_REFCNT(obj, refcnt) Py_REFCNT(obj) = (refcnt) + #define __Pyx_SET_SIZE(obj, size) Py_SIZE(obj) = (size) +#endif +#if CYTHON_ASSUME_SAFE_MACROS + #define __Pyx_PySequence_SIZE(seq) Py_SIZE(seq) +#else + #define __Pyx_PySequence_SIZE(seq) PySequence_Size(seq) +#endif +#if PY_MAJOR_VERSION >= 3 + #define PyIntObject PyLongObject + #define PyInt_Type PyLong_Type + #define PyInt_Check(op) PyLong_Check(op) + #define PyInt_CheckExact(op) PyLong_CheckExact(op) + #define PyInt_FromString PyLong_FromString + #define PyInt_FromUnicode PyLong_FromUnicode + #define PyInt_FromLong PyLong_FromLong + #define PyInt_FromSize_t PyLong_FromSize_t + #define PyInt_FromSsize_t PyLong_FromSsize_t + #define PyInt_AsLong PyLong_AsLong + #define PyInt_AS_LONG PyLong_AS_LONG + #define PyInt_AsSsize_t PyLong_AsSsize_t + #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask + #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask + #define PyNumber_Int PyNumber_Long +#endif +#if PY_MAJOR_VERSION >= 3 + #define PyBoolObject PyLongObject +#endif +#if PY_MAJOR_VERSION >= 3 && CYTHON_COMPILING_IN_PYPY + #ifndef PyUnicode_InternFromString + #define PyUnicode_InternFromString(s) PyUnicode_FromString(s) + #endif +#endif +#if PY_VERSION_HEX < 0x030200A4 + typedef long Py_hash_t; + #define __Pyx_PyInt_FromHash_t PyInt_FromLong + #define __Pyx_PyInt_AsHash_t PyInt_AsLong +#else + #define __Pyx_PyInt_FromHash_t PyInt_FromSsize_t + #define __Pyx_PyInt_AsHash_t PyInt_AsSsize_t +#endif +#if PY_MAJOR_VERSION >= 3 + #define __Pyx_PyMethod_New(func, self, klass) ((self) ? ((void)(klass), PyMethod_New(func, self)) : __Pyx_NewRef(func)) +#else + #define __Pyx_PyMethod_New(func, self, klass) PyMethod_New(func, self, klass) +#endif +#if CYTHON_USE_ASYNC_SLOTS + #if PY_VERSION_HEX >= 0x030500B1 + #define __Pyx_PyAsyncMethodsStruct PyAsyncMethods + #define __Pyx_PyType_AsAsync(obj) (Py_TYPE(obj)->tp_as_async) + #else + #define __Pyx_PyType_AsAsync(obj) ((__Pyx_PyAsyncMethodsStruct*) (Py_TYPE(obj)->tp_reserved)) + #endif +#else + #define __Pyx_PyType_AsAsync(obj) NULL +#endif +#ifndef __Pyx_PyAsyncMethodsStruct + typedef struct { + unaryfunc am_await; + unaryfunc am_aiter; + unaryfunc am_anext; + } __Pyx_PyAsyncMethodsStruct; +#endif + +#if defined(WIN32) || defined(MS_WINDOWS) + #define _USE_MATH_DEFINES +#endif +#include +#ifdef NAN +#define __PYX_NAN() ((float) NAN) +#else +static CYTHON_INLINE float __PYX_NAN() { + float value; + memset(&value, 0xFF, sizeof(value)); + return value; +} +#endif +#if defined(__CYGWIN__) && defined(_LDBL_EQ_DBL) +#define __Pyx_truncl trunc +#else +#define __Pyx_truncl truncl +#endif + +#define __PYX_MARK_ERR_POS(f_index, lineno) \ + { __pyx_filename = __pyx_f[f_index]; (void)__pyx_filename; __pyx_lineno = lineno; (void)__pyx_lineno; __pyx_clineno = __LINE__; (void)__pyx_clineno; } +#define __PYX_ERR(f_index, lineno, Ln_error) \ + { __PYX_MARK_ERR_POS(f_index, lineno) goto Ln_error; } + +#ifndef __PYX_EXTERN_C + #ifdef __cplusplus + #define __PYX_EXTERN_C extern "C" + #else + #define __PYX_EXTERN_C extern + #endif +#endif + +#define __PYX_HAVE__nms__cpu_nms +#define __PYX_HAVE_API__nms__cpu_nms +/* Early includes */ +#include +#include +#include "numpy/arrayobject.h" +#include "numpy/ndarrayobject.h" +#include "numpy/ndarraytypes.h" +#include "numpy/arrayscalars.h" +#include "numpy/ufuncobject.h" + + /* NumPy API declarations from "numpy/__init__.pxd" */ + +#ifdef _OPENMP +#include +#endif /* _OPENMP */ + +#if defined(PYREX_WITHOUT_ASSERTIONS) && !defined(CYTHON_WITHOUT_ASSERTIONS) +#define CYTHON_WITHOUT_ASSERTIONS +#endif + +typedef struct {PyObject **p; const char *s; const Py_ssize_t n; const char* encoding; + const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; + +#define __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 0 +#define __PYX_DEFAULT_STRING_ENCODING_IS_UTF8 0 +#define __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT (PY_MAJOR_VERSION >= 3 && __PYX_DEFAULT_STRING_ENCODING_IS_UTF8) +#define __PYX_DEFAULT_STRING_ENCODING "" +#define __Pyx_PyObject_FromString __Pyx_PyBytes_FromString +#define __Pyx_PyObject_FromStringAndSize __Pyx_PyBytes_FromStringAndSize +#define __Pyx_uchar_cast(c) ((unsigned char)c) +#define __Pyx_long_cast(x) ((long)x) +#define __Pyx_fits_Py_ssize_t(v, type, is_signed) (\ + (sizeof(type) < sizeof(Py_ssize_t)) ||\ + (sizeof(type) > sizeof(Py_ssize_t) &&\ + likely(v < (type)PY_SSIZE_T_MAX ||\ + v == (type)PY_SSIZE_T_MAX) &&\ + (!is_signed || likely(v > (type)PY_SSIZE_T_MIN ||\ + v == (type)PY_SSIZE_T_MIN))) ||\ + (sizeof(type) == sizeof(Py_ssize_t) &&\ + (is_signed || likely(v < (type)PY_SSIZE_T_MAX ||\ + v == (type)PY_SSIZE_T_MAX))) ) +static CYTHON_INLINE int __Pyx_is_valid_index(Py_ssize_t i, Py_ssize_t limit) { + return (size_t) i < (size_t) limit; +} +#if defined (__cplusplus) && __cplusplus >= 201103L + #include + #define __Pyx_sst_abs(value) std::abs(value) +#elif SIZEOF_INT >= SIZEOF_SIZE_T + #define __Pyx_sst_abs(value) abs(value) +#elif SIZEOF_LONG >= SIZEOF_SIZE_T + #define __Pyx_sst_abs(value) labs(value) +#elif defined (_MSC_VER) + #define __Pyx_sst_abs(value) ((Py_ssize_t)_abs64(value)) +#elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L + #define __Pyx_sst_abs(value) llabs(value) +#elif defined (__GNUC__) + #define __Pyx_sst_abs(value) __builtin_llabs(value) +#else + #define __Pyx_sst_abs(value) ((value<0) ? -value : value) +#endif +static CYTHON_INLINE const char* __Pyx_PyObject_AsString(PyObject*); +static CYTHON_INLINE const char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length); +#define __Pyx_PyByteArray_FromString(s) PyByteArray_FromStringAndSize((const char*)s, strlen((const char*)s)) +#define __Pyx_PyByteArray_FromStringAndSize(s, l) PyByteArray_FromStringAndSize((const char*)s, l) +#define __Pyx_PyBytes_FromString PyBytes_FromString +#define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize +static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char*); +#if PY_MAJOR_VERSION < 3 + #define __Pyx_PyStr_FromString __Pyx_PyBytes_FromString + #define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize +#else + #define __Pyx_PyStr_FromString __Pyx_PyUnicode_FromString + #define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize +#endif +#define __Pyx_PyBytes_AsWritableString(s) ((char*) PyBytes_AS_STRING(s)) +#define __Pyx_PyBytes_AsWritableSString(s) ((signed char*) PyBytes_AS_STRING(s)) +#define __Pyx_PyBytes_AsWritableUString(s) ((unsigned char*) PyBytes_AS_STRING(s)) +#define __Pyx_PyBytes_AsString(s) ((const char*) PyBytes_AS_STRING(s)) +#define __Pyx_PyBytes_AsSString(s) ((const signed char*) PyBytes_AS_STRING(s)) +#define __Pyx_PyBytes_AsUString(s) ((const unsigned char*) PyBytes_AS_STRING(s)) +#define __Pyx_PyObject_AsWritableString(s) ((char*) __Pyx_PyObject_AsString(s)) +#define __Pyx_PyObject_AsWritableSString(s) ((signed char*) __Pyx_PyObject_AsString(s)) +#define __Pyx_PyObject_AsWritableUString(s) ((unsigned char*) __Pyx_PyObject_AsString(s)) +#define __Pyx_PyObject_AsSString(s) ((const signed char*) __Pyx_PyObject_AsString(s)) +#define __Pyx_PyObject_AsUString(s) ((const unsigned char*) __Pyx_PyObject_AsString(s)) +#define __Pyx_PyObject_FromCString(s) __Pyx_PyObject_FromString((const char*)s) +#define __Pyx_PyBytes_FromCString(s) __Pyx_PyBytes_FromString((const char*)s) +#define __Pyx_PyByteArray_FromCString(s) __Pyx_PyByteArray_FromString((const char*)s) +#define __Pyx_PyStr_FromCString(s) __Pyx_PyStr_FromString((const char*)s) +#define __Pyx_PyUnicode_FromCString(s) __Pyx_PyUnicode_FromString((const char*)s) +static CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u) { + const Py_UNICODE *u_end = u; + while (*u_end++) ; + return (size_t)(u_end - u - 1); +} +#define __Pyx_PyUnicode_FromUnicode(u) PyUnicode_FromUnicode(u, __Pyx_Py_UNICODE_strlen(u)) +#define __Pyx_PyUnicode_FromUnicodeAndLength PyUnicode_FromUnicode +#define __Pyx_PyUnicode_AsUnicode PyUnicode_AsUnicode +#define __Pyx_NewRef(obj) (Py_INCREF(obj), obj) +#define __Pyx_Owned_Py_None(b) __Pyx_NewRef(Py_None) +static CYTHON_INLINE PyObject * __Pyx_PyBool_FromLong(long b); +static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*); +static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject*); +static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x); +#define __Pyx_PySequence_Tuple(obj)\ + (likely(PyTuple_CheckExact(obj)) ? __Pyx_NewRef(obj) : PySequence_Tuple(obj)) +static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*); +static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t); +#if CYTHON_ASSUME_SAFE_MACROS +#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x)) +#else +#define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x) +#endif +#define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x)) +#if PY_MAJOR_VERSION >= 3 +#define __Pyx_PyNumber_Int(x) (PyLong_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Long(x)) +#else +#define __Pyx_PyNumber_Int(x) (PyInt_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Int(x)) +#endif +#define __Pyx_PyNumber_Float(x) (PyFloat_CheckExact(x) ? __Pyx_NewRef(x) : PyNumber_Float(x)) +#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII +static int __Pyx_sys_getdefaultencoding_not_ascii; +static int __Pyx_init_sys_getdefaultencoding_params(void) { + PyObject* sys; + PyObject* default_encoding = NULL; + PyObject* ascii_chars_u = NULL; + PyObject* ascii_chars_b = NULL; + const char* default_encoding_c; + sys = PyImport_ImportModule("sys"); + if (!sys) goto bad; + default_encoding = PyObject_CallMethod(sys, (char*) "getdefaultencoding", NULL); + Py_DECREF(sys); + if (!default_encoding) goto bad; + default_encoding_c = PyBytes_AsString(default_encoding); + if (!default_encoding_c) goto bad; + if (strcmp(default_encoding_c, "ascii") == 0) { + __Pyx_sys_getdefaultencoding_not_ascii = 0; + } else { + char ascii_chars[128]; + int c; + for (c = 0; c < 128; c++) { + ascii_chars[c] = c; + } + __Pyx_sys_getdefaultencoding_not_ascii = 1; + ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL); + if (!ascii_chars_u) goto bad; + ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL); + if (!ascii_chars_b || !PyBytes_Check(ascii_chars_b) || memcmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) { + PyErr_Format( + PyExc_ValueError, + "This module compiled with c_string_encoding=ascii, but default encoding '%.200s' is not a superset of ascii.", + default_encoding_c); + goto bad; + } + Py_DECREF(ascii_chars_u); + Py_DECREF(ascii_chars_b); + } + Py_DECREF(default_encoding); + return 0; +bad: + Py_XDECREF(default_encoding); + Py_XDECREF(ascii_chars_u); + Py_XDECREF(ascii_chars_b); + return -1; +} +#endif +#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3 +#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL) +#else +#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL) +#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT +static char* __PYX_DEFAULT_STRING_ENCODING; +static int __Pyx_init_sys_getdefaultencoding_params(void) { + PyObject* sys; + PyObject* default_encoding = NULL; + char* default_encoding_c; + sys = PyImport_ImportModule("sys"); + if (!sys) goto bad; + default_encoding = PyObject_CallMethod(sys, (char*) (const char*) "getdefaultencoding", NULL); + Py_DECREF(sys); + if (!default_encoding) goto bad; + default_encoding_c = PyBytes_AsString(default_encoding); + if (!default_encoding_c) goto bad; + __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c) + 1); + if (!__PYX_DEFAULT_STRING_ENCODING) goto bad; + strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c); + Py_DECREF(default_encoding); + return 0; +bad: + Py_XDECREF(default_encoding); + return -1; +} +#endif +#endif + + +/* Test for GCC > 2.95 */ +#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))) + #define likely(x) __builtin_expect(!!(x), 1) + #define unlikely(x) __builtin_expect(!!(x), 0) +#else /* !__GNUC__ or GCC < 2.95 */ + #define likely(x) (x) + #define unlikely(x) (x) +#endif /* __GNUC__ */ +static CYTHON_INLINE void __Pyx_pretend_to_initialize(void* ptr) { (void)ptr; } + +static PyObject *__pyx_m = NULL; +static PyObject *__pyx_d; +static PyObject *__pyx_b; +static PyObject *__pyx_cython_runtime = NULL; +static PyObject *__pyx_empty_tuple; +static PyObject *__pyx_empty_bytes; +static PyObject *__pyx_empty_unicode; +static int __pyx_lineno; +static int __pyx_clineno = 0; +static const char * __pyx_cfilenm= __FILE__; +static const char *__pyx_filename; + +/* Header.proto */ +#if !defined(CYTHON_CCOMPLEX) + #if defined(__cplusplus) + #define CYTHON_CCOMPLEX 1 + #elif defined(_Complex_I) + #define CYTHON_CCOMPLEX 1 + #else + #define CYTHON_CCOMPLEX 0 + #endif +#endif +#if CYTHON_CCOMPLEX + #ifdef __cplusplus + #include + #else + #include + #endif +#endif +#if CYTHON_CCOMPLEX && !defined(__cplusplus) && defined(__sun__) && defined(__GNUC__) + #undef _Complex_I + #define _Complex_I 1.0fj +#endif + + +static const char *__pyx_f[] = { + "nms/cpu_nms.pyx", + "__init__.pxd", + "type.pxd", +}; +/* BufferFormatStructs.proto */ +#define IS_UNSIGNED(type) (((type) -1) > 0) +struct __Pyx_StructField_; +#define __PYX_BUF_FLAGS_PACKED_STRUCT (1 << 0) +typedef struct { + const char* name; + struct __Pyx_StructField_* fields; + size_t size; + size_t arraysize[8]; + int ndim; + char typegroup; + char is_unsigned; + int flags; +} __Pyx_TypeInfo; +typedef struct __Pyx_StructField_ { + __Pyx_TypeInfo* type; + const char* name; + size_t offset; +} __Pyx_StructField; +typedef struct { + __Pyx_StructField* field; + size_t parent_offset; +} __Pyx_BufFmt_StackElem; +typedef struct { + __Pyx_StructField root; + __Pyx_BufFmt_StackElem* head; + size_t fmt_offset; + size_t new_count, enc_count; + size_t struct_alignment; + int is_complex; + char enc_type; + char new_packmode; + char enc_packmode; + char is_valid_array; +} __Pyx_BufFmt_Context; + + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":690 + * # in Cython to enable them only on the right systems. + * + * ctypedef npy_int8 int8_t # <<<<<<<<<<<<<< + * ctypedef npy_int16 int16_t + * ctypedef npy_int32 int32_t + */ +typedef npy_int8 __pyx_t_5numpy_int8_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":691 + * + * ctypedef npy_int8 int8_t + * ctypedef npy_int16 int16_t # <<<<<<<<<<<<<< + * ctypedef npy_int32 int32_t + * ctypedef npy_int64 int64_t + */ +typedef npy_int16 __pyx_t_5numpy_int16_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":692 + * ctypedef npy_int8 int8_t + * ctypedef npy_int16 int16_t + * ctypedef npy_int32 int32_t # <<<<<<<<<<<<<< + * ctypedef npy_int64 int64_t + * #ctypedef npy_int96 int96_t + */ +typedef npy_int32 __pyx_t_5numpy_int32_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":693 + * ctypedef npy_int16 int16_t + * ctypedef npy_int32 int32_t + * ctypedef npy_int64 int64_t # <<<<<<<<<<<<<< + * #ctypedef npy_int96 int96_t + * #ctypedef npy_int128 int128_t + */ +typedef npy_int64 __pyx_t_5numpy_int64_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":697 + * #ctypedef npy_int128 int128_t + * + * ctypedef npy_uint8 uint8_t # <<<<<<<<<<<<<< + * ctypedef npy_uint16 uint16_t + * ctypedef npy_uint32 uint32_t + */ +typedef npy_uint8 __pyx_t_5numpy_uint8_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":698 + * + * ctypedef npy_uint8 uint8_t + * ctypedef npy_uint16 uint16_t # <<<<<<<<<<<<<< + * ctypedef npy_uint32 uint32_t + * ctypedef npy_uint64 uint64_t + */ +typedef npy_uint16 __pyx_t_5numpy_uint16_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":699 + * ctypedef npy_uint8 uint8_t + * ctypedef npy_uint16 uint16_t + * ctypedef npy_uint32 uint32_t # <<<<<<<<<<<<<< + * ctypedef npy_uint64 uint64_t + * #ctypedef npy_uint96 uint96_t + */ +typedef npy_uint32 __pyx_t_5numpy_uint32_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":700 + * ctypedef npy_uint16 uint16_t + * ctypedef npy_uint32 uint32_t + * ctypedef npy_uint64 uint64_t # <<<<<<<<<<<<<< + * #ctypedef npy_uint96 uint96_t + * #ctypedef npy_uint128 uint128_t + */ +typedef npy_uint64 __pyx_t_5numpy_uint64_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":704 + * #ctypedef npy_uint128 uint128_t + * + * ctypedef npy_float32 float32_t # <<<<<<<<<<<<<< + * ctypedef npy_float64 float64_t + * #ctypedef npy_float80 float80_t + */ +typedef npy_float32 __pyx_t_5numpy_float32_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":705 + * + * ctypedef npy_float32 float32_t + * ctypedef npy_float64 float64_t # <<<<<<<<<<<<<< + * #ctypedef npy_float80 float80_t + * #ctypedef npy_float128 float128_t + */ +typedef npy_float64 __pyx_t_5numpy_float64_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":714 + * # The int types are mapped a bit surprising -- + * # numpy.int corresponds to 'l' and numpy.long to 'q' + * ctypedef npy_long int_t # <<<<<<<<<<<<<< + * ctypedef npy_longlong long_t + * ctypedef npy_longlong longlong_t + */ +typedef npy_long __pyx_t_5numpy_int_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":715 + * # numpy.int corresponds to 'l' and numpy.long to 'q' + * ctypedef npy_long int_t + * ctypedef npy_longlong long_t # <<<<<<<<<<<<<< + * ctypedef npy_longlong longlong_t + * + */ +typedef npy_longlong __pyx_t_5numpy_long_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":716 + * ctypedef npy_long int_t + * ctypedef npy_longlong long_t + * ctypedef npy_longlong longlong_t # <<<<<<<<<<<<<< + * + * ctypedef npy_ulong uint_t + */ +typedef npy_longlong __pyx_t_5numpy_longlong_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":718 + * ctypedef npy_longlong longlong_t + * + * ctypedef npy_ulong uint_t # <<<<<<<<<<<<<< + * ctypedef npy_ulonglong ulong_t + * ctypedef npy_ulonglong ulonglong_t + */ +typedef npy_ulong __pyx_t_5numpy_uint_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":719 + * + * ctypedef npy_ulong uint_t + * ctypedef npy_ulonglong ulong_t # <<<<<<<<<<<<<< + * ctypedef npy_ulonglong ulonglong_t + * + */ +typedef npy_ulonglong __pyx_t_5numpy_ulong_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":720 + * ctypedef npy_ulong uint_t + * ctypedef npy_ulonglong ulong_t + * ctypedef npy_ulonglong ulonglong_t # <<<<<<<<<<<<<< + * + * ctypedef npy_intp intp_t + */ +typedef npy_ulonglong __pyx_t_5numpy_ulonglong_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":722 + * ctypedef npy_ulonglong ulonglong_t + * + * ctypedef npy_intp intp_t # <<<<<<<<<<<<<< + * ctypedef npy_uintp uintp_t + * + */ +typedef npy_intp __pyx_t_5numpy_intp_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":723 + * + * ctypedef npy_intp intp_t + * ctypedef npy_uintp uintp_t # <<<<<<<<<<<<<< + * + * ctypedef npy_double float_t + */ +typedef npy_uintp __pyx_t_5numpy_uintp_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":725 + * ctypedef npy_uintp uintp_t + * + * ctypedef npy_double float_t # <<<<<<<<<<<<<< + * ctypedef npy_double double_t + * ctypedef npy_longdouble longdouble_t + */ +typedef npy_double __pyx_t_5numpy_float_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":726 + * + * ctypedef npy_double float_t + * ctypedef npy_double double_t # <<<<<<<<<<<<<< + * ctypedef npy_longdouble longdouble_t + * + */ +typedef npy_double __pyx_t_5numpy_double_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":727 + * ctypedef npy_double float_t + * ctypedef npy_double double_t + * ctypedef npy_longdouble longdouble_t # <<<<<<<<<<<<<< + * + * ctypedef npy_cfloat cfloat_t + */ +typedef npy_longdouble __pyx_t_5numpy_longdouble_t; +/* Declarations.proto */ +#if CYTHON_CCOMPLEX + #ifdef __cplusplus + typedef ::std::complex< float > __pyx_t_float_complex; + #else + typedef float _Complex __pyx_t_float_complex; + #endif +#else + typedef struct { float real, imag; } __pyx_t_float_complex; +#endif +static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float, float); + +/* Declarations.proto */ +#if CYTHON_CCOMPLEX + #ifdef __cplusplus + typedef ::std::complex< double > __pyx_t_double_complex; + #else + typedef double _Complex __pyx_t_double_complex; + #endif +#else + typedef struct { double real, imag; } __pyx_t_double_complex; +#endif +static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double, double); + + +/*--- Type declarations ---*/ + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":729 + * ctypedef npy_longdouble longdouble_t + * + * ctypedef npy_cfloat cfloat_t # <<<<<<<<<<<<<< + * ctypedef npy_cdouble cdouble_t + * ctypedef npy_clongdouble clongdouble_t + */ +typedef npy_cfloat __pyx_t_5numpy_cfloat_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":730 + * + * ctypedef npy_cfloat cfloat_t + * ctypedef npy_cdouble cdouble_t # <<<<<<<<<<<<<< + * ctypedef npy_clongdouble clongdouble_t + * + */ +typedef npy_cdouble __pyx_t_5numpy_cdouble_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":731 + * ctypedef npy_cfloat cfloat_t + * ctypedef npy_cdouble cdouble_t + * ctypedef npy_clongdouble clongdouble_t # <<<<<<<<<<<<<< + * + * ctypedef npy_cdouble complex_t + */ +typedef npy_clongdouble __pyx_t_5numpy_clongdouble_t; + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":733 + * ctypedef npy_clongdouble clongdouble_t + * + * ctypedef npy_cdouble complex_t # <<<<<<<<<<<<<< + * + * cdef inline object PyArray_MultiIterNew1(a): + */ +typedef npy_cdouble __pyx_t_5numpy_complex_t; + +/* --- Runtime support code (head) --- */ +/* Refnanny.proto */ +#ifndef CYTHON_REFNANNY + #define CYTHON_REFNANNY 0 +#endif +#if CYTHON_REFNANNY + typedef struct { + void (*INCREF)(void*, PyObject*, int); + void (*DECREF)(void*, PyObject*, int); + void (*GOTREF)(void*, PyObject*, int); + void (*GIVEREF)(void*, PyObject*, int); + void* (*SetupContext)(const char*, int, const char*); + void (*FinishContext)(void**); + } __Pyx_RefNannyAPIStruct; + static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL; + static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname); + #define __Pyx_RefNannyDeclarations void *__pyx_refnanny = NULL; +#ifdef WITH_THREAD + #define __Pyx_RefNannySetupContext(name, acquire_gil)\ + if (acquire_gil) {\ + PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();\ + __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__);\ + PyGILState_Release(__pyx_gilstate_save);\ + } else {\ + __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__);\ + } +#else + #define __Pyx_RefNannySetupContext(name, acquire_gil)\ + __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__) +#endif + #define __Pyx_RefNannyFinishContext()\ + __Pyx_RefNanny->FinishContext(&__pyx_refnanny) + #define __Pyx_INCREF(r) __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__) + #define __Pyx_DECREF(r) __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__) + #define __Pyx_GOTREF(r) __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__) + #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__) + #define __Pyx_XINCREF(r) do { if((r) != NULL) {__Pyx_INCREF(r); }} while(0) + #define __Pyx_XDECREF(r) do { if((r) != NULL) {__Pyx_DECREF(r); }} while(0) + #define __Pyx_XGOTREF(r) do { if((r) != NULL) {__Pyx_GOTREF(r); }} while(0) + #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);}} while(0) +#else + #define __Pyx_RefNannyDeclarations + #define __Pyx_RefNannySetupContext(name, acquire_gil) + #define __Pyx_RefNannyFinishContext() + #define __Pyx_INCREF(r) Py_INCREF(r) + #define __Pyx_DECREF(r) Py_DECREF(r) + #define __Pyx_GOTREF(r) + #define __Pyx_GIVEREF(r) + #define __Pyx_XINCREF(r) Py_XINCREF(r) + #define __Pyx_XDECREF(r) Py_XDECREF(r) + #define __Pyx_XGOTREF(r) + #define __Pyx_XGIVEREF(r) +#endif +#define __Pyx_XDECREF_SET(r, v) do {\ + PyObject *tmp = (PyObject *) r;\ + r = v; __Pyx_XDECREF(tmp);\ + } while (0) +#define __Pyx_DECREF_SET(r, v) do {\ + PyObject *tmp = (PyObject *) r;\ + r = v; __Pyx_DECREF(tmp);\ + } while (0) +#define __Pyx_CLEAR(r) do { PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);} while(0) +#define __Pyx_XCLEAR(r) do { if((r) != NULL) {PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);}} while(0) + +/* PyObjectGetAttrStr.proto */ +#if CYTHON_USE_TYPE_SLOTS +static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name); +#else +#define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n) +#endif + +/* GetBuiltinName.proto */ +static PyObject *__Pyx_GetBuiltinName(PyObject *name); + +/* RaiseArgTupleInvalid.proto */ +static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact, + Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); + +/* RaiseDoubleKeywords.proto */ +static void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); + +/* ParseKeywords.proto */ +static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[],\ + PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args,\ + const char* function_name); + +/* ArgTypeTest.proto */ +#define __Pyx_ArgTypeTest(obj, type, none_allowed, name, exact)\ + ((likely((Py_TYPE(obj) == type) | (none_allowed && (obj == Py_None)))) ? 1 :\ + __Pyx__ArgTypeTest(obj, type, name, exact)) +static int __Pyx__ArgTypeTest(PyObject *obj, PyTypeObject *type, const char *name, int exact); + +/* IsLittleEndian.proto */ +static CYTHON_INLINE int __Pyx_Is_Little_Endian(void); + +/* BufferFormatCheck.proto */ +static const char* __Pyx_BufFmt_CheckString(__Pyx_BufFmt_Context* ctx, const char* ts); +static void __Pyx_BufFmt_Init(__Pyx_BufFmt_Context* ctx, + __Pyx_BufFmt_StackElem* stack, + __Pyx_TypeInfo* type); + +/* BufferGetAndValidate.proto */ +#define __Pyx_GetBufferAndValidate(buf, obj, dtype, flags, nd, cast, stack)\ + ((obj == Py_None || obj == NULL) ?\ + (__Pyx_ZeroBuffer(buf), 0) :\ + __Pyx__GetBufferAndValidate(buf, obj, dtype, flags, nd, cast, stack)) +static int __Pyx__GetBufferAndValidate(Py_buffer* buf, PyObject* obj, + __Pyx_TypeInfo* dtype, int flags, int nd, int cast, __Pyx_BufFmt_StackElem* stack); +static void __Pyx_ZeroBuffer(Py_buffer* buf); +static CYTHON_INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info); +static Py_ssize_t __Pyx_minusones[] = { -1, -1, -1, -1, -1, -1, -1, -1 }; +static Py_ssize_t __Pyx_zeros[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; + +/* GetItemInt.proto */ +#define __Pyx_GetItemInt(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck)\ + (__Pyx_fits_Py_ssize_t(i, type, is_signed) ?\ + __Pyx_GetItemInt_Fast(o, (Py_ssize_t)i, is_list, wraparound, boundscheck) :\ + (is_list ? (PyErr_SetString(PyExc_IndexError, "list index out of range"), (PyObject*)NULL) :\ + __Pyx_GetItemInt_Generic(o, to_py_func(i)))) +#define __Pyx_GetItemInt_List(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck)\ + (__Pyx_fits_Py_ssize_t(i, type, is_signed) ?\ + __Pyx_GetItemInt_List_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) :\ + (PyErr_SetString(PyExc_IndexError, "list index out of range"), (PyObject*)NULL)) +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i, + int wraparound, int boundscheck); +#define __Pyx_GetItemInt_Tuple(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck)\ + (__Pyx_fits_Py_ssize_t(i, type, is_signed) ?\ + __Pyx_GetItemInt_Tuple_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) :\ + (PyErr_SetString(PyExc_IndexError, "tuple index out of range"), (PyObject*)NULL)) +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i, + int wraparound, int boundscheck); +static PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j); +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i, + int is_list, int wraparound, int boundscheck); + +/* ObjectGetItem.proto */ +#if CYTHON_USE_TYPE_SLOTS +static CYTHON_INLINE PyObject *__Pyx_PyObject_GetItem(PyObject *obj, PyObject* key); +#else +#define __Pyx_PyObject_GetItem(obj, key) PyObject_GetItem(obj, key) +#endif + +/* ExtTypeTest.proto */ +static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); + +/* PyIntBinop.proto */ +#if !CYTHON_COMPILING_IN_PYPY +static PyObject* __Pyx_PyInt_AddObjC(PyObject *op1, PyObject *op2, long intval, int inplace, int zerodivision_check); +#else +#define __Pyx_PyInt_AddObjC(op1, op2, intval, inplace, zerodivision_check)\ + (inplace ? PyNumber_InPlaceAdd(op1, op2) : PyNumber_Add(op1, op2)) +#endif + +/* PyFunctionFastCall.proto */ +#if CYTHON_FAST_PYCALL +#define __Pyx_PyFunction_FastCall(func, args, nargs)\ + __Pyx_PyFunction_FastCallDict((func), (args), (nargs), NULL) +#if 1 || PY_VERSION_HEX < 0x030600B1 +static PyObject *__Pyx_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs, PyObject *kwargs); +#else +#define __Pyx_PyFunction_FastCallDict(func, args, nargs, kwargs) _PyFunction_FastCallDict(func, args, nargs, kwargs) +#endif +#define __Pyx_BUILD_ASSERT_EXPR(cond)\ + (sizeof(char [1 - 2*!(cond)]) - 1) +#ifndef Py_MEMBER_SIZE +#define Py_MEMBER_SIZE(type, member) sizeof(((type *)0)->member) +#endif + static size_t __pyx_pyframe_localsplus_offset = 0; + #include "frameobject.h" + #define __Pxy_PyFrame_Initialize_Offsets()\ + ((void)__Pyx_BUILD_ASSERT_EXPR(sizeof(PyFrameObject) == offsetof(PyFrameObject, f_localsplus) + Py_MEMBER_SIZE(PyFrameObject, f_localsplus)),\ + (void)(__pyx_pyframe_localsplus_offset = ((size_t)PyFrame_Type.tp_basicsize) - Py_MEMBER_SIZE(PyFrameObject, f_localsplus))) + #define __Pyx_PyFrame_GetLocalsplus(frame)\ + (assert(__pyx_pyframe_localsplus_offset), (PyObject **)(((char *)(frame)) + __pyx_pyframe_localsplus_offset)) +#endif + +/* PyObjectCall.proto */ +#if CYTHON_COMPILING_IN_CPYTHON +static CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw); +#else +#define __Pyx_PyObject_Call(func, arg, kw) PyObject_Call(func, arg, kw) +#endif + +/* PyObjectCallMethO.proto */ +#if CYTHON_COMPILING_IN_CPYTHON +static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg); +#endif + +/* PyObjectCallNoArg.proto */ +#if CYTHON_COMPILING_IN_CPYTHON +static CYTHON_INLINE PyObject* __Pyx_PyObject_CallNoArg(PyObject *func); +#else +#define __Pyx_PyObject_CallNoArg(func) __Pyx_PyObject_Call(func, __pyx_empty_tuple, NULL) +#endif + +/* PyCFunctionFastCall.proto */ +#if CYTHON_FAST_PYCCALL +static CYTHON_INLINE PyObject *__Pyx_PyCFunction_FastCall(PyObject *func, PyObject **args, Py_ssize_t nargs); +#else +#define __Pyx_PyCFunction_FastCall(func, args, nargs) (assert(0), NULL) +#endif + +/* PyObjectCallOneArg.proto */ +static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg); + +/* PyDictVersioning.proto */ +#if CYTHON_USE_DICT_VERSIONS && CYTHON_USE_TYPE_SLOTS +#define __PYX_DICT_VERSION_INIT ((PY_UINT64_T) -1) +#define __PYX_GET_DICT_VERSION(dict) (((PyDictObject*)(dict))->ma_version_tag) +#define __PYX_UPDATE_DICT_CACHE(dict, value, cache_var, version_var)\ + (version_var) = __PYX_GET_DICT_VERSION(dict);\ + (cache_var) = (value); +#define __PYX_PY_DICT_LOOKUP_IF_MODIFIED(VAR, DICT, LOOKUP) {\ + static PY_UINT64_T __pyx_dict_version = 0;\ + static PyObject *__pyx_dict_cached_value = NULL;\ + if (likely(__PYX_GET_DICT_VERSION(DICT) == __pyx_dict_version)) {\ + (VAR) = __pyx_dict_cached_value;\ + } else {\ + (VAR) = __pyx_dict_cached_value = (LOOKUP);\ + __pyx_dict_version = __PYX_GET_DICT_VERSION(DICT);\ + }\ +} +static CYTHON_INLINE PY_UINT64_T __Pyx_get_tp_dict_version(PyObject *obj); +static CYTHON_INLINE PY_UINT64_T __Pyx_get_object_dict_version(PyObject *obj); +static CYTHON_INLINE int __Pyx_object_dict_version_matches(PyObject* obj, PY_UINT64_T tp_dict_version, PY_UINT64_T obj_dict_version); +#else +#define __PYX_GET_DICT_VERSION(dict) (0) +#define __PYX_UPDATE_DICT_CACHE(dict, value, cache_var, version_var) +#define __PYX_PY_DICT_LOOKUP_IF_MODIFIED(VAR, DICT, LOOKUP) (VAR) = (LOOKUP); +#endif + +/* GetModuleGlobalName.proto */ +#if CYTHON_USE_DICT_VERSIONS +#define __Pyx_GetModuleGlobalName(var, name) {\ + static PY_UINT64_T __pyx_dict_version = 0;\ + static PyObject *__pyx_dict_cached_value = NULL;\ + (var) = (likely(__pyx_dict_version == __PYX_GET_DICT_VERSION(__pyx_d))) ?\ + (likely(__pyx_dict_cached_value) ? __Pyx_NewRef(__pyx_dict_cached_value) : __Pyx_GetBuiltinName(name)) :\ + __Pyx__GetModuleGlobalName(name, &__pyx_dict_version, &__pyx_dict_cached_value);\ +} +#define __Pyx_GetModuleGlobalNameUncached(var, name) {\ + PY_UINT64_T __pyx_dict_version;\ + PyObject *__pyx_dict_cached_value;\ + (var) = __Pyx__GetModuleGlobalName(name, &__pyx_dict_version, &__pyx_dict_cached_value);\ +} +static PyObject *__Pyx__GetModuleGlobalName(PyObject *name, PY_UINT64_T *dict_version, PyObject **dict_cached_value); +#else +#define __Pyx_GetModuleGlobalName(var, name) (var) = __Pyx__GetModuleGlobalName(name) +#define __Pyx_GetModuleGlobalNameUncached(var, name) (var) = __Pyx__GetModuleGlobalName(name) +static CYTHON_INLINE PyObject *__Pyx__GetModuleGlobalName(PyObject *name); +#endif + +/* BufferIndexError.proto */ +static void __Pyx_RaiseBufferIndexError(int axis); + +#define __Pyx_BufPtrStrided1d(type, buf, i0, s0) (type)((char*)buf + i0 * s0) +/* ListAppend.proto */ +#if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS +static CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) { + PyListObject* L = (PyListObject*) list; + Py_ssize_t len = Py_SIZE(list); + if (likely(L->allocated > len) & likely(len > (L->allocated >> 1))) { + Py_INCREF(x); + PyList_SET_ITEM(list, len, x); + __Pyx_SET_SIZE(list, len + 1); + return 0; + } + return PyList_Append(list, x); +} +#else +#define __Pyx_PyList_Append(L,x) PyList_Append(L,x) +#endif + +/* PyThreadStateGet.proto */ +#if CYTHON_FAST_THREAD_STATE +#define __Pyx_PyThreadState_declare PyThreadState *__pyx_tstate; +#define __Pyx_PyThreadState_assign __pyx_tstate = __Pyx_PyThreadState_Current; +#define __Pyx_PyErr_Occurred() __pyx_tstate->curexc_type +#else +#define __Pyx_PyThreadState_declare +#define __Pyx_PyThreadState_assign +#define __Pyx_PyErr_Occurred() PyErr_Occurred() +#endif + +/* PyErrFetchRestore.proto */ +#if CYTHON_FAST_THREAD_STATE +#define __Pyx_PyErr_Clear() __Pyx_ErrRestore(NULL, NULL, NULL) +#define __Pyx_ErrRestoreWithState(type, value, tb) __Pyx_ErrRestoreInState(PyThreadState_GET(), type, value, tb) +#define __Pyx_ErrFetchWithState(type, value, tb) __Pyx_ErrFetchInState(PyThreadState_GET(), type, value, tb) +#define __Pyx_ErrRestore(type, value, tb) __Pyx_ErrRestoreInState(__pyx_tstate, type, value, tb) +#define __Pyx_ErrFetch(type, value, tb) __Pyx_ErrFetchInState(__pyx_tstate, type, value, tb) +static CYTHON_INLINE void __Pyx_ErrRestoreInState(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb); +static CYTHON_INLINE void __Pyx_ErrFetchInState(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); +#if CYTHON_COMPILING_IN_CPYTHON +#define __Pyx_PyErr_SetNone(exc) (Py_INCREF(exc), __Pyx_ErrRestore((exc), NULL, NULL)) +#else +#define __Pyx_PyErr_SetNone(exc) PyErr_SetNone(exc) +#endif +#else +#define __Pyx_PyErr_Clear() PyErr_Clear() +#define __Pyx_PyErr_SetNone(exc) PyErr_SetNone(exc) +#define __Pyx_ErrRestoreWithState(type, value, tb) PyErr_Restore(type, value, tb) +#define __Pyx_ErrFetchWithState(type, value, tb) PyErr_Fetch(type, value, tb) +#define __Pyx_ErrRestoreInState(tstate, type, value, tb) PyErr_Restore(type, value, tb) +#define __Pyx_ErrFetchInState(tstate, type, value, tb) PyErr_Fetch(type, value, tb) +#define __Pyx_ErrRestore(type, value, tb) PyErr_Restore(type, value, tb) +#define __Pyx_ErrFetch(type, value, tb) PyErr_Fetch(type, value, tb) +#endif + +#define __Pyx_BufPtrStrided2d(type, buf, i0, s0, i1, s1) (type)((char*)buf + i0 * s0 + i1 * s1) +/* PyObjectCall2Args.proto */ +static CYTHON_UNUSED PyObject* __Pyx_PyObject_Call2Args(PyObject* function, PyObject* arg1, PyObject* arg2); + +/* ListCompAppend.proto */ +#if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS +static CYTHON_INLINE int __Pyx_ListComp_Append(PyObject* list, PyObject* x) { + PyListObject* L = (PyListObject*) list; + Py_ssize_t len = Py_SIZE(list); + if (likely(L->allocated > len)) { + Py_INCREF(x); + PyList_SET_ITEM(list, len, x); + __Pyx_SET_SIZE(list, len + 1); + return 0; + } + return PyList_Append(list, x); +} +#else +#define __Pyx_ListComp_Append(L,x) PyList_Append(L,x) +#endif + +/* GetTopmostException.proto */ +#if CYTHON_USE_EXC_INFO_STACK +static _PyErr_StackItem * __Pyx_PyErr_GetTopmostException(PyThreadState *tstate); +#endif + +/* SaveResetException.proto */ +#if CYTHON_FAST_THREAD_STATE +#define __Pyx_ExceptionSave(type, value, tb) __Pyx__ExceptionSave(__pyx_tstate, type, value, tb) +static CYTHON_INLINE void __Pyx__ExceptionSave(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); +#define __Pyx_ExceptionReset(type, value, tb) __Pyx__ExceptionReset(__pyx_tstate, type, value, tb) +static CYTHON_INLINE void __Pyx__ExceptionReset(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb); +#else +#define __Pyx_ExceptionSave(type, value, tb) PyErr_GetExcInfo(type, value, tb) +#define __Pyx_ExceptionReset(type, value, tb) PyErr_SetExcInfo(type, value, tb) +#endif + +/* PyErrExceptionMatches.proto */ +#if CYTHON_FAST_THREAD_STATE +#define __Pyx_PyErr_ExceptionMatches(err) __Pyx_PyErr_ExceptionMatchesInState(__pyx_tstate, err) +static CYTHON_INLINE int __Pyx_PyErr_ExceptionMatchesInState(PyThreadState* tstate, PyObject* err); +#else +#define __Pyx_PyErr_ExceptionMatches(err) PyErr_ExceptionMatches(err) +#endif + +/* GetException.proto */ +#if CYTHON_FAST_THREAD_STATE +#define __Pyx_GetException(type, value, tb) __Pyx__GetException(__pyx_tstate, type, value, tb) +static int __Pyx__GetException(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb); +#else +static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb); +#endif + +/* RaiseException.proto */ +static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause); + +/* TypeImport.proto */ +#ifndef __PYX_HAVE_RT_ImportType_proto +#define __PYX_HAVE_RT_ImportType_proto +enum __Pyx_ImportType_CheckSize { + __Pyx_ImportType_CheckSize_Error = 0, + __Pyx_ImportType_CheckSize_Warn = 1, + __Pyx_ImportType_CheckSize_Ignore = 2 +}; +static PyTypeObject *__Pyx_ImportType(PyObject* module, const char *module_name, const char *class_name, size_t size, enum __Pyx_ImportType_CheckSize check_size); +#endif + +/* Import.proto */ +static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); + +/* CLineInTraceback.proto */ +#ifdef CYTHON_CLINE_IN_TRACEBACK +#define __Pyx_CLineForTraceback(tstate, c_line) (((CYTHON_CLINE_IN_TRACEBACK)) ? c_line : 0) +#else +static int __Pyx_CLineForTraceback(PyThreadState *tstate, int c_line); +#endif + +/* CodeObjectCache.proto */ +typedef struct { + PyCodeObject* code_object; + int code_line; +} __Pyx_CodeObjectCacheEntry; +struct __Pyx_CodeObjectCache { + int count; + int max_count; + __Pyx_CodeObjectCacheEntry* entries; +}; +static struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL}; +static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line); +static PyCodeObject *__pyx_find_code_object(int code_line); +static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object); + +/* AddTraceback.proto */ +static void __Pyx_AddTraceback(const char *funcname, int c_line, + int py_line, const char *filename); + +/* BufferStructDeclare.proto */ +typedef struct { + Py_ssize_t shape, strides, suboffsets; +} __Pyx_Buf_DimInfo; +typedef struct { + size_t refcount; + Py_buffer pybuffer; +} __Pyx_Buffer; +typedef struct { + __Pyx_Buffer *rcbuffer; + char *data; + __Pyx_Buf_DimInfo diminfo[8]; +} __Pyx_LocalBuf_ND; + +#if PY_MAJOR_VERSION < 3 + static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags); + static void __Pyx_ReleaseBuffer(Py_buffer *view); +#else + #define __Pyx_GetBuffer PyObject_GetBuffer + #define __Pyx_ReleaseBuffer PyBuffer_Release +#endif + + +/* GCCDiagnostics.proto */ +#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) +#define __Pyx_HAS_GCC_DIAGNOSTIC +#endif + +/* RealImag.proto */ +#if CYTHON_CCOMPLEX + #ifdef __cplusplus + #define __Pyx_CREAL(z) ((z).real()) + #define __Pyx_CIMAG(z) ((z).imag()) + #else + #define __Pyx_CREAL(z) (__real__(z)) + #define __Pyx_CIMAG(z) (__imag__(z)) + #endif +#else + #define __Pyx_CREAL(z) ((z).real) + #define __Pyx_CIMAG(z) ((z).imag) +#endif +#if defined(__cplusplus) && CYTHON_CCOMPLEX\ + && (defined(_WIN32) || defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 5 || __GNUC__ == 4 && __GNUC_MINOR__ >= 4 )) || __cplusplus >= 201103) + #define __Pyx_SET_CREAL(z,x) ((z).real(x)) + #define __Pyx_SET_CIMAG(z,y) ((z).imag(y)) +#else + #define __Pyx_SET_CREAL(z,x) __Pyx_CREAL(z) = (x) + #define __Pyx_SET_CIMAG(z,y) __Pyx_CIMAG(z) = (y) +#endif + +/* Arithmetic.proto */ +#if CYTHON_CCOMPLEX + #define __Pyx_c_eq_float(a, b) ((a)==(b)) + #define __Pyx_c_sum_float(a, b) ((a)+(b)) + #define __Pyx_c_diff_float(a, b) ((a)-(b)) + #define __Pyx_c_prod_float(a, b) ((a)*(b)) + #define __Pyx_c_quot_float(a, b) ((a)/(b)) + #define __Pyx_c_neg_float(a) (-(a)) + #ifdef __cplusplus + #define __Pyx_c_is_zero_float(z) ((z)==(float)0) + #define __Pyx_c_conj_float(z) (::std::conj(z)) + #if 1 + #define __Pyx_c_abs_float(z) (::std::abs(z)) + #define __Pyx_c_pow_float(a, b) (::std::pow(a, b)) + #endif + #else + #define __Pyx_c_is_zero_float(z) ((z)==0) + #define __Pyx_c_conj_float(z) (conjf(z)) + #if 1 + #define __Pyx_c_abs_float(z) (cabsf(z)) + #define __Pyx_c_pow_float(a, b) (cpowf(a, b)) + #endif + #endif +#else + static CYTHON_INLINE int __Pyx_c_eq_float(__pyx_t_float_complex, __pyx_t_float_complex); + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_sum_float(__pyx_t_float_complex, __pyx_t_float_complex); + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_diff_float(__pyx_t_float_complex, __pyx_t_float_complex); + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_prod_float(__pyx_t_float_complex, __pyx_t_float_complex); + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_quot_float(__pyx_t_float_complex, __pyx_t_float_complex); + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_neg_float(__pyx_t_float_complex); + static CYTHON_INLINE int __Pyx_c_is_zero_float(__pyx_t_float_complex); + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_conj_float(__pyx_t_float_complex); + #if 1 + static CYTHON_INLINE float __Pyx_c_abs_float(__pyx_t_float_complex); + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_pow_float(__pyx_t_float_complex, __pyx_t_float_complex); + #endif +#endif + +/* Arithmetic.proto */ +#if CYTHON_CCOMPLEX + #define __Pyx_c_eq_double(a, b) ((a)==(b)) + #define __Pyx_c_sum_double(a, b) ((a)+(b)) + #define __Pyx_c_diff_double(a, b) ((a)-(b)) + #define __Pyx_c_prod_double(a, b) ((a)*(b)) + #define __Pyx_c_quot_double(a, b) ((a)/(b)) + #define __Pyx_c_neg_double(a) (-(a)) + #ifdef __cplusplus + #define __Pyx_c_is_zero_double(z) ((z)==(double)0) + #define __Pyx_c_conj_double(z) (::std::conj(z)) + #if 1 + #define __Pyx_c_abs_double(z) (::std::abs(z)) + #define __Pyx_c_pow_double(a, b) (::std::pow(a, b)) + #endif + #else + #define __Pyx_c_is_zero_double(z) ((z)==0) + #define __Pyx_c_conj_double(z) (conj(z)) + #if 1 + #define __Pyx_c_abs_double(z) (cabs(z)) + #define __Pyx_c_pow_double(a, b) (cpow(a, b)) + #endif + #endif +#else + static CYTHON_INLINE int __Pyx_c_eq_double(__pyx_t_double_complex, __pyx_t_double_complex); + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_sum_double(__pyx_t_double_complex, __pyx_t_double_complex); + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_diff_double(__pyx_t_double_complex, __pyx_t_double_complex); + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_prod_double(__pyx_t_double_complex, __pyx_t_double_complex); + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_quot_double(__pyx_t_double_complex, __pyx_t_double_complex); + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_neg_double(__pyx_t_double_complex); + static CYTHON_INLINE int __Pyx_c_is_zero_double(__pyx_t_double_complex); + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_conj_double(__pyx_t_double_complex); + #if 1 + static CYTHON_INLINE double __Pyx_c_abs_double(__pyx_t_double_complex); + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_pow_double(__pyx_t_double_complex, __pyx_t_double_complex); + #endif +#endif + +/* CIntFromPy.proto */ +static CYTHON_INLINE unsigned int __Pyx_PyInt_As_unsigned_int(PyObject *); + +/* CIntToPy.proto */ +static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value); + +/* CIntFromPy.proto */ +static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *); + +/* CIntToPy.proto */ +static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value); + +/* CIntToPy.proto */ +static CYTHON_INLINE PyObject* __Pyx_PyInt_From_unsigned_int(unsigned int value); + +/* CIntFromPy.proto */ +static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *); + +/* FastTypeChecks.proto */ +#if CYTHON_COMPILING_IN_CPYTHON +#define __Pyx_TypeCheck(obj, type) __Pyx_IsSubtype(Py_TYPE(obj), (PyTypeObject *)type) +static CYTHON_INLINE int __Pyx_IsSubtype(PyTypeObject *a, PyTypeObject *b); +static CYTHON_INLINE int __Pyx_PyErr_GivenExceptionMatches(PyObject *err, PyObject *type); +static CYTHON_INLINE int __Pyx_PyErr_GivenExceptionMatches2(PyObject *err, PyObject *type1, PyObject *type2); +#else +#define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type) +#define __Pyx_PyErr_GivenExceptionMatches(err, type) PyErr_GivenExceptionMatches(err, type) +#define __Pyx_PyErr_GivenExceptionMatches2(err, type1, type2) (PyErr_GivenExceptionMatches(err, type1) || PyErr_GivenExceptionMatches(err, type2)) +#endif +#define __Pyx_PyException_Check(obj) __Pyx_TypeCheck(obj, PyExc_Exception) + +/* CheckBinaryVersion.proto */ +static int __Pyx_check_binary_version(void); + +/* InitStrings.proto */ +static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); + + +/* Module declarations from 'cpython.buffer' */ + +/* Module declarations from 'libc.string' */ + +/* Module declarations from 'libc.stdio' */ + +/* Module declarations from '__builtin__' */ + +/* Module declarations from 'cpython.type' */ +static PyTypeObject *__pyx_ptype_7cpython_4type_type = 0; + +/* Module declarations from 'cpython' */ + +/* Module declarations from 'cpython.object' */ + +/* Module declarations from 'cpython.ref' */ + +/* Module declarations from 'cpython.mem' */ + +/* Module declarations from 'numpy' */ + +/* Module declarations from 'numpy' */ +static PyTypeObject *__pyx_ptype_5numpy_dtype = 0; +static PyTypeObject *__pyx_ptype_5numpy_flatiter = 0; +static PyTypeObject *__pyx_ptype_5numpy_broadcast = 0; +static PyTypeObject *__pyx_ptype_5numpy_ndarray = 0; +static PyTypeObject *__pyx_ptype_5numpy_generic = 0; +static PyTypeObject *__pyx_ptype_5numpy_number = 0; +static PyTypeObject *__pyx_ptype_5numpy_integer = 0; +static PyTypeObject *__pyx_ptype_5numpy_signedinteger = 0; +static PyTypeObject *__pyx_ptype_5numpy_unsignedinteger = 0; +static PyTypeObject *__pyx_ptype_5numpy_inexact = 0; +static PyTypeObject *__pyx_ptype_5numpy_floating = 0; +static PyTypeObject *__pyx_ptype_5numpy_complexfloating = 0; +static PyTypeObject *__pyx_ptype_5numpy_flexible = 0; +static PyTypeObject *__pyx_ptype_5numpy_character = 0; +static PyTypeObject *__pyx_ptype_5numpy_ufunc = 0; + +/* Module declarations from 'nms.cpu_nms' */ +static CYTHON_INLINE __pyx_t_5numpy_float32_t __pyx_f_3nms_7cpu_nms_max(__pyx_t_5numpy_float32_t, __pyx_t_5numpy_float32_t); /*proto*/ +static CYTHON_INLINE __pyx_t_5numpy_float32_t __pyx_f_3nms_7cpu_nms_min(__pyx_t_5numpy_float32_t, __pyx_t_5numpy_float32_t); /*proto*/ +static __Pyx_TypeInfo __Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t = { "float32_t", NULL, sizeof(__pyx_t_5numpy_float32_t), { 0 }, 0, 'R', 0, 0 }; +static __Pyx_TypeInfo __Pyx_TypeInfo_nn___pyx_t_5numpy_int_t = { "int_t", NULL, sizeof(__pyx_t_5numpy_int_t), { 0 }, 0, IS_UNSIGNED(__pyx_t_5numpy_int_t) ? 'U' : 'I', IS_UNSIGNED(__pyx_t_5numpy_int_t), 0 }; +static __Pyx_TypeInfo __Pyx_TypeInfo_float = { "float", NULL, sizeof(float), { 0 }, 0, 'R', 0, 0 }; +#define __Pyx_MODULE_NAME "nms.cpu_nms" +extern int __pyx_module_is_main_nms__cpu_nms; +int __pyx_module_is_main_nms__cpu_nms = 0; + +/* Implementation of 'nms.cpu_nms' */ +static PyObject *__pyx_builtin_range; +static PyObject *__pyx_builtin_ImportError; +static const char __pyx_k_N[] = "N"; +static const char __pyx_k_h[] = "h"; +static const char __pyx_k_i[] = "_i"; +static const char __pyx_k_j[] = "_j"; +static const char __pyx_k_s[] = "s"; +static const char __pyx_k_w[] = "w"; +static const char __pyx_k_Nt[] = "Nt"; +static const char __pyx_k_ih[] = "ih"; +static const char __pyx_k_iw[] = "iw"; +static const char __pyx_k_np[] = "np"; +static const char __pyx_k_ov[] = "ov"; +static const char __pyx_k_ts[] = "ts"; +static const char __pyx_k_ua[] = "ua"; +static const char __pyx_k_x1[] = "x1"; +static const char __pyx_k_x2[] = "x2"; +static const char __pyx_k_y1[] = "y1"; +static const char __pyx_k_y2[] = "y2"; +static const char __pyx_k_exp[] = "exp"; +static const char __pyx_k_i_2[] = "i"; +static const char __pyx_k_int[] = "int"; +static const char __pyx_k_ix1[] = "ix1"; +static const char __pyx_k_ix2[] = "ix2"; +static const char __pyx_k_iy1[] = "iy1"; +static const char __pyx_k_iy2[] = "iy2"; +static const char __pyx_k_j_2[] = "j"; +static const char __pyx_k_ovr[] = "ovr"; +static const char __pyx_k_pos[] = "pos"; +static const char __pyx_k_tx1[] = "tx1"; +static const char __pyx_k_tx2[] = "tx2"; +static const char __pyx_k_ty1[] = "ty1"; +static const char __pyx_k_ty2[] = "ty2"; +static const char __pyx_k_xx1[] = "xx1"; +static const char __pyx_k_xx2[] = "xx2"; +static const char __pyx_k_yy1[] = "yy1"; +static const char __pyx_k_yy2[] = "yy2"; +static const char __pyx_k_area[] = "area"; +static const char __pyx_k_dets[] = "dets"; +static const char __pyx_k_keep[] = "keep"; +static const char __pyx_k_main[] = "__main__"; +static const char __pyx_k_name[] = "__name__"; +static const char __pyx_k_test[] = "__test__"; +static const char __pyx_k_areas[] = "areas"; +static const char __pyx_k_boxes[] = "boxes"; +static const char __pyx_k_dtype[] = "dtype"; +static const char __pyx_k_iarea[] = "iarea"; +static const char __pyx_k_inter[] = "inter"; +static const char __pyx_k_ndets[] = "ndets"; +static const char __pyx_k_numpy[] = "numpy"; +static const char __pyx_k_order[] = "order"; +static const char __pyx_k_range[] = "range"; +static const char __pyx_k_sigma[] = "sigma"; +static const char __pyx_k_zeros[] = "zeros"; +static const char __pyx_k_import[] = "__import__"; +static const char __pyx_k_maxpos[] = "maxpos"; +static const char __pyx_k_method[] = "method"; +static const char __pyx_k_scores[] = "scores"; +static const char __pyx_k_thresh[] = "thresh"; +static const char __pyx_k_weight[] = "weight"; +static const char __pyx_k_argsort[] = "argsort"; +static const char __pyx_k_cpu_nms[] = "cpu_nms"; +static const char __pyx_k_box_area[] = "box_area"; +static const char __pyx_k_maxscore[] = "maxscore"; +static const char __pyx_k_threshold[] = "threshold"; +static const char __pyx_k_suppressed[] = "suppressed"; +static const char __pyx_k_ImportError[] = "ImportError"; +static const char __pyx_k_nms_cpu_nms[] = "nms.cpu_nms"; +static const char __pyx_k_cpu_soft_nms[] = "cpu_soft_nms"; +static const char __pyx_k_nms_cpu_nms_pyx[] = "nms/cpu_nms.pyx"; +static const char __pyx_k_cline_in_traceback[] = "cline_in_traceback"; +static const char __pyx_k_numpy_core_multiarray_failed_to[] = "numpy.core.multiarray failed to import"; +static const char __pyx_k_numpy_core_umath_failed_to_impor[] = "numpy.core.umath failed to import"; +static PyObject *__pyx_n_s_ImportError; +static PyObject *__pyx_n_s_N; +static PyObject *__pyx_n_s_Nt; +static PyObject *__pyx_n_s_area; +static PyObject *__pyx_n_s_areas; +static PyObject *__pyx_n_s_argsort; +static PyObject *__pyx_n_s_box_area; +static PyObject *__pyx_n_s_boxes; +static PyObject *__pyx_n_s_cline_in_traceback; +static PyObject *__pyx_n_s_cpu_nms; +static PyObject *__pyx_n_s_cpu_soft_nms; +static PyObject *__pyx_n_s_dets; +static PyObject *__pyx_n_s_dtype; +static PyObject *__pyx_n_s_exp; +static PyObject *__pyx_n_s_h; +static PyObject *__pyx_n_s_i; +static PyObject *__pyx_n_s_i_2; +static PyObject *__pyx_n_s_iarea; +static PyObject *__pyx_n_s_ih; +static PyObject *__pyx_n_s_import; +static PyObject *__pyx_n_s_int; +static PyObject *__pyx_n_s_inter; +static PyObject *__pyx_n_s_iw; +static PyObject *__pyx_n_s_ix1; +static PyObject *__pyx_n_s_ix2; +static PyObject *__pyx_n_s_iy1; +static PyObject *__pyx_n_s_iy2; +static PyObject *__pyx_n_s_j; +static PyObject *__pyx_n_s_j_2; +static PyObject *__pyx_n_s_keep; +static PyObject *__pyx_n_s_main; +static PyObject *__pyx_n_s_maxpos; +static PyObject *__pyx_n_s_maxscore; +static PyObject *__pyx_n_s_method; +static PyObject *__pyx_n_s_name; +static PyObject *__pyx_n_s_ndets; +static PyObject *__pyx_n_s_nms_cpu_nms; +static PyObject *__pyx_kp_s_nms_cpu_nms_pyx; +static PyObject *__pyx_n_s_np; +static PyObject *__pyx_n_s_numpy; +static PyObject *__pyx_kp_s_numpy_core_multiarray_failed_to; +static PyObject *__pyx_kp_s_numpy_core_umath_failed_to_impor; +static PyObject *__pyx_n_s_order; +static PyObject *__pyx_n_s_ov; +static PyObject *__pyx_n_s_ovr; +static PyObject *__pyx_n_s_pos; +static PyObject *__pyx_n_s_range; +static PyObject *__pyx_n_s_s; +static PyObject *__pyx_n_s_scores; +static PyObject *__pyx_n_s_sigma; +static PyObject *__pyx_n_s_suppressed; +static PyObject *__pyx_n_s_test; +static PyObject *__pyx_n_s_thresh; +static PyObject *__pyx_n_s_threshold; +static PyObject *__pyx_n_s_ts; +static PyObject *__pyx_n_s_tx1; +static PyObject *__pyx_n_s_tx2; +static PyObject *__pyx_n_s_ty1; +static PyObject *__pyx_n_s_ty2; +static PyObject *__pyx_n_s_ua; +static PyObject *__pyx_n_s_w; +static PyObject *__pyx_n_s_weight; +static PyObject *__pyx_n_s_x1; +static PyObject *__pyx_n_s_x2; +static PyObject *__pyx_n_s_xx1; +static PyObject *__pyx_n_s_xx2; +static PyObject *__pyx_n_s_y1; +static PyObject *__pyx_n_s_y2; +static PyObject *__pyx_n_s_yy1; +static PyObject *__pyx_n_s_yy2; +static PyObject *__pyx_n_s_zeros; +static PyObject *__pyx_pf_3nms_7cpu_nms_cpu_nms(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_dets, PyObject *__pyx_v_thresh); /* proto */ +static PyObject *__pyx_pf_3nms_7cpu_nms_2cpu_soft_nms(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_boxes, float __pyx_v_sigma, float __pyx_v_Nt, float __pyx_v_threshold, unsigned int __pyx_v_method); /* proto */ +static PyObject *__pyx_int_0; +static PyObject *__pyx_int_1; +static PyObject *__pyx_int_2; +static PyObject *__pyx_int_3; +static PyObject *__pyx_int_4; +static PyObject *__pyx_int_neg_1; +static PyObject *__pyx_slice_; +static PyObject *__pyx_slice__7; +static PyObject *__pyx_tuple__2; +static PyObject *__pyx_tuple__3; +static PyObject *__pyx_tuple__4; +static PyObject *__pyx_tuple__5; +static PyObject *__pyx_tuple__6; +static PyObject *__pyx_tuple__8; +static PyObject *__pyx_tuple__9; +static PyObject *__pyx_tuple__10; +static PyObject *__pyx_tuple__12; +static PyObject *__pyx_codeobj__11; +static PyObject *__pyx_codeobj__13; +/* Late includes */ + +/* "nms/cpu_nms.pyx":11 + * cimport numpy as np + * + * cdef inline np.float32_t max(np.float32_t a, np.float32_t b): # <<<<<<<<<<<<<< + * return a if a >= b else b + * + */ + +static CYTHON_INLINE __pyx_t_5numpy_float32_t __pyx_f_3nms_7cpu_nms_max(__pyx_t_5numpy_float32_t __pyx_v_a, __pyx_t_5numpy_float32_t __pyx_v_b) { + __pyx_t_5numpy_float32_t __pyx_r; + __Pyx_RefNannyDeclarations + __pyx_t_5numpy_float32_t __pyx_t_1; + __Pyx_RefNannySetupContext("max", 0); + + /* "nms/cpu_nms.pyx":12 + * + * cdef inline np.float32_t max(np.float32_t a, np.float32_t b): + * return a if a >= b else b # <<<<<<<<<<<<<< + * + * cdef inline np.float32_t min(np.float32_t a, np.float32_t b): + */ + if (((__pyx_v_a >= __pyx_v_b) != 0)) { + __pyx_t_1 = __pyx_v_a; + } else { + __pyx_t_1 = __pyx_v_b; + } + __pyx_r = __pyx_t_1; + goto __pyx_L0; + + /* "nms/cpu_nms.pyx":11 + * cimport numpy as np + * + * cdef inline np.float32_t max(np.float32_t a, np.float32_t b): # <<<<<<<<<<<<<< + * return a if a >= b else b + * + */ + + /* function exit code */ + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "nms/cpu_nms.pyx":14 + * return a if a >= b else b + * + * cdef inline np.float32_t min(np.float32_t a, np.float32_t b): # <<<<<<<<<<<<<< + * return a if a <= b else b + * + */ + +static CYTHON_INLINE __pyx_t_5numpy_float32_t __pyx_f_3nms_7cpu_nms_min(__pyx_t_5numpy_float32_t __pyx_v_a, __pyx_t_5numpy_float32_t __pyx_v_b) { + __pyx_t_5numpy_float32_t __pyx_r; + __Pyx_RefNannyDeclarations + __pyx_t_5numpy_float32_t __pyx_t_1; + __Pyx_RefNannySetupContext("min", 0); + + /* "nms/cpu_nms.pyx":15 + * + * cdef inline np.float32_t min(np.float32_t a, np.float32_t b): + * return a if a <= b else b # <<<<<<<<<<<<<< + * + * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh): + */ + if (((__pyx_v_a <= __pyx_v_b) != 0)) { + __pyx_t_1 = __pyx_v_a; + } else { + __pyx_t_1 = __pyx_v_b; + } + __pyx_r = __pyx_t_1; + goto __pyx_L0; + + /* "nms/cpu_nms.pyx":14 + * return a if a >= b else b + * + * cdef inline np.float32_t min(np.float32_t a, np.float32_t b): # <<<<<<<<<<<<<< + * return a if a <= b else b + * + */ + + /* function exit code */ + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "nms/cpu_nms.pyx":17 + * return a if a <= b else b + * + * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh): # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0] + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] + */ + +/* Python wrapper */ +static PyObject *__pyx_pw_3nms_7cpu_nms_1cpu_nms(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyMethodDef __pyx_mdef_3nms_7cpu_nms_1cpu_nms = {"cpu_nms", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_3nms_7cpu_nms_1cpu_nms, METH_VARARGS|METH_KEYWORDS, 0}; +static PyObject *__pyx_pw_3nms_7cpu_nms_1cpu_nms(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyArrayObject *__pyx_v_dets = 0; + PyObject *__pyx_v_thresh = 0; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("cpu_nms (wrapper)", 0); + { + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_dets,&__pyx_n_s_thresh,0}; + PyObject* values[2] = {0,0}; + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args; + const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); + switch (pos_args) { + case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); + CYTHON_FALLTHROUGH; + case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); + CYTHON_FALLTHROUGH; + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + kw_args = PyDict_Size(__pyx_kwds); + switch (pos_args) { + case 0: + if (likely((values[0] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_dets)) != 0)) kw_args--; + else goto __pyx_L5_argtuple_error; + CYTHON_FALLTHROUGH; + case 1: + if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_thresh)) != 0)) kw_args--; + else { + __Pyx_RaiseArgtupleInvalid("cpu_nms", 1, 2, 2, 1); __PYX_ERR(0, 17, __pyx_L3_error) + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "cpu_nms") < 0)) __PYX_ERR(0, 17, __pyx_L3_error) + } + } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { + goto __pyx_L5_argtuple_error; + } else { + values[0] = PyTuple_GET_ITEM(__pyx_args, 0); + values[1] = PyTuple_GET_ITEM(__pyx_args, 1); + } + __pyx_v_dets = ((PyArrayObject *)values[0]); + __pyx_v_thresh = ((PyObject*)values[1]); + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("cpu_nms", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 17, __pyx_L3_error) + __pyx_L3_error:; + __Pyx_AddTraceback("nms.cpu_nms.cpu_nms", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_dets), __pyx_ptype_5numpy_ndarray, 1, "dets", 0))) __PYX_ERR(0, 17, __pyx_L1_error) + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_thresh), (&PyFloat_Type), 1, "thresh", 1))) __PYX_ERR(0, 17, __pyx_L1_error) + __pyx_r = __pyx_pf_3nms_7cpu_nms_cpu_nms(__pyx_self, __pyx_v_dets, __pyx_v_thresh); + + /* function exit code */ + goto __pyx_L0; + __pyx_L1_error:; + __pyx_r = NULL; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +static PyObject *__pyx_pf_3nms_7cpu_nms_cpu_nms(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_dets, PyObject *__pyx_v_thresh) { + PyArrayObject *__pyx_v_x1 = 0; + PyArrayObject *__pyx_v_y1 = 0; + PyArrayObject *__pyx_v_x2 = 0; + PyArrayObject *__pyx_v_y2 = 0; + PyArrayObject *__pyx_v_scores = 0; + PyArrayObject *__pyx_v_areas = 0; + PyArrayObject *__pyx_v_order = 0; + int __pyx_v_ndets; + PyArrayObject *__pyx_v_suppressed = 0; + int __pyx_v__i; + int __pyx_v__j; + int __pyx_v_i; + int __pyx_v_j; + __pyx_t_5numpy_float32_t __pyx_v_ix1; + __pyx_t_5numpy_float32_t __pyx_v_iy1; + __pyx_t_5numpy_float32_t __pyx_v_ix2; + __pyx_t_5numpy_float32_t __pyx_v_iy2; + __pyx_t_5numpy_float32_t __pyx_v_iarea; + __pyx_t_5numpy_float32_t __pyx_v_xx1; + __pyx_t_5numpy_float32_t __pyx_v_yy1; + __pyx_t_5numpy_float32_t __pyx_v_xx2; + __pyx_t_5numpy_float32_t __pyx_v_yy2; + __pyx_t_5numpy_float32_t __pyx_v_w; + __pyx_t_5numpy_float32_t __pyx_v_h; + __pyx_t_5numpy_float32_t __pyx_v_inter; + __pyx_t_5numpy_float32_t __pyx_v_ovr; + PyObject *__pyx_v_keep = NULL; + __Pyx_LocalBuf_ND __pyx_pybuffernd_areas; + __Pyx_Buffer __pyx_pybuffer_areas; + __Pyx_LocalBuf_ND __pyx_pybuffernd_dets; + __Pyx_Buffer __pyx_pybuffer_dets; + __Pyx_LocalBuf_ND __pyx_pybuffernd_order; + __Pyx_Buffer __pyx_pybuffer_order; + __Pyx_LocalBuf_ND __pyx_pybuffernd_scores; + __Pyx_Buffer __pyx_pybuffer_scores; + __Pyx_LocalBuf_ND __pyx_pybuffernd_suppressed; + __Pyx_Buffer __pyx_pybuffer_suppressed; + __Pyx_LocalBuf_ND __pyx_pybuffernd_x1; + __Pyx_Buffer __pyx_pybuffer_x1; + __Pyx_LocalBuf_ND __pyx_pybuffernd_x2; + __Pyx_Buffer __pyx_pybuffer_x2; + __Pyx_LocalBuf_ND __pyx_pybuffernd_y1; + __Pyx_Buffer __pyx_pybuffer_y1; + __Pyx_LocalBuf_ND __pyx_pybuffernd_y2; + __Pyx_Buffer __pyx_pybuffer_y2; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + PyArrayObject *__pyx_t_2 = NULL; + PyArrayObject *__pyx_t_3 = NULL; + PyArrayObject *__pyx_t_4 = NULL; + PyArrayObject *__pyx_t_5 = NULL; + PyArrayObject *__pyx_t_6 = NULL; + PyObject *__pyx_t_7 = NULL; + PyObject *__pyx_t_8 = NULL; + PyArrayObject *__pyx_t_9 = NULL; + PyArrayObject *__pyx_t_10 = NULL; + PyObject *__pyx_t_11 = NULL; + PyObject *__pyx_t_12 = NULL; + PyArrayObject *__pyx_t_13 = NULL; + int __pyx_t_14; + int __pyx_t_15; + int __pyx_t_16; + Py_ssize_t __pyx_t_17; + int __pyx_t_18; + int __pyx_t_19; + int __pyx_t_20; + int __pyx_t_21; + int __pyx_t_22; + int __pyx_t_23; + __pyx_t_5numpy_float32_t __pyx_t_24; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("cpu_nms", 0); + __pyx_pybuffer_x1.pybuffer.buf = NULL; + __pyx_pybuffer_x1.refcount = 0; + __pyx_pybuffernd_x1.data = NULL; + __pyx_pybuffernd_x1.rcbuffer = &__pyx_pybuffer_x1; + __pyx_pybuffer_y1.pybuffer.buf = NULL; + __pyx_pybuffer_y1.refcount = 0; + __pyx_pybuffernd_y1.data = NULL; + __pyx_pybuffernd_y1.rcbuffer = &__pyx_pybuffer_y1; + __pyx_pybuffer_x2.pybuffer.buf = NULL; + __pyx_pybuffer_x2.refcount = 0; + __pyx_pybuffernd_x2.data = NULL; + __pyx_pybuffernd_x2.rcbuffer = &__pyx_pybuffer_x2; + __pyx_pybuffer_y2.pybuffer.buf = NULL; + __pyx_pybuffer_y2.refcount = 0; + __pyx_pybuffernd_y2.data = NULL; + __pyx_pybuffernd_y2.rcbuffer = &__pyx_pybuffer_y2; + __pyx_pybuffer_scores.pybuffer.buf = NULL; + __pyx_pybuffer_scores.refcount = 0; + __pyx_pybuffernd_scores.data = NULL; + __pyx_pybuffernd_scores.rcbuffer = &__pyx_pybuffer_scores; + __pyx_pybuffer_areas.pybuffer.buf = NULL; + __pyx_pybuffer_areas.refcount = 0; + __pyx_pybuffernd_areas.data = NULL; + __pyx_pybuffernd_areas.rcbuffer = &__pyx_pybuffer_areas; + __pyx_pybuffer_order.pybuffer.buf = NULL; + __pyx_pybuffer_order.refcount = 0; + __pyx_pybuffernd_order.data = NULL; + __pyx_pybuffernd_order.rcbuffer = &__pyx_pybuffer_order; + __pyx_pybuffer_suppressed.pybuffer.buf = NULL; + __pyx_pybuffer_suppressed.refcount = 0; + __pyx_pybuffernd_suppressed.data = NULL; + __pyx_pybuffernd_suppressed.rcbuffer = &__pyx_pybuffer_suppressed; + __pyx_pybuffer_dets.pybuffer.buf = NULL; + __pyx_pybuffer_dets.refcount = 0; + __pyx_pybuffernd_dets.data = NULL; + __pyx_pybuffernd_dets.rcbuffer = &__pyx_pybuffer_dets; + { + __Pyx_BufFmt_StackElem __pyx_stack[1]; + if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_dets.rcbuffer->pybuffer, (PyObject*)__pyx_v_dets, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) __PYX_ERR(0, 17, __pyx_L1_error) + } + __pyx_pybuffernd_dets.diminfo[0].strides = __pyx_pybuffernd_dets.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_dets.diminfo[0].shape = __pyx_pybuffernd_dets.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_dets.diminfo[1].strides = __pyx_pybuffernd_dets.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_dets.diminfo[1].shape = __pyx_pybuffernd_dets.rcbuffer->pybuffer.shape[1]; + + /* "nms/cpu_nms.pyx":18 + * + * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh): + * cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0] # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] + * cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2] + */ + __pyx_t_1 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_tuple__2); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 18, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 18, __pyx_L1_error) + __pyx_t_2 = ((PyArrayObject *)__pyx_t_1); + { + __Pyx_BufFmt_StackElem __pyx_stack[1]; + if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_x1.rcbuffer->pybuffer, (PyObject*)__pyx_t_2, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) { + __pyx_v_x1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_x1.rcbuffer->pybuffer.buf = NULL; + __PYX_ERR(0, 18, __pyx_L1_error) + } else {__pyx_pybuffernd_x1.diminfo[0].strides = __pyx_pybuffernd_x1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_x1.diminfo[0].shape = __pyx_pybuffernd_x1.rcbuffer->pybuffer.shape[0]; + } + } + __pyx_t_2 = 0; + __pyx_v_x1 = ((PyArrayObject *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "nms/cpu_nms.pyx":19 + * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh): + * cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0] + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2] + * cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3] + */ + __pyx_t_1 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_tuple__3); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 19, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 19, __pyx_L1_error) + __pyx_t_3 = ((PyArrayObject *)__pyx_t_1); + { + __Pyx_BufFmt_StackElem __pyx_stack[1]; + if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_y1.rcbuffer->pybuffer, (PyObject*)__pyx_t_3, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) { + __pyx_v_y1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_y1.rcbuffer->pybuffer.buf = NULL; + __PYX_ERR(0, 19, __pyx_L1_error) + } else {__pyx_pybuffernd_y1.diminfo[0].strides = __pyx_pybuffernd_y1.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_y1.diminfo[0].shape = __pyx_pybuffernd_y1.rcbuffer->pybuffer.shape[0]; + } + } + __pyx_t_3 = 0; + __pyx_v_y1 = ((PyArrayObject *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "nms/cpu_nms.pyx":20 + * cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0] + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] + * cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2] # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3] + * cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4] + */ + __pyx_t_1 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_tuple__4); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 20, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 20, __pyx_L1_error) + __pyx_t_4 = ((PyArrayObject *)__pyx_t_1); + { + __Pyx_BufFmt_StackElem __pyx_stack[1]; + if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_x2.rcbuffer->pybuffer, (PyObject*)__pyx_t_4, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) { + __pyx_v_x2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_x2.rcbuffer->pybuffer.buf = NULL; + __PYX_ERR(0, 20, __pyx_L1_error) + } else {__pyx_pybuffernd_x2.diminfo[0].strides = __pyx_pybuffernd_x2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_x2.diminfo[0].shape = __pyx_pybuffernd_x2.rcbuffer->pybuffer.shape[0]; + } + } + __pyx_t_4 = 0; + __pyx_v_x2 = ((PyArrayObject *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "nms/cpu_nms.pyx":21 + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] + * cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2] + * cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3] # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4] + * + */ + __pyx_t_1 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_tuple__5); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 21, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 21, __pyx_L1_error) + __pyx_t_5 = ((PyArrayObject *)__pyx_t_1); + { + __Pyx_BufFmt_StackElem __pyx_stack[1]; + if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_y2.rcbuffer->pybuffer, (PyObject*)__pyx_t_5, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) { + __pyx_v_y2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_y2.rcbuffer->pybuffer.buf = NULL; + __PYX_ERR(0, 21, __pyx_L1_error) + } else {__pyx_pybuffernd_y2.diminfo[0].strides = __pyx_pybuffernd_y2.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_y2.diminfo[0].shape = __pyx_pybuffernd_y2.rcbuffer->pybuffer.shape[0]; + } + } + __pyx_t_5 = 0; + __pyx_v_y2 = ((PyArrayObject *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "nms/cpu_nms.pyx":22 + * cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2] + * cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3] + * cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4] # <<<<<<<<<<<<<< + * + * cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1) + */ + __pyx_t_1 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_tuple__6); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 22, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 22, __pyx_L1_error) + __pyx_t_6 = ((PyArrayObject *)__pyx_t_1); + { + __Pyx_BufFmt_StackElem __pyx_stack[1]; + if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_scores.rcbuffer->pybuffer, (PyObject*)__pyx_t_6, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) { + __pyx_v_scores = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_scores.rcbuffer->pybuffer.buf = NULL; + __PYX_ERR(0, 22, __pyx_L1_error) + } else {__pyx_pybuffernd_scores.diminfo[0].strides = __pyx_pybuffernd_scores.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_scores.diminfo[0].shape = __pyx_pybuffernd_scores.rcbuffer->pybuffer.shape[0]; + } + } + __pyx_t_6 = 0; + __pyx_v_scores = ((PyArrayObject *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "nms/cpu_nms.pyx":24 + * cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4] + * + * cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1) # <<<<<<<<<<<<<< + * cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1] + * + */ + __pyx_t_1 = PyNumber_Subtract(((PyObject *)__pyx_v_x2), ((PyObject *)__pyx_v_x1)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 24, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_7 = __Pyx_PyInt_AddObjC(__pyx_t_1, __pyx_int_1, 1, 0, 0); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 24, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyNumber_Subtract(((PyObject *)__pyx_v_y2), ((PyObject *)__pyx_v_y1)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 24, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_8 = __Pyx_PyInt_AddObjC(__pyx_t_1, __pyx_int_1, 1, 0, 0); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 24, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_8); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyNumber_Multiply(__pyx_t_7, __pyx_t_8); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 24, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 24, __pyx_L1_error) + __pyx_t_9 = ((PyArrayObject *)__pyx_t_1); + { + __Pyx_BufFmt_StackElem __pyx_stack[1]; + if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_areas.rcbuffer->pybuffer, (PyObject*)__pyx_t_9, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) { + __pyx_v_areas = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_areas.rcbuffer->pybuffer.buf = NULL; + __PYX_ERR(0, 24, __pyx_L1_error) + } else {__pyx_pybuffernd_areas.diminfo[0].strides = __pyx_pybuffernd_areas.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_areas.diminfo[0].shape = __pyx_pybuffernd_areas.rcbuffer->pybuffer.shape[0]; + } + } + __pyx_t_9 = 0; + __pyx_v_areas = ((PyArrayObject *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "nms/cpu_nms.pyx":25 + * + * cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1) + * cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1] # <<<<<<<<<<<<<< + * + * cdef int ndets = dets.shape[0] + */ + __pyx_t_8 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_scores), __pyx_n_s_argsort); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 25, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_8); + __pyx_t_7 = NULL; + if (CYTHON_UNPACK_METHODS && likely(PyMethod_Check(__pyx_t_8))) { + __pyx_t_7 = PyMethod_GET_SELF(__pyx_t_8); + if (likely(__pyx_t_7)) { + PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_8); + __Pyx_INCREF(__pyx_t_7); + __Pyx_INCREF(function); + __Pyx_DECREF_SET(__pyx_t_8, function); + } + } + __pyx_t_1 = (__pyx_t_7) ? __Pyx_PyObject_CallOneArg(__pyx_t_8, __pyx_t_7) : __Pyx_PyObject_CallNoArg(__pyx_t_8); + __Pyx_XDECREF(__pyx_t_7); __pyx_t_7 = 0; + if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 25, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + __pyx_t_8 = __Pyx_PyObject_GetItem(__pyx_t_1, __pyx_slice__7); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 25, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_8); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (!(likely(((__pyx_t_8) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_8, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 25, __pyx_L1_error) + __pyx_t_10 = ((PyArrayObject *)__pyx_t_8); + { + __Pyx_BufFmt_StackElem __pyx_stack[1]; + if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_order.rcbuffer->pybuffer, (PyObject*)__pyx_t_10, &__Pyx_TypeInfo_nn___pyx_t_5numpy_int_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) { + __pyx_v_order = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_order.rcbuffer->pybuffer.buf = NULL; + __PYX_ERR(0, 25, __pyx_L1_error) + } else {__pyx_pybuffernd_order.diminfo[0].strides = __pyx_pybuffernd_order.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_order.diminfo[0].shape = __pyx_pybuffernd_order.rcbuffer->pybuffer.shape[0]; + } + } + __pyx_t_10 = 0; + __pyx_v_order = ((PyArrayObject *)__pyx_t_8); + __pyx_t_8 = 0; + + /* "nms/cpu_nms.pyx":27 + * cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1] + * + * cdef int ndets = dets.shape[0] # <<<<<<<<<<<<<< + * cdef np.ndarray[np.int_t, ndim=1] suppressed = \ + * np.zeros((ndets), dtype=np.int) + */ + __pyx_v_ndets = (__pyx_v_dets->dimensions[0]); + + /* "nms/cpu_nms.pyx":29 + * cdef int ndets = dets.shape[0] + * cdef np.ndarray[np.int_t, ndim=1] suppressed = \ + * np.zeros((ndets), dtype=np.int) # <<<<<<<<<<<<<< + * + * # nominal indices + */ + __Pyx_GetModuleGlobalName(__pyx_t_8, __pyx_n_s_np); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_8); + __pyx_t_1 = __Pyx_PyObject_GetAttrStr(__pyx_t_8, __pyx_n_s_zeros); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + __pyx_t_8 = __Pyx_PyInt_From_int(__pyx_v_ndets); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_8); + __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_7); + __Pyx_GIVEREF(__pyx_t_8); + PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_8); + __pyx_t_8 = 0; + __pyx_t_8 = __Pyx_PyDict_NewPresized(1); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_8); + __Pyx_GetModuleGlobalName(__pyx_t_11, __pyx_n_s_np); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_11); + __pyx_t_12 = __Pyx_PyObject_GetAttrStr(__pyx_t_11, __pyx_n_s_int); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_12); + __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; + if (PyDict_SetItem(__pyx_t_8, __pyx_n_s_dtype, __pyx_t_12) < 0) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0; + __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_t_7, __pyx_t_8); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_12); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + if (!(likely(((__pyx_t_12) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_12, __pyx_ptype_5numpy_ndarray))))) __PYX_ERR(0, 29, __pyx_L1_error) + __pyx_t_13 = ((PyArrayObject *)__pyx_t_12); + { + __Pyx_BufFmt_StackElem __pyx_stack[1]; + if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_suppressed.rcbuffer->pybuffer, (PyObject*)__pyx_t_13, &__Pyx_TypeInfo_nn___pyx_t_5numpy_int_t, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 1, 0, __pyx_stack) == -1)) { + __pyx_v_suppressed = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_suppressed.rcbuffer->pybuffer.buf = NULL; + __PYX_ERR(0, 28, __pyx_L1_error) + } else {__pyx_pybuffernd_suppressed.diminfo[0].strides = __pyx_pybuffernd_suppressed.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_suppressed.diminfo[0].shape = __pyx_pybuffernd_suppressed.rcbuffer->pybuffer.shape[0]; + } + } + __pyx_t_13 = 0; + __pyx_v_suppressed = ((PyArrayObject *)__pyx_t_12); + __pyx_t_12 = 0; + + /* "nms/cpu_nms.pyx":42 + * cdef np.float32_t inter, ovr + * + * keep = [] # <<<<<<<<<<<<<< + * for _i in range(ndets): + * i = order[_i] + */ + __pyx_t_12 = PyList_New(0); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 42, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_12); + __pyx_v_keep = ((PyObject*)__pyx_t_12); + __pyx_t_12 = 0; + + /* "nms/cpu_nms.pyx":43 + * + * keep = [] + * for _i in range(ndets): # <<<<<<<<<<<<<< + * i = order[_i] + * if suppressed[i] == 1: + */ + __pyx_t_14 = __pyx_v_ndets; + __pyx_t_15 = __pyx_t_14; + for (__pyx_t_16 = 0; __pyx_t_16 < __pyx_t_15; __pyx_t_16+=1) { + __pyx_v__i = __pyx_t_16; + + /* "nms/cpu_nms.pyx":44 + * keep = [] + * for _i in range(ndets): + * i = order[_i] # <<<<<<<<<<<<<< + * if suppressed[i] == 1: + * continue + */ + __pyx_t_17 = __pyx_v__i; + __pyx_t_18 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_order.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_18 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_order.diminfo[0].shape)) __pyx_t_18 = 0; + if (unlikely(__pyx_t_18 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_18); + __PYX_ERR(0, 44, __pyx_L1_error) + } + __pyx_v_i = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int_t *, __pyx_pybuffernd_order.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_order.diminfo[0].strides)); + + /* "nms/cpu_nms.pyx":45 + * for _i in range(ndets): + * i = order[_i] + * if suppressed[i] == 1: # <<<<<<<<<<<<<< + * continue + * keep.append(i) + */ + __pyx_t_17 = __pyx_v_i; + __pyx_t_18 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_suppressed.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_18 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_suppressed.diminfo[0].shape)) __pyx_t_18 = 0; + if (unlikely(__pyx_t_18 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_18); + __PYX_ERR(0, 45, __pyx_L1_error) + } + __pyx_t_19 = (((*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int_t *, __pyx_pybuffernd_suppressed.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_suppressed.diminfo[0].strides)) == 1) != 0); + if (__pyx_t_19) { + + /* "nms/cpu_nms.pyx":46 + * i = order[_i] + * if suppressed[i] == 1: + * continue # <<<<<<<<<<<<<< + * keep.append(i) + * ix1 = x1[i] + */ + goto __pyx_L3_continue; + + /* "nms/cpu_nms.pyx":45 + * for _i in range(ndets): + * i = order[_i] + * if suppressed[i] == 1: # <<<<<<<<<<<<<< + * continue + * keep.append(i) + */ + } + + /* "nms/cpu_nms.pyx":47 + * if suppressed[i] == 1: + * continue + * keep.append(i) # <<<<<<<<<<<<<< + * ix1 = x1[i] + * iy1 = y1[i] + */ + __pyx_t_12 = __Pyx_PyInt_From_int(__pyx_v_i); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 47, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_12); + __pyx_t_20 = __Pyx_PyList_Append(__pyx_v_keep, __pyx_t_12); if (unlikely(__pyx_t_20 == ((int)-1))) __PYX_ERR(0, 47, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0; + + /* "nms/cpu_nms.pyx":48 + * continue + * keep.append(i) + * ix1 = x1[i] # <<<<<<<<<<<<<< + * iy1 = y1[i] + * ix2 = x2[i] + */ + __pyx_t_17 = __pyx_v_i; + __pyx_t_18 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_x1.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_18 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_x1.diminfo[0].shape)) __pyx_t_18 = 0; + if (unlikely(__pyx_t_18 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_18); + __PYX_ERR(0, 48, __pyx_L1_error) + } + __pyx_v_ix1 = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_x1.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_x1.diminfo[0].strides)); + + /* "nms/cpu_nms.pyx":49 + * keep.append(i) + * ix1 = x1[i] + * iy1 = y1[i] # <<<<<<<<<<<<<< + * ix2 = x2[i] + * iy2 = y2[i] + */ + __pyx_t_17 = __pyx_v_i; + __pyx_t_18 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_y1.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_18 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_y1.diminfo[0].shape)) __pyx_t_18 = 0; + if (unlikely(__pyx_t_18 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_18); + __PYX_ERR(0, 49, __pyx_L1_error) + } + __pyx_v_iy1 = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_y1.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_y1.diminfo[0].strides)); + + /* "nms/cpu_nms.pyx":50 + * ix1 = x1[i] + * iy1 = y1[i] + * ix2 = x2[i] # <<<<<<<<<<<<<< + * iy2 = y2[i] + * iarea = areas[i] + */ + __pyx_t_17 = __pyx_v_i; + __pyx_t_18 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_x2.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_18 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_x2.diminfo[0].shape)) __pyx_t_18 = 0; + if (unlikely(__pyx_t_18 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_18); + __PYX_ERR(0, 50, __pyx_L1_error) + } + __pyx_v_ix2 = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_x2.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_x2.diminfo[0].strides)); + + /* "nms/cpu_nms.pyx":51 + * iy1 = y1[i] + * ix2 = x2[i] + * iy2 = y2[i] # <<<<<<<<<<<<<< + * iarea = areas[i] + * for _j in range(_i + 1, ndets): + */ + __pyx_t_17 = __pyx_v_i; + __pyx_t_18 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_y2.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_18 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_y2.diminfo[0].shape)) __pyx_t_18 = 0; + if (unlikely(__pyx_t_18 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_18); + __PYX_ERR(0, 51, __pyx_L1_error) + } + __pyx_v_iy2 = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_y2.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_y2.diminfo[0].strides)); + + /* "nms/cpu_nms.pyx":52 + * ix2 = x2[i] + * iy2 = y2[i] + * iarea = areas[i] # <<<<<<<<<<<<<< + * for _j in range(_i + 1, ndets): + * j = order[_j] + */ + __pyx_t_17 = __pyx_v_i; + __pyx_t_18 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_areas.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_18 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_areas.diminfo[0].shape)) __pyx_t_18 = 0; + if (unlikely(__pyx_t_18 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_18); + __PYX_ERR(0, 52, __pyx_L1_error) + } + __pyx_v_iarea = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_areas.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_areas.diminfo[0].strides)); + + /* "nms/cpu_nms.pyx":53 + * iy2 = y2[i] + * iarea = areas[i] + * for _j in range(_i + 1, ndets): # <<<<<<<<<<<<<< + * j = order[_j] + * if suppressed[j] == 1: + */ + __pyx_t_18 = __pyx_v_ndets; + __pyx_t_21 = __pyx_t_18; + for (__pyx_t_22 = (__pyx_v__i + 1); __pyx_t_22 < __pyx_t_21; __pyx_t_22+=1) { + __pyx_v__j = __pyx_t_22; + + /* "nms/cpu_nms.pyx":54 + * iarea = areas[i] + * for _j in range(_i + 1, ndets): + * j = order[_j] # <<<<<<<<<<<<<< + * if suppressed[j] == 1: + * continue + */ + __pyx_t_17 = __pyx_v__j; + __pyx_t_23 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_order.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_23 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_order.diminfo[0].shape)) __pyx_t_23 = 0; + if (unlikely(__pyx_t_23 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_23); + __PYX_ERR(0, 54, __pyx_L1_error) + } + __pyx_v_j = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int_t *, __pyx_pybuffernd_order.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_order.diminfo[0].strides)); + + /* "nms/cpu_nms.pyx":55 + * for _j in range(_i + 1, ndets): + * j = order[_j] + * if suppressed[j] == 1: # <<<<<<<<<<<<<< + * continue + * xx1 = max(ix1, x1[j]) + */ + __pyx_t_17 = __pyx_v_j; + __pyx_t_23 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_suppressed.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_23 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_suppressed.diminfo[0].shape)) __pyx_t_23 = 0; + if (unlikely(__pyx_t_23 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_23); + __PYX_ERR(0, 55, __pyx_L1_error) + } + __pyx_t_19 = (((*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int_t *, __pyx_pybuffernd_suppressed.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_suppressed.diminfo[0].strides)) == 1) != 0); + if (__pyx_t_19) { + + /* "nms/cpu_nms.pyx":56 + * j = order[_j] + * if suppressed[j] == 1: + * continue # <<<<<<<<<<<<<< + * xx1 = max(ix1, x1[j]) + * yy1 = max(iy1, y1[j]) + */ + goto __pyx_L6_continue; + + /* "nms/cpu_nms.pyx":55 + * for _j in range(_i + 1, ndets): + * j = order[_j] + * if suppressed[j] == 1: # <<<<<<<<<<<<<< + * continue + * xx1 = max(ix1, x1[j]) + */ + } + + /* "nms/cpu_nms.pyx":57 + * if suppressed[j] == 1: + * continue + * xx1 = max(ix1, x1[j]) # <<<<<<<<<<<<<< + * yy1 = max(iy1, y1[j]) + * xx2 = min(ix2, x2[j]) + */ + __pyx_t_17 = __pyx_v_j; + __pyx_t_23 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_x1.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_23 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_x1.diminfo[0].shape)) __pyx_t_23 = 0; + if (unlikely(__pyx_t_23 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_23); + __PYX_ERR(0, 57, __pyx_L1_error) + } + __pyx_v_xx1 = __pyx_f_3nms_7cpu_nms_max(__pyx_v_ix1, (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_x1.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_x1.diminfo[0].strides))); + + /* "nms/cpu_nms.pyx":58 + * continue + * xx1 = max(ix1, x1[j]) + * yy1 = max(iy1, y1[j]) # <<<<<<<<<<<<<< + * xx2 = min(ix2, x2[j]) + * yy2 = min(iy2, y2[j]) + */ + __pyx_t_17 = __pyx_v_j; + __pyx_t_23 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_y1.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_23 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_y1.diminfo[0].shape)) __pyx_t_23 = 0; + if (unlikely(__pyx_t_23 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_23); + __PYX_ERR(0, 58, __pyx_L1_error) + } + __pyx_v_yy1 = __pyx_f_3nms_7cpu_nms_max(__pyx_v_iy1, (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_y1.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_y1.diminfo[0].strides))); + + /* "nms/cpu_nms.pyx":59 + * xx1 = max(ix1, x1[j]) + * yy1 = max(iy1, y1[j]) + * xx2 = min(ix2, x2[j]) # <<<<<<<<<<<<<< + * yy2 = min(iy2, y2[j]) + * w = max(0.0, xx2 - xx1 + 1) + */ + __pyx_t_17 = __pyx_v_j; + __pyx_t_23 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_x2.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_23 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_x2.diminfo[0].shape)) __pyx_t_23 = 0; + if (unlikely(__pyx_t_23 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_23); + __PYX_ERR(0, 59, __pyx_L1_error) + } + __pyx_v_xx2 = __pyx_f_3nms_7cpu_nms_min(__pyx_v_ix2, (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_x2.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_x2.diminfo[0].strides))); + + /* "nms/cpu_nms.pyx":60 + * yy1 = max(iy1, y1[j]) + * xx2 = min(ix2, x2[j]) + * yy2 = min(iy2, y2[j]) # <<<<<<<<<<<<<< + * w = max(0.0, xx2 - xx1 + 1) + * h = max(0.0, yy2 - yy1 + 1) + */ + __pyx_t_17 = __pyx_v_j; + __pyx_t_23 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_y2.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_23 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_y2.diminfo[0].shape)) __pyx_t_23 = 0; + if (unlikely(__pyx_t_23 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_23); + __PYX_ERR(0, 60, __pyx_L1_error) + } + __pyx_v_yy2 = __pyx_f_3nms_7cpu_nms_min(__pyx_v_iy2, (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_y2.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_y2.diminfo[0].strides))); + + /* "nms/cpu_nms.pyx":61 + * xx2 = min(ix2, x2[j]) + * yy2 = min(iy2, y2[j]) + * w = max(0.0, xx2 - xx1 + 1) # <<<<<<<<<<<<<< + * h = max(0.0, yy2 - yy1 + 1) + * inter = w * h + */ + __pyx_v_w = __pyx_f_3nms_7cpu_nms_max(0.0, ((__pyx_v_xx2 - __pyx_v_xx1) + 1.0)); + + /* "nms/cpu_nms.pyx":62 + * yy2 = min(iy2, y2[j]) + * w = max(0.0, xx2 - xx1 + 1) + * h = max(0.0, yy2 - yy1 + 1) # <<<<<<<<<<<<<< + * inter = w * h + * ovr = inter / (iarea + areas[j] - inter) + */ + __pyx_v_h = __pyx_f_3nms_7cpu_nms_max(0.0, ((__pyx_v_yy2 - __pyx_v_yy1) + 1.0)); + + /* "nms/cpu_nms.pyx":63 + * w = max(0.0, xx2 - xx1 + 1) + * h = max(0.0, yy2 - yy1 + 1) + * inter = w * h # <<<<<<<<<<<<<< + * ovr = inter / (iarea + areas[j] - inter) + * if ovr >= thresh: + */ + __pyx_v_inter = (__pyx_v_w * __pyx_v_h); + + /* "nms/cpu_nms.pyx":64 + * h = max(0.0, yy2 - yy1 + 1) + * inter = w * h + * ovr = inter / (iarea + areas[j] - inter) # <<<<<<<<<<<<<< + * if ovr >= thresh: + * suppressed[j] = 1 + */ + __pyx_t_17 = __pyx_v_j; + __pyx_t_23 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_areas.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_23 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_areas.diminfo[0].shape)) __pyx_t_23 = 0; + if (unlikely(__pyx_t_23 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_23); + __PYX_ERR(0, 64, __pyx_L1_error) + } + __pyx_t_24 = ((__pyx_v_iarea + (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_areas.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_areas.diminfo[0].strides))) - __pyx_v_inter); + if (unlikely(__pyx_t_24 == 0)) { + PyErr_SetString(PyExc_ZeroDivisionError, "float division"); + __PYX_ERR(0, 64, __pyx_L1_error) + } + __pyx_v_ovr = (__pyx_v_inter / __pyx_t_24); + + /* "nms/cpu_nms.pyx":65 + * inter = w * h + * ovr = inter / (iarea + areas[j] - inter) + * if ovr >= thresh: # <<<<<<<<<<<<<< + * suppressed[j] = 1 + * + */ + __pyx_t_12 = PyFloat_FromDouble(__pyx_v_ovr); if (unlikely(!__pyx_t_12)) __PYX_ERR(0, 65, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_12); + __pyx_t_8 = PyObject_RichCompare(__pyx_t_12, __pyx_v_thresh, Py_GE); __Pyx_XGOTREF(__pyx_t_8); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 65, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0; + __pyx_t_19 = __Pyx_PyObject_IsTrue(__pyx_t_8); if (unlikely(__pyx_t_19 < 0)) __PYX_ERR(0, 65, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + if (__pyx_t_19) { + + /* "nms/cpu_nms.pyx":66 + * ovr = inter / (iarea + areas[j] - inter) + * if ovr >= thresh: + * suppressed[j] = 1 # <<<<<<<<<<<<<< + * + * return keep + */ + __pyx_t_17 = __pyx_v_j; + __pyx_t_23 = -1; + if (__pyx_t_17 < 0) { + __pyx_t_17 += __pyx_pybuffernd_suppressed.diminfo[0].shape; + if (unlikely(__pyx_t_17 < 0)) __pyx_t_23 = 0; + } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_suppressed.diminfo[0].shape)) __pyx_t_23 = 0; + if (unlikely(__pyx_t_23 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_23); + __PYX_ERR(0, 66, __pyx_L1_error) + } + *__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int_t *, __pyx_pybuffernd_suppressed.rcbuffer->pybuffer.buf, __pyx_t_17, __pyx_pybuffernd_suppressed.diminfo[0].strides) = 1; + + /* "nms/cpu_nms.pyx":65 + * inter = w * h + * ovr = inter / (iarea + areas[j] - inter) + * if ovr >= thresh: # <<<<<<<<<<<<<< + * suppressed[j] = 1 + * + */ + } + __pyx_L6_continue:; + } + __pyx_L3_continue:; + } + + /* "nms/cpu_nms.pyx":68 + * suppressed[j] = 1 + * + * return keep # <<<<<<<<<<<<<< + * + * def cpu_soft_nms(np.ndarray[float, ndim=2] boxes, float sigma=0.5, float Nt=0.3, float threshold=0.001, unsigned int method=0): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(__pyx_v_keep); + __pyx_r = __pyx_v_keep; + goto __pyx_L0; + + /* "nms/cpu_nms.pyx":17 + * return a if a <= b else b + * + * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh): # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0] + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] + */ + + /* function exit code */ + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_8); + __Pyx_XDECREF(__pyx_t_11); + __Pyx_XDECREF(__pyx_t_12); + { PyObject *__pyx_type, *__pyx_value, *__pyx_tb; + __Pyx_PyThreadState_declare + __Pyx_PyThreadState_assign + __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_areas.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_dets.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_order.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_scores.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_suppressed.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_x1.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_x2.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_y1.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_y2.rcbuffer->pybuffer); + __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);} + __Pyx_AddTraceback("nms.cpu_nms.cpu_nms", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + goto __pyx_L2; + __pyx_L0:; + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_areas.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_dets.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_order.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_scores.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_suppressed.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_x1.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_x2.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_y1.rcbuffer->pybuffer); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_y2.rcbuffer->pybuffer); + __pyx_L2:; + __Pyx_XDECREF((PyObject *)__pyx_v_x1); + __Pyx_XDECREF((PyObject *)__pyx_v_y1); + __Pyx_XDECREF((PyObject *)__pyx_v_x2); + __Pyx_XDECREF((PyObject *)__pyx_v_y2); + __Pyx_XDECREF((PyObject *)__pyx_v_scores); + __Pyx_XDECREF((PyObject *)__pyx_v_areas); + __Pyx_XDECREF((PyObject *)__pyx_v_order); + __Pyx_XDECREF((PyObject *)__pyx_v_suppressed); + __Pyx_XDECREF(__pyx_v_keep); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "nms/cpu_nms.pyx":70 + * return keep + * + * def cpu_soft_nms(np.ndarray[float, ndim=2] boxes, float sigma=0.5, float Nt=0.3, float threshold=0.001, unsigned int method=0): # <<<<<<<<<<<<<< + * cdef unsigned int N = boxes.shape[0] + * cdef float iw, ih, box_area + */ + +/* Python wrapper */ +static PyObject *__pyx_pw_3nms_7cpu_nms_3cpu_soft_nms(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyMethodDef __pyx_mdef_3nms_7cpu_nms_3cpu_soft_nms = {"cpu_soft_nms", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_3nms_7cpu_nms_3cpu_soft_nms, METH_VARARGS|METH_KEYWORDS, 0}; +static PyObject *__pyx_pw_3nms_7cpu_nms_3cpu_soft_nms(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyArrayObject *__pyx_v_boxes = 0; + float __pyx_v_sigma; + float __pyx_v_Nt; + float __pyx_v_threshold; + unsigned int __pyx_v_method; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("cpu_soft_nms (wrapper)", 0); + { + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_boxes,&__pyx_n_s_sigma,&__pyx_n_s_Nt,&__pyx_n_s_threshold,&__pyx_n_s_method,0}; + PyObject* values[5] = {0,0,0,0,0}; + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args; + const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); + switch (pos_args) { + case 5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4); + CYTHON_FALLTHROUGH; + case 4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3); + CYTHON_FALLTHROUGH; + case 3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2); + CYTHON_FALLTHROUGH; + case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); + CYTHON_FALLTHROUGH; + case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); + CYTHON_FALLTHROUGH; + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + kw_args = PyDict_Size(__pyx_kwds); + switch (pos_args) { + case 0: + if (likely((values[0] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_boxes)) != 0)) kw_args--; + else goto __pyx_L5_argtuple_error; + CYTHON_FALLTHROUGH; + case 1: + if (kw_args > 0) { + PyObject* value = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_sigma); + if (value) { values[1] = value; kw_args--; } + } + CYTHON_FALLTHROUGH; + case 2: + if (kw_args > 0) { + PyObject* value = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_Nt); + if (value) { values[2] = value; kw_args--; } + } + CYTHON_FALLTHROUGH; + case 3: + if (kw_args > 0) { + PyObject* value = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_threshold); + if (value) { values[3] = value; kw_args--; } + } + CYTHON_FALLTHROUGH; + case 4: + if (kw_args > 0) { + PyObject* value = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_method); + if (value) { values[4] = value; kw_args--; } + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "cpu_soft_nms") < 0)) __PYX_ERR(0, 70, __pyx_L3_error) + } + } else { + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 5: values[4] = PyTuple_GET_ITEM(__pyx_args, 4); + CYTHON_FALLTHROUGH; + case 4: values[3] = PyTuple_GET_ITEM(__pyx_args, 3); + CYTHON_FALLTHROUGH; + case 3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2); + CYTHON_FALLTHROUGH; + case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); + CYTHON_FALLTHROUGH; + case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); + break; + default: goto __pyx_L5_argtuple_error; + } + } + __pyx_v_boxes = ((PyArrayObject *)values[0]); + if (values[1]) { + __pyx_v_sigma = __pyx_PyFloat_AsFloat(values[1]); if (unlikely((__pyx_v_sigma == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 70, __pyx_L3_error) + } else { + __pyx_v_sigma = ((float)0.5); + } + if (values[2]) { + __pyx_v_Nt = __pyx_PyFloat_AsFloat(values[2]); if (unlikely((__pyx_v_Nt == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 70, __pyx_L3_error) + } else { + __pyx_v_Nt = ((float)0.3); + } + if (values[3]) { + __pyx_v_threshold = __pyx_PyFloat_AsFloat(values[3]); if (unlikely((__pyx_v_threshold == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 70, __pyx_L3_error) + } else { + __pyx_v_threshold = ((float)0.001); + } + if (values[4]) { + __pyx_v_method = __Pyx_PyInt_As_unsigned_int(values[4]); if (unlikely((__pyx_v_method == (unsigned int)-1) && PyErr_Occurred())) __PYX_ERR(0, 70, __pyx_L3_error) + } else { + __pyx_v_method = ((unsigned int)0); + } + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("cpu_soft_nms", 0, 1, 5, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 70, __pyx_L3_error) + __pyx_L3_error:; + __Pyx_AddTraceback("nms.cpu_nms.cpu_soft_nms", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_boxes), __pyx_ptype_5numpy_ndarray, 1, "boxes", 0))) __PYX_ERR(0, 70, __pyx_L1_error) + __pyx_r = __pyx_pf_3nms_7cpu_nms_2cpu_soft_nms(__pyx_self, __pyx_v_boxes, __pyx_v_sigma, __pyx_v_Nt, __pyx_v_threshold, __pyx_v_method); + + /* function exit code */ + goto __pyx_L0; + __pyx_L1_error:; + __pyx_r = NULL; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +static PyObject *__pyx_pf_3nms_7cpu_nms_2cpu_soft_nms(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_boxes, float __pyx_v_sigma, float __pyx_v_Nt, float __pyx_v_threshold, unsigned int __pyx_v_method) { + unsigned int __pyx_v_N; + float __pyx_v_iw; + float __pyx_v_ih; + float __pyx_v_ua; + int __pyx_v_pos; + float __pyx_v_maxscore; + int __pyx_v_maxpos; + float __pyx_v_x1; + float __pyx_v_x2; + float __pyx_v_y1; + float __pyx_v_y2; + float __pyx_v_tx1; + float __pyx_v_tx2; + float __pyx_v_ty1; + float __pyx_v_ty2; + float __pyx_v_ts; + float __pyx_v_area; + float __pyx_v_weight; + float __pyx_v_ov; + PyObject *__pyx_v_i = NULL; + CYTHON_UNUSED PyObject *__pyx_v_s = NULL; + PyObject *__pyx_v_keep = NULL; + __Pyx_LocalBuf_ND __pyx_pybuffernd_boxes; + __Pyx_Buffer __pyx_pybuffer_boxes; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + Py_ssize_t __pyx_t_3; + PyObject *(*__pyx_t_4)(PyObject *); + PyObject *__pyx_t_5 = NULL; + float __pyx_t_6; + int __pyx_t_7; + int __pyx_t_8; + Py_ssize_t __pyx_t_9; + Py_ssize_t __pyx_t_10; + PyObject *__pyx_t_11 = NULL; + PyObject *__pyx_t_12 = NULL; + Py_ssize_t __pyx_t_13; + Py_ssize_t __pyx_t_14; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("cpu_soft_nms", 0); + __pyx_pybuffer_boxes.pybuffer.buf = NULL; + __pyx_pybuffer_boxes.refcount = 0; + __pyx_pybuffernd_boxes.data = NULL; + __pyx_pybuffernd_boxes.rcbuffer = &__pyx_pybuffer_boxes; + { + __Pyx_BufFmt_StackElem __pyx_stack[1]; + if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_boxes.rcbuffer->pybuffer, (PyObject*)__pyx_v_boxes, &__Pyx_TypeInfo_float, PyBUF_FORMAT| PyBUF_STRIDES| PyBUF_WRITABLE, 2, 0, __pyx_stack) == -1)) __PYX_ERR(0, 70, __pyx_L1_error) + } + __pyx_pybuffernd_boxes.diminfo[0].strides = __pyx_pybuffernd_boxes.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_boxes.diminfo[0].shape = __pyx_pybuffernd_boxes.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_boxes.diminfo[1].strides = __pyx_pybuffernd_boxes.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_boxes.diminfo[1].shape = __pyx_pybuffernd_boxes.rcbuffer->pybuffer.shape[1]; + + /* "nms/cpu_nms.pyx":71 + * + * def cpu_soft_nms(np.ndarray[float, ndim=2] boxes, float sigma=0.5, float Nt=0.3, float threshold=0.001, unsigned int method=0): + * cdef unsigned int N = boxes.shape[0] # <<<<<<<<<<<<<< + * cdef float iw, ih, box_area + * cdef float ua + */ + __pyx_v_N = (__pyx_v_boxes->dimensions[0]); + + /* "nms/cpu_nms.pyx":74 + * cdef float iw, ih, box_area + * cdef float ua + * cdef int pos = 0 # <<<<<<<<<<<<<< + * cdef float maxscore = 0 + * cdef int maxpos = 0 + */ + __pyx_v_pos = 0; + + /* "nms/cpu_nms.pyx":75 + * cdef float ua + * cdef int pos = 0 + * cdef float maxscore = 0 # <<<<<<<<<<<<<< + * cdef int maxpos = 0 + * cdef float x1,x2,y1,y2,tx1,tx2,ty1,ty2,ts,area,weight,ov + */ + __pyx_v_maxscore = 0.0; + + /* "nms/cpu_nms.pyx":76 + * cdef int pos = 0 + * cdef float maxscore = 0 + * cdef int maxpos = 0 # <<<<<<<<<<<<<< + * cdef float x1,x2,y1,y2,tx1,tx2,ty1,ty2,ts,area,weight,ov + * + */ + __pyx_v_maxpos = 0; + + /* "nms/cpu_nms.pyx":79 + * cdef float x1,x2,y1,y2,tx1,tx2,ty1,ty2,ts,area,weight,ov + * + * for i in range(N): # <<<<<<<<<<<<<< + * maxscore = boxes[i, 4] + * maxpos = i + */ + __pyx_t_1 = __Pyx_PyInt_From_unsigned_int(__pyx_v_N); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 79, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = __Pyx_PyObject_CallOneArg(__pyx_builtin_range, __pyx_t_1); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 79, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (likely(PyList_CheckExact(__pyx_t_2)) || PyTuple_CheckExact(__pyx_t_2)) { + __pyx_t_1 = __pyx_t_2; __Pyx_INCREF(__pyx_t_1); __pyx_t_3 = 0; + __pyx_t_4 = NULL; + } else { + __pyx_t_3 = -1; __pyx_t_1 = PyObject_GetIter(__pyx_t_2); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 79, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_4 = Py_TYPE(__pyx_t_1)->tp_iternext; if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 79, __pyx_L1_error) + } + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + for (;;) { + if (likely(!__pyx_t_4)) { + if (likely(PyList_CheckExact(__pyx_t_1))) { + if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_1)) break; + #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS + __pyx_t_2 = PyList_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) __PYX_ERR(0, 79, __pyx_L1_error) + #else + __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 79, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + #endif + } else { + if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_1)) break; + #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS + __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_3); __Pyx_INCREF(__pyx_t_2); __pyx_t_3++; if (unlikely(0 < 0)) __PYX_ERR(0, 79, __pyx_L1_error) + #else + __pyx_t_2 = PySequence_ITEM(__pyx_t_1, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 79, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + #endif + } + } else { + __pyx_t_2 = __pyx_t_4(__pyx_t_1); + if (unlikely(!__pyx_t_2)) { + PyObject* exc_type = PyErr_Occurred(); + if (exc_type) { + if (likely(__Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear(); + else __PYX_ERR(0, 79, __pyx_L1_error) + } + break; + } + __Pyx_GOTREF(__pyx_t_2); + } + __Pyx_XDECREF_SET(__pyx_v_i, __pyx_t_2); + __pyx_t_2 = 0; + + /* "nms/cpu_nms.pyx":80 + * + * for i in range(N): + * maxscore = boxes[i, 4] # <<<<<<<<<<<<<< + * maxpos = i + * + */ + __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 80, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_4); + __Pyx_GIVEREF(__pyx_int_4); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_int_4); + __pyx_t_5 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_boxes), __pyx_t_2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 80, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_5); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 80, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_maxscore = __pyx_t_6; + + /* "nms/cpu_nms.pyx":81 + * for i in range(N): + * maxscore = boxes[i, 4] + * maxpos = i # <<<<<<<<<<<<<< + * + * tx1 = boxes[i,0] + */ + __pyx_t_7 = __Pyx_PyInt_As_int(__pyx_v_i); if (unlikely((__pyx_t_7 == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 81, __pyx_L1_error) + __pyx_v_maxpos = __pyx_t_7; + + /* "nms/cpu_nms.pyx":83 + * maxpos = i + * + * tx1 = boxes[i,0] # <<<<<<<<<<<<<< + * ty1 = boxes[i,1] + * tx2 = boxes[i,2] + */ + __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 83, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_0); + __Pyx_GIVEREF(__pyx_int_0); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_0); + __pyx_t_2 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_boxes), __pyx_t_5); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 83, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_2); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 83, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_v_tx1 = __pyx_t_6; + + /* "nms/cpu_nms.pyx":84 + * + * tx1 = boxes[i,0] + * ty1 = boxes[i,1] # <<<<<<<<<<<<<< + * tx2 = boxes[i,2] + * ty2 = boxes[i,3] + */ + __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 84, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_1); + __Pyx_GIVEREF(__pyx_int_1); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_int_1); + __pyx_t_5 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_boxes), __pyx_t_2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 84, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_5); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 84, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_ty1 = __pyx_t_6; + + /* "nms/cpu_nms.pyx":85 + * tx1 = boxes[i,0] + * ty1 = boxes[i,1] + * tx2 = boxes[i,2] # <<<<<<<<<<<<<< + * ty2 = boxes[i,3] + * ts = boxes[i,4] + */ + __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 85, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_2); + __Pyx_GIVEREF(__pyx_int_2); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_2); + __pyx_t_2 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_boxes), __pyx_t_5); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 85, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_2); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 85, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_v_tx2 = __pyx_t_6; + + /* "nms/cpu_nms.pyx":86 + * ty1 = boxes[i,1] + * tx2 = boxes[i,2] + * ty2 = boxes[i,3] # <<<<<<<<<<<<<< + * ts = boxes[i,4] + * + */ + __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 86, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_3); + __Pyx_GIVEREF(__pyx_int_3); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_int_3); + __pyx_t_5 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_boxes), __pyx_t_2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 86, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_5); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 86, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_ty2 = __pyx_t_6; + + /* "nms/cpu_nms.pyx":87 + * tx2 = boxes[i,2] + * ty2 = boxes[i,3] + * ts = boxes[i,4] # <<<<<<<<<<<<<< + * + * pos = i + 1 + */ + __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 87, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_4); + __Pyx_GIVEREF(__pyx_int_4); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_4); + __pyx_t_2 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_boxes), __pyx_t_5); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 87, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_2); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 87, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_v_ts = __pyx_t_6; + + /* "nms/cpu_nms.pyx":89 + * ts = boxes[i,4] + * + * pos = i + 1 # <<<<<<<<<<<<<< + * # get max box + * while pos < N: + */ + __pyx_t_2 = __Pyx_PyInt_AddObjC(__pyx_v_i, __pyx_int_1, 1, 0, 0); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 89, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_7 = __Pyx_PyInt_As_int(__pyx_t_2); if (unlikely((__pyx_t_7 == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 89, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_v_pos = __pyx_t_7; + + /* "nms/cpu_nms.pyx":91 + * pos = i + 1 + * # get max box + * while pos < N: # <<<<<<<<<<<<<< + * if maxscore < boxes[pos, 4]: + * maxscore = boxes[pos, 4] + */ + while (1) { + __pyx_t_8 = ((__pyx_v_pos < __pyx_v_N) != 0); + if (!__pyx_t_8) break; + + /* "nms/cpu_nms.pyx":92 + * # get max box + * while pos < N: + * if maxscore < boxes[pos, 4]: # <<<<<<<<<<<<<< + * maxscore = boxes[pos, 4] + * maxpos = pos + */ + __pyx_t_9 = __pyx_v_pos; + __pyx_t_10 = 4; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 92, __pyx_L1_error) + } + __pyx_t_8 = ((__pyx_v_maxscore < (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides))) != 0); + if (__pyx_t_8) { + + /* "nms/cpu_nms.pyx":93 + * while pos < N: + * if maxscore < boxes[pos, 4]: + * maxscore = boxes[pos, 4] # <<<<<<<<<<<<<< + * maxpos = pos + * pos = pos + 1 + */ + __pyx_t_10 = __pyx_v_pos; + __pyx_t_9 = 4; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 93, __pyx_L1_error) + } + __pyx_v_maxscore = (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides)); + + /* "nms/cpu_nms.pyx":94 + * if maxscore < boxes[pos, 4]: + * maxscore = boxes[pos, 4] + * maxpos = pos # <<<<<<<<<<<<<< + * pos = pos + 1 + * + */ + __pyx_v_maxpos = __pyx_v_pos; + + /* "nms/cpu_nms.pyx":92 + * # get max box + * while pos < N: + * if maxscore < boxes[pos, 4]: # <<<<<<<<<<<<<< + * maxscore = boxes[pos, 4] + * maxpos = pos + */ + } + + /* "nms/cpu_nms.pyx":95 + * maxscore = boxes[pos, 4] + * maxpos = pos + * pos = pos + 1 # <<<<<<<<<<<<<< + * + * # add max box as a detection + */ + __pyx_v_pos = (__pyx_v_pos + 1); + } + + /* "nms/cpu_nms.pyx":98 + * + * # add max box as a detection + * boxes[i,0] = boxes[maxpos,0] # <<<<<<<<<<<<<< + * boxes[i,1] = boxes[maxpos,1] + * boxes[i,2] = boxes[maxpos,2] + */ + __pyx_t_9 = __pyx_v_maxpos; + __pyx_t_10 = 0; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 98, __pyx_L1_error) + } + __pyx_t_2 = PyFloat_FromDouble((*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides))); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 98, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 98, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_0); + __Pyx_GIVEREF(__pyx_int_0); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_0); + if (unlikely(PyObject_SetItem(((PyObject *)__pyx_v_boxes), __pyx_t_5, __pyx_t_2) < 0)) __PYX_ERR(0, 98, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "nms/cpu_nms.pyx":99 + * # add max box as a detection + * boxes[i,0] = boxes[maxpos,0] + * boxes[i,1] = boxes[maxpos,1] # <<<<<<<<<<<<<< + * boxes[i,2] = boxes[maxpos,2] + * boxes[i,3] = boxes[maxpos,3] + */ + __pyx_t_10 = __pyx_v_maxpos; + __pyx_t_9 = 1; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 99, __pyx_L1_error) + } + __pyx_t_2 = PyFloat_FromDouble((*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides))); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 99, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 99, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_1); + __Pyx_GIVEREF(__pyx_int_1); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_1); + if (unlikely(PyObject_SetItem(((PyObject *)__pyx_v_boxes), __pyx_t_5, __pyx_t_2) < 0)) __PYX_ERR(0, 99, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "nms/cpu_nms.pyx":100 + * boxes[i,0] = boxes[maxpos,0] + * boxes[i,1] = boxes[maxpos,1] + * boxes[i,2] = boxes[maxpos,2] # <<<<<<<<<<<<<< + * boxes[i,3] = boxes[maxpos,3] + * boxes[i,4] = boxes[maxpos,4] + */ + __pyx_t_9 = __pyx_v_maxpos; + __pyx_t_10 = 2; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 100, __pyx_L1_error) + } + __pyx_t_2 = PyFloat_FromDouble((*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides))); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 100, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 100, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_2); + __Pyx_GIVEREF(__pyx_int_2); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_2); + if (unlikely(PyObject_SetItem(((PyObject *)__pyx_v_boxes), __pyx_t_5, __pyx_t_2) < 0)) __PYX_ERR(0, 100, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "nms/cpu_nms.pyx":101 + * boxes[i,1] = boxes[maxpos,1] + * boxes[i,2] = boxes[maxpos,2] + * boxes[i,3] = boxes[maxpos,3] # <<<<<<<<<<<<<< + * boxes[i,4] = boxes[maxpos,4] + * + */ + __pyx_t_10 = __pyx_v_maxpos; + __pyx_t_9 = 3; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 101, __pyx_L1_error) + } + __pyx_t_2 = PyFloat_FromDouble((*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides))); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 101, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 101, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_3); + __Pyx_GIVEREF(__pyx_int_3); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_3); + if (unlikely(PyObject_SetItem(((PyObject *)__pyx_v_boxes), __pyx_t_5, __pyx_t_2) < 0)) __PYX_ERR(0, 101, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "nms/cpu_nms.pyx":102 + * boxes[i,2] = boxes[maxpos,2] + * boxes[i,3] = boxes[maxpos,3] + * boxes[i,4] = boxes[maxpos,4] # <<<<<<<<<<<<<< + * + * # swap ith box with position of max box + */ + __pyx_t_9 = __pyx_v_maxpos; + __pyx_t_10 = 4; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 102, __pyx_L1_error) + } + __pyx_t_2 = PyFloat_FromDouble((*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides))); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 102, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 102, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_4); + __Pyx_GIVEREF(__pyx_int_4); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_4); + if (unlikely(PyObject_SetItem(((PyObject *)__pyx_v_boxes), __pyx_t_5, __pyx_t_2) < 0)) __PYX_ERR(0, 102, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + + /* "nms/cpu_nms.pyx":105 + * + * # swap ith box with position of max box + * boxes[maxpos,0] = tx1 # <<<<<<<<<<<<<< + * boxes[maxpos,1] = ty1 + * boxes[maxpos,2] = tx2 + */ + __pyx_t_10 = __pyx_v_maxpos; + __pyx_t_9 = 0; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 105, __pyx_L1_error) + } + *__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides) = __pyx_v_tx1; + + /* "nms/cpu_nms.pyx":106 + * # swap ith box with position of max box + * boxes[maxpos,0] = tx1 + * boxes[maxpos,1] = ty1 # <<<<<<<<<<<<<< + * boxes[maxpos,2] = tx2 + * boxes[maxpos,3] = ty2 + */ + __pyx_t_9 = __pyx_v_maxpos; + __pyx_t_10 = 1; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 106, __pyx_L1_error) + } + *__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides) = __pyx_v_ty1; + + /* "nms/cpu_nms.pyx":107 + * boxes[maxpos,0] = tx1 + * boxes[maxpos,1] = ty1 + * boxes[maxpos,2] = tx2 # <<<<<<<<<<<<<< + * boxes[maxpos,3] = ty2 + * boxes[maxpos,4] = ts + */ + __pyx_t_10 = __pyx_v_maxpos; + __pyx_t_9 = 2; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 107, __pyx_L1_error) + } + *__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides) = __pyx_v_tx2; + + /* "nms/cpu_nms.pyx":108 + * boxes[maxpos,1] = ty1 + * boxes[maxpos,2] = tx2 + * boxes[maxpos,3] = ty2 # <<<<<<<<<<<<<< + * boxes[maxpos,4] = ts + * + */ + __pyx_t_9 = __pyx_v_maxpos; + __pyx_t_10 = 3; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 108, __pyx_L1_error) + } + *__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides) = __pyx_v_ty2; + + /* "nms/cpu_nms.pyx":109 + * boxes[maxpos,2] = tx2 + * boxes[maxpos,3] = ty2 + * boxes[maxpos,4] = ts # <<<<<<<<<<<<<< + * + * tx1 = boxes[i,0] + */ + __pyx_t_10 = __pyx_v_maxpos; + __pyx_t_9 = 4; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 109, __pyx_L1_error) + } + *__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides) = __pyx_v_ts; + + /* "nms/cpu_nms.pyx":111 + * boxes[maxpos,4] = ts + * + * tx1 = boxes[i,0] # <<<<<<<<<<<<<< + * ty1 = boxes[i,1] + * tx2 = boxes[i,2] + */ + __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 111, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_0); + __Pyx_GIVEREF(__pyx_int_0); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_int_0); + __pyx_t_5 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_boxes), __pyx_t_2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 111, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_5); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 111, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_tx1 = __pyx_t_6; + + /* "nms/cpu_nms.pyx":112 + * + * tx1 = boxes[i,0] + * ty1 = boxes[i,1] # <<<<<<<<<<<<<< + * tx2 = boxes[i,2] + * ty2 = boxes[i,3] + */ + __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 112, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_1); + __Pyx_GIVEREF(__pyx_int_1); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_1); + __pyx_t_2 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_boxes), __pyx_t_5); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 112, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_2); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 112, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_v_ty1 = __pyx_t_6; + + /* "nms/cpu_nms.pyx":113 + * tx1 = boxes[i,0] + * ty1 = boxes[i,1] + * tx2 = boxes[i,2] # <<<<<<<<<<<<<< + * ty2 = boxes[i,3] + * ts = boxes[i,4] + */ + __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 113, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_2); + __Pyx_GIVEREF(__pyx_int_2); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_int_2); + __pyx_t_5 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_boxes), __pyx_t_2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 113, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_5); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 113, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_tx2 = __pyx_t_6; + + /* "nms/cpu_nms.pyx":114 + * ty1 = boxes[i,1] + * tx2 = boxes[i,2] + * ty2 = boxes[i,3] # <<<<<<<<<<<<<< + * ts = boxes[i,4] + * + */ + __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 114, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_3); + __Pyx_GIVEREF(__pyx_int_3); + PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_int_3); + __pyx_t_2 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_boxes), __pyx_t_5); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 114, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_2); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 114, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_v_ty2 = __pyx_t_6; + + /* "nms/cpu_nms.pyx":115 + * tx2 = boxes[i,2] + * ty2 = boxes[i,3] + * ts = boxes[i,4] # <<<<<<<<<<<<<< + * + * pos = i + 1 + */ + __pyx_t_2 = PyTuple_New(2); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 115, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __Pyx_INCREF(__pyx_v_i); + __Pyx_GIVEREF(__pyx_v_i); + PyTuple_SET_ITEM(__pyx_t_2, 0, __pyx_v_i); + __Pyx_INCREF(__pyx_int_4); + __Pyx_GIVEREF(__pyx_int_4); + PyTuple_SET_ITEM(__pyx_t_2, 1, __pyx_int_4); + __pyx_t_5 = __Pyx_PyObject_GetItem(((PyObject *)__pyx_v_boxes), __pyx_t_2); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 115, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_5); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 115, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_ts = __pyx_t_6; + + /* "nms/cpu_nms.pyx":117 + * ts = boxes[i,4] + * + * pos = i + 1 # <<<<<<<<<<<<<< + * # NMS iterations, note that N changes if detection boxes fall below threshold + * while pos < N: + */ + __pyx_t_5 = __Pyx_PyInt_AddObjC(__pyx_v_i, __pyx_int_1, 1, 0, 0); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 117, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_7 = __Pyx_PyInt_As_int(__pyx_t_5); if (unlikely((__pyx_t_7 == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 117, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_pos = __pyx_t_7; + + /* "nms/cpu_nms.pyx":119 + * pos = i + 1 + * # NMS iterations, note that N changes if detection boxes fall below threshold + * while pos < N: # <<<<<<<<<<<<<< + * x1 = boxes[pos, 0] + * y1 = boxes[pos, 1] + */ + while (1) { + __pyx_t_8 = ((__pyx_v_pos < __pyx_v_N) != 0); + if (!__pyx_t_8) break; + + /* "nms/cpu_nms.pyx":120 + * # NMS iterations, note that N changes if detection boxes fall below threshold + * while pos < N: + * x1 = boxes[pos, 0] # <<<<<<<<<<<<<< + * y1 = boxes[pos, 1] + * x2 = boxes[pos, 2] + */ + __pyx_t_9 = __pyx_v_pos; + __pyx_t_10 = 0; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 120, __pyx_L1_error) + } + __pyx_v_x1 = (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides)); + + /* "nms/cpu_nms.pyx":121 + * while pos < N: + * x1 = boxes[pos, 0] + * y1 = boxes[pos, 1] # <<<<<<<<<<<<<< + * x2 = boxes[pos, 2] + * y2 = boxes[pos, 3] + */ + __pyx_t_10 = __pyx_v_pos; + __pyx_t_9 = 1; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 121, __pyx_L1_error) + } + __pyx_v_y1 = (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides)); + + /* "nms/cpu_nms.pyx":122 + * x1 = boxes[pos, 0] + * y1 = boxes[pos, 1] + * x2 = boxes[pos, 2] # <<<<<<<<<<<<<< + * y2 = boxes[pos, 3] + * s = boxes[pos, 4] + */ + __pyx_t_9 = __pyx_v_pos; + __pyx_t_10 = 2; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 122, __pyx_L1_error) + } + __pyx_v_x2 = (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides)); + + /* "nms/cpu_nms.pyx":123 + * y1 = boxes[pos, 1] + * x2 = boxes[pos, 2] + * y2 = boxes[pos, 3] # <<<<<<<<<<<<<< + * s = boxes[pos, 4] + * + */ + __pyx_t_10 = __pyx_v_pos; + __pyx_t_9 = 3; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 123, __pyx_L1_error) + } + __pyx_v_y2 = (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides)); + + /* "nms/cpu_nms.pyx":124 + * x2 = boxes[pos, 2] + * y2 = boxes[pos, 3] + * s = boxes[pos, 4] # <<<<<<<<<<<<<< + * + * area = (x2 - x1 + 1) * (y2 - y1 + 1) + */ + __pyx_t_9 = __pyx_v_pos; + __pyx_t_10 = 4; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 124, __pyx_L1_error) + } + __pyx_t_5 = PyFloat_FromDouble((*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides))); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 124, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_XDECREF_SET(__pyx_v_s, __pyx_t_5); + __pyx_t_5 = 0; + + /* "nms/cpu_nms.pyx":126 + * s = boxes[pos, 4] + * + * area = (x2 - x1 + 1) * (y2 - y1 + 1) # <<<<<<<<<<<<<< + * iw = (min(tx2, x2) - max(tx1, x1) + 1) + * if iw > 0: + */ + __pyx_v_area = (((__pyx_v_x2 - __pyx_v_x1) + 1.0) * ((__pyx_v_y2 - __pyx_v_y1) + 1.0)); + + /* "nms/cpu_nms.pyx":127 + * + * area = (x2 - x1 + 1) * (y2 - y1 + 1) + * iw = (min(tx2, x2) - max(tx1, x1) + 1) # <<<<<<<<<<<<<< + * if iw > 0: + * ih = (min(ty2, y2) - max(ty1, y1) + 1) + */ + __pyx_v_iw = ((__pyx_f_3nms_7cpu_nms_min(__pyx_v_tx2, __pyx_v_x2) - __pyx_f_3nms_7cpu_nms_max(__pyx_v_tx1, __pyx_v_x1)) + 1.0); + + /* "nms/cpu_nms.pyx":128 + * area = (x2 - x1 + 1) * (y2 - y1 + 1) + * iw = (min(tx2, x2) - max(tx1, x1) + 1) + * if iw > 0: # <<<<<<<<<<<<<< + * ih = (min(ty2, y2) - max(ty1, y1) + 1) + * if ih > 0: + */ + __pyx_t_8 = ((__pyx_v_iw > 0.0) != 0); + if (__pyx_t_8) { + + /* "nms/cpu_nms.pyx":129 + * iw = (min(tx2, x2) - max(tx1, x1) + 1) + * if iw > 0: + * ih = (min(ty2, y2) - max(ty1, y1) + 1) # <<<<<<<<<<<<<< + * if ih > 0: + * ua = float((tx2 - tx1 + 1) * (ty2 - ty1 + 1) + area - iw * ih) + */ + __pyx_v_ih = ((__pyx_f_3nms_7cpu_nms_min(__pyx_v_ty2, __pyx_v_y2) - __pyx_f_3nms_7cpu_nms_max(__pyx_v_ty1, __pyx_v_y1)) + 1.0); + + /* "nms/cpu_nms.pyx":130 + * if iw > 0: + * ih = (min(ty2, y2) - max(ty1, y1) + 1) + * if ih > 0: # <<<<<<<<<<<<<< + * ua = float((tx2 - tx1 + 1) * (ty2 - ty1 + 1) + area - iw * ih) + * ov = iw * ih / ua #iou between max box and detection box + */ + __pyx_t_8 = ((__pyx_v_ih > 0.0) != 0); + if (__pyx_t_8) { + + /* "nms/cpu_nms.pyx":131 + * ih = (min(ty2, y2) - max(ty1, y1) + 1) + * if ih > 0: + * ua = float((tx2 - tx1 + 1) * (ty2 - ty1 + 1) + area - iw * ih) # <<<<<<<<<<<<<< + * ov = iw * ih / ua #iou between max box and detection box + * + */ + __pyx_v_ua = ((double)(((((__pyx_v_tx2 - __pyx_v_tx1) + 1.0) * ((__pyx_v_ty2 - __pyx_v_ty1) + 1.0)) + __pyx_v_area) - (__pyx_v_iw * __pyx_v_ih))); + + /* "nms/cpu_nms.pyx":132 + * if ih > 0: + * ua = float((tx2 - tx1 + 1) * (ty2 - ty1 + 1) + area - iw * ih) + * ov = iw * ih / ua #iou between max box and detection box # <<<<<<<<<<<<<< + * + * if method == 1: # linear + */ + __pyx_t_6 = (__pyx_v_iw * __pyx_v_ih); + if (unlikely(__pyx_v_ua == 0)) { + PyErr_SetString(PyExc_ZeroDivisionError, "float division"); + __PYX_ERR(0, 132, __pyx_L1_error) + } + __pyx_v_ov = (__pyx_t_6 / __pyx_v_ua); + + /* "nms/cpu_nms.pyx":134 + * ov = iw * ih / ua #iou between max box and detection box + * + * if method == 1: # linear # <<<<<<<<<<<<<< + * if ov > Nt: + * weight = 1 - ov + */ + switch (__pyx_v_method) { + case 1: + + /* "nms/cpu_nms.pyx":135 + * + * if method == 1: # linear + * if ov > Nt: # <<<<<<<<<<<<<< + * weight = 1 - ov + * else: + */ + __pyx_t_8 = ((__pyx_v_ov > __pyx_v_Nt) != 0); + if (__pyx_t_8) { + + /* "nms/cpu_nms.pyx":136 + * if method == 1: # linear + * if ov > Nt: + * weight = 1 - ov # <<<<<<<<<<<<<< + * else: + * weight = 1 + */ + __pyx_v_weight = (1.0 - __pyx_v_ov); + + /* "nms/cpu_nms.pyx":135 + * + * if method == 1: # linear + * if ov > Nt: # <<<<<<<<<<<<<< + * weight = 1 - ov + * else: + */ + goto __pyx_L12; + } + + /* "nms/cpu_nms.pyx":138 + * weight = 1 - ov + * else: + * weight = 1 # <<<<<<<<<<<<<< + * elif method == 2: # gaussian + * weight = np.exp(-(ov * ov)/sigma) + */ + /*else*/ { + __pyx_v_weight = 1.0; + } + __pyx_L12:; + + /* "nms/cpu_nms.pyx":134 + * ov = iw * ih / ua #iou between max box and detection box + * + * if method == 1: # linear # <<<<<<<<<<<<<< + * if ov > Nt: + * weight = 1 - ov + */ + break; + case 2: + + /* "nms/cpu_nms.pyx":140 + * weight = 1 + * elif method == 2: # gaussian + * weight = np.exp(-(ov * ov)/sigma) # <<<<<<<<<<<<<< + * else: # original NMS + * if ov > Nt: + */ + __Pyx_GetModuleGlobalName(__pyx_t_2, __pyx_n_s_np); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 140, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_11 = __Pyx_PyObject_GetAttrStr(__pyx_t_2, __pyx_n_s_exp); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 140, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_11); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_6 = (-(__pyx_v_ov * __pyx_v_ov)); + if (unlikely(__pyx_v_sigma == 0)) { + PyErr_SetString(PyExc_ZeroDivisionError, "float division"); + __PYX_ERR(0, 140, __pyx_L1_error) + } + __pyx_t_2 = PyFloat_FromDouble((__pyx_t_6 / __pyx_v_sigma)); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 140, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_12 = NULL; + if (CYTHON_UNPACK_METHODS && unlikely(PyMethod_Check(__pyx_t_11))) { + __pyx_t_12 = PyMethod_GET_SELF(__pyx_t_11); + if (likely(__pyx_t_12)) { + PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_11); + __Pyx_INCREF(__pyx_t_12); + __Pyx_INCREF(function); + __Pyx_DECREF_SET(__pyx_t_11, function); + } + } + __pyx_t_5 = (__pyx_t_12) ? __Pyx_PyObject_Call2Args(__pyx_t_11, __pyx_t_12, __pyx_t_2) : __Pyx_PyObject_CallOneArg(__pyx_t_11, __pyx_t_2); + __Pyx_XDECREF(__pyx_t_12); __pyx_t_12 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 140, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; + __pyx_t_6 = __pyx_PyFloat_AsFloat(__pyx_t_5); if (unlikely((__pyx_t_6 == (float)-1) && PyErr_Occurred())) __PYX_ERR(0, 140, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_weight = __pyx_t_6; + + /* "nms/cpu_nms.pyx":139 + * else: + * weight = 1 + * elif method == 2: # gaussian # <<<<<<<<<<<<<< + * weight = np.exp(-(ov * ov)/sigma) + * else: # original NMS + */ + break; + default: + + /* "nms/cpu_nms.pyx":142 + * weight = np.exp(-(ov * ov)/sigma) + * else: # original NMS + * if ov > Nt: # <<<<<<<<<<<<<< + * weight = 0 + * else: + */ + __pyx_t_8 = ((__pyx_v_ov > __pyx_v_Nt) != 0); + if (__pyx_t_8) { + + /* "nms/cpu_nms.pyx":143 + * else: # original NMS + * if ov > Nt: + * weight = 0 # <<<<<<<<<<<<<< + * else: + * weight = 1 + */ + __pyx_v_weight = 0.0; + + /* "nms/cpu_nms.pyx":142 + * weight = np.exp(-(ov * ov)/sigma) + * else: # original NMS + * if ov > Nt: # <<<<<<<<<<<<<< + * weight = 0 + * else: + */ + goto __pyx_L13; + } + + /* "nms/cpu_nms.pyx":145 + * weight = 0 + * else: + * weight = 1 # <<<<<<<<<<<<<< + * + * boxes[pos, 4] = weight*boxes[pos, 4] + */ + /*else*/ { + __pyx_v_weight = 1.0; + } + __pyx_L13:; + break; + } + + /* "nms/cpu_nms.pyx":147 + * weight = 1 + * + * boxes[pos, 4] = weight*boxes[pos, 4] # <<<<<<<<<<<<<< + * + * # if box score falls below threshold, discard the box by swapping with last box + */ + __pyx_t_10 = __pyx_v_pos; + __pyx_t_9 = 4; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 147, __pyx_L1_error) + } + __pyx_t_13 = __pyx_v_pos; + __pyx_t_14 = 4; + __pyx_t_7 = -1; + if (__pyx_t_13 < 0) { + __pyx_t_13 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_13 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_13 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_14 < 0) { + __pyx_t_14 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_14 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_14 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 147, __pyx_L1_error) + } + *__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_13, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_14, __pyx_pybuffernd_boxes.diminfo[1].strides) = (__pyx_v_weight * (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides))); + + /* "nms/cpu_nms.pyx":151 + * # if box score falls below threshold, discard the box by swapping with last box + * # update N + * if boxes[pos, 4] < threshold: # <<<<<<<<<<<<<< + * boxes[pos,0] = boxes[N-1, 0] + * boxes[pos,1] = boxes[N-1, 1] + */ + __pyx_t_9 = __pyx_v_pos; + __pyx_t_10 = 4; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 151, __pyx_L1_error) + } + __pyx_t_8 = (((*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides)) < __pyx_v_threshold) != 0); + if (__pyx_t_8) { + + /* "nms/cpu_nms.pyx":152 + * # update N + * if boxes[pos, 4] < threshold: + * boxes[pos,0] = boxes[N-1, 0] # <<<<<<<<<<<<<< + * boxes[pos,1] = boxes[N-1, 1] + * boxes[pos,2] = boxes[N-1, 2] + */ + __pyx_t_10 = (__pyx_v_N - 1); + __pyx_t_9 = 0; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 152, __pyx_L1_error) + } + __pyx_t_14 = __pyx_v_pos; + __pyx_t_13 = 0; + __pyx_t_7 = -1; + if (__pyx_t_14 < 0) { + __pyx_t_14 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_14 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_14 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_13 < 0) { + __pyx_t_13 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_13 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_13 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 152, __pyx_L1_error) + } + *__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_14, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_13, __pyx_pybuffernd_boxes.diminfo[1].strides) = (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides)); + + /* "nms/cpu_nms.pyx":153 + * if boxes[pos, 4] < threshold: + * boxes[pos,0] = boxes[N-1, 0] + * boxes[pos,1] = boxes[N-1, 1] # <<<<<<<<<<<<<< + * boxes[pos,2] = boxes[N-1, 2] + * boxes[pos,3] = boxes[N-1, 3] + */ + __pyx_t_9 = (__pyx_v_N - 1); + __pyx_t_10 = 1; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 153, __pyx_L1_error) + } + __pyx_t_13 = __pyx_v_pos; + __pyx_t_14 = 1; + __pyx_t_7 = -1; + if (__pyx_t_13 < 0) { + __pyx_t_13 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_13 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_13 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_14 < 0) { + __pyx_t_14 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_14 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_14 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 153, __pyx_L1_error) + } + *__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_13, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_14, __pyx_pybuffernd_boxes.diminfo[1].strides) = (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides)); + + /* "nms/cpu_nms.pyx":154 + * boxes[pos,0] = boxes[N-1, 0] + * boxes[pos,1] = boxes[N-1, 1] + * boxes[pos,2] = boxes[N-1, 2] # <<<<<<<<<<<<<< + * boxes[pos,3] = boxes[N-1, 3] + * boxes[pos,4] = boxes[N-1, 4] + */ + __pyx_t_10 = (__pyx_v_N - 1); + __pyx_t_9 = 2; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 154, __pyx_L1_error) + } + __pyx_t_14 = __pyx_v_pos; + __pyx_t_13 = 2; + __pyx_t_7 = -1; + if (__pyx_t_14 < 0) { + __pyx_t_14 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_14 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_14 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_13 < 0) { + __pyx_t_13 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_13 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_13 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 154, __pyx_L1_error) + } + *__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_14, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_13, __pyx_pybuffernd_boxes.diminfo[1].strides) = (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides)); + + /* "nms/cpu_nms.pyx":155 + * boxes[pos,1] = boxes[N-1, 1] + * boxes[pos,2] = boxes[N-1, 2] + * boxes[pos,3] = boxes[N-1, 3] # <<<<<<<<<<<<<< + * boxes[pos,4] = boxes[N-1, 4] + * N = N - 1 + */ + __pyx_t_9 = (__pyx_v_N - 1); + __pyx_t_10 = 3; + __pyx_t_7 = -1; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 155, __pyx_L1_error) + } + __pyx_t_13 = __pyx_v_pos; + __pyx_t_14 = 3; + __pyx_t_7 = -1; + if (__pyx_t_13 < 0) { + __pyx_t_13 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_13 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_13 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_14 < 0) { + __pyx_t_14 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_14 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_14 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 155, __pyx_L1_error) + } + *__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_13, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_14, __pyx_pybuffernd_boxes.diminfo[1].strides) = (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[1].strides)); + + /* "nms/cpu_nms.pyx":156 + * boxes[pos,2] = boxes[N-1, 2] + * boxes[pos,3] = boxes[N-1, 3] + * boxes[pos,4] = boxes[N-1, 4] # <<<<<<<<<<<<<< + * N = N - 1 + * pos = pos - 1 + */ + __pyx_t_10 = (__pyx_v_N - 1); + __pyx_t_9 = 4; + __pyx_t_7 = -1; + if (__pyx_t_10 < 0) { + __pyx_t_10 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_10 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_9 < 0) { + __pyx_t_9 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_9 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_9 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 156, __pyx_L1_error) + } + __pyx_t_14 = __pyx_v_pos; + __pyx_t_13 = 4; + __pyx_t_7 = -1; + if (__pyx_t_14 < 0) { + __pyx_t_14 += __pyx_pybuffernd_boxes.diminfo[0].shape; + if (unlikely(__pyx_t_14 < 0)) __pyx_t_7 = 0; + } else if (unlikely(__pyx_t_14 >= __pyx_pybuffernd_boxes.diminfo[0].shape)) __pyx_t_7 = 0; + if (__pyx_t_13 < 0) { + __pyx_t_13 += __pyx_pybuffernd_boxes.diminfo[1].shape; + if (unlikely(__pyx_t_13 < 0)) __pyx_t_7 = 1; + } else if (unlikely(__pyx_t_13 >= __pyx_pybuffernd_boxes.diminfo[1].shape)) __pyx_t_7 = 1; + if (unlikely(__pyx_t_7 != -1)) { + __Pyx_RaiseBufferIndexError(__pyx_t_7); + __PYX_ERR(0, 156, __pyx_L1_error) + } + *__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_14, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_13, __pyx_pybuffernd_boxes.diminfo[1].strides) = (*__Pyx_BufPtrStrided2d(float *, __pyx_pybuffernd_boxes.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_boxes.diminfo[0].strides, __pyx_t_9, __pyx_pybuffernd_boxes.diminfo[1].strides)); + + /* "nms/cpu_nms.pyx":157 + * boxes[pos,3] = boxes[N-1, 3] + * boxes[pos,4] = boxes[N-1, 4] + * N = N - 1 # <<<<<<<<<<<<<< + * pos = pos - 1 + * + */ + __pyx_v_N = (__pyx_v_N - 1); + + /* "nms/cpu_nms.pyx":158 + * boxes[pos,4] = boxes[N-1, 4] + * N = N - 1 + * pos = pos - 1 # <<<<<<<<<<<<<< + * + * pos = pos + 1 + */ + __pyx_v_pos = (__pyx_v_pos - 1); + + /* "nms/cpu_nms.pyx":151 + * # if box score falls below threshold, discard the box by swapping with last box + * # update N + * if boxes[pos, 4] < threshold: # <<<<<<<<<<<<<< + * boxes[pos,0] = boxes[N-1, 0] + * boxes[pos,1] = boxes[N-1, 1] + */ + } + + /* "nms/cpu_nms.pyx":130 + * if iw > 0: + * ih = (min(ty2, y2) - max(ty1, y1) + 1) + * if ih > 0: # <<<<<<<<<<<<<< + * ua = float((tx2 - tx1 + 1) * (ty2 - ty1 + 1) + area - iw * ih) + * ov = iw * ih / ua #iou between max box and detection box + */ + } + + /* "nms/cpu_nms.pyx":128 + * area = (x2 - x1 + 1) * (y2 - y1 + 1) + * iw = (min(tx2, x2) - max(tx1, x1) + 1) + * if iw > 0: # <<<<<<<<<<<<<< + * ih = (min(ty2, y2) - max(ty1, y1) + 1) + * if ih > 0: + */ + } + + /* "nms/cpu_nms.pyx":160 + * pos = pos - 1 + * + * pos = pos + 1 # <<<<<<<<<<<<<< + * + * keep = [i for i in range(N)] + */ + __pyx_v_pos = (__pyx_v_pos + 1); + } + + /* "nms/cpu_nms.pyx":79 + * cdef float x1,x2,y1,y2,tx1,tx2,ty1,ty2,ts,area,weight,ov + * + * for i in range(N): # <<<<<<<<<<<<<< + * maxscore = boxes[i, 4] + * maxpos = i + */ + } + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "nms/cpu_nms.pyx":162 + * pos = pos + 1 + * + * keep = [i for i in range(N)] # <<<<<<<<<<<<<< + * return keep + */ + __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 162, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_5 = __Pyx_PyInt_From_unsigned_int(__pyx_v_N); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 162, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_11 = __Pyx_PyObject_CallOneArg(__pyx_builtin_range, __pyx_t_5); if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 162, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_11); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + if (likely(PyList_CheckExact(__pyx_t_11)) || PyTuple_CheckExact(__pyx_t_11)) { + __pyx_t_5 = __pyx_t_11; __Pyx_INCREF(__pyx_t_5); __pyx_t_3 = 0; + __pyx_t_4 = NULL; + } else { + __pyx_t_3 = -1; __pyx_t_5 = PyObject_GetIter(__pyx_t_11); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 162, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_4 = Py_TYPE(__pyx_t_5)->tp_iternext; if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 162, __pyx_L1_error) + } + __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; + for (;;) { + if (likely(!__pyx_t_4)) { + if (likely(PyList_CheckExact(__pyx_t_5))) { + if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_5)) break; + #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS + __pyx_t_11 = PyList_GET_ITEM(__pyx_t_5, __pyx_t_3); __Pyx_INCREF(__pyx_t_11); __pyx_t_3++; if (unlikely(0 < 0)) __PYX_ERR(0, 162, __pyx_L1_error) + #else + __pyx_t_11 = PySequence_ITEM(__pyx_t_5, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 162, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_11); + #endif + } else { + if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_5)) break; + #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS + __pyx_t_11 = PyTuple_GET_ITEM(__pyx_t_5, __pyx_t_3); __Pyx_INCREF(__pyx_t_11); __pyx_t_3++; if (unlikely(0 < 0)) __PYX_ERR(0, 162, __pyx_L1_error) + #else + __pyx_t_11 = PySequence_ITEM(__pyx_t_5, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_11)) __PYX_ERR(0, 162, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_11); + #endif + } + } else { + __pyx_t_11 = __pyx_t_4(__pyx_t_5); + if (unlikely(!__pyx_t_11)) { + PyObject* exc_type = PyErr_Occurred(); + if (exc_type) { + if (likely(__Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear(); + else __PYX_ERR(0, 162, __pyx_L1_error) + } + break; + } + __Pyx_GOTREF(__pyx_t_11); + } + __Pyx_XDECREF_SET(__pyx_v_i, __pyx_t_11); + __pyx_t_11 = 0; + if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_v_i))) __PYX_ERR(0, 162, __pyx_L1_error) + } + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_v_keep = ((PyObject*)__pyx_t_1); + __pyx_t_1 = 0; + + /* "nms/cpu_nms.pyx":163 + * + * keep = [i for i in range(N)] + * return keep # <<<<<<<<<<<<<< + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(__pyx_v_keep); + __pyx_r = __pyx_v_keep; + goto __pyx_L0; + + /* "nms/cpu_nms.pyx":70 + * return keep + * + * def cpu_soft_nms(np.ndarray[float, ndim=2] boxes, float sigma=0.5, float Nt=0.3, float threshold=0.001, unsigned int method=0): # <<<<<<<<<<<<<< + * cdef unsigned int N = boxes.shape[0] + * cdef float iw, ih, box_area + */ + + /* function exit code */ + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_XDECREF(__pyx_t_5); + __Pyx_XDECREF(__pyx_t_11); + __Pyx_XDECREF(__pyx_t_12); + { PyObject *__pyx_type, *__pyx_value, *__pyx_tb; + __Pyx_PyThreadState_declare + __Pyx_PyThreadState_assign + __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb); + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_boxes.rcbuffer->pybuffer); + __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);} + __Pyx_AddTraceback("nms.cpu_nms.cpu_soft_nms", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + goto __pyx_L2; + __pyx_L0:; + __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_boxes.rcbuffer->pybuffer); + __pyx_L2:; + __Pyx_XDECREF(__pyx_v_i); + __Pyx_XDECREF(__pyx_v_s); + __Pyx_XDECREF(__pyx_v_keep); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":735 + * ctypedef npy_cdouble complex_t + * + * cdef inline object PyArray_MultiIterNew1(a): # <<<<<<<<<<<<<< + * return PyArray_MultiIterNew(1, a) + * + */ + +static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew1(PyObject *__pyx_v_a) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("PyArray_MultiIterNew1", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":736 + * + * cdef inline object PyArray_MultiIterNew1(a): + * return PyArray_MultiIterNew(1, a) # <<<<<<<<<<<<<< + * + * cdef inline object PyArray_MultiIterNew2(a, b): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyArray_MultiIterNew(1, ((void *)__pyx_v_a)); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 736, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":735 + * ctypedef npy_cdouble complex_t + * + * cdef inline object PyArray_MultiIterNew1(a): # <<<<<<<<<<<<<< + * return PyArray_MultiIterNew(1, a) + * + */ + + /* function exit code */ + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("numpy.PyArray_MultiIterNew1", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":738 + * return PyArray_MultiIterNew(1, a) + * + * cdef inline object PyArray_MultiIterNew2(a, b): # <<<<<<<<<<<<<< + * return PyArray_MultiIterNew(2, a, b) + * + */ + +static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew2(PyObject *__pyx_v_a, PyObject *__pyx_v_b) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("PyArray_MultiIterNew2", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":739 + * + * cdef inline object PyArray_MultiIterNew2(a, b): + * return PyArray_MultiIterNew(2, a, b) # <<<<<<<<<<<<<< + * + * cdef inline object PyArray_MultiIterNew3(a, b, c): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyArray_MultiIterNew(2, ((void *)__pyx_v_a), ((void *)__pyx_v_b)); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 739, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":738 + * return PyArray_MultiIterNew(1, a) + * + * cdef inline object PyArray_MultiIterNew2(a, b): # <<<<<<<<<<<<<< + * return PyArray_MultiIterNew(2, a, b) + * + */ + + /* function exit code */ + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("numpy.PyArray_MultiIterNew2", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":741 + * return PyArray_MultiIterNew(2, a, b) + * + * cdef inline object PyArray_MultiIterNew3(a, b, c): # <<<<<<<<<<<<<< + * return PyArray_MultiIterNew(3, a, b, c) + * + */ + +static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew3(PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("PyArray_MultiIterNew3", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":742 + * + * cdef inline object PyArray_MultiIterNew3(a, b, c): + * return PyArray_MultiIterNew(3, a, b, c) # <<<<<<<<<<<<<< + * + * cdef inline object PyArray_MultiIterNew4(a, b, c, d): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyArray_MultiIterNew(3, ((void *)__pyx_v_a), ((void *)__pyx_v_b), ((void *)__pyx_v_c)); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 742, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":741 + * return PyArray_MultiIterNew(2, a, b) + * + * cdef inline object PyArray_MultiIterNew3(a, b, c): # <<<<<<<<<<<<<< + * return PyArray_MultiIterNew(3, a, b, c) + * + */ + + /* function exit code */ + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("numpy.PyArray_MultiIterNew3", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":744 + * return PyArray_MultiIterNew(3, a, b, c) + * + * cdef inline object PyArray_MultiIterNew4(a, b, c, d): # <<<<<<<<<<<<<< + * return PyArray_MultiIterNew(4, a, b, c, d) + * + */ + +static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew4(PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c, PyObject *__pyx_v_d) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("PyArray_MultiIterNew4", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":745 + * + * cdef inline object PyArray_MultiIterNew4(a, b, c, d): + * return PyArray_MultiIterNew(4, a, b, c, d) # <<<<<<<<<<<<<< + * + * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyArray_MultiIterNew(4, ((void *)__pyx_v_a), ((void *)__pyx_v_b), ((void *)__pyx_v_c), ((void *)__pyx_v_d)); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 745, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":744 + * return PyArray_MultiIterNew(3, a, b, c) + * + * cdef inline object PyArray_MultiIterNew4(a, b, c, d): # <<<<<<<<<<<<<< + * return PyArray_MultiIterNew(4, a, b, c, d) + * + */ + + /* function exit code */ + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("numpy.PyArray_MultiIterNew4", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":747 + * return PyArray_MultiIterNew(4, a, b, c, d) + * + * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e): # <<<<<<<<<<<<<< + * return PyArray_MultiIterNew(5, a, b, c, d, e) + * + */ + +static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew5(PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c, PyObject *__pyx_v_d, PyObject *__pyx_v_e) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("PyArray_MultiIterNew5", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":748 + * + * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e): + * return PyArray_MultiIterNew(5, a, b, c, d, e) # <<<<<<<<<<<<<< + * + * cdef inline tuple PyDataType_SHAPE(dtype d): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyArray_MultiIterNew(5, ((void *)__pyx_v_a), ((void *)__pyx_v_b), ((void *)__pyx_v_c), ((void *)__pyx_v_d), ((void *)__pyx_v_e)); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 748, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":747 + * return PyArray_MultiIterNew(4, a, b, c, d) + * + * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e): # <<<<<<<<<<<<<< + * return PyArray_MultiIterNew(5, a, b, c, d, e) + * + */ + + /* function exit code */ + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("numpy.PyArray_MultiIterNew5", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = 0; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":750 + * return PyArray_MultiIterNew(5, a, b, c, d, e) + * + * cdef inline tuple PyDataType_SHAPE(dtype d): # <<<<<<<<<<<<<< + * if PyDataType_HASSUBARRAY(d): + * return d.subarray.shape + */ + +static CYTHON_INLINE PyObject *__pyx_f_5numpy_PyDataType_SHAPE(PyArray_Descr *__pyx_v_d) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + int __pyx_t_1; + __Pyx_RefNannySetupContext("PyDataType_SHAPE", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":751 + * + * cdef inline tuple PyDataType_SHAPE(dtype d): + * if PyDataType_HASSUBARRAY(d): # <<<<<<<<<<<<<< + * return d.subarray.shape + * else: + */ + __pyx_t_1 = (PyDataType_HASSUBARRAY(__pyx_v_d) != 0); + if (__pyx_t_1) { + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":752 + * cdef inline tuple PyDataType_SHAPE(dtype d): + * if PyDataType_HASSUBARRAY(d): + * return d.subarray.shape # <<<<<<<<<<<<<< + * else: + * return () + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject*)__pyx_v_d->subarray->shape)); + __pyx_r = ((PyObject*)__pyx_v_d->subarray->shape); + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":751 + * + * cdef inline tuple PyDataType_SHAPE(dtype d): + * if PyDataType_HASSUBARRAY(d): # <<<<<<<<<<<<<< + * return d.subarray.shape + * else: + */ + } + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":754 + * return d.subarray.shape + * else: + * return () # <<<<<<<<<<<<<< + * + * + */ + /*else*/ { + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(__pyx_empty_tuple); + __pyx_r = __pyx_empty_tuple; + goto __pyx_L0; + } + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":750 + * return PyArray_MultiIterNew(5, a, b, c, d, e) + * + * cdef inline tuple PyDataType_SHAPE(dtype d): # <<<<<<<<<<<<<< + * if PyDataType_HASSUBARRAY(d): + * return d.subarray.shape + */ + + /* function exit code */ + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":931 + * int _import_umath() except -1 + * + * cdef inline void set_array_base(ndarray arr, object base): # <<<<<<<<<<<<<< + * Py_INCREF(base) # important to do this before stealing the reference below! + * PyArray_SetBaseObject(arr, base) + */ + +static CYTHON_INLINE void __pyx_f_5numpy_set_array_base(PyArrayObject *__pyx_v_arr, PyObject *__pyx_v_base) { + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("set_array_base", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":932 + * + * cdef inline void set_array_base(ndarray arr, object base): + * Py_INCREF(base) # important to do this before stealing the reference below! # <<<<<<<<<<<<<< + * PyArray_SetBaseObject(arr, base) + * + */ + Py_INCREF(__pyx_v_base); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":933 + * cdef inline void set_array_base(ndarray arr, object base): + * Py_INCREF(base) # important to do this before stealing the reference below! + * PyArray_SetBaseObject(arr, base) # <<<<<<<<<<<<<< + * + * cdef inline object get_array_base(ndarray arr): + */ + (void)(PyArray_SetBaseObject(__pyx_v_arr, __pyx_v_base)); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":931 + * int _import_umath() except -1 + * + * cdef inline void set_array_base(ndarray arr, object base): # <<<<<<<<<<<<<< + * Py_INCREF(base) # important to do this before stealing the reference below! + * PyArray_SetBaseObject(arr, base) + */ + + /* function exit code */ + __Pyx_RefNannyFinishContext(); +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":935 + * PyArray_SetBaseObject(arr, base) + * + * cdef inline object get_array_base(ndarray arr): # <<<<<<<<<<<<<< + * base = PyArray_BASE(arr) + * if base is NULL: + */ + +static CYTHON_INLINE PyObject *__pyx_f_5numpy_get_array_base(PyArrayObject *__pyx_v_arr) { + PyObject *__pyx_v_base; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + int __pyx_t_1; + __Pyx_RefNannySetupContext("get_array_base", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":936 + * + * cdef inline object get_array_base(ndarray arr): + * base = PyArray_BASE(arr) # <<<<<<<<<<<<<< + * if base is NULL: + * return None + */ + __pyx_v_base = PyArray_BASE(__pyx_v_arr); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":937 + * cdef inline object get_array_base(ndarray arr): + * base = PyArray_BASE(arr) + * if base is NULL: # <<<<<<<<<<<<<< + * return None + * return base + */ + __pyx_t_1 = ((__pyx_v_base == NULL) != 0); + if (__pyx_t_1) { + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":938 + * base = PyArray_BASE(arr) + * if base is NULL: + * return None # <<<<<<<<<<<<<< + * return base + * + */ + __Pyx_XDECREF(__pyx_r); + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":937 + * cdef inline object get_array_base(ndarray arr): + * base = PyArray_BASE(arr) + * if base is NULL: # <<<<<<<<<<<<<< + * return None + * return base + */ + } + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":939 + * if base is NULL: + * return None + * return base # <<<<<<<<<<<<<< + * + * # Versions of the import_* functions which are more suitable for + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_base)); + __pyx_r = ((PyObject *)__pyx_v_base); + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":935 + * PyArray_SetBaseObject(arr, base) + * + * cdef inline object get_array_base(ndarray arr): # <<<<<<<<<<<<<< + * base = PyArray_BASE(arr) + * if base is NULL: + */ + + /* function exit code */ + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":943 + * # Versions of the import_* functions which are more suitable for + * # Cython code. + * cdef inline int import_array() except -1: # <<<<<<<<<<<<<< + * try: + * __pyx_import_array() + */ + +static CYTHON_INLINE int __pyx_f_5numpy_import_array(void) { + int __pyx_r; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + int __pyx_t_4; + PyObject *__pyx_t_5 = NULL; + PyObject *__pyx_t_6 = NULL; + PyObject *__pyx_t_7 = NULL; + PyObject *__pyx_t_8 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("import_array", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":944 + * # Cython code. + * cdef inline int import_array() except -1: + * try: # <<<<<<<<<<<<<< + * __pyx_import_array() + * except Exception: + */ + { + __Pyx_PyThreadState_declare + __Pyx_PyThreadState_assign + __Pyx_ExceptionSave(&__pyx_t_1, &__pyx_t_2, &__pyx_t_3); + __Pyx_XGOTREF(__pyx_t_1); + __Pyx_XGOTREF(__pyx_t_2); + __Pyx_XGOTREF(__pyx_t_3); + /*try:*/ { + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":945 + * cdef inline int import_array() except -1: + * try: + * __pyx_import_array() # <<<<<<<<<<<<<< + * except Exception: + * raise ImportError("numpy.core.multiarray failed to import") + */ + __pyx_t_4 = _import_array(); if (unlikely(__pyx_t_4 == ((int)-1))) __PYX_ERR(1, 945, __pyx_L3_error) + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":944 + * # Cython code. + * cdef inline int import_array() except -1: + * try: # <<<<<<<<<<<<<< + * __pyx_import_array() + * except Exception: + */ + } + __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0; + goto __pyx_L8_try_end; + __pyx_L3_error:; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":946 + * try: + * __pyx_import_array() + * except Exception: # <<<<<<<<<<<<<< + * raise ImportError("numpy.core.multiarray failed to import") + * + */ + __pyx_t_4 = __Pyx_PyErr_ExceptionMatches(((PyObject *)(&((PyTypeObject*)PyExc_Exception)[0]))); + if (__pyx_t_4) { + __Pyx_AddTraceback("numpy.import_array", __pyx_clineno, __pyx_lineno, __pyx_filename); + if (__Pyx_GetException(&__pyx_t_5, &__pyx_t_6, &__pyx_t_7) < 0) __PYX_ERR(1, 946, __pyx_L5_except_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_GOTREF(__pyx_t_6); + __Pyx_GOTREF(__pyx_t_7); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":947 + * __pyx_import_array() + * except Exception: + * raise ImportError("numpy.core.multiarray failed to import") # <<<<<<<<<<<<<< + * + * cdef inline int import_umath() except -1: + */ + __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ImportError, __pyx_tuple__8, NULL); if (unlikely(!__pyx_t_8)) __PYX_ERR(1, 947, __pyx_L5_except_error) + __Pyx_GOTREF(__pyx_t_8); + __Pyx_Raise(__pyx_t_8, 0, 0, 0); + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + __PYX_ERR(1, 947, __pyx_L5_except_error) + } + goto __pyx_L5_except_error; + __pyx_L5_except_error:; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":944 + * # Cython code. + * cdef inline int import_array() except -1: + * try: # <<<<<<<<<<<<<< + * __pyx_import_array() + * except Exception: + */ + __Pyx_XGIVEREF(__pyx_t_1); + __Pyx_XGIVEREF(__pyx_t_2); + __Pyx_XGIVEREF(__pyx_t_3); + __Pyx_ExceptionReset(__pyx_t_1, __pyx_t_2, __pyx_t_3); + goto __pyx_L1_error; + __pyx_L8_try_end:; + } + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":943 + * # Versions of the import_* functions which are more suitable for + * # Cython code. + * cdef inline int import_array() except -1: # <<<<<<<<<<<<<< + * try: + * __pyx_import_array() + */ + + /* function exit code */ + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_5); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_8); + __Pyx_AddTraceback("numpy.import_array", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = -1; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":949 + * raise ImportError("numpy.core.multiarray failed to import") + * + * cdef inline int import_umath() except -1: # <<<<<<<<<<<<<< + * try: + * _import_umath() + */ + +static CYTHON_INLINE int __pyx_f_5numpy_import_umath(void) { + int __pyx_r; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + int __pyx_t_4; + PyObject *__pyx_t_5 = NULL; + PyObject *__pyx_t_6 = NULL; + PyObject *__pyx_t_7 = NULL; + PyObject *__pyx_t_8 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("import_umath", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":950 + * + * cdef inline int import_umath() except -1: + * try: # <<<<<<<<<<<<<< + * _import_umath() + * except Exception: + */ + { + __Pyx_PyThreadState_declare + __Pyx_PyThreadState_assign + __Pyx_ExceptionSave(&__pyx_t_1, &__pyx_t_2, &__pyx_t_3); + __Pyx_XGOTREF(__pyx_t_1); + __Pyx_XGOTREF(__pyx_t_2); + __Pyx_XGOTREF(__pyx_t_3); + /*try:*/ { + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":951 + * cdef inline int import_umath() except -1: + * try: + * _import_umath() # <<<<<<<<<<<<<< + * except Exception: + * raise ImportError("numpy.core.umath failed to import") + */ + __pyx_t_4 = _import_umath(); if (unlikely(__pyx_t_4 == ((int)-1))) __PYX_ERR(1, 951, __pyx_L3_error) + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":950 + * + * cdef inline int import_umath() except -1: + * try: # <<<<<<<<<<<<<< + * _import_umath() + * except Exception: + */ + } + __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0; + goto __pyx_L8_try_end; + __pyx_L3_error:; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":952 + * try: + * _import_umath() + * except Exception: # <<<<<<<<<<<<<< + * raise ImportError("numpy.core.umath failed to import") + * + */ + __pyx_t_4 = __Pyx_PyErr_ExceptionMatches(((PyObject *)(&((PyTypeObject*)PyExc_Exception)[0]))); + if (__pyx_t_4) { + __Pyx_AddTraceback("numpy.import_umath", __pyx_clineno, __pyx_lineno, __pyx_filename); + if (__Pyx_GetException(&__pyx_t_5, &__pyx_t_6, &__pyx_t_7) < 0) __PYX_ERR(1, 952, __pyx_L5_except_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_GOTREF(__pyx_t_6); + __Pyx_GOTREF(__pyx_t_7); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":953 + * _import_umath() + * except Exception: + * raise ImportError("numpy.core.umath failed to import") # <<<<<<<<<<<<<< + * + * cdef inline int import_ufunc() except -1: + */ + __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ImportError, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_8)) __PYX_ERR(1, 953, __pyx_L5_except_error) + __Pyx_GOTREF(__pyx_t_8); + __Pyx_Raise(__pyx_t_8, 0, 0, 0); + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + __PYX_ERR(1, 953, __pyx_L5_except_error) + } + goto __pyx_L5_except_error; + __pyx_L5_except_error:; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":950 + * + * cdef inline int import_umath() except -1: + * try: # <<<<<<<<<<<<<< + * _import_umath() + * except Exception: + */ + __Pyx_XGIVEREF(__pyx_t_1); + __Pyx_XGIVEREF(__pyx_t_2); + __Pyx_XGIVEREF(__pyx_t_3); + __Pyx_ExceptionReset(__pyx_t_1, __pyx_t_2, __pyx_t_3); + goto __pyx_L1_error; + __pyx_L8_try_end:; + } + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":949 + * raise ImportError("numpy.core.multiarray failed to import") + * + * cdef inline int import_umath() except -1: # <<<<<<<<<<<<<< + * try: + * _import_umath() + */ + + /* function exit code */ + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_5); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_8); + __Pyx_AddTraceback("numpy.import_umath", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = -1; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":955 + * raise ImportError("numpy.core.umath failed to import") + * + * cdef inline int import_ufunc() except -1: # <<<<<<<<<<<<<< + * try: + * _import_umath() + */ + +static CYTHON_INLINE int __pyx_f_5numpy_import_ufunc(void) { + int __pyx_r; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + int __pyx_t_4; + PyObject *__pyx_t_5 = NULL; + PyObject *__pyx_t_6 = NULL; + PyObject *__pyx_t_7 = NULL; + PyObject *__pyx_t_8 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("import_ufunc", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":956 + * + * cdef inline int import_ufunc() except -1: + * try: # <<<<<<<<<<<<<< + * _import_umath() + * except Exception: + */ + { + __Pyx_PyThreadState_declare + __Pyx_PyThreadState_assign + __Pyx_ExceptionSave(&__pyx_t_1, &__pyx_t_2, &__pyx_t_3); + __Pyx_XGOTREF(__pyx_t_1); + __Pyx_XGOTREF(__pyx_t_2); + __Pyx_XGOTREF(__pyx_t_3); + /*try:*/ { + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":957 + * cdef inline int import_ufunc() except -1: + * try: + * _import_umath() # <<<<<<<<<<<<<< + * except Exception: + * raise ImportError("numpy.core.umath failed to import") + */ + __pyx_t_4 = _import_umath(); if (unlikely(__pyx_t_4 == ((int)-1))) __PYX_ERR(1, 957, __pyx_L3_error) + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":956 + * + * cdef inline int import_ufunc() except -1: + * try: # <<<<<<<<<<<<<< + * _import_umath() + * except Exception: + */ + } + __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0; + goto __pyx_L8_try_end; + __pyx_L3_error:; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":958 + * try: + * _import_umath() + * except Exception: # <<<<<<<<<<<<<< + * raise ImportError("numpy.core.umath failed to import") + * + */ + __pyx_t_4 = __Pyx_PyErr_ExceptionMatches(((PyObject *)(&((PyTypeObject*)PyExc_Exception)[0]))); + if (__pyx_t_4) { + __Pyx_AddTraceback("numpy.import_ufunc", __pyx_clineno, __pyx_lineno, __pyx_filename); + if (__Pyx_GetException(&__pyx_t_5, &__pyx_t_6, &__pyx_t_7) < 0) __PYX_ERR(1, 958, __pyx_L5_except_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_GOTREF(__pyx_t_6); + __Pyx_GOTREF(__pyx_t_7); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":959 + * _import_umath() + * except Exception: + * raise ImportError("numpy.core.umath failed to import") # <<<<<<<<<<<<<< + * + * cdef extern from *: + */ + __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ImportError, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_8)) __PYX_ERR(1, 959, __pyx_L5_except_error) + __Pyx_GOTREF(__pyx_t_8); + __Pyx_Raise(__pyx_t_8, 0, 0, 0); + __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0; + __PYX_ERR(1, 959, __pyx_L5_except_error) + } + goto __pyx_L5_except_error; + __pyx_L5_except_error:; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":956 + * + * cdef inline int import_ufunc() except -1: + * try: # <<<<<<<<<<<<<< + * _import_umath() + * except Exception: + */ + __Pyx_XGIVEREF(__pyx_t_1); + __Pyx_XGIVEREF(__pyx_t_2); + __Pyx_XGIVEREF(__pyx_t_3); + __Pyx_ExceptionReset(__pyx_t_1, __pyx_t_2, __pyx_t_3); + goto __pyx_L1_error; + __pyx_L8_try_end:; + } + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":955 + * raise ImportError("numpy.core.umath failed to import") + * + * cdef inline int import_ufunc() except -1: # <<<<<<<<<<<<<< + * try: + * _import_umath() + */ + + /* function exit code */ + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_5); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_8); + __Pyx_AddTraceback("numpy.import_ufunc", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = -1; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":969 + * + * + * cdef inline bint is_timedelta64_object(object obj): # <<<<<<<<<<<<<< + * """ + * Cython equivalent of `isinstance(obj, np.timedelta64)` + */ + +static CYTHON_INLINE int __pyx_f_5numpy_is_timedelta64_object(PyObject *__pyx_v_obj) { + int __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("is_timedelta64_object", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":981 + * bool + * """ + * return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type) # <<<<<<<<<<<<<< + * + * + */ + __pyx_r = PyObject_TypeCheck(__pyx_v_obj, (&PyTimedeltaArrType_Type)); + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":969 + * + * + * cdef inline bint is_timedelta64_object(object obj): # <<<<<<<<<<<<<< + * """ + * Cython equivalent of `isinstance(obj, np.timedelta64)` + */ + + /* function exit code */ + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":984 + * + * + * cdef inline bint is_datetime64_object(object obj): # <<<<<<<<<<<<<< + * """ + * Cython equivalent of `isinstance(obj, np.datetime64)` + */ + +static CYTHON_INLINE int __pyx_f_5numpy_is_datetime64_object(PyObject *__pyx_v_obj) { + int __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("is_datetime64_object", 0); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":996 + * bool + * """ + * return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type) # <<<<<<<<<<<<<< + * + * + */ + __pyx_r = PyObject_TypeCheck(__pyx_v_obj, (&PyDatetimeArrType_Type)); + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":984 + * + * + * cdef inline bint is_datetime64_object(object obj): # <<<<<<<<<<<<<< + * """ + * Cython equivalent of `isinstance(obj, np.datetime64)` + */ + + /* function exit code */ + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":999 + * + * + * cdef inline npy_datetime get_datetime64_value(object obj) nogil: # <<<<<<<<<<<<<< + * """ + * returns the int64 value underlying scalar numpy datetime64 object + */ + +static CYTHON_INLINE npy_datetime __pyx_f_5numpy_get_datetime64_value(PyObject *__pyx_v_obj) { + npy_datetime __pyx_r; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":1006 + * also needed. That can be found using `get_datetime64_unit`. + * """ + * return (obj).obval # <<<<<<<<<<<<<< + * + * + */ + __pyx_r = ((PyDatetimeScalarObject *)__pyx_v_obj)->obval; + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":999 + * + * + * cdef inline npy_datetime get_datetime64_value(object obj) nogil: # <<<<<<<<<<<<<< + * """ + * returns the int64 value underlying scalar numpy datetime64 object + */ + + /* function exit code */ + __pyx_L0:; + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":1009 + * + * + * cdef inline npy_timedelta get_timedelta64_value(object obj) nogil: # <<<<<<<<<<<<<< + * """ + * returns the int64 value underlying scalar numpy timedelta64 object + */ + +static CYTHON_INLINE npy_timedelta __pyx_f_5numpy_get_timedelta64_value(PyObject *__pyx_v_obj) { + npy_timedelta __pyx_r; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":1013 + * returns the int64 value underlying scalar numpy timedelta64 object + * """ + * return (obj).obval # <<<<<<<<<<<<<< + * + * + */ + __pyx_r = ((PyTimedeltaScalarObject *)__pyx_v_obj)->obval; + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":1009 + * + * + * cdef inline npy_timedelta get_timedelta64_value(object obj) nogil: # <<<<<<<<<<<<<< + * """ + * returns the int64 value underlying scalar numpy timedelta64 object + */ + + /* function exit code */ + __pyx_L0:; + return __pyx_r; +} + +/* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":1016 + * + * + * cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil: # <<<<<<<<<<<<<< + * """ + * returns the unit part of the dtype for a numpy datetime64 object. + */ + +static CYTHON_INLINE NPY_DATETIMEUNIT __pyx_f_5numpy_get_datetime64_unit(PyObject *__pyx_v_obj) { + NPY_DATETIMEUNIT __pyx_r; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":1020 + * returns the unit part of the dtype for a numpy datetime64 object. + * """ + * return (obj).obmeta.base # <<<<<<<<<<<<<< + */ + __pyx_r = ((NPY_DATETIMEUNIT)((PyDatetimeScalarObject *)__pyx_v_obj)->obmeta.base); + goto __pyx_L0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":1016 + * + * + * cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil: # <<<<<<<<<<<<<< + * """ + * returns the unit part of the dtype for a numpy datetime64 object. + */ + + /* function exit code */ + __pyx_L0:; + return __pyx_r; +} + +static PyMethodDef __pyx_methods[] = { + {0, 0, 0, 0} +}; + +#if PY_MAJOR_VERSION >= 3 +#if CYTHON_PEP489_MULTI_PHASE_INIT +static PyObject* __pyx_pymod_create(PyObject *spec, PyModuleDef *def); /*proto*/ +static int __pyx_pymod_exec_cpu_nms(PyObject* module); /*proto*/ +static PyModuleDef_Slot __pyx_moduledef_slots[] = { + {Py_mod_create, (void*)__pyx_pymod_create}, + {Py_mod_exec, (void*)__pyx_pymod_exec_cpu_nms}, + {0, NULL} +}; +#endif + +static struct PyModuleDef __pyx_moduledef = { + PyModuleDef_HEAD_INIT, + "cpu_nms", + 0, /* m_doc */ + #if CYTHON_PEP489_MULTI_PHASE_INIT + 0, /* m_size */ + #else + -1, /* m_size */ + #endif + __pyx_methods /* m_methods */, + #if CYTHON_PEP489_MULTI_PHASE_INIT + __pyx_moduledef_slots, /* m_slots */ + #else + NULL, /* m_reload */ + #endif + NULL, /* m_traverse */ + NULL, /* m_clear */ + NULL /* m_free */ +}; +#endif +#ifndef CYTHON_SMALL_CODE +#if defined(__clang__) + #define CYTHON_SMALL_CODE +#elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) + #define CYTHON_SMALL_CODE __attribute__((cold)) +#else + #define CYTHON_SMALL_CODE +#endif +#endif + +static __Pyx_StringTabEntry __pyx_string_tab[] = { + {&__pyx_n_s_ImportError, __pyx_k_ImportError, sizeof(__pyx_k_ImportError), 0, 0, 1, 1}, + {&__pyx_n_s_N, __pyx_k_N, sizeof(__pyx_k_N), 0, 0, 1, 1}, + {&__pyx_n_s_Nt, __pyx_k_Nt, sizeof(__pyx_k_Nt), 0, 0, 1, 1}, + {&__pyx_n_s_area, __pyx_k_area, sizeof(__pyx_k_area), 0, 0, 1, 1}, + {&__pyx_n_s_areas, __pyx_k_areas, sizeof(__pyx_k_areas), 0, 0, 1, 1}, + {&__pyx_n_s_argsort, __pyx_k_argsort, sizeof(__pyx_k_argsort), 0, 0, 1, 1}, + {&__pyx_n_s_box_area, __pyx_k_box_area, sizeof(__pyx_k_box_area), 0, 0, 1, 1}, + {&__pyx_n_s_boxes, __pyx_k_boxes, sizeof(__pyx_k_boxes), 0, 0, 1, 1}, + {&__pyx_n_s_cline_in_traceback, __pyx_k_cline_in_traceback, sizeof(__pyx_k_cline_in_traceback), 0, 0, 1, 1}, + {&__pyx_n_s_cpu_nms, __pyx_k_cpu_nms, sizeof(__pyx_k_cpu_nms), 0, 0, 1, 1}, + {&__pyx_n_s_cpu_soft_nms, __pyx_k_cpu_soft_nms, sizeof(__pyx_k_cpu_soft_nms), 0, 0, 1, 1}, + {&__pyx_n_s_dets, __pyx_k_dets, sizeof(__pyx_k_dets), 0, 0, 1, 1}, + {&__pyx_n_s_dtype, __pyx_k_dtype, sizeof(__pyx_k_dtype), 0, 0, 1, 1}, + {&__pyx_n_s_exp, __pyx_k_exp, sizeof(__pyx_k_exp), 0, 0, 1, 1}, + {&__pyx_n_s_h, __pyx_k_h, sizeof(__pyx_k_h), 0, 0, 1, 1}, + {&__pyx_n_s_i, __pyx_k_i, sizeof(__pyx_k_i), 0, 0, 1, 1}, + {&__pyx_n_s_i_2, __pyx_k_i_2, sizeof(__pyx_k_i_2), 0, 0, 1, 1}, + {&__pyx_n_s_iarea, __pyx_k_iarea, sizeof(__pyx_k_iarea), 0, 0, 1, 1}, + {&__pyx_n_s_ih, __pyx_k_ih, sizeof(__pyx_k_ih), 0, 0, 1, 1}, + {&__pyx_n_s_import, __pyx_k_import, sizeof(__pyx_k_import), 0, 0, 1, 1}, + {&__pyx_n_s_int, __pyx_k_int, sizeof(__pyx_k_int), 0, 0, 1, 1}, + {&__pyx_n_s_inter, __pyx_k_inter, sizeof(__pyx_k_inter), 0, 0, 1, 1}, + {&__pyx_n_s_iw, __pyx_k_iw, sizeof(__pyx_k_iw), 0, 0, 1, 1}, + {&__pyx_n_s_ix1, __pyx_k_ix1, sizeof(__pyx_k_ix1), 0, 0, 1, 1}, + {&__pyx_n_s_ix2, __pyx_k_ix2, sizeof(__pyx_k_ix2), 0, 0, 1, 1}, + {&__pyx_n_s_iy1, __pyx_k_iy1, sizeof(__pyx_k_iy1), 0, 0, 1, 1}, + {&__pyx_n_s_iy2, __pyx_k_iy2, sizeof(__pyx_k_iy2), 0, 0, 1, 1}, + {&__pyx_n_s_j, __pyx_k_j, sizeof(__pyx_k_j), 0, 0, 1, 1}, + {&__pyx_n_s_j_2, __pyx_k_j_2, sizeof(__pyx_k_j_2), 0, 0, 1, 1}, + {&__pyx_n_s_keep, __pyx_k_keep, sizeof(__pyx_k_keep), 0, 0, 1, 1}, + {&__pyx_n_s_main, __pyx_k_main, sizeof(__pyx_k_main), 0, 0, 1, 1}, + {&__pyx_n_s_maxpos, __pyx_k_maxpos, sizeof(__pyx_k_maxpos), 0, 0, 1, 1}, + {&__pyx_n_s_maxscore, __pyx_k_maxscore, sizeof(__pyx_k_maxscore), 0, 0, 1, 1}, + {&__pyx_n_s_method, __pyx_k_method, sizeof(__pyx_k_method), 0, 0, 1, 1}, + {&__pyx_n_s_name, __pyx_k_name, sizeof(__pyx_k_name), 0, 0, 1, 1}, + {&__pyx_n_s_ndets, __pyx_k_ndets, sizeof(__pyx_k_ndets), 0, 0, 1, 1}, + {&__pyx_n_s_nms_cpu_nms, __pyx_k_nms_cpu_nms, sizeof(__pyx_k_nms_cpu_nms), 0, 0, 1, 1}, + {&__pyx_kp_s_nms_cpu_nms_pyx, __pyx_k_nms_cpu_nms_pyx, sizeof(__pyx_k_nms_cpu_nms_pyx), 0, 0, 1, 0}, + {&__pyx_n_s_np, __pyx_k_np, sizeof(__pyx_k_np), 0, 0, 1, 1}, + {&__pyx_n_s_numpy, __pyx_k_numpy, sizeof(__pyx_k_numpy), 0, 0, 1, 1}, + {&__pyx_kp_s_numpy_core_multiarray_failed_to, __pyx_k_numpy_core_multiarray_failed_to, sizeof(__pyx_k_numpy_core_multiarray_failed_to), 0, 0, 1, 0}, + {&__pyx_kp_s_numpy_core_umath_failed_to_impor, __pyx_k_numpy_core_umath_failed_to_impor, sizeof(__pyx_k_numpy_core_umath_failed_to_impor), 0, 0, 1, 0}, + {&__pyx_n_s_order, __pyx_k_order, sizeof(__pyx_k_order), 0, 0, 1, 1}, + {&__pyx_n_s_ov, __pyx_k_ov, sizeof(__pyx_k_ov), 0, 0, 1, 1}, + {&__pyx_n_s_ovr, __pyx_k_ovr, sizeof(__pyx_k_ovr), 0, 0, 1, 1}, + {&__pyx_n_s_pos, __pyx_k_pos, sizeof(__pyx_k_pos), 0, 0, 1, 1}, + {&__pyx_n_s_range, __pyx_k_range, sizeof(__pyx_k_range), 0, 0, 1, 1}, + {&__pyx_n_s_s, __pyx_k_s, sizeof(__pyx_k_s), 0, 0, 1, 1}, + {&__pyx_n_s_scores, __pyx_k_scores, sizeof(__pyx_k_scores), 0, 0, 1, 1}, + {&__pyx_n_s_sigma, __pyx_k_sigma, sizeof(__pyx_k_sigma), 0, 0, 1, 1}, + {&__pyx_n_s_suppressed, __pyx_k_suppressed, sizeof(__pyx_k_suppressed), 0, 0, 1, 1}, + {&__pyx_n_s_test, __pyx_k_test, sizeof(__pyx_k_test), 0, 0, 1, 1}, + {&__pyx_n_s_thresh, __pyx_k_thresh, sizeof(__pyx_k_thresh), 0, 0, 1, 1}, + {&__pyx_n_s_threshold, __pyx_k_threshold, sizeof(__pyx_k_threshold), 0, 0, 1, 1}, + {&__pyx_n_s_ts, __pyx_k_ts, sizeof(__pyx_k_ts), 0, 0, 1, 1}, + {&__pyx_n_s_tx1, __pyx_k_tx1, sizeof(__pyx_k_tx1), 0, 0, 1, 1}, + {&__pyx_n_s_tx2, __pyx_k_tx2, sizeof(__pyx_k_tx2), 0, 0, 1, 1}, + {&__pyx_n_s_ty1, __pyx_k_ty1, sizeof(__pyx_k_ty1), 0, 0, 1, 1}, + {&__pyx_n_s_ty2, __pyx_k_ty2, sizeof(__pyx_k_ty2), 0, 0, 1, 1}, + {&__pyx_n_s_ua, __pyx_k_ua, sizeof(__pyx_k_ua), 0, 0, 1, 1}, + {&__pyx_n_s_w, __pyx_k_w, sizeof(__pyx_k_w), 0, 0, 1, 1}, + {&__pyx_n_s_weight, __pyx_k_weight, sizeof(__pyx_k_weight), 0, 0, 1, 1}, + {&__pyx_n_s_x1, __pyx_k_x1, sizeof(__pyx_k_x1), 0, 0, 1, 1}, + {&__pyx_n_s_x2, __pyx_k_x2, sizeof(__pyx_k_x2), 0, 0, 1, 1}, + {&__pyx_n_s_xx1, __pyx_k_xx1, sizeof(__pyx_k_xx1), 0, 0, 1, 1}, + {&__pyx_n_s_xx2, __pyx_k_xx2, sizeof(__pyx_k_xx2), 0, 0, 1, 1}, + {&__pyx_n_s_y1, __pyx_k_y1, sizeof(__pyx_k_y1), 0, 0, 1, 1}, + {&__pyx_n_s_y2, __pyx_k_y2, sizeof(__pyx_k_y2), 0, 0, 1, 1}, + {&__pyx_n_s_yy1, __pyx_k_yy1, sizeof(__pyx_k_yy1), 0, 0, 1, 1}, + {&__pyx_n_s_yy2, __pyx_k_yy2, sizeof(__pyx_k_yy2), 0, 0, 1, 1}, + {&__pyx_n_s_zeros, __pyx_k_zeros, sizeof(__pyx_k_zeros), 0, 0, 1, 1}, + {0, 0, 0, 0, 0, 0, 0} +}; +static CYTHON_SMALL_CODE int __Pyx_InitCachedBuiltins(void) { + __pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_range) __PYX_ERR(0, 43, __pyx_L1_error) + __pyx_builtin_ImportError = __Pyx_GetBuiltinName(__pyx_n_s_ImportError); if (!__pyx_builtin_ImportError) __PYX_ERR(1, 947, __pyx_L1_error) + return 0; + __pyx_L1_error:; + return -1; +} + +static CYTHON_SMALL_CODE int __Pyx_InitCachedConstants(void) { + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0); + + /* "nms/cpu_nms.pyx":18 + * + * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh): + * cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0] # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] + * cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2] + */ + __pyx_slice_ = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice_)) __PYX_ERR(0, 18, __pyx_L1_error) + __Pyx_GOTREF(__pyx_slice_); + __Pyx_GIVEREF(__pyx_slice_); + __pyx_tuple__2 = PyTuple_Pack(2, __pyx_slice_, __pyx_int_0); if (unlikely(!__pyx_tuple__2)) __PYX_ERR(0, 18, __pyx_L1_error) + __Pyx_GOTREF(__pyx_tuple__2); + __Pyx_GIVEREF(__pyx_tuple__2); + + /* "nms/cpu_nms.pyx":19 + * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh): + * cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0] + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2] + * cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3] + */ + __pyx_tuple__3 = PyTuple_Pack(2, __pyx_slice_, __pyx_int_1); if (unlikely(!__pyx_tuple__3)) __PYX_ERR(0, 19, __pyx_L1_error) + __Pyx_GOTREF(__pyx_tuple__3); + __Pyx_GIVEREF(__pyx_tuple__3); + + /* "nms/cpu_nms.pyx":20 + * cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0] + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] + * cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2] # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3] + * cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4] + */ + __pyx_tuple__4 = PyTuple_Pack(2, __pyx_slice_, __pyx_int_2); if (unlikely(!__pyx_tuple__4)) __PYX_ERR(0, 20, __pyx_L1_error) + __Pyx_GOTREF(__pyx_tuple__4); + __Pyx_GIVEREF(__pyx_tuple__4); + + /* "nms/cpu_nms.pyx":21 + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] + * cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2] + * cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3] # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4] + * + */ + __pyx_tuple__5 = PyTuple_Pack(2, __pyx_slice_, __pyx_int_3); if (unlikely(!__pyx_tuple__5)) __PYX_ERR(0, 21, __pyx_L1_error) + __Pyx_GOTREF(__pyx_tuple__5); + __Pyx_GIVEREF(__pyx_tuple__5); + + /* "nms/cpu_nms.pyx":22 + * cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2] + * cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3] + * cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4] # <<<<<<<<<<<<<< + * + * cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1) + */ + __pyx_tuple__6 = PyTuple_Pack(2, __pyx_slice_, __pyx_int_4); if (unlikely(!__pyx_tuple__6)) __PYX_ERR(0, 22, __pyx_L1_error) + __Pyx_GOTREF(__pyx_tuple__6); + __Pyx_GIVEREF(__pyx_tuple__6); + + /* "nms/cpu_nms.pyx":25 + * + * cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1) + * cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1] # <<<<<<<<<<<<<< + * + * cdef int ndets = dets.shape[0] + */ + __pyx_slice__7 = PySlice_New(Py_None, Py_None, __pyx_int_neg_1); if (unlikely(!__pyx_slice__7)) __PYX_ERR(0, 25, __pyx_L1_error) + __Pyx_GOTREF(__pyx_slice__7); + __Pyx_GIVEREF(__pyx_slice__7); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":947 + * __pyx_import_array() + * except Exception: + * raise ImportError("numpy.core.multiarray failed to import") # <<<<<<<<<<<<<< + * + * cdef inline int import_umath() except -1: + */ + __pyx_tuple__8 = PyTuple_Pack(1, __pyx_kp_s_numpy_core_multiarray_failed_to); if (unlikely(!__pyx_tuple__8)) __PYX_ERR(1, 947, __pyx_L1_error) + __Pyx_GOTREF(__pyx_tuple__8); + __Pyx_GIVEREF(__pyx_tuple__8); + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":953 + * _import_umath() + * except Exception: + * raise ImportError("numpy.core.umath failed to import") # <<<<<<<<<<<<<< + * + * cdef inline int import_ufunc() except -1: + */ + __pyx_tuple__9 = PyTuple_Pack(1, __pyx_kp_s_numpy_core_umath_failed_to_impor); if (unlikely(!__pyx_tuple__9)) __PYX_ERR(1, 953, __pyx_L1_error) + __Pyx_GOTREF(__pyx_tuple__9); + __Pyx_GIVEREF(__pyx_tuple__9); + + /* "nms/cpu_nms.pyx":17 + * return a if a <= b else b + * + * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh): # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0] + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] + */ + __pyx_tuple__10 = PyTuple_Pack(29, __pyx_n_s_dets, __pyx_n_s_thresh, __pyx_n_s_x1, __pyx_n_s_y1, __pyx_n_s_x2, __pyx_n_s_y2, __pyx_n_s_scores, __pyx_n_s_areas, __pyx_n_s_order, __pyx_n_s_ndets, __pyx_n_s_suppressed, __pyx_n_s_i, __pyx_n_s_j, __pyx_n_s_i_2, __pyx_n_s_j_2, __pyx_n_s_ix1, __pyx_n_s_iy1, __pyx_n_s_ix2, __pyx_n_s_iy2, __pyx_n_s_iarea, __pyx_n_s_xx1, __pyx_n_s_yy1, __pyx_n_s_xx2, __pyx_n_s_yy2, __pyx_n_s_w, __pyx_n_s_h, __pyx_n_s_inter, __pyx_n_s_ovr, __pyx_n_s_keep); if (unlikely(!__pyx_tuple__10)) __PYX_ERR(0, 17, __pyx_L1_error) + __Pyx_GOTREF(__pyx_tuple__10); + __Pyx_GIVEREF(__pyx_tuple__10); + __pyx_codeobj__11 = (PyObject*)__Pyx_PyCode_New(2, 0, 29, 0, CO_OPTIMIZED|CO_NEWLOCALS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__10, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_nms_cpu_nms_pyx, __pyx_n_s_cpu_nms, 17, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__11)) __PYX_ERR(0, 17, __pyx_L1_error) + + /* "nms/cpu_nms.pyx":70 + * return keep + * + * def cpu_soft_nms(np.ndarray[float, ndim=2] boxes, float sigma=0.5, float Nt=0.3, float threshold=0.001, unsigned int method=0): # <<<<<<<<<<<<<< + * cdef unsigned int N = boxes.shape[0] + * cdef float iw, ih, box_area + */ + __pyx_tuple__12 = PyTuple_Pack(28, __pyx_n_s_boxes, __pyx_n_s_sigma, __pyx_n_s_Nt, __pyx_n_s_threshold, __pyx_n_s_method, __pyx_n_s_N, __pyx_n_s_iw, __pyx_n_s_ih, __pyx_n_s_box_area, __pyx_n_s_ua, __pyx_n_s_pos, __pyx_n_s_maxscore, __pyx_n_s_maxpos, __pyx_n_s_x1, __pyx_n_s_x2, __pyx_n_s_y1, __pyx_n_s_y2, __pyx_n_s_tx1, __pyx_n_s_tx2, __pyx_n_s_ty1, __pyx_n_s_ty2, __pyx_n_s_ts, __pyx_n_s_area, __pyx_n_s_weight, __pyx_n_s_ov, __pyx_n_s_i_2, __pyx_n_s_s, __pyx_n_s_keep); if (unlikely(!__pyx_tuple__12)) __PYX_ERR(0, 70, __pyx_L1_error) + __Pyx_GOTREF(__pyx_tuple__12); + __Pyx_GIVEREF(__pyx_tuple__12); + __pyx_codeobj__13 = (PyObject*)__Pyx_PyCode_New(5, 0, 28, 0, CO_OPTIMIZED|CO_NEWLOCALS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__12, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_nms_cpu_nms_pyx, __pyx_n_s_cpu_soft_nms, 70, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__13)) __PYX_ERR(0, 70, __pyx_L1_error) + __Pyx_RefNannyFinishContext(); + return 0; + __pyx_L1_error:; + __Pyx_RefNannyFinishContext(); + return -1; +} + +static CYTHON_SMALL_CODE int __Pyx_InitGlobals(void) { + if (__Pyx_InitStrings(__pyx_string_tab) < 0) __PYX_ERR(0, 1, __pyx_L1_error); + __pyx_int_0 = PyInt_FromLong(0); if (unlikely(!__pyx_int_0)) __PYX_ERR(0, 1, __pyx_L1_error) + __pyx_int_1 = PyInt_FromLong(1); if (unlikely(!__pyx_int_1)) __PYX_ERR(0, 1, __pyx_L1_error) + __pyx_int_2 = PyInt_FromLong(2); if (unlikely(!__pyx_int_2)) __PYX_ERR(0, 1, __pyx_L1_error) + __pyx_int_3 = PyInt_FromLong(3); if (unlikely(!__pyx_int_3)) __PYX_ERR(0, 1, __pyx_L1_error) + __pyx_int_4 = PyInt_FromLong(4); if (unlikely(!__pyx_int_4)) __PYX_ERR(0, 1, __pyx_L1_error) + __pyx_int_neg_1 = PyInt_FromLong(-1); if (unlikely(!__pyx_int_neg_1)) __PYX_ERR(0, 1, __pyx_L1_error) + return 0; + __pyx_L1_error:; + return -1; +} + +static CYTHON_SMALL_CODE int __Pyx_modinit_global_init_code(void); /*proto*/ +static CYTHON_SMALL_CODE int __Pyx_modinit_variable_export_code(void); /*proto*/ +static CYTHON_SMALL_CODE int __Pyx_modinit_function_export_code(void); /*proto*/ +static CYTHON_SMALL_CODE int __Pyx_modinit_type_init_code(void); /*proto*/ +static CYTHON_SMALL_CODE int __Pyx_modinit_type_import_code(void); /*proto*/ +static CYTHON_SMALL_CODE int __Pyx_modinit_variable_import_code(void); /*proto*/ +static CYTHON_SMALL_CODE int __Pyx_modinit_function_import_code(void); /*proto*/ + +static int __Pyx_modinit_global_init_code(void) { + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__Pyx_modinit_global_init_code", 0); + /*--- Global init code ---*/ + __Pyx_RefNannyFinishContext(); + return 0; +} + +static int __Pyx_modinit_variable_export_code(void) { + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__Pyx_modinit_variable_export_code", 0); + /*--- Variable export code ---*/ + __Pyx_RefNannyFinishContext(); + return 0; +} + +static int __Pyx_modinit_function_export_code(void) { + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__Pyx_modinit_function_export_code", 0); + /*--- Function export code ---*/ + __Pyx_RefNannyFinishContext(); + return 0; +} + +static int __Pyx_modinit_type_init_code(void) { + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__Pyx_modinit_type_init_code", 0); + /*--- Type init code ---*/ + __Pyx_RefNannyFinishContext(); + return 0; +} + +static int __Pyx_modinit_type_import_code(void) { + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("__Pyx_modinit_type_import_code", 0); + /*--- Type import code ---*/ + __pyx_t_1 = PyImport_ImportModule(__Pyx_BUILTIN_MODULE_NAME); if (unlikely(!__pyx_t_1)) __PYX_ERR(2, 9, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_ptype_7cpython_4type_type = __Pyx_ImportType(__pyx_t_1, __Pyx_BUILTIN_MODULE_NAME, "type", + #if defined(PYPY_VERSION_NUM) && PYPY_VERSION_NUM < 0x050B0000 + sizeof(PyTypeObject), + #else + sizeof(PyHeapTypeObject), + #endif + __Pyx_ImportType_CheckSize_Warn); + if (!__pyx_ptype_7cpython_4type_type) __PYX_ERR(2, 9, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyImport_ImportModule("numpy"); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 200, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_ptype_5numpy_dtype = __Pyx_ImportType(__pyx_t_1, "numpy", "dtype", sizeof(PyArray_Descr), __Pyx_ImportType_CheckSize_Ignore); + if (!__pyx_ptype_5numpy_dtype) __PYX_ERR(1, 200, __pyx_L1_error) + __pyx_ptype_5numpy_flatiter = __Pyx_ImportType(__pyx_t_1, "numpy", "flatiter", sizeof(PyArrayIterObject), __Pyx_ImportType_CheckSize_Ignore); + if (!__pyx_ptype_5numpy_flatiter) __PYX_ERR(1, 223, __pyx_L1_error) + __pyx_ptype_5numpy_broadcast = __Pyx_ImportType(__pyx_t_1, "numpy", "broadcast", sizeof(PyArrayMultiIterObject), __Pyx_ImportType_CheckSize_Ignore); + if (!__pyx_ptype_5numpy_broadcast) __PYX_ERR(1, 227, __pyx_L1_error) + __pyx_ptype_5numpy_ndarray = __Pyx_ImportType(__pyx_t_1, "numpy", "ndarray", sizeof(PyArrayObject), __Pyx_ImportType_CheckSize_Ignore); + if (!__pyx_ptype_5numpy_ndarray) __PYX_ERR(1, 239, __pyx_L1_error) + __pyx_ptype_5numpy_generic = __Pyx_ImportType(__pyx_t_1, "numpy", "generic", sizeof(PyObject), __Pyx_ImportType_CheckSize_Warn); + if (!__pyx_ptype_5numpy_generic) __PYX_ERR(1, 771, __pyx_L1_error) + __pyx_ptype_5numpy_number = __Pyx_ImportType(__pyx_t_1, "numpy", "number", sizeof(PyObject), __Pyx_ImportType_CheckSize_Warn); + if (!__pyx_ptype_5numpy_number) __PYX_ERR(1, 773, __pyx_L1_error) + __pyx_ptype_5numpy_integer = __Pyx_ImportType(__pyx_t_1, "numpy", "integer", sizeof(PyObject), __Pyx_ImportType_CheckSize_Warn); + if (!__pyx_ptype_5numpy_integer) __PYX_ERR(1, 775, __pyx_L1_error) + __pyx_ptype_5numpy_signedinteger = __Pyx_ImportType(__pyx_t_1, "numpy", "signedinteger", sizeof(PyObject), __Pyx_ImportType_CheckSize_Warn); + if (!__pyx_ptype_5numpy_signedinteger) __PYX_ERR(1, 777, __pyx_L1_error) + __pyx_ptype_5numpy_unsignedinteger = __Pyx_ImportType(__pyx_t_1, "numpy", "unsignedinteger", sizeof(PyObject), __Pyx_ImportType_CheckSize_Warn); + if (!__pyx_ptype_5numpy_unsignedinteger) __PYX_ERR(1, 779, __pyx_L1_error) + __pyx_ptype_5numpy_inexact = __Pyx_ImportType(__pyx_t_1, "numpy", "inexact", sizeof(PyObject), __Pyx_ImportType_CheckSize_Warn); + if (!__pyx_ptype_5numpy_inexact) __PYX_ERR(1, 781, __pyx_L1_error) + __pyx_ptype_5numpy_floating = __Pyx_ImportType(__pyx_t_1, "numpy", "floating", sizeof(PyObject), __Pyx_ImportType_CheckSize_Warn); + if (!__pyx_ptype_5numpy_floating) __PYX_ERR(1, 783, __pyx_L1_error) + __pyx_ptype_5numpy_complexfloating = __Pyx_ImportType(__pyx_t_1, "numpy", "complexfloating", sizeof(PyObject), __Pyx_ImportType_CheckSize_Warn); + if (!__pyx_ptype_5numpy_complexfloating) __PYX_ERR(1, 785, __pyx_L1_error) + __pyx_ptype_5numpy_flexible = __Pyx_ImportType(__pyx_t_1, "numpy", "flexible", sizeof(PyObject), __Pyx_ImportType_CheckSize_Warn); + if (!__pyx_ptype_5numpy_flexible) __PYX_ERR(1, 787, __pyx_L1_error) + __pyx_ptype_5numpy_character = __Pyx_ImportType(__pyx_t_1, "numpy", "character", sizeof(PyObject), __Pyx_ImportType_CheckSize_Warn); + if (!__pyx_ptype_5numpy_character) __PYX_ERR(1, 789, __pyx_L1_error) + __pyx_ptype_5numpy_ufunc = __Pyx_ImportType(__pyx_t_1, "numpy", "ufunc", sizeof(PyUFuncObject), __Pyx_ImportType_CheckSize_Ignore); + if (!__pyx_ptype_5numpy_ufunc) __PYX_ERR(1, 827, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_RefNannyFinishContext(); + return 0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_RefNannyFinishContext(); + return -1; +} + +static int __Pyx_modinit_variable_import_code(void) { + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__Pyx_modinit_variable_import_code", 0); + /*--- Variable import code ---*/ + __Pyx_RefNannyFinishContext(); + return 0; +} + +static int __Pyx_modinit_function_import_code(void) { + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__Pyx_modinit_function_import_code", 0); + /*--- Function import code ---*/ + __Pyx_RefNannyFinishContext(); + return 0; +} + + +#ifndef CYTHON_NO_PYINIT_EXPORT +#define __Pyx_PyMODINIT_FUNC PyMODINIT_FUNC +#elif PY_MAJOR_VERSION < 3 +#ifdef __cplusplus +#define __Pyx_PyMODINIT_FUNC extern "C" void +#else +#define __Pyx_PyMODINIT_FUNC void +#endif +#else +#ifdef __cplusplus +#define __Pyx_PyMODINIT_FUNC extern "C" PyObject * +#else +#define __Pyx_PyMODINIT_FUNC PyObject * +#endif +#endif + + +#if PY_MAJOR_VERSION < 3 +__Pyx_PyMODINIT_FUNC initcpu_nms(void) CYTHON_SMALL_CODE; /*proto*/ +__Pyx_PyMODINIT_FUNC initcpu_nms(void) +#else +__Pyx_PyMODINIT_FUNC PyInit_cpu_nms(void) CYTHON_SMALL_CODE; /*proto*/ +__Pyx_PyMODINIT_FUNC PyInit_cpu_nms(void) +#if CYTHON_PEP489_MULTI_PHASE_INIT +{ + return PyModuleDef_Init(&__pyx_moduledef); +} +static CYTHON_SMALL_CODE int __Pyx_check_single_interpreter(void) { + #if PY_VERSION_HEX >= 0x030700A1 + static PY_INT64_T main_interpreter_id = -1; + PY_INT64_T current_id = PyInterpreterState_GetID(PyThreadState_Get()->interp); + if (main_interpreter_id == -1) { + main_interpreter_id = current_id; + return (unlikely(current_id == -1)) ? -1 : 0; + } else if (unlikely(main_interpreter_id != current_id)) + #else + static PyInterpreterState *main_interpreter = NULL; + PyInterpreterState *current_interpreter = PyThreadState_Get()->interp; + if (!main_interpreter) { + main_interpreter = current_interpreter; + } else if (unlikely(main_interpreter != current_interpreter)) + #endif + { + PyErr_SetString( + PyExc_ImportError, + "Interpreter change detected - this module can only be loaded into one interpreter per process."); + return -1; + } + return 0; +} +static CYTHON_SMALL_CODE int __Pyx_copy_spec_to_module(PyObject *spec, PyObject *moddict, const char* from_name, const char* to_name, int allow_none) { + PyObject *value = PyObject_GetAttrString(spec, from_name); + int result = 0; + if (likely(value)) { + if (allow_none || value != Py_None) { + result = PyDict_SetItemString(moddict, to_name, value); + } + Py_DECREF(value); + } else if (PyErr_ExceptionMatches(PyExc_AttributeError)) { + PyErr_Clear(); + } else { + result = -1; + } + return result; +} +static CYTHON_SMALL_CODE PyObject* __pyx_pymod_create(PyObject *spec, CYTHON_UNUSED PyModuleDef *def) { + PyObject *module = NULL, *moddict, *modname; + if (__Pyx_check_single_interpreter()) + return NULL; + if (__pyx_m) + return __Pyx_NewRef(__pyx_m); + modname = PyObject_GetAttrString(spec, "name"); + if (unlikely(!modname)) goto bad; + module = PyModule_NewObject(modname); + Py_DECREF(modname); + if (unlikely(!module)) goto bad; + moddict = PyModule_GetDict(module); + if (unlikely(!moddict)) goto bad; + if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "loader", "__loader__", 1) < 0)) goto bad; + if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "origin", "__file__", 1) < 0)) goto bad; + if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "parent", "__package__", 1) < 0)) goto bad; + if (unlikely(__Pyx_copy_spec_to_module(spec, moddict, "submodule_search_locations", "__path__", 0) < 0)) goto bad; + return module; +bad: + Py_XDECREF(module); + return NULL; +} + + +static CYTHON_SMALL_CODE int __pyx_pymod_exec_cpu_nms(PyObject *__pyx_pyinit_module) +#endif +#endif +{ + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannyDeclarations + #if CYTHON_PEP489_MULTI_PHASE_INIT + if (__pyx_m) { + if (__pyx_m == __pyx_pyinit_module) return 0; + PyErr_SetString(PyExc_RuntimeError, "Module 'cpu_nms' has already been imported. Re-initialisation is not supported."); + return -1; + } + #elif PY_MAJOR_VERSION >= 3 + if (__pyx_m) return __Pyx_NewRef(__pyx_m); + #endif + #if CYTHON_REFNANNY +__Pyx_RefNanny = __Pyx_RefNannyImportAPI("refnanny"); +if (!__Pyx_RefNanny) { + PyErr_Clear(); + __Pyx_RefNanny = __Pyx_RefNannyImportAPI("Cython.Runtime.refnanny"); + if (!__Pyx_RefNanny) + Py_FatalError("failed to import 'refnanny' module"); +} +#endif + __Pyx_RefNannySetupContext("__Pyx_PyMODINIT_FUNC PyInit_cpu_nms(void)", 0); + if (__Pyx_check_binary_version() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + #ifdef __Pxy_PyFrame_Initialize_Offsets + __Pxy_PyFrame_Initialize_Offsets(); + #endif + __pyx_empty_tuple = PyTuple_New(0); if (unlikely(!__pyx_empty_tuple)) __PYX_ERR(0, 1, __pyx_L1_error) + __pyx_empty_bytes = PyBytes_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_bytes)) __PYX_ERR(0, 1, __pyx_L1_error) + __pyx_empty_unicode = PyUnicode_FromStringAndSize("", 0); if (unlikely(!__pyx_empty_unicode)) __PYX_ERR(0, 1, __pyx_L1_error) + #ifdef __Pyx_CyFunction_USED + if (__pyx_CyFunction_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + #endif + #ifdef __Pyx_FusedFunction_USED + if (__pyx_FusedFunction_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + #endif + #ifdef __Pyx_Coroutine_USED + if (__pyx_Coroutine_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + #endif + #ifdef __Pyx_Generator_USED + if (__pyx_Generator_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + #endif + #ifdef __Pyx_AsyncGen_USED + if (__pyx_AsyncGen_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + #endif + #ifdef __Pyx_StopAsyncIteration_USED + if (__pyx_StopAsyncIteration_init() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + #endif + /*--- Library function declarations ---*/ + /*--- Threads initialization code ---*/ + #if defined(WITH_THREAD) && PY_VERSION_HEX < 0x030700F0 && defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS + PyEval_InitThreads(); + #endif + /*--- Module creation code ---*/ + #if CYTHON_PEP489_MULTI_PHASE_INIT + __pyx_m = __pyx_pyinit_module; + Py_INCREF(__pyx_m); + #else + #if PY_MAJOR_VERSION < 3 + __pyx_m = Py_InitModule4("cpu_nms", __pyx_methods, 0, 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m); + #else + __pyx_m = PyModule_Create(&__pyx_moduledef); + #endif + if (unlikely(!__pyx_m)) __PYX_ERR(0, 1, __pyx_L1_error) + #endif + __pyx_d = PyModule_GetDict(__pyx_m); if (unlikely(!__pyx_d)) __PYX_ERR(0, 1, __pyx_L1_error) + Py_INCREF(__pyx_d); + __pyx_b = PyImport_AddModule(__Pyx_BUILTIN_MODULE_NAME); if (unlikely(!__pyx_b)) __PYX_ERR(0, 1, __pyx_L1_error) + Py_INCREF(__pyx_b); + __pyx_cython_runtime = PyImport_AddModule((char *) "cython_runtime"); if (unlikely(!__pyx_cython_runtime)) __PYX_ERR(0, 1, __pyx_L1_error) + Py_INCREF(__pyx_cython_runtime); + if (PyObject_SetAttrString(__pyx_m, "__builtins__", __pyx_b) < 0) __PYX_ERR(0, 1, __pyx_L1_error); + /*--- Initialize various global constants etc. ---*/ + if (__Pyx_InitGlobals() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + #if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT) + if (__Pyx_init_sys_getdefaultencoding_params() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + #endif + if (__pyx_module_is_main_nms__cpu_nms) { + if (PyObject_SetAttr(__pyx_m, __pyx_n_s_name, __pyx_n_s_main) < 0) __PYX_ERR(0, 1, __pyx_L1_error) + } + #if PY_MAJOR_VERSION >= 3 + { + PyObject *modules = PyImport_GetModuleDict(); if (unlikely(!modules)) __PYX_ERR(0, 1, __pyx_L1_error) + if (!PyDict_GetItemString(modules, "nms.cpu_nms")) { + if (unlikely(PyDict_SetItemString(modules, "nms.cpu_nms", __pyx_m) < 0)) __PYX_ERR(0, 1, __pyx_L1_error) + } + } + #endif + /*--- Builtin init code ---*/ + if (__Pyx_InitCachedBuiltins() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + /*--- Constants init code ---*/ + if (__Pyx_InitCachedConstants() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + /*--- Global type/function init code ---*/ + (void)__Pyx_modinit_global_init_code(); + (void)__Pyx_modinit_variable_export_code(); + (void)__Pyx_modinit_function_export_code(); + (void)__Pyx_modinit_type_init_code(); + if (unlikely(__Pyx_modinit_type_import_code() < 0)) __PYX_ERR(0, 1, __pyx_L1_error) + (void)__Pyx_modinit_variable_import_code(); + (void)__Pyx_modinit_function_import_code(); + /*--- Execution code ---*/ + #if defined(__Pyx_Generator_USED) || defined(__Pyx_Coroutine_USED) + if (__Pyx_patch_abc() < 0) __PYX_ERR(0, 1, __pyx_L1_error) + #endif + + /* "nms/cpu_nms.pyx":8 + * # -------------------------------------------------------- + * + * import numpy as np # <<<<<<<<<<<<<< + * cimport numpy as np + * + */ + __pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 8, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_1) < 0) __PYX_ERR(0, 8, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "nms/cpu_nms.pyx":17 + * return a if a <= b else b + * + * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh): # <<<<<<<<<<<<<< + * cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0] + * cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] + */ + __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_3nms_7cpu_nms_1cpu_nms, NULL, __pyx_n_s_nms_cpu_nms); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 17, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + if (PyDict_SetItem(__pyx_d, __pyx_n_s_cpu_nms, __pyx_t_1) < 0) __PYX_ERR(0, 17, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "nms/cpu_nms.pyx":70 + * return keep + * + * def cpu_soft_nms(np.ndarray[float, ndim=2] boxes, float sigma=0.5, float Nt=0.3, float threshold=0.001, unsigned int method=0): # <<<<<<<<<<<<<< + * cdef unsigned int N = boxes.shape[0] + * cdef float iw, ih, box_area + */ + __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_3nms_7cpu_nms_3cpu_soft_nms, NULL, __pyx_n_s_nms_cpu_nms); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 70, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + if (PyDict_SetItem(__pyx_d, __pyx_n_s_cpu_soft_nms, __pyx_t_1) < 0) __PYX_ERR(0, 70, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "nms/cpu_nms.pyx":1 + * # -------------------------------------------------------- # <<<<<<<<<<<<<< + * # Fast R-CNN + * # Copyright (c) 2015 Microsoft + */ + __pyx_t_1 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 1, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + + /* "../../../../../../../root/anaconda3/lib/python3.9/site-packages/numpy/__init__.pxd":1016 + * + * + * cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil: # <<<<<<<<<<<<<< + * """ + * returns the unit part of the dtype for a numpy datetime64 object. + */ + + /*--- Wrapped vars code ---*/ + + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + if (__pyx_m) { + if (__pyx_d) { + __Pyx_AddTraceback("init nms.cpu_nms", __pyx_clineno, __pyx_lineno, __pyx_filename); + } + Py_CLEAR(__pyx_m); + } else if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_ImportError, "init nms.cpu_nms"); + } + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + #if CYTHON_PEP489_MULTI_PHASE_INIT + return (__pyx_m != NULL) ? 0 : -1; + #elif PY_MAJOR_VERSION >= 3 + return __pyx_m; + #else + return; + #endif +} + +/* --- Runtime support code --- */ +/* Refnanny */ +#if CYTHON_REFNANNY +static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) { + PyObject *m = NULL, *p = NULL; + void *r = NULL; + m = PyImport_ImportModule(modname); + if (!m) goto end; + p = PyObject_GetAttrString(m, "RefNannyAPI"); + if (!p) goto end; + r = PyLong_AsVoidPtr(p); +end: + Py_XDECREF(p); + Py_XDECREF(m); + return (__Pyx_RefNannyAPIStruct *)r; +} +#endif + +/* PyObjectGetAttrStr */ +#if CYTHON_USE_TYPE_SLOTS +static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name) { + PyTypeObject* tp = Py_TYPE(obj); + if (likely(tp->tp_getattro)) + return tp->tp_getattro(obj, attr_name); +#if PY_MAJOR_VERSION < 3 + if (likely(tp->tp_getattr)) + return tp->tp_getattr(obj, PyString_AS_STRING(attr_name)); +#endif + return PyObject_GetAttr(obj, attr_name); +} +#endif + +/* GetBuiltinName */ +static PyObject *__Pyx_GetBuiltinName(PyObject *name) { + PyObject* result = __Pyx_PyObject_GetAttrStr(__pyx_b, name); + if (unlikely(!result)) { + PyErr_Format(PyExc_NameError, +#if PY_MAJOR_VERSION >= 3 + "name '%U' is not defined", name); +#else + "name '%.200s' is not defined", PyString_AS_STRING(name)); +#endif + } + return result; +} + +/* RaiseArgTupleInvalid */ +static void __Pyx_RaiseArgtupleInvalid( + const char* func_name, + int exact, + Py_ssize_t num_min, + Py_ssize_t num_max, + Py_ssize_t num_found) +{ + Py_ssize_t num_expected; + const char *more_or_less; + if (num_found < num_min) { + num_expected = num_min; + more_or_less = "at least"; + } else { + num_expected = num_max; + more_or_less = "at most"; + } + if (exact) { + more_or_less = "exactly"; + } + PyErr_Format(PyExc_TypeError, + "%.200s() takes %.8s %" CYTHON_FORMAT_SSIZE_T "d positional argument%.1s (%" CYTHON_FORMAT_SSIZE_T "d given)", + func_name, more_or_less, num_expected, + (num_expected == 1) ? "" : "s", num_found); +} + +/* RaiseDoubleKeywords */ +static void __Pyx_RaiseDoubleKeywordsError( + const char* func_name, + PyObject* kw_name) +{ + PyErr_Format(PyExc_TypeError, + #if PY_MAJOR_VERSION >= 3 + "%s() got multiple values for keyword argument '%U'", func_name, kw_name); + #else + "%s() got multiple values for keyword argument '%s'", func_name, + PyString_AsString(kw_name)); + #endif +} + +/* ParseKeywords */ +static int __Pyx_ParseOptionalKeywords( + PyObject *kwds, + PyObject **argnames[], + PyObject *kwds2, + PyObject *values[], + Py_ssize_t num_pos_args, + const char* function_name) +{ + PyObject *key = 0, *value = 0; + Py_ssize_t pos = 0; + PyObject*** name; + PyObject*** first_kw_arg = argnames + num_pos_args; + while (PyDict_Next(kwds, &pos, &key, &value)) { + name = first_kw_arg; + while (*name && (**name != key)) name++; + if (*name) { + values[name-argnames] = value; + continue; + } + name = first_kw_arg; + #if PY_MAJOR_VERSION < 3 + if (likely(PyString_Check(key))) { + while (*name) { + if ((CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**name) == PyString_GET_SIZE(key)) + && _PyString_Eq(**name, key)) { + values[name-argnames] = value; + break; + } + name++; + } + if (*name) continue; + else { + PyObject*** argname = argnames; + while (argname != first_kw_arg) { + if ((**argname == key) || ( + (CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**argname) == PyString_GET_SIZE(key)) + && _PyString_Eq(**argname, key))) { + goto arg_passed_twice; + } + argname++; + } + } + } else + #endif + if (likely(PyUnicode_Check(key))) { + while (*name) { + int cmp = (**name == key) ? 0 : + #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3 + (__Pyx_PyUnicode_GET_LENGTH(**name) != __Pyx_PyUnicode_GET_LENGTH(key)) ? 1 : + #endif + PyUnicode_Compare(**name, key); + if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad; + if (cmp == 0) { + values[name-argnames] = value; + break; + } + name++; + } + if (*name) continue; + else { + PyObject*** argname = argnames; + while (argname != first_kw_arg) { + int cmp = (**argname == key) ? 0 : + #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3 + (__Pyx_PyUnicode_GET_LENGTH(**argname) != __Pyx_PyUnicode_GET_LENGTH(key)) ? 1 : + #endif + PyUnicode_Compare(**argname, key); + if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad; + if (cmp == 0) goto arg_passed_twice; + argname++; + } + } + } else + goto invalid_keyword_type; + if (kwds2) { + if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad; + } else { + goto invalid_keyword; + } + } + return 0; +arg_passed_twice: + __Pyx_RaiseDoubleKeywordsError(function_name, key); + goto bad; +invalid_keyword_type: + PyErr_Format(PyExc_TypeError, + "%.200s() keywords must be strings", function_name); + goto bad; +invalid_keyword: + PyErr_Format(PyExc_TypeError, + #if PY_MAJOR_VERSION < 3 + "%.200s() got an unexpected keyword argument '%.200s'", + function_name, PyString_AsString(key)); + #else + "%s() got an unexpected keyword argument '%U'", + function_name, key); + #endif +bad: + return -1; +} + +/* ArgTypeTest */ +static int __Pyx__ArgTypeTest(PyObject *obj, PyTypeObject *type, const char *name, int exact) +{ + if (unlikely(!type)) { + PyErr_SetString(PyExc_SystemError, "Missing type object"); + return 0; + } + else if (exact) { + #if PY_MAJOR_VERSION == 2 + if ((type == &PyBaseString_Type) && likely(__Pyx_PyBaseString_CheckExact(obj))) return 1; + #endif + } + else { + if (likely(__Pyx_TypeCheck(obj, type))) return 1; + } + PyErr_Format(PyExc_TypeError, + "Argument '%.200s' has incorrect type (expected %.200s, got %.200s)", + name, type->tp_name, Py_TYPE(obj)->tp_name); + return 0; +} + +/* IsLittleEndian */ +static CYTHON_INLINE int __Pyx_Is_Little_Endian(void) +{ + union { + uint32_t u32; + uint8_t u8[4]; + } S; + S.u32 = 0x01020304; + return S.u8[0] == 4; +} + +/* BufferFormatCheck */ +static void __Pyx_BufFmt_Init(__Pyx_BufFmt_Context* ctx, + __Pyx_BufFmt_StackElem* stack, + __Pyx_TypeInfo* type) { + stack[0].field = &ctx->root; + stack[0].parent_offset = 0; + ctx->root.type = type; + ctx->root.name = "buffer dtype"; + ctx->root.offset = 0; + ctx->head = stack; + ctx->head->field = &ctx->root; + ctx->fmt_offset = 0; + ctx->head->parent_offset = 0; + ctx->new_packmode = '@'; + ctx->enc_packmode = '@'; + ctx->new_count = 1; + ctx->enc_count = 0; + ctx->enc_type = 0; + ctx->is_complex = 0; + ctx->is_valid_array = 0; + ctx->struct_alignment = 0; + while (type->typegroup == 'S') { + ++ctx->head; + ctx->head->field = type->fields; + ctx->head->parent_offset = 0; + type = type->fields->type; + } +} +static int __Pyx_BufFmt_ParseNumber(const char** ts) { + int count; + const char* t = *ts; + if (*t < '0' || *t > '9') { + return -1; + } else { + count = *t++ - '0'; + while (*t >= '0' && *t <= '9') { + count *= 10; + count += *t++ - '0'; + } + } + *ts = t; + return count; +} +static int __Pyx_BufFmt_ExpectNumber(const char **ts) { + int number = __Pyx_BufFmt_ParseNumber(ts); + if (number == -1) + PyErr_Format(PyExc_ValueError,\ + "Does not understand character buffer dtype format string ('%c')", **ts); + return number; +} +static void __Pyx_BufFmt_RaiseUnexpectedChar(char ch) { + PyErr_Format(PyExc_ValueError, + "Unexpected format string character: '%c'", ch); +} +static const char* __Pyx_BufFmt_DescribeTypeChar(char ch, int is_complex) { + switch (ch) { + case '?': return "'bool'"; + case 'c': return "'char'"; + case 'b': return "'signed char'"; + case 'B': return "'unsigned char'"; + case 'h': return "'short'"; + case 'H': return "'unsigned short'"; + case 'i': return "'int'"; + case 'I': return "'unsigned int'"; + case 'l': return "'long'"; + case 'L': return "'unsigned long'"; + case 'q': return "'long long'"; + case 'Q': return "'unsigned long long'"; + case 'f': return (is_complex ? "'complex float'" : "'float'"); + case 'd': return (is_complex ? "'complex double'" : "'double'"); + case 'g': return (is_complex ? "'complex long double'" : "'long double'"); + case 'T': return "a struct"; + case 'O': return "Python object"; + case 'P': return "a pointer"; + case 's': case 'p': return "a string"; + case 0: return "end"; + default: return "unparseable format string"; + } +} +static size_t __Pyx_BufFmt_TypeCharToStandardSize(char ch, int is_complex) { + switch (ch) { + case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1; + case 'h': case 'H': return 2; + case 'i': case 'I': case 'l': case 'L': return 4; + case 'q': case 'Q': return 8; + case 'f': return (is_complex ? 8 : 4); + case 'd': return (is_complex ? 16 : 8); + case 'g': { + PyErr_SetString(PyExc_ValueError, "Python does not define a standard format string size for long double ('g').."); + return 0; + } + case 'O': case 'P': return sizeof(void*); + default: + __Pyx_BufFmt_RaiseUnexpectedChar(ch); + return 0; + } +} +static size_t __Pyx_BufFmt_TypeCharToNativeSize(char ch, int is_complex) { + switch (ch) { + case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1; + case 'h': case 'H': return sizeof(short); + case 'i': case 'I': return sizeof(int); + case 'l': case 'L': return sizeof(long); + #ifdef HAVE_LONG_LONG + case 'q': case 'Q': return sizeof(PY_LONG_LONG); + #endif + case 'f': return sizeof(float) * (is_complex ? 2 : 1); + case 'd': return sizeof(double) * (is_complex ? 2 : 1); + case 'g': return sizeof(long double) * (is_complex ? 2 : 1); + case 'O': case 'P': return sizeof(void*); + default: { + __Pyx_BufFmt_RaiseUnexpectedChar(ch); + return 0; + } + } +} +typedef struct { char c; short x; } __Pyx_st_short; +typedef struct { char c; int x; } __Pyx_st_int; +typedef struct { char c; long x; } __Pyx_st_long; +typedef struct { char c; float x; } __Pyx_st_float; +typedef struct { char c; double x; } __Pyx_st_double; +typedef struct { char c; long double x; } __Pyx_st_longdouble; +typedef struct { char c; void *x; } __Pyx_st_void_p; +#ifdef HAVE_LONG_LONG +typedef struct { char c; PY_LONG_LONG x; } __Pyx_st_longlong; +#endif +static size_t __Pyx_BufFmt_TypeCharToAlignment(char ch, CYTHON_UNUSED int is_complex) { + switch (ch) { + case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1; + case 'h': case 'H': return sizeof(__Pyx_st_short) - sizeof(short); + case 'i': case 'I': return sizeof(__Pyx_st_int) - sizeof(int); + case 'l': case 'L': return sizeof(__Pyx_st_long) - sizeof(long); +#ifdef HAVE_LONG_LONG + case 'q': case 'Q': return sizeof(__Pyx_st_longlong) - sizeof(PY_LONG_LONG); +#endif + case 'f': return sizeof(__Pyx_st_float) - sizeof(float); + case 'd': return sizeof(__Pyx_st_double) - sizeof(double); + case 'g': return sizeof(__Pyx_st_longdouble) - sizeof(long double); + case 'P': case 'O': return sizeof(__Pyx_st_void_p) - sizeof(void*); + default: + __Pyx_BufFmt_RaiseUnexpectedChar(ch); + return 0; + } +} +/* These are for computing the padding at the end of the struct to align + on the first member of the struct. This will probably the same as above, + but we don't have any guarantees. + */ +typedef struct { short x; char c; } __Pyx_pad_short; +typedef struct { int x; char c; } __Pyx_pad_int; +typedef struct { long x; char c; } __Pyx_pad_long; +typedef struct { float x; char c; } __Pyx_pad_float; +typedef struct { double x; char c; } __Pyx_pad_double; +typedef struct { long double x; char c; } __Pyx_pad_longdouble; +typedef struct { void *x; char c; } __Pyx_pad_void_p; +#ifdef HAVE_LONG_LONG +typedef struct { PY_LONG_LONG x; char c; } __Pyx_pad_longlong; +#endif +static size_t __Pyx_BufFmt_TypeCharToPadding(char ch, CYTHON_UNUSED int is_complex) { + switch (ch) { + case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1; + case 'h': case 'H': return sizeof(__Pyx_pad_short) - sizeof(short); + case 'i': case 'I': return sizeof(__Pyx_pad_int) - sizeof(int); + case 'l': case 'L': return sizeof(__Pyx_pad_long) - sizeof(long); +#ifdef HAVE_LONG_LONG + case 'q': case 'Q': return sizeof(__Pyx_pad_longlong) - sizeof(PY_LONG_LONG); +#endif + case 'f': return sizeof(__Pyx_pad_float) - sizeof(float); + case 'd': return sizeof(__Pyx_pad_double) - sizeof(double); + case 'g': return sizeof(__Pyx_pad_longdouble) - sizeof(long double); + case 'P': case 'O': return sizeof(__Pyx_pad_void_p) - sizeof(void*); + default: + __Pyx_BufFmt_RaiseUnexpectedChar(ch); + return 0; + } +} +static char __Pyx_BufFmt_TypeCharToGroup(char ch, int is_complex) { + switch (ch) { + case 'c': + return 'H'; + case 'b': case 'h': case 'i': + case 'l': case 'q': case 's': case 'p': + return 'I'; + case '?': case 'B': case 'H': case 'I': case 'L': case 'Q': + return 'U'; + case 'f': case 'd': case 'g': + return (is_complex ? 'C' : 'R'); + case 'O': + return 'O'; + case 'P': + return 'P'; + default: { + __Pyx_BufFmt_RaiseUnexpectedChar(ch); + return 0; + } + } +} +static void __Pyx_BufFmt_RaiseExpected(__Pyx_BufFmt_Context* ctx) { + if (ctx->head == NULL || ctx->head->field == &ctx->root) { + const char* expected; + const char* quote; + if (ctx->head == NULL) { + expected = "end"; + quote = ""; + } else { + expected = ctx->head->field->type->name; + quote = "'"; + } + PyErr_Format(PyExc_ValueError, + "Buffer dtype mismatch, expected %s%s%s but got %s", + quote, expected, quote, + __Pyx_BufFmt_DescribeTypeChar(ctx->enc_type, ctx->is_complex)); + } else { + __Pyx_StructField* field = ctx->head->field; + __Pyx_StructField* parent = (ctx->head - 1)->field; + PyErr_Format(PyExc_ValueError, + "Buffer dtype mismatch, expected '%s' but got %s in '%s.%s'", + field->type->name, __Pyx_BufFmt_DescribeTypeChar(ctx->enc_type, ctx->is_complex), + parent->type->name, field->name); + } +} +static int __Pyx_BufFmt_ProcessTypeChunk(__Pyx_BufFmt_Context* ctx) { + char group; + size_t size, offset, arraysize = 1; + if (ctx->enc_type == 0) return 0; + if (ctx->head->field->type->arraysize[0]) { + int i, ndim = 0; + if (ctx->enc_type == 's' || ctx->enc_type == 'p') { + ctx->is_valid_array = ctx->head->field->type->ndim == 1; + ndim = 1; + if (ctx->enc_count != ctx->head->field->type->arraysize[0]) { + PyErr_Format(PyExc_ValueError, + "Expected a dimension of size %zu, got %zu", + ctx->head->field->type->arraysize[0], ctx->enc_count); + return -1; + } + } + if (!ctx->is_valid_array) { + PyErr_Format(PyExc_ValueError, "Expected %d dimensions, got %d", + ctx->head->field->type->ndim, ndim); + return -1; + } + for (i = 0; i < ctx->head->field->type->ndim; i++) { + arraysize *= ctx->head->field->type->arraysize[i]; + } + ctx->is_valid_array = 0; + ctx->enc_count = 1; + } + group = __Pyx_BufFmt_TypeCharToGroup(ctx->enc_type, ctx->is_complex); + do { + __Pyx_StructField* field = ctx->head->field; + __Pyx_TypeInfo* type = field->type; + if (ctx->enc_packmode == '@' || ctx->enc_packmode == '^') { + size = __Pyx_BufFmt_TypeCharToNativeSize(ctx->enc_type, ctx->is_complex); + } else { + size = __Pyx_BufFmt_TypeCharToStandardSize(ctx->enc_type, ctx->is_complex); + } + if (ctx->enc_packmode == '@') { + size_t align_at = __Pyx_BufFmt_TypeCharToAlignment(ctx->enc_type, ctx->is_complex); + size_t align_mod_offset; + if (align_at == 0) return -1; + align_mod_offset = ctx->fmt_offset % align_at; + if (align_mod_offset > 0) ctx->fmt_offset += align_at - align_mod_offset; + if (ctx->struct_alignment == 0) + ctx->struct_alignment = __Pyx_BufFmt_TypeCharToPadding(ctx->enc_type, + ctx->is_complex); + } + if (type->size != size || type->typegroup != group) { + if (type->typegroup == 'C' && type->fields != NULL) { + size_t parent_offset = ctx->head->parent_offset + field->offset; + ++ctx->head; + ctx->head->field = type->fields; + ctx->head->parent_offset = parent_offset; + continue; + } + if ((type->typegroup == 'H' || group == 'H') && type->size == size) { + } else { + __Pyx_BufFmt_RaiseExpected(ctx); + return -1; + } + } + offset = ctx->head->parent_offset + field->offset; + if (ctx->fmt_offset != offset) { + PyErr_Format(PyExc_ValueError, + "Buffer dtype mismatch; next field is at offset %" CYTHON_FORMAT_SSIZE_T "d but %" CYTHON_FORMAT_SSIZE_T "d expected", + (Py_ssize_t)ctx->fmt_offset, (Py_ssize_t)offset); + return -1; + } + ctx->fmt_offset += size; + if (arraysize) + ctx->fmt_offset += (arraysize - 1) * size; + --ctx->enc_count; + while (1) { + if (field == &ctx->root) { + ctx->head = NULL; + if (ctx->enc_count != 0) { + __Pyx_BufFmt_RaiseExpected(ctx); + return -1; + } + break; + } + ctx->head->field = ++field; + if (field->type == NULL) { + --ctx->head; + field = ctx->head->field; + continue; + } else if (field->type->typegroup == 'S') { + size_t parent_offset = ctx->head->parent_offset + field->offset; + if (field->type->fields->type == NULL) continue; + field = field->type->fields; + ++ctx->head; + ctx->head->field = field; + ctx->head->parent_offset = parent_offset; + break; + } else { + break; + } + } + } while (ctx->enc_count); + ctx->enc_type = 0; + ctx->is_complex = 0; + return 0; +} +static PyObject * +__pyx_buffmt_parse_array(__Pyx_BufFmt_Context* ctx, const char** tsp) +{ + const char *ts = *tsp; + int i = 0, number, ndim; + ++ts; + if (ctx->new_count != 1) { + PyErr_SetString(PyExc_ValueError, + "Cannot handle repeated arrays in format string"); + return NULL; + } + if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL; + ndim = ctx->head->field->type->ndim; + while (*ts && *ts != ')') { + switch (*ts) { + case ' ': case '\f': case '\r': case '\n': case '\t': case '\v': continue; + default: break; + } + number = __Pyx_BufFmt_ExpectNumber(&ts); + if (number == -1) return NULL; + if (i < ndim && (size_t) number != ctx->head->field->type->arraysize[i]) + return PyErr_Format(PyExc_ValueError, + "Expected a dimension of size %zu, got %d", + ctx->head->field->type->arraysize[i], number); + if (*ts != ',' && *ts != ')') + return PyErr_Format(PyExc_ValueError, + "Expected a comma in format string, got '%c'", *ts); + if (*ts == ',') ts++; + i++; + } + if (i != ndim) + return PyErr_Format(PyExc_ValueError, "Expected %d dimension(s), got %d", + ctx->head->field->type->ndim, i); + if (!*ts) { + PyErr_SetString(PyExc_ValueError, + "Unexpected end of format string, expected ')'"); + return NULL; + } + ctx->is_valid_array = 1; + ctx->new_count = 1; + *tsp = ++ts; + return Py_None; +} +static const char* __Pyx_BufFmt_CheckString(__Pyx_BufFmt_Context* ctx, const char* ts) { + int got_Z = 0; + while (1) { + switch(*ts) { + case 0: + if (ctx->enc_type != 0 && ctx->head == NULL) { + __Pyx_BufFmt_RaiseExpected(ctx); + return NULL; + } + if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL; + if (ctx->head != NULL) { + __Pyx_BufFmt_RaiseExpected(ctx); + return NULL; + } + return ts; + case ' ': + case '\r': + case '\n': + ++ts; + break; + case '<': + if (!__Pyx_Is_Little_Endian()) { + PyErr_SetString(PyExc_ValueError, "Little-endian buffer not supported on big-endian compiler"); + return NULL; + } + ctx->new_packmode = '='; + ++ts; + break; + case '>': + case '!': + if (__Pyx_Is_Little_Endian()) { + PyErr_SetString(PyExc_ValueError, "Big-endian buffer not supported on little-endian compiler"); + return NULL; + } + ctx->new_packmode = '='; + ++ts; + break; + case '=': + case '@': + case '^': + ctx->new_packmode = *ts++; + break; + case 'T': + { + const char* ts_after_sub; + size_t i, struct_count = ctx->new_count; + size_t struct_alignment = ctx->struct_alignment; + ctx->new_count = 1; + ++ts; + if (*ts != '{') { + PyErr_SetString(PyExc_ValueError, "Buffer acquisition: Expected '{' after 'T'"); + return NULL; + } + if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL; + ctx->enc_type = 0; + ctx->enc_count = 0; + ctx->struct_alignment = 0; + ++ts; + ts_after_sub = ts; + for (i = 0; i != struct_count; ++i) { + ts_after_sub = __Pyx_BufFmt_CheckString(ctx, ts); + if (!ts_after_sub) return NULL; + } + ts = ts_after_sub; + if (struct_alignment) ctx->struct_alignment = struct_alignment; + } + break; + case '}': + { + size_t alignment = ctx->struct_alignment; + ++ts; + if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL; + ctx->enc_type = 0; + if (alignment && ctx->fmt_offset % alignment) { + ctx->fmt_offset += alignment - (ctx->fmt_offset % alignment); + } + } + return ts; + case 'x': + if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL; + ctx->fmt_offset += ctx->new_count; + ctx->new_count = 1; + ctx->enc_count = 0; + ctx->enc_type = 0; + ctx->enc_packmode = ctx->new_packmode; + ++ts; + break; + case 'Z': + got_Z = 1; + ++ts; + if (*ts != 'f' && *ts != 'd' && *ts != 'g') { + __Pyx_BufFmt_RaiseUnexpectedChar('Z'); + return NULL; + } + CYTHON_FALLTHROUGH; + case '?': case 'c': case 'b': case 'B': case 'h': case 'H': case 'i': case 'I': + case 'l': case 'L': case 'q': case 'Q': + case 'f': case 'd': case 'g': + case 'O': case 'p': + if ((ctx->enc_type == *ts) && (got_Z == ctx->is_complex) && + (ctx->enc_packmode == ctx->new_packmode) && (!ctx->is_valid_array)) { + ctx->enc_count += ctx->new_count; + ctx->new_count = 1; + got_Z = 0; + ++ts; + break; + } + CYTHON_FALLTHROUGH; + case 's': + if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL; + ctx->enc_count = ctx->new_count; + ctx->enc_packmode = ctx->new_packmode; + ctx->enc_type = *ts; + ctx->is_complex = got_Z; + ++ts; + ctx->new_count = 1; + got_Z = 0; + break; + case ':': + ++ts; + while(*ts != ':') ++ts; + ++ts; + break; + case '(': + if (!__pyx_buffmt_parse_array(ctx, &ts)) return NULL; + break; + default: + { + int number = __Pyx_BufFmt_ExpectNumber(&ts); + if (number == -1) return NULL; + ctx->new_count = (size_t)number; + } + } + } +} + +/* BufferGetAndValidate */ + static CYTHON_INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info) { + if (unlikely(info->buf == NULL)) return; + if (info->suboffsets == __Pyx_minusones) info->suboffsets = NULL; + __Pyx_ReleaseBuffer(info); +} +static void __Pyx_ZeroBuffer(Py_buffer* buf) { + buf->buf = NULL; + buf->obj = NULL; + buf->strides = __Pyx_zeros; + buf->shape = __Pyx_zeros; + buf->suboffsets = __Pyx_minusones; +} +static int __Pyx__GetBufferAndValidate( + Py_buffer* buf, PyObject* obj, __Pyx_TypeInfo* dtype, int flags, + int nd, int cast, __Pyx_BufFmt_StackElem* stack) +{ + buf->buf = NULL; + if (unlikely(__Pyx_GetBuffer(obj, buf, flags) == -1)) { + __Pyx_ZeroBuffer(buf); + return -1; + } + if (unlikely(buf->ndim != nd)) { + PyErr_Format(PyExc_ValueError, + "Buffer has wrong number of dimensions (expected %d, got %d)", + nd, buf->ndim); + goto fail; + } + if (!cast) { + __Pyx_BufFmt_Context ctx; + __Pyx_BufFmt_Init(&ctx, stack, dtype); + if (!__Pyx_BufFmt_CheckString(&ctx, buf->format)) goto fail; + } + if (unlikely((size_t)buf->itemsize != dtype->size)) { + PyErr_Format(PyExc_ValueError, + "Item size of buffer (%" CYTHON_FORMAT_SSIZE_T "d byte%s) does not match size of '%s' (%" CYTHON_FORMAT_SSIZE_T "d byte%s)", + buf->itemsize, (buf->itemsize > 1) ? "s" : "", + dtype->name, (Py_ssize_t)dtype->size, (dtype->size > 1) ? "s" : ""); + goto fail; + } + if (buf->suboffsets == NULL) buf->suboffsets = __Pyx_minusones; + return 0; +fail:; + __Pyx_SafeReleaseBuffer(buf); + return -1; +} + +/* GetItemInt */ + static PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) { + PyObject *r; + if (!j) return NULL; + r = PyObject_GetItem(o, j); + Py_DECREF(j); + return r; +} +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i, + CYTHON_NCP_UNUSED int wraparound, + CYTHON_NCP_UNUSED int boundscheck) { +#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS + Py_ssize_t wrapped_i = i; + if (wraparound & unlikely(i < 0)) { + wrapped_i += PyList_GET_SIZE(o); + } + if ((!boundscheck) || likely(__Pyx_is_valid_index(wrapped_i, PyList_GET_SIZE(o)))) { + PyObject *r = PyList_GET_ITEM(o, wrapped_i); + Py_INCREF(r); + return r; + } + return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); +#else + return PySequence_GetItem(o, i); +#endif +} +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i, + CYTHON_NCP_UNUSED int wraparound, + CYTHON_NCP_UNUSED int boundscheck) { +#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS + Py_ssize_t wrapped_i = i; + if (wraparound & unlikely(i < 0)) { + wrapped_i += PyTuple_GET_SIZE(o); + } + if ((!boundscheck) || likely(__Pyx_is_valid_index(wrapped_i, PyTuple_GET_SIZE(o)))) { + PyObject *r = PyTuple_GET_ITEM(o, wrapped_i); + Py_INCREF(r); + return r; + } + return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); +#else + return PySequence_GetItem(o, i); +#endif +} +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i, int is_list, + CYTHON_NCP_UNUSED int wraparound, + CYTHON_NCP_UNUSED int boundscheck) { +#if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS && CYTHON_USE_TYPE_SLOTS + if (is_list || PyList_CheckExact(o)) { + Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyList_GET_SIZE(o); + if ((!boundscheck) || (likely(__Pyx_is_valid_index(n, PyList_GET_SIZE(o))))) { + PyObject *r = PyList_GET_ITEM(o, n); + Py_INCREF(r); + return r; + } + } + else if (PyTuple_CheckExact(o)) { + Py_ssize_t n = ((!wraparound) | likely(i >= 0)) ? i : i + PyTuple_GET_SIZE(o); + if ((!boundscheck) || likely(__Pyx_is_valid_index(n, PyTuple_GET_SIZE(o)))) { + PyObject *r = PyTuple_GET_ITEM(o, n); + Py_INCREF(r); + return r; + } + } else { + PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence; + if (likely(m && m->sq_item)) { + if (wraparound && unlikely(i < 0) && likely(m->sq_length)) { + Py_ssize_t l = m->sq_length(o); + if (likely(l >= 0)) { + i += l; + } else { + if (!PyErr_ExceptionMatches(PyExc_OverflowError)) + return NULL; + PyErr_Clear(); + } + } + return m->sq_item(o, i); + } + } +#else + if (is_list || PySequence_Check(o)) { + return PySequence_GetItem(o, i); + } +#endif + return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); +} + +/* ObjectGetItem */ + #if CYTHON_USE_TYPE_SLOTS +static PyObject *__Pyx_PyObject_GetIndex(PyObject *obj, PyObject* index) { + PyObject *runerr; + Py_ssize_t key_value; + PySequenceMethods *m = Py_TYPE(obj)->tp_as_sequence; + if (unlikely(!(m && m->sq_item))) { + PyErr_Format(PyExc_TypeError, "'%.200s' object is not subscriptable", Py_TYPE(obj)->tp_name); + return NULL; + } + key_value = __Pyx_PyIndex_AsSsize_t(index); + if (likely(key_value != -1 || !(runerr = PyErr_Occurred()))) { + return __Pyx_GetItemInt_Fast(obj, key_value, 0, 1, 1); + } + if (PyErr_GivenExceptionMatches(runerr, PyExc_OverflowError)) { + PyErr_Clear(); + PyErr_Format(PyExc_IndexError, "cannot fit '%.200s' into an index-sized integer", Py_TYPE(index)->tp_name); + } + return NULL; +} +static PyObject *__Pyx_PyObject_GetItem(PyObject *obj, PyObject* key) { + PyMappingMethods *m = Py_TYPE(obj)->tp_as_mapping; + if (likely(m && m->mp_subscript)) { + return m->mp_subscript(obj, key); + } + return __Pyx_PyObject_GetIndex(obj, key); +} +#endif + +/* ExtTypeTest */ + static CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) { + if (unlikely(!type)) { + PyErr_SetString(PyExc_SystemError, "Missing type object"); + return 0; + } + if (likely(__Pyx_TypeCheck(obj, type))) + return 1; + PyErr_Format(PyExc_TypeError, "Cannot convert %.200s to %.200s", + Py_TYPE(obj)->tp_name, type->tp_name); + return 0; +} + +/* PyIntBinop */ + #if !CYTHON_COMPILING_IN_PYPY +static PyObject* __Pyx_PyInt_AddObjC(PyObject *op1, PyObject *op2, CYTHON_UNUSED long intval, int inplace, int zerodivision_check) { + (void)inplace; + (void)zerodivision_check; + #if PY_MAJOR_VERSION < 3 + if (likely(PyInt_CheckExact(op1))) { + const long b = intval; + long x; + long a = PyInt_AS_LONG(op1); + x = (long)((unsigned long)a + b); + if (likely((x^a) >= 0 || (x^b) >= 0)) + return PyInt_FromLong(x); + return PyLong_Type.tp_as_number->nb_add(op1, op2); + } + #endif + #if CYTHON_USE_PYLONG_INTERNALS + if (likely(PyLong_CheckExact(op1))) { + const long b = intval; + long a, x; +#ifdef HAVE_LONG_LONG + const PY_LONG_LONG llb = intval; + PY_LONG_LONG lla, llx; +#endif + const digit* digits = ((PyLongObject*)op1)->ob_digit; + const Py_ssize_t size = Py_SIZE(op1); + if (likely(__Pyx_sst_abs(size) <= 1)) { + a = likely(size) ? digits[0] : 0; + if (size == -1) a = -a; + } else { + switch (size) { + case -2: + if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { + a = -(long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0])); + break; +#ifdef HAVE_LONG_LONG + } else if (8 * sizeof(PY_LONG_LONG) - 1 > 2 * PyLong_SHIFT) { + lla = -(PY_LONG_LONG) (((((unsigned PY_LONG_LONG)digits[1]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[0])); + goto long_long; +#endif + } + CYTHON_FALLTHROUGH; + case 2: + if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { + a = (long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0])); + break; +#ifdef HAVE_LONG_LONG + } else if (8 * sizeof(PY_LONG_LONG) - 1 > 2 * PyLong_SHIFT) { + lla = (PY_LONG_LONG) (((((unsigned PY_LONG_LONG)digits[1]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[0])); + goto long_long; +#endif + } + CYTHON_FALLTHROUGH; + case -3: + if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { + a = -(long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0])); + break; +#ifdef HAVE_LONG_LONG + } else if (8 * sizeof(PY_LONG_LONG) - 1 > 3 * PyLong_SHIFT) { + lla = -(PY_LONG_LONG) (((((((unsigned PY_LONG_LONG)digits[2]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[1]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[0])); + goto long_long; +#endif + } + CYTHON_FALLTHROUGH; + case 3: + if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { + a = (long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0])); + break; +#ifdef HAVE_LONG_LONG + } else if (8 * sizeof(PY_LONG_LONG) - 1 > 3 * PyLong_SHIFT) { + lla = (PY_LONG_LONG) (((((((unsigned PY_LONG_LONG)digits[2]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[1]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[0])); + goto long_long; +#endif + } + CYTHON_FALLTHROUGH; + case -4: + if (8 * sizeof(long) - 1 > 4 * PyLong_SHIFT) { + a = -(long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0])); + break; +#ifdef HAVE_LONG_LONG + } else if (8 * sizeof(PY_LONG_LONG) - 1 > 4 * PyLong_SHIFT) { + lla = -(PY_LONG_LONG) (((((((((unsigned PY_LONG_LONG)digits[3]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[2]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[1]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[0])); + goto long_long; +#endif + } + CYTHON_FALLTHROUGH; + case 4: + if (8 * sizeof(long) - 1 > 4 * PyLong_SHIFT) { + a = (long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0])); + break; +#ifdef HAVE_LONG_LONG + } else if (8 * sizeof(PY_LONG_LONG) - 1 > 4 * PyLong_SHIFT) { + lla = (PY_LONG_LONG) (((((((((unsigned PY_LONG_LONG)digits[3]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[2]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[1]) << PyLong_SHIFT) | (unsigned PY_LONG_LONG)digits[0])); + goto long_long; +#endif + } + CYTHON_FALLTHROUGH; + default: return PyLong_Type.tp_as_number->nb_add(op1, op2); + } + } + x = a + b; + return PyLong_FromLong(x); +#ifdef HAVE_LONG_LONG + long_long: + llx = lla + llb; + return PyLong_FromLongLong(llx); +#endif + + + } + #endif + if (PyFloat_CheckExact(op1)) { + const long b = intval; + double a = PyFloat_AS_DOUBLE(op1); + double result; + PyFPE_START_PROTECT("add", return NULL) + result = ((double)a) + (double)b; + PyFPE_END_PROTECT(result) + return PyFloat_FromDouble(result); + } + return (inplace ? PyNumber_InPlaceAdd : PyNumber_Add)(op1, op2); +} +#endif + +/* PyFunctionFastCall */ + #if CYTHON_FAST_PYCALL +static PyObject* __Pyx_PyFunction_FastCallNoKw(PyCodeObject *co, PyObject **args, Py_ssize_t na, + PyObject *globals) { + PyFrameObject *f; + PyThreadState *tstate = __Pyx_PyThreadState_Current; + PyObject **fastlocals; + Py_ssize_t i; + PyObject *result; + assert(globals != NULL); + /* XXX Perhaps we should create a specialized + PyFrame_New() that doesn't take locals, but does + take builtins without sanity checking them. + */ + assert(tstate != NULL); + f = PyFrame_New(tstate, co, globals, NULL); + if (f == NULL) { + return NULL; + } + fastlocals = __Pyx_PyFrame_GetLocalsplus(f); + for (i = 0; i < na; i++) { + Py_INCREF(*args); + fastlocals[i] = *args++; + } + result = PyEval_EvalFrameEx(f,0); + ++tstate->recursion_depth; + Py_DECREF(f); + --tstate->recursion_depth; + return result; +} +#if 1 || PY_VERSION_HEX < 0x030600B1 +static PyObject *__Pyx_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs, PyObject *kwargs) { + PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func); + PyObject *globals = PyFunction_GET_GLOBALS(func); + PyObject *argdefs = PyFunction_GET_DEFAULTS(func); + PyObject *closure; +#if PY_MAJOR_VERSION >= 3 + PyObject *kwdefs; +#endif + PyObject *kwtuple, **k; + PyObject **d; + Py_ssize_t nd; + Py_ssize_t nk; + PyObject *result; + assert(kwargs == NULL || PyDict_Check(kwargs)); + nk = kwargs ? PyDict_Size(kwargs) : 0; + if (Py_EnterRecursiveCall((char*)" while calling a Python object")) { + return NULL; + } + if ( +#if PY_MAJOR_VERSION >= 3 + co->co_kwonlyargcount == 0 && +#endif + likely(kwargs == NULL || nk == 0) && + co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) { + if (argdefs == NULL && co->co_argcount == nargs) { + result = __Pyx_PyFunction_FastCallNoKw(co, args, nargs, globals); + goto done; + } + else if (nargs == 0 && argdefs != NULL + && co->co_argcount == Py_SIZE(argdefs)) { + /* function called with no arguments, but all parameters have + a default value: use default values as arguments .*/ + args = &PyTuple_GET_ITEM(argdefs, 0); + result =__Pyx_PyFunction_FastCallNoKw(co, args, Py_SIZE(argdefs), globals); + goto done; + } + } + if (kwargs != NULL) { + Py_ssize_t pos, i; + kwtuple = PyTuple_New(2 * nk); + if (kwtuple == NULL) { + result = NULL; + goto done; + } + k = &PyTuple_GET_ITEM(kwtuple, 0); + pos = i = 0; + while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) { + Py_INCREF(k[i]); + Py_INCREF(k[i+1]); + i += 2; + } + nk = i / 2; + } + else { + kwtuple = NULL; + k = NULL; + } + closure = PyFunction_GET_CLOSURE(func); +#if PY_MAJOR_VERSION >= 3 + kwdefs = PyFunction_GET_KW_DEFAULTS(func); +#endif + if (argdefs != NULL) { + d = &PyTuple_GET_ITEM(argdefs, 0); + nd = Py_SIZE(argdefs); + } + else { + d = NULL; + nd = 0; + } +#if PY_MAJOR_VERSION >= 3 + result = PyEval_EvalCodeEx((PyObject*)co, globals, (PyObject *)NULL, + args, (int)nargs, + k, (int)nk, + d, (int)nd, kwdefs, closure); +#else + result = PyEval_EvalCodeEx(co, globals, (PyObject *)NULL, + args, (int)nargs, + k, (int)nk, + d, (int)nd, closure); +#endif + Py_XDECREF(kwtuple); +done: + Py_LeaveRecursiveCall(); + return result; +} +#endif +#endif + +/* PyObjectCall */ + #if CYTHON_COMPILING_IN_CPYTHON +static CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) { + PyObject *result; + ternaryfunc call = Py_TYPE(func)->tp_call; + if (unlikely(!call)) + return PyObject_Call(func, arg, kw); + if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object"))) + return NULL; + result = (*call)(func, arg, kw); + Py_LeaveRecursiveCall(); + if (unlikely(!result) && unlikely(!PyErr_Occurred())) { + PyErr_SetString( + PyExc_SystemError, + "NULL result without error in PyObject_Call"); + } + return result; +} +#endif + +/* PyObjectCallMethO */ + #if CYTHON_COMPILING_IN_CPYTHON +static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg) { + PyObject *self, *result; + PyCFunction cfunc; + cfunc = PyCFunction_GET_FUNCTION(func); + self = PyCFunction_GET_SELF(func); + if (unlikely(Py_EnterRecursiveCall((char*)" while calling a Python object"))) + return NULL; + result = cfunc(self, arg); + Py_LeaveRecursiveCall(); + if (unlikely(!result) && unlikely(!PyErr_Occurred())) { + PyErr_SetString( + PyExc_SystemError, + "NULL result without error in PyObject_Call"); + } + return result; +} +#endif + +/* PyObjectCallNoArg */ + #if CYTHON_COMPILING_IN_CPYTHON +static CYTHON_INLINE PyObject* __Pyx_PyObject_CallNoArg(PyObject *func) { +#if CYTHON_FAST_PYCALL + if (PyFunction_Check(func)) { + return __Pyx_PyFunction_FastCall(func, NULL, 0); + } +#endif +#ifdef __Pyx_CyFunction_USED + if (likely(PyCFunction_Check(func) || __Pyx_CyFunction_Check(func))) +#else + if (likely(PyCFunction_Check(func))) +#endif + { + if (likely(PyCFunction_GET_FLAGS(func) & METH_NOARGS)) { + return __Pyx_PyObject_CallMethO(func, NULL); + } + } + return __Pyx_PyObject_Call(func, __pyx_empty_tuple, NULL); +} +#endif + +/* PyCFunctionFastCall */ + #if CYTHON_FAST_PYCCALL +static CYTHON_INLINE PyObject * __Pyx_PyCFunction_FastCall(PyObject *func_obj, PyObject **args, Py_ssize_t nargs) { + PyCFunctionObject *func = (PyCFunctionObject*)func_obj; + PyCFunction meth = PyCFunction_GET_FUNCTION(func); + PyObject *self = PyCFunction_GET_SELF(func); + int flags = PyCFunction_GET_FLAGS(func); + assert(PyCFunction_Check(func)); + assert(METH_FASTCALL == (flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_KEYWORDS | METH_STACKLESS))); + assert(nargs >= 0); + assert(nargs == 0 || args != NULL); + /* _PyCFunction_FastCallDict() must not be called with an exception set, + because it may clear it (directly or indirectly) and so the + caller loses its exception */ + assert(!PyErr_Occurred()); + if ((PY_VERSION_HEX < 0x030700A0) || unlikely(flags & METH_KEYWORDS)) { + return (*((__Pyx_PyCFunctionFastWithKeywords)(void*)meth)) (self, args, nargs, NULL); + } else { + return (*((__Pyx_PyCFunctionFast)(void*)meth)) (self, args, nargs); + } +} +#endif + +/* PyObjectCallOneArg */ + #if CYTHON_COMPILING_IN_CPYTHON +static PyObject* __Pyx__PyObject_CallOneArg(PyObject *func, PyObject *arg) { + PyObject *result; + PyObject *args = PyTuple_New(1); + if (unlikely(!args)) return NULL; + Py_INCREF(arg); + PyTuple_SET_ITEM(args, 0, arg); + result = __Pyx_PyObject_Call(func, args, NULL); + Py_DECREF(args); + return result; +} +static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg) { +#if CYTHON_FAST_PYCALL + if (PyFunction_Check(func)) { + return __Pyx_PyFunction_FastCall(func, &arg, 1); + } +#endif + if (likely(PyCFunction_Check(func))) { + if (likely(PyCFunction_GET_FLAGS(func) & METH_O)) { + return __Pyx_PyObject_CallMethO(func, arg); +#if CYTHON_FAST_PYCCALL + } else if (__Pyx_PyFastCFunction_Check(func)) { + return __Pyx_PyCFunction_FastCall(func, &arg, 1); +#endif + } + } + return __Pyx__PyObject_CallOneArg(func, arg); +} +#else +static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg) { + PyObject *result; + PyObject *args = PyTuple_Pack(1, arg); + if (unlikely(!args)) return NULL; + result = __Pyx_PyObject_Call(func, args, NULL); + Py_DECREF(args); + return result; +} +#endif + +/* PyDictVersioning */ + #if CYTHON_USE_DICT_VERSIONS && CYTHON_USE_TYPE_SLOTS +static CYTHON_INLINE PY_UINT64_T __Pyx_get_tp_dict_version(PyObject *obj) { + PyObject *dict = Py_TYPE(obj)->tp_dict; + return likely(dict) ? __PYX_GET_DICT_VERSION(dict) : 0; +} +static CYTHON_INLINE PY_UINT64_T __Pyx_get_object_dict_version(PyObject *obj) { + PyObject **dictptr = NULL; + Py_ssize_t offset = Py_TYPE(obj)->tp_dictoffset; + if (offset) { +#if CYTHON_COMPILING_IN_CPYTHON + dictptr = (likely(offset > 0)) ? (PyObject **) ((char *)obj + offset) : _PyObject_GetDictPtr(obj); +#else + dictptr = _PyObject_GetDictPtr(obj); +#endif + } + return (dictptr && *dictptr) ? __PYX_GET_DICT_VERSION(*dictptr) : 0; +} +static CYTHON_INLINE int __Pyx_object_dict_version_matches(PyObject* obj, PY_UINT64_T tp_dict_version, PY_UINT64_T obj_dict_version) { + PyObject *dict = Py_TYPE(obj)->tp_dict; + if (unlikely(!dict) || unlikely(tp_dict_version != __PYX_GET_DICT_VERSION(dict))) + return 0; + return obj_dict_version == __Pyx_get_object_dict_version(obj); +} +#endif + +/* GetModuleGlobalName */ + #if CYTHON_USE_DICT_VERSIONS +static PyObject *__Pyx__GetModuleGlobalName(PyObject *name, PY_UINT64_T *dict_version, PyObject **dict_cached_value) +#else +static CYTHON_INLINE PyObject *__Pyx__GetModuleGlobalName(PyObject *name) +#endif +{ + PyObject *result; +#if !CYTHON_AVOID_BORROWED_REFS +#if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030500A1 + result = _PyDict_GetItem_KnownHash(__pyx_d, name, ((PyASCIIObject *) name)->hash); + __PYX_UPDATE_DICT_CACHE(__pyx_d, result, *dict_cached_value, *dict_version) + if (likely(result)) { + return __Pyx_NewRef(result); + } else if (unlikely(PyErr_Occurred())) { + return NULL; + } +#else + result = PyDict_GetItem(__pyx_d, name); + __PYX_UPDATE_DICT_CACHE(__pyx_d, result, *dict_cached_value, *dict_version) + if (likely(result)) { + return __Pyx_NewRef(result); + } +#endif +#else + result = PyObject_GetItem(__pyx_d, name); + __PYX_UPDATE_DICT_CACHE(__pyx_d, result, *dict_cached_value, *dict_version) + if (likely(result)) { + return __Pyx_NewRef(result); + } + PyErr_Clear(); +#endif + return __Pyx_GetBuiltinName(name); +} + +/* BufferIndexError */ + static void __Pyx_RaiseBufferIndexError(int axis) { + PyErr_Format(PyExc_IndexError, + "Out of bounds on buffer access (axis %d)", axis); +} + +/* PyErrFetchRestore */ + #if CYTHON_FAST_THREAD_STATE +static CYTHON_INLINE void __Pyx_ErrRestoreInState(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb) { + PyObject *tmp_type, *tmp_value, *tmp_tb; + tmp_type = tstate->curexc_type; + tmp_value = tstate->curexc_value; + tmp_tb = tstate->curexc_traceback; + tstate->curexc_type = type; + tstate->curexc_value = value; + tstate->curexc_traceback = tb; + Py_XDECREF(tmp_type); + Py_XDECREF(tmp_value); + Py_XDECREF(tmp_tb); +} +static CYTHON_INLINE void __Pyx_ErrFetchInState(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb) { + *type = tstate->curexc_type; + *value = tstate->curexc_value; + *tb = tstate->curexc_traceback; + tstate->curexc_type = 0; + tstate->curexc_value = 0; + tstate->curexc_traceback = 0; +} +#endif + +/* PyObjectCall2Args */ + static CYTHON_UNUSED PyObject* __Pyx_PyObject_Call2Args(PyObject* function, PyObject* arg1, PyObject* arg2) { + PyObject *args, *result = NULL; + #if CYTHON_FAST_PYCALL + if (PyFunction_Check(function)) { + PyObject *args[2] = {arg1, arg2}; + return __Pyx_PyFunction_FastCall(function, args, 2); + } + #endif + #if CYTHON_FAST_PYCCALL + if (__Pyx_PyFastCFunction_Check(function)) { + PyObject *args[2] = {arg1, arg2}; + return __Pyx_PyCFunction_FastCall(function, args, 2); + } + #endif + args = PyTuple_New(2); + if (unlikely(!args)) goto done; + Py_INCREF(arg1); + PyTuple_SET_ITEM(args, 0, arg1); + Py_INCREF(arg2); + PyTuple_SET_ITEM(args, 1, arg2); + Py_INCREF(function); + result = __Pyx_PyObject_Call(function, args, NULL); + Py_DECREF(args); + Py_DECREF(function); +done: + return result; +} + +/* GetTopmostException */ + #if CYTHON_USE_EXC_INFO_STACK +static _PyErr_StackItem * +__Pyx_PyErr_GetTopmostException(PyThreadState *tstate) +{ + _PyErr_StackItem *exc_info = tstate->exc_info; + while ((exc_info->exc_type == NULL || exc_info->exc_type == Py_None) && + exc_info->previous_item != NULL) + { + exc_info = exc_info->previous_item; + } + return exc_info; +} +#endif + +/* SaveResetException */ + #if CYTHON_FAST_THREAD_STATE +static CYTHON_INLINE void __Pyx__ExceptionSave(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb) { + #if CYTHON_USE_EXC_INFO_STACK + _PyErr_StackItem *exc_info = __Pyx_PyErr_GetTopmostException(tstate); + *type = exc_info->exc_type; + *value = exc_info->exc_value; + *tb = exc_info->exc_traceback; + #else + *type = tstate->exc_type; + *value = tstate->exc_value; + *tb = tstate->exc_traceback; + #endif + Py_XINCREF(*type); + Py_XINCREF(*value); + Py_XINCREF(*tb); +} +static CYTHON_INLINE void __Pyx__ExceptionReset(PyThreadState *tstate, PyObject *type, PyObject *value, PyObject *tb) { + PyObject *tmp_type, *tmp_value, *tmp_tb; + #if CYTHON_USE_EXC_INFO_STACK + _PyErr_StackItem *exc_info = tstate->exc_info; + tmp_type = exc_info->exc_type; + tmp_value = exc_info->exc_value; + tmp_tb = exc_info->exc_traceback; + exc_info->exc_type = type; + exc_info->exc_value = value; + exc_info->exc_traceback = tb; + #else + tmp_type = tstate->exc_type; + tmp_value = tstate->exc_value; + tmp_tb = tstate->exc_traceback; + tstate->exc_type = type; + tstate->exc_value = value; + tstate->exc_traceback = tb; + #endif + Py_XDECREF(tmp_type); + Py_XDECREF(tmp_value); + Py_XDECREF(tmp_tb); +} +#endif + +/* PyErrExceptionMatches */ + #if CYTHON_FAST_THREAD_STATE +static int __Pyx_PyErr_ExceptionMatchesTuple(PyObject *exc_type, PyObject *tuple) { + Py_ssize_t i, n; + n = PyTuple_GET_SIZE(tuple); +#if PY_MAJOR_VERSION >= 3 + for (i=0; icurexc_type; + if (exc_type == err) return 1; + if (unlikely(!exc_type)) return 0; + if (unlikely(PyTuple_Check(err))) + return __Pyx_PyErr_ExceptionMatchesTuple(exc_type, err); + return __Pyx_PyErr_GivenExceptionMatches(exc_type, err); +} +#endif + +/* GetException */ + #if CYTHON_FAST_THREAD_STATE +static int __Pyx__GetException(PyThreadState *tstate, PyObject **type, PyObject **value, PyObject **tb) +#else +static int __Pyx_GetException(PyObject **type, PyObject **value, PyObject **tb) +#endif +{ + PyObject *local_type, *local_value, *local_tb; +#if CYTHON_FAST_THREAD_STATE + PyObject *tmp_type, *tmp_value, *tmp_tb; + local_type = tstate->curexc_type; + local_value = tstate->curexc_value; + local_tb = tstate->curexc_traceback; + tstate->curexc_type = 0; + tstate->curexc_value = 0; + tstate->curexc_traceback = 0; +#else + PyErr_Fetch(&local_type, &local_value, &local_tb); +#endif + PyErr_NormalizeException(&local_type, &local_value, &local_tb); +#if CYTHON_FAST_THREAD_STATE + if (unlikely(tstate->curexc_type)) +#else + if (unlikely(PyErr_Occurred())) +#endif + goto bad; + #if PY_MAJOR_VERSION >= 3 + if (local_tb) { + if (unlikely(PyException_SetTraceback(local_value, local_tb) < 0)) + goto bad; + } + #endif + Py_XINCREF(local_tb); + Py_XINCREF(local_type); + Py_XINCREF(local_value); + *type = local_type; + *value = local_value; + *tb = local_tb; +#if CYTHON_FAST_THREAD_STATE + #if CYTHON_USE_EXC_INFO_STACK + { + _PyErr_StackItem *exc_info = tstate->exc_info; + tmp_type = exc_info->exc_type; + tmp_value = exc_info->exc_value; + tmp_tb = exc_info->exc_traceback; + exc_info->exc_type = local_type; + exc_info->exc_value = local_value; + exc_info->exc_traceback = local_tb; + } + #else + tmp_type = tstate->exc_type; + tmp_value = tstate->exc_value; + tmp_tb = tstate->exc_traceback; + tstate->exc_type = local_type; + tstate->exc_value = local_value; + tstate->exc_traceback = local_tb; + #endif + Py_XDECREF(tmp_type); + Py_XDECREF(tmp_value); + Py_XDECREF(tmp_tb); +#else + PyErr_SetExcInfo(local_type, local_value, local_tb); +#endif + return 0; +bad: + *type = 0; + *value = 0; + *tb = 0; + Py_XDECREF(local_type); + Py_XDECREF(local_value); + Py_XDECREF(local_tb); + return -1; +} + +/* RaiseException */ + #if PY_MAJOR_VERSION < 3 +static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, + CYTHON_UNUSED PyObject *cause) { + __Pyx_PyThreadState_declare + Py_XINCREF(type); + if (!value || value == Py_None) + value = NULL; + else + Py_INCREF(value); + if (!tb || tb == Py_None) + tb = NULL; + else { + Py_INCREF(tb); + if (!PyTraceBack_Check(tb)) { + PyErr_SetString(PyExc_TypeError, + "raise: arg 3 must be a traceback or None"); + goto raise_error; + } + } + if (PyType_Check(type)) { +#if CYTHON_COMPILING_IN_PYPY + if (!value) { + Py_INCREF(Py_None); + value = Py_None; + } +#endif + PyErr_NormalizeException(&type, &value, &tb); + } else { + if (value) { + PyErr_SetString(PyExc_TypeError, + "instance exception may not have a separate value"); + goto raise_error; + } + value = type; + type = (PyObject*) Py_TYPE(type); + Py_INCREF(type); + if (!PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject *)PyExc_BaseException)) { + PyErr_SetString(PyExc_TypeError, + "raise: exception class must be a subclass of BaseException"); + goto raise_error; + } + } + __Pyx_PyThreadState_assign + __Pyx_ErrRestore(type, value, tb); + return; +raise_error: + Py_XDECREF(value); + Py_XDECREF(type); + Py_XDECREF(tb); + return; +} +#else +static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause) { + PyObject* owned_instance = NULL; + if (tb == Py_None) { + tb = 0; + } else if (tb && !PyTraceBack_Check(tb)) { + PyErr_SetString(PyExc_TypeError, + "raise: arg 3 must be a traceback or None"); + goto bad; + } + if (value == Py_None) + value = 0; + if (PyExceptionInstance_Check(type)) { + if (value) { + PyErr_SetString(PyExc_TypeError, + "instance exception may not have a separate value"); + goto bad; + } + value = type; + type = (PyObject*) Py_TYPE(value); + } else if (PyExceptionClass_Check(type)) { + PyObject *instance_class = NULL; + if (value && PyExceptionInstance_Check(value)) { + instance_class = (PyObject*) Py_TYPE(value); + if (instance_class != type) { + int is_subclass = PyObject_IsSubclass(instance_class, type); + if (!is_subclass) { + instance_class = NULL; + } else if (unlikely(is_subclass == -1)) { + goto bad; + } else { + type = instance_class; + } + } + } + if (!instance_class) { + PyObject *args; + if (!value) + args = PyTuple_New(0); + else if (PyTuple_Check(value)) { + Py_INCREF(value); + args = value; + } else + args = PyTuple_Pack(1, value); + if (!args) + goto bad; + owned_instance = PyObject_Call(type, args, NULL); + Py_DECREF(args); + if (!owned_instance) + goto bad; + value = owned_instance; + if (!PyExceptionInstance_Check(value)) { + PyErr_Format(PyExc_TypeError, + "calling %R should have returned an instance of " + "BaseException, not %R", + type, Py_TYPE(value)); + goto bad; + } + } + } else { + PyErr_SetString(PyExc_TypeError, + "raise: exception class must be a subclass of BaseException"); + goto bad; + } + if (cause) { + PyObject *fixed_cause; + if (cause == Py_None) { + fixed_cause = NULL; + } else if (PyExceptionClass_Check(cause)) { + fixed_cause = PyObject_CallObject(cause, NULL); + if (fixed_cause == NULL) + goto bad; + } else if (PyExceptionInstance_Check(cause)) { + fixed_cause = cause; + Py_INCREF(fixed_cause); + } else { + PyErr_SetString(PyExc_TypeError, + "exception causes must derive from " + "BaseException"); + goto bad; + } + PyException_SetCause(value, fixed_cause); + } + PyErr_SetObject(type, value); + if (tb) { +#if CYTHON_COMPILING_IN_PYPY + PyObject *tmp_type, *tmp_value, *tmp_tb; + PyErr_Fetch(&tmp_type, &tmp_value, &tmp_tb); + Py_INCREF(tb); + PyErr_Restore(tmp_type, tmp_value, tb); + Py_XDECREF(tmp_tb); +#else + PyThreadState *tstate = __Pyx_PyThreadState_Current; + PyObject* tmp_tb = tstate->curexc_traceback; + if (tb != tmp_tb) { + Py_INCREF(tb); + tstate->curexc_traceback = tb; + Py_XDECREF(tmp_tb); + } +#endif + } +bad: + Py_XDECREF(owned_instance); + return; +} +#endif + +/* TypeImport */ + #ifndef __PYX_HAVE_RT_ImportType +#define __PYX_HAVE_RT_ImportType +static PyTypeObject *__Pyx_ImportType(PyObject *module, const char *module_name, const char *class_name, + size_t size, enum __Pyx_ImportType_CheckSize check_size) +{ + PyObject *result = 0; + char warning[200]; + Py_ssize_t basicsize; +#ifdef Py_LIMITED_API + PyObject *py_basicsize; +#endif + result = PyObject_GetAttrString(module, class_name); + if (!result) + goto bad; + if (!PyType_Check(result)) { + PyErr_Format(PyExc_TypeError, + "%.200s.%.200s is not a type object", + module_name, class_name); + goto bad; + } +#ifndef Py_LIMITED_API + basicsize = ((PyTypeObject *)result)->tp_basicsize; +#else + py_basicsize = PyObject_GetAttrString(result, "__basicsize__"); + if (!py_basicsize) + goto bad; + basicsize = PyLong_AsSsize_t(py_basicsize); + Py_DECREF(py_basicsize); + py_basicsize = 0; + if (basicsize == (Py_ssize_t)-1 && PyErr_Occurred()) + goto bad; +#endif + if ((size_t)basicsize < size) { + PyErr_Format(PyExc_ValueError, + "%.200s.%.200s size changed, may indicate binary incompatibility. " + "Expected %zd from C header, got %zd from PyObject", + module_name, class_name, size, basicsize); + goto bad; + } + if (check_size == __Pyx_ImportType_CheckSize_Error && (size_t)basicsize != size) { + PyErr_Format(PyExc_ValueError, + "%.200s.%.200s size changed, may indicate binary incompatibility. " + "Expected %zd from C header, got %zd from PyObject", + module_name, class_name, size, basicsize); + goto bad; + } + else if (check_size == __Pyx_ImportType_CheckSize_Warn && (size_t)basicsize > size) { + PyOS_snprintf(warning, sizeof(warning), + "%s.%s size changed, may indicate binary incompatibility. " + "Expected %zd from C header, got %zd from PyObject", + module_name, class_name, size, basicsize); + if (PyErr_WarnEx(NULL, warning, 0) < 0) goto bad; + } + return (PyTypeObject *)result; +bad: + Py_XDECREF(result); + return NULL; +} +#endif + +/* Import */ + static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) { + PyObject *empty_list = 0; + PyObject *module = 0; + PyObject *global_dict = 0; + PyObject *empty_dict = 0; + PyObject *list; + #if PY_MAJOR_VERSION < 3 + PyObject *py_import; + py_import = __Pyx_PyObject_GetAttrStr(__pyx_b, __pyx_n_s_import); + if (!py_import) + goto bad; + #endif + if (from_list) + list = from_list; + else { + empty_list = PyList_New(0); + if (!empty_list) + goto bad; + list = empty_list; + } + global_dict = PyModule_GetDict(__pyx_m); + if (!global_dict) + goto bad; + empty_dict = PyDict_New(); + if (!empty_dict) + goto bad; + { + #if PY_MAJOR_VERSION >= 3 + if (level == -1) { + if ((1) && (strchr(__Pyx_MODULE_NAME, '.'))) { + module = PyImport_ImportModuleLevelObject( + name, global_dict, empty_dict, list, 1); + if (!module) { + if (!PyErr_ExceptionMatches(PyExc_ImportError)) + goto bad; + PyErr_Clear(); + } + } + level = 0; + } + #endif + if (!module) { + #if PY_MAJOR_VERSION < 3 + PyObject *py_level = PyInt_FromLong(level); + if (!py_level) + goto bad; + module = PyObject_CallFunctionObjArgs(py_import, + name, global_dict, empty_dict, list, py_level, (PyObject *)NULL); + Py_DECREF(py_level); + #else + module = PyImport_ImportModuleLevelObject( + name, global_dict, empty_dict, list, level); + #endif + } + } +bad: + #if PY_MAJOR_VERSION < 3 + Py_XDECREF(py_import); + #endif + Py_XDECREF(empty_list); + Py_XDECREF(empty_dict); + return module; +} + +/* CLineInTraceback */ + #ifndef CYTHON_CLINE_IN_TRACEBACK +static int __Pyx_CLineForTraceback(CYTHON_NCP_UNUSED PyThreadState *tstate, int c_line) { + PyObject *use_cline; + PyObject *ptype, *pvalue, *ptraceback; +#if CYTHON_COMPILING_IN_CPYTHON + PyObject **cython_runtime_dict; +#endif + if (unlikely(!__pyx_cython_runtime)) { + return c_line; + } + __Pyx_ErrFetchInState(tstate, &ptype, &pvalue, &ptraceback); +#if CYTHON_COMPILING_IN_CPYTHON + cython_runtime_dict = _PyObject_GetDictPtr(__pyx_cython_runtime); + if (likely(cython_runtime_dict)) { + __PYX_PY_DICT_LOOKUP_IF_MODIFIED( + use_cline, *cython_runtime_dict, + __Pyx_PyDict_GetItemStr(*cython_runtime_dict, __pyx_n_s_cline_in_traceback)) + } else +#endif + { + PyObject *use_cline_obj = __Pyx_PyObject_GetAttrStr(__pyx_cython_runtime, __pyx_n_s_cline_in_traceback); + if (use_cline_obj) { + use_cline = PyObject_Not(use_cline_obj) ? Py_False : Py_True; + Py_DECREF(use_cline_obj); + } else { + PyErr_Clear(); + use_cline = NULL; + } + } + if (!use_cline) { + c_line = 0; + PyObject_SetAttr(__pyx_cython_runtime, __pyx_n_s_cline_in_traceback, Py_False); + } + else if (use_cline == Py_False || (use_cline != Py_True && PyObject_Not(use_cline) != 0)) { + c_line = 0; + } + __Pyx_ErrRestoreInState(tstate, ptype, pvalue, ptraceback); + return c_line; +} +#endif + +/* CodeObjectCache */ + static int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line) { + int start = 0, mid = 0, end = count - 1; + if (end >= 0 && code_line > entries[end].code_line) { + return count; + } + while (start < end) { + mid = start + (end - start) / 2; + if (code_line < entries[mid].code_line) { + end = mid; + } else if (code_line > entries[mid].code_line) { + start = mid + 1; + } else { + return mid; + } + } + if (code_line <= entries[mid].code_line) { + return mid; + } else { + return mid + 1; + } +} +static PyCodeObject *__pyx_find_code_object(int code_line) { + PyCodeObject* code_object; + int pos; + if (unlikely(!code_line) || unlikely(!__pyx_code_cache.entries)) { + return NULL; + } + pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line); + if (unlikely(pos >= __pyx_code_cache.count) || unlikely(__pyx_code_cache.entries[pos].code_line != code_line)) { + return NULL; + } + code_object = __pyx_code_cache.entries[pos].code_object; + Py_INCREF(code_object); + return code_object; +} +static void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) { + int pos, i; + __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries; + if (unlikely(!code_line)) { + return; + } + if (unlikely(!entries)) { + entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Malloc(64*sizeof(__Pyx_CodeObjectCacheEntry)); + if (likely(entries)) { + __pyx_code_cache.entries = entries; + __pyx_code_cache.max_count = 64; + __pyx_code_cache.count = 1; + entries[0].code_line = code_line; + entries[0].code_object = code_object; + Py_INCREF(code_object); + } + return; + } + pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line); + if ((pos < __pyx_code_cache.count) && unlikely(__pyx_code_cache.entries[pos].code_line == code_line)) { + PyCodeObject* tmp = entries[pos].code_object; + entries[pos].code_object = code_object; + Py_DECREF(tmp); + return; + } + if (__pyx_code_cache.count == __pyx_code_cache.max_count) { + int new_max = __pyx_code_cache.max_count + 64; + entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Realloc( + __pyx_code_cache.entries, ((size_t)new_max) * sizeof(__Pyx_CodeObjectCacheEntry)); + if (unlikely(!entries)) { + return; + } + __pyx_code_cache.entries = entries; + __pyx_code_cache.max_count = new_max; + } + for (i=__pyx_code_cache.count; i>pos; i--) { + entries[i] = entries[i-1]; + } + entries[pos].code_line = code_line; + entries[pos].code_object = code_object; + __pyx_code_cache.count++; + Py_INCREF(code_object); +} + +/* AddTraceback */ + #include "compile.h" +#include "frameobject.h" +#include "traceback.h" +static PyCodeObject* __Pyx_CreateCodeObjectForTraceback( + const char *funcname, int c_line, + int py_line, const char *filename) { + PyCodeObject *py_code = 0; + PyObject *py_srcfile = 0; + PyObject *py_funcname = 0; + #if PY_MAJOR_VERSION < 3 + py_srcfile = PyString_FromString(filename); + #else + py_srcfile = PyUnicode_FromString(filename); + #endif + if (!py_srcfile) goto bad; + if (c_line) { + #if PY_MAJOR_VERSION < 3 + py_funcname = PyString_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line); + #else + py_funcname = PyUnicode_FromFormat( "%s (%s:%d)", funcname, __pyx_cfilenm, c_line); + #endif + } + else { + #if PY_MAJOR_VERSION < 3 + py_funcname = PyString_FromString(funcname); + #else + py_funcname = PyUnicode_FromString(funcname); + #endif + } + if (!py_funcname) goto bad; + py_code = __Pyx_PyCode_New( + 0, + 0, + 0, + 0, + 0, + __pyx_empty_bytes, /*PyObject *code,*/ + __pyx_empty_tuple, /*PyObject *consts,*/ + __pyx_empty_tuple, /*PyObject *names,*/ + __pyx_empty_tuple, /*PyObject *varnames,*/ + __pyx_empty_tuple, /*PyObject *freevars,*/ + __pyx_empty_tuple, /*PyObject *cellvars,*/ + py_srcfile, /*PyObject *filename,*/ + py_funcname, /*PyObject *name,*/ + py_line, + __pyx_empty_bytes /*PyObject *lnotab*/ + ); + Py_DECREF(py_srcfile); + Py_DECREF(py_funcname); + return py_code; +bad: + Py_XDECREF(py_srcfile); + Py_XDECREF(py_funcname); + return NULL; +} +static void __Pyx_AddTraceback(const char *funcname, int c_line, + int py_line, const char *filename) { + PyCodeObject *py_code = 0; + PyFrameObject *py_frame = 0; + PyThreadState *tstate = __Pyx_PyThreadState_Current; + if (c_line) { + c_line = __Pyx_CLineForTraceback(tstate, c_line); + } + py_code = __pyx_find_code_object(c_line ? -c_line : py_line); + if (!py_code) { + py_code = __Pyx_CreateCodeObjectForTraceback( + funcname, c_line, py_line, filename); + if (!py_code) goto bad; + __pyx_insert_code_object(c_line ? -c_line : py_line, py_code); + } + py_frame = PyFrame_New( + tstate, /*PyThreadState *tstate,*/ + py_code, /*PyCodeObject *code,*/ + __pyx_d, /*PyObject *globals,*/ + 0 /*PyObject *locals*/ + ); + if (!py_frame) goto bad; + __Pyx_PyFrame_SetLineNumber(py_frame, py_line); + PyTraceBack_Here(py_frame); +bad: + Py_XDECREF(py_code); + Py_XDECREF(py_frame); +} + +#if PY_MAJOR_VERSION < 3 +static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags) { + if (PyObject_CheckBuffer(obj)) return PyObject_GetBuffer(obj, view, flags); + PyErr_Format(PyExc_TypeError, "'%.200s' does not have the buffer interface", Py_TYPE(obj)->tp_name); + return -1; +} +static void __Pyx_ReleaseBuffer(Py_buffer *view) { + PyObject *obj = view->obj; + if (!obj) return; + if (PyObject_CheckBuffer(obj)) { + PyBuffer_Release(view); + return; + } + if ((0)) {} + view->obj = NULL; + Py_DECREF(obj); +} +#endif + + + /* CIntFromPyVerify */ + #define __PYX_VERIFY_RETURN_INT(target_type, func_type, func_value)\ + __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 0) +#define __PYX_VERIFY_RETURN_INT_EXC(target_type, func_type, func_value)\ + __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, 1) +#define __PYX__VERIFY_RETURN_INT(target_type, func_type, func_value, exc)\ + {\ + func_type value = func_value;\ + if (sizeof(target_type) < sizeof(func_type)) {\ + if (unlikely(value != (func_type) (target_type) value)) {\ + func_type zero = 0;\ + if (exc && unlikely(value == (func_type)-1 && PyErr_Occurred()))\ + return (target_type) -1;\ + if (is_unsigned && unlikely(value < zero))\ + goto raise_neg_overflow;\ + else\ + goto raise_overflow;\ + }\ + }\ + return (target_type) value;\ + } + +/* Declarations */ + #if CYTHON_CCOMPLEX + #ifdef __cplusplus + static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) { + return ::std::complex< float >(x, y); + } + #else + static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) { + return x + y*(__pyx_t_float_complex)_Complex_I; + } + #endif +#else + static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) { + __pyx_t_float_complex z; + z.real = x; + z.imag = y; + return z; + } +#endif + +/* Arithmetic */ + #if CYTHON_CCOMPLEX +#else + static CYTHON_INLINE int __Pyx_c_eq_float(__pyx_t_float_complex a, __pyx_t_float_complex b) { + return (a.real == b.real) && (a.imag == b.imag); + } + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_sum_float(__pyx_t_float_complex a, __pyx_t_float_complex b) { + __pyx_t_float_complex z; + z.real = a.real + b.real; + z.imag = a.imag + b.imag; + return z; + } + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_diff_float(__pyx_t_float_complex a, __pyx_t_float_complex b) { + __pyx_t_float_complex z; + z.real = a.real - b.real; + z.imag = a.imag - b.imag; + return z; + } + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_prod_float(__pyx_t_float_complex a, __pyx_t_float_complex b) { + __pyx_t_float_complex z; + z.real = a.real * b.real - a.imag * b.imag; + z.imag = a.real * b.imag + a.imag * b.real; + return z; + } + #if 1 + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_quot_float(__pyx_t_float_complex a, __pyx_t_float_complex b) { + if (b.imag == 0) { + return __pyx_t_float_complex_from_parts(a.real / b.real, a.imag / b.real); + } else if (fabsf(b.real) >= fabsf(b.imag)) { + if (b.real == 0 && b.imag == 0) { + return __pyx_t_float_complex_from_parts(a.real / b.real, a.imag / b.imag); + } else { + float r = b.imag / b.real; + float s = (float)(1.0) / (b.real + b.imag * r); + return __pyx_t_float_complex_from_parts( + (a.real + a.imag * r) * s, (a.imag - a.real * r) * s); + } + } else { + float r = b.real / b.imag; + float s = (float)(1.0) / (b.imag + b.real * r); + return __pyx_t_float_complex_from_parts( + (a.real * r + a.imag) * s, (a.imag * r - a.real) * s); + } + } + #else + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_quot_float(__pyx_t_float_complex a, __pyx_t_float_complex b) { + if (b.imag == 0) { + return __pyx_t_float_complex_from_parts(a.real / b.real, a.imag / b.real); + } else { + float denom = b.real * b.real + b.imag * b.imag; + return __pyx_t_float_complex_from_parts( + (a.real * b.real + a.imag * b.imag) / denom, + (a.imag * b.real - a.real * b.imag) / denom); + } + } + #endif + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_neg_float(__pyx_t_float_complex a) { + __pyx_t_float_complex z; + z.real = -a.real; + z.imag = -a.imag; + return z; + } + static CYTHON_INLINE int __Pyx_c_is_zero_float(__pyx_t_float_complex a) { + return (a.real == 0) && (a.imag == 0); + } + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_conj_float(__pyx_t_float_complex a) { + __pyx_t_float_complex z; + z.real = a.real; + z.imag = -a.imag; + return z; + } + #if 1 + static CYTHON_INLINE float __Pyx_c_abs_float(__pyx_t_float_complex z) { + #if !defined(HAVE_HYPOT) || defined(_MSC_VER) + return sqrtf(z.real*z.real + z.imag*z.imag); + #else + return hypotf(z.real, z.imag); + #endif + } + static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_pow_float(__pyx_t_float_complex a, __pyx_t_float_complex b) { + __pyx_t_float_complex z; + float r, lnr, theta, z_r, z_theta; + if (b.imag == 0 && b.real == (int)b.real) { + if (b.real < 0) { + float denom = a.real * a.real + a.imag * a.imag; + a.real = a.real / denom; + a.imag = -a.imag / denom; + b.real = -b.real; + } + switch ((int)b.real) { + case 0: + z.real = 1; + z.imag = 0; + return z; + case 1: + return a; + case 2: + return __Pyx_c_prod_float(a, a); + case 3: + z = __Pyx_c_prod_float(a, a); + return __Pyx_c_prod_float(z, a); + case 4: + z = __Pyx_c_prod_float(a, a); + return __Pyx_c_prod_float(z, z); + } + } + if (a.imag == 0) { + if (a.real == 0) { + return a; + } else if (b.imag == 0) { + z.real = powf(a.real, b.real); + z.imag = 0; + return z; + } else if (a.real > 0) { + r = a.real; + theta = 0; + } else { + r = -a.real; + theta = atan2f(0.0, -1.0); + } + } else { + r = __Pyx_c_abs_float(a); + theta = atan2f(a.imag, a.real); + } + lnr = logf(r); + z_r = expf(lnr * b.real - theta * b.imag); + z_theta = theta * b.real + lnr * b.imag; + z.real = z_r * cosf(z_theta); + z.imag = z_r * sinf(z_theta); + return z; + } + #endif +#endif + +/* Declarations */ + #if CYTHON_CCOMPLEX + #ifdef __cplusplus + static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) { + return ::std::complex< double >(x, y); + } + #else + static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) { + return x + y*(__pyx_t_double_complex)_Complex_I; + } + #endif +#else + static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) { + __pyx_t_double_complex z; + z.real = x; + z.imag = y; + return z; + } +#endif + +/* Arithmetic */ + #if CYTHON_CCOMPLEX +#else + static CYTHON_INLINE int __Pyx_c_eq_double(__pyx_t_double_complex a, __pyx_t_double_complex b) { + return (a.real == b.real) && (a.imag == b.imag); + } + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_sum_double(__pyx_t_double_complex a, __pyx_t_double_complex b) { + __pyx_t_double_complex z; + z.real = a.real + b.real; + z.imag = a.imag + b.imag; + return z; + } + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_diff_double(__pyx_t_double_complex a, __pyx_t_double_complex b) { + __pyx_t_double_complex z; + z.real = a.real - b.real; + z.imag = a.imag - b.imag; + return z; + } + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_prod_double(__pyx_t_double_complex a, __pyx_t_double_complex b) { + __pyx_t_double_complex z; + z.real = a.real * b.real - a.imag * b.imag; + z.imag = a.real * b.imag + a.imag * b.real; + return z; + } + #if 1 + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_quot_double(__pyx_t_double_complex a, __pyx_t_double_complex b) { + if (b.imag == 0) { + return __pyx_t_double_complex_from_parts(a.real / b.real, a.imag / b.real); + } else if (fabs(b.real) >= fabs(b.imag)) { + if (b.real == 0 && b.imag == 0) { + return __pyx_t_double_complex_from_parts(a.real / b.real, a.imag / b.imag); + } else { + double r = b.imag / b.real; + double s = (double)(1.0) / (b.real + b.imag * r); + return __pyx_t_double_complex_from_parts( + (a.real + a.imag * r) * s, (a.imag - a.real * r) * s); + } + } else { + double r = b.real / b.imag; + double s = (double)(1.0) / (b.imag + b.real * r); + return __pyx_t_double_complex_from_parts( + (a.real * r + a.imag) * s, (a.imag * r - a.real) * s); + } + } + #else + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_quot_double(__pyx_t_double_complex a, __pyx_t_double_complex b) { + if (b.imag == 0) { + return __pyx_t_double_complex_from_parts(a.real / b.real, a.imag / b.real); + } else { + double denom = b.real * b.real + b.imag * b.imag; + return __pyx_t_double_complex_from_parts( + (a.real * b.real + a.imag * b.imag) / denom, + (a.imag * b.real - a.real * b.imag) / denom); + } + } + #endif + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_neg_double(__pyx_t_double_complex a) { + __pyx_t_double_complex z; + z.real = -a.real; + z.imag = -a.imag; + return z; + } + static CYTHON_INLINE int __Pyx_c_is_zero_double(__pyx_t_double_complex a) { + return (a.real == 0) && (a.imag == 0); + } + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_conj_double(__pyx_t_double_complex a) { + __pyx_t_double_complex z; + z.real = a.real; + z.imag = -a.imag; + return z; + } + #if 1 + static CYTHON_INLINE double __Pyx_c_abs_double(__pyx_t_double_complex z) { + #if !defined(HAVE_HYPOT) || defined(_MSC_VER) + return sqrt(z.real*z.real + z.imag*z.imag); + #else + return hypot(z.real, z.imag); + #endif + } + static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_pow_double(__pyx_t_double_complex a, __pyx_t_double_complex b) { + __pyx_t_double_complex z; + double r, lnr, theta, z_r, z_theta; + if (b.imag == 0 && b.real == (int)b.real) { + if (b.real < 0) { + double denom = a.real * a.real + a.imag * a.imag; + a.real = a.real / denom; + a.imag = -a.imag / denom; + b.real = -b.real; + } + switch ((int)b.real) { + case 0: + z.real = 1; + z.imag = 0; + return z; + case 1: + return a; + case 2: + return __Pyx_c_prod_double(a, a); + case 3: + z = __Pyx_c_prod_double(a, a); + return __Pyx_c_prod_double(z, a); + case 4: + z = __Pyx_c_prod_double(a, a); + return __Pyx_c_prod_double(z, z); + } + } + if (a.imag == 0) { + if (a.real == 0) { + return a; + } else if (b.imag == 0) { + z.real = pow(a.real, b.real); + z.imag = 0; + return z; + } else if (a.real > 0) { + r = a.real; + theta = 0; + } else { + r = -a.real; + theta = atan2(0.0, -1.0); + } + } else { + r = __Pyx_c_abs_double(a); + theta = atan2(a.imag, a.real); + } + lnr = log(r); + z_r = exp(lnr * b.real - theta * b.imag); + z_theta = theta * b.real + lnr * b.imag; + z.real = z_r * cos(z_theta); + z.imag = z_r * sin(z_theta); + return z; + } + #endif +#endif + +/* CIntFromPy */ + static CYTHON_INLINE unsigned int __Pyx_PyInt_As_unsigned_int(PyObject *x) { +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#endif + const unsigned int neg_one = (unsigned int) -1, const_zero = (unsigned int) 0; +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic pop +#endif + const int is_unsigned = neg_one > const_zero; +#if PY_MAJOR_VERSION < 3 + if (likely(PyInt_Check(x))) { + if (sizeof(unsigned int) < sizeof(long)) { + __PYX_VERIFY_RETURN_INT(unsigned int, long, PyInt_AS_LONG(x)) + } else { + long val = PyInt_AS_LONG(x); + if (is_unsigned && unlikely(val < 0)) { + goto raise_neg_overflow; + } + return (unsigned int) val; + } + } else +#endif + if (likely(PyLong_Check(x))) { + if (is_unsigned) { +#if CYTHON_USE_PYLONG_INTERNALS + const digit* digits = ((PyLongObject*)x)->ob_digit; + switch (Py_SIZE(x)) { + case 0: return (unsigned int) 0; + case 1: __PYX_VERIFY_RETURN_INT(unsigned int, digit, digits[0]) + case 2: + if (8 * sizeof(unsigned int) > 1 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(unsigned int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(unsigned int) >= 2 * PyLong_SHIFT) { + return (unsigned int) (((((unsigned int)digits[1]) << PyLong_SHIFT) | (unsigned int)digits[0])); + } + } + break; + case 3: + if (8 * sizeof(unsigned int) > 2 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(unsigned int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(unsigned int) >= 3 * PyLong_SHIFT) { + return (unsigned int) (((((((unsigned int)digits[2]) << PyLong_SHIFT) | (unsigned int)digits[1]) << PyLong_SHIFT) | (unsigned int)digits[0])); + } + } + break; + case 4: + if (8 * sizeof(unsigned int) > 3 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(unsigned int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(unsigned int) >= 4 * PyLong_SHIFT) { + return (unsigned int) (((((((((unsigned int)digits[3]) << PyLong_SHIFT) | (unsigned int)digits[2]) << PyLong_SHIFT) | (unsigned int)digits[1]) << PyLong_SHIFT) | (unsigned int)digits[0])); + } + } + break; + } +#endif +#if CYTHON_COMPILING_IN_CPYTHON + if (unlikely(Py_SIZE(x) < 0)) { + goto raise_neg_overflow; + } +#else + { + int result = PyObject_RichCompareBool(x, Py_False, Py_LT); + if (unlikely(result < 0)) + return (unsigned int) -1; + if (unlikely(result == 1)) + goto raise_neg_overflow; + } +#endif + if (sizeof(unsigned int) <= sizeof(unsigned long)) { + __PYX_VERIFY_RETURN_INT_EXC(unsigned int, unsigned long, PyLong_AsUnsignedLong(x)) +#ifdef HAVE_LONG_LONG + } else if (sizeof(unsigned int) <= sizeof(unsigned PY_LONG_LONG)) { + __PYX_VERIFY_RETURN_INT_EXC(unsigned int, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x)) +#endif + } + } else { +#if CYTHON_USE_PYLONG_INTERNALS + const digit* digits = ((PyLongObject*)x)->ob_digit; + switch (Py_SIZE(x)) { + case 0: return (unsigned int) 0; + case -1: __PYX_VERIFY_RETURN_INT(unsigned int, sdigit, (sdigit) (-(sdigit)digits[0])) + case 1: __PYX_VERIFY_RETURN_INT(unsigned int, digit, +digits[0]) + case -2: + if (8 * sizeof(unsigned int) - 1 > 1 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(unsigned int, long, -(long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(unsigned int) - 1 > 2 * PyLong_SHIFT) { + return (unsigned int) (((unsigned int)-1)*(((((unsigned int)digits[1]) << PyLong_SHIFT) | (unsigned int)digits[0]))); + } + } + break; + case 2: + if (8 * sizeof(unsigned int) > 1 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(unsigned int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(unsigned int) - 1 > 2 * PyLong_SHIFT) { + return (unsigned int) ((((((unsigned int)digits[1]) << PyLong_SHIFT) | (unsigned int)digits[0]))); + } + } + break; + case -3: + if (8 * sizeof(unsigned int) - 1 > 2 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(unsigned int, long, -(long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(unsigned int) - 1 > 3 * PyLong_SHIFT) { + return (unsigned int) (((unsigned int)-1)*(((((((unsigned int)digits[2]) << PyLong_SHIFT) | (unsigned int)digits[1]) << PyLong_SHIFT) | (unsigned int)digits[0]))); + } + } + break; + case 3: + if (8 * sizeof(unsigned int) > 2 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(unsigned int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(unsigned int) - 1 > 3 * PyLong_SHIFT) { + return (unsigned int) ((((((((unsigned int)digits[2]) << PyLong_SHIFT) | (unsigned int)digits[1]) << PyLong_SHIFT) | (unsigned int)digits[0]))); + } + } + break; + case -4: + if (8 * sizeof(unsigned int) - 1 > 3 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(unsigned int, long, -(long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(unsigned int) - 1 > 4 * PyLong_SHIFT) { + return (unsigned int) (((unsigned int)-1)*(((((((((unsigned int)digits[3]) << PyLong_SHIFT) | (unsigned int)digits[2]) << PyLong_SHIFT) | (unsigned int)digits[1]) << PyLong_SHIFT) | (unsigned int)digits[0]))); + } + } + break; + case 4: + if (8 * sizeof(unsigned int) > 3 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(unsigned int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(unsigned int) - 1 > 4 * PyLong_SHIFT) { + return (unsigned int) ((((((((((unsigned int)digits[3]) << PyLong_SHIFT) | (unsigned int)digits[2]) << PyLong_SHIFT) | (unsigned int)digits[1]) << PyLong_SHIFT) | (unsigned int)digits[0]))); + } + } + break; + } +#endif + if (sizeof(unsigned int) <= sizeof(long)) { + __PYX_VERIFY_RETURN_INT_EXC(unsigned int, long, PyLong_AsLong(x)) +#ifdef HAVE_LONG_LONG + } else if (sizeof(unsigned int) <= sizeof(PY_LONG_LONG)) { + __PYX_VERIFY_RETURN_INT_EXC(unsigned int, PY_LONG_LONG, PyLong_AsLongLong(x)) +#endif + } + } + { +#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray) + PyErr_SetString(PyExc_RuntimeError, + "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers"); +#else + unsigned int val; + PyObject *v = __Pyx_PyNumber_IntOrLong(x); + #if PY_MAJOR_VERSION < 3 + if (likely(v) && !PyLong_Check(v)) { + PyObject *tmp = v; + v = PyNumber_Long(tmp); + Py_DECREF(tmp); + } + #endif + if (likely(v)) { + int one = 1; int is_little = (int)*(unsigned char *)&one; + unsigned char *bytes = (unsigned char *)&val; + int ret = _PyLong_AsByteArray((PyLongObject *)v, + bytes, sizeof(val), + is_little, !is_unsigned); + Py_DECREF(v); + if (likely(!ret)) + return val; + } +#endif + return (unsigned int) -1; + } + } else { + unsigned int val; + PyObject *tmp = __Pyx_PyNumber_IntOrLong(x); + if (!tmp) return (unsigned int) -1; + val = __Pyx_PyInt_As_unsigned_int(tmp); + Py_DECREF(tmp); + return val; + } +raise_overflow: + PyErr_SetString(PyExc_OverflowError, + "value too large to convert to unsigned int"); + return (unsigned int) -1; +raise_neg_overflow: + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to unsigned int"); + return (unsigned int) -1; +} + +/* CIntToPy */ + static CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value) { +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#endif + const int neg_one = (int) -1, const_zero = (int) 0; +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic pop +#endif + const int is_unsigned = neg_one > const_zero; + if (is_unsigned) { + if (sizeof(int) < sizeof(long)) { + return PyInt_FromLong((long) value); + } else if (sizeof(int) <= sizeof(unsigned long)) { + return PyLong_FromUnsignedLong((unsigned long) value); +#ifdef HAVE_LONG_LONG + } else if (sizeof(int) <= sizeof(unsigned PY_LONG_LONG)) { + return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG) value); +#endif + } + } else { + if (sizeof(int) <= sizeof(long)) { + return PyInt_FromLong((long) value); +#ifdef HAVE_LONG_LONG + } else if (sizeof(int) <= sizeof(PY_LONG_LONG)) { + return PyLong_FromLongLong((PY_LONG_LONG) value); +#endif + } + } + { + int one = 1; int little = (int)*(unsigned char *)&one; + unsigned char *bytes = (unsigned char *)&value; + return _PyLong_FromByteArray(bytes, sizeof(int), + little, !is_unsigned); + } +} + +/* CIntFromPy */ + static CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) { +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#endif + const int neg_one = (int) -1, const_zero = (int) 0; +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic pop +#endif + const int is_unsigned = neg_one > const_zero; +#if PY_MAJOR_VERSION < 3 + if (likely(PyInt_Check(x))) { + if (sizeof(int) < sizeof(long)) { + __PYX_VERIFY_RETURN_INT(int, long, PyInt_AS_LONG(x)) + } else { + long val = PyInt_AS_LONG(x); + if (is_unsigned && unlikely(val < 0)) { + goto raise_neg_overflow; + } + return (int) val; + } + } else +#endif + if (likely(PyLong_Check(x))) { + if (is_unsigned) { +#if CYTHON_USE_PYLONG_INTERNALS + const digit* digits = ((PyLongObject*)x)->ob_digit; + switch (Py_SIZE(x)) { + case 0: return (int) 0; + case 1: __PYX_VERIFY_RETURN_INT(int, digit, digits[0]) + case 2: + if (8 * sizeof(int) > 1 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(int) >= 2 * PyLong_SHIFT) { + return (int) (((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); + } + } + break; + case 3: + if (8 * sizeof(int) > 2 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(int) >= 3 * PyLong_SHIFT) { + return (int) (((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); + } + } + break; + case 4: + if (8 * sizeof(int) > 3 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(int) >= 4 * PyLong_SHIFT) { + return (int) (((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0])); + } + } + break; + } +#endif +#if CYTHON_COMPILING_IN_CPYTHON + if (unlikely(Py_SIZE(x) < 0)) { + goto raise_neg_overflow; + } +#else + { + int result = PyObject_RichCompareBool(x, Py_False, Py_LT); + if (unlikely(result < 0)) + return (int) -1; + if (unlikely(result == 1)) + goto raise_neg_overflow; + } +#endif + if (sizeof(int) <= sizeof(unsigned long)) { + __PYX_VERIFY_RETURN_INT_EXC(int, unsigned long, PyLong_AsUnsignedLong(x)) +#ifdef HAVE_LONG_LONG + } else if (sizeof(int) <= sizeof(unsigned PY_LONG_LONG)) { + __PYX_VERIFY_RETURN_INT_EXC(int, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x)) +#endif + } + } else { +#if CYTHON_USE_PYLONG_INTERNALS + const digit* digits = ((PyLongObject*)x)->ob_digit; + switch (Py_SIZE(x)) { + case 0: return (int) 0; + case -1: __PYX_VERIFY_RETURN_INT(int, sdigit, (sdigit) (-(sdigit)digits[0])) + case 1: __PYX_VERIFY_RETURN_INT(int, digit, +digits[0]) + case -2: + if (8 * sizeof(int) - 1 > 1 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { + return (int) (((int)-1)*(((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); + } + } + break; + case 2: + if (8 * sizeof(int) > 1 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { + return (int) ((((((int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); + } + } + break; + case -3: + if (8 * sizeof(int) - 1 > 2 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { + return (int) (((int)-1)*(((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); + } + } + break; + case 3: + if (8 * sizeof(int) > 2 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { + return (int) ((((((((int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); + } + } + break; + case -4: + if (8 * sizeof(int) - 1 > 3 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(int, long, -(long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(int) - 1 > 4 * PyLong_SHIFT) { + return (int) (((int)-1)*(((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); + } + } + break; + case 4: + if (8 * sizeof(int) > 3 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(int, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(int) - 1 > 4 * PyLong_SHIFT) { + return (int) ((((((((((int)digits[3]) << PyLong_SHIFT) | (int)digits[2]) << PyLong_SHIFT) | (int)digits[1]) << PyLong_SHIFT) | (int)digits[0]))); + } + } + break; + } +#endif + if (sizeof(int) <= sizeof(long)) { + __PYX_VERIFY_RETURN_INT_EXC(int, long, PyLong_AsLong(x)) +#ifdef HAVE_LONG_LONG + } else if (sizeof(int) <= sizeof(PY_LONG_LONG)) { + __PYX_VERIFY_RETURN_INT_EXC(int, PY_LONG_LONG, PyLong_AsLongLong(x)) +#endif + } + } + { +#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray) + PyErr_SetString(PyExc_RuntimeError, + "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers"); +#else + int val; + PyObject *v = __Pyx_PyNumber_IntOrLong(x); + #if PY_MAJOR_VERSION < 3 + if (likely(v) && !PyLong_Check(v)) { + PyObject *tmp = v; + v = PyNumber_Long(tmp); + Py_DECREF(tmp); + } + #endif + if (likely(v)) { + int one = 1; int is_little = (int)*(unsigned char *)&one; + unsigned char *bytes = (unsigned char *)&val; + int ret = _PyLong_AsByteArray((PyLongObject *)v, + bytes, sizeof(val), + is_little, !is_unsigned); + Py_DECREF(v); + if (likely(!ret)) + return val; + } +#endif + return (int) -1; + } + } else { + int val; + PyObject *tmp = __Pyx_PyNumber_IntOrLong(x); + if (!tmp) return (int) -1; + val = __Pyx_PyInt_As_int(tmp); + Py_DECREF(tmp); + return val; + } +raise_overflow: + PyErr_SetString(PyExc_OverflowError, + "value too large to convert to int"); + return (int) -1; +raise_neg_overflow: + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to int"); + return (int) -1; +} + +/* CIntToPy */ + static CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) { +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#endif + const long neg_one = (long) -1, const_zero = (long) 0; +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic pop +#endif + const int is_unsigned = neg_one > const_zero; + if (is_unsigned) { + if (sizeof(long) < sizeof(long)) { + return PyInt_FromLong((long) value); + } else if (sizeof(long) <= sizeof(unsigned long)) { + return PyLong_FromUnsignedLong((unsigned long) value); +#ifdef HAVE_LONG_LONG + } else if (sizeof(long) <= sizeof(unsigned PY_LONG_LONG)) { + return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG) value); +#endif + } + } else { + if (sizeof(long) <= sizeof(long)) { + return PyInt_FromLong((long) value); +#ifdef HAVE_LONG_LONG + } else if (sizeof(long) <= sizeof(PY_LONG_LONG)) { + return PyLong_FromLongLong((PY_LONG_LONG) value); +#endif + } + } + { + int one = 1; int little = (int)*(unsigned char *)&one; + unsigned char *bytes = (unsigned char *)&value; + return _PyLong_FromByteArray(bytes, sizeof(long), + little, !is_unsigned); + } +} + +/* CIntToPy */ + static CYTHON_INLINE PyObject* __Pyx_PyInt_From_unsigned_int(unsigned int value) { +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#endif + const unsigned int neg_one = (unsigned int) -1, const_zero = (unsigned int) 0; +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic pop +#endif + const int is_unsigned = neg_one > const_zero; + if (is_unsigned) { + if (sizeof(unsigned int) < sizeof(long)) { + return PyInt_FromLong((long) value); + } else if (sizeof(unsigned int) <= sizeof(unsigned long)) { + return PyLong_FromUnsignedLong((unsigned long) value); +#ifdef HAVE_LONG_LONG + } else if (sizeof(unsigned int) <= sizeof(unsigned PY_LONG_LONG)) { + return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG) value); +#endif + } + } else { + if (sizeof(unsigned int) <= sizeof(long)) { + return PyInt_FromLong((long) value); +#ifdef HAVE_LONG_LONG + } else if (sizeof(unsigned int) <= sizeof(PY_LONG_LONG)) { + return PyLong_FromLongLong((PY_LONG_LONG) value); +#endif + } + } + { + int one = 1; int little = (int)*(unsigned char *)&one; + unsigned char *bytes = (unsigned char *)&value; + return _PyLong_FromByteArray(bytes, sizeof(unsigned int), + little, !is_unsigned); + } +} + +/* CIntFromPy */ + static CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) { +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wconversion" +#endif + const long neg_one = (long) -1, const_zero = (long) 0; +#ifdef __Pyx_HAS_GCC_DIAGNOSTIC +#pragma GCC diagnostic pop +#endif + const int is_unsigned = neg_one > const_zero; +#if PY_MAJOR_VERSION < 3 + if (likely(PyInt_Check(x))) { + if (sizeof(long) < sizeof(long)) { + __PYX_VERIFY_RETURN_INT(long, long, PyInt_AS_LONG(x)) + } else { + long val = PyInt_AS_LONG(x); + if (is_unsigned && unlikely(val < 0)) { + goto raise_neg_overflow; + } + return (long) val; + } + } else +#endif + if (likely(PyLong_Check(x))) { + if (is_unsigned) { +#if CYTHON_USE_PYLONG_INTERNALS + const digit* digits = ((PyLongObject*)x)->ob_digit; + switch (Py_SIZE(x)) { + case 0: return (long) 0; + case 1: __PYX_VERIFY_RETURN_INT(long, digit, digits[0]) + case 2: + if (8 * sizeof(long) > 1 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(long) >= 2 * PyLong_SHIFT) { + return (long) (((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); + } + } + break; + case 3: + if (8 * sizeof(long) > 2 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(long) >= 3 * PyLong_SHIFT) { + return (long) (((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); + } + } + break; + case 4: + if (8 * sizeof(long) > 3 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(long) >= 4 * PyLong_SHIFT) { + return (long) (((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0])); + } + } + break; + } +#endif +#if CYTHON_COMPILING_IN_CPYTHON + if (unlikely(Py_SIZE(x) < 0)) { + goto raise_neg_overflow; + } +#else + { + int result = PyObject_RichCompareBool(x, Py_False, Py_LT); + if (unlikely(result < 0)) + return (long) -1; + if (unlikely(result == 1)) + goto raise_neg_overflow; + } +#endif + if (sizeof(long) <= sizeof(unsigned long)) { + __PYX_VERIFY_RETURN_INT_EXC(long, unsigned long, PyLong_AsUnsignedLong(x)) +#ifdef HAVE_LONG_LONG + } else if (sizeof(long) <= sizeof(unsigned PY_LONG_LONG)) { + __PYX_VERIFY_RETURN_INT_EXC(long, unsigned PY_LONG_LONG, PyLong_AsUnsignedLongLong(x)) +#endif + } + } else { +#if CYTHON_USE_PYLONG_INTERNALS + const digit* digits = ((PyLongObject*)x)->ob_digit; + switch (Py_SIZE(x)) { + case 0: return (long) 0; + case -1: __PYX_VERIFY_RETURN_INT(long, sdigit, (sdigit) (-(sdigit)digits[0])) + case 1: __PYX_VERIFY_RETURN_INT(long, digit, +digits[0]) + case -2: + if (8 * sizeof(long) - 1 > 1 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { + return (long) (((long)-1)*(((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); + } + } + break; + case 2: + if (8 * sizeof(long) > 1 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 2 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { + return (long) ((((((long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); + } + } + break; + case -3: + if (8 * sizeof(long) - 1 > 2 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { + return (long) (((long)-1)*(((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); + } + } + break; + case 3: + if (8 * sizeof(long) > 2 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 3 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { + return (long) ((((((((long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); + } + } + break; + case -4: + if (8 * sizeof(long) - 1 > 3 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(long, long, -(long) (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(long) - 1 > 4 * PyLong_SHIFT) { + return (long) (((long)-1)*(((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); + } + } + break; + case 4: + if (8 * sizeof(long) > 3 * PyLong_SHIFT) { + if (8 * sizeof(unsigned long) > 4 * PyLong_SHIFT) { + __PYX_VERIFY_RETURN_INT(long, unsigned long, (((((((((unsigned long)digits[3]) << PyLong_SHIFT) | (unsigned long)digits[2]) << PyLong_SHIFT) | (unsigned long)digits[1]) << PyLong_SHIFT) | (unsigned long)digits[0]))) + } else if (8 * sizeof(long) - 1 > 4 * PyLong_SHIFT) { + return (long) ((((((((((long)digits[3]) << PyLong_SHIFT) | (long)digits[2]) << PyLong_SHIFT) | (long)digits[1]) << PyLong_SHIFT) | (long)digits[0]))); + } + } + break; + } +#endif + if (sizeof(long) <= sizeof(long)) { + __PYX_VERIFY_RETURN_INT_EXC(long, long, PyLong_AsLong(x)) +#ifdef HAVE_LONG_LONG + } else if (sizeof(long) <= sizeof(PY_LONG_LONG)) { + __PYX_VERIFY_RETURN_INT_EXC(long, PY_LONG_LONG, PyLong_AsLongLong(x)) +#endif + } + } + { +#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray) + PyErr_SetString(PyExc_RuntimeError, + "_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers"); +#else + long val; + PyObject *v = __Pyx_PyNumber_IntOrLong(x); + #if PY_MAJOR_VERSION < 3 + if (likely(v) && !PyLong_Check(v)) { + PyObject *tmp = v; + v = PyNumber_Long(tmp); + Py_DECREF(tmp); + } + #endif + if (likely(v)) { + int one = 1; int is_little = (int)*(unsigned char *)&one; + unsigned char *bytes = (unsigned char *)&val; + int ret = _PyLong_AsByteArray((PyLongObject *)v, + bytes, sizeof(val), + is_little, !is_unsigned); + Py_DECREF(v); + if (likely(!ret)) + return val; + } +#endif + return (long) -1; + } + } else { + long val; + PyObject *tmp = __Pyx_PyNumber_IntOrLong(x); + if (!tmp) return (long) -1; + val = __Pyx_PyInt_As_long(tmp); + Py_DECREF(tmp); + return val; + } +raise_overflow: + PyErr_SetString(PyExc_OverflowError, + "value too large to convert to long"); + return (long) -1; +raise_neg_overflow: + PyErr_SetString(PyExc_OverflowError, + "can't convert negative value to long"); + return (long) -1; +} + +/* FastTypeChecks */ + #if CYTHON_COMPILING_IN_CPYTHON +static int __Pyx_InBases(PyTypeObject *a, PyTypeObject *b) { + while (a) { + a = a->tp_base; + if (a == b) + return 1; + } + return b == &PyBaseObject_Type; +} +static CYTHON_INLINE int __Pyx_IsSubtype(PyTypeObject *a, PyTypeObject *b) { + PyObject *mro; + if (a == b) return 1; + mro = a->tp_mro; + if (likely(mro)) { + Py_ssize_t i, n; + n = PyTuple_GET_SIZE(mro); + for (i = 0; i < n; i++) { + if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) + return 1; + } + return 0; + } + return __Pyx_InBases(a, b); +} +#if PY_MAJOR_VERSION == 2 +static int __Pyx_inner_PyErr_GivenExceptionMatches2(PyObject *err, PyObject* exc_type1, PyObject* exc_type2) { + PyObject *exception, *value, *tb; + int res; + __Pyx_PyThreadState_declare + __Pyx_PyThreadState_assign + __Pyx_ErrFetch(&exception, &value, &tb); + res = exc_type1 ? PyObject_IsSubclass(err, exc_type1) : 0; + if (unlikely(res == -1)) { + PyErr_WriteUnraisable(err); + res = 0; + } + if (!res) { + res = PyObject_IsSubclass(err, exc_type2); + if (unlikely(res == -1)) { + PyErr_WriteUnraisable(err); + res = 0; + } + } + __Pyx_ErrRestore(exception, value, tb); + return res; +} +#else +static CYTHON_INLINE int __Pyx_inner_PyErr_GivenExceptionMatches2(PyObject *err, PyObject* exc_type1, PyObject *exc_type2) { + int res = exc_type1 ? __Pyx_IsSubtype((PyTypeObject*)err, (PyTypeObject*)exc_type1) : 0; + if (!res) { + res = __Pyx_IsSubtype((PyTypeObject*)err, (PyTypeObject*)exc_type2); + } + return res; +} +#endif +static int __Pyx_PyErr_GivenExceptionMatchesTuple(PyObject *exc_type, PyObject *tuple) { + Py_ssize_t i, n; + assert(PyExceptionClass_Check(exc_type)); + n = PyTuple_GET_SIZE(tuple); +#if PY_MAJOR_VERSION >= 3 + for (i=0; ip) { + #if PY_MAJOR_VERSION < 3 + if (t->is_unicode) { + *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL); + } else if (t->intern) { + *t->p = PyString_InternFromString(t->s); + } else { + *t->p = PyString_FromStringAndSize(t->s, t->n - 1); + } + #else + if (t->is_unicode | t->is_str) { + if (t->intern) { + *t->p = PyUnicode_InternFromString(t->s); + } else if (t->encoding) { + *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL); + } else { + *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1); + } + } else { + *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1); + } + #endif + if (!*t->p) + return -1; + if (PyObject_Hash(*t->p) == -1) + return -1; + ++t; + } + return 0; +} + +static CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(const char* c_str) { + return __Pyx_PyUnicode_FromStringAndSize(c_str, (Py_ssize_t)strlen(c_str)); +} +static CYTHON_INLINE const char* __Pyx_PyObject_AsString(PyObject* o) { + Py_ssize_t ignore; + return __Pyx_PyObject_AsStringAndSize(o, &ignore); +} +#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT +#if !CYTHON_PEP393_ENABLED +static const char* __Pyx_PyUnicode_AsStringAndSize(PyObject* o, Py_ssize_t *length) { + char* defenc_c; + PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL); + if (!defenc) return NULL; + defenc_c = PyBytes_AS_STRING(defenc); +#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII + { + char* end = defenc_c + PyBytes_GET_SIZE(defenc); + char* c; + for (c = defenc_c; c < end; c++) { + if ((unsigned char) (*c) >= 128) { + PyUnicode_AsASCIIString(o); + return NULL; + } + } + } +#endif + *length = PyBytes_GET_SIZE(defenc); + return defenc_c; +} +#else +static CYTHON_INLINE const char* __Pyx_PyUnicode_AsStringAndSize(PyObject* o, Py_ssize_t *length) { + if (unlikely(__Pyx_PyUnicode_READY(o) == -1)) return NULL; +#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII + if (likely(PyUnicode_IS_ASCII(o))) { + *length = PyUnicode_GET_LENGTH(o); + return PyUnicode_AsUTF8(o); + } else { + PyUnicode_AsASCIIString(o); + return NULL; + } +#else + return PyUnicode_AsUTF8AndSize(o, length); +#endif +} +#endif +#endif +static CYTHON_INLINE const char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) { +#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT + if ( +#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII + __Pyx_sys_getdefaultencoding_not_ascii && +#endif + PyUnicode_Check(o)) { + return __Pyx_PyUnicode_AsStringAndSize(o, length); + } else +#endif +#if (!CYTHON_COMPILING_IN_PYPY) || (defined(PyByteArray_AS_STRING) && defined(PyByteArray_GET_SIZE)) + if (PyByteArray_Check(o)) { + *length = PyByteArray_GET_SIZE(o); + return PyByteArray_AS_STRING(o); + } else +#endif + { + char* result; + int r = PyBytes_AsStringAndSize(o, &result, length); + if (unlikely(r < 0)) { + return NULL; + } else { + return result; + } + } +} +static CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) { + int is_true = x == Py_True; + if (is_true | (x == Py_False) | (x == Py_None)) return is_true; + else return PyObject_IsTrue(x); +} +static CYTHON_INLINE int __Pyx_PyObject_IsTrueAndDecref(PyObject* x) { + int retval; + if (unlikely(!x)) return -1; + retval = __Pyx_PyObject_IsTrue(x); + Py_DECREF(x); + return retval; +} +static PyObject* __Pyx_PyNumber_IntOrLongWrongResultType(PyObject* result, const char* type_name) { +#if PY_MAJOR_VERSION >= 3 + if (PyLong_Check(result)) { + if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, + "__int__ returned non-int (type %.200s). " + "The ability to return an instance of a strict subclass of int " + "is deprecated, and may be removed in a future version of Python.", + Py_TYPE(result)->tp_name)) { + Py_DECREF(result); + return NULL; + } + return result; + } +#endif + PyErr_Format(PyExc_TypeError, + "__%.4s__ returned non-%.4s (type %.200s)", + type_name, type_name, Py_TYPE(result)->tp_name); + Py_DECREF(result); + return NULL; +} +static CYTHON_INLINE PyObject* __Pyx_PyNumber_IntOrLong(PyObject* x) { +#if CYTHON_USE_TYPE_SLOTS + PyNumberMethods *m; +#endif + const char *name = NULL; + PyObject *res = NULL; +#if PY_MAJOR_VERSION < 3 + if (likely(PyInt_Check(x) || PyLong_Check(x))) +#else + if (likely(PyLong_Check(x))) +#endif + return __Pyx_NewRef(x); +#if CYTHON_USE_TYPE_SLOTS + m = Py_TYPE(x)->tp_as_number; + #if PY_MAJOR_VERSION < 3 + if (m && m->nb_int) { + name = "int"; + res = m->nb_int(x); + } + else if (m && m->nb_long) { + name = "long"; + res = m->nb_long(x); + } + #else + if (likely(m && m->nb_int)) { + name = "int"; + res = m->nb_int(x); + } + #endif +#else + if (!PyBytes_CheckExact(x) && !PyUnicode_CheckExact(x)) { + res = PyNumber_Int(x); + } +#endif + if (likely(res)) { +#if PY_MAJOR_VERSION < 3 + if (unlikely(!PyInt_Check(res) && !PyLong_Check(res))) { +#else + if (unlikely(!PyLong_CheckExact(res))) { +#endif + return __Pyx_PyNumber_IntOrLongWrongResultType(res, name); + } + } + else if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, + "an integer is required"); + } + return res; +} +static CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) { + Py_ssize_t ival; + PyObject *x; +#if PY_MAJOR_VERSION < 3 + if (likely(PyInt_CheckExact(b))) { + if (sizeof(Py_ssize_t) >= sizeof(long)) + return PyInt_AS_LONG(b); + else + return PyInt_AsSsize_t(b); + } +#endif + if (likely(PyLong_CheckExact(b))) { + #if CYTHON_USE_PYLONG_INTERNALS + const digit* digits = ((PyLongObject*)b)->ob_digit; + const Py_ssize_t size = Py_SIZE(b); + if (likely(__Pyx_sst_abs(size) <= 1)) { + ival = likely(size) ? digits[0] : 0; + if (size == -1) ival = -ival; + return ival; + } else { + switch (size) { + case 2: + if (8 * sizeof(Py_ssize_t) > 2 * PyLong_SHIFT) { + return (Py_ssize_t) (((((size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); + } + break; + case -2: + if (8 * sizeof(Py_ssize_t) > 2 * PyLong_SHIFT) { + return -(Py_ssize_t) (((((size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); + } + break; + case 3: + if (8 * sizeof(Py_ssize_t) > 3 * PyLong_SHIFT) { + return (Py_ssize_t) (((((((size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); + } + break; + case -3: + if (8 * sizeof(Py_ssize_t) > 3 * PyLong_SHIFT) { + return -(Py_ssize_t) (((((((size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); + } + break; + case 4: + if (8 * sizeof(Py_ssize_t) > 4 * PyLong_SHIFT) { + return (Py_ssize_t) (((((((((size_t)digits[3]) << PyLong_SHIFT) | (size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); + } + break; + case -4: + if (8 * sizeof(Py_ssize_t) > 4 * PyLong_SHIFT) { + return -(Py_ssize_t) (((((((((size_t)digits[3]) << PyLong_SHIFT) | (size_t)digits[2]) << PyLong_SHIFT) | (size_t)digits[1]) << PyLong_SHIFT) | (size_t)digits[0])); + } + break; + } + } + #endif + return PyLong_AsSsize_t(b); + } + x = PyNumber_Index(b); + if (!x) return -1; + ival = PyInt_AsSsize_t(x); + Py_DECREF(x); + return ival; +} +static CYTHON_INLINE PyObject * __Pyx_PyBool_FromLong(long b) { + return b ? __Pyx_NewRef(Py_True) : __Pyx_NewRef(Py_False); +} +static CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) { + return PyInt_FromSize_t(ival); +} + + +#endif /* Py_PYTHON_H */ diff --git a/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.cpython-36m-x86_64-linux-gnu.so b/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.cpython-36m-x86_64-linux-gnu.so new file mode 100644 index 0000000000000000000000000000000000000000..528ab50cad93b03a06b356739aa2afcd912b6f55 Binary files /dev/null and b/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.cpython-36m-x86_64-linux-gnu.so differ diff --git a/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.cpython-38-x86_64-linux-gnu.so b/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.cpython-38-x86_64-linux-gnu.so new file mode 100644 index 0000000000000000000000000000000000000000..05fca50146394c402826b1fb0e4694bafb722f35 Binary files /dev/null and b/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.cpython-38-x86_64-linux-gnu.so differ diff --git a/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.pyx b/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.pyx new file mode 100644 index 0000000000000000000000000000000000000000..5f921bb2e0ad8be2f9f35b59a452327b191fae78 --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/nms/cpu_nms.pyx @@ -0,0 +1,163 @@ +# -------------------------------------------------------- +# Fast R-CNN +# Copyright (c) 2015 Microsoft +# Licensed under The MIT License [see LICENSE for details] +# Written by Ross Girshick +# -------------------------------------------------------- + +import numpy as np +cimport numpy as np + +cdef inline np.float32_t max(np.float32_t a, np.float32_t b): + return a if a >= b else b + +cdef inline np.float32_t min(np.float32_t a, np.float32_t b): + return a if a <= b else b + +def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh): + cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0] + cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1] + cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2] + cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3] + cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4] + + cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1) + cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1] + + cdef int ndets = dets.shape[0] + cdef np.ndarray[np.int_t, ndim=1] suppressed = \ + np.zeros((ndets), dtype=np.int) + + # nominal indices + cdef int _i, _j + # sorted indices + cdef int i, j + # temp variables for box i's (the box currently under consideration) + cdef np.float32_t ix1, iy1, ix2, iy2, iarea + # variables for computing overlap with box j (lower scoring box) + cdef np.float32_t xx1, yy1, xx2, yy2 + cdef np.float32_t w, h + cdef np.float32_t inter, ovr + + keep = [] + for _i in range(ndets): + i = order[_i] + if suppressed[i] == 1: + continue + keep.append(i) + ix1 = x1[i] + iy1 = y1[i] + ix2 = x2[i] + iy2 = y2[i] + iarea = areas[i] + for _j in range(_i + 1, ndets): + j = order[_j] + if suppressed[j] == 1: + continue + xx1 = max(ix1, x1[j]) + yy1 = max(iy1, y1[j]) + xx2 = min(ix2, x2[j]) + yy2 = min(iy2, y2[j]) + w = max(0.0, xx2 - xx1 + 1) + h = max(0.0, yy2 - yy1 + 1) + inter = w * h + ovr = inter / (iarea + areas[j] - inter) + if ovr >= thresh: + suppressed[j] = 1 + + return keep + +def cpu_soft_nms(np.ndarray[float, ndim=2] boxes, float sigma=0.5, float Nt=0.3, float threshold=0.001, unsigned int method=0): + cdef unsigned int N = boxes.shape[0] + cdef float iw, ih, box_area + cdef float ua + cdef int pos = 0 + cdef float maxscore = 0 + cdef int maxpos = 0 + cdef float x1,x2,y1,y2,tx1,tx2,ty1,ty2,ts,area,weight,ov + + for i in range(N): + maxscore = boxes[i, 4] + maxpos = i + + tx1 = boxes[i,0] + ty1 = boxes[i,1] + tx2 = boxes[i,2] + ty2 = boxes[i,3] + ts = boxes[i,4] + + pos = i + 1 + # get max box + while pos < N: + if maxscore < boxes[pos, 4]: + maxscore = boxes[pos, 4] + maxpos = pos + pos = pos + 1 + + # add max box as a detection + boxes[i,0] = boxes[maxpos,0] + boxes[i,1] = boxes[maxpos,1] + boxes[i,2] = boxes[maxpos,2] + boxes[i,3] = boxes[maxpos,3] + boxes[i,4] = boxes[maxpos,4] + + # swap ith box with position of max box + boxes[maxpos,0] = tx1 + boxes[maxpos,1] = ty1 + boxes[maxpos,2] = tx2 + boxes[maxpos,3] = ty2 + boxes[maxpos,4] = ts + + tx1 = boxes[i,0] + ty1 = boxes[i,1] + tx2 = boxes[i,2] + ty2 = boxes[i,3] + ts = boxes[i,4] + + pos = i + 1 + # NMS iterations, note that N changes if detection boxes fall below threshold + while pos < N: + x1 = boxes[pos, 0] + y1 = boxes[pos, 1] + x2 = boxes[pos, 2] + y2 = boxes[pos, 3] + s = boxes[pos, 4] + + area = (x2 - x1 + 1) * (y2 - y1 + 1) + iw = (min(tx2, x2) - max(tx1, x1) + 1) + if iw > 0: + ih = (min(ty2, y2) - max(ty1, y1) + 1) + if ih > 0: + ua = float((tx2 - tx1 + 1) * (ty2 - ty1 + 1) + area - iw * ih) + ov = iw * ih / ua #iou between max box and detection box + + if method == 1: # linear + if ov > Nt: + weight = 1 - ov + else: + weight = 1 + elif method == 2: # gaussian + weight = np.exp(-(ov * ov)/sigma) + else: # original NMS + if ov > Nt: + weight = 0 + else: + weight = 1 + + boxes[pos, 4] = weight*boxes[pos, 4] + + # if box score falls below threshold, discard the box by swapping with last box + # update N + if boxes[pos, 4] < threshold: + boxes[pos,0] = boxes[N-1, 0] + boxes[pos,1] = boxes[N-1, 1] + boxes[pos,2] = boxes[N-1, 2] + boxes[pos,3] = boxes[N-1, 3] + boxes[pos,4] = boxes[N-1, 4] + N = N - 1 + pos = pos - 1 + + pos = pos + 1 + + keep = [i for i in range(N)] + return keep diff --git a/third_party/PIPNet/FaceBoxesV2/utils/nms/gpu_nms.hpp b/third_party/PIPNet/FaceBoxesV2/utils/nms/gpu_nms.hpp new file mode 100644 index 0000000000000000000000000000000000000000..68b6d42cd88b59496b22a9e77919abe529b09014 --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/nms/gpu_nms.hpp @@ -0,0 +1,2 @@ +void _nms(int* keep_out, int* num_out, const float* boxes_host, int boxes_num, + int boxes_dim, float nms_overlap_thresh, int device_id); diff --git a/third_party/PIPNet/FaceBoxesV2/utils/nms/gpu_nms.pyx b/third_party/PIPNet/FaceBoxesV2/utils/nms/gpu_nms.pyx new file mode 100644 index 0000000000000000000000000000000000000000..59d84afe94e42de3c456b73580ed83358a2b30d8 --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/nms/gpu_nms.pyx @@ -0,0 +1,31 @@ +# -------------------------------------------------------- +# Faster R-CNN +# Copyright (c) 2015 Microsoft +# Licensed under The MIT License [see LICENSE for details] +# Written by Ross Girshick +# -------------------------------------------------------- + +import numpy as np +cimport numpy as np + +assert sizeof(int) == sizeof(np.int32_t) + +cdef extern from "gpu_nms.hpp": + void _nms(np.int32_t*, int*, np.float32_t*, int, int, float, int) + +def gpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh, + np.int32_t device_id=0): + cdef int boxes_num = dets.shape[0] + cdef int boxes_dim = dets.shape[1] + cdef int num_out + cdef np.ndarray[np.int32_t, ndim=1] \ + keep = np.zeros(boxes_num, dtype=np.int32) + cdef np.ndarray[np.float32_t, ndim=1] \ + scores = dets[:, 4] + cdef np.ndarray[np.int_t, ndim=1] \ + order = scores.argsort()[::-1] + cdef np.ndarray[np.float32_t, ndim=2] \ + sorted_dets = dets[order, :] + _nms(&keep[0], &num_out, &sorted_dets[0, 0], boxes_num, boxes_dim, thresh, device_id) + keep = keep[:num_out] + return list(order[keep]) diff --git a/third_party/PIPNet/FaceBoxesV2/utils/nms/nms_kernel.cu b/third_party/PIPNet/FaceBoxesV2/utils/nms/nms_kernel.cu new file mode 100644 index 0000000000000000000000000000000000000000..038a59012f60ebdf1182ecb778eb3b01a69bc5ed --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/nms/nms_kernel.cu @@ -0,0 +1,144 @@ +// ------------------------------------------------------------------ +// Faster R-CNN +// Copyright (c) 2015 Microsoft +// Licensed under The MIT License [see fast-rcnn/LICENSE for details] +// Written by Shaoqing Ren +// ------------------------------------------------------------------ + +#include "gpu_nms.hpp" +#include +#include + +#define CUDA_CHECK(condition) \ + /* Code block avoids redefinition of cudaError_t error */ \ + do { \ + cudaError_t error = condition; \ + if (error != cudaSuccess) { \ + std::cout << cudaGetErrorString(error) << std::endl; \ + } \ + } while (0) + +#define DIVUP(m,n) ((m) / (n) + ((m) % (n) > 0)) +int const threadsPerBlock = sizeof(unsigned long long) * 8; + +__device__ inline float devIoU(float const * const a, float const * const b) { + float left = max(a[0], b[0]), right = min(a[2], b[2]); + float top = max(a[1], b[1]), bottom = min(a[3], b[3]); + float width = max(right - left + 1, 0.f), height = max(bottom - top + 1, 0.f); + float interS = width * height; + float Sa = (a[2] - a[0] + 1) * (a[3] - a[1] + 1); + float Sb = (b[2] - b[0] + 1) * (b[3] - b[1] + 1); + return interS / (Sa + Sb - interS); +} + +__global__ void nms_kernel(const int n_boxes, const float nms_overlap_thresh, + const float *dev_boxes, unsigned long long *dev_mask) { + const int row_start = blockIdx.y; + const int col_start = blockIdx.x; + + // if (row_start > col_start) return; + + const int row_size = + min(n_boxes - row_start * threadsPerBlock, threadsPerBlock); + const int col_size = + min(n_boxes - col_start * threadsPerBlock, threadsPerBlock); + + __shared__ float block_boxes[threadsPerBlock * 5]; + if (threadIdx.x < col_size) { + block_boxes[threadIdx.x * 5 + 0] = + dev_boxes[(threadsPerBlock * col_start + threadIdx.x) * 5 + 0]; + block_boxes[threadIdx.x * 5 + 1] = + dev_boxes[(threadsPerBlock * col_start + threadIdx.x) * 5 + 1]; + block_boxes[threadIdx.x * 5 + 2] = + dev_boxes[(threadsPerBlock * col_start + threadIdx.x) * 5 + 2]; + block_boxes[threadIdx.x * 5 + 3] = + dev_boxes[(threadsPerBlock * col_start + threadIdx.x) * 5 + 3]; + block_boxes[threadIdx.x * 5 + 4] = + dev_boxes[(threadsPerBlock * col_start + threadIdx.x) * 5 + 4]; + } + __syncthreads(); + + if (threadIdx.x < row_size) { + const int cur_box_idx = threadsPerBlock * row_start + threadIdx.x; + const float *cur_box = dev_boxes + cur_box_idx * 5; + int i = 0; + unsigned long long t = 0; + int start = 0; + if (row_start == col_start) { + start = threadIdx.x + 1; + } + for (i = start; i < col_size; i++) { + if (devIoU(cur_box, block_boxes + i * 5) > nms_overlap_thresh) { + t |= 1ULL << i; + } + } + const int col_blocks = DIVUP(n_boxes, threadsPerBlock); + dev_mask[cur_box_idx * col_blocks + col_start] = t; + } +} + +void _set_device(int device_id) { + int current_device; + CUDA_CHECK(cudaGetDevice(¤t_device)); + if (current_device == device_id) { + return; + } + // The call to cudaSetDevice must come before any calls to Get, which + // may perform initialization using the GPU. + CUDA_CHECK(cudaSetDevice(device_id)); +} + +void _nms(int* keep_out, int* num_out, const float* boxes_host, int boxes_num, + int boxes_dim, float nms_overlap_thresh, int device_id) { + _set_device(device_id); + + float* boxes_dev = NULL; + unsigned long long* mask_dev = NULL; + + const int col_blocks = DIVUP(boxes_num, threadsPerBlock); + + CUDA_CHECK(cudaMalloc(&boxes_dev, + boxes_num * boxes_dim * sizeof(float))); + CUDA_CHECK(cudaMemcpy(boxes_dev, + boxes_host, + boxes_num * boxes_dim * sizeof(float), + cudaMemcpyHostToDevice)); + + CUDA_CHECK(cudaMalloc(&mask_dev, + boxes_num * col_blocks * sizeof(unsigned long long))); + + dim3 blocks(DIVUP(boxes_num, threadsPerBlock), + DIVUP(boxes_num, threadsPerBlock)); + dim3 threads(threadsPerBlock); + nms_kernel<<>>(boxes_num, + nms_overlap_thresh, + boxes_dev, + mask_dev); + + std::vector mask_host(boxes_num * col_blocks); + CUDA_CHECK(cudaMemcpy(&mask_host[0], + mask_dev, + sizeof(unsigned long long) * boxes_num * col_blocks, + cudaMemcpyDeviceToHost)); + + std::vector remv(col_blocks); + memset(&remv[0], 0, sizeof(unsigned long long) * col_blocks); + + int num_to_keep = 0; + for (int i = 0; i < boxes_num; i++) { + int nblock = i / threadsPerBlock; + int inblock = i % threadsPerBlock; + + if (!(remv[nblock] & (1ULL << inblock))) { + keep_out[num_to_keep++] = i; + unsigned long long *p = &mask_host[0] + i * col_blocks; + for (int j = nblock; j < col_blocks; j++) { + remv[j] |= p[j]; + } + } + } + *num_out = num_to_keep; + + CUDA_CHECK(cudaFree(boxes_dev)); + CUDA_CHECK(cudaFree(mask_dev)); +} diff --git a/third_party/PIPNet/FaceBoxesV2/utils/nms/py_cpu_nms.py b/third_party/PIPNet/FaceBoxesV2/utils/nms/py_cpu_nms.py new file mode 100644 index 0000000000000000000000000000000000000000..54e7b25fef72b518df6dcf8d6fb78b986796c6e3 --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/nms/py_cpu_nms.py @@ -0,0 +1,38 @@ +# -------------------------------------------------------- +# Fast R-CNN +# Copyright (c) 2015 Microsoft +# Licensed under The MIT License [see LICENSE for details] +# Written by Ross Girshick +# -------------------------------------------------------- + +import numpy as np + +def py_cpu_nms(dets, thresh): + """Pure Python NMS baseline.""" + x1 = dets[:, 0] + y1 = dets[:, 1] + x2 = dets[:, 2] + y2 = dets[:, 3] + scores = dets[:, 4] + + areas = (x2 - x1 + 1) * (y2 - y1 + 1) + order = scores.argsort()[::-1] + + keep = [] + while order.size > 0: + i = order[0] + keep.append(i) + xx1 = np.maximum(x1[i], x1[order[1:]]) + yy1 = np.maximum(y1[i], y1[order[1:]]) + xx2 = np.minimum(x2[i], x2[order[1:]]) + yy2 = np.minimum(y2[i], y2[order[1:]]) + + w = np.maximum(0.0, xx2 - xx1 + 1) + h = np.maximum(0.0, yy2 - yy1 + 1) + inter = w * h + ovr = inter / (areas[i] + areas[order[1:]] - inter) + + inds = np.where(ovr <= thresh)[0] + order = order[inds + 1] + + return keep diff --git a/third_party/PIPNet/FaceBoxesV2/utils/nms_wrapper.py b/third_party/PIPNet/FaceBoxesV2/utils/nms_wrapper.py new file mode 100644 index 0000000000000000000000000000000000000000..d529875fac67e070ea865a1ba0cc3d248847827f --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/nms_wrapper.py @@ -0,0 +1,15 @@ +# -------------------------------------------------------- +# Fast R-CNN +# Copyright (c) 2015 Microsoft +# Licensed under The MIT License [see LICENSE for details] +# Written by Ross Girshick +# -------------------------------------------------------- + +from .nms.cpu_nms import cpu_nms, cpu_soft_nms + +def nms(dets, thresh): + """Dispatch to either CPU or GPU NMS implementations.""" + + if dets.shape[0] == 0: + return [] + return cpu_nms(dets, thresh) diff --git a/third_party/PIPNet/FaceBoxesV2/utils/prior_box.py b/third_party/PIPNet/FaceBoxesV2/utils/prior_box.py new file mode 100644 index 0000000000000000000000000000000000000000..e5536670afe139de420bc16bd88238fd2a90735b --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/prior_box.py @@ -0,0 +1,43 @@ +import torch +from itertools import product as product +import numpy as np +from math import ceil + + +class PriorBox(object): + def __init__(self, cfg, image_size=None, phase='train'): + super(PriorBox, self).__init__() + #self.aspect_ratios = cfg['aspect_ratios'] + self.min_sizes = cfg['min_sizes'] + self.steps = cfg['steps'] + self.clip = cfg['clip'] + self.image_size = image_size + self.feature_maps = [[ceil(self.image_size[0]/step), ceil(self.image_size[1]/step)] for step in self.steps] + + def forward(self): + anchors = [] + for k, f in enumerate(self.feature_maps): + min_sizes = self.min_sizes[k] + for i, j in product(range(f[0]), range(f[1])): + for min_size in min_sizes: + s_kx = min_size / self.image_size[1] + s_ky = min_size / self.image_size[0] + if min_size == 32: + dense_cx = [x*self.steps[k]/self.image_size[1] for x in [j+0, j+0.25, j+0.5, j+0.75]] + dense_cy = [y*self.steps[k]/self.image_size[0] for y in [i+0, i+0.25, i+0.5, i+0.75]] + for cy, cx in product(dense_cy, dense_cx): + anchors += [cx, cy, s_kx, s_ky] + elif min_size == 64: + dense_cx = [x*self.steps[k]/self.image_size[1] for x in [j+0, j+0.5]] + dense_cy = [y*self.steps[k]/self.image_size[0] for y in [i+0, i+0.5]] + for cy, cx in product(dense_cy, dense_cx): + anchors += [cx, cy, s_kx, s_ky] + else: + cx = (j + 0.5) * self.steps[k] / self.image_size[1] + cy = (i + 0.5) * self.steps[k] / self.image_size[0] + anchors += [cx, cy, s_kx, s_ky] + # back to torch land + output = torch.Tensor(anchors).view(-1, 4) + if self.clip: + output.clamp_(max=1, min=0) + return output diff --git a/third_party/PIPNet/FaceBoxesV2/utils/timer.py b/third_party/PIPNet/FaceBoxesV2/utils/timer.py new file mode 100644 index 0000000000000000000000000000000000000000..e4b3b8098a5ad41f8d18d42b6b2fedb694aa5508 --- /dev/null +++ b/third_party/PIPNet/FaceBoxesV2/utils/timer.py @@ -0,0 +1,40 @@ +# -------------------------------------------------------- +# Fast R-CNN +# Copyright (c) 2015 Microsoft +# Licensed under The MIT License [see LICENSE for details] +# Written by Ross Girshick +# -------------------------------------------------------- + +import time + + +class Timer(object): + """A simple timer.""" + def __init__(self): + self.total_time = 0. + self.calls = 0 + self.start_time = 0. + self.diff = 0. + self.average_time = 0. + + def tic(self): + # using time.time instead of time.clock because time time.clock + # does not normalize for multithreading + self.start_time = time.time() + + def toc(self, average=True): + self.diff = time.time() - self.start_time + self.total_time += self.diff + self.calls += 1 + self.average_time = self.total_time / self.calls + if average: + return self.average_time + else: + return self.diff + + def clear(self): + self.total_time = 0. + self.calls = 0 + self.start_time = 0. + self.diff = 0. + self.average_time = 0. diff --git a/third_party/PIPNet/LICENSE b/third_party/PIPNet/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..643b2d0cf63445ede21be6a0f9dfd37e23c75388 --- /dev/null +++ b/third_party/PIPNet/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 Haibo Jin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/third_party/PIPNet/README.md b/third_party/PIPNet/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9bc552a05fc2194f7dbc208a18a3273e943dccf0 --- /dev/null +++ b/third_party/PIPNet/README.md @@ -0,0 +1,153 @@ +# Pixel-in-Pixel Net: Towards Efficient Facial Landmark Detection in the Wild +## Introduction +This is the code of paper [Pixel-in-Pixel Net: Towards Efficient Facial Landmark Detection in the Wild](https://arxiv.org/abs/2003.03771). We propose a novel facial landmark detector, PIPNet, that is **fast**, **accurate**, and **robust**. PIPNet can be trained under two settings: (1) supervised learning; (2) generalizable semi-supervised learning (GSSL). With GSSL, PIPNet has better cross-domain generalization performance by utilizing massive amounts of unlabeled data across domains. + +speed +Figure 1. Comparison to existing methods on speed-accuracy tradeoff, tested on WFLW full test set (closer to bottom-right corner is better).

+ +det_heads +Figure 2. Comparison of different detection heads.
+ +## Installation +1. Install Python3 and PyTorch >= v1.1 +2. Clone this repository. +```Shell +git clone https://github.com/jhb86253817/PIPNet.git +``` +3. Install the dependencies in requirements.txt. +```Shell +pip install -r requirements.txt +``` + +## Demo +1. We use a [modified version](https://github.com/jhb86253817/FaceBoxesV2) of [FaceBoxes](https://github.com/zisianw/FaceBoxes.PyTorch) as the face detector, so go to folder `FaceBoxesV2/utils`, run `sh make.sh` to build for NMS. +2. Back to folder `PIPNet`, create two empty folders `logs` and `snapshots`. For PIPNets, you can download our trained models from [here](https://drive.google.com/drive/folders/17OwDgJUfuc5_ymQ3QruD8pUnh5zHreP2?usp=sharing), and put them under folder `snapshots/DATA_NAME/EXPERIMENT_NAME/`. +3. Edit `run_demo.sh` to choose the config file and input source you want, then run `sh run_demo.sh`. We support image, video, and camera as the input. Some sample predictions can be seen as follows. +* PIPNet-ResNet18 trained on WFLW, with image `images/1.jpg` as the input: +1_out_WFLW_model + +* PIPNet-ResNet18 trained on WFLW, with a snippet from *Shaolin Soccer* as the input: +shaolin_soccer + +* PIPNet-ResNet18 trained on WFLW, with video `videos/002.avi` as the input: +002_out_WFLW_model + +* PIPNet-ResNet18 trained on 300W+CelebA (GSSL), with video `videos/007.avi` as the input: +007_out_300W_CELEBA_model + +## Training + +### Supervised Learning +Datasets: [300W](https://ibug.doc.ic.ac.uk/resources/facial-point-annotations/), [COFW](http://www.vision.caltech.edu/xpburgos/ICCV13/), [WFLW](https://wywu.github.io/projects/LAB/WFLW.html), [AFLW](https://www.tugraz.at/institute/icg/research/team-bischof/lrs/downloads/aflw/) + +1. Download the datasets from official sources, then put them under folder `data`. The folder structure should look like this: +```` +PIPNet +-- FaceBoxesV2 +-- lib +-- experiments +-- logs +-- snapshots +-- data + |-- data_300W + |-- afw + |-- helen + |-- ibug + |-- lfpw + |-- COFW + |-- COFW_train_color.mat + |-- COFW_test_color.mat + |-- WFLW + |-- WFLW_images + |-- WFLW_annotations + |-- AFLW + |-- flickr + |-- AFLWinfo_release.mat +```` +2. Go to folder `lib`, preprocess a dataset by running ```python preprocess.py DATA_NAME```. For example, to process 300W: +``` +python preprocess.py data_300W +``` +3. Back to folder `PIPNet`, edit `run_train.sh` to choose the config file you want. Then, train the model by running: +``` +sh run_train.sh +``` + +### Generalizable Semi-supervised Learning +Datasets: +* data_300W_COFW_WFLW: 300W + COFW-68 (unlabeled) + WFLW-68 (unlabeled) +* data_300W_CELEBA: 300W + CelebA (unlabeled) + +1. Download 300W, COFW, and WFLW as in the supervised learning setting. Download annotations of COFW-68 test from [here](https://github.com/golnazghiasi/cofw68-benchmark). For 300W+CelebA, you also need to download the in-the-wild CelebA images from [here](http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html), and the [face bounding boxes](https://drive.google.com/drive/folders/17OwDgJUfuc5_ymQ3QruD8pUnh5zHreP2?usp=sharing) detected by us. The folder structure should look like this: +```` +PIPNet +-- FaceBoxesV2 +-- lib +-- experiments +-- logs +-- snapshots +-- data + |-- data_300W + |-- afw + |-- helen + |-- ibug + |-- lfpw + |-- COFW + |-- COFW_train_color.mat + |-- COFW_test_color.mat + |-- WFLW + |-- WFLW_images + |-- WFLW_annotations + |-- data_300W_COFW_WFLW + |-- cofw68_test_annotations + |-- cofw68_test_bboxes.mat + |-- CELEBA + |-- img_celeba + |-- celeba_bboxes.txt + |-- data_300W_CELEBA + |-- cofw68_test_annotations + |-- cofw68_test_bboxes.mat +```` +2. Go to folder `lib`, preprocess a dataset by running ```python preprocess_gssl.py DATA_NAME```. + To process data_300W_COFW_WFLW, run + ``` + python preprocess_gssl.py data_300W_COFW_WFLW + ``` + To process data_300W_CELEBA, run + ``` + python preprocess_gssl.py CELEBA + ``` + and + ``` + python preprocess_gssl.py data_300W_CELEBA + ``` +3. Back to folder `PIPNet`, edit `run_train.sh` to choose the config file you want. Then, train the model by running: +``` +sh run_train.sh +``` + +## Evaluation +1. Edit `run_test.sh` to choose the config file you want. Then, test the model by running: +``` +sh run_test.sh +``` + +## Citation +```` +@article{JLS21, + title={Pixel-in-Pixel Net: Towards Efficient Facial Landmark Detection in the Wild}, + author={Haibo Jin and Shengcai Liao and Ling Shao}, + journal={International Journal of Computer Vision}, + publisher={Springer Science and Business Media LLC}, + ISSN={1573-1405}, + url={http://dx.doi.org/10.1007/s11263-021-01521-4}, + DOI={10.1007/s11263-021-01521-4}, + year={2021}, + month={Sep} +} +```` + +## Acknowledgement +We thank the following great works: +* [human-pose-estimation.pytorch](https://github.com/microsoft/human-pose-estimation.pytorch) +* [HRNet-Facial-Landmark-Detection](https://github.com/HRNet/HRNet-Facial-Landmark-Detection) diff --git a/third_party/PIPNet/lib/data_utils.py b/third_party/PIPNet/lib/data_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..2ba8e93b20dfcebbdc1895dda6563bd6ee5870bb --- /dev/null +++ b/third_party/PIPNet/lib/data_utils.py @@ -0,0 +1,166 @@ +import torch.utils.data as data +import torch +from PIL import Image, ImageFilter +import os, cv2 +import numpy as np +import random +from scipy.stats import norm +from math import floor + +def random_translate(image, target): + if random.random() > 0.5: + image_height, image_width = image.size + a = 1 + b = 0 + #c = 30 #left/right (i.e. 5/-5) + c = int((random.random()-0.5) * 60) + d = 0 + e = 1 + #f = 30 #up/down (i.e. 5/-5) + f = int((random.random()-0.5) * 60) + image = image.transform(image.size, Image.AFFINE, (a, b, c, d, e, f)) + target_translate = target.copy() + target_translate = target_translate.reshape(-1, 2) + target_translate[:, 0] -= 1.*c/image_width + target_translate[:, 1] -= 1.*f/image_height + target_translate = target_translate.flatten() + target_translate[target_translate < 0] = 0 + target_translate[target_translate > 1] = 1 + return image, target_translate + else: + return image, target + +def random_blur(image): + if random.random() > 0.7: + image = image.filter(ImageFilter.GaussianBlur(random.random()*5)) + return image + +def random_occlusion(image): + if random.random() > 0.5: + image_np = np.array(image).astype(np.uint8) + image_np = image_np[:,:,::-1] + image_height, image_width, _ = image_np.shape + occ_height = int(image_height*0.4*random.random()) + occ_width = int(image_width*0.4*random.random()) + occ_xmin = int((image_width - occ_width - 10) * random.random()) + occ_ymin = int((image_height - occ_height - 10) * random.random()) + image_np[occ_ymin:occ_ymin+occ_height, occ_xmin:occ_xmin+occ_width, 0] = int(random.random() * 255) + image_np[occ_ymin:occ_ymin+occ_height, occ_xmin:occ_xmin+occ_width, 1] = int(random.random() * 255) + image_np[occ_ymin:occ_ymin+occ_height, occ_xmin:occ_xmin+occ_width, 2] = int(random.random() * 255) + image_pil = Image.fromarray(image_np[:,:,::-1].astype('uint8'), 'RGB') + return image_pil + else: + return image + +def random_flip(image, target, points_flip): + if random.random() > 0.5: + image = image.transpose(Image.FLIP_LEFT_RIGHT) + target = np.array(target).reshape(-1, 2) + target = target[points_flip, :] + target[:,0] = 1-target[:,0] + target = target.flatten() + return image, target + else: + return image, target + +def random_rotate(image, target, angle_max): + if random.random() > 0.5: + center_x = 0.5 + center_y = 0.5 + landmark_num= int(len(target) / 2) + target_center = np.array(target) - np.array([center_x, center_y]*landmark_num) + target_center = target_center.reshape(landmark_num, 2) + theta_max = np.radians(angle_max) + theta = random.uniform(-theta_max, theta_max) + angle = np.degrees(theta) + image = image.rotate(angle) + + c, s = np.cos(theta), np.sin(theta) + rot = np.array(((c,-s), (s, c))) + target_center_rot = np.matmul(target_center, rot) + target_rot = target_center_rot.reshape(landmark_num*2) + np.array([center_x, center_y]*landmark_num) + return image, target_rot + else: + return image, target + +def gen_target_pip(target, meanface_indices, target_map, target_local_x, target_local_y, target_nb_x, target_nb_y): + num_nb = len(meanface_indices[0]) + map_channel, map_height, map_width = target_map.shape + target = target.reshape(-1, 2) + assert map_channel == target.shape[0] + + for i in range(map_channel): + mu_x = int(floor(target[i][0] * map_width)) + mu_y = int(floor(target[i][1] * map_height)) + mu_x = max(0, mu_x) + mu_y = max(0, mu_y) + mu_x = min(mu_x, map_width-1) + mu_y = min(mu_y, map_height-1) + target_map[i, mu_y, mu_x] = 1 + shift_x = target[i][0] * map_width - mu_x + shift_y = target[i][1] * map_height - mu_y + target_local_x[i, mu_y, mu_x] = shift_x + target_local_y[i, mu_y, mu_x] = shift_y + + for j in range(num_nb): + nb_x = target[meanface_indices[i][j]][0] * map_width - mu_x + nb_y = target[meanface_indices[i][j]][1] * map_height - mu_y + target_nb_x[num_nb*i+j, mu_y, mu_x] = nb_x + target_nb_y[num_nb*i+j, mu_y, mu_x] = nb_y + + return target_map, target_local_x, target_local_y, target_nb_x, target_nb_y + +class ImageFolder_pip(data.Dataset): + def __init__(self, root, imgs, input_size, num_lms, net_stride, points_flip, meanface_indices, transform=None, target_transform=None): + self.root = root + self.imgs = imgs + self.num_lms = num_lms + self.net_stride = net_stride + self.points_flip = points_flip + self.meanface_indices = meanface_indices + self.num_nb = len(meanface_indices[0]) + self.transform = transform + self.target_transform = target_transform + self.input_size = input_size + + def __getitem__(self, index): + + img_name, target = self.imgs[index] + + img = Image.open(os.path.join(self.root, img_name)).convert('RGB') + img, target = random_translate(img, target) + img = random_occlusion(img) + img, target = random_flip(img, target, self.points_flip) + img, target = random_rotate(img, target, 30) + img = random_blur(img) + + target_map = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + target_local_x = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + target_local_y = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + target_nb_x = np.zeros((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + target_nb_y = np.zeros((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + target_map, target_local_x, target_local_y, target_nb_x, target_nb_y = gen_target_pip(target, self.meanface_indices, target_map, target_local_x, target_local_y, target_nb_x, target_nb_y) + + target_map = torch.from_numpy(target_map).float() + target_local_x = torch.from_numpy(target_local_x).float() + target_local_y = torch.from_numpy(target_local_y).float() + target_nb_x = torch.from_numpy(target_nb_x).float() + target_nb_y = torch.from_numpy(target_nb_y).float() + + if self.transform is not None: + img = self.transform(img) + if self.target_transform is not None: + target_map = self.target_transform(target_map) + target_local_x = self.target_transform(target_local_x) + target_local_y = self.target_transform(target_local_y) + target_nb_x = self.target_transform(target_nb_x) + target_nb_y = self.target_transform(target_nb_y) + + return img, target_map, target_local_x, target_local_y, target_nb_x, target_nb_y + + def __len__(self): + return len(self.imgs) + +if __name__ == '__main__': + pass + diff --git a/third_party/PIPNet/lib/data_utils_gssl.py b/third_party/PIPNet/lib/data_utils_gssl.py new file mode 100644 index 0000000000000000000000000000000000000000..af0d8acb4455a9b3fdfd13fd26e1a6eecb87101d --- /dev/null +++ b/third_party/PIPNet/lib/data_utils_gssl.py @@ -0,0 +1,290 @@ +import torch.utils.data as data +import torch +from PIL import Image, ImageFilter +import os, cv2 +import numpy as np +import random +from scipy.stats import norm +from math import floor + +def random_translate(image, target): + if random.random() > 0.5: + image_height, image_width = image.size + a = 1 + b = 0 + #c = 30 #left/right (i.e. 5/-5) + c = int((random.random()-0.5) * 60) + d = 0 + e = 1 + #f = 30 #up/down (i.e. 5/-5) + f = int((random.random()-0.5) * 60) + image = image.transform(image.size, Image.AFFINE, (a, b, c, d, e, f)) + target_translate = target.copy() + target_translate = target_translate.reshape(-1, 2) + target_translate[:, 0] -= 1.*c/image_width + target_translate[:, 1] -= 1.*f/image_height + target_translate = target_translate.flatten() + target_translate[target_translate < 0] = 0 + target_translate[target_translate > 1] = 1 + return image, target_translate + else: + return image, target + +def random_blur(image): + if random.random() > 0.7: + image = image.filter(ImageFilter.GaussianBlur(random.random()*5)) + return image + +def random_occlusion(image): + if random.random() > 0.5: + image_np = np.array(image).astype(np.uint8) + image_np = image_np[:,:,::-1] + image_height, image_width, _ = image_np.shape + occ_height = int(image_height*0.4*random.random()) + occ_width = int(image_width*0.4*random.random()) + occ_xmin = int((image_width - occ_width - 10) * random.random()) + occ_ymin = int((image_height - occ_height - 10) * random.random()) + image_np[occ_ymin:occ_ymin+occ_height, occ_xmin:occ_xmin+occ_width, 0] = int(random.random() * 255) + image_np[occ_ymin:occ_ymin+occ_height, occ_xmin:occ_xmin+occ_width, 1] = int(random.random() * 255) + image_np[occ_ymin:occ_ymin+occ_height, occ_xmin:occ_xmin+occ_width, 2] = int(random.random() * 255) + image_pil = Image.fromarray(image_np[:,:,::-1].astype('uint8'), 'RGB') + return image_pil + else: + return image + +def random_flip(image, target, points_flip): + if random.random() > 0.5: + image = image.transpose(Image.FLIP_LEFT_RIGHT) + target = np.array(target).reshape(-1, 2) + target = target[points_flip, :] + target[:,0] = 1-target[:,0] + target = target.flatten() + return image, target + else: + return image, target + +def random_rotate(image, target, angle_max): + if random.random() > 0.5: + center_x = 0.5 + center_y = 0.5 + landmark_num= int(len(target) / 2) + target_center = np.array(target) - np.array([center_x, center_y]*landmark_num) + target_center = target_center.reshape(landmark_num, 2) + theta_max = np.radians(angle_max) + theta = random.uniform(-theta_max, theta_max) + angle = np.degrees(theta) + image = image.rotate(angle) + + c, s = np.cos(theta), np.sin(theta) + rot = np.array(((c,-s), (s, c))) + target_center_rot = np.matmul(target_center, rot) + target_rot = target_center_rot.reshape(landmark_num*2) + np.array([center_x, center_y]*landmark_num) + return image, target_rot + else: + return image, target + +def gen_target_pip(target, meanface_indices, target_map1, target_map2, target_map3, target_local_x, target_local_y, target_nb_x, target_nb_y): + num_nb = len(meanface_indices[0]) + map_channel1, map_height1, map_width1 = target_map1.shape + map_channel2, map_height2, map_width2 = target_map2.shape + map_channel3, map_height3, map_width3 = target_map3.shape + target = target.reshape(-1, 2) + assert map_channel1 == target.shape[0] + + for i in range(map_channel1): + mu_x1 = int(floor(target[i][0] * map_width1)) + mu_y1 = int(floor(target[i][1] * map_height1)) + mu_x1 = max(0, mu_x1) + mu_y1 = max(0, mu_y1) + mu_x1 = min(mu_x1, map_width1-1) + mu_y1 = min(mu_y1, map_height1-1) + target_map1[i, mu_y1, mu_x1] = 1 + + shift_x = target[i][0] * map_width1 - mu_x1 + shift_y = target[i][1] * map_height1 - mu_y1 + target_local_x[i, mu_y1, mu_x1] = shift_x + target_local_y[i, mu_y1, mu_x1] = shift_y + + for j in range(num_nb): + nb_x = target[meanface_indices[i][j]][0] * map_width1 - mu_x1 + nb_y = target[meanface_indices[i][j]][1] * map_height1 - mu_y1 + target_nb_x[num_nb*i+j, mu_y1, mu_x1] = nb_x + target_nb_y[num_nb*i+j, mu_y1, mu_x1] = nb_y + + mu_x2 = int(floor(target[i][0] * map_width2)) + mu_y2 = int(floor(target[i][1] * map_height2)) + mu_x2 = max(0, mu_x2) + mu_y2 = max(0, mu_y2) + mu_x2 = min(mu_x2, map_width2-1) + mu_y2 = min(mu_y2, map_height2-1) + target_map2[i, mu_y2, mu_x2] = 1 + + mu_x3 = int(floor(target[i][0] * map_width3)) + mu_y3 = int(floor(target[i][1] * map_height3)) + mu_x3 = max(0, mu_x3) + mu_y3 = max(0, mu_y3) + mu_x3 = min(mu_x3, map_width3-1) + mu_y3 = min(mu_y3, map_height3-1) + target_map3[i, mu_y3, mu_x3] = 1 + + return target_map1, target_map2, target_map3, target_local_x, target_local_y, target_nb_x, target_nb_y + +def gen_target_pip_cls1(target, target_map1): + map_channel1, map_height1, map_width1 = target_map1.shape + target = target.reshape(-1, 2) + assert map_channel1 == target.shape[0] + + for i in range(map_channel1): + mu_x1 = int(floor(target[i][0] * map_width1)) + mu_y1 = int(floor(target[i][1] * map_height1)) + mu_x1 = max(0, mu_x1) + mu_y1 = max(0, mu_y1) + mu_x1 = min(mu_x1, map_width1-1) + mu_y1 = min(mu_y1, map_height1-1) + target_map1[i, mu_y1, mu_x1] = 1 + + return target_map1 + +def gen_target_pip_cls2(target, target_map2): + map_channel2, map_height2, map_width2 = target_map2.shape + target = target.reshape(-1, 2) + assert map_channel2 == target.shape[0] + + for i in range(map_channel2): + mu_x2 = int(floor(target[i][0] * map_width2)) + mu_y2 = int(floor(target[i][1] * map_height2)) + mu_x2 = max(0, mu_x2) + mu_y2 = max(0, mu_y2) + mu_x2 = min(mu_x2, map_width2-1) + mu_y2 = min(mu_y2, map_height2-1) + target_map2[i, mu_y2, mu_x2] = 1 + + return target_map2 + +def gen_target_pip_cls3(target, target_map3): + map_channel3, map_height3, map_width3 = target_map3.shape + target = target.reshape(-1, 2) + assert map_channel3 == target.shape[0] + + for i in range(map_channel3): + mu_x3 = int(floor(target[i][0] * map_width3)) + mu_y3 = int(floor(target[i][1] * map_height3)) + mu_x3 = max(0, mu_x3) + mu_y3 = max(0, mu_y3) + mu_x3 = min(mu_x3, map_width3-1) + mu_y3 = min(mu_y3, map_height3-1) + target_map3[i, mu_y3, mu_x3] = 1 + + return target_map3 + +class ImageFolder_pip(data.Dataset): + def __init__(self, root, imgs, input_size, num_lms, net_stride, points_flip, meanface_indices, transform=None, target_transform=None): + self.root = root + self.imgs = imgs + self.num_lms = num_lms + self.net_stride = net_stride + self.points_flip = points_flip + self.meanface_indices = meanface_indices + self.num_nb = len(meanface_indices[0]) + self.transform = transform + self.target_transform = target_transform + self.input_size = input_size + + def __getitem__(self, index): + """ + Args: + index (int): Index + Returns: + tuple: (image, target) where target is class_index of the target class. + """ + img_name, target_type, target = self.imgs[index] + img = Image.open(os.path.join(self.root, img_name)).convert('RGB') + + img, target = random_translate(img, target) + img = random_occlusion(img) + img, target = random_flip(img, target, self.points_flip) + img, target = random_rotate(img, target, 30) + img = random_blur(img) + + target_map1 = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + target_map2 = np.zeros((self.num_lms, int(self.input_size/self.net_stride/2), int(self.input_size/self.net_stride/2))) + target_map3 = np.zeros((self.num_lms, int(self.input_size/self.net_stride/4), int(self.input_size/self.net_stride/4))) + target_local_x = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + target_local_y = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + target_nb_x = np.zeros((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + target_nb_y = np.zeros((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + + mask_map1 = np.ones((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_map2 = np.ones((self.num_lms, int(self.input_size/self.net_stride/2), int(self.input_size/self.net_stride/2))) + mask_map3 = np.ones((self.num_lms, int(self.input_size/self.net_stride/4), int(self.input_size/self.net_stride/4))) + mask_local_x = np.ones((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_local_y = np.ones((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_nb_x = np.ones((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_nb_y = np.ones((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + + if target_type == 'std': + target_map1, target_map2, target_map3, target_local_x, target_local_y, target_nb_x, target_nb_y = gen_target_pip(target, self.meanface_indices, target_map1, target_map2, target_map3, target_local_x, target_local_y, target_nb_x, target_nb_y) + mask_map2 = np.zeros((self.num_lms, int(self.input_size/self.net_stride/2), int(self.input_size/self.net_stride/2))) + mask_map3 = np.zeros((self.num_lms, int(self.input_size/self.net_stride/4), int(self.input_size/self.net_stride/4))) + elif target_type == 'cls1': + target_map1 = gen_target_pip_cls1(target, target_map1) + mask_map2 = np.zeros((self.num_lms, int(self.input_size/self.net_stride/2), int(self.input_size/self.net_stride/2))) + mask_map3 = np.zeros((self.num_lms, int(self.input_size/self.net_stride/4), int(self.input_size/self.net_stride/4))) + mask_local_x = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_local_y = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_nb_x = np.zeros((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_nb_y = np.zeros((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + elif target_type == 'cls2': + target_map2 = gen_target_pip_cls2(target, target_map2) + mask_map1 = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_map3 = np.zeros((self.num_lms, int(self.input_size/self.net_stride/4), int(self.input_size/self.net_stride/4))) + mask_local_x = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_local_y = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_nb_x = np.zeros((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_nb_y = np.zeros((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + elif target_type == 'cls3': + target_map3 = gen_target_pip_cls3(target, target_map3) + mask_map1 = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_map2 = np.zeros((self.num_lms, int(self.input_size/self.net_stride/2), int(self.input_size/self.net_stride/2))) + mask_local_x = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_local_y = np.zeros((self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_nb_x = np.zeros((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + mask_nb_y = np.zeros((self.num_nb*self.num_lms, int(self.input_size/self.net_stride), int(self.input_size/self.net_stride))) + else: + print('No such target type!') + exit(0) + + target_map1 = torch.from_numpy(target_map1).float() + target_map2 = torch.from_numpy(target_map2).float() + target_map3 = torch.from_numpy(target_map3).float() + target_local_x = torch.from_numpy(target_local_x).float() + target_local_y = torch.from_numpy(target_local_y).float() + target_nb_x = torch.from_numpy(target_nb_x).float() + target_nb_y = torch.from_numpy(target_nb_y).float() + mask_map1 = torch.from_numpy(mask_map1).float() + mask_map2 = torch.from_numpy(mask_map2).float() + mask_map3 = torch.from_numpy(mask_map3).float() + mask_local_x = torch.from_numpy(mask_local_x).float() + mask_local_y = torch.from_numpy(mask_local_y).float() + mask_nb_x = torch.from_numpy(mask_nb_x).float() + mask_nb_y = torch.from_numpy(mask_nb_y).float() + + if self.transform is not None: + img = self.transform(img) + if self.target_transform is not None: + target_map1 = self.target_transform(target_map1) + target_map2 = self.target_transform(target_map2) + target_map3 = self.target_transform(target_map3) + target_local_x = self.target_transform(target_local_x) + target_local_y = self.target_transform(target_local_y) + target_nb_x = self.target_transform(target_nb_x) + target_nb_y = self.target_transform(target_nb_y) + + return img, target_map1, target_map2, target_map3, target_local_x, target_local_y, target_nb_x, target_nb_y, mask_map1, mask_map2, mask_map3, mask_local_x, mask_local_y, mask_nb_x, mask_nb_y + + def __len__(self): + return len(self.imgs) + +if __name__ == '__main__': + pass + diff --git a/third_party/PIPNet/lib/demo.py b/third_party/PIPNet/lib/demo.py new file mode 100644 index 0000000000000000000000000000000000000000..49847caaca06a3558d755bdccf0615579d6f935c --- /dev/null +++ b/third_party/PIPNet/lib/demo.py @@ -0,0 +1,159 @@ +import cv2 +import sys + +sys.path.insert(0, "FaceBoxesV2") +sys.path.insert(0, "..") +from math import floor +from faceboxes_detector import * + +import torch +import torch.nn.parallel +import torch.utils.data +import torchvision.transforms as transforms +import torchvision.models as models + +from networks import * +from functions import * +from PIPNet.reverse_index import ri1, ri2 + + +class Config: + def __init__(self): + self.det_head = "pip" + self.net_stride = 32 + self.batch_size = 16 + self.init_lr = 0.0001 + self.num_epochs = 60 + self.decay_steps = [30, 50] + self.input_size = 256 + self.backbone = "resnet101" + self.pretrained = True + self.criterion_cls = "l2" + self.criterion_reg = "l1" + self.cls_loss_weight = 10 + self.reg_loss_weight = 1 + self.num_lms = 98 + self.save_interval = self.num_epochs + self.num_nb = 10 + self.use_gpu = True + self.gpu_id = 3 + + +def get_lmk_model(): + + cfg = Config() + + resnet101 = models.resnet101(pretrained=cfg.pretrained) + net = Pip_resnet101( + resnet101, + cfg.num_nb, + num_lms=cfg.num_lms, + input_size=cfg.input_size, + net_stride=cfg.net_stride, + ) + + if cfg.use_gpu: + device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") + else: + device = torch.device("cpu") + net = net.to(device) + + weight_file = "/apdcephfs/share_1290939/ahbanliang/codes/PIPNet/snapshots/WFLW/pip_32_16_60_r101_l2_l1_10_1_nb10/epoch59.pth" + state_dict = torch.load(weight_file, map_location=device) + net.load_state_dict(state_dict) + + detector = FaceBoxesDetector( + "FaceBoxes", + "FaceBoxesV2/weights/FaceBoxesV2.pth", + use_gpu=True, + device="cuda:0", + ) + return net, detector + + +def demo_image( + image_file, + net, + detector, + input_size=256, + net_stride=32, + num_nb=10, + use_gpu=True, + device="cuda:0", +): + + my_thresh = 0.6 + det_box_scale = 1.2 + net.eval() + preprocess = transforms.Compose( + [ + transforms.Resize((256, 256)), + transforms.ToTensor(), + transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), + ] + ) + reverse_index1, reverse_index2, max_len = ri1, ri2, 17 + image = cv2.imread(image_file) + image_height, image_width, _ = image.shape + detections, _ = detector.detect(image, my_thresh, 1) + for i in range(len(detections)): + det_xmin = detections[i][2] + det_ymin = detections[i][3] + det_width = detections[i][4] + det_height = detections[i][5] + det_xmax = det_xmin + det_width - 1 + det_ymax = det_ymin + det_height - 1 + + det_xmin -= int(det_width * (det_box_scale - 1) / 2) + # remove a part of top area for alignment, see paper for details + det_ymin += int(det_height * (det_box_scale - 1) / 2) + det_xmax += int(det_width * (det_box_scale - 1) / 2) + det_ymax += int(det_height * (det_box_scale - 1) / 2) + det_xmin = max(det_xmin, 0) + det_ymin = max(det_ymin, 0) + det_xmax = min(det_xmax, image_width - 1) + det_ymax = min(det_ymax, image_height - 1) + det_width = det_xmax - det_xmin + 1 + det_height = det_ymax - det_ymin + 1 + cv2.rectangle(image, (det_xmin, det_ymin), (det_xmax, det_ymax), (0, 0, 255), 2) + det_crop = image[det_ymin:det_ymax, det_xmin:det_xmax, :] + det_crop = cv2.resize(det_crop, (input_size, input_size)) + inputs = Image.fromarray(det_crop[:, :, ::-1].astype("uint8"), "RGB") + inputs = preprocess(inputs).unsqueeze(0) + inputs = inputs.to(device) + ( + lms_pred_x, + lms_pred_y, + lms_pred_nb_x, + lms_pred_nb_y, + outputs_cls, + max_cls, + ) = forward_pip(net, inputs, preprocess, input_size, net_stride, num_nb) + lms_pred = torch.cat((lms_pred_x, lms_pred_y), dim=1).flatten() + tmp_nb_x = lms_pred_nb_x[reverse_index1, reverse_index2].view(98, max_len) + tmp_nb_y = lms_pred_nb_y[reverse_index1, reverse_index2].view(98, max_len) + tmp_x = torch.mean(torch.cat((lms_pred_x, tmp_nb_x), dim=1), dim=1).view(-1, 1) + tmp_y = torch.mean(torch.cat((lms_pred_y, tmp_nb_y), dim=1), dim=1).view(-1, 1) + lms_pred_merge = torch.cat((tmp_x, tmp_y), dim=1).flatten() + lms_pred = lms_pred.cpu().numpy() + lms_pred_merge = lms_pred_merge.cpu().numpy() + for i in range(98): + x_pred = lms_pred_merge[i * 2] * det_width + y_pred = lms_pred_merge[i * 2 + 1] * det_height + cv2.circle( + image, + (int(x_pred) + det_xmin, int(y_pred) + det_ymin), + 1, + (0, 0, 255), + 2, + ) + cv2.imwrite("images/1_out.jpg", image) + + +if __name__ == "__main__": + net, detector = get_lmk_model() + demo_image( + "/apdcephfs/private_ahbanliang/codes/Real-ESRGAN-master/tmp_frames/yanikefu/frame00000046.png", + net, + detector, + ) diff --git a/third_party/PIPNet/lib/demo_video.py b/third_party/PIPNet/lib/demo_video.py new file mode 100644 index 0000000000000000000000000000000000000000..519e251c8ef084d630dc1521d70f84a1da919d32 --- /dev/null +++ b/third_party/PIPNet/lib/demo_video.py @@ -0,0 +1,141 @@ +import cv2, os +import sys +sys.path.insert(0, 'FaceBoxesV2') +sys.path.insert(0, '..') +import numpy as np +import pickle +import importlib +from math import floor +from faceboxes_detector import * +import time + +import torch +import torch.nn as nn +import torch.nn.parallel +import torch.optim as optim +import torch.utils.data +import torch.nn.functional as F +import torchvision.transforms as transforms +import torchvision.datasets as datasets +import torchvision.models as models + +from networks import * +import data_utils +from functions import * + +if not len(sys.argv) == 3: + print('Format:') + print('python lib/demo_video.py config_file video_file') + exit(0) +experiment_name = sys.argv[1].split('/')[-1][:-3] +data_name = sys.argv[1].split('/')[-2] +config_path = '.experiments.{}.{}'.format(data_name, experiment_name) +video_file = sys.argv[2] + +my_config = importlib.import_module(config_path, package='PIPNet') +Config = getattr(my_config, 'Config') +cfg = Config() +cfg.experiment_name = experiment_name +cfg.data_name = data_name + +save_dir = os.path.join('./snapshots', cfg.data_name, cfg.experiment_name) + +meanface_indices, reverse_index1, reverse_index2, max_len = get_meanface(os.path.join('data', cfg.data_name, 'meanface.txt'), cfg.num_nb) + +if cfg.backbone == 'resnet18': + resnet18 = models.resnet18(pretrained=cfg.pretrained) + net = Pip_resnet18(resnet18, cfg.num_nb, num_lms=cfg.num_lms, input_size=cfg.input_size, net_stride=cfg.net_stride) +elif cfg.backbone == 'resnet50': + resnet50 = models.resnet50(pretrained=cfg.pretrained) + net = Pip_resnet50(resnet50, cfg.num_nb, num_lms=cfg.num_lms, input_size=cfg.input_size, net_stride=cfg.net_stride) +elif cfg.backbone == 'resnet101': + resnet101 = models.resnet101(pretrained=cfg.pretrained) + net = Pip_resnet101(resnet101, cfg.num_nb, num_lms=cfg.num_lms, input_size=cfg.input_size, net_stride=cfg.net_stride) +else: + print('No such backbone!') + exit(0) + +if cfg.use_gpu: + device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") +else: + device = torch.device("cpu") +net = net.to(device) + +weight_file = os.path.join(save_dir, 'epoch%d.pth' % (cfg.num_epochs-1)) +state_dict = torch.load(weight_file, map_location=device) +net.load_state_dict(state_dict) + +normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], + std=[0.229, 0.224, 0.225]) +preprocess = transforms.Compose([transforms.Resize((cfg.input_size, cfg.input_size)), transforms.ToTensor(), normalize]) + +def demo_video(video_file, net, preprocess, input_size, net_stride, num_nb, use_gpu, device): + detector = FaceBoxesDetector('FaceBoxes', 'FaceBoxesV2/weights/FaceBoxesV2.pth', use_gpu, device) + my_thresh = 0.9 + det_box_scale = 1.2 + + net.eval() + if video_file == 'camera': + cap = cv2.VideoCapture(0) + else: + cap = cv2.VideoCapture(video_file) + if (cap.isOpened()== False): + print("Error opening video stream or file") + frame_width = int(cap.get(3)) + frame_height = int(cap.get(4)) + count = 0 + while(cap.isOpened()): + ret, frame = cap.read() + if ret == True: + detections, _ = detector.detect(frame, my_thresh, 1) + for i in range(len(detections)): + det_xmin = detections[i][2] + det_ymin = detections[i][3] + det_width = detections[i][4] + det_height = detections[i][5] + det_xmax = det_xmin + det_width - 1 + det_ymax = det_ymin + det_height - 1 + + det_xmin -= int(det_width * (det_box_scale-1)/2) + # remove a part of top area for alignment, see paper for details + det_ymin += int(det_height * (det_box_scale-1)/2) + det_xmax += int(det_width * (det_box_scale-1)/2) + det_ymax += int(det_height * (det_box_scale-1)/2) + det_xmin = max(det_xmin, 0) + det_ymin = max(det_ymin, 0) + det_xmax = min(det_xmax, frame_width-1) + det_ymax = min(det_ymax, frame_height-1) + det_width = det_xmax - det_xmin + 1 + det_height = det_ymax - det_ymin + 1 + cv2.rectangle(frame, (det_xmin, det_ymin), (det_xmax, det_ymax), (0, 0, 255), 2) + det_crop = frame[det_ymin:det_ymax, det_xmin:det_xmax, :] + det_crop = cv2.resize(det_crop, (input_size, input_size)) + inputs = Image.fromarray(det_crop[:,:,::-1].astype('uint8'), 'RGB') + inputs = preprocess(inputs).unsqueeze(0) + inputs = inputs.to(device) + lms_pred_x, lms_pred_y, lms_pred_nb_x, lms_pred_nb_y, outputs_cls, max_cls = forward_pip(net, inputs, preprocess, input_size, net_stride, num_nb) + lms_pred = torch.cat((lms_pred_x, lms_pred_y), dim=1).flatten() + tmp_nb_x = lms_pred_nb_x[reverse_index1, reverse_index2].view(cfg.num_lms, max_len) + tmp_nb_y = lms_pred_nb_y[reverse_index1, reverse_index2].view(cfg.num_lms, max_len) + tmp_x = torch.mean(torch.cat((lms_pred_x, tmp_nb_x), dim=1), dim=1).view(-1,1) + tmp_y = torch.mean(torch.cat((lms_pred_y, tmp_nb_y), dim=1), dim=1).view(-1,1) + lms_pred_merge = torch.cat((tmp_x, tmp_y), dim=1).flatten() + lms_pred = lms_pred.cpu().numpy() + lms_pred_merge = lms_pred_merge.cpu().numpy() + for i in range(cfg.num_lms): + x_pred = lms_pred_merge[i*2] * det_width + y_pred = lms_pred_merge[i*2+1] * det_height + cv2.circle(frame, (int(x_pred)+det_xmin, int(y_pred)+det_ymin), 1, (0, 0, 255), 2) + + count += 1 + #cv2.imwrite('video_out2/'+str(count)+'.jpg', frame) + cv2.imshow('1', frame) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + else: + break + + cap.release() + cv2.destroyAllWindows() + +demo_video(video_file, net, preprocess, cfg.input_size, cfg.net_stride, cfg.num_nb, cfg.use_gpu, device) diff --git a/third_party/PIPNet/lib/functions.py b/third_party/PIPNet/lib/functions.py new file mode 100644 index 0000000000000000000000000000000000000000..6144d3a0977e159075e2f2522314e5f26ac5553d --- /dev/null +++ b/third_party/PIPNet/lib/functions.py @@ -0,0 +1,210 @@ +import os, cv2 +import numpy as np +from PIL import Image, ImageFilter +import logging +import torch +import torch.nn as nn +import random +import time +from scipy.integrate import simps + + +def get_label(data_name, label_file, task_type=None): + label_path = os.path.join('data', data_name, label_file) + with open(label_path, 'r') as f: + labels = f.readlines() + labels = [x.strip().split() for x in labels] + if len(labels[0])==1: + return labels + + labels_new = [] + for label in labels: + image_name = label[0] + target = label[1:] + target = np.array([float(x) for x in target]) + if task_type is None: + labels_new.append([image_name, target]) + else: + labels_new.append([image_name, task_type, target]) + return labels_new + +def get_meanface(meanface_file, num_nb): + with open(meanface_file) as f: + meanface = f.readlines()[0] + + meanface = meanface.strip().split() + meanface = [float(x) for x in meanface] + meanface = np.array(meanface).reshape(-1, 2) + # each landmark predicts num_nb neighbors + meanface_indices = [] + for i in range(meanface.shape[0]): + pt = meanface[i,:] + dists = np.sum(np.power(pt-meanface, 2), axis=1) + indices = np.argsort(dists) + meanface_indices.append(indices[1:1+num_nb]) + + # each landmark predicted by X neighbors, X varies + meanface_indices_reversed = {} + for i in range(meanface.shape[0]): + meanface_indices_reversed[i] = [[],[]] + for i in range(meanface.shape[0]): + for j in range(num_nb): + meanface_indices_reversed[meanface_indices[i][j]][0].append(i) + meanface_indices_reversed[meanface_indices[i][j]][1].append(j) + + max_len = 0 + for i in range(meanface.shape[0]): + tmp_len = len(meanface_indices_reversed[i][0]) + if tmp_len > max_len: + max_len = tmp_len + + # tricks, make them have equal length for efficient computation + for i in range(meanface.shape[0]): + tmp_len = len(meanface_indices_reversed[i][0]) + meanface_indices_reversed[i][0] += meanface_indices_reversed[i][0]*10 + meanface_indices_reversed[i][1] += meanface_indices_reversed[i][1]*10 + meanface_indices_reversed[i][0] = meanface_indices_reversed[i][0][:max_len] + meanface_indices_reversed[i][1] = meanface_indices_reversed[i][1][:max_len] + + # make the indices 1-dim + reverse_index1 = [] + reverse_index2 = [] + for i in range(meanface.shape[0]): + reverse_index1 += meanface_indices_reversed[i][0] + reverse_index2 += meanface_indices_reversed[i][1] + return meanface_indices, reverse_index1, reverse_index2, max_len + +def compute_loss_pip(outputs_map, outputs_local_x, outputs_local_y, outputs_nb_x, outputs_nb_y, labels_map, labels_local_x, labels_local_y, labels_nb_x, labels_nb_y, criterion_cls, criterion_reg, num_nb): + + tmp_batch, tmp_channel, tmp_height, tmp_width = outputs_map.size() + labels_map = labels_map.view(tmp_batch*tmp_channel, -1) + labels_max_ids = torch.argmax(labels_map, 1) + labels_max_ids = labels_max_ids.view(-1, 1) + labels_max_ids_nb = labels_max_ids.repeat(1, num_nb).view(-1, 1) + + outputs_local_x = outputs_local_x.view(tmp_batch*tmp_channel, -1) + outputs_local_x_select = torch.gather(outputs_local_x, 1, labels_max_ids) + outputs_local_y = outputs_local_y.view(tmp_batch*tmp_channel, -1) + outputs_local_y_select = torch.gather(outputs_local_y, 1, labels_max_ids) + outputs_nb_x = outputs_nb_x.view(tmp_batch*num_nb*tmp_channel, -1) + outputs_nb_x_select = torch.gather(outputs_nb_x, 1, labels_max_ids_nb) + outputs_nb_y = outputs_nb_y.view(tmp_batch*num_nb*tmp_channel, -1) + outputs_nb_y_select = torch.gather(outputs_nb_y, 1, labels_max_ids_nb) + + labels_local_x = labels_local_x.view(tmp_batch*tmp_channel, -1) + labels_local_x_select = torch.gather(labels_local_x, 1, labels_max_ids) + labels_local_y = labels_local_y.view(tmp_batch*tmp_channel, -1) + labels_local_y_select = torch.gather(labels_local_y, 1, labels_max_ids) + labels_nb_x = labels_nb_x.view(tmp_batch*num_nb*tmp_channel, -1) + labels_nb_x_select = torch.gather(labels_nb_x, 1, labels_max_ids_nb) + labels_nb_y = labels_nb_y.view(tmp_batch*num_nb*tmp_channel, -1) + labels_nb_y_select = torch.gather(labels_nb_y, 1, labels_max_ids_nb) + + labels_map = labels_map.view(tmp_batch, tmp_channel, tmp_height, tmp_width) + loss_map = criterion_cls(outputs_map, labels_map) + loss_x = criterion_reg(outputs_local_x_select, labels_local_x_select) + loss_y = criterion_reg(outputs_local_y_select, labels_local_y_select) + loss_nb_x = criterion_reg(outputs_nb_x_select, labels_nb_x_select) + loss_nb_y = criterion_reg(outputs_nb_y_select, labels_nb_y_select) + return loss_map, loss_x, loss_y, loss_nb_x, loss_nb_y + +def train_model(det_head, net, train_loader, criterion_cls, criterion_reg, cls_loss_weight, reg_loss_weight, num_nb, optimizer, num_epochs, scheduler, save_dir, save_interval, device): + for epoch in range(num_epochs): + print('Epoch {}/{}'.format(epoch, num_epochs - 1)) + logging.info('Epoch {}/{}'.format(epoch, num_epochs - 1)) + print('-' * 10) + logging.info('-' * 10) + net.train() + epoch_loss = 0.0 + + for i, data in enumerate(train_loader): + if det_head == 'pip': + inputs, labels_map, labels_x, labels_y, labels_nb_x, labels_nb_y = data + inputs = inputs.to(device) + labels_map = labels_map.to(device) + labels_x = labels_x.to(device) + labels_y = labels_y.to(device) + labels_nb_x = labels_nb_x.to(device) + labels_nb_y = labels_nb_y.to(device) + outputs_map, outputs_x, outputs_y, outputs_nb_x, outputs_nb_y = net(inputs) + loss_map, loss_x, loss_y, loss_nb_x, loss_nb_y = compute_loss_pip(outputs_map, outputs_x, outputs_y, outputs_nb_x, outputs_nb_y, labels_map, labels_x, labels_y, labels_nb_x, labels_nb_y, criterion_cls, criterion_reg, num_nb) + loss = cls_loss_weight*loss_map + reg_loss_weight*loss_x + reg_loss_weight*loss_y + reg_loss_weight*loss_nb_x + reg_loss_weight*loss_nb_y + else: + print('No such head:', det_head) + exit(0) + + optimizer.zero_grad() + loss.backward() + optimizer.step() + if i%10 == 0: + if det_head == 'pip': + print('[Epoch {:d}/{:d}, Batch {:d}/{:d}] '.format( + epoch, num_epochs-1, i, len(train_loader)-1, loss.item(), cls_loss_weight*loss_map.item(), reg_loss_weight*loss_x.item(), reg_loss_weight*loss_y.item(), reg_loss_weight*loss_nb_x.item(), reg_loss_weight*loss_nb_y.item())) + logging.info('[Epoch {:d}/{:d}, Batch {:d}/{:d}] '.format( + epoch, num_epochs-1, i, len(train_loader)-1, loss.item(), cls_loss_weight*loss_map.item(), reg_loss_weight*loss_x.item(), reg_loss_weight*loss_y.item(), reg_loss_weight*loss_nb_x.item(), reg_loss_weight*loss_nb_y.item())) + else: + print('No such head:', det_head) + exit(0) + epoch_loss += loss.item() + epoch_loss /= len(train_loader) + if epoch%(save_interval-1) == 0 and epoch > 0: + filename = os.path.join(save_dir, 'epoch%d.pth' % epoch) + torch.save(net.state_dict(), filename) + print(filename, 'saved') + scheduler.step() + return net + +def forward_pip(net, inputs, preprocess, input_size, net_stride, num_nb): + net.eval() + with torch.no_grad(): + outputs_cls, outputs_x, outputs_y, outputs_nb_x, outputs_nb_y = net(inputs) + tmp_batch, tmp_channel, tmp_height, tmp_width = outputs_cls.size() + assert tmp_batch == 1 + + outputs_cls = outputs_cls.view(tmp_batch*tmp_channel, -1) + max_ids = torch.argmax(outputs_cls, 1) + max_cls = torch.max(outputs_cls, 1)[0] + max_ids = max_ids.view(-1, 1) + max_ids_nb = max_ids.repeat(1, num_nb).view(-1, 1) + + outputs_x = outputs_x.view(tmp_batch*tmp_channel, -1) + outputs_x_select = torch.gather(outputs_x, 1, max_ids) + outputs_x_select = outputs_x_select.squeeze(1) + outputs_y = outputs_y.view(tmp_batch*tmp_channel, -1) + outputs_y_select = torch.gather(outputs_y, 1, max_ids) + outputs_y_select = outputs_y_select.squeeze(1) + + outputs_nb_x = outputs_nb_x.view(tmp_batch*num_nb*tmp_channel, -1) + outputs_nb_x_select = torch.gather(outputs_nb_x, 1, max_ids_nb) + outputs_nb_x_select = outputs_nb_x_select.squeeze(1).view(-1, num_nb) + outputs_nb_y = outputs_nb_y.view(tmp_batch*num_nb*tmp_channel, -1) + outputs_nb_y_select = torch.gather(outputs_nb_y, 1, max_ids_nb) + outputs_nb_y_select = outputs_nb_y_select.squeeze(1).view(-1, num_nb) + + tmp_x = (max_ids%tmp_width).view(-1,1).float()+outputs_x_select.view(-1,1) + tmp_y = (max_ids//tmp_width).view(-1,1).float()+outputs_y_select.view(-1,1) + tmp_x /= 1.0 * input_size / net_stride + tmp_y /= 1.0 * input_size / net_stride + + tmp_nb_x = (max_ids%tmp_width).view(-1,1).float()+outputs_nb_x_select + tmp_nb_y = (max_ids//tmp_width).view(-1,1).float()+outputs_nb_y_select + tmp_nb_x = tmp_nb_x.view(-1, num_nb) + tmp_nb_y = tmp_nb_y.view(-1, num_nb) + tmp_nb_x /= 1.0 * input_size / net_stride + tmp_nb_y /= 1.0 * input_size / net_stride + + return tmp_x, tmp_y, tmp_nb_x, tmp_nb_y, outputs_cls, max_cls + +def compute_nme(lms_pred, lms_gt, norm): + lms_pred = lms_pred.reshape((-1, 2)) + lms_gt = lms_gt.reshape((-1, 2)) + nme = np.mean(np.linalg.norm(lms_pred - lms_gt, axis=1)) / norm + return nme + +def compute_fr_and_auc(nmes, thres=0.1, step=0.0001): + num_data = len(nmes) + xs = np.arange(0, thres + step, step) + ys = np.array([np.count_nonzero(nmes <= x) for x in xs]) / float(num_data) + fr = 1.0 - ys[-1] + auc = simps(ys, x=xs) / thres + return fr, auc diff --git a/third_party/PIPNet/lib/functions_gssl.py b/third_party/PIPNet/lib/functions_gssl.py new file mode 100644 index 0000000000000000000000000000000000000000..a89dcd936af289860ce3ca4c43339901debf05b3 --- /dev/null +++ b/third_party/PIPNet/lib/functions_gssl.py @@ -0,0 +1,241 @@ +import os, cv2 +import numpy as np +from PIL import Image, ImageFilter +import logging +import torch +import torch.nn as nn +import random + +def get_label(data_name, label_file, task_type=None): + label_path = os.path.join('data', data_name, label_file) + with open(label_path, 'r') as f: + labels = f.readlines() + labels = [x.strip().split() for x in labels] + if len(labels[0])==1: + return labels + + labels_new = [] + for label in labels: + image_name = label[0] + target = label[1:] + target = np.array([float(x) for x in target]) + if task_type is None: + labels_new.append([image_name, target]) + else: + labels_new.append([image_name, task_type, target]) + return labels_new + +def get_meanface(meanface_file, num_nb): + with open(meanface_file) as f: + meanface = f.readlines()[0] + + meanface = meanface.strip().split() + meanface = [float(x) for x in meanface] + meanface = np.array(meanface).reshape(-1, 2) + # each landmark predicts num_nb neighbors + meanface_indices = [] + for i in range(meanface.shape[0]): + pt = meanface[i,:] + dists = np.sum(np.power(pt-meanface, 2), axis=1) + indices = np.argsort(dists) + meanface_indices.append(indices[1:1+num_nb]) + + # each landmark predicted by X neighbors, X varies + meanface_indices_reversed = {} + for i in range(meanface.shape[0]): + meanface_indices_reversed[i] = [[],[]] + for i in range(meanface.shape[0]): + for j in range(num_nb): + meanface_indices_reversed[meanface_indices[i][j]][0].append(i) + meanface_indices_reversed[meanface_indices[i][j]][1].append(j) + + max_len = 0 + for i in range(meanface.shape[0]): + tmp_len = len(meanface_indices_reversed[i][0]) + if tmp_len > max_len: + max_len = tmp_len + + # tricks, make them have equal length for efficient computation + for i in range(meanface.shape[0]): + tmp_len = len(meanface_indices_reversed[i][0]) + meanface_indices_reversed[i][0] += meanface_indices_reversed[i][0]*10 + meanface_indices_reversed[i][1] += meanface_indices_reversed[i][1]*10 + meanface_indices_reversed[i][0] = meanface_indices_reversed[i][0][:max_len] + meanface_indices_reversed[i][1] = meanface_indices_reversed[i][1][:max_len] + + # make the indices 1-dim + reverse_index1 = [] + reverse_index2 = [] + for i in range(meanface.shape[0]): + reverse_index1 += meanface_indices_reversed[i][0] + reverse_index2 += meanface_indices_reversed[i][1] + return meanface_indices, reverse_index1, reverse_index2, max_len + +def compute_loss_pip(outputs_map1, outputs_map2, outputs_map3, outputs_local_x, outputs_local_y, outputs_nb_x, outputs_nb_y, labels_map1, labels_map2, labels_map3, labels_local_x, labels_local_y, labels_nb_x, labels_nb_y, masks_map1, masks_map2, masks_map3, masks_local_x, masks_local_y, masks_nb_x, masks_nb_y, criterion_cls, criterion_reg, num_nb): + + tmp_batch, tmp_channel, tmp_height, tmp_width = outputs_map1.size() + labels_map1 = labels_map1.view(tmp_batch*tmp_channel, -1) + labels_max_ids = torch.argmax(labels_map1, 1) + labels_max_ids = labels_max_ids.view(-1, 1) + labels_max_ids_nb = labels_max_ids.repeat(1, num_nb).view(-1, 1) + + outputs_local_x = outputs_local_x.view(tmp_batch*tmp_channel, -1) + outputs_local_x_select = torch.gather(outputs_local_x, 1, labels_max_ids) + outputs_local_y = outputs_local_y.view(tmp_batch*tmp_channel, -1) + outputs_local_y_select = torch.gather(outputs_local_y, 1, labels_max_ids) + outputs_nb_x = outputs_nb_x.view(tmp_batch*num_nb*tmp_channel, -1) + outputs_nb_x_select = torch.gather(outputs_nb_x, 1, labels_max_ids_nb) + outputs_nb_y = outputs_nb_y.view(tmp_batch*num_nb*tmp_channel, -1) + outputs_nb_y_select = torch.gather(outputs_nb_y, 1, labels_max_ids_nb) + + labels_local_x = labels_local_x.view(tmp_batch*tmp_channel, -1) + labels_local_x_select = torch.gather(labels_local_x, 1, labels_max_ids) + labels_local_y = labels_local_y.view(tmp_batch*tmp_channel, -1) + labels_local_y_select = torch.gather(labels_local_y, 1, labels_max_ids) + labels_nb_x = labels_nb_x.view(tmp_batch*num_nb*tmp_channel, -1) + labels_nb_x_select = torch.gather(labels_nb_x, 1, labels_max_ids_nb) + labels_nb_y = labels_nb_y.view(tmp_batch*num_nb*tmp_channel, -1) + labels_nb_y_select = torch.gather(labels_nb_y, 1, labels_max_ids_nb) + + masks_local_x = masks_local_x.view(tmp_batch*tmp_channel, -1) + masks_local_x_select = torch.gather(masks_local_x, 1, labels_max_ids) + masks_local_y = masks_local_y.view(tmp_batch*tmp_channel, -1) + masks_local_y_select = torch.gather(masks_local_y, 1, labels_max_ids) + masks_nb_x = masks_nb_x.view(tmp_batch*num_nb*tmp_channel, -1) + masks_nb_x_select = torch.gather(masks_nb_x, 1, labels_max_ids_nb) + masks_nb_y = masks_nb_y.view(tmp_batch*num_nb*tmp_channel, -1) + masks_nb_y_select = torch.gather(masks_nb_y, 1, labels_max_ids_nb) + + ########################################## + outputs_map1 = outputs_map1.view(tmp_batch*tmp_channel, -1) + outputs_map2 = outputs_map2.view(tmp_batch*tmp_channel, -1) + outputs_map3 = outputs_map3.view(tmp_batch*tmp_channel, -1) + labels_map2 = labels_map2.view(tmp_batch*tmp_channel, -1) + labels_map3 = labels_map3.view(tmp_batch*tmp_channel, -1) + masks_map1 = masks_map1.view(tmp_batch*tmp_channel, -1) + masks_map2 = masks_map2.view(tmp_batch*tmp_channel, -1) + masks_map3 = masks_map3.view(tmp_batch*tmp_channel, -1) + outputs_map = torch.cat([outputs_map1, outputs_map2, outputs_map3], 1) + labels_map = torch.cat([labels_map1, labels_map2, labels_map3], 1) + masks_map = torch.cat([masks_map1, masks_map2, masks_map3], 1) + loss_map = criterion_cls(outputs_map*masks_map, labels_map*masks_map) + if not masks_map.sum() == 0: + loss_map /= masks_map.sum() + ########################################## + + loss_x = criterion_reg(outputs_local_x_select*masks_local_x_select, labels_local_x_select*masks_local_x_select) + if not masks_local_x_select.sum() == 0: + loss_x /= masks_local_x_select.sum() + loss_y = criterion_reg(outputs_local_y_select*masks_local_y_select, labels_local_y_select*masks_local_y_select) + if not masks_local_y_select.sum() == 0: + loss_y /= masks_local_y_select.sum() + loss_nb_x = criterion_reg(outputs_nb_x_select*masks_nb_x_select, labels_nb_x_select*masks_nb_x_select) + if not masks_nb_x_select.sum() == 0: + loss_nb_x /= masks_nb_x_select.sum() + loss_nb_y = criterion_reg(outputs_nb_y_select*masks_nb_y_select, labels_nb_y_select*masks_nb_y_select) + if not masks_nb_y_select.sum() == 0: + loss_nb_y /= masks_nb_y_select.sum() + return loss_map, loss_x, loss_y, loss_nb_x, loss_nb_y + +def train_model(det_head, net, train_loader, criterion_cls, criterion_reg, cls_loss_weight, reg_loss_weight, num_nb, optimizer, num_epochs, scheduler, save_dir, save_interval, device): + for epoch in range(num_epochs): + print('Epoch {}/{}'.format(epoch, num_epochs - 1)) + logging.info('Epoch {}/{}'.format(epoch, num_epochs - 1)) + print('-' * 10) + logging.info('-' * 10) + net.train() + epoch_loss = 0.0 + + for i, data in enumerate(train_loader): + if det_head == 'pip': + inputs, labels_map1, labels_map2, labels_map3, labels_x, labels_y, labels_nb_x, labels_nb_y, masks_map1, masks_map2, masks_map3, masks_x, masks_y, masks_nb_x, masks_nb_y = data + inputs = inputs.to(device) + labels_map1 = labels_map1.to(device) + labels_map2 = labels_map2.to(device) + labels_map3 = labels_map3.to(device) + labels_x = labels_x.to(device) + labels_y = labels_y.to(device) + labels_nb_x = labels_nb_x.to(device) + labels_nb_y = labels_nb_y.to(device) + masks_map1 = masks_map1.to(device) + masks_map2 = masks_map2.to(device) + masks_map3 = masks_map3.to(device) + masks_x = masks_x.to(device) + masks_y = masks_y.to(device) + masks_nb_x = masks_nb_x.to(device) + masks_nb_y = masks_nb_y.to(device) + outputs_map1, outputs_map2, outputs_map3, outputs_x, outputs_y, outputs_nb_x, outputs_nb_y = net(inputs) + loss_map, loss_x, loss_y, loss_nb_x, loss_nb_y = compute_loss_pip(outputs_map1, outputs_map2, outputs_map3, outputs_x, outputs_y, outputs_nb_x, outputs_nb_y, labels_map1, labels_map2, labels_map3, labels_x, labels_y, labels_nb_x, labels_nb_y, masks_map1, masks_map2, masks_map3, masks_x, masks_y, masks_nb_x, masks_nb_y, criterion_cls, criterion_reg, num_nb) + loss = cls_loss_weight*loss_map + reg_loss_weight*loss_x + reg_loss_weight*loss_y + reg_loss_weight*loss_nb_x + reg_loss_weight*loss_nb_y + else: + print('No such head:', det_head) + exit(0) + + optimizer.zero_grad() + loss.backward() + optimizer.step() + if i%10 == 0: + if det_head == 'pip': + print('[Epoch {:d}/{:d}, Batch {:d}/{:d}] '.format( + epoch, num_epochs-1, i, len(train_loader)-1, loss.item(), cls_loss_weight*loss_map.item(), reg_loss_weight*loss_x.item(), reg_loss_weight*loss_y.item(), reg_loss_weight*loss_nb_x.item(), reg_loss_weight*loss_nb_y.item())) + logging.info('[Epoch {:d}/{:d}, Batch {:d}/{:d}] '.format( + epoch, num_epochs-1, i, len(train_loader)-1, loss.item(), cls_loss_weight*loss_map.item(), reg_loss_weight*loss_x.item(), reg_loss_weight*loss_y.item(), reg_loss_weight*loss_nb_x.item(), reg_loss_weight*loss_nb_y.item())) + else: + print('No such head:', det_head) + exit(0) + epoch_loss += loss.item() + epoch_loss /= len(train_loader) + if epoch%(save_interval-1) == 0 and epoch > 0: + filename = os.path.join(save_dir, 'epoch%d.pth' % epoch) + torch.save(net.state_dict(), filename) + print(filename, 'saved') + scheduler.step() + return net + +def forward_pip(net, inputs, preprocess, input_size, net_stride, num_nb): + net.eval() + with torch.no_grad(): + outputs_cls1, outputs_cls2, outputs_cls3, outputs_x, outputs_y, outputs_nb_x, outputs_nb_y = net(inputs) + tmp_batch, tmp_channel, tmp_height, tmp_width = outputs_cls1.size() + assert tmp_batch == 1 + + outputs_cls1 = outputs_cls1.view(tmp_batch*tmp_channel, -1) + max_ids = torch.argmax(outputs_cls1, 1) + max_cls = torch.max(outputs_cls1, 1)[0] + max_ids = max_ids.view(-1, 1) + max_ids_nb = max_ids.repeat(1, num_nb).view(-1, 1) + + outputs_x = outputs_x.view(tmp_batch*tmp_channel, -1) + outputs_x_select = torch.gather(outputs_x, 1, max_ids) + outputs_x_select = outputs_x_select.squeeze(1) + outputs_y = outputs_y.view(tmp_batch*tmp_channel, -1) + outputs_y_select = torch.gather(outputs_y, 1, max_ids) + outputs_y_select = outputs_y_select.squeeze(1) + + outputs_nb_x = outputs_nb_x.view(tmp_batch*num_nb*tmp_channel, -1) + outputs_nb_x_select = torch.gather(outputs_nb_x, 1, max_ids_nb) + outputs_nb_x_select = outputs_nb_x_select.squeeze(1).view(-1, num_nb) + outputs_nb_y = outputs_nb_y.view(tmp_batch*num_nb*tmp_channel, -1) + outputs_nb_y_select = torch.gather(outputs_nb_y, 1, max_ids_nb) + outputs_nb_y_select = outputs_nb_y_select.squeeze(1).view(-1, num_nb) + + tmp_x = (max_ids%tmp_width).view(-1,1).float()+outputs_x_select.view(-1,1) + tmp_y = (max_ids//tmp_width).view(-1,1).float()+outputs_y_select.view(-1,1) + tmp_x /= 1.0 * input_size / net_stride + tmp_y /= 1.0 * input_size / net_stride + + tmp_nb_x = (max_ids%tmp_width).view(-1,1).float()+outputs_nb_x_select + tmp_nb_y = (max_ids//tmp_width).view(-1,1).float()+outputs_nb_y_select + tmp_nb_x = tmp_nb_x.view(-1, num_nb) + tmp_nb_y = tmp_nb_y.view(-1, num_nb) + tmp_nb_x /= 1.0 * input_size / net_stride + tmp_nb_y /= 1.0 * input_size / net_stride + + return tmp_x, tmp_y, tmp_nb_x, tmp_nb_y, [outputs_cls1, outputs_cls2, outputs_cls3], max_cls + +def compute_nme(lms_pred, lms_gt, norm): + lms_pred = lms_pred.reshape((-1, 2)) + lms_gt = lms_gt.reshape((-1, 2)) + nme = np.mean(np.linalg.norm(lms_pred - lms_gt, axis=1)) / norm + return nme + diff --git a/third_party/PIPNet/lib/mobilenetv3.py b/third_party/PIPNet/lib/mobilenetv3.py new file mode 100644 index 0000000000000000000000000000000000000000..7297df7eb7773863b83c4b2de999ecfdb9e5c507 --- /dev/null +++ b/third_party/PIPNet/lib/mobilenetv3.py @@ -0,0 +1,233 @@ +""" +Creates a MobileNetV3 Model as defined in: +Andrew Howard, Mark Sandler, Grace Chu, Liang-Chieh Chen, Bo Chen, Mingxing Tan, Weijun Wang, Yukun Zhu, Ruoming Pang, Vijay Vasudevan, Quoc V. Le, Hartwig Adam. (2019). +Searching for MobileNetV3 +arXiv preprint arXiv:1905.02244. +""" + +import torch +import torch.nn as nn +import math + + +__all__ = ['mobilenetv3_large', 'mobilenetv3_small'] + + +def _make_divisible(v, divisor, min_value=None): + """ + This function is taken from the original tf repo. + It ensures that all layers have a channel number that is divisible by 8 + It can be seen here: + https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py + :param v: + :param divisor: + :param min_value: + :return: + """ + if min_value is None: + min_value = divisor + new_v = max(min_value, int(v + divisor / 2) // divisor * divisor) + # Make sure that round down does not go down by more than 10%. + if new_v < 0.9 * v: + new_v += divisor + return new_v + + +class h_sigmoid(nn.Module): + def __init__(self, inplace=True): + super(h_sigmoid, self).__init__() + self.relu = nn.ReLU6(inplace=inplace) + + def forward(self, x): + return self.relu(x + 3) / 6 + + +class h_swish(nn.Module): + def __init__(self, inplace=True): + super(h_swish, self).__init__() + self.sigmoid = h_sigmoid(inplace=inplace) + + def forward(self, x): + return x * self.sigmoid(x) + + +class SELayer(nn.Module): + def __init__(self, channel, reduction=4): + super(SELayer, self).__init__() + self.avg_pool = nn.AdaptiveAvgPool2d(1) + self.fc = nn.Sequential( + nn.Linear(channel, _make_divisible(channel // reduction, 8)), + nn.ReLU(inplace=True), + nn.Linear(_make_divisible(channel // reduction, 8), channel), + h_sigmoid() + ) + + def forward(self, x): + b, c, _, _ = x.size() + y = self.avg_pool(x).view(b, c) + y = self.fc(y).view(b, c, 1, 1) + return x * y + + +def conv_3x3_bn(inp, oup, stride): + return nn.Sequential( + nn.Conv2d(inp, oup, 3, stride, 1, bias=False), + nn.BatchNorm2d(oup), + h_swish() + ) + + +def conv_1x1_bn(inp, oup): + return nn.Sequential( + nn.Conv2d(inp, oup, 1, 1, 0, bias=False), + nn.BatchNorm2d(oup), + h_swish() + ) + + +class InvertedResidual(nn.Module): + def __init__(self, inp, hidden_dim, oup, kernel_size, stride, use_se, use_hs): + super(InvertedResidual, self).__init__() + assert stride in [1, 2] + + self.identity = stride == 1 and inp == oup + + if inp == hidden_dim: + self.conv = nn.Sequential( + # dw + nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim, bias=False), + nn.BatchNorm2d(hidden_dim), + h_swish() if use_hs else nn.ReLU(inplace=True), + # Squeeze-and-Excite + SELayer(hidden_dim) if use_se else nn.Identity(), + # pw-linear + nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False), + nn.BatchNorm2d(oup), + ) + else: + self.conv = nn.Sequential( + # pw + nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False), + nn.BatchNorm2d(hidden_dim), + h_swish() if use_hs else nn.ReLU(inplace=True), + # dw + nn.Conv2d(hidden_dim, hidden_dim, kernel_size, stride, (kernel_size - 1) // 2, groups=hidden_dim, bias=False), + nn.BatchNorm2d(hidden_dim), + # Squeeze-and-Excite + SELayer(hidden_dim) if use_se else nn.Identity(), + h_swish() if use_hs else nn.ReLU(inplace=True), + # pw-linear + nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False), + nn.BatchNorm2d(oup), + ) + + def forward(self, x): + if self.identity: + return x + self.conv(x) + else: + return self.conv(x) + + +class MobileNetV3(nn.Module): + def __init__(self, cfgs, mode, num_classes=1000, width_mult=1.): + super(MobileNetV3, self).__init__() + # setting of inverted residual blocks + self.cfgs = cfgs + assert mode in ['large', 'small'] + + # building first layer + input_channel = _make_divisible(16 * width_mult, 8) + layers = [conv_3x3_bn(3, input_channel, 2)] + # building inverted residual blocks + block = InvertedResidual + for k, t, c, use_se, use_hs, s in self.cfgs: + output_channel = _make_divisible(c * width_mult, 8) + exp_size = _make_divisible(input_channel * t, 8) + layers.append(block(input_channel, exp_size, output_channel, k, s, use_se, use_hs)) + input_channel = output_channel + self.features = nn.Sequential(*layers) + # building last several layers + self.conv = conv_1x1_bn(input_channel, exp_size) + self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) + output_channel = {'large': 1280, 'small': 1024} + output_channel = _make_divisible(output_channel[mode] * width_mult, 8) if width_mult > 1.0 else output_channel[mode] + self.classifier = nn.Sequential( + nn.Linear(exp_size, output_channel), + h_swish(), + nn.Dropout(0.2), + nn.Linear(output_channel, num_classes), + ) + + self._initialize_weights() + + def forward(self, x): + x = self.features(x) + x = self.conv(x) + x = self.avgpool(x) + x = x.view(x.size(0), -1) + x = self.classifier(x) + return x + + def _initialize_weights(self): + for m in self.modules(): + if isinstance(m, nn.Conv2d): + n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels + m.weight.data.normal_(0, math.sqrt(2. / n)) + if m.bias is not None: + m.bias.data.zero_() + elif isinstance(m, nn.BatchNorm2d): + m.weight.data.fill_(1) + m.bias.data.zero_() + elif isinstance(m, nn.Linear): + m.weight.data.normal_(0, 0.01) + m.bias.data.zero_() + + +def mobilenetv3_large(**kwargs): + """ + Constructs a MobileNetV3-Large model + """ + cfgs = [ + # k, t, c, SE, HS, s + [3, 1, 16, 0, 0, 1], + [3, 4, 24, 0, 0, 2], + [3, 3, 24, 0, 0, 1], + [5, 3, 40, 1, 0, 2], + [5, 3, 40, 1, 0, 1], + [5, 3, 40, 1, 0, 1], + [3, 6, 80, 0, 1, 2], + [3, 2.5, 80, 0, 1, 1], + [3, 2.3, 80, 0, 1, 1], + [3, 2.3, 80, 0, 1, 1], + [3, 6, 112, 1, 1, 1], + [3, 6, 112, 1, 1, 1], + [5, 6, 160, 1, 1, 2], + [5, 6, 160, 1, 1, 1], + [5, 6, 160, 1, 1, 1] + ] + return MobileNetV3(cfgs, mode='large', **kwargs) + + +def mobilenetv3_small(**kwargs): + """ + Constructs a MobileNetV3-Small model + """ + cfgs = [ + # k, t, c, SE, HS, s + [3, 1, 16, 1, 0, 2], + [3, 4.5, 24, 0, 0, 2], + [3, 3.67, 24, 0, 0, 1], + [5, 4, 40, 1, 1, 2], + [5, 6, 40, 1, 1, 1], + [5, 6, 40, 1, 1, 1], + [5, 3, 48, 1, 1, 1], + [5, 3, 48, 1, 1, 1], + [5, 6, 96, 1, 1, 2], + [5, 6, 96, 1, 1, 1], + [5, 6, 96, 1, 1, 1], + ] + + return MobileNetV3(cfgs, mode='small', **kwargs) + + + diff --git a/third_party/PIPNet/lib/networks.py b/third_party/PIPNet/lib/networks.py new file mode 100644 index 0000000000000000000000000000000000000000..bc05f039a5c3a14fe41a4f4bbef0342701a9ec28 --- /dev/null +++ b/third_party/PIPNet/lib/networks.py @@ -0,0 +1,415 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +import torchvision.models as models +import numpy as np + +# net_stride output_size +# 128 2x2 +# 64 4x4 +# 32 8x8 +# pip regression, resnet101 +class Pip_resnet101(nn.Module): + def __init__(self, resnet, num_nb, num_lms=68, input_size=256, net_stride=32): + super(Pip_resnet101, self).__init__() + self.num_nb = num_nb + self.num_lms = num_lms + self.input_size = input_size + self.net_stride = net_stride + self.conv1 = resnet.conv1 + self.bn1 = resnet.bn1 + self.maxpool = resnet.maxpool + self.sigmoid = nn.Sigmoid() + self.layer1 = resnet.layer1 + self.layer2 = resnet.layer2 + self.layer3 = resnet.layer3 + self.layer4 = resnet.layer4 + if self.net_stride == 128: + self.layer5 = nn.Conv2d(2048, 512, kernel_size=3, stride=2, padding=1) + self.bn5 = nn.BatchNorm2d(512) + self.layer6 = nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1) + self.bn6 = nn.BatchNorm2d(512) + # init + nn.init.normal_(self.layer5.weight, std=0.001) + if self.layer5.bias is not None: + nn.init.constant_(self.layer5.bias, 0) + nn.init.constant_(self.bn5.weight, 1) + nn.init.constant_(self.bn5.bias, 0) + + nn.init.normal_(self.layer6.weight, std=0.001) + if self.layer6.bias is not None: + nn.init.constant_(self.layer6.bias, 0) + nn.init.constant_(self.bn6.weight, 1) + nn.init.constant_(self.bn6.bias, 0) + elif self.net_stride == 64: + self.layer5 = nn.Conv2d(2048, 512, kernel_size=3, stride=2, padding=1) + self.bn5 = nn.BatchNorm2d(512) + # init + nn.init.normal_(self.layer5.weight, std=0.001) + if self.layer5.bias is not None: + nn.init.constant_(self.layer5.bias, 0) + nn.init.constant_(self.bn5.weight, 1) + nn.init.constant_(self.bn5.bias, 0) + elif self.net_stride == 32: + pass + else: + print('No such net_stride!') + exit(0) + + self.cls_layer = nn.Conv2d(2048, num_lms, kernel_size=1, stride=1, padding=0) + self.x_layer = nn.Conv2d(2048, num_lms, kernel_size=1, stride=1, padding=0) + self.y_layer = nn.Conv2d(2048, num_lms, kernel_size=1, stride=1, padding=0) + self.nb_x_layer = nn.Conv2d(2048, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + self.nb_y_layer = nn.Conv2d(2048, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + + nn.init.normal_(self.cls_layer.weight, std=0.001) + if self.cls_layer.bias is not None: + nn.init.constant_(self.cls_layer.bias, 0) + + nn.init.normal_(self.x_layer.weight, std=0.001) + if self.x_layer.bias is not None: + nn.init.constant_(self.x_layer.bias, 0) + + nn.init.normal_(self.y_layer.weight, std=0.001) + if self.y_layer.bias is not None: + nn.init.constant_(self.y_layer.bias, 0) + + nn.init.normal_(self.nb_x_layer.weight, std=0.001) + if self.nb_x_layer.bias is not None: + nn.init.constant_(self.nb_x_layer.bias, 0) + + nn.init.normal_(self.nb_y_layer.weight, std=0.001) + if self.nb_y_layer.bias is not None: + nn.init.constant_(self.nb_y_layer.bias, 0) + + def forward(self, x): + x = self.conv1(x) + x = self.bn1(x) + x = F.relu(x) + x = self.maxpool(x) + x = self.layer1(x) + x = self.layer2(x) + x = self.layer3(x) + x = self.layer4(x) + if self.net_stride == 128: + x = F.relu(self.bn5(self.layer5(x))) + x = F.relu(self.bn6(self.layer6(x))) + elif self.net_stride == 64: + x = F.relu(self.bn5(self.layer5(x))) + else: + pass + x1 = self.cls_layer(x) + x2 = self.x_layer(x) + x3 = self.y_layer(x) + x4 = self.nb_x_layer(x) + x5 = self.nb_y_layer(x) + return x1, x2, x3, x4, x5 + +# net_stride output_size +# 128 2x2 +# 64 4x4 +# 32 8x8 +# pip regression, resnet50 +class Pip_resnet50(nn.Module): + def __init__(self, resnet, num_nb, num_lms=68, input_size=256, net_stride=32): + super(Pip_resnet50, self).__init__() + self.num_nb = num_nb + self.num_lms = num_lms + self.input_size = input_size + self.net_stride = net_stride + self.conv1 = resnet.conv1 + self.bn1 = resnet.bn1 + self.maxpool = resnet.maxpool + self.sigmoid = nn.Sigmoid() + self.layer1 = resnet.layer1 + self.layer2 = resnet.layer2 + self.layer3 = resnet.layer3 + self.layer4 = resnet.layer4 + if self.net_stride == 128: + self.layer5 = nn.Conv2d(2048, 512, kernel_size=3, stride=2, padding=1) + self.bn5 = nn.BatchNorm2d(512) + self.layer6 = nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1) + self.bn6 = nn.BatchNorm2d(512) + # init + nn.init.normal_(self.layer5.weight, std=0.001) + if self.layer5.bias is not None: + nn.init.constant_(self.layer5.bias, 0) + nn.init.constant_(self.bn5.weight, 1) + nn.init.constant_(self.bn5.bias, 0) + + nn.init.normal_(self.layer6.weight, std=0.001) + if self.layer6.bias is not None: + nn.init.constant_(self.layer6.bias, 0) + nn.init.constant_(self.bn6.weight, 1) + nn.init.constant_(self.bn6.bias, 0) + elif self.net_stride == 64: + self.layer5 = nn.Conv2d(2048, 512, kernel_size=3, stride=2, padding=1) + self.bn5 = nn.BatchNorm2d(512) + # init + nn.init.normal_(self.layer5.weight, std=0.001) + if self.layer5.bias is not None: + nn.init.constant_(self.layer5.bias, 0) + nn.init.constant_(self.bn5.weight, 1) + nn.init.constant_(self.bn5.bias, 0) + elif self.net_stride == 32: + pass + else: + print('No such net_stride!') + exit(0) + + self.cls_layer = nn.Conv2d(2048, num_lms, kernel_size=1, stride=1, padding=0) + self.x_layer = nn.Conv2d(2048, num_lms, kernel_size=1, stride=1, padding=0) + self.y_layer = nn.Conv2d(2048, num_lms, kernel_size=1, stride=1, padding=0) + self.nb_x_layer = nn.Conv2d(2048, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + self.nb_y_layer = nn.Conv2d(2048, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + + nn.init.normal_(self.cls_layer.weight, std=0.001) + if self.cls_layer.bias is not None: + nn.init.constant_(self.cls_layer.bias, 0) + + nn.init.normal_(self.x_layer.weight, std=0.001) + if self.x_layer.bias is not None: + nn.init.constant_(self.x_layer.bias, 0) + + nn.init.normal_(self.y_layer.weight, std=0.001) + if self.y_layer.bias is not None: + nn.init.constant_(self.y_layer.bias, 0) + + nn.init.normal_(self.nb_x_layer.weight, std=0.001) + if self.nb_x_layer.bias is not None: + nn.init.constant_(self.nb_x_layer.bias, 0) + + nn.init.normal_(self.nb_y_layer.weight, std=0.001) + if self.nb_y_layer.bias is not None: + nn.init.constant_(self.nb_y_layer.bias, 0) + + def forward(self, x): + x = self.conv1(x) + x = self.bn1(x) + x = F.relu(x) + x = self.maxpool(x) + x = self.layer1(x) + x = self.layer2(x) + x = self.layer3(x) + x = self.layer4(x) + if self.net_stride == 128: + x = F.relu(self.bn5(self.layer5(x))) + x = F.relu(self.bn6(self.layer6(x))) + elif self.net_stride == 64: + x = F.relu(self.bn5(self.layer5(x))) + else: + pass + x1 = self.cls_layer(x) + x2 = self.x_layer(x) + x3 = self.y_layer(x) + x4 = self.nb_x_layer(x) + x5 = self.nb_y_layer(x) + return x1, x2, x3, x4, x5 + +# net_stride output_size +# 128 2x2 +# 64 4x4 +# 32 8x8 +# pip regression, resnet18 +class Pip_resnet18(nn.Module): + def __init__(self, resnet, num_nb, num_lms=68, input_size=256, net_stride=32): + super(Pip_resnet18, self).__init__() + self.num_nb = num_nb + self.num_lms = num_lms + self.input_size = input_size + self.net_stride = net_stride + self.conv1 = resnet.conv1 + self.bn1 = resnet.bn1 + self.maxpool = resnet.maxpool + self.sigmoid = nn.Sigmoid() + self.layer1 = resnet.layer1 + self.layer2 = resnet.layer2 + self.layer3 = resnet.layer3 + self.layer4 = resnet.layer4 + if self.net_stride == 128: + self.layer5 = nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1) + self.bn5 = nn.BatchNorm2d(512) + self.layer6 = nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1) + self.bn6 = nn.BatchNorm2d(512) + # init + nn.init.normal_(self.layer5.weight, std=0.001) + if self.layer5.bias is not None: + nn.init.constant_(self.layer5.bias, 0) + nn.init.constant_(self.bn5.weight, 1) + nn.init.constant_(self.bn5.bias, 0) + + nn.init.normal_(self.layer6.weight, std=0.001) + if self.layer6.bias is not None: + nn.init.constant_(self.layer6.bias, 0) + nn.init.constant_(self.bn6.weight, 1) + nn.init.constant_(self.bn6.bias, 0) + elif self.net_stride == 64: + self.layer5 = nn.Conv2d(512, 512, kernel_size=3, stride=2, padding=1) + self.bn5 = nn.BatchNorm2d(512) + # init + nn.init.normal_(self.layer5.weight, std=0.001) + if self.layer5.bias is not None: + nn.init.constant_(self.layer5.bias, 0) + nn.init.constant_(self.bn5.weight, 1) + nn.init.constant_(self.bn5.bias, 0) + elif self.net_stride == 32: + pass + elif self.net_stride == 16: + self.deconv1 = nn.ConvTranspose2d(512, 512, kernel_size=4, stride=2, padding=1, bias=False) + self.bn_deconv1 = nn.BatchNorm2d(512) + nn.init.normal_(self.deconv1.weight, std=0.001) + if self.deconv1.bias is not None: + nn.init.constant_(self.deconv1.bias, 0) + nn.init.constant_(self.bn_deconv1.weight, 1) + nn.init.constant_(self.bn_deconv1.bias, 0) + else: + print('No such net_stride!') + exit(0) + + self.cls_layer = nn.Conv2d(512, num_lms, kernel_size=1, stride=1, padding=0) + self.x_layer = nn.Conv2d(512, num_lms, kernel_size=1, stride=1, padding=0) + self.y_layer = nn.Conv2d(512, num_lms, kernel_size=1, stride=1, padding=0) + self.nb_x_layer = nn.Conv2d(512, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + self.nb_y_layer = nn.Conv2d(512, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + + nn.init.normal_(self.cls_layer.weight, std=0.001) + if self.cls_layer.bias is not None: + nn.init.constant_(self.cls_layer.bias, 0) + + nn.init.normal_(self.x_layer.weight, std=0.001) + if self.x_layer.bias is not None: + nn.init.constant_(self.x_layer.bias, 0) + + nn.init.normal_(self.y_layer.weight, std=0.001) + if self.y_layer.bias is not None: + nn.init.constant_(self.y_layer.bias, 0) + + nn.init.normal_(self.nb_x_layer.weight, std=0.001) + if self.nb_x_layer.bias is not None: + nn.init.constant_(self.nb_x_layer.bias, 0) + + nn.init.normal_(self.nb_y_layer.weight, std=0.001) + if self.nb_y_layer.bias is not None: + nn.init.constant_(self.nb_y_layer.bias, 0) + + def forward(self, x): + x = self.conv1(x) + x = self.bn1(x) + x = F.relu(x) + x = self.maxpool(x) + x = self.layer1(x) + x = self.layer2(x) + x = self.layer3(x) + x = self.layer4(x) + if self.net_stride == 128: + x = F.relu(self.bn5(self.layer5(x))) + x = F.relu(self.bn6(self.layer6(x))) + elif self.net_stride == 64: + x = F.relu(self.bn5(self.layer5(x))) + elif self.net_stride == 16: + x = F.relu(self.bn_deconv1(self.deconv1(x))) + else: + pass + x1 = self.cls_layer(x) + x2 = self.x_layer(x) + x3 = self.y_layer(x) + x4 = self.nb_x_layer(x) + x5 = self.nb_y_layer(x) + return x1, x2, x3, x4, x5 + +class Pip_mbnetv2(nn.Module): + def __init__(self, mbnet, num_nb, num_lms=68, input_size=256, net_stride=32): + super(Pip_mbnetv2, self).__init__() + self.num_nb = num_nb + self.num_lms = num_lms + self.input_size = input_size + self.net_stride = net_stride + self.features = mbnet.features + self.sigmoid = nn.Sigmoid() + + self.cls_layer = nn.Conv2d(1280, num_lms, kernel_size=1, stride=1, padding=0) + self.x_layer = nn.Conv2d(1280, num_lms, kernel_size=1, stride=1, padding=0) + self.y_layer = nn.Conv2d(1280, num_lms, kernel_size=1, stride=1, padding=0) + self.nb_x_layer = nn.Conv2d(1280, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + self.nb_y_layer = nn.Conv2d(1280, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + + nn.init.normal_(self.cls_layer.weight, std=0.001) + if self.cls_layer.bias is not None: + nn.init.constant_(self.cls_layer.bias, 0) + + nn.init.normal_(self.x_layer.weight, std=0.001) + if self.x_layer.bias is not None: + nn.init.constant_(self.x_layer.bias, 0) + + nn.init.normal_(self.y_layer.weight, std=0.001) + if self.y_layer.bias is not None: + nn.init.constant_(self.y_layer.bias, 0) + + nn.init.normal_(self.nb_x_layer.weight, std=0.001) + if self.nb_x_layer.bias is not None: + nn.init.constant_(self.nb_x_layer.bias, 0) + + nn.init.normal_(self.nb_y_layer.weight, std=0.001) + if self.nb_y_layer.bias is not None: + nn.init.constant_(self.nb_y_layer.bias, 0) + + def forward(self, x): + x = self.features(x) + x1 = self.cls_layer(x) + x2 = self.x_layer(x) + x3 = self.y_layer(x) + x4 = self.nb_x_layer(x) + x5 = self.nb_y_layer(x) + return x1, x2, x3, x4, x5 + +class Pip_mbnetv3(nn.Module): + def __init__(self, mbnet, num_nb, num_lms=68, input_size=256, net_stride=32): + super(Pip_mbnetv3, self).__init__() + self.num_nb = num_nb + self.num_lms = num_lms + self.input_size = input_size + self.net_stride = net_stride + self.features = mbnet.features + self.conv = mbnet.conv + self.sigmoid = nn.Sigmoid() + + self.cls_layer = nn.Conv2d(960, num_lms, kernel_size=1, stride=1, padding=0) + self.x_layer = nn.Conv2d(960, num_lms, kernel_size=1, stride=1, padding=0) + self.y_layer = nn.Conv2d(960, num_lms, kernel_size=1, stride=1, padding=0) + self.nb_x_layer = nn.Conv2d(960, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + self.nb_y_layer = nn.Conv2d(960, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + + nn.init.normal_(self.cls_layer.weight, std=0.001) + if self.cls_layer.bias is not None: + nn.init.constant_(self.cls_layer.bias, 0) + + nn.init.normal_(self.x_layer.weight, std=0.001) + if self.x_layer.bias is not None: + nn.init.constant_(self.x_layer.bias, 0) + + nn.init.normal_(self.y_layer.weight, std=0.001) + if self.y_layer.bias is not None: + nn.init.constant_(self.y_layer.bias, 0) + + nn.init.normal_(self.nb_x_layer.weight, std=0.001) + if self.nb_x_layer.bias is not None: + nn.init.constant_(self.nb_x_layer.bias, 0) + + nn.init.normal_(self.nb_y_layer.weight, std=0.001) + if self.nb_y_layer.bias is not None: + nn.init.constant_(self.nb_y_layer.bias, 0) + + def forward(self, x): + x = self.features(x) + x = self.conv(x) + x1 = self.cls_layer(x) + x2 = self.x_layer(x) + x3 = self.y_layer(x) + x4 = self.nb_x_layer(x) + x5 = self.nb_y_layer(x) + return x1, x2, x3, x4, x5 + + +if __name__ == '__main__': + pass + diff --git a/third_party/PIPNet/lib/networks_gssl.py b/third_party/PIPNet/lib/networks_gssl.py new file mode 100644 index 0000000000000000000000000000000000000000..405f5d8e328afb4f0b735a7b8d3a37cf63ad72e8 --- /dev/null +++ b/third_party/PIPNet/lib/networks_gssl.py @@ -0,0 +1,80 @@ +import torch +import torch.nn as nn +import torch.nn.functional as F +import torchvision.models as models +import numpy as np +import time + +# net_stride output_size +# 128 2x2 +# 64 4x4 +# 32 8x8 +# pip regression, resnet18, for GSSL +class Pip_resnet18(nn.Module): + def __init__(self, resnet, num_nb, num_lms=68, input_size=256, net_stride=32): + super(Pip_resnet18, self).__init__() + self.num_nb = num_nb + self.num_lms = num_lms + self.input_size = input_size + self.net_stride = net_stride + self.conv1 = resnet.conv1 + self.bn1 = resnet.bn1 + self.maxpool = resnet.maxpool + self.sigmoid = nn.Sigmoid() + self.layer1 = resnet.layer1 + self.layer2 = resnet.layer2 + self.layer3 = resnet.layer3 + self.layer4 = resnet.layer4 + + self.my_maxpool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0) + + self.cls_layer = nn.Conv2d(512, num_lms, kernel_size=1, stride=1, padding=0) + self.x_layer = nn.Conv2d(512, num_lms, kernel_size=1, stride=1, padding=0) + self.y_layer = nn.Conv2d(512, num_lms, kernel_size=1, stride=1, padding=0) + self.nb_x_layer = nn.Conv2d(512, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + self.nb_y_layer = nn.Conv2d(512, num_nb*num_lms, kernel_size=1, stride=1, padding=0) + + # init + nn.init.normal_(self.cls_layer.weight, std=0.001) + if self.cls_layer.bias is not None: + nn.init.constant_(self.cls_layer.bias, 0) + + nn.init.normal_(self.x_layer.weight, std=0.001) + if self.x_layer.bias is not None: + nn.init.constant_(self.x_layer.bias, 0) + + nn.init.normal_(self.y_layer.weight, std=0.001) + if self.y_layer.bias is not None: + nn.init.constant_(self.y_layer.bias, 0) + + nn.init.normal_(self.nb_x_layer.weight, std=0.001) + if self.nb_x_layer.bias is not None: + nn.init.constant_(self.nb_x_layer.bias, 0) + + nn.init.normal_(self.nb_y_layer.weight, std=0.001) + if self.nb_y_layer.bias is not None: + nn.init.constant_(self.nb_y_layer.bias, 0) + + def forward(self, x): + x = self.conv1(x) + x = self.bn1(x) + x = F.relu(x) + x = self.maxpool(x) + x = self.layer1(x) + x = self.layer2(x) + x = self.layer3(x) + x = self.layer4(x) + cls1 = self.cls_layer(x) + offset_x = self.x_layer(x) + offset_y = self.y_layer(x) + nb_x = self.nb_x_layer(x) + nb_y = self.nb_y_layer(x) + x = self.my_maxpool(x) + cls2 = self.cls_layer(x) + x = self.my_maxpool(x) + cls3 = self.cls_layer(x) + return cls1, cls2, cls3, offset_x, offset_y, nb_x, nb_y + +if __name__ == '__main__': + pass + diff --git a/third_party/PIPNet/lib/preprocess.py b/third_party/PIPNet/lib/preprocess.py new file mode 100644 index 0000000000000000000000000000000000000000..737decf2d3e4b0bc29cdd03c30c61e04ade04242 --- /dev/null +++ b/third_party/PIPNet/lib/preprocess.py @@ -0,0 +1,554 @@ +import os, cv2 +import hdf5storage +import numpy as np +import sys + +def process_300w(root_folder, folder_name, image_name, label_name, target_size): + image_path = os.path.join(root_folder, folder_name, image_name) + label_path = os.path.join(root_folder, folder_name, label_name) + + with open(label_path, 'r') as ff: + anno = ff.readlines()[3:-1] + anno = [x.strip().split() for x in anno] + anno = [[int(float(x[0])), int(float(x[1]))] for x in anno] + image = cv2.imread(image_path) + image_height, image_width, _ = image.shape + anno_x = [x[0] for x in anno] + anno_y = [x[1] for x in anno] + bbox_xmin = min(anno_x) + bbox_ymin = min(anno_y) + bbox_xmax = max(anno_x) + bbox_ymax = max(anno_y) + bbox_width = bbox_xmax - bbox_xmin + bbox_height = bbox_ymax - bbox_ymin + scale = 1.1 + bbox_xmin -= int((scale-1)/2*bbox_width) + bbox_ymin -= int((scale-1)/2*bbox_height) + bbox_width *= scale + bbox_height *= scale + bbox_width = int(bbox_width) + bbox_height = int(bbox_height) + bbox_xmin = max(bbox_xmin, 0) + bbox_ymin = max(bbox_ymin, 0) + bbox_width = min(bbox_width, image_width-bbox_xmin-1) + bbox_height = min(bbox_height, image_height-bbox_ymin-1) + anno = [[(x-bbox_xmin)/bbox_width, (y-bbox_ymin)/bbox_height] for x,y in anno] + + bbox_xmax = bbox_xmin + bbox_width + bbox_ymax = bbox_ymin + bbox_height + image_crop = image[bbox_ymin:bbox_ymax, bbox_xmin:bbox_xmax, :] + image_crop = cv2.resize(image_crop, (target_size, target_size)) + return image_crop, anno + +def process_cofw(image, bbox, anno, target_size): + image_height, image_width, _ = image.shape + anno_x = anno[:29] + anno_y = anno[29:58] + ################################ + xmin, ymin, width, height = bbox + xmax = xmin + width -1 + ymax = ymin + height -1 + ################################ + xmin = max(xmin, 0) + ymin = max(ymin, 0) + xmax = min(xmax, image_width-1) + ymax = min(ymax, image_height-1) + anno_x = (anno_x - xmin) / (xmax - xmin) + anno_y = (anno_y - ymin) / (ymax - ymin) + anno = np.concatenate([anno_x.reshape(-1,1), anno_y.reshape(-1,1)], axis=1) + anno = list(anno) + anno = [list(x) for x in anno] + image_crop = image[int(ymin):int(ymax), int(xmin):int(xmax), :] + image_crop = cv2.resize(image_crop, (target_size, target_size)) + return image_crop, anno + +def process_wflw(anno, target_size): + image_name = anno[-1] + image_path = os.path.join('..', 'data', 'WFLW', 'WFLW_images', image_name) + image = cv2.imread(image_path) + image_height, image_width, _ = image.shape + lms = anno[:196] + lms = [float(x) for x in lms] + lms_x = lms[0::2] + lms_y = lms[1::2] + lms_x = [x if x >=0 else 0 for x in lms_x] + lms_x = [x if x <=image_width else image_width for x in lms_x] + lms_y = [y if y >=0 else 0 for y in lms_y] + lms_y = [y if y <=image_height else image_height for y in lms_y] + lms = [[x,y] for x,y in zip(lms_x, lms_y)] + lms = [x for z in lms for x in z] + bbox = anno[196:200] + bbox = [float(x) for x in bbox] + attrs = anno[200:206] + attrs = np.array([int(x) for x in attrs]) + bbox_xmin, bbox_ymin, bbox_xmax, bbox_ymax = bbox + + width = bbox_xmax - bbox_xmin + height = bbox_ymax - bbox_ymin + scale = 1.2 + bbox_xmin -= width * (scale-1)/2 + bbox_ymin -= height * (scale-1)/2 + bbox_xmax += width * (scale-1)/2 + bbox_ymax += height * (scale-1)/2 + bbox_xmin = max(bbox_xmin, 0) + bbox_ymin = max(bbox_ymin, 0) + bbox_xmax = min(bbox_xmax, image_width-1) + bbox_ymax = min(bbox_ymax, image_height-1) + width = bbox_xmax - bbox_xmin + height = bbox_ymax - bbox_ymin + image_crop = image[int(bbox_ymin):int(bbox_ymax), int(bbox_xmin):int(bbox_xmax), :] + image_crop = cv2.resize(image_crop, (target_size, target_size)) + + tmp1 = [bbox_xmin, bbox_ymin]*98 + tmp1 = np.array(tmp1) + tmp2 = [width, height]*98 + tmp2 = np.array(tmp2) + lms = np.array(lms) - tmp1 + lms = lms / tmp2 + lms = lms.tolist() + lms = zip(lms[0::2], lms[1::2]) + return image_crop, list(lms) + +def process_aflw(root_folder, image_name, bbox, anno, target_size): + image = cv2.imread(os.path.join(root_folder, 'AFLW', 'flickr', image_name)) + image_height, image_width, _ = image.shape + anno_x = anno[:19] + anno_y = anno[19:] + anno_x = [x if x >=0 else 0 for x in anno_x] + anno_x = [x if x <=image_width else image_width for x in anno_x] + anno_y = [y if y >=0 else 0 for y in anno_y] + anno_y = [y if y <=image_height else image_height for y in anno_y] + anno_x_min = min(anno_x) + anno_x_max = max(anno_x) + anno_y_min = min(anno_y) + anno_y_max = max(anno_y) + xmin, xmax, ymin, ymax = bbox + + xmin = max(xmin, 0) + ymin = max(ymin, 0) + xmax = min(xmax, image_width-1) + ymax = min(ymax, image_height-1) + + image_crop = image[int(ymin):int(ymax), int(xmin):int(xmax), :] + image_crop = cv2.resize(image_crop, (target_size, target_size)) + + anno_x = (np.array(anno_x) - xmin) / (xmax - xmin) + anno_y = (np.array(anno_y) - ymin) / (ymax - ymin) + + anno = np.concatenate([anno_x.reshape(-1,1), anno_y.reshape(-1,1)], axis=1).flatten() + anno = zip(anno[0::2], anno[1::2]) + return image_crop, anno + +def gen_meanface(root_folder, data_name): + with open(os.path.join(root_folder, data_name, 'train.txt'), 'r') as f: + annos = f.readlines() + annos = [x.strip().split()[1:] for x in annos] + annos = [[float(x) for x in anno] for anno in annos] + annos = np.array(annos) + meanface = np.mean(annos, axis=0) + meanface = meanface.tolist() + meanface = [str(x) for x in meanface] + + with open(os.path.join(root_folder, data_name, 'meanface.txt'), 'w') as f: + f.write(' '.join(meanface)) + +def convert_wflw(root_folder, data_name): + with open(os.path.join('../data/WFLW/test.txt'), 'r') as f: + annos = f.readlines() + annos = [x.strip().split() for x in annos] + annos_new = [] + for anno in annos: + annos_new.append([]) + # name + annos_new[-1].append(anno[0]) + anno = anno[1:] + # jaw + for i in range(17): + annos_new[-1].append(anno[i*2*2]) + annos_new[-1].append(anno[i*2*2+1]) + # left eyebrow + annos_new[-1].append(anno[33*2]) + annos_new[-1].append(anno[33*2+1]) + annos_new[-1].append(anno[34*2]) + annos_new[-1].append(str((float(anno[34*2+1])+float(anno[41*2+1]))/2)) + annos_new[-1].append(anno[35*2]) + annos_new[-1].append(str((float(anno[35*2+1])+float(anno[40*2+1]))/2)) + annos_new[-1].append(anno[36*2]) + annos_new[-1].append(str((float(anno[36*2+1])+float(anno[39*2+1]))/2)) + annos_new[-1].append(anno[37*2]) + annos_new[-1].append(str((float(anno[37*2+1])+float(anno[38*2+1]))/2)) + # right eyebrow + annos_new[-1].append(anno[42*2]) + annos_new[-1].append(str((float(anno[42*2+1])+float(anno[50*2+1]))/2)) + annos_new[-1].append(anno[43*2]) + annos_new[-1].append(str((float(anno[43*2+1])+float(anno[49*2+1]))/2)) + annos_new[-1].append(anno[44*2]) + annos_new[-1].append(str((float(anno[44*2+1])+float(anno[48*2+1]))/2)) + annos_new[-1].append(anno[45*2]) + annos_new[-1].append(str((float(anno[45*2+1])+float(anno[47*2+1]))/2)) + annos_new[-1].append(anno[46*2]) + annos_new[-1].append(anno[46*2+1]) + # nose + for i in range(51, 60): + annos_new[-1].append(anno[i*2]) + annos_new[-1].append(anno[i*2+1]) + # left eye + annos_new[-1].append(anno[60*2]) + annos_new[-1].append(anno[60*2+1]) + annos_new[-1].append(str(0.666*float(anno[61*2])+0.333*float(anno[62*2]))) + annos_new[-1].append(str(0.666*float(anno[61*2+1])+0.333*float(anno[62*2+1]))) + annos_new[-1].append(str(0.666*float(anno[63*2])+0.333*float(anno[62*2]))) + annos_new[-1].append(str(0.666*float(anno[63*2+1])+0.333*float(anno[62*2+1]))) + annos_new[-1].append(anno[64*2]) + annos_new[-1].append(anno[64*2+1]) + annos_new[-1].append(str(0.666*float(anno[65*2])+0.333*float(anno[66*2]))) + annos_new[-1].append(str(0.666*float(anno[65*2+1])+0.333*float(anno[66*2+1]))) + annos_new[-1].append(str(0.666*float(anno[67*2])+0.333*float(anno[66*2]))) + annos_new[-1].append(str(0.666*float(anno[67*2+1])+0.333*float(anno[66*2+1]))) + # right eye + annos_new[-1].append(anno[68*2]) + annos_new[-1].append(anno[68*2+1]) + annos_new[-1].append(str(0.666*float(anno[69*2])+0.333*float(anno[70*2]))) + annos_new[-1].append(str(0.666*float(anno[69*2+1])+0.333*float(anno[70*2+1]))) + annos_new[-1].append(str(0.666*float(anno[71*2])+0.333*float(anno[70*2]))) + annos_new[-1].append(str(0.666*float(anno[71*2+1])+0.333*float(anno[70*2+1]))) + annos_new[-1].append(anno[72*2]) + annos_new[-1].append(anno[72*2+1]) + annos_new[-1].append(str(0.666*float(anno[73*2])+0.333*float(anno[74*2]))) + annos_new[-1].append(str(0.666*float(anno[73*2+1])+0.333*float(anno[74*2+1]))) + annos_new[-1].append(str(0.666*float(anno[75*2])+0.333*float(anno[74*2]))) + annos_new[-1].append(str(0.666*float(anno[75*2+1])+0.333*float(anno[74*2+1]))) + # mouth + for i in range(76, 96): + annos_new[-1].append(anno[i*2]) + annos_new[-1].append(anno[i*2+1]) + + with open(os.path.join(root_folder, data_name, 'test.txt'), 'w') as f: + for anno in annos_new: + f.write(' '.join(anno)+'\n') + + +def gen_data(root_folder, data_name, target_size): + if not os.path.exists(os.path.join(root_folder, data_name, 'images_train')): + os.mkdir(os.path.join(root_folder, data_name, 'images_train')) + if not os.path.exists(os.path.join(root_folder, data_name, 'images_test')): + os.mkdir(os.path.join(root_folder, data_name, 'images_test')) + + ################################################################################################################ + if data_name == 'data_300W': + folders_train = ['afw', 'helen/trainset', 'lfpw/trainset'] + annos_train = {} + for folder_train in folders_train: + all_files = sorted(os.listdir(os.path.join(root_folder, data_name, folder_train))) + image_files = [x for x in all_files if '.pts' not in x] + label_files = [x for x in all_files if '.pts' in x] + assert len(image_files) == len(label_files) + for image_name, label_name in zip(image_files, label_files): + print(image_name) + image_crop, anno = process_300w(os.path.join(root_folder, 'data_300W'), folder_train, image_name, label_name, target_size) + image_crop_name = folder_train.replace('/', '_')+'_'+image_name + cv2.imwrite(os.path.join(root_folder, data_name, 'images_train', image_crop_name), image_crop) + annos_train[image_crop_name] = anno + with open(os.path.join(root_folder, data_name, 'train.txt'), 'w') as f: + for image_crop_name, anno in annos_train.items(): + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + + + folders_test = ['helen/testset', 'lfpw/testset', 'ibug'] + annos_test = {} + for folder_test in folders_test: + all_files = sorted(os.listdir(os.path.join(root_folder, data_name, folder_test))) + image_files = [x for x in all_files if '.pts' not in x] + label_files = [x for x in all_files if '.pts' in x] + assert len(image_files) == len(label_files) + for image_name, label_name in zip(image_files, label_files): + print(image_name) + image_crop, anno = process_300w(os.path.join(root_folder, 'data_300W'), folder_test, image_name, label_name, target_size) + image_crop_name = folder_test.replace('/', '_')+'_'+image_name + cv2.imwrite(os.path.join(root_folder, data_name, 'images_test', image_crop_name), image_crop) + annos_test[image_crop_name] = anno + with open(os.path.join(root_folder, data_name, 'test.txt'), 'w') as f: + for image_crop_name, anno in annos_test.items(): + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + + annos = None + with open(os.path.join(root_folder, data_name, 'test.txt'), 'r') as f: + annos = f.readlines() + with open(os.path.join(root_folder, data_name, 'test_common.txt'), 'w') as f: + for anno in annos: + if not 'ibug' in anno: + f.write(anno) + with open(os.path.join(root_folder, data_name, 'test_challenge.txt'), 'w') as f: + for anno in annos: + if 'ibug' in anno: + f.write(anno) + + gen_meanface(root_folder, data_name) + ################################################################################################################ + elif data_name == 'COFW': + train_file = 'COFW_train_color.mat' + train_mat = hdf5storage.loadmat(os.path.join(root_folder, 'COFW', train_file)) + images = train_mat['IsTr'] + bboxes = train_mat['bboxesTr'] + annos = train_mat['phisTr'] + + count = 1 + with open(os.path.join(root_folder, 'COFW', 'train.txt'), 'w') as f: + for i in range(images.shape[0]): + image = images[i, 0] + # grayscale + if len(image.shape) == 2: + image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) + # swap rgb channel to bgr + else: + image = image[:,:,::-1] + bbox = bboxes[i, :] + anno = annos[i, :] + image_crop, anno = process_cofw(image, bbox, anno, target_size) + pad_num = 4-len(str(count)) + image_crop_name = 'cofw_train_' + '0' * pad_num + str(count) + '.jpg' + print(image_crop_name) + cv2.imwrite(os.path.join(root_folder, 'COFW', 'images_train', image_crop_name), image_crop) + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + count += 1 + + test_file = 'COFW_test_color.mat' + test_mat = hdf5storage.loadmat(os.path.join(root_folder, 'COFW', test_file)) + images = test_mat['IsT'] + bboxes = test_mat['bboxesT'] + annos = test_mat['phisT'] + + count = 1 + with open(os.path.join(root_folder, 'COFW', 'test.txt'), 'w') as f: + for i in range(images.shape[0]): + image = images[i, 0] + # grayscale + if len(image.shape) == 2: + image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) + # swap rgb channel to bgr + else: + image = image[:,:,::-1] + bbox = bboxes[i, :] + anno = annos[i, :] + image_crop, anno = process_cofw(image, bbox, anno, target_size) + pad_num = 4-len(str(count)) + image_crop_name = 'cofw_test_' + '0' * pad_num + str(count) + '.jpg' + print(image_crop_name) + cv2.imwrite(os.path.join(root_folder, 'COFW', 'images_test', image_crop_name), image_crop) + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + count += 1 + gen_meanface(root_folder, data_name) + ################################################################################################################ + elif data_name == 'WFLW': + train_file = 'list_98pt_rect_attr_train.txt' + with open(os.path.join(root_folder, 'WFLW', 'WFLW_annotations', 'list_98pt_rect_attr_train_test', train_file), 'r') as f: + annos_train = f.readlines() + annos_train = [x.strip().split() for x in annos_train] + count = 1 + with open(os.path.join(root_folder, 'WFLW', 'train.txt'), 'w') as f: + for anno_train in annos_train: + image_crop, anno = process_wflw(anno_train, target_size) + pad_num = 4-len(str(count)) + image_crop_name = 'wflw_train_' + '0' * pad_num + str(count) + '.jpg' + print(image_crop_name) + cv2.imwrite(os.path.join(root_folder, 'WFLW', 'images_train', image_crop_name), image_crop) + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + count += 1 + + test_file = 'list_98pt_rect_attr_test.txt' + with open(os.path.join(root_folder, 'WFLW', 'WFLW_annotations', 'list_98pt_rect_attr_train_test', test_file), 'r') as f: + annos_test = f.readlines() + annos_test = [x.strip().split() for x in annos_test] + names_mapping = {} + count = 1 + with open(os.path.join(root_folder, 'WFLW', 'test.txt'), 'w') as f: + for anno_test in annos_test: + image_crop, anno = process_wflw(anno_test, target_size) + pad_num = 4-len(str(count)) + image_crop_name = 'wflw_test_' + '0' * pad_num + str(count) + '.jpg' + print(image_crop_name) + names_mapping[anno_test[0]+'_'+anno_test[-1]] = [image_crop_name, anno] + cv2.imwrite(os.path.join(root_folder, 'WFLW', 'images_test', image_crop_name), image_crop) + f.write(image_crop_name+' ') + for x,y in list(anno): + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + count += 1 + + test_pose_file = 'list_98pt_test_largepose.txt' + with open(os.path.join(root_folder, 'WFLW', 'WFLW_annotations', 'list_98pt_test', test_pose_file), 'r') as f: + annos_pose_test = f.readlines() + names_pose = [x.strip().split() for x in annos_pose_test] + names_pose = [x[0]+'_'+x[-1] for x in names_pose] + with open(os.path.join(root_folder, 'WFLW', 'test_pose.txt'), 'w') as f: + for name_pose in names_pose: + if name_pose in names_mapping: + image_crop_name, anno = names_mapping[name_pose] + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + else: + print('error!') + exit(0) + + test_expr_file = 'list_98pt_test_expression.txt' + with open(os.path.join(root_folder, 'WFLW', 'WFLW_annotations', 'list_98pt_test', test_expr_file), 'r') as f: + annos_expr_test = f.readlines() + names_expr = [x.strip().split() for x in annos_expr_test] + names_expr = [x[0]+'_'+x[-1] for x in names_expr] + with open(os.path.join(root_folder, 'WFLW', 'test_expr.txt'), 'w') as f: + for name_expr in names_expr: + if name_expr in names_mapping: + image_crop_name, anno = names_mapping[name_expr] + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + else: + print('error!') + exit(0) + + test_illu_file = 'list_98pt_test_illumination.txt' + with open(os.path.join(root_folder, 'WFLW', 'WFLW_annotations', 'list_98pt_test', test_illu_file), 'r') as f: + annos_illu_test = f.readlines() + names_illu = [x.strip().split() for x in annos_illu_test] + names_illu = [x[0]+'_'+x[-1] for x in names_illu] + with open(os.path.join(root_folder, 'WFLW', 'test_illu.txt'), 'w') as f: + for name_illu in names_illu: + if name_illu in names_mapping: + image_crop_name, anno = names_mapping[name_illu] + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + else: + print('error!') + exit(0) + + test_mu_file = 'list_98pt_test_makeup.txt' + with open(os.path.join(root_folder, 'WFLW', 'WFLW_annotations', 'list_98pt_test', test_mu_file), 'r') as f: + annos_mu_test = f.readlines() + names_mu = [x.strip().split() for x in annos_mu_test] + names_mu = [x[0]+'_'+x[-1] for x in names_mu] + with open(os.path.join(root_folder, 'WFLW', 'test_mu.txt'), 'w') as f: + for name_mu in names_mu: + if name_mu in names_mapping: + image_crop_name, anno = names_mapping[name_mu] + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + else: + print('error!') + exit(0) + + test_occu_file = 'list_98pt_test_occlusion.txt' + with open(os.path.join(root_folder, 'WFLW', 'WFLW_annotations', 'list_98pt_test', test_occu_file), 'r') as f: + annos_occu_test = f.readlines() + names_occu = [x.strip().split() for x in annos_occu_test] + names_occu = [x[0]+'_'+x[-1] for x in names_occu] + with open(os.path.join(root_folder, 'WFLW', 'test_occu.txt'), 'w') as f: + for name_occu in names_occu: + if name_occu in names_mapping: + image_crop_name, anno = names_mapping[name_occu] + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + else: + print('error!') + exit(0) + + + test_blur_file = 'list_98pt_test_blur.txt' + with open(os.path.join(root_folder, 'WFLW', 'WFLW_annotations', 'list_98pt_test', test_blur_file), 'r') as f: + annos_blur_test = f.readlines() + names_blur = [x.strip().split() for x in annos_blur_test] + names_blur = [x[0]+'_'+x[-1] for x in names_blur] + with open(os.path.join(root_folder, 'WFLW', 'test_blur.txt'), 'w') as f: + for name_blur in names_blur: + if name_blur in names_mapping: + image_crop_name, anno = names_mapping[name_blur] + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + else: + print('error!') + exit(0) + gen_meanface(root_folder, data_name) + ################################################################################################################ + elif data_name == 'AFLW': + mat = hdf5storage.loadmat('../data/AFLW/AFLWinfo_release.mat') + bboxes = mat['bbox'] + annos = mat['data'] + mask_new = mat['mask_new'] + nameList = mat['nameList'] + ra = mat['ra'][0] + train_indices = ra[:20000] + test_indices = ra[20000:] + + with open(os.path.join(root_folder, 'AFLW', 'train.txt'), 'w') as f: + for index in train_indices: + # from matlab index + image_name = nameList[index-1][0][0] + bbox = bboxes[index-1] + anno = annos[index-1] + image_crop, anno = process_aflw(root_folder, image_name, bbox, anno, target_size) + pad_num = 5-len(str(index)) + image_crop_name = 'aflw_train_' + '0' * pad_num + str(index) + '.jpg' + print(image_crop_name) + cv2.imwrite(os.path.join(root_folder, 'AFLW', 'images_train', image_crop_name), image_crop) + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + + with open(os.path.join(root_folder, 'AFLW', 'test.txt'), 'w') as f: + for index in test_indices: + # from matlab index + image_name = nameList[index-1][0][0] + bbox = bboxes[index-1] + anno = annos[index-1] + image_crop, anno = process_aflw(root_folder, image_name, bbox, anno, target_size) + pad_num = 5-len(str(index)) + image_crop_name = 'aflw_test_' + '0' * pad_num + str(index) + '.jpg' + print(image_crop_name) + cv2.imwrite(os.path.join(root_folder, 'AFLW', 'images_test', image_crop_name), image_crop) + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + gen_meanface(root_folder, data_name) + else: + print('Wrong data!') + +if __name__ == '__main__': + if len(sys.argv) < 2: + print('please input the data name.') + print('1. data_300W') + print('2. COFW') + print('3. WFLW') + print('4. AFLW') + exit(0) + else: + data_name = sys.argv[1] + gen_data('../data', data_name, 256) + + diff --git a/third_party/PIPNet/lib/preprocess_gssl.py b/third_party/PIPNet/lib/preprocess_gssl.py new file mode 100644 index 0000000000000000000000000000000000000000..ea804e6874f0ac49ba53bac27db83c8f3285e43f --- /dev/null +++ b/third_party/PIPNet/lib/preprocess_gssl.py @@ -0,0 +1,544 @@ +import os, cv2 +import hdf5storage +import numpy as np +import sys + +def process_300w(root_folder, folder_name, image_name, label_name, target_size): + image_path = os.path.join(root_folder, folder_name, image_name) + label_path = os.path.join(root_folder, folder_name, label_name) + + with open(label_path, 'r') as ff: + anno = ff.readlines()[3:-1] + anno = [x.strip().split() for x in anno] + anno = [[int(float(x[0])), int(float(x[1]))] for x in anno] + image = cv2.imread(image_path) + image_height, image_width, _ = image.shape + anno_x = [x[0] for x in anno] + anno_y = [x[1] for x in anno] + bbox_xmin = min(anno_x) + bbox_ymin = min(anno_y) + bbox_xmax = max(anno_x) + bbox_ymax = max(anno_y) + bbox_width = bbox_xmax - bbox_xmin + bbox_height = bbox_ymax - bbox_ymin + scale = 1.3 + bbox_xmin -= int((scale-1)/2*bbox_width) + bbox_ymin -= int((scale-1)/2*bbox_height) + bbox_width *= scale + bbox_height *= scale + bbox_width = int(bbox_width) + bbox_height = int(bbox_height) + bbox_xmin = max(bbox_xmin, 0) + bbox_ymin = max(bbox_ymin, 0) + bbox_width = min(bbox_width, image_width-bbox_xmin-1) + bbox_height = min(bbox_height, image_height-bbox_ymin-1) + anno = [[(x-bbox_xmin)/bbox_width, (y-bbox_ymin)/bbox_height] for x,y in anno] + + bbox_xmax = bbox_xmin + bbox_width + bbox_ymax = bbox_ymin + bbox_height + image_crop = image[bbox_ymin:bbox_ymax, bbox_xmin:bbox_xmax, :] + image_crop = cv2.resize(image_crop, (target_size, target_size)) + return image_crop, anno + +def process_wflw(anno, target_size): + image_name = anno[-1] + image_path = os.path.join('..', 'data', 'WFLW', 'WFLW_images', image_name) + image = cv2.imread(image_path) + image_height, image_width, _ = image.shape + lms = anno[:196] + lms = [float(x) for x in lms] + lms_x = lms[0::2] + lms_y = lms[1::2] + lms_x = [x if x >=0 else 0 for x in lms_x] + lms_x = [x if x <=image_width else image_width for x in lms_x] + lms_y = [y if y >=0 else 0 for y in lms_y] + lms_y = [y if y <=image_height else image_height for y in lms_y] + lms = [[x,y] for x,y in zip(lms_x, lms_y)] + lms = [x for z in lms for x in z] + bbox = anno[196:200] + bbox = [float(x) for x in bbox] + attrs = anno[200:206] + attrs = np.array([int(x) for x in attrs]) + bbox_xmin, bbox_ymin, bbox_xmax, bbox_ymax = bbox + + width = bbox_xmax - bbox_xmin + height = bbox_ymax - bbox_ymin + scale = 1.2 + bbox_xmin -= width * (scale-1)/2 + # remove a part of top area for alignment, see details in paper + bbox_ymin += height * (scale-1)/2 + bbox_xmax += width * (scale-1)/2 + bbox_ymax += height * (scale-1)/2 + bbox_xmin = max(bbox_xmin, 0) + bbox_ymin = max(bbox_ymin, 0) + bbox_xmax = min(bbox_xmax, image_width-1) + bbox_ymax = min(bbox_ymax, image_height-1) + width = bbox_xmax - bbox_xmin + height = bbox_ymax - bbox_ymin + image_crop = image[int(bbox_ymin):int(bbox_ymax), int(bbox_xmin):int(bbox_xmax), :] + image_crop = cv2.resize(image_crop, (target_size, target_size)) + + tmp1 = [bbox_xmin, bbox_ymin]*98 + tmp1 = np.array(tmp1) + tmp2 = [width, height]*98 + tmp2 = np.array(tmp2) + lms = np.array(lms) - tmp1 + lms = lms / tmp2 + lms = lms.tolist() + lms = zip(lms[0::2], lms[1::2]) + return image_crop, list(lms) + +def process_celeba(root_folder, image_name, bbox, target_size): + image = cv2.imread(os.path.join(root_folder, 'CELEBA', 'img_celeba', image_name)) + image_height, image_width, _ = image.shape + xmin, ymin, xmax, ymax = bbox + width = xmax - xmin + 1 + height = ymax - ymin + 1 + scale = 1.2 + xmin -= width * (scale-1)/2 + # remove a part of top area for alignment, see details in paper + ymin += height * (scale+0.1-1)/2 + xmax += width * (scale-1)/2 + ymax += height * (scale-1)/2 + xmin = max(xmin, 0) + ymin = max(ymin, 0) + xmax = min(xmax, image_width-1) + ymax = min(ymax, image_height-1) + image_crop = image[int(ymin):int(ymax), int(xmin):int(xmax), :] + image_crop = cv2.resize(image_crop, (target_size, target_size)) + return image_crop + +def process_cofw_68_train(image, bbox, anno, target_size): + image_height, image_width, _ = image.shape + anno_x = anno[:29] + anno_y = anno[29:58] + xmin, ymin, width, height = bbox + xmax = xmin + width -1 + ymax = ymin + height -1 + scale = 1.3 + xmin -= width * (scale-1)/2 + ymin -= height * (scale-1)/2 + xmax += width * (scale-1)/2 + ymax += height * (scale-1)/2 + xmin = max(xmin, 0) + ymin = max(ymin, 0) + xmax = min(xmax, image_width-1) + ymax = min(ymax, image_height-1) + anno_x = (anno_x - xmin) / (xmax - xmin) + anno_y = (anno_y - ymin) / (ymax - ymin) + anno = np.concatenate([anno_x.reshape(-1,1), anno_y.reshape(-1,1)], axis=1) + anno = list(anno) + anno = [list(x) for x in anno] + image_crop = image[int(ymin):int(ymax), int(xmin):int(xmax), :] + image_crop = cv2.resize(image_crop, (target_size, target_size)) + return image_crop, anno + +def process_cofw_68_test(image, bbox, anno, target_size): + image_height, image_width, _ = image.shape + anno_x = anno[:,0].flatten() + anno_y = anno[:,1].flatten() + + xmin, ymin, width, height = bbox + xmax = xmin + width -1 + ymax = ymin + height -1 + + scale = 1.3 + xmin -= width * (scale-1)/2 + ymin -= height * (scale-1)/2 + xmax += width * (scale-1)/2 + ymax += height * (scale-1)/2 + xmin = max(xmin, 0) + ymin = max(ymin, 0) + xmax = min(xmax, image_width-1) + ymax = min(ymax, image_height-1) + anno_x = (anno_x - xmin) / (xmax - xmin) + anno_y = (anno_y - ymin) / (ymax - ymin) + anno = np.concatenate([anno_x.reshape(-1,1), anno_y.reshape(-1,1)], axis=1) + anno = list(anno) + anno = [list(x) for x in anno] + image_crop = image[int(ymin):int(ymax), int(xmin):int(xmax), :] + image_crop = cv2.resize(image_crop, (target_size, target_size)) + return image_crop, anno + +def gen_meanface(root_folder, data_name): + with open(os.path.join(root_folder, data_name, 'train_300W.txt'), 'r') as f: + annos = f.readlines() + annos = [x.strip().split()[1:] for x in annos] + annos = [[float(x) for x in anno] for anno in annos] + annos = np.array(annos) + meanface = np.mean(annos, axis=0) + meanface = meanface.tolist() + meanface = [str(x) for x in meanface] + + with open(os.path.join(root_folder, data_name, 'meanface.txt'), 'w') as f: + f.write(' '.join(meanface)) + +def convert_wflw(root_folder, data_name): + with open(os.path.join(root_folder, data_name, 'test_WFLW_98.txt'), 'r') as f: + annos = f.readlines() + annos = [x.strip().split() for x in annos] + annos_new = [] + for anno in annos: + annos_new.append([]) + # name + annos_new[-1].append(anno[0]) + anno = anno[1:] + # jaw + for i in range(17): + annos_new[-1].append(anno[i*2*2]) + annos_new[-1].append(anno[i*2*2+1]) + # left eyebrow + annos_new[-1].append(anno[33*2]) + annos_new[-1].append(anno[33*2+1]) + annos_new[-1].append(anno[34*2]) + annos_new[-1].append(str((float(anno[34*2+1])+float(anno[41*2+1]))/2)) + annos_new[-1].append(anno[35*2]) + annos_new[-1].append(str((float(anno[35*2+1])+float(anno[40*2+1]))/2)) + annos_new[-1].append(anno[36*2]) + annos_new[-1].append(str((float(anno[36*2+1])+float(anno[39*2+1]))/2)) + annos_new[-1].append(anno[37*2]) + annos_new[-1].append(str((float(anno[37*2+1])+float(anno[38*2+1]))/2)) + # right eyebrow + annos_new[-1].append(anno[42*2]) + annos_new[-1].append(str((float(anno[42*2+1])+float(anno[50*2+1]))/2)) + annos_new[-1].append(anno[43*2]) + annos_new[-1].append(str((float(anno[43*2+1])+float(anno[49*2+1]))/2)) + annos_new[-1].append(anno[44*2]) + annos_new[-1].append(str((float(anno[44*2+1])+float(anno[48*2+1]))/2)) + annos_new[-1].append(anno[45*2]) + annos_new[-1].append(str((float(anno[45*2+1])+float(anno[47*2+1]))/2)) + annos_new[-1].append(anno[46*2]) + annos_new[-1].append(anno[46*2+1]) + # nose + for i in range(51, 60): + annos_new[-1].append(anno[i*2]) + annos_new[-1].append(anno[i*2+1]) + # left eye + annos_new[-1].append(anno[60*2]) + annos_new[-1].append(anno[60*2+1]) + annos_new[-1].append(str(0.666*float(anno[61*2])+0.333*float(anno[62*2]))) + annos_new[-1].append(str(0.666*float(anno[61*2+1])+0.333*float(anno[62*2+1]))) + annos_new[-1].append(str(0.666*float(anno[63*2])+0.333*float(anno[62*2]))) + annos_new[-1].append(str(0.666*float(anno[63*2+1])+0.333*float(anno[62*2+1]))) + annos_new[-1].append(anno[64*2]) + annos_new[-1].append(anno[64*2+1]) + annos_new[-1].append(str(0.666*float(anno[65*2])+0.333*float(anno[66*2]))) + annos_new[-1].append(str(0.666*float(anno[65*2+1])+0.333*float(anno[66*2+1]))) + annos_new[-1].append(str(0.666*float(anno[67*2])+0.333*float(anno[66*2]))) + annos_new[-1].append(str(0.666*float(anno[67*2+1])+0.333*float(anno[66*2+1]))) + # right eye + annos_new[-1].append(anno[68*2]) + annos_new[-1].append(anno[68*2+1]) + annos_new[-1].append(str(0.666*float(anno[69*2])+0.333*float(anno[70*2]))) + annos_new[-1].append(str(0.666*float(anno[69*2+1])+0.333*float(anno[70*2+1]))) + annos_new[-1].append(str(0.666*float(anno[71*2])+0.333*float(anno[70*2]))) + annos_new[-1].append(str(0.666*float(anno[71*2+1])+0.333*float(anno[70*2+1]))) + annos_new[-1].append(anno[72*2]) + annos_new[-1].append(anno[72*2+1]) + annos_new[-1].append(str(0.666*float(anno[73*2])+0.333*float(anno[74*2]))) + annos_new[-1].append(str(0.666*float(anno[73*2+1])+0.333*float(anno[74*2+1]))) + annos_new[-1].append(str(0.666*float(anno[75*2])+0.333*float(anno[74*2]))) + annos_new[-1].append(str(0.666*float(anno[75*2+1])+0.333*float(anno[74*2+1]))) + # mouth + for i in range(76, 96): + annos_new[-1].append(anno[i*2]) + annos_new[-1].append(anno[i*2+1]) + + with open(os.path.join(root_folder, data_name, 'test_WFLW.txt'), 'w') as f: + for anno in annos_new: + f.write(' '.join(anno)+'\n') + +def gen_data(root_folder, data_name, target_size): + if not os.path.exists(os.path.join(root_folder, data_name, 'images_train')): + os.mkdir(os.path.join(root_folder, data_name, 'images_train')) + if not os.path.exists(os.path.join(root_folder, data_name, 'images_test')): + os.mkdir(os.path.join(root_folder, data_name, 'images_test')) + ################################################################################################################ + if data_name == 'CELEBA': + os.system('rmdir ../data/CELEBA/images_test') + with open(os.path.join(root_folder, data_name, 'celeba_bboxes.txt'), 'r') as f: + bboxes = f.readlines() + + bboxes = [x.strip().split() for x in bboxes] + with open(os.path.join(root_folder, data_name, 'train.txt'), 'w') as f: + for bbox in bboxes: + image_name = bbox[0] + print(image_name) + f.write(image_name+'\n') + bbox = bbox[1:] + bbox = [int(x) for x in bbox] + image_crop = process_celeba(root_folder, image_name, bbox, target_size) + cv2.imwrite(os.path.join(root_folder, data_name, 'images_train', image_name), image_crop) + ################################################################################################################ + elif data_name == 'data_300W_CELEBA': + os.system('cp -r ../data/CELEBA/images_train ../data/data_300W_CELEBA/.') + os.system('cp ../data/CELEBA/train.txt ../data/data_300W_CELEBA/train_CELEBA.txt') + + os.system('rmdir ../data/data_300W_CELEBA/images_test') + if not os.path.exists(os.path.join(root_folder, data_name, 'images_test_300W')): + os.mkdir(os.path.join(root_folder, data_name, 'images_test_300W')) + if not os.path.exists(os.path.join(root_folder, data_name, 'images_test_COFW')): + os.mkdir(os.path.join(root_folder, data_name, 'images_test_COFW')) + if not os.path.exists(os.path.join(root_folder, data_name, 'images_test_WFLW')): + os.mkdir(os.path.join(root_folder, data_name, 'images_test_WFLW')) + + # train for data_300W + folders_train = ['afw', 'helen/trainset', 'lfpw/trainset'] + annos_train = {} + for folder_train in folders_train: + all_files = sorted(os.listdir(os.path.join(root_folder, 'data_300W', folder_train))) + image_files = [x for x in all_files if '.pts' not in x] + label_files = [x for x in all_files if '.pts' in x] + assert len(image_files) == len(label_files) + for image_name, label_name in zip(image_files, label_files): + print(image_name) + image_crop, anno = process_300w(os.path.join(root_folder, 'data_300W'), folder_train, image_name, label_name, target_size) + image_crop_name = folder_train.replace('/', '_')+'_'+image_name + cv2.imwrite(os.path.join(root_folder, data_name, 'images_train', image_crop_name), image_crop) + annos_train[image_crop_name] = anno + with open(os.path.join(root_folder, data_name, 'train_300W.txt'), 'w') as f: + for image_crop_name, anno in annos_train.items(): + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + + # test for data_300W + folders_test = ['helen/testset', 'lfpw/testset', 'ibug'] + annos_test = {} + for folder_test in folders_test: + all_files = sorted(os.listdir(os.path.join(root_folder, 'data_300W', folder_test))) + image_files = [x for x in all_files if '.pts' not in x] + label_files = [x for x in all_files if '.pts' in x] + assert len(image_files) == len(label_files) + for image_name, label_name in zip(image_files, label_files): + print(image_name) + image_crop, anno = process_300w(os.path.join(root_folder, 'data_300W'), folder_test, image_name, label_name, target_size) + image_crop_name = folder_test.replace('/', '_')+'_'+image_name + cv2.imwrite(os.path.join(root_folder, data_name, 'images_test_300W', image_crop_name), image_crop) + annos_test[image_crop_name] = anno + with open(os.path.join(root_folder, data_name, 'test_300W.txt'), 'w') as f: + for image_crop_name, anno in annos_test.items(): + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + + # test for COFW_68 + test_mat = hdf5storage.loadmat(os.path.join('../data/COFW', 'COFW_test_color.mat')) + images = test_mat['IsT'] + + bboxes_mat = hdf5storage.loadmat(os.path.join('../data/data_300W_CELEBA', 'cofw68_test_bboxes.mat')) + bboxes = bboxes_mat['bboxes'] + image_num = images.shape[0] + with open('../data/data_300W_CELEBA/test_COFW.txt', 'w') as f: + for i in range(image_num): + image = images[i,0] + # grayscale + if len(image.shape) == 2: + image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) + # swap rgb channel to bgr + else: + image = image[:,:,::-1] + + bbox = bboxes[i,:] + anno_mat = hdf5storage.loadmat(os.path.join('../data/data_300W_CELEBA/cofw68_test_annotations', str(i+1)+'_points.mat')) + anno = anno_mat['Points'] + image_crop, anno = process_cofw_68_test(image, bbox, anno, target_size) + pad_num = 4-len(str(i+1)) + image_crop_name = 'cofw_test_' + '0' * pad_num + str(i+1) + '.jpg' + cv2.imwrite(os.path.join('../data/data_300W_CELEBA/images_test_COFW', image_crop_name), image_crop) + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + + # test for WFLW_68 + test_file = 'list_98pt_rect_attr_test.txt' + with open(os.path.join(root_folder, 'WFLW', 'WFLW_annotations', 'list_98pt_rect_attr_train_test', test_file), 'r') as f: + annos_test = f.readlines() + annos_test = [x.strip().split() for x in annos_test] + names_mapping = {} + count = 1 + with open(os.path.join(root_folder, 'data_300W_CELEBA', 'test_WFLW_98.txt'), 'w') as f: + for anno_test in annos_test: + image_crop, anno = process_wflw(anno_test, target_size) + pad_num = 4-len(str(count)) + image_crop_name = 'wflw_test_' + '0' * pad_num + str(count) + '.jpg' + print(image_crop_name) + names_mapping[anno_test[0]+'_'+anno_test[-1]] = [image_crop_name, anno] + cv2.imwrite(os.path.join(root_folder, data_name, 'images_test_WFLW', image_crop_name), image_crop) + f.write(image_crop_name+' ') + for x,y in list(anno): + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + count += 1 + + convert_wflw(root_folder, data_name) + + gen_meanface(root_folder, data_name) + ################################################################################################################ + elif data_name == 'data_300W_COFW_WFLW': + + os.system('rmdir ../data/data_300W_COFW_WFLW/images_test') + if not os.path.exists(os.path.join(root_folder, data_name, 'images_test_300W')): + os.mkdir(os.path.join(root_folder, data_name, 'images_test_300W')) + if not os.path.exists(os.path.join(root_folder, data_name, 'images_test_COFW')): + os.mkdir(os.path.join(root_folder, data_name, 'images_test_COFW')) + if not os.path.exists(os.path.join(root_folder, data_name, 'images_test_WFLW')): + os.mkdir(os.path.join(root_folder, data_name, 'images_test_WFLW')) + + # train for data_300W + folders_train = ['afw', 'helen/trainset', 'lfpw/trainset'] + annos_train = {} + for folder_train in folders_train: + all_files = sorted(os.listdir(os.path.join(root_folder, 'data_300W', folder_train))) + image_files = [x for x in all_files if '.pts' not in x] + label_files = [x for x in all_files if '.pts' in x] + assert len(image_files) == len(label_files) + for image_name, label_name in zip(image_files, label_files): + print(image_name) + image_crop, anno = process_300w(os.path.join(root_folder, 'data_300W'), folder_train, image_name, label_name, target_size) + image_crop_name = folder_train.replace('/', '_')+'_'+image_name + cv2.imwrite(os.path.join(root_folder, data_name, 'images_train', image_crop_name), image_crop) + annos_train[image_crop_name] = anno + with open(os.path.join(root_folder, data_name, 'train_300W.txt'), 'w') as f: + for image_crop_name, anno in annos_train.items(): + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + + # test for data_300W + folders_test = ['helen/testset', 'lfpw/testset', 'ibug'] + annos_test = {} + for folder_test in folders_test: + all_files = sorted(os.listdir(os.path.join(root_folder, 'data_300W', folder_test))) + image_files = [x for x in all_files if '.pts' not in x] + label_files = [x for x in all_files if '.pts' in x] + assert len(image_files) == len(label_files) + for image_name, label_name in zip(image_files, label_files): + print(image_name) + image_crop, anno = process_300w(os.path.join(root_folder, 'data_300W'), folder_test, image_name, label_name, target_size) + image_crop_name = folder_test.replace('/', '_')+'_'+image_name + cv2.imwrite(os.path.join(root_folder, data_name, 'images_test_300W', image_crop_name), image_crop) + annos_test[image_crop_name] = anno + with open(os.path.join(root_folder, data_name, 'test_300W.txt'), 'w') as f: + for image_crop_name, anno in annos_test.items(): + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + + # train for COFW_68 + ################### + train_file = 'COFW_train_color.mat' + train_mat = hdf5storage.loadmat(os.path.join(root_folder, 'COFW', train_file)) + images = train_mat['IsTr'] + bboxes = train_mat['bboxesTr'] + annos = train_mat['phisTr'] + + count = 1 + with open('../data/data_300W_COFW_WFLW/train_COFW.txt', 'w') as f: + for i in range(images.shape[0]): + image = images[i, 0] + # grayscale + if len(image.shape) == 2: + image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) + # swap rgb channel to bgr + else: + image = image[:,:,::-1] + bbox = bboxes[i, :] + anno = annos[i, :] + image_crop, anno = process_cofw_68_train(image, bbox, anno, target_size) + pad_num = 4-len(str(count)) + image_crop_name = 'cofw_train_' + '0' * pad_num + str(count) + '.jpg' + f.write(image_crop_name+'\n') + cv2.imwrite(os.path.join(root_folder, 'data_300W_COFW_WFLW', 'images_train', image_crop_name), image_crop) + count += 1 + ################### + + # test for COFW_68 + test_mat = hdf5storage.loadmat(os.path.join('../data/COFW', 'COFW_test_color.mat')) + images = test_mat['IsT'] + + bboxes_mat = hdf5storage.loadmat(os.path.join('../data/data_300W_COFW_WFLW', 'cofw68_test_bboxes.mat')) + bboxes = bboxes_mat['bboxes'] + image_num = images.shape[0] + with open('../data/data_300W_COFW_WFLW/test_COFW.txt', 'w') as f: + for i in range(image_num): + image = images[i,0] + # grayscale + if len(image.shape) == 2: + image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) + # swap rgb channel to bgr + else: + image = image[:,:,::-1] + + bbox = bboxes[i,:] + anno_mat = hdf5storage.loadmat(os.path.join('../data/data_300W_COFW_WFLW/cofw68_test_annotations', str(i+1)+'_points.mat')) + anno = anno_mat['Points'] + image_crop, anno = process_cofw_68_test(image, bbox, anno, target_size) + pad_num = 4-len(str(i+1)) + image_crop_name = 'cofw_test_' + '0' * pad_num + str(i+1) + '.jpg' + cv2.imwrite(os.path.join('../data/data_300W_COFW_WFLW/images_test_COFW', image_crop_name), image_crop) + f.write(image_crop_name+' ') + for x,y in anno: + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + + # train for WFLW_68 + train_file = 'list_98pt_rect_attr_train.txt' + with open(os.path.join('../data', 'WFLW', 'WFLW_annotations', 'list_98pt_rect_attr_train_test', train_file), 'r') as f: + annos_train = f.readlines() + annos_train = [x.strip().split() for x in annos_train] + count = 1 + with open('../data/data_300W_COFW_WFLW/train_WFLW.txt', 'w') as f: + for anno_train in annos_train: + image_crop, anno = process_wflw(anno_train, target_size) + pad_num = 4-len(str(count)) + image_crop_name = 'wflw_train_' + '0' * pad_num + str(count) + '.jpg' + print(image_crop_name) + f.write(image_crop_name+'\n') + cv2.imwrite(os.path.join(root_folder, 'data_300W_COFW_WFLW', 'images_train', image_crop_name), image_crop) + count += 1 + + # test for WFLW_68 + test_file = 'list_98pt_rect_attr_test.txt' + with open(os.path.join(root_folder, 'WFLW', 'WFLW_annotations', 'list_98pt_rect_attr_train_test', test_file), 'r') as f: + annos_test = f.readlines() + annos_test = [x.strip().split() for x in annos_test] + names_mapping = {} + count = 1 + with open(os.path.join(root_folder, 'data_300W_COFW_WFLW', 'test_WFLW_98.txt'), 'w') as f: + for anno_test in annos_test: + image_crop, anno = process_wflw(anno_test, target_size) + pad_num = 4-len(str(count)) + image_crop_name = 'wflw_test_' + '0' * pad_num + str(count) + '.jpg' + print(image_crop_name) + names_mapping[anno_test[0]+'_'+anno_test[-1]] = [image_crop_name, anno] + cv2.imwrite(os.path.join(root_folder, data_name, 'images_test_WFLW', image_crop_name), image_crop) + f.write(image_crop_name+' ') + for x,y in list(anno): + f.write(str(x)+' '+str(y)+' ') + f.write('\n') + count += 1 + + convert_wflw(root_folder, data_name) + + gen_meanface(root_folder, data_name) + else: + print('Wrong data!') + +if __name__ == '__main__': + if len(sys.argv) < 2: + print('please input the data name.') + print('1. CELEBA') + print('2. data_300W_CELEBA') + print('3. data_300W_COFW_WFLW') + exit(0) + else: + data_name = sys.argv[1] + gen_data('../data', data_name, 256) + + diff --git a/third_party/PIPNet/lib/tools.py b/third_party/PIPNet/lib/tools.py new file mode 100644 index 0000000000000000000000000000000000000000..26e7b02160b90a624e6e83d9192ca0e01c8901d3 --- /dev/null +++ b/third_party/PIPNet/lib/tools.py @@ -0,0 +1,174 @@ +import cv2 +import sys + + +from math import floor +from third_party.PIPNet.FaceBoxesV2.faceboxes_detector import * + +import torch +import torch.nn.parallel +import torch.utils.data +import torchvision.transforms as transforms +import torchvision.models as models + +from third_party.PIPNet.lib.networks import * +from third_party.PIPNet.lib.functions import * +from third_party.PIPNet.reverse_index import ri1, ri2 + + +make_abs_path = lambda fn: os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), fn)) + + +class Config: + def __init__(self): + self.det_head = "pip" + self.net_stride = 32 + self.batch_size = 16 + self.init_lr = 0.0001 + self.num_epochs = 60 + self.decay_steps = [30, 50] + self.input_size = 256 + self.backbone = "resnet101" + self.pretrained = True + self.criterion_cls = "l2" + self.criterion_reg = "l1" + self.cls_loss_weight = 10 + self.reg_loss_weight = 1 + self.num_lms = 98 + self.save_interval = self.num_epochs + self.num_nb = 10 + self.use_gpu = True + self.gpu_id = 3 + + +def get_lmk_model(): + + cfg = Config() + + resnet101 = models.resnet101(pretrained=cfg.pretrained) + net = Pip_resnet101( + resnet101, + cfg.num_nb, + num_lms=cfg.num_lms, + input_size=cfg.input_size, + net_stride=cfg.net_stride, + ) + + if cfg.use_gpu: + device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") + else: + device = torch.device("cpu") + net = net.to(device) + + weight_file = make_abs_path('../../../weights/PIPNet/epoch59.pth') + state_dict = torch.load(weight_file, map_location=device) + net.load_state_dict(state_dict) + + detector = FaceBoxesDetector( + "FaceBoxes", + make_abs_path("./../../weights/PIPNet/FaceBoxesV2.pth"), + use_gpu=torch.cuda.is_available(), + device=device, + ) + return net, detector + + +def demo_image( + image_file, + net, + detector, + input_size=256, + net_stride=32, + num_nb=10, + use_gpu=True, + device="cuda:0", +): + + my_thresh = 0.6 + det_box_scale = 1.2 + net.eval() + preprocess = transforms.Compose( + [ + transforms.Resize((256, 256)), + transforms.ToTensor(), + transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), + ] + ) + reverse_index1, reverse_index2, max_len = ri1, ri2, 17 + # image = cv2.imread(image_file) + image = image_file + image_height, image_width, _ = image.shape + detections, _ = detector.detect(image, my_thresh, 1) + lmks = [] + for i in range(len(detections)): + det_xmin = detections[i][2] + det_ymin = detections[i][3] + det_width = detections[i][4] + det_height = detections[i][5] + det_xmax = det_xmin + det_width - 1 + det_ymax = det_ymin + det_height - 1 + + det_xmin -= int(det_width * (det_box_scale - 1) / 2) + # remove a part of top area for alignment, see paper for details + det_ymin += int(det_height * (det_box_scale - 1) / 2) + det_xmax += int(det_width * (det_box_scale - 1) / 2) + det_ymax += int(det_height * (det_box_scale - 1) / 2) + det_xmin = max(det_xmin, 0) + det_ymin = max(det_ymin, 0) + det_xmax = min(det_xmax, image_width - 1) + det_ymax = min(det_ymax, image_height - 1) + det_width = det_xmax - det_xmin + 1 + det_height = det_ymax - det_ymin + 1 + + # cv2.rectangle(image, (det_xmin, det_ymin), (det_xmax, det_ymax), (0, 0, 255), 2) + + det_crop = image[det_ymin:det_ymax, det_xmin:det_xmax, :] + det_crop = cv2.resize(det_crop, (input_size, input_size)) + inputs = Image.fromarray(det_crop[:, :, ::-1].astype("uint8"), "RGB") + inputs = preprocess(inputs).unsqueeze(0) + inputs = inputs.to(device) + ( + lms_pred_x, + lms_pred_y, + lms_pred_nb_x, + lms_pred_nb_y, + outputs_cls, + max_cls, + ) = forward_pip(net, inputs, preprocess, input_size, net_stride, num_nb) + lms_pred = torch.cat((lms_pred_x, lms_pred_y), dim=1).flatten() + tmp_nb_x = lms_pred_nb_x[reverse_index1, reverse_index2].view(98, max_len) + tmp_nb_y = lms_pred_nb_y[reverse_index1, reverse_index2].view(98, max_len) + tmp_x = torch.mean(torch.cat((lms_pred_x, tmp_nb_x), dim=1), dim=1).view(-1, 1) + tmp_y = torch.mean(torch.cat((lms_pred_y, tmp_nb_y), dim=1), dim=1).view(-1, 1) + lms_pred_merge = torch.cat((tmp_x, tmp_y), dim=1).flatten() + lms_pred = lms_pred.cpu().numpy() + lms_pred_merge = lms_pred_merge.cpu().numpy() + lmk_ = [] + for i in range(98): + x_pred = lms_pred_merge[i * 2] * det_width + y_pred = lms_pred_merge[i * 2 + 1] * det_height + + # cv2.circle( + # image, + # (int(x_pred) + det_xmin, int(y_pred) + det_ymin), + # 1, + # (0, 0, 255), + # 1, + # ) + + lmk_.append([int(x_pred) + det_xmin, int(y_pred) + det_ymin]) + lmks.append(np.array(lmk_)) + + # image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) + # cv2.imwrite("./1_out.jpg", image_bgr) + + return lmks + + +if __name__ == "__main__": + net, detector = get_lmk_model() + demo_image( + "/apdcephfs/private_ahbanliang/codes/Real-ESRGAN-master/tmp_frames/yanikefu/frame00000046.png", + net, + detector, + ) diff --git a/third_party/PIPNet/lib/train.py b/third_party/PIPNet/lib/train.py new file mode 100644 index 0000000000000000000000000000000000000000..1bf2089a171500e948948f1cdfc12f91fca8686c --- /dev/null +++ b/third_party/PIPNet/lib/train.py @@ -0,0 +1,196 @@ +import cv2, os +import sys +sys.path.insert(0, '..') +import numpy as np +from PIL import Image +import logging +import copy +import importlib + +import torch +import torch.nn as nn +import torch.optim as optim +import torch.utils.data +import torch.nn.functional as F +import torchvision.transforms as transforms +import torchvision.datasets as datasets +import torchvision.models as models + +from networks import * +import data_utils +from functions import * +from mobilenetv3 import mobilenetv3_large + +if not len(sys.argv) == 2: + print('Format:') + print('python lib/train.py config_file') + exit(0) +experiment_name = sys.argv[1].split('/')[-1][:-3] +data_name = sys.argv[1].split('/')[-2] +config_path = '.experiments.{}.{}'.format(data_name, experiment_name) + +my_config = importlib.import_module(config_path, package='PIPNet') +Config = getattr(my_config, 'Config') +cfg = Config() +cfg.experiment_name = experiment_name +cfg.data_name = data_name + +os.environ['CUDA_VISIBLE_DEVICES'] = str(cfg.gpu_id) + +if not os.path.exists(os.path.join('./snapshots', cfg.data_name)): + os.mkdir(os.path.join('./snapshots', cfg.data_name)) +save_dir = os.path.join('./snapshots', cfg.data_name, cfg.experiment_name) +if not os.path.exists(save_dir): + os.mkdir(save_dir) + +if not os.path.exists(os.path.join('./logs', cfg.data_name)): + os.mkdir(os.path.join('./logs', cfg.data_name)) +log_dir = os.path.join('./logs', cfg.data_name, cfg.experiment_name) +if not os.path.exists(log_dir): + os.mkdir(log_dir) + +logging.basicConfig(filename=os.path.join(log_dir, 'train.log'), level=logging.INFO) + +print('###########################################') +print('experiment_name:', cfg.experiment_name) +print('data_name:', cfg.data_name) +print('det_head:', cfg.det_head) +print('net_stride:', cfg.net_stride) +print('batch_size:', cfg.batch_size) +print('init_lr:', cfg.init_lr) +print('num_epochs:', cfg.num_epochs) +print('decay_steps:', cfg.decay_steps) +print('input_size:', cfg.input_size) +print('backbone:', cfg.backbone) +print('pretrained:', cfg.pretrained) +print('criterion_cls:', cfg.criterion_cls) +print('criterion_reg:', cfg.criterion_reg) +print('cls_loss_weight:', cfg.cls_loss_weight) +print('reg_loss_weight:', cfg.reg_loss_weight) +print('num_lms:', cfg.num_lms) +print('save_interval:', cfg.save_interval) +print('num_nb:', cfg.num_nb) +print('use_gpu:', cfg.use_gpu) +print('gpu_id:', cfg.gpu_id) +print('###########################################') +logging.info('###########################################') +logging.info('experiment_name: {}'.format(cfg.experiment_name)) +logging.info('data_name: {}'.format(cfg.data_name)) +logging.info('det_head: {}'.format(cfg.det_head)) +logging.info('net_stride: {}'.format(cfg.net_stride)) +logging.info('batch_size: {}'.format(cfg.batch_size)) +logging.info('init_lr: {}'.format(cfg.init_lr)) +logging.info('num_epochs: {}'.format(cfg.num_epochs)) +logging.info('decay_steps: {}'.format(cfg.decay_steps)) +logging.info('input_size: {}'.format(cfg.input_size)) +logging.info('backbone: {}'.format(cfg.backbone)) +logging.info('pretrained: {}'.format(cfg.pretrained)) +logging.info('criterion_cls: {}'.format(cfg.criterion_cls)) +logging.info('criterion_reg: {}'.format(cfg.criterion_reg)) +logging.info('cls_loss_weight: {}'.format(cfg.cls_loss_weight)) +logging.info('reg_loss_weight: {}'.format(cfg.reg_loss_weight)) +logging.info('num_lms: {}'.format(cfg.num_lms)) +logging.info('save_interval: {}'.format(cfg.save_interval)) +logging.info('num_nb: {}'.format(cfg.num_nb)) +logging.info('use_gpu: {}'.format(cfg.use_gpu)) +logging.info('gpu_id: {}'.format(cfg.gpu_id)) +logging.info('###########################################') + +if cfg.det_head == 'pip': + meanface_indices, _, _, _ = get_meanface(os.path.join('data', cfg.data_name, 'meanface.txt'), cfg.num_nb) + + +if cfg.det_head == 'pip': + if cfg.backbone == 'resnet18': + resnet18 = models.resnet18(pretrained=cfg.pretrained) + net = Pip_resnet18(resnet18, cfg.num_nb, num_lms=cfg.num_lms, input_size=cfg.input_size, net_stride=cfg.net_stride) + elif cfg.backbone == 'resnet50': + resnet50 = models.resnet50(pretrained=cfg.pretrained) + net = Pip_resnet50(resnet50, cfg.num_nb, num_lms=cfg.num_lms, input_size=cfg.input_size, net_stride=cfg.net_stride) + elif cfg.backbone == 'resnet101': + resnet101 = models.resnet101(pretrained=cfg.pretrained) + net = Pip_resnet101(resnet101, cfg.num_nb, num_lms=cfg.num_lms, input_size=cfg.input_size, net_stride=cfg.net_stride) + elif cfg.backbone == 'mobilenet_v2': + mbnet = models.mobilenet_v2(pretrained=cfg.pretrained) + net = Pip_mbnetv2(mbnet, cfg.num_nb, num_lms=cfg.num_lms, input_size=cfg.input_size, net_stride=cfg.net_stride) + elif cfg.backbone == 'mobilenet_v3': + mbnet = mobilenetv3_large() + if cfg.pretrained: + mbnet.load_state_dict(torch.load('lib/mobilenetv3-large-1cd25616.pth')) + net = Pip_mbnetv3(mbnet, cfg.num_nb, num_lms=cfg.num_lms, input_size=cfg.input_size, net_stride=cfg.net_stride) + else: + print('No such backbone!') + exit(0) +else: + print('No such head:', cfg.det_head) + exit(0) + +if cfg.use_gpu: + device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") +else: + device = torch.device("cpu") +net = net.to(device) + +criterion_cls = None +if cfg.criterion_cls == 'l2': + criterion_cls = nn.MSELoss() +elif cfg.criterion_cls == 'l1': + criterion_cls = nn.L1Loss() +else: + print('No such cls criterion:', cfg.criterion_cls) + +criterion_reg = None +if cfg.criterion_reg == 'l1': + criterion_reg = nn.L1Loss() +elif cfg.criterion_reg == 'l2': + criterion_reg = nn.MSELoss() +else: + print('No such reg criterion:', cfg.criterion_reg) + +points_flip = None +if cfg.data_name == 'data_300W': + points_flip = [17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 28, 29, 30, 31, 36, 35, 34, 33, 32, 46, 45, 44, 43, 48, 47, 40, 39, 38, 37, 42, 41, 55, 54, 53, 52, 51, 50, 49, 60, 59, 58, 57, 56, 65, 64, 63, 62, 61, 68, 67, 66] + points_flip = (np.array(points_flip)-1).tolist() + assert len(points_flip) == 68 +elif cfg.data_name == 'WFLW': + points_flip = [32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 46, 45, 44, 43, 42, 50, 49, 48, 47, 37, 36, 35, 34, 33, 41, 40, 39, 38, 51, 52, 53, 54, 59, 58, 57, 56, 55, 72, 71, 70, 69, 68, 75, 74, 73, 64, 63, 62, 61, 60, 67, 66, 65, 82, 81, 80, 79, 78, 77, 76, 87, 86, 85, 84, 83, 92, 91, 90, 89, 88, 95, 94, 93, 97, 96] + assert len(points_flip) == 98 +elif cfg.data_name == 'COFW': + points_flip = [2, 1, 4, 3, 7, 8, 5, 6, 10, 9, 12, 11, 15, 16, 13, 14, 18, 17, 20, 19, 21, 22, 24, 23, 25, 26, 27, 28, 29] + points_flip = (np.array(points_flip)-1).tolist() + assert len(points_flip) == 29 +elif cfg.data_name == 'AFLW': + points_flip = [6, 5, 4, 3, 2, 1, 12, 11, 10, 9, 8, 7, 15, 14, 13, 18, 17, 16, 19] + points_flip = (np.array(points_flip)-1).tolist() + assert len(points_flip) == 19 +else: + print('No such data!') + exit(0) + +normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], + std=[0.229, 0.224, 0.225]) + +if cfg.pretrained: + optimizer = optim.Adam(net.parameters(), lr=cfg.init_lr) +else: + optimizer = optim.Adam(net.parameters(), lr=cfg.init_lr, weight_decay=5e-4) +scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=cfg.decay_steps, gamma=0.1) + +labels = get_label(cfg.data_name, 'train.txt') + +if cfg.det_head == 'pip': + train_data = data_utils.ImageFolder_pip(os.path.join('data', cfg.data_name, 'images_train'), + labels, cfg.input_size, cfg.num_lms, + cfg.net_stride, points_flip, meanface_indices, + transforms.Compose([ + transforms.RandomGrayscale(0.2), + transforms.ToTensor(), + normalize])) +else: + print('No such head:', cfg.det_head) + exit(0) + +train_loader = torch.utils.data.DataLoader(train_data, batch_size=cfg.batch_size, shuffle=True, num_workers=8, pin_memory=True, drop_last=True) + +train_model(cfg.det_head, net, train_loader, criterion_cls, criterion_reg, cfg.cls_loss_weight, cfg.reg_loss_weight, cfg.num_nb, optimizer, cfg.num_epochs, scheduler, save_dir, cfg.save_interval, device) + diff --git a/third_party/PIPNet/lib/train_gssl.py b/third_party/PIPNet/lib/train_gssl.py new file mode 100644 index 0000000000000000000000000000000000000000..94ea69ab7be3f1ad04152f98e5fadcb9a7e465f7 --- /dev/null +++ b/third_party/PIPNet/lib/train_gssl.py @@ -0,0 +1,303 @@ +import cv2, os +import sys +sys.path.insert(0, '..') +import numpy as np +from PIL import Image +import logging +import importlib + +import torch +import torch.nn as nn +import torch.optim as optim +import torch.utils.data +import torch.nn.functional as F +import torchvision.transforms as transforms +import torchvision.datasets as datasets +import torchvision.models as models + +from networks_gssl import * +import data_utils_gssl +from functions_gssl import * + +if not len(sys.argv) == 2: + print('Format:') + print('python lib/train_gssl.py config_file') + exit(0) +experiment_name = sys.argv[1].split('/')[-1][:-3] +data_name = sys.argv[1].split('/')[-2] +config_path = '.experiments.{}.{}'.format(data_name, experiment_name) + +my_config = importlib.import_module(config_path, package='PIPNet') +Config = getattr(my_config, 'Config') +cfg = Config() +cfg.experiment_name = experiment_name +cfg.data_name = data_name + +os.environ['CUDA_VISIBLE_DEVICES'] = str(cfg.gpu_id) + +if not os.path.exists(os.path.join('./snapshots', cfg.data_name)): + os.mkdir(os.path.join('./snapshots', cfg.data_name)) +save_dir = os.path.join('./snapshots', cfg.data_name, cfg.experiment_name) +if not os.path.exists(save_dir): + os.mkdir(save_dir) + +if not os.path.exists(os.path.join('./logs', cfg.data_name)): + os.mkdir(os.path.join('./logs', cfg.data_name)) +log_dir = os.path.join('./logs', cfg.data_name, cfg.experiment_name) +if not os.path.exists(log_dir): + os.mkdir(log_dir) + +logging.basicConfig(filename=os.path.join(log_dir, 'train.log'), level=logging.INFO) + +print('###########################################') +print('experiment_name:', cfg.experiment_name) +print('data_name:', cfg.data_name) +print('det_head:', cfg.det_head) +print('net_stride:', cfg.net_stride) +print('batch_size:', cfg.batch_size) +print('init_lr:', cfg.init_lr) +print('num_epochs:', cfg.num_epochs) +print('decay_steps:', cfg.decay_steps) +print('input_size:', cfg.input_size) +print('backbone:', cfg.backbone) +print('pretrained:', cfg.pretrained) +print('criterion_cls:', cfg.criterion_cls) +print('criterion_reg:', cfg.criterion_reg) +print('cls_loss_weight:', cfg.cls_loss_weight) +print('reg_loss_weight:', cfg.reg_loss_weight) +print('num_lms:', cfg.num_lms) +print('save_interval:', cfg.save_interval) +print('num_nb:', cfg.num_nb) +print('use_gpu:', cfg.use_gpu) +print('gpu_id:', cfg.gpu_id) +print('curriculum:', cfg.curriculum) +print('###########################################') +logging.info('###########################################') +logging.info('experiment_name: {}'.format(cfg.experiment_name)) +logging.info('data_name: {}'.format(cfg.data_name)) +logging.info('det_head: {}'.format(cfg.det_head)) +logging.info('net_stride: {}'.format(cfg.net_stride)) +logging.info('batch_size: {}'.format(cfg.batch_size)) +logging.info('init_lr: {}'.format(cfg.init_lr)) +logging.info('num_epochs: {}'.format(cfg.num_epochs)) +logging.info('decay_steps: {}'.format(cfg.decay_steps)) +logging.info('input_size: {}'.format(cfg.input_size)) +logging.info('backbone: {}'.format(cfg.backbone)) +logging.info('pretrained: {}'.format(cfg.pretrained)) +logging.info('criterion_cls: {}'.format(cfg.criterion_cls)) +logging.info('criterion_reg: {}'.format(cfg.criterion_reg)) +logging.info('cls_loss_weight: {}'.format(cfg.cls_loss_weight)) +logging.info('reg_loss_weight: {}'.format(cfg.reg_loss_weight)) +logging.info('num_lms: {}'.format(cfg.num_lms)) +logging.info('save_interval: {}'.format(cfg.save_interval)) +logging.info('num_nb: {}'.format(cfg.num_nb)) +logging.info('use_gpu: {}'.format(cfg.use_gpu)) +logging.info('gpu_id: {}'.format(cfg.gpu_id)) +logging.info('###########################################') + +if cfg.curriculum: + # self-training with curriculum + task_type_list = ['cls3', 'cls2', 'std', 'std', 'std'] +else: + # standard self-training + task_type_list = ['std']*3 + +meanface_indices, reverse_index1, reverse_index2, max_len = get_meanface(os.path.join('data', cfg.data_name, 'meanface.txt'), cfg.num_nb) + +if cfg.det_head == 'pip': + if cfg.backbone == 'resnet18': + resnet18 = models.resnet18(pretrained=cfg.pretrained) + net = Pip_resnet18(resnet18, cfg.num_nb, num_lms=cfg.num_lms, input_size=cfg.input_size, net_stride=cfg.net_stride) + else: + print('No such backbone!') + exit(0) +else: + print('No such head:', cfg.det_head) + exit(0) + +if cfg.use_gpu: + device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") +else: + device = torch.device("cpu") +net = net.to(device) + +criterion_cls = None +if cfg.criterion_cls == 'l2': + criterion_cls = nn.MSELoss(reduction='sum') +elif cfg.criterion_cls == 'l1': + criterion_cls = nn.L1Loss() +else: + print('No such cls criterion:', cfg.criterion_cls) + +criterion_reg = None +if cfg.criterion_reg == 'l1': + criterion_reg = nn.L1Loss(reduction='sum') +elif cfg.criterion_reg == 'l2': + criterion_reg = nn.MSELoss() +else: + print('No such reg criterion:', cfg.criterion_reg) + +points_flip = [17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 28, 29, 30, 31, 36, 35, 34, 33, 32, 46, 45, 44, 43, 48, 47, 40, 39, 38, 37, 42, 41, 55, 54, 53, 52, 51, 50, 49, 60, 59, 58, 57, 56, 65, 64, 63, 62, 61, 68, 67, 66] +points_flip = (np.array(points_flip)-1).tolist() +assert len(points_flip) == 68 + +normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], + std=[0.229, 0.224, 0.225]) + +optimizer = optim.Adam(net.parameters(), lr=cfg.init_lr) +scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=cfg.decay_steps, gamma=0.1) + +labels = get_label(cfg.data_name, 'train_300W.txt', 'std') + +train_data = data_utils_gssl.ImageFolder_pip(os.path.join('data', cfg.data_name, 'images_train'), + labels, cfg.input_size, cfg.num_lms, + cfg.net_stride, points_flip, meanface_indices, + transforms.Compose([ + transforms.RandomGrayscale(0.2), + transforms.ToTensor(), + normalize])) + +train_loader = torch.utils.data.DataLoader(train_data, batch_size=cfg.batch_size, shuffle=True, num_workers=8, pin_memory=True, drop_last=True) + +train_model(cfg.det_head, net, train_loader, criterion_cls, criterion_reg, cfg.cls_loss_weight, cfg.reg_loss_weight, cfg.num_nb, optimizer, cfg.num_epochs, scheduler, save_dir, cfg.save_interval, device) + +############### +# test +norm_indices = [36, 45] + +preprocess = transforms.Compose([transforms.Resize((cfg.input_size, cfg.input_size)), transforms.ToTensor(), normalize]) +test_data_list = ['300W', 'COFW', 'WFLW'] +for test_data in test_data_list: + labels = get_label(cfg.data_name, 'test_'+test_data+'.txt') + nmes = [] + norm = None + for label in labels: + image_name = label[0] + lms_gt = label[1] + image_path = os.path.join('data', cfg.data_name, 'images_test_'+test_data, image_name) + image = cv2.imread(image_path) + image = cv2.resize(image, (cfg.input_size, cfg.input_size)) + inputs = Image.fromarray(image[:,:,::-1].astype('uint8'), 'RGB') + inputs = preprocess(inputs).unsqueeze(0) + inputs = inputs.to(device) + lms_pred_x, lms_pred_y, lms_pred_nb_x, lms_pred_nb_y, outputs_cls, max_cls = forward_pip(net, inputs, preprocess, cfg.input_size, cfg.net_stride, cfg.num_nb) + # inter-ocular + norm = np.linalg.norm(lms_gt.reshape(-1, 2)[norm_indices[0]] - lms_gt.reshape(-1, 2)[norm_indices[1]]) + ############################# + # merge neighbor predictions + lms_pred = torch.cat((lms_pred_x, lms_pred_y), dim=1).flatten().cpu().numpy() + tmp_nb_x = lms_pred_nb_x[reverse_index1, reverse_index2].view(cfg.num_lms, max_len) + tmp_nb_y = lms_pred_nb_y[reverse_index1, reverse_index2].view(cfg.num_lms, max_len) + tmp_x = torch.mean(torch.cat((lms_pred_x, tmp_nb_x), dim=1), dim=1).view(-1,1) + tmp_y = torch.mean(torch.cat((lms_pred_y, tmp_nb_y), dim=1), dim=1).view(-1,1) + lms_pred_merge = torch.cat((tmp_x, tmp_y), dim=1).flatten().cpu().numpy() + ############################# + nme = compute_nme(lms_pred_merge, lms_gt, norm) + nmes.append(nme) + + print('{} nme: {}'.format(test_data, np.mean(nmes))) + logging.info('{} nme: {}'.format(test_data, np.mean(nmes))) + +for ti, task_type in enumerate(task_type_list): + print('###################################################') + print('Iter:', ti, 'task_type:', task_type) + ############### + # estimate + if cfg.data_name == 'data_300W_COFW_WFLW': + est_data_list = ['COFW', 'WFLW'] + elif cfg.data_name == 'data_300W_CELEBA': + est_data_list = ['CELEBA'] + else: + print('No such data!') + exit(0) + est_preds = [] + for est_data in est_data_list: + labels = get_label(cfg.data_name, 'train_'+est_data+'.txt') + for label in labels: + image_name = label[0] + #print(image_name) + image_path = os.path.join('data', cfg.data_name, 'images_train', image_name) + image = cv2.imread(image_path) + image = cv2.resize(image, (cfg.input_size, cfg.input_size)) + inputs = Image.fromarray(image[:,:,::-1].astype('uint8'), 'RGB') + inputs = preprocess(inputs).unsqueeze(0) + inputs = inputs.to(device) + lms_pred_x, lms_pred_y, lms_pred_nb_x, lms_pred_nb_y, outputs_cls, max_cls = forward_pip(net, inputs, preprocess, cfg.input_size, cfg.net_stride, cfg.num_nb) + ############################# + # merge neighbor predictions + lms_pred = torch.cat((lms_pred_x, lms_pred_y), dim=1).flatten().cpu().numpy() + tmp_nb_x = lms_pred_nb_x[reverse_index1, reverse_index2].view(cfg.num_lms, max_len) + tmp_nb_y = lms_pred_nb_y[reverse_index1, reverse_index2].view(cfg.num_lms, max_len) + tmp_x = torch.mean(torch.cat((lms_pred_x, tmp_nb_x), dim=1), dim=1).view(-1,1) + tmp_y = torch.mean(torch.cat((lms_pred_y, tmp_nb_y), dim=1), dim=1).view(-1,1) + lms_pred_merge = torch.cat((tmp_x, tmp_y), dim=1).flatten().cpu().numpy() + ############################# + est_preds.append([image_name, task_type, lms_pred_merge]) + + ################ + # GSSL + if cfg.det_head == 'pip': + if cfg.backbone == 'resnet18': + resnet18 = models.resnet18(pretrained=cfg.pretrained) + net = Pip_resnet18(resnet18, cfg.num_nb, num_lms=cfg.num_lms, input_size=cfg.input_size, net_stride=cfg.net_stride) + else: + print('No such backbone!') + exit(0) + else: + print('No such head:', cfg.det_head) + exit(0) + + net = net.to(device) + optimizer = optim.Adam(net.parameters(), lr=cfg.init_lr) + scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=cfg.decay_steps, gamma=0.1) + labels = get_label(cfg.data_name, 'train_300W.txt', 'std') + labels += est_preds + + train_data = data_utils_gssl.ImageFolder_pip(os.path.join('data', cfg.data_name, 'images_train'), + labels, cfg.input_size, cfg.num_lms, + cfg.net_stride, points_flip, meanface_indices, + transforms.Compose([ + transforms.RandomGrayscale(0.2), + transforms.ToTensor(), + normalize])) + + train_loader = torch.utils.data.DataLoader(train_data, batch_size=cfg.batch_size, shuffle=True, num_workers=8, pin_memory=True, drop_last=True) + + train_model(cfg.det_head, net, train_loader, criterion_cls, criterion_reg, cfg.cls_loss_weight, cfg.reg_loss_weight, cfg.num_nb, optimizer, cfg.num_epochs, scheduler, save_dir, cfg.save_interval, device) + + ############### + # test + preprocess = transforms.Compose([transforms.Resize((cfg.input_size, cfg.input_size)), transforms.ToTensor(), normalize]) + test_data_list = ['300W', 'COFW', 'WFLW'] + for test_data in test_data_list: + labels = get_label(cfg.data_name, 'test_'+test_data+'.txt') + nmes = [] + norm = None + for label in labels: + image_name = label[0] + lms_gt = label[1] + image_path = os.path.join('data', cfg.data_name, 'images_test_'+test_data, image_name) + image = cv2.imread(image_path) + image = cv2.resize(image, (cfg.input_size, cfg.input_size)) + inputs = Image.fromarray(image[:,:,::-1].astype('uint8'), 'RGB') + inputs = preprocess(inputs).unsqueeze(0) + inputs = inputs.to(device) + lms_pred_x, lms_pred_y, lms_pred_nb_x, lms_pred_nb_y, outputs_cls, max_cls = forward_pip(net, inputs, preprocess, cfg.input_size, cfg.net_stride, cfg.num_nb) + # inter-ocular + norm = np.linalg.norm(lms_gt.reshape(-1, 2)[norm_indices[0]] - lms_gt.reshape(-1, 2)[norm_indices[1]]) + ############################# + # merge neighbor predictions + lms_pred = torch.cat((lms_pred_x, lms_pred_y), dim=1).flatten().cpu().numpy() + tmp_nb_x = lms_pred_nb_x[reverse_index1, reverse_index2].view(cfg.num_lms, max_len) + tmp_nb_y = lms_pred_nb_y[reverse_index1, reverse_index2].view(cfg.num_lms, max_len) + tmp_x = torch.mean(torch.cat((lms_pred_x, tmp_nb_x), dim=1), dim=1).view(-1,1) + tmp_y = torch.mean(torch.cat((lms_pred_y, tmp_nb_y), dim=1), dim=1).view(-1,1) + lms_pred_merge = torch.cat((tmp_x, tmp_y), dim=1).flatten().cpu().numpy() + ############################# + nme = compute_nme(lms_pred_merge, lms_gt, norm) + nmes.append(nme) + + print('{} nme: {}'.format(test_data, np.mean(nmes))) + logging.info('{} nme: {}'.format(test_data, np.mean(nmes))) + + diff --git a/third_party/PIPNet/requirements.txt b/third_party/PIPNet/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..22ddb050a36ae15e848c827d097fa2549c57f460 --- /dev/null +++ b/third_party/PIPNet/requirements.txt @@ -0,0 +1,3 @@ +opencv-python +scipy +Cython diff --git a/third_party/PIPNet/reverse_index.py b/third_party/PIPNet/reverse_index.py new file mode 100644 index 0000000000000000000000000000000000000000..fca564be567690b2f2085d96e9a24e8d76d31012 --- /dev/null +++ b/third_party/PIPNet/reverse_index.py @@ -0,0 +1,3338 @@ +ri1 = [ + 1, + 2, + 3, + 4, + 5, + 33, + 1, + 2, + 3, + 4, + 5, + 33, + 1, + 2, + 3, + 4, + 5, + 0, + 2, + 3, + 4, + 5, + 6, + 33, + 0, + 2, + 3, + 4, + 5, + 6, + 33, + 0, + 2, + 3, + 0, + 1, + 3, + 4, + 5, + 6, + 0, + 1, + 3, + 4, + 5, + 6, + 0, + 1, + 3, + 4, + 5, + 0, + 1, + 2, + 4, + 5, + 6, + 7, + 0, + 1, + 2, + 4, + 5, + 6, + 7, + 0, + 1, + 2, + 0, + 1, + 2, + 3, + 5, + 6, + 7, + 8, + 0, + 1, + 2, + 3, + 5, + 6, + 7, + 8, + 0, + 1, + 2, + 3, + 4, + 6, + 7, + 8, + 9, + 1, + 2, + 3, + 4, + 6, + 7, + 8, + 9, + 1, + 2, + 3, + 4, + 5, + 7, + 8, + 9, + 10, + 2, + 3, + 4, + 5, + 7, + 8, + 9, + 10, + 2, + 3, + 4, + 5, + 6, + 8, + 9, + 10, + 3, + 4, + 5, + 6, + 8, + 9, + 10, + 3, + 4, + 5, + 4, + 5, + 6, + 7, + 9, + 10, + 11, + 4, + 5, + 6, + 7, + 9, + 10, + 11, + 4, + 5, + 6, + 4, + 5, + 6, + 7, + 8, + 10, + 11, + 12, + 4, + 5, + 6, + 7, + 8, + 10, + 11, + 12, + 4, + 5, + 6, + 7, + 8, + 9, + 11, + 12, + 13, + 76, + 5, + 6, + 7, + 8, + 9, + 11, + 12, + 13, + 7, + 8, + 9, + 10, + 12, + 13, + 14, + 76, + 88, + 7, + 8, + 9, + 10, + 12, + 13, + 14, + 76, + 8, + 9, + 10, + 11, + 13, + 14, + 15, + 8, + 9, + 10, + 11, + 13, + 14, + 15, + 8, + 9, + 10, + 10, + 11, + 12, + 14, + 15, + 16, + 10, + 11, + 12, + 14, + 15, + 16, + 10, + 11, + 12, + 14, + 15, + 11, + 12, + 13, + 15, + 16, + 17, + 11, + 12, + 13, + 15, + 16, + 17, + 11, + 12, + 13, + 15, + 16, + 12, + 13, + 14, + 16, + 17, + 18, + 12, + 13, + 14, + 16, + 17, + 18, + 12, + 13, + 14, + 16, + 17, + 13, + 14, + 15, + 17, + 18, + 19, + 13, + 14, + 15, + 17, + 18, + 19, + 13, + 14, + 15, + 17, + 18, + 14, + 15, + 16, + 18, + 19, + 20, + 14, + 15, + 16, + 18, + 19, + 20, + 14, + 15, + 16, + 18, + 19, + 15, + 16, + 17, + 19, + 20, + 21, + 15, + 16, + 17, + 19, + 20, + 21, + 15, + 16, + 17, + 19, + 20, + 16, + 17, + 18, + 20, + 21, + 22, + 16, + 17, + 18, + 20, + 21, + 22, + 16, + 17, + 18, + 20, + 21, + 17, + 18, + 19, + 21, + 22, + 23, + 24, + 17, + 18, + 19, + 21, + 22, + 23, + 24, + 17, + 18, + 19, + 18, + 19, + 20, + 22, + 23, + 24, + 25, + 82, + 18, + 19, + 20, + 22, + 23, + 24, + 25, + 82, + 18, + 19, + 20, + 21, + 23, + 24, + 25, + 26, + 27, + 19, + 20, + 21, + 23, + 24, + 25, + 26, + 27, + 19, + 20, + 21, + 22, + 24, + 25, + 26, + 27, + 28, + 20, + 21, + 22, + 24, + 25, + 26, + 27, + 28, + 20, + 21, + 22, + 23, + 25, + 26, + 27, + 28, + 21, + 22, + 23, + 25, + 26, + 27, + 28, + 21, + 22, + 23, + 21, + 22, + 23, + 24, + 26, + 27, + 28, + 29, + 21, + 22, + 23, + 24, + 26, + 27, + 28, + 29, + 21, + 22, + 23, + 24, + 25, + 27, + 28, + 29, + 30, + 22, + 23, + 24, + 25, + 27, + 28, + 29, + 30, + 22, + 23, + 24, + 25, + 26, + 28, + 29, + 30, + 31, + 23, + 24, + 25, + 26, + 28, + 29, + 30, + 31, + 23, + 24, + 25, + 26, + 27, + 29, + 30, + 31, + 32, + 24, + 25, + 26, + 27, + 29, + 30, + 31, + 32, + 24, + 25, + 26, + 27, + 28, + 30, + 31, + 32, + 25, + 26, + 27, + 28, + 30, + 31, + 32, + 25, + 26, + 27, + 26, + 27, + 28, + 29, + 31, + 32, + 26, + 27, + 28, + 29, + 31, + 32, + 26, + 27, + 28, + 29, + 31, + 26, + 27, + 28, + 29, + 30, + 32, + 46, + 26, + 27, + 28, + 29, + 30, + 32, + 46, + 26, + 27, + 28, + 27, + 28, + 29, + 30, + 31, + 46, + 27, + 28, + 29, + 30, + 31, + 46, + 27, + 28, + 29, + 30, + 31, + 0, + 1, + 2, + 3, + 34, + 41, + 60, + 0, + 1, + 2, + 3, + 34, + 41, + 60, + 0, + 1, + 2, + 0, + 33, + 35, + 40, + 41, + 60, + 0, + 33, + 35, + 40, + 41, + 60, + 0, + 33, + 35, + 40, + 41, + 33, + 34, + 36, + 37, + 39, + 40, + 41, + 60, + 61, + 62, + 33, + 34, + 36, + 37, + 39, + 40, + 41, + 34, + 35, + 37, + 38, + 39, + 40, + 63, + 64, + 34, + 35, + 37, + 38, + 39, + 40, + 63, + 64, + 34, + 36, + 38, + 39, + 51, + 64, + 36, + 38, + 39, + 51, + 64, + 36, + 38, + 39, + 51, + 64, + 36, + 38, + 36, + 37, + 39, + 51, + 52, + 63, + 64, + 65, + 36, + 37, + 39, + 51, + 52, + 63, + 64, + 65, + 36, + 35, + 36, + 37, + 38, + 40, + 62, + 63, + 64, + 65, + 66, + 67, + 96, + 35, + 36, + 37, + 38, + 40, + 33, + 34, + 35, + 36, + 37, + 38, + 39, + 41, + 60, + 61, + 62, + 63, + 65, + 66, + 67, + 96, + 33, + 0, + 1, + 2, + 33, + 34, + 35, + 40, + 60, + 61, + 67, + 0, + 1, + 2, + 33, + 34, + 35, + 40, + 43, + 49, + 50, + 51, + 68, + 43, + 49, + 50, + 51, + 68, + 43, + 49, + 50, + 51, + 68, + 43, + 49, + 42, + 44, + 45, + 48, + 49, + 50, + 68, + 69, + 42, + 44, + 45, + 48, + 49, + 50, + 68, + 69, + 42, + 42, + 43, + 45, + 46, + 47, + 48, + 49, + 70, + 42, + 43, + 45, + 46, + 47, + 48, + 49, + 70, + 42, + 32, + 44, + 46, + 47, + 48, + 71, + 72, + 73, + 32, + 44, + 46, + 47, + 48, + 71, + 72, + 73, + 32, + 29, + 30, + 31, + 32, + 45, + 47, + 72, + 29, + 30, + 31, + 32, + 45, + 47, + 72, + 29, + 30, + 31, + 30, + 31, + 32, + 44, + 45, + 46, + 48, + 71, + 72, + 73, + 30, + 31, + 32, + 44, + 45, + 46, + 48, + 42, + 43, + 44, + 45, + 46, + 47, + 49, + 50, + 69, + 70, + 71, + 72, + 73, + 74, + 75, + 97, + 42, + 42, + 43, + 44, + 48, + 50, + 68, + 69, + 70, + 74, + 75, + 97, + 42, + 43, + 44, + 48, + 50, + 68, + 42, + 43, + 49, + 51, + 52, + 68, + 69, + 75, + 42, + 43, + 49, + 51, + 52, + 68, + 69, + 75, + 42, + 37, + 38, + 42, + 50, + 52, + 53, + 64, + 68, + 37, + 38, + 42, + 50, + 52, + 53, + 64, + 68, + 37, + 51, + 53, + 54, + 51, + 53, + 54, + 51, + 53, + 54, + 51, + 53, + 54, + 51, + 53, + 54, + 51, + 53, + 51, + 52, + 54, + 55, + 56, + 57, + 59, + 51, + 52, + 54, + 55, + 56, + 57, + 59, + 51, + 52, + 54, + 52, + 53, + 55, + 56, + 57, + 58, + 59, + 52, + 53, + 55, + 56, + 57, + 58, + 59, + 52, + 53, + 55, + 53, + 54, + 56, + 57, + 76, + 77, + 78, + 88, + 53, + 54, + 56, + 57, + 76, + 77, + 78, + 88, + 53, + 53, + 54, + 55, + 57, + 58, + 77, + 78, + 79, + 88, + 53, + 54, + 55, + 57, + 58, + 77, + 78, + 79, + 53, + 54, + 55, + 56, + 58, + 59, + 78, + 79, + 80, + 90, + 53, + 54, + 55, + 56, + 58, + 59, + 78, + 53, + 54, + 56, + 57, + 59, + 79, + 80, + 81, + 82, + 92, + 53, + 54, + 56, + 57, + 59, + 79, + 80, + 53, + 54, + 57, + 58, + 80, + 81, + 82, + 92, + 53, + 54, + 57, + 58, + 80, + 81, + 82, + 92, + 53, + 0, + 1, + 2, + 3, + 4, + 33, + 34, + 41, + 61, + 62, + 66, + 67, + 96, + 0, + 1, + 2, + 3, + 0, + 1, + 33, + 34, + 35, + 40, + 41, + 60, + 62, + 63, + 65, + 66, + 67, + 96, + 0, + 1, + 33, + 33, + 34, + 35, + 36, + 37, + 38, + 39, + 40, + 41, + 60, + 61, + 63, + 64, + 65, + 66, + 67, + 96, + 35, + 36, + 37, + 38, + 39, + 40, + 51, + 52, + 61, + 62, + 64, + 65, + 66, + 67, + 96, + 35, + 36, + 36, + 37, + 38, + 39, + 51, + 52, + 53, + 63, + 65, + 66, + 96, + 36, + 37, + 38, + 39, + 51, + 52, + 36, + 37, + 38, + 39, + 52, + 61, + 62, + 63, + 64, + 66, + 67, + 96, + 36, + 37, + 38, + 39, + 52, + 41, + 60, + 61, + 62, + 63, + 64, + 65, + 67, + 96, + 41, + 60, + 61, + 62, + 63, + 64, + 65, + 67, + 0, + 1, + 2, + 3, + 33, + 34, + 35, + 40, + 41, + 60, + 61, + 62, + 65, + 66, + 96, + 0, + 1, + 42, + 43, + 49, + 50, + 51, + 52, + 53, + 69, + 74, + 75, + 97, + 42, + 43, + 49, + 50, + 51, + 52, + 42, + 43, + 44, + 48, + 49, + 50, + 51, + 68, + 70, + 71, + 73, + 74, + 75, + 97, + 42, + 43, + 44, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 68, + 69, + 71, + 72, + 73, + 74, + 75, + 97, + 31, + 32, + 44, + 45, + 46, + 47, + 48, + 69, + 70, + 72, + 73, + 74, + 75, + 97, + 31, + 32, + 44, + 28, + 29, + 30, + 31, + 32, + 45, + 46, + 47, + 70, + 71, + 73, + 74, + 97, + 28, + 29, + 30, + 31, + 29, + 30, + 31, + 32, + 44, + 45, + 46, + 47, + 48, + 70, + 71, + 72, + 74, + 75, + 97, + 29, + 30, + 47, + 68, + 69, + 70, + 71, + 72, + 73, + 75, + 97, + 47, + 68, + 69, + 70, + 71, + 72, + 73, + 75, + 42, + 43, + 49, + 50, + 52, + 68, + 69, + 70, + 71, + 72, + 73, + 74, + 97, + 42, + 43, + 49, + 50, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 55, + 77, + 87, + 88, + 89, + 95, + 6, + 7, + 8, + 9, + 55, + 56, + 76, + 78, + 86, + 87, + 88, + 89, + 95, + 55, + 56, + 76, + 78, + 86, + 87, + 88, + 89, + 54, + 55, + 56, + 57, + 58, + 76, + 77, + 79, + 80, + 85, + 86, + 87, + 88, + 89, + 90, + 94, + 95, + 54, + 55, + 56, + 57, + 58, + 59, + 77, + 78, + 80, + 81, + 84, + 85, + 86, + 89, + 90, + 91, + 94, + 54, + 57, + 58, + 59, + 78, + 79, + 81, + 82, + 83, + 84, + 85, + 90, + 91, + 92, + 93, + 94, + 54, + 58, + 59, + 80, + 82, + 83, + 84, + 91, + 92, + 93, + 58, + 59, + 80, + 82, + 83, + 84, + 91, + 92, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 59, + 81, + 83, + 91, + 92, + 93, + 20, + 21, + 22, + 23, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 81, + 82, + 84, + 91, + 92, + 93, + 17, + 18, + 19, + 20, + 16, + 17, + 18, + 19, + 20, + 81, + 82, + 83, + 85, + 91, + 92, + 93, + 94, + 16, + 17, + 18, + 19, + 14, + 15, + 16, + 17, + 18, + 83, + 84, + 86, + 87, + 90, + 93, + 94, + 95, + 14, + 15, + 16, + 17, + 11, + 12, + 13, + 14, + 15, + 16, + 76, + 77, + 85, + 87, + 88, + 89, + 94, + 95, + 11, + 12, + 13, + 9, + 10, + 11, + 12, + 13, + 14, + 76, + 77, + 86, + 88, + 89, + 95, + 9, + 10, + 11, + 12, + 13, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 55, + 76, + 77, + 86, + 87, + 89, + 95, + 7, + 8, + 9, + 55, + 56, + 76, + 77, + 78, + 79, + 86, + 87, + 88, + 90, + 95, + 55, + 56, + 76, + 77, + 78, + 79, + 56, + 57, + 58, + 78, + 79, + 80, + 83, + 84, + 85, + 86, + 87, + 89, + 91, + 92, + 93, + 94, + 95, + 58, + 59, + 79, + 80, + 81, + 82, + 83, + 84, + 85, + 90, + 92, + 93, + 94, + 58, + 59, + 79, + 80, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 59, + 81, + 82, + 83, + 84, + 91, + 93, + 19, + 20, + 21, + 18, + 19, + 79, + 80, + 81, + 82, + 83, + 84, + 85, + 90, + 91, + 92, + 94, + 18, + 19, + 79, + 80, + 15, + 16, + 17, + 78, + 79, + 80, + 83, + 84, + 85, + 86, + 87, + 89, + 90, + 91, + 93, + 95, + 15, + 13, + 14, + 15, + 76, + 77, + 78, + 85, + 86, + 87, + 88, + 89, + 90, + 94, + 13, + 14, + 15, + 76, + 34, + 35, + 36, + 38, + 39, + 40, + 41, + 60, + 61, + 62, + 63, + 64, + 65, + 66, + 67, + 34, + 35, + 43, + 44, + 45, + 47, + 48, + 49, + 50, + 68, + 69, + 70, + 71, + 72, + 73, + 74, + 75, + 43, + 44, +] + + +ri2 = [ + 0, + 2, + 4, + 6, + 8, + 4, + 0, + 2, + 4, + 6, + 8, + 4, + 0, + 2, + 4, + 6, + 8, + 0, + 0, + 2, + 4, + 6, + 8, + 8, + 0, + 0, + 2, + 4, + 6, + 8, + 8, + 0, + 0, + 2, + 1, + 1, + 0, + 2, + 4, + 6, + 1, + 1, + 0, + 2, + 4, + 6, + 1, + 1, + 0, + 2, + 4, + 3, + 2, + 1, + 0, + 2, + 4, + 6, + 3, + 2, + 1, + 0, + 2, + 4, + 6, + 3, + 2, + 1, + 6, + 3, + 3, + 1, + 0, + 2, + 4, + 7, + 6, + 3, + 3, + 1, + 0, + 2, + 4, + 7, + 6, + 6, + 4, + 3, + 1, + 0, + 2, + 4, + 8, + 6, + 4, + 3, + 1, + 0, + 2, + 4, + 8, + 6, + 7, + 5, + 3, + 1, + 0, + 2, + 4, + 9, + 7, + 5, + 3, + 1, + 0, + 2, + 4, + 9, + 7, + 6, + 5, + 3, + 1, + 0, + 2, + 4, + 6, + 5, + 3, + 1, + 0, + 2, + 4, + 6, + 5, + 3, + 7, + 5, + 3, + 1, + 0, + 2, + 4, + 7, + 5, + 3, + 1, + 0, + 2, + 4, + 7, + 5, + 3, + 9, + 7, + 5, + 3, + 1, + 0, + 2, + 5, + 9, + 7, + 5, + 3, + 1, + 0, + 2, + 5, + 9, + 9, + 7, + 5, + 3, + 1, + 0, + 2, + 5, + 8, + 9, + 7, + 5, + 3, + 1, + 0, + 2, + 5, + 7, + 5, + 3, + 1, + 0, + 2, + 5, + 9, + 9, + 7, + 5, + 3, + 1, + 0, + 2, + 5, + 9, + 9, + 5, + 3, + 1, + 0, + 2, + 4, + 9, + 5, + 3, + 1, + 0, + 2, + 4, + 9, + 5, + 3, + 6, + 3, + 1, + 0, + 2, + 6, + 6, + 3, + 1, + 0, + 2, + 6, + 6, + 3, + 1, + 0, + 2, + 7, + 3, + 1, + 0, + 3, + 7, + 7, + 3, + 1, + 0, + 3, + 7, + 7, + 3, + 1, + 0, + 3, + 6, + 3, + 1, + 1, + 3, + 6, + 6, + 3, + 1, + 1, + 3, + 6, + 6, + 3, + 1, + 1, + 3, + 7, + 3, + 1, + 1, + 3, + 7, + 7, + 3, + 1, + 1, + 3, + 7, + 7, + 3, + 1, + 1, + 3, + 6, + 3, + 0, + 1, + 3, + 6, + 6, + 3, + 0, + 1, + 3, + 6, + 6, + 3, + 0, + 1, + 3, + 7, + 2, + 0, + 1, + 3, + 5, + 7, + 2, + 0, + 1, + 3, + 5, + 7, + 2, + 0, + 1, + 3, + 5, + 2, + 0, + 1, + 3, + 5, + 5, + 2, + 0, + 1, + 3, + 5, + 5, + 2, + 0, + 1, + 3, + 4, + 2, + 0, + 1, + 3, + 5, + 8, + 4, + 2, + 0, + 1, + 3, + 5, + 8, + 4, + 2, + 0, + 5, + 2, + 0, + 1, + 3, + 5, + 7, + 9, + 5, + 2, + 0, + 1, + 3, + 5, + 7, + 9, + 5, + 4, + 2, + 0, + 1, + 3, + 5, + 7, + 9, + 4, + 2, + 0, + 1, + 3, + 5, + 7, + 9, + 4, + 4, + 2, + 0, + 1, + 3, + 5, + 7, + 9, + 4, + 2, + 0, + 1, + 3, + 5, + 7, + 9, + 4, + 4, + 2, + 0, + 1, + 3, + 5, + 7, + 4, + 2, + 0, + 1, + 3, + 5, + 7, + 4, + 2, + 0, + 9, + 4, + 2, + 0, + 1, + 3, + 5, + 6, + 9, + 4, + 2, + 0, + 1, + 3, + 5, + 6, + 9, + 9, + 4, + 2, + 0, + 1, + 3, + 5, + 6, + 9, + 4, + 2, + 0, + 1, + 3, + 5, + 6, + 9, + 8, + 4, + 2, + 0, + 1, + 3, + 4, + 6, + 8, + 4, + 2, + 0, + 1, + 3, + 4, + 6, + 8, + 6, + 4, + 2, + 0, + 1, + 3, + 3, + 5, + 6, + 4, + 2, + 0, + 1, + 3, + 3, + 5, + 6, + 6, + 4, + 2, + 0, + 1, + 2, + 3, + 6, + 4, + 2, + 0, + 1, + 2, + 3, + 6, + 4, + 2, + 6, + 4, + 2, + 0, + 1, + 1, + 6, + 4, + 2, + 0, + 1, + 1, + 6, + 4, + 2, + 0, + 1, + 8, + 6, + 4, + 2, + 0, + 0, + 9, + 8, + 6, + 4, + 2, + 0, + 0, + 9, + 8, + 6, + 4, + 8, + 6, + 4, + 2, + 0, + 6, + 8, + 6, + 4, + 2, + 0, + 6, + 8, + 6, + 4, + 2, + 0, + 2, + 4, + 5, + 8, + 3, + 1, + 6, + 2, + 4, + 5, + 8, + 3, + 1, + 6, + 2, + 4, + 5, + 7, + 1, + 1, + 5, + 0, + 8, + 7, + 1, + 1, + 5, + 0, + 8, + 7, + 1, + 1, + 5, + 0, + 7, + 1, + 2, + 8, + 6, + 0, + 5, + 9, + 8, + 8, + 7, + 1, + 2, + 8, + 6, + 0, + 5, + 8, + 2, + 1, + 4, + 0, + 6, + 7, + 9, + 8, + 2, + 1, + 4, + 0, + 6, + 7, + 9, + 8, + 1, + 0, + 5, + 5, + 7, + 1, + 0, + 5, + 5, + 7, + 1, + 0, + 5, + 5, + 7, + 1, + 0, + 4, + 0, + 2, + 2, + 6, + 6, + 2, + 8, + 4, + 0, + 2, + 2, + 6, + 6, + 2, + 8, + 4, + 4, + 0, + 2, + 1, + 4, + 7, + 4, + 4, + 5, + 9, + 9, + 7, + 4, + 0, + 2, + 1, + 4, + 5, + 2, + 0, + 3, + 9, + 9, + 4, + 2, + 7, + 5, + 4, + 8, + 9, + 8, + 6, + 6, + 5, + 5, + 7, + 9, + 0, + 0, + 3, + 3, + 2, + 6, + 7, + 5, + 7, + 9, + 0, + 0, + 3, + 3, + 2, + 5, + 0, + 6, + 7, + 2, + 5, + 0, + 6, + 7, + 2, + 5, + 0, + 6, + 7, + 2, + 5, + 1, + 1, + 8, + 5, + 0, + 4, + 9, + 7, + 1, + 1, + 8, + 5, + 0, + 4, + 9, + 7, + 1, + 8, + 1, + 1, + 7, + 4, + 0, + 6, + 9, + 8, + 1, + 1, + 7, + 4, + 0, + 6, + 9, + 8, + 7, + 2, + 1, + 0, + 6, + 9, + 8, + 9, + 7, + 2, + 1, + 0, + 6, + 9, + 8, + 9, + 7, + 8, + 5, + 4, + 2, + 2, + 1, + 6, + 8, + 5, + 4, + 2, + 2, + 1, + 6, + 8, + 5, + 4, + 9, + 7, + 6, + 3, + 0, + 0, + 3, + 6, + 2, + 7, + 9, + 7, + 6, + 3, + 0, + 0, + 3, + 7, + 3, + 0, + 3, + 5, + 2, + 2, + 9, + 8, + 4, + 5, + 7, + 6, + 7, + 9, + 6, + 7, + 2, + 0, + 4, + 2, + 1, + 3, + 2, + 7, + 9, + 5, + 8, + 2, + 0, + 4, + 2, + 1, + 3, + 0, + 4, + 3, + 1, + 5, + 2, + 6, + 8, + 0, + 4, + 3, + 1, + 5, + 2, + 6, + 8, + 0, + 5, + 6, + 5, + 5, + 1, + 5, + 8, + 8, + 5, + 6, + 5, + 5, + 1, + 5, + 8, + 8, + 5, + 0, + 1, + 9, + 0, + 1, + 9, + 0, + 1, + 9, + 0, + 1, + 9, + 0, + 1, + 9, + 0, + 1, + 7, + 0, + 1, + 9, + 9, + 9, + 9, + 7, + 0, + 1, + 9, + 9, + 9, + 9, + 7, + 0, + 1, + 4, + 0, + 5, + 2, + 0, + 2, + 4, + 4, + 0, + 5, + 2, + 0, + 2, + 4, + 4, + 0, + 5, + 6, + 5, + 0, + 8, + 6, + 6, + 9, + 6, + 6, + 5, + 0, + 8, + 6, + 6, + 9, + 6, + 6, + 3, + 2, + 0, + 2, + 7, + 7, + 5, + 7, + 8, + 3, + 2, + 0, + 2, + 7, + 7, + 5, + 7, + 2, + 0, + 2, + 1, + 1, + 2, + 4, + 3, + 5, + 7, + 2, + 0, + 2, + 1, + 1, + 2, + 4, + 4, + 3, + 7, + 1, + 0, + 5, + 4, + 8, + 8, + 8, + 4, + 3, + 7, + 1, + 0, + 5, + 4, + 7, + 4, + 7, + 0, + 9, + 6, + 6, + 6, + 7, + 4, + 7, + 0, + 9, + 6, + 6, + 6, + 7, + 4, + 5, + 6, + 7, + 8, + 2, + 5, + 4, + 1, + 9, + 6, + 1, + 9, + 4, + 5, + 6, + 7, + 8, + 9, + 3, + 4, + 6, + 2, + 3, + 1, + 2, + 9, + 7, + 4, + 0, + 5, + 8, + 9, + 3, + 9, + 6, + 5, + 6, + 7, + 7, + 3, + 1, + 7, + 4, + 2, + 3, + 6, + 4, + 1, + 4, + 0, + 8, + 5, + 3, + 3, + 1, + 8, + 8, + 9, + 7, + 3, + 1, + 0, + 5, + 8, + 3, + 8, + 5, + 8, + 4, + 2, + 8, + 4, + 3, + 9, + 1, + 1, + 7, + 8, + 8, + 4, + 2, + 8, + 4, + 3, + 9, + 6, + 5, + 9, + 7, + 9, + 6, + 0, + 0, + 3, + 5, + 2, + 9, + 6, + 5, + 9, + 7, + 9, + 3, + 4, + 1, + 5, + 5, + 3, + 2, + 1, + 9, + 3, + 4, + 1, + 5, + 5, + 3, + 2, + 9, + 8, + 8, + 9, + 6, + 7, + 9, + 9, + 6, + 0, + 0, + 5, + 6, + 2, + 4, + 9, + 8, + 4, + 8, + 8, + 2, + 3, + 2, + 8, + 1, + 8, + 1, + 9, + 4, + 8, + 8, + 2, + 3, + 2, + 3, + 5, + 8, + 8, + 1, + 3, + 9, + 0, + 3, + 7, + 8, + 5, + 0, + 5, + 3, + 5, + 8, + 9, + 6, + 5, + 6, + 8, + 6, + 1, + 4, + 7, + 6, + 4, + 2, + 5, + 4, + 2, + 4, + 0, + 9, + 8, + 6, + 4, + 3, + 3, + 4, + 9, + 1, + 1, + 0, + 4, + 7, + 2, + 9, + 8, + 6, + 8, + 7, + 7, + 5, + 4, + 5, + 2, + 5, + 8, + 1, + 1, + 6, + 7, + 8, + 7, + 7, + 5, + 9, + 8, + 8, + 9, + 9, + 7, + 4, + 7, + 9, + 5, + 0, + 0, + 1, + 6, + 3, + 9, + 8, + 9, + 5, + 5, + 2, + 4, + 3, + 2, + 3, + 1, + 9, + 5, + 5, + 2, + 4, + 3, + 2, + 3, + 6, + 9, + 9, + 6, + 8, + 1, + 0, + 6, + 8, + 9, + 5, + 3, + 4, + 6, + 9, + 9, + 6, + 9, + 8, + 6, + 6, + 5, + 6, + 7, + 8, + 4, + 2, + 0, + 8, + 7, + 9, + 8, + 6, + 6, + 1, + 5, + 2, + 7, + 5, + 3, + 2, + 0, + 3, + 1, + 5, + 2, + 7, + 5, + 3, + 2, + 0, + 7, + 4, + 3, + 4, + 9, + 7, + 5, + 1, + 3, + 7, + 7, + 6, + 7, + 2, + 2, + 3, + 4, + 6, + 7, + 4, + 3, + 4, + 6, + 9, + 0, + 0, + 9, + 9, + 6, + 9, + 7, + 0, + 7, + 2, + 8, + 5, + 3, + 3, + 3, + 2, + 5, + 7, + 6, + 7, + 8, + 3, + 2, + 7, + 4, + 4, + 8, + 5, + 1, + 6, + 2, + 3, + 5, + 0, + 2, + 3, + 5, + 1, + 6, + 2, + 3, + 5, + 0, + 2, + 7, + 6, + 6, + 6, + 7, + 8, + 9, + 8, + 4, + 2, + 8, + 0, + 8, + 7, + 6, + 6, + 6, + 8, + 7, + 6, + 5, + 7, + 8, + 9, + 3, + 1, + 1, + 3, + 1, + 2, + 8, + 7, + 6, + 5, + 7, + 5, + 4, + 5, + 9, + 7, + 5, + 5, + 1, + 4, + 5, + 1, + 5, + 7, + 5, + 4, + 5, + 8, + 5, + 4, + 6, + 8, + 8, + 2, + 2, + 8, + 4, + 9, + 0, + 9, + 8, + 5, + 4, + 6, + 9, + 8, + 4, + 4, + 6, + 8, + 5, + 8, + 2, + 5, + 5, + 4, + 6, + 1, + 9, + 8, + 4, + 9, + 8, + 5, + 4, + 6, + 7, + 1, + 3, + 1, + 1, + 3, + 2, + 9, + 8, + 5, + 4, + 6, + 9, + 8, + 7, + 7, + 8, + 9, + 9, + 6, + 0, + 2, + 8, + 1, + 5, + 5, + 9, + 8, + 7, + 3, + 6, + 3, + 0, + 2, + 8, + 3, + 4, + 3, + 6, + 0, + 3, + 6, + 3, + 0, + 2, + 8, + 8, + 6, + 8, + 1, + 0, + 1, + 9, + 6, + 3, + 6, + 9, + 6, + 6, + 9, + 7, + 1, + 8, + 6, + 5, + 6, + 2, + 0, + 3, + 4, + 3, + 9, + 5, + 3, + 0, + 9, + 6, + 5, + 6, + 2, + 9, + 8, + 8, + 7, + 7, + 9, + 9, + 7, + 2, + 0, + 1, + 8, + 5, + 5, + 9, + 8, + 8, + 9, + 8, + 9, + 8, + 1, + 4, + 0, + 0, + 4, + 8, + 1, + 4, + 7, + 9, + 8, + 9, + 8, + 8, + 9, + 9, + 6, + 4, + 7, + 7, + 4, + 0, + 4, + 7, + 9, + 1, + 9, + 6, + 6, + 8, + 8, + 9, + 9, + 4, + 1, + 8, + 5, + 0, + 0, + 4, + 1, + 9, + 8, + 8, + 9, + 9, + 4, + 9, + 7, + 7, + 8, + 7, + 7, + 8, + 5, + 3, + 0, + 2, + 3, + 2, + 0, + 3, + 9, + 7, + 7, + 7, + 9, + 8, + 7, + 7, + 8, + 4, + 3, + 0, + 3, + 4, + 3, + 0, + 2, + 7, + 7, +] diff --git a/third_party/PIPNet/run_demo.sh b/third_party/PIPNet/run_demo.sh new file mode 100644 index 0000000000000000000000000000000000000000..c45d630a56d3c117227da5a7c41b51a90d3f9144 --- /dev/null +++ b/third_party/PIPNet/run_demo.sh @@ -0,0 +1,11 @@ +# image +python lib/demo.py experiments/WFLW/pip_32_16_60_r18_l2_l1_10_1_nb10.py images/1.jpg +#python lib/demo.py experiments/data_300W_CELEBA/pip_32_16_60_r18_l2_l1_10_1_nb10_wcc.py images/2.jpg + +# video +#python lib/demo_video.py experiments/WFLW/pip_32_16_60_r18_l2_l1_10_1_nb10.py videos/002.avi +#python lib/demo_video.py experiments/data_300W_CELEBA/pip_32_16_60_r18_l2_l1_10_1_nb10_wcc.py videos/007.avi + +# camera +#python lib/demo_video.py experiments/WFLW/pip_32_16_60_r18_l2_l1_10_1_nb10.py camera + diff --git a/third_party/PIPNet/run_test.sh b/third_party/PIPNet/run_test.sh new file mode 100644 index 0000000000000000000000000000000000000000..c81b0a03155df79d38b149a86956ca82a3f339d4 --- /dev/null +++ b/third_party/PIPNet/run_test.sh @@ -0,0 +1,34 @@ +# supervised learning + +# 300W, resnet18 +#python lib/test.py experiments/data_300W/pip_32_16_60_r18_l2_l1_10_1_nb10.py test.txt images_test +# 300W, resnet101 +#python lib/test.py experiments/data_300W/pip_32_16_60_r101_l2_l1_10_1_nb10.py test.txt images_test + +# COFW, resnet18 +#python lib/test.py experiments/COFW/pip_32_16_60_r18_l2_l1_10_1_nb10.py test.txt images_test +# COFW, resnet101 +#python lib/test.py experiments/COFW/pip_32_16_60_r101_l2_l1_10_1_nb10.py test.txt images_test + +# WFLW, resnet18 +#python lib/test.py experiments/WFLW/pip_32_16_60_r18_l2_l1_10_1_nb10.py test.txt images_test +# WFLW, resnet101 +#python lib/test.py experiments/WFLW/pip_32_16_60_r101_l2_l1_10_1_nb10.py test.txt images_test + +# AFLW, resnet18 +#python lib/test.py experiments/AFLW/pip_32_16_60_r18_l2_l1_10_1_nb10.py test.txt images_test +# AFLW, resnet101 +#python lib/test.py experiments/AFLW/pip_32_16_60_r101_l2_l1_10_1_nb10.py test.txt images_test + +###################################################################################### +# GSSL + +# 300W + COFW_68 (unlabeled) + WFLW_68 (unlabeled), resnet18, with curriculum +#python lib/test.py experiments/data_300W_COFW_WFLW/pip_32_16_60_r18_l2_l1_10_1_nb10_wcc.py test_300W.txt images_test_300W +#python lib/test.py experiments/data_300W_COFW_WFLW/pip_32_16_60_r18_l2_l1_10_1_nb10_wcc.py test_COFW.txt images_test_COFW +#python lib/test.py experiments/data_300W_COFW_WFLW/pip_32_16_60_r18_l2_l1_10_1_nb10_wcc.py test_WFLW.txt images_test_WFLW + +# 300W + CelebA (unlabeled), resnet18, with curriculum +#python lib/test.py experiments/data_300W_CELEBA/pip_32_16_60_r18_l2_l1_10_1_nb10_wcc.py test_300W.txt images_test_300W +#python lib/test.py experiments/data_300W_CELEBA/pip_32_16_60_r18_l2_l1_10_1_nb10_wcc.py test_COFW.txt images_test_COFW +#python lib/test.py experiments/data_300W_CELEBA/pip_32_16_60_r18_l2_l1_10_1_nb10_wcc.py test_WFLW.txt images_test_WFLW diff --git a/third_party/PIPNet/run_train.sh b/third_party/PIPNet/run_train.sh new file mode 100644 index 0000000000000000000000000000000000000000..7fce31dad1055e1a05503ebcf3b8538493ff0899 --- /dev/null +++ b/third_party/PIPNet/run_train.sh @@ -0,0 +1,33 @@ +###################################################################################### +# supervised learning + +# 300W, resnet18 +#python lib/train.py experiments/data_300W/pip_32_16_60_r18_l2_l1_10_1_nb10.py +# 300W, resnet101 +#python lib/train.py experiments/data_300W/pip_32_16_60_r101_l2_l1_10_1_nb10.py + +# COFW, resnet18 +#python lib/train.py experiments/COFW/pip_32_16_60_r18_l2_l1_10_1_nb10.py +# COFW, resnet101 +#python lib/train.py experiments/COFW/pip_32_16_60_r101_l2_l1_10_1_nb10.py + +# WFLW, resnet18 +#python lib/train.py experiments/WFLW/pip_32_16_60_r18_l2_l1_10_1_nb10.py +# WFLW, resnet101 +#python lib/train.py experiments/WFLW/pip_32_16_60_r101_l2_l1_10_1_nb10.py + +# AFLW, resnet18 +#python lib/train.py experiments/AFLW/pip_32_16_60_r18_l2_l1_10_1_nb10.py +# AFLW, resnet101 +#python lib/train.py experiments/AFLW/pip_32_16_60_r101_l2_l1_10_1_nb10.py + +###################################################################################### +# GSSL + +# 300W + COFW_68 (unlabeled) + WFLW_68 (unlabeled), resnet18, with curriculum +#python lib/train_gssl.py experiments/data_300W_COFW_WFLW/pip_32_16_60_r18_l2_l1_10_1_nb10_wcc.py + +# 300W + CelebA (unlabeled), resnet18, with curriculum +#nohup python lib/train_gssl.py experiments/data_300W_CELEBA/pip_32_16_60_r18_l2_l1_10_1_nb10_wcc.py & + + diff --git a/weights/PIPNet/FaceBoxesV2.pth b/weights/PIPNet/FaceBoxesV2.pth new file mode 100644 index 0000000000000000000000000000000000000000..47a5cadd52bf7a6a32a4071ea2cd24270c5f33e9 --- /dev/null +++ b/weights/PIPNet/FaceBoxesV2.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aae07fec4b62ac655508c06336662538803407852312ca5009fd93fb487d8cd7 +size 4153573 diff --git a/weights/PIPNet/epoch59.pth b/weights/PIPNet/epoch59.pth new file mode 100644 index 0000000000000000000000000000000000000000..c1d94c91a338e8bcc4ed41636a050dd0a78dc6ae --- /dev/null +++ b/weights/PIPNet/epoch59.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:63ad4b1598d8933da1fcf9170cbe4b624660bc4d42debce42ac44e90772cbba0 +size 189011113 diff --git a/weights/arcface/mouth_net_28_56_84_112.pth b/weights/arcface/mouth_net_28_56_84_112.pth new file mode 100644 index 0000000000000000000000000000000000000000..331d029af66b59cf178f07bcce04de32eb2eb9f1 --- /dev/null +++ b/weights/arcface/mouth_net_28_56_84_112.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:be0e327057e3dcf1d676e3a186f7059df4d8f1ea9d9dab71a76c74823a8b51bf +size 127486882 diff --git a/weights/arcface/ms1mv3_arcface_r100_fp16/backbone.pth b/weights/arcface/ms1mv3_arcface_r100_fp16/backbone.pth new file mode 100644 index 0000000000000000000000000000000000000000..6485846fec5a982c50891066be733f5a5a191385 --- /dev/null +++ b/weights/arcface/ms1mv3_arcface_r100_fp16/backbone.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:a566a62357f0c55b679d9ff2f022a294486568be0c00665d39029d0e46a8109b +size 261223796 diff --git a/weights/79999_iter.pth b/weights/bisenet/79999_iter.pth similarity index 100% rename from weights/79999_iter.pth rename to weights/bisenet/79999_iter.pth diff --git a/weights/extracted/G_mouth1_t38_post.pth b/weights/extracted/G_mouth1_t38_post.pth new file mode 100644 index 0000000000000000000000000000000000000000..46a8c01e2653e0c61e86938cd763d698448f57ff --- /dev/null +++ b/weights/extracted/G_mouth1_t38_post.pth @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9cb7e021a8af0eb4197abb90148254d25495ee7adf80ce1e35435dc81c7c487d +size 1140729201