[
  {
    "path": "README.md",
    "content": "# grasp_multiObject_multiGrasp\n\nThis is the implementation of our RA-L work 'Real-world Multi-object, Multi-grasp Detection'. The detector takes RGB-D image input and predicts multiple grasp candidates for a single object or multiple objects, in a single shot. The original arxiv paper can be found [here](https://arxiv.org/pdf/1802.00520.pdf). The final version will be updated after publication process.\n\n<p align=\"center\">\n<img src=\"https://github.com/ivalab/grasp_multiObject_multiGrasp/blob/master/fig/multi_grasp_multi_object.png\" alt=\"drawing\" width=\"300\"/>\n</p>\n\nIf you find it helpful for your research, please consider citing:\n\n    @inproceedings{chu2018deep,\n      title = {Real-World Multiobject, Multigrasp Detection},\n      author = {F. Chu and R. Xu and P. A. Vela},\n      journal = {IEEE Robotics and Automation Letters},\n      year = {2018},\n      volume = {3},\n      number = {4},\n      pages = {3355-3362},\n      DOI = {10.1109/LRA.2018.2852777},\n      ISSN = {2377-3766},\n      month = {Oct}\n    }\n\n\nIf you encounter any questions, please contact me at fujenchu[at]gatech[dot]edu\n\n\n### Demo\n1. Clone this repository\n```\ngit clone https://github.com/ivalab/grasp_multiObject_multiGrasp.git\ncd grasp_multiObject_multiGrasp\n```\n\n2. Build Cython modules\n```\ncd lib\nmake clean\nmake\ncd ..\n```\n\n3. Install [Python COCO API](https://github.com/cocodataset/cocoapi)\n```\ncd data\ngit clone https://github.com/pdollar/coco.git\ncd coco/PythonAPI\nmake\ncd ../../..\n```\n\n4. Download pretrained models\n- trained model for grasp on [dropbox drive](https://www.dropbox.com/s/ldapcpanzqdu7tc/models.zip?dl=0) \n- put under `output/res50/train/default/`\n\n5. Run demo\n```\n./tools/demo_graspRGD.py --net res50 --dataset grasp\n```\nyou can see images pop out.\n\n### Train\n1. Generate data   \n1-1. Download [Cornell Dataset](http://pr.cs.cornell.edu/grasping/rect_data/data.php)   \n1-2. Run `dataPreprocessingTest_fasterrcnn_split.m` (please modify paths according to your structure)   \n1-3. Follow 'Format Your Dataset' section [here](https://github.com/zeyuanxy/fast-rcnn/tree/master/help/train) to check if your data follows VOC format   \n\n2. Train\n```\n./experiments/scripts/train_faster_rcnn.sh 0 graspRGB res50\n```\n\n### ROS version?\nYes! please find it [HERE](https://github.com/ivaROS/ros_deep_grasp)\n\n### Acknowledgment\n\nThis repo borrows tons of code from\n- [tf-faster-rcnn](https://github.com/endernewton/tf-faster-rcnn) by endernewton\n\n### Resources\n- [multi-object grasp dataset](https://github.com/ivalab/grasp_multiObject)\n- [grasp annotation tool](https://github.com/ivalab/grasp_annotation_tool)\n"
  },
  {
    "path": "data/scripts/dataPreprocessingTest_fasterrcnn_split.m",
    "content": "%% script to test dataPreprocessing\r\n%% created by Fu-Jen Chu on 09/15/2016\r\n\r\nclose all\r\nclear\r\n\r\n%parpool(4)\r\naddpath('/media/fujenchu/home3/data/grasps/')\r\n\r\n\r\n% generate list for splits\r\nlist = [100:949 1000:1034];\r\nlist_idx = randperm(length(list));\r\ntrain_list_idx = list_idx(length(list)/5+1:end);\r\ntest_list_idx = list_idx(1:length(list)/5);\r\ntrain_list = list(train_list_idx);\r\ntest_list = list(test_list_idx);\r\n\r\n\r\nfor folder = 1:10\r\ndisplay(['processing folder ' int2str(folder)])\r\n\r\nimgDataDir = ['/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps/' sprintf('%02d',folder) '_rgd'];\r\ntxtDataDir = ['/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps/' sprintf('%02d',folder)];\r\n\r\n%imgDataOutDir = ['/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps/' sprintf('%02d',folder) '_Cropped320_rgd'];\r\nimgDataOutDir = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/Images';\r\nannotationDataOutDir = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/Annotations';\r\nimgSetTrain = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/ImageSets/train.txt'; \r\nimgSetTest = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_tf/data/ImageSets/test.txt'; \r\n\r\nimgFiles = dir([imgDataDir '/*.png']);\r\ntxtFiles = dir([txtDataDir '/*pos.txt']);\r\n\r\nlogfileID = fopen('log.txt','a');\r\nmainfileID = fopen(['/home/fujenchu/projects/deepLearning/deepGraspExtensiveOffline/data/grasps/scripts/trainttt' sprintf('%02d',folder) '.txt'],'a');\r\nfor idx = 1:length(imgFiles) \r\n    %% display progress\r\n    tic\r\n    display(['processing folder: ' sprintf('%02d',folder) ', imgFiles: ' int2str(idx)])\r\n    \r\n    %% reading data\r\n    imgName = imgFiles(idx).name;\r\n    [pathstr,imgname] = fileparts(imgName);\r\n    \r\n    filenum = str2num(imgname(4:7));\r\n    if(any(test_list == filenum))\r\n        file_writeID = fopen(imgSetTest,'a');\r\n        fprintf(file_writeID, '%s\\n', [imgDataDir(1:end-3) 'Cropped320_rgd/' imgname '_preprocessed_1.png' ] );\r\n        fclose(file_writeID);\r\n        continue;\r\n    end\r\n    \r\n    txtName = txtFiles(idx).name;\r\n    [pathstr,txtname] = fileparts(txtName);\r\n\r\n    img = imread([imgDataDir '/' imgname '.png']);\r\n    fileID = fopen([txtDataDir '/' txtname '.txt'],'r');\r\n    sizeA = [2 100];\r\n    bbsIn_all = fscanf(fileID, '%f %f', sizeA);\r\n    fclose(fileID);\r\n    \r\n    %% data pre-processing\r\n    [imagesOut bbsOut] = dataPreprocessing_fasterrcnn(img, bbsIn_all, 227, 5, 5);\r\n    \r\n    % for each augmented image\r\n    for i = 1:1:size(imagesOut,2)\r\n        \r\n        % for each bbs\r\n        file_writeID = fopen([annotationDataOutDir '/' imgname '_preprocessed_' int2str(i) '.txt'],'w');\r\n        printCount = 0;\r\n        for ibbs = 1:1:size(bbsOut{i},2)\r\n          A = bbsOut{i}{ibbs};  \r\n          xy_ctr = sum(A,2)/4; x_ctr = xy_ctr(1); y_ctr = xy_ctr(2);\r\n          width = sqrt(sum((A(:,1) - A(:,2)).^2)); height = sqrt(sum((A(:,2) - A(:,3)).^2));\r\n          if(A(1,1) > A(1,2))\r\n              theta = atan((A(2,2)-A(2,1))/(A(1,1)-A(1,2)));\r\n          else\r\n              theta = atan((A(2,1)-A(2,2))/(A(1,2)-A(1,1))); % note y is facing down\r\n          end  \r\n    \r\n          % process to fasterrcnn\r\n          x_min = x_ctr - width/2; x_max = x_ctr + width/2;\r\n          y_min = y_ctr - height/2; y_max = y_ctr + height/2;\r\n          %if(x_min < 0 || y_min < 0 || x_max > 227 || y_max > 227) display('yoooooooo'); end\r\n          if((x_min < 0 && x_max < 0) || (y_min > 227 && y_max > 227) || (x_min > 227 && x_max > 227) || (y_min < 0 && y_max < 0)) display('xxxxxxxxx'); break; end\r\n          cls = round((theta/pi*180+90)/10) + 1;\r\n          \r\n          % write as lefttop rightdown, Xmin Ymin Xmax Ymax, ex: 261 109 511 705  (x水平 y垂直)\r\n          fprintf(file_writeID, '%d %f %f %f %f\\n', cls, x_min, y_min, x_max, y_max );   \r\n          printCount = printCount+1;\r\n        end\r\n        if(printCount == 0) fprintf(logfileID, '%s\\n', [imgname '_preprocessed_' int2str(i) ]);end\r\n        \r\n        fclose(file_writeID);\r\n        imwrite(imagesOut{i}, [imgDataOutDir '/' imgname '_preprocessed_' int2str(i) '.png']); \r\n        \r\n        % write filename to imageSet \r\n        file_writeID = fopen(imgSetTrain,'a');\r\n        fprintf(file_writeID, '%s\\n', [imgname '_preprocessed_' int2str(i) ] );\r\n        fclose(file_writeID);\r\n\r\n    end\r\n    \r\n\r\n    toc\r\nend\r\nfclose(mainfileID);\r\n\r\nend\r\n"
  },
  {
    "path": "data/scripts/dataPreprocessing_fasterrcnn.m",
    "content": "function [imagesOut bbsOut] = dataPreprocessing( imageIn, bbsIn_all, cropSize, translationShiftNumber, roatateAngleNumber)\r\n% dataPreprocessing function perfroms\r\n% 1) croping\r\n% 2) padding\r\n% 3) rotatation\r\n% 4) shifting\r\n%\r\n% for a input image with a bbs as 4 points, \r\n% dataPreprocessing outputs a set of images with corresponding bbs.\r\n%\r\n%\r\n% Inputs: \r\n%   imageIn: input image (480 by 640 by 3)\r\n%   bbsIn: bounding box (2 by 4)\r\n%   cropSize: output image size\r\n%   shift: shifting offset\r\n%   rotate: rotation angle\r\n%\r\n% Outputs:\r\n%   imagesOut: output images (n images)\r\n%   bbsOut: output bbs according to shift and rotation\r\n%\r\n%% created by Fu-Jen Chu on 09/15/2016\r\n\r\ndebug_dev = 0;\r\ndebug = 0;\r\n%% show image and bbs\r\nif(debug_dev)\r\nfigure(1); imshow(imageIn); hold on;\r\nx = bbsIn_all(1, [1:3]);\r\ny = bbsIn_all(2, [1:3]);\r\nplot(x,y); hold off;\r\nend\r\n\r\n%% crop image and padding image\r\n% cropping to 321 by 321 from center\r\nimgCrop = imcrop(imageIn, [145 65 351 351]); \r\n\r\n% padding to 501 by 501\r\nimgPadding = padarray(imgCrop, [75 75], 'replicate', 'both');\r\n\r\ncount = 1;\r\nfor i_rotate = 1:roatateAngleNumber*translationShiftNumber*translationShiftNumber\r\n    % random roatateAngle\r\n    theta = randi(360)-1;\r\n    %theta = 0;\r\n\r\n    % random translationShift\r\n    dx = randi(101)-51;\r\n    %dx = 0;\r\n\r\n    %% rotation and shifting\r\n    % random translationShift\r\n    dy = randi(101)-51;\r\n    %dy = 0;\r\n\r\n    imgRotate = imrotate(imgPadding, theta);\r\n    if(debug_dev)figure(2); imshow(imgRotate);end\r\n    imgCropRotate = imcrop(imgRotate, [size(imgRotate,1)/2-160-dx size(imgRotate,1)/2-160-dy 320 320]);\r\n    if(debug_dev)figure(3); imshow(imgCropRotate);end \r\n    imgResize = imresize(imgCropRotate, [cropSize cropSize]);\r\n    if(debug)figure(4); imshow(imgResize); hold on;end\r\n\r\n    %% modify bbs\r\n    [m, n] = size(bbsIn_all);\r\n    bbsNum = n/4;\r\n    \r\n    countbbs = 1;\r\n    for idx = 1:bbsNum\r\n      bbsIn =  bbsIn_all(:,idx*4-3:idx*4); \r\n      if(sum(sum(isnan(bbsIn)))) continue; end\r\n      \r\n      bbsInShift = bbsIn - repmat([320; 240], 1, 4);\r\n      R = [cos(theta/180*pi) -sin(theta/180*pi); sin(theta/180*pi) cos(theta/180*pi)];\r\n      bbsRotated = (bbsInShift'*R)';\r\n      bbsInShiftBack = (bbsRotated + repmat([160; 160], 1, 4) + repmat([dx; dy], 1, 4))*cropSize/320;\r\n      if(debug)\r\n        figure(4)\r\n        x = bbsInShiftBack(1, [1:4 1]);\r\n        y = bbsInShiftBack(2, [1:4 1]);\r\n        plot(x,y); hold on; pause(0.01);\r\n      end\r\n      bbsOut{count}{countbbs} = bbsInShiftBack;\r\n      countbbs = countbbs + 1;\r\n    end\r\n    \r\n    imagesOut{count} = imgResize;\r\n    count = count +1;\r\n\r\n   \r\nend\r\n\r\n\r\n\r\nend\r\n"
  },
  {
    "path": "data/scripts/fetch_faster_rcnn_models.sh",
    "content": "#!/bin/bash\n\nDIR=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )/../\" && pwd )\"\ncd $DIR\n\nNET=res101\nFILE=voc_0712_80k-110k.tgz\n# replace it with gs11655.sp.cs.cmu.edu if ladoga.graphics.cs.cmu.edu does not work\nURL=http://ladoga.graphics.cs.cmu.edu/xinleic/tf-faster-rcnn/$NET/$FILE\nCHECKSUM=cb32e9df553153d311cc5095b2f8c340\n\nif [ -f $FILE ]; then\n  echo \"File already exists. Checking md5...\"\n  os=`uname -s`\n  if [ \"$os\" = \"Linux\" ]; then\n    checksum=`md5sum $FILE | awk '{ print $1 }'`\n  elif [ \"$os\" = \"Darwin\" ]; then\n    checksum=`cat $FILE | md5`\n  fi\n  if [ \"$checksum\" = \"$CHECKSUM\" ]; then\n    echo \"Checksum is correct. No need to download.\"\n    exit 0\n  else\n    echo \"Checksum is incorrect. Need to download again.\"\n  fi\nfi\n\necho \"Downloading Resnet 101 Faster R-CNN models Pret-trained on VOC 07+12 (340M)...\"\n\nwget $URL -O $FILE\n\necho \"Unzipping...\"\n\ntar zxvf $FILE\n\necho \"Done. Please run this command again to verify that checksum = $CHECKSUM.\"\n"
  },
  {
    "path": "experiments/cfgs/res101-lg.yml",
    "content": "EXP_DIR: res101-lg\nTRAIN:\n  HAS_RPN: True\n  IMS_PER_BATCH: 1\n  BBOX_NORMALIZE_TARGETS_PRECOMPUTED: True\n  RPN_POSITIVE_OVERLAP: 0.7\n  RPN_BATCHSIZE: 256\n  PROPOSAL_METHOD: gt\n  BG_THRESH_LO: 0.0\n  DISPLAY: 20\n  BATCH_SIZE: 256\n  WEIGHT_DECAY: 0.0001\n  DOUBLE_BIAS: False\n  SNAPSHOT_PREFIX: res101_faster_rcnn\n  SCALES: [800]\n  MAX_SIZE: 1333\nTEST:\n  HAS_RPN: True\n  SCALES: [800]\n  MAX_SIZE: 1333\n  RPN_POST_NMS_TOP_N: 1000\nPOOLING_MODE: crop\nANCHOR_SCALES: [2,4,8,16,32]\n"
  },
  {
    "path": "experiments/cfgs/res101.yml",
    "content": "EXP_DIR: res101\nTRAIN:\n  HAS_RPN: True\n  IMS_PER_BATCH: 1\n  BBOX_NORMALIZE_TARGETS_PRECOMPUTED: True\n  RPN_POSITIVE_OVERLAP: 0.7\n  RPN_BATCHSIZE: 256\n  PROPOSAL_METHOD: gt\n  BG_THRESH_LO: 0.0\n  DISPLAY: 20\n  BATCH_SIZE: 256\n  WEIGHT_DECAY: 0.0001\n  DOUBLE_BIAS: False\n  SNAPSHOT_PREFIX: res101_faster_rcnn\nTEST:\n  HAS_RPN: True\nPOOLING_MODE: crop\n"
  },
  {
    "path": "experiments/cfgs/res50.yml",
    "content": "EXP_DIR: res50\nTRAIN:\n  HAS_RPN: True\n  IMS_PER_BATCH: 1\n  BBOX_NORMALIZE_TARGETS_PRECOMPUTED: True\n  RPN_POSITIVE_OVERLAP: 0.7\n  RPN_BATCHSIZE: 256\n  PROPOSAL_METHOD: gt\n  BG_THRESH_LO: 0.0\n  DISPLAY: 20\n  BATCH_SIZE: 256\n  WEIGHT_DECAY: 0.0001\n  DOUBLE_BIAS: False\n  SNAPSHOT_PREFIX: res50_faster_rcnn\nTEST:\n  HAS_RPN: True\nPOOLING_MODE: crop\n"
  },
  {
    "path": "experiments/cfgs/vgg16.yml",
    "content": "EXP_DIR: vgg16\nTRAIN:\n  HAS_RPN: True\n  IMS_PER_BATCH: 1\n  BBOX_NORMALIZE_TARGETS_PRECOMPUTED: True\n  RPN_POSITIVE_OVERLAP: 0.7\n  RPN_BATCHSIZE: 256\n  PROPOSAL_METHOD: gt\n  BG_THRESH_LO: 0.0\n  DISPLAY: 20\n  BATCH_SIZE: 256\n  SNAPSHOT_PREFIX: vgg16_faster_rcnn\nTEST:\n  HAS_RPN: True\nPOOLING_MODE: crop\n"
  },
  {
    "path": "experiments/logs/.gitignore",
    "content": "*.txt.*\n"
  },
  {
    "path": "experiments/scripts/convert_vgg16.sh",
    "content": "#!/bin/bash\n\nset -x\nset -e\n\nexport PYTHONUNBUFFERED=\"True\"\n\nGPU_ID=$1\nDATASET=$2\nNET=vgg16\n\narray=( $@ )\nlen=${#array[@]}\nEXTRA_ARGS=${array[@]:2:$len}\nEXTRA_ARGS_SLUG=${EXTRA_ARGS// /_}\n\ncase ${DATASET} in\n  pascal_voc)\n    TRAIN_IMDB=\"voc_2007_trainval\"\n    TEST_IMDB=\"voc_2007_test\"\n    STEPSIZE=50000\n    ITERS=70000\n    ANCHORS=\"[8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  pascal_voc_0712)\n    TRAIN_IMDB=\"voc_2007_trainval+voc_2012_trainval\"\n    TEST_IMDB=\"voc_2007_test\"\n    STEPSIZE=80000\n    ITERS=110000\n    ANCHORS=\"[8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  coco)\n    TRAIN_IMDB=\"coco_2014_train+coco_2014_valminusminival\"\n    TEST_IMDB=\"coco_2014_minival\"\n    STEPSIZE=350000\n    ITERS=490000\n    ANCHORS=\"[4,8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  *)\n    echo \"No dataset given\"\n    exit\n    ;;\nesac\n\nset +x\nNET_FINAL=${NET}_faster_rcnn_iter_${ITERS}\nset -x\n\nif [ ! -f ${NET_FINAL}.index ]; then\n    if [[ ! -z  ${EXTRA_ARGS_SLUG}  ]]; then\n        CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/convert_from_depre.py \\\n            --snapshot ${NET_FINAL} \\\n            --imdb ${TRAIN_IMDB} \\\n            --iters ${ITERS} \\\n            --cfg experiments/cfgs/${NET}.yml \\\n            --tag ${EXTRA_ARGS_SLUG} \\\n            --set ANCHOR_SCALES ${ANCHORS} ANCHOR_RATIOS ${RATIOS} TRAIN.STEPSIZE ${STEPSIZE} ${EXTRA_ARGS}\n    else\n        CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/convert_from_depre.py \\\n            --snapshot ${NET_FINAL} \\\n            --imdb ${TRAIN_IMDB} \\\n            --iters ${ITERS} \\\n            --cfg experiments/cfgs/${NET}.yml \\\n            --set ANCHOR_SCALES ${ANCHORS} ANCHOR_RATIOS ${RATIOS} TRAIN.STEPSIZE ${STEPSIZE} ${EXTRA_ARGS}\n    fi\nfi\n\n"
  },
  {
    "path": "experiments/scripts/test_faster_rcnn.sh",
    "content": "#!/bin/bash\n\nset -x\nset -e\n\nexport PYTHONUNBUFFERED=\"True\"\n\nGPU_ID=$1\nDATASET=$2\nNET=$3\n\narray=( $@ )\nlen=${#array[@]}\nEXTRA_ARGS=${array[@]:3:$len}\nEXTRA_ARGS_SLUG=${EXTRA_ARGS// /_}\n\ncase ${DATASET} in\n  pascal_voc)\n    TRAIN_IMDB=\"voc_2007_trainval\"\n    TEST_IMDB=\"voc_2007_test\"\n    ITERS=70000\n    ANCHORS=\"[8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  pascal_voc_0712)\n    TRAIN_IMDB=\"voc_2007_trainval+voc_2012_trainval\"\n    TEST_IMDB=\"voc_2007_test\"\n    ITERS=110000\n    ANCHORS=\"[8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  coco)\n    TRAIN_IMDB=\"coco_2014_train+coco_2014_valminusminival\"\n    TEST_IMDB=\"coco_2014_minival\"\n    ITERS=490000\n    ANCHORS=\"[4,8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  *)\n    echo \"No dataset given\"\n    exit\n    ;;\nesac\n\nLOG=\"experiments/logs/test_${NET}_${TRAIN_IMDB}_${EXTRA_ARGS_SLUG}.txt.`date +'%Y-%m-%d_%H-%M-%S'`\"\nexec &> >(tee -a \"$LOG\")\necho Logging output to \"$LOG\"\n\nset +x\nif [[ ! -z  ${EXTRA_ARGS_SLUG}  ]]; then\n  NET_FINAL=output/${NET}/${TRAIN_IMDB}/${EXTRA_ARGS_SLUG}/${NET}_faster_rcnn_iter_${ITERS}.ckpt\nelse\n  NET_FINAL=output/${NET}/${TRAIN_IMDB}/default/${NET}_faster_rcnn_iter_${ITERS}.ckpt\nfi\nset -x\n\nif [[ ! -z  ${EXTRA_ARGS_SLUG}  ]]; then\n  CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/test_net.py \\\n    --imdb ${TEST_IMDB} \\\n    --model ${NET_FINAL} \\\n    --cfg experiments/cfgs/${NET}.yml \\\n    --tag ${EXTRA_ARGS_SLUG} \\\n    --net ${NET} \\\n    --set ANCHOR_SCALES ${ANCHORS} ANCHOR_RATIOS ${RATIOS} ${EXTRA_ARGS}\nelse\n  CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/test_net.py \\\n    --imdb ${TEST_IMDB} \\\n    --model ${NET_FINAL} \\\n    --cfg experiments/cfgs/${NET}.yml \\\n    --net ${NET} \\\n    --set ANCHOR_SCALES ${ANCHORS} ANCHOR_RATIOS ${RATIOS} ${EXTRA_ARGS}\nfi\n\n"
  },
  {
    "path": "experiments/scripts/train_faster_rcnn.sh",
    "content": "#!/bin/bash\n\nset -x\nset -e\n\nexport PYTHONUNBUFFERED=\"True\"\n\nGPU_ID=$1\nDATASET=$2\nNET=$3\n\narray=( $@ )\nlen=${#array[@]}\nEXTRA_ARGS=${array[@]:3:$len}\nEXTRA_ARGS_SLUG=${EXTRA_ARGS// /_}\n\ncase ${DATASET} in\n  graspRGB)\n    TRAIN_IMDB=\"graspRGB_train\"\n    TEST_IMDB=\"graspRGB_test\"\n    STEPSIZE=50000\n    ITERS=240000\n    #ANCHORS=\"[2,4,8,16,32]\"\n    ANCHORS=\"[8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  pascal_voc)\n    TRAIN_IMDB=\"voc_2007_trainval\"\n    TEST_IMDB=\"voc_2007_test\"\n    STEPSIZE=50000\n    ITERS=70000\n    ANCHORS=\"[8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  pascal_voc_0712)\n    TRAIN_IMDB=\"voc_2007_trainval+voc_2012_trainval\"\n    TEST_IMDB=\"voc_2007_test\"\n    STEPSIZE=80000\n    ITERS=110000\n    ANCHORS=\"[8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  coco)\n    TRAIN_IMDB=\"coco_2014_train+coco_2014_valminusminival\"\n    TEST_IMDB=\"coco_2014_minival\"\n    STEPSIZE=350000\n    ITERS=490000\n    ANCHORS=\"[4,8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  *)\n    echo \"No dataset given\"\n    exit\n    ;;\nesac\n\nLOG=\"experiments/logs/${NET}_${TRAIN_IMDB}_${EXTRA_ARGS_SLUG}_${NET}.txt.`date +'%Y-%m-%d_%H-%M-%S'`\"\nexec &> >(tee -a \"$LOG\")\necho Logging output to \"$LOG\"\n\nset +x\nif [[ ! -z  ${EXTRA_ARGS_SLUG}  ]]; then\n    NET_FINAL=output/${NET}/${TRAIN_IMDB}/${EXTRA_ARGS_SLUG}/${NET}_faster_rcnn_iter_${ITERS}.ckpt\nelse\n    NET_FINAL=output/${NET}/${TRAIN_IMDB}/default/${NET}_faster_rcnn_iter_${ITERS}.ckpt\nfi\nset -x\n\nif [ ! -f ${NET_FINAL}.index ]; then\n    if [[ ! -z  ${EXTRA_ARGS_SLUG}  ]]; then\n        CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/trainval_net.py \\\n            --weight data/imagenet_weights/${NET}.ckpt \\\n            --imdb ${TRAIN_IMDB} \\\n            --imdbval ${TEST_IMDB} \\\n            --iters ${ITERS} \\\n            --cfg experiments/cfgs/${NET}.yml \\\n            --tag ${EXTRA_ARGS_SLUG} \\\n            --net ${NET} \\\n            --set ANCHOR_SCALES ${ANCHORS} ANCHOR_RATIOS ${RATIOS} TRAIN.STEPSIZE ${STEPSIZE} ${EXTRA_ARGS}\n    else\n        CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/trainval_net.py \\\n            --weight data/imagenet_weights/${NET}.ckpt \\\n            --imdb ${TRAIN_IMDB} \\\n            --imdbval ${TEST_IMDB} \\\n            --iters ${ITERS} \\\n            --cfg experiments/cfgs/${NET}.yml \\\n            --net ${NET} \\\n            --set ANCHOR_SCALES ${ANCHORS} ANCHOR_RATIOS ${RATIOS} TRAIN.STEPSIZE ${STEPSIZE} ${EXTRA_ARGS}\n    fi\nfi\n\n./experiments/scripts/test_faster_rcnn.sh $@\n"
  },
  {
    "path": "experiments/scripts/train_faster_rcnn.sh~",
    "content": "#!/bin/bash\n\nset -x\nset -e\n\nexport PYTHONUNBUFFERED=\"True\"\n\nGPU_ID=$1\nDATASET=$2\nNET=$3\n\narray=( $@ )\nlen=${#array[@]}\nEXTRA_ARGS=${array[@]:3:$len}\nEXTRA_ARGS_SLUG=${EXTRA_ARGS// /_}\n\ncase ${DATASET} in\n  graspRGB)\n    TRAIN_IMDB=\"graspRGB_train\"\n    TEST_IMDB=\"graspRGB_test\"\n    STEPSIZE=50000\n    ITERS=160000\n    #ANCHORS=\"[2,4,8,16,32]\"\n    ANCHORS=\"[8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  pascal_voc)\n    TRAIN_IMDB=\"voc_2007_trainval\"\n    TEST_IMDB=\"voc_2007_test\"\n    STEPSIZE=50000\n    ITERS=70000\n    ANCHORS=\"[8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  pascal_voc_0712)\n    TRAIN_IMDB=\"voc_2007_trainval+voc_2012_trainval\"\n    TEST_IMDB=\"voc_2007_test\"\n    STEPSIZE=80000\n    ITERS=110000\n    ANCHORS=\"[8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  coco)\n    TRAIN_IMDB=\"coco_2014_train+coco_2014_valminusminival\"\n    TEST_IMDB=\"coco_2014_minival\"\n    STEPSIZE=350000\n    ITERS=490000\n    ANCHORS=\"[4,8,16,32]\"\n    RATIOS=\"[0.5,1,2]\"\n    ;;\n  *)\n    echo \"No dataset given\"\n    exit\n    ;;\nesac\n\nLOG=\"experiments/logs/${NET}_${TRAIN_IMDB}_${EXTRA_ARGS_SLUG}_${NET}.txt.`date +'%Y-%m-%d_%H-%M-%S'`\"\nexec &> >(tee -a \"$LOG\")\necho Logging output to \"$LOG\"\n\nset +x\nif [[ ! -z  ${EXTRA_ARGS_SLUG}  ]]; then\n    NET_FINAL=output/${NET}/${TRAIN_IMDB}/${EXTRA_ARGS_SLUG}/${NET}_faster_rcnn_iter_${ITERS}.ckpt\nelse\n    NET_FINAL=output/${NET}/${TRAIN_IMDB}/default/${NET}_faster_rcnn_iter_${ITERS}.ckpt\nfi\nset -x\n\nif [ ! -f ${NET_FINAL}.index ]; then\n    if [[ ! -z  ${EXTRA_ARGS_SLUG}  ]]; then\n        CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/trainval_net.py \\\n            --weight data/imagenet_weights/${NET}.ckpt \\\n            --imdb ${TRAIN_IMDB} \\\n            --imdbval ${TEST_IMDB} \\\n            --iters ${ITERS} \\\n            --cfg experiments/cfgs/${NET}.yml \\\n            --tag ${EXTRA_ARGS_SLUG} \\\n            --net ${NET} \\\n            --set ANCHOR_SCALES ${ANCHORS} ANCHOR_RATIOS ${RATIOS} TRAIN.STEPSIZE ${STEPSIZE} ${EXTRA_ARGS}\n    else\n        CUDA_VISIBLE_DEVICES=${GPU_ID} time python ./tools/trainval_net.py \\\n            --weight data/imagenet_weights/${NET}.ckpt \\\n            --imdb ${TRAIN_IMDB} \\\n            --imdbval ${TEST_IMDB} \\\n            --iters ${ITERS} \\\n            --cfg experiments/cfgs/${NET}.yml \\\n            --net ${NET} \\\n            --set ANCHOR_SCALES ${ANCHORS} ANCHOR_RATIOS ${RATIOS} TRAIN.STEPSIZE ${STEPSIZE} ${EXTRA_ARGS}\n    fi\nfi\n\n./experiments/scripts/test_faster_rcnn.sh $@\n"
  },
  {
    "path": "lib/Makefile",
    "content": "all:\n\tpython setup.py build_ext --inplace\n\trm -rf build\nclean:\n\trm -rf */*.pyc\n\trm -rf */*.so\n"
  },
  {
    "path": "lib/datasets/VOCdevkit-matlab-wrapper/get_voc_opts.m",
    "content": "function VOCopts = get_voc_opts(path)\n\ntmp = pwd;\ncd(path);\ntry\n  addpath('VOCcode');\n  VOCinit;\ncatch\n  rmpath('VOCcode');\n  cd(tmp);\n  error(sprintf('VOCcode directory not found under %s', path));\nend\nrmpath('VOCcode');\ncd(tmp);\n"
  },
  {
    "path": "lib/datasets/VOCdevkit-matlab-wrapper/voc_eval.m",
    "content": "function res = voc_eval(path, comp_id, test_set, output_dir)\n\nVOCopts = get_voc_opts(path);\nVOCopts.testset = test_set;\n\nfor i = 1:length(VOCopts.classes)\n  cls = VOCopts.classes{i};\n  res(i) = voc_eval_cls(cls, VOCopts, comp_id, output_dir);\nend\n\nfprintf('\\n~~~~~~~~~~~~~~~~~~~~\\n');\nfprintf('Results:\\n');\naps = [res(:).ap]';\nfprintf('%.1f\\n', aps * 100);\nfprintf('%.1f\\n', mean(aps) * 100);\nfprintf('~~~~~~~~~~~~~~~~~~~~\\n');\n\nfunction res = voc_eval_cls(cls, VOCopts, comp_id, output_dir)\n\ntest_set = VOCopts.testset;\nyear = VOCopts.dataset(4:end);\n\naddpath(fullfile(VOCopts.datadir, 'VOCcode'));\n\nres_fn = sprintf(VOCopts.detrespath, comp_id, cls);\n\nrecall = [];\nprec = [];\nap = 0;\nap_auc = 0;\n\ndo_eval = (str2num(year) <= 2007) | ~strcmp(test_set, 'test');\nif do_eval\n  % Bug in VOCevaldet requires that tic has been called first\n  tic;\n  [recall, prec, ap] = VOCevaldet(VOCopts, comp_id, cls, true);\n  ap_auc = xVOCap(recall, prec);\n\n  % force plot limits\n  ylim([0 1]);\n  xlim([0 1]);\n\n  print(gcf, '-djpeg', '-r0', ...\n        [output_dir '/' cls '_pr.jpg']);\nend\nfprintf('!!! %s : %.4f %.4f\\n', cls, ap, ap_auc);\n\nres.recall = recall;\nres.prec = prec;\nres.ap = ap;\nres.ap_auc = ap_auc;\n\nsave([output_dir '/' cls '_pr.mat'], ...\n     'res', 'recall', 'prec', 'ap', 'ap_auc');\n\nrmpath(fullfile(VOCopts.datadir, 'VOCcode'));\n"
  },
  {
    "path": "lib/datasets/VOCdevkit-matlab-wrapper/xVOCap.m",
    "content": "function ap = xVOCap(rec,prec)\r\n% From the PASCAL VOC 2011 devkit\r\n\r\nmrec=[0 ; rec ; 1];\r\nmpre=[0 ; prec ; 0];\r\nfor i=numel(mpre)-1:-1:1\r\n    mpre(i)=max(mpre(i),mpre(i+1));\r\nend\r\ni=find(mrec(2:end)~=mrec(1:end-1))+1;\r\nap=sum((mrec(i)-mrec(i-1)).*mpre(i));\r\n"
  },
  {
    "path": "lib/datasets/__init__.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n"
  },
  {
    "path": "lib/datasets/coco.py",
    "content": "# --------------------------------------------------------\n# Fast/er R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick and Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nfrom datasets.imdb import imdb\nimport datasets.ds_utils as ds_utils\nfrom model.config import cfg\nimport os.path as osp\nimport sys\nimport os\nimport numpy as np\nimport scipy.sparse\nimport scipy.io as sio\nimport pickle\nimport json\nimport uuid\n# COCO API\nfrom pycocotools.coco import COCO\nfrom pycocotools.cocoeval import COCOeval\nfrom pycocotools import mask as COCOmask\n\nclass coco(imdb):\n  def __init__(self, image_set, year):\n    imdb.__init__(self, 'coco_' + year + '_' + image_set)\n    # COCO specific config options\n    self.config = {'use_salt': True,\n                   'cleanup': True}\n    # name, paths\n    self._year = year\n    self._image_set = image_set\n    self._data_path = osp.join(cfg.DATA_DIR, 'coco')\n    # load COCO API, classes, class <-> id mappings\n    self._COCO = COCO(self._get_ann_file())\n    cats = self._COCO.loadCats(self._COCO.getCatIds())\n    self._classes = tuple(['__background__'] + [c['name'] for c in cats])\n    self._class_to_ind = dict(list(zip(self.classes, list(range(self.num_classes)))))\n    self._class_to_coco_cat_id = dict(list(zip([c['name'] for c in cats],\n                                               self._COCO.getCatIds())))\n    self._image_index = self._load_image_set_index()\n    # Default to roidb handler\n    self.set_proposal_method('gt')\n    self.competition_mode(False)\n\n    # Some image sets are \"views\" (i.e. subsets) into others.\n    # For example, minival2014 is a random 5000 image subset of val2014.\n    # This mapping tells us where the view's images and proposals come from.\n    self._view_map = {\n      'minival2014': 'val2014',  # 5k val2014 subset\n      'valminusminival2014': 'val2014',  # val2014 \\setminus minival2014\n      'test-dev2015': 'test2015',\n    }\n    coco_name = image_set + year  # e.g., \"val2014\"\n    self._data_name = (self._view_map[coco_name]\n                       if coco_name in self._view_map\n                       else coco_name)\n    # Dataset splits that have ground-truth annotations (test splits\n    # do not have gt annotations)\n    self._gt_splits = ('train', 'val', 'minival')\n\n  def _get_ann_file(self):\n    prefix = 'instances' if self._image_set.find('test') == -1 \\\n      else 'image_info'\n    return osp.join(self._data_path, 'annotations',\n                    prefix + '_' + self._image_set + self._year + '.json')\n\n  def _load_image_set_index(self):\n    \"\"\"\n    Load image ids.\n    \"\"\"\n    image_ids = self._COCO.getImgIds()\n    return image_ids\n\n  def _get_widths(self):\n    anns = self._COCO.loadImgs(self._image_index)\n    widths = [ann['width'] for ann in anns]\n    return widths\n\n  def image_path_at(self, i):\n    \"\"\"\n    Return the absolute path to image i in the image sequence.\n    \"\"\"\n    return self.image_path_from_index(self._image_index[i])\n\n  def image_path_from_index(self, index):\n    \"\"\"\n    Construct an image path from the image's \"index\" identifier.\n    \"\"\"\n    # Example image path for index=119993:\n    #   images/train2014/COCO_train2014_000000119993.jpg\n    file_name = ('COCO_' + self._data_name + '_' +\n                 str(index).zfill(12) + '.jpg')\n    image_path = osp.join(self._data_path, 'images',\n                          self._data_name, file_name)\n    assert osp.exists(image_path), \\\n      'Path does not exist: {}'.format(image_path)\n    return image_path\n\n  def gt_roidb(self):\n    \"\"\"\n    Return the database of ground-truth regions of interest.\n    This function loads/saves from/to a cache file to speed up future calls.\n    \"\"\"\n    cache_file = osp.join(self.cache_path, self.name + '_gt_roidb.pkl')\n    if osp.exists(cache_file):\n      with open(cache_file, 'rb') as fid:\n        roidb = pickle.load(fid)\n      print('{} gt roidb loaded from {}'.format(self.name, cache_file))\n      return roidb\n\n    gt_roidb = [self._load_coco_annotation(index)\n                for index in self._image_index]\n\n    with open(cache_file, 'wb') as fid:\n      pickle.dump(gt_roidb, fid, pickle.HIGHEST_PROTOCOL)\n    print('wrote gt roidb to {}'.format(cache_file))\n    return gt_roidb\n\n  def _load_coco_annotation(self, index):\n    \"\"\"\n    Loads COCO bounding-box instance annotations. Crowd instances are\n    handled by marking their overlaps (with all categories) to -1. This\n    overlap value means that crowd \"instances\" are excluded from training.\n    \"\"\"\n    im_ann = self._COCO.loadImgs(index)[0]\n    width = im_ann['width']\n    height = im_ann['height']\n\n    annIds = self._COCO.getAnnIds(imgIds=index, iscrowd=None)\n    objs = self._COCO.loadAnns(annIds)\n    # Sanitize bboxes -- some are invalid\n    valid_objs = []\n    for obj in objs:\n      x1 = np.max((0, obj['bbox'][0]))\n      y1 = np.max((0, obj['bbox'][1]))\n      x2 = np.min((width - 1, x1 + np.max((0, obj['bbox'][2] - 1))))\n      y2 = np.min((height - 1, y1 + np.max((0, obj['bbox'][3] - 1))))\n      if obj['area'] > 0 and x2 >= x1 and y2 >= y1:\n        obj['clean_bbox'] = [x1, y1, x2, y2]\n        valid_objs.append(obj)\n    objs = valid_objs\n    num_objs = len(objs)\n\n    boxes = np.zeros((num_objs, 4), dtype=np.uint16)\n    gt_classes = np.zeros((num_objs), dtype=np.int32)\n    overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)\n    seg_areas = np.zeros((num_objs), dtype=np.float32)\n\n    # Lookup table to map from COCO category ids to our internal class\n    # indices\n    coco_cat_id_to_class_ind = dict([(self._class_to_coco_cat_id[cls],\n                                      self._class_to_ind[cls])\n                                     for cls in self._classes[1:]])\n\n    for ix, obj in enumerate(objs):\n      cls = coco_cat_id_to_class_ind[obj['category_id']]\n      boxes[ix, :] = obj['clean_bbox']\n      gt_classes[ix] = cls\n      seg_areas[ix] = obj['area']\n      if obj['iscrowd']:\n        # Set overlap to -1 for all classes for crowd objects\n        # so they will be excluded during training\n        overlaps[ix, :] = -1.0\n      else:\n        overlaps[ix, cls] = 1.0\n\n    ds_utils.validate_boxes(boxes, width=width, height=height)\n    overlaps = scipy.sparse.csr_matrix(overlaps)\n    return {'width': width,\n            'height': height,\n            'boxes': boxes,\n            'gt_classes': gt_classes,\n            'gt_overlaps': overlaps,\n            'flipped': False,\n            'seg_areas': seg_areas}\n\n  def _get_widths(self):\n    return [r['width'] for r in self.roidb]\n\n  def append_flipped_images(self):\n    num_images = self.num_images\n    widths = self._get_widths()\n    for i in range(num_images):\n      boxes = self.roidb[i]['boxes'].copy()\n      oldx1 = boxes[:, 0].copy()\n      oldx2 = boxes[:, 2].copy()\n      boxes[:, 0] = widths[i] - oldx2 - 1\n      boxes[:, 2] = widths[i] - oldx1 - 1\n      assert (boxes[:, 2] >= boxes[:, 0]).all()\n      entry = {'width': widths[i],\n               'height': self.roidb[i]['height'],\n               'boxes': boxes,\n               'gt_classes': self.roidb[i]['gt_classes'],\n               'gt_overlaps': self.roidb[i]['gt_overlaps'],\n               'flipped': True,\n               'seg_areas': self.roidb[i]['seg_areas']}\n\n      self.roidb.append(entry)\n    self._image_index = self._image_index * 2\n\n  def _get_box_file(self, index):\n    # first 14 chars / first 22 chars / all chars + .mat\n    # COCO_val2014_0/COCO_val2014_000000447/COCO_val2014_000000447991.mat\n    file_name = ('COCO_' + self._data_name +\n                 '_' + str(index).zfill(12) + '.mat')\n    return osp.join(file_name[:14], file_name[:22], file_name)\n\n  def _print_detection_eval_metrics(self, coco_eval):\n    IoU_lo_thresh = 0.5\n    IoU_hi_thresh = 0.95\n\n    def _get_thr_ind(coco_eval, thr):\n      ind = np.where((coco_eval.params.iouThrs > thr - 1e-5) &\n                     (coco_eval.params.iouThrs < thr + 1e-5))[0][0]\n      iou_thr = coco_eval.params.iouThrs[ind]\n      assert np.isclose(iou_thr, thr)\n      return ind\n\n    ind_lo = _get_thr_ind(coco_eval, IoU_lo_thresh)\n    ind_hi = _get_thr_ind(coco_eval, IoU_hi_thresh)\n    # precision has dims (iou, recall, cls, area range, max dets)\n    # area range index 0: all area ranges\n    # max dets index 2: 100 per image\n    precision = \\\n      coco_eval.eval['precision'][ind_lo:(ind_hi + 1), :, :, 0, 2]\n    ap_default = np.mean(precision[precision > -1])\n    print(('~~~~ Mean and per-category AP @ IoU=[{:.2f},{:.2f}] '\n           '~~~~').format(IoU_lo_thresh, IoU_hi_thresh))\n    print('{:.1f}'.format(100 * ap_default))\n    for cls_ind, cls in enumerate(self.classes):\n      if cls == '__background__':\n        continue\n      # minus 1 because of __background__\n      precision = coco_eval.eval['precision'][ind_lo:(ind_hi + 1), :, cls_ind - 1, 0, 2]\n      ap = np.mean(precision[precision > -1])\n      print('{:.1f}'.format(100 * ap))\n\n    print('~~~~ Summary metrics ~~~~')\n    coco_eval.summarize()\n\n  def _do_detection_eval(self, res_file, output_dir):\n    ann_type = 'bbox'\n    coco_dt = self._COCO.loadRes(res_file)\n    coco_eval = COCOeval(self._COCO, coco_dt)\n    coco_eval.params.useSegm = (ann_type == 'segm')\n    coco_eval.evaluate()\n    coco_eval.accumulate()\n    self._print_detection_eval_metrics(coco_eval)\n    eval_file = osp.join(output_dir, 'detection_results.pkl')\n    with open(eval_file, 'wb') as fid:\n      pickle.dump(coco_eval, fid, pickle.HIGHEST_PROTOCOL)\n    print('Wrote COCO eval results to: {}'.format(eval_file))\n\n  def _coco_results_one_category(self, boxes, cat_id):\n    results = []\n    for im_ind, index in enumerate(self.image_index):\n      dets = boxes[im_ind].astype(np.float)\n      if dets == []:\n        continue\n      scores = dets[:, -1]\n      xs = dets[:, 0]\n      ys = dets[:, 1]\n      ws = dets[:, 2] - xs + 1\n      hs = dets[:, 3] - ys + 1\n      results.extend(\n        [{'image_id': index,\n          'category_id': cat_id,\n          'bbox': [xs[k], ys[k], ws[k], hs[k]],\n          'score': scores[k]} for k in range(dets.shape[0])])\n    return results\n\n  def _write_coco_results_file(self, all_boxes, res_file):\n    # [{\"image_id\": 42,\n    #   \"category_id\": 18,\n    #   \"bbox\": [258.15,41.29,348.26,243.78],\n    #   \"score\": 0.236}, ...]\n    results = []\n    for cls_ind, cls in enumerate(self.classes):\n      if cls == '__background__':\n        continue\n      print('Collecting {} results ({:d}/{:d})'.format(cls, cls_ind,\n                                                       self.num_classes - 1))\n      coco_cat_id = self._class_to_coco_cat_id[cls]\n      results.extend(self._coco_results_one_category(all_boxes[cls_ind],\n                                                     coco_cat_id))\n    print('Writing results json to {}'.format(res_file))\n    with open(res_file, 'w') as fid:\n      json.dump(results, fid)\n\n  def evaluate_detections(self, all_boxes, output_dir):\n    res_file = osp.join(output_dir, ('detections_' +\n                                     self._image_set +\n                                     self._year +\n                                     '_results'))\n    if self.config['use_salt']:\n      res_file += '_{}'.format(str(uuid.uuid4()))\n    res_file += '.json'\n    self._write_coco_results_file(all_boxes, res_file)\n    # Only do evaluation on non-test sets\n    if self._image_set.find('test') == -1:\n      self._do_detection_eval(res_file, output_dir)\n    # Optionally cleanup results json file\n    if self.config['cleanup']:\n      os.remove(res_file)\n\n  def competition_mode(self, on):\n    if on:\n      self.config['use_salt'] = False\n      self.config['cleanup'] = False\n    else:\n      self.config['use_salt'] = True\n      self.config['cleanup'] = True\n"
  },
  {
    "path": "lib/datasets/ds_utils.py",
    "content": "# --------------------------------------------------------\n# Fast/er R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\n\n\ndef unique_boxes(boxes, scale=1.0):\n  \"\"\"Return indices of unique boxes.\"\"\"\n  v = np.array([1, 1e3, 1e6, 1e9])\n  hashes = np.round(boxes * scale).dot(v)\n  _, index = np.unique(hashes, return_index=True)\n  return np.sort(index)\n\n\ndef xywh_to_xyxy(boxes):\n  \"\"\"Convert [x y w h] box format to [x1 y1 x2 y2] format.\"\"\"\n  return np.hstack((boxes[:, 0:2], boxes[:, 0:2] + boxes[:, 2:4] - 1))\n\n\ndef xyxy_to_xywh(boxes):\n  \"\"\"Convert [x1 y1 x2 y2] box format to [x y w h] format.\"\"\"\n  return np.hstack((boxes[:, 0:2], boxes[:, 2:4] - boxes[:, 0:2] + 1))\n\n\ndef validate_boxes(boxes, width=0, height=0):\n  \"\"\"Check that a set of boxes are valid.\"\"\"\n  x1 = boxes[:, 0]\n  y1 = boxes[:, 1]\n  x2 = boxes[:, 2]\n  y2 = boxes[:, 3]\n  assert (x1 >= 0).all()\n  assert (y1 >= 0).all()\n  assert (x2 >= x1).all()\n  assert (y2 >= y1).all()\n  assert (x2 < width).all()\n  assert (y2 < height).all()\n\n\ndef filter_small_boxes(boxes, min_size):\n  w = boxes[:, 2] - boxes[:, 0]\n  h = boxes[:, 3] - boxes[:, 1]\n  keep = np.where((w >= min_size) & (h > min_size))[0]\n  return keep\n"
  },
  {
    "path": "lib/datasets/factory.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"Factory method for easily getting imdbs by name.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\nfrom datasets.graspRGB import graspRGB\n\n__sets = {}\nfrom datasets.pascal_voc import pascal_voc\nfrom datasets.coco import coco\n\nimport numpy as np\n\n# Set up voc_<year>_<split> \nfor year in ['2007', '2012']:\n  for split in ['train', 'val', 'trainval', 'test']:\n    name = 'voc_{}_{}'.format(year, split)\n    __sets[name] = (lambda split=split, year=year: pascal_voc(split, year))\n\n# Set up coco_2014_<split>\nfor year in ['2014']:\n  for split in ['train', 'val', 'minival', 'valminusminival', 'trainval']:\n    name = 'coco_{}_{}'.format(year, split)\n    __sets[name] = (lambda split=split, year=year: coco(split, year))\n\n# Set up coco_2015_<split>\nfor year in ['2015']:\n  for split in ['test', 'test-dev']:\n    name = 'coco_{}_{}'.format(year, split)\n    __sets[name] = (lambda split=split, year=year: coco(split, year))\n\n# Set up graspRGB_<split> using selective search \"fast\" mode # added by FC\ngraspRGB_devkit_path = '/media/fujenchu/home3/fasterrcnn_grasp/rgb_multibbs_5_5_5_object_tf'\nfor split in ['train', 'test']:\n    name = '{}_{}'.format('graspRGB', split)\n    __sets[name] = (lambda split=split: graspRGB(split, graspRGB_devkit_path))\n\ndef get_imdb(name):\n  \"\"\"Get an imdb (image database) by name.\"\"\"\n  if name not in __sets:\n    raise KeyError('Unknown dataset: {}'.format(name))\n  return __sets[name]()\n\n\ndef list_imdbs():\n  \"\"\"List all registered imdbs.\"\"\"\n  return list(__sets.keys())\n"
  },
  {
    "path": "lib/datasets/factory.py~",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"Factory method for easily getting imdbs by name.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\nfrom datasets.graspRGB import graspRGB\n\n__sets = {}\nfrom datasets.pascal_voc import pascal_voc\nfrom datasets.coco import coco\n\nimport numpy as np\n\n# Set up voc_<year>_<split> \nfor year in ['2007', '2012']:\n  for split in ['train', 'val', 'trainval', 'test']:\n    name = 'voc_{}_{}'.format(year, split)\n    __sets[name] = (lambda split=split, year=year: pascal_voc(split, year))\n\n# Set up coco_2014_<split>\nfor year in ['2014']:\n  for split in ['train', 'val', 'minival', 'valminusminival', 'trainval']:\n    name = 'coco_{}_{}'.format(year, split)\n    __sets[name] = (lambda split=split, year=year: coco(split, year))\n\n# Set up coco_2015_<split>\nfor year in ['2015']:\n  for split in ['test', 'test-dev']:\n    name = 'coco_{}_{}'.format(year, split)\n    __sets[name] = (lambda split=split, year=year: coco(split, year))\n\n# Set up graspRGB_<split> using selective search \"fast\" mode # added by FC\ngraspRGB_devkit_path = '/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_object_tf'\nfor split in ['train', 'test']:\n    name = '{}_{}'.format('graspRGB', split)\n    __sets[name] = (lambda split=split: graspRGB(split, graspRGB_devkit_path))\n\ndef get_imdb(name):\n  \"\"\"Get an imdb (image database) by name.\"\"\"\n  if name not in __sets:\n    raise KeyError('Unknown dataset: {}'.format(name))\n  return __sets[name]()\n\n\ndef list_imdbs():\n  \"\"\"List all registered imdbs.\"\"\"\n  return list(__sets.keys())\n"
  },
  {
    "path": "lib/datasets/graspRGB.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\nimport datasets\nimport datasets.graspRGB\nimport os\nfrom datasets.imdb import imdb\nimport datasets.ds_utils as ds_utils\nimport xml.etree.ElementTree as ET\nimport numpy as np\nimport scipy.sparse\nimport scipy.io as sio\nimport utils.cython_bbox\nimport cPickle\nimport subprocess\nimport uuid\nfrom voc_eval import voc_eval\n\nclass graspRGB(imdb):\n    def __init__(self, image_set, devkit_path):\n        imdb.__init__(self, image_set)\n        self._image_set = image_set\n        self._devkit_path = devkit_path\n        self._data_path = os.path.join(self._devkit_path, 'data')\n        self._classes = ('__background__', # always index 0\n                         'angle_01', 'angle_02', 'angle_03', 'angle_04', 'angle_05',\n                         'angle_06', 'angle_07', 'angle_08', 'angle_09', 'angle_10',\n                         'angle_11', 'angle_12', 'angle_13', 'angle_14', 'angle_15',\n                         'angle_16', 'angle_17', 'angle_18', 'angle_19')\n        self._class_to_ind = dict(zip(self.classes, xrange(self.num_classes)))\n        self._image_ext = ['.jpg', '.png']\n        self._image_index = self._load_image_set_index()\n        # Default to roidb handler\n        self._roidb_handler = self.selective_search_roidb\n        self._salt = str(uuid.uuid4())\n        self._comp_id = 'comp4'\n\n        # Specific config options\n        self.config = {'cleanup'     : True,\n                       'use_salt'    : True,\n                       'use_diff'    : False,\n                       'matlab_eval' : False,\n                       'rpn_file'    : None,\n                       'min_size'    : 2}\n\n        assert os.path.exists(self._devkit_path), \\\n                'Devkit path does not exist: {}'.format(self._devkit_path)\n        assert os.path.exists(self._data_path), \\\n                'Path does not exist: {}'.format(self._data_path)\n\n    def image_path_at(self, i):\n        \"\"\"\n        Return the absolute path to image i in the image sequence.\n        \"\"\"\n        return self.image_path_from_index(self._image_index[i])\n\n    def image_path_from_index(self, index):\n        \"\"\"\n        Construct an image path from the image's \"index\" identifier.\n        \"\"\"\n        for ext in self._image_ext:\n            image_path = os.path.join(self._data_path, 'Images',\n                                  index + ext)\n            if os.path.exists(image_path):\n                break\n        assert os.path.exists(image_path), \\\n                'Path does not exist: {}'.format(image_path)\n\treturn image_path\n\n    def _load_image_set_index(self):\n        \"\"\"\n        Load the indexes listed in this dataset's image set file.\n        \"\"\"\n        # Example path to image set file:\n        # self._data_path + /ImageSets/val.txt\n        image_set_file = os.path.join(self._data_path, 'ImageSets', \n                                      self._image_set + '.txt')\n        assert os.path.exists(image_set_file), \\\n                'Path does not exist: {}'.format(image_set_file)\n        with open(image_set_file) as f:\n            image_index = [x.strip() for x in f.readlines()] \n        # if you output image_index, it's ['I00001', 'I00002', ..] all the file names, not just numbers\n        return image_index\n\n    def gt_roidb(self):\n        \"\"\"\n        Return the database of ground-truth regions of interest.\n\n        This function loads/saves from/to a cache file to speed up future calls.\n        \"\"\"\n        cache_file = os.path.join(self.cache_path, self.name + '_gt_roidb.pkl')\n        if os.path.exists(cache_file):\n            with open(cache_file, 'rb') as fid:\n                roidb = cPickle.load(fid)\n            print '{} gt roidb loaded from {}'.format(self.name, cache_file)\n            return roidb\n\n        gt_roidb = [self._load_graspRGB_annotation(index)\n                    for index in self.image_index]\n        with open(cache_file, 'wb') as fid:\n            cPickle.dump(gt_roidb, fid, cPickle.HIGHEST_PROTOCOL)\n        print 'wrote gt roidb to {}'.format(cache_file)\n\n        return gt_roidb\n\n    def selective_search_roidb(self):\n        \"\"\"\n        Return the database of selective search regions of interest.\n        Ground-truth ROIs are also included.\n\n        This function loads/saves from/to a cache file to speed up future calls.\n        \"\"\"\n        cache_file = os.path.join(self.cache_path,\n                                  self.name + '_selective_search_roidb.pkl')\n\n        if os.path.exists(cache_file):\n            with open(cache_file, 'rb') as fid:\n                roidb = cPickle.load(fid)\n            print '{} ss roidb loaded from {}'.format(self.name, cache_file)\n            return roidb\n\n        if self._image_set != 'test':\n            gt_roidb = self.gt_roidb()\n            ss_roidb = self._load_selective_search_roidb(gt_roidb)\n            roidb = imdb.merge_roidbs(gt_roidb, ss_roidb)\n        else:\n            roidb = self._load_selective_search_roidb(None)\n            print len(roidb)\n\twith open(cache_file, 'wb') as fid:\n            cPickle.dump(roidb, fid, cPickle.HIGHEST_PROTOCOL)\n        print 'wrote ss roidb to {}'.format(cache_file)\n\n        return roidb\n\n    def _load_selective_search_roidb(self, gt_roidb):\n        filename = os.path.abspath(os.path.join(self._devkit_path,\n                                                self.name + '.mat'))\n        assert os.path.exists(filename), \\\n               'Selective search data not found at: {}'.format(filename)\n        print filename\n\traw_data = sio.loadmat(filename)['all_boxes'].ravel()\n\n        box_list = []\n        for i in xrange(raw_data.shape[0]):\n            box_list.append(raw_data[i][:, (1, 0, 3, 2)] - 1)\n\n\treturn self.create_roidb_from_box_list(box_list, gt_roidb)\n\n    def selective_search_IJCV_roidb(self):\n        \"\"\"\n        eturn the database of selective search regions of interest.\n        Ground-truth ROIs are also included.\n\n        This function loads/saves from/to a cache file to speed up future calls.\n        \"\"\"\n        cache_file = os.path.join(self.cache_path,\n                '{:s}_selective_search_IJCV_top_{:d}_roidb.pkl'.\n                format(self.name, self.config['top_k']))\n\n        if os.path.exists(cache_file):\n            with open(cache_file, 'rb') as fid:\n                roidb = cPickle.load(fid)\n            print '{} ss roidb loaded from {}'.format(self.name, cache_file)\n            return roidb\n\n        gt_roidb = self.gt_roidb()\n        ss_roidb = self._load_selective_search_IJCV_roidb(gt_roidb)\n        roidb = datasets.imdb.merge_roidbs(gt_roidb, ss_roidb)\n        with open(cache_file, 'wb') as fid:\n            cPickle.dump(roidb, fid, cPickle.HIGHEST_PROTOCOL)\n        print 'wrote ss roidb to {}'.format(cache_file)\n\n        return roidb\n\n    def _load_selective_search_IJCV_roidb(self, gt_roidb):\n        IJCV_path = os.path.abspath(os.path.join(self.cache_path, '..',\n                                                 'selective_search_IJCV_data',\n                                                 self.name))\n        assert os.path.exists(IJCV_path), \\\n               'Selective search IJCV data not found at: {}'.format(IJCV_path)\n\n        top_k = self.config['top_k']\n        box_list = []\n        for i in xrange(self.num_images):\n            filename = os.path.join(IJCV_path, self.image_index[i] + '.mat')\n            raw_data = sio.loadmat(filename)\n            box_list.append((raw_data['boxes'][:top_k, :]-1).astype(np.uint16))\n\n        return self.create_roidb_from_box_list(box_list, gt_roidb)\n\n    def _load_graspRGB_annotation(self, index):\n        \"\"\"\n        Load image and bounding boxes info from txt files of graspRGB.\n        \"\"\"\n        filename = os.path.join(self._data_path, 'Annotations', index + '.txt')\n        print 'Loading: {}'.format(filename)\n\twith open(filename) as f:\n            data = f.readlines()\n \n        num_objs = len(data)\n        print len(data)\n        if len(data) == 0:\n           print 'yooooooooo'\n           import sys\n           sys.exit()\n        boxes = np.zeros((num_objs, 4), dtype=np.uint16)\n        gt_classes = np.zeros((num_objs), dtype=np.int32)\n        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)\n        # \"Seg\" area for pascal is just the box area\n        seg_areas = np.zeros((num_objs), dtype=np.float32)\n\n        # Load object bounding boxes into a data frame.\n        for ix, aline in enumerate(data):\n            # Make pixel indexes 0-based\n\t    tokens = aline.strip().split()   \n\t    if len(tokens) != 5:\n\t\tcontinue\n\t    cls = int(tokens[0])     # this file uses 0 as the background\n\t    x1 = float(tokens[1]) \n\t    y1 = float(tokens[2]) \n\t    x2 = float(tokens[3]) \n\t    y2 = float(tokens[4]) \n\n            # if not doing this, there is negative value when bbs around boundary of image, and when it got read back, it becomes 655xx \n            if (x1<0 and x2<0) or (y1<0 and y2<0):\n               print 'yooooooooo'\n               import sys\n               sys.exit()\n            if x1 < 0:\n                x1 = 0\n            if x2 < 0:\n                x2 = 0\n            if y1 < 0:\n                y1 = 0\n            if y2 < 0:\n                y2 = 0\n\n\t    gt_classes[ix] = cls\t \n\t    boxes[ix, :] = [x1, y1, x2, y2]\n\t    overlaps[ix, cls] = 1.0\n            seg_areas[ix] = (x2 - x1 + 1) * (y2 - y1 + 1)\n\n        overlaps = scipy.sparse.csr_matrix(overlaps)\n\n        return {'boxes' : boxes,\n                'gt_classes': gt_classes,\n                'gt_overlaps' : overlaps,\n                'flipped' : False,\n                'seg_areas' : seg_areas}\n\n    def _write_graspRGB_results_file(self, all_boxes):\n        use_salt = self.config['use_salt']\n        comp_id = 'comp4'\n        if use_salt:\n            comp_id += '-{}'.format(os.getpid())\n\n        # VOCdevkit/results/comp4-44503_det_test_aeroplane.txt\n        path = os.path.join(self._devkit_path, 'results', self.name, comp_id + '_')\n        for cls_ind, cls in enumerate(self.classes):\n            if cls == '__background__':\n                continue\n            print 'Writing {} results file'.format(cls)\n            filename = path + 'det_' + self._image_set + '_' + cls + '.txt'\n            with open(filename, 'wt') as f:\n                for im_ind, index in enumerate(self.image_index):\n                    dets = all_boxes[cls_ind][im_ind]\n                    if dets == []:\n                        continue\n                    # the VOCdevkit expects 1-based indices\n                    for k in xrange(dets.shape[0]):\n                        f.write('{:s} {:.3f} {:.1f} {:.1f} {:.1f} {:.1f}\\n'.\n                                format(index, dets[k, -1],\n                                       dets[k, 0] + 1, dets[k, 1] + 1,\n                                       dets[k, 2] + 1, dets[k, 3] + 1))\n        return comp_id\n\n    def _do_matlab_eval(self, comp_id, output_dir='output'):\n        rm_results = self.config['cleanup']\n\n        path = os.path.join(os.path.dirname(__file__),\n                            'VOCdevkit-matlab-wrapper')\n        cmd = 'cd {} && '.format(path)\n        cmd += '{:s} -nodisplay -nodesktop '.format(datasets.MATLAB)\n        cmd += '-r \"dbstop if error; '\n        cmd += 'setenv(\\'LC_ALL\\',\\'C\\'); voc_eval(\\'{:s}\\',\\'{:s}\\',\\'{:s}\\',\\'{:s}\\',{:d}); quit;\"' \\\n               .format(self._devkit_path, comp_id,\n                       self._image_set, output_dir, int(rm_results))\n        print('Running:\\n{}'.format(cmd))\n        status = subprocess.call(cmd, shell=True)\n\n    def evaluate_detections(self, all_boxes, output_dir):\n        comp_id = self._write_graspRGB_results_file(all_boxes)\n        self._do_matlab_eval(comp_id, output_dir)\n\n    def competition_mode(self, on):\n        if on:\n            self.config['use_salt'] = False\n            self.config['cleanup'] = False\n        else:\n            self.config['use_salt'] = True\n            self.config['cleanup'] = True\n\nif __name__ == '__main__':\n    d = datasets.graspRGB('train', '')\n    res = d.roidb\n    from IPython import embed; embed()\n"
  },
  {
    "path": "lib/datasets/graspRGB.py~",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\nimport datasets\nimport datasets.graspRGB\nimport os\nfrom datasets.imdb import imdb\nimport datasets.ds_utils as ds_utils\nimport xml.etree.ElementTree as ET\nimport numpy as np\nimport scipy.sparse\nimport scipy.io as sio\nimport utils.cython_bbox\nimport cPickle\nimport subprocess\nimport uuid\nfrom voc_eval import voc_eval\n\nclass graspRGB(imdb):\n    def __init__(self, image_set, devkit_path):\n        imdb.__init__(self, image_set)\n        self._image_set = image_set\n        self._devkit_path = devkit_path\n        self._data_path = os.path.join(self._devkit_path, 'data')\n        self._classes = ('__background__', # always index 0\n                         'angle_01', 'angle_02', 'angle_03', 'angle_04', 'angle_05',\n                         'angle_06', 'angle_07', 'angle_08', 'angle_09', 'angle_10',\n                         'angle_11', 'angle_12', 'angle_13', 'angle_14', 'angle_15',\n                         'angle_16', 'angle_17', 'angle_18', 'angle_19')\n        self._class_to_ind = dict(zip(self.classes, xrange(self.num_classes)))\n        self._image_ext = ['.jpg', '.png']\n        self._image_index = self._load_image_set_index()\n        # Default to roidb handler\n        self._roidb_handler = self.selective_search_roidb\n        self._salt = str(uuid.uuid4())\n        self._comp_id = 'comp4'\n\n        # Specific config options\n        self.config = {'cleanup'     : True,\n                       'use_salt'    : True,\n                       'use_diff'    : False,\n                       'matlab_eval' : False,\n                       'rpn_file'    : None,\n                       'min_size'    : 2}\n\n        assert os.path.exists(self._devkit_path), \\\n                'Devkit path does not exist: {}'.format(self._devkit_path)\n        assert os.path.exists(self._data_path), \\\n                'Path does not exist: {}'.format(self._data_path)\n\n    def image_path_at(self, i):\n        \"\"\"\n        Return the absolute path to image i in the image sequence.\n        \"\"\"\n        return self.image_path_from_index(self._image_index[i])\n\n    def image_path_from_index(self, index):\n        \"\"\"\n        Construct an image path from the image's \"index\" identifier.\n        \"\"\"\n        for ext in self._image_ext:\n            image_path = os.path.join(self._data_path, 'Images',\n                                  index + ext)\n            if os.path.exists(image_path):\n                break\n        assert os.path.exists(image_path), \\\n                'Path does not exist: {}'.format(image_path)\n\treturn image_path\n\n    def _load_image_set_index(self):\n        \"\"\"\n        Load the indexes listed in this dataset's image set file.\n        \"\"\"\n        # Example path to image set file:\n        # self._data_path + /ImageSets/val.txt\n        image_set_file = os.path.join(self._data_path, 'ImageSets', \n                                      self._image_set + '.txt')\n        assert os.path.exists(image_set_file), \\\n                'Path does not exist: {}'.format(image_set_file)\n        with open(image_set_file) as f:\n            image_index = [x.strip() for x in f.readlines()] \n        # if you output image_index, it's ['I00001', 'I00002', ..] all the file names, not just numbers\n        return image_index\n\n    def gt_roidb(self):\n        \"\"\"\n        Return the database of ground-truth regions of interest.\n\n        This function loads/saves from/to a cache file to speed up future calls.\n        \"\"\"\n        cache_file = os.path.join(self.cache_path, self.name + '_gt_roidb.pkl')\n        if os.path.exists(cache_file):\n            with open(cache_file, 'rb') as fid:\n                roidb = cPickle.load(fid)\n            print '{} gt roidb loaded from {}'.format(self.name, cache_file)\n            return roidb\n\n        gt_roidb = [self._load_graspRGB_annotation(index)\n                    for index in self.image_index]\n        with open(cache_file, 'wb') as fid:\n            cPickle.dump(gt_roidb, fid, cPickle.HIGHEST_PROTOCOL)\n        print 'wrote gt roidb to {}'.format(cache_file)\n\n        return gt_roidb\n\n    def selective_search_roidb(self):\n        \"\"\"\n        Return the database of selective search regions of interest.\n        Ground-truth ROIs are also included.\n\n        This function loads/saves from/to a cache file to speed up future calls.\n        \"\"\"\n        cache_file = os.path.join(self.cache_path,\n                                  self.name + '_selective_search_roidb.pkl')\n\n        if os.path.exists(cache_file):\n            with open(cache_file, 'rb') as fid:\n                roidb = cPickle.load(fid)\n            print '{} ss roidb loaded from {}'.format(self.name, cache_file)\n            return roidb\n\n        if self._image_set != 'test':\n            gt_roidb = self.gt_roidb()\n            ss_roidb = self._load_selective_search_roidb(gt_roidb)\n            roidb = imdb.merge_roidbs(gt_roidb, ss_roidb)\n        else:\n            roidb = self._load_selective_search_roidb(None)\n            print len(roidb)\n\twith open(cache_file, 'wb') as fid:\n            cPickle.dump(roidb, fid, cPickle.HIGHEST_PROTOCOL)\n        print 'wrote ss roidb to {}'.format(cache_file)\n\n        return roidb\n\n    def _load_selective_search_roidb(self, gt_roidb):\n        filename = os.path.abspath(os.path.join(self._devkit_path,\n                                                self.name + '.mat'))\n        assert os.path.exists(filename), \\\n               'Selective search data not found at: {}'.format(filename)\n        print filename\n\traw_data = sio.loadmat(filename)['all_boxes'].ravel()\n\n        box_list = []\n        for i in xrange(raw_data.shape[0]):\n            box_list.append(raw_data[i][:, (1, 0, 3, 2)] - 1)\n\n\treturn self.create_roidb_from_box_list(box_list, gt_roidb)\n\n    def selective_search_IJCV_roidb(self):\n        \"\"\"\n        eturn the database of selective search regions of interest.\n        Ground-truth ROIs are also included.\n\n        This function loads/saves from/to a cache file to speed up future calls.\n        \"\"\"\n        cache_file = os.path.join(self.cache_path,\n                '{:s}_selective_search_IJCV_top_{:d}_roidb.pkl'.\n                format(self.name, self.config['top_k']))\n\n        if os.path.exists(cache_file):\n            with open(cache_file, 'rb') as fid:\n                roidb = cPickle.load(fid)\n            print '{} ss roidb loaded from {}'.format(self.name, cache_file)\n            return roidb\n\n        gt_roidb = self.gt_roidb()\n        ss_roidb = self._load_selective_search_IJCV_roidb(gt_roidb)\n        roidb = datasets.imdb.merge_roidbs(gt_roidb, ss_roidb)\n        with open(cache_file, 'wb') as fid:\n            cPickle.dump(roidb, fid, cPickle.HIGHEST_PROTOCOL)\n        print 'wrote ss roidb to {}'.format(cache_file)\n\n        return roidb\n\n    def _load_selective_search_IJCV_roidb(self, gt_roidb):\n        IJCV_path = os.path.abspath(os.path.join(self.cache_path, '..',\n                                                 'selective_search_IJCV_data',\n                                                 self.name))\n        assert os.path.exists(IJCV_path), \\\n               'Selective search IJCV data not found at: {}'.format(IJCV_path)\n\n        top_k = self.config['top_k']\n        box_list = []\n        for i in xrange(self.num_images):\n            filename = os.path.join(IJCV_path, self.image_index[i] + '.mat')\n            raw_data = sio.loadmat(filename)\n            box_list.append((raw_data['boxes'][:top_k, :]-1).astype(np.uint16))\n\n        return self.create_roidb_from_box_list(box_list, gt_roidb)\n\n    def _load_graspRGB_annotation(self, index):\n        \"\"\"\n        Load image and bounding boxes info from txt files of graspRGB.\n        \"\"\"\n        filename = os.path.join(self._data_path, 'Annotations', index + '.txt')\n        print 'Loading: {}'.format(filename)\n\twith open(filename) as f:\n            data = f.readlines()\n \n        num_objs = len(data)\n        print len(data)\n        if len(data) == 0:\n           print 'yooooooooo'\n           import sys\n           sys.exit()\n        boxes = np.zeros((num_objs, 4), dtype=np.uint16)\n        gt_classes = np.zeros((num_objs), dtype=np.int32)\n        overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)\n        # \"Seg\" area for pascal is just the box area\n        seg_areas = np.zeros((num_objs), dtype=np.float32)\n\n        # Load object bounding boxes into a data frame.\n        for ix, aline in enumerate(data):\n            # Make pixel indexes 0-based\n\t    tokens = aline.strip().split()   \n\t    if len(tokens) != 5:\n\t\tcontinue\n\t    cls = float(tokens[0])     # this file uses 0 as the background\n\t    x1 = float(tokens[1]) \n\t    y1 = float(tokens[2]) \n\t    x2 = float(tokens[3]) \n\t    y2 = float(tokens[4]) \n\n            # if not doing this, there is negative value when bbs around boundary of image, and when it got read back, it becomes 655xx \n            if (x1<0 and x2<0) or (y1<0 and y2<0):\n               print 'yooooooooo'\n               import sys\n               sys.exit()\n            if x1 < 0:\n                x1 = 0\n            if x2 < 0:\n                x2 = 0\n            if y1 < 0:\n                y1 = 0\n            if y2 < 0:\n                y2 = 0\n\n\t    gt_classes[ix] = cls\t \n\t    boxes[ix, :] = [x1, y1, x2, y2]\n\t    overlaps[ix, cls] = 1.0\n            seg_areas[ix] = (x2 - x1 + 1) * (y2 - y1 + 1)\n\n        overlaps = scipy.sparse.csr_matrix(overlaps)\n\n        return {'boxes' : boxes,\n                'gt_classes': gt_classes,\n                'gt_overlaps' : overlaps,\n                'flipped' : False,\n                'seg_areas' : seg_areas}\n\n    def _write_graspRGB_results_file(self, all_boxes):\n        use_salt = self.config['use_salt']\n        comp_id = 'comp4'\n        if use_salt:\n            comp_id += '-{}'.format(os.getpid())\n\n        # VOCdevkit/results/comp4-44503_det_test_aeroplane.txt\n        path = os.path.join(self._devkit_path, 'results', self.name, comp_id + '_')\n        for cls_ind, cls in enumerate(self.classes):\n            if cls == '__background__':\n                continue\n            print 'Writing {} results file'.format(cls)\n            filename = path + 'det_' + self._image_set + '_' + cls + '.txt'\n            with open(filename, 'wt') as f:\n                for im_ind, index in enumerate(self.image_index):\n                    dets = all_boxes[cls_ind][im_ind]\n                    if dets == []:\n                        continue\n                    # the VOCdevkit expects 1-based indices\n                    for k in xrange(dets.shape[0]):\n                        f.write('{:s} {:.3f} {:.1f} {:.1f} {:.1f} {:.1f}\\n'.\n                                format(index, dets[k, -1],\n                                       dets[k, 0] + 1, dets[k, 1] + 1,\n                                       dets[k, 2] + 1, dets[k, 3] + 1))\n        return comp_id\n\n    def _do_matlab_eval(self, comp_id, output_dir='output'):\n        rm_results = self.config['cleanup']\n\n        path = os.path.join(os.path.dirname(__file__),\n                            'VOCdevkit-matlab-wrapper')\n        cmd = 'cd {} && '.format(path)\n        cmd += '{:s} -nodisplay -nodesktop '.format(datasets.MATLAB)\n        cmd += '-r \"dbstop if error; '\n        cmd += 'setenv(\\'LC_ALL\\',\\'C\\'); voc_eval(\\'{:s}\\',\\'{:s}\\',\\'{:s}\\',\\'{:s}\\',{:d}); quit;\"' \\\n               .format(self._devkit_path, comp_id,\n                       self._image_set, output_dir, int(rm_results))\n        print('Running:\\n{}'.format(cmd))\n        status = subprocess.call(cmd, shell=True)\n\n    def evaluate_detections(self, all_boxes, output_dir):\n        comp_id = self._write_graspRGB_results_file(all_boxes)\n        self._do_matlab_eval(comp_id, output_dir)\n\n    def competition_mode(self, on):\n        if on:\n            self.config['use_salt'] = False\n            self.config['cleanup'] = False\n        else:\n            self.config['use_salt'] = True\n            self.config['cleanup'] = True\n\nif __name__ == '__main__':\n    d = datasets.graspRGB('train', '')\n    res = d.roidb\n    from IPython import embed; embed()\n"
  },
  {
    "path": "lib/datasets/imdb.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick and Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport os\nimport os.path as osp\nimport PIL\nfrom utils.cython_bbox import bbox_overlaps\nimport numpy as np\nimport scipy.sparse\nfrom model.config import cfg\n\n\nclass imdb(object):\n  \"\"\"Image database.\"\"\"\n\n  def __init__(self, name, classes=None):\n    self._name = name\n    self._num_classes = 0\n    if not classes:\n      self._classes = []\n    else:\n      self._classes = classes\n    self._image_index = []\n    self._obj_proposer = 'gt'\n    self._roidb = None\n    self._roidb_handler = self.default_roidb\n    # Use this dict for storing dataset specific config options\n    self.config = {}\n\n  @property\n  def name(self):\n    return self._name\n\n  @property\n  def num_classes(self):\n    return len(self._classes)\n\n  @property\n  def classes(self):\n    return self._classes\n\n  @property\n  def image_index(self):\n    return self._image_index\n\n  @property\n  def roidb_handler(self):\n    return self._roidb_handler\n\n  @roidb_handler.setter\n  def roidb_handler(self, val):\n    self._roidb_handler = val\n\n  def set_proposal_method(self, method):\n    method = eval('self.' + method + '_roidb')\n    self.roidb_handler = method\n\n  @property\n  def roidb(self):\n    # A roidb is a list of dictionaries, each with the following keys:\n    #   boxes\n    #   gt_overlaps\n    #   gt_classes\n    #   flipped\n    if self._roidb is not None:\n      return self._roidb\n    self._roidb = self.roidb_handler()\n    return self._roidb\n\n  @property\n  def cache_path(self):\n    cache_path = osp.abspath(osp.join(cfg.DATA_DIR, 'cache'))\n    if not os.path.exists(cache_path):\n      os.makedirs(cache_path)\n    return cache_path\n\n  @property\n  def num_images(self):\n    return len(self.image_index)\n\n  def image_path_at(self, i):\n    raise NotImplementedError\n\n  def default_roidb(self):\n    raise NotImplementedError\n\n  def evaluate_detections(self, all_boxes, output_dir=None):\n    \"\"\"\n    all_boxes is a list of length number-of-classes.\n    Each list element is a list of length number-of-images.\n    Each of those list elements is either an empty list []\n    or a numpy array of detection.\n\n    all_boxes[class][image] = [] or np.array of shape #dets x 5\n    \"\"\"\n    raise NotImplementedError\n\n  def _get_widths(self):\n    return [PIL.Image.open(self.image_path_at(i)).size[0]\n            for i in range(self.num_images)]\n\n  def append_flipped_images(self):\n    num_images = self.num_images\n    widths = self._get_widths()\n    for i in range(num_images):\n      boxes = self.roidb[i]['boxes'].copy()\n      oldx1 = boxes[:, 0].copy()\n      oldx2 = boxes[:, 2].copy()\n      boxes[:, 0] = widths[i] - oldx2 - 1\n      boxes[:, 2] = widths[i] - oldx1 - 1\n      assert (boxes[:, 2] >= boxes[:, 0]).all()\n      entry = {'boxes': boxes,\n               'gt_overlaps': self.roidb[i]['gt_overlaps'],\n               'gt_classes': self.roidb[i]['gt_classes'],\n               'flipped': True}\n      self.roidb.append(entry)\n    self._image_index = self._image_index * 2\n\n  def evaluate_recall(self, candidate_boxes=None, thresholds=None,\n                      area='all', limit=None):\n    \"\"\"Evaluate detection proposal recall metrics.\n\n    Returns:\n        results: dictionary of results with keys\n            'ar': average recall\n            'recalls': vector recalls at each IoU overlap threshold\n            'thresholds': vector of IoU overlap thresholds\n            'gt_overlaps': vector of all ground-truth overlaps\n    \"\"\"\n    # Record max overlap value for each gt box\n    # Return vector of overlap values\n    areas = {'all': 0, 'small': 1, 'medium': 2, 'large': 3,\n             '96-128': 4, '128-256': 5, '256-512': 6, '512-inf': 7}\n    area_ranges = [[0 ** 2, 1e5 ** 2],  # all\n                   [0 ** 2, 32 ** 2],  # small\n                   [32 ** 2, 96 ** 2],  # medium\n                   [96 ** 2, 1e5 ** 2],  # large\n                   [96 ** 2, 128 ** 2],  # 96-128\n                   [128 ** 2, 256 ** 2],  # 128-256\n                   [256 ** 2, 512 ** 2],  # 256-512\n                   [512 ** 2, 1e5 ** 2],  # 512-inf\n                   ]\n    assert area in areas, 'unknown area range: {}'.format(area)\n    area_range = area_ranges[areas[area]]\n    gt_overlaps = np.zeros(0)\n    num_pos = 0\n    for i in range(self.num_images):\n      # Checking for max_overlaps == 1 avoids including crowd annotations\n      # (...pretty hacking :/)\n      max_gt_overlaps = self.roidb[i]['gt_overlaps'].toarray().max(axis=1)\n      gt_inds = np.where((self.roidb[i]['gt_classes'] > 0) &\n                         (max_gt_overlaps == 1))[0]\n      gt_boxes = self.roidb[i]['boxes'][gt_inds, :]\n      gt_areas = self.roidb[i]['seg_areas'][gt_inds]\n      valid_gt_inds = np.where((gt_areas >= area_range[0]) &\n                               (gt_areas <= area_range[1]))[0]\n      gt_boxes = gt_boxes[valid_gt_inds, :]\n      num_pos += len(valid_gt_inds)\n\n      if candidate_boxes is None:\n        # If candidate_boxes is not supplied, the default is to use the\n        # non-ground-truth boxes from this roidb\n        non_gt_inds = np.where(self.roidb[i]['gt_classes'] == 0)[0]\n        boxes = self.roidb[i]['boxes'][non_gt_inds, :]\n      else:\n        boxes = candidate_boxes[i]\n      if boxes.shape[0] == 0:\n        continue\n      if limit is not None and boxes.shape[0] > limit:\n        boxes = boxes[:limit, :]\n\n      overlaps = bbox_overlaps(boxes.astype(np.float),\n                               gt_boxes.astype(np.float))\n\n      _gt_overlaps = np.zeros((gt_boxes.shape[0]))\n      for j in range(gt_boxes.shape[0]):\n        # find which proposal box maximally covers each gt box\n        argmax_overlaps = overlaps.argmax(axis=0)\n        # and get the iou amount of coverage for each gt box\n        max_overlaps = overlaps.max(axis=0)\n        # find which gt box is 'best' covered (i.e. 'best' = most iou)\n        gt_ind = max_overlaps.argmax()\n        gt_ovr = max_overlaps.max()\n        assert (gt_ovr >= 0)\n        # find the proposal box that covers the best covered gt box\n        box_ind = argmax_overlaps[gt_ind]\n        # record the iou coverage of this gt box\n        _gt_overlaps[j] = overlaps[box_ind, gt_ind]\n        assert (_gt_overlaps[j] == gt_ovr)\n        # mark the proposal box and the gt box as used\n        overlaps[box_ind, :] = -1\n        overlaps[:, gt_ind] = -1\n      # append recorded iou coverage level\n      gt_overlaps = np.hstack((gt_overlaps, _gt_overlaps))\n\n    gt_overlaps = np.sort(gt_overlaps)\n    if thresholds is None:\n      step = 0.05\n      thresholds = np.arange(0.5, 0.95 + 1e-5, step)\n    recalls = np.zeros_like(thresholds)\n    # compute recall for each iou threshold\n    for i, t in enumerate(thresholds):\n      recalls[i] = (gt_overlaps >= t).sum() / float(num_pos)\n    # ar = 2 * np.trapz(recalls, thresholds)\n    ar = recalls.mean()\n    return {'ar': ar, 'recalls': recalls, 'thresholds': thresholds,\n            'gt_overlaps': gt_overlaps}\n\n  def create_roidb_from_box_list(self, box_list, gt_roidb):\n    assert len(box_list) == self.num_images, \\\n      'Number of boxes must match number of ground-truth images'\n    roidb = []\n    for i in range(self.num_images):\n      boxes = box_list[i]\n      num_boxes = boxes.shape[0]\n      overlaps = np.zeros((num_boxes, self.num_classes), dtype=np.float32)\n\n      if gt_roidb is not None and gt_roidb[i]['boxes'].size > 0:\n        gt_boxes = gt_roidb[i]['boxes']\n        gt_classes = gt_roidb[i]['gt_classes']\n        gt_overlaps = bbox_overlaps(boxes.astype(np.float),\n                                    gt_boxes.astype(np.float))\n        argmaxes = gt_overlaps.argmax(axis=1)\n        maxes = gt_overlaps.max(axis=1)\n        I = np.where(maxes > 0)[0]\n        overlaps[I, gt_classes[argmaxes[I]]] = maxes[I]\n\n      overlaps = scipy.sparse.csr_matrix(overlaps)\n      roidb.append({\n        'boxes': boxes,\n        'gt_classes': np.zeros((num_boxes,), dtype=np.int32),\n        'gt_overlaps': overlaps,\n        'flipped': False,\n        'seg_areas': np.zeros((num_boxes,), dtype=np.float32),\n      })\n    return roidb\n\n  @staticmethod\n  def merge_roidbs(a, b):\n    assert len(a) == len(b)\n    for i in range(len(a)):\n      a[i]['boxes'] = np.vstack((a[i]['boxes'], b[i]['boxes']))\n      a[i]['gt_classes'] = np.hstack((a[i]['gt_classes'],\n                                      b[i]['gt_classes']))\n      a[i]['gt_overlaps'] = scipy.sparse.vstack([a[i]['gt_overlaps'],\n                                                 b[i]['gt_overlaps']])\n      a[i]['seg_areas'] = np.hstack((a[i]['seg_areas'],\n                                     b[i]['seg_areas']))\n    return a\n\n  def competition_mode(self, on):\n    \"\"\"Turn competition mode on or off.\"\"\"\n    pass\n"
  },
  {
    "path": "lib/datasets/pascal_voc.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick and Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport os\nfrom datasets.imdb import imdb\nimport datasets.ds_utils as ds_utils\nimport xml.etree.ElementTree as ET\nimport numpy as np\nimport scipy.sparse\nimport scipy.io as sio\nimport utils.cython_bbox\nimport pickle\nimport subprocess\nimport uuid\nfrom .voc_eval import voc_eval\nfrom model.config import cfg\n\n\nclass pascal_voc(imdb):\n  def __init__(self, image_set, year, devkit_path=None):\n    imdb.__init__(self, 'voc_' + year + '_' + image_set)\n    self._year = year\n    self._image_set = image_set\n    self._devkit_path = self._get_default_path() if devkit_path is None \\\n      else devkit_path\n    self._data_path = os.path.join(self._devkit_path, 'VOC' + self._year)\n    self._classes = ('__background__',  # always index 0\n                     'aeroplane', 'bicycle', 'bird', 'boat',\n                     'bottle', 'bus', 'car', 'cat', 'chair',\n                     'cow', 'diningtable', 'dog', 'horse',\n                     'motorbike', 'person', 'pottedplant',\n                     'sheep', 'sofa', 'train', 'tvmonitor')\n    self._class_to_ind = dict(list(zip(self.classes, list(range(self.num_classes)))))\n    self._image_ext = '.jpg'\n    self._image_index = self._load_image_set_index()\n    # Default to roidb handler\n    self._roidb_handler = self.gt_roidb\n    self._salt = str(uuid.uuid4())\n    self._comp_id = 'comp4'\n\n    # PASCAL specific config options\n    self.config = {'cleanup': True,\n                   'use_salt': True,\n                   'use_diff': False,\n                   'matlab_eval': False,\n                   'rpn_file': None}\n\n    assert os.path.exists(self._devkit_path), \\\n      'VOCdevkit path does not exist: {}'.format(self._devkit_path)\n    assert os.path.exists(self._data_path), \\\n      'Path does not exist: {}'.format(self._data_path)\n\n  def image_path_at(self, i):\n    \"\"\"\n    Return the absolute path to image i in the image sequence.\n    \"\"\"\n    return self.image_path_from_index(self._image_index[i])\n\n  def image_path_from_index(self, index):\n    \"\"\"\n    Construct an image path from the image's \"index\" identifier.\n    \"\"\"\n    image_path = os.path.join(self._data_path, 'JPEGImages',\n                              index + self._image_ext)\n    assert os.path.exists(image_path), \\\n      'Path does not exist: {}'.format(image_path)\n    return image_path\n\n  def _load_image_set_index(self):\n    \"\"\"\n    Load the indexes listed in this dataset's image set file.\n    \"\"\"\n    # Example path to image set file:\n    # self._devkit_path + /VOCdevkit2007/VOC2007/ImageSets/Main/val.txt\n    image_set_file = os.path.join(self._data_path, 'ImageSets', 'Main',\n                                  self._image_set + '.txt')\n    assert os.path.exists(image_set_file), \\\n      'Path does not exist: {}'.format(image_set_file)\n    with open(image_set_file) as f:\n      image_index = [x.strip() for x in f.readlines()]\n    return image_index\n\n  def _get_default_path(self):\n    \"\"\"\n    Return the default path where PASCAL VOC is expected to be installed.\n    \"\"\"\n    return os.path.join(cfg.DATA_DIR, 'VOCdevkit' + self._year)\n\n  def gt_roidb(self):\n    \"\"\"\n    Return the database of ground-truth regions of interest.\n\n    This function loads/saves from/to a cache file to speed up future calls.\n    \"\"\"\n    cache_file = os.path.join(self.cache_path, self.name + '_gt_roidb.pkl')\n    if os.path.exists(cache_file):\n      with open(cache_file, 'rb') as fid:\n        try:\n          roidb = pickle.load(fid)\n        except:\n          roidb = pickle.load(fid, encoding='bytes')\n      print('{} gt roidb loaded from {}'.format(self.name, cache_file))\n      return roidb\n\n    gt_roidb = [self._load_pascal_annotation(index)\n                for index in self.image_index]\n    with open(cache_file, 'wb') as fid:\n      pickle.dump(gt_roidb, fid, pickle.HIGHEST_PROTOCOL)\n    print('wrote gt roidb to {}'.format(cache_file))\n\n    return gt_roidb\n\n  def rpn_roidb(self):\n    if int(self._year) == 2007 or self._image_set != 'test':\n      gt_roidb = self.gt_roidb()\n      rpn_roidb = self._load_rpn_roidb(gt_roidb)\n      roidb = imdb.merge_roidbs(gt_roidb, rpn_roidb)\n    else:\n      roidb = self._load_rpn_roidb(None)\n\n    return roidb\n\n  def _load_rpn_roidb(self, gt_roidb):\n    filename = self.config['rpn_file']\n    print('loading {}'.format(filename))\n    assert os.path.exists(filename), \\\n      'rpn data not found at: {}'.format(filename)\n    with open(filename, 'rb') as f:\n      box_list = pickle.load(f)\n    return self.create_roidb_from_box_list(box_list, gt_roidb)\n\n  def _load_pascal_annotation(self, index):\n    \"\"\"\n    Load image and bounding boxes info from XML file in the PASCAL VOC\n    format.\n    \"\"\"\n    filename = os.path.join(self._data_path, 'Annotations', index + '.xml')\n    tree = ET.parse(filename)\n    objs = tree.findall('object')\n    if not self.config['use_diff']:\n      # Exclude the samples labeled as difficult\n      non_diff_objs = [\n        obj for obj in objs if int(obj.find('difficult').text) == 0]\n      # if len(non_diff_objs) != len(objs):\n      #     print 'Removed {} difficult objects'.format(\n      #         len(objs) - len(non_diff_objs))\n      objs = non_diff_objs\n    num_objs = len(objs)\n\n    boxes = np.zeros((num_objs, 4), dtype=np.uint16)\n    gt_classes = np.zeros((num_objs), dtype=np.int32)\n    overlaps = np.zeros((num_objs, self.num_classes), dtype=np.float32)\n    # \"Seg\" area for pascal is just the box area\n    seg_areas = np.zeros((num_objs), dtype=np.float32)\n\n    # Load object bounding boxes into a data frame.\n    for ix, obj in enumerate(objs):\n      bbox = obj.find('bndbox')\n      # Make pixel indexes 0-based\n      x1 = float(bbox.find('xmin').text) - 1\n      y1 = float(bbox.find('ymin').text) - 1\n      x2 = float(bbox.find('xmax').text) - 1\n      y2 = float(bbox.find('ymax').text) - 1\n      cls = self._class_to_ind[obj.find('name').text.lower().strip()]\n      boxes[ix, :] = [x1, y1, x2, y2]\n      gt_classes[ix] = cls\n      overlaps[ix, cls] = 1.0\n      seg_areas[ix] = (x2 - x1 + 1) * (y2 - y1 + 1)\n\n    overlaps = scipy.sparse.csr_matrix(overlaps)\n\n    return {'boxes': boxes,\n            'gt_classes': gt_classes,\n            'gt_overlaps': overlaps,\n            'flipped': False,\n            'seg_areas': seg_areas}\n\n  def _get_comp_id(self):\n    comp_id = (self._comp_id + '_' + self._salt if self.config['use_salt']\n               else self._comp_id)\n    return comp_id\n\n  def _get_voc_results_file_template(self):\n    # VOCdevkit/results/VOC2007/Main/<comp_id>_det_test_aeroplane.txt\n    filename = self._get_comp_id() + '_det_' + self._image_set + '_{:s}.txt'\n    path = os.path.join(\n      self._devkit_path,\n      'results',\n      'VOC' + self._year,\n      'Main',\n      filename)\n    return path\n\n  def _write_voc_results_file(self, all_boxes):\n    for cls_ind, cls in enumerate(self.classes):\n      if cls == '__background__':\n        continue\n      print('Writing {} VOC results file'.format(cls))\n      filename = self._get_voc_results_file_template().format(cls)\n      with open(filename, 'wt') as f:\n        for im_ind, index in enumerate(self.image_index):\n          dets = all_boxes[cls_ind][im_ind]\n          if dets == []:\n            continue\n          # the VOCdevkit expects 1-based indices\n          for k in range(dets.shape[0]):\n            f.write('{:s} {:.3f} {:.1f} {:.1f} {:.1f} {:.1f}\\n'.\n                    format(index, dets[k, -1],\n                           dets[k, 0] + 1, dets[k, 1] + 1,\n                           dets[k, 2] + 1, dets[k, 3] + 1))\n\n  def _do_python_eval(self, output_dir='output'):\n    annopath = os.path.join(\n      self._devkit_path,\n      'VOC' + self._year,\n      'Annotations',\n      '{:s}.xml')\n    imagesetfile = os.path.join(\n      self._devkit_path,\n      'VOC' + self._year,\n      'ImageSets',\n      'Main',\n      self._image_set + '.txt')\n    cachedir = os.path.join(self._devkit_path, 'annotations_cache')\n    aps = []\n    # The PASCAL VOC metric changed in 2010\n    use_07_metric = True if int(self._year) < 2010 else False\n    print('VOC07 metric? ' + ('Yes' if use_07_metric else 'No'))\n    if not os.path.isdir(output_dir):\n      os.mkdir(output_dir)\n    for i, cls in enumerate(self._classes):\n      if cls == '__background__':\n        continue\n      filename = self._get_voc_results_file_template().format(cls)\n      rec, prec, ap = voc_eval(\n        filename, annopath, imagesetfile, cls, cachedir, ovthresh=0.5,\n        use_07_metric=use_07_metric)\n      aps += [ap]\n      print(('AP for {} = {:.4f}'.format(cls, ap)))\n      with open(os.path.join(output_dir, cls + '_pr.pkl'), 'wb') as f:\n        pickle.dump({'rec': rec, 'prec': prec, 'ap': ap}, f)\n    print(('Mean AP = {:.4f}'.format(np.mean(aps))))\n    print('~~~~~~~~')\n    print('Results:')\n    for ap in aps:\n      print(('{:.3f}'.format(ap)))\n    print(('{:.3f}'.format(np.mean(aps))))\n    print('~~~~~~~~')\n    print('')\n    print('--------------------------------------------------------------')\n    print('Results computed with the **unofficial** Python eval code.')\n    print('Results should be very close to the official MATLAB eval code.')\n    print('Recompute with `./tools/reval.py --matlab ...` for your paper.')\n    print('-- Thanks, The Management')\n    print('--------------------------------------------------------------')\n\n  def _do_matlab_eval(self, output_dir='output'):\n    print('-----------------------------------------------------')\n    print('Computing results with the official MATLAB eval code.')\n    print('-----------------------------------------------------')\n    path = os.path.join(cfg.ROOT_DIR, 'lib', 'datasets',\n                        'VOCdevkit-matlab-wrapper')\n    cmd = 'cd {} && '.format(path)\n    cmd += '{:s} -nodisplay -nodesktop '.format(cfg.MATLAB)\n    cmd += '-r \"dbstop if error; '\n    cmd += 'voc_eval(\\'{:s}\\',\\'{:s}\\',\\'{:s}\\',\\'{:s}\\'); quit;\"' \\\n      .format(self._devkit_path, self._get_comp_id(),\n              self._image_set, output_dir)\n    print(('Running:\\n{}'.format(cmd)))\n    status = subprocess.call(cmd, shell=True)\n\n  def evaluate_detections(self, all_boxes, output_dir):\n    self._write_voc_results_file(all_boxes)\n    self._do_python_eval(output_dir)\n    if self.config['matlab_eval']:\n      self._do_matlab_eval(output_dir)\n    if self.config['cleanup']:\n      for cls in self._classes:\n        if cls == '__background__':\n          continue\n        filename = self._get_voc_results_file_template().format(cls)\n        os.remove(filename)\n\n  def competition_mode(self, on):\n    if on:\n      self.config['use_salt'] = False\n      self.config['cleanup'] = False\n    else:\n      self.config['use_salt'] = True\n      self.config['cleanup'] = True\n\n\nif __name__ == '__main__':\n  from datasets.pascal_voc import pascal_voc\n\n  d = pascal_voc('trainval', '2007')\n  res = d.roidb\n  from IPython import embed;\n\n  embed()\n"
  },
  {
    "path": "lib/datasets/tools/mcg_munge.py",
    "content": "import os\nimport sys\n\n\"\"\"Hacky tool to convert file system layout of MCG boxes downloaded from\nhttp://www.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/mcg/\nso that it's consistent with those computed by Jan Hosang (see:\nhttp://www.mpi-inf.mpg.de/departments/computer-vision-and-multimodal-\n  computing/research/object-recognition-and-scene-understanding/how-\n  good-are-detection-proposals-really/)\n\nNB: Boxes from the MCG website are in (y1, x1, y2, x2) order.\nBoxes from Hosang et al. are in (x1, y1, x2, y2) order.\n\"\"\"\n\ndef munge(src_dir):\n    # stored as: ./MCG-COCO-val2014-boxes/COCO_val2014_000000193401.mat\n    # want:      ./MCG/mat/COCO_val2014_0/COCO_val2014_000000141/COCO_val2014_000000141334.mat\n\n    files = os.listdir(src_dir)\n    for fn in files:\n        base, ext = os.path.splitext(fn)\n        # first 14 chars / first 22 chars / all chars + .mat\n        # COCO_val2014_0/COCO_val2014_000000447/COCO_val2014_000000447991.mat\n        first = base[:14]\n        second = base[:22]\n        dst_dir = os.path.join('MCG', 'mat', first, second)\n        if not os.path.exists(dst_dir):\n            os.makedirs(dst_dir)\n        src = os.path.join(src_dir, fn)\n        dst = os.path.join(dst_dir, fn)\n        print 'MV: {} -> {}'.format(src, dst)\n        os.rename(src, dst)\n\nif __name__ == '__main__':\n    # src_dir should look something like:\n    #  src_dir = 'MCG-COCO-val2014-boxes'\n    src_dir = sys.argv[1]\n    munge(src_dir)\n"
  },
  {
    "path": "lib/datasets/voc_eval.py",
    "content": "# --------------------------------------------------------\n# Fast/er R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Bharath Hariharan\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport xml.etree.ElementTree as ET\nimport os\nimport pickle\nimport numpy as np\n\ndef parse_rec(filename):\n  \"\"\" Parse a PASCAL VOC xml file \"\"\"\n  tree = ET.parse(filename)\n  objects = []\n  for obj in tree.findall('object'):\n    obj_struct = {}\n    obj_struct['name'] = obj.find('name').text\n    obj_struct['pose'] = obj.find('pose').text\n    obj_struct['truncated'] = int(obj.find('truncated').text)\n    obj_struct['difficult'] = int(obj.find('difficult').text)\n    bbox = obj.find('bndbox')\n    obj_struct['bbox'] = [int(bbox.find('xmin').text),\n                          int(bbox.find('ymin').text),\n                          int(bbox.find('xmax').text),\n                          int(bbox.find('ymax').text)]\n    objects.append(obj_struct)\n\n  return objects\n\n\ndef voc_ap(rec, prec, use_07_metric=False):\n  \"\"\" ap = voc_ap(rec, prec, [use_07_metric])\n  Compute VOC AP given precision and recall.\n  If use_07_metric is true, uses the\n  VOC 07 11 point method (default:False).\n  \"\"\"\n  if use_07_metric:\n    # 11 point metric\n    ap = 0.\n    for t in np.arange(0., 1.1, 0.1):\n      if np.sum(rec >= t) == 0:\n        p = 0\n      else:\n        p = np.max(prec[rec >= t])\n      ap = ap + p / 11.\n  else:\n    # correct AP calculation\n    # first append sentinel values at the end\n    mrec = np.concatenate(([0.], rec, [1.]))\n    mpre = np.concatenate(([0.], prec, [0.]))\n\n    # compute the precision envelope\n    for i in range(mpre.size - 1, 0, -1):\n      mpre[i - 1] = np.maximum(mpre[i - 1], mpre[i])\n\n    # to calculate area under PR curve, look for points\n    # where X axis (recall) changes value\n    i = np.where(mrec[1:] != mrec[:-1])[0]\n\n    # and sum (\\Delta recall) * prec\n    ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1])\n  return ap\n\n\ndef voc_eval(detpath,\n             annopath,\n             imagesetfile,\n             classname,\n             cachedir,\n             ovthresh=0.5,\n             use_07_metric=False):\n  \"\"\"rec, prec, ap = voc_eval(detpath,\n                              annopath,\n                              imagesetfile,\n                              classname,\n                              [ovthresh],\n                              [use_07_metric])\n\n  Top level function that does the PASCAL VOC evaluation.\n\n  detpath: Path to detections\n      detpath.format(classname) should produce the detection results file.\n  annopath: Path to annotations\n      annopath.format(imagename) should be the xml annotations file.\n  imagesetfile: Text file containing the list of images, one image per line.\n  classname: Category name (duh)\n  cachedir: Directory for caching the annotations\n  [ovthresh]: Overlap threshold (default = 0.5)\n  [use_07_metric]: Whether to use VOC07's 11 point AP computation\n      (default False)\n  \"\"\"\n  # assumes detections are in detpath.format(classname)\n  # assumes annotations are in annopath.format(imagename)\n  # assumes imagesetfile is a text file with each line an image name\n  # cachedir caches the annotations in a pickle file\n\n  # first load gt\n  if not os.path.isdir(cachedir):\n    os.mkdir(cachedir)\n  cachefile = os.path.join(cachedir, 'annots.pkl')\n  # read list of images\n  with open(imagesetfile, 'r') as f:\n    lines = f.readlines()\n  imagenames = [x.strip() for x in lines]\n\n  if not os.path.isfile(cachefile):\n    # load annots\n    recs = {}\n    for i, imagename in enumerate(imagenames):\n      recs[imagename] = parse_rec(annopath.format(imagename))\n      if i % 100 == 0:\n        print('Reading annotation for {:d}/{:d}'.format(\n          i + 1, len(imagenames)))\n    # save\n    print('Saving cached annotations to {:s}'.format(cachefile))\n    with open(cachefile, 'w') as f:\n      pickle.dump(recs, f)\n  else:\n    # load\n    with open(cachefile, 'rb') as f:\n      try:\n        recs = pickle.load(f)\n      except:\n        recs = pickle.load(f, encoding='bytes')\n\n  # extract gt objects for this class\n  class_recs = {}\n  npos = 0\n  for imagename in imagenames:\n    R = [obj for obj in recs[imagename] if obj['name'] == classname]\n    bbox = np.array([x['bbox'] for x in R])\n    difficult = np.array([x['difficult'] for x in R]).astype(np.bool)\n    det = [False] * len(R)\n    npos = npos + sum(~difficult)\n    class_recs[imagename] = {'bbox': bbox,\n                             'difficult': difficult,\n                             'det': det}\n\n  # read dets\n  detfile = detpath.format(classname)\n  with open(detfile, 'r') as f:\n    lines = f.readlines()\n\n  splitlines = [x.strip().split(' ') for x in lines]\n  image_ids = [x[0] for x in splitlines]\n  confidence = np.array([float(x[1]) for x in splitlines])\n  BB = np.array([[float(z) for z in x[2:]] for x in splitlines])\n\n  nd = len(image_ids)\n  tp = np.zeros(nd)\n  fp = np.zeros(nd)\n\n  if BB.shape[0] > 0:\n    # sort by confidence\n    sorted_ind = np.argsort(-confidence)\n    sorted_scores = np.sort(-confidence)\n    BB = BB[sorted_ind, :]\n    image_ids = [image_ids[x] for x in sorted_ind]\n\n    # go down dets and mark TPs and FPs\n    for d in range(nd):\n      R = class_recs[image_ids[d]]\n      bb = BB[d, :].astype(float)\n      ovmax = -np.inf\n      BBGT = R['bbox'].astype(float)\n\n      if BBGT.size > 0:\n        # compute overlaps\n        # intersection\n        ixmin = np.maximum(BBGT[:, 0], bb[0])\n        iymin = np.maximum(BBGT[:, 1], bb[1])\n        ixmax = np.minimum(BBGT[:, 2], bb[2])\n        iymax = np.minimum(BBGT[:, 3], bb[3])\n        iw = np.maximum(ixmax - ixmin + 1., 0.)\n        ih = np.maximum(iymax - iymin + 1., 0.)\n        inters = iw * ih\n\n        # union\n        uni = ((bb[2] - bb[0] + 1.) * (bb[3] - bb[1] + 1.) +\n               (BBGT[:, 2] - BBGT[:, 0] + 1.) *\n               (BBGT[:, 3] - BBGT[:, 1] + 1.) - inters)\n\n        overlaps = inters / uni\n        ovmax = np.max(overlaps)\n        jmax = np.argmax(overlaps)\n\n      if ovmax > ovthresh:\n        if not R['difficult'][jmax]:\n          if not R['det'][jmax]:\n            tp[d] = 1.\n            R['det'][jmax] = 1\n          else:\n            fp[d] = 1.\n      else:\n        fp[d] = 1.\n\n  # compute precision recall\n  fp = np.cumsum(fp)\n  tp = np.cumsum(tp)\n  rec = tp / float(npos)\n  # avoid divide by zero in case the first detection matches a difficult\n  # ground truth\n  prec = tp / np.maximum(tp + fp, np.finfo(np.float64).eps)\n  ap = voc_ap(rec, prec, use_07_metric)\n\n  return rec, prec, ap\n"
  },
  {
    "path": "lib/layer_utils/__init__.py",
    "content": ""
  },
  {
    "path": "lib/layer_utils/anchor_target_layer.py",
    "content": "# --------------------------------------------------------\n# Faster R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick and Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport os\nfrom model.config import cfg\nimport numpy as np\nimport numpy.random as npr\nfrom utils.cython_bbox import bbox_overlaps\nfrom model.bbox_transform import bbox_transform\n\ndef anchor_target_layer(rpn_cls_score, gt_boxes, im_info, _feat_stride, all_anchors, num_anchors):\n  \"\"\"Same as the anchor target layer in original Fast/er RCNN \"\"\"\n  A = num_anchors\n  total_anchors = all_anchors.shape[0]\n  K = total_anchors / num_anchors\n  im_info = im_info[0]\n\n  # allow boxes to sit over the edge by a small amount\n  _allowed_border = 0\n\n  # map of shape (..., H, W)\n  height, width = rpn_cls_score.shape[1:3]\n\n  # only keep anchors inside the image\n  inds_inside = np.where(\n    (all_anchors[:, 0] >= -_allowed_border) &\n    (all_anchors[:, 1] >= -_allowed_border) &\n    (all_anchors[:, 2] < im_info[1] + _allowed_border) &  # width\n    (all_anchors[:, 3] < im_info[0] + _allowed_border)  # height\n  )[0]\n\n  # keep only inside anchors\n  anchors = all_anchors[inds_inside, :]\n\n  # label: 1 is positive, 0 is negative, -1 is dont care\n  labels = np.empty((len(inds_inside),), dtype=np.float32)\n  labels.fill(-1)\n\n  # overlaps between the anchors and the gt boxes\n  # overlaps (ex, gt)\n  overlaps = bbox_overlaps(\n    np.ascontiguousarray(anchors, dtype=np.float),\n    np.ascontiguousarray(gt_boxes, dtype=np.float))\n  argmax_overlaps = overlaps.argmax(axis=1)\n  max_overlaps = overlaps[np.arange(len(inds_inside)), argmax_overlaps]\n  gt_argmax_overlaps = overlaps.argmax(axis=0)\n  gt_max_overlaps = overlaps[gt_argmax_overlaps,\n                             np.arange(overlaps.shape[1])]\n  gt_argmax_overlaps = np.where(overlaps == gt_max_overlaps)[0]\n\n  if not cfg.TRAIN.RPN_CLOBBER_POSITIVES:\n    # assign bg labels first so that positive labels can clobber them\n    # first set the negatives\n    labels[max_overlaps < cfg.TRAIN.RPN_NEGATIVE_OVERLAP] = 0\n\n  # fg label: for each gt, anchor with highest overlap\n  labels[gt_argmax_overlaps] = 1\n\n  # fg label: above threshold IOU\n  labels[max_overlaps >= cfg.TRAIN.RPN_POSITIVE_OVERLAP] = 1\n\n  if cfg.TRAIN.RPN_CLOBBER_POSITIVES:\n    # assign bg labels last so that negative labels can clobber positives\n    labels[max_overlaps < cfg.TRAIN.RPN_NEGATIVE_OVERLAP] = 0\n\n  # subsample positive labels if we have too many\n  num_fg = int(cfg.TRAIN.RPN_FG_FRACTION * cfg.TRAIN.RPN_BATCHSIZE)\n  fg_inds = np.where(labels == 1)[0]\n  if len(fg_inds) > num_fg:\n    disable_inds = npr.choice(\n      fg_inds, size=(len(fg_inds) - num_fg), replace=False)\n    labels[disable_inds] = -1\n\n  # subsample negative labels if we have too many\n  num_bg = cfg.TRAIN.RPN_BATCHSIZE - np.sum(labels == 1)\n  bg_inds = np.where(labels == 0)[0]\n  if len(bg_inds) > num_bg:\n    disable_inds = npr.choice(\n      bg_inds, size=(len(bg_inds) - num_bg), replace=False)\n    labels[disable_inds] = -1\n\n  bbox_targets = np.zeros((len(inds_inside), 4), dtype=np.float32)\n  bbox_targets = _compute_targets(anchors, gt_boxes[argmax_overlaps, :])\n\n  bbox_inside_weights = np.zeros((len(inds_inside), 4), dtype=np.float32)\n  # only the positive ones have regression targets\n  bbox_inside_weights[labels == 1, :] = np.array(cfg.TRAIN.RPN_BBOX_INSIDE_WEIGHTS)\n\n  bbox_outside_weights = np.zeros((len(inds_inside), 4), dtype=np.float32)\n  if cfg.TRAIN.RPN_POSITIVE_WEIGHT < 0:\n    # uniform weighting of examples (given non-uniform sampling)\n    num_examples = np.sum(labels >= 0)\n    positive_weights = np.ones((1, 4)) * 1.0 / num_examples\n    negative_weights = np.ones((1, 4)) * 1.0 / num_examples\n  else:\n    assert ((cfg.TRAIN.RPN_POSITIVE_WEIGHT > 0) &\n            (cfg.TRAIN.RPN_POSITIVE_WEIGHT < 1))\n    positive_weights = (cfg.TRAIN.RPN_POSITIVE_WEIGHT /\n                        np.sum(labels == 1))\n    negative_weights = ((1.0 - cfg.TRAIN.RPN_POSITIVE_WEIGHT) /\n                        np.sum(labels == 0))\n  bbox_outside_weights[labels == 1, :] = positive_weights\n  bbox_outside_weights[labels == 0, :] = negative_weights\n\n  # map up to original set of anchors\n  labels = _unmap(labels, total_anchors, inds_inside, fill=-1)\n  bbox_targets = _unmap(bbox_targets, total_anchors, inds_inside, fill=0)\n  bbox_inside_weights = _unmap(bbox_inside_weights, total_anchors, inds_inside, fill=0)\n  bbox_outside_weights = _unmap(bbox_outside_weights, total_anchors, inds_inside, fill=0)\n\n  # labels\n  labels = labels.reshape((1, height, width, A)).transpose(0, 3, 1, 2)\n  labels = labels.reshape((1, 1, A * height, width))\n  rpn_labels = labels\n\n  # bbox_targets\n  bbox_targets = bbox_targets \\\n    .reshape((1, height, width, A * 4))\n\n  rpn_bbox_targets = bbox_targets\n  # bbox_inside_weights\n  bbox_inside_weights = bbox_inside_weights \\\n    .reshape((1, height, width, A * 4))\n\n  rpn_bbox_inside_weights = bbox_inside_weights\n\n  # bbox_outside_weights\n  bbox_outside_weights = bbox_outside_weights \\\n    .reshape((1, height, width, A * 4))\n\n  rpn_bbox_outside_weights = bbox_outside_weights\n  return rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights\n\n\ndef _unmap(data, count, inds, fill=0):\n  \"\"\" Unmap a subset of item (data) back to the original set of items (of\n  size count) \"\"\"\n  if len(data.shape) == 1:\n    ret = np.empty((count,), dtype=np.float32)\n    ret.fill(fill)\n    ret[inds] = data\n  else:\n    ret = np.empty((count,) + data.shape[1:], dtype=np.float32)\n    ret.fill(fill)\n    ret[inds, :] = data\n  return ret\n\n\ndef _compute_targets(ex_rois, gt_rois):\n  \"\"\"Compute bounding-box regression targets for an image.\"\"\"\n\n  assert ex_rois.shape[0] == gt_rois.shape[0]\n  assert ex_rois.shape[1] == 4\n  assert gt_rois.shape[1] == 5\n\n  return bbox_transform(ex_rois, gt_rois[:, :4]).astype(np.float32, copy=False)\n"
  },
  {
    "path": "lib/layer_utils/generate_anchors.py",
    "content": "# --------------------------------------------------------\n# Faster R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick and Sean Bell\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\n\n\n# Verify that we compute the same anchors as Shaoqing's matlab implementation:\n#\n#    >> load output/rpn_cachedir/faster_rcnn_VOC2007_ZF_stage1_rpn/anchors.mat\n#    >> anchors\n#\n#    anchors =\n#\n#       -83   -39   100    56\n#      -175   -87   192   104\n#      -359  -183   376   200\n#       -55   -55    72    72\n#      -119  -119   136   136\n#      -247  -247   264   264\n#       -35   -79    52    96\n#       -79  -167    96   184\n#      -167  -343   184   360\n\n# array([[ -83.,  -39.,  100.,   56.],\n#       [-175.,  -87.,  192.,  104.],\n#       [-359., -183.,  376.,  200.],\n#       [ -55.,  -55.,   72.,   72.],\n#       [-119., -119.,  136.,  136.],\n#       [-247., -247.,  264.,  264.],\n#       [ -35.,  -79.,   52.,   96.],\n#       [ -79., -167.,   96.,  184.],\n#       [-167., -343.,  184.,  360.]])\n\ndef generate_anchors(base_size=16, ratios=[0.5, 1, 2],\n                     scales=2 ** np.arange(3, 6)):\n  \"\"\"\n  Generate anchor (reference) windows by enumerating aspect ratios X\n  scales wrt a reference (0, 0, 15, 15) window.\n  \"\"\"\n\n  base_anchor = np.array([1, 1, base_size, base_size]) - 1\n  ratio_anchors = _ratio_enum(base_anchor, ratios)\n  anchors = np.vstack([_scale_enum(ratio_anchors[i, :], scales)\n                       for i in range(ratio_anchors.shape[0])])\n  return anchors\n\n\ndef _whctrs(anchor):\n  \"\"\"\n  Return width, height, x center, and y center for an anchor (window).\n  \"\"\"\n\n  w = anchor[2] - anchor[0] + 1\n  h = anchor[3] - anchor[1] + 1\n  x_ctr = anchor[0] + 0.5 * (w - 1)\n  y_ctr = anchor[1] + 0.5 * (h - 1)\n  return w, h, x_ctr, y_ctr\n\n\ndef _mkanchors(ws, hs, x_ctr, y_ctr):\n  \"\"\"\n  Given a vector of widths (ws) and heights (hs) around a center\n  (x_ctr, y_ctr), output a set of anchors (windows).\n  \"\"\"\n\n  ws = ws[:, np.newaxis]\n  hs = hs[:, np.newaxis]\n  anchors = np.hstack((x_ctr - 0.5 * (ws - 1),\n                       y_ctr - 0.5 * (hs - 1),\n                       x_ctr + 0.5 * (ws - 1),\n                       y_ctr + 0.5 * (hs - 1)))\n  return anchors\n\n\ndef _ratio_enum(anchor, ratios):\n  \"\"\"\n  Enumerate a set of anchors for each aspect ratio wrt an anchor.\n  \"\"\"\n\n  w, h, x_ctr, y_ctr = _whctrs(anchor)\n  size = w * h\n  size_ratios = size / ratios\n  ws = np.round(np.sqrt(size_ratios))\n  hs = np.round(ws * ratios)\n  anchors = _mkanchors(ws, hs, x_ctr, y_ctr)\n  return anchors\n\n\ndef _scale_enum(anchor, scales):\n  \"\"\"\n  Enumerate a set of anchors for each scale wrt an anchor.\n  \"\"\"\n\n  w, h, x_ctr, y_ctr = _whctrs(anchor)\n  ws = w * scales\n  hs = h * scales\n  anchors = _mkanchors(ws, hs, x_ctr, y_ctr)\n  return anchors\n\n\nif __name__ == '__main__':\n  import time\n\n  t = time.time()\n  a = generate_anchors()\n  print(time.time() - t)\n  print(a)\n  from IPython import embed;\n\n  embed()\n"
  },
  {
    "path": "lib/layer_utils/proposal_layer.py",
    "content": "# --------------------------------------------------------\n# Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick and Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\nfrom model.config import cfg\nfrom model.bbox_transform import bbox_transform_inv, clip_boxes\nfrom model.nms_wrapper import nms\n\n\ndef proposal_layer(rpn_cls_prob, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchors, num_anchors):\n  \"\"\"A simplified version compared to fast/er RCNN\n     For details please see the technical report\n  \"\"\"\n  if type(cfg_key) == bytes:\n      cfg_key = cfg_key.decode('utf-8')\n  pre_nms_topN = cfg[cfg_key].RPN_PRE_NMS_TOP_N\n  post_nms_topN = cfg[cfg_key].RPN_POST_NMS_TOP_N\n  nms_thresh = cfg[cfg_key].RPN_NMS_THRESH\n\n  im_info = im_info[0]\n  # Get the scores and bounding boxes\n  scores = rpn_cls_prob[:, :, :, num_anchors:]\n  rpn_bbox_pred = rpn_bbox_pred.reshape((-1, 4))\n  scores = scores.reshape((-1, 1))\n  proposals = bbox_transform_inv(anchors, rpn_bbox_pred)\n  proposals = clip_boxes(proposals, im_info[:2])\n\n  # Pick the top region proposals\n  order = scores.ravel().argsort()[::-1]\n  if pre_nms_topN > 0:\n    order = order[:pre_nms_topN]\n  proposals = proposals[order, :]\n  scores = scores[order]\n\n  # Non-maximal suppression\n  keep = nms(np.hstack((proposals, scores)), nms_thresh)\n\n  # Pick th top region proposals after NMS\n  if post_nms_topN > 0:\n    keep = keep[:post_nms_topN]\n  proposals = proposals[keep, :]\n  scores = scores[keep]\n\n  # Only support single image as input\n  batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32)\n  blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False)))\n\n  return blob, scores\n"
  },
  {
    "path": "lib/layer_utils/proposal_target_layer.py",
    "content": "# --------------------------------------------------------\n# Faster R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick, Sean Bell and Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\nimport numpy.random as npr\nfrom model.config import cfg\nfrom model.bbox_transform import bbox_transform\nfrom utils.cython_bbox import bbox_overlaps\n\n\ndef proposal_target_layer(rpn_rois, rpn_scores, gt_boxes, _num_classes):\n  \"\"\"\n  Assign object detection proposals to ground-truth targets. Produces proposal\n  classification labels and bounding-box regression targets.\n  \"\"\"\n\n  # Proposal ROIs (0, x1, y1, x2, y2) coming from RPN\n  # (i.e., rpn.proposal_layer.ProposalLayer), or any other source\n  all_rois = rpn_rois\n  all_scores = rpn_scores\n\n  # Include ground-truth boxes in the set of candidate rois\n  if cfg.TRAIN.USE_GT:\n    zeros = np.zeros((gt_boxes.shape[0], 1), dtype=gt_boxes.dtype)\n    all_rois = np.vstack(\n      (all_rois, np.hstack((zeros, gt_boxes[:, :-1])))\n    )\n    # not sure if it a wise appending, but anyway i am not using it\n    all_scores = np.vstack((all_scores, zeros))\n\n  num_images = 1\n  rois_per_image = cfg.TRAIN.BATCH_SIZE / num_images\n  fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)\n\n  # Sample rois with classification labels and bounding box regression\n  # targets\n  labels, rois, roi_scores, bbox_targets, bbox_inside_weights = _sample_rois(\n    all_rois, all_scores, gt_boxes, fg_rois_per_image,\n    rois_per_image, _num_classes)\n\n  rois = rois.reshape(-1, 5)\n  roi_scores = roi_scores.reshape(-1)\n  labels = labels.reshape(-1, 1)\n  bbox_targets = bbox_targets.reshape(-1, _num_classes * 4)\n  bbox_inside_weights = bbox_inside_weights.reshape(-1, _num_classes * 4)\n  bbox_outside_weights = np.array(bbox_inside_weights > 0).astype(np.float32)\n\n  return rois, roi_scores, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights\n\n\ndef _get_bbox_regression_labels(bbox_target_data, num_classes):\n  \"\"\"Bounding-box regression targets (bbox_target_data) are stored in a\n  compact form N x (class, tx, ty, tw, th)\n\n  This function expands those targets into the 4-of-4*K representation used\n  by the network (i.e. only one class has non-zero targets).\n\n  Returns:\n      bbox_target (ndarray): N x 4K blob of regression targets\n      bbox_inside_weights (ndarray): N x 4K blob of loss weights\n  \"\"\"\n\n  clss = bbox_target_data[:, 0]\n  bbox_targets = np.zeros((clss.size, 4 * num_classes), dtype=np.float32)\n  bbox_inside_weights = np.zeros(bbox_targets.shape, dtype=np.float32)\n  inds = np.where(clss > 0)[0]\n  for ind in inds:\n    cls = clss[ind]\n    start = int(4 * cls)\n    end = start + 4\n    bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]\n    bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS\n  return bbox_targets, bbox_inside_weights\n\n\ndef _compute_targets(ex_rois, gt_rois, labels):\n  \"\"\"Compute bounding-box regression targets for an image.\"\"\"\n\n  assert ex_rois.shape[0] == gt_rois.shape[0]\n  assert ex_rois.shape[1] == 4\n  assert gt_rois.shape[1] == 4\n\n  targets = bbox_transform(ex_rois, gt_rois)\n  if cfg.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED:\n    # Optionally normalize targets by a precomputed mean and stdev\n    targets = ((targets - np.array(cfg.TRAIN.BBOX_NORMALIZE_MEANS))\n               / np.array(cfg.TRAIN.BBOX_NORMALIZE_STDS))\n  return np.hstack(\n    (labels[:, np.newaxis], targets)).astype(np.float32, copy=False)\n\n\ndef _sample_rois(all_rois, all_scores, gt_boxes, fg_rois_per_image, rois_per_image, num_classes):\n  \"\"\"Generate a random sample of RoIs comprising foreground and background\n  examples.\n  \"\"\"\n  # overlaps: (rois x gt_boxes)\n  overlaps = bbox_overlaps(\n    np.ascontiguousarray(all_rois[:, 1:5], dtype=np.float),\n    np.ascontiguousarray(gt_boxes[:, :4], dtype=np.float))\n  gt_assignment = overlaps.argmax(axis=1)\n  max_overlaps = overlaps.max(axis=1)\n  labels = gt_boxes[gt_assignment, 4]\n\n  # Select foreground RoIs as those with >= FG_THRESH overlap\n  fg_inds = np.where(max_overlaps >= cfg.TRAIN.FG_THRESH)[0]\n  # Guard against the case when an image has fewer than fg_rois_per_image\n  # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)\n  bg_inds = np.where((max_overlaps < cfg.TRAIN.BG_THRESH_HI) &\n                     (max_overlaps >= cfg.TRAIN.BG_THRESH_LO))[0]\n\n  # Small modification to the original version where we ensure a fixed number of regions are sampled\n  if fg_inds.size > 0 and bg_inds.size > 0:\n    fg_rois_per_image = min(fg_rois_per_image, fg_inds.size)\n    fg_inds = npr.choice(fg_inds, size=int(fg_rois_per_image), replace=False)\n    bg_rois_per_image = rois_per_image - fg_rois_per_image\n    to_replace = bg_inds.size < bg_rois_per_image\n    bg_inds = npr.choice(bg_inds, size=int(bg_rois_per_image), replace=to_replace)\n  elif fg_inds.size > 0:\n    to_replace = fg_inds.size < rois_per_image\n    fg_inds = npr.choice(fg_inds, size=int(rois_per_image), replace=to_replace)\n    fg_rois_per_image = rois_per_image\n  elif bg_inds.size > 0:\n    to_replace = bg_inds.size < rois_per_image\n    bg_inds = npr.choice(bg_inds, size=int(rois_per_image), replace=to_replace)\n    fg_rois_per_image = 0\n  else:\n    import pdb\n    pdb.set_trace()\n\n  # The indices that we're selecting (both fg and bg)\n  keep_inds = np.append(fg_inds, bg_inds)\n  # Select sampled values from various arrays:\n  labels = labels[keep_inds]\n  # Clamp labels for the background RoIs to 0\n  labels[int(fg_rois_per_image):] = 0\n  rois = all_rois[keep_inds]\n  roi_scores = all_scores[keep_inds]\n\n  bbox_target_data = _compute_targets(\n    rois[:, 1:5], gt_boxes[gt_assignment[keep_inds], :4], labels)\n\n  bbox_targets, bbox_inside_weights = \\\n    _get_bbox_regression_labels(bbox_target_data, num_classes)\n\n  return labels, rois, roi_scores, bbox_targets, bbox_inside_weights\n"
  },
  {
    "path": "lib/layer_utils/proposal_top_layer.py",
    "content": "# --------------------------------------------------------\n# Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\nfrom model.config import cfg\nfrom model.bbox_transform import bbox_transform_inv, clip_boxes\nimport numpy.random as npr\n\ndef proposal_top_layer(rpn_cls_prob, rpn_bbox_pred, im_info, _feat_stride, anchors, num_anchors):\n  \"\"\"A layer that just selects the top region proposals\n     without using non-maximal suppression,\n     For details please see the technical report\n  \"\"\"\n  rpn_top_n = cfg.TEST.RPN_TOP_N\n  im_info = im_info[0]\n\n  scores = rpn_cls_prob[:, :, :, num_anchors:]\n\n  rpn_bbox_pred = rpn_bbox_pred.reshape((-1, 4))\n  scores = scores.reshape((-1, 1))\n\n  length = scores.shape[0]\n  if length < rpn_top_n:\n    # Random selection, maybe unnecessary and loses good proposals\n    # But such case rarely happens\n    top_inds = npr.choice(length, size=rpn_top_n, replace=True)\n  else:\n    top_inds = scores.argsort(0)[::-1]\n    top_inds = top_inds[:rpn_top_n]\n    top_inds = top_inds.reshape(rpn_top_n, )\n\n  # Do the selection here\n  anchors = anchors[top_inds, :]\n  rpn_bbox_pred = rpn_bbox_pred[top_inds, :]\n  scores = scores[top_inds]\n\n  # Convert anchors into proposals via bbox transformations\n  proposals = bbox_transform_inv(anchors, rpn_bbox_pred)\n\n  # Clip predicted boxes to image\n  proposals = clip_boxes(proposals, im_info[:2])\n\n  # Output rois blob\n  # Our RPN implementation only supports a single input image, so all\n  # batch inds are 0\n  batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32)\n  blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False)))\n  return blob, scores\n"
  },
  {
    "path": "lib/layer_utils/snippets.py",
    "content": "# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\nimport numpy.random as npr\nfrom model.config import cfg\nfrom layer_utils.generate_anchors import generate_anchors\nfrom model.bbox_transform import bbox_transform_inv, clip_boxes\nfrom utils.cython_bbox import bbox_overlaps\n\ndef generate_anchors_pre(height, width, feat_stride, anchor_scales=(8,16,32), anchor_ratios=(0.5,1,2)):\n  \"\"\" A wrapper function to generate anchors given different scales\n    Also return the number of anchors in variable 'length'\n  \"\"\"\n  anchors = generate_anchors(ratios=np.array(anchor_ratios), scales=np.array(anchor_scales))\n  A = anchors.shape[0]\n  shift_x = np.arange(0, width) * feat_stride\n  shift_y = np.arange(0, height) * feat_stride\n  shift_x, shift_y = np.meshgrid(shift_x, shift_y)\n  shifts = np.vstack((shift_x.ravel(), shift_y.ravel(), shift_x.ravel(), shift_y.ravel())).transpose()\n  K = shifts.shape[0]\n  # width changes faster, so here it is H, W, C\n  anchors = anchors.reshape((1, A, 4)) + shifts.reshape((1, K, 4)).transpose((1, 0, 2))\n  anchors = anchors.reshape((K * A, 4)).astype(np.float32, copy=False)\n  length = np.int32(anchors.shape[0])\n\n  return anchors, length\n"
  },
  {
    "path": "lib/model/__init__.py",
    "content": "from . import config\n"
  },
  {
    "path": "lib/model/bbox_transform.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\n\ndef bbox_transform(ex_rois, gt_rois):\n  ex_widths = ex_rois[:, 2] - ex_rois[:, 0] + 1.0\n  ex_heights = ex_rois[:, 3] - ex_rois[:, 1] + 1.0\n  ex_ctr_x = ex_rois[:, 0] + 0.5 * ex_widths\n  ex_ctr_y = ex_rois[:, 1] + 0.5 * ex_heights\n\n  gt_widths = gt_rois[:, 2] - gt_rois[:, 0] + 1.0\n  gt_heights = gt_rois[:, 3] - gt_rois[:, 1] + 1.0\n  gt_ctr_x = gt_rois[:, 0] + 0.5 * gt_widths\n  gt_ctr_y = gt_rois[:, 1] + 0.5 * gt_heights\n\n  targets_dx = (gt_ctr_x - ex_ctr_x) / ex_widths\n  targets_dy = (gt_ctr_y - ex_ctr_y) / ex_heights\n  targets_dw = np.log(gt_widths / ex_widths)\n  targets_dh = np.log(gt_heights / ex_heights)\n\n  targets = np.vstack(\n    (targets_dx, targets_dy, targets_dw, targets_dh)).transpose()\n  return targets\n\n\ndef bbox_transform_inv(boxes, deltas):\n  if boxes.shape[0] == 0:\n    return np.zeros((0, deltas.shape[1]), dtype=deltas.dtype)\n\n  boxes = boxes.astype(deltas.dtype, copy=False)\n  widths = boxes[:, 2] - boxes[:, 0] + 1.0\n  heights = boxes[:, 3] - boxes[:, 1] + 1.0\n  ctr_x = boxes[:, 0] + 0.5 * widths\n  ctr_y = boxes[:, 1] + 0.5 * heights\n\n  dx = deltas[:, 0::4]\n  dy = deltas[:, 1::4]\n  dw = deltas[:, 2::4]\n  dh = deltas[:, 3::4]\n  \n  pred_ctr_x = dx * widths[:, np.newaxis] + ctr_x[:, np.newaxis]\n  pred_ctr_y = dy * heights[:, np.newaxis] + ctr_y[:, np.newaxis]\n  pred_w = np.exp(dw) * widths[:, np.newaxis]\n  pred_h = np.exp(dh) * heights[:, np.newaxis]\n\n  pred_boxes = np.zeros(deltas.shape, dtype=deltas.dtype)\n  # x1\n  pred_boxes[:, 0::4] = pred_ctr_x - 0.5 * pred_w\n  # y1\n  pred_boxes[:, 1::4] = pred_ctr_y - 0.5 * pred_h\n  # x2\n  pred_boxes[:, 2::4] = pred_ctr_x + 0.5 * pred_w\n  # y2\n  pred_boxes[:, 3::4] = pred_ctr_y + 0.5 * pred_h\n\n  return pred_boxes\n\n\ndef clip_boxes(boxes, im_shape):\n  \"\"\"\n  Clip boxes to image boundaries.\n  \"\"\"\n\n  # x1 >= 0\n  boxes[:, 0::4] = np.maximum(np.minimum(boxes[:, 0::4], im_shape[1] - 1), 0)\n  # y1 >= 0\n  boxes[:, 1::4] = np.maximum(np.minimum(boxes[:, 1::4], im_shape[0] - 1), 0)\n  # x2 < im_shape[1]\n  boxes[:, 2::4] = np.maximum(np.minimum(boxes[:, 2::4], im_shape[1] - 1), 0)\n  # y2 < im_shape[0]\n  boxes[:, 3::4] = np.maximum(np.minimum(boxes[:, 3::4], im_shape[0] - 1), 0)\n  return boxes\n"
  },
  {
    "path": "lib/model/config.py",
    "content": "from __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport os\nimport os.path as osp\nimport numpy as np\n# `pip install easydict` if you don't have it\nfrom easydict import EasyDict as edict\n\n__C = edict()\n# Consumers can get config by:\n#   from fast_rcnn_config import cfg\ncfg = __C\n\n#\n# Training options\n#\n__C.TRAIN = edict()\n\n# Initial learning rate\n__C.TRAIN.LEARNING_RATE = 0.0001\n\n# Momentum\n__C.TRAIN.MOMENTUM = 0.9\n\n# Weight decay, for regularization\n__C.TRAIN.WEIGHT_DECAY = 0.0005\n\n# Factor for reducing the learning rate\n__C.TRAIN.GAMMA = 0.1\n\n# Step size for reducing the learning rate, currently only support one step\n__C.TRAIN.STEPSIZE = 30000\n\n# Iteration intervals for showing the loss during training, on command line interface\n__C.TRAIN.DISPLAY = 10\n\n# Whether to double the learning rate for bias\n__C.TRAIN.DOUBLE_BIAS = True\n\n# Whether to initialize the weights with truncated normal distribution \n__C.TRAIN.TRUNCATED = False\n\n# Whether to have weight decay on bias as well\n__C.TRAIN.BIAS_DECAY = False\n\n# Whether to add ground truth boxes to the pool when sampling regions\n__C.TRAIN.USE_GT = False\n\n# Whether to use aspect-ratio grouping of training images, introduced merely for saving\n# GPU memory\n__C.TRAIN.ASPECT_GROUPING = False\n\n# The number of snapshots kept, older ones are deleted to save space\n__C.TRAIN.SNAPSHOT_KEPT = 3\n\n# The time interval for saving tensorflow summaries\n__C.TRAIN.SUMMARY_INTERVAL = 180\n\n# Scale to use during training (can list multiple scales)\n# The scale is the pixel size of an image's shortest side\n__C.TRAIN.SCALES = (600,)\n\n# Max pixel size of the longest side of a scaled input image\n__C.TRAIN.MAX_SIZE = 1000\n\n# Images to use per minibatch\n__C.TRAIN.IMS_PER_BATCH = 1\n\n# Minibatch size (number of regions of interest [ROIs])\n__C.TRAIN.BATCH_SIZE = 128\n\n# Fraction of minibatch that is labeled foreground (i.e. class > 0)\n__C.TRAIN.FG_FRACTION = 0.25\n\n# Overlap threshold for a ROI to be considered foreground (if >= FG_THRESH)\n__C.TRAIN.FG_THRESH = 0.5\n\n# Overlap threshold for a ROI to be considered background (class = 0 if\n# overlap in [LO, HI))\n__C.TRAIN.BG_THRESH_HI = 0.5\n__C.TRAIN.BG_THRESH_LO = 0.1\n\n# Use horizontally-flipped images during training?\n#__C.TRAIN.USE_FLIPPED = True\n__C.TRAIN.USE_FLIPPED = False\n\n# Train bounding-box regressors\n__C.TRAIN.BBOX_REG = True\n\n# Overlap required between a ROI and ground-truth box in order for that ROI to\n# be used as a bounding-box regression training example\n__C.TRAIN.BBOX_THRESH = 0.5\n\n# Iterations between snapshots\n__C.TRAIN.SNAPSHOT_ITERS = 3000\n\n# solver.prototxt specifies the snapshot path prefix, this adds an optional\n# infix to yield the path: <prefix>[_<infix>]_iters_XYZ.caffemodel\n__C.TRAIN.SNAPSHOT_PREFIX = 'res101_faster_rcnn'\n# __C.TRAIN.SNAPSHOT_INFIX = ''\n\n# Use a prefetch thread in roi_data_layer.layer\n# So far I haven't found this useful; likely more engineering work is required\n# __C.TRAIN.USE_PREFETCH = False\n\n# Normalize the targets (subtract empirical mean, divide by empirical stddev)\n__C.TRAIN.BBOX_NORMALIZE_TARGETS = True\n# Deprecated (inside weights)\n__C.TRAIN.BBOX_INSIDE_WEIGHTS = (1.0, 1.0, 1.0, 1.0)\n# Normalize the targets using \"precomputed\" (or made up) means and stdevs\n# (BBOX_NORMALIZE_TARGETS must also be True)\n__C.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED = True\n__C.TRAIN.BBOX_NORMALIZE_MEANS = (0.0, 0.0, 0.0, 0.0)\n__C.TRAIN.BBOX_NORMALIZE_STDS = (0.1, 0.1, 0.2, 0.2)\n\n# Train using these proposals\n__C.TRAIN.PROPOSAL_METHOD = 'gt'\n\n# Make minibatches from images that have similar aspect ratios (i.e. both\n# tall and thin or both short and wide) in order to avoid wasting computation\n# on zero-padding.\n\n# Use RPN to detect objects\n__C.TRAIN.HAS_RPN = True\n# IOU >= thresh: positive example\n__C.TRAIN.RPN_POSITIVE_OVERLAP = 0.7\n# IOU < thresh: negative example\n__C.TRAIN.RPN_NEGATIVE_OVERLAP = 0.3\n# If an anchor statisfied by positive and negative conditions set to negative\n__C.TRAIN.RPN_CLOBBER_POSITIVES = False\n# Max number of foreground examples\n__C.TRAIN.RPN_FG_FRACTION = 0.5\n# Total number of examples\n__C.TRAIN.RPN_BATCHSIZE = 256\n# NMS threshold used on RPN proposals\n__C.TRAIN.RPN_NMS_THRESH = 0.7\n# Number of top scoring boxes to keep before apply NMS to RPN proposals\n__C.TRAIN.RPN_PRE_NMS_TOP_N = 12000\n# Number of top scoring boxes to keep after applying NMS to RPN proposals\n__C.TRAIN.RPN_POST_NMS_TOP_N = 2000\n# Proposal height and width both need to be greater than RPN_MIN_SIZE (at orig image scale)\n# __C.TRAIN.RPN_MIN_SIZE = 16\n# Deprecated (outside weights)\n__C.TRAIN.RPN_BBOX_INSIDE_WEIGHTS = (1.0, 1.0, 1.0, 1.0)\n# Give the positive RPN examples weight of p * 1 / {num positives}\n# and give negatives a weight of (1 - p)\n# Set to -1.0 to use uniform example weighting\n__C.TRAIN.RPN_POSITIVE_WEIGHT = -1.0\n# Whether to use all ground truth bounding boxes for training, \n# For COCO, setting USE_ALL_GT to False will exclude boxes that are flagged as ''iscrowd''\n__C.TRAIN.USE_ALL_GT = True\n\n#\n# Testing options\n#\n__C.TEST = edict()\n\n# Scale to use during testing (can NOT list multiple scales)\n# The scale is the pixel size of an image's shortest side\n__C.TEST.SCALES = (600,)\n\n# Max pixel size of the longest side of a scaled input image\n__C.TEST.MAX_SIZE = 1000\n\n# Overlap threshold used for non-maximum suppression (suppress boxes with\n# IoU >= this threshold)\n__C.TEST.NMS = 0.3\n\n# Experimental: treat the (K+1) units in the cls_score layer as linear\n# predictors (trained, eg, with one-vs-rest SVMs).\n__C.TEST.SVM = False\n\n# Test using bounding-box regressors\n__C.TEST.BBOX_REG = True\n\n# Propose boxes\n__C.TEST.HAS_RPN = False\n\n# Test using these proposals\n__C.TEST.PROPOSAL_METHOD = 'gt'\n\n## NMS threshold used on RPN proposals\n__C.TEST.RPN_NMS_THRESH = 0.7\n## Number of top scoring boxes to keep before apply NMS to RPN proposals\n__C.TEST.RPN_PRE_NMS_TOP_N = 6000\n\n## Number of top scoring boxes to keep after applying NMS to RPN proposals\n__C.TEST.RPN_POST_NMS_TOP_N = 300\n\n# Proposal height and width both need to be greater than RPN_MIN_SIZE (at orig image scale)\n# __C.TEST.RPN_MIN_SIZE = 16\n\n# Testing mode, default to be 'nms', 'top' is slower but better\n# See report for details\n__C.TEST.MODE = 'nms'\n\n# Only useful when TEST.MODE is 'top', specifies the number of top proposals to select\n__C.TEST.RPN_TOP_N = 5000\n\n#\n# ResNet options\n#\n\n__C.RESNET = edict()\n\n# Option to set if max-pooling is appended after crop_and_resize. \n# if true, the region will be resized to a squre of 2xPOOLING_SIZE, \n# then 2x2 max-pooling is applied; otherwise the region will be directly\n# resized to a square of POOLING_SIZE\n__C.RESNET.MAX_POOL = False\n\n# Number of fixed blocks during finetuning, by default the first of all 4 blocks is fixed\n# Range: 0 (none) to 3 (all)\n__C.RESNET.FIXED_BLOCKS = 1\n\n# Whether to tune the batch nomalization parameters during training\n__C.RESNET.BN_TRAIN = False\n\n#\n# MISC\n#\n\n# The mapping from image coordinates to feature map coordinates might cause\n# some boxes that are distinct in image space to become identical in feature\n# coordinates. If DEDUP_BOXES > 0, then DEDUP_BOXES is used as the scale factor\n# for identifying duplicate boxes.\n# 1/16 is correct for {Alex,Caffe}Net, VGG_CNN_M_1024, and VGG16\n__C.DEDUP_BOXES = 1. / 16.\n\n# Pixel mean values (BGR order) as a (1, 1, 3) array\n# We use the same pixel mean for all networks even though it's not exactly what\n# they were trained with\n__C.PIXEL_MEANS = np.array([[[102.9801, 115.9465, 122.7717]]])\n\n# For reproducibility\n__C.RNG_SEED = 3\n\n# A small number that's used many times\n__C.EPS = 1e-14\n\n# Root directory of project\n__C.ROOT_DIR = osp.abspath(osp.join(osp.dirname(__file__), '..', '..'))\n\n# Data directory\n__C.DATA_DIR = osp.abspath(osp.join(__C.ROOT_DIR, 'data'))\n\n# Name (or path to) the matlab executable\n__C.MATLAB = 'matlab'\n\n# Place outputs under an experiments directory\n__C.EXP_DIR = 'default'\n\n# Use GPU implementation of non-maximum suppression\n__C.USE_GPU_NMS = True\n\n# Default GPU device id\n__C.GPU_ID = 0\n\n# Default pooling mode, only 'crop' is available\n__C.POOLING_MODE = 'crop'\n\n# Size of the pooled region after RoI pooling\n__C.POOLING_SIZE = 7\n\n# Anchor scales for RPN\n__C.ANCHOR_SCALES = [8,16,32]\n\n# Anchor ratios for RPN\n__C.ANCHOR_RATIOS = [0.5,1,2]\n\n\ndef get_output_dir(imdb, weights_filename):\n  \"\"\"Return the directory where experimental artifacts are placed.\n  If the directory does not exist, it is created.\n\n  A canonical path is built using the name from an imdb and a network\n  (if not None).\n  \"\"\"\n  outdir = osp.abspath(osp.join(__C.ROOT_DIR, 'output', __C.EXP_DIR, imdb.name))\n  if weights_filename is None:\n    weights_filename = 'default'\n  outdir = osp.join(outdir, weights_filename)\n  if not os.path.exists(outdir):\n    os.makedirs(outdir)\n  return outdir\n\n\ndef get_output_tb_dir(imdb, weights_filename):\n  \"\"\"Return the directory where tensorflow summaries are placed.\n  If the directory does not exist, it is created.\n\n  A canonical path is built using the name from an imdb and a network\n  (if not None).\n  \"\"\"\n  outdir = osp.abspath(osp.join(__C.ROOT_DIR, 'tensorboard', __C.EXP_DIR, imdb.name))\n  if weights_filename is None:\n    weights_filename = 'default'\n  outdir = osp.join(outdir, weights_filename)\n  if not os.path.exists(outdir):\n    os.makedirs(outdir)\n  return outdir\n\n\ndef _merge_a_into_b(a, b):\n  \"\"\"Merge config dictionary a into config dictionary b, clobbering the\n  options in b whenever they are also specified in a.\n  \"\"\"\n  if type(a) is not edict:\n    return\n\n  for k, v in a.items():\n    # a must specify keys that are in b\n    if k not in b:\n      raise KeyError('{} is not a valid config key'.format(k))\n\n    # the types must match, too\n    old_type = type(b[k])\n    if old_type is not type(v):\n      if isinstance(b[k], np.ndarray):\n        v = np.array(v, dtype=b[k].dtype)\n      else:\n        raise ValueError(('Type mismatch ({} vs. {}) '\n                          'for config key: {}').format(type(b[k]),\n                                                       type(v), k))\n\n    # recursively merge dicts\n    if type(v) is edict:\n      try:\n        _merge_a_into_b(a[k], b[k])\n      except:\n        print(('Error under config key: {}'.format(k)))\n        raise\n    else:\n      b[k] = v\n\n\ndef cfg_from_file(filename):\n  \"\"\"Load a config file and merge it into the default options.\"\"\"\n  import yaml\n  with open(filename, 'r') as f:\n    yaml_cfg = edict(yaml.load(f))\n\n  _merge_a_into_b(yaml_cfg, __C)\n\n\ndef cfg_from_list(cfg_list):\n  \"\"\"Set config keys via list (e.g., from command line).\"\"\"\n  from ast import literal_eval\n  assert len(cfg_list) % 2 == 0\n  for k, v in zip(cfg_list[0::2], cfg_list[1::2]):\n    key_list = k.split('.')\n    d = __C\n    for subkey in key_list[:-1]:\n      assert subkey in d\n      d = d[subkey]\n    subkey = key_list[-1]\n    assert subkey in d\n    try:\n      value = literal_eval(v)\n    except:\n      # handle the case when v is a string literal\n      value = v\n    assert type(value) == type(d[subkey]), \\\n      'type {} does not match original type {}'.format(\n        type(value), type(d[subkey]))\n    d[subkey] = value\n"
  },
  {
    "path": "lib/model/config.py~",
    "content": "from __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport os\nimport os.path as osp\nimport numpy as np\n# `pip install easydict` if you don't have it\nfrom easydict import EasyDict as edict\n\n__C = edict()\n# Consumers can get config by:\n#   from fast_rcnn_config import cfg\ncfg = __C\n\n#\n# Training options\n#\n__C.TRAIN = edict()\n\n# Initial learning rate\n__C.TRAIN.LEARNING_RATE = 0.001\n\n# Momentum\n__C.TRAIN.MOMENTUM = 0.9\n\n# Weight decay, for regularization\n__C.TRAIN.WEIGHT_DECAY = 0.0005\n\n# Factor for reducing the learning rate\n__C.TRAIN.GAMMA = 0.1\n\n# Step size for reducing the learning rate, currently only support one step\n__C.TRAIN.STEPSIZE = 30000\n\n# Iteration intervals for showing the loss during training, on command line interface\n__C.TRAIN.DISPLAY = 10\n\n# Whether to double the learning rate for bias\n__C.TRAIN.DOUBLE_BIAS = True\n\n# Whether to initialize the weights with truncated normal distribution \n__C.TRAIN.TRUNCATED = False\n\n# Whether to have weight decay on bias as well\n__C.TRAIN.BIAS_DECAY = False\n\n# Whether to add ground truth boxes to the pool when sampling regions\n__C.TRAIN.USE_GT = False\n\n# Whether to use aspect-ratio grouping of training images, introduced merely for saving\n# GPU memory\n__C.TRAIN.ASPECT_GROUPING = False\n\n# The number of snapshots kept, older ones are deleted to save space\n__C.TRAIN.SNAPSHOT_KEPT = 3\n\n# The time interval for saving tensorflow summaries\n__C.TRAIN.SUMMARY_INTERVAL = 180\n\n# Scale to use during training (can list multiple scales)\n# The scale is the pixel size of an image's shortest side\n__C.TRAIN.SCALES = (600,)\n\n# Max pixel size of the longest side of a scaled input image\n__C.TRAIN.MAX_SIZE = 1000\n\n# Images to use per minibatch\n__C.TRAIN.IMS_PER_BATCH = 1\n\n# Minibatch size (number of regions of interest [ROIs])\n__C.TRAIN.BATCH_SIZE = 128\n\n# Fraction of minibatch that is labeled foreground (i.e. class > 0)\n__C.TRAIN.FG_FRACTION = 0.25\n\n# Overlap threshold for a ROI to be considered foreground (if >= FG_THRESH)\n__C.TRAIN.FG_THRESH = 0.5\n\n# Overlap threshold for a ROI to be considered background (class = 0 if\n# overlap in [LO, HI))\n__C.TRAIN.BG_THRESH_HI = 0.5\n__C.TRAIN.BG_THRESH_LO = 0.1\n\n# Use horizontally-flipped images during training?\n#__C.TRAIN.USE_FLIPPED = True\n__C.TRAIN.USE_FLIPPED = False\n\n# Train bounding-box regressors\n__C.TRAIN.BBOX_REG = True\n\n# Overlap required between a ROI and ground-truth box in order for that ROI to\n# be used as a bounding-box regression training example\n__C.TRAIN.BBOX_THRESH = 0.5\n\n# Iterations between snapshots\n__C.TRAIN.SNAPSHOT_ITERS = 3000\n\n# solver.prototxt specifies the snapshot path prefix, this adds an optional\n# infix to yield the path: <prefix>[_<infix>]_iters_XYZ.caffemodel\n__C.TRAIN.SNAPSHOT_PREFIX = 'res101_faster_rcnn'\n# __C.TRAIN.SNAPSHOT_INFIX = ''\n\n# Use a prefetch thread in roi_data_layer.layer\n# So far I haven't found this useful; likely more engineering work is required\n# __C.TRAIN.USE_PREFETCH = False\n\n# Normalize the targets (subtract empirical mean, divide by empirical stddev)\n__C.TRAIN.BBOX_NORMALIZE_TARGETS = True\n# Deprecated (inside weights)\n__C.TRAIN.BBOX_INSIDE_WEIGHTS = (1.0, 1.0, 1.0, 1.0)\n# Normalize the targets using \"precomputed\" (or made up) means and stdevs\n# (BBOX_NORMALIZE_TARGETS must also be True)\n__C.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED = True\n__C.TRAIN.BBOX_NORMALIZE_MEANS = (0.0, 0.0, 0.0, 0.0)\n__C.TRAIN.BBOX_NORMALIZE_STDS = (0.1, 0.1, 0.2, 0.2)\n\n# Train using these proposals\n__C.TRAIN.PROPOSAL_METHOD = 'gt'\n\n# Make minibatches from images that have similar aspect ratios (i.e. both\n# tall and thin or both short and wide) in order to avoid wasting computation\n# on zero-padding.\n\n# Use RPN to detect objects\n__C.TRAIN.HAS_RPN = True\n# IOU >= thresh: positive example\n__C.TRAIN.RPN_POSITIVE_OVERLAP = 0.7\n# IOU < thresh: negative example\n__C.TRAIN.RPN_NEGATIVE_OVERLAP = 0.3\n# If an anchor statisfied by positive and negative conditions set to negative\n__C.TRAIN.RPN_CLOBBER_POSITIVES = False\n# Max number of foreground examples\n__C.TRAIN.RPN_FG_FRACTION = 0.5\n# Total number of examples\n__C.TRAIN.RPN_BATCHSIZE = 256\n# NMS threshold used on RPN proposals\n__C.TRAIN.RPN_NMS_THRESH = 0.7\n# Number of top scoring boxes to keep before apply NMS to RPN proposals\n__C.TRAIN.RPN_PRE_NMS_TOP_N = 12000\n# Number of top scoring boxes to keep after applying NMS to RPN proposals\n__C.TRAIN.RPN_POST_NMS_TOP_N = 2000\n# Proposal height and width both need to be greater than RPN_MIN_SIZE (at orig image scale)\n# __C.TRAIN.RPN_MIN_SIZE = 16\n# Deprecated (outside weights)\n__C.TRAIN.RPN_BBOX_INSIDE_WEIGHTS = (1.0, 1.0, 1.0, 1.0)\n# Give the positive RPN examples weight of p * 1 / {num positives}\n# and give negatives a weight of (1 - p)\n# Set to -1.0 to use uniform example weighting\n__C.TRAIN.RPN_POSITIVE_WEIGHT = -1.0\n# Whether to use all ground truth bounding boxes for training, \n# For COCO, setting USE_ALL_GT to False will exclude boxes that are flagged as ''iscrowd''\n__C.TRAIN.USE_ALL_GT = True\n\n#\n# Testing options\n#\n__C.TEST = edict()\n\n# Scale to use during testing (can NOT list multiple scales)\n# The scale is the pixel size of an image's shortest side\n__C.TEST.SCALES = (600,)\n\n# Max pixel size of the longest side of a scaled input image\n__C.TEST.MAX_SIZE = 1000\n\n# Overlap threshold used for non-maximum suppression (suppress boxes with\n# IoU >= this threshold)\n__C.TEST.NMS = 0.3\n\n# Experimental: treat the (K+1) units in the cls_score layer as linear\n# predictors (trained, eg, with one-vs-rest SVMs).\n__C.TEST.SVM = False\n\n# Test using bounding-box regressors\n__C.TEST.BBOX_REG = True\n\n# Propose boxes\n__C.TEST.HAS_RPN = False\n\n# Test using these proposals\n__C.TEST.PROPOSAL_METHOD = 'gt'\n\n## NMS threshold used on RPN proposals\n__C.TEST.RPN_NMS_THRESH = 0.7\n## Number of top scoring boxes to keep before apply NMS to RPN proposals\n__C.TEST.RPN_PRE_NMS_TOP_N = 6000\n\n## Number of top scoring boxes to keep after applying NMS to RPN proposals\n__C.TEST.RPN_POST_NMS_TOP_N = 300\n\n# Proposal height and width both need to be greater than RPN_MIN_SIZE (at orig image scale)\n# __C.TEST.RPN_MIN_SIZE = 16\n\n# Testing mode, default to be 'nms', 'top' is slower but better\n# See report for details\n__C.TEST.MODE = 'nms'\n\n# Only useful when TEST.MODE is 'top', specifies the number of top proposals to select\n__C.TEST.RPN_TOP_N = 5000\n\n#\n# ResNet options\n#\n\n__C.RESNET = edict()\n\n# Option to set if max-pooling is appended after crop_and_resize. \n# if true, the region will be resized to a squre of 2xPOOLING_SIZE, \n# then 2x2 max-pooling is applied; otherwise the region will be directly\n# resized to a square of POOLING_SIZE\n__C.RESNET.MAX_POOL = False\n\n# Number of fixed blocks during finetuning, by default the first of all 4 blocks is fixed\n# Range: 0 (none) to 3 (all)\n__C.RESNET.FIXED_BLOCKS = 1\n\n# Whether to tune the batch nomalization parameters during training\n__C.RESNET.BN_TRAIN = False\n\n#\n# MISC\n#\n\n# The mapping from image coordinates to feature map coordinates might cause\n# some boxes that are distinct in image space to become identical in feature\n# coordinates. If DEDUP_BOXES > 0, then DEDUP_BOXES is used as the scale factor\n# for identifying duplicate boxes.\n# 1/16 is correct for {Alex,Caffe}Net, VGG_CNN_M_1024, and VGG16\n__C.DEDUP_BOXES = 1. / 16.\n\n# Pixel mean values (BGR order) as a (1, 1, 3) array\n# We use the same pixel mean for all networks even though it's not exactly what\n# they were trained with\n__C.PIXEL_MEANS = np.array([[[102.9801, 115.9465, 122.7717]]])\n\n# For reproducibility\n__C.RNG_SEED = 3\n\n# A small number that's used many times\n__C.EPS = 1e-14\n\n# Root directory of project\n__C.ROOT_DIR = osp.abspath(osp.join(osp.dirname(__file__), '..', '..'))\n\n# Data directory\n__C.DATA_DIR = osp.abspath(osp.join(__C.ROOT_DIR, 'data'))\n\n# Name (or path to) the matlab executable\n__C.MATLAB = 'matlab'\n\n# Place outputs under an experiments directory\n__C.EXP_DIR = 'default'\n\n# Use GPU implementation of non-maximum suppression\n__C.USE_GPU_NMS = True\n\n# Default GPU device id\n__C.GPU_ID = 0\n\n# Default pooling mode, only 'crop' is available\n__C.POOLING_MODE = 'crop'\n\n# Size of the pooled region after RoI pooling\n__C.POOLING_SIZE = 7\n\n# Anchor scales for RPN\n__C.ANCHOR_SCALES = [8,16,32]\n\n# Anchor ratios for RPN\n__C.ANCHOR_RATIOS = [0.5,1,2]\n\n\ndef get_output_dir(imdb, weights_filename):\n  \"\"\"Return the directory where experimental artifacts are placed.\n  If the directory does not exist, it is created.\n\n  A canonical path is built using the name from an imdb and a network\n  (if not None).\n  \"\"\"\n  outdir = osp.abspath(osp.join(__C.ROOT_DIR, 'output', __C.EXP_DIR, imdb.name))\n  if weights_filename is None:\n    weights_filename = 'default'\n  outdir = osp.join(outdir, weights_filename)\n  if not os.path.exists(outdir):\n    os.makedirs(outdir)\n  return outdir\n\n\ndef get_output_tb_dir(imdb, weights_filename):\n  \"\"\"Return the directory where tensorflow summaries are placed.\n  If the directory does not exist, it is created.\n\n  A canonical path is built using the name from an imdb and a network\n  (if not None).\n  \"\"\"\n  outdir = osp.abspath(osp.join(__C.ROOT_DIR, 'tensorboard', __C.EXP_DIR, imdb.name))\n  if weights_filename is None:\n    weights_filename = 'default'\n  outdir = osp.join(outdir, weights_filename)\n  if not os.path.exists(outdir):\n    os.makedirs(outdir)\n  return outdir\n\n\ndef _merge_a_into_b(a, b):\n  \"\"\"Merge config dictionary a into config dictionary b, clobbering the\n  options in b whenever they are also specified in a.\n  \"\"\"\n  if type(a) is not edict:\n    return\n\n  for k, v in a.items():\n    # a must specify keys that are in b\n    if k not in b:\n      raise KeyError('{} is not a valid config key'.format(k))\n\n    # the types must match, too\n    old_type = type(b[k])\n    if old_type is not type(v):\n      if isinstance(b[k], np.ndarray):\n        v = np.array(v, dtype=b[k].dtype)\n      else:\n        raise ValueError(('Type mismatch ({} vs. {}) '\n                          'for config key: {}').format(type(b[k]),\n                                                       type(v), k))\n\n    # recursively merge dicts\n    if type(v) is edict:\n      try:\n        _merge_a_into_b(a[k], b[k])\n      except:\n        print(('Error under config key: {}'.format(k)))\n        raise\n    else:\n      b[k] = v\n\n\ndef cfg_from_file(filename):\n  \"\"\"Load a config file and merge it into the default options.\"\"\"\n  import yaml\n  with open(filename, 'r') as f:\n    yaml_cfg = edict(yaml.load(f))\n\n  _merge_a_into_b(yaml_cfg, __C)\n\n\ndef cfg_from_list(cfg_list):\n  \"\"\"Set config keys via list (e.g., from command line).\"\"\"\n  from ast import literal_eval\n  assert len(cfg_list) % 2 == 0\n  for k, v in zip(cfg_list[0::2], cfg_list[1::2]):\n    key_list = k.split('.')\n    d = __C\n    for subkey in key_list[:-1]:\n      assert subkey in d\n      d = d[subkey]\n    subkey = key_list[-1]\n    assert subkey in d\n    try:\n      value = literal_eval(v)\n    except:\n      # handle the case when v is a string literal\n      value = v\n    assert type(value) == type(d[subkey]), \\\n      'type {} does not match original type {}'.format(\n        type(value), type(d[subkey]))\n    d[subkey] = value\n"
  },
  {
    "path": "lib/model/nms_wrapper.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nfrom model.config import cfg\nfrom nms.gpu_nms import gpu_nms\nfrom nms.cpu_nms import cpu_nms\n\ndef nms(dets, thresh, force_cpu=False):\n  \"\"\"Dispatch to either CPU or GPU NMS implementations.\"\"\"\n\n  if dets.shape[0] == 0:\n    return []\n  if cfg.USE_GPU_NMS and not force_cpu:\n    return gpu_nms(dets, thresh, device_id=cfg.GPU_ID)\n  else:\n    return cpu_nms(dets, thresh)\n"
  },
  {
    "path": "lib/model/test.py",
    "content": "# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport cv2\nimport numpy as np\ntry:\n  import cPickle as pickle\nexcept ImportError:\n  import pickle\nimport os\nimport math\n\nfrom utils.timer import Timer\nfrom utils.cython_nms import nms, nms_new\nfrom utils.boxes_grid import get_boxes_grid\nfrom utils.blob import im_list_to_blob\n\nfrom model.config import cfg, get_output_dir\nfrom model.bbox_transform import clip_boxes, bbox_transform_inv\n\ndef _get_image_blob(im):\n  \"\"\"Converts an image into a network input.\n  Arguments:\n    im (ndarray): a color image in BGR order\n  Returns:\n    blob (ndarray): a data blob holding an image pyramid\n    im_scale_factors (list): list of image scales (relative to im) used\n      in the image pyramid\n  \"\"\"\n  im_orig = im.astype(np.float32, copy=True)\n  im_orig -= cfg.PIXEL_MEANS\n\n  im_shape = im_orig.shape\n  im_size_min = np.min(im_shape[0:2])\n  im_size_max = np.max(im_shape[0:2])\n\n  processed_ims = []\n  im_scale_factors = []\n\n  for target_size in cfg.TEST.SCALES:\n    im_scale = float(target_size) / float(im_size_min)\n    # Prevent the biggest axis from being more than MAX_SIZE\n    if np.round(im_scale * im_size_max) > cfg.TEST.MAX_SIZE:\n      im_scale = float(cfg.TEST.MAX_SIZE) / float(im_size_max)\n    im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale,\n            interpolation=cv2.INTER_LINEAR)\n    im_scale_factors.append(im_scale)\n    processed_ims.append(im)\n\n  # Create a blob to hold the input images\n  blob = im_list_to_blob(processed_ims)\n\n  return blob, np.array(im_scale_factors)\n\ndef _get_blobs(im):\n  \"\"\"Convert an image and RoIs within that image into network inputs.\"\"\"\n  blobs = {}\n  blobs['data'], im_scale_factors = _get_image_blob(im)\n\n  return blobs, im_scale_factors\n\ndef _clip_boxes(boxes, im_shape):\n  \"\"\"Clip boxes to image boundaries.\"\"\"\n  # x1 >= 0\n  boxes[:, 0::4] = np.maximum(boxes[:, 0::4], 0)\n  # y1 >= 0\n  boxes[:, 1::4] = np.maximum(boxes[:, 1::4], 0)\n  # x2 < im_shape[1]\n  boxes[:, 2::4] = np.minimum(boxes[:, 2::4], im_shape[1] - 1)\n  # y2 < im_shape[0]\n  boxes[:, 3::4] = np.minimum(boxes[:, 3::4], im_shape[0] - 1)\n  return boxes\n\ndef _rescale_boxes(boxes, inds, scales):\n  \"\"\"Rescale boxes according to image rescaling.\"\"\"\n  for i in range(boxes.shape[0]):\n    boxes[i,:] = boxes[i,:] / scales[int(inds[i])]\n\n  return boxes\n\ndef im_detect(sess, net, im):\n  blobs, im_scales = _get_blobs(im)\n  assert len(im_scales) == 1, \"Only single-image batch implemented\"\n   \n  im_blob = blobs['data']\n  # seems to have height, width, and image scales\n  # still not sure about the scale, maybe full image it is 1.\n  blobs['im_info'] = np.array([[im_blob.shape[1], im_blob.shape[2], im_scales[0]]], dtype=np.float32)\n\n  _, scores, bbox_pred, rois = net.test_image(sess, blobs['data'], blobs['im_info'])\n  \n  boxes = rois[:, 1:5] / im_scales[0]\n  # print(scores.shape, bbox_pred.shape, rois.shape, boxes.shape)\n  scores = np.reshape(scores, [scores.shape[0], -1])\n  bbox_pred = np.reshape(bbox_pred, [bbox_pred.shape[0], -1])\n  if cfg.TEST.BBOX_REG:\n    # Apply bounding-box regression deltas\n    box_deltas = bbox_pred\n    pred_boxes = bbox_transform_inv(boxes, box_deltas)\n    pred_boxes = _clip_boxes(pred_boxes, im.shape)\n  else:\n    # Simply repeat the boxes, once for each class\n    pred_boxes = np.tile(boxes, (1, scores.shape[1]))\n\n  return scores, pred_boxes\n\ndef apply_nms(all_boxes, thresh):\n  \"\"\"Apply non-maximum suppression to all predicted boxes output by the\n  test_net method.\n  \"\"\"\n  num_classes = len(all_boxes)\n  num_images = len(all_boxes[0])\n  nms_boxes = [[[] for _ in range(num_images)] for _ in range(num_classes)]\n  for cls_ind in range(num_classes):\n    for im_ind in range(num_images):\n      dets = all_boxes[cls_ind][im_ind]\n      if dets == []:\n        continue\n\n      x1 = dets[:, 0]\n      y1 = dets[:, 1]\n      x2 = dets[:, 2]\n      y2 = dets[:, 3]\n      scores = dets[:, 4]\n      inds = np.where((x2 > x1) & (y2 > y1) & (scores > cfg.TEST.DET_THRESHOLD))[0]\n      dets = dets[inds,:]\n      if dets == []:\n        continue\n\n      keep = nms(dets, thresh)\n      if len(keep) == 0:\n        continue\n      nms_boxes[cls_ind][im_ind] = dets[keep, :].copy()\n  return nms_boxes\n\ndef test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05):\n  np.random.seed(cfg.RNG_SEED)\n  \"\"\"Test a Fast R-CNN network on an image database.\"\"\"\n  num_images = len(imdb.image_index)\n  # all detections are collected into:\n  #  all_boxes[cls][image] = N x 5 array of detections in\n  #  (x1, y1, x2, y2, score)\n  all_boxes = [[[] for _ in range(num_images)]\n         for _ in range(imdb.num_classes)]\n\n  output_dir = get_output_dir(imdb, weights_filename)\n  # timers\n  _t = {'im_detect' : Timer(), 'misc' : Timer()}\n\n  for i in range(num_images):\n    im = cv2.imread(imdb.image_path_at(i))\n\n    _t['im_detect'].tic()\n    scores, boxes = im_detect(sess, net, im)\n    _t['im_detect'].toc()\n\n    _t['misc'].tic()\n\n    # skip j = 0, because it's the background class\n    for j in range(1, imdb.num_classes):\n      inds = np.where(scores[:, j] > thresh)[0]\n      cls_scores = scores[inds, j]\n      cls_boxes = boxes[inds, j*4:(j+1)*4]\n      cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \\\n        .astype(np.float32, copy=False)\n      keep = nms(cls_dets, cfg.TEST.NMS)\n      cls_dets = cls_dets[keep, :]\n      all_boxes[j][i] = cls_dets\n\n    # Limit to max_per_image detections *over all classes*\n    if max_per_image > 0:\n      image_scores = np.hstack([all_boxes[j][i][:, -1]\n                    for j in range(1, imdb.num_classes)])\n      if len(image_scores) > max_per_image:\n        image_thresh = np.sort(image_scores)[-max_per_image]\n        for j in range(1, imdb.num_classes):\n          keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]\n          all_boxes[j][i] = all_boxes[j][i][keep, :]\n    _t['misc'].toc()\n\n    print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \\\n        .format(i + 1, num_images, _t['im_detect'].average_time,\n            _t['misc'].average_time))\n\n  det_file = os.path.join(output_dir, 'detections.pkl')\n  with open(det_file, 'wb') as f:\n    pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)\n\n  print('Evaluating detections')\n  imdb.evaluate_detections(all_boxes, output_dir)\n\n"
  },
  {
    "path": "lib/model/test.py~",
    "content": "# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport cv2\nimport numpy as np\ntry:\n  import cPickle as pickle\nexcept ImportError:\n  import pickle\nimport os\nimport math\n\nfrom utils.timer import Timer\nfrom utils.cython_nms import nms, nms_new\nfrom utils.boxes_grid import get_boxes_grid\nfrom utils.blob import im_list_to_blob\n\nfrom model.config import cfg, get_output_dir\nfrom model.bbox_transform import clip_boxes, bbox_transform_inv\n\ndef _get_image_blob(im):\n  \"\"\"Converts an image into a network input.\n  Arguments:\n    im (ndarray): a color image in BGR order\n  Returns:\n    blob (ndarray): a data blob holding an image pyramid\n    im_scale_factors (list): list of image scales (relative to im) used\n      in the image pyramid\n  \"\"\"\n  im_orig = im.astype(np.float32, copy=True)\n  im_orig -= cfg.PIXEL_MEANS\n\n  im_shape = im_orig.shape\n  im_size_min = np.min(im_shape[0:2])\n  im_size_max = np.max(im_shape[0:2])\n\n  processed_ims = []\n  im_scale_factors = []\n\n  for target_size in cfg.TEST.SCALES:\n    im_scale = float(target_size) / float(im_size_min)\n    # Prevent the biggest axis from being more than MAX_SIZE\n    if np.round(im_scale * im_size_max) > cfg.TEST.MAX_SIZE:\n      im_scale = float(cfg.TEST.MAX_SIZE) / float(im_size_max)\n    im = cv2.resize(im_orig, None, None, fx=im_scale, fy=im_scale,\n            interpolation=cv2.INTER_LINEAR)\n    im_scale_factors.append(im_scale)\n    processed_ims.append(im)\n\n  # Create a blob to hold the input images\n  blob = im_list_to_blob(processed_ims)\n\n  return blob, np.array(im_scale_factors)\n\ndef _get_blobs(im):\n  \"\"\"Convert an image and RoIs within that image into network inputs.\"\"\"\n  blobs = {}\n  blobs['data'], im_scale_factors = _get_image_blob(im)\n\n  return blobs, im_scale_factors\n\ndef _clip_boxes(boxes, im_shape):\n  \"\"\"Clip boxes to image boundaries.\"\"\"\n  # x1 >= 0\n  boxes[:, 0::4] = np.maximum(boxes[:, 0::4], 0)\n  # y1 >= 0\n  boxes[:, 1::4] = np.maximum(boxes[:, 1::4], 0)\n  # x2 < im_shape[1]\n  boxes[:, 2::4] = np.minimum(boxes[:, 2::4], im_shape[1] - 1)\n  # y2 < im_shape[0]\n  boxes[:, 3::4] = np.minimum(boxes[:, 3::4], im_shape[0] - 1)\n  return boxes\n\ndef _rescale_boxes(boxes, inds, scales):\n  \"\"\"Rescale boxes according to image rescaling.\"\"\"\n  for i in range(boxes.shape[0]):\n    boxes[i,:] = boxes[i,:] / scales[int(inds[i])]\n\n  return boxes\n\ndef im_detect(sess, net, im):\n  blobs, im_scales = _get_blobs(im)\n  assert len(im_scales) == 1, \"Only single-image batch implemented\"\n  print (im_scales)\n  im_blob = blobs['data']\n  # seems to have height, width, and image scales\n  # still not sure about the scale, maybe full image it is 1.\n  blobs['im_info'] = np.array([[im_blob.shape[1], im_blob.shape[2], im_scales[0]]], dtype=np.float32)\n\n  _, scores, bbox_pred, rois = net.test_image(sess, blobs['data'], blobs['im_info'])\n  \n  boxes = rois[:, 1:5] / im_scales[0]\n  # print(scores.shape, bbox_pred.shape, rois.shape, boxes.shape)\n  scores = np.reshape(scores, [scores.shape[0], -1])\n  bbox_pred = np.reshape(bbox_pred, [bbox_pred.shape[0], -1])\n  if cfg.TEST.BBOX_REG:\n    # Apply bounding-box regression deltas\n    box_deltas = bbox_pred\n    pred_boxes = bbox_transform_inv(boxes, box_deltas)\n    pred_boxes = _clip_boxes(pred_boxes, im.shape)\n  else:\n    # Simply repeat the boxes, once for each class\n    pred_boxes = np.tile(boxes, (1, scores.shape[1]))\n\n  return scores, pred_boxes\n\ndef apply_nms(all_boxes, thresh):\n  \"\"\"Apply non-maximum suppression to all predicted boxes output by the\n  test_net method.\n  \"\"\"\n  num_classes = len(all_boxes)\n  num_images = len(all_boxes[0])\n  nms_boxes = [[[] for _ in range(num_images)] for _ in range(num_classes)]\n  for cls_ind in range(num_classes):\n    for im_ind in range(num_images):\n      dets = all_boxes[cls_ind][im_ind]\n      if dets == []:\n        continue\n\n      x1 = dets[:, 0]\n      y1 = dets[:, 1]\n      x2 = dets[:, 2]\n      y2 = dets[:, 3]\n      scores = dets[:, 4]\n      inds = np.where((x2 > x1) & (y2 > y1) & (scores > cfg.TEST.DET_THRESHOLD))[0]\n      dets = dets[inds,:]\n      if dets == []:\n        continue\n\n      keep = nms(dets, thresh)\n      if len(keep) == 0:\n        continue\n      nms_boxes[cls_ind][im_ind] = dets[keep, :].copy()\n  return nms_boxes\n\ndef test_net(sess, net, imdb, weights_filename, max_per_image=100, thresh=0.05):\n  np.random.seed(cfg.RNG_SEED)\n  \"\"\"Test a Fast R-CNN network on an image database.\"\"\"\n  num_images = len(imdb.image_index)\n  # all detections are collected into:\n  #  all_boxes[cls][image] = N x 5 array of detections in\n  #  (x1, y1, x2, y2, score)\n  all_boxes = [[[] for _ in range(num_images)]\n         for _ in range(imdb.num_classes)]\n\n  output_dir = get_output_dir(imdb, weights_filename)\n  # timers\n  _t = {'im_detect' : Timer(), 'misc' : Timer()}\n\n  for i in range(num_images):\n    im = cv2.imread(imdb.image_path_at(i))\n\n    _t['im_detect'].tic()\n    scores, boxes = im_detect(sess, net, im)\n    _t['im_detect'].toc()\n\n    _t['misc'].tic()\n\n    # skip j = 0, because it's the background class\n    for j in range(1, imdb.num_classes):\n      inds = np.where(scores[:, j] > thresh)[0]\n      cls_scores = scores[inds, j]\n      cls_boxes = boxes[inds, j*4:(j+1)*4]\n      cls_dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])) \\\n        .astype(np.float32, copy=False)\n      keep = nms(cls_dets, cfg.TEST.NMS)\n      cls_dets = cls_dets[keep, :]\n      all_boxes[j][i] = cls_dets\n\n    # Limit to max_per_image detections *over all classes*\n    if max_per_image > 0:\n      image_scores = np.hstack([all_boxes[j][i][:, -1]\n                    for j in range(1, imdb.num_classes)])\n      if len(image_scores) > max_per_image:\n        image_thresh = np.sort(image_scores)[-max_per_image]\n        for j in range(1, imdb.num_classes):\n          keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]\n          all_boxes[j][i] = all_boxes[j][i][keep, :]\n    _t['misc'].toc()\n\n    print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s' \\\n        .format(i + 1, num_images, _t['im_detect'].average_time,\n            _t['misc'].average_time))\n\n  det_file = os.path.join(output_dir, 'detections.pkl')\n  with open(det_file, 'wb') as f:\n    pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)\n\n  print('Evaluating detections')\n  imdb.evaluate_detections(all_boxes, output_dir)\n\n"
  },
  {
    "path": "lib/model/train_val.py",
    "content": "# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen and Zheqi He\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nfrom model.config import cfg\nimport roi_data_layer.roidb as rdl_roidb\nfrom roi_data_layer.layer import RoIDataLayer\nfrom utils.timer import Timer\ntry:\n  import cPickle as pickle\nexcept ImportError:\n  import pickle\nimport numpy as np\nimport os\nimport sys\nimport glob\nimport time\n\nimport tensorflow as tf\nfrom tensorflow.python import pywrap_tensorflow\n\nclass SolverWrapper(object):\n  \"\"\"\n    A wrapper class for the training process\n  \"\"\"\n\n  def __init__(self, sess, network, imdb, roidb, valroidb, output_dir, tbdir, pretrained_model=None):\n    self.net = network\n    self.imdb = imdb\n    self.roidb = roidb\n    self.valroidb = valroidb\n    self.output_dir = output_dir\n    self.tbdir = tbdir\n    # Simply put '_val' at the end to save the summaries from the validation set\n    self.tbvaldir = tbdir + '_val'\n    if not os.path.exists(self.tbvaldir):\n      os.makedirs(self.tbvaldir)\n    self.pretrained_model = pretrained_model\n\n  def snapshot(self, sess, iter):\n    net = self.net\n\n    if not os.path.exists(self.output_dir):\n      os.makedirs(self.output_dir)\n\n    # Store the model snapshot\n    filename = cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_{:d}'.format(iter) + '.ckpt'\n    filename = os.path.join(self.output_dir, filename)\n    self.saver.save(sess, filename)\n    print('Wrote snapshot to: {:s}'.format(filename))\n\n    # Also store some meta information, random state, etc.\n    nfilename = cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_{:d}'.format(iter) + '.pkl'\n    nfilename = os.path.join(self.output_dir, nfilename)\n    # current state of numpy random\n    st0 = np.random.get_state()\n    # current position in the database\n    cur = self.data_layer._cur\n    # current shuffled indeces of the database\n    perm = self.data_layer._perm\n    # current position in the validation database\n    cur_val = self.data_layer_val._cur\n    # current shuffled indeces of the validation database\n    perm_val = self.data_layer_val._perm\n\n    # Dump the meta info\n    with open(nfilename, 'wb') as fid:\n      pickle.dump(st0, fid, pickle.HIGHEST_PROTOCOL)\n      pickle.dump(cur, fid, pickle.HIGHEST_PROTOCOL)\n      pickle.dump(perm, fid, pickle.HIGHEST_PROTOCOL)\n      pickle.dump(cur_val, fid, pickle.HIGHEST_PROTOCOL)\n      pickle.dump(perm_val, fid, pickle.HIGHEST_PROTOCOL)\n      pickle.dump(iter, fid, pickle.HIGHEST_PROTOCOL)\n\n    return filename, nfilename\n\n  def get_variables_in_checkpoint_file(self, file_name):\n    try:\n      reader = pywrap_tensorflow.NewCheckpointReader(file_name)\n      var_to_shape_map = reader.get_variable_to_shape_map()\n      return var_to_shape_map \n    except Exception as e:  # pylint: disable=broad-except\n      print(str(e))\n      if \"corrupted compressed block contents\" in str(e):\n        print(\"It's likely that your checkpoint file has been compressed \"\n              \"with SNAPPY.\")\n\n  def train_model(self, sess, max_iters):\n    # Build data layers for both training and validation set\n    self.data_layer = RoIDataLayer(self.roidb, self.imdb.num_classes)\n    self.data_layer_val = RoIDataLayer(self.valroidb, self.imdb.num_classes, random=True)\n\n    # Determine different scales for anchors, see paper\n    with sess.graph.as_default():\n      # Set the random seed for tensorflow\n      tf.set_random_seed(cfg.RNG_SEED)\n      # Build the main computation graph\n      layers = self.net.create_architecture(sess, 'TRAIN', self.imdb.num_classes, tag='default',\n                                            anchor_scales=cfg.ANCHOR_SCALES,\n                                            anchor_ratios=cfg.ANCHOR_RATIOS)\n      # Define the loss\n      loss = layers['total_loss']\n      # Set learning rate and momentum\n      lr = tf.Variable(cfg.TRAIN.LEARNING_RATE, trainable=False)\n      momentum = cfg.TRAIN.MOMENTUM\n      self.optimizer = tf.train.MomentumOptimizer(lr, momentum)\n\n      # Compute the gradients wrt the loss\n      gvs = self.optimizer.compute_gradients(loss)\n      # Double the gradient of the bias if set\n      if cfg.TRAIN.DOUBLE_BIAS:\n        final_gvs = []\n        with tf.variable_scope('Gradient_Mult') as scope:\n          for grad, var in gvs:\n            scale = 1.\n            if cfg.TRAIN.DOUBLE_BIAS and '/biases:' in var.name:\n              scale *= 2.\n            if not np.allclose(scale, 1.0):\n              grad = tf.multiply(grad, scale)\n            final_gvs.append((grad, var))\n        train_op = self.optimizer.apply_gradients(final_gvs)\n      else:\n        train_op = self.optimizer.apply_gradients(gvs)\n\n      # We will handle the snapshots ourselves\n      self.saver = tf.train.Saver(max_to_keep=100000)\n      # Write the train and validation information to tensorboard\n      self.writer = tf.summary.FileWriter(self.tbdir, sess.graph)\n      self.valwriter = tf.summary.FileWriter(self.tbvaldir)\n\n    # Find previous snapshots if there is any to restore from\n    sfiles = os.path.join(self.output_dir, cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_*.ckpt.meta')\n    sfiles = glob.glob(sfiles)\n    sfiles.sort(key=os.path.getmtime)\n    # Get the snapshot name in TensorFlow\n    redstr = '_iter_{:d}.'.format(cfg.TRAIN.STEPSIZE+1)\n    sfiles = [ss.replace('.meta', '') for ss in sfiles]\n    sfiles = [ss for ss in sfiles if redstr not in ss]\n\n    nfiles = os.path.join(self.output_dir, cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_*.pkl')\n    nfiles = glob.glob(nfiles)\n    nfiles.sort(key=os.path.getmtime)\n    nfiles = [nn for nn in nfiles if redstr not in nn]\n\n    lsf = len(sfiles)\n    assert len(nfiles) == lsf\n\n    np_paths = nfiles\n    ss_paths = sfiles\n\n    if lsf == 0:\n      # Fresh train directly from ImageNet weights\n      print('Loading initial model weights from {:s}'.format(self.pretrained_model))\n      variables = tf.global_variables()\n      # Initialize all variables first\n      sess.run(tf.variables_initializer(variables, name='init'))\n      var_keep_dic = self.get_variables_in_checkpoint_file(self.pretrained_model)\n      # Get the variables to restore, ignorizing the variables to fix\n      variables_to_restore = self.net.get_variables_to_restore(variables, var_keep_dic)\n\n      restorer = tf.train.Saver(variables_to_restore)\n      restorer.restore(sess, self.pretrained_model)\n      print('Loaded.')\n      # Need to fix the variables before loading, so that the RGB weights are changed to BGR\n      # For VGG16 it also changes the convolutional weights fc6 and fc7 to\n      # fully connected weights\n      self.net.fix_variables(sess, self.pretrained_model)\n      print('Fixed.')\n      sess.run(tf.assign(lr, cfg.TRAIN.LEARNING_RATE))\n      last_snapshot_iter = 0\n    else:\n      # Get the most recent snapshot and restore\n      ss_paths = [ss_paths[-1]]\n      np_paths = [np_paths[-1]]\n\n      print('Restorining model snapshots from {:s}'.format(sfiles[-1]))\n      self.saver.restore(sess, str(sfiles[-1]))\n      print('Restored.')\n      # Needs to restore the other hyperparameters/states for training, (TODO xinlei) I have\n      # tried my best to find the random states so that it can be recovered exactly\n      # However the Tensorflow state is currently not available\n      with open(str(nfiles[-1]), 'rb') as fid:\n        st0 = pickle.load(fid)\n        cur = pickle.load(fid)\n        perm = pickle.load(fid)\n        cur_val = pickle.load(fid)\n        perm_val = pickle.load(fid)\n        last_snapshot_iter = pickle.load(fid)\n\n        np.random.set_state(st0)\n        self.data_layer._cur = cur\n        self.data_layer._perm = perm\n        self.data_layer_val._cur = cur_val\n        self.data_layer_val._perm = perm_val\n\n        # Set the learning rate, only reduce once\n        if last_snapshot_iter > cfg.TRAIN.STEPSIZE:\n          sess.run(tf.assign(lr, cfg.TRAIN.LEARNING_RATE * cfg.TRAIN.GAMMA))\n        else:\n          sess.run(tf.assign(lr, cfg.TRAIN.LEARNING_RATE))\n\n    a = np.sum([np.prod(v.get_shape().as_list()) for v in tf.trainable_variables()])\n    print(a)\n\n    timer = Timer()\n    iter = last_snapshot_iter + 1\n    last_summary_time = time.time()\n    while iter < max_iters + 1:\n      # Learning rate\n      if iter == cfg.TRAIN.STEPSIZE + 1:\n        # Add snapshot here before reducing the learning rate\n        self.snapshot(sess, iter)\n        sess.run(tf.assign(lr, cfg.TRAIN.LEARNING_RATE * cfg.TRAIN.GAMMA))\n\n      timer.tic()\n      # Get training data, one batch at a time\n      blobs = self.data_layer.forward()\n\n      now = time.time()\n      if now - last_summary_time > cfg.TRAIN.SUMMARY_INTERVAL:\n        # Compute the graph with summary\n        rpn_loss_cls, rpn_loss_box, loss_cls, loss_box, total_loss, summary = \\\n          self.net.train_step_with_summary(sess, blobs, train_op)\n        self.writer.add_summary(summary, float(iter))\n        # Also check the summary on the validation set\n        blobs_val = self.data_layer_val.forward()\n        summary_val = self.net.get_summary(sess, blobs_val)\n        self.valwriter.add_summary(summary_val, float(iter))\n        last_summary_time = now\n      else:\n        # Compute the graph without summary\n        rpn_loss_cls, rpn_loss_box, loss_cls, loss_box, total_loss = \\\n          self.net.train_step(sess, blobs, train_op)\n      timer.toc()\n\n      # Display training information\n      if iter % (cfg.TRAIN.DISPLAY) == 0:\n        print('iter: %d / %d, total loss: %.6f\\n >>> rpn_loss_cls: %.6f\\n '\n              '>>> rpn_loss_box: %.6f\\n >>> loss_cls: %.6f\\n >>> loss_box: %.6f\\n >>> lr: %f' % \\\n              (iter, max_iters, total_loss, rpn_loss_cls, rpn_loss_box, loss_cls, loss_box, lr.eval()))\n        print('speed: {:.3f}s / iter'.format(timer.average_time))\n\n      if iter % cfg.TRAIN.SNAPSHOT_ITERS == 0:\n        last_snapshot_iter = iter\n        snapshot_path, np_path = self.snapshot(sess, iter)\n        np_paths.append(np_path)\n        ss_paths.append(snapshot_path)\n\n        # Remove the old snapshots if there are too many\n        if len(np_paths) > cfg.TRAIN.SNAPSHOT_KEPT:\n          to_remove = len(np_paths) - cfg.TRAIN.SNAPSHOT_KEPT\n          for c in range(to_remove):\n            nfile = np_paths[0]\n            os.remove(str(nfile))\n            np_paths.remove(nfile)\n\n        if len(ss_paths) > cfg.TRAIN.SNAPSHOT_KEPT:\n          to_remove = len(ss_paths) - cfg.TRAIN.SNAPSHOT_KEPT\n          for c in range(to_remove):\n            sfile = ss_paths[0]\n            # To make the code compatible to earlier versions of Tensorflow,\n            # where the naming tradition for checkpoints are different\n            if os.path.exists(str(sfile)):\n              os.remove(str(sfile))\n            else:\n              os.remove(str(sfile + '.data-00000-of-00001'))\n              os.remove(str(sfile + '.index'))\n            sfile_meta = sfile + '.meta'\n            os.remove(str(sfile_meta))\n            ss_paths.remove(sfile)\n\n      iter += 1\n\n    if last_snapshot_iter != iter - 1:\n      self.snapshot(sess, iter - 1)\n\n    self.writer.close()\n    self.valwriter.close()\n\n\ndef get_training_roidb(imdb):\n  \"\"\"Returns a roidb (Region of Interest database) for use in training.\"\"\"\n  if cfg.TRAIN.USE_FLIPPED:\n    print('Appending horizontally-flipped training examples...')\n    imdb.append_flipped_images()\n    print('done')\n\n  print('Preparing training data...')\n  rdl_roidb.prepare_roidb(imdb)\n  print('done')\n\n  return imdb.roidb\n\n\ndef filter_roidb(roidb):\n  \"\"\"Remove roidb entries that have no usable RoIs.\"\"\"\n\n  def is_valid(entry):\n    # Valid images have:\n    #   (1) At least one foreground RoI OR\n    #   (2) At least one background RoI\n    overlaps = entry['max_overlaps']\n    # find boxes with sufficient overlap\n    fg_inds = np.where(overlaps >= cfg.TRAIN.FG_THRESH)[0]\n    # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)\n    bg_inds = np.where((overlaps < cfg.TRAIN.BG_THRESH_HI) &\n                       (overlaps >= cfg.TRAIN.BG_THRESH_LO))[0]\n    # image is only valid if such boxes exist\n    valid = len(fg_inds) > 0 or len(bg_inds) > 0\n    return valid\n\n  num = len(roidb)\n  filtered_roidb = [entry for entry in roidb if is_valid(entry)]\n  num_after = len(filtered_roidb)\n  print('Filtered {} roidb entries: {} -> {}'.format(num - num_after,\n                                                     num, num_after))\n  return filtered_roidb\n\n\ndef train_net(network, imdb, roidb, valroidb, output_dir, tb_dir,\n              pretrained_model=None,\n              max_iters=40000):\n  \"\"\"Train a Fast R-CNN network.\"\"\"\n  roidb = filter_roidb(roidb)\n  valroidb = filter_roidb(valroidb)\n\n  tfconfig = tf.ConfigProto(allow_soft_placement=True)\n  tfconfig.gpu_options.allow_growth = True\n\n  with tf.Session(config=tfconfig) as sess:\n    sw = SolverWrapper(sess, network, imdb, roidb, valroidb, output_dir, tb_dir,\n                       pretrained_model=pretrained_model)\n    print('Solving...')\n    sw.train_model(sess, max_iters)\n    print('done solving')\n"
  },
  {
    "path": "lib/model/train_val.py~",
    "content": "# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen and Zheqi He\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nfrom model.config import cfg\nimport roi_data_layer.roidb as rdl_roidb\nfrom roi_data_layer.layer import RoIDataLayer\nfrom utils.timer import Timer\ntry:\n  import cPickle as pickle\nexcept ImportError:\n  import pickle\nimport numpy as np\nimport os\nimport sys\nimport glob\nimport time\n\nimport tensorflow as tf\nfrom tensorflow.python import pywrap_tensorflow\n\nclass SolverWrapper(object):\n  \"\"\"\n    A wrapper class for the training process\n  \"\"\"\n\n  def __init__(self, sess, network, imdb, roidb, valroidb, output_dir, tbdir, pretrained_model=None):\n    self.net = network\n    self.imdb = imdb\n    self.roidb = roidb\n    self.valroidb = valroidb\n    self.output_dir = output_dir\n    self.tbdir = tbdir\n    # Simply put '_val' at the end to save the summaries from the validation set\n    self.tbvaldir = tbdir + '_val'\n    if not os.path.exists(self.tbvaldir):\n      os.makedirs(self.tbvaldir)\n    self.pretrained_model = pretrained_model\n\n  def snapshot(self, sess, iter):\n    net = self.net\n\n    if not os.path.exists(self.output_dir):\n      os.makedirs(self.output_dir)\n\n    # Store the model snapshot\n    filename = cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_{:d}'.format(iter) + '.ckpt'\n    filename = os.path.join(self.output_dir, filename)\n    self.saver.save(sess, filename)\n    print('Wrote snapshot to: {:s}'.format(filename))\n\n    # Also store some meta information, random state, etc.\n    nfilename = cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_{:d}'.format(iter) + '.pkl'\n    nfilename = os.path.join(self.output_dir, nfilename)\n    # current state of numpy random\n    st0 = np.random.get_state()\n    # current position in the database\n    cur = self.data_layer._cur\n    # current shuffled indeces of the database\n    perm = self.data_layer._perm\n    # current position in the validation database\n    cur_val = self.data_layer_val._cur\n    # current shuffled indeces of the validation database\n    perm_val = self.data_layer_val._perm\n\n    # Dump the meta info\n    with open(nfilename, 'wb') as fid:\n      pickle.dump(st0, fid, pickle.HIGHEST_PROTOCOL)\n      pickle.dump(cur, fid, pickle.HIGHEST_PROTOCOL)\n      pickle.dump(perm, fid, pickle.HIGHEST_PROTOCOL)\n      pickle.dump(cur_val, fid, pickle.HIGHEST_PROTOCOL)\n      pickle.dump(perm_val, fid, pickle.HIGHEST_PROTOCOL)\n      pickle.dump(iter, fid, pickle.HIGHEST_PROTOCOL)\n\n    return filename, nfilename\n\n  def get_variables_in_checkpoint_file(self, file_name):\n    try:\n      reader = pywrap_tensorflow.NewCheckpointReader(file_name)\n      var_to_shape_map = reader.get_variable_to_shape_map()\n      return var_to_shape_map \n    except Exception as e:  # pylint: disable=broad-except\n      print(str(e))\n      if \"corrupted compressed block contents\" in str(e):\n        print(\"It's likely that your checkpoint file has been compressed \"\n              \"with SNAPPY.\")\n\n  def train_model(self, sess, max_iters):\n    # Build data layers for both training and validation set\n    self.data_layer = RoIDataLayer(self.roidb, self.imdb.num_classes)\n    self.data_layer_val = RoIDataLayer(self.valroidb, self.imdb.num_classes, random=True)\n\n    # Determine different scales for anchors, see paper\n    with sess.graph.as_default():\n      # Set the random seed for tensorflow\n      tf.set_random_seed(cfg.RNG_SEED)\n      # Build the main computation graph\n      layers = self.net.create_architecture(sess, 'TRAIN', self.imdb.num_classes, tag='default',\n                                            anchor_scales=cfg.ANCHOR_SCALES,\n                                            anchor_ratios=cfg.ANCHOR_RATIOS)\n      # Define the loss\n      loss = layers['total_loss']\n      # Set learning rate and momentum\n      lr = tf.Variable(cfg.TRAIN.LEARNING_RATE, trainable=False)\n      momentum = cfg.TRAIN.MOMENTUM\n      self.optimizer = tf.train.MomentumOptimizer(lr, momentum)\n\n      # Compute the gradients wrt the loss\n      gvs = self.optimizer.compute_gradients(loss)\n      # Double the gradient of the bias if set\n      if cfg.TRAIN.DOUBLE_BIAS:\n        final_gvs = []\n        with tf.variable_scope('Gradient_Mult') as scope:\n          for grad, var in gvs:\n            scale = 1.\n            if cfg.TRAIN.DOUBLE_BIAS and '/biases:' in var.name:\n              scale *= 2.\n            if not np.allclose(scale, 1.0):\n              grad = tf.multiply(grad, scale)\n            final_gvs.append((grad, var))\n        train_op = self.optimizer.apply_gradients(final_gvs)\n      else:\n        train_op = self.optimizer.apply_gradients(gvs)\n\n      # We will handle the snapshots ourselves\n      self.saver = tf.train.Saver(max_to_keep=100000)\n      # Write the train and validation information to tensorboard\n      self.writer = tf.summary.FileWriter(self.tbdir, sess.graph)\n      self.valwriter = tf.summary.FileWriter(self.tbvaldir)\n\n    # Find previous snapshots if there is any to restore from\n    sfiles = os.path.join(self.output_dir, cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_*.ckpt.meta')\n    sfiles = glob.glob(sfiles)\n    sfiles.sort(key=os.path.getmtime)\n    # Get the snapshot name in TensorFlow\n    redstr = '_iter_{:d}.'.format(cfg.TRAIN.STEPSIZE+1)\n    sfiles = [ss.replace('.meta', '') for ss in sfiles]\n    sfiles = [ss for ss in sfiles if redstr not in ss]\n\n    nfiles = os.path.join(self.output_dir, cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_*.pkl')\n    nfiles = glob.glob(nfiles)\n    nfiles.sort(key=os.path.getmtime)\n    nfiles = [nn for nn in nfiles if redstr not in nn]\n\n    lsf = len(sfiles)\n    assert len(nfiles) == lsf\n\n    np_paths = nfiles\n    ss_paths = sfiles\n\n    if lsf == 0:\n      # Fresh train directly from ImageNet weights\n      print('Loading initial model weights from {:s}'.format(self.pretrained_model))\n      variables = tf.global_variables()\n      # Initialize all variables first\n      sess.run(tf.variables_initializer(variables, name='init'))\n      var_keep_dic = self.get_variables_in_checkpoint_file(self.pretrained_model)\n      # Get the variables to restore, ignorizing the variables to fix\n      variables_to_restore = self.net.get_variables_to_restore(variables, var_keep_dic)\n\n      restorer = tf.train.Saver(variables_to_restore)\n      restorer.restore(sess, self.pretrained_model)\n      print('Loaded.')\n      # Need to fix the variables before loading, so that the RGB weights are changed to BGR\n      # For VGG16 it also changes the convolutional weights fc6 and fc7 to\n      # fully connected weights\n      self.net.fix_variables(sess, self.pretrained_model)\n      print('Fixed.')\n      sess.run(tf.assign(lr, cfg.TRAIN.LEARNING_RATE))\n      last_snapshot_iter = 0\n    else:\n      # Get the most recent snapshot and restore\n      ss_paths = [ss_paths[-1]]\n      np_paths = [np_paths[-1]]\n\n      print('Restorining model snapshots from {:s}'.format(sfiles[-1]))\n      self.saver.restore(sess, str(sfiles[-1]))\n      print('Restored.')\n      # Needs to restore the other hyperparameters/states for training, (TODO xinlei) I have\n      # tried my best to find the random states so that it can be recovered exactly\n      # However the Tensorflow state is currently not available\n      with open(str(nfiles[-1]), 'rb') as fid:\n        st0 = pickle.load(fid)\n        cur = pickle.load(fid)\n        perm = pickle.load(fid)\n        cur_val = pickle.load(fid)\n        perm_val = pickle.load(fid)\n        last_snapshot_iter = pickle.load(fid)\n\n        np.random.set_state(st0)\n        self.data_layer._cur = cur\n        self.data_layer._perm = perm\n        self.data_layer_val._cur = cur_val\n        self.data_layer_val._perm = perm_val\n\n        # Set the learning rate, only reduce once\n        if last_snapshot_iter > cfg.TRAIN.STEPSIZE:\n          sess.run(tf.assign(lr, cfg.TRAIN.LEARNING_RATE * cfg.TRAIN.GAMMA))\n        else:\n          sess.run(tf.assign(lr, cfg.TRAIN.LEARNING_RATE))\n\n    np.sum([np.prod(v.get_shape().as_list()) for v in tf.trainable_variables()])\n\n    timer = Timer()\n    iter = last_snapshot_iter + 1\n    last_summary_time = time.time()\n    while iter < max_iters + 1:\n      # Learning rate\n      if iter == cfg.TRAIN.STEPSIZE + 1:\n        # Add snapshot here before reducing the learning rate\n        self.snapshot(sess, iter)\n        sess.run(tf.assign(lr, cfg.TRAIN.LEARNING_RATE * cfg.TRAIN.GAMMA))\n\n      timer.tic()\n      # Get training data, one batch at a time\n      blobs = self.data_layer.forward()\n\n      now = time.time()\n      if now - last_summary_time > cfg.TRAIN.SUMMARY_INTERVAL:\n        # Compute the graph with summary\n        rpn_loss_cls, rpn_loss_box, loss_cls, loss_box, total_loss, summary = \\\n          self.net.train_step_with_summary(sess, blobs, train_op)\n        self.writer.add_summary(summary, float(iter))\n        # Also check the summary on the validation set\n        blobs_val = self.data_layer_val.forward()\n        summary_val = self.net.get_summary(sess, blobs_val)\n        self.valwriter.add_summary(summary_val, float(iter))\n        last_summary_time = now\n      else:\n        # Compute the graph without summary\n        rpn_loss_cls, rpn_loss_box, loss_cls, loss_box, total_loss = \\\n          self.net.train_step(sess, blobs, train_op)\n      timer.toc()\n\n      # Display training information\n      if iter % (cfg.TRAIN.DISPLAY) == 0:\n        print('iter: %d / %d, total loss: %.6f\\n >>> rpn_loss_cls: %.6f\\n '\n              '>>> rpn_loss_box: %.6f\\n >>> loss_cls: %.6f\\n >>> loss_box: %.6f\\n >>> lr: %f' % \\\n              (iter, max_iters, total_loss, rpn_loss_cls, rpn_loss_box, loss_cls, loss_box, lr.eval()))\n        print('speed: {:.3f}s / iter'.format(timer.average_time))\n\n      if iter % cfg.TRAIN.SNAPSHOT_ITERS == 0:\n        last_snapshot_iter = iter\n        snapshot_path, np_path = self.snapshot(sess, iter)\n        np_paths.append(np_path)\n        ss_paths.append(snapshot_path)\n\n        # Remove the old snapshots if there are too many\n        if len(np_paths) > cfg.TRAIN.SNAPSHOT_KEPT:\n          to_remove = len(np_paths) - cfg.TRAIN.SNAPSHOT_KEPT\n          for c in range(to_remove):\n            nfile = np_paths[0]\n            os.remove(str(nfile))\n            np_paths.remove(nfile)\n\n        if len(ss_paths) > cfg.TRAIN.SNAPSHOT_KEPT:\n          to_remove = len(ss_paths) - cfg.TRAIN.SNAPSHOT_KEPT\n          for c in range(to_remove):\n            sfile = ss_paths[0]\n            # To make the code compatible to earlier versions of Tensorflow,\n            # where the naming tradition for checkpoints are different\n            if os.path.exists(str(sfile)):\n              os.remove(str(sfile))\n            else:\n              os.remove(str(sfile + '.data-00000-of-00001'))\n              os.remove(str(sfile + '.index'))\n            sfile_meta = sfile + '.meta'\n            os.remove(str(sfile_meta))\n            ss_paths.remove(sfile)\n\n      iter += 1\n\n    if last_snapshot_iter != iter - 1:\n      self.snapshot(sess, iter - 1)\n\n    self.writer.close()\n    self.valwriter.close()\n\n\ndef get_training_roidb(imdb):\n  \"\"\"Returns a roidb (Region of Interest database) for use in training.\"\"\"\n  if cfg.TRAIN.USE_FLIPPED:\n    print('Appending horizontally-flipped training examples...')\n    imdb.append_flipped_images()\n    print('done')\n\n  print('Preparing training data...')\n  rdl_roidb.prepare_roidb(imdb)\n  print('done')\n\n  return imdb.roidb\n\n\ndef filter_roidb(roidb):\n  \"\"\"Remove roidb entries that have no usable RoIs.\"\"\"\n\n  def is_valid(entry):\n    # Valid images have:\n    #   (1) At least one foreground RoI OR\n    #   (2) At least one background RoI\n    overlaps = entry['max_overlaps']\n    # find boxes with sufficient overlap\n    fg_inds = np.where(overlaps >= cfg.TRAIN.FG_THRESH)[0]\n    # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)\n    bg_inds = np.where((overlaps < cfg.TRAIN.BG_THRESH_HI) &\n                       (overlaps >= cfg.TRAIN.BG_THRESH_LO))[0]\n    # image is only valid if such boxes exist\n    valid = len(fg_inds) > 0 or len(bg_inds) > 0\n    return valid\n\n  num = len(roidb)\n  filtered_roidb = [entry for entry in roidb if is_valid(entry)]\n  num_after = len(filtered_roidb)\n  print('Filtered {} roidb entries: {} -> {}'.format(num - num_after,\n                                                     num, num_after))\n  return filtered_roidb\n\n\ndef train_net(network, imdb, roidb, valroidb, output_dir, tb_dir,\n              pretrained_model=None,\n              max_iters=40000):\n  \"\"\"Train a Fast R-CNN network.\"\"\"\n  roidb = filter_roidb(roidb)\n  valroidb = filter_roidb(valroidb)\n\n  tfconfig = tf.ConfigProto(allow_soft_placement=True)\n  tfconfig.gpu_options.allow_growth = True\n\n  with tf.Session(config=tfconfig) as sess:\n    sw = SolverWrapper(sess, network, imdb, roidb, valroidb, output_dir, tb_dir,\n                       pretrained_model=pretrained_model)\n    print('Solving...')\n    sw.train_model(sess, max_iters)\n    print('done solving')\n"
  },
  {
    "path": "lib/nets/__init__.py",
    "content": ""
  },
  {
    "path": "lib/nets/network.py",
    "content": "# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport tensorflow as tf\nimport tensorflow.contrib.slim as slim\nfrom tensorflow.contrib.slim import losses\nfrom tensorflow.contrib.slim import arg_scope\n\nimport numpy as np\n\nfrom layer_utils.snippets import generate_anchors_pre\nfrom layer_utils.proposal_layer import proposal_layer\nfrom layer_utils.proposal_top_layer import proposal_top_layer\nfrom layer_utils.anchor_target_layer import anchor_target_layer\nfrom layer_utils.proposal_target_layer import proposal_target_layer\n\nfrom model.config import cfg\n\nclass Network(object):\n  def __init__(self, batch_size=1):\n    self._feat_stride = [16, ]\n    self._feat_compress = [1. / 16., ]\n    self._batch_size = batch_size\n    self._predictions = {}\n    self._losses = {}\n    self._anchor_targets = {}\n    self._proposal_targets = {}\n    self._layers = {}\n    self._act_summaries = []\n    self._score_summaries = {}\n    self._train_summaries = []\n    self._event_summaries = {}\n    self._variables_to_fix = {}\n\n  def _add_image_summary(self, image, boxes):\n    # add back mean\n    image += cfg.PIXEL_MEANS\n    # bgr to rgb (opencv uses bgr)\n    channels = tf.unstack (image, axis=-1)\n    image    = tf.stack ([channels[2], channels[1], channels[0]], axis=-1)\n    # dims for normalization\n    width  = tf.to_float(tf.shape(image)[2])\n    height = tf.to_float(tf.shape(image)[1])\n    # from [x1, y1, x2, y2, cls] to normalized [y1, x1, y1, x1]\n    cols = tf.unstack(boxes, axis=1)\n    boxes = tf.stack([cols[1] / height,\n                      cols[0] / width,\n                      cols[3] / height,\n                      cols[2] / width], axis=1)\n    # add batch dimension (assume batch_size==1)\n    assert image.get_shape()[0] == 1\n    boxes = tf.expand_dims(boxes, dim=0)\n    image = tf.image.draw_bounding_boxes(image, boxes)\n    \n    return tf.summary.image('ground_truth', image)\n\n  def _add_act_summary(self, tensor):\n    tf.summary.histogram('ACT/' + tensor.op.name + '/activations', tensor)\n    tf.summary.scalar('ACT/' + tensor.op.name + '/zero_fraction',\n                      tf.nn.zero_fraction(tensor))\n\n  def _add_score_summary(self, key, tensor):\n    tf.summary.histogram('SCORE/' + tensor.op.name + '/' + key + '/scores', tensor)\n\n  def _add_train_summary(self, var):\n    tf.summary.histogram('TRAIN/' + var.op.name, var)\n\n  def _reshape_layer(self, bottom, num_dim, name):\n    input_shape = tf.shape(bottom)\n    with tf.variable_scope(name) as scope:\n      # change the channel to the caffe format\n      to_caffe = tf.transpose(bottom, [0, 3, 1, 2])\n      # then force it to have channel 2\n      reshaped = tf.reshape(to_caffe,\n                            tf.concat(axis=0, values=[[self._batch_size], [num_dim, -1], [input_shape[2]]]))\n      # then swap the channel back\n      to_tf = tf.transpose(reshaped, [0, 2, 3, 1])\n      return to_tf\n\n  def _softmax_layer(self, bottom, name):\n    if name == 'rpn_cls_prob_reshape':\n      input_shape = tf.shape(bottom)\n      bottom_reshaped = tf.reshape(bottom, [-1, input_shape[-1]])\n      reshaped_score = tf.nn.softmax(bottom_reshaped, name=name)\n      return tf.reshape(reshaped_score, input_shape)\n    return tf.nn.softmax(bottom, name=name)\n\n  def _proposal_top_layer(self, rpn_cls_prob, rpn_bbox_pred, name):\n    with tf.variable_scope(name) as scope:\n      rois, rpn_scores = tf.py_func(proposal_top_layer,\n                                    [rpn_cls_prob, rpn_bbox_pred, self._im_info,\n                                     self._feat_stride, self._anchors, self._num_anchors],\n                                    [tf.float32, tf.float32])\n      rois.set_shape([cfg.TEST.RPN_TOP_N, 5])\n      rpn_scores.set_shape([cfg.TEST.RPN_TOP_N, 1])\n\n    return rois, rpn_scores\n\n  def _proposal_layer(self, rpn_cls_prob, rpn_bbox_pred, name):\n    with tf.variable_scope(name) as scope:\n      rois, rpn_scores = tf.py_func(proposal_layer,\n                                    [rpn_cls_prob, rpn_bbox_pred, self._im_info, self._mode,\n                                     self._feat_stride, self._anchors, self._num_anchors],\n                                    [tf.float32, tf.float32])\n      rois.set_shape([None, 5])\n      rpn_scores.set_shape([None, 1])\n\n    return rois, rpn_scores\n\n  # Only use it if you have roi_pooling op written in tf.image\n  def _roi_pool_layer(self, bootom, rois, name):\n    with tf.variable_scope(name) as scope:\n      return tf.image.roi_pooling(bootom, rois,\n                                  pooled_height=cfg.POOLING_SIZE,\n                                  pooled_width=cfg.POOLING_SIZE,\n                                  spatial_scale=1. / 16.)[0]\n\n  def _crop_pool_layer(self, bottom, rois, name):\n    with tf.variable_scope(name) as scope:\n      batch_ids = tf.squeeze(tf.slice(rois, [0, 0], [-1, 1], name=\"batch_id\"), [1])\n      # Get the normalized coordinates of bboxes\n      bottom_shape = tf.shape(bottom)\n      height = (tf.to_float(bottom_shape[1]) - 1.) * np.float32(self._feat_stride[0])\n      width = (tf.to_float(bottom_shape[2]) - 1.) * np.float32(self._feat_stride[0])\n      x1 = tf.slice(rois, [0, 1], [-1, 1], name=\"x1\") / width\n      y1 = tf.slice(rois, [0, 2], [-1, 1], name=\"y1\") / height\n      x2 = tf.slice(rois, [0, 3], [-1, 1], name=\"x2\") / width\n      y2 = tf.slice(rois, [0, 4], [-1, 1], name=\"y2\") / height\n      # Won't be backpropagated to rois anyway, but to save time\n      bboxes = tf.stop_gradient(tf.concat([y1, x1, y2, x2], axis=1))\n      pre_pool_size = cfg.POOLING_SIZE * 2\n      crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [pre_pool_size, pre_pool_size], name=\"crops\")\n\n    return slim.max_pool2d(crops, [2, 2], padding='SAME')\n\n  def _dropout_layer(self, bottom, name, ratio=0.5):\n    return tf.nn.dropout(bottom, ratio, name=name)\n\n  def _anchor_target_layer(self, rpn_cls_score, name):\n    with tf.variable_scope(name) as scope:\n      rpn_labels, rpn_bbox_targets, rpn_bbox_inside_weights, rpn_bbox_outside_weights = tf.py_func(\n        anchor_target_layer,\n        [rpn_cls_score, self._gt_boxes, self._im_info, self._feat_stride, self._anchors, self._num_anchors],\n        [tf.float32, tf.float32, tf.float32, tf.float32])\n\n      rpn_labels.set_shape([1, 1, None, None])\n      rpn_bbox_targets.set_shape([1, None, None, self._num_anchors * 4])\n      rpn_bbox_inside_weights.set_shape([1, None, None, self._num_anchors * 4])\n      rpn_bbox_outside_weights.set_shape([1, None, None, self._num_anchors * 4])\n\n      rpn_labels = tf.to_int32(rpn_labels, name=\"to_int32\")\n      self._anchor_targets['rpn_labels'] = rpn_labels\n      self._anchor_targets['rpn_bbox_targets'] = rpn_bbox_targets\n      self._anchor_targets['rpn_bbox_inside_weights'] = rpn_bbox_inside_weights\n      self._anchor_targets['rpn_bbox_outside_weights'] = rpn_bbox_outside_weights\n\n      self._score_summaries.update(self._anchor_targets)\n\n    return rpn_labels\n\n  def _proposal_target_layer(self, rois, roi_scores, name):\n    with tf.variable_scope(name) as scope:\n      rois, roi_scores, labels, bbox_targets, bbox_inside_weights, bbox_outside_weights = tf.py_func(\n        proposal_target_layer,\n        [rois, roi_scores, self._gt_boxes, self._num_classes],\n        [tf.float32, tf.float32, tf.float32, tf.float32, tf.float32, tf.float32])\n\n      rois.set_shape([cfg.TRAIN.BATCH_SIZE, 5])\n      roi_scores.set_shape([cfg.TRAIN.BATCH_SIZE])\n      labels.set_shape([cfg.TRAIN.BATCH_SIZE, 1])\n      bbox_targets.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])\n      bbox_inside_weights.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])\n      bbox_outside_weights.set_shape([cfg.TRAIN.BATCH_SIZE, self._num_classes * 4])\n\n      self._proposal_targets['rois'] = rois\n      self._proposal_targets['labels'] = tf.to_int32(labels, name=\"to_int32\")\n      self._proposal_targets['bbox_targets'] = bbox_targets\n      self._proposal_targets['bbox_inside_weights'] = bbox_inside_weights\n      self._proposal_targets['bbox_outside_weights'] = bbox_outside_weights\n\n      self._score_summaries.update(self._proposal_targets)\n\n      return rois, roi_scores\n\n  def _anchor_component(self):\n    with tf.variable_scope('ANCHOR_' + self._tag) as scope:\n      # just to get the shape right\n      height = tf.to_int32(tf.ceil(self._im_info[0, 0] / np.float32(self._feat_stride[0])))\n      width = tf.to_int32(tf.ceil(self._im_info[0, 1] / np.float32(self._feat_stride[0])))\n      anchors, anchor_length = tf.py_func(generate_anchors_pre,\n                                          [height, width,\n                                           self._feat_stride, self._anchor_scales, self._anchor_ratios],\n                                          [tf.float32, tf.int32], name=\"generate_anchors\")\n      anchors.set_shape([None, 4])\n      anchor_length.set_shape([])\n      self._anchors = anchors\n      self._anchor_length = anchor_length\n\n  def build_network(self, sess, is_training=True):\n    raise NotImplementedError\n\n  def _smooth_l1_loss(self, bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights, sigma=1.0, dim=[1]):\n    sigma_2 = sigma ** 2\n    box_diff = bbox_pred - bbox_targets\n    in_box_diff = bbox_inside_weights * box_diff\n    abs_in_box_diff = tf.abs(in_box_diff)\n    smoothL1_sign = tf.stop_gradient(tf.to_float(tf.less(abs_in_box_diff, 1. / sigma_2)))\n    in_loss_box = tf.pow(in_box_diff, 2) * (sigma_2 / 2.) * smoothL1_sign \\\n                  + (abs_in_box_diff - (0.5 / sigma_2)) * (1. - smoothL1_sign)\n    out_loss_box = bbox_outside_weights * in_loss_box\n    loss_box = tf.reduce_mean(tf.reduce_sum(\n      out_loss_box,\n      axis=dim\n    ))\n    return loss_box\n\n  def _add_losses(self, sigma_rpn=3.0):\n    with tf.variable_scope('loss_' + self._tag) as scope:\n      # RPN, class loss\n      rpn_cls_score = tf.reshape(self._predictions['rpn_cls_score_reshape'], [-1, 2])\n      rpn_label = tf.reshape(self._anchor_targets['rpn_labels'], [-1])\n      rpn_select = tf.where(tf.not_equal(rpn_label, -1))\n      rpn_cls_score = tf.reshape(tf.gather(rpn_cls_score, rpn_select), [-1, 2])\n      rpn_label = tf.reshape(tf.gather(rpn_label, rpn_select), [-1])\n      rpn_cross_entropy = tf.reduce_mean(\n        tf.nn.sparse_softmax_cross_entropy_with_logits(logits=rpn_cls_score, labels=rpn_label))\n\n      # RPN, bbox loss\n      rpn_bbox_pred = self._predictions['rpn_bbox_pred']\n      rpn_bbox_targets = self._anchor_targets['rpn_bbox_targets']\n      rpn_bbox_inside_weights = self._anchor_targets['rpn_bbox_inside_weights']\n      rpn_bbox_outside_weights = self._anchor_targets['rpn_bbox_outside_weights']\n\n      rpn_loss_box = self._smooth_l1_loss(rpn_bbox_pred, rpn_bbox_targets, rpn_bbox_inside_weights,\n                                          rpn_bbox_outside_weights, sigma=sigma_rpn, dim=[1, 2, 3])\n\n      # RCNN, class loss\n      cls_score = self._predictions[\"cls_score\"]\n      label = tf.reshape(self._proposal_targets[\"labels\"], [-1])\n\n      cross_entropy = tf.reduce_mean(\n        tf.nn.sparse_softmax_cross_entropy_with_logits(\n          logits=tf.reshape(cls_score, [-1, self._num_classes]), labels=label))\n\n      # RCNN, bbox loss\n      bbox_pred = self._predictions['bbox_pred']\n      bbox_targets = self._proposal_targets['bbox_targets']\n      bbox_inside_weights = self._proposal_targets['bbox_inside_weights']\n      bbox_outside_weights = self._proposal_targets['bbox_outside_weights']\n\n      loss_box = self._smooth_l1_loss(bbox_pred, bbox_targets, bbox_inside_weights, bbox_outside_weights)\n\n      self._losses['cross_entropy'] = cross_entropy\n      self._losses['loss_box'] = loss_box\n      self._losses['rpn_cross_entropy'] = rpn_cross_entropy\n      self._losses['rpn_loss_box'] = rpn_loss_box\n\n      loss = cross_entropy + loss_box + rpn_cross_entropy + rpn_loss_box\n      self._losses['total_loss'] = loss\n\n      self._event_summaries.update(self._losses)\n\n    return loss\n\n  def create_architecture(self, sess, mode, num_classes, tag=None,\n                          anchor_scales=(8, 16, 32), anchor_ratios=(0.5, 1, 2)):\n    self._image = tf.placeholder(tf.float32, shape=[self._batch_size, None, None, 3])\n    self._im_info = tf.placeholder(tf.float32, shape=[self._batch_size, 3])\n    self._gt_boxes = tf.placeholder(tf.float32, shape=[None, 5])\n    self._tag = tag\n\n    self._num_classes = num_classes\n    self._mode = mode\n    self._anchor_scales = anchor_scales\n    self._num_scales = len(anchor_scales)\n\n    self._anchor_ratios = anchor_ratios\n    self._num_ratios = len(anchor_ratios)\n\n    self._num_anchors = self._num_scales * self._num_ratios\n\n    training = mode == 'TRAIN'\n    testing = mode == 'TEST'\n\n    assert tag != None\n\n    # handle most of the regularizers here\n    weights_regularizer = tf.contrib.layers.l2_regularizer(cfg.TRAIN.WEIGHT_DECAY)\n    if cfg.TRAIN.BIAS_DECAY:\n      biases_regularizer = weights_regularizer\n    else:\n      biases_regularizer = tf.no_regularizer\n\n    # list as many types of layers as possible, even if they are not used now\n    with arg_scope([slim.conv2d, slim.conv2d_in_plane, \\\n                    slim.conv2d_transpose, slim.separable_conv2d, slim.fully_connected], \n                    weights_regularizer=weights_regularizer,\n                    biases_regularizer=biases_regularizer, \n                    biases_initializer=tf.constant_initializer(0.0)): \n      rois, cls_prob, bbox_pred = self.build_network(sess, training)\n\n    layers_to_output = {'rois': rois}\n    layers_to_output.update(self._predictions)\n\n    for var in tf.trainable_variables():\n      self._train_summaries.append(var)\n\n    if mode == 'TEST':\n      stds = np.tile(np.array(cfg.TRAIN.BBOX_NORMALIZE_STDS), (self._num_classes))\n      means = np.tile(np.array(cfg.TRAIN.BBOX_NORMALIZE_MEANS), (self._num_classes))\n      self._predictions[\"bbox_pred\"] *= stds\n      self._predictions[\"bbox_pred\"] += means\n    else:\n      self._add_losses()\n      layers_to_output.update(self._losses)\n\n    val_summaries = []\n    with tf.device(\"/cpu:0\"):\n      val_summaries.append(self._add_image_summary(self._image, self._gt_boxes))\n      for key, var in self._event_summaries.items():\n        val_summaries.append(tf.summary.scalar(key, var))\n      for key, var in self._score_summaries.items():\n        self._add_score_summary(key, var)\n      for var in self._act_summaries:\n        self._add_act_summary(var)\n      for var in self._train_summaries:\n        self._add_train_summary(var)\n\n    self._summary_op = tf.summary.merge_all()\n    if not testing:\n      self._summary_op_val = tf.summary.merge(val_summaries)\n\n    return layers_to_output\n\n  def get_variables_to_restore(self, variables, var_keep_dic):\n    raise NotImplementedError\n\n  def fix_variables(self, sess, pretrained_model):\n    raise NotImplementedError\n\n  # Extract the head feature maps, for example for vgg16 it is conv5_3\n  # only useful during testing mode\n  def extract_head(self, sess, image):\n    feed_dict = {self._image: image}\n    feat = sess.run(self._layers[\"head\"], feed_dict=feed_dict)\n    return feat\n\n  # only useful during testing mode\n  def test_image(self, sess, image, im_info):\n    feed_dict = {self._image: image,\n                 self._im_info: im_info}\n    cls_score, cls_prob, bbox_pred, rois = sess.run([self._predictions[\"cls_score\"],\n                                                     self._predictions['cls_prob'],\n                                                     self._predictions['bbox_pred'],\n                                                     self._predictions['rois']],\n                                                    feed_dict=feed_dict)\n    return cls_score, cls_prob, bbox_pred, rois\n\n  def get_summary(self, sess, blobs):\n    feed_dict = {self._image: blobs['data'], self._im_info: blobs['im_info'],\n                 self._gt_boxes: blobs['gt_boxes']}\n    summary = sess.run(self._summary_op_val, feed_dict=feed_dict)\n\n    return summary\n\n  def train_step(self, sess, blobs, train_op):\n    feed_dict = {self._image: blobs['data'], self._im_info: blobs['im_info'],\n                 self._gt_boxes: blobs['gt_boxes']}\n    rpn_loss_cls, rpn_loss_box, loss_cls, loss_box, loss, _ = sess.run([self._losses[\"rpn_cross_entropy\"],\n                                                                        self._losses['rpn_loss_box'],\n                                                                        self._losses['cross_entropy'],\n                                                                        self._losses['loss_box'],\n                                                                        self._losses['total_loss'],\n                                                                        train_op],\n                                                                       feed_dict=feed_dict)\n    return rpn_loss_cls, rpn_loss_box, loss_cls, loss_box, loss\n\n  def train_step_with_summary(self, sess, blobs, train_op):\n    feed_dict = {self._image: blobs['data'], self._im_info: blobs['im_info'],\n                 self._gt_boxes: blobs['gt_boxes']}\n    rpn_loss_cls, rpn_loss_box, loss_cls, loss_box, loss, summary, _ = sess.run([self._losses[\"rpn_cross_entropy\"],\n                                                                                 self._losses['rpn_loss_box'],\n                                                                                 self._losses['cross_entropy'],\n                                                                                 self._losses['loss_box'],\n                                                                                 self._losses['total_loss'],\n                                                                                 self._summary_op,\n                                                                                 train_op],\n                                                                                feed_dict=feed_dict)\n    return rpn_loss_cls, rpn_loss_box, loss_cls, loss_box, loss, summary\n\n  def train_step_no_return(self, sess, blobs, train_op):\n    feed_dict = {self._image: blobs['data'], self._im_info: blobs['im_info'],\n                 self._gt_boxes: blobs['gt_boxes']}\n    sess.run([train_op], feed_dict=feed_dict)\n\n"
  },
  {
    "path": "lib/nets/resnet_v1.py",
    "content": "# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Zheqi He and Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport tensorflow as tf\nimport tensorflow.contrib.slim as slim\nfrom tensorflow.contrib.slim import losses\nfrom tensorflow.contrib.slim import arg_scope\nfrom tensorflow.contrib.slim.python.slim.nets import resnet_utils\nfrom tensorflow.contrib.slim.python.slim.nets import resnet_v1\nimport numpy as np\n\nfrom nets.network import Network\nfrom tensorflow.python.framework import ops\nfrom tensorflow.contrib.layers.python.layers import regularizers\nfrom tensorflow.python.ops import nn_ops\nfrom tensorflow.contrib.layers.python.layers import initializers\nfrom tensorflow.contrib.layers.python.layers import layers\nfrom model.config import cfg\n\ndef resnet_arg_scope(is_training=True,\n                     weight_decay=cfg.TRAIN.WEIGHT_DECAY,\n                     batch_norm_decay=0.997,\n                     batch_norm_epsilon=1e-5,\n                     batch_norm_scale=True):\n  batch_norm_params = {\n    # NOTE 'is_training' here does not work because inside resnet it gets reset:\n    # https://github.com/tensorflow/models/blob/master/slim/nets/resnet_v1.py#L187\n    'is_training': False,\n    'decay': batch_norm_decay,\n    'epsilon': batch_norm_epsilon,\n    'scale': batch_norm_scale,\n    'trainable': cfg.RESNET.BN_TRAIN,\n    'updates_collections': ops.GraphKeys.UPDATE_OPS\n  }\n\n  with arg_scope(\n      [slim.conv2d],\n      weights_regularizer=regularizers.l2_regularizer(weight_decay),\n      weights_initializer=initializers.variance_scaling_initializer(),\n      trainable=is_training,\n      activation_fn=nn_ops.relu,\n      normalizer_fn=layers.batch_norm,\n      normalizer_params=batch_norm_params):\n    with arg_scope([layers.batch_norm], **batch_norm_params) as arg_sc:\n      return arg_sc\n\nclass resnetv1(Network):\n  def __init__(self, batch_size=1, num_layers=50):\n    Network.__init__(self, batch_size=batch_size)\n    self._num_layers = num_layers\n    self._resnet_scope = 'resnet_v1_%d' % num_layers\n\n  def _crop_pool_layer(self, bottom, rois, name):\n    with tf.variable_scope(name) as scope:\n      batch_ids = tf.squeeze(tf.slice(rois, [0, 0], [-1, 1], name=\"batch_id\"), [1])\n      # Get the normalized coordinates of bboxes\n      bottom_shape = tf.shape(bottom)\n      height = (tf.to_float(bottom_shape[1]) - 1.) * np.float32(self._feat_stride[0])\n      width = (tf.to_float(bottom_shape[2]) - 1.) * np.float32(self._feat_stride[0])\n      x1 = tf.slice(rois, [0, 1], [-1, 1], name=\"x1\") / width\n      y1 = tf.slice(rois, [0, 2], [-1, 1], name=\"y1\") / height\n      x2 = tf.slice(rois, [0, 3], [-1, 1], name=\"x2\") / width\n      y2 = tf.slice(rois, [0, 4], [-1, 1], name=\"y2\") / height\n      # Won't be backpropagated to rois anyway, but to save time\n      bboxes = tf.stop_gradient(tf.concat([y1, x1, y2, x2], 1))\n      if cfg.RESNET.MAX_POOL:\n        pre_pool_size = cfg.POOLING_SIZE * 2\n        crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [pre_pool_size, pre_pool_size],\n                                         name=\"crops\")\n        crops = slim.max_pool2d(crops, [2, 2], padding='SAME')\n      else:\n        crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [cfg.POOLING_SIZE, cfg.POOLING_SIZE],\n                                         name=\"crops\")\n    return crops\n\n  # Do the first few layers manually, because 'SAME' padding can behave inconsistently\n  # for images of different sizes: sometimes 0, sometimes 1\n  def build_base(self):\n    with tf.variable_scope(self._resnet_scope, self._resnet_scope):\n      net = resnet_utils.conv2d_same(self._image, 64, 7, stride=2, scope='conv1')\n      net = tf.pad(net, [[0, 0], [1, 1], [1, 1], [0, 0]])\n      net = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID', scope='pool1')\n\n    return net\n\n  def build_network(self, sess, is_training=True):\n    # select initializers\n    if cfg.TRAIN.TRUNCATED:\n      initializer = tf.truncated_normal_initializer(mean=0.0, stddev=0.01)\n      initializer_bbox = tf.truncated_normal_initializer(mean=0.0, stddev=0.001)\n    else:\n      initializer = tf.random_normal_initializer(mean=0.0, stddev=0.01)\n      initializer_bbox = tf.random_normal_initializer(mean=0.0, stddev=0.001)\n    bottleneck = resnet_v1.bottleneck\n    # choose different blocks for different number of layers\n    if self._num_layers == 50:\n      blocks = [\n        resnet_utils.Block('block1', bottleneck,\n                           [(256, 64, 1)] * 2 + [(256, 64, 2)]),\n        resnet_utils.Block('block2', bottleneck,\n                           [(512, 128, 1)] * 3 + [(512, 128, 2)]),\n        # Use stride-1 for the last conv4 layer\n        resnet_utils.Block('block3', bottleneck,\n                           [(1024, 256, 1)] * 5 + [(1024, 256, 1)]),\n        resnet_utils.Block('block4', bottleneck, [(2048, 512, 1)] * 3)\n      ]\n    elif self._num_layers == 101:\n      blocks = [\n        resnet_utils.Block('block1', bottleneck,\n                           [(256, 64, 1)] * 2 + [(256, 64, 2)]),\n        resnet_utils.Block('block2', bottleneck,\n                           [(512, 128, 1)] * 3 + [(512, 128, 2)]),\n        # Use stride-1 for the last conv4 layer\n        resnet_utils.Block('block3', bottleneck,\n                           [(1024, 256, 1)] * 22 + [(1024, 256, 1)]),\n        resnet_utils.Block('block4', bottleneck, [(2048, 512, 1)] * 3)\n      ]\n    elif self._num_layers == 152:\n      blocks = [\n        resnet_utils.Block('block1', bottleneck,\n                           [(256, 64, 1)] * 2 + [(256, 64, 2)]),\n        resnet_utils.Block('block2', bottleneck,\n                           [(512, 128, 1)] * 7 + [(512, 128, 2)]),\n        # Use stride-1 for the last conv4 layer\n        resnet_utils.Block('block3', bottleneck,\n                           [(1024, 256, 1)] * 35 + [(1024, 256, 1)]),\n        resnet_utils.Block('block4', bottleneck, [(2048, 512, 1)] * 3)\n      ]\n    else:\n      # other numbers are not supported\n      raise NotImplementedError\n\n    assert (0 <= cfg.RESNET.FIXED_BLOCKS < 4)\n    if cfg.RESNET.FIXED_BLOCKS == 3:\n      with slim.arg_scope(resnet_arg_scope(is_training=False)):\n        net = self.build_base()\n        net_conv4, _ = resnet_v1.resnet_v1(net,\n                                           blocks[0:cfg.RESNET.FIXED_BLOCKS],\n                                           global_pool=False,\n                                           include_root_block=False,\n                                           scope=self._resnet_scope)\n    elif cfg.RESNET.FIXED_BLOCKS > 0:\n      with slim.arg_scope(resnet_arg_scope(is_training=False)):\n        net = self.build_base()\n        net, _ = resnet_v1.resnet_v1(net,\n                                     blocks[0:cfg.RESNET.FIXED_BLOCKS],\n                                     global_pool=False,\n                                     include_root_block=False,\n                                     scope=self._resnet_scope)\n\n      with slim.arg_scope(resnet_arg_scope(is_training=is_training)):\n        net_conv4, _ = resnet_v1.resnet_v1(net,\n                                           blocks[cfg.RESNET.FIXED_BLOCKS:-1],\n                                           global_pool=False,\n                                           include_root_block=False,\n                                           scope=self._resnet_scope)\n    else:  # cfg.RESNET.FIXED_BLOCKS == 0\n      with slim.arg_scope(resnet_arg_scope(is_training=is_training)):\n        net = self.build_base()\n        net_conv4, _ = resnet_v1.resnet_v1(net,\n                                           blocks[0:-1],\n                                           global_pool=False,\n                                           include_root_block=False,\n                                           scope=self._resnet_scope)\n\n    self._act_summaries.append(net_conv4)\n    self._layers['head'] = net_conv4\n    with tf.variable_scope(self._resnet_scope, self._resnet_scope):\n      # build the anchors for the image\n      self._anchor_component()\n\n      # rpn\n      rpn = slim.conv2d(net_conv4, 512, [3, 3], trainable=is_training, weights_initializer=initializer,\n                        scope=\"rpn_conv/3x3\")\n      self._act_summaries.append(rpn)\n      rpn_cls_score = slim.conv2d(rpn, self._num_anchors * 2, [1, 1], trainable=is_training,\n                                  weights_initializer=initializer,\n                                  padding='VALID', activation_fn=None, scope='rpn_cls_score')\n      # change it so that the score has 2 as its channel size\n      rpn_cls_score_reshape = self._reshape_layer(rpn_cls_score, 2, 'rpn_cls_score_reshape')\n      rpn_cls_prob_reshape = self._softmax_layer(rpn_cls_score_reshape, \"rpn_cls_prob_reshape\")\n      rpn_cls_prob = self._reshape_layer(rpn_cls_prob_reshape, self._num_anchors * 2, \"rpn_cls_prob\")\n      rpn_bbox_pred = slim.conv2d(rpn, self._num_anchors * 4, [1, 1], trainable=is_training,\n                                  weights_initializer=initializer,\n                                  padding='VALID', activation_fn=None, scope='rpn_bbox_pred')\n      if is_training:\n        rois, roi_scores = self._proposal_layer(rpn_cls_prob, rpn_bbox_pred, \"rois\")\n        rpn_labels = self._anchor_target_layer(rpn_cls_score, \"anchor\")\n        # Try to have a determinestic order for the computing graph, for reproducibility\n        with tf.control_dependencies([rpn_labels]):\n          rois, _ = self._proposal_target_layer(rois, roi_scores, \"rpn_rois\")\n      else:\n        if cfg.TEST.MODE == 'nms':\n          rois, _ = self._proposal_layer(rpn_cls_prob, rpn_bbox_pred, \"rois\")\n        elif cfg.TEST.MODE == 'top':\n          rois, _ = self._proposal_top_layer(rpn_cls_prob, rpn_bbox_pred, \"rois\")\n        else:\n          raise NotImplementedError\n\n      # rcnn\n      if cfg.POOLING_MODE == 'crop':\n        pool5 = self._crop_pool_layer(net_conv4, rois, \"pool5\")\n      else:\n        raise NotImplementedError\n\n    with slim.arg_scope(resnet_arg_scope(is_training=is_training)):\n      fc7, _ = resnet_v1.resnet_v1(pool5,\n                                   blocks[-1:],\n                                   global_pool=False,\n                                   include_root_block=False,\n                                   scope=self._resnet_scope)\n\n    with tf.variable_scope(self._resnet_scope, self._resnet_scope):\n      # Average pooling done by reduce_mean\n      fc7 = tf.reduce_mean(fc7, axis=[1, 2])\n      cls_score = slim.fully_connected(fc7, self._num_classes, weights_initializer=initializer,\n                                       trainable=is_training, activation_fn=None, scope='cls_score')\n      cls_prob = self._softmax_layer(cls_score, \"cls_prob\")\n      bbox_pred = slim.fully_connected(fc7, self._num_classes * 4, weights_initializer=initializer_bbox,\n                                       trainable=is_training,\n                                       activation_fn=None, scope='bbox_pred')\n    self._predictions[\"rpn_cls_score\"] = rpn_cls_score\n    self._predictions[\"rpn_cls_score_reshape\"] = rpn_cls_score_reshape\n    self._predictions[\"rpn_cls_prob\"] = rpn_cls_prob\n    self._predictions[\"rpn_bbox_pred\"] = rpn_bbox_pred\n    self._predictions[\"cls_score\"] = cls_score\n    self._predictions[\"cls_prob\"] = cls_prob\n    self._predictions[\"bbox_pred\"] = bbox_pred\n    self._predictions[\"rois\"] = rois\n\n    self._score_summaries.update(self._predictions)\n\n    return rois, cls_prob, bbox_pred\n\n  def get_variables_to_restore(self, variables, var_keep_dic):\n    variables_to_restore = []\n\n    for v in variables:\n      # exclude the first conv layer to swap RGB to BGR\n      if v.name == (self._resnet_scope + '/conv1/weights:0'):\n        self._variables_to_fix[v.name] = v\n        continue\n      if v.name.split(':')[0] in var_keep_dic:\n        #if v.name == 'resnet_v1_50/cls_score/weights/Momentum:0' \\\n        #  or v.name == 'resnet_v1_50/cls_score/biases/Momentum:0' \\\n        #  or v.name == 'resnet_v1_50/bbox_pred/weights/Momentum:0' \\\n        #  or v.name == 'resnet_v1_50/bbox_pred/biases/Momentum:0' \\\n        #  or v.name == 'resnet_v1_50/cls_score/weights:0' \\\n        #  or v.name == 'resnet_v1_50/cls_score/biases:0' \\\n        #  or v.name == 'resnet_v1_50/bbox_pred/weights:0' \\\n        #  or v.name == 'resnet_v1_50/bbox_pred/biases:0':\n        #  continue\n\n        print('Varibles restored: %s' % v.name)\n        variables_to_restore.append(v)\n\n    return variables_to_restore\n\n  def fix_variables(self, sess, pretrained_model):\n    print('Fix Resnet V1 layers..')\n    with tf.variable_scope('Fix_Resnet_V1') as scope:\n      with tf.device(\"/cpu:0\"):\n        # fix RGB to BGR\n        conv1_rgb = tf.get_variable(\"conv1_rgb\", [7, 7, 3, 64], trainable=False)\n        restorer_fc = tf.train.Saver({self._resnet_scope + \"/conv1/weights\": conv1_rgb})\n        restorer_fc.restore(sess, pretrained_model)\n\n        sess.run(tf.assign(self._variables_to_fix[self._resnet_scope + '/conv1/weights:0'], \n                           tf.reverse(conv1_rgb, [2])))\n"
  },
  {
    "path": "lib/nets/resnet_v1.py~",
    "content": "# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Zheqi He and Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport tensorflow as tf\nimport tensorflow.contrib.slim as slim\nfrom tensorflow.contrib.slim import losses\nfrom tensorflow.contrib.slim import arg_scope\nfrom tensorflow.contrib.slim.python.slim.nets import resnet_utils\nfrom tensorflow.contrib.slim.python.slim.nets import resnet_v1\nimport numpy as np\n\nfrom nets.network import Network\nfrom tensorflow.python.framework import ops\nfrom tensorflow.contrib.layers.python.layers import regularizers\nfrom tensorflow.python.ops import nn_ops\nfrom tensorflow.contrib.layers.python.layers import initializers\nfrom tensorflow.contrib.layers.python.layers import layers\nfrom model.config import cfg\n\ndef resnet_arg_scope(is_training=True,\n                     weight_decay=cfg.TRAIN.WEIGHT_DECAY,\n                     batch_norm_decay=0.997,\n                     batch_norm_epsilon=1e-5,\n                     batch_norm_scale=True):\n  batch_norm_params = {\n    # NOTE 'is_training' here does not work because inside resnet it gets reset:\n    # https://github.com/tensorflow/models/blob/master/slim/nets/resnet_v1.py#L187\n    'is_training': False,\n    'decay': batch_norm_decay,\n    'epsilon': batch_norm_epsilon,\n    'scale': batch_norm_scale,\n    'trainable': cfg.RESNET.BN_TRAIN,\n    'updates_collections': ops.GraphKeys.UPDATE_OPS\n  }\n\n  with arg_scope(\n      [slim.conv2d],\n      weights_regularizer=regularizers.l2_regularizer(weight_decay),\n      weights_initializer=initializers.variance_scaling_initializer(),\n      trainable=is_training,\n      activation_fn=nn_ops.relu,\n      normalizer_fn=layers.batch_norm,\n      normalizer_params=batch_norm_params):\n    with arg_scope([layers.batch_norm], **batch_norm_params) as arg_sc:\n      return arg_sc\n\nclass resnetv1(Network):\n  def __init__(self, batch_size=1, num_layers=50):\n    Network.__init__(self, batch_size=batch_size)\n    self._num_layers = num_layers\n    self._resnet_scope = 'resnet_v1_%d' % num_layers\n\n  def _crop_pool_layer(self, bottom, rois, name):\n    with tf.variable_scope(name) as scope:\n      batch_ids = tf.squeeze(tf.slice(rois, [0, 0], [-1, 1], name=\"batch_id\"), [1])\n      # Get the normalized coordinates of bboxes\n      bottom_shape = tf.shape(bottom)\n      height = (tf.to_float(bottom_shape[1]) - 1.) * np.float32(self._feat_stride[0])\n      width = (tf.to_float(bottom_shape[2]) - 1.) * np.float32(self._feat_stride[0])\n      x1 = tf.slice(rois, [0, 1], [-1, 1], name=\"x1\") / width\n      y1 = tf.slice(rois, [0, 2], [-1, 1], name=\"y1\") / height\n      x2 = tf.slice(rois, [0, 3], [-1, 1], name=\"x2\") / width\n      y2 = tf.slice(rois, [0, 4], [-1, 1], name=\"y2\") / height\n      # Won't be backpropagated to rois anyway, but to save time\n      bboxes = tf.stop_gradient(tf.concat([y1, x1, y2, x2], 1))\n      if cfg.RESNET.MAX_POOL:\n        pre_pool_size = cfg.POOLING_SIZE * 2\n        crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [pre_pool_size, pre_pool_size],\n                                         name=\"crops\")\n        crops = slim.max_pool2d(crops, [2, 2], padding='SAME')\n      else:\n        crops = tf.image.crop_and_resize(bottom, bboxes, tf.to_int32(batch_ids), [cfg.POOLING_SIZE, cfg.POOLING_SIZE],\n                                         name=\"crops\")\n    return crops\n\n  # Do the first few layers manually, because 'SAME' padding can behave inconsistently\n  # for images of different sizes: sometimes 0, sometimes 1\n  def build_base(self):\n    with tf.variable_scope(self._resnet_scope, self._resnet_scope):\n      net = resnet_utils.conv2d_same(self._image, 64, 7, stride=2, scope='conv1')\n      net = tf.pad(net, [[0, 0], [1, 1], [1, 1], [0, 0]])\n      net = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID', scope='pool1')\n\n    return net\n\n  def build_network(self, sess, is_training=True):\n    # select initializers\n    if cfg.TRAIN.TRUNCATED:\n      initializer = tf.truncated_normal_initializer(mean=0.0, stddev=0.01)\n      initializer_bbox = tf.truncated_normal_initializer(mean=0.0, stddev=0.001)\n    else:\n      initializer = tf.random_normal_initializer(mean=0.0, stddev=0.01)\n      initializer_bbox = tf.random_normal_initializer(mean=0.0, stddev=0.001)\n    bottleneck = resnet_v1.bottleneck\n    # choose different blocks for different number of layers\n    if self._num_layers == 50:\n      blocks = [\n        resnet_utils.Block('block1', bottleneck,\n                           [(256, 64, 1)] * 2 + [(256, 64, 2)]),\n        resnet_utils.Block('block2', bottleneck,\n                           [(512, 128, 1)] * 3 + [(512, 128, 2)]),\n        # Use stride-1 for the last conv4 layer\n        resnet_utils.Block('block3', bottleneck,\n                           [(1024, 256, 1)] * 5 + [(1024, 256, 1)]),\n        resnet_utils.Block('block4', bottleneck, [(2048, 512, 1)] * 3)\n      ]\n    elif self._num_layers == 101:\n      blocks = [\n        resnet_utils.Block('block1', bottleneck,\n                           [(256, 64, 1)] * 2 + [(256, 64, 2)]),\n        resnet_utils.Block('block2', bottleneck,\n                           [(512, 128, 1)] * 3 + [(512, 128, 2)]),\n        # Use stride-1 for the last conv4 layer\n        resnet_utils.Block('block3', bottleneck,\n                           [(1024, 256, 1)] * 22 + [(1024, 256, 1)]),\n        resnet_utils.Block('block4', bottleneck, [(2048, 512, 1)] * 3)\n      ]\n    elif self._num_layers == 152:\n      blocks = [\n        resnet_utils.Block('block1', bottleneck,\n                           [(256, 64, 1)] * 2 + [(256, 64, 2)]),\n        resnet_utils.Block('block2', bottleneck,\n                           [(512, 128, 1)] * 7 + [(512, 128, 2)]),\n        # Use stride-1 for the last conv4 layer\n        resnet_utils.Block('block3', bottleneck,\n                           [(1024, 256, 1)] * 35 + [(1024, 256, 1)]),\n        resnet_utils.Block('block4', bottleneck, [(2048, 512, 1)] * 3)\n      ]\n    else:\n      # other numbers are not supported\n      raise NotImplementedError\n\n    assert (0 <= cfg.RESNET.FIXED_BLOCKS < 4)\n    if cfg.RESNET.FIXED_BLOCKS == 3:\n      with slim.arg_scope(resnet_arg_scope(is_training=False)):\n        net = self.build_base()\n        net_conv4, _ = resnet_v1.resnet_v1(net,\n                                           blocks[0:cfg.RESNET.FIXED_BLOCKS],\n                                           global_pool=False,\n                                           include_root_block=False,\n                                           scope=self._resnet_scope)\n    elif cfg.RESNET.FIXED_BLOCKS > 0:\n      with slim.arg_scope(resnet_arg_scope(is_training=False)):\n        net = self.build_base()\n        net, _ = resnet_v1.resnet_v1(net,\n                                     blocks[0:cfg.RESNET.FIXED_BLOCKS],\n                                     global_pool=False,\n                                     include_root_block=False,\n                                     scope=self._resnet_scope)\n\n      with slim.arg_scope(resnet_arg_scope(is_training=is_training)):\n        net_conv4, _ = resnet_v1.resnet_v1(net,\n                                           blocks[cfg.RESNET.FIXED_BLOCKS:-1],\n                                           global_pool=False,\n                                           include_root_block=False,\n                                           scope=self._resnet_scope)\n    else:  # cfg.RESNET.FIXED_BLOCKS == 0\n      with slim.arg_scope(resnet_arg_scope(is_training=is_training)):\n        net = self.build_base()\n        net_conv4, _ = resnet_v1.resnet_v1(net,\n                                           blocks[0:-1],\n                                           global_pool=False,\n                                           include_root_block=False,\n                                           scope=self._resnet_scope)\n\n    self._act_summaries.append(net_conv4)\n    self._layers['head'] = net_conv4\n    with tf.variable_scope(self._resnet_scope, self._resnet_scope):\n      # build the anchors for the image\n      self._anchor_component()\n\n      # rpn\n      rpn = slim.conv2d(net_conv4, 512, [3, 3], trainable=is_training, weights_initializer=initializer,\n                        scope=\"rpn_conv/3x3\")\n      self._act_summaries.append(rpn)\n      rpn_cls_score = slim.conv2d(rpn, self._num_anchors * 2, [1, 1], trainable=is_training,\n                                  weights_initializer=initializer,\n                                  padding='VALID', activation_fn=None, scope='rpn_cls_score')\n      # change it so that the score has 2 as its channel size\n      rpn_cls_score_reshape = self._reshape_layer(rpn_cls_score, 2, 'rpn_cls_score_reshape')\n      rpn_cls_prob_reshape = self._softmax_layer(rpn_cls_score_reshape, \"rpn_cls_prob_reshape\")\n      rpn_cls_prob = self._reshape_layer(rpn_cls_prob_reshape, self._num_anchors * 2, \"rpn_cls_prob\")\n      rpn_bbox_pred = slim.conv2d(rpn, self._num_anchors * 4, [1, 1], trainable=is_training,\n                                  weights_initializer=initializer,\n                                  padding='VALID', activation_fn=None, scope='rpn_bbox_pred')\n      if is_training:\n        rois, roi_scores = self._proposal_layer(rpn_cls_prob, rpn_bbox_pred, \"rois\")\n        rpn_labels = self._anchor_target_layer(rpn_cls_score, \"anchor\")\n        # Try to have a determinestic order for the computing graph, for reproducibility\n        with tf.control_dependencies([rpn_labels]):\n          rois, _ = self._proposal_target_layer(rois, roi_scores, \"rpn_rois\")\n      else:\n        if cfg.TEST.MODE == 'nms':\n          rois, _ = self._proposal_layer(rpn_cls_prob, rpn_bbox_pred, \"rois\")\n        elif cfg.TEST.MODE == 'top':\n          rois, _ = self._proposal_top_layer(rpn_cls_prob, rpn_bbox_pred, \"rois\")\n        else:\n          raise NotImplementedError\n\n      # rcnn\n      if cfg.POOLING_MODE == 'crop':\n        pool5 = self._crop_pool_layer(net_conv4, rois, \"pool5\")\n      else:\n        raise NotImplementedError\n\n    with slim.arg_scope(resnet_arg_scope(is_training=is_training)):\n      fc7, _ = resnet_v1.resnet_v1(pool5,\n                                   blocks[-1:],\n                                   global_pool=False,\n                                   include_root_block=False,\n                                   scope=self._resnet_scope)\n\n    with tf.variable_scope(self._resnet_scope, self._resnet_scope):\n      # Average pooling done by reduce_mean\n      fc7 = tf.reduce_mean(fc7, axis=[1, 2])\n      cls_score = slim.fully_connected(fc7, self._num_classes, weights_initializer=initializer,\n                                       trainable=is_training, activation_fn=None, scope='cls_score')\n      cls_prob = self._softmax_layer(cls_score, \"cls_prob\")\n      bbox_pred = slim.fully_connected(fc7, self._num_classes * 4, weights_initializer=initializer_bbox,\n                                       trainable=is_training,\n                                       activation_fn=None, scope='bbox_pred')\n    self._predictions[\"rpn_cls_score\"] = rpn_cls_score\n    self._predictions[\"rpn_cls_score_reshape\"] = rpn_cls_score_reshape\n    self._predictions[\"rpn_cls_prob\"] = rpn_cls_prob\n    self._predictions[\"rpn_bbox_pred\"] = rpn_bbox_pred\n    self._predictions[\"cls_score\"] = cls_score\n    self._predictions[\"cls_prob\"] = cls_prob\n    self._predictions[\"bbox_pred\"] = bbox_pred\n    self._predictions[\"rois\"] = rois\n\n    self._score_summaries.update(self._predictions)\n\n    return rois, cls_prob, bbox_pred\n\n  def get_variables_to_restore(self, variables, var_keep_dic):\n    variables_to_restore = []\n\n    for v in variables:\n      # exclude the first conv layer to swap RGB to BGR\n      if v.name == (self._resnet_scope + '/conv1/weights:0'):\n        self._variables_to_fix[v.name] = v\n        continue\n      if v.name.split(':')[0] in var_keep_dic:\n        if v.name == 'resnet_v1_50/cls_score/weights/Momentum:0' \\\n          or v.name == 'resnet_v1_50/cls_score/biases/Momentum:0' \\\n          or v.name == 'resnet_v1_50/bbox_pred/weights/Momentum:0' \\\n          or v.name == 'resnet_v1_50/bbox_pred/biases/Momentum:0' \\\n          or v.name == 'resnet_v1_50/cls_score/weights:0' \\\n          or v.name == 'resnet_v1_50/cls_score/biases:0' \\\n          or v.name == 'resnet_v1_50/bbox_pred/weights:0' \\\n          or v.name == 'resnet_v1_50/bbox_pred/biases:0':\n          continue\n\n        print('Varibles restored: %s' % v.name)\n        variables_to_restore.append(v)\n\n    return variables_to_restore\n\n  def fix_variables(self, sess, pretrained_model):\n    print('Fix Resnet V1 layers..')\n    with tf.variable_scope('Fix_Resnet_V1') as scope:\n      with tf.device(\"/cpu:0\"):\n        # fix RGB to BGR\n        conv1_rgb = tf.get_variable(\"conv1_rgb\", [7, 7, 3, 64], trainable=False)\n        restorer_fc = tf.train.Saver({self._resnet_scope + \"/conv1/weights\": conv1_rgb})\n        restorer_fc.restore(sess, pretrained_model)\n\n        sess.run(tf.assign(self._variables_to_fix[self._resnet_scope + '/conv1/weights:0'], \n                           tf.reverse(conv1_rgb, [2])))\n"
  },
  {
    "path": "lib/nets/vgg16.py",
    "content": "# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport tensorflow as tf\nimport tensorflow.contrib.slim as slim\nfrom tensorflow.contrib.slim import losses\nfrom tensorflow.contrib.slim import arg_scope\nimport numpy as np\n\nfrom nets.network import Network\nfrom model.config import cfg\n\nclass vgg16(Network):\n  def __init__(self, batch_size=1):\n    Network.__init__(self, batch_size=batch_size)\n\n  def build_network(self, sess, is_training=True):\n    with tf.variable_scope('vgg_16', 'vgg_16'):\n      # select initializers\n      if cfg.TRAIN.TRUNCATED:\n        initializer = tf.truncated_normal_initializer(mean=0.0, stddev=0.01)\n        initializer_bbox = tf.truncated_normal_initializer(mean=0.0, stddev=0.001)\n      else:\n        initializer = tf.random_normal_initializer(mean=0.0, stddev=0.01)\n        initializer_bbox = tf.random_normal_initializer(mean=0.0, stddev=0.001)\n\n      net = slim.repeat(self._image, 2, slim.conv2d, 64, [3, 3],\n                        trainable=False, scope='conv1')\n      net = slim.max_pool2d(net, [2, 2], padding='SAME', scope='pool1')\n      net = slim.repeat(net, 2, slim.conv2d, 128, [3, 3],\n                        trainable=False, scope='conv2')\n      net = slim.max_pool2d(net, [2, 2], padding='SAME', scope='pool2')\n      net = slim.repeat(net, 3, slim.conv2d, 256, [3, 3],\n                        trainable=is_training, scope='conv3')\n      net = slim.max_pool2d(net, [2, 2], padding='SAME', scope='pool3')\n      net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3],\n                        trainable=is_training, scope='conv4')\n      net = slim.max_pool2d(net, [2, 2], padding='SAME', scope='pool4')\n      net = slim.repeat(net, 3, slim.conv2d, 512, [3, 3],\n                        trainable=is_training, scope='conv5')\n      self._act_summaries.append(net)\n      self._layers['head'] = net\n      # build the anchors for the image\n      self._anchor_component()\n\n      # rpn\n      rpn = slim.conv2d(net, 512, [3, 3], trainable=is_training, weights_initializer=initializer, scope=\"rpn_conv/3x3\")\n      self._act_summaries.append(rpn)\n      rpn_cls_score = slim.conv2d(rpn, self._num_anchors * 2, [1, 1], trainable=is_training,\n                                  weights_initializer=initializer,\n                                  padding='VALID', activation_fn=None, scope='rpn_cls_score')\n      # change it so that the score has 2 as its channel size\n      rpn_cls_score_reshape = self._reshape_layer(rpn_cls_score, 2, 'rpn_cls_score_reshape')\n      rpn_cls_prob_reshape = self._softmax_layer(rpn_cls_score_reshape, \"rpn_cls_prob_reshape\")\n      rpn_cls_prob = self._reshape_layer(rpn_cls_prob_reshape, self._num_anchors * 2, \"rpn_cls_prob\")\n      rpn_bbox_pred = slim.conv2d(rpn, self._num_anchors * 4, [1, 1], trainable=is_training,\n                                  weights_initializer=initializer,\n                                  padding='VALID', activation_fn=None, scope='rpn_bbox_pred')\n      if is_training:\n        rois, roi_scores = self._proposal_layer(rpn_cls_prob, rpn_bbox_pred, \"rois\")\n        rpn_labels = self._anchor_target_layer(rpn_cls_score, \"anchor\")\n        # Try to have a determinestic order for the computing graph, for reproducibility\n        with tf.control_dependencies([rpn_labels]):\n          rois, _ = self._proposal_target_layer(rois, roi_scores, \"rpn_rois\")\n      else:\n        if cfg.TEST.MODE == 'nms':\n          rois, _ = self._proposal_layer(rpn_cls_prob, rpn_bbox_pred, \"rois\")\n        elif cfg.TEST.MODE == 'top':\n          rois, _ = self._proposal_top_layer(rpn_cls_prob, rpn_bbox_pred, \"rois\")\n        else:\n          raise NotImplementedError\n\n      # rcnn\n      if cfg.POOLING_MODE == 'crop':\n        pool5 = self._crop_pool_layer(net, rois, \"pool5\")\n      else:\n        raise NotImplementedError\n\n      pool5_flat = slim.flatten(pool5, scope='flatten')\n      fc6 = slim.fully_connected(pool5_flat, 4096, scope='fc6')\n      if is_training:\n        fc6 = slim.dropout(fc6, keep_prob=0.5, is_training=True, scope='dropout6')\n      fc7 = slim.fully_connected(fc6, 4096, scope='fc7')\n      if is_training:\n        fc7 = slim.dropout(fc7, keep_prob=0.5, is_training=True, scope='dropout7')\n      cls_score = slim.fully_connected(fc7, self._num_classes, \n                                       weights_initializer=initializer,\n                                       trainable=is_training,\n                                       activation_fn=None, scope='cls_score')\n      cls_prob = self._softmax_layer(cls_score, \"cls_prob\")\n      bbox_pred = slim.fully_connected(fc7, self._num_classes * 4, \n                                       weights_initializer=initializer_bbox,\n                                       trainable=is_training,\n                                       activation_fn=None, scope='bbox_pred')\n\n      self._predictions[\"rpn_cls_score\"] = rpn_cls_score\n      self._predictions[\"rpn_cls_score_reshape\"] = rpn_cls_score_reshape\n      self._predictions[\"rpn_cls_prob\"] = rpn_cls_prob\n      self._predictions[\"rpn_bbox_pred\"] = rpn_bbox_pred\n      self._predictions[\"cls_score\"] = cls_score\n      self._predictions[\"cls_prob\"] = cls_prob\n      self._predictions[\"bbox_pred\"] = bbox_pred\n      self._predictions[\"rois\"] = rois\n\n      self._score_summaries.update(self._predictions)\n\n      return rois, cls_prob, bbox_pred\n\n  def get_variables_to_restore(self, variables, var_keep_dic):\n    variables_to_restore = []\n\n    for v in variables:\n      # exclude the conv weights that are fc weights in vgg16\n      if v.name == 'vgg_16/fc6/weights:0' or v.name == 'vgg_16/fc7/weights:0':\n        self._variables_to_fix[v.name] = v\n        continue\n      # exclude the first conv layer to swap RGB to BGR\n      if v.name == 'vgg_16/conv1/conv1_1/weights:0':\n        self._variables_to_fix[v.name] = v\n        continue\n      if v.name.split(':')[0] in var_keep_dic:\n        print('Varibles restored: %s' % v.name)\n        variables_to_restore.append(v)\n\n    return variables_to_restore\n\n  def fix_variables(self, sess, pretrained_model):\n    print('Fix VGG16 layers..')\n    with tf.variable_scope('Fix_VGG16') as scope:\n      with tf.device(\"/cpu:0\"):\n        # fix the vgg16 issue from conv weights to fc weights\n        # fix RGB to BGR\n        fc6_conv = tf.get_variable(\"fc6_conv\", [7, 7, 512, 4096], trainable=False)\n        fc7_conv = tf.get_variable(\"fc7_conv\", [1, 1, 4096, 4096], trainable=False)\n        conv1_rgb = tf.get_variable(\"conv1_rgb\", [3, 3, 3, 64], trainable=False)\n        restorer_fc = tf.train.Saver({\"vgg_16/fc6/weights\": fc6_conv, \n                                      \"vgg_16/fc7/weights\": fc7_conv,\n                                      \"vgg_16/conv1/conv1_1/weights\": conv1_rgb})\n        restorer_fc.restore(sess, pretrained_model)\n\n        sess.run(tf.assign(self._variables_to_fix['vgg_16/fc6/weights:0'], tf.reshape(fc6_conv, \n                            self._variables_to_fix['vgg_16/fc6/weights:0'].get_shape())))\n        sess.run(tf.assign(self._variables_to_fix['vgg_16/fc7/weights:0'], tf.reshape(fc7_conv, \n                            self._variables_to_fix['vgg_16/fc7/weights:0'].get_shape())))\n        sess.run(tf.assign(self._variables_to_fix['vgg_16/conv1/conv1_1/weights:0'], \n                            tf.reverse(conv1_rgb, [2])))"
  },
  {
    "path": "lib/nms/.gitignore",
    "content": ""
  },
  {
    "path": "lib/nms/__init__.py",
    "content": ""
  },
  {
    "path": "lib/nms/cpu_nms.c",
    "content": "/* Generated by Cython 0.20.1 on Wed Oct  5 13:15:30 2016 */\n\n#define PY_SSIZE_T_CLEAN\n#ifndef CYTHON_USE_PYLONG_INTERNALS\n#ifdef PYLONG_BITS_IN_DIGIT\n#define CYTHON_USE_PYLONG_INTERNALS 0\n#else\n#include \"pyconfig.h\"\n#ifdef PYLONG_BITS_IN_DIGIT\n#define CYTHON_USE_PYLONG_INTERNALS 1\n#else\n#define CYTHON_USE_PYLONG_INTERNALS 0\n#endif\n#endif\n#endif\n#include \"Python.h\"\n#ifndef Py_PYTHON_H\n    #error Python headers needed to compile C extensions, please install development version of Python.\n#elif PY_VERSION_HEX < 0x02040000\n    #error Cython requires Python 2.4+.\n#else\n#define CYTHON_ABI \"0_20_1\"\n#include <stddef.h> /* For offsetof */\n#ifndef offsetof\n#define offsetof(type, member) ( (size_t) & ((type*)0) -> member )\n#endif\n#if !defined(WIN32) && !defined(MS_WINDOWS)\n  #ifndef __stdcall\n    #define __stdcall\n  #endif\n  #ifndef __cdecl\n    #define __cdecl\n  #endif\n  #ifndef __fastcall\n    #define __fastcall\n  #endif\n#endif\n#ifndef DL_IMPORT\n  #define DL_IMPORT(t) t\n#endif\n#ifndef DL_EXPORT\n  #define DL_EXPORT(t) t\n#endif\n#ifndef PY_LONG_LONG\n  #define PY_LONG_LONG LONG_LONG\n#endif\n#ifndef Py_HUGE_VAL\n  #define Py_HUGE_VAL HUGE_VAL\n#endif\n#ifdef PYPY_VERSION\n#define CYTHON_COMPILING_IN_PYPY 1\n#define CYTHON_COMPILING_IN_CPYTHON 0\n#else\n#define CYTHON_COMPILING_IN_PYPY 0\n#define CYTHON_COMPILING_IN_CPYTHON 1\n#endif\n#if CYTHON_COMPILING_IN_PYPY\n#define Py_OptimizeFlag 0\n#endif\n#if PY_VERSION_HEX < 0x02050000\n  typedef int Py_ssize_t;\n  #define PY_SSIZE_T_MAX INT_MAX\n  #define PY_SSIZE_T_MIN INT_MIN\n  #define PY_FORMAT_SIZE_T \"\"\n  #define CYTHON_FORMAT_SSIZE_T \"\"\n  #define PyInt_FromSsize_t(z) PyInt_FromLong(z)\n  #define PyInt_AsSsize_t(o)   __Pyx_PyInt_As_int(o)\n  #define PyNumber_Index(o)    ((PyNumber_Check(o) && !PyFloat_Check(o)) ? PyNumber_Int(o) : \\\n                                (PyErr_Format(PyExc_TypeError, \\\n                                              \"expected index value, got %.200s\", Py_TYPE(o)->tp_name), \\\n                                 (PyObject*)0))\n  #define __Pyx_PyIndex_Check(o) (PyNumber_Check(o) && !PyFloat_Check(o) && \\\n                                  !PyComplex_Check(o))\n  #define PyIndex_Check __Pyx_PyIndex_Check\n  #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message)\n  #define __PYX_BUILD_PY_SSIZE_T \"i\"\n#else\n  #define __PYX_BUILD_PY_SSIZE_T \"n\"\n  #define CYTHON_FORMAT_SSIZE_T \"z\"\n  #define __Pyx_PyIndex_Check PyIndex_Check\n#endif\n#if PY_VERSION_HEX < 0x02060000\n  #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)\n  #define Py_TYPE(ob)   (((PyObject*)(ob))->ob_type)\n  #define Py_SIZE(ob)   (((PyVarObject*)(ob))->ob_size)\n  #define PyVarObject_HEAD_INIT(type, size) \\\n          PyObject_HEAD_INIT(type) size,\n  #define PyType_Modified(t)\n  typedef struct {\n     void *buf;\n     PyObject *obj;\n     Py_ssize_t len;\n     Py_ssize_t itemsize;\n     int readonly;\n     int ndim;\n     char *format;\n     Py_ssize_t *shape;\n     Py_ssize_t *strides;\n     Py_ssize_t *suboffsets;\n     void *internal;\n  } Py_buffer;\n  #define PyBUF_SIMPLE 0\n  #define PyBUF_WRITABLE 0x0001\n  #define PyBUF_FORMAT 0x0004\n  #define PyBUF_ND 0x0008\n  #define PyBUF_STRIDES (0x0010 | PyBUF_ND)\n  #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)\n  #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)\n  #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)\n  #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)\n  #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_FORMAT | PyBUF_WRITABLE)\n  #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_FORMAT | PyBUF_WRITABLE)\n  typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);\n  typedef void (*releasebufferproc)(PyObject *, Py_buffer *);\n#endif\n#if PY_MAJOR_VERSION < 3\n  #define __Pyx_BUILTIN_MODULE_NAME \"__builtin__\"\n  #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \\\n          PyCode_New(a+k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\n  #define __Pyx_DefaultClassType PyClass_Type\n#else\n  #define __Pyx_BUILTIN_MODULE_NAME \"builtins\"\n  #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \\\n          PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\n  #define __Pyx_DefaultClassType PyType_Type\n#endif\n#if PY_VERSION_HEX < 0x02060000\n  #define PyUnicode_FromString(s) PyUnicode_Decode(s, strlen(s), \"UTF-8\", \"strict\")\n#endif\n#if PY_MAJOR_VERSION >= 3\n  #define Py_TPFLAGS_CHECKTYPES 0\n  #define Py_TPFLAGS_HAVE_INDEX 0\n#endif\n#if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)\n  #define Py_TPFLAGS_HAVE_NEWBUFFER 0\n#endif\n#if PY_VERSION_HEX < 0x02060000\n  #define Py_TPFLAGS_HAVE_VERSION_TAG 0\n#endif\n#if PY_VERSION_HEX < 0x02060000 && !defined(Py_TPFLAGS_IS_ABSTRACT)\n  #define Py_TPFLAGS_IS_ABSTRACT 0\n#endif\n#if PY_VERSION_HEX < 0x030400a1 && !defined(Py_TPFLAGS_HAVE_FINALIZE)\n  #define Py_TPFLAGS_HAVE_FINALIZE 0\n#endif\n#if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND)\n  #define CYTHON_PEP393_ENABLED 1\n  #define __Pyx_PyUnicode_READY(op)       (likely(PyUnicode_IS_READY(op)) ? \\\n                                              0 : _PyUnicode_Ready((PyObject *)(op)))\n  #define __Pyx_PyUnicode_GET_LENGTH(u)   PyUnicode_GET_LENGTH(u)\n  #define __Pyx_PyUnicode_READ_CHAR(u, i) PyUnicode_READ_CHAR(u, i)\n  #define __Pyx_PyUnicode_KIND(u)         PyUnicode_KIND(u)\n  #define __Pyx_PyUnicode_DATA(u)         PyUnicode_DATA(u)\n  #define __Pyx_PyUnicode_READ(k, d, i)   PyUnicode_READ(k, d, i)\n#else\n  #define CYTHON_PEP393_ENABLED 0\n  #define __Pyx_PyUnicode_READY(op)       (0)\n  #define __Pyx_PyUnicode_GET_LENGTH(u)   PyUnicode_GET_SIZE(u)\n  #define __Pyx_PyUnicode_READ_CHAR(u, i) ((Py_UCS4)(PyUnicode_AS_UNICODE(u)[i]))\n  #define __Pyx_PyUnicode_KIND(u)         (sizeof(Py_UNICODE))\n  #define __Pyx_PyUnicode_DATA(u)         ((void*)PyUnicode_AS_UNICODE(u))\n  #define __Pyx_PyUnicode_READ(k, d, i)   ((void)(k), (Py_UCS4)(((Py_UNICODE*)d)[i]))\n#endif\n#if CYTHON_COMPILING_IN_PYPY\n  #define __Pyx_PyUnicode_Concat(a, b)      PyNumber_Add(a, b)\n  #define __Pyx_PyUnicode_ConcatSafe(a, b)  PyNumber_Add(a, b)\n#else\n  #define __Pyx_PyUnicode_Concat(a, b)      PyUnicode_Concat(a, b)\n  #define __Pyx_PyUnicode_ConcatSafe(a, b)  ((unlikely((a) == Py_None) || unlikely((b) == Py_None)) ? \\\n      PyNumber_Add(a, b) : __Pyx_PyUnicode_Concat(a, b))\n#endif\n#define __Pyx_PyString_FormatSafe(a, b)  ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : __Pyx_PyString_Format(a, b))\n#define __Pyx_PyUnicode_FormatSafe(a, b)  ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : PyUnicode_Format(a, b))\n#if PY_MAJOR_VERSION >= 3\n  #define __Pyx_PyString_Format(a, b)  PyUnicode_Format(a, b)\n#else\n  #define __Pyx_PyString_Format(a, b)  PyString_Format(a, b)\n#endif\n#if PY_MAJOR_VERSION >= 3\n  #define PyBaseString_Type            PyUnicode_Type\n  #define PyStringObject               PyUnicodeObject\n  #define PyString_Type                PyUnicode_Type\n  #define PyString_Check               PyUnicode_Check\n  #define PyString_CheckExact          PyUnicode_CheckExact\n#endif\n#if PY_VERSION_HEX < 0x02060000\n  #define PyBytesObject                PyStringObject\n  #define PyBytes_Type                 PyString_Type\n  #define PyBytes_Check                PyString_Check\n  #define PyBytes_CheckExact           PyString_CheckExact\n  #define PyBytes_FromString           PyString_FromString\n  #define PyBytes_FromStringAndSize    PyString_FromStringAndSize\n  #define PyBytes_FromFormat           PyString_FromFormat\n  #define PyBytes_DecodeEscape         PyString_DecodeEscape\n  #define PyBytes_AsString             PyString_AsString\n  #define PyBytes_AsStringAndSize      PyString_AsStringAndSize\n  #define PyBytes_Size                 PyString_Size\n  #define PyBytes_AS_STRING            PyString_AS_STRING\n  #define PyBytes_GET_SIZE             PyString_GET_SIZE\n  #define PyBytes_Repr                 PyString_Repr\n  #define PyBytes_Concat               PyString_Concat\n  #define PyBytes_ConcatAndDel         PyString_ConcatAndDel\n#endif\n#if PY_MAJOR_VERSION >= 3\n  #define __Pyx_PyBaseString_Check(obj) PyUnicode_Check(obj)\n  #define __Pyx_PyBaseString_CheckExact(obj) PyUnicode_CheckExact(obj)\n#else\n  #define __Pyx_PyBaseString_Check(obj) (PyString_CheckExact(obj) || PyUnicode_CheckExact(obj) || \\\n                                         PyString_Check(obj) || PyUnicode_Check(obj))\n  #define __Pyx_PyBaseString_CheckExact(obj) (PyString_CheckExact(obj) || PyUnicode_CheckExact(obj))\n#endif\n#if PY_VERSION_HEX < 0x02060000\n  #define PySet_Check(obj)             PyObject_TypeCheck(obj, &PySet_Type)\n  #define PyFrozenSet_Check(obj)       PyObject_TypeCheck(obj, &PyFrozenSet_Type)\n#endif\n#ifndef PySet_CheckExact\n  #define PySet_CheckExact(obj)        (Py_TYPE(obj) == &PySet_Type)\n#endif\n#define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type)\n#if PY_MAJOR_VERSION >= 3\n  #define PyIntObject                  PyLongObject\n  #define PyInt_Type                   PyLong_Type\n  #define PyInt_Check(op)              PyLong_Check(op)\n  #define PyInt_CheckExact(op)         PyLong_CheckExact(op)\n  #define PyInt_FromString             PyLong_FromString\n  #define PyInt_FromUnicode            PyLong_FromUnicode\n  #define PyInt_FromLong               PyLong_FromLong\n  #define PyInt_FromSize_t             PyLong_FromSize_t\n  #define PyInt_FromSsize_t            PyLong_FromSsize_t\n  #define PyInt_AsLong                 PyLong_AsLong\n  #define PyInt_AS_LONG                PyLong_AS_LONG\n  #define PyInt_AsSsize_t              PyLong_AsSsize_t\n  #define PyInt_AsUnsignedLongMask     PyLong_AsUnsignedLongMask\n  #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask\n  #define PyNumber_Int                 PyNumber_Long\n#endif\n#if PY_MAJOR_VERSION >= 3\n  #define PyBoolObject                 PyLongObject\n#endif\n#if PY_VERSION_HEX < 0x030200A4\n  typedef long Py_hash_t;\n  #define __Pyx_PyInt_FromHash_t PyInt_FromLong\n  #define __Pyx_PyInt_AsHash_t   PyInt_AsLong\n#else\n  #define __Pyx_PyInt_FromHash_t PyInt_FromSsize_t\n  #define __Pyx_PyInt_AsHash_t   PyInt_AsSsize_t\n#endif\n#if (PY_MAJOR_VERSION < 3) || (PY_VERSION_HEX >= 0x03010300)\n  #define __Pyx_PySequence_GetSlice(obj, a, b) PySequence_GetSlice(obj, a, b)\n  #define __Pyx_PySequence_SetSlice(obj, a, b, value) PySequence_SetSlice(obj, a, b, value)\n  #define __Pyx_PySequence_DelSlice(obj, a, b) PySequence_DelSlice(obj, a, b)\n#else\n  #define __Pyx_PySequence_GetSlice(obj, a, b) (unlikely(!(obj)) ? \\\n        (PyErr_SetString(PyExc_SystemError, \"null argument to internal routine\"), (PyObject*)0) : \\\n        (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_GetSlice(obj, a, b)) : \\\n            (PyErr_Format(PyExc_TypeError, \"'%.200s' object is unsliceable\", (obj)->ob_type->tp_name), (PyObject*)0)))\n  #define __Pyx_PySequence_SetSlice(obj, a, b, value) (unlikely(!(obj)) ? \\\n        (PyErr_SetString(PyExc_SystemError, \"null argument to internal routine\"), -1) : \\\n        (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_SetSlice(obj, a, b, value)) : \\\n            (PyErr_Format(PyExc_TypeError, \"'%.200s' object doesn't support slice assignment\", (obj)->ob_type->tp_name), -1)))\n  #define __Pyx_PySequence_DelSlice(obj, a, b) (unlikely(!(obj)) ? \\\n        (PyErr_SetString(PyExc_SystemError, \"null argument to internal routine\"), -1) : \\\n        (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_DelSlice(obj, a, b)) : \\\n            (PyErr_Format(PyExc_TypeError, \"'%.200s' object doesn't support slice deletion\", (obj)->ob_type->tp_name), -1)))\n#endif\n#if PY_MAJOR_VERSION >= 3\n  #define PyMethod_New(func, self, klass) ((self) ? PyMethod_New(func, self) : PyInstanceMethod_New(func))\n#endif\n#if PY_VERSION_HEX < 0x02050000\n  #define __Pyx_GetAttrString(o,n)   PyObject_GetAttrString((o),((char *)(n)))\n  #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a))\n  #define __Pyx_DelAttrString(o,n)   PyObject_DelAttrString((o),((char *)(n)))\n#else\n  #define __Pyx_GetAttrString(o,n)   PyObject_GetAttrString((o),(n))\n  #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a))\n  #define __Pyx_DelAttrString(o,n)   PyObject_DelAttrString((o),(n))\n#endif\n#if PY_VERSION_HEX < 0x02050000\n  #define __Pyx_NAMESTR(n) ((char *)(n))\n  #define __Pyx_DOCSTR(n)  ((char *)(n))\n#else\n  #define __Pyx_NAMESTR(n) (n)\n  #define __Pyx_DOCSTR(n)  (n)\n#endif\n#ifndef CYTHON_INLINE\n  #if defined(__GNUC__)\n    #define CYTHON_INLINE __inline__\n  #elif defined(_MSC_VER)\n    #define CYTHON_INLINE __inline\n  #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L\n    #define CYTHON_INLINE inline\n  #else\n    #define CYTHON_INLINE\n  #endif\n#endif\n#ifndef CYTHON_RESTRICT\n  #if defined(__GNUC__)\n    #define CYTHON_RESTRICT __restrict__\n  #elif defined(_MSC_VER) && _MSC_VER >= 1400\n    #define CYTHON_RESTRICT __restrict\n  #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L\n    #define CYTHON_RESTRICT restrict\n  #else\n    #define CYTHON_RESTRICT\n  #endif\n#endif\n#ifdef NAN\n#define __PYX_NAN() ((float) NAN)\n#else\nstatic CYTHON_INLINE float __PYX_NAN() {\n  /* Initialize NaN. The sign is irrelevant, an exponent with all bits 1 and\n   a nonzero mantissa means NaN. If the first bit in the mantissa is 1, it is\n   a quiet NaN. */\n  float value;\n  memset(&value, 0xFF, sizeof(value));\n  return value;\n}\n#endif\n\n\n#if PY_MAJOR_VERSION >= 3\n  #define __Pyx_PyNumber_Divide(x,y)         PyNumber_TrueDivide(x,y)\n  #define __Pyx_PyNumber_InPlaceDivide(x,y)  PyNumber_InPlaceTrueDivide(x,y)\n#else\n  #define __Pyx_PyNumber_Divide(x,y)         PyNumber_Divide(x,y)\n  #define __Pyx_PyNumber_InPlaceDivide(x,y)  PyNumber_InPlaceDivide(x,y)\n#endif\n\n#ifndef __PYX_EXTERN_C\n  #ifdef __cplusplus\n    #define __PYX_EXTERN_C extern \"C\"\n  #else\n    #define __PYX_EXTERN_C extern\n  #endif\n#endif\n\n#if defined(WIN32) || defined(MS_WINDOWS)\n#define _USE_MATH_DEFINES\n#endif\n#include <math.h>\n#define __PYX_HAVE__nms__cpu_nms\n#define __PYX_HAVE_API__nms__cpu_nms\n#include \"string.h\"\n#include \"stdio.h\"\n#include \"stdlib.h\"\n#include \"numpy/arrayobject.h\"\n#include \"numpy/ufuncobject.h\"\n#ifdef _OPENMP\n#include <omp.h>\n#endif /* _OPENMP */\n\n#ifdef PYREX_WITHOUT_ASSERTIONS\n#define CYTHON_WITHOUT_ASSERTIONS\n#endif\n\n#ifndef CYTHON_UNUSED\n# if defined(__GNUC__)\n#   if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))\n#     define CYTHON_UNUSED __attribute__ ((__unused__))\n#   else\n#     define CYTHON_UNUSED\n#   endif\n# elif defined(__ICC) || (defined(__INTEL_COMPILER) && !defined(_MSC_VER))\n#   define CYTHON_UNUSED __attribute__ ((__unused__))\n# else\n#   define CYTHON_UNUSED\n# endif\n#endif\ntypedef struct {PyObject **p; char *s; const Py_ssize_t n; const char* encoding;\n                const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; /*proto*/\n\n#define __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 0\n#define __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 0\n#define __PYX_DEFAULT_STRING_ENCODING \"\"\n#define __Pyx_PyObject_FromString __Pyx_PyBytes_FromString\n#define __Pyx_PyObject_FromStringAndSize __Pyx_PyBytes_FromStringAndSize\n#define __Pyx_fits_Py_ssize_t(v, type, is_signed)  (    \\\n    (sizeof(type) < sizeof(Py_ssize_t))  ||             \\\n    (sizeof(type) > sizeof(Py_ssize_t) &&               \\\n          likely(v < (type)PY_SSIZE_T_MAX ||            \\\n                 v == (type)PY_SSIZE_T_MAX)  &&         \\\n          (!is_signed || likely(v > (type)PY_SSIZE_T_MIN ||       \\\n                                v == (type)PY_SSIZE_T_MIN)))  ||  \\\n    (sizeof(type) == sizeof(Py_ssize_t) &&              \\\n          (is_signed || likely(v < (type)PY_SSIZE_T_MAX ||        \\\n                               v == (type)PY_SSIZE_T_MAX)))  )\nstatic CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject*);\nstatic CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length);\n#define __Pyx_PyByteArray_FromString(s) PyByteArray_FromStringAndSize((const char*)s, strlen((const char*)s))\n#define __Pyx_PyByteArray_FromStringAndSize(s, l) PyByteArray_FromStringAndSize((const char*)s, l)\n#define __Pyx_PyBytes_FromString        PyBytes_FromString\n#define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize\nstatic CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char*);\n#if PY_MAJOR_VERSION < 3\n    #define __Pyx_PyStr_FromString        __Pyx_PyBytes_FromString\n    #define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize\n#else\n    #define __Pyx_PyStr_FromString        __Pyx_PyUnicode_FromString\n    #define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize\n#endif\n#define __Pyx_PyObject_AsSString(s)    ((signed char*) __Pyx_PyObject_AsString(s))\n#define __Pyx_PyObject_AsUString(s)    ((unsigned char*) __Pyx_PyObject_AsString(s))\n#define __Pyx_PyObject_FromUString(s)  __Pyx_PyObject_FromString((char*)s)\n#define __Pyx_PyBytes_FromUString(s)   __Pyx_PyBytes_FromString((char*)s)\n#define __Pyx_PyByteArray_FromUString(s)   __Pyx_PyByteArray_FromString((char*)s)\n#define __Pyx_PyStr_FromUString(s)     __Pyx_PyStr_FromString((char*)s)\n#define __Pyx_PyUnicode_FromUString(s) __Pyx_PyUnicode_FromString((char*)s)\n#if PY_MAJOR_VERSION < 3\nstatic CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u)\n{\n    const Py_UNICODE *u_end = u;\n    while (*u_end++) ;\n    return u_end - u - 1;\n}\n#else\n#define __Pyx_Py_UNICODE_strlen Py_UNICODE_strlen\n#endif\n#define __Pyx_PyUnicode_FromUnicode(u)       PyUnicode_FromUnicode(u, __Pyx_Py_UNICODE_strlen(u))\n#define __Pyx_PyUnicode_FromUnicodeAndLength PyUnicode_FromUnicode\n#define __Pyx_PyUnicode_AsUnicode            PyUnicode_AsUnicode\n#define __Pyx_Owned_Py_None(b) (Py_INCREF(Py_None), Py_None)\n#define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False))\nstatic CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*);\nstatic CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x);\nstatic CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);\nstatic CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);\n#if CYTHON_COMPILING_IN_CPYTHON\n#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))\n#else\n#define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x)\n#endif\n#define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x))\n#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII\nstatic int __Pyx_sys_getdefaultencoding_not_ascii;\nstatic int __Pyx_init_sys_getdefaultencoding_params(void) {\n    PyObject* sys = NULL;\n    PyObject* default_encoding = NULL;\n    PyObject* ascii_chars_u = NULL;\n    PyObject* ascii_chars_b = NULL;\n    sys = PyImport_ImportModule(\"sys\");\n    if (sys == NULL) goto bad;\n    default_encoding = PyObject_CallMethod(sys, (char*) (const char*) \"getdefaultencoding\", NULL);\n    if (default_encoding == NULL) goto bad;\n    if (strcmp(PyBytes_AsString(default_encoding), \"ascii\") == 0) {\n        __Pyx_sys_getdefaultencoding_not_ascii = 0;\n    } else {\n        const char* default_encoding_c = PyBytes_AS_STRING(default_encoding);\n        char ascii_chars[128];\n        int c;\n        for (c = 0; c < 128; c++) {\n            ascii_chars[c] = c;\n        }\n        __Pyx_sys_getdefaultencoding_not_ascii = 1;\n        ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL);\n        if (ascii_chars_u == NULL) goto bad;\n        ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL);\n        if (ascii_chars_b == NULL || strncmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) {\n            PyErr_Format(\n                PyExc_ValueError,\n                \"This module compiled with c_string_encoding=ascii, but default encoding '%.200s' is not a superset of ascii.\",\n                default_encoding_c);\n            goto bad;\n        }\n    }\n    Py_XDECREF(sys);\n    Py_XDECREF(default_encoding);\n    Py_XDECREF(ascii_chars_u);\n    Py_XDECREF(ascii_chars_b);\n    return 0;\nbad:\n    Py_XDECREF(sys);\n    Py_XDECREF(default_encoding);\n    Py_XDECREF(ascii_chars_u);\n    Py_XDECREF(ascii_chars_b);\n    return -1;\n}\n#endif\n#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3\n#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL)\n#else\n#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL)\n#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT\nstatic char* __PYX_DEFAULT_STRING_ENCODING;\nstatic int __Pyx_init_sys_getdefaultencoding_params(void) {\n    PyObject* sys = NULL;\n    PyObject* default_encoding = NULL;\n    char* default_encoding_c;\n    sys = PyImport_ImportModule(\"sys\");\n    if (sys == NULL) goto bad;\n    default_encoding = PyObject_CallMethod(sys, (char*) (const char*) \"getdefaultencoding\", NULL);\n    if (default_encoding == NULL) goto bad;\n    default_encoding_c = PyBytes_AS_STRING(default_encoding);\n    __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c));\n    strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c);\n    Py_DECREF(sys);\n    Py_DECREF(default_encoding);\n    return 0;\nbad:\n    Py_XDECREF(sys);\n    Py_XDECREF(default_encoding);\n    return -1;\n}\n#endif\n#endif\n\n\n#ifdef __GNUC__\n  /* Test for GCC > 2.95 */\n  #if __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))\n    #define likely(x)   __builtin_expect(!!(x), 1)\n    #define unlikely(x) __builtin_expect(!!(x), 0)\n  #else /* __GNUC__ > 2 ... */\n    #define likely(x)   (x)\n    #define unlikely(x) (x)\n  #endif /* __GNUC__ > 2 ... */\n#else /* __GNUC__ */\n  #define likely(x)   (x)\n  #define unlikely(x) (x)\n#endif /* __GNUC__ */\n\nstatic PyObject *__pyx_m;\nstatic PyObject *__pyx_d;\nstatic PyObject *__pyx_b;\nstatic PyObject *__pyx_empty_tuple;\nstatic PyObject *__pyx_empty_bytes;\nstatic int __pyx_lineno;\nstatic int __pyx_clineno = 0;\nstatic const char * __pyx_cfilenm= __FILE__;\nstatic const char *__pyx_filename;\n\n#if !defined(CYTHON_CCOMPLEX)\n  #if defined(__cplusplus)\n    #define CYTHON_CCOMPLEX 1\n  #elif defined(_Complex_I)\n    #define CYTHON_CCOMPLEX 1\n  #else\n    #define CYTHON_CCOMPLEX 0\n  #endif\n#endif\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    #include <complex>\n  #else\n    #include <complex.h>\n  #endif\n#endif\n#if CYTHON_CCOMPLEX && !defined(__cplusplus) && defined(__sun__) && defined(__GNUC__)\n  #undef _Complex_I\n  #define _Complex_I 1.0fj\n#endif\n\n\nstatic const char *__pyx_f[] = {\n  \"cpu_nms.pyx\",\n  \"__init__.pxd\",\n  \"type.pxd\",\n};\n#define IS_UNSIGNED(type) (((type) -1) > 0)\nstruct __Pyx_StructField_;\n#define __PYX_BUF_FLAGS_PACKED_STRUCT (1 << 0)\ntypedef struct {\n  const char* name; /* for error messages only */\n  struct __Pyx_StructField_* fields;\n  size_t size;     /* sizeof(type) */\n  size_t arraysize[8]; /* length of array in each dimension */\n  int ndim;\n  char typegroup; /* _R_eal, _C_omplex, Signed _I_nt, _U_nsigned int, _S_truct, _P_ointer, _O_bject, c_H_ar */\n  char is_unsigned;\n  int flags;\n} __Pyx_TypeInfo;\ntypedef struct __Pyx_StructField_ {\n  __Pyx_TypeInfo* type;\n  const char* name;\n  size_t offset;\n} __Pyx_StructField;\ntypedef struct {\n  __Pyx_StructField* field;\n  size_t parent_offset;\n} __Pyx_BufFmt_StackElem;\ntypedef struct {\n  __Pyx_StructField root;\n  __Pyx_BufFmt_StackElem* head;\n  size_t fmt_offset;\n  size_t new_count, enc_count;\n  size_t struct_alignment;\n  int is_complex;\n  char enc_type;\n  char new_packmode;\n  char enc_packmode;\n  char is_valid_array;\n} __Pyx_BufFmt_Context;\n\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":723\n * # in Cython to enable them only on the right systems.\n * \n * ctypedef npy_int8       int8_t             # <<<<<<<<<<<<<<\n * ctypedef npy_int16      int16_t\n * ctypedef npy_int32      int32_t\n */\ntypedef npy_int8 __pyx_t_5numpy_int8_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":724\n * \n * ctypedef npy_int8       int8_t\n * ctypedef npy_int16      int16_t             # <<<<<<<<<<<<<<\n * ctypedef npy_int32      int32_t\n * ctypedef npy_int64      int64_t\n */\ntypedef npy_int16 __pyx_t_5numpy_int16_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":725\n * ctypedef npy_int8       int8_t\n * ctypedef npy_int16      int16_t\n * ctypedef npy_int32      int32_t             # <<<<<<<<<<<<<<\n * ctypedef npy_int64      int64_t\n * #ctypedef npy_int96      int96_t\n */\ntypedef npy_int32 __pyx_t_5numpy_int32_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":726\n * ctypedef npy_int16      int16_t\n * ctypedef npy_int32      int32_t\n * ctypedef npy_int64      int64_t             # <<<<<<<<<<<<<<\n * #ctypedef npy_int96      int96_t\n * #ctypedef npy_int128     int128_t\n */\ntypedef npy_int64 __pyx_t_5numpy_int64_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":730\n * #ctypedef npy_int128     int128_t\n * \n * ctypedef npy_uint8      uint8_t             # <<<<<<<<<<<<<<\n * ctypedef npy_uint16     uint16_t\n * ctypedef npy_uint32     uint32_t\n */\ntypedef npy_uint8 __pyx_t_5numpy_uint8_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":731\n * \n * ctypedef npy_uint8      uint8_t\n * ctypedef npy_uint16     uint16_t             # <<<<<<<<<<<<<<\n * ctypedef npy_uint32     uint32_t\n * ctypedef npy_uint64     uint64_t\n */\ntypedef npy_uint16 __pyx_t_5numpy_uint16_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":732\n * ctypedef npy_uint8      uint8_t\n * ctypedef npy_uint16     uint16_t\n * ctypedef npy_uint32     uint32_t             # <<<<<<<<<<<<<<\n * ctypedef npy_uint64     uint64_t\n * #ctypedef npy_uint96     uint96_t\n */\ntypedef npy_uint32 __pyx_t_5numpy_uint32_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":733\n * ctypedef npy_uint16     uint16_t\n * ctypedef npy_uint32     uint32_t\n * ctypedef npy_uint64     uint64_t             # <<<<<<<<<<<<<<\n * #ctypedef npy_uint96     uint96_t\n * #ctypedef npy_uint128    uint128_t\n */\ntypedef npy_uint64 __pyx_t_5numpy_uint64_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":737\n * #ctypedef npy_uint128    uint128_t\n * \n * ctypedef npy_float32    float32_t             # <<<<<<<<<<<<<<\n * ctypedef npy_float64    float64_t\n * #ctypedef npy_float80    float80_t\n */\ntypedef npy_float32 __pyx_t_5numpy_float32_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":738\n * \n * ctypedef npy_float32    float32_t\n * ctypedef npy_float64    float64_t             # <<<<<<<<<<<<<<\n * #ctypedef npy_float80    float80_t\n * #ctypedef npy_float128   float128_t\n */\ntypedef npy_float64 __pyx_t_5numpy_float64_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":747\n * # The int types are mapped a bit surprising --\n * # numpy.int corresponds to 'l' and numpy.long to 'q'\n * ctypedef npy_long       int_t             # <<<<<<<<<<<<<<\n * ctypedef npy_longlong   long_t\n * ctypedef npy_longlong   longlong_t\n */\ntypedef npy_long __pyx_t_5numpy_int_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":748\n * # numpy.int corresponds to 'l' and numpy.long to 'q'\n * ctypedef npy_long       int_t\n * ctypedef npy_longlong   long_t             # <<<<<<<<<<<<<<\n * ctypedef npy_longlong   longlong_t\n * \n */\ntypedef npy_longlong __pyx_t_5numpy_long_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":749\n * ctypedef npy_long       int_t\n * ctypedef npy_longlong   long_t\n * ctypedef npy_longlong   longlong_t             # <<<<<<<<<<<<<<\n * \n * ctypedef npy_ulong      uint_t\n */\ntypedef npy_longlong __pyx_t_5numpy_longlong_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":751\n * ctypedef npy_longlong   longlong_t\n * \n * ctypedef npy_ulong      uint_t             # <<<<<<<<<<<<<<\n * ctypedef npy_ulonglong  ulong_t\n * ctypedef npy_ulonglong  ulonglong_t\n */\ntypedef npy_ulong __pyx_t_5numpy_uint_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":752\n * \n * ctypedef npy_ulong      uint_t\n * ctypedef npy_ulonglong  ulong_t             # <<<<<<<<<<<<<<\n * ctypedef npy_ulonglong  ulonglong_t\n * \n */\ntypedef npy_ulonglong __pyx_t_5numpy_ulong_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":753\n * ctypedef npy_ulong      uint_t\n * ctypedef npy_ulonglong  ulong_t\n * ctypedef npy_ulonglong  ulonglong_t             # <<<<<<<<<<<<<<\n * \n * ctypedef npy_intp       intp_t\n */\ntypedef npy_ulonglong __pyx_t_5numpy_ulonglong_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":755\n * ctypedef npy_ulonglong  ulonglong_t\n * \n * ctypedef npy_intp       intp_t             # <<<<<<<<<<<<<<\n * ctypedef npy_uintp      uintp_t\n * \n */\ntypedef npy_intp __pyx_t_5numpy_intp_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":756\n * \n * ctypedef npy_intp       intp_t\n * ctypedef npy_uintp      uintp_t             # <<<<<<<<<<<<<<\n * \n * ctypedef npy_double     float_t\n */\ntypedef npy_uintp __pyx_t_5numpy_uintp_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":758\n * ctypedef npy_uintp      uintp_t\n * \n * ctypedef npy_double     float_t             # <<<<<<<<<<<<<<\n * ctypedef npy_double     double_t\n * ctypedef npy_longdouble longdouble_t\n */\ntypedef npy_double __pyx_t_5numpy_float_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":759\n * \n * ctypedef npy_double     float_t\n * ctypedef npy_double     double_t             # <<<<<<<<<<<<<<\n * ctypedef npy_longdouble longdouble_t\n * \n */\ntypedef npy_double __pyx_t_5numpy_double_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":760\n * ctypedef npy_double     float_t\n * ctypedef npy_double     double_t\n * ctypedef npy_longdouble longdouble_t             # <<<<<<<<<<<<<<\n * \n * ctypedef npy_cfloat      cfloat_t\n */\ntypedef npy_longdouble __pyx_t_5numpy_longdouble_t;\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    typedef ::std::complex< float > __pyx_t_float_complex;\n  #else\n    typedef float _Complex __pyx_t_float_complex;\n  #endif\n#else\n    typedef struct { float real, imag; } __pyx_t_float_complex;\n#endif\n\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    typedef ::std::complex< double > __pyx_t_double_complex;\n  #else\n    typedef double _Complex __pyx_t_double_complex;\n  #endif\n#else\n    typedef struct { double real, imag; } __pyx_t_double_complex;\n#endif\n\n\n/*--- Type declarations ---*/\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":762\n * ctypedef npy_longdouble longdouble_t\n * \n * ctypedef npy_cfloat      cfloat_t             # <<<<<<<<<<<<<<\n * ctypedef npy_cdouble     cdouble_t\n * ctypedef npy_clongdouble clongdouble_t\n */\ntypedef npy_cfloat __pyx_t_5numpy_cfloat_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":763\n * \n * ctypedef npy_cfloat      cfloat_t\n * ctypedef npy_cdouble     cdouble_t             # <<<<<<<<<<<<<<\n * ctypedef npy_clongdouble clongdouble_t\n * \n */\ntypedef npy_cdouble __pyx_t_5numpy_cdouble_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":764\n * ctypedef npy_cfloat      cfloat_t\n * ctypedef npy_cdouble     cdouble_t\n * ctypedef npy_clongdouble clongdouble_t             # <<<<<<<<<<<<<<\n * \n * ctypedef npy_cdouble     complex_t\n */\ntypedef npy_clongdouble __pyx_t_5numpy_clongdouble_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":766\n * ctypedef npy_clongdouble clongdouble_t\n * \n * ctypedef npy_cdouble     complex_t             # <<<<<<<<<<<<<<\n * \n * cdef inline object PyArray_MultiIterNew1(a):\n */\ntypedef npy_cdouble __pyx_t_5numpy_complex_t;\n#ifndef CYTHON_REFNANNY\n  #define CYTHON_REFNANNY 0\n#endif\n#if CYTHON_REFNANNY\n  typedef struct {\n    void (*INCREF)(void*, PyObject*, int);\n    void (*DECREF)(void*, PyObject*, int);\n    void (*GOTREF)(void*, PyObject*, int);\n    void (*GIVEREF)(void*, PyObject*, int);\n    void* (*SetupContext)(const char*, int, const char*);\n    void (*FinishContext)(void**);\n  } __Pyx_RefNannyAPIStruct;\n  static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL;\n  static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname); /*proto*/\n  #define __Pyx_RefNannyDeclarations void *__pyx_refnanny = NULL;\n#ifdef WITH_THREAD\n  #define __Pyx_RefNannySetupContext(name, acquire_gil) \\\n          if (acquire_gil) { \\\n              PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure(); \\\n              __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__); \\\n              PyGILState_Release(__pyx_gilstate_save); \\\n          } else { \\\n              __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__); \\\n          }\n#else\n  #define __Pyx_RefNannySetupContext(name, acquire_gil) \\\n          __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__)\n#endif\n  #define __Pyx_RefNannyFinishContext() \\\n          __Pyx_RefNanny->FinishContext(&__pyx_refnanny)\n  #define __Pyx_INCREF(r)  __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__)\n  #define __Pyx_DECREF(r)  __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__)\n  #define __Pyx_GOTREF(r)  __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__)\n  #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__)\n  #define __Pyx_XINCREF(r)  do { if((r) != NULL) {__Pyx_INCREF(r); }} while(0)\n  #define __Pyx_XDECREF(r)  do { if((r) != NULL) {__Pyx_DECREF(r); }} while(0)\n  #define __Pyx_XGOTREF(r)  do { if((r) != NULL) {__Pyx_GOTREF(r); }} while(0)\n  #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);}} while(0)\n#else\n  #define __Pyx_RefNannyDeclarations\n  #define __Pyx_RefNannySetupContext(name, acquire_gil)\n  #define __Pyx_RefNannyFinishContext()\n  #define __Pyx_INCREF(r) Py_INCREF(r)\n  #define __Pyx_DECREF(r) Py_DECREF(r)\n  #define __Pyx_GOTREF(r)\n  #define __Pyx_GIVEREF(r)\n  #define __Pyx_XINCREF(r) Py_XINCREF(r)\n  #define __Pyx_XDECREF(r) Py_XDECREF(r)\n  #define __Pyx_XGOTREF(r)\n  #define __Pyx_XGIVEREF(r)\n#endif /* CYTHON_REFNANNY */\n#define __Pyx_XDECREF_SET(r, v) do {                            \\\n        PyObject *tmp = (PyObject *) r;                         \\\n        r = v; __Pyx_XDECREF(tmp);                              \\\n    } while (0)\n#define __Pyx_DECREF_SET(r, v) do {                             \\\n        PyObject *tmp = (PyObject *) r;                         \\\n        r = v; __Pyx_DECREF(tmp);                               \\\n    } while (0)\n#define __Pyx_CLEAR(r)    do { PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);} while(0)\n#define __Pyx_XCLEAR(r)   do { if((r) != NULL) {PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);}} while(0)\n\n#if CYTHON_COMPILING_IN_CPYTHON\nstatic CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name) {\n    PyTypeObject* tp = Py_TYPE(obj);\n    if (likely(tp->tp_getattro))\n        return tp->tp_getattro(obj, attr_name);\n#if PY_MAJOR_VERSION < 3\n    if (likely(tp->tp_getattr))\n        return tp->tp_getattr(obj, PyString_AS_STRING(attr_name));\n#endif\n    return PyObject_GetAttr(obj, attr_name);\n}\n#else\n#define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n)\n#endif\n\nstatic PyObject *__Pyx_GetBuiltinName(PyObject *name); /*proto*/\n\nstatic void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,\n    Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/\n\nstatic void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); /*proto*/\n\nstatic int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \\\n    PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, \\\n    const char* function_name); /*proto*/\n\nstatic CYTHON_INLINE int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,\n    const char *name, int exact); /*proto*/\n\nstatic CYTHON_INLINE int  __Pyx_GetBufferAndValidate(Py_buffer* buf, PyObject* obj,\n    __Pyx_TypeInfo* dtype, int flags, int nd, int cast, __Pyx_BufFmt_StackElem* stack);\nstatic CYTHON_INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info);\n\nstatic CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/\n\n#if CYTHON_COMPILING_IN_CPYTHON\nstatic CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw); /*proto*/\n#else\n#define __Pyx_PyObject_Call(func, arg, kw) PyObject_Call(func, arg, kw)\n#endif\n\nstatic CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name); /*proto*/\n\nstatic void __Pyx_RaiseBufferIndexError(int axis); /*proto*/\n\n#define __Pyx_BufPtrStrided1d(type, buf, i0, s0) (type)((char*)buf + i0 * s0)\n#if CYTHON_COMPILING_IN_CPYTHON\nstatic CYTHON_INLINE int __Pyx_PyList_Append(PyObject* list, PyObject* x) {\n    PyListObject* L = (PyListObject*) list;\n    Py_ssize_t len = Py_SIZE(list);\n    if (likely(L->allocated > len) & likely(len > (L->allocated >> 1))) {\n        Py_INCREF(x);\n        PyList_SET_ITEM(list, len, x);\n        Py_SIZE(list) = len+1;\n        return 0;\n    }\n    return PyList_Append(list, x);\n}\n#else\n#define __Pyx_PyList_Append(L,x) PyList_Append(L,x)\n#endif\n\n#ifndef __PYX_FORCE_INIT_THREADS\n  #define __PYX_FORCE_INIT_THREADS 0\n#endif\n\nstatic CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/\nstatic CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/\n\nstatic void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause); /*proto*/\n\nstatic CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);\n\nstatic CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);\n\nstatic CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void);\n\ntypedef struct {\n  Py_ssize_t shape, strides, suboffsets;\n} __Pyx_Buf_DimInfo;\ntypedef struct {\n  size_t refcount;\n  Py_buffer pybuffer;\n} __Pyx_Buffer;\ntypedef struct {\n  __Pyx_Buffer *rcbuffer;\n  char *data;\n  __Pyx_Buf_DimInfo diminfo[8];\n} __Pyx_LocalBuf_ND;\n\n#if PY_MAJOR_VERSION < 3\n    static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags);\n    static void __Pyx_ReleaseBuffer(Py_buffer *view);\n#else\n    #define __Pyx_GetBuffer PyObject_GetBuffer\n    #define __Pyx_ReleaseBuffer PyBuffer_Release\n#endif\n\n\nstatic Py_ssize_t __Pyx_zeros[] = {0, 0, 0, 0, 0, 0, 0, 0};\nstatic Py_ssize_t __Pyx_minusones[] = {-1, -1, -1, -1, -1, -1, -1, -1};\n\nstatic PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); /*proto*/\n\nstatic CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value);\n\nstatic CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *);\n\nstatic CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value);\n\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    #define __Pyx_CREAL(z) ((z).real())\n    #define __Pyx_CIMAG(z) ((z).imag())\n  #else\n    #define __Pyx_CREAL(z) (__real__(z))\n    #define __Pyx_CIMAG(z) (__imag__(z))\n  #endif\n#else\n    #define __Pyx_CREAL(z) ((z).real)\n    #define __Pyx_CIMAG(z) ((z).imag)\n#endif\n#if (defined(_WIN32) || defined(__clang__)) && defined(__cplusplus) && CYTHON_CCOMPLEX\n    #define __Pyx_SET_CREAL(z,x) ((z).real(x))\n    #define __Pyx_SET_CIMAG(z,y) ((z).imag(y))\n#else\n    #define __Pyx_SET_CREAL(z,x) __Pyx_CREAL(z) = (x)\n    #define __Pyx_SET_CIMAG(z,y) __Pyx_CIMAG(z) = (y)\n#endif\n\nstatic CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float, float);\n\n#if CYTHON_CCOMPLEX\n    #define __Pyx_c_eqf(a, b)   ((a)==(b))\n    #define __Pyx_c_sumf(a, b)  ((a)+(b))\n    #define __Pyx_c_difff(a, b) ((a)-(b))\n    #define __Pyx_c_prodf(a, b) ((a)*(b))\n    #define __Pyx_c_quotf(a, b) ((a)/(b))\n    #define __Pyx_c_negf(a)     (-(a))\n  #ifdef __cplusplus\n    #define __Pyx_c_is_zerof(z) ((z)==(float)0)\n    #define __Pyx_c_conjf(z)    (::std::conj(z))\n    #if 1\n        #define __Pyx_c_absf(z)     (::std::abs(z))\n        #define __Pyx_c_powf(a, b)  (::std::pow(a, b))\n    #endif\n  #else\n    #define __Pyx_c_is_zerof(z) ((z)==0)\n    #define __Pyx_c_conjf(z)    (conjf(z))\n    #if 1\n        #define __Pyx_c_absf(z)     (cabsf(z))\n        #define __Pyx_c_powf(a, b)  (cpowf(a, b))\n    #endif\n #endif\n#else\n    static CYTHON_INLINE int __Pyx_c_eqf(__pyx_t_float_complex, __pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_sumf(__pyx_t_float_complex, __pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_difff(__pyx_t_float_complex, __pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_prodf(__pyx_t_float_complex, __pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_quotf(__pyx_t_float_complex, __pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_negf(__pyx_t_float_complex);\n    static CYTHON_INLINE int __Pyx_c_is_zerof(__pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_conjf(__pyx_t_float_complex);\n    #if 1\n        static CYTHON_INLINE float __Pyx_c_absf(__pyx_t_float_complex);\n        static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_powf(__pyx_t_float_complex, __pyx_t_float_complex);\n    #endif\n#endif\n\nstatic CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double, double);\n\n#if CYTHON_CCOMPLEX\n    #define __Pyx_c_eq(a, b)   ((a)==(b))\n    #define __Pyx_c_sum(a, b)  ((a)+(b))\n    #define __Pyx_c_diff(a, b) ((a)-(b))\n    #define __Pyx_c_prod(a, b) ((a)*(b))\n    #define __Pyx_c_quot(a, b) ((a)/(b))\n    #define __Pyx_c_neg(a)     (-(a))\n  #ifdef __cplusplus\n    #define __Pyx_c_is_zero(z) ((z)==(double)0)\n    #define __Pyx_c_conj(z)    (::std::conj(z))\n    #if 1\n        #define __Pyx_c_abs(z)     (::std::abs(z))\n        #define __Pyx_c_pow(a, b)  (::std::pow(a, b))\n    #endif\n  #else\n    #define __Pyx_c_is_zero(z) ((z)==0)\n    #define __Pyx_c_conj(z)    (conj(z))\n    #if 1\n        #define __Pyx_c_abs(z)     (cabs(z))\n        #define __Pyx_c_pow(a, b)  (cpow(a, b))\n    #endif\n #endif\n#else\n    static CYTHON_INLINE int __Pyx_c_eq(__pyx_t_double_complex, __pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_sum(__pyx_t_double_complex, __pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_diff(__pyx_t_double_complex, __pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_prod(__pyx_t_double_complex, __pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_quot(__pyx_t_double_complex, __pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_neg(__pyx_t_double_complex);\n    static CYTHON_INLINE int __Pyx_c_is_zero(__pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_conj(__pyx_t_double_complex);\n    #if 1\n        static CYTHON_INLINE double __Pyx_c_abs(__pyx_t_double_complex);\n        static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_pow(__pyx_t_double_complex, __pyx_t_double_complex);\n    #endif\n#endif\n\nstatic CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *);\n\nstatic int __Pyx_check_binary_version(void);\n\n#if !defined(__Pyx_PyIdentifier_FromString)\n#if PY_MAJOR_VERSION < 3\n  #define __Pyx_PyIdentifier_FromString(s) PyString_FromString(s)\n#else\n  #define __Pyx_PyIdentifier_FromString(s) PyUnicode_FromString(s)\n#endif\n#endif\n\nstatic PyObject *__Pyx_ImportModule(const char *name); /*proto*/\n\nstatic PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, size_t size, int strict);  /*proto*/\n\ntypedef struct {\n    int code_line;\n    PyCodeObject* code_object;\n} __Pyx_CodeObjectCacheEntry;\nstruct __Pyx_CodeObjectCache {\n    int count;\n    int max_count;\n    __Pyx_CodeObjectCacheEntry* entries;\n};\nstatic struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL};\nstatic int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line);\nstatic PyCodeObject *__pyx_find_code_object(int code_line);\nstatic void __pyx_insert_code_object(int code_line, PyCodeObject* code_object);\n\nstatic void __Pyx_AddTraceback(const char *funcname, int c_line,\n                               int py_line, const char *filename); /*proto*/\n\nstatic int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/\n\n\n/* Module declarations from 'cpython.buffer' */\n\n/* Module declarations from 'cpython.ref' */\n\n/* Module declarations from 'libc.string' */\n\n/* Module declarations from 'libc.stdio' */\n\n/* Module declarations from 'cpython.object' */\n\n/* Module declarations from '__builtin__' */\n\n/* Module declarations from 'cpython.type' */\nstatic PyTypeObject *__pyx_ptype_7cpython_4type_type = 0;\n\n/* Module declarations from 'libc.stdlib' */\n\n/* Module declarations from 'numpy' */\n\n/* Module declarations from 'numpy' */\nstatic PyTypeObject *__pyx_ptype_5numpy_dtype = 0;\nstatic PyTypeObject *__pyx_ptype_5numpy_flatiter = 0;\nstatic PyTypeObject *__pyx_ptype_5numpy_broadcast = 0;\nstatic PyTypeObject *__pyx_ptype_5numpy_ndarray = 0;\nstatic PyTypeObject *__pyx_ptype_5numpy_ufunc = 0;\nstatic CYTHON_INLINE char *__pyx_f_5numpy__util_dtypestring(PyArray_Descr *, char *, char *, int *); /*proto*/\n\n/* Module declarations from 'nms.cpu_nms' */\nstatic CYTHON_INLINE __pyx_t_5numpy_float32_t __pyx_f_3nms_7cpu_nms_max(__pyx_t_5numpy_float32_t, __pyx_t_5numpy_float32_t); /*proto*/\nstatic CYTHON_INLINE __pyx_t_5numpy_float32_t __pyx_f_3nms_7cpu_nms_min(__pyx_t_5numpy_float32_t, __pyx_t_5numpy_float32_t); /*proto*/\nstatic __Pyx_TypeInfo __Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t = { \"float32_t\", NULL, sizeof(__pyx_t_5numpy_float32_t), { 0 }, 0, 'R', 0, 0 };\nstatic __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 };\n#define __Pyx_MODULE_NAME \"nms.cpu_nms\"\nint __pyx_module_is_main_nms__cpu_nms = 0;\n\n/* Implementation of 'nms.cpu_nms' */\nstatic PyObject *__pyx_builtin_range;\nstatic PyObject *__pyx_builtin_ValueError;\nstatic PyObject *__pyx_builtin_RuntimeError;\nstatic PyObject *__pyx_pf_3nms_7cpu_nms_cpu_nms(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_dets, PyObject *__pyx_v_thresh); /* proto */\nstatic int __pyx_pf_5numpy_7ndarray___getbuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags); /* proto */\nstatic void __pyx_pf_5numpy_7ndarray_2__releasebuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info); /* proto */\nstatic char __pyx_k_B[] = \"B\";\nstatic char __pyx_k_H[] = \"H\";\nstatic char __pyx_k_I[] = \"I\";\nstatic char __pyx_k_L[] = \"L\";\nstatic char __pyx_k_O[] = \"O\";\nstatic char __pyx_k_Q[] = \"Q\";\nstatic char __pyx_k_b[] = \"b\";\nstatic char __pyx_k_d[] = \"d\";\nstatic char __pyx_k_f[] = \"f\";\nstatic char __pyx_k_g[] = \"g\";\nstatic char __pyx_k_h[] = \"h\";\nstatic char __pyx_k_i[] = \"i\";\nstatic char __pyx_k_j[] = \"_j\";\nstatic char __pyx_k_l[] = \"l\";\nstatic char __pyx_k_q[] = \"q\";\nstatic char __pyx_k_w[] = \"w\";\nstatic char __pyx_k_Zd[] = \"Zd\";\nstatic char __pyx_k_Zf[] = \"Zf\";\nstatic char __pyx_k_Zg[] = \"Zg\";\nstatic char __pyx_k_np[] = \"np\";\nstatic char __pyx_k_x1[] = \"x1\";\nstatic char __pyx_k_x2[] = \"x2\";\nstatic char __pyx_k_y1[] = \"y1\";\nstatic char __pyx_k_y2[] = \"y2\";\nstatic char __pyx_k_i_2[] = \"_i\";\nstatic char __pyx_k_int[] = \"int\";\nstatic char __pyx_k_ix1[] = \"ix1\";\nstatic char __pyx_k_ix2[] = \"ix2\";\nstatic char __pyx_k_iy1[] = \"iy1\";\nstatic char __pyx_k_iy2[] = \"iy2\";\nstatic char __pyx_k_j_2[] = \"j\";\nstatic char __pyx_k_ovr[] = \"ovr\";\nstatic char __pyx_k_xx1[] = \"xx1\";\nstatic char __pyx_k_xx2[] = \"xx2\";\nstatic char __pyx_k_yy1[] = \"yy1\";\nstatic char __pyx_k_yy2[] = \"yy2\";\nstatic char __pyx_k_dets[] = \"dets\";\nstatic char __pyx_k_keep[] = \"keep\";\nstatic char __pyx_k_main[] = \"__main__\";\nstatic char __pyx_k_test[] = \"__test__\";\nstatic char __pyx_k_areas[] = \"areas\";\nstatic char __pyx_k_dtype[] = \"dtype\";\nstatic char __pyx_k_iarea[] = \"iarea\";\nstatic char __pyx_k_inter[] = \"inter\";\nstatic char __pyx_k_ndets[] = \"ndets\";\nstatic char __pyx_k_numpy[] = \"numpy\";\nstatic char __pyx_k_order[] = \"order\";\nstatic char __pyx_k_range[] = \"range\";\nstatic char __pyx_k_zeros[] = \"zeros\";\nstatic char __pyx_k_import[] = \"__import__\";\nstatic char __pyx_k_scores[] = \"scores\";\nstatic char __pyx_k_thresh[] = \"thresh\";\nstatic char __pyx_k_argsort[] = \"argsort\";\nstatic char __pyx_k_cpu_nms[] = \"cpu_nms\";\nstatic char __pyx_k_ValueError[] = \"ValueError\";\nstatic char __pyx_k_suppressed[] = \"suppressed\";\nstatic char __pyx_k_nms_cpu_nms[] = \"nms.cpu_nms\";\nstatic char __pyx_k_RuntimeError[] = \"RuntimeError\";\nstatic char __pyx_k_pyx_getbuffer[] = \"__pyx_getbuffer\";\nstatic char __pyx_k_pyx_releasebuffer[] = \"__pyx_releasebuffer\";\nstatic char __pyx_k_ndarray_is_not_C_contiguous[] = \"ndarray is not C contiguous\";\nstatic char __pyx_k_nfs_yoda_xinleic_Inf_Code_Faste[] = \"/nfs.yoda/xinleic/Inf/Code/Faster-RCNN_TF/lib/nms/cpu_nms.pyx\";\nstatic char __pyx_k_unknown_dtype_code_in_numpy_pxd[] = \"unknown dtype code in numpy.pxd (%d)\";\nstatic char __pyx_k_Format_string_allocated_too_shor[] = \"Format string allocated too short, see comment in numpy.pxd\";\nstatic char __pyx_k_Non_native_byte_order_not_suppor[] = \"Non-native byte order not supported\";\nstatic char __pyx_k_ndarray_is_not_Fortran_contiguou[] = \"ndarray is not Fortran contiguous\";\nstatic char __pyx_k_Format_string_allocated_too_shor_2[] = \"Format string allocated too short.\";\nstatic PyObject *__pyx_kp_u_Format_string_allocated_too_shor;\nstatic PyObject *__pyx_kp_u_Format_string_allocated_too_shor_2;\nstatic PyObject *__pyx_kp_u_Non_native_byte_order_not_suppor;\nstatic PyObject *__pyx_n_s_RuntimeError;\nstatic PyObject *__pyx_n_s_ValueError;\nstatic PyObject *__pyx_n_s_areas;\nstatic PyObject *__pyx_n_s_argsort;\nstatic PyObject *__pyx_n_s_cpu_nms;\nstatic PyObject *__pyx_n_s_dets;\nstatic PyObject *__pyx_n_s_dtype;\nstatic PyObject *__pyx_n_s_h;\nstatic PyObject *__pyx_n_s_i;\nstatic PyObject *__pyx_n_s_i_2;\nstatic PyObject *__pyx_n_s_iarea;\nstatic PyObject *__pyx_n_s_import;\nstatic PyObject *__pyx_n_s_int;\nstatic PyObject *__pyx_n_s_inter;\nstatic PyObject *__pyx_n_s_ix1;\nstatic PyObject *__pyx_n_s_ix2;\nstatic PyObject *__pyx_n_s_iy1;\nstatic PyObject *__pyx_n_s_iy2;\nstatic PyObject *__pyx_n_s_j;\nstatic PyObject *__pyx_n_s_j_2;\nstatic PyObject *__pyx_n_s_keep;\nstatic PyObject *__pyx_n_s_main;\nstatic PyObject *__pyx_kp_u_ndarray_is_not_C_contiguous;\nstatic PyObject *__pyx_kp_u_ndarray_is_not_Fortran_contiguou;\nstatic PyObject *__pyx_n_s_ndets;\nstatic PyObject *__pyx_kp_s_nfs_yoda_xinleic_Inf_Code_Faste;\nstatic PyObject *__pyx_n_s_nms_cpu_nms;\nstatic PyObject *__pyx_n_s_np;\nstatic PyObject *__pyx_n_s_numpy;\nstatic PyObject *__pyx_n_s_order;\nstatic PyObject *__pyx_n_s_ovr;\nstatic PyObject *__pyx_n_s_pyx_getbuffer;\nstatic PyObject *__pyx_n_s_pyx_releasebuffer;\nstatic PyObject *__pyx_n_s_range;\nstatic PyObject *__pyx_n_s_scores;\nstatic PyObject *__pyx_n_s_suppressed;\nstatic PyObject *__pyx_n_s_test;\nstatic PyObject *__pyx_n_s_thresh;\nstatic PyObject *__pyx_kp_u_unknown_dtype_code_in_numpy_pxd;\nstatic PyObject *__pyx_n_s_w;\nstatic PyObject *__pyx_n_s_x1;\nstatic PyObject *__pyx_n_s_x2;\nstatic PyObject *__pyx_n_s_xx1;\nstatic PyObject *__pyx_n_s_xx2;\nstatic PyObject *__pyx_n_s_y1;\nstatic PyObject *__pyx_n_s_y2;\nstatic PyObject *__pyx_n_s_yy1;\nstatic PyObject *__pyx_n_s_yy2;\nstatic PyObject *__pyx_n_s_zeros;\nstatic PyObject *__pyx_int_0;\nstatic PyObject *__pyx_int_1;\nstatic PyObject *__pyx_int_2;\nstatic PyObject *__pyx_int_3;\nstatic PyObject *__pyx_int_4;\nstatic PyObject *__pyx_int_neg_1;\nstatic PyObject *__pyx_slice_;\nstatic PyObject *__pyx_slice__3;\nstatic PyObject *__pyx_slice__5;\nstatic PyObject *__pyx_slice__7;\nstatic PyObject *__pyx_slice__9;\nstatic PyObject *__pyx_tuple__2;\nstatic PyObject *__pyx_tuple__4;\nstatic PyObject *__pyx_tuple__6;\nstatic PyObject *__pyx_tuple__8;\nstatic PyObject *__pyx_slice__11;\nstatic PyObject *__pyx_tuple__10;\nstatic PyObject *__pyx_tuple__12;\nstatic PyObject *__pyx_tuple__13;\nstatic PyObject *__pyx_tuple__14;\nstatic PyObject *__pyx_tuple__15;\nstatic PyObject *__pyx_tuple__16;\nstatic PyObject *__pyx_tuple__17;\nstatic PyObject *__pyx_tuple__18;\nstatic PyObject *__pyx_codeobj__19;\n\n/* \"nms/cpu_nms.pyx\":11\n * cimport numpy as np\n * \n * cdef inline np.float32_t max(np.float32_t a, np.float32_t b):             # <<<<<<<<<<<<<<\n *     return a if a >= b else b\n * \n */\n\nstatic 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) {\n  __pyx_t_5numpy_float32_t __pyx_r;\n  __Pyx_RefNannyDeclarations\n  __pyx_t_5numpy_float32_t __pyx_t_1;\n  __Pyx_RefNannySetupContext(\"max\", 0);\n\n  /* \"nms/cpu_nms.pyx\":12\n * \n * cdef inline np.float32_t max(np.float32_t a, np.float32_t b):\n *     return a if a >= b else b             # <<<<<<<<<<<<<<\n * \n * cdef inline np.float32_t min(np.float32_t a, np.float32_t b):\n */\n  if (((__pyx_v_a >= __pyx_v_b) != 0)) {\n    __pyx_t_1 = __pyx_v_a;\n  } else {\n    __pyx_t_1 = __pyx_v_b;\n  }\n  __pyx_r = __pyx_t_1;\n  goto __pyx_L0;\n\n  /* \"nms/cpu_nms.pyx\":11\n * cimport numpy as np\n * \n * cdef inline np.float32_t max(np.float32_t a, np.float32_t b):             # <<<<<<<<<<<<<<\n *     return a if a >= b else b\n * \n */\n\n  /* function exit code */\n  __pyx_L0:;\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"nms/cpu_nms.pyx\":14\n *     return a if a >= b else b\n * \n * cdef inline np.float32_t min(np.float32_t a, np.float32_t b):             # <<<<<<<<<<<<<<\n *     return a if a <= b else b\n * \n */\n\nstatic 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) {\n  __pyx_t_5numpy_float32_t __pyx_r;\n  __Pyx_RefNannyDeclarations\n  __pyx_t_5numpy_float32_t __pyx_t_1;\n  __Pyx_RefNannySetupContext(\"min\", 0);\n\n  /* \"nms/cpu_nms.pyx\":15\n * \n * cdef inline np.float32_t min(np.float32_t a, np.float32_t b):\n *     return a if a <= b else b             # <<<<<<<<<<<<<<\n * \n * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):\n */\n  if (((__pyx_v_a <= __pyx_v_b) != 0)) {\n    __pyx_t_1 = __pyx_v_a;\n  } else {\n    __pyx_t_1 = __pyx_v_b;\n  }\n  __pyx_r = __pyx_t_1;\n  goto __pyx_L0;\n\n  /* \"nms/cpu_nms.pyx\":14\n *     return a if a >= b else b\n * \n * cdef inline np.float32_t min(np.float32_t a, np.float32_t b):             # <<<<<<<<<<<<<<\n *     return a if a <= b else b\n * \n */\n\n  /* function exit code */\n  __pyx_L0:;\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"nms/cpu_nms.pyx\":17\n *     return a if a <= b else b\n * \n * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n */\n\n/* Python wrapper */\nstatic PyObject *__pyx_pw_3nms_7cpu_nms_1cpu_nms(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\nstatic PyMethodDef __pyx_mdef_3nms_7cpu_nms_1cpu_nms = {__Pyx_NAMESTR(\"cpu_nms\"), (PyCFunction)__pyx_pw_3nms_7cpu_nms_1cpu_nms, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)};\nstatic PyObject *__pyx_pw_3nms_7cpu_nms_1cpu_nms(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n  PyArrayObject *__pyx_v_dets = 0;\n  PyObject *__pyx_v_thresh = 0;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  PyObject *__pyx_r = 0;\n  __Pyx_RefNannyDeclarations\n  __Pyx_RefNannySetupContext(\"cpu_nms (wrapper)\", 0);\n  {\n    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_dets,&__pyx_n_s_thresh,0};\n    PyObject* values[2] = {0,0};\n    if (unlikely(__pyx_kwds)) {\n      Py_ssize_t kw_args;\n      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n      switch (pos_args) {\n        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n        case  0: break;\n        default: goto __pyx_L5_argtuple_error;\n      }\n      kw_args = PyDict_Size(__pyx_kwds);\n      switch (pos_args) {\n        case  0:\n        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_dets)) != 0)) kw_args--;\n        else goto __pyx_L5_argtuple_error;\n        case  1:\n        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_thresh)) != 0)) kw_args--;\n        else {\n          __Pyx_RaiseArgtupleInvalid(\"cpu_nms\", 1, 2, 2, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n        }\n      }\n      if (unlikely(kw_args > 0)) {\n        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"cpu_nms\") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n      }\n    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {\n      goto __pyx_L5_argtuple_error;\n    } else {\n      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n    }\n    __pyx_v_dets = ((PyArrayObject *)values[0]);\n    __pyx_v_thresh = ((PyObject*)values[1]);\n  }\n  goto __pyx_L4_argument_unpacking_done;\n  __pyx_L5_argtuple_error:;\n  __Pyx_RaiseArgtupleInvalid(\"cpu_nms\", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n  __pyx_L3_error:;\n  __Pyx_AddTraceback(\"nms.cpu_nms.cpu_nms\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __Pyx_RefNannyFinishContext();\n  return NULL;\n  __pyx_L4_argument_unpacking_done:;\n  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_dets), __pyx_ptype_5numpy_ndarray, 1, \"dets\", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_thresh), (&PyFloat_Type), 1, \"thresh\", 1))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_r = __pyx_pf_3nms_7cpu_nms_cpu_nms(__pyx_self, __pyx_v_dets, __pyx_v_thresh);\n\n  /* function exit code */\n  goto __pyx_L0;\n  __pyx_L1_error:;\n  __pyx_r = NULL;\n  __pyx_L0:;\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\nstatic PyObject *__pyx_pf_3nms_7cpu_nms_cpu_nms(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_dets, PyObject *__pyx_v_thresh) {\n  PyArrayObject *__pyx_v_x1 = 0;\n  PyArrayObject *__pyx_v_y1 = 0;\n  PyArrayObject *__pyx_v_x2 = 0;\n  PyArrayObject *__pyx_v_y2 = 0;\n  PyArrayObject *__pyx_v_scores = 0;\n  PyArrayObject *__pyx_v_areas = 0;\n  PyArrayObject *__pyx_v_order = 0;\n  int __pyx_v_ndets;\n  PyArrayObject *__pyx_v_suppressed = 0;\n  int __pyx_v__i;\n  int __pyx_v__j;\n  int __pyx_v_i;\n  int __pyx_v_j;\n  __pyx_t_5numpy_float32_t __pyx_v_ix1;\n  __pyx_t_5numpy_float32_t __pyx_v_iy1;\n  __pyx_t_5numpy_float32_t __pyx_v_ix2;\n  __pyx_t_5numpy_float32_t __pyx_v_iy2;\n  __pyx_t_5numpy_float32_t __pyx_v_iarea;\n  __pyx_t_5numpy_float32_t __pyx_v_xx1;\n  __pyx_t_5numpy_float32_t __pyx_v_yy1;\n  __pyx_t_5numpy_float32_t __pyx_v_xx2;\n  __pyx_t_5numpy_float32_t __pyx_v_yy2;\n  __pyx_t_5numpy_float32_t __pyx_v_w;\n  __pyx_t_5numpy_float32_t __pyx_v_h;\n  __pyx_t_5numpy_float32_t __pyx_v_inter;\n  __pyx_t_5numpy_float32_t __pyx_v_ovr;\n  PyObject *__pyx_v_keep = NULL;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_areas;\n  __Pyx_Buffer __pyx_pybuffer_areas;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_dets;\n  __Pyx_Buffer __pyx_pybuffer_dets;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_order;\n  __Pyx_Buffer __pyx_pybuffer_order;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_scores;\n  __Pyx_Buffer __pyx_pybuffer_scores;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_suppressed;\n  __Pyx_Buffer __pyx_pybuffer_suppressed;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_x1;\n  __Pyx_Buffer __pyx_pybuffer_x1;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_x2;\n  __Pyx_Buffer __pyx_pybuffer_x2;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_y1;\n  __Pyx_Buffer __pyx_pybuffer_y1;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_y2;\n  __Pyx_Buffer __pyx_pybuffer_y2;\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  PyArrayObject *__pyx_t_2 = NULL;\n  PyArrayObject *__pyx_t_3 = NULL;\n  PyArrayObject *__pyx_t_4 = NULL;\n  PyArrayObject *__pyx_t_5 = NULL;\n  PyArrayObject *__pyx_t_6 = NULL;\n  PyObject *__pyx_t_7 = NULL;\n  PyObject *__pyx_t_8 = NULL;\n  PyArrayObject *__pyx_t_9 = NULL;\n  PyArrayObject *__pyx_t_10 = NULL;\n  PyObject *__pyx_t_11 = NULL;\n  PyObject *__pyx_t_12 = NULL;\n  PyArrayObject *__pyx_t_13 = NULL;\n  int __pyx_t_14;\n  int __pyx_t_15;\n  int __pyx_t_16;\n  int __pyx_t_17;\n  int __pyx_t_18;\n  int __pyx_t_19;\n  int __pyx_t_20;\n  int __pyx_t_21;\n  int __pyx_t_22;\n  int __pyx_t_23;\n  int __pyx_t_24;\n  int __pyx_t_25;\n  int __pyx_t_26;\n  int __pyx_t_27;\n  int __pyx_t_28;\n  int __pyx_t_29;\n  int __pyx_t_30;\n  int __pyx_t_31;\n  int __pyx_t_32;\n  int __pyx_t_33;\n  int __pyx_t_34;\n  __pyx_t_5numpy_float32_t __pyx_t_35;\n  int __pyx_t_36;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"cpu_nms\", 0);\n  __pyx_pybuffer_x1.pybuffer.buf = NULL;\n  __pyx_pybuffer_x1.refcount = 0;\n  __pyx_pybuffernd_x1.data = NULL;\n  __pyx_pybuffernd_x1.rcbuffer = &__pyx_pybuffer_x1;\n  __pyx_pybuffer_y1.pybuffer.buf = NULL;\n  __pyx_pybuffer_y1.refcount = 0;\n  __pyx_pybuffernd_y1.data = NULL;\n  __pyx_pybuffernd_y1.rcbuffer = &__pyx_pybuffer_y1;\n  __pyx_pybuffer_x2.pybuffer.buf = NULL;\n  __pyx_pybuffer_x2.refcount = 0;\n  __pyx_pybuffernd_x2.data = NULL;\n  __pyx_pybuffernd_x2.rcbuffer = &__pyx_pybuffer_x2;\n  __pyx_pybuffer_y2.pybuffer.buf = NULL;\n  __pyx_pybuffer_y2.refcount = 0;\n  __pyx_pybuffernd_y2.data = NULL;\n  __pyx_pybuffernd_y2.rcbuffer = &__pyx_pybuffer_y2;\n  __pyx_pybuffer_scores.pybuffer.buf = NULL;\n  __pyx_pybuffer_scores.refcount = 0;\n  __pyx_pybuffernd_scores.data = NULL;\n  __pyx_pybuffernd_scores.rcbuffer = &__pyx_pybuffer_scores;\n  __pyx_pybuffer_areas.pybuffer.buf = NULL;\n  __pyx_pybuffer_areas.refcount = 0;\n  __pyx_pybuffernd_areas.data = NULL;\n  __pyx_pybuffernd_areas.rcbuffer = &__pyx_pybuffer_areas;\n  __pyx_pybuffer_order.pybuffer.buf = NULL;\n  __pyx_pybuffer_order.refcount = 0;\n  __pyx_pybuffernd_order.data = NULL;\n  __pyx_pybuffernd_order.rcbuffer = &__pyx_pybuffer_order;\n  __pyx_pybuffer_suppressed.pybuffer.buf = NULL;\n  __pyx_pybuffer_suppressed.refcount = 0;\n  __pyx_pybuffernd_suppressed.data = NULL;\n  __pyx_pybuffernd_suppressed.rcbuffer = &__pyx_pybuffer_suppressed;\n  __pyx_pybuffer_dets.pybuffer.buf = NULL;\n  __pyx_pybuffer_dets.refcount = 0;\n  __pyx_pybuffernd_dets.data = NULL;\n  __pyx_pybuffernd_dets.rcbuffer = &__pyx_pybuffer_dets;\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    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_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  }\n  __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];\n\n  /* \"nms/cpu_nms.pyx\":18\n * \n * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):\n *     cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n *     cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]\n */\n  __pyx_t_1 = PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_tuple__2); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __Pyx_GOTREF(__pyx_t_1);\n  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_2 = ((PyArrayObject *)__pyx_t_1);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    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)) {\n      __pyx_v_x1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_x1.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } 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];\n    }\n  }\n  __pyx_t_2 = 0;\n  __pyx_v_x1 = ((PyArrayObject *)__pyx_t_1);\n  __pyx_t_1 = 0;\n\n  /* \"nms/cpu_nms.pyx\":19\n * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):\n *     cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]\n *     cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]\n */\n  __pyx_t_1 = PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_tuple__4); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __Pyx_GOTREF(__pyx_t_1);\n  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_3 = ((PyArrayObject *)__pyx_t_1);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    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)) {\n      __pyx_v_y1 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_y1.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } 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];\n    }\n  }\n  __pyx_t_3 = 0;\n  __pyx_v_y1 = ((PyArrayObject *)__pyx_t_1);\n  __pyx_t_1 = 0;\n\n  /* \"nms/cpu_nms.pyx\":20\n *     cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n *     cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]\n *     cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4]\n */\n  __pyx_t_1 = PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_tuple__6); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __Pyx_GOTREF(__pyx_t_1);\n  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_4 = ((PyArrayObject *)__pyx_t_1);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    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)) {\n      __pyx_v_x2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_x2.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } 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];\n    }\n  }\n  __pyx_t_4 = 0;\n  __pyx_v_x2 = ((PyArrayObject *)__pyx_t_1);\n  __pyx_t_1 = 0;\n\n  /* \"nms/cpu_nms.pyx\":21\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n *     cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]\n *     cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4]\n * \n */\n  __pyx_t_1 = PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_tuple__8); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __Pyx_GOTREF(__pyx_t_1);\n  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_5 = ((PyArrayObject *)__pyx_t_1);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    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)) {\n      __pyx_v_y2 = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_y2.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } 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];\n    }\n  }\n  __pyx_t_5 = 0;\n  __pyx_v_y2 = ((PyArrayObject *)__pyx_t_1);\n  __pyx_t_1 = 0;\n\n  /* \"nms/cpu_nms.pyx\":22\n *     cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]\n *     cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]\n *     cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4]             # <<<<<<<<<<<<<<\n * \n *     cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1)\n */\n  __pyx_t_1 = PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_tuple__10); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __Pyx_GOTREF(__pyx_t_1);\n  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_6 = ((PyArrayObject *)__pyx_t_1);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    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)) {\n      __pyx_v_scores = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_scores.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } 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];\n    }\n  }\n  __pyx_t_6 = 0;\n  __pyx_v_scores = ((PyArrayObject *)__pyx_t_1);\n  __pyx_t_1 = 0;\n\n  /* \"nms/cpu_nms.pyx\":24\n *     cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4]\n * \n *     cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1)             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1]\n * \n */\n  __pyx_t_1 = PyNumber_Subtract(((PyObject *)__pyx_v_x2), ((PyObject *)__pyx_v_x1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_t_7 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_7);\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n  __pyx_t_1 = PyNumber_Subtract(((PyObject *)__pyx_v_y2), ((PyObject *)__pyx_v_y1)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_t_8 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_8);\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n  __pyx_t_1 = PyNumber_Multiply(__pyx_t_7, __pyx_t_8); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;\n  __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;\n  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_9 = ((PyArrayObject *)__pyx_t_1);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    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)) {\n      __pyx_v_areas = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_areas.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } 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];\n    }\n  }\n  __pyx_t_9 = 0;\n  __pyx_v_areas = ((PyArrayObject *)__pyx_t_1);\n  __pyx_t_1 = 0;\n\n  /* \"nms/cpu_nms.pyx\":25\n * \n *     cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1)\n *     cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1]             # <<<<<<<<<<<<<<\n * \n *     cdef int ndets = dets.shape[0]\n */\n  __pyx_t_1 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_scores), __pyx_n_s_argsort); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_t_8 = __Pyx_PyObject_Call(__pyx_t_1, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_8);\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n  __pyx_t_1 = PyObject_GetItem(__pyx_t_8, __pyx_slice__11); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __Pyx_GOTREF(__pyx_t_1);\n  __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;\n  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_10 = ((PyArrayObject *)__pyx_t_1);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    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)) {\n      __pyx_v_order = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_order.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } 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];\n    }\n  }\n  __pyx_t_10 = 0;\n  __pyx_v_order = ((PyArrayObject *)__pyx_t_1);\n  __pyx_t_1 = 0;\n\n  /* \"nms/cpu_nms.pyx\":27\n *     cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1]\n * \n *     cdef int ndets = dets.shape[0]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.int_t, ndim=1] suppressed = \\\n *             np.zeros((ndets), dtype=np.int)\n */\n  __pyx_v_ndets = (__pyx_v_dets->dimensions[0]);\n\n  /* \"nms/cpu_nms.pyx\":29\n *     cdef int ndets = dets.shape[0]\n *     cdef np.ndarray[np.int_t, ndim=1] suppressed = \\\n *             np.zeros((ndets), dtype=np.int)             # <<<<<<<<<<<<<<\n * \n *     # nominal indices\n */\n  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_t_8 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_zeros); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_8);\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_ndets); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_t_7 = PyTuple_New(1); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_7);\n  PyTuple_SET_ITEM(__pyx_t_7, 0, __pyx_t_1);\n  __Pyx_GIVEREF(__pyx_t_1);\n  __pyx_t_1 = 0;\n  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_t_11 = __Pyx_GetModuleGlobalName(__pyx_n_s_np); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_11);\n  __pyx_t_12 = __Pyx_PyObject_GetAttrStr(__pyx_t_11, __pyx_n_s_int); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_12);\n  __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0;\n  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_dtype, __pyx_t_12) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;\n  __pyx_t_12 = __Pyx_PyObject_Call(__pyx_t_8, __pyx_t_7, __pyx_t_1); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_12);\n  __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;\n  __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n  if (!(likely(((__pyx_t_12) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_12, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_13 = ((PyArrayObject *)__pyx_t_12);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    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)) {\n      __pyx_v_suppressed = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_suppressed.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } 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];\n    }\n  }\n  __pyx_t_13 = 0;\n  __pyx_v_suppressed = ((PyArrayObject *)__pyx_t_12);\n  __pyx_t_12 = 0;\n\n  /* \"nms/cpu_nms.pyx\":42\n *     cdef np.float32_t inter, ovr\n * \n *     keep = []             # <<<<<<<<<<<<<<\n *     for _i in range(ndets):\n *         i = order[_i]\n */\n  __pyx_t_12 = PyList_New(0); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_12);\n  __pyx_v_keep = ((PyObject*)__pyx_t_12);\n  __pyx_t_12 = 0;\n\n  /* \"nms/cpu_nms.pyx\":43\n * \n *     keep = []\n *     for _i in range(ndets):             # <<<<<<<<<<<<<<\n *         i = order[_i]\n *         if suppressed[i] == 1:\n */\n  __pyx_t_14 = __pyx_v_ndets;\n  for (__pyx_t_15 = 0; __pyx_t_15 < __pyx_t_14; __pyx_t_15+=1) {\n    __pyx_v__i = __pyx_t_15;\n\n    /* \"nms/cpu_nms.pyx\":44\n *     keep = []\n *     for _i in range(ndets):\n *         i = order[_i]             # <<<<<<<<<<<<<<\n *         if suppressed[i] == 1:\n *             continue\n */\n    __pyx_t_16 = __pyx_v__i;\n    __pyx_t_17 = -1;\n    if (__pyx_t_16 < 0) {\n      __pyx_t_16 += __pyx_pybuffernd_order.diminfo[0].shape;\n      if (unlikely(__pyx_t_16 < 0)) __pyx_t_17 = 0;\n    } else if (unlikely(__pyx_t_16 >= __pyx_pybuffernd_order.diminfo[0].shape)) __pyx_t_17 = 0;\n    if (unlikely(__pyx_t_17 != -1)) {\n      __Pyx_RaiseBufferIndexError(__pyx_t_17);\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n    __pyx_v_i = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int_t *, __pyx_pybuffernd_order.rcbuffer->pybuffer.buf, __pyx_t_16, __pyx_pybuffernd_order.diminfo[0].strides));\n\n    /* \"nms/cpu_nms.pyx\":45\n *     for _i in range(ndets):\n *         i = order[_i]\n *         if suppressed[i] == 1:             # <<<<<<<<<<<<<<\n *             continue\n *         keep.append(i)\n */\n    __pyx_t_17 = __pyx_v_i;\n    __pyx_t_18 = -1;\n    if (__pyx_t_17 < 0) {\n      __pyx_t_17 += __pyx_pybuffernd_suppressed.diminfo[0].shape;\n      if (unlikely(__pyx_t_17 < 0)) __pyx_t_18 = 0;\n    } else if (unlikely(__pyx_t_17 >= __pyx_pybuffernd_suppressed.diminfo[0].shape)) __pyx_t_18 = 0;\n    if (unlikely(__pyx_t_18 != -1)) {\n      __Pyx_RaiseBufferIndexError(__pyx_t_18);\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 45; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n    __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);\n    if (__pyx_t_19) {\n\n      /* \"nms/cpu_nms.pyx\":46\n *         i = order[_i]\n *         if suppressed[i] == 1:\n *             continue             # <<<<<<<<<<<<<<\n *         keep.append(i)\n *         ix1 = x1[i]\n */\n      goto __pyx_L3_continue;\n    }\n\n    /* \"nms/cpu_nms.pyx\":47\n *         if suppressed[i] == 1:\n *             continue\n *         keep.append(i)             # <<<<<<<<<<<<<<\n *         ix1 = x1[i]\n *         iy1 = y1[i]\n */\n    __pyx_t_12 = __Pyx_PyInt_From_int(__pyx_v_i); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_GOTREF(__pyx_t_12);\n    __pyx_t_20 = __Pyx_PyList_Append(__pyx_v_keep, __pyx_t_12); if (unlikely(__pyx_t_20 == -1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;\n\n    /* \"nms/cpu_nms.pyx\":48\n *             continue\n *         keep.append(i)\n *         ix1 = x1[i]             # <<<<<<<<<<<<<<\n *         iy1 = y1[i]\n *         ix2 = x2[i]\n */\n    __pyx_t_18 = __pyx_v_i;\n    __pyx_t_21 = -1;\n    if (__pyx_t_18 < 0) {\n      __pyx_t_18 += __pyx_pybuffernd_x1.diminfo[0].shape;\n      if (unlikely(__pyx_t_18 < 0)) __pyx_t_21 = 0;\n    } else if (unlikely(__pyx_t_18 >= __pyx_pybuffernd_x1.diminfo[0].shape)) __pyx_t_21 = 0;\n    if (unlikely(__pyx_t_21 != -1)) {\n      __Pyx_RaiseBufferIndexError(__pyx_t_21);\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n    __pyx_v_ix1 = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_x1.rcbuffer->pybuffer.buf, __pyx_t_18, __pyx_pybuffernd_x1.diminfo[0].strides));\n\n    /* \"nms/cpu_nms.pyx\":49\n *         keep.append(i)\n *         ix1 = x1[i]\n *         iy1 = y1[i]             # <<<<<<<<<<<<<<\n *         ix2 = x2[i]\n *         iy2 = y2[i]\n */\n    __pyx_t_21 = __pyx_v_i;\n    __pyx_t_22 = -1;\n    if (__pyx_t_21 < 0) {\n      __pyx_t_21 += __pyx_pybuffernd_y1.diminfo[0].shape;\n      if (unlikely(__pyx_t_21 < 0)) __pyx_t_22 = 0;\n    } else if (unlikely(__pyx_t_21 >= __pyx_pybuffernd_y1.diminfo[0].shape)) __pyx_t_22 = 0;\n    if (unlikely(__pyx_t_22 != -1)) {\n      __Pyx_RaiseBufferIndexError(__pyx_t_22);\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n    __pyx_v_iy1 = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_y1.rcbuffer->pybuffer.buf, __pyx_t_21, __pyx_pybuffernd_y1.diminfo[0].strides));\n\n    /* \"nms/cpu_nms.pyx\":50\n *         ix1 = x1[i]\n *         iy1 = y1[i]\n *         ix2 = x2[i]             # <<<<<<<<<<<<<<\n *         iy2 = y2[i]\n *         iarea = areas[i]\n */\n    __pyx_t_22 = __pyx_v_i;\n    __pyx_t_23 = -1;\n    if (__pyx_t_22 < 0) {\n      __pyx_t_22 += __pyx_pybuffernd_x2.diminfo[0].shape;\n      if (unlikely(__pyx_t_22 < 0)) __pyx_t_23 = 0;\n    } else if (unlikely(__pyx_t_22 >= __pyx_pybuffernd_x2.diminfo[0].shape)) __pyx_t_23 = 0;\n    if (unlikely(__pyx_t_23 != -1)) {\n      __Pyx_RaiseBufferIndexError(__pyx_t_23);\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n    __pyx_v_ix2 = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_x2.rcbuffer->pybuffer.buf, __pyx_t_22, __pyx_pybuffernd_x2.diminfo[0].strides));\n\n    /* \"nms/cpu_nms.pyx\":51\n *         iy1 = y1[i]\n *         ix2 = x2[i]\n *         iy2 = y2[i]             # <<<<<<<<<<<<<<\n *         iarea = areas[i]\n *         for _j in range(_i + 1, ndets):\n */\n    __pyx_t_23 = __pyx_v_i;\n    __pyx_t_24 = -1;\n    if (__pyx_t_23 < 0) {\n      __pyx_t_23 += __pyx_pybuffernd_y2.diminfo[0].shape;\n      if (unlikely(__pyx_t_23 < 0)) __pyx_t_24 = 0;\n    } else if (unlikely(__pyx_t_23 >= __pyx_pybuffernd_y2.diminfo[0].shape)) __pyx_t_24 = 0;\n    if (unlikely(__pyx_t_24 != -1)) {\n      __Pyx_RaiseBufferIndexError(__pyx_t_24);\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 51; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n    __pyx_v_iy2 = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_y2.rcbuffer->pybuffer.buf, __pyx_t_23, __pyx_pybuffernd_y2.diminfo[0].strides));\n\n    /* \"nms/cpu_nms.pyx\":52\n *         ix2 = x2[i]\n *         iy2 = y2[i]\n *         iarea = areas[i]             # <<<<<<<<<<<<<<\n *         for _j in range(_i + 1, ndets):\n *             j = order[_j]\n */\n    __pyx_t_24 = __pyx_v_i;\n    __pyx_t_25 = -1;\n    if (__pyx_t_24 < 0) {\n      __pyx_t_24 += __pyx_pybuffernd_areas.diminfo[0].shape;\n      if (unlikely(__pyx_t_24 < 0)) __pyx_t_25 = 0;\n    } else if (unlikely(__pyx_t_24 >= __pyx_pybuffernd_areas.diminfo[0].shape)) __pyx_t_25 = 0;\n    if (unlikely(__pyx_t_25 != -1)) {\n      __Pyx_RaiseBufferIndexError(__pyx_t_25);\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n    __pyx_v_iarea = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_areas.rcbuffer->pybuffer.buf, __pyx_t_24, __pyx_pybuffernd_areas.diminfo[0].strides));\n\n    /* \"nms/cpu_nms.pyx\":53\n *         iy2 = y2[i]\n *         iarea = areas[i]\n *         for _j in range(_i + 1, ndets):             # <<<<<<<<<<<<<<\n *             j = order[_j]\n *             if suppressed[j] == 1:\n */\n    __pyx_t_25 = __pyx_v_ndets;\n    for (__pyx_t_26 = (__pyx_v__i + 1); __pyx_t_26 < __pyx_t_25; __pyx_t_26+=1) {\n      __pyx_v__j = __pyx_t_26;\n\n      /* \"nms/cpu_nms.pyx\":54\n *         iarea = areas[i]\n *         for _j in range(_i + 1, ndets):\n *             j = order[_j]             # <<<<<<<<<<<<<<\n *             if suppressed[j] == 1:\n *                 continue\n */\n      __pyx_t_27 = __pyx_v__j;\n      __pyx_t_28 = -1;\n      if (__pyx_t_27 < 0) {\n        __pyx_t_27 += __pyx_pybuffernd_order.diminfo[0].shape;\n        if (unlikely(__pyx_t_27 < 0)) __pyx_t_28 = 0;\n      } else if (unlikely(__pyx_t_27 >= __pyx_pybuffernd_order.diminfo[0].shape)) __pyx_t_28 = 0;\n      if (unlikely(__pyx_t_28 != -1)) {\n        __Pyx_RaiseBufferIndexError(__pyx_t_28);\n        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      __pyx_v_j = (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int_t *, __pyx_pybuffernd_order.rcbuffer->pybuffer.buf, __pyx_t_27, __pyx_pybuffernd_order.diminfo[0].strides));\n\n      /* \"nms/cpu_nms.pyx\":55\n *         for _j in range(_i + 1, ndets):\n *             j = order[_j]\n *             if suppressed[j] == 1:             # <<<<<<<<<<<<<<\n *                 continue\n *             xx1 = max(ix1, x1[j])\n */\n      __pyx_t_28 = __pyx_v_j;\n      __pyx_t_29 = -1;\n      if (__pyx_t_28 < 0) {\n        __pyx_t_28 += __pyx_pybuffernd_suppressed.diminfo[0].shape;\n        if (unlikely(__pyx_t_28 < 0)) __pyx_t_29 = 0;\n      } else if (unlikely(__pyx_t_28 >= __pyx_pybuffernd_suppressed.diminfo[0].shape)) __pyx_t_29 = 0;\n      if (unlikely(__pyx_t_29 != -1)) {\n        __Pyx_RaiseBufferIndexError(__pyx_t_29);\n        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      __pyx_t_19 = (((*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int_t *, __pyx_pybuffernd_suppressed.rcbuffer->pybuffer.buf, __pyx_t_28, __pyx_pybuffernd_suppressed.diminfo[0].strides)) == 1) != 0);\n      if (__pyx_t_19) {\n\n        /* \"nms/cpu_nms.pyx\":56\n *             j = order[_j]\n *             if suppressed[j] == 1:\n *                 continue             # <<<<<<<<<<<<<<\n *             xx1 = max(ix1, x1[j])\n *             yy1 = max(iy1, y1[j])\n */\n        goto __pyx_L6_continue;\n      }\n\n      /* \"nms/cpu_nms.pyx\":57\n *             if suppressed[j] == 1:\n *                 continue\n *             xx1 = max(ix1, x1[j])             # <<<<<<<<<<<<<<\n *             yy1 = max(iy1, y1[j])\n *             xx2 = min(ix2, x2[j])\n */\n      __pyx_t_29 = __pyx_v_j;\n      __pyx_t_30 = -1;\n      if (__pyx_t_29 < 0) {\n        __pyx_t_29 += __pyx_pybuffernd_x1.diminfo[0].shape;\n        if (unlikely(__pyx_t_29 < 0)) __pyx_t_30 = 0;\n      } else if (unlikely(__pyx_t_29 >= __pyx_pybuffernd_x1.diminfo[0].shape)) __pyx_t_30 = 0;\n      if (unlikely(__pyx_t_30 != -1)) {\n        __Pyx_RaiseBufferIndexError(__pyx_t_30);\n        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 57; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      __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_29, __pyx_pybuffernd_x1.diminfo[0].strides)));\n\n      /* \"nms/cpu_nms.pyx\":58\n *                 continue\n *             xx1 = max(ix1, x1[j])\n *             yy1 = max(iy1, y1[j])             # <<<<<<<<<<<<<<\n *             xx2 = min(ix2, x2[j])\n *             yy2 = min(iy2, y2[j])\n */\n      __pyx_t_30 = __pyx_v_j;\n      __pyx_t_31 = -1;\n      if (__pyx_t_30 < 0) {\n        __pyx_t_30 += __pyx_pybuffernd_y1.diminfo[0].shape;\n        if (unlikely(__pyx_t_30 < 0)) __pyx_t_31 = 0;\n      } else if (unlikely(__pyx_t_30 >= __pyx_pybuffernd_y1.diminfo[0].shape)) __pyx_t_31 = 0;\n      if (unlikely(__pyx_t_31 != -1)) {\n        __Pyx_RaiseBufferIndexError(__pyx_t_31);\n        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 58; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      __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_30, __pyx_pybuffernd_y1.diminfo[0].strides)));\n\n      /* \"nms/cpu_nms.pyx\":59\n *             xx1 = max(ix1, x1[j])\n *             yy1 = max(iy1, y1[j])\n *             xx2 = min(ix2, x2[j])             # <<<<<<<<<<<<<<\n *             yy2 = min(iy2, y2[j])\n *             w = max(0.0, xx2 - xx1 + 1)\n */\n      __pyx_t_31 = __pyx_v_j;\n      __pyx_t_32 = -1;\n      if (__pyx_t_31 < 0) {\n        __pyx_t_31 += __pyx_pybuffernd_x2.diminfo[0].shape;\n        if (unlikely(__pyx_t_31 < 0)) __pyx_t_32 = 0;\n      } else if (unlikely(__pyx_t_31 >= __pyx_pybuffernd_x2.diminfo[0].shape)) __pyx_t_32 = 0;\n      if (unlikely(__pyx_t_32 != -1)) {\n        __Pyx_RaiseBufferIndexError(__pyx_t_32);\n        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      __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_31, __pyx_pybuffernd_x2.diminfo[0].strides)));\n\n      /* \"nms/cpu_nms.pyx\":60\n *             yy1 = max(iy1, y1[j])\n *             xx2 = min(ix2, x2[j])\n *             yy2 = min(iy2, y2[j])             # <<<<<<<<<<<<<<\n *             w = max(0.0, xx2 - xx1 + 1)\n *             h = max(0.0, yy2 - yy1 + 1)\n */\n      __pyx_t_32 = __pyx_v_j;\n      __pyx_t_33 = -1;\n      if (__pyx_t_32 < 0) {\n        __pyx_t_32 += __pyx_pybuffernd_y2.diminfo[0].shape;\n        if (unlikely(__pyx_t_32 < 0)) __pyx_t_33 = 0;\n      } else if (unlikely(__pyx_t_32 >= __pyx_pybuffernd_y2.diminfo[0].shape)) __pyx_t_33 = 0;\n      if (unlikely(__pyx_t_33 != -1)) {\n        __Pyx_RaiseBufferIndexError(__pyx_t_33);\n        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      __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_32, __pyx_pybuffernd_y2.diminfo[0].strides)));\n\n      /* \"nms/cpu_nms.pyx\":61\n *             xx2 = min(ix2, x2[j])\n *             yy2 = min(iy2, y2[j])\n *             w = max(0.0, xx2 - xx1 + 1)             # <<<<<<<<<<<<<<\n *             h = max(0.0, yy2 - yy1 + 1)\n *             inter = w * h\n */\n      __pyx_v_w = __pyx_f_3nms_7cpu_nms_max(0.0, ((__pyx_v_xx2 - __pyx_v_xx1) + 1.0));\n\n      /* \"nms/cpu_nms.pyx\":62\n *             yy2 = min(iy2, y2[j])\n *             w = max(0.0, xx2 - xx1 + 1)\n *             h = max(0.0, yy2 - yy1 + 1)             # <<<<<<<<<<<<<<\n *             inter = w * h\n *             ovr = inter / (iarea + areas[j] - inter)\n */\n      __pyx_v_h = __pyx_f_3nms_7cpu_nms_max(0.0, ((__pyx_v_yy2 - __pyx_v_yy1) + 1.0));\n\n      /* \"nms/cpu_nms.pyx\":63\n *             w = max(0.0, xx2 - xx1 + 1)\n *             h = max(0.0, yy2 - yy1 + 1)\n *             inter = w * h             # <<<<<<<<<<<<<<\n *             ovr = inter / (iarea + areas[j] - inter)\n *             if ovr >= thresh:\n */\n      __pyx_v_inter = (__pyx_v_w * __pyx_v_h);\n\n      /* \"nms/cpu_nms.pyx\":64\n *             h = max(0.0, yy2 - yy1 + 1)\n *             inter = w * h\n *             ovr = inter / (iarea + areas[j] - inter)             # <<<<<<<<<<<<<<\n *             if ovr >= thresh:\n *                 suppressed[j] = 1\n */\n      __pyx_t_33 = __pyx_v_j;\n      __pyx_t_34 = -1;\n      if (__pyx_t_33 < 0) {\n        __pyx_t_33 += __pyx_pybuffernd_areas.diminfo[0].shape;\n        if (unlikely(__pyx_t_33 < 0)) __pyx_t_34 = 0;\n      } else if (unlikely(__pyx_t_33 >= __pyx_pybuffernd_areas.diminfo[0].shape)) __pyx_t_34 = 0;\n      if (unlikely(__pyx_t_34 != -1)) {\n        __Pyx_RaiseBufferIndexError(__pyx_t_34);\n        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      __pyx_t_35 = ((__pyx_v_iarea + (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_areas.rcbuffer->pybuffer.buf, __pyx_t_33, __pyx_pybuffernd_areas.diminfo[0].strides))) - __pyx_v_inter);\n      if (unlikely(__pyx_t_35 == 0)) {\n        #ifdef WITH_THREAD\n        PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();\n        #endif\n        PyErr_SetString(PyExc_ZeroDivisionError, \"float division\");\n        #ifdef WITH_THREAD\n        PyGILState_Release(__pyx_gilstate_save);\n        #endif\n        {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      __pyx_v_ovr = (__pyx_v_inter / __pyx_t_35);\n\n      /* \"nms/cpu_nms.pyx\":65\n *             inter = w * h\n *             ovr = inter / (iarea + areas[j] - inter)\n *             if ovr >= thresh:             # <<<<<<<<<<<<<<\n *                 suppressed[j] = 1\n * \n */\n      __pyx_t_12 = PyFloat_FromDouble(__pyx_v_ovr); if (unlikely(!__pyx_t_12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_12);\n      __pyx_t_1 = PyObject_RichCompare(__pyx_t_12, __pyx_v_thresh, Py_GE); __Pyx_XGOTREF(__pyx_t_1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_12); __pyx_t_12 = 0;\n      __pyx_t_19 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_19 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n      if (__pyx_t_19) {\n\n        /* \"nms/cpu_nms.pyx\":66\n *             ovr = inter / (iarea + areas[j] - inter)\n *             if ovr >= thresh:\n *                 suppressed[j] = 1             # <<<<<<<<<<<<<<\n * \n *     return keep\n */\n        __pyx_t_34 = __pyx_v_j;\n        __pyx_t_36 = -1;\n        if (__pyx_t_34 < 0) {\n          __pyx_t_34 += __pyx_pybuffernd_suppressed.diminfo[0].shape;\n          if (unlikely(__pyx_t_34 < 0)) __pyx_t_36 = 0;\n        } else if (unlikely(__pyx_t_34 >= __pyx_pybuffernd_suppressed.diminfo[0].shape)) __pyx_t_36 = 0;\n        if (unlikely(__pyx_t_36 != -1)) {\n          __Pyx_RaiseBufferIndexError(__pyx_t_36);\n          {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n        }\n        *__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int_t *, __pyx_pybuffernd_suppressed.rcbuffer->pybuffer.buf, __pyx_t_34, __pyx_pybuffernd_suppressed.diminfo[0].strides) = 1;\n        goto __pyx_L9;\n      }\n      __pyx_L9:;\n      __pyx_L6_continue:;\n    }\n    __pyx_L3_continue:;\n  }\n\n  /* \"nms/cpu_nms.pyx\":68\n *                 suppressed[j] = 1\n * \n *     return keep             # <<<<<<<<<<<<<<\n */\n  __Pyx_XDECREF(__pyx_r);\n  __Pyx_INCREF(__pyx_v_keep);\n  __pyx_r = __pyx_v_keep;\n  goto __pyx_L0;\n\n  /* \"nms/cpu_nms.pyx\":17\n *     return a if a <= b else b\n * \n * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_XDECREF(__pyx_t_7);\n  __Pyx_XDECREF(__pyx_t_8);\n  __Pyx_XDECREF(__pyx_t_11);\n  __Pyx_XDECREF(__pyx_t_12);\n  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;\n    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_areas.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_dets.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_order.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_scores.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_suppressed.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_x1.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_x2.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_y1.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_y2.rcbuffer->pybuffer);\n  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}\n  __Pyx_AddTraceback(\"nms.cpu_nms.cpu_nms\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = NULL;\n  goto __pyx_L2;\n  __pyx_L0:;\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_areas.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_dets.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_order.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_scores.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_suppressed.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_x1.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_x2.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_y1.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_y2.rcbuffer->pybuffer);\n  __pyx_L2:;\n  __Pyx_XDECREF((PyObject *)__pyx_v_x1);\n  __Pyx_XDECREF((PyObject *)__pyx_v_y1);\n  __Pyx_XDECREF((PyObject *)__pyx_v_x2);\n  __Pyx_XDECREF((PyObject *)__pyx_v_y2);\n  __Pyx_XDECREF((PyObject *)__pyx_v_scores);\n  __Pyx_XDECREF((PyObject *)__pyx_v_areas);\n  __Pyx_XDECREF((PyObject *)__pyx_v_order);\n  __Pyx_XDECREF((PyObject *)__pyx_v_suppressed);\n  __Pyx_XDECREF(__pyx_v_keep);\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":194\n *         # experimental exception made for __getbuffer__ and __releasebuffer__\n *         # -- the details of this may change.\n *         def __getbuffer__(ndarray self, Py_buffer* info, int flags):             # <<<<<<<<<<<<<<\n *             # This implementation of getbuffer is geared towards Cython\n *             # requirements, and does not yet fullfill the PEP.\n */\n\n/* Python wrapper */\nstatic CYTHON_UNUSED int __pyx_pw_5numpy_7ndarray_1__getbuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags); /*proto*/\nstatic CYTHON_UNUSED int __pyx_pw_5numpy_7ndarray_1__getbuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags) {\n  int __pyx_r;\n  __Pyx_RefNannyDeclarations\n  __Pyx_RefNannySetupContext(\"__getbuffer__ (wrapper)\", 0);\n  __pyx_r = __pyx_pf_5numpy_7ndarray___getbuffer__(((PyArrayObject *)__pyx_v_self), ((Py_buffer *)__pyx_v_info), ((int)__pyx_v_flags));\n\n  /* function exit code */\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\nstatic int __pyx_pf_5numpy_7ndarray___getbuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags) {\n  int __pyx_v_copy_shape;\n  int __pyx_v_i;\n  int __pyx_v_ndim;\n  int __pyx_v_endian_detector;\n  int __pyx_v_little_endian;\n  int __pyx_v_t;\n  char *__pyx_v_f;\n  PyArray_Descr *__pyx_v_descr = 0;\n  int __pyx_v_offset;\n  int __pyx_v_hasfields;\n  int __pyx_r;\n  __Pyx_RefNannyDeclarations\n  int __pyx_t_1;\n  int __pyx_t_2;\n  int __pyx_t_3;\n  PyObject *__pyx_t_4 = NULL;\n  int __pyx_t_5;\n  int __pyx_t_6;\n  int __pyx_t_7;\n  PyObject *__pyx_t_8 = NULL;\n  char *__pyx_t_9;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"__getbuffer__\", 0);\n  if (__pyx_v_info != NULL) {\n    __pyx_v_info->obj = Py_None; __Pyx_INCREF(Py_None);\n    __Pyx_GIVEREF(__pyx_v_info->obj);\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":200\n *             # of flags\n * \n *             if info == NULL: return             # <<<<<<<<<<<<<<\n * \n *             cdef int copy_shape, i, ndim\n */\n  __pyx_t_1 = ((__pyx_v_info == NULL) != 0);\n  if (__pyx_t_1) {\n    __pyx_r = 0;\n    goto __pyx_L0;\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":203\n * \n *             cdef int copy_shape, i, ndim\n *             cdef int endian_detector = 1             # <<<<<<<<<<<<<<\n *             cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)\n * \n */\n  __pyx_v_endian_detector = 1;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":204\n *             cdef int copy_shape, i, ndim\n *             cdef int endian_detector = 1\n *             cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)             # <<<<<<<<<<<<<<\n * \n *             ndim = PyArray_NDIM(self)\n */\n  __pyx_v_little_endian = ((((char *)(&__pyx_v_endian_detector))[0]) != 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":206\n *             cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)\n * \n *             ndim = PyArray_NDIM(self)             # <<<<<<<<<<<<<<\n * \n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):\n */\n  __pyx_v_ndim = PyArray_NDIM(__pyx_v_self);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":208\n *             ndim = PyArray_NDIM(self)\n * \n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):             # <<<<<<<<<<<<<<\n *                 copy_shape = 1\n *             else:\n */\n  __pyx_t_1 = (((sizeof(npy_intp)) != (sizeof(Py_ssize_t))) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":209\n * \n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):\n *                 copy_shape = 1             # <<<<<<<<<<<<<<\n *             else:\n *                 copy_shape = 0\n */\n    __pyx_v_copy_shape = 1;\n    goto __pyx_L4;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":211\n *                 copy_shape = 1\n *             else:\n *                 copy_shape = 0             # <<<<<<<<<<<<<<\n * \n *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)\n */\n    __pyx_v_copy_shape = 0;\n  }\n  __pyx_L4:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":213\n *                 copy_shape = 0\n * \n *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)             # <<<<<<<<<<<<<<\n *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not C contiguous\")\n */\n  __pyx_t_1 = (((__pyx_v_flags & PyBUF_C_CONTIGUOUS) == PyBUF_C_CONTIGUOUS) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":214\n * \n *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):             # <<<<<<<<<<<<<<\n *                 raise ValueError(u\"ndarray is not C contiguous\")\n * \n */\n    __pyx_t_2 = ((!(PyArray_CHKFLAGS(__pyx_v_self, NPY_C_CONTIGUOUS) != 0)) != 0);\n    __pyx_t_3 = __pyx_t_2;\n  } else {\n    __pyx_t_3 = __pyx_t_1;\n  }\n  if (__pyx_t_3) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":215\n *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not C contiguous\")             # <<<<<<<<<<<<<<\n * \n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)\n */\n    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__12, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_GOTREF(__pyx_t_4);\n    __Pyx_Raise(__pyx_t_4, 0, 0, 0);\n    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n    {__pyx_filename = __pyx_f[1]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":217\n *                 raise ValueError(u\"ndarray is not C contiguous\")\n * \n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)             # <<<<<<<<<<<<<<\n *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not Fortran contiguous\")\n */\n  __pyx_t_3 = (((__pyx_v_flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS) != 0);\n  if (__pyx_t_3) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":218\n * \n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):             # <<<<<<<<<<<<<<\n *                 raise ValueError(u\"ndarray is not Fortran contiguous\")\n * \n */\n    __pyx_t_1 = ((!(PyArray_CHKFLAGS(__pyx_v_self, NPY_F_CONTIGUOUS) != 0)) != 0);\n    __pyx_t_2 = __pyx_t_1;\n  } else {\n    __pyx_t_2 = __pyx_t_3;\n  }\n  if (__pyx_t_2) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":219\n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not Fortran contiguous\")             # <<<<<<<<<<<<<<\n * \n *             info.buf = PyArray_DATA(self)\n */\n    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__13, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_GOTREF(__pyx_t_4);\n    __Pyx_Raise(__pyx_t_4, 0, 0, 0);\n    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n    {__pyx_filename = __pyx_f[1]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":221\n *                 raise ValueError(u\"ndarray is not Fortran contiguous\")\n * \n *             info.buf = PyArray_DATA(self)             # <<<<<<<<<<<<<<\n *             info.ndim = ndim\n *             if copy_shape:\n */\n  __pyx_v_info->buf = PyArray_DATA(__pyx_v_self);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":222\n * \n *             info.buf = PyArray_DATA(self)\n *             info.ndim = ndim             # <<<<<<<<<<<<<<\n *             if copy_shape:\n *                 # Allocate new buffer for strides and shape info.\n */\n  __pyx_v_info->ndim = __pyx_v_ndim;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":223\n *             info.buf = PyArray_DATA(self)\n *             info.ndim = ndim\n *             if copy_shape:             # <<<<<<<<<<<<<<\n *                 # Allocate new buffer for strides and shape info.\n *                 # This is allocated as one block, strides first.\n */\n  __pyx_t_2 = (__pyx_v_copy_shape != 0);\n  if (__pyx_t_2) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":226\n *                 # Allocate new buffer for strides and shape info.\n *                 # This is allocated as one block, strides first.\n *                 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * <size_t>ndim * 2)             # <<<<<<<<<<<<<<\n *                 info.shape = info.strides + ndim\n *                 for i in range(ndim):\n */\n    __pyx_v_info->strides = ((Py_ssize_t *)malloc((((sizeof(Py_ssize_t)) * ((size_t)__pyx_v_ndim)) * 2)));\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":227\n *                 # This is allocated as one block, strides first.\n *                 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * <size_t>ndim * 2)\n *                 info.shape = info.strides + ndim             # <<<<<<<<<<<<<<\n *                 for i in range(ndim):\n *                     info.strides[i] = PyArray_STRIDES(self)[i]\n */\n    __pyx_v_info->shape = (__pyx_v_info->strides + __pyx_v_ndim);\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":228\n *                 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * <size_t>ndim * 2)\n *                 info.shape = info.strides + ndim\n *                 for i in range(ndim):             # <<<<<<<<<<<<<<\n *                     info.strides[i] = PyArray_STRIDES(self)[i]\n *                     info.shape[i] = PyArray_DIMS(self)[i]\n */\n    __pyx_t_5 = __pyx_v_ndim;\n    for (__pyx_t_6 = 0; __pyx_t_6 < __pyx_t_5; __pyx_t_6+=1) {\n      __pyx_v_i = __pyx_t_6;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":229\n *                 info.shape = info.strides + ndim\n *                 for i in range(ndim):\n *                     info.strides[i] = PyArray_STRIDES(self)[i]             # <<<<<<<<<<<<<<\n *                     info.shape[i] = PyArray_DIMS(self)[i]\n *             else:\n */\n      (__pyx_v_info->strides[__pyx_v_i]) = (PyArray_STRIDES(__pyx_v_self)[__pyx_v_i]);\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":230\n *                 for i in range(ndim):\n *                     info.strides[i] = PyArray_STRIDES(self)[i]\n *                     info.shape[i] = PyArray_DIMS(self)[i]             # <<<<<<<<<<<<<<\n *             else:\n *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)\n */\n      (__pyx_v_info->shape[__pyx_v_i]) = (PyArray_DIMS(__pyx_v_self)[__pyx_v_i]);\n    }\n    goto __pyx_L7;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":232\n *                     info.shape[i] = PyArray_DIMS(self)[i]\n *             else:\n *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)             # <<<<<<<<<<<<<<\n *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)\n *             info.suboffsets = NULL\n */\n    __pyx_v_info->strides = ((Py_ssize_t *)PyArray_STRIDES(__pyx_v_self));\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":233\n *             else:\n *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)\n *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)             # <<<<<<<<<<<<<<\n *             info.suboffsets = NULL\n *             info.itemsize = PyArray_ITEMSIZE(self)\n */\n    __pyx_v_info->shape = ((Py_ssize_t *)PyArray_DIMS(__pyx_v_self));\n  }\n  __pyx_L7:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":234\n *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)\n *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)\n *             info.suboffsets = NULL             # <<<<<<<<<<<<<<\n *             info.itemsize = PyArray_ITEMSIZE(self)\n *             info.readonly = not PyArray_ISWRITEABLE(self)\n */\n  __pyx_v_info->suboffsets = NULL;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":235\n *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)\n *             info.suboffsets = NULL\n *             info.itemsize = PyArray_ITEMSIZE(self)             # <<<<<<<<<<<<<<\n *             info.readonly = not PyArray_ISWRITEABLE(self)\n * \n */\n  __pyx_v_info->itemsize = PyArray_ITEMSIZE(__pyx_v_self);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":236\n *             info.suboffsets = NULL\n *             info.itemsize = PyArray_ITEMSIZE(self)\n *             info.readonly = not PyArray_ISWRITEABLE(self)             # <<<<<<<<<<<<<<\n * \n *             cdef int t\n */\n  __pyx_v_info->readonly = (!(PyArray_ISWRITEABLE(__pyx_v_self) != 0));\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":239\n * \n *             cdef int t\n *             cdef char* f = NULL             # <<<<<<<<<<<<<<\n *             cdef dtype descr = self.descr\n *             cdef list stack\n */\n  __pyx_v_f = NULL;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":240\n *             cdef int t\n *             cdef char* f = NULL\n *             cdef dtype descr = self.descr             # <<<<<<<<<<<<<<\n *             cdef list stack\n *             cdef int offset\n */\n  __pyx_t_4 = ((PyObject *)__pyx_v_self->descr);\n  __Pyx_INCREF(__pyx_t_4);\n  __pyx_v_descr = ((PyArray_Descr *)__pyx_t_4);\n  __pyx_t_4 = 0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":244\n *             cdef int offset\n * \n *             cdef bint hasfields = PyDataType_HASFIELDS(descr)             # <<<<<<<<<<<<<<\n * \n *             if not hasfields and not copy_shape:\n */\n  __pyx_v_hasfields = PyDataType_HASFIELDS(__pyx_v_descr);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":246\n *             cdef bint hasfields = PyDataType_HASFIELDS(descr)\n * \n *             if not hasfields and not copy_shape:             # <<<<<<<<<<<<<<\n *                 # do not call releasebuffer\n *                 info.obj = None\n */\n  __pyx_t_2 = ((!(__pyx_v_hasfields != 0)) != 0);\n  if (__pyx_t_2) {\n    __pyx_t_3 = ((!(__pyx_v_copy_shape != 0)) != 0);\n    __pyx_t_1 = __pyx_t_3;\n  } else {\n    __pyx_t_1 = __pyx_t_2;\n  }\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":248\n *             if not hasfields and not copy_shape:\n *                 # do not call releasebuffer\n *                 info.obj = None             # <<<<<<<<<<<<<<\n *             else:\n *                 # need to call releasebuffer\n */\n    __Pyx_INCREF(Py_None);\n    __Pyx_GIVEREF(Py_None);\n    __Pyx_GOTREF(__pyx_v_info->obj);\n    __Pyx_DECREF(__pyx_v_info->obj);\n    __pyx_v_info->obj = Py_None;\n    goto __pyx_L10;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":251\n *             else:\n *                 # need to call releasebuffer\n *                 info.obj = self             # <<<<<<<<<<<<<<\n * \n *             if not hasfields:\n */\n    __Pyx_INCREF(((PyObject *)__pyx_v_self));\n    __Pyx_GIVEREF(((PyObject *)__pyx_v_self));\n    __Pyx_GOTREF(__pyx_v_info->obj);\n    __Pyx_DECREF(__pyx_v_info->obj);\n    __pyx_v_info->obj = ((PyObject *)__pyx_v_self);\n  }\n  __pyx_L10:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":253\n *                 info.obj = self\n * \n *             if not hasfields:             # <<<<<<<<<<<<<<\n *                 t = descr.type_num\n *                 if ((descr.byteorder == c'>' and little_endian) or\n */\n  __pyx_t_1 = ((!(__pyx_v_hasfields != 0)) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":254\n * \n *             if not hasfields:\n *                 t = descr.type_num             # <<<<<<<<<<<<<<\n *                 if ((descr.byteorder == c'>' and little_endian) or\n *                     (descr.byteorder == c'<' and not little_endian)):\n */\n    __pyx_t_5 = __pyx_v_descr->type_num;\n    __pyx_v_t = __pyx_t_5;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":255\n *             if not hasfields:\n *                 t = descr.type_num\n *                 if ((descr.byteorder == c'>' and little_endian) or             # <<<<<<<<<<<<<<\n *                     (descr.byteorder == c'<' and not little_endian)):\n *                     raise ValueError(u\"Non-native byte order not supported\")\n */\n    __pyx_t_1 = ((__pyx_v_descr->byteorder == '>') != 0);\n    if (__pyx_t_1) {\n      __pyx_t_2 = (__pyx_v_little_endian != 0);\n    } else {\n      __pyx_t_2 = __pyx_t_1;\n    }\n    if (!__pyx_t_2) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":256\n *                 t = descr.type_num\n *                 if ((descr.byteorder == c'>' and little_endian) or\n *                     (descr.byteorder == c'<' and not little_endian)):             # <<<<<<<<<<<<<<\n *                     raise ValueError(u\"Non-native byte order not supported\")\n *                 if   t == NPY_BYTE:        f = \"b\"\n */\n      __pyx_t_1 = ((__pyx_v_descr->byteorder == '<') != 0);\n      if (__pyx_t_1) {\n        __pyx_t_3 = ((!(__pyx_v_little_endian != 0)) != 0);\n        __pyx_t_7 = __pyx_t_3;\n      } else {\n        __pyx_t_7 = __pyx_t_1;\n      }\n      __pyx_t_1 = __pyx_t_7;\n    } else {\n      __pyx_t_1 = __pyx_t_2;\n    }\n    if (__pyx_t_1) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":257\n *                 if ((descr.byteorder == c'>' and little_endian) or\n *                     (descr.byteorder == c'<' and not little_endian)):\n *                     raise ValueError(u\"Non-native byte order not supported\")             # <<<<<<<<<<<<<<\n *                 if   t == NPY_BYTE:        f = \"b\"\n *                 elif t == NPY_UBYTE:       f = \"B\"\n */\n      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__14, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __Pyx_Raise(__pyx_t_4, 0, 0, 0);\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":274\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"\n *                 elif t == NPY_CLONGDOUBLE: f = \"Zg\"\n *                 elif t == NPY_OBJECT:      f = \"O\"             # <<<<<<<<<<<<<<\n *                 else:\n *                     raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n */\n    switch (__pyx_v_t) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":258\n *                     (descr.byteorder == c'<' and not little_endian)):\n *                     raise ValueError(u\"Non-native byte order not supported\")\n *                 if   t == NPY_BYTE:        f = \"b\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_UBYTE:       f = \"B\"\n *                 elif t == NPY_SHORT:       f = \"h\"\n */\n      case NPY_BYTE:\n      __pyx_v_f = __pyx_k_b;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":259\n *                     raise ValueError(u\"Non-native byte order not supported\")\n *                 if   t == NPY_BYTE:        f = \"b\"\n *                 elif t == NPY_UBYTE:       f = \"B\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_SHORT:       f = \"h\"\n *                 elif t == NPY_USHORT:      f = \"H\"\n */\n      case NPY_UBYTE:\n      __pyx_v_f = __pyx_k_B;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":260\n *                 if   t == NPY_BYTE:        f = \"b\"\n *                 elif t == NPY_UBYTE:       f = \"B\"\n *                 elif t == NPY_SHORT:       f = \"h\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_USHORT:      f = \"H\"\n *                 elif t == NPY_INT:         f = \"i\"\n */\n      case NPY_SHORT:\n      __pyx_v_f = __pyx_k_h;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":261\n *                 elif t == NPY_UBYTE:       f = \"B\"\n *                 elif t == NPY_SHORT:       f = \"h\"\n *                 elif t == NPY_USHORT:      f = \"H\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_INT:         f = \"i\"\n *                 elif t == NPY_UINT:        f = \"I\"\n */\n      case NPY_USHORT:\n      __pyx_v_f = __pyx_k_H;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":262\n *                 elif t == NPY_SHORT:       f = \"h\"\n *                 elif t == NPY_USHORT:      f = \"H\"\n *                 elif t == NPY_INT:         f = \"i\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_UINT:        f = \"I\"\n *                 elif t == NPY_LONG:        f = \"l\"\n */\n      case NPY_INT:\n      __pyx_v_f = __pyx_k_i;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":263\n *                 elif t == NPY_USHORT:      f = \"H\"\n *                 elif t == NPY_INT:         f = \"i\"\n *                 elif t == NPY_UINT:        f = \"I\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_LONG:        f = \"l\"\n *                 elif t == NPY_ULONG:       f = \"L\"\n */\n      case NPY_UINT:\n      __pyx_v_f = __pyx_k_I;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":264\n *                 elif t == NPY_INT:         f = \"i\"\n *                 elif t == NPY_UINT:        f = \"I\"\n *                 elif t == NPY_LONG:        f = \"l\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_ULONG:       f = \"L\"\n *                 elif t == NPY_LONGLONG:    f = \"q\"\n */\n      case NPY_LONG:\n      __pyx_v_f = __pyx_k_l;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":265\n *                 elif t == NPY_UINT:        f = \"I\"\n *                 elif t == NPY_LONG:        f = \"l\"\n *                 elif t == NPY_ULONG:       f = \"L\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_LONGLONG:    f = \"q\"\n *                 elif t == NPY_ULONGLONG:   f = \"Q\"\n */\n      case NPY_ULONG:\n      __pyx_v_f = __pyx_k_L;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":266\n *                 elif t == NPY_LONG:        f = \"l\"\n *                 elif t == NPY_ULONG:       f = \"L\"\n *                 elif t == NPY_LONGLONG:    f = \"q\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_ULONGLONG:   f = \"Q\"\n *                 elif t == NPY_FLOAT:       f = \"f\"\n */\n      case NPY_LONGLONG:\n      __pyx_v_f = __pyx_k_q;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":267\n *                 elif t == NPY_ULONG:       f = \"L\"\n *                 elif t == NPY_LONGLONG:    f = \"q\"\n *                 elif t == NPY_ULONGLONG:   f = \"Q\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_FLOAT:       f = \"f\"\n *                 elif t == NPY_DOUBLE:      f = \"d\"\n */\n      case NPY_ULONGLONG:\n      __pyx_v_f = __pyx_k_Q;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":268\n *                 elif t == NPY_LONGLONG:    f = \"q\"\n *                 elif t == NPY_ULONGLONG:   f = \"Q\"\n *                 elif t == NPY_FLOAT:       f = \"f\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_DOUBLE:      f = \"d\"\n *                 elif t == NPY_LONGDOUBLE:  f = \"g\"\n */\n      case NPY_FLOAT:\n      __pyx_v_f = __pyx_k_f;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":269\n *                 elif t == NPY_ULONGLONG:   f = \"Q\"\n *                 elif t == NPY_FLOAT:       f = \"f\"\n *                 elif t == NPY_DOUBLE:      f = \"d\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_LONGDOUBLE:  f = \"g\"\n *                 elif t == NPY_CFLOAT:      f = \"Zf\"\n */\n      case NPY_DOUBLE:\n      __pyx_v_f = __pyx_k_d;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":270\n *                 elif t == NPY_FLOAT:       f = \"f\"\n *                 elif t == NPY_DOUBLE:      f = \"d\"\n *                 elif t == NPY_LONGDOUBLE:  f = \"g\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_CFLOAT:      f = \"Zf\"\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"\n */\n      case NPY_LONGDOUBLE:\n      __pyx_v_f = __pyx_k_g;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":271\n *                 elif t == NPY_DOUBLE:      f = \"d\"\n *                 elif t == NPY_LONGDOUBLE:  f = \"g\"\n *                 elif t == NPY_CFLOAT:      f = \"Zf\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"\n *                 elif t == NPY_CLONGDOUBLE: f = \"Zg\"\n */\n      case NPY_CFLOAT:\n      __pyx_v_f = __pyx_k_Zf;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":272\n *                 elif t == NPY_LONGDOUBLE:  f = \"g\"\n *                 elif t == NPY_CFLOAT:      f = \"Zf\"\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_CLONGDOUBLE: f = \"Zg\"\n *                 elif t == NPY_OBJECT:      f = \"O\"\n */\n      case NPY_CDOUBLE:\n      __pyx_v_f = __pyx_k_Zd;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":273\n *                 elif t == NPY_CFLOAT:      f = \"Zf\"\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"\n *                 elif t == NPY_CLONGDOUBLE: f = \"Zg\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_OBJECT:      f = \"O\"\n *                 else:\n */\n      case NPY_CLONGDOUBLE:\n      __pyx_v_f = __pyx_k_Zg;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":274\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"\n *                 elif t == NPY_CLONGDOUBLE: f = \"Zg\"\n *                 elif t == NPY_OBJECT:      f = \"O\"             # <<<<<<<<<<<<<<\n *                 else:\n *                     raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n */\n      case NPY_OBJECT:\n      __pyx_v_f = __pyx_k_O;\n      break;\n      default:\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":276\n *                 elif t == NPY_OBJECT:      f = \"O\"\n *                 else:\n *                     raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)             # <<<<<<<<<<<<<<\n *                 info.format = f\n *                 return\n */\n      __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_t); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_8 = PyUnicode_Format(__pyx_kp_u_unknown_dtype_code_in_numpy_pxd, __pyx_t_4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_8);\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_8);\n      __Pyx_GIVEREF(__pyx_t_8);\n      __pyx_t_8 = 0;\n      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_8);\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __Pyx_Raise(__pyx_t_8, 0, 0, 0);\n      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;\n      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      break;\n    }\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":277\n *                 else:\n *                     raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n *                 info.format = f             # <<<<<<<<<<<<<<\n *                 return\n *             else:\n */\n    __pyx_v_info->format = __pyx_v_f;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":278\n *                     raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n *                 info.format = f\n *                 return             # <<<<<<<<<<<<<<\n *             else:\n *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)\n */\n    __pyx_r = 0;\n    goto __pyx_L0;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":280\n *                 return\n *             else:\n *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)             # <<<<<<<<<<<<<<\n *                 info.format[0] = c'^' # Native data types, manual alignment\n *                 offset = 0\n */\n    __pyx_v_info->format = ((char *)malloc(255));\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":281\n *             else:\n *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)\n *                 info.format[0] = c'^' # Native data types, manual alignment             # <<<<<<<<<<<<<<\n *                 offset = 0\n *                 f = _util_dtypestring(descr, info.format + 1,\n */\n    (__pyx_v_info->format[0]) = '^';\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":282\n *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)\n *                 info.format[0] = c'^' # Native data types, manual alignment\n *                 offset = 0             # <<<<<<<<<<<<<<\n *                 f = _util_dtypestring(descr, info.format + 1,\n *                                       info.format + _buffer_format_string_len,\n */\n    __pyx_v_offset = 0;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":283\n *                 info.format[0] = c'^' # Native data types, manual alignment\n *                 offset = 0\n *                 f = _util_dtypestring(descr, info.format + 1,             # <<<<<<<<<<<<<<\n *                                       info.format + _buffer_format_string_len,\n *                                       &offset)\n */\n    __pyx_t_9 = __pyx_f_5numpy__util_dtypestring(__pyx_v_descr, (__pyx_v_info->format + 1), (__pyx_v_info->format + 255), (&__pyx_v_offset)); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 283; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __pyx_v_f = __pyx_t_9;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":286\n *                                       info.format + _buffer_format_string_len,\n *                                       &offset)\n *                 f[0] = c'\\0' # Terminate format string             # <<<<<<<<<<<<<<\n * \n *         def __releasebuffer__(ndarray self, Py_buffer* info):\n */\n    (__pyx_v_f[0]) = '\\x00';\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":194\n *         # experimental exception made for __getbuffer__ and __releasebuffer__\n *         # -- the details of this may change.\n *         def __getbuffer__(ndarray self, Py_buffer* info, int flags):             # <<<<<<<<<<<<<<\n *             # This implementation of getbuffer is geared towards Cython\n *             # requirements, and does not yet fullfill the PEP.\n */\n\n  /* function exit code */\n  __pyx_r = 0;\n  goto __pyx_L0;\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_4);\n  __Pyx_XDECREF(__pyx_t_8);\n  __Pyx_AddTraceback(\"numpy.ndarray.__getbuffer__\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = -1;\n  if (__pyx_v_info != NULL && __pyx_v_info->obj != NULL) {\n    __Pyx_GOTREF(__pyx_v_info->obj);\n    __Pyx_DECREF(__pyx_v_info->obj); __pyx_v_info->obj = NULL;\n  }\n  goto __pyx_L2;\n  __pyx_L0:;\n  if (__pyx_v_info != NULL && __pyx_v_info->obj == Py_None) {\n    __Pyx_GOTREF(Py_None);\n    __Pyx_DECREF(Py_None); __pyx_v_info->obj = NULL;\n  }\n  __pyx_L2:;\n  __Pyx_XDECREF((PyObject *)__pyx_v_descr);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":288\n *                 f[0] = c'\\0' # Terminate format string\n * \n *         def __releasebuffer__(ndarray self, Py_buffer* info):             # <<<<<<<<<<<<<<\n *             if PyArray_HASFIELDS(self):\n *                 stdlib.free(info.format)\n */\n\n/* Python wrapper */\nstatic CYTHON_UNUSED void __pyx_pw_5numpy_7ndarray_3__releasebuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info); /*proto*/\nstatic CYTHON_UNUSED void __pyx_pw_5numpy_7ndarray_3__releasebuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info) {\n  __Pyx_RefNannyDeclarations\n  __Pyx_RefNannySetupContext(\"__releasebuffer__ (wrapper)\", 0);\n  __pyx_pf_5numpy_7ndarray_2__releasebuffer__(((PyArrayObject *)__pyx_v_self), ((Py_buffer *)__pyx_v_info));\n\n  /* function exit code */\n  __Pyx_RefNannyFinishContext();\n}\n\nstatic void __pyx_pf_5numpy_7ndarray_2__releasebuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info) {\n  __Pyx_RefNannyDeclarations\n  int __pyx_t_1;\n  __Pyx_RefNannySetupContext(\"__releasebuffer__\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":289\n * \n *         def __releasebuffer__(ndarray self, Py_buffer* info):\n *             if PyArray_HASFIELDS(self):             # <<<<<<<<<<<<<<\n *                 stdlib.free(info.format)\n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):\n */\n  __pyx_t_1 = (PyArray_HASFIELDS(__pyx_v_self) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":290\n *         def __releasebuffer__(ndarray self, Py_buffer* info):\n *             if PyArray_HASFIELDS(self):\n *                 stdlib.free(info.format)             # <<<<<<<<<<<<<<\n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):\n *                 stdlib.free(info.strides)\n */\n    free(__pyx_v_info->format);\n    goto __pyx_L3;\n  }\n  __pyx_L3:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":291\n *             if PyArray_HASFIELDS(self):\n *                 stdlib.free(info.format)\n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):             # <<<<<<<<<<<<<<\n *                 stdlib.free(info.strides)\n *                 # info.shape was stored after info.strides in the same block\n */\n  __pyx_t_1 = (((sizeof(npy_intp)) != (sizeof(Py_ssize_t))) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":292\n *                 stdlib.free(info.format)\n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):\n *                 stdlib.free(info.strides)             # <<<<<<<<<<<<<<\n *                 # info.shape was stored after info.strides in the same block\n * \n */\n    free(__pyx_v_info->strides);\n    goto __pyx_L4;\n  }\n  __pyx_L4:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":288\n *                 f[0] = c'\\0' # Terminate format string\n * \n *         def __releasebuffer__(ndarray self, Py_buffer* info):             # <<<<<<<<<<<<<<\n *             if PyArray_HASFIELDS(self):\n *                 stdlib.free(info.format)\n */\n\n  /* function exit code */\n  __Pyx_RefNannyFinishContext();\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":768\n * ctypedef npy_cdouble     complex_t\n * \n * cdef inline object PyArray_MultiIterNew1(a):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(1, <void*>a)\n * \n */\n\nstatic CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew1(PyObject *__pyx_v_a) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"PyArray_MultiIterNew1\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":769\n * \n * cdef inline object PyArray_MultiIterNew1(a):\n *     return PyArray_MultiIterNew(1, <void*>a)             # <<<<<<<<<<<<<<\n * \n * cdef inline object PyArray_MultiIterNew2(a, b):\n */\n  __Pyx_XDECREF(__pyx_r);\n  __pyx_t_1 = PyArray_MultiIterNew(1, ((void *)__pyx_v_a)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_r = __pyx_t_1;\n  __pyx_t_1 = 0;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":768\n * ctypedef npy_cdouble     complex_t\n * \n * cdef inline object PyArray_MultiIterNew1(a):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(1, <void*>a)\n * \n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_AddTraceback(\"numpy.PyArray_MultiIterNew1\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = 0;\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":771\n *     return PyArray_MultiIterNew(1, <void*>a)\n * \n * cdef inline object PyArray_MultiIterNew2(a, b):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)\n * \n */\n\nstatic CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew2(PyObject *__pyx_v_a, PyObject *__pyx_v_b) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"PyArray_MultiIterNew2\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":772\n * \n * cdef inline object PyArray_MultiIterNew2(a, b):\n *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)             # <<<<<<<<<<<<<<\n * \n * cdef inline object PyArray_MultiIterNew3(a, b, c):\n */\n  __Pyx_XDECREF(__pyx_r);\n  __pyx_t_1 = PyArray_MultiIterNew(2, ((void *)__pyx_v_a), ((void *)__pyx_v_b)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_r = __pyx_t_1;\n  __pyx_t_1 = 0;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":771\n *     return PyArray_MultiIterNew(1, <void*>a)\n * \n * cdef inline object PyArray_MultiIterNew2(a, b):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)\n * \n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_AddTraceback(\"numpy.PyArray_MultiIterNew2\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = 0;\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":774\n *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)\n * \n * cdef inline object PyArray_MultiIterNew3(a, b, c):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)\n * \n */\n\nstatic CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew3(PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"PyArray_MultiIterNew3\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":775\n * \n * cdef inline object PyArray_MultiIterNew3(a, b, c):\n *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)             # <<<<<<<<<<<<<<\n * \n * cdef inline object PyArray_MultiIterNew4(a, b, c, d):\n */\n  __Pyx_XDECREF(__pyx_r);\n  __pyx_t_1 = PyArray_MultiIterNew(3, ((void *)__pyx_v_a), ((void *)__pyx_v_b), ((void *)__pyx_v_c)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_r = __pyx_t_1;\n  __pyx_t_1 = 0;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":774\n *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)\n * \n * cdef inline object PyArray_MultiIterNew3(a, b, c):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)\n * \n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_AddTraceback(\"numpy.PyArray_MultiIterNew3\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = 0;\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":777\n *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)\n * \n * cdef inline object PyArray_MultiIterNew4(a, b, c, d):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)\n * \n */\n\nstatic CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew4(PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c, PyObject *__pyx_v_d) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"PyArray_MultiIterNew4\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":778\n * \n * cdef inline object PyArray_MultiIterNew4(a, b, c, d):\n *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)             # <<<<<<<<<<<<<<\n * \n * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):\n */\n  __Pyx_XDECREF(__pyx_r);\n  __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_filename = __pyx_f[1]; __pyx_lineno = 778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_r = __pyx_t_1;\n  __pyx_t_1 = 0;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":777\n *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)\n * \n * cdef inline object PyArray_MultiIterNew4(a, b, c, d):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)\n * \n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_AddTraceback(\"numpy.PyArray_MultiIterNew4\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = 0;\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":780\n *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)\n * \n * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)\n * \n */\n\nstatic 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) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"PyArray_MultiIterNew5\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":781\n * \n * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):\n *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)             # <<<<<<<<<<<<<<\n * \n * cdef inline char* _util_dtypestring(dtype descr, char* f, char* end, int* offset) except NULL:\n */\n  __Pyx_XDECREF(__pyx_r);\n  __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_filename = __pyx_f[1]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_r = __pyx_t_1;\n  __pyx_t_1 = 0;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":780\n *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)\n * \n * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)\n * \n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_AddTraceback(\"numpy.PyArray_MultiIterNew5\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = 0;\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":783\n *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)\n * \n * cdef inline char* _util_dtypestring(dtype descr, char* f, char* end, int* offset) except NULL:             # <<<<<<<<<<<<<<\n *     # Recursive utility function used in __getbuffer__ to get format\n *     # string. The new location in the format string is returned.\n */\n\nstatic CYTHON_INLINE char *__pyx_f_5numpy__util_dtypestring(PyArray_Descr *__pyx_v_descr, char *__pyx_v_f, char *__pyx_v_end, int *__pyx_v_offset) {\n  PyArray_Descr *__pyx_v_child = 0;\n  int __pyx_v_endian_detector;\n  int __pyx_v_little_endian;\n  PyObject *__pyx_v_fields = 0;\n  PyObject *__pyx_v_childname = NULL;\n  PyObject *__pyx_v_new_offset = NULL;\n  PyObject *__pyx_v_t = NULL;\n  char *__pyx_r;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  Py_ssize_t __pyx_t_2;\n  PyObject *__pyx_t_3 = NULL;\n  PyObject *__pyx_t_4 = NULL;\n  int __pyx_t_5;\n  int __pyx_t_6;\n  int __pyx_t_7;\n  int __pyx_t_8;\n  int __pyx_t_9;\n  long __pyx_t_10;\n  char *__pyx_t_11;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"_util_dtypestring\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":790\n *     cdef int delta_offset\n *     cdef tuple i\n *     cdef int endian_detector = 1             # <<<<<<<<<<<<<<\n *     cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)\n *     cdef tuple fields\n */\n  __pyx_v_endian_detector = 1;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":791\n *     cdef tuple i\n *     cdef int endian_detector = 1\n *     cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)             # <<<<<<<<<<<<<<\n *     cdef tuple fields\n * \n */\n  __pyx_v_little_endian = ((((char *)(&__pyx_v_endian_detector))[0]) != 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":794\n *     cdef tuple fields\n * \n *     for childname in descr.names:             # <<<<<<<<<<<<<<\n *         fields = descr.fields[childname]\n *         child, new_offset = fields\n */\n  if (unlikely(__pyx_v_descr->names == Py_None)) {\n    PyErr_SetString(PyExc_TypeError, \"'NoneType' object is not iterable\");\n    {__pyx_filename = __pyx_f[1]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  }\n  __pyx_t_1 = __pyx_v_descr->names; __Pyx_INCREF(__pyx_t_1); __pyx_t_2 = 0;\n  for (;;) {\n    if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_1)) break;\n    #if CYTHON_COMPILING_IN_CPYTHON\n    __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_3); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    #else\n    __pyx_t_3 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    #endif\n    __Pyx_XDECREF_SET(__pyx_v_childname, __pyx_t_3);\n    __pyx_t_3 = 0;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":795\n * \n *     for childname in descr.names:\n *         fields = descr.fields[childname]             # <<<<<<<<<<<<<<\n *         child, new_offset = fields\n * \n */\n    __pyx_t_3 = PyObject_GetItem(__pyx_v_descr->fields, __pyx_v_childname); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n    __Pyx_GOTREF(__pyx_t_3);\n    if (!(likely(PyTuple_CheckExact(__pyx_t_3))||((__pyx_t_3) == Py_None)||(PyErr_Format(PyExc_TypeError, \"Expected %.16s, got %.200s\", \"tuple\", Py_TYPE(__pyx_t_3)->tp_name), 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_XDECREF_SET(__pyx_v_fields, ((PyObject*)__pyx_t_3));\n    __pyx_t_3 = 0;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":796\n *     for childname in descr.names:\n *         fields = descr.fields[childname]\n *         child, new_offset = fields             # <<<<<<<<<<<<<<\n * \n *         if (end - f) - <int>(new_offset - offset[0]) < 15:\n */\n    if (likely(__pyx_v_fields != Py_None)) {\n      PyObject* sequence = __pyx_v_fields;\n      #if CYTHON_COMPILING_IN_CPYTHON\n      Py_ssize_t size = Py_SIZE(sequence);\n      #else\n      Py_ssize_t size = PySequence_Size(sequence);\n      #endif\n      if (unlikely(size != 2)) {\n        if (size > 2) __Pyx_RaiseTooManyValuesError(2);\n        else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);\n        {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      #if CYTHON_COMPILING_IN_CPYTHON\n      __pyx_t_3 = PyTuple_GET_ITEM(sequence, 0); \n      __pyx_t_4 = PyTuple_GET_ITEM(sequence, 1); \n      __Pyx_INCREF(__pyx_t_3);\n      __Pyx_INCREF(__pyx_t_4);\n      #else\n      __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      #endif\n    } else {\n      __Pyx_RaiseNoneNotIterableError(); {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n    if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_dtype))))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_XDECREF_SET(__pyx_v_child, ((PyArray_Descr *)__pyx_t_3));\n    __pyx_t_3 = 0;\n    __Pyx_XDECREF_SET(__pyx_v_new_offset, __pyx_t_4);\n    __pyx_t_4 = 0;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":798\n *         child, new_offset = fields\n * \n *         if (end - f) - <int>(new_offset - offset[0]) < 15:             # <<<<<<<<<<<<<<\n *             raise RuntimeError(u\"Format string allocated too short, see comment in numpy.pxd\")\n * \n */\n    __pyx_t_4 = __Pyx_PyInt_From_int((__pyx_v_offset[0])); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_GOTREF(__pyx_t_4);\n    __pyx_t_3 = PyNumber_Subtract(__pyx_v_new_offset, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_GOTREF(__pyx_t_3);\n    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n    __pyx_t_5 = __Pyx_PyInt_As_int(__pyx_t_3); if (unlikely((__pyx_t_5 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n    __pyx_t_6 = ((((__pyx_v_end - __pyx_v_f) - ((int)__pyx_t_5)) < 15) != 0);\n    if (__pyx_t_6) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":799\n * \n *         if (end - f) - <int>(new_offset - offset[0]) < 15:\n *             raise RuntimeError(u\"Format string allocated too short, see comment in numpy.pxd\")             # <<<<<<<<<<<<<<\n * \n *         if ((child.byteorder == c'>' and little_endian) or\n */\n      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_RuntimeError, __pyx_tuple__15, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __Pyx_Raise(__pyx_t_3, 0, 0, 0);\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":801\n *             raise RuntimeError(u\"Format string allocated too short, see comment in numpy.pxd\")\n * \n *         if ((child.byteorder == c'>' and little_endian) or             # <<<<<<<<<<<<<<\n *             (child.byteorder == c'<' and not little_endian)):\n *             raise ValueError(u\"Non-native byte order not supported\")\n */\n    __pyx_t_6 = ((__pyx_v_child->byteorder == '>') != 0);\n    if (__pyx_t_6) {\n      __pyx_t_7 = (__pyx_v_little_endian != 0);\n    } else {\n      __pyx_t_7 = __pyx_t_6;\n    }\n    if (!__pyx_t_7) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":802\n * \n *         if ((child.byteorder == c'>' and little_endian) or\n *             (child.byteorder == c'<' and not little_endian)):             # <<<<<<<<<<<<<<\n *             raise ValueError(u\"Non-native byte order not supported\")\n *             # One could encode it in the format string and have Cython\n */\n      __pyx_t_6 = ((__pyx_v_child->byteorder == '<') != 0);\n      if (__pyx_t_6) {\n        __pyx_t_8 = ((!(__pyx_v_little_endian != 0)) != 0);\n        __pyx_t_9 = __pyx_t_8;\n      } else {\n        __pyx_t_9 = __pyx_t_6;\n      }\n      __pyx_t_6 = __pyx_t_9;\n    } else {\n      __pyx_t_6 = __pyx_t_7;\n    }\n    if (__pyx_t_6) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":803\n *         if ((child.byteorder == c'>' and little_endian) or\n *             (child.byteorder == c'<' and not little_endian)):\n *             raise ValueError(u\"Non-native byte order not supported\")             # <<<<<<<<<<<<<<\n *             # One could encode it in the format string and have Cython\n *             # complain instead, BUT: < and > in format strings also imply\n */\n      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__16, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __Pyx_Raise(__pyx_t_3, 0, 0, 0);\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":813\n * \n *         # Output padding bytes\n *         while offset[0] < new_offset:             # <<<<<<<<<<<<<<\n *             f[0] = 120 # \"x\"; pad byte\n *             f += 1\n */\n    while (1) {\n      __pyx_t_3 = __Pyx_PyInt_From_int((__pyx_v_offset[0])); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_t_3, __pyx_v_new_offset, Py_LT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (!__pyx_t_6) break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":814\n *         # Output padding bytes\n *         while offset[0] < new_offset:\n *             f[0] = 120 # \"x\"; pad byte             # <<<<<<<<<<<<<<\n *             f += 1\n *             offset[0] += 1\n */\n      (__pyx_v_f[0]) = 120;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":815\n *         while offset[0] < new_offset:\n *             f[0] = 120 # \"x\"; pad byte\n *             f += 1             # <<<<<<<<<<<<<<\n *             offset[0] += 1\n * \n */\n      __pyx_v_f = (__pyx_v_f + 1);\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":816\n *             f[0] = 120 # \"x\"; pad byte\n *             f += 1\n *             offset[0] += 1             # <<<<<<<<<<<<<<\n * \n *         offset[0] += child.itemsize\n */\n      __pyx_t_10 = 0;\n      (__pyx_v_offset[__pyx_t_10]) = ((__pyx_v_offset[__pyx_t_10]) + 1);\n    }\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":818\n *             offset[0] += 1\n * \n *         offset[0] += child.itemsize             # <<<<<<<<<<<<<<\n * \n *         if not PyDataType_HASFIELDS(child):\n */\n    __pyx_t_10 = 0;\n    (__pyx_v_offset[__pyx_t_10]) = ((__pyx_v_offset[__pyx_t_10]) + __pyx_v_child->elsize);\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":820\n *         offset[0] += child.itemsize\n * \n *         if not PyDataType_HASFIELDS(child):             # <<<<<<<<<<<<<<\n *             t = child.type_num\n *             if end - f < 5:\n */\n    __pyx_t_6 = ((!(PyDataType_HASFIELDS(__pyx_v_child) != 0)) != 0);\n    if (__pyx_t_6) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":821\n * \n *         if not PyDataType_HASFIELDS(child):\n *             t = child.type_num             # <<<<<<<<<<<<<<\n *             if end - f < 5:\n *                 raise RuntimeError(u\"Format string allocated too short.\")\n */\n      __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_child->type_num); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __Pyx_XDECREF_SET(__pyx_v_t, __pyx_t_4);\n      __pyx_t_4 = 0;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":822\n *         if not PyDataType_HASFIELDS(child):\n *             t = child.type_num\n *             if end - f < 5:             # <<<<<<<<<<<<<<\n *                 raise RuntimeError(u\"Format string allocated too short.\")\n * \n */\n      __pyx_t_6 = (((__pyx_v_end - __pyx_v_f) < 5) != 0);\n      if (__pyx_t_6) {\n\n        /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":823\n *             t = child.type_num\n *             if end - f < 5:\n *                 raise RuntimeError(u\"Format string allocated too short.\")             # <<<<<<<<<<<<<<\n * \n *             # Until ticket #99 is fixed, use integers to avoid warnings\n */\n        __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_RuntimeError, __pyx_tuple__17, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n        __Pyx_GOTREF(__pyx_t_4);\n        __Pyx_Raise(__pyx_t_4, 0, 0, 0);\n        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n        {__pyx_filename = __pyx_f[1]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":826\n * \n *             # Until ticket #99 is fixed, use integers to avoid warnings\n *             if   t == NPY_BYTE:        f[0] =  98 #\"b\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_UBYTE:       f[0] =  66 #\"B\"\n *             elif t == NPY_SHORT:       f[0] = 104 #\"h\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_BYTE); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 98;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":827\n *             # Until ticket #99 is fixed, use integers to avoid warnings\n *             if   t == NPY_BYTE:        f[0] =  98 #\"b\"\n *             elif t == NPY_UBYTE:       f[0] =  66 #\"B\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_SHORT:       f[0] = 104 #\"h\"\n *             elif t == NPY_USHORT:      f[0] =  72 #\"H\"\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_UBYTE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 66;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":828\n *             if   t == NPY_BYTE:        f[0] =  98 #\"b\"\n *             elif t == NPY_UBYTE:       f[0] =  66 #\"B\"\n *             elif t == NPY_SHORT:       f[0] = 104 #\"h\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_USHORT:      f[0] =  72 #\"H\"\n *             elif t == NPY_INT:         f[0] = 105 #\"i\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_SHORT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 104;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":829\n *             elif t == NPY_UBYTE:       f[0] =  66 #\"B\"\n *             elif t == NPY_SHORT:       f[0] = 104 #\"h\"\n *             elif t == NPY_USHORT:      f[0] =  72 #\"H\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_INT:         f[0] = 105 #\"i\"\n *             elif t == NPY_UINT:        f[0] =  73 #\"I\"\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_USHORT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 72;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":830\n *             elif t == NPY_SHORT:       f[0] = 104 #\"h\"\n *             elif t == NPY_USHORT:      f[0] =  72 #\"H\"\n *             elif t == NPY_INT:         f[0] = 105 #\"i\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_UINT:        f[0] =  73 #\"I\"\n *             elif t == NPY_LONG:        f[0] = 108 #\"l\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_INT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 105;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":831\n *             elif t == NPY_USHORT:      f[0] =  72 #\"H\"\n *             elif t == NPY_INT:         f[0] = 105 #\"i\"\n *             elif t == NPY_UINT:        f[0] =  73 #\"I\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_LONG:        f[0] = 108 #\"l\"\n *             elif t == NPY_ULONG:       f[0] = 76  #\"L\"\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_UINT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 73;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":832\n *             elif t == NPY_INT:         f[0] = 105 #\"i\"\n *             elif t == NPY_UINT:        f[0] =  73 #\"I\"\n *             elif t == NPY_LONG:        f[0] = 108 #\"l\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_ULONG:       f[0] = 76  #\"L\"\n *             elif t == NPY_LONGLONG:    f[0] = 113 #\"q\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_LONG); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 108;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":833\n *             elif t == NPY_UINT:        f[0] =  73 #\"I\"\n *             elif t == NPY_LONG:        f[0] = 108 #\"l\"\n *             elif t == NPY_ULONG:       f[0] = 76  #\"L\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_LONGLONG:    f[0] = 113 #\"q\"\n *             elif t == NPY_ULONGLONG:   f[0] = 81  #\"Q\"\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_ULONG); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 76;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":834\n *             elif t == NPY_LONG:        f[0] = 108 #\"l\"\n *             elif t == NPY_ULONG:       f[0] = 76  #\"L\"\n *             elif t == NPY_LONGLONG:    f[0] = 113 #\"q\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_ULONGLONG:   f[0] = 81  #\"Q\"\n *             elif t == NPY_FLOAT:       f[0] = 102 #\"f\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_LONGLONG); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 113;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":835\n *             elif t == NPY_ULONG:       f[0] = 76  #\"L\"\n *             elif t == NPY_LONGLONG:    f[0] = 113 #\"q\"\n *             elif t == NPY_ULONGLONG:   f[0] = 81  #\"Q\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_FLOAT:       f[0] = 102 #\"f\"\n *             elif t == NPY_DOUBLE:      f[0] = 100 #\"d\"\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_ULONGLONG); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 81;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":836\n *             elif t == NPY_LONGLONG:    f[0] = 113 #\"q\"\n *             elif t == NPY_ULONGLONG:   f[0] = 81  #\"Q\"\n *             elif t == NPY_FLOAT:       f[0] = 102 #\"f\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_DOUBLE:      f[0] = 100 #\"d\"\n *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #\"g\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_FLOAT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 102;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":837\n *             elif t == NPY_ULONGLONG:   f[0] = 81  #\"Q\"\n *             elif t == NPY_FLOAT:       f[0] = 102 #\"f\"\n *             elif t == NPY_DOUBLE:      f[0] = 100 #\"d\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #\"g\"\n *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_DOUBLE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 100;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":838\n *             elif t == NPY_FLOAT:       f[0] = 102 #\"f\"\n *             elif t == NPY_DOUBLE:      f[0] = 100 #\"d\"\n *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #\"g\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf\n *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_LONGDOUBLE); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 103;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":839\n *             elif t == NPY_DOUBLE:      f[0] = 100 #\"d\"\n *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #\"g\"\n *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf             # <<<<<<<<<<<<<<\n *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd\n *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_CFLOAT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 90;\n        (__pyx_v_f[1]) = 102;\n        __pyx_v_f = (__pyx_v_f + 1);\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":840\n *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #\"g\"\n *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf\n *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd             # <<<<<<<<<<<<<<\n *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg\n *             elif t == NPY_OBJECT:      f[0] = 79 #\"O\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_CDOUBLE); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 90;\n        (__pyx_v_f[1]) = 100;\n        __pyx_v_f = (__pyx_v_f + 1);\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":841\n *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf\n *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd\n *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg             # <<<<<<<<<<<<<<\n *             elif t == NPY_OBJECT:      f[0] = 79 #\"O\"\n *             else:\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_CLONGDOUBLE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 90;\n        (__pyx_v_f[1]) = 103;\n        __pyx_v_f = (__pyx_v_f + 1);\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":842\n *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd\n *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg\n *             elif t == NPY_OBJECT:      f[0] = 79 #\"O\"             # <<<<<<<<<<<<<<\n *             else:\n *                 raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_OBJECT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 79;\n        goto __pyx_L11;\n      }\n      /*else*/ {\n\n        /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":844\n *             elif t == NPY_OBJECT:      f[0] = 79 #\"O\"\n *             else:\n *                 raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)             # <<<<<<<<<<<<<<\n *             f += 1\n *         else:\n */\n        __pyx_t_3 = PyUnicode_Format(__pyx_kp_u_unknown_dtype_code_in_numpy_pxd, __pyx_v_t); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n        __Pyx_GOTREF(__pyx_t_3);\n        __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n        __Pyx_GOTREF(__pyx_t_4);\n        PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_3);\n        __Pyx_GIVEREF(__pyx_t_3);\n        __pyx_t_3 = 0;\n        __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n        __Pyx_GOTREF(__pyx_t_3);\n        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n        __Pyx_Raise(__pyx_t_3, 0, 0, 0);\n        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n        {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      __pyx_L11:;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":845\n *             else:\n *                 raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n *             f += 1             # <<<<<<<<<<<<<<\n *         else:\n *             # Cython ignores struct boundary information (\"T{...}\"),\n */\n      __pyx_v_f = (__pyx_v_f + 1);\n      goto __pyx_L9;\n    }\n    /*else*/ {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":849\n *             # Cython ignores struct boundary information (\"T{...}\"),\n *             # so don't output it\n *             f = _util_dtypestring(child, f, end, offset)             # <<<<<<<<<<<<<<\n *     return f\n * \n */\n      __pyx_t_11 = __pyx_f_5numpy__util_dtypestring(__pyx_v_child, __pyx_v_f, __pyx_v_end, __pyx_v_offset); if (unlikely(__pyx_t_11 == NULL)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 849; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __pyx_v_f = __pyx_t_11;\n    }\n    __pyx_L9:;\n  }\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":850\n *             # so don't output it\n *             f = _util_dtypestring(child, f, end, offset)\n *     return f             # <<<<<<<<<<<<<<\n * \n * \n */\n  __pyx_r = __pyx_v_f;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":783\n *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)\n * \n * cdef inline char* _util_dtypestring(dtype descr, char* f, char* end, int* offset) except NULL:             # <<<<<<<<<<<<<<\n *     # Recursive utility function used in __getbuffer__ to get format\n *     # string. The new location in the format string is returned.\n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_XDECREF(__pyx_t_3);\n  __Pyx_XDECREF(__pyx_t_4);\n  __Pyx_AddTraceback(\"numpy._util_dtypestring\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = NULL;\n  __pyx_L0:;\n  __Pyx_XDECREF((PyObject *)__pyx_v_child);\n  __Pyx_XDECREF(__pyx_v_fields);\n  __Pyx_XDECREF(__pyx_v_childname);\n  __Pyx_XDECREF(__pyx_v_new_offset);\n  __Pyx_XDECREF(__pyx_v_t);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":966\n * \n * \n * cdef inline void set_array_base(ndarray arr, object base):             # <<<<<<<<<<<<<<\n *      cdef PyObject* baseptr\n *      if base is None:\n */\n\nstatic CYTHON_INLINE void __pyx_f_5numpy_set_array_base(PyArrayObject *__pyx_v_arr, PyObject *__pyx_v_base) {\n  PyObject *__pyx_v_baseptr;\n  __Pyx_RefNannyDeclarations\n  int __pyx_t_1;\n  int __pyx_t_2;\n  __Pyx_RefNannySetupContext(\"set_array_base\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":968\n * cdef inline void set_array_base(ndarray arr, object base):\n *      cdef PyObject* baseptr\n *      if base is None:             # <<<<<<<<<<<<<<\n *          baseptr = NULL\n *      else:\n */\n  __pyx_t_1 = (__pyx_v_base == Py_None);\n  __pyx_t_2 = (__pyx_t_1 != 0);\n  if (__pyx_t_2) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":969\n *      cdef PyObject* baseptr\n *      if base is None:\n *          baseptr = NULL             # <<<<<<<<<<<<<<\n *      else:\n *          Py_INCREF(base) # important to do this before decref below!\n */\n    __pyx_v_baseptr = NULL;\n    goto __pyx_L3;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":971\n *          baseptr = NULL\n *      else:\n *          Py_INCREF(base) # important to do this before decref below!             # <<<<<<<<<<<<<<\n *          baseptr = <PyObject*>base\n *      Py_XDECREF(arr.base)\n */\n    Py_INCREF(__pyx_v_base);\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":972\n *      else:\n *          Py_INCREF(base) # important to do this before decref below!\n *          baseptr = <PyObject*>base             # <<<<<<<<<<<<<<\n *      Py_XDECREF(arr.base)\n *      arr.base = baseptr\n */\n    __pyx_v_baseptr = ((PyObject *)__pyx_v_base);\n  }\n  __pyx_L3:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":973\n *          Py_INCREF(base) # important to do this before decref below!\n *          baseptr = <PyObject*>base\n *      Py_XDECREF(arr.base)             # <<<<<<<<<<<<<<\n *      arr.base = baseptr\n * \n */\n  Py_XDECREF(__pyx_v_arr->base);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":974\n *          baseptr = <PyObject*>base\n *      Py_XDECREF(arr.base)\n *      arr.base = baseptr             # <<<<<<<<<<<<<<\n * \n * cdef inline object get_array_base(ndarray arr):\n */\n  __pyx_v_arr->base = __pyx_v_baseptr;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":966\n * \n * \n * cdef inline void set_array_base(ndarray arr, object base):             # <<<<<<<<<<<<<<\n *      cdef PyObject* baseptr\n *      if base is None:\n */\n\n  /* function exit code */\n  __Pyx_RefNannyFinishContext();\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":976\n *      arr.base = baseptr\n * \n * cdef inline object get_array_base(ndarray arr):             # <<<<<<<<<<<<<<\n *     if arr.base is NULL:\n *         return None\n */\n\nstatic CYTHON_INLINE PyObject *__pyx_f_5numpy_get_array_base(PyArrayObject *__pyx_v_arr) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  int __pyx_t_1;\n  __Pyx_RefNannySetupContext(\"get_array_base\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":977\n * \n * cdef inline object get_array_base(ndarray arr):\n *     if arr.base is NULL:             # <<<<<<<<<<<<<<\n *         return None\n *     else:\n */\n  __pyx_t_1 = ((__pyx_v_arr->base == NULL) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":978\n * cdef inline object get_array_base(ndarray arr):\n *     if arr.base is NULL:\n *         return None             # <<<<<<<<<<<<<<\n *     else:\n *         return <object>arr.base\n */\n    __Pyx_XDECREF(__pyx_r);\n    __Pyx_INCREF(Py_None);\n    __pyx_r = Py_None;\n    goto __pyx_L0;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":980\n *         return None\n *     else:\n *         return <object>arr.base             # <<<<<<<<<<<<<<\n */\n    __Pyx_XDECREF(__pyx_r);\n    __Pyx_INCREF(((PyObject *)__pyx_v_arr->base));\n    __pyx_r = ((PyObject *)__pyx_v_arr->base);\n    goto __pyx_L0;\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":976\n *      arr.base = baseptr\n * \n * cdef inline object get_array_base(ndarray arr):             # <<<<<<<<<<<<<<\n *     if arr.base is NULL:\n *         return None\n */\n\n  /* function exit code */\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\nstatic PyMethodDef __pyx_methods[] = {\n  {0, 0, 0, 0}\n};\n\n#if PY_MAJOR_VERSION >= 3\nstatic struct PyModuleDef __pyx_moduledef = {\n  #if PY_VERSION_HEX < 0x03020000\n    { PyObject_HEAD_INIT(NULL) NULL, 0, NULL },\n  #else\n    PyModuleDef_HEAD_INIT,\n  #endif\n    __Pyx_NAMESTR(\"cpu_nms\"),\n    0, /* m_doc */\n    -1, /* m_size */\n    __pyx_methods /* m_methods */,\n    NULL, /* m_reload */\n    NULL, /* m_traverse */\n    NULL, /* m_clear */\n    NULL /* m_free */\n};\n#endif\n\nstatic __Pyx_StringTabEntry __pyx_string_tab[] = {\n  {&__pyx_kp_u_Format_string_allocated_too_shor, __pyx_k_Format_string_allocated_too_shor, sizeof(__pyx_k_Format_string_allocated_too_shor), 0, 1, 0, 0},\n  {&__pyx_kp_u_Format_string_allocated_too_shor_2, __pyx_k_Format_string_allocated_too_shor_2, sizeof(__pyx_k_Format_string_allocated_too_shor_2), 0, 1, 0, 0},\n  {&__pyx_kp_u_Non_native_byte_order_not_suppor, __pyx_k_Non_native_byte_order_not_suppor, sizeof(__pyx_k_Non_native_byte_order_not_suppor), 0, 1, 0, 0},\n  {&__pyx_n_s_RuntimeError, __pyx_k_RuntimeError, sizeof(__pyx_k_RuntimeError), 0, 0, 1, 1},\n  {&__pyx_n_s_ValueError, __pyx_k_ValueError, sizeof(__pyx_k_ValueError), 0, 0, 1, 1},\n  {&__pyx_n_s_areas, __pyx_k_areas, sizeof(__pyx_k_areas), 0, 0, 1, 1},\n  {&__pyx_n_s_argsort, __pyx_k_argsort, sizeof(__pyx_k_argsort), 0, 0, 1, 1},\n  {&__pyx_n_s_cpu_nms, __pyx_k_cpu_nms, sizeof(__pyx_k_cpu_nms), 0, 0, 1, 1},\n  {&__pyx_n_s_dets, __pyx_k_dets, sizeof(__pyx_k_dets), 0, 0, 1, 1},\n  {&__pyx_n_s_dtype, __pyx_k_dtype, sizeof(__pyx_k_dtype), 0, 0, 1, 1},\n  {&__pyx_n_s_h, __pyx_k_h, sizeof(__pyx_k_h), 0, 0, 1, 1},\n  {&__pyx_n_s_i, __pyx_k_i, sizeof(__pyx_k_i), 0, 0, 1, 1},\n  {&__pyx_n_s_i_2, __pyx_k_i_2, sizeof(__pyx_k_i_2), 0, 0, 1, 1},\n  {&__pyx_n_s_iarea, __pyx_k_iarea, sizeof(__pyx_k_iarea), 0, 0, 1, 1},\n  {&__pyx_n_s_import, __pyx_k_import, sizeof(__pyx_k_import), 0, 0, 1, 1},\n  {&__pyx_n_s_int, __pyx_k_int, sizeof(__pyx_k_int), 0, 0, 1, 1},\n  {&__pyx_n_s_inter, __pyx_k_inter, sizeof(__pyx_k_inter), 0, 0, 1, 1},\n  {&__pyx_n_s_ix1, __pyx_k_ix1, sizeof(__pyx_k_ix1), 0, 0, 1, 1},\n  {&__pyx_n_s_ix2, __pyx_k_ix2, sizeof(__pyx_k_ix2), 0, 0, 1, 1},\n  {&__pyx_n_s_iy1, __pyx_k_iy1, sizeof(__pyx_k_iy1), 0, 0, 1, 1},\n  {&__pyx_n_s_iy2, __pyx_k_iy2, sizeof(__pyx_k_iy2), 0, 0, 1, 1},\n  {&__pyx_n_s_j, __pyx_k_j, sizeof(__pyx_k_j), 0, 0, 1, 1},\n  {&__pyx_n_s_j_2, __pyx_k_j_2, sizeof(__pyx_k_j_2), 0, 0, 1, 1},\n  {&__pyx_n_s_keep, __pyx_k_keep, sizeof(__pyx_k_keep), 0, 0, 1, 1},\n  {&__pyx_n_s_main, __pyx_k_main, sizeof(__pyx_k_main), 0, 0, 1, 1},\n  {&__pyx_kp_u_ndarray_is_not_C_contiguous, __pyx_k_ndarray_is_not_C_contiguous, sizeof(__pyx_k_ndarray_is_not_C_contiguous), 0, 1, 0, 0},\n  {&__pyx_kp_u_ndarray_is_not_Fortran_contiguou, __pyx_k_ndarray_is_not_Fortran_contiguou, sizeof(__pyx_k_ndarray_is_not_Fortran_contiguou), 0, 1, 0, 0},\n  {&__pyx_n_s_ndets, __pyx_k_ndets, sizeof(__pyx_k_ndets), 0, 0, 1, 1},\n  {&__pyx_kp_s_nfs_yoda_xinleic_Inf_Code_Faste, __pyx_k_nfs_yoda_xinleic_Inf_Code_Faste, sizeof(__pyx_k_nfs_yoda_xinleic_Inf_Code_Faste), 0, 0, 1, 0},\n  {&__pyx_n_s_nms_cpu_nms, __pyx_k_nms_cpu_nms, sizeof(__pyx_k_nms_cpu_nms), 0, 0, 1, 1},\n  {&__pyx_n_s_np, __pyx_k_np, sizeof(__pyx_k_np), 0, 0, 1, 1},\n  {&__pyx_n_s_numpy, __pyx_k_numpy, sizeof(__pyx_k_numpy), 0, 0, 1, 1},\n  {&__pyx_n_s_order, __pyx_k_order, sizeof(__pyx_k_order), 0, 0, 1, 1},\n  {&__pyx_n_s_ovr, __pyx_k_ovr, sizeof(__pyx_k_ovr), 0, 0, 1, 1},\n  {&__pyx_n_s_pyx_getbuffer, __pyx_k_pyx_getbuffer, sizeof(__pyx_k_pyx_getbuffer), 0, 0, 1, 1},\n  {&__pyx_n_s_pyx_releasebuffer, __pyx_k_pyx_releasebuffer, sizeof(__pyx_k_pyx_releasebuffer), 0, 0, 1, 1},\n  {&__pyx_n_s_range, __pyx_k_range, sizeof(__pyx_k_range), 0, 0, 1, 1},\n  {&__pyx_n_s_scores, __pyx_k_scores, sizeof(__pyx_k_scores), 0, 0, 1, 1},\n  {&__pyx_n_s_suppressed, __pyx_k_suppressed, sizeof(__pyx_k_suppressed), 0, 0, 1, 1},\n  {&__pyx_n_s_test, __pyx_k_test, sizeof(__pyx_k_test), 0, 0, 1, 1},\n  {&__pyx_n_s_thresh, __pyx_k_thresh, sizeof(__pyx_k_thresh), 0, 0, 1, 1},\n  {&__pyx_kp_u_unknown_dtype_code_in_numpy_pxd, __pyx_k_unknown_dtype_code_in_numpy_pxd, sizeof(__pyx_k_unknown_dtype_code_in_numpy_pxd), 0, 1, 0, 0},\n  {&__pyx_n_s_w, __pyx_k_w, sizeof(__pyx_k_w), 0, 0, 1, 1},\n  {&__pyx_n_s_x1, __pyx_k_x1, sizeof(__pyx_k_x1), 0, 0, 1, 1},\n  {&__pyx_n_s_x2, __pyx_k_x2, sizeof(__pyx_k_x2), 0, 0, 1, 1},\n  {&__pyx_n_s_xx1, __pyx_k_xx1, sizeof(__pyx_k_xx1), 0, 0, 1, 1},\n  {&__pyx_n_s_xx2, __pyx_k_xx2, sizeof(__pyx_k_xx2), 0, 0, 1, 1},\n  {&__pyx_n_s_y1, __pyx_k_y1, sizeof(__pyx_k_y1), 0, 0, 1, 1},\n  {&__pyx_n_s_y2, __pyx_k_y2, sizeof(__pyx_k_y2), 0, 0, 1, 1},\n  {&__pyx_n_s_yy1, __pyx_k_yy1, sizeof(__pyx_k_yy1), 0, 0, 1, 1},\n  {&__pyx_n_s_yy2, __pyx_k_yy2, sizeof(__pyx_k_yy2), 0, 0, 1, 1},\n  {&__pyx_n_s_zeros, __pyx_k_zeros, sizeof(__pyx_k_zeros), 0, 0, 1, 1},\n  {0, 0, 0, 0, 0, 0, 0}\n};\nstatic int __Pyx_InitCachedBuiltins(void) {\n  __pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_range) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_builtin_RuntimeError = __Pyx_GetBuiltinName(__pyx_n_s_RuntimeError); if (!__pyx_builtin_RuntimeError) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  return 0;\n  __pyx_L1_error:;\n  return -1;\n}\n\nstatic int __Pyx_InitCachedConstants(void) {\n  __Pyx_RefNannyDeclarations\n  __Pyx_RefNannySetupContext(\"__Pyx_InitCachedConstants\", 0);\n\n  /* \"nms/cpu_nms.pyx\":18\n * \n * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):\n *     cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n *     cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]\n */\n  __pyx_slice_ = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice_)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_slice_);\n  __Pyx_GIVEREF(__pyx_slice_);\n  __pyx_tuple__2 = PyTuple_Pack(2, __pyx_slice_, __pyx_int_0); if (unlikely(!__pyx_tuple__2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__2);\n  __Pyx_GIVEREF(__pyx_tuple__2);\n\n  /* \"nms/cpu_nms.pyx\":19\n * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):\n *     cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]\n *     cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]\n */\n  __pyx_slice__3 = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice__3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_slice__3);\n  __Pyx_GIVEREF(__pyx_slice__3);\n  __pyx_tuple__4 = PyTuple_Pack(2, __pyx_slice__3, __pyx_int_1); if (unlikely(!__pyx_tuple__4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__4);\n  __Pyx_GIVEREF(__pyx_tuple__4);\n\n  /* \"nms/cpu_nms.pyx\":20\n *     cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n *     cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]\n *     cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4]\n */\n  __pyx_slice__5 = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice__5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_slice__5);\n  __Pyx_GIVEREF(__pyx_slice__5);\n  __pyx_tuple__6 = PyTuple_Pack(2, __pyx_slice__5, __pyx_int_2); if (unlikely(!__pyx_tuple__6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__6);\n  __Pyx_GIVEREF(__pyx_tuple__6);\n\n  /* \"nms/cpu_nms.pyx\":21\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n *     cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]\n *     cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4]\n * \n */\n  __pyx_slice__7 = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice__7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_slice__7);\n  __Pyx_GIVEREF(__pyx_slice__7);\n  __pyx_tuple__8 = PyTuple_Pack(2, __pyx_slice__7, __pyx_int_3); if (unlikely(!__pyx_tuple__8)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__8);\n  __Pyx_GIVEREF(__pyx_tuple__8);\n\n  /* \"nms/cpu_nms.pyx\":22\n *     cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]\n *     cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]\n *     cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4]             # <<<<<<<<<<<<<<\n * \n *     cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1)\n */\n  __pyx_slice__9 = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice__9)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_slice__9);\n  __Pyx_GIVEREF(__pyx_slice__9);\n  __pyx_tuple__10 = PyTuple_Pack(2, __pyx_slice__9, __pyx_int_4); if (unlikely(!__pyx_tuple__10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__10);\n  __Pyx_GIVEREF(__pyx_tuple__10);\n\n  /* \"nms/cpu_nms.pyx\":25\n * \n *     cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1)\n *     cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1]             # <<<<<<<<<<<<<<\n * \n *     cdef int ndets = dets.shape[0]\n */\n  __pyx_slice__11 = PySlice_New(Py_None, Py_None, __pyx_int_neg_1); if (unlikely(!__pyx_slice__11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_slice__11);\n  __Pyx_GIVEREF(__pyx_slice__11);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":215\n *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not C contiguous\")             # <<<<<<<<<<<<<<\n * \n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)\n */\n  __pyx_tuple__12 = PyTuple_Pack(1, __pyx_kp_u_ndarray_is_not_C_contiguous); if (unlikely(!__pyx_tuple__12)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__12);\n  __Pyx_GIVEREF(__pyx_tuple__12);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":219\n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not Fortran contiguous\")             # <<<<<<<<<<<<<<\n * \n *             info.buf = PyArray_DATA(self)\n */\n  __pyx_tuple__13 = PyTuple_Pack(1, __pyx_kp_u_ndarray_is_not_Fortran_contiguou); if (unlikely(!__pyx_tuple__13)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__13);\n  __Pyx_GIVEREF(__pyx_tuple__13);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":257\n *                 if ((descr.byteorder == c'>' and little_endian) or\n *                     (descr.byteorder == c'<' and not little_endian)):\n *                     raise ValueError(u\"Non-native byte order not supported\")             # <<<<<<<<<<<<<<\n *                 if   t == NPY_BYTE:        f = \"b\"\n *                 elif t == NPY_UBYTE:       f = \"B\"\n */\n  __pyx_tuple__14 = PyTuple_Pack(1, __pyx_kp_u_Non_native_byte_order_not_suppor); if (unlikely(!__pyx_tuple__14)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__14);\n  __Pyx_GIVEREF(__pyx_tuple__14);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":799\n * \n *         if (end - f) - <int>(new_offset - offset[0]) < 15:\n *             raise RuntimeError(u\"Format string allocated too short, see comment in numpy.pxd\")             # <<<<<<<<<<<<<<\n * \n *         if ((child.byteorder == c'>' and little_endian) or\n */\n  __pyx_tuple__15 = PyTuple_Pack(1, __pyx_kp_u_Format_string_allocated_too_shor); if (unlikely(!__pyx_tuple__15)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__15);\n  __Pyx_GIVEREF(__pyx_tuple__15);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":803\n *         if ((child.byteorder == c'>' and little_endian) or\n *             (child.byteorder == c'<' and not little_endian)):\n *             raise ValueError(u\"Non-native byte order not supported\")             # <<<<<<<<<<<<<<\n *             # One could encode it in the format string and have Cython\n *             # complain instead, BUT: < and > in format strings also imply\n */\n  __pyx_tuple__16 = PyTuple_Pack(1, __pyx_kp_u_Non_native_byte_order_not_suppor); if (unlikely(!__pyx_tuple__16)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__16);\n  __Pyx_GIVEREF(__pyx_tuple__16);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":823\n *             t = child.type_num\n *             if end - f < 5:\n *                 raise RuntimeError(u\"Format string allocated too short.\")             # <<<<<<<<<<<<<<\n * \n *             # Until ticket #99 is fixed, use integers to avoid warnings\n */\n  __pyx_tuple__17 = PyTuple_Pack(1, __pyx_kp_u_Format_string_allocated_too_shor_2); if (unlikely(!__pyx_tuple__17)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__17);\n  __Pyx_GIVEREF(__pyx_tuple__17);\n\n  /* \"nms/cpu_nms.pyx\":17\n *     return a if a <= b else b\n * \n * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n */\n  __pyx_tuple__18 = 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_2, __pyx_n_s_j, __pyx_n_s_i, __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__18)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__18);\n  __Pyx_GIVEREF(__pyx_tuple__18);\n  __pyx_codeobj__19 = (PyObject*)__Pyx_PyCode_New(2, 0, 29, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__18, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_nfs_yoda_xinleic_Inf_Code_Faste, __pyx_n_s_cpu_nms, 17, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_RefNannyFinishContext();\n  return 0;\n  __pyx_L1_error:;\n  __Pyx_RefNannyFinishContext();\n  return -1;\n}\n\nstatic int __Pyx_InitGlobals(void) {\n  if (__Pyx_InitStrings(__pyx_string_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __pyx_int_0 = PyInt_FromLong(0); if (unlikely(!__pyx_int_0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_int_1 = PyInt_FromLong(1); if (unlikely(!__pyx_int_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_int_2 = PyInt_FromLong(2); if (unlikely(!__pyx_int_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_int_3 = PyInt_FromLong(3); if (unlikely(!__pyx_int_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_int_4 = PyInt_FromLong(4); if (unlikely(!__pyx_int_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_int_neg_1 = PyInt_FromLong(-1); if (unlikely(!__pyx_int_neg_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  return 0;\n  __pyx_L1_error:;\n  return -1;\n}\n\n#if PY_MAJOR_VERSION < 3\nPyMODINIT_FUNC initcpu_nms(void); /*proto*/\nPyMODINIT_FUNC initcpu_nms(void)\n#else\nPyMODINIT_FUNC PyInit_cpu_nms(void); /*proto*/\nPyMODINIT_FUNC PyInit_cpu_nms(void)\n#endif\n{\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannyDeclarations\n  #if CYTHON_REFNANNY\n  __Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"refnanny\");\n  if (!__Pyx_RefNanny) {\n      PyErr_Clear();\n      __Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"Cython.Runtime.refnanny\");\n      if (!__Pyx_RefNanny)\n          Py_FatalError(\"failed to import 'refnanny' module\");\n  }\n  #endif\n  __Pyx_RefNannySetupContext(\"PyMODINIT_FUNC PyInit_cpu_nms(void)\", 0);\n  if ( __Pyx_check_binary_version() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_empty_tuple = PyTuple_New(0); if (unlikely(!__pyx_empty_tuple)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_empty_bytes = PyBytes_FromStringAndSize(\"\", 0); if (unlikely(!__pyx_empty_bytes)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #ifdef __Pyx_CyFunction_USED\n  if (__Pyx_CyFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #endif\n  #ifdef __Pyx_FusedFunction_USED\n  if (__pyx_FusedFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #endif\n  #ifdef __Pyx_Generator_USED\n  if (__pyx_Generator_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #endif\n  /*--- Library function declarations ---*/\n  /*--- Threads initialization code ---*/\n  #if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS\n  #ifdef WITH_THREAD /* Python build with threading support? */\n  PyEval_InitThreads();\n  #endif\n  #endif\n  /*--- Module creation code ---*/\n  #if PY_MAJOR_VERSION < 3\n  __pyx_m = Py_InitModule4(__Pyx_NAMESTR(\"cpu_nms\"), __pyx_methods, 0, 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m);\n  #else\n  __pyx_m = PyModule_Create(&__pyx_moduledef);\n  #endif\n  if (unlikely(!__pyx_m)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_d = PyModule_GetDict(__pyx_m); if (unlikely(!__pyx_d)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  Py_INCREF(__pyx_d);\n  __pyx_b = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME)); if (unlikely(!__pyx_b)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #if CYTHON_COMPILING_IN_PYPY\n  Py_INCREF(__pyx_b);\n  #endif\n  if (__Pyx_SetAttrString(__pyx_m, \"__builtins__\", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  /*--- Initialize various global constants etc. ---*/\n  if (unlikely(__Pyx_InitGlobals() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT)\n  if (__Pyx_init_sys_getdefaultencoding_params() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #endif\n  if (__pyx_module_is_main_nms__cpu_nms) {\n    if (__Pyx_SetAttrString(__pyx_m, \"__name__\", __pyx_n_s_main) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  }\n  #if PY_MAJOR_VERSION >= 3\n  {\n    PyObject *modules = PyImport_GetModuleDict(); if (unlikely(!modules)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    if (!PyDict_GetItemString(modules, \"nms.cpu_nms\")) {\n      if (unlikely(PyDict_SetItemString(modules, \"nms.cpu_nms\", __pyx_m) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n  }\n  #endif\n  /*--- Builtin init code ---*/\n  if (unlikely(__Pyx_InitCachedBuiltins() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  /*--- Constants init code ---*/\n  if (unlikely(__Pyx_InitCachedConstants() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  /*--- Global init code ---*/\n  /*--- Variable export code ---*/\n  /*--- Function export code ---*/\n  /*--- Type init code ---*/\n  /*--- Type import code ---*/\n  __pyx_ptype_7cpython_4type_type = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, \"type\", \n  #if CYTHON_COMPILING_IN_PYPY\n  sizeof(PyTypeObject),\n  #else\n  sizeof(PyHeapTypeObject),\n  #endif\n  0); if (unlikely(!__pyx_ptype_7cpython_4type_type)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_ptype_5numpy_dtype = __Pyx_ImportType(\"numpy\", \"dtype\", sizeof(PyArray_Descr), 0); if (unlikely(!__pyx_ptype_5numpy_dtype)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_ptype_5numpy_flatiter = __Pyx_ImportType(\"numpy\", \"flatiter\", sizeof(PyArrayIterObject), 0); if (unlikely(!__pyx_ptype_5numpy_flatiter)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_ptype_5numpy_broadcast = __Pyx_ImportType(\"numpy\", \"broadcast\", sizeof(PyArrayMultiIterObject), 0); if (unlikely(!__pyx_ptype_5numpy_broadcast)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_ptype_5numpy_ndarray = __Pyx_ImportType(\"numpy\", \"ndarray\", sizeof(PyArrayObject), 0); if (unlikely(!__pyx_ptype_5numpy_ndarray)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_ptype_5numpy_ufunc = __Pyx_ImportType(\"numpy\", \"ufunc\", sizeof(PyUFuncObject), 0); if (unlikely(!__pyx_ptype_5numpy_ufunc)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 861; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  /*--- Variable import code ---*/\n  /*--- Function import code ---*/\n  /*--- Execution code ---*/\n\n  /* \"nms/cpu_nms.pyx\":8\n * # --------------------------------------------------------\n * \n * import numpy as np             # <<<<<<<<<<<<<<\n * cimport numpy as np\n * \n */\n  __pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n\n  /* \"nms/cpu_nms.pyx\":17\n *     return a if a <= b else b\n * \n * def cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]\n *     cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n */\n  __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_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  if (PyDict_SetItem(__pyx_d, __pyx_n_s_cpu_nms, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n\n  /* \"nms/cpu_nms.pyx\":1\n * # --------------------------------------------------------             # <<<<<<<<<<<<<<\n * # Fast R-CNN\n * # Copyright (c) 2015 Microsoft\n */\n  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":976\n *      arr.base = baseptr\n * \n * cdef inline object get_array_base(ndarray arr):             # <<<<<<<<<<<<<<\n *     if arr.base is NULL:\n *         return None\n */\n  goto __pyx_L0;\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  if (__pyx_m) {\n    __Pyx_AddTraceback(\"init nms.cpu_nms\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n    Py_DECREF(__pyx_m); __pyx_m = 0;\n  } else if (!PyErr_Occurred()) {\n    PyErr_SetString(PyExc_ImportError, \"init nms.cpu_nms\");\n  }\n  __pyx_L0:;\n  __Pyx_RefNannyFinishContext();\n  #if PY_MAJOR_VERSION < 3\n  return;\n  #else\n  return __pyx_m;\n  #endif\n}\n\n/* Runtime support code */\n#if CYTHON_REFNANNY\nstatic __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) {\n    PyObject *m = NULL, *p = NULL;\n    void *r = NULL;\n    m = PyImport_ImportModule((char *)modname);\n    if (!m) goto end;\n    p = PyObject_GetAttrString(m, (char *)\"RefNannyAPI\");\n    if (!p) goto end;\n    r = PyLong_AsVoidPtr(p);\nend:\n    Py_XDECREF(p);\n    Py_XDECREF(m);\n    return (__Pyx_RefNannyAPIStruct *)r;\n}\n#endif /* CYTHON_REFNANNY */\n\nstatic PyObject *__Pyx_GetBuiltinName(PyObject *name) {\n    PyObject* result = __Pyx_PyObject_GetAttrStr(__pyx_b, name);\n    if (unlikely(!result)) {\n        PyErr_Format(PyExc_NameError,\n#if PY_MAJOR_VERSION >= 3\n            \"name '%U' is not defined\", name);\n#else\n            \"name '%.200s' is not defined\", PyString_AS_STRING(name));\n#endif\n    }\n    return result;\n}\n\nstatic void __Pyx_RaiseArgtupleInvalid(\n    const char* func_name,\n    int exact,\n    Py_ssize_t num_min,\n    Py_ssize_t num_max,\n    Py_ssize_t num_found)\n{\n    Py_ssize_t num_expected;\n    const char *more_or_less;\n    if (num_found < num_min) {\n        num_expected = num_min;\n        more_or_less = \"at least\";\n    } else {\n        num_expected = num_max;\n        more_or_less = \"at most\";\n    }\n    if (exact) {\n        more_or_less = \"exactly\";\n    }\n    PyErr_Format(PyExc_TypeError,\n                 \"%.200s() takes %.8s %\" CYTHON_FORMAT_SSIZE_T \"d positional argument%.1s (%\" CYTHON_FORMAT_SSIZE_T \"d given)\",\n                 func_name, more_or_less, num_expected,\n                 (num_expected == 1) ? \"\" : \"s\", num_found);\n}\n\nstatic void __Pyx_RaiseDoubleKeywordsError(\n    const char* func_name,\n    PyObject* kw_name)\n{\n    PyErr_Format(PyExc_TypeError,\n        #if PY_MAJOR_VERSION >= 3\n        \"%s() got multiple values for keyword argument '%U'\", func_name, kw_name);\n        #else\n        \"%s() got multiple values for keyword argument '%s'\", func_name,\n        PyString_AsString(kw_name));\n        #endif\n}\n\nstatic int __Pyx_ParseOptionalKeywords(\n    PyObject *kwds,\n    PyObject **argnames[],\n    PyObject *kwds2,\n    PyObject *values[],\n    Py_ssize_t num_pos_args,\n    const char* function_name)\n{\n    PyObject *key = 0, *value = 0;\n    Py_ssize_t pos = 0;\n    PyObject*** name;\n    PyObject*** first_kw_arg = argnames + num_pos_args;\n    while (PyDict_Next(kwds, &pos, &key, &value)) {\n        name = first_kw_arg;\n        while (*name && (**name != key)) name++;\n        if (*name) {\n            values[name-argnames] = value;\n            continue;\n        }\n        name = first_kw_arg;\n        #if PY_MAJOR_VERSION < 3\n        if (likely(PyString_CheckExact(key)) || likely(PyString_Check(key))) {\n            while (*name) {\n                if ((CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**name) == PyString_GET_SIZE(key))\n                        && _PyString_Eq(**name, key)) {\n                    values[name-argnames] = value;\n                    break;\n                }\n                name++;\n            }\n            if (*name) continue;\n            else {\n                PyObject*** argname = argnames;\n                while (argname != first_kw_arg) {\n                    if ((**argname == key) || (\n                            (CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**argname) == PyString_GET_SIZE(key))\n                             && _PyString_Eq(**argname, key))) {\n                        goto arg_passed_twice;\n                    }\n                    argname++;\n                }\n            }\n        } else\n        #endif\n        if (likely(PyUnicode_Check(key))) {\n            while (*name) {\n                int cmp = (**name == key) ? 0 :\n                #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3\n                    (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :\n                #endif\n                    PyUnicode_Compare(**name, key);\n                if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;\n                if (cmp == 0) {\n                    values[name-argnames] = value;\n                    break;\n                }\n                name++;\n            }\n            if (*name) continue;\n            else {\n                PyObject*** argname = argnames;\n                while (argname != first_kw_arg) {\n                    int cmp = (**argname == key) ? 0 :\n                    #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3\n                        (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :\n                    #endif\n                        PyUnicode_Compare(**argname, key);\n                    if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;\n                    if (cmp == 0) goto arg_passed_twice;\n                    argname++;\n                }\n            }\n        } else\n            goto invalid_keyword_type;\n        if (kwds2) {\n            if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;\n        } else {\n            goto invalid_keyword;\n        }\n    }\n    return 0;\narg_passed_twice:\n    __Pyx_RaiseDoubleKeywordsError(function_name, key);\n    goto bad;\ninvalid_keyword_type:\n    PyErr_Format(PyExc_TypeError,\n        \"%.200s() keywords must be strings\", function_name);\n    goto bad;\ninvalid_keyword:\n    PyErr_Format(PyExc_TypeError,\n    #if PY_MAJOR_VERSION < 3\n        \"%.200s() got an unexpected keyword argument '%.200s'\",\n        function_name, PyString_AsString(key));\n    #else\n        \"%s() got an unexpected keyword argument '%U'\",\n        function_name, key);\n    #endif\nbad:\n    return -1;\n}\n\nstatic void __Pyx_RaiseArgumentTypeInvalid(const char* name, PyObject *obj, PyTypeObject *type) {\n    PyErr_Format(PyExc_TypeError,\n        \"Argument '%.200s' has incorrect type (expected %.200s, got %.200s)\",\n        name, type->tp_name, Py_TYPE(obj)->tp_name);\n}\nstatic CYTHON_INLINE int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,\n    const char *name, int exact)\n{\n    if (unlikely(!type)) {\n        PyErr_SetString(PyExc_SystemError, \"Missing type object\");\n        return 0;\n    }\n    if (none_allowed && obj == Py_None) return 1;\n    else if (exact) {\n        if (likely(Py_TYPE(obj) == type)) return 1;\n        #if PY_MAJOR_VERSION == 2\n        else if ((type == &PyBaseString_Type) && likely(__Pyx_PyBaseString_CheckExact(obj))) return 1;\n        #endif\n    }\n    else {\n        if (likely(PyObject_TypeCheck(obj, type))) return 1;\n    }\n    __Pyx_RaiseArgumentTypeInvalid(name, obj, type);\n    return 0;\n}\n\nstatic CYTHON_INLINE int __Pyx_IsLittleEndian(void) {\n  unsigned int n = 1;\n  return *(unsigned char*)(&n) != 0;\n}\nstatic void __Pyx_BufFmt_Init(__Pyx_BufFmt_Context* ctx,\n                              __Pyx_BufFmt_StackElem* stack,\n                              __Pyx_TypeInfo* type) {\n  stack[0].field = &ctx->root;\n  stack[0].parent_offset = 0;\n  ctx->root.type = type;\n  ctx->root.name = \"buffer dtype\";\n  ctx->root.offset = 0;\n  ctx->head = stack;\n  ctx->head->field = &ctx->root;\n  ctx->fmt_offset = 0;\n  ctx->head->parent_offset = 0;\n  ctx->new_packmode = '@';\n  ctx->enc_packmode = '@';\n  ctx->new_count = 1;\n  ctx->enc_count = 0;\n  ctx->enc_type = 0;\n  ctx->is_complex = 0;\n  ctx->is_valid_array = 0;\n  ctx->struct_alignment = 0;\n  while (type->typegroup == 'S') {\n    ++ctx->head;\n    ctx->head->field = type->fields;\n    ctx->head->parent_offset = 0;\n    type = type->fields->type;\n  }\n}\nstatic int __Pyx_BufFmt_ParseNumber(const char** ts) {\n    int count;\n    const char* t = *ts;\n    if (*t < '0' || *t > '9') {\n      return -1;\n    } else {\n        count = *t++ - '0';\n        while (*t >= '0' && *t < '9') {\n            count *= 10;\n            count += *t++ - '0';\n        }\n    }\n    *ts = t;\n    return count;\n}\nstatic int __Pyx_BufFmt_ExpectNumber(const char **ts) {\n    int number = __Pyx_BufFmt_ParseNumber(ts);\n    if (number == -1) /* First char was not a digit */\n        PyErr_Format(PyExc_ValueError,\\\n                     \"Does not understand character buffer dtype format string ('%c')\", **ts);\n    return number;\n}\nstatic void __Pyx_BufFmt_RaiseUnexpectedChar(char ch) {\n  PyErr_Format(PyExc_ValueError,\n               \"Unexpected format string character: '%c'\", ch);\n}\nstatic const char* __Pyx_BufFmt_DescribeTypeChar(char ch, int is_complex) {\n  switch (ch) {\n    case 'c': return \"'char'\";\n    case 'b': return \"'signed char'\";\n    case 'B': return \"'unsigned char'\";\n    case 'h': return \"'short'\";\n    case 'H': return \"'unsigned short'\";\n    case 'i': return \"'int'\";\n    case 'I': return \"'unsigned int'\";\n    case 'l': return \"'long'\";\n    case 'L': return \"'unsigned long'\";\n    case 'q': return \"'long long'\";\n    case 'Q': return \"'unsigned long long'\";\n    case 'f': return (is_complex ? \"'complex float'\" : \"'float'\");\n    case 'd': return (is_complex ? \"'complex double'\" : \"'double'\");\n    case 'g': return (is_complex ? \"'complex long double'\" : \"'long double'\");\n    case 'T': return \"a struct\";\n    case 'O': return \"Python object\";\n    case 'P': return \"a pointer\";\n    case 's': case 'p': return \"a string\";\n    case 0: return \"end\";\n    default: return \"unparseable format string\";\n  }\n}\nstatic size_t __Pyx_BufFmt_TypeCharToStandardSize(char ch, int is_complex) {\n  switch (ch) {\n    case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1;\n    case 'h': case 'H': return 2;\n    case 'i': case 'I': case 'l': case 'L': return 4;\n    case 'q': case 'Q': return 8;\n    case 'f': return (is_complex ? 8 : 4);\n    case 'd': return (is_complex ? 16 : 8);\n    case 'g': {\n      PyErr_SetString(PyExc_ValueError, \"Python does not define a standard format string size for long double ('g')..\");\n      return 0;\n    }\n    case 'O': case 'P': return sizeof(void*);\n    default:\n      __Pyx_BufFmt_RaiseUnexpectedChar(ch);\n      return 0;\n    }\n}\nstatic size_t __Pyx_BufFmt_TypeCharToNativeSize(char ch, int is_complex) {\n  switch (ch) {\n    case 'c': case 'b': case 'B': case 's': case 'p': return 1;\n    case 'h': case 'H': return sizeof(short);\n    case 'i': case 'I': return sizeof(int);\n    case 'l': case 'L': return sizeof(long);\n    #ifdef HAVE_LONG_LONG\n    case 'q': case 'Q': return sizeof(PY_LONG_LONG);\n    #endif\n    case 'f': return sizeof(float) * (is_complex ? 2 : 1);\n    case 'd': return sizeof(double) * (is_complex ? 2 : 1);\n    case 'g': return sizeof(long double) * (is_complex ? 2 : 1);\n    case 'O': case 'P': return sizeof(void*);\n    default: {\n      __Pyx_BufFmt_RaiseUnexpectedChar(ch);\n      return 0;\n    }\n  }\n}\ntypedef struct { char c; short x; } __Pyx_st_short;\ntypedef struct { char c; int x; } __Pyx_st_int;\ntypedef struct { char c; long x; } __Pyx_st_long;\ntypedef struct { char c; float x; } __Pyx_st_float;\ntypedef struct { char c; double x; } __Pyx_st_double;\ntypedef struct { char c; long double x; } __Pyx_st_longdouble;\ntypedef struct { char c; void *x; } __Pyx_st_void_p;\n#ifdef HAVE_LONG_LONG\ntypedef struct { char c; PY_LONG_LONG x; } __Pyx_st_longlong;\n#endif\nstatic size_t __Pyx_BufFmt_TypeCharToAlignment(char ch, CYTHON_UNUSED int is_complex) {\n  switch (ch) {\n    case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1;\n    case 'h': case 'H': return sizeof(__Pyx_st_short) - sizeof(short);\n    case 'i': case 'I': return sizeof(__Pyx_st_int) - sizeof(int);\n    case 'l': case 'L': return sizeof(__Pyx_st_long) - sizeof(long);\n#ifdef HAVE_LONG_LONG\n    case 'q': case 'Q': return sizeof(__Pyx_st_longlong) - sizeof(PY_LONG_LONG);\n#endif\n    case 'f': return sizeof(__Pyx_st_float) - sizeof(float);\n    case 'd': return sizeof(__Pyx_st_double) - sizeof(double);\n    case 'g': return sizeof(__Pyx_st_longdouble) - sizeof(long double);\n    case 'P': case 'O': return sizeof(__Pyx_st_void_p) - sizeof(void*);\n    default:\n      __Pyx_BufFmt_RaiseUnexpectedChar(ch);\n      return 0;\n    }\n}\n/* These are for computing the padding at the end of the struct to align\n   on the first member of the struct. This will probably the same as above,\n   but we don't have any guarantees.\n */\ntypedef struct { short x; char c; } __Pyx_pad_short;\ntypedef struct { int x; char c; } __Pyx_pad_int;\ntypedef struct { long x; char c; } __Pyx_pad_long;\ntypedef struct { float x; char c; } __Pyx_pad_float;\ntypedef struct { double x; char c; } __Pyx_pad_double;\ntypedef struct { long double x; char c; } __Pyx_pad_longdouble;\ntypedef struct { void *x; char c; } __Pyx_pad_void_p;\n#ifdef HAVE_LONG_LONG\ntypedef struct { PY_LONG_LONG x; char c; } __Pyx_pad_longlong;\n#endif\nstatic size_t __Pyx_BufFmt_TypeCharToPadding(char ch, CYTHON_UNUSED int is_complex) {\n  switch (ch) {\n    case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1;\n    case 'h': case 'H': return sizeof(__Pyx_pad_short) - sizeof(short);\n    case 'i': case 'I': return sizeof(__Pyx_pad_int) - sizeof(int);\n    case 'l': case 'L': return sizeof(__Pyx_pad_long) - sizeof(long);\n#ifdef HAVE_LONG_LONG\n    case 'q': case 'Q': return sizeof(__Pyx_pad_longlong) - sizeof(PY_LONG_LONG);\n#endif\n    case 'f': return sizeof(__Pyx_pad_float) - sizeof(float);\n    case 'd': return sizeof(__Pyx_pad_double) - sizeof(double);\n    case 'g': return sizeof(__Pyx_pad_longdouble) - sizeof(long double);\n    case 'P': case 'O': return sizeof(__Pyx_pad_void_p) - sizeof(void*);\n    default:\n      __Pyx_BufFmt_RaiseUnexpectedChar(ch);\n      return 0;\n    }\n}\nstatic char __Pyx_BufFmt_TypeCharToGroup(char ch, int is_complex) {\n  switch (ch) {\n    case 'c':\n        return 'H';\n    case 'b': case 'h': case 'i':\n    case 'l': case 'q': case 's': case 'p':\n        return 'I';\n    case 'B': case 'H': case 'I': case 'L': case 'Q':\n        return 'U';\n    case 'f': case 'd': case 'g':\n        return (is_complex ? 'C' : 'R');\n    case 'O':\n        return 'O';\n    case 'P':\n        return 'P';\n    default: {\n      __Pyx_BufFmt_RaiseUnexpectedChar(ch);\n      return 0;\n    }\n  }\n}\nstatic void __Pyx_BufFmt_RaiseExpected(__Pyx_BufFmt_Context* ctx) {\n  if (ctx->head == NULL || ctx->head->field == &ctx->root) {\n    const char* expected;\n    const char* quote;\n    if (ctx->head == NULL) {\n      expected = \"end\";\n      quote = \"\";\n    } else {\n      expected = ctx->head->field->type->name;\n      quote = \"'\";\n    }\n    PyErr_Format(PyExc_ValueError,\n                 \"Buffer dtype mismatch, expected %s%s%s but got %s\",\n                 quote, expected, quote,\n                 __Pyx_BufFmt_DescribeTypeChar(ctx->enc_type, ctx->is_complex));\n  } else {\n    __Pyx_StructField* field = ctx->head->field;\n    __Pyx_StructField* parent = (ctx->head - 1)->field;\n    PyErr_Format(PyExc_ValueError,\n                 \"Buffer dtype mismatch, expected '%s' but got %s in '%s.%s'\",\n                 field->type->name, __Pyx_BufFmt_DescribeTypeChar(ctx->enc_type, ctx->is_complex),\n                 parent->type->name, field->name);\n  }\n}\nstatic int __Pyx_BufFmt_ProcessTypeChunk(__Pyx_BufFmt_Context* ctx) {\n  char group;\n  size_t size, offset, arraysize = 1;\n  if (ctx->enc_type == 0) return 0;\n  if (ctx->head->field->type->arraysize[0]) {\n    int i, ndim = 0;\n    if (ctx->enc_type == 's' || ctx->enc_type == 'p') {\n        ctx->is_valid_array = ctx->head->field->type->ndim == 1;\n        ndim = 1;\n        if (ctx->enc_count != ctx->head->field->type->arraysize[0]) {\n            PyErr_Format(PyExc_ValueError,\n                         \"Expected a dimension of size %zu, got %zu\",\n                         ctx->head->field->type->arraysize[0], ctx->enc_count);\n            return -1;\n        }\n    }\n    if (!ctx->is_valid_array) {\n      PyErr_Format(PyExc_ValueError, \"Expected %d dimensions, got %d\",\n                   ctx->head->field->type->ndim, ndim);\n      return -1;\n    }\n    for (i = 0; i < ctx->head->field->type->ndim; i++) {\n      arraysize *= ctx->head->field->type->arraysize[i];\n    }\n    ctx->is_valid_array = 0;\n    ctx->enc_count = 1;\n  }\n  group = __Pyx_BufFmt_TypeCharToGroup(ctx->enc_type, ctx->is_complex);\n  do {\n    __Pyx_StructField* field = ctx->head->field;\n    __Pyx_TypeInfo* type = field->type;\n    if (ctx->enc_packmode == '@' || ctx->enc_packmode == '^') {\n      size = __Pyx_BufFmt_TypeCharToNativeSize(ctx->enc_type, ctx->is_complex);\n    } else {\n      size = __Pyx_BufFmt_TypeCharToStandardSize(ctx->enc_type, ctx->is_complex);\n    }\n    if (ctx->enc_packmode == '@') {\n      size_t align_at = __Pyx_BufFmt_TypeCharToAlignment(ctx->enc_type, ctx->is_complex);\n      size_t align_mod_offset;\n      if (align_at == 0) return -1;\n      align_mod_offset = ctx->fmt_offset % align_at;\n      if (align_mod_offset > 0) ctx->fmt_offset += align_at - align_mod_offset;\n      if (ctx->struct_alignment == 0)\n          ctx->struct_alignment = __Pyx_BufFmt_TypeCharToPadding(ctx->enc_type,\n                                                                 ctx->is_complex);\n    }\n    if (type->size != size || type->typegroup != group) {\n      if (type->typegroup == 'C' && type->fields != NULL) {\n        size_t parent_offset = ctx->head->parent_offset + field->offset;\n        ++ctx->head;\n        ctx->head->field = type->fields;\n        ctx->head->parent_offset = parent_offset;\n        continue;\n      }\n      if ((type->typegroup == 'H' || group == 'H') && type->size == size) {\n      } else {\n          __Pyx_BufFmt_RaiseExpected(ctx);\n          return -1;\n      }\n    }\n    offset = ctx->head->parent_offset + field->offset;\n    if (ctx->fmt_offset != offset) {\n      PyErr_Format(PyExc_ValueError,\n                   \"Buffer dtype mismatch; next field is at offset %\" CYTHON_FORMAT_SSIZE_T \"d but %\" CYTHON_FORMAT_SSIZE_T \"d expected\",\n                   (Py_ssize_t)ctx->fmt_offset, (Py_ssize_t)offset);\n      return -1;\n    }\n    ctx->fmt_offset += size;\n    if (arraysize)\n      ctx->fmt_offset += (arraysize - 1) * size;\n    --ctx->enc_count; /* Consume from buffer string */\n    while (1) {\n      if (field == &ctx->root) {\n        ctx->head = NULL;\n        if (ctx->enc_count != 0) {\n          __Pyx_BufFmt_RaiseExpected(ctx);\n          return -1;\n        }\n        break; /* breaks both loops as ctx->enc_count == 0 */\n      }\n      ctx->head->field = ++field;\n      if (field->type == NULL) {\n        --ctx->head;\n        field = ctx->head->field;\n        continue;\n      } else if (field->type->typegroup == 'S') {\n        size_t parent_offset = ctx->head->parent_offset + field->offset;\n        if (field->type->fields->type == NULL) continue; /* empty struct */\n        field = field->type->fields;\n        ++ctx->head;\n        ctx->head->field = field;\n        ctx->head->parent_offset = parent_offset;\n        break;\n      } else {\n        break;\n      }\n    }\n  } while (ctx->enc_count);\n  ctx->enc_type = 0;\n  ctx->is_complex = 0;\n  return 0;\n}\nstatic CYTHON_INLINE PyObject *\n__pyx_buffmt_parse_array(__Pyx_BufFmt_Context* ctx, const char** tsp)\n{\n    const char *ts = *tsp;\n    int i = 0, number;\n    int ndim = ctx->head->field->type->ndim;\n;\n    ++ts;\n    if (ctx->new_count != 1) {\n        PyErr_SetString(PyExc_ValueError,\n                        \"Cannot handle repeated arrays in format string\");\n        return NULL;\n    }\n    if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n    while (*ts && *ts != ')') {\n        switch (*ts) {\n            case ' ': case '\\f': case '\\r': case '\\n': case '\\t': case '\\v':  continue;\n            default:  break;  /* not a 'break' in the loop */\n        }\n        number = __Pyx_BufFmt_ExpectNumber(&ts);\n        if (number == -1) return NULL;\n        if (i < ndim && (size_t) number != ctx->head->field->type->arraysize[i])\n            return PyErr_Format(PyExc_ValueError,\n                        \"Expected a dimension of size %zu, got %d\",\n                        ctx->head->field->type->arraysize[i], number);\n        if (*ts != ',' && *ts != ')')\n            return PyErr_Format(PyExc_ValueError,\n                                \"Expected a comma in format string, got '%c'\", *ts);\n        if (*ts == ',') ts++;\n        i++;\n    }\n    if (i != ndim)\n        return PyErr_Format(PyExc_ValueError, \"Expected %d dimension(s), got %d\",\n                            ctx->head->field->type->ndim, i);\n    if (!*ts) {\n        PyErr_SetString(PyExc_ValueError,\n                        \"Unexpected end of format string, expected ')'\");\n        return NULL;\n    }\n    ctx->is_valid_array = 1;\n    ctx->new_count = 1;\n    *tsp = ++ts;\n    return Py_None;\n}\nstatic const char* __Pyx_BufFmt_CheckString(__Pyx_BufFmt_Context* ctx, const char* ts) {\n  int got_Z = 0;\n  while (1) {\n    switch(*ts) {\n      case 0:\n        if (ctx->enc_type != 0 && ctx->head == NULL) {\n          __Pyx_BufFmt_RaiseExpected(ctx);\n          return NULL;\n        }\n        if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n        if (ctx->head != NULL) {\n          __Pyx_BufFmt_RaiseExpected(ctx);\n          return NULL;\n        }\n                return ts;\n      case ' ':\n      case 10:\n      case 13:\n        ++ts;\n        break;\n      case '<':\n        if (!__Pyx_IsLittleEndian()) {\n          PyErr_SetString(PyExc_ValueError, \"Little-endian buffer not supported on big-endian compiler\");\n          return NULL;\n        }\n        ctx->new_packmode = '=';\n        ++ts;\n        break;\n      case '>':\n      case '!':\n        if (__Pyx_IsLittleEndian()) {\n          PyErr_SetString(PyExc_ValueError, \"Big-endian buffer not supported on little-endian compiler\");\n          return NULL;\n        }\n        ctx->new_packmode = '=';\n        ++ts;\n        break;\n      case '=':\n      case '@':\n      case '^':\n        ctx->new_packmode = *ts++;\n        break;\n      case 'T': /* substruct */\n        {\n          const char* ts_after_sub;\n          size_t i, struct_count = ctx->new_count;\n          size_t struct_alignment = ctx->struct_alignment;\n          ctx->new_count = 1;\n          ++ts;\n          if (*ts != '{') {\n            PyErr_SetString(PyExc_ValueError, \"Buffer acquisition: Expected '{' after 'T'\");\n            return NULL;\n          }\n          if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n          ctx->enc_type = 0; /* Erase processed last struct element */\n          ctx->enc_count = 0;\n          ctx->struct_alignment = 0;\n          ++ts;\n          ts_after_sub = ts;\n          for (i = 0; i != struct_count; ++i) {\n            ts_after_sub = __Pyx_BufFmt_CheckString(ctx, ts);\n            if (!ts_after_sub) return NULL;\n          }\n          ts = ts_after_sub;\n          if (struct_alignment) ctx->struct_alignment = struct_alignment;\n        }\n        break;\n      case '}': /* end of substruct; either repeat or move on */\n        {\n          size_t alignment = ctx->struct_alignment;\n          ++ts;\n          if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n          ctx->enc_type = 0; /* Erase processed last struct element */\n          if (alignment && ctx->fmt_offset % alignment) {\n            ctx->fmt_offset += alignment - (ctx->fmt_offset % alignment);\n          }\n        }\n        return ts;\n      case 'x':\n        if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n        ctx->fmt_offset += ctx->new_count;\n        ctx->new_count = 1;\n        ctx->enc_count = 0;\n        ctx->enc_type = 0;\n        ctx->enc_packmode = ctx->new_packmode;\n        ++ts;\n        break;\n      case 'Z':\n        got_Z = 1;\n        ++ts;\n        if (*ts != 'f' && *ts != 'd' && *ts != 'g') {\n          __Pyx_BufFmt_RaiseUnexpectedChar('Z');\n          return NULL;\n        }        /* fall through */\n      case 'c': case 'b': case 'B': case 'h': case 'H': case 'i': case 'I':\n      case 'l': case 'L': case 'q': case 'Q':\n      case 'f': case 'd': case 'g':\n      case 'O': case 's': case 'p':\n        if (ctx->enc_type == *ts && got_Z == ctx->is_complex &&\n            ctx->enc_packmode == ctx->new_packmode) {\n          ctx->enc_count += ctx->new_count;\n        } else {\n          if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n          ctx->enc_count = ctx->new_count;\n          ctx->enc_packmode = ctx->new_packmode;\n          ctx->enc_type = *ts;\n          ctx->is_complex = got_Z;\n        }\n        ++ts;\n        ctx->new_count = 1;\n        got_Z = 0;\n        break;\n      case ':':\n        ++ts;\n        while(*ts != ':') ++ts;\n        ++ts;\n        break;\n      case '(':\n        if (!__pyx_buffmt_parse_array(ctx, &ts)) return NULL;\n        break;\n      default:\n        {\n          int number = __Pyx_BufFmt_ExpectNumber(&ts);\n          if (number == -1) return NULL;\n          ctx->new_count = (size_t)number;\n        }\n    }\n  }\n}\nstatic CYTHON_INLINE void __Pyx_ZeroBuffer(Py_buffer* buf) {\n  buf->buf = NULL;\n  buf->obj = NULL;\n  buf->strides = __Pyx_zeros;\n  buf->shape = __Pyx_zeros;\n  buf->suboffsets = __Pyx_minusones;\n}\nstatic CYTHON_INLINE int __Pyx_GetBufferAndValidate(\n        Py_buffer* buf, PyObject* obj,  __Pyx_TypeInfo* dtype, int flags,\n        int nd, int cast, __Pyx_BufFmt_StackElem* stack)\n{\n  if (obj == Py_None || obj == NULL) {\n    __Pyx_ZeroBuffer(buf);\n    return 0;\n  }\n  buf->buf = NULL;\n  if (__Pyx_GetBuffer(obj, buf, flags) == -1) goto fail;\n  if (buf->ndim != nd) {\n    PyErr_Format(PyExc_ValueError,\n                 \"Buffer has wrong number of dimensions (expected %d, got %d)\",\n                 nd, buf->ndim);\n    goto fail;\n  }\n  if (!cast) {\n    __Pyx_BufFmt_Context ctx;\n    __Pyx_BufFmt_Init(&ctx, stack, dtype);\n    if (!__Pyx_BufFmt_CheckString(&ctx, buf->format)) goto fail;\n  }\n  if ((unsigned)buf->itemsize != dtype->size) {\n    PyErr_Format(PyExc_ValueError,\n      \"Item size of buffer (%\" CYTHON_FORMAT_SSIZE_T \"d byte%s) does not match size of '%s' (%\" CYTHON_FORMAT_SSIZE_T \"d byte%s)\",\n      buf->itemsize, (buf->itemsize > 1) ? \"s\" : \"\",\n      dtype->name, (Py_ssize_t)dtype->size, (dtype->size > 1) ? \"s\" : \"\");\n    goto fail;\n  }\n  if (buf->suboffsets == NULL) buf->suboffsets = __Pyx_minusones;\n  return 0;\nfail:;\n  __Pyx_ZeroBuffer(buf);\n  return -1;\n}\nstatic CYTHON_INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info) {\n  if (info->buf == NULL) return;\n  if (info->suboffsets == __Pyx_minusones) info->suboffsets = NULL;\n  __Pyx_ReleaseBuffer(info);\n}\n\nstatic CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {\n    if (unlikely(!type)) {\n        PyErr_SetString(PyExc_SystemError, \"Missing type object\");\n        return 0;\n    }\n    if (likely(PyObject_TypeCheck(obj, type)))\n        return 1;\n    PyErr_Format(PyExc_TypeError, \"Cannot convert %.200s to %.200s\",\n                 Py_TYPE(obj)->tp_name, type->tp_name);\n    return 0;\n}\n\n#if CYTHON_COMPILING_IN_CPYTHON\nstatic CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) {\n    PyObject *result;\n    ternaryfunc call = func->ob_type->tp_call;\n    if (unlikely(!call))\n        return PyObject_Call(func, arg, kw);\n#if PY_VERSION_HEX >= 0x02060000\n    if (unlikely(Py_EnterRecursiveCall((char*)\" while calling a Python object\")))\n        return NULL;\n#endif\n    result = (*call)(func, arg, kw);\n#if PY_VERSION_HEX >= 0x02060000\n    Py_LeaveRecursiveCall();\n#endif\n    if (unlikely(!result) && unlikely(!PyErr_Occurred())) {\n        PyErr_SetString(\n            PyExc_SystemError,\n            \"NULL result without error in PyObject_Call\");\n    }\n    return result;\n}\n#endif\n\nstatic CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) {\n    PyObject *result;\n#if CYTHON_COMPILING_IN_CPYTHON\n    result = PyDict_GetItem(__pyx_d, name);\n    if (result) {\n        Py_INCREF(result);\n    } else {\n#else\n    result = PyObject_GetItem(__pyx_d, name);\n    if (!result) {\n        PyErr_Clear();\n#endif\n        result = __Pyx_GetBuiltinName(name);\n    }\n    return result;\n}\n\nstatic void __Pyx_RaiseBufferIndexError(int axis) {\n  PyErr_Format(PyExc_IndexError,\n     \"Out of bounds on buffer access (axis %d)\", axis);\n}\n\nstatic CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb) {\n#if CYTHON_COMPILING_IN_CPYTHON\n    PyObject *tmp_type, *tmp_value, *tmp_tb;\n    PyThreadState *tstate = PyThreadState_GET();\n    tmp_type = tstate->curexc_type;\n    tmp_value = tstate->curexc_value;\n    tmp_tb = tstate->curexc_traceback;\n    tstate->curexc_type = type;\n    tstate->curexc_value = value;\n    tstate->curexc_traceback = tb;\n    Py_XDECREF(tmp_type);\n    Py_XDECREF(tmp_value);\n    Py_XDECREF(tmp_tb);\n#else\n    PyErr_Restore(type, value, tb);\n#endif\n}\nstatic CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb) {\n#if CYTHON_COMPILING_IN_CPYTHON\n    PyThreadState *tstate = PyThreadState_GET();\n    *type = tstate->curexc_type;\n    *value = tstate->curexc_value;\n    *tb = tstate->curexc_traceback;\n    tstate->curexc_type = 0;\n    tstate->curexc_value = 0;\n    tstate->curexc_traceback = 0;\n#else\n    PyErr_Fetch(type, value, tb);\n#endif\n}\n\n#if PY_MAJOR_VERSION < 3\nstatic void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb,\n                        CYTHON_UNUSED PyObject *cause) {\n    Py_XINCREF(type);\n    if (!value || value == Py_None)\n        value = NULL;\n    else\n        Py_INCREF(value);\n    if (!tb || tb == Py_None)\n        tb = NULL;\n    else {\n        Py_INCREF(tb);\n        if (!PyTraceBack_Check(tb)) {\n            PyErr_SetString(PyExc_TypeError,\n                \"raise: arg 3 must be a traceback or None\");\n            goto raise_error;\n        }\n    }\n    #if PY_VERSION_HEX < 0x02050000\n    if (PyClass_Check(type)) {\n    #else\n    if (PyType_Check(type)) {\n    #endif\n#if CYTHON_COMPILING_IN_PYPY\n        if (!value) {\n            Py_INCREF(Py_None);\n            value = Py_None;\n        }\n#endif\n        PyErr_NormalizeException(&type, &value, &tb);\n    } else {\n        if (value) {\n            PyErr_SetString(PyExc_TypeError,\n                \"instance exception may not have a separate value\");\n            goto raise_error;\n        }\n        value = type;\n        #if PY_VERSION_HEX < 0x02050000\n        if (PyInstance_Check(type)) {\n            type = (PyObject*) ((PyInstanceObject*)type)->in_class;\n            Py_INCREF(type);\n        } else {\n            type = 0;\n            PyErr_SetString(PyExc_TypeError,\n                \"raise: exception must be an old-style class or instance\");\n            goto raise_error;\n        }\n        #else\n        type = (PyObject*) Py_TYPE(type);\n        Py_INCREF(type);\n        if (!PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject *)PyExc_BaseException)) {\n            PyErr_SetString(PyExc_TypeError,\n                \"raise: exception class must be a subclass of BaseException\");\n            goto raise_error;\n        }\n        #endif\n    }\n    __Pyx_ErrRestore(type, value, tb);\n    return;\nraise_error:\n    Py_XDECREF(value);\n    Py_XDECREF(type);\n    Py_XDECREF(tb);\n    return;\n}\n#else /* Python 3+ */\nstatic void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause) {\n    PyObject* owned_instance = NULL;\n    if (tb == Py_None) {\n        tb = 0;\n    } else if (tb && !PyTraceBack_Check(tb)) {\n        PyErr_SetString(PyExc_TypeError,\n            \"raise: arg 3 must be a traceback or None\");\n        goto bad;\n    }\n    if (value == Py_None)\n        value = 0;\n    if (PyExceptionInstance_Check(type)) {\n        if (value) {\n            PyErr_SetString(PyExc_TypeError,\n                \"instance exception may not have a separate value\");\n            goto bad;\n        }\n        value = type;\n        type = (PyObject*) Py_TYPE(value);\n    } else if (PyExceptionClass_Check(type)) {\n        PyObject *instance_class = NULL;\n        if (value && PyExceptionInstance_Check(value)) {\n            instance_class = (PyObject*) Py_TYPE(value);\n            if (instance_class != type) {\n                if (PyObject_IsSubclass(instance_class, type)) {\n                    type = instance_class;\n                } else {\n                    instance_class = NULL;\n                }\n            }\n        }\n        if (!instance_class) {\n            PyObject *args;\n            if (!value)\n                args = PyTuple_New(0);\n            else if (PyTuple_Check(value)) {\n                Py_INCREF(value);\n                args = value;\n            } else\n                args = PyTuple_Pack(1, value);\n            if (!args)\n                goto bad;\n            owned_instance = PyObject_Call(type, args, NULL);\n            Py_DECREF(args);\n            if (!owned_instance)\n                goto bad;\n            value = owned_instance;\n            if (!PyExceptionInstance_Check(value)) {\n                PyErr_Format(PyExc_TypeError,\n                             \"calling %R should have returned an instance of \"\n                             \"BaseException, not %R\",\n                             type, Py_TYPE(value));\n                goto bad;\n            }\n        }\n    } else {\n        PyErr_SetString(PyExc_TypeError,\n            \"raise: exception class must be a subclass of BaseException\");\n        goto bad;\n    }\n#if PY_VERSION_HEX >= 0x03030000\n    if (cause) {\n#else\n    if (cause && cause != Py_None) {\n#endif\n        PyObject *fixed_cause;\n        if (cause == Py_None) {\n            fixed_cause = NULL;\n        } else if (PyExceptionClass_Check(cause)) {\n            fixed_cause = PyObject_CallObject(cause, NULL);\n            if (fixed_cause == NULL)\n                goto bad;\n        } else if (PyExceptionInstance_Check(cause)) {\n            fixed_cause = cause;\n            Py_INCREF(fixed_cause);\n        } else {\n            PyErr_SetString(PyExc_TypeError,\n                            \"exception causes must derive from \"\n                            \"BaseException\");\n            goto bad;\n        }\n        PyException_SetCause(value, fixed_cause);\n    }\n    PyErr_SetObject(type, value);\n    if (tb) {\n        PyThreadState *tstate = PyThreadState_GET();\n        PyObject* tmp_tb = tstate->curexc_traceback;\n        if (tb != tmp_tb) {\n            Py_INCREF(tb);\n            tstate->curexc_traceback = tb;\n            Py_XDECREF(tmp_tb);\n        }\n    }\nbad:\n    Py_XDECREF(owned_instance);\n    return;\n}\n#endif\n\nstatic CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {\n    PyErr_Format(PyExc_ValueError,\n                 \"too many values to unpack (expected %\" CYTHON_FORMAT_SSIZE_T \"d)\", expected);\n}\n\nstatic CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {\n    PyErr_Format(PyExc_ValueError,\n                 \"need more than %\" CYTHON_FORMAT_SSIZE_T \"d value%.1s to unpack\",\n                 index, (index == 1) ? \"\" : \"s\");\n}\n\nstatic CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void) {\n    PyErr_SetString(PyExc_TypeError, \"'NoneType' object is not iterable\");\n}\n\n#if PY_MAJOR_VERSION < 3\nstatic int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags) {\n  #if PY_VERSION_HEX >= 0x02060000\n    if (PyObject_CheckBuffer(obj)) return PyObject_GetBuffer(obj, view, flags);\n  #endif\n        if (PyObject_TypeCheck(obj, __pyx_ptype_5numpy_ndarray)) return __pyx_pw_5numpy_7ndarray_1__getbuffer__(obj, view, flags);\n  #if PY_VERSION_HEX < 0x02060000\n    if (obj->ob_type->tp_dict) {\n        PyObject *getbuffer_cobj = PyObject_GetItem(\n            obj->ob_type->tp_dict, __pyx_n_s_pyx_getbuffer);\n        if (getbuffer_cobj) {\n            getbufferproc func = (getbufferproc) PyCObject_AsVoidPtr(getbuffer_cobj);\n            Py_DECREF(getbuffer_cobj);\n            if (!func)\n                goto fail;\n            return func(obj, view, flags);\n        } else {\n            PyErr_Clear();\n        }\n    }\n  #endif\n    PyErr_Format(PyExc_TypeError, \"'%.200s' does not have the buffer interface\", Py_TYPE(obj)->tp_name);\n#if PY_VERSION_HEX < 0x02060000\nfail:\n#endif\n    return -1;\n}\nstatic void __Pyx_ReleaseBuffer(Py_buffer *view) {\n    PyObject *obj = view->obj;\n    if (!obj) return;\n  #if PY_VERSION_HEX >= 0x02060000\n    if (PyObject_CheckBuffer(obj)) {\n        PyBuffer_Release(view);\n        return;\n    }\n  #endif\n        if (PyObject_TypeCheck(obj, __pyx_ptype_5numpy_ndarray)) { __pyx_pw_5numpy_7ndarray_3__releasebuffer__(obj, view); return; }\n  #if PY_VERSION_HEX < 0x02060000\n    if (obj->ob_type->tp_dict) {\n        PyObject *releasebuffer_cobj = PyObject_GetItem(\n            obj->ob_type->tp_dict, __pyx_n_s_pyx_releasebuffer);\n        if (releasebuffer_cobj) {\n            releasebufferproc func = (releasebufferproc) PyCObject_AsVoidPtr(releasebuffer_cobj);\n            Py_DECREF(releasebuffer_cobj);\n            if (!func)\n                goto fail;\n            func(obj, view);\n            return;\n        } else {\n            PyErr_Clear();\n        }\n    }\n  #endif\n    goto nofail;\n#if PY_VERSION_HEX < 0x02060000\nfail:\n#endif\n    PyErr_WriteUnraisable(obj);\nnofail:\n    Py_DECREF(obj);\n    view->obj = NULL;\n}\n#endif /*  PY_MAJOR_VERSION < 3 */\n\n\n        static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) {\n    PyObject *empty_list = 0;\n    PyObject *module = 0;\n    PyObject *global_dict = 0;\n    PyObject *empty_dict = 0;\n    PyObject *list;\n    #if PY_VERSION_HEX < 0x03030000\n    PyObject *py_import;\n    py_import = __Pyx_PyObject_GetAttrStr(__pyx_b, __pyx_n_s_import);\n    if (!py_import)\n        goto bad;\n    #endif\n    if (from_list)\n        list = from_list;\n    else {\n        empty_list = PyList_New(0);\n        if (!empty_list)\n            goto bad;\n        list = empty_list;\n    }\n    global_dict = PyModule_GetDict(__pyx_m);\n    if (!global_dict)\n        goto bad;\n    empty_dict = PyDict_New();\n    if (!empty_dict)\n        goto bad;\n    #if PY_VERSION_HEX >= 0x02050000\n    {\n        #if PY_MAJOR_VERSION >= 3\n        if (level == -1) {\n            if (strchr(__Pyx_MODULE_NAME, '.')) {\n                #if PY_VERSION_HEX < 0x03030000\n                PyObject *py_level = PyInt_FromLong(1);\n                if (!py_level)\n                    goto bad;\n                module = PyObject_CallFunctionObjArgs(py_import,\n                    name, global_dict, empty_dict, list, py_level, NULL);\n                Py_DECREF(py_level);\n                #else\n                module = PyImport_ImportModuleLevelObject(\n                    name, global_dict, empty_dict, list, 1);\n                #endif\n                if (!module) {\n                    if (!PyErr_ExceptionMatches(PyExc_ImportError))\n                        goto bad;\n                    PyErr_Clear();\n                }\n            }\n            level = 0; /* try absolute import on failure */\n        }\n        #endif\n        if (!module) {\n            #if PY_VERSION_HEX < 0x03030000\n            PyObject *py_level = PyInt_FromLong(level);\n            if (!py_level)\n                goto bad;\n            module = PyObject_CallFunctionObjArgs(py_import,\n                name, global_dict, empty_dict, list, py_level, NULL);\n            Py_DECREF(py_level);\n            #else\n            module = PyImport_ImportModuleLevelObject(\n                name, global_dict, empty_dict, list, level);\n            #endif\n        }\n    }\n    #else\n    if (level>0) {\n        PyErr_SetString(PyExc_RuntimeError, \"Relative import is not supported for Python <=2.4.\");\n        goto bad;\n    }\n    module = PyObject_CallFunctionObjArgs(py_import,\n        name, global_dict, empty_dict, list, NULL);\n    #endif\nbad:\n    #if PY_VERSION_HEX < 0x03030000\n    Py_XDECREF(py_import);\n    #endif\n    Py_XDECREF(empty_list);\n    Py_XDECREF(empty_dict);\n    return module;\n}\n\nstatic CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value) {\n    const int neg_one = (int) -1, const_zero = 0;\n    const int is_unsigned = neg_one > const_zero;\n    if (is_unsigned) {\n        if (sizeof(int) < sizeof(long)) {\n            return PyInt_FromLong((long) value);\n        } else if (sizeof(int) <= sizeof(unsigned long)) {\n            return PyLong_FromUnsignedLong((unsigned long) value);\n        } else if (sizeof(int) <= sizeof(unsigned long long)) {\n            return PyLong_FromUnsignedLongLong((unsigned long long) value);\n        }\n    } else {\n        if (sizeof(int) <= sizeof(long)) {\n            return PyInt_FromLong((long) value);\n        } else if (sizeof(int) <= sizeof(long long)) {\n            return PyLong_FromLongLong((long long) value);\n        }\n    }\n    {\n        int one = 1; int little = (int)*(unsigned char *)&one;\n        unsigned char *bytes = (unsigned char *)&value;\n        return _PyLong_FromByteArray(bytes, sizeof(int),\n                                     little, !is_unsigned);\n    }\n}\n\n#define __PYX_VERIFY_RETURN_INT(target_type, func_type, func)             \\\n    {                                                                     \\\n        func_type value = func(x);                                        \\\n        if (sizeof(target_type) < sizeof(func_type)) {                    \\\n            if (unlikely(value != (func_type) (target_type) value)) {     \\\n                func_type zero = 0;                                       \\\n                PyErr_SetString(PyExc_OverflowError,                      \\\n                    (is_unsigned && unlikely(value < zero)) ?             \\\n                    \"can't convert negative value to \" #target_type :     \\\n                    \"value too large to convert to \" #target_type);       \\\n                return (target_type) -1;                                  \\\n            }                                                             \\\n        }                                                                 \\\n        return (target_type) value;                                       \\\n    }\n\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n  #include \"longintrepr.h\"\n #endif\n#endif\nstatic CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) {\n    const int neg_one = (int) -1, const_zero = 0;\n    const int is_unsigned = neg_one > const_zero;\n#if PY_MAJOR_VERSION < 3\n    if (likely(PyInt_Check(x))) {\n        if (sizeof(int) < sizeof(long)) {\n            __PYX_VERIFY_RETURN_INT(int, long, PyInt_AS_LONG)\n        } else {\n            long val = PyInt_AS_LONG(x);\n            if (is_unsigned && unlikely(val < 0)) {\n                PyErr_SetString(PyExc_OverflowError,\n                                \"can't convert negative value to int\");\n                return (int) -1;\n            }\n            return (int) val;\n        }\n    } else\n#endif\n    if (likely(PyLong_Check(x))) {\n        if (is_unsigned) {\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n            if (sizeof(digit) <= sizeof(int)) {\n                switch (Py_SIZE(x)) {\n                    case  0: return 0;\n                    case  1: return (int) ((PyLongObject*)x)->ob_digit[0];\n                }\n            }\n #endif\n#endif\n            if (unlikely(Py_SIZE(x) < 0)) {\n                PyErr_SetString(PyExc_OverflowError,\n                                \"can't convert negative value to int\");\n                return (int) -1;\n            }\n            if (sizeof(int) <= sizeof(unsigned long)) {\n                __PYX_VERIFY_RETURN_INT(int, unsigned long, PyLong_AsUnsignedLong)\n            } else if (sizeof(int) <= sizeof(unsigned long long)) {\n                __PYX_VERIFY_RETURN_INT(int, unsigned long long, PyLong_AsUnsignedLongLong)\n            }\n        } else {\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n            if (sizeof(digit) <= sizeof(int)) {\n                switch (Py_SIZE(x)) {\n                    case  0: return 0;\n                    case  1: return +(int) ((PyLongObject*)x)->ob_digit[0];\n                    case -1: return -(int) ((PyLongObject*)x)->ob_digit[0];\n                }\n            }\n #endif\n#endif\n            if (sizeof(int) <= sizeof(long)) {\n                __PYX_VERIFY_RETURN_INT(int, long, PyLong_AsLong)\n            } else if (sizeof(int) <= sizeof(long long)) {\n                __PYX_VERIFY_RETURN_INT(int, long long, PyLong_AsLongLong)\n            }\n        }\n        {\n#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)\n            PyErr_SetString(PyExc_RuntimeError,\n                            \"_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers\");\n#else\n            int val;\n            PyObject *v = __Pyx_PyNumber_Int(x);\n #if PY_MAJOR_VERSION < 3\n            if (likely(v) && !PyLong_Check(v)) {\n                PyObject *tmp = v;\n                v = PyNumber_Long(tmp);\n                Py_DECREF(tmp);\n            }\n #endif\n            if (likely(v)) {\n                int one = 1; int is_little = (int)*(unsigned char *)&one;\n                unsigned char *bytes = (unsigned char *)&val;\n                int ret = _PyLong_AsByteArray((PyLongObject *)v,\n                                              bytes, sizeof(val),\n                                              is_little, !is_unsigned);\n                Py_DECREF(v);\n                if (likely(!ret))\n                    return val;\n            }\n#endif\n            return (int) -1;\n        }\n    } else {\n        int val;\n        PyObject *tmp = __Pyx_PyNumber_Int(x);\n        if (!tmp) return (int) -1;\n        val = __Pyx_PyInt_As_int(tmp);\n        Py_DECREF(tmp);\n        return val;\n    }\n}\n\nstatic CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) {\n    const long neg_one = (long) -1, const_zero = 0;\n    const int is_unsigned = neg_one > const_zero;\n    if (is_unsigned) {\n        if (sizeof(long) < sizeof(long)) {\n            return PyInt_FromLong((long) value);\n        } else if (sizeof(long) <= sizeof(unsigned long)) {\n            return PyLong_FromUnsignedLong((unsigned long) value);\n        } else if (sizeof(long) <= sizeof(unsigned long long)) {\n            return PyLong_FromUnsignedLongLong((unsigned long long) value);\n        }\n    } else {\n        if (sizeof(long) <= sizeof(long)) {\n            return PyInt_FromLong((long) value);\n        } else if (sizeof(long) <= sizeof(long long)) {\n            return PyLong_FromLongLong((long long) value);\n        }\n    }\n    {\n        int one = 1; int little = (int)*(unsigned char *)&one;\n        unsigned char *bytes = (unsigned char *)&value;\n        return _PyLong_FromByteArray(bytes, sizeof(long),\n                                     little, !is_unsigned);\n    }\n}\n\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) {\n      return ::std::complex< float >(x, y);\n    }\n  #else\n    static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) {\n      return x + y*(__pyx_t_float_complex)_Complex_I;\n    }\n  #endif\n#else\n    static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) {\n      __pyx_t_float_complex z;\n      z.real = x;\n      z.imag = y;\n      return z;\n    }\n#endif\n\n#if CYTHON_CCOMPLEX\n#else\n    static CYTHON_INLINE int __Pyx_c_eqf(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n       return (a.real == b.real) && (a.imag == b.imag);\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_sumf(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n        __pyx_t_float_complex z;\n        z.real = a.real + b.real;\n        z.imag = a.imag + b.imag;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_difff(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n        __pyx_t_float_complex z;\n        z.real = a.real - b.real;\n        z.imag = a.imag - b.imag;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_prodf(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n        __pyx_t_float_complex z;\n        z.real = a.real * b.real - a.imag * b.imag;\n        z.imag = a.real * b.imag + a.imag * b.real;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_quotf(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n        __pyx_t_float_complex z;\n        float denom = b.real * b.real + b.imag * b.imag;\n        z.real = (a.real * b.real + a.imag * b.imag) / denom;\n        z.imag = (a.imag * b.real - a.real * b.imag) / denom;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_negf(__pyx_t_float_complex a) {\n        __pyx_t_float_complex z;\n        z.real = -a.real;\n        z.imag = -a.imag;\n        return z;\n    }\n    static CYTHON_INLINE int __Pyx_c_is_zerof(__pyx_t_float_complex a) {\n       return (a.real == 0) && (a.imag == 0);\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_conjf(__pyx_t_float_complex a) {\n        __pyx_t_float_complex z;\n        z.real =  a.real;\n        z.imag = -a.imag;\n        return z;\n    }\n    #if 1\n        static CYTHON_INLINE float __Pyx_c_absf(__pyx_t_float_complex z) {\n          #if !defined(HAVE_HYPOT) || defined(_MSC_VER)\n            return sqrtf(z.real*z.real + z.imag*z.imag);\n          #else\n            return hypotf(z.real, z.imag);\n          #endif\n        }\n        static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_powf(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n            __pyx_t_float_complex z;\n            float r, lnr, theta, z_r, z_theta;\n            if (b.imag == 0 && b.real == (int)b.real) {\n                if (b.real < 0) {\n                    float denom = a.real * a.real + a.imag * a.imag;\n                    a.real = a.real / denom;\n                    a.imag = -a.imag / denom;\n                    b.real = -b.real;\n                }\n                switch ((int)b.real) {\n                    case 0:\n                        z.real = 1;\n                        z.imag = 0;\n                        return z;\n                    case 1:\n                        return a;\n                    case 2:\n                        z = __Pyx_c_prodf(a, a);\n                        return __Pyx_c_prodf(a, a);\n                    case 3:\n                        z = __Pyx_c_prodf(a, a);\n                        return __Pyx_c_prodf(z, a);\n                    case 4:\n                        z = __Pyx_c_prodf(a, a);\n                        return __Pyx_c_prodf(z, z);\n                }\n            }\n            if (a.imag == 0) {\n                if (a.real == 0) {\n                    return a;\n                }\n                r = a.real;\n                theta = 0;\n            } else {\n                r = __Pyx_c_absf(a);\n                theta = atan2f(a.imag, a.real);\n            }\n            lnr = logf(r);\n            z_r = expf(lnr * b.real - theta * b.imag);\n            z_theta = theta * b.real + lnr * b.imag;\n            z.real = z_r * cosf(z_theta);\n            z.imag = z_r * sinf(z_theta);\n            return z;\n        }\n    #endif\n#endif\n\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) {\n      return ::std::complex< double >(x, y);\n    }\n  #else\n    static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) {\n      return x + y*(__pyx_t_double_complex)_Complex_I;\n    }\n  #endif\n#else\n    static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) {\n      __pyx_t_double_complex z;\n      z.real = x;\n      z.imag = y;\n      return z;\n    }\n#endif\n\n#if CYTHON_CCOMPLEX\n#else\n    static CYTHON_INLINE int __Pyx_c_eq(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n       return (a.real == b.real) && (a.imag == b.imag);\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_sum(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n        __pyx_t_double_complex z;\n        z.real = a.real + b.real;\n        z.imag = a.imag + b.imag;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_diff(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n        __pyx_t_double_complex z;\n        z.real = a.real - b.real;\n        z.imag = a.imag - b.imag;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_prod(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n        __pyx_t_double_complex z;\n        z.real = a.real * b.real - a.imag * b.imag;\n        z.imag = a.real * b.imag + a.imag * b.real;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_quot(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n        __pyx_t_double_complex z;\n        double denom = b.real * b.real + b.imag * b.imag;\n        z.real = (a.real * b.real + a.imag * b.imag) / denom;\n        z.imag = (a.imag * b.real - a.real * b.imag) / denom;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_neg(__pyx_t_double_complex a) {\n        __pyx_t_double_complex z;\n        z.real = -a.real;\n        z.imag = -a.imag;\n        return z;\n    }\n    static CYTHON_INLINE int __Pyx_c_is_zero(__pyx_t_double_complex a) {\n       return (a.real == 0) && (a.imag == 0);\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_conj(__pyx_t_double_complex a) {\n        __pyx_t_double_complex z;\n        z.real =  a.real;\n        z.imag = -a.imag;\n        return z;\n    }\n    #if 1\n        static CYTHON_INLINE double __Pyx_c_abs(__pyx_t_double_complex z) {\n          #if !defined(HAVE_HYPOT) || defined(_MSC_VER)\n            return sqrt(z.real*z.real + z.imag*z.imag);\n          #else\n            return hypot(z.real, z.imag);\n          #endif\n        }\n        static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_pow(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n            __pyx_t_double_complex z;\n            double r, lnr, theta, z_r, z_theta;\n            if (b.imag == 0 && b.real == (int)b.real) {\n                if (b.real < 0) {\n                    double denom = a.real * a.real + a.imag * a.imag;\n                    a.real = a.real / denom;\n                    a.imag = -a.imag / denom;\n                    b.real = -b.real;\n                }\n                switch ((int)b.real) {\n                    case 0:\n                        z.real = 1;\n                        z.imag = 0;\n                        return z;\n                    case 1:\n                        return a;\n                    case 2:\n                        z = __Pyx_c_prod(a, a);\n                        return __Pyx_c_prod(a, a);\n                    case 3:\n                        z = __Pyx_c_prod(a, a);\n                        return __Pyx_c_prod(z, a);\n                    case 4:\n                        z = __Pyx_c_prod(a, a);\n                        return __Pyx_c_prod(z, z);\n                }\n            }\n            if (a.imag == 0) {\n                if (a.real == 0) {\n                    return a;\n                }\n                r = a.real;\n                theta = 0;\n            } else {\n                r = __Pyx_c_abs(a);\n                theta = atan2(a.imag, a.real);\n            }\n            lnr = log(r);\n            z_r = exp(lnr * b.real - theta * b.imag);\n            z_theta = theta * b.real + lnr * b.imag;\n            z.real = z_r * cos(z_theta);\n            z.imag = z_r * sin(z_theta);\n            return z;\n        }\n    #endif\n#endif\n\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n  #include \"longintrepr.h\"\n #endif\n#endif\nstatic CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {\n    const long neg_one = (long) -1, const_zero = 0;\n    const int is_unsigned = neg_one > const_zero;\n#if PY_MAJOR_VERSION < 3\n    if (likely(PyInt_Check(x))) {\n        if (sizeof(long) < sizeof(long)) {\n            __PYX_VERIFY_RETURN_INT(long, long, PyInt_AS_LONG)\n        } else {\n            long val = PyInt_AS_LONG(x);\n            if (is_unsigned && unlikely(val < 0)) {\n                PyErr_SetString(PyExc_OverflowError,\n                                \"can't convert negative value to long\");\n                return (long) -1;\n            }\n            return (long) val;\n        }\n    } else\n#endif\n    if (likely(PyLong_Check(x))) {\n        if (is_unsigned) {\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n            if (sizeof(digit) <= sizeof(long)) {\n                switch (Py_SIZE(x)) {\n                    case  0: return 0;\n                    case  1: return (long) ((PyLongObject*)x)->ob_digit[0];\n                }\n            }\n #endif\n#endif\n            if (unlikely(Py_SIZE(x) < 0)) {\n                PyErr_SetString(PyExc_OverflowError,\n                                \"can't convert negative value to long\");\n                return (long) -1;\n            }\n            if (sizeof(long) <= sizeof(unsigned long)) {\n                __PYX_VERIFY_RETURN_INT(long, unsigned long, PyLong_AsUnsignedLong)\n            } else if (sizeof(long) <= sizeof(unsigned long long)) {\n                __PYX_VERIFY_RETURN_INT(long, unsigned long long, PyLong_AsUnsignedLongLong)\n            }\n        } else {\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n            if (sizeof(digit) <= sizeof(long)) {\n                switch (Py_SIZE(x)) {\n                    case  0: return 0;\n                    case  1: return +(long) ((PyLongObject*)x)->ob_digit[0];\n                    case -1: return -(long) ((PyLongObject*)x)->ob_digit[0];\n                }\n            }\n #endif\n#endif\n            if (sizeof(long) <= sizeof(long)) {\n                __PYX_VERIFY_RETURN_INT(long, long, PyLong_AsLong)\n            } else if (sizeof(long) <= sizeof(long long)) {\n                __PYX_VERIFY_RETURN_INT(long, long long, PyLong_AsLongLong)\n            }\n        }\n        {\n#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)\n            PyErr_SetString(PyExc_RuntimeError,\n                            \"_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers\");\n#else\n            long val;\n            PyObject *v = __Pyx_PyNumber_Int(x);\n #if PY_MAJOR_VERSION < 3\n            if (likely(v) && !PyLong_Check(v)) {\n                PyObject *tmp = v;\n                v = PyNumber_Long(tmp);\n                Py_DECREF(tmp);\n            }\n #endif\n            if (likely(v)) {\n                int one = 1; int is_little = (int)*(unsigned char *)&one;\n                unsigned char *bytes = (unsigned char *)&val;\n                int ret = _PyLong_AsByteArray((PyLongObject *)v,\n                                              bytes, sizeof(val),\n                                              is_little, !is_unsigned);\n                Py_DECREF(v);\n                if (likely(!ret))\n                    return val;\n            }\n#endif\n            return (long) -1;\n        }\n    } else {\n        long val;\n        PyObject *tmp = __Pyx_PyNumber_Int(x);\n        if (!tmp) return (long) -1;\n        val = __Pyx_PyInt_As_long(tmp);\n        Py_DECREF(tmp);\n        return val;\n    }\n}\n\nstatic int __Pyx_check_binary_version(void) {\n    char ctversion[4], rtversion[4];\n    PyOS_snprintf(ctversion, 4, \"%d.%d\", PY_MAJOR_VERSION, PY_MINOR_VERSION);\n    PyOS_snprintf(rtversion, 4, \"%s\", Py_GetVersion());\n    if (ctversion[0] != rtversion[0] || ctversion[2] != rtversion[2]) {\n        char message[200];\n        PyOS_snprintf(message, sizeof(message),\n                      \"compiletime version %s of module '%.100s' \"\n                      \"does not match runtime version %s\",\n                      ctversion, __Pyx_MODULE_NAME, rtversion);\n        #if PY_VERSION_HEX < 0x02050000\n        return PyErr_Warn(NULL, message);\n        #else\n        return PyErr_WarnEx(NULL, message, 1);\n        #endif\n    }\n    return 0;\n}\n\n#ifndef __PYX_HAVE_RT_ImportModule\n#define __PYX_HAVE_RT_ImportModule\nstatic PyObject *__Pyx_ImportModule(const char *name) {\n    PyObject *py_name = 0;\n    PyObject *py_module = 0;\n    py_name = __Pyx_PyIdentifier_FromString(name);\n    if (!py_name)\n        goto bad;\n    py_module = PyImport_Import(py_name);\n    Py_DECREF(py_name);\n    return py_module;\nbad:\n    Py_XDECREF(py_name);\n    return 0;\n}\n#endif\n\n#ifndef __PYX_HAVE_RT_ImportType\n#define __PYX_HAVE_RT_ImportType\nstatic PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,\n    size_t size, int strict)\n{\n    PyObject *py_module = 0;\n    PyObject *result = 0;\n    PyObject *py_name = 0;\n    char warning[200];\n    Py_ssize_t basicsize;\n#ifdef Py_LIMITED_API\n    PyObject *py_basicsize;\n#endif\n    py_module = __Pyx_ImportModule(module_name);\n    if (!py_module)\n        goto bad;\n    py_name = __Pyx_PyIdentifier_FromString(class_name);\n    if (!py_name)\n        goto bad;\n    result = PyObject_GetAttr(py_module, py_name);\n    Py_DECREF(py_name);\n    py_name = 0;\n    Py_DECREF(py_module);\n    py_module = 0;\n    if (!result)\n        goto bad;\n    if (!PyType_Check(result)) {\n        PyErr_Format(PyExc_TypeError,\n            \"%.200s.%.200s is not a type object\",\n            module_name, class_name);\n        goto bad;\n    }\n#ifndef Py_LIMITED_API\n    basicsize = ((PyTypeObject *)result)->tp_basicsize;\n#else\n    py_basicsize = PyObject_GetAttrString(result, \"__basicsize__\");\n    if (!py_basicsize)\n        goto bad;\n    basicsize = PyLong_AsSsize_t(py_basicsize);\n    Py_DECREF(py_basicsize);\n    py_basicsize = 0;\n    if (basicsize == (Py_ssize_t)-1 && PyErr_Occurred())\n        goto bad;\n#endif\n    if (!strict && (size_t)basicsize > size) {\n        PyOS_snprintf(warning, sizeof(warning),\n            \"%s.%s size changed, may indicate binary incompatibility\",\n            module_name, class_name);\n        #if PY_VERSION_HEX < 0x02050000\n        if (PyErr_Warn(NULL, warning) < 0) goto bad;\n        #else\n        if (PyErr_WarnEx(NULL, warning, 0) < 0) goto bad;\n        #endif\n    }\n    else if ((size_t)basicsize != size) {\n        PyErr_Format(PyExc_ValueError,\n            \"%.200s.%.200s has the wrong size, try recompiling\",\n            module_name, class_name);\n        goto bad;\n    }\n    return (PyTypeObject *)result;\nbad:\n    Py_XDECREF(py_module);\n    Py_XDECREF(result);\n    return NULL;\n}\n#endif\n\nstatic int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line) {\n    int start = 0, mid = 0, end = count - 1;\n    if (end >= 0 && code_line > entries[end].code_line) {\n        return count;\n    }\n    while (start < end) {\n        mid = (start + end) / 2;\n        if (code_line < entries[mid].code_line) {\n            end = mid;\n        } else if (code_line > entries[mid].code_line) {\n             start = mid + 1;\n        } else {\n            return mid;\n        }\n    }\n    if (code_line <= entries[mid].code_line) {\n        return mid;\n    } else {\n        return mid + 1;\n    }\n}\nstatic PyCodeObject *__pyx_find_code_object(int code_line) {\n    PyCodeObject* code_object;\n    int pos;\n    if (unlikely(!code_line) || unlikely(!__pyx_code_cache.entries)) {\n        return NULL;\n    }\n    pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line);\n    if (unlikely(pos >= __pyx_code_cache.count) || unlikely(__pyx_code_cache.entries[pos].code_line != code_line)) {\n        return NULL;\n    }\n    code_object = __pyx_code_cache.entries[pos].code_object;\n    Py_INCREF(code_object);\n    return code_object;\n}\nstatic void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) {\n    int pos, i;\n    __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries;\n    if (unlikely(!code_line)) {\n        return;\n    }\n    if (unlikely(!entries)) {\n        entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Malloc(64*sizeof(__Pyx_CodeObjectCacheEntry));\n        if (likely(entries)) {\n            __pyx_code_cache.entries = entries;\n            __pyx_code_cache.max_count = 64;\n            __pyx_code_cache.count = 1;\n            entries[0].code_line = code_line;\n            entries[0].code_object = code_object;\n            Py_INCREF(code_object);\n        }\n        return;\n    }\n    pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line);\n    if ((pos < __pyx_code_cache.count) && unlikely(__pyx_code_cache.entries[pos].code_line == code_line)) {\n        PyCodeObject* tmp = entries[pos].code_object;\n        entries[pos].code_object = code_object;\n        Py_DECREF(tmp);\n        return;\n    }\n    if (__pyx_code_cache.count == __pyx_code_cache.max_count) {\n        int new_max = __pyx_code_cache.max_count + 64;\n        entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Realloc(\n            __pyx_code_cache.entries, new_max*sizeof(__Pyx_CodeObjectCacheEntry));\n        if (unlikely(!entries)) {\n            return;\n        }\n        __pyx_code_cache.entries = entries;\n        __pyx_code_cache.max_count = new_max;\n    }\n    for (i=__pyx_code_cache.count; i>pos; i--) {\n        entries[i] = entries[i-1];\n    }\n    entries[pos].code_line = code_line;\n    entries[pos].code_object = code_object;\n    __pyx_code_cache.count++;\n    Py_INCREF(code_object);\n}\n\n#include \"compile.h\"\n#include \"frameobject.h\"\n#include \"traceback.h\"\nstatic PyCodeObject* __Pyx_CreateCodeObjectForTraceback(\n            const char *funcname, int c_line,\n            int py_line, const char *filename) {\n    PyCodeObject *py_code = 0;\n    PyObject *py_srcfile = 0;\n    PyObject *py_funcname = 0;\n    #if PY_MAJOR_VERSION < 3\n    py_srcfile = PyString_FromString(filename);\n    #else\n    py_srcfile = PyUnicode_FromString(filename);\n    #endif\n    if (!py_srcfile) goto bad;\n    if (c_line) {\n        #if PY_MAJOR_VERSION < 3\n        py_funcname = PyString_FromFormat( \"%s (%s:%d)\", funcname, __pyx_cfilenm, c_line);\n        #else\n        py_funcname = PyUnicode_FromFormat( \"%s (%s:%d)\", funcname, __pyx_cfilenm, c_line);\n        #endif\n    }\n    else {\n        #if PY_MAJOR_VERSION < 3\n        py_funcname = PyString_FromString(funcname);\n        #else\n        py_funcname = PyUnicode_FromString(funcname);\n        #endif\n    }\n    if (!py_funcname) goto bad;\n    py_code = __Pyx_PyCode_New(\n        0,            /*int argcount,*/\n        0,            /*int kwonlyargcount,*/\n        0,            /*int nlocals,*/\n        0,            /*int stacksize,*/\n        0,            /*int flags,*/\n        __pyx_empty_bytes, /*PyObject *code,*/\n        __pyx_empty_tuple, /*PyObject *consts,*/\n        __pyx_empty_tuple, /*PyObject *names,*/\n        __pyx_empty_tuple, /*PyObject *varnames,*/\n        __pyx_empty_tuple, /*PyObject *freevars,*/\n        __pyx_empty_tuple, /*PyObject *cellvars,*/\n        py_srcfile,   /*PyObject *filename,*/\n        py_funcname,  /*PyObject *name,*/\n        py_line,      /*int firstlineno,*/\n        __pyx_empty_bytes  /*PyObject *lnotab*/\n    );\n    Py_DECREF(py_srcfile);\n    Py_DECREF(py_funcname);\n    return py_code;\nbad:\n    Py_XDECREF(py_srcfile);\n    Py_XDECREF(py_funcname);\n    return NULL;\n}\nstatic void __Pyx_AddTraceback(const char *funcname, int c_line,\n                               int py_line, const char *filename) {\n    PyCodeObject *py_code = 0;\n    PyObject *py_globals = 0;\n    PyFrameObject *py_frame = 0;\n    py_code = __pyx_find_code_object(c_line ? c_line : py_line);\n    if (!py_code) {\n        py_code = __Pyx_CreateCodeObjectForTraceback(\n            funcname, c_line, py_line, filename);\n        if (!py_code) goto bad;\n        __pyx_insert_code_object(c_line ? c_line : py_line, py_code);\n    }\n    py_globals = PyModule_GetDict(__pyx_m);\n    if (!py_globals) goto bad;\n    py_frame = PyFrame_New(\n        PyThreadState_GET(), /*PyThreadState *tstate,*/\n        py_code,             /*PyCodeObject *code,*/\n        py_globals,          /*PyObject *globals,*/\n        0                    /*PyObject *locals*/\n    );\n    if (!py_frame) goto bad;\n    py_frame->f_lineno = py_line;\n    PyTraceBack_Here(py_frame);\nbad:\n    Py_XDECREF(py_code);\n    Py_XDECREF(py_frame);\n}\n\nstatic int __Pyx_InitStrings(__Pyx_StringTabEntry *t) {\n    while (t->p) {\n        #if PY_MAJOR_VERSION < 3\n        if (t->is_unicode) {\n            *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL);\n        } else if (t->intern) {\n            *t->p = PyString_InternFromString(t->s);\n        } else {\n            *t->p = PyString_FromStringAndSize(t->s, t->n - 1);\n        }\n        #else  /* Python 3+ has unicode identifiers */\n        if (t->is_unicode | t->is_str) {\n            if (t->intern) {\n                *t->p = PyUnicode_InternFromString(t->s);\n            } else if (t->encoding) {\n                *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL);\n            } else {\n                *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1);\n            }\n        } else {\n            *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1);\n        }\n        #endif\n        if (!*t->p)\n            return -1;\n        ++t;\n    }\n    return 0;\n}\n\nstatic CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char* c_str) {\n    return __Pyx_PyUnicode_FromStringAndSize(c_str, strlen(c_str));\n}\nstatic CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject* o) {\n    Py_ssize_t ignore;\n    return __Pyx_PyObject_AsStringAndSize(o, &ignore);\n}\nstatic CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) {\n#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT\n    if (\n#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII\n            __Pyx_sys_getdefaultencoding_not_ascii &&\n#endif\n            PyUnicode_Check(o)) {\n#if PY_VERSION_HEX < 0x03030000\n        char* defenc_c;\n        PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL);\n        if (!defenc) return NULL;\n        defenc_c = PyBytes_AS_STRING(defenc);\n#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII\n        {\n            char* end = defenc_c + PyBytes_GET_SIZE(defenc);\n            char* c;\n            for (c = defenc_c; c < end; c++) {\n                if ((unsigned char) (*c) >= 128) {\n                    PyUnicode_AsASCIIString(o);\n                    return NULL;\n                }\n            }\n        }\n#endif /*__PYX_DEFAULT_STRING_ENCODING_IS_ASCII*/\n        *length = PyBytes_GET_SIZE(defenc);\n        return defenc_c;\n#else /* PY_VERSION_HEX < 0x03030000 */\n        if (PyUnicode_READY(o) == -1) return NULL;\n#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII\n        if (PyUnicode_IS_ASCII(o)) {\n            *length = PyUnicode_GET_DATA_SIZE(o);\n            return PyUnicode_AsUTF8(o);\n        } else {\n            PyUnicode_AsASCIIString(o);\n            return NULL;\n        }\n#else /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */\n        return PyUnicode_AsUTF8AndSize(o, length);\n#endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */\n#endif /* PY_VERSION_HEX < 0x03030000 */\n    } else\n#endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII  || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT */\n#if !CYTHON_COMPILING_IN_PYPY\n#if PY_VERSION_HEX >= 0x02060000\n    if (PyByteArray_Check(o)) {\n        *length = PyByteArray_GET_SIZE(o);\n        return PyByteArray_AS_STRING(o);\n    } else\n#endif\n#endif\n    {\n        char* result;\n        int r = PyBytes_AsStringAndSize(o, &result, length);\n        if (unlikely(r < 0)) {\n            return NULL;\n        } else {\n            return result;\n        }\n    }\n}\nstatic CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {\n   int is_true = x == Py_True;\n   if (is_true | (x == Py_False) | (x == Py_None)) return is_true;\n   else return PyObject_IsTrue(x);\n}\nstatic CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x) {\n  PyNumberMethods *m;\n  const char *name = NULL;\n  PyObject *res = NULL;\n#if PY_MAJOR_VERSION < 3\n  if (PyInt_Check(x) || PyLong_Check(x))\n#else\n  if (PyLong_Check(x))\n#endif\n    return Py_INCREF(x), x;\n  m = Py_TYPE(x)->tp_as_number;\n#if PY_MAJOR_VERSION < 3\n  if (m && m->nb_int) {\n    name = \"int\";\n    res = PyNumber_Int(x);\n  }\n  else if (m && m->nb_long) {\n    name = \"long\";\n    res = PyNumber_Long(x);\n  }\n#else\n  if (m && m->nb_int) {\n    name = \"int\";\n    res = PyNumber_Long(x);\n  }\n#endif\n  if (res) {\n#if PY_MAJOR_VERSION < 3\n    if (!PyInt_Check(res) && !PyLong_Check(res)) {\n#else\n    if (!PyLong_Check(res)) {\n#endif\n      PyErr_Format(PyExc_TypeError,\n                   \"__%.4s__ returned non-%.4s (type %.200s)\",\n                   name, name, Py_TYPE(res)->tp_name);\n      Py_DECREF(res);\n      return NULL;\n    }\n  }\n  else if (!PyErr_Occurred()) {\n    PyErr_SetString(PyExc_TypeError,\n                    \"an integer is required\");\n  }\n  return res;\n}\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n  #include \"longintrepr.h\"\n #endif\n#endif\nstatic CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {\n  Py_ssize_t ival;\n  PyObject *x;\n#if PY_MAJOR_VERSION < 3\n  if (likely(PyInt_CheckExact(b)))\n      return PyInt_AS_LONG(b);\n#endif\n  if (likely(PyLong_CheckExact(b))) {\n    #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n     #if CYTHON_USE_PYLONG_INTERNALS\n       switch (Py_SIZE(b)) {\n       case -1: return -(sdigit)((PyLongObject*)b)->ob_digit[0];\n       case  0: return 0;\n       case  1: return ((PyLongObject*)b)->ob_digit[0];\n       }\n     #endif\n    #endif\n  #if PY_VERSION_HEX < 0x02060000\n    return PyInt_AsSsize_t(b);\n  #else\n    return PyLong_AsSsize_t(b);\n  #endif\n  }\n  x = PyNumber_Index(b);\n  if (!x) return -1;\n  ival = PyInt_AsSsize_t(x);\n  Py_DECREF(x);\n  return ival;\n}\nstatic CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {\n#if PY_VERSION_HEX < 0x02050000\n   if (ival <= LONG_MAX)\n       return PyInt_FromLong((long)ival);\n   else {\n       unsigned char *bytes = (unsigned char *) &ival;\n       int one = 1; int little = (int)*(unsigned char*)&one;\n       return _PyLong_FromByteArray(bytes, sizeof(size_t), little, 0);\n   }\n#else\n   return PyInt_FromSize_t(ival);\n#endif\n}\n\n\n#endif /* Py_PYTHON_H */\n"
  },
  {
    "path": "lib/nms/cpu_nms.pyx",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\nimport numpy as np\ncimport numpy as np\n\ncdef inline np.float32_t max(np.float32_t a, np.float32_t b):\n    return a if a >= b else b\n\ncdef inline np.float32_t min(np.float32_t a, np.float32_t b):\n    return a if a <= b else b\n\ndef cpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):\n    cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]\n    cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n    cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]\n    cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]\n    cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4]\n\n    cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1)\n    cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1]\n\n    cdef int ndets = dets.shape[0]\n    cdef np.ndarray[np.int_t, ndim=1] suppressed = \\\n            np.zeros((ndets), dtype=np.int)\n\n    # nominal indices\n    cdef int _i, _j\n    # sorted indices\n    cdef int i, j\n    # temp variables for box i's (the box currently under consideration)\n    cdef np.float32_t ix1, iy1, ix2, iy2, iarea\n    # variables for computing overlap with box j (lower scoring box)\n    cdef np.float32_t xx1, yy1, xx2, yy2\n    cdef np.float32_t w, h\n    cdef np.float32_t inter, ovr\n\n    keep = []\n    for _i in range(ndets):\n        i = order[_i]\n        if suppressed[i] == 1:\n            continue\n        keep.append(i)\n        ix1 = x1[i]\n        iy1 = y1[i]\n        ix2 = x2[i]\n        iy2 = y2[i]\n        iarea = areas[i]\n        for _j in range(_i + 1, ndets):\n            j = order[_j]\n            if suppressed[j] == 1:\n                continue\n            xx1 = max(ix1, x1[j])\n            yy1 = max(iy1, y1[j])\n            xx2 = min(ix2, x2[j])\n            yy2 = min(iy2, y2[j])\n            w = max(0.0, xx2 - xx1 + 1)\n            h = max(0.0, yy2 - yy1 + 1)\n            inter = w * h\n            ovr = inter / (iarea + areas[j] - inter)\n            if ovr >= thresh:\n                suppressed[j] = 1\n\n    return keep\n"
  },
  {
    "path": "lib/nms/gpu_nms.cpp",
    "content": "/* Generated by Cython 0.20.1 on Wed Oct  5 13:15:30 2016 */\n\n#define PY_SSIZE_T_CLEAN\n#ifndef CYTHON_USE_PYLONG_INTERNALS\n#ifdef PYLONG_BITS_IN_DIGIT\n#define CYTHON_USE_PYLONG_INTERNALS 0\n#else\n#include \"pyconfig.h\"\n#ifdef PYLONG_BITS_IN_DIGIT\n#define CYTHON_USE_PYLONG_INTERNALS 1\n#else\n#define CYTHON_USE_PYLONG_INTERNALS 0\n#endif\n#endif\n#endif\n#include \"Python.h\"\n#ifndef Py_PYTHON_H\n    #error Python headers needed to compile C extensions, please install development version of Python.\n#elif PY_VERSION_HEX < 0x02040000\n    #error Cython requires Python 2.4+.\n#else\n#define CYTHON_ABI \"0_20_1\"\n#include <stddef.h> /* For offsetof */\n#ifndef offsetof\n#define offsetof(type, member) ( (size_t) & ((type*)0) -> member )\n#endif\n#if !defined(WIN32) && !defined(MS_WINDOWS)\n  #ifndef __stdcall\n    #define __stdcall\n  #endif\n  #ifndef __cdecl\n    #define __cdecl\n  #endif\n  #ifndef __fastcall\n    #define __fastcall\n  #endif\n#endif\n#ifndef DL_IMPORT\n  #define DL_IMPORT(t) t\n#endif\n#ifndef DL_EXPORT\n  #define DL_EXPORT(t) t\n#endif\n#ifndef PY_LONG_LONG\n  #define PY_LONG_LONG LONG_LONG\n#endif\n#ifndef Py_HUGE_VAL\n  #define Py_HUGE_VAL HUGE_VAL\n#endif\n#ifdef PYPY_VERSION\n#define CYTHON_COMPILING_IN_PYPY 1\n#define CYTHON_COMPILING_IN_CPYTHON 0\n#else\n#define CYTHON_COMPILING_IN_PYPY 0\n#define CYTHON_COMPILING_IN_CPYTHON 1\n#endif\n#if CYTHON_COMPILING_IN_PYPY\n#define Py_OptimizeFlag 0\n#endif\n#if PY_VERSION_HEX < 0x02050000\n  typedef int Py_ssize_t;\n  #define PY_SSIZE_T_MAX INT_MAX\n  #define PY_SSIZE_T_MIN INT_MIN\n  #define PY_FORMAT_SIZE_T \"\"\n  #define CYTHON_FORMAT_SSIZE_T \"\"\n  #define PyInt_FromSsize_t(z) PyInt_FromLong(z)\n  #define PyInt_AsSsize_t(o)   __Pyx_PyInt_As_int(o)\n  #define PyNumber_Index(o)    ((PyNumber_Check(o) && !PyFloat_Check(o)) ? PyNumber_Int(o) : \\\n                                (PyErr_Format(PyExc_TypeError, \\\n                                              \"expected index value, got %.200s\", Py_TYPE(o)->tp_name), \\\n                                 (PyObject*)0))\n  #define __Pyx_PyIndex_Check(o) (PyNumber_Check(o) && !PyFloat_Check(o) && \\\n                                  !PyComplex_Check(o))\n  #define PyIndex_Check __Pyx_PyIndex_Check\n  #define PyErr_WarnEx(category, message, stacklevel) PyErr_Warn(category, message)\n  #define __PYX_BUILD_PY_SSIZE_T \"i\"\n#else\n  #define __PYX_BUILD_PY_SSIZE_T \"n\"\n  #define CYTHON_FORMAT_SSIZE_T \"z\"\n  #define __Pyx_PyIndex_Check PyIndex_Check\n#endif\n#if PY_VERSION_HEX < 0x02060000\n  #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt)\n  #define Py_TYPE(ob)   (((PyObject*)(ob))->ob_type)\n  #define Py_SIZE(ob)   (((PyVarObject*)(ob))->ob_size)\n  #define PyVarObject_HEAD_INIT(type, size) \\\n          PyObject_HEAD_INIT(type) size,\n  #define PyType_Modified(t)\n  typedef struct {\n     void *buf;\n     PyObject *obj;\n     Py_ssize_t len;\n     Py_ssize_t itemsize;\n     int readonly;\n     int ndim;\n     char *format;\n     Py_ssize_t *shape;\n     Py_ssize_t *strides;\n     Py_ssize_t *suboffsets;\n     void *internal;\n  } Py_buffer;\n  #define PyBUF_SIMPLE 0\n  #define PyBUF_WRITABLE 0x0001\n  #define PyBUF_FORMAT 0x0004\n  #define PyBUF_ND 0x0008\n  #define PyBUF_STRIDES (0x0010 | PyBUF_ND)\n  #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES)\n  #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES)\n  #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES)\n  #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES)\n  #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_FORMAT | PyBUF_WRITABLE)\n  #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_FORMAT | PyBUF_WRITABLE)\n  typedef int (*getbufferproc)(PyObject *, Py_buffer *, int);\n  typedef void (*releasebufferproc)(PyObject *, Py_buffer *);\n#endif\n#if PY_MAJOR_VERSION < 3\n  #define __Pyx_BUILTIN_MODULE_NAME \"__builtin__\"\n  #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \\\n          PyCode_New(a+k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\n  #define __Pyx_DefaultClassType PyClass_Type\n#else\n  #define __Pyx_BUILTIN_MODULE_NAME \"builtins\"\n  #define __Pyx_PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos) \\\n          PyCode_New(a, k, l, s, f, code, c, n, v, fv, cell, fn, name, fline, lnos)\n  #define __Pyx_DefaultClassType PyType_Type\n#endif\n#if PY_VERSION_HEX < 0x02060000\n  #define PyUnicode_FromString(s) PyUnicode_Decode(s, strlen(s), \"UTF-8\", \"strict\")\n#endif\n#if PY_MAJOR_VERSION >= 3\n  #define Py_TPFLAGS_CHECKTYPES 0\n  #define Py_TPFLAGS_HAVE_INDEX 0\n#endif\n#if (PY_VERSION_HEX < 0x02060000) || (PY_MAJOR_VERSION >= 3)\n  #define Py_TPFLAGS_HAVE_NEWBUFFER 0\n#endif\n#if PY_VERSION_HEX < 0x02060000\n  #define Py_TPFLAGS_HAVE_VERSION_TAG 0\n#endif\n#if PY_VERSION_HEX < 0x02060000 && !defined(Py_TPFLAGS_IS_ABSTRACT)\n  #define Py_TPFLAGS_IS_ABSTRACT 0\n#endif\n#if PY_VERSION_HEX < 0x030400a1 && !defined(Py_TPFLAGS_HAVE_FINALIZE)\n  #define Py_TPFLAGS_HAVE_FINALIZE 0\n#endif\n#if PY_VERSION_HEX > 0x03030000 && defined(PyUnicode_KIND)\n  #define CYTHON_PEP393_ENABLED 1\n  #define __Pyx_PyUnicode_READY(op)       (likely(PyUnicode_IS_READY(op)) ? \\\n                                              0 : _PyUnicode_Ready((PyObject *)(op)))\n  #define __Pyx_PyUnicode_GET_LENGTH(u)   PyUnicode_GET_LENGTH(u)\n  #define __Pyx_PyUnicode_READ_CHAR(u, i) PyUnicode_READ_CHAR(u, i)\n  #define __Pyx_PyUnicode_KIND(u)         PyUnicode_KIND(u)\n  #define __Pyx_PyUnicode_DATA(u)         PyUnicode_DATA(u)\n  #define __Pyx_PyUnicode_READ(k, d, i)   PyUnicode_READ(k, d, i)\n#else\n  #define CYTHON_PEP393_ENABLED 0\n  #define __Pyx_PyUnicode_READY(op)       (0)\n  #define __Pyx_PyUnicode_GET_LENGTH(u)   PyUnicode_GET_SIZE(u)\n  #define __Pyx_PyUnicode_READ_CHAR(u, i) ((Py_UCS4)(PyUnicode_AS_UNICODE(u)[i]))\n  #define __Pyx_PyUnicode_KIND(u)         (sizeof(Py_UNICODE))\n  #define __Pyx_PyUnicode_DATA(u)         ((void*)PyUnicode_AS_UNICODE(u))\n  #define __Pyx_PyUnicode_READ(k, d, i)   ((void)(k), (Py_UCS4)(((Py_UNICODE*)d)[i]))\n#endif\n#if CYTHON_COMPILING_IN_PYPY\n  #define __Pyx_PyUnicode_Concat(a, b)      PyNumber_Add(a, b)\n  #define __Pyx_PyUnicode_ConcatSafe(a, b)  PyNumber_Add(a, b)\n#else\n  #define __Pyx_PyUnicode_Concat(a, b)      PyUnicode_Concat(a, b)\n  #define __Pyx_PyUnicode_ConcatSafe(a, b)  ((unlikely((a) == Py_None) || unlikely((b) == Py_None)) ? \\\n      PyNumber_Add(a, b) : __Pyx_PyUnicode_Concat(a, b))\n#endif\n#define __Pyx_PyString_FormatSafe(a, b)  ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : __Pyx_PyString_Format(a, b))\n#define __Pyx_PyUnicode_FormatSafe(a, b)  ((unlikely((a) == Py_None)) ? PyNumber_Remainder(a, b) : PyUnicode_Format(a, b))\n#if PY_MAJOR_VERSION >= 3\n  #define __Pyx_PyString_Format(a, b)  PyUnicode_Format(a, b)\n#else\n  #define __Pyx_PyString_Format(a, b)  PyString_Format(a, b)\n#endif\n#if PY_MAJOR_VERSION >= 3\n  #define PyBaseString_Type            PyUnicode_Type\n  #define PyStringObject               PyUnicodeObject\n  #define PyString_Type                PyUnicode_Type\n  #define PyString_Check               PyUnicode_Check\n  #define PyString_CheckExact          PyUnicode_CheckExact\n#endif\n#if PY_VERSION_HEX < 0x02060000\n  #define PyBytesObject                PyStringObject\n  #define PyBytes_Type                 PyString_Type\n  #define PyBytes_Check                PyString_Check\n  #define PyBytes_CheckExact           PyString_CheckExact\n  #define PyBytes_FromString           PyString_FromString\n  #define PyBytes_FromStringAndSize    PyString_FromStringAndSize\n  #define PyBytes_FromFormat           PyString_FromFormat\n  #define PyBytes_DecodeEscape         PyString_DecodeEscape\n  #define PyBytes_AsString             PyString_AsString\n  #define PyBytes_AsStringAndSize      PyString_AsStringAndSize\n  #define PyBytes_Size                 PyString_Size\n  #define PyBytes_AS_STRING            PyString_AS_STRING\n  #define PyBytes_GET_SIZE             PyString_GET_SIZE\n  #define PyBytes_Repr                 PyString_Repr\n  #define PyBytes_Concat               PyString_Concat\n  #define PyBytes_ConcatAndDel         PyString_ConcatAndDel\n#endif\n#if PY_MAJOR_VERSION >= 3\n  #define __Pyx_PyBaseString_Check(obj) PyUnicode_Check(obj)\n  #define __Pyx_PyBaseString_CheckExact(obj) PyUnicode_CheckExact(obj)\n#else\n  #define __Pyx_PyBaseString_Check(obj) (PyString_CheckExact(obj) || PyUnicode_CheckExact(obj) || \\\n                                         PyString_Check(obj) || PyUnicode_Check(obj))\n  #define __Pyx_PyBaseString_CheckExact(obj) (PyString_CheckExact(obj) || PyUnicode_CheckExact(obj))\n#endif\n#if PY_VERSION_HEX < 0x02060000\n  #define PySet_Check(obj)             PyObject_TypeCheck(obj, &PySet_Type)\n  #define PyFrozenSet_Check(obj)       PyObject_TypeCheck(obj, &PyFrozenSet_Type)\n#endif\n#ifndef PySet_CheckExact\n  #define PySet_CheckExact(obj)        (Py_TYPE(obj) == &PySet_Type)\n#endif\n#define __Pyx_TypeCheck(obj, type) PyObject_TypeCheck(obj, (PyTypeObject *)type)\n#if PY_MAJOR_VERSION >= 3\n  #define PyIntObject                  PyLongObject\n  #define PyInt_Type                   PyLong_Type\n  #define PyInt_Check(op)              PyLong_Check(op)\n  #define PyInt_CheckExact(op)         PyLong_CheckExact(op)\n  #define PyInt_FromString             PyLong_FromString\n  #define PyInt_FromUnicode            PyLong_FromUnicode\n  #define PyInt_FromLong               PyLong_FromLong\n  #define PyInt_FromSize_t             PyLong_FromSize_t\n  #define PyInt_FromSsize_t            PyLong_FromSsize_t\n  #define PyInt_AsLong                 PyLong_AsLong\n  #define PyInt_AS_LONG                PyLong_AS_LONG\n  #define PyInt_AsSsize_t              PyLong_AsSsize_t\n  #define PyInt_AsUnsignedLongMask     PyLong_AsUnsignedLongMask\n  #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask\n  #define PyNumber_Int                 PyNumber_Long\n#endif\n#if PY_MAJOR_VERSION >= 3\n  #define PyBoolObject                 PyLongObject\n#endif\n#if PY_VERSION_HEX < 0x030200A4\n  typedef long Py_hash_t;\n  #define __Pyx_PyInt_FromHash_t PyInt_FromLong\n  #define __Pyx_PyInt_AsHash_t   PyInt_AsLong\n#else\n  #define __Pyx_PyInt_FromHash_t PyInt_FromSsize_t\n  #define __Pyx_PyInt_AsHash_t   PyInt_AsSsize_t\n#endif\n#if (PY_MAJOR_VERSION < 3) || (PY_VERSION_HEX >= 0x03010300)\n  #define __Pyx_PySequence_GetSlice(obj, a, b) PySequence_GetSlice(obj, a, b)\n  #define __Pyx_PySequence_SetSlice(obj, a, b, value) PySequence_SetSlice(obj, a, b, value)\n  #define __Pyx_PySequence_DelSlice(obj, a, b) PySequence_DelSlice(obj, a, b)\n#else\n  #define __Pyx_PySequence_GetSlice(obj, a, b) (unlikely(!(obj)) ? \\\n        (PyErr_SetString(PyExc_SystemError, \"null argument to internal routine\"), (PyObject*)0) : \\\n        (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_GetSlice(obj, a, b)) : \\\n            (PyErr_Format(PyExc_TypeError, \"'%.200s' object is unsliceable\", (obj)->ob_type->tp_name), (PyObject*)0)))\n  #define __Pyx_PySequence_SetSlice(obj, a, b, value) (unlikely(!(obj)) ? \\\n        (PyErr_SetString(PyExc_SystemError, \"null argument to internal routine\"), -1) : \\\n        (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_SetSlice(obj, a, b, value)) : \\\n            (PyErr_Format(PyExc_TypeError, \"'%.200s' object doesn't support slice assignment\", (obj)->ob_type->tp_name), -1)))\n  #define __Pyx_PySequence_DelSlice(obj, a, b) (unlikely(!(obj)) ? \\\n        (PyErr_SetString(PyExc_SystemError, \"null argument to internal routine\"), -1) : \\\n        (likely((obj)->ob_type->tp_as_mapping) ? (PySequence_DelSlice(obj, a, b)) : \\\n            (PyErr_Format(PyExc_TypeError, \"'%.200s' object doesn't support slice deletion\", (obj)->ob_type->tp_name), -1)))\n#endif\n#if PY_MAJOR_VERSION >= 3\n  #define PyMethod_New(func, self, klass) ((self) ? PyMethod_New(func, self) : PyInstanceMethod_New(func))\n#endif\n#if PY_VERSION_HEX < 0x02050000\n  #define __Pyx_GetAttrString(o,n)   PyObject_GetAttrString((o),((char *)(n)))\n  #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),((char *)(n)),(a))\n  #define __Pyx_DelAttrString(o,n)   PyObject_DelAttrString((o),((char *)(n)))\n#else\n  #define __Pyx_GetAttrString(o,n)   PyObject_GetAttrString((o),(n))\n  #define __Pyx_SetAttrString(o,n,a) PyObject_SetAttrString((o),(n),(a))\n  #define __Pyx_DelAttrString(o,n)   PyObject_DelAttrString((o),(n))\n#endif\n#if PY_VERSION_HEX < 0x02050000\n  #define __Pyx_NAMESTR(n) ((char *)(n))\n  #define __Pyx_DOCSTR(n)  ((char *)(n))\n#else\n  #define __Pyx_NAMESTR(n) (n)\n  #define __Pyx_DOCSTR(n)  (n)\n#endif\n#ifndef CYTHON_INLINE\n  #if defined(__GNUC__)\n    #define CYTHON_INLINE __inline__\n  #elif defined(_MSC_VER)\n    #define CYTHON_INLINE __inline\n  #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L\n    #define CYTHON_INLINE inline\n  #else\n    #define CYTHON_INLINE\n  #endif\n#endif\n#ifndef CYTHON_RESTRICT\n  #if defined(__GNUC__)\n    #define CYTHON_RESTRICT __restrict__\n  #elif defined(_MSC_VER) && _MSC_VER >= 1400\n    #define CYTHON_RESTRICT __restrict\n  #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L\n    #define CYTHON_RESTRICT restrict\n  #else\n    #define CYTHON_RESTRICT\n  #endif\n#endif\n#ifdef NAN\n#define __PYX_NAN() ((float) NAN)\n#else\nstatic CYTHON_INLINE float __PYX_NAN() {\n  /* Initialize NaN. The sign is irrelevant, an exponent with all bits 1 and\n   a nonzero mantissa means NaN. If the first bit in the mantissa is 1, it is\n   a quiet NaN. */\n  float value;\n  memset(&value, 0xFF, sizeof(value));\n  return value;\n}\n#endif\n\n\n#if PY_MAJOR_VERSION >= 3\n  #define __Pyx_PyNumber_Divide(x,y)         PyNumber_TrueDivide(x,y)\n  #define __Pyx_PyNumber_InPlaceDivide(x,y)  PyNumber_InPlaceTrueDivide(x,y)\n#else\n  #define __Pyx_PyNumber_Divide(x,y)         PyNumber_Divide(x,y)\n  #define __Pyx_PyNumber_InPlaceDivide(x,y)  PyNumber_InPlaceDivide(x,y)\n#endif\n\n#ifndef __PYX_EXTERN_C\n  #ifdef __cplusplus\n    #define __PYX_EXTERN_C extern \"C\"\n  #else\n    #define __PYX_EXTERN_C extern\n  #endif\n#endif\n\n#if defined(WIN32) || defined(MS_WINDOWS)\n#define _USE_MATH_DEFINES\n#endif\n#include <math.h>\n#define __PYX_HAVE__nms__gpu_nms\n#define __PYX_HAVE_API__nms__gpu_nms\n#include \"string.h\"\n#include \"stdio.h\"\n#include \"stdlib.h\"\n#include \"numpy/arrayobject.h\"\n#include \"numpy/ufuncobject.h\"\n#include \"gpu_nms.hpp\"\n#ifdef _OPENMP\n#include <omp.h>\n#endif /* _OPENMP */\n\n#ifdef PYREX_WITHOUT_ASSERTIONS\n#define CYTHON_WITHOUT_ASSERTIONS\n#endif\n\n#ifndef CYTHON_UNUSED\n# if defined(__GNUC__)\n#   if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))\n#     define CYTHON_UNUSED __attribute__ ((__unused__))\n#   else\n#     define CYTHON_UNUSED\n#   endif\n# elif defined(__ICC) || (defined(__INTEL_COMPILER) && !defined(_MSC_VER))\n#   define CYTHON_UNUSED __attribute__ ((__unused__))\n# else\n#   define CYTHON_UNUSED\n# endif\n#endif\ntypedef struct {PyObject **p; char *s; const Py_ssize_t n; const char* encoding;\n                const char is_unicode; const char is_str; const char intern; } __Pyx_StringTabEntry; /*proto*/\n\n#define __PYX_DEFAULT_STRING_ENCODING_IS_ASCII 0\n#define __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT 0\n#define __PYX_DEFAULT_STRING_ENCODING \"\"\n#define __Pyx_PyObject_FromString __Pyx_PyBytes_FromString\n#define __Pyx_PyObject_FromStringAndSize __Pyx_PyBytes_FromStringAndSize\n#define __Pyx_fits_Py_ssize_t(v, type, is_signed)  (    \\\n    (sizeof(type) < sizeof(Py_ssize_t))  ||             \\\n    (sizeof(type) > sizeof(Py_ssize_t) &&               \\\n          likely(v < (type)PY_SSIZE_T_MAX ||            \\\n                 v == (type)PY_SSIZE_T_MAX)  &&         \\\n          (!is_signed || likely(v > (type)PY_SSIZE_T_MIN ||       \\\n                                v == (type)PY_SSIZE_T_MIN)))  ||  \\\n    (sizeof(type) == sizeof(Py_ssize_t) &&              \\\n          (is_signed || likely(v < (type)PY_SSIZE_T_MAX ||        \\\n                               v == (type)PY_SSIZE_T_MAX)))  )\nstatic CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject*);\nstatic CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject*, Py_ssize_t* length);\n#define __Pyx_PyByteArray_FromString(s) PyByteArray_FromStringAndSize((const char*)s, strlen((const char*)s))\n#define __Pyx_PyByteArray_FromStringAndSize(s, l) PyByteArray_FromStringAndSize((const char*)s, l)\n#define __Pyx_PyBytes_FromString        PyBytes_FromString\n#define __Pyx_PyBytes_FromStringAndSize PyBytes_FromStringAndSize\nstatic CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char*);\n#if PY_MAJOR_VERSION < 3\n    #define __Pyx_PyStr_FromString        __Pyx_PyBytes_FromString\n    #define __Pyx_PyStr_FromStringAndSize __Pyx_PyBytes_FromStringAndSize\n#else\n    #define __Pyx_PyStr_FromString        __Pyx_PyUnicode_FromString\n    #define __Pyx_PyStr_FromStringAndSize __Pyx_PyUnicode_FromStringAndSize\n#endif\n#define __Pyx_PyObject_AsSString(s)    ((signed char*) __Pyx_PyObject_AsString(s))\n#define __Pyx_PyObject_AsUString(s)    ((unsigned char*) __Pyx_PyObject_AsString(s))\n#define __Pyx_PyObject_FromUString(s)  __Pyx_PyObject_FromString((char*)s)\n#define __Pyx_PyBytes_FromUString(s)   __Pyx_PyBytes_FromString((char*)s)\n#define __Pyx_PyByteArray_FromUString(s)   __Pyx_PyByteArray_FromString((char*)s)\n#define __Pyx_PyStr_FromUString(s)     __Pyx_PyStr_FromString((char*)s)\n#define __Pyx_PyUnicode_FromUString(s) __Pyx_PyUnicode_FromString((char*)s)\n#if PY_MAJOR_VERSION < 3\nstatic CYTHON_INLINE size_t __Pyx_Py_UNICODE_strlen(const Py_UNICODE *u)\n{\n    const Py_UNICODE *u_end = u;\n    while (*u_end++) ;\n    return u_end - u - 1;\n}\n#else\n#define __Pyx_Py_UNICODE_strlen Py_UNICODE_strlen\n#endif\n#define __Pyx_PyUnicode_FromUnicode(u)       PyUnicode_FromUnicode(u, __Pyx_Py_UNICODE_strlen(u))\n#define __Pyx_PyUnicode_FromUnicodeAndLength PyUnicode_FromUnicode\n#define __Pyx_PyUnicode_AsUnicode            PyUnicode_AsUnicode\n#define __Pyx_Owned_Py_None(b) (Py_INCREF(Py_None), Py_None)\n#define __Pyx_PyBool_FromLong(b) ((b) ? (Py_INCREF(Py_True), Py_True) : (Py_INCREF(Py_False), Py_False))\nstatic CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject*);\nstatic CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x);\nstatic CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject*);\nstatic CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t);\n#if CYTHON_COMPILING_IN_CPYTHON\n#define __pyx_PyFloat_AsDouble(x) (PyFloat_CheckExact(x) ? PyFloat_AS_DOUBLE(x) : PyFloat_AsDouble(x))\n#else\n#define __pyx_PyFloat_AsDouble(x) PyFloat_AsDouble(x)\n#endif\n#define __pyx_PyFloat_AsFloat(x) ((float) __pyx_PyFloat_AsDouble(x))\n#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII\nstatic int __Pyx_sys_getdefaultencoding_not_ascii;\nstatic int __Pyx_init_sys_getdefaultencoding_params(void) {\n    PyObject* sys = NULL;\n    PyObject* default_encoding = NULL;\n    PyObject* ascii_chars_u = NULL;\n    PyObject* ascii_chars_b = NULL;\n    sys = PyImport_ImportModule(\"sys\");\n    if (sys == NULL) goto bad;\n    default_encoding = PyObject_CallMethod(sys, (char*) (const char*) \"getdefaultencoding\", NULL);\n    if (default_encoding == NULL) goto bad;\n    if (strcmp(PyBytes_AsString(default_encoding), \"ascii\") == 0) {\n        __Pyx_sys_getdefaultencoding_not_ascii = 0;\n    } else {\n        const char* default_encoding_c = PyBytes_AS_STRING(default_encoding);\n        char ascii_chars[128];\n        int c;\n        for (c = 0; c < 128; c++) {\n            ascii_chars[c] = c;\n        }\n        __Pyx_sys_getdefaultencoding_not_ascii = 1;\n        ascii_chars_u = PyUnicode_DecodeASCII(ascii_chars, 128, NULL);\n        if (ascii_chars_u == NULL) goto bad;\n        ascii_chars_b = PyUnicode_AsEncodedString(ascii_chars_u, default_encoding_c, NULL);\n        if (ascii_chars_b == NULL || strncmp(ascii_chars, PyBytes_AS_STRING(ascii_chars_b), 128) != 0) {\n            PyErr_Format(\n                PyExc_ValueError,\n                \"This module compiled with c_string_encoding=ascii, but default encoding '%.200s' is not a superset of ascii.\",\n                default_encoding_c);\n            goto bad;\n        }\n    }\n    Py_XDECREF(sys);\n    Py_XDECREF(default_encoding);\n    Py_XDECREF(ascii_chars_u);\n    Py_XDECREF(ascii_chars_b);\n    return 0;\nbad:\n    Py_XDECREF(sys);\n    Py_XDECREF(default_encoding);\n    Py_XDECREF(ascii_chars_u);\n    Py_XDECREF(ascii_chars_b);\n    return -1;\n}\n#endif\n#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT && PY_MAJOR_VERSION >= 3\n#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_DecodeUTF8(c_str, size, NULL)\n#else\n#define __Pyx_PyUnicode_FromStringAndSize(c_str, size) PyUnicode_Decode(c_str, size, __PYX_DEFAULT_STRING_ENCODING, NULL)\n#if __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT\nstatic char* __PYX_DEFAULT_STRING_ENCODING;\nstatic int __Pyx_init_sys_getdefaultencoding_params(void) {\n    PyObject* sys = NULL;\n    PyObject* default_encoding = NULL;\n    char* default_encoding_c;\n    sys = PyImport_ImportModule(\"sys\");\n    if (sys == NULL) goto bad;\n    default_encoding = PyObject_CallMethod(sys, (char*) (const char*) \"getdefaultencoding\", NULL);\n    if (default_encoding == NULL) goto bad;\n    default_encoding_c = PyBytes_AS_STRING(default_encoding);\n    __PYX_DEFAULT_STRING_ENCODING = (char*) malloc(strlen(default_encoding_c));\n    strcpy(__PYX_DEFAULT_STRING_ENCODING, default_encoding_c);\n    Py_DECREF(sys);\n    Py_DECREF(default_encoding);\n    return 0;\nbad:\n    Py_XDECREF(sys);\n    Py_XDECREF(default_encoding);\n    return -1;\n}\n#endif\n#endif\n\n\n#ifdef __GNUC__\n  /* Test for GCC > 2.95 */\n  #if __GNUC__ > 2 || (__GNUC__ == 2 && (__GNUC_MINOR__ > 95))\n    #define likely(x)   __builtin_expect(!!(x), 1)\n    #define unlikely(x) __builtin_expect(!!(x), 0)\n  #else /* __GNUC__ > 2 ... */\n    #define likely(x)   (x)\n    #define unlikely(x) (x)\n  #endif /* __GNUC__ > 2 ... */\n#else /* __GNUC__ */\n  #define likely(x)   (x)\n  #define unlikely(x) (x)\n#endif /* __GNUC__ */\n\nstatic PyObject *__pyx_m;\nstatic PyObject *__pyx_d;\nstatic PyObject *__pyx_b;\nstatic PyObject *__pyx_empty_tuple;\nstatic PyObject *__pyx_empty_bytes;\nstatic int __pyx_lineno;\nstatic int __pyx_clineno = 0;\nstatic const char * __pyx_cfilenm= __FILE__;\nstatic const char *__pyx_filename;\n\n#if !defined(CYTHON_CCOMPLEX)\n  #if defined(__cplusplus)\n    #define CYTHON_CCOMPLEX 1\n  #elif defined(_Complex_I)\n    #define CYTHON_CCOMPLEX 1\n  #else\n    #define CYTHON_CCOMPLEX 0\n  #endif\n#endif\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    #include <complex>\n  #else\n    #include <complex.h>\n  #endif\n#endif\n#if CYTHON_CCOMPLEX && !defined(__cplusplus) && defined(__sun__) && defined(__GNUC__)\n  #undef _Complex_I\n  #define _Complex_I 1.0fj\n#endif\n\n\nstatic const char *__pyx_f[] = {\n  \"gpu_nms.pyx\",\n  \"__init__.pxd\",\n  \"type.pxd\",\n};\n#define IS_UNSIGNED(type) (((type) -1) > 0)\nstruct __Pyx_StructField_;\n#define __PYX_BUF_FLAGS_PACKED_STRUCT (1 << 0)\ntypedef struct {\n  const char* name; /* for error messages only */\n  struct __Pyx_StructField_* fields;\n  size_t size;     /* sizeof(type) */\n  size_t arraysize[8]; /* length of array in each dimension */\n  int ndim;\n  char typegroup; /* _R_eal, _C_omplex, Signed _I_nt, _U_nsigned int, _S_truct, _P_ointer, _O_bject, c_H_ar */\n  char is_unsigned;\n  int flags;\n} __Pyx_TypeInfo;\ntypedef struct __Pyx_StructField_ {\n  __Pyx_TypeInfo* type;\n  const char* name;\n  size_t offset;\n} __Pyx_StructField;\ntypedef struct {\n  __Pyx_StructField* field;\n  size_t parent_offset;\n} __Pyx_BufFmt_StackElem;\ntypedef struct {\n  __Pyx_StructField root;\n  __Pyx_BufFmt_StackElem* head;\n  size_t fmt_offset;\n  size_t new_count, enc_count;\n  size_t struct_alignment;\n  int is_complex;\n  char enc_type;\n  char new_packmode;\n  char enc_packmode;\n  char is_valid_array;\n} __Pyx_BufFmt_Context;\n\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":723\n * # in Cython to enable them only on the right systems.\n * \n * ctypedef npy_int8       int8_t             # <<<<<<<<<<<<<<\n * ctypedef npy_int16      int16_t\n * ctypedef npy_int32      int32_t\n */\ntypedef npy_int8 __pyx_t_5numpy_int8_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":724\n * \n * ctypedef npy_int8       int8_t\n * ctypedef npy_int16      int16_t             # <<<<<<<<<<<<<<\n * ctypedef npy_int32      int32_t\n * ctypedef npy_int64      int64_t\n */\ntypedef npy_int16 __pyx_t_5numpy_int16_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":725\n * ctypedef npy_int8       int8_t\n * ctypedef npy_int16      int16_t\n * ctypedef npy_int32      int32_t             # <<<<<<<<<<<<<<\n * ctypedef npy_int64      int64_t\n * #ctypedef npy_int96      int96_t\n */\ntypedef npy_int32 __pyx_t_5numpy_int32_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":726\n * ctypedef npy_int16      int16_t\n * ctypedef npy_int32      int32_t\n * ctypedef npy_int64      int64_t             # <<<<<<<<<<<<<<\n * #ctypedef npy_int96      int96_t\n * #ctypedef npy_int128     int128_t\n */\ntypedef npy_int64 __pyx_t_5numpy_int64_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":730\n * #ctypedef npy_int128     int128_t\n * \n * ctypedef npy_uint8      uint8_t             # <<<<<<<<<<<<<<\n * ctypedef npy_uint16     uint16_t\n * ctypedef npy_uint32     uint32_t\n */\ntypedef npy_uint8 __pyx_t_5numpy_uint8_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":731\n * \n * ctypedef npy_uint8      uint8_t\n * ctypedef npy_uint16     uint16_t             # <<<<<<<<<<<<<<\n * ctypedef npy_uint32     uint32_t\n * ctypedef npy_uint64     uint64_t\n */\ntypedef npy_uint16 __pyx_t_5numpy_uint16_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":732\n * ctypedef npy_uint8      uint8_t\n * ctypedef npy_uint16     uint16_t\n * ctypedef npy_uint32     uint32_t             # <<<<<<<<<<<<<<\n * ctypedef npy_uint64     uint64_t\n * #ctypedef npy_uint96     uint96_t\n */\ntypedef npy_uint32 __pyx_t_5numpy_uint32_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":733\n * ctypedef npy_uint16     uint16_t\n * ctypedef npy_uint32     uint32_t\n * ctypedef npy_uint64     uint64_t             # <<<<<<<<<<<<<<\n * #ctypedef npy_uint96     uint96_t\n * #ctypedef npy_uint128    uint128_t\n */\ntypedef npy_uint64 __pyx_t_5numpy_uint64_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":737\n * #ctypedef npy_uint128    uint128_t\n * \n * ctypedef npy_float32    float32_t             # <<<<<<<<<<<<<<\n * ctypedef npy_float64    float64_t\n * #ctypedef npy_float80    float80_t\n */\ntypedef npy_float32 __pyx_t_5numpy_float32_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":738\n * \n * ctypedef npy_float32    float32_t\n * ctypedef npy_float64    float64_t             # <<<<<<<<<<<<<<\n * #ctypedef npy_float80    float80_t\n * #ctypedef npy_float128   float128_t\n */\ntypedef npy_float64 __pyx_t_5numpy_float64_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":747\n * # The int types are mapped a bit surprising --\n * # numpy.int corresponds to 'l' and numpy.long to 'q'\n * ctypedef npy_long       int_t             # <<<<<<<<<<<<<<\n * ctypedef npy_longlong   long_t\n * ctypedef npy_longlong   longlong_t\n */\ntypedef npy_long __pyx_t_5numpy_int_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":748\n * # numpy.int corresponds to 'l' and numpy.long to 'q'\n * ctypedef npy_long       int_t\n * ctypedef npy_longlong   long_t             # <<<<<<<<<<<<<<\n * ctypedef npy_longlong   longlong_t\n * \n */\ntypedef npy_longlong __pyx_t_5numpy_long_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":749\n * ctypedef npy_long       int_t\n * ctypedef npy_longlong   long_t\n * ctypedef npy_longlong   longlong_t             # <<<<<<<<<<<<<<\n * \n * ctypedef npy_ulong      uint_t\n */\ntypedef npy_longlong __pyx_t_5numpy_longlong_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":751\n * ctypedef npy_longlong   longlong_t\n * \n * ctypedef npy_ulong      uint_t             # <<<<<<<<<<<<<<\n * ctypedef npy_ulonglong  ulong_t\n * ctypedef npy_ulonglong  ulonglong_t\n */\ntypedef npy_ulong __pyx_t_5numpy_uint_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":752\n * \n * ctypedef npy_ulong      uint_t\n * ctypedef npy_ulonglong  ulong_t             # <<<<<<<<<<<<<<\n * ctypedef npy_ulonglong  ulonglong_t\n * \n */\ntypedef npy_ulonglong __pyx_t_5numpy_ulong_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":753\n * ctypedef npy_ulong      uint_t\n * ctypedef npy_ulonglong  ulong_t\n * ctypedef npy_ulonglong  ulonglong_t             # <<<<<<<<<<<<<<\n * \n * ctypedef npy_intp       intp_t\n */\ntypedef npy_ulonglong __pyx_t_5numpy_ulonglong_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":755\n * ctypedef npy_ulonglong  ulonglong_t\n * \n * ctypedef npy_intp       intp_t             # <<<<<<<<<<<<<<\n * ctypedef npy_uintp      uintp_t\n * \n */\ntypedef npy_intp __pyx_t_5numpy_intp_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":756\n * \n * ctypedef npy_intp       intp_t\n * ctypedef npy_uintp      uintp_t             # <<<<<<<<<<<<<<\n * \n * ctypedef npy_double     float_t\n */\ntypedef npy_uintp __pyx_t_5numpy_uintp_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":758\n * ctypedef npy_uintp      uintp_t\n * \n * ctypedef npy_double     float_t             # <<<<<<<<<<<<<<\n * ctypedef npy_double     double_t\n * ctypedef npy_longdouble longdouble_t\n */\ntypedef npy_double __pyx_t_5numpy_float_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":759\n * \n * ctypedef npy_double     float_t\n * ctypedef npy_double     double_t             # <<<<<<<<<<<<<<\n * ctypedef npy_longdouble longdouble_t\n * \n */\ntypedef npy_double __pyx_t_5numpy_double_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":760\n * ctypedef npy_double     float_t\n * ctypedef npy_double     double_t\n * ctypedef npy_longdouble longdouble_t             # <<<<<<<<<<<<<<\n * \n * ctypedef npy_cfloat      cfloat_t\n */\ntypedef npy_longdouble __pyx_t_5numpy_longdouble_t;\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    typedef ::std::complex< float > __pyx_t_float_complex;\n  #else\n    typedef float _Complex __pyx_t_float_complex;\n  #endif\n#else\n    typedef struct { float real, imag; } __pyx_t_float_complex;\n#endif\n\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    typedef ::std::complex< double > __pyx_t_double_complex;\n  #else\n    typedef double _Complex __pyx_t_double_complex;\n  #endif\n#else\n    typedef struct { double real, imag; } __pyx_t_double_complex;\n#endif\n\n\n/*--- Type declarations ---*/\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":762\n * ctypedef npy_longdouble longdouble_t\n * \n * ctypedef npy_cfloat      cfloat_t             # <<<<<<<<<<<<<<\n * ctypedef npy_cdouble     cdouble_t\n * ctypedef npy_clongdouble clongdouble_t\n */\ntypedef npy_cfloat __pyx_t_5numpy_cfloat_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":763\n * \n * ctypedef npy_cfloat      cfloat_t\n * ctypedef npy_cdouble     cdouble_t             # <<<<<<<<<<<<<<\n * ctypedef npy_clongdouble clongdouble_t\n * \n */\ntypedef npy_cdouble __pyx_t_5numpy_cdouble_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":764\n * ctypedef npy_cfloat      cfloat_t\n * ctypedef npy_cdouble     cdouble_t\n * ctypedef npy_clongdouble clongdouble_t             # <<<<<<<<<<<<<<\n * \n * ctypedef npy_cdouble     complex_t\n */\ntypedef npy_clongdouble __pyx_t_5numpy_clongdouble_t;\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":766\n * ctypedef npy_clongdouble clongdouble_t\n * \n * ctypedef npy_cdouble     complex_t             # <<<<<<<<<<<<<<\n * \n * cdef inline object PyArray_MultiIterNew1(a):\n */\ntypedef npy_cdouble __pyx_t_5numpy_complex_t;\n#ifndef CYTHON_REFNANNY\n  #define CYTHON_REFNANNY 0\n#endif\n#if CYTHON_REFNANNY\n  typedef struct {\n    void (*INCREF)(void*, PyObject*, int);\n    void (*DECREF)(void*, PyObject*, int);\n    void (*GOTREF)(void*, PyObject*, int);\n    void (*GIVEREF)(void*, PyObject*, int);\n    void* (*SetupContext)(const char*, int, const char*);\n    void (*FinishContext)(void**);\n  } __Pyx_RefNannyAPIStruct;\n  static __Pyx_RefNannyAPIStruct *__Pyx_RefNanny = NULL;\n  static __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname); /*proto*/\n  #define __Pyx_RefNannyDeclarations void *__pyx_refnanny = NULL;\n#ifdef WITH_THREAD\n  #define __Pyx_RefNannySetupContext(name, acquire_gil) \\\n          if (acquire_gil) { \\\n              PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure(); \\\n              __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__); \\\n              PyGILState_Release(__pyx_gilstate_save); \\\n          } else { \\\n              __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__); \\\n          }\n#else\n  #define __Pyx_RefNannySetupContext(name, acquire_gil) \\\n          __pyx_refnanny = __Pyx_RefNanny->SetupContext((name), __LINE__, __FILE__)\n#endif\n  #define __Pyx_RefNannyFinishContext() \\\n          __Pyx_RefNanny->FinishContext(&__pyx_refnanny)\n  #define __Pyx_INCREF(r)  __Pyx_RefNanny->INCREF(__pyx_refnanny, (PyObject *)(r), __LINE__)\n  #define __Pyx_DECREF(r)  __Pyx_RefNanny->DECREF(__pyx_refnanny, (PyObject *)(r), __LINE__)\n  #define __Pyx_GOTREF(r)  __Pyx_RefNanny->GOTREF(__pyx_refnanny, (PyObject *)(r), __LINE__)\n  #define __Pyx_GIVEREF(r) __Pyx_RefNanny->GIVEREF(__pyx_refnanny, (PyObject *)(r), __LINE__)\n  #define __Pyx_XINCREF(r)  do { if((r) != NULL) {__Pyx_INCREF(r); }} while(0)\n  #define __Pyx_XDECREF(r)  do { if((r) != NULL) {__Pyx_DECREF(r); }} while(0)\n  #define __Pyx_XGOTREF(r)  do { if((r) != NULL) {__Pyx_GOTREF(r); }} while(0)\n  #define __Pyx_XGIVEREF(r) do { if((r) != NULL) {__Pyx_GIVEREF(r);}} while(0)\n#else\n  #define __Pyx_RefNannyDeclarations\n  #define __Pyx_RefNannySetupContext(name, acquire_gil)\n  #define __Pyx_RefNannyFinishContext()\n  #define __Pyx_INCREF(r) Py_INCREF(r)\n  #define __Pyx_DECREF(r) Py_DECREF(r)\n  #define __Pyx_GOTREF(r)\n  #define __Pyx_GIVEREF(r)\n  #define __Pyx_XINCREF(r) Py_XINCREF(r)\n  #define __Pyx_XDECREF(r) Py_XDECREF(r)\n  #define __Pyx_XGOTREF(r)\n  #define __Pyx_XGIVEREF(r)\n#endif /* CYTHON_REFNANNY */\n#define __Pyx_XDECREF_SET(r, v) do {                            \\\n        PyObject *tmp = (PyObject *) r;                         \\\n        r = v; __Pyx_XDECREF(tmp);                              \\\n    } while (0)\n#define __Pyx_DECREF_SET(r, v) do {                             \\\n        PyObject *tmp = (PyObject *) r;                         \\\n        r = v; __Pyx_DECREF(tmp);                               \\\n    } while (0)\n#define __Pyx_CLEAR(r)    do { PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);} while(0)\n#define __Pyx_XCLEAR(r)   do { if((r) != NULL) {PyObject* tmp = ((PyObject*)(r)); r = NULL; __Pyx_DECREF(tmp);}} while(0)\n\nstatic void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact,\n    Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/\n\nstatic void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); /*proto*/\n\nstatic int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \\\n    PyObject *kwds2, PyObject *values[], Py_ssize_t num_pos_args, \\\n    const char* function_name); /*proto*/\n\nstatic CYTHON_INLINE int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,\n    const char *name, int exact); /*proto*/\n\nstatic CYTHON_INLINE int  __Pyx_GetBufferAndValidate(Py_buffer* buf, PyObject* obj,\n    __Pyx_TypeInfo* dtype, int flags, int nd, int cast, __Pyx_BufFmt_StackElem* stack);\nstatic CYTHON_INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info);\n\n#if CYTHON_COMPILING_IN_CPYTHON\nstatic CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name) {\n    PyTypeObject* tp = Py_TYPE(obj);\n    if (likely(tp->tp_getattro))\n        return tp->tp_getattro(obj, attr_name);\n#if PY_MAJOR_VERSION < 3\n    if (likely(tp->tp_getattr))\n        return tp->tp_getattr(obj, PyString_AS_STRING(attr_name));\n#endif\n    return PyObject_GetAttr(obj, attr_name);\n}\n#else\n#define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n)\n#endif\n\nstatic PyObject *__Pyx_GetBuiltinName(PyObject *name); /*proto*/\n\nstatic CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name); /*proto*/\n\n#if CYTHON_COMPILING_IN_CPYTHON\nstatic CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw); /*proto*/\n#else\n#define __Pyx_PyObject_Call(func, arg, kw) PyObject_Call(func, arg, kw)\n#endif\n\nstatic CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type); /*proto*/\n\nstatic void __Pyx_RaiseBufferIndexError(int axis); /*proto*/\n\n#define __Pyx_BufPtrStrided1d(type, buf, i0, s0) (type)((char*)buf + i0 * s0)\n#define __Pyx_BufPtrStrided2d(type, buf, i0, s0, i1, s1) (type)((char*)buf + i0 * s0 + i1 * s1)\nstatic CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(\n        PyObject* obj, Py_ssize_t cstart, Py_ssize_t cstop,\n        PyObject** py_start, PyObject** py_stop, PyObject** py_slice,\n        int has_cstart, int has_cstop, int wraparound);\n\nstatic void __Pyx_RaiseBufferFallbackError(void); /*proto*/\n\nstatic CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/\nstatic CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/\n\nstatic void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause); /*proto*/\n\nstatic CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected);\n\nstatic CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index);\n\nstatic CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void);\n\ntypedef struct {\n  Py_ssize_t shape, strides, suboffsets;\n} __Pyx_Buf_DimInfo;\ntypedef struct {\n  size_t refcount;\n  Py_buffer pybuffer;\n} __Pyx_Buffer;\ntypedef struct {\n  __Pyx_Buffer *rcbuffer;\n  char *data;\n  __Pyx_Buf_DimInfo diminfo[8];\n} __Pyx_LocalBuf_ND;\n\n#if PY_MAJOR_VERSION < 3\n    static int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags);\n    static void __Pyx_ReleaseBuffer(Py_buffer *view);\n#else\n    #define __Pyx_GetBuffer PyObject_GetBuffer\n    #define __Pyx_ReleaseBuffer PyBuffer_Release\n#endif\n\n\nstatic Py_ssize_t __Pyx_zeros[] = {0, 0, 0, 0, 0, 0, 0, 0};\nstatic Py_ssize_t __Pyx_minusones[] = {-1, -1, -1, -1, -1, -1, -1, -1};\n\nstatic PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level); /*proto*/\n\nstatic CYTHON_INLINE npy_int32 __Pyx_PyInt_As_npy_int32(PyObject *);\n\nstatic CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value);\n\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    #define __Pyx_CREAL(z) ((z).real())\n    #define __Pyx_CIMAG(z) ((z).imag())\n  #else\n    #define __Pyx_CREAL(z) (__real__(z))\n    #define __Pyx_CIMAG(z) (__imag__(z))\n  #endif\n#else\n    #define __Pyx_CREAL(z) ((z).real)\n    #define __Pyx_CIMAG(z) ((z).imag)\n#endif\n#if (defined(_WIN32) || defined(__clang__)) && defined(__cplusplus) && CYTHON_CCOMPLEX\n    #define __Pyx_SET_CREAL(z,x) ((z).real(x))\n    #define __Pyx_SET_CIMAG(z,y) ((z).imag(y))\n#else\n    #define __Pyx_SET_CREAL(z,x) __Pyx_CREAL(z) = (x)\n    #define __Pyx_SET_CIMAG(z,y) __Pyx_CIMAG(z) = (y)\n#endif\n\nstatic CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float, float);\n\n#if CYTHON_CCOMPLEX\n    #define __Pyx_c_eqf(a, b)   ((a)==(b))\n    #define __Pyx_c_sumf(a, b)  ((a)+(b))\n    #define __Pyx_c_difff(a, b) ((a)-(b))\n    #define __Pyx_c_prodf(a, b) ((a)*(b))\n    #define __Pyx_c_quotf(a, b) ((a)/(b))\n    #define __Pyx_c_negf(a)     (-(a))\n  #ifdef __cplusplus\n    #define __Pyx_c_is_zerof(z) ((z)==(float)0)\n    #define __Pyx_c_conjf(z)    (::std::conj(z))\n    #if 1\n        #define __Pyx_c_absf(z)     (::std::abs(z))\n        #define __Pyx_c_powf(a, b)  (::std::pow(a, b))\n    #endif\n  #else\n    #define __Pyx_c_is_zerof(z) ((z)==0)\n    #define __Pyx_c_conjf(z)    (conjf(z))\n    #if 1\n        #define __Pyx_c_absf(z)     (cabsf(z))\n        #define __Pyx_c_powf(a, b)  (cpowf(a, b))\n    #endif\n #endif\n#else\n    static CYTHON_INLINE int __Pyx_c_eqf(__pyx_t_float_complex, __pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_sumf(__pyx_t_float_complex, __pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_difff(__pyx_t_float_complex, __pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_prodf(__pyx_t_float_complex, __pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_quotf(__pyx_t_float_complex, __pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_negf(__pyx_t_float_complex);\n    static CYTHON_INLINE int __Pyx_c_is_zerof(__pyx_t_float_complex);\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_conjf(__pyx_t_float_complex);\n    #if 1\n        static CYTHON_INLINE float __Pyx_c_absf(__pyx_t_float_complex);\n        static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_powf(__pyx_t_float_complex, __pyx_t_float_complex);\n    #endif\n#endif\n\nstatic CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double, double);\n\n#if CYTHON_CCOMPLEX\n    #define __Pyx_c_eq(a, b)   ((a)==(b))\n    #define __Pyx_c_sum(a, b)  ((a)+(b))\n    #define __Pyx_c_diff(a, b) ((a)-(b))\n    #define __Pyx_c_prod(a, b) ((a)*(b))\n    #define __Pyx_c_quot(a, b) ((a)/(b))\n    #define __Pyx_c_neg(a)     (-(a))\n  #ifdef __cplusplus\n    #define __Pyx_c_is_zero(z) ((z)==(double)0)\n    #define __Pyx_c_conj(z)    (::std::conj(z))\n    #if 1\n        #define __Pyx_c_abs(z)     (::std::abs(z))\n        #define __Pyx_c_pow(a, b)  (::std::pow(a, b))\n    #endif\n  #else\n    #define __Pyx_c_is_zero(z) ((z)==0)\n    #define __Pyx_c_conj(z)    (conj(z))\n    #if 1\n        #define __Pyx_c_abs(z)     (cabs(z))\n        #define __Pyx_c_pow(a, b)  (cpow(a, b))\n    #endif\n #endif\n#else\n    static CYTHON_INLINE int __Pyx_c_eq(__pyx_t_double_complex, __pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_sum(__pyx_t_double_complex, __pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_diff(__pyx_t_double_complex, __pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_prod(__pyx_t_double_complex, __pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_quot(__pyx_t_double_complex, __pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_neg(__pyx_t_double_complex);\n    static CYTHON_INLINE int __Pyx_c_is_zero(__pyx_t_double_complex);\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_conj(__pyx_t_double_complex);\n    #if 1\n        static CYTHON_INLINE double __Pyx_c_abs(__pyx_t_double_complex);\n        static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_pow(__pyx_t_double_complex, __pyx_t_double_complex);\n    #endif\n#endif\n\nstatic CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *);\n\nstatic CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value);\n\nstatic CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *);\n\nstatic int __Pyx_check_binary_version(void);\n\n#if !defined(__Pyx_PyIdentifier_FromString)\n#if PY_MAJOR_VERSION < 3\n  #define __Pyx_PyIdentifier_FromString(s) PyString_FromString(s)\n#else\n  #define __Pyx_PyIdentifier_FromString(s) PyUnicode_FromString(s)\n#endif\n#endif\n\nstatic PyObject *__Pyx_ImportModule(const char *name); /*proto*/\n\nstatic PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name, size_t size, int strict);  /*proto*/\n\ntypedef struct {\n    int code_line;\n    PyCodeObject* code_object;\n} __Pyx_CodeObjectCacheEntry;\nstruct __Pyx_CodeObjectCache {\n    int count;\n    int max_count;\n    __Pyx_CodeObjectCacheEntry* entries;\n};\nstatic struct __Pyx_CodeObjectCache __pyx_code_cache = {0,0,NULL};\nstatic int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line);\nstatic PyCodeObject *__pyx_find_code_object(int code_line);\nstatic void __pyx_insert_code_object(int code_line, PyCodeObject* code_object);\n\nstatic void __Pyx_AddTraceback(const char *funcname, int c_line,\n                               int py_line, const char *filename); /*proto*/\n\nstatic int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/\n\n\n/* Module declarations from 'cpython.buffer' */\n\n/* Module declarations from 'cpython.ref' */\n\n/* Module declarations from 'libc.string' */\n\n/* Module declarations from 'libc.stdio' */\n\n/* Module declarations from 'cpython.object' */\n\n/* Module declarations from '__builtin__' */\n\n/* Module declarations from 'cpython.type' */\nstatic PyTypeObject *__pyx_ptype_7cpython_4type_type = 0;\n\n/* Module declarations from 'libc.stdlib' */\n\n/* Module declarations from 'numpy' */\n\n/* Module declarations from 'numpy' */\nstatic PyTypeObject *__pyx_ptype_5numpy_dtype = 0;\nstatic PyTypeObject *__pyx_ptype_5numpy_flatiter = 0;\nstatic PyTypeObject *__pyx_ptype_5numpy_broadcast = 0;\nstatic PyTypeObject *__pyx_ptype_5numpy_ndarray = 0;\nstatic PyTypeObject *__pyx_ptype_5numpy_ufunc = 0;\nstatic CYTHON_INLINE char *__pyx_f_5numpy__util_dtypestring(PyArray_Descr *, char *, char *, int *); /*proto*/\n\n/* Module declarations from 'nms.gpu_nms' */\nstatic __Pyx_TypeInfo __Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t = { \"float32_t\", NULL, sizeof(__pyx_t_5numpy_float32_t), { 0 }, 0, 'R', 0, 0 };\nstatic __Pyx_TypeInfo __Pyx_TypeInfo_nn___pyx_t_5numpy_int32_t = { \"int32_t\", NULL, sizeof(__pyx_t_5numpy_int32_t), { 0 }, 0, IS_UNSIGNED(__pyx_t_5numpy_int32_t) ? 'U' : 'I', IS_UNSIGNED(__pyx_t_5numpy_int32_t), 0 };\nstatic __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 };\n#define __Pyx_MODULE_NAME \"nms.gpu_nms\"\nint __pyx_module_is_main_nms__gpu_nms = 0;\n\n/* Implementation of 'nms.gpu_nms' */\nstatic PyObject *__pyx_builtin_ValueError;\nstatic PyObject *__pyx_builtin_range;\nstatic PyObject *__pyx_builtin_RuntimeError;\nstatic PyObject *__pyx_pf_3nms_7gpu_nms_gpu_nms(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_dets, PyObject *__pyx_v_thresh, __pyx_t_5numpy_int32_t __pyx_v_device_id); /* proto */\nstatic int __pyx_pf_5numpy_7ndarray___getbuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags); /* proto */\nstatic void __pyx_pf_5numpy_7ndarray_2__releasebuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info); /* proto */\nstatic char __pyx_k_B[] = \"B\";\nstatic char __pyx_k_H[] = \"H\";\nstatic char __pyx_k_I[] = \"I\";\nstatic char __pyx_k_L[] = \"L\";\nstatic char __pyx_k_O[] = \"O\";\nstatic char __pyx_k_Q[] = \"Q\";\nstatic char __pyx_k_b[] = \"b\";\nstatic char __pyx_k_d[] = \"d\";\nstatic char __pyx_k_f[] = \"f\";\nstatic char __pyx_k_g[] = \"g\";\nstatic char __pyx_k_h[] = \"h\";\nstatic char __pyx_k_i[] = \"i\";\nstatic char __pyx_k_l[] = \"l\";\nstatic char __pyx_k_q[] = \"q\";\nstatic char __pyx_k_Zd[] = \"Zd\";\nstatic char __pyx_k_Zf[] = \"Zf\";\nstatic char __pyx_k_Zg[] = \"Zg\";\nstatic char __pyx_k_np[] = \"np\";\nstatic char __pyx_k_dets[] = \"dets\";\nstatic char __pyx_k_keep[] = \"keep\";\nstatic char __pyx_k_main[] = \"__main__\";\nstatic char __pyx_k_test[] = \"__test__\";\nstatic char __pyx_k_dtype[] = \"dtype\";\nstatic char __pyx_k_int32[] = \"int32\";\nstatic char __pyx_k_numpy[] = \"numpy\";\nstatic char __pyx_k_order[] = \"order\";\nstatic char __pyx_k_range[] = \"range\";\nstatic char __pyx_k_zeros[] = \"zeros\";\nstatic char __pyx_k_import[] = \"__import__\";\nstatic char __pyx_k_scores[] = \"scores\";\nstatic char __pyx_k_thresh[] = \"thresh\";\nstatic char __pyx_k_argsort[] = \"argsort\";\nstatic char __pyx_k_gpu_nms[] = \"gpu_nms\";\nstatic char __pyx_k_num_out[] = \"num_out\";\nstatic char __pyx_k_boxes_dim[] = \"boxes_dim\";\nstatic char __pyx_k_boxes_num[] = \"boxes_num\";\nstatic char __pyx_k_device_id[] = \"device_id\";\nstatic char __pyx_k_ValueError[] = \"ValueError\";\nstatic char __pyx_k_nms_gpu_nms[] = \"nms.gpu_nms\";\nstatic char __pyx_k_sorted_dets[] = \"sorted_dets\";\nstatic char __pyx_k_RuntimeError[] = \"RuntimeError\";\nstatic char __pyx_k_pyx_getbuffer[] = \"__pyx_getbuffer\";\nstatic char __pyx_k_pyx_releasebuffer[] = \"__pyx_releasebuffer\";\nstatic char __pyx_k_ndarray_is_not_C_contiguous[] = \"ndarray is not C contiguous\";\nstatic char __pyx_k_nfs_yoda_xinleic_Inf_Code_Faste[] = \"/nfs.yoda/xinleic/Inf/Code/Faster-RCNN_TF/lib/nms/gpu_nms.pyx\";\nstatic char __pyx_k_unknown_dtype_code_in_numpy_pxd[] = \"unknown dtype code in numpy.pxd (%d)\";\nstatic char __pyx_k_Format_string_allocated_too_shor[] = \"Format string allocated too short, see comment in numpy.pxd\";\nstatic char __pyx_k_Non_native_byte_order_not_suppor[] = \"Non-native byte order not supported\";\nstatic char __pyx_k_ndarray_is_not_Fortran_contiguou[] = \"ndarray is not Fortran contiguous\";\nstatic char __pyx_k_Format_string_allocated_too_shor_2[] = \"Format string allocated too short.\";\nstatic PyObject *__pyx_kp_u_Format_string_allocated_too_shor;\nstatic PyObject *__pyx_kp_u_Format_string_allocated_too_shor_2;\nstatic PyObject *__pyx_kp_u_Non_native_byte_order_not_suppor;\nstatic PyObject *__pyx_n_s_RuntimeError;\nstatic PyObject *__pyx_n_s_ValueError;\nstatic PyObject *__pyx_n_s_argsort;\nstatic PyObject *__pyx_n_s_boxes_dim;\nstatic PyObject *__pyx_n_s_boxes_num;\nstatic PyObject *__pyx_n_s_dets;\nstatic PyObject *__pyx_n_s_device_id;\nstatic PyObject *__pyx_n_s_dtype;\nstatic PyObject *__pyx_n_s_gpu_nms;\nstatic PyObject *__pyx_n_s_import;\nstatic PyObject *__pyx_n_s_int32;\nstatic PyObject *__pyx_n_s_keep;\nstatic PyObject *__pyx_n_s_main;\nstatic PyObject *__pyx_kp_u_ndarray_is_not_C_contiguous;\nstatic PyObject *__pyx_kp_u_ndarray_is_not_Fortran_contiguou;\nstatic PyObject *__pyx_kp_s_nfs_yoda_xinleic_Inf_Code_Faste;\nstatic PyObject *__pyx_n_s_nms_gpu_nms;\nstatic PyObject *__pyx_n_s_np;\nstatic PyObject *__pyx_n_s_num_out;\nstatic PyObject *__pyx_n_s_numpy;\nstatic PyObject *__pyx_n_s_order;\nstatic PyObject *__pyx_n_s_pyx_getbuffer;\nstatic PyObject *__pyx_n_s_pyx_releasebuffer;\nstatic PyObject *__pyx_n_s_range;\nstatic PyObject *__pyx_n_s_scores;\nstatic PyObject *__pyx_n_s_sorted_dets;\nstatic PyObject *__pyx_n_s_test;\nstatic PyObject *__pyx_n_s_thresh;\nstatic PyObject *__pyx_kp_u_unknown_dtype_code_in_numpy_pxd;\nstatic PyObject *__pyx_n_s_zeros;\nstatic PyObject *__pyx_int_4;\nstatic PyObject *__pyx_int_neg_1;\nstatic PyObject *__pyx_slice_;\nstatic PyObject *__pyx_slice__3;\nstatic PyObject *__pyx_slice__4;\nstatic PyObject *__pyx_tuple__2;\nstatic PyObject *__pyx_tuple__5;\nstatic PyObject *__pyx_tuple__6;\nstatic PyObject *__pyx_tuple__7;\nstatic PyObject *__pyx_tuple__8;\nstatic PyObject *__pyx_tuple__9;\nstatic PyObject *__pyx_tuple__10;\nstatic PyObject *__pyx_tuple__11;\nstatic PyObject *__pyx_codeobj__12;\n\n/* \"nms/gpu_nms.pyx\":16\n *     void _nms(np.int32_t*, int*, np.float32_t*, int, int, float, int)\n * \n * def gpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh,             # <<<<<<<<<<<<<<\n *             np.int32_t device_id=0):\n *     cdef int boxes_num = dets.shape[0]\n */\n\n/* Python wrapper */\nstatic PyObject *__pyx_pw_3nms_7gpu_nms_1gpu_nms(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\nstatic PyMethodDef __pyx_mdef_3nms_7gpu_nms_1gpu_nms = {__Pyx_NAMESTR(\"gpu_nms\"), (PyCFunction)__pyx_pw_3nms_7gpu_nms_1gpu_nms, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)};\nstatic PyObject *__pyx_pw_3nms_7gpu_nms_1gpu_nms(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n  PyArrayObject *__pyx_v_dets = 0;\n  PyObject *__pyx_v_thresh = 0;\n  __pyx_t_5numpy_int32_t __pyx_v_device_id;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  PyObject *__pyx_r = 0;\n  __Pyx_RefNannyDeclarations\n  __Pyx_RefNannySetupContext(\"gpu_nms (wrapper)\", 0);\n  {\n    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_dets,&__pyx_n_s_thresh,&__pyx_n_s_device_id,0};\n    PyObject* values[3] = {0,0,0};\n    if (unlikely(__pyx_kwds)) {\n      Py_ssize_t kw_args;\n      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n      switch (pos_args) {\n        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n        case  0: break;\n        default: goto __pyx_L5_argtuple_error;\n      }\n      kw_args = PyDict_Size(__pyx_kwds);\n      switch (pos_args) {\n        case  0:\n        if (likely((values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_dets)) != 0)) kw_args--;\n        else goto __pyx_L5_argtuple_error;\n        case  1:\n        if (likely((values[1] = PyDict_GetItem(__pyx_kwds, __pyx_n_s_thresh)) != 0)) kw_args--;\n        else {\n          __Pyx_RaiseArgtupleInvalid(\"gpu_nms\", 0, 2, 3, 1); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n        }\n        case  2:\n        if (kw_args > 0) {\n          PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s_device_id);\n          if (value) { values[2] = value; kw_args--; }\n        }\n      }\n      if (unlikely(kw_args > 0)) {\n        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"gpu_nms\") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n      }\n    } else {\n      switch (PyTuple_GET_SIZE(__pyx_args)) {\n        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n        values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n        break;\n        default: goto __pyx_L5_argtuple_error;\n      }\n    }\n    __pyx_v_dets = ((PyArrayObject *)values[0]);\n    __pyx_v_thresh = ((PyObject*)values[1]);\n    if (values[2]) {\n      __pyx_v_device_id = __Pyx_PyInt_As_npy_int32(values[2]); if (unlikely((__pyx_v_device_id == (npy_int32)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n    } else {\n      __pyx_v_device_id = ((__pyx_t_5numpy_int32_t)0);\n    }\n  }\n  goto __pyx_L4_argument_unpacking_done;\n  __pyx_L5_argtuple_error:;\n  __Pyx_RaiseArgtupleInvalid(\"gpu_nms\", 0, 2, 3, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L3_error;}\n  __pyx_L3_error:;\n  __Pyx_AddTraceback(\"nms.gpu_nms.gpu_nms\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __Pyx_RefNannyFinishContext();\n  return NULL;\n  __pyx_L4_argument_unpacking_done:;\n  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_dets), __pyx_ptype_5numpy_ndarray, 1, \"dets\", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_thresh), (&PyFloat_Type), 1, \"thresh\", 1))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_r = __pyx_pf_3nms_7gpu_nms_gpu_nms(__pyx_self, __pyx_v_dets, __pyx_v_thresh, __pyx_v_device_id);\n\n  /* function exit code */\n  goto __pyx_L0;\n  __pyx_L1_error:;\n  __pyx_r = NULL;\n  __pyx_L0:;\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\nstatic PyObject *__pyx_pf_3nms_7gpu_nms_gpu_nms(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_dets, PyObject *__pyx_v_thresh, __pyx_t_5numpy_int32_t __pyx_v_device_id) {\n  int __pyx_v_boxes_num;\n  int __pyx_v_boxes_dim;\n  int __pyx_v_num_out;\n  PyArrayObject *__pyx_v_keep = 0;\n  PyArrayObject *__pyx_v_scores = 0;\n  PyArrayObject *__pyx_v_order = 0;\n  PyArrayObject *__pyx_v_sorted_dets = 0;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_dets;\n  __Pyx_Buffer __pyx_pybuffer_dets;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_keep;\n  __Pyx_Buffer __pyx_pybuffer_keep;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_order;\n  __Pyx_Buffer __pyx_pybuffer_order;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_scores;\n  __Pyx_Buffer __pyx_pybuffer_scores;\n  __Pyx_LocalBuf_ND __pyx_pybuffernd_sorted_dets;\n  __Pyx_Buffer __pyx_pybuffer_sorted_dets;\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  PyObject *__pyx_t_2 = NULL;\n  PyObject *__pyx_t_3 = NULL;\n  PyObject *__pyx_t_4 = NULL;\n  PyObject *__pyx_t_5 = NULL;\n  PyArrayObject *__pyx_t_6 = NULL;\n  PyArrayObject *__pyx_t_7 = NULL;\n  PyArrayObject *__pyx_t_8 = NULL;\n  PyArrayObject *__pyx_t_9 = NULL;\n  long __pyx_t_10;\n  int __pyx_t_11;\n  long __pyx_t_12;\n  long __pyx_t_13;\n  float __pyx_t_14;\n  PyObject *__pyx_t_15 = NULL;\n  PyObject *__pyx_t_16 = NULL;\n  PyObject *__pyx_t_17 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"gpu_nms\", 0);\n  __pyx_pybuffer_keep.pybuffer.buf = NULL;\n  __pyx_pybuffer_keep.refcount = 0;\n  __pyx_pybuffernd_keep.data = NULL;\n  __pyx_pybuffernd_keep.rcbuffer = &__pyx_pybuffer_keep;\n  __pyx_pybuffer_scores.pybuffer.buf = NULL;\n  __pyx_pybuffer_scores.refcount = 0;\n  __pyx_pybuffernd_scores.data = NULL;\n  __pyx_pybuffernd_scores.rcbuffer = &__pyx_pybuffer_scores;\n  __pyx_pybuffer_order.pybuffer.buf = NULL;\n  __pyx_pybuffer_order.refcount = 0;\n  __pyx_pybuffernd_order.data = NULL;\n  __pyx_pybuffernd_order.rcbuffer = &__pyx_pybuffer_order;\n  __pyx_pybuffer_sorted_dets.pybuffer.buf = NULL;\n  __pyx_pybuffer_sorted_dets.refcount = 0;\n  __pyx_pybuffernd_sorted_dets.data = NULL;\n  __pyx_pybuffernd_sorted_dets.rcbuffer = &__pyx_pybuffer_sorted_dets;\n  __pyx_pybuffer_dets.pybuffer.buf = NULL;\n  __pyx_pybuffer_dets.refcount = 0;\n  __pyx_pybuffernd_dets.data = NULL;\n  __pyx_pybuffernd_dets.rcbuffer = &__pyx_pybuffer_dets;\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    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_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  }\n  __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];\n\n  /* \"nms/gpu_nms.pyx\":18\n * def gpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh,\n *             np.int32_t device_id=0):\n *     cdef int boxes_num = dets.shape[0]             # <<<<<<<<<<<<<<\n *     cdef int boxes_dim = dets.shape[1]\n *     cdef int num_out\n */\n  __pyx_v_boxes_num = (__pyx_v_dets->dimensions[0]);\n\n  /* \"nms/gpu_nms.pyx\":19\n *             np.int32_t device_id=0):\n *     cdef int boxes_num = dets.shape[0]\n *     cdef int boxes_dim = dets.shape[1]             # <<<<<<<<<<<<<<\n *     cdef int num_out\n *     cdef np.ndarray[np.int32_t, ndim=1] \\\n */\n  __pyx_v_boxes_dim = (__pyx_v_dets->dimensions[1]);\n\n  /* \"nms/gpu_nms.pyx\":22\n *     cdef int num_out\n *     cdef np.ndarray[np.int32_t, ndim=1] \\\n *         keep = np.zeros(boxes_num, dtype=np.int32)             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=1] \\\n *         scores = dets[:, 4]\n */\n  __pyx_t_1 = __Pyx_GetModuleGlobalName(__pyx_n_s_np); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_t_1, __pyx_n_s_zeros); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_2);\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n  __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_boxes_num); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_3);\n  PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_1);\n  __Pyx_GIVEREF(__pyx_t_1);\n  __pyx_t_1 = 0;\n  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_t_4 = __Pyx_GetModuleGlobalName(__pyx_n_s_np); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_4);\n  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(__pyx_t_4, __pyx_n_s_int32); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_5);\n  __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n  if (PyDict_SetItem(__pyx_t_1, __pyx_n_s_dtype, __pyx_t_5) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n  __pyx_t_5 = __Pyx_PyObject_Call(__pyx_t_2, __pyx_t_3, __pyx_t_1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_5);\n  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n  if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_6 = ((PyArrayObject *)__pyx_t_5);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_keep.rcbuffer->pybuffer, (PyObject*)__pyx_t_6, &__Pyx_TypeInfo_nn___pyx_t_5numpy_int32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {\n      __pyx_v_keep = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_keep.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } else {__pyx_pybuffernd_keep.diminfo[0].strides = __pyx_pybuffernd_keep.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_keep.diminfo[0].shape = __pyx_pybuffernd_keep.rcbuffer->pybuffer.shape[0];\n    }\n  }\n  __pyx_t_6 = 0;\n  __pyx_v_keep = ((PyArrayObject *)__pyx_t_5);\n  __pyx_t_5 = 0;\n\n  /* \"nms/gpu_nms.pyx\":24\n *         keep = np.zeros(boxes_num, dtype=np.int32)\n *     cdef np.ndarray[np.float32_t, ndim=1] \\\n *         scores = dets[:, 4]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.int_t, ndim=1] \\\n *         order = scores.argsort()[::-1]\n */\n  __pyx_t_5 = PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_tuple__2); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __Pyx_GOTREF(__pyx_t_5);\n  if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_7 = ((PyArrayObject *)__pyx_t_5);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_scores.rcbuffer->pybuffer, (PyObject*)__pyx_t_7, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {\n      __pyx_v_scores = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_scores.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 23; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } 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];\n    }\n  }\n  __pyx_t_7 = 0;\n  __pyx_v_scores = ((PyArrayObject *)__pyx_t_5);\n  __pyx_t_5 = 0;\n\n  /* \"nms/gpu_nms.pyx\":26\n *         scores = dets[:, 4]\n *     cdef np.ndarray[np.int_t, ndim=1] \\\n *         order = scores.argsort()[::-1]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=2] \\\n *         sorted_dets = dets[order, :]\n */\n  __pyx_t_5 = __Pyx_PyObject_GetAttrStr(((PyObject *)__pyx_v_scores), __pyx_n_s_argsort); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 26; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_5);\n  __pyx_t_1 = __Pyx_PyObject_Call(__pyx_t_5, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 26; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n  __pyx_t_5 = PyObject_GetItem(__pyx_t_1, __pyx_slice__3); if (unlikely(__pyx_t_5 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 26; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __Pyx_GOTREF(__pyx_t_5);\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n  if (!(likely(((__pyx_t_5) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_5, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 26; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_8 = ((PyArrayObject *)__pyx_t_5);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_order.rcbuffer->pybuffer, (PyObject*)__pyx_t_8, &__Pyx_TypeInfo_nn___pyx_t_5numpy_int_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {\n      __pyx_v_order = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_order.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } 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];\n    }\n  }\n  __pyx_t_8 = 0;\n  __pyx_v_order = ((PyArrayObject *)__pyx_t_5);\n  __pyx_t_5 = 0;\n\n  /* \"nms/gpu_nms.pyx\":28\n *         order = scores.argsort()[::-1]\n *     cdef np.ndarray[np.float32_t, ndim=2] \\\n *         sorted_dets = dets[order, :]             # <<<<<<<<<<<<<<\n *     _nms(&keep[0], &num_out, &sorted_dets[0, 0], boxes_num, boxes_dim, thresh, device_id)\n *     keep = keep[:num_out]\n */\n  __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_5);\n  __Pyx_INCREF(((PyObject *)__pyx_v_order));\n  PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_v_order));\n  __Pyx_GIVEREF(((PyObject *)__pyx_v_order));\n  __Pyx_INCREF(__pyx_slice__4);\n  PyTuple_SET_ITEM(__pyx_t_5, 1, __pyx_slice__4);\n  __Pyx_GIVEREF(__pyx_slice__4);\n  __pyx_t_1 = PyObject_GetItem(((PyObject *)__pyx_v_dets), __pyx_t_5); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __Pyx_GOTREF(__pyx_t_1);\n  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_9 = ((PyArrayObject *)__pyx_t_1);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_sorted_dets.rcbuffer->pybuffer, (PyObject*)__pyx_t_9, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float32_t, PyBUF_FORMAT| PyBUF_STRIDES, 2, 0, __pyx_stack) == -1)) {\n      __pyx_v_sorted_dets = ((PyArrayObject *)Py_None); __Pyx_INCREF(Py_None); __pyx_pybuffernd_sorted_dets.rcbuffer->pybuffer.buf = NULL;\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 27; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    } else {__pyx_pybuffernd_sorted_dets.diminfo[0].strides = __pyx_pybuffernd_sorted_dets.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_sorted_dets.diminfo[0].shape = __pyx_pybuffernd_sorted_dets.rcbuffer->pybuffer.shape[0]; __pyx_pybuffernd_sorted_dets.diminfo[1].strides = __pyx_pybuffernd_sorted_dets.rcbuffer->pybuffer.strides[1]; __pyx_pybuffernd_sorted_dets.diminfo[1].shape = __pyx_pybuffernd_sorted_dets.rcbuffer->pybuffer.shape[1];\n    }\n  }\n  __pyx_t_9 = 0;\n  __pyx_v_sorted_dets = ((PyArrayObject *)__pyx_t_1);\n  __pyx_t_1 = 0;\n\n  /* \"nms/gpu_nms.pyx\":29\n *     cdef np.ndarray[np.float32_t, ndim=2] \\\n *         sorted_dets = dets[order, :]\n *     _nms(&keep[0], &num_out, &sorted_dets[0, 0], boxes_num, boxes_dim, thresh, device_id)             # <<<<<<<<<<<<<<\n *     keep = keep[:num_out]\n *     return list(order[keep])\n */\n  __pyx_t_10 = 0;\n  __pyx_t_11 = -1;\n  if (__pyx_t_10 < 0) {\n    __pyx_t_10 += __pyx_pybuffernd_keep.diminfo[0].shape;\n    if (unlikely(__pyx_t_10 < 0)) __pyx_t_11 = 0;\n  } else if (unlikely(__pyx_t_10 >= __pyx_pybuffernd_keep.diminfo[0].shape)) __pyx_t_11 = 0;\n  if (unlikely(__pyx_t_11 != -1)) {\n    __Pyx_RaiseBufferIndexError(__pyx_t_11);\n    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  }\n  __pyx_t_12 = 0;\n  __pyx_t_13 = 0;\n  __pyx_t_11 = -1;\n  if (__pyx_t_12 < 0) {\n    __pyx_t_12 += __pyx_pybuffernd_sorted_dets.diminfo[0].shape;\n    if (unlikely(__pyx_t_12 < 0)) __pyx_t_11 = 0;\n  } else if (unlikely(__pyx_t_12 >= __pyx_pybuffernd_sorted_dets.diminfo[0].shape)) __pyx_t_11 = 0;\n  if (__pyx_t_13 < 0) {\n    __pyx_t_13 += __pyx_pybuffernd_sorted_dets.diminfo[1].shape;\n    if (unlikely(__pyx_t_13 < 0)) __pyx_t_11 = 1;\n  } else if (unlikely(__pyx_t_13 >= __pyx_pybuffernd_sorted_dets.diminfo[1].shape)) __pyx_t_11 = 1;\n  if (unlikely(__pyx_t_11 != -1)) {\n    __Pyx_RaiseBufferIndexError(__pyx_t_11);\n    {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  }\n  __pyx_t_14 = __pyx_PyFloat_AsFloat(__pyx_v_thresh); if (unlikely((__pyx_t_14 == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  _nms((&(*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_int32_t *, __pyx_pybuffernd_keep.rcbuffer->pybuffer.buf, __pyx_t_10, __pyx_pybuffernd_keep.diminfo[0].strides))), (&__pyx_v_num_out), (&(*__Pyx_BufPtrStrided2d(__pyx_t_5numpy_float32_t *, __pyx_pybuffernd_sorted_dets.rcbuffer->pybuffer.buf, __pyx_t_12, __pyx_pybuffernd_sorted_dets.diminfo[0].strides, __pyx_t_13, __pyx_pybuffernd_sorted_dets.diminfo[1].strides))), __pyx_v_boxes_num, __pyx_v_boxes_dim, __pyx_t_14, __pyx_v_device_id);\n\n  /* \"nms/gpu_nms.pyx\":30\n *         sorted_dets = dets[order, :]\n *     _nms(&keep[0], &num_out, &sorted_dets[0, 0], boxes_num, boxes_dim, thresh, device_id)\n *     keep = keep[:num_out]             # <<<<<<<<<<<<<<\n *     return list(order[keep])\n */\n  __pyx_t_1 = __Pyx_PyObject_GetSlice(((PyObject *)__pyx_v_keep), 0, __pyx_v_num_out, NULL, NULL, NULL, 0, 1, 1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  if (!(likely(((__pyx_t_1) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_1, __pyx_ptype_5numpy_ndarray))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_t_6 = ((PyArrayObject *)__pyx_t_1);\n  {\n    __Pyx_BufFmt_StackElem __pyx_stack[1];\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_keep.rcbuffer->pybuffer);\n    __pyx_t_11 = __Pyx_GetBufferAndValidate(&__pyx_pybuffernd_keep.rcbuffer->pybuffer, (PyObject*)__pyx_t_6, &__Pyx_TypeInfo_nn___pyx_t_5numpy_int32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack);\n    if (unlikely(__pyx_t_11 < 0)) {\n      PyErr_Fetch(&__pyx_t_15, &__pyx_t_16, &__pyx_t_17);\n      if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_keep.rcbuffer->pybuffer, (PyObject*)__pyx_v_keep, &__Pyx_TypeInfo_nn___pyx_t_5numpy_int32_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) {\n        Py_XDECREF(__pyx_t_15); Py_XDECREF(__pyx_t_16); Py_XDECREF(__pyx_t_17);\n        __Pyx_RaiseBufferFallbackError();\n      } else {\n        PyErr_Restore(__pyx_t_15, __pyx_t_16, __pyx_t_17);\n      }\n    }\n    __pyx_pybuffernd_keep.diminfo[0].strides = __pyx_pybuffernd_keep.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_keep.diminfo[0].shape = __pyx_pybuffernd_keep.rcbuffer->pybuffer.shape[0];\n    if (unlikely(__pyx_t_11 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  }\n  __pyx_t_6 = 0;\n  __Pyx_DECREF_SET(__pyx_v_keep, ((PyArrayObject *)__pyx_t_1));\n  __pyx_t_1 = 0;\n\n  /* \"nms/gpu_nms.pyx\":31\n *     _nms(&keep[0], &num_out, &sorted_dets[0, 0], boxes_num, boxes_dim, thresh, device_id)\n *     keep = keep[:num_out]\n *     return list(order[keep])             # <<<<<<<<<<<<<<\n */\n  __Pyx_XDECREF(__pyx_r);\n  __pyx_t_1 = PyObject_GetItem(((PyObject *)__pyx_v_order), ((PyObject *)__pyx_v_keep)); if (unlikely(__pyx_t_1 == NULL)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_t_5 = PyTuple_New(1); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_5);\n  PyTuple_SET_ITEM(__pyx_t_5, 0, __pyx_t_1);\n  __Pyx_GIVEREF(__pyx_t_1);\n  __pyx_t_1 = 0;\n  __pyx_t_1 = __Pyx_PyObject_Call(((PyObject *)((PyObject*)(&PyList_Type))), __pyx_t_5, NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n  __pyx_r = __pyx_t_1;\n  __pyx_t_1 = 0;\n  goto __pyx_L0;\n\n  /* \"nms/gpu_nms.pyx\":16\n *     void _nms(np.int32_t*, int*, np.float32_t*, int, int, float, int)\n * \n * def gpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh,             # <<<<<<<<<<<<<<\n *             np.int32_t device_id=0):\n *     cdef int boxes_num = dets.shape[0]\n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_XDECREF(__pyx_t_2);\n  __Pyx_XDECREF(__pyx_t_3);\n  __Pyx_XDECREF(__pyx_t_4);\n  __Pyx_XDECREF(__pyx_t_5);\n  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;\n    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_dets.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_keep.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_order.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_scores.rcbuffer->pybuffer);\n    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_sorted_dets.rcbuffer->pybuffer);\n  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}\n  __Pyx_AddTraceback(\"nms.gpu_nms.gpu_nms\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = NULL;\n  goto __pyx_L2;\n  __pyx_L0:;\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_dets.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_keep.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_order.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_scores.rcbuffer->pybuffer);\n  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_sorted_dets.rcbuffer->pybuffer);\n  __pyx_L2:;\n  __Pyx_XDECREF((PyObject *)__pyx_v_keep);\n  __Pyx_XDECREF((PyObject *)__pyx_v_scores);\n  __Pyx_XDECREF((PyObject *)__pyx_v_order);\n  __Pyx_XDECREF((PyObject *)__pyx_v_sorted_dets);\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":194\n *         # experimental exception made for __getbuffer__ and __releasebuffer__\n *         # -- the details of this may change.\n *         def __getbuffer__(ndarray self, Py_buffer* info, int flags):             # <<<<<<<<<<<<<<\n *             # This implementation of getbuffer is geared towards Cython\n *             # requirements, and does not yet fullfill the PEP.\n */\n\n/* Python wrapper */\nstatic CYTHON_UNUSED int __pyx_pw_5numpy_7ndarray_1__getbuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags); /*proto*/\nstatic CYTHON_UNUSED int __pyx_pw_5numpy_7ndarray_1__getbuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags) {\n  int __pyx_r;\n  __Pyx_RefNannyDeclarations\n  __Pyx_RefNannySetupContext(\"__getbuffer__ (wrapper)\", 0);\n  __pyx_r = __pyx_pf_5numpy_7ndarray___getbuffer__(((PyArrayObject *)__pyx_v_self), ((Py_buffer *)__pyx_v_info), ((int)__pyx_v_flags));\n\n  /* function exit code */\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\nstatic int __pyx_pf_5numpy_7ndarray___getbuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info, int __pyx_v_flags) {\n  int __pyx_v_copy_shape;\n  int __pyx_v_i;\n  int __pyx_v_ndim;\n  int __pyx_v_endian_detector;\n  int __pyx_v_little_endian;\n  int __pyx_v_t;\n  char *__pyx_v_f;\n  PyArray_Descr *__pyx_v_descr = 0;\n  int __pyx_v_offset;\n  int __pyx_v_hasfields;\n  int __pyx_r;\n  __Pyx_RefNannyDeclarations\n  int __pyx_t_1;\n  int __pyx_t_2;\n  int __pyx_t_3;\n  PyObject *__pyx_t_4 = NULL;\n  int __pyx_t_5;\n  int __pyx_t_6;\n  int __pyx_t_7;\n  PyObject *__pyx_t_8 = NULL;\n  char *__pyx_t_9;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"__getbuffer__\", 0);\n  if (__pyx_v_info != NULL) {\n    __pyx_v_info->obj = Py_None; __Pyx_INCREF(Py_None);\n    __Pyx_GIVEREF(__pyx_v_info->obj);\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":200\n *             # of flags\n * \n *             if info == NULL: return             # <<<<<<<<<<<<<<\n * \n *             cdef int copy_shape, i, ndim\n */\n  __pyx_t_1 = ((__pyx_v_info == NULL) != 0);\n  if (__pyx_t_1) {\n    __pyx_r = 0;\n    goto __pyx_L0;\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":203\n * \n *             cdef int copy_shape, i, ndim\n *             cdef int endian_detector = 1             # <<<<<<<<<<<<<<\n *             cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)\n * \n */\n  __pyx_v_endian_detector = 1;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":204\n *             cdef int copy_shape, i, ndim\n *             cdef int endian_detector = 1\n *             cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)             # <<<<<<<<<<<<<<\n * \n *             ndim = PyArray_NDIM(self)\n */\n  __pyx_v_little_endian = ((((char *)(&__pyx_v_endian_detector))[0]) != 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":206\n *             cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)\n * \n *             ndim = PyArray_NDIM(self)             # <<<<<<<<<<<<<<\n * \n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):\n */\n  __pyx_v_ndim = PyArray_NDIM(__pyx_v_self);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":208\n *             ndim = PyArray_NDIM(self)\n * \n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):             # <<<<<<<<<<<<<<\n *                 copy_shape = 1\n *             else:\n */\n  __pyx_t_1 = (((sizeof(npy_intp)) != (sizeof(Py_ssize_t))) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":209\n * \n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):\n *                 copy_shape = 1             # <<<<<<<<<<<<<<\n *             else:\n *                 copy_shape = 0\n */\n    __pyx_v_copy_shape = 1;\n    goto __pyx_L4;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":211\n *                 copy_shape = 1\n *             else:\n *                 copy_shape = 0             # <<<<<<<<<<<<<<\n * \n *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)\n */\n    __pyx_v_copy_shape = 0;\n  }\n  __pyx_L4:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":213\n *                 copy_shape = 0\n * \n *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)             # <<<<<<<<<<<<<<\n *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not C contiguous\")\n */\n  __pyx_t_1 = (((__pyx_v_flags & PyBUF_C_CONTIGUOUS) == PyBUF_C_CONTIGUOUS) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":214\n * \n *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):             # <<<<<<<<<<<<<<\n *                 raise ValueError(u\"ndarray is not C contiguous\")\n * \n */\n    __pyx_t_2 = ((!(PyArray_CHKFLAGS(__pyx_v_self, NPY_C_CONTIGUOUS) != 0)) != 0);\n    __pyx_t_3 = __pyx_t_2;\n  } else {\n    __pyx_t_3 = __pyx_t_1;\n  }\n  if (__pyx_t_3) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":215\n *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not C contiguous\")             # <<<<<<<<<<<<<<\n * \n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)\n */\n    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__5, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_GOTREF(__pyx_t_4);\n    __Pyx_Raise(__pyx_t_4, 0, 0, 0);\n    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n    {__pyx_filename = __pyx_f[1]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":217\n *                 raise ValueError(u\"ndarray is not C contiguous\")\n * \n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)             # <<<<<<<<<<<<<<\n *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not Fortran contiguous\")\n */\n  __pyx_t_3 = (((__pyx_v_flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS) != 0);\n  if (__pyx_t_3) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":218\n * \n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):             # <<<<<<<<<<<<<<\n *                 raise ValueError(u\"ndarray is not Fortran contiguous\")\n * \n */\n    __pyx_t_1 = ((!(PyArray_CHKFLAGS(__pyx_v_self, NPY_F_CONTIGUOUS) != 0)) != 0);\n    __pyx_t_2 = __pyx_t_1;\n  } else {\n    __pyx_t_2 = __pyx_t_3;\n  }\n  if (__pyx_t_2) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":219\n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not Fortran contiguous\")             # <<<<<<<<<<<<<<\n * \n *             info.buf = PyArray_DATA(self)\n */\n    __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__6, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_GOTREF(__pyx_t_4);\n    __Pyx_Raise(__pyx_t_4, 0, 0, 0);\n    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n    {__pyx_filename = __pyx_f[1]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":221\n *                 raise ValueError(u\"ndarray is not Fortran contiguous\")\n * \n *             info.buf = PyArray_DATA(self)             # <<<<<<<<<<<<<<\n *             info.ndim = ndim\n *             if copy_shape:\n */\n  __pyx_v_info->buf = PyArray_DATA(__pyx_v_self);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":222\n * \n *             info.buf = PyArray_DATA(self)\n *             info.ndim = ndim             # <<<<<<<<<<<<<<\n *             if copy_shape:\n *                 # Allocate new buffer for strides and shape info.\n */\n  __pyx_v_info->ndim = __pyx_v_ndim;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":223\n *             info.buf = PyArray_DATA(self)\n *             info.ndim = ndim\n *             if copy_shape:             # <<<<<<<<<<<<<<\n *                 # Allocate new buffer for strides and shape info.\n *                 # This is allocated as one block, strides first.\n */\n  __pyx_t_2 = (__pyx_v_copy_shape != 0);\n  if (__pyx_t_2) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":226\n *                 # Allocate new buffer for strides and shape info.\n *                 # This is allocated as one block, strides first.\n *                 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * <size_t>ndim * 2)             # <<<<<<<<<<<<<<\n *                 info.shape = info.strides + ndim\n *                 for i in range(ndim):\n */\n    __pyx_v_info->strides = ((Py_ssize_t *)malloc((((sizeof(Py_ssize_t)) * ((size_t)__pyx_v_ndim)) * 2)));\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":227\n *                 # This is allocated as one block, strides first.\n *                 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * <size_t>ndim * 2)\n *                 info.shape = info.strides + ndim             # <<<<<<<<<<<<<<\n *                 for i in range(ndim):\n *                     info.strides[i] = PyArray_STRIDES(self)[i]\n */\n    __pyx_v_info->shape = (__pyx_v_info->strides + __pyx_v_ndim);\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":228\n *                 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * <size_t>ndim * 2)\n *                 info.shape = info.strides + ndim\n *                 for i in range(ndim):             # <<<<<<<<<<<<<<\n *                     info.strides[i] = PyArray_STRIDES(self)[i]\n *                     info.shape[i] = PyArray_DIMS(self)[i]\n */\n    __pyx_t_5 = __pyx_v_ndim;\n    for (__pyx_t_6 = 0; __pyx_t_6 < __pyx_t_5; __pyx_t_6+=1) {\n      __pyx_v_i = __pyx_t_6;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":229\n *                 info.shape = info.strides + ndim\n *                 for i in range(ndim):\n *                     info.strides[i] = PyArray_STRIDES(self)[i]             # <<<<<<<<<<<<<<\n *                     info.shape[i] = PyArray_DIMS(self)[i]\n *             else:\n */\n      (__pyx_v_info->strides[__pyx_v_i]) = (PyArray_STRIDES(__pyx_v_self)[__pyx_v_i]);\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":230\n *                 for i in range(ndim):\n *                     info.strides[i] = PyArray_STRIDES(self)[i]\n *                     info.shape[i] = PyArray_DIMS(self)[i]             # <<<<<<<<<<<<<<\n *             else:\n *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)\n */\n      (__pyx_v_info->shape[__pyx_v_i]) = (PyArray_DIMS(__pyx_v_self)[__pyx_v_i]);\n    }\n    goto __pyx_L7;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":232\n *                     info.shape[i] = PyArray_DIMS(self)[i]\n *             else:\n *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)             # <<<<<<<<<<<<<<\n *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)\n *             info.suboffsets = NULL\n */\n    __pyx_v_info->strides = ((Py_ssize_t *)PyArray_STRIDES(__pyx_v_self));\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":233\n *             else:\n *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)\n *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)             # <<<<<<<<<<<<<<\n *             info.suboffsets = NULL\n *             info.itemsize = PyArray_ITEMSIZE(self)\n */\n    __pyx_v_info->shape = ((Py_ssize_t *)PyArray_DIMS(__pyx_v_self));\n  }\n  __pyx_L7:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":234\n *                 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)\n *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)\n *             info.suboffsets = NULL             # <<<<<<<<<<<<<<\n *             info.itemsize = PyArray_ITEMSIZE(self)\n *             info.readonly = not PyArray_ISWRITEABLE(self)\n */\n  __pyx_v_info->suboffsets = NULL;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":235\n *                 info.shape = <Py_ssize_t*>PyArray_DIMS(self)\n *             info.suboffsets = NULL\n *             info.itemsize = PyArray_ITEMSIZE(self)             # <<<<<<<<<<<<<<\n *             info.readonly = not PyArray_ISWRITEABLE(self)\n * \n */\n  __pyx_v_info->itemsize = PyArray_ITEMSIZE(__pyx_v_self);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":236\n *             info.suboffsets = NULL\n *             info.itemsize = PyArray_ITEMSIZE(self)\n *             info.readonly = not PyArray_ISWRITEABLE(self)             # <<<<<<<<<<<<<<\n * \n *             cdef int t\n */\n  __pyx_v_info->readonly = (!(PyArray_ISWRITEABLE(__pyx_v_self) != 0));\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":239\n * \n *             cdef int t\n *             cdef char* f = NULL             # <<<<<<<<<<<<<<\n *             cdef dtype descr = self.descr\n *             cdef list stack\n */\n  __pyx_v_f = NULL;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":240\n *             cdef int t\n *             cdef char* f = NULL\n *             cdef dtype descr = self.descr             # <<<<<<<<<<<<<<\n *             cdef list stack\n *             cdef int offset\n */\n  __pyx_t_4 = ((PyObject *)__pyx_v_self->descr);\n  __Pyx_INCREF(__pyx_t_4);\n  __pyx_v_descr = ((PyArray_Descr *)__pyx_t_4);\n  __pyx_t_4 = 0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":244\n *             cdef int offset\n * \n *             cdef bint hasfields = PyDataType_HASFIELDS(descr)             # <<<<<<<<<<<<<<\n * \n *             if not hasfields and not copy_shape:\n */\n  __pyx_v_hasfields = PyDataType_HASFIELDS(__pyx_v_descr);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":246\n *             cdef bint hasfields = PyDataType_HASFIELDS(descr)\n * \n *             if not hasfields and not copy_shape:             # <<<<<<<<<<<<<<\n *                 # do not call releasebuffer\n *                 info.obj = None\n */\n  __pyx_t_2 = ((!(__pyx_v_hasfields != 0)) != 0);\n  if (__pyx_t_2) {\n    __pyx_t_3 = ((!(__pyx_v_copy_shape != 0)) != 0);\n    __pyx_t_1 = __pyx_t_3;\n  } else {\n    __pyx_t_1 = __pyx_t_2;\n  }\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":248\n *             if not hasfields and not copy_shape:\n *                 # do not call releasebuffer\n *                 info.obj = None             # <<<<<<<<<<<<<<\n *             else:\n *                 # need to call releasebuffer\n */\n    __Pyx_INCREF(Py_None);\n    __Pyx_GIVEREF(Py_None);\n    __Pyx_GOTREF(__pyx_v_info->obj);\n    __Pyx_DECREF(__pyx_v_info->obj);\n    __pyx_v_info->obj = Py_None;\n    goto __pyx_L10;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":251\n *             else:\n *                 # need to call releasebuffer\n *                 info.obj = self             # <<<<<<<<<<<<<<\n * \n *             if not hasfields:\n */\n    __Pyx_INCREF(((PyObject *)__pyx_v_self));\n    __Pyx_GIVEREF(((PyObject *)__pyx_v_self));\n    __Pyx_GOTREF(__pyx_v_info->obj);\n    __Pyx_DECREF(__pyx_v_info->obj);\n    __pyx_v_info->obj = ((PyObject *)__pyx_v_self);\n  }\n  __pyx_L10:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":253\n *                 info.obj = self\n * \n *             if not hasfields:             # <<<<<<<<<<<<<<\n *                 t = descr.type_num\n *                 if ((descr.byteorder == c'>' and little_endian) or\n */\n  __pyx_t_1 = ((!(__pyx_v_hasfields != 0)) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":254\n * \n *             if not hasfields:\n *                 t = descr.type_num             # <<<<<<<<<<<<<<\n *                 if ((descr.byteorder == c'>' and little_endian) or\n *                     (descr.byteorder == c'<' and not little_endian)):\n */\n    __pyx_t_5 = __pyx_v_descr->type_num;\n    __pyx_v_t = __pyx_t_5;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":255\n *             if not hasfields:\n *                 t = descr.type_num\n *                 if ((descr.byteorder == c'>' and little_endian) or             # <<<<<<<<<<<<<<\n *                     (descr.byteorder == c'<' and not little_endian)):\n *                     raise ValueError(u\"Non-native byte order not supported\")\n */\n    __pyx_t_1 = ((__pyx_v_descr->byteorder == '>') != 0);\n    if (__pyx_t_1) {\n      __pyx_t_2 = (__pyx_v_little_endian != 0);\n    } else {\n      __pyx_t_2 = __pyx_t_1;\n    }\n    if (!__pyx_t_2) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":256\n *                 t = descr.type_num\n *                 if ((descr.byteorder == c'>' and little_endian) or\n *                     (descr.byteorder == c'<' and not little_endian)):             # <<<<<<<<<<<<<<\n *                     raise ValueError(u\"Non-native byte order not supported\")\n *                 if   t == NPY_BYTE:        f = \"b\"\n */\n      __pyx_t_1 = ((__pyx_v_descr->byteorder == '<') != 0);\n      if (__pyx_t_1) {\n        __pyx_t_3 = ((!(__pyx_v_little_endian != 0)) != 0);\n        __pyx_t_7 = __pyx_t_3;\n      } else {\n        __pyx_t_7 = __pyx_t_1;\n      }\n      __pyx_t_1 = __pyx_t_7;\n    } else {\n      __pyx_t_1 = __pyx_t_2;\n    }\n    if (__pyx_t_1) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":257\n *                 if ((descr.byteorder == c'>' and little_endian) or\n *                     (descr.byteorder == c'<' and not little_endian)):\n *                     raise ValueError(u\"Non-native byte order not supported\")             # <<<<<<<<<<<<<<\n *                 if   t == NPY_BYTE:        f = \"b\"\n *                 elif t == NPY_UBYTE:       f = \"B\"\n */\n      __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__7, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __Pyx_Raise(__pyx_t_4, 0, 0, 0);\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":274\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"\n *                 elif t == NPY_CLONGDOUBLE: f = \"Zg\"\n *                 elif t == NPY_OBJECT:      f = \"O\"             # <<<<<<<<<<<<<<\n *                 else:\n *                     raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n */\n    switch (__pyx_v_t) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":258\n *                     (descr.byteorder == c'<' and not little_endian)):\n *                     raise ValueError(u\"Non-native byte order not supported\")\n *                 if   t == NPY_BYTE:        f = \"b\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_UBYTE:       f = \"B\"\n *                 elif t == NPY_SHORT:       f = \"h\"\n */\n      case NPY_BYTE:\n      __pyx_v_f = __pyx_k_b;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":259\n *                     raise ValueError(u\"Non-native byte order not supported\")\n *                 if   t == NPY_BYTE:        f = \"b\"\n *                 elif t == NPY_UBYTE:       f = \"B\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_SHORT:       f = \"h\"\n *                 elif t == NPY_USHORT:      f = \"H\"\n */\n      case NPY_UBYTE:\n      __pyx_v_f = __pyx_k_B;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":260\n *                 if   t == NPY_BYTE:        f = \"b\"\n *                 elif t == NPY_UBYTE:       f = \"B\"\n *                 elif t == NPY_SHORT:       f = \"h\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_USHORT:      f = \"H\"\n *                 elif t == NPY_INT:         f = \"i\"\n */\n      case NPY_SHORT:\n      __pyx_v_f = __pyx_k_h;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":261\n *                 elif t == NPY_UBYTE:       f = \"B\"\n *                 elif t == NPY_SHORT:       f = \"h\"\n *                 elif t == NPY_USHORT:      f = \"H\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_INT:         f = \"i\"\n *                 elif t == NPY_UINT:        f = \"I\"\n */\n      case NPY_USHORT:\n      __pyx_v_f = __pyx_k_H;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":262\n *                 elif t == NPY_SHORT:       f = \"h\"\n *                 elif t == NPY_USHORT:      f = \"H\"\n *                 elif t == NPY_INT:         f = \"i\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_UINT:        f = \"I\"\n *                 elif t == NPY_LONG:        f = \"l\"\n */\n      case NPY_INT:\n      __pyx_v_f = __pyx_k_i;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":263\n *                 elif t == NPY_USHORT:      f = \"H\"\n *                 elif t == NPY_INT:         f = \"i\"\n *                 elif t == NPY_UINT:        f = \"I\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_LONG:        f = \"l\"\n *                 elif t == NPY_ULONG:       f = \"L\"\n */\n      case NPY_UINT:\n      __pyx_v_f = __pyx_k_I;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":264\n *                 elif t == NPY_INT:         f = \"i\"\n *                 elif t == NPY_UINT:        f = \"I\"\n *                 elif t == NPY_LONG:        f = \"l\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_ULONG:       f = \"L\"\n *                 elif t == NPY_LONGLONG:    f = \"q\"\n */\n      case NPY_LONG:\n      __pyx_v_f = __pyx_k_l;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":265\n *                 elif t == NPY_UINT:        f = \"I\"\n *                 elif t == NPY_LONG:        f = \"l\"\n *                 elif t == NPY_ULONG:       f = \"L\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_LONGLONG:    f = \"q\"\n *                 elif t == NPY_ULONGLONG:   f = \"Q\"\n */\n      case NPY_ULONG:\n      __pyx_v_f = __pyx_k_L;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":266\n *                 elif t == NPY_LONG:        f = \"l\"\n *                 elif t == NPY_ULONG:       f = \"L\"\n *                 elif t == NPY_LONGLONG:    f = \"q\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_ULONGLONG:   f = \"Q\"\n *                 elif t == NPY_FLOAT:       f = \"f\"\n */\n      case NPY_LONGLONG:\n      __pyx_v_f = __pyx_k_q;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":267\n *                 elif t == NPY_ULONG:       f = \"L\"\n *                 elif t == NPY_LONGLONG:    f = \"q\"\n *                 elif t == NPY_ULONGLONG:   f = \"Q\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_FLOAT:       f = \"f\"\n *                 elif t == NPY_DOUBLE:      f = \"d\"\n */\n      case NPY_ULONGLONG:\n      __pyx_v_f = __pyx_k_Q;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":268\n *                 elif t == NPY_LONGLONG:    f = \"q\"\n *                 elif t == NPY_ULONGLONG:   f = \"Q\"\n *                 elif t == NPY_FLOAT:       f = \"f\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_DOUBLE:      f = \"d\"\n *                 elif t == NPY_LONGDOUBLE:  f = \"g\"\n */\n      case NPY_FLOAT:\n      __pyx_v_f = __pyx_k_f;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":269\n *                 elif t == NPY_ULONGLONG:   f = \"Q\"\n *                 elif t == NPY_FLOAT:       f = \"f\"\n *                 elif t == NPY_DOUBLE:      f = \"d\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_LONGDOUBLE:  f = \"g\"\n *                 elif t == NPY_CFLOAT:      f = \"Zf\"\n */\n      case NPY_DOUBLE:\n      __pyx_v_f = __pyx_k_d;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":270\n *                 elif t == NPY_FLOAT:       f = \"f\"\n *                 elif t == NPY_DOUBLE:      f = \"d\"\n *                 elif t == NPY_LONGDOUBLE:  f = \"g\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_CFLOAT:      f = \"Zf\"\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"\n */\n      case NPY_LONGDOUBLE:\n      __pyx_v_f = __pyx_k_g;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":271\n *                 elif t == NPY_DOUBLE:      f = \"d\"\n *                 elif t == NPY_LONGDOUBLE:  f = \"g\"\n *                 elif t == NPY_CFLOAT:      f = \"Zf\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"\n *                 elif t == NPY_CLONGDOUBLE: f = \"Zg\"\n */\n      case NPY_CFLOAT:\n      __pyx_v_f = __pyx_k_Zf;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":272\n *                 elif t == NPY_LONGDOUBLE:  f = \"g\"\n *                 elif t == NPY_CFLOAT:      f = \"Zf\"\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_CLONGDOUBLE: f = \"Zg\"\n *                 elif t == NPY_OBJECT:      f = \"O\"\n */\n      case NPY_CDOUBLE:\n      __pyx_v_f = __pyx_k_Zd;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":273\n *                 elif t == NPY_CFLOAT:      f = \"Zf\"\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"\n *                 elif t == NPY_CLONGDOUBLE: f = \"Zg\"             # <<<<<<<<<<<<<<\n *                 elif t == NPY_OBJECT:      f = \"O\"\n *                 else:\n */\n      case NPY_CLONGDOUBLE:\n      __pyx_v_f = __pyx_k_Zg;\n      break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":274\n *                 elif t == NPY_CDOUBLE:     f = \"Zd\"\n *                 elif t == NPY_CLONGDOUBLE: f = \"Zg\"\n *                 elif t == NPY_OBJECT:      f = \"O\"             # <<<<<<<<<<<<<<\n *                 else:\n *                     raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n */\n      case NPY_OBJECT:\n      __pyx_v_f = __pyx_k_O;\n      break;\n      default:\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":276\n *                 elif t == NPY_OBJECT:      f = \"O\"\n *                 else:\n *                     raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)             # <<<<<<<<<<<<<<\n *                 info.format = f\n *                 return\n */\n      __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_t); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_8 = PyUnicode_Format(__pyx_kp_u_unknown_dtype_code_in_numpy_pxd, __pyx_t_4); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_8);\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_8);\n      __Pyx_GIVEREF(__pyx_t_8);\n      __pyx_t_8 = 0;\n      __pyx_t_8 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_8);\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __Pyx_Raise(__pyx_t_8, 0, 0, 0);\n      __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;\n      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 276; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      break;\n    }\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":277\n *                 else:\n *                     raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n *                 info.format = f             # <<<<<<<<<<<<<<\n *                 return\n *             else:\n */\n    __pyx_v_info->format = __pyx_v_f;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":278\n *                     raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n *                 info.format = f\n *                 return             # <<<<<<<<<<<<<<\n *             else:\n *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)\n */\n    __pyx_r = 0;\n    goto __pyx_L0;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":280\n *                 return\n *             else:\n *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)             # <<<<<<<<<<<<<<\n *                 info.format[0] = c'^' # Native data types, manual alignment\n *                 offset = 0\n */\n    __pyx_v_info->format = ((char *)malloc(255));\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":281\n *             else:\n *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)\n *                 info.format[0] = c'^' # Native data types, manual alignment             # <<<<<<<<<<<<<<\n *                 offset = 0\n *                 f = _util_dtypestring(descr, info.format + 1,\n */\n    (__pyx_v_info->format[0]) = '^';\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":282\n *                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)\n *                 info.format[0] = c'^' # Native data types, manual alignment\n *                 offset = 0             # <<<<<<<<<<<<<<\n *                 f = _util_dtypestring(descr, info.format + 1,\n *                                       info.format + _buffer_format_string_len,\n */\n    __pyx_v_offset = 0;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":283\n *                 info.format[0] = c'^' # Native data types, manual alignment\n *                 offset = 0\n *                 f = _util_dtypestring(descr, info.format + 1,             # <<<<<<<<<<<<<<\n *                                       info.format + _buffer_format_string_len,\n *                                       &offset)\n */\n    __pyx_t_9 = __pyx_f_5numpy__util_dtypestring(__pyx_v_descr, (__pyx_v_info->format + 1), (__pyx_v_info->format + 255), (&__pyx_v_offset)); if (unlikely(__pyx_t_9 == NULL)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 283; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __pyx_v_f = __pyx_t_9;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":286\n *                                       info.format + _buffer_format_string_len,\n *                                       &offset)\n *                 f[0] = c'\\0' # Terminate format string             # <<<<<<<<<<<<<<\n * \n *         def __releasebuffer__(ndarray self, Py_buffer* info):\n */\n    (__pyx_v_f[0]) = '\\x00';\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":194\n *         # experimental exception made for __getbuffer__ and __releasebuffer__\n *         # -- the details of this may change.\n *         def __getbuffer__(ndarray self, Py_buffer* info, int flags):             # <<<<<<<<<<<<<<\n *             # This implementation of getbuffer is geared towards Cython\n *             # requirements, and does not yet fullfill the PEP.\n */\n\n  /* function exit code */\n  __pyx_r = 0;\n  goto __pyx_L0;\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_4);\n  __Pyx_XDECREF(__pyx_t_8);\n  __Pyx_AddTraceback(\"numpy.ndarray.__getbuffer__\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = -1;\n  if (__pyx_v_info != NULL && __pyx_v_info->obj != NULL) {\n    __Pyx_GOTREF(__pyx_v_info->obj);\n    __Pyx_DECREF(__pyx_v_info->obj); __pyx_v_info->obj = NULL;\n  }\n  goto __pyx_L2;\n  __pyx_L0:;\n  if (__pyx_v_info != NULL && __pyx_v_info->obj == Py_None) {\n    __Pyx_GOTREF(Py_None);\n    __Pyx_DECREF(Py_None); __pyx_v_info->obj = NULL;\n  }\n  __pyx_L2:;\n  __Pyx_XDECREF((PyObject *)__pyx_v_descr);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":288\n *                 f[0] = c'\\0' # Terminate format string\n * \n *         def __releasebuffer__(ndarray self, Py_buffer* info):             # <<<<<<<<<<<<<<\n *             if PyArray_HASFIELDS(self):\n *                 stdlib.free(info.format)\n */\n\n/* Python wrapper */\nstatic CYTHON_UNUSED void __pyx_pw_5numpy_7ndarray_3__releasebuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info); /*proto*/\nstatic CYTHON_UNUSED void __pyx_pw_5numpy_7ndarray_3__releasebuffer__(PyObject *__pyx_v_self, Py_buffer *__pyx_v_info) {\n  __Pyx_RefNannyDeclarations\n  __Pyx_RefNannySetupContext(\"__releasebuffer__ (wrapper)\", 0);\n  __pyx_pf_5numpy_7ndarray_2__releasebuffer__(((PyArrayObject *)__pyx_v_self), ((Py_buffer *)__pyx_v_info));\n\n  /* function exit code */\n  __Pyx_RefNannyFinishContext();\n}\n\nstatic void __pyx_pf_5numpy_7ndarray_2__releasebuffer__(PyArrayObject *__pyx_v_self, Py_buffer *__pyx_v_info) {\n  __Pyx_RefNannyDeclarations\n  int __pyx_t_1;\n  __Pyx_RefNannySetupContext(\"__releasebuffer__\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":289\n * \n *         def __releasebuffer__(ndarray self, Py_buffer* info):\n *             if PyArray_HASFIELDS(self):             # <<<<<<<<<<<<<<\n *                 stdlib.free(info.format)\n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):\n */\n  __pyx_t_1 = (PyArray_HASFIELDS(__pyx_v_self) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":290\n *         def __releasebuffer__(ndarray self, Py_buffer* info):\n *             if PyArray_HASFIELDS(self):\n *                 stdlib.free(info.format)             # <<<<<<<<<<<<<<\n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):\n *                 stdlib.free(info.strides)\n */\n    free(__pyx_v_info->format);\n    goto __pyx_L3;\n  }\n  __pyx_L3:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":291\n *             if PyArray_HASFIELDS(self):\n *                 stdlib.free(info.format)\n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):             # <<<<<<<<<<<<<<\n *                 stdlib.free(info.strides)\n *                 # info.shape was stored after info.strides in the same block\n */\n  __pyx_t_1 = (((sizeof(npy_intp)) != (sizeof(Py_ssize_t))) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":292\n *                 stdlib.free(info.format)\n *             if sizeof(npy_intp) != sizeof(Py_ssize_t):\n *                 stdlib.free(info.strides)             # <<<<<<<<<<<<<<\n *                 # info.shape was stored after info.strides in the same block\n * \n */\n    free(__pyx_v_info->strides);\n    goto __pyx_L4;\n  }\n  __pyx_L4:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":288\n *                 f[0] = c'\\0' # Terminate format string\n * \n *         def __releasebuffer__(ndarray self, Py_buffer* info):             # <<<<<<<<<<<<<<\n *             if PyArray_HASFIELDS(self):\n *                 stdlib.free(info.format)\n */\n\n  /* function exit code */\n  __Pyx_RefNannyFinishContext();\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":768\n * ctypedef npy_cdouble     complex_t\n * \n * cdef inline object PyArray_MultiIterNew1(a):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(1, <void*>a)\n * \n */\n\nstatic CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew1(PyObject *__pyx_v_a) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"PyArray_MultiIterNew1\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":769\n * \n * cdef inline object PyArray_MultiIterNew1(a):\n *     return PyArray_MultiIterNew(1, <void*>a)             # <<<<<<<<<<<<<<\n * \n * cdef inline object PyArray_MultiIterNew2(a, b):\n */\n  __Pyx_XDECREF(__pyx_r);\n  __pyx_t_1 = PyArray_MultiIterNew(1, ((void *)__pyx_v_a)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 769; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_r = __pyx_t_1;\n  __pyx_t_1 = 0;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":768\n * ctypedef npy_cdouble     complex_t\n * \n * cdef inline object PyArray_MultiIterNew1(a):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(1, <void*>a)\n * \n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_AddTraceback(\"numpy.PyArray_MultiIterNew1\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = 0;\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":771\n *     return PyArray_MultiIterNew(1, <void*>a)\n * \n * cdef inline object PyArray_MultiIterNew2(a, b):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)\n * \n */\n\nstatic CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew2(PyObject *__pyx_v_a, PyObject *__pyx_v_b) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"PyArray_MultiIterNew2\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":772\n * \n * cdef inline object PyArray_MultiIterNew2(a, b):\n *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)             # <<<<<<<<<<<<<<\n * \n * cdef inline object PyArray_MultiIterNew3(a, b, c):\n */\n  __Pyx_XDECREF(__pyx_r);\n  __pyx_t_1 = PyArray_MultiIterNew(2, ((void *)__pyx_v_a), ((void *)__pyx_v_b)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 772; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_r = __pyx_t_1;\n  __pyx_t_1 = 0;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":771\n *     return PyArray_MultiIterNew(1, <void*>a)\n * \n * cdef inline object PyArray_MultiIterNew2(a, b):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)\n * \n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_AddTraceback(\"numpy.PyArray_MultiIterNew2\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = 0;\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":774\n *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)\n * \n * cdef inline object PyArray_MultiIterNew3(a, b, c):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)\n * \n */\n\nstatic CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew3(PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"PyArray_MultiIterNew3\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":775\n * \n * cdef inline object PyArray_MultiIterNew3(a, b, c):\n *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)             # <<<<<<<<<<<<<<\n * \n * cdef inline object PyArray_MultiIterNew4(a, b, c, d):\n */\n  __Pyx_XDECREF(__pyx_r);\n  __pyx_t_1 = PyArray_MultiIterNew(3, ((void *)__pyx_v_a), ((void *)__pyx_v_b), ((void *)__pyx_v_c)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 775; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_r = __pyx_t_1;\n  __pyx_t_1 = 0;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":774\n *     return PyArray_MultiIterNew(2, <void*>a, <void*>b)\n * \n * cdef inline object PyArray_MultiIterNew3(a, b, c):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)\n * \n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_AddTraceback(\"numpy.PyArray_MultiIterNew3\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = 0;\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":777\n *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)\n * \n * cdef inline object PyArray_MultiIterNew4(a, b, c, d):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)\n * \n */\n\nstatic CYTHON_INLINE PyObject *__pyx_f_5numpy_PyArray_MultiIterNew4(PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_c, PyObject *__pyx_v_d) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"PyArray_MultiIterNew4\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":778\n * \n * cdef inline object PyArray_MultiIterNew4(a, b, c, d):\n *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)             # <<<<<<<<<<<<<<\n * \n * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):\n */\n  __Pyx_XDECREF(__pyx_r);\n  __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_filename = __pyx_f[1]; __pyx_lineno = 778; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_r = __pyx_t_1;\n  __pyx_t_1 = 0;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":777\n *     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)\n * \n * cdef inline object PyArray_MultiIterNew4(a, b, c, d):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)\n * \n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_AddTraceback(\"numpy.PyArray_MultiIterNew4\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = 0;\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":780\n *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)\n * \n * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)\n * \n */\n\nstatic 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) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"PyArray_MultiIterNew5\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":781\n * \n * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):\n *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)             # <<<<<<<<<<<<<<\n * \n * cdef inline char* _util_dtypestring(dtype descr, char* f, char* end, int* offset) except NULL:\n */\n  __Pyx_XDECREF(__pyx_r);\n  __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_filename = __pyx_f[1]; __pyx_lineno = 781; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  __pyx_r = __pyx_t_1;\n  __pyx_t_1 = 0;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":780\n *     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)\n * \n * cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):             # <<<<<<<<<<<<<<\n *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)\n * \n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_AddTraceback(\"numpy.PyArray_MultiIterNew5\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = 0;\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":783\n *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)\n * \n * cdef inline char* _util_dtypestring(dtype descr, char* f, char* end, int* offset) except NULL:             # <<<<<<<<<<<<<<\n *     # Recursive utility function used in __getbuffer__ to get format\n *     # string. The new location in the format string is returned.\n */\n\nstatic CYTHON_INLINE char *__pyx_f_5numpy__util_dtypestring(PyArray_Descr *__pyx_v_descr, char *__pyx_v_f, char *__pyx_v_end, int *__pyx_v_offset) {\n  PyArray_Descr *__pyx_v_child = 0;\n  int __pyx_v_endian_detector;\n  int __pyx_v_little_endian;\n  PyObject *__pyx_v_fields = 0;\n  PyObject *__pyx_v_childname = NULL;\n  PyObject *__pyx_v_new_offset = NULL;\n  PyObject *__pyx_v_t = NULL;\n  char *__pyx_r;\n  __Pyx_RefNannyDeclarations\n  PyObject *__pyx_t_1 = NULL;\n  Py_ssize_t __pyx_t_2;\n  PyObject *__pyx_t_3 = NULL;\n  PyObject *__pyx_t_4 = NULL;\n  int __pyx_t_5;\n  int __pyx_t_6;\n  int __pyx_t_7;\n  int __pyx_t_8;\n  int __pyx_t_9;\n  long __pyx_t_10;\n  char *__pyx_t_11;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannySetupContext(\"_util_dtypestring\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":790\n *     cdef int delta_offset\n *     cdef tuple i\n *     cdef int endian_detector = 1             # <<<<<<<<<<<<<<\n *     cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)\n *     cdef tuple fields\n */\n  __pyx_v_endian_detector = 1;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":791\n *     cdef tuple i\n *     cdef int endian_detector = 1\n *     cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)             # <<<<<<<<<<<<<<\n *     cdef tuple fields\n * \n */\n  __pyx_v_little_endian = ((((char *)(&__pyx_v_endian_detector))[0]) != 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":794\n *     cdef tuple fields\n * \n *     for childname in descr.names:             # <<<<<<<<<<<<<<\n *         fields = descr.fields[childname]\n *         child, new_offset = fields\n */\n  if (unlikely(__pyx_v_descr->names == Py_None)) {\n    PyErr_SetString(PyExc_TypeError, \"'NoneType' object is not iterable\");\n    {__pyx_filename = __pyx_f[1]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  }\n  __pyx_t_1 = __pyx_v_descr->names; __Pyx_INCREF(__pyx_t_1); __pyx_t_2 = 0;\n  for (;;) {\n    if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_1)) break;\n    #if CYTHON_COMPILING_IN_CPYTHON\n    __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_1, __pyx_t_2); __Pyx_INCREF(__pyx_t_3); __pyx_t_2++; if (unlikely(0 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    #else\n    __pyx_t_3 = PySequence_ITEM(__pyx_t_1, __pyx_t_2); __pyx_t_2++; if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 794; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    #endif\n    __Pyx_XDECREF_SET(__pyx_v_childname, __pyx_t_3);\n    __pyx_t_3 = 0;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":795\n * \n *     for childname in descr.names:\n *         fields = descr.fields[childname]             # <<<<<<<<<<<<<<\n *         child, new_offset = fields\n * \n */\n    __pyx_t_3 = PyObject_GetItem(__pyx_v_descr->fields, __pyx_v_childname); if (unlikely(__pyx_t_3 == NULL)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n    __Pyx_GOTREF(__pyx_t_3);\n    if (!(likely(PyTuple_CheckExact(__pyx_t_3))||((__pyx_t_3) == Py_None)||(PyErr_Format(PyExc_TypeError, \"Expected %.16s, got %.200s\", \"tuple\", Py_TYPE(__pyx_t_3)->tp_name), 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 795; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_XDECREF_SET(__pyx_v_fields, ((PyObject*)__pyx_t_3));\n    __pyx_t_3 = 0;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":796\n *     for childname in descr.names:\n *         fields = descr.fields[childname]\n *         child, new_offset = fields             # <<<<<<<<<<<<<<\n * \n *         if (end - f) - <int>(new_offset - offset[0]) < 15:\n */\n    if (likely(__pyx_v_fields != Py_None)) {\n      PyObject* sequence = __pyx_v_fields;\n      #if CYTHON_COMPILING_IN_CPYTHON\n      Py_ssize_t size = Py_SIZE(sequence);\n      #else\n      Py_ssize_t size = PySequence_Size(sequence);\n      #endif\n      if (unlikely(size != 2)) {\n        if (size > 2) __Pyx_RaiseTooManyValuesError(2);\n        else if (size >= 0) __Pyx_RaiseNeedMoreValuesError(size);\n        {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      #if CYTHON_COMPILING_IN_CPYTHON\n      __pyx_t_3 = PyTuple_GET_ITEM(sequence, 0); \n      __pyx_t_4 = PyTuple_GET_ITEM(sequence, 1); \n      __Pyx_INCREF(__pyx_t_3);\n      __Pyx_INCREF(__pyx_t_4);\n      #else\n      __pyx_t_3 = PySequence_ITEM(sequence, 0); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PySequence_ITEM(sequence, 1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      #endif\n    } else {\n      __Pyx_RaiseNoneNotIterableError(); {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n    if (!(likely(((__pyx_t_3) == Py_None) || likely(__Pyx_TypeTest(__pyx_t_3, __pyx_ptype_5numpy_dtype))))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 796; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_XDECREF_SET(__pyx_v_child, ((PyArray_Descr *)__pyx_t_3));\n    __pyx_t_3 = 0;\n    __Pyx_XDECREF_SET(__pyx_v_new_offset, __pyx_t_4);\n    __pyx_t_4 = 0;\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":798\n *         child, new_offset = fields\n * \n *         if (end - f) - <int>(new_offset - offset[0]) < 15:             # <<<<<<<<<<<<<<\n *             raise RuntimeError(u\"Format string allocated too short, see comment in numpy.pxd\")\n * \n */\n    __pyx_t_4 = __Pyx_PyInt_From_int((__pyx_v_offset[0])); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_GOTREF(__pyx_t_4);\n    __pyx_t_3 = PyNumber_Subtract(__pyx_v_new_offset, __pyx_t_4); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_GOTREF(__pyx_t_3);\n    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n    __pyx_t_5 = __Pyx_PyInt_As_int(__pyx_t_3); if (unlikely((__pyx_t_5 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 798; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n    __pyx_t_6 = ((((__pyx_v_end - __pyx_v_f) - ((int)__pyx_t_5)) < 15) != 0);\n    if (__pyx_t_6) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":799\n * \n *         if (end - f) - <int>(new_offset - offset[0]) < 15:\n *             raise RuntimeError(u\"Format string allocated too short, see comment in numpy.pxd\")             # <<<<<<<<<<<<<<\n * \n *         if ((child.byteorder == c'>' and little_endian) or\n */\n      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_RuntimeError, __pyx_tuple__8, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __Pyx_Raise(__pyx_t_3, 0, 0, 0);\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":801\n *             raise RuntimeError(u\"Format string allocated too short, see comment in numpy.pxd\")\n * \n *         if ((child.byteorder == c'>' and little_endian) or             # <<<<<<<<<<<<<<\n *             (child.byteorder == c'<' and not little_endian)):\n *             raise ValueError(u\"Non-native byte order not supported\")\n */\n    __pyx_t_6 = ((__pyx_v_child->byteorder == '>') != 0);\n    if (__pyx_t_6) {\n      __pyx_t_7 = (__pyx_v_little_endian != 0);\n    } else {\n      __pyx_t_7 = __pyx_t_6;\n    }\n    if (!__pyx_t_7) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":802\n * \n *         if ((child.byteorder == c'>' and little_endian) or\n *             (child.byteorder == c'<' and not little_endian)):             # <<<<<<<<<<<<<<\n *             raise ValueError(u\"Non-native byte order not supported\")\n *             # One could encode it in the format string and have Cython\n */\n      __pyx_t_6 = ((__pyx_v_child->byteorder == '<') != 0);\n      if (__pyx_t_6) {\n        __pyx_t_8 = ((!(__pyx_v_little_endian != 0)) != 0);\n        __pyx_t_9 = __pyx_t_8;\n      } else {\n        __pyx_t_9 = __pyx_t_6;\n      }\n      __pyx_t_6 = __pyx_t_9;\n    } else {\n      __pyx_t_6 = __pyx_t_7;\n    }\n    if (__pyx_t_6) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":803\n *         if ((child.byteorder == c'>' and little_endian) or\n *             (child.byteorder == c'<' and not little_endian)):\n *             raise ValueError(u\"Non-native byte order not supported\")             # <<<<<<<<<<<<<<\n *             # One could encode it in the format string and have Cython\n *             # complain instead, BUT: < and > in format strings also imply\n */\n      __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_tuple__9, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __Pyx_Raise(__pyx_t_3, 0, 0, 0);\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      {__pyx_filename = __pyx_f[1]; __pyx_lineno = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":813\n * \n *         # Output padding bytes\n *         while offset[0] < new_offset:             # <<<<<<<<<<<<<<\n *             f[0] = 120 # \"x\"; pad byte\n *             f += 1\n */\n    while (1) {\n      __pyx_t_3 = __Pyx_PyInt_From_int((__pyx_v_offset[0])); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_t_3, __pyx_v_new_offset, Py_LT); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 813; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (!__pyx_t_6) break;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":814\n *         # Output padding bytes\n *         while offset[0] < new_offset:\n *             f[0] = 120 # \"x\"; pad byte             # <<<<<<<<<<<<<<\n *             f += 1\n *             offset[0] += 1\n */\n      (__pyx_v_f[0]) = 120;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":815\n *         while offset[0] < new_offset:\n *             f[0] = 120 # \"x\"; pad byte\n *             f += 1             # <<<<<<<<<<<<<<\n *             offset[0] += 1\n * \n */\n      __pyx_v_f = (__pyx_v_f + 1);\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":816\n *             f[0] = 120 # \"x\"; pad byte\n *             f += 1\n *             offset[0] += 1             # <<<<<<<<<<<<<<\n * \n *         offset[0] += child.itemsize\n */\n      __pyx_t_10 = 0;\n      (__pyx_v_offset[__pyx_t_10]) = ((__pyx_v_offset[__pyx_t_10]) + 1);\n    }\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":818\n *             offset[0] += 1\n * \n *         offset[0] += child.itemsize             # <<<<<<<<<<<<<<\n * \n *         if not PyDataType_HASFIELDS(child):\n */\n    __pyx_t_10 = 0;\n    (__pyx_v_offset[__pyx_t_10]) = ((__pyx_v_offset[__pyx_t_10]) + __pyx_v_child->elsize);\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":820\n *         offset[0] += child.itemsize\n * \n *         if not PyDataType_HASFIELDS(child):             # <<<<<<<<<<<<<<\n *             t = child.type_num\n *             if end - f < 5:\n */\n    __pyx_t_6 = ((!(PyDataType_HASFIELDS(__pyx_v_child) != 0)) != 0);\n    if (__pyx_t_6) {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":821\n * \n *         if not PyDataType_HASFIELDS(child):\n *             t = child.type_num             # <<<<<<<<<<<<<<\n *             if end - f < 5:\n *                 raise RuntimeError(u\"Format string allocated too short.\")\n */\n      __pyx_t_4 = __Pyx_PyInt_From_int(__pyx_v_child->type_num); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 821; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __Pyx_XDECREF_SET(__pyx_v_t, __pyx_t_4);\n      __pyx_t_4 = 0;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":822\n *         if not PyDataType_HASFIELDS(child):\n *             t = child.type_num\n *             if end - f < 5:             # <<<<<<<<<<<<<<\n *                 raise RuntimeError(u\"Format string allocated too short.\")\n * \n */\n      __pyx_t_6 = (((__pyx_v_end - __pyx_v_f) < 5) != 0);\n      if (__pyx_t_6) {\n\n        /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":823\n *             t = child.type_num\n *             if end - f < 5:\n *                 raise RuntimeError(u\"Format string allocated too short.\")             # <<<<<<<<<<<<<<\n * \n *             # Until ticket #99 is fixed, use integers to avoid warnings\n */\n        __pyx_t_4 = __Pyx_PyObject_Call(__pyx_builtin_RuntimeError, __pyx_tuple__10, NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n        __Pyx_GOTREF(__pyx_t_4);\n        __Pyx_Raise(__pyx_t_4, 0, 0, 0);\n        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n        {__pyx_filename = __pyx_f[1]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":826\n * \n *             # Until ticket #99 is fixed, use integers to avoid warnings\n *             if   t == NPY_BYTE:        f[0] =  98 #\"b\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_UBYTE:       f[0] =  66 #\"B\"\n *             elif t == NPY_SHORT:       f[0] = 104 #\"h\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_BYTE); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 826; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 98;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":827\n *             # Until ticket #99 is fixed, use integers to avoid warnings\n *             if   t == NPY_BYTE:        f[0] =  98 #\"b\"\n *             elif t == NPY_UBYTE:       f[0] =  66 #\"B\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_SHORT:       f[0] = 104 #\"h\"\n *             elif t == NPY_USHORT:      f[0] =  72 #\"H\"\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_UBYTE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 827; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 66;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":828\n *             if   t == NPY_BYTE:        f[0] =  98 #\"b\"\n *             elif t == NPY_UBYTE:       f[0] =  66 #\"B\"\n *             elif t == NPY_SHORT:       f[0] = 104 #\"h\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_USHORT:      f[0] =  72 #\"H\"\n *             elif t == NPY_INT:         f[0] = 105 #\"i\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_SHORT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 828; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 104;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":829\n *             elif t == NPY_UBYTE:       f[0] =  66 #\"B\"\n *             elif t == NPY_SHORT:       f[0] = 104 #\"h\"\n *             elif t == NPY_USHORT:      f[0] =  72 #\"H\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_INT:         f[0] = 105 #\"i\"\n *             elif t == NPY_UINT:        f[0] =  73 #\"I\"\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_USHORT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 829; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 72;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":830\n *             elif t == NPY_SHORT:       f[0] = 104 #\"h\"\n *             elif t == NPY_USHORT:      f[0] =  72 #\"H\"\n *             elif t == NPY_INT:         f[0] = 105 #\"i\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_UINT:        f[0] =  73 #\"I\"\n *             elif t == NPY_LONG:        f[0] = 108 #\"l\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_INT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 830; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 105;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":831\n *             elif t == NPY_USHORT:      f[0] =  72 #\"H\"\n *             elif t == NPY_INT:         f[0] = 105 #\"i\"\n *             elif t == NPY_UINT:        f[0] =  73 #\"I\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_LONG:        f[0] = 108 #\"l\"\n *             elif t == NPY_ULONG:       f[0] = 76  #\"L\"\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_UINT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 831; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 73;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":832\n *             elif t == NPY_INT:         f[0] = 105 #\"i\"\n *             elif t == NPY_UINT:        f[0] =  73 #\"I\"\n *             elif t == NPY_LONG:        f[0] = 108 #\"l\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_ULONG:       f[0] = 76  #\"L\"\n *             elif t == NPY_LONGLONG:    f[0] = 113 #\"q\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_LONG); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 832; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 108;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":833\n *             elif t == NPY_UINT:        f[0] =  73 #\"I\"\n *             elif t == NPY_LONG:        f[0] = 108 #\"l\"\n *             elif t == NPY_ULONG:       f[0] = 76  #\"L\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_LONGLONG:    f[0] = 113 #\"q\"\n *             elif t == NPY_ULONGLONG:   f[0] = 81  #\"Q\"\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_ULONG); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 833; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 76;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":834\n *             elif t == NPY_LONG:        f[0] = 108 #\"l\"\n *             elif t == NPY_ULONG:       f[0] = 76  #\"L\"\n *             elif t == NPY_LONGLONG:    f[0] = 113 #\"q\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_ULONGLONG:   f[0] = 81  #\"Q\"\n *             elif t == NPY_FLOAT:       f[0] = 102 #\"f\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_LONGLONG); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 834; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 113;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":835\n *             elif t == NPY_ULONG:       f[0] = 76  #\"L\"\n *             elif t == NPY_LONGLONG:    f[0] = 113 #\"q\"\n *             elif t == NPY_ULONGLONG:   f[0] = 81  #\"Q\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_FLOAT:       f[0] = 102 #\"f\"\n *             elif t == NPY_DOUBLE:      f[0] = 100 #\"d\"\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_ULONGLONG); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 835; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 81;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":836\n *             elif t == NPY_LONGLONG:    f[0] = 113 #\"q\"\n *             elif t == NPY_ULONGLONG:   f[0] = 81  #\"Q\"\n *             elif t == NPY_FLOAT:       f[0] = 102 #\"f\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_DOUBLE:      f[0] = 100 #\"d\"\n *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #\"g\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_FLOAT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 836; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 102;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":837\n *             elif t == NPY_ULONGLONG:   f[0] = 81  #\"Q\"\n *             elif t == NPY_FLOAT:       f[0] = 102 #\"f\"\n *             elif t == NPY_DOUBLE:      f[0] = 100 #\"d\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #\"g\"\n *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_DOUBLE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 837; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 100;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":838\n *             elif t == NPY_FLOAT:       f[0] = 102 #\"f\"\n *             elif t == NPY_DOUBLE:      f[0] = 100 #\"d\"\n *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #\"g\"             # <<<<<<<<<<<<<<\n *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf\n *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_LONGDOUBLE); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 838; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 103;\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":839\n *             elif t == NPY_DOUBLE:      f[0] = 100 #\"d\"\n *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #\"g\"\n *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf             # <<<<<<<<<<<<<<\n *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd\n *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_CFLOAT); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 839; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 90;\n        (__pyx_v_f[1]) = 102;\n        __pyx_v_f = (__pyx_v_f + 1);\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":840\n *             elif t == NPY_LONGDOUBLE:  f[0] = 103 #\"g\"\n *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf\n *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd             # <<<<<<<<<<<<<<\n *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg\n *             elif t == NPY_OBJECT:      f[0] = 79 #\"O\"\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_CDOUBLE); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 840; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 90;\n        (__pyx_v_f[1]) = 100;\n        __pyx_v_f = (__pyx_v_f + 1);\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":841\n *             elif t == NPY_CFLOAT:      f[0] = 90; f[1] = 102; f += 1 # Zf\n *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd\n *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg             # <<<<<<<<<<<<<<\n *             elif t == NPY_OBJECT:      f[0] = 79 #\"O\"\n *             else:\n */\n      __pyx_t_3 = PyInt_FromLong(NPY_CLONGDOUBLE); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_3);\n      __pyx_t_4 = PyObject_RichCompare(__pyx_v_t, __pyx_t_3, Py_EQ); __Pyx_XGOTREF(__pyx_t_4); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_4); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 841; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 90;\n        (__pyx_v_f[1]) = 103;\n        __pyx_v_f = (__pyx_v_f + 1);\n        goto __pyx_L11;\n      }\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":842\n *             elif t == NPY_CDOUBLE:     f[0] = 90; f[1] = 100; f += 1 # Zd\n *             elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg\n *             elif t == NPY_OBJECT:      f[0] = 79 #\"O\"             # <<<<<<<<<<<<<<\n *             else:\n *                 raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n */\n      __pyx_t_4 = PyInt_FromLong(NPY_OBJECT); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_GOTREF(__pyx_t_4);\n      __pyx_t_3 = PyObject_RichCompare(__pyx_v_t, __pyx_t_4, Py_EQ); __Pyx_XGOTREF(__pyx_t_3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n      __pyx_t_6 = __Pyx_PyObject_IsTrue(__pyx_t_3); if (unlikely(__pyx_t_6 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 842; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n      if (__pyx_t_6) {\n        (__pyx_v_f[0]) = 79;\n        goto __pyx_L11;\n      }\n      /*else*/ {\n\n        /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":844\n *             elif t == NPY_OBJECT:      f[0] = 79 #\"O\"\n *             else:\n *                 raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)             # <<<<<<<<<<<<<<\n *             f += 1\n *         else:\n */\n        __pyx_t_3 = PyUnicode_Format(__pyx_kp_u_unknown_dtype_code_in_numpy_pxd, __pyx_v_t); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n        __Pyx_GOTREF(__pyx_t_3);\n        __pyx_t_4 = PyTuple_New(1); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n        __Pyx_GOTREF(__pyx_t_4);\n        PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_3);\n        __Pyx_GIVEREF(__pyx_t_3);\n        __pyx_t_3 = 0;\n        __pyx_t_3 = __Pyx_PyObject_Call(__pyx_builtin_ValueError, __pyx_t_4, NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n        __Pyx_GOTREF(__pyx_t_3);\n        __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n        __Pyx_Raise(__pyx_t_3, 0, 0, 0);\n        __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n        {__pyx_filename = __pyx_f[1]; __pyx_lineno = 844; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      }\n      __pyx_L11:;\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":845\n *             else:\n *                 raise ValueError(u\"unknown dtype code in numpy.pxd (%d)\" % t)\n *             f += 1             # <<<<<<<<<<<<<<\n *         else:\n *             # Cython ignores struct boundary information (\"T{...}\"),\n */\n      __pyx_v_f = (__pyx_v_f + 1);\n      goto __pyx_L9;\n    }\n    /*else*/ {\n\n      /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":849\n *             # Cython ignores struct boundary information (\"T{...}\"),\n *             # so don't output it\n *             f = _util_dtypestring(child, f, end, offset)             # <<<<<<<<<<<<<<\n *     return f\n * \n */\n      __pyx_t_11 = __pyx_f_5numpy__util_dtypestring(__pyx_v_child, __pyx_v_f, __pyx_v_end, __pyx_v_offset); if (unlikely(__pyx_t_11 == NULL)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 849; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n      __pyx_v_f = __pyx_t_11;\n    }\n    __pyx_L9:;\n  }\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":850\n *             # so don't output it\n *             f = _util_dtypestring(child, f, end, offset)\n *     return f             # <<<<<<<<<<<<<<\n * \n * \n */\n  __pyx_r = __pyx_v_f;\n  goto __pyx_L0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":783\n *     return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)\n * \n * cdef inline char* _util_dtypestring(dtype descr, char* f, char* end, int* offset) except NULL:             # <<<<<<<<<<<<<<\n *     # Recursive utility function used in __getbuffer__ to get format\n *     # string. The new location in the format string is returned.\n */\n\n  /* function exit code */\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  __Pyx_XDECREF(__pyx_t_3);\n  __Pyx_XDECREF(__pyx_t_4);\n  __Pyx_AddTraceback(\"numpy._util_dtypestring\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n  __pyx_r = NULL;\n  __pyx_L0:;\n  __Pyx_XDECREF((PyObject *)__pyx_v_child);\n  __Pyx_XDECREF(__pyx_v_fields);\n  __Pyx_XDECREF(__pyx_v_childname);\n  __Pyx_XDECREF(__pyx_v_new_offset);\n  __Pyx_XDECREF(__pyx_v_t);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":966\n * \n * \n * cdef inline void set_array_base(ndarray arr, object base):             # <<<<<<<<<<<<<<\n *      cdef PyObject* baseptr\n *      if base is None:\n */\n\nstatic CYTHON_INLINE void __pyx_f_5numpy_set_array_base(PyArrayObject *__pyx_v_arr, PyObject *__pyx_v_base) {\n  PyObject *__pyx_v_baseptr;\n  __Pyx_RefNannyDeclarations\n  int __pyx_t_1;\n  int __pyx_t_2;\n  __Pyx_RefNannySetupContext(\"set_array_base\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":968\n * cdef inline void set_array_base(ndarray arr, object base):\n *      cdef PyObject* baseptr\n *      if base is None:             # <<<<<<<<<<<<<<\n *          baseptr = NULL\n *      else:\n */\n  __pyx_t_1 = (__pyx_v_base == Py_None);\n  __pyx_t_2 = (__pyx_t_1 != 0);\n  if (__pyx_t_2) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":969\n *      cdef PyObject* baseptr\n *      if base is None:\n *          baseptr = NULL             # <<<<<<<<<<<<<<\n *      else:\n *          Py_INCREF(base) # important to do this before decref below!\n */\n    __pyx_v_baseptr = NULL;\n    goto __pyx_L3;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":971\n *          baseptr = NULL\n *      else:\n *          Py_INCREF(base) # important to do this before decref below!             # <<<<<<<<<<<<<<\n *          baseptr = <PyObject*>base\n *      Py_XDECREF(arr.base)\n */\n    Py_INCREF(__pyx_v_base);\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":972\n *      else:\n *          Py_INCREF(base) # important to do this before decref below!\n *          baseptr = <PyObject*>base             # <<<<<<<<<<<<<<\n *      Py_XDECREF(arr.base)\n *      arr.base = baseptr\n */\n    __pyx_v_baseptr = ((PyObject *)__pyx_v_base);\n  }\n  __pyx_L3:;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":973\n *          Py_INCREF(base) # important to do this before decref below!\n *          baseptr = <PyObject*>base\n *      Py_XDECREF(arr.base)             # <<<<<<<<<<<<<<\n *      arr.base = baseptr\n * \n */\n  Py_XDECREF(__pyx_v_arr->base);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":974\n *          baseptr = <PyObject*>base\n *      Py_XDECREF(arr.base)\n *      arr.base = baseptr             # <<<<<<<<<<<<<<\n * \n * cdef inline object get_array_base(ndarray arr):\n */\n  __pyx_v_arr->base = __pyx_v_baseptr;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":966\n * \n * \n * cdef inline void set_array_base(ndarray arr, object base):             # <<<<<<<<<<<<<<\n *      cdef PyObject* baseptr\n *      if base is None:\n */\n\n  /* function exit code */\n  __Pyx_RefNannyFinishContext();\n}\n\n/* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":976\n *      arr.base = baseptr\n * \n * cdef inline object get_array_base(ndarray arr):             # <<<<<<<<<<<<<<\n *     if arr.base is NULL:\n *         return None\n */\n\nstatic CYTHON_INLINE PyObject *__pyx_f_5numpy_get_array_base(PyArrayObject *__pyx_v_arr) {\n  PyObject *__pyx_r = NULL;\n  __Pyx_RefNannyDeclarations\n  int __pyx_t_1;\n  __Pyx_RefNannySetupContext(\"get_array_base\", 0);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":977\n * \n * cdef inline object get_array_base(ndarray arr):\n *     if arr.base is NULL:             # <<<<<<<<<<<<<<\n *         return None\n *     else:\n */\n  __pyx_t_1 = ((__pyx_v_arr->base == NULL) != 0);\n  if (__pyx_t_1) {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":978\n * cdef inline object get_array_base(ndarray arr):\n *     if arr.base is NULL:\n *         return None             # <<<<<<<<<<<<<<\n *     else:\n *         return <object>arr.base\n */\n    __Pyx_XDECREF(__pyx_r);\n    __Pyx_INCREF(Py_None);\n    __pyx_r = Py_None;\n    goto __pyx_L0;\n  }\n  /*else*/ {\n\n    /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":980\n *         return None\n *     else:\n *         return <object>arr.base             # <<<<<<<<<<<<<<\n */\n    __Pyx_XDECREF(__pyx_r);\n    __Pyx_INCREF(((PyObject *)__pyx_v_arr->base));\n    __pyx_r = ((PyObject *)__pyx_v_arr->base);\n    goto __pyx_L0;\n  }\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":976\n *      arr.base = baseptr\n * \n * cdef inline object get_array_base(ndarray arr):             # <<<<<<<<<<<<<<\n *     if arr.base is NULL:\n *         return None\n */\n\n  /* function exit code */\n  __pyx_L0:;\n  __Pyx_XGIVEREF(__pyx_r);\n  __Pyx_RefNannyFinishContext();\n  return __pyx_r;\n}\n\nstatic PyMethodDef __pyx_methods[] = {\n  {0, 0, 0, 0}\n};\n\n#if PY_MAJOR_VERSION >= 3\nstatic struct PyModuleDef __pyx_moduledef = {\n  #if PY_VERSION_HEX < 0x03020000\n    { PyObject_HEAD_INIT(NULL) NULL, 0, NULL },\n  #else\n    PyModuleDef_HEAD_INIT,\n  #endif\n    __Pyx_NAMESTR(\"gpu_nms\"),\n    0, /* m_doc */\n    -1, /* m_size */\n    __pyx_methods /* m_methods */,\n    NULL, /* m_reload */\n    NULL, /* m_traverse */\n    NULL, /* m_clear */\n    NULL /* m_free */\n};\n#endif\n\nstatic __Pyx_StringTabEntry __pyx_string_tab[] = {\n  {&__pyx_kp_u_Format_string_allocated_too_shor, __pyx_k_Format_string_allocated_too_shor, sizeof(__pyx_k_Format_string_allocated_too_shor), 0, 1, 0, 0},\n  {&__pyx_kp_u_Format_string_allocated_too_shor_2, __pyx_k_Format_string_allocated_too_shor_2, sizeof(__pyx_k_Format_string_allocated_too_shor_2), 0, 1, 0, 0},\n  {&__pyx_kp_u_Non_native_byte_order_not_suppor, __pyx_k_Non_native_byte_order_not_suppor, sizeof(__pyx_k_Non_native_byte_order_not_suppor), 0, 1, 0, 0},\n  {&__pyx_n_s_RuntimeError, __pyx_k_RuntimeError, sizeof(__pyx_k_RuntimeError), 0, 0, 1, 1},\n  {&__pyx_n_s_ValueError, __pyx_k_ValueError, sizeof(__pyx_k_ValueError), 0, 0, 1, 1},\n  {&__pyx_n_s_argsort, __pyx_k_argsort, sizeof(__pyx_k_argsort), 0, 0, 1, 1},\n  {&__pyx_n_s_boxes_dim, __pyx_k_boxes_dim, sizeof(__pyx_k_boxes_dim), 0, 0, 1, 1},\n  {&__pyx_n_s_boxes_num, __pyx_k_boxes_num, sizeof(__pyx_k_boxes_num), 0, 0, 1, 1},\n  {&__pyx_n_s_dets, __pyx_k_dets, sizeof(__pyx_k_dets), 0, 0, 1, 1},\n  {&__pyx_n_s_device_id, __pyx_k_device_id, sizeof(__pyx_k_device_id), 0, 0, 1, 1},\n  {&__pyx_n_s_dtype, __pyx_k_dtype, sizeof(__pyx_k_dtype), 0, 0, 1, 1},\n  {&__pyx_n_s_gpu_nms, __pyx_k_gpu_nms, sizeof(__pyx_k_gpu_nms), 0, 0, 1, 1},\n  {&__pyx_n_s_import, __pyx_k_import, sizeof(__pyx_k_import), 0, 0, 1, 1},\n  {&__pyx_n_s_int32, __pyx_k_int32, sizeof(__pyx_k_int32), 0, 0, 1, 1},\n  {&__pyx_n_s_keep, __pyx_k_keep, sizeof(__pyx_k_keep), 0, 0, 1, 1},\n  {&__pyx_n_s_main, __pyx_k_main, sizeof(__pyx_k_main), 0, 0, 1, 1},\n  {&__pyx_kp_u_ndarray_is_not_C_contiguous, __pyx_k_ndarray_is_not_C_contiguous, sizeof(__pyx_k_ndarray_is_not_C_contiguous), 0, 1, 0, 0},\n  {&__pyx_kp_u_ndarray_is_not_Fortran_contiguou, __pyx_k_ndarray_is_not_Fortran_contiguou, sizeof(__pyx_k_ndarray_is_not_Fortran_contiguou), 0, 1, 0, 0},\n  {&__pyx_kp_s_nfs_yoda_xinleic_Inf_Code_Faste, __pyx_k_nfs_yoda_xinleic_Inf_Code_Faste, sizeof(__pyx_k_nfs_yoda_xinleic_Inf_Code_Faste), 0, 0, 1, 0},\n  {&__pyx_n_s_nms_gpu_nms, __pyx_k_nms_gpu_nms, sizeof(__pyx_k_nms_gpu_nms), 0, 0, 1, 1},\n  {&__pyx_n_s_np, __pyx_k_np, sizeof(__pyx_k_np), 0, 0, 1, 1},\n  {&__pyx_n_s_num_out, __pyx_k_num_out, sizeof(__pyx_k_num_out), 0, 0, 1, 1},\n  {&__pyx_n_s_numpy, __pyx_k_numpy, sizeof(__pyx_k_numpy), 0, 0, 1, 1},\n  {&__pyx_n_s_order, __pyx_k_order, sizeof(__pyx_k_order), 0, 0, 1, 1},\n  {&__pyx_n_s_pyx_getbuffer, __pyx_k_pyx_getbuffer, sizeof(__pyx_k_pyx_getbuffer), 0, 0, 1, 1},\n  {&__pyx_n_s_pyx_releasebuffer, __pyx_k_pyx_releasebuffer, sizeof(__pyx_k_pyx_releasebuffer), 0, 0, 1, 1},\n  {&__pyx_n_s_range, __pyx_k_range, sizeof(__pyx_k_range), 0, 0, 1, 1},\n  {&__pyx_n_s_scores, __pyx_k_scores, sizeof(__pyx_k_scores), 0, 0, 1, 1},\n  {&__pyx_n_s_sorted_dets, __pyx_k_sorted_dets, sizeof(__pyx_k_sorted_dets), 0, 0, 1, 1},\n  {&__pyx_n_s_test, __pyx_k_test, sizeof(__pyx_k_test), 0, 0, 1, 1},\n  {&__pyx_n_s_thresh, __pyx_k_thresh, sizeof(__pyx_k_thresh), 0, 0, 1, 1},\n  {&__pyx_kp_u_unknown_dtype_code_in_numpy_pxd, __pyx_k_unknown_dtype_code_in_numpy_pxd, sizeof(__pyx_k_unknown_dtype_code_in_numpy_pxd), 0, 1, 0, 0},\n  {&__pyx_n_s_zeros, __pyx_k_zeros, sizeof(__pyx_k_zeros), 0, 0, 1, 1},\n  {0, 0, 0, 0, 0, 0, 0}\n};\nstatic int __Pyx_InitCachedBuiltins(void) {\n  __pyx_builtin_ValueError = __Pyx_GetBuiltinName(__pyx_n_s_ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_builtin_range = __Pyx_GetBuiltinName(__pyx_n_s_range); if (!__pyx_builtin_range) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 228; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_builtin_RuntimeError = __Pyx_GetBuiltinName(__pyx_n_s_RuntimeError); if (!__pyx_builtin_RuntimeError) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  return 0;\n  __pyx_L1_error:;\n  return -1;\n}\n\nstatic int __Pyx_InitCachedConstants(void) {\n  __Pyx_RefNannyDeclarations\n  __Pyx_RefNannySetupContext(\"__Pyx_InitCachedConstants\", 0);\n\n  /* \"nms/gpu_nms.pyx\":24\n *         keep = np.zeros(boxes_num, dtype=np.int32)\n *     cdef np.ndarray[np.float32_t, ndim=1] \\\n *         scores = dets[:, 4]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.int_t, ndim=1] \\\n *         order = scores.argsort()[::-1]\n */\n  __pyx_slice_ = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice_)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_slice_);\n  __Pyx_GIVEREF(__pyx_slice_);\n  __pyx_tuple__2 = PyTuple_Pack(2, __pyx_slice_, __pyx_int_4); if (unlikely(!__pyx_tuple__2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__2);\n  __Pyx_GIVEREF(__pyx_tuple__2);\n\n  /* \"nms/gpu_nms.pyx\":26\n *         scores = dets[:, 4]\n *     cdef np.ndarray[np.int_t, ndim=1] \\\n *         order = scores.argsort()[::-1]             # <<<<<<<<<<<<<<\n *     cdef np.ndarray[np.float32_t, ndim=2] \\\n *         sorted_dets = dets[order, :]\n */\n  __pyx_slice__3 = PySlice_New(Py_None, Py_None, __pyx_int_neg_1); if (unlikely(!__pyx_slice__3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 26; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_slice__3);\n  __Pyx_GIVEREF(__pyx_slice__3);\n\n  /* \"nms/gpu_nms.pyx\":28\n *         order = scores.argsort()[::-1]\n *     cdef np.ndarray[np.float32_t, ndim=2] \\\n *         sorted_dets = dets[order, :]             # <<<<<<<<<<<<<<\n *     _nms(&keep[0], &num_out, &sorted_dets[0, 0], boxes_num, boxes_dim, thresh, device_id)\n *     keep = keep[:num_out]\n */\n  __pyx_slice__4 = PySlice_New(Py_None, Py_None, Py_None); if (unlikely(!__pyx_slice__4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_slice__4);\n  __Pyx_GIVEREF(__pyx_slice__4);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":215\n *             if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not C contiguous\")             # <<<<<<<<<<<<<<\n * \n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)\n */\n  __pyx_tuple__5 = PyTuple_Pack(1, __pyx_kp_u_ndarray_is_not_C_contiguous); if (unlikely(!__pyx_tuple__5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 215; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__5);\n  __Pyx_GIVEREF(__pyx_tuple__5);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":219\n *             if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)\n *                 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):\n *                 raise ValueError(u\"ndarray is not Fortran contiguous\")             # <<<<<<<<<<<<<<\n * \n *             info.buf = PyArray_DATA(self)\n */\n  __pyx_tuple__6 = PyTuple_Pack(1, __pyx_kp_u_ndarray_is_not_Fortran_contiguou); if (unlikely(!__pyx_tuple__6)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 219; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__6);\n  __Pyx_GIVEREF(__pyx_tuple__6);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":257\n *                 if ((descr.byteorder == c'>' and little_endian) or\n *                     (descr.byteorder == c'<' and not little_endian)):\n *                     raise ValueError(u\"Non-native byte order not supported\")             # <<<<<<<<<<<<<<\n *                 if   t == NPY_BYTE:        f = \"b\"\n *                 elif t == NPY_UBYTE:       f = \"B\"\n */\n  __pyx_tuple__7 = PyTuple_Pack(1, __pyx_kp_u_Non_native_byte_order_not_suppor); if (unlikely(!__pyx_tuple__7)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 257; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__7);\n  __Pyx_GIVEREF(__pyx_tuple__7);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":799\n * \n *         if (end - f) - <int>(new_offset - offset[0]) < 15:\n *             raise RuntimeError(u\"Format string allocated too short, see comment in numpy.pxd\")             # <<<<<<<<<<<<<<\n * \n *         if ((child.byteorder == c'>' and little_endian) or\n */\n  __pyx_tuple__8 = PyTuple_Pack(1, __pyx_kp_u_Format_string_allocated_too_shor); if (unlikely(!__pyx_tuple__8)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 799; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__8);\n  __Pyx_GIVEREF(__pyx_tuple__8);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":803\n *         if ((child.byteorder == c'>' and little_endian) or\n *             (child.byteorder == c'<' and not little_endian)):\n *             raise ValueError(u\"Non-native byte order not supported\")             # <<<<<<<<<<<<<<\n *             # One could encode it in the format string and have Cython\n *             # complain instead, BUT: < and > in format strings also imply\n */\n  __pyx_tuple__9 = PyTuple_Pack(1, __pyx_kp_u_Non_native_byte_order_not_suppor); if (unlikely(!__pyx_tuple__9)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 803; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__9);\n  __Pyx_GIVEREF(__pyx_tuple__9);\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":823\n *             t = child.type_num\n *             if end - f < 5:\n *                 raise RuntimeError(u\"Format string allocated too short.\")             # <<<<<<<<<<<<<<\n * \n *             # Until ticket #99 is fixed, use integers to avoid warnings\n */\n  __pyx_tuple__10 = PyTuple_Pack(1, __pyx_kp_u_Format_string_allocated_too_shor_2); if (unlikely(!__pyx_tuple__10)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 823; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__10);\n  __Pyx_GIVEREF(__pyx_tuple__10);\n\n  /* \"nms/gpu_nms.pyx\":16\n *     void _nms(np.int32_t*, int*, np.float32_t*, int, int, float, int)\n * \n * def gpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh,             # <<<<<<<<<<<<<<\n *             np.int32_t device_id=0):\n *     cdef int boxes_num = dets.shape[0]\n */\n  __pyx_tuple__11 = PyTuple_Pack(10, __pyx_n_s_dets, __pyx_n_s_thresh, __pyx_n_s_device_id, __pyx_n_s_boxes_num, __pyx_n_s_boxes_dim, __pyx_n_s_num_out, __pyx_n_s_keep, __pyx_n_s_scores, __pyx_n_s_order, __pyx_n_s_sorted_dets); if (unlikely(!__pyx_tuple__11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_tuple__11);\n  __Pyx_GIVEREF(__pyx_tuple__11);\n  __pyx_codeobj__12 = (PyObject*)__Pyx_PyCode_New(3, 0, 10, 0, 0, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple__11, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_nfs_yoda_xinleic_Inf_Code_Faste, __pyx_n_s_gpu_nms, 16, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__12)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_RefNannyFinishContext();\n  return 0;\n  __pyx_L1_error:;\n  __Pyx_RefNannyFinishContext();\n  return -1;\n}\n\nstatic int __Pyx_InitGlobals(void) {\n  if (__Pyx_InitStrings(__pyx_string_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  __pyx_int_4 = PyInt_FromLong(4); if (unlikely(!__pyx_int_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_int_neg_1 = PyInt_FromLong(-1); if (unlikely(!__pyx_int_neg_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  return 0;\n  __pyx_L1_error:;\n  return -1;\n}\n\n#if PY_MAJOR_VERSION < 3\nPyMODINIT_FUNC initgpu_nms(void); /*proto*/\nPyMODINIT_FUNC initgpu_nms(void)\n#else\nPyMODINIT_FUNC PyInit_gpu_nms(void); /*proto*/\nPyMODINIT_FUNC PyInit_gpu_nms(void)\n#endif\n{\n  PyObject *__pyx_t_1 = NULL;\n  int __pyx_lineno = 0;\n  const char *__pyx_filename = NULL;\n  int __pyx_clineno = 0;\n  __Pyx_RefNannyDeclarations\n  #if CYTHON_REFNANNY\n  __Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"refnanny\");\n  if (!__Pyx_RefNanny) {\n      PyErr_Clear();\n      __Pyx_RefNanny = __Pyx_RefNannyImportAPI(\"Cython.Runtime.refnanny\");\n      if (!__Pyx_RefNanny)\n          Py_FatalError(\"failed to import 'refnanny' module\");\n  }\n  #endif\n  __Pyx_RefNannySetupContext(\"PyMODINIT_FUNC PyInit_gpu_nms(void)\", 0);\n  if ( __Pyx_check_binary_version() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_empty_tuple = PyTuple_New(0); if (unlikely(!__pyx_empty_tuple)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_empty_bytes = PyBytes_FromStringAndSize(\"\", 0); if (unlikely(!__pyx_empty_bytes)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #ifdef __Pyx_CyFunction_USED\n  if (__Pyx_CyFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #endif\n  #ifdef __Pyx_FusedFunction_USED\n  if (__pyx_FusedFunction_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #endif\n  #ifdef __Pyx_Generator_USED\n  if (__pyx_Generator_init() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #endif\n  /*--- Library function declarations ---*/\n  /*--- Threads initialization code ---*/\n  #if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS\n  #ifdef WITH_THREAD /* Python build with threading support? */\n  PyEval_InitThreads();\n  #endif\n  #endif\n  /*--- Module creation code ---*/\n  #if PY_MAJOR_VERSION < 3\n  __pyx_m = Py_InitModule4(__Pyx_NAMESTR(\"gpu_nms\"), __pyx_methods, 0, 0, PYTHON_API_VERSION); Py_XINCREF(__pyx_m);\n  #else\n  __pyx_m = PyModule_Create(&__pyx_moduledef);\n  #endif\n  if (unlikely(!__pyx_m)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_d = PyModule_GetDict(__pyx_m); if (unlikely(!__pyx_d)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  Py_INCREF(__pyx_d);\n  __pyx_b = PyImport_AddModule(__Pyx_NAMESTR(__Pyx_BUILTIN_MODULE_NAME)); if (unlikely(!__pyx_b)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #if CYTHON_COMPILING_IN_PYPY\n  Py_INCREF(__pyx_b);\n  #endif\n  if (__Pyx_SetAttrString(__pyx_m, \"__builtins__\", __pyx_b) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  /*--- Initialize various global constants etc. ---*/\n  if (unlikely(__Pyx_InitGlobals() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT)\n  if (__Pyx_init_sys_getdefaultencoding_params() < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  #endif\n  if (__pyx_module_is_main_nms__gpu_nms) {\n    if (__Pyx_SetAttrString(__pyx_m, \"__name__\", __pyx_n_s_main) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;};\n  }\n  #if PY_MAJOR_VERSION >= 3\n  {\n    PyObject *modules = PyImport_GetModuleDict(); if (unlikely(!modules)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    if (!PyDict_GetItemString(modules, \"nms.gpu_nms\")) {\n      if (unlikely(PyDict_SetItemString(modules, \"nms.gpu_nms\", __pyx_m) < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n  }\n  #endif\n  /*--- Builtin init code ---*/\n  if (unlikely(__Pyx_InitCachedBuiltins() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  /*--- Constants init code ---*/\n  if (unlikely(__Pyx_InitCachedConstants() < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  /*--- Global init code ---*/\n  /*--- Variable export code ---*/\n  /*--- Function export code ---*/\n  /*--- Type init code ---*/\n  /*--- Type import code ---*/\n  __pyx_ptype_7cpython_4type_type = __Pyx_ImportType(__Pyx_BUILTIN_MODULE_NAME, \"type\", \n  #if CYTHON_COMPILING_IN_PYPY\n  sizeof(PyTypeObject),\n  #else\n  sizeof(PyHeapTypeObject),\n  #endif\n  0); if (unlikely(!__pyx_ptype_7cpython_4type_type)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_ptype_5numpy_dtype = __Pyx_ImportType(\"numpy\", \"dtype\", sizeof(PyArray_Descr), 0); if (unlikely(!__pyx_ptype_5numpy_dtype)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 155; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_ptype_5numpy_flatiter = __Pyx_ImportType(\"numpy\", \"flatiter\", sizeof(PyArrayIterObject), 0); if (unlikely(!__pyx_ptype_5numpy_flatiter)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 165; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_ptype_5numpy_broadcast = __Pyx_ImportType(\"numpy\", \"broadcast\", sizeof(PyArrayMultiIterObject), 0); if (unlikely(!__pyx_ptype_5numpy_broadcast)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 169; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_ptype_5numpy_ndarray = __Pyx_ImportType(\"numpy\", \"ndarray\", sizeof(PyArrayObject), 0); if (unlikely(!__pyx_ptype_5numpy_ndarray)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 178; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __pyx_ptype_5numpy_ufunc = __Pyx_ImportType(\"numpy\", \"ufunc\", sizeof(PyUFuncObject), 0); if (unlikely(!__pyx_ptype_5numpy_ufunc)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 861; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  /*--- Variable import code ---*/\n  /*--- Function import code ---*/\n  /*--- Execution code ---*/\n\n  /* \"nms/gpu_nms.pyx\":8\n * # --------------------------------------------------------\n * \n * import numpy as np             # <<<<<<<<<<<<<<\n * cimport numpy as np\n * \n */\n  __pyx_t_1 = __Pyx_Import(__pyx_n_s_numpy, 0, -1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  if (PyDict_SetItem(__pyx_d, __pyx_n_s_np, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n\n  /* \"nms/gpu_nms.pyx\":11\n * cimport numpy as np\n * \n * assert sizeof(int) == sizeof(np.int32_t)             # <<<<<<<<<<<<<<\n * \n * cdef extern from \"gpu_nms.hpp\":\n */\n  #ifndef CYTHON_WITHOUT_ASSERTIONS\n  if (unlikely(!Py_OptimizeFlag)) {\n    if (unlikely(!(((sizeof(int)) == (sizeof(__pyx_t_5numpy_int32_t))) != 0))) {\n      PyErr_SetNone(PyExc_AssertionError);\n      {__pyx_filename = __pyx_f[0]; __pyx_lineno = 11; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n    }\n  }\n  #endif\n\n  /* \"nms/gpu_nms.pyx\":16\n *     void _nms(np.int32_t*, int*, np.float32_t*, int, int, float, int)\n * \n * def gpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh,             # <<<<<<<<<<<<<<\n *             np.int32_t device_id=0):\n *     cdef int boxes_num = dets.shape[0]\n */\n  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_3nms_7gpu_nms_1gpu_nms, NULL, __pyx_n_s_nms_gpu_nms); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  if (PyDict_SetItem(__pyx_d, __pyx_n_s_gpu_nms, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n\n  /* \"nms/gpu_nms.pyx\":1\n * # --------------------------------------------------------             # <<<<<<<<<<<<<<\n * # Faster R-CNN\n * # Copyright (c) 2015 Microsoft\n */\n  __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_GOTREF(__pyx_t_1);\n  if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}\n  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n\n  /* \"/home/xinleic/anaconda/lib/python2.7/site-packages/Cython/Includes/numpy/__init__.pxd\":976\n *      arr.base = baseptr\n * \n * cdef inline object get_array_base(ndarray arr):             # <<<<<<<<<<<<<<\n *     if arr.base is NULL:\n *         return None\n */\n  goto __pyx_L0;\n  __pyx_L1_error:;\n  __Pyx_XDECREF(__pyx_t_1);\n  if (__pyx_m) {\n    __Pyx_AddTraceback(\"init nms.gpu_nms\", __pyx_clineno, __pyx_lineno, __pyx_filename);\n    Py_DECREF(__pyx_m); __pyx_m = 0;\n  } else if (!PyErr_Occurred()) {\n    PyErr_SetString(PyExc_ImportError, \"init nms.gpu_nms\");\n  }\n  __pyx_L0:;\n  __Pyx_RefNannyFinishContext();\n  #if PY_MAJOR_VERSION < 3\n  return;\n  #else\n  return __pyx_m;\n  #endif\n}\n\n/* Runtime support code */\n#if CYTHON_REFNANNY\nstatic __Pyx_RefNannyAPIStruct *__Pyx_RefNannyImportAPI(const char *modname) {\n    PyObject *m = NULL, *p = NULL;\n    void *r = NULL;\n    m = PyImport_ImportModule((char *)modname);\n    if (!m) goto end;\n    p = PyObject_GetAttrString(m, (char *)\"RefNannyAPI\");\n    if (!p) goto end;\n    r = PyLong_AsVoidPtr(p);\nend:\n    Py_XDECREF(p);\n    Py_XDECREF(m);\n    return (__Pyx_RefNannyAPIStruct *)r;\n}\n#endif /* CYTHON_REFNANNY */\n\nstatic void __Pyx_RaiseArgtupleInvalid(\n    const char* func_name,\n    int exact,\n    Py_ssize_t num_min,\n    Py_ssize_t num_max,\n    Py_ssize_t num_found)\n{\n    Py_ssize_t num_expected;\n    const char *more_or_less;\n    if (num_found < num_min) {\n        num_expected = num_min;\n        more_or_less = \"at least\";\n    } else {\n        num_expected = num_max;\n        more_or_less = \"at most\";\n    }\n    if (exact) {\n        more_or_less = \"exactly\";\n    }\n    PyErr_Format(PyExc_TypeError,\n                 \"%.200s() takes %.8s %\" CYTHON_FORMAT_SSIZE_T \"d positional argument%.1s (%\" CYTHON_FORMAT_SSIZE_T \"d given)\",\n                 func_name, more_or_less, num_expected,\n                 (num_expected == 1) ? \"\" : \"s\", num_found);\n}\n\nstatic void __Pyx_RaiseDoubleKeywordsError(\n    const char* func_name,\n    PyObject* kw_name)\n{\n    PyErr_Format(PyExc_TypeError,\n        #if PY_MAJOR_VERSION >= 3\n        \"%s() got multiple values for keyword argument '%U'\", func_name, kw_name);\n        #else\n        \"%s() got multiple values for keyword argument '%s'\", func_name,\n        PyString_AsString(kw_name));\n        #endif\n}\n\nstatic int __Pyx_ParseOptionalKeywords(\n    PyObject *kwds,\n    PyObject **argnames[],\n    PyObject *kwds2,\n    PyObject *values[],\n    Py_ssize_t num_pos_args,\n    const char* function_name)\n{\n    PyObject *key = 0, *value = 0;\n    Py_ssize_t pos = 0;\n    PyObject*** name;\n    PyObject*** first_kw_arg = argnames + num_pos_args;\n    while (PyDict_Next(kwds, &pos, &key, &value)) {\n        name = first_kw_arg;\n        while (*name && (**name != key)) name++;\n        if (*name) {\n            values[name-argnames] = value;\n            continue;\n        }\n        name = first_kw_arg;\n        #if PY_MAJOR_VERSION < 3\n        if (likely(PyString_CheckExact(key)) || likely(PyString_Check(key))) {\n            while (*name) {\n                if ((CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**name) == PyString_GET_SIZE(key))\n                        && _PyString_Eq(**name, key)) {\n                    values[name-argnames] = value;\n                    break;\n                }\n                name++;\n            }\n            if (*name) continue;\n            else {\n                PyObject*** argname = argnames;\n                while (argname != first_kw_arg) {\n                    if ((**argname == key) || (\n                            (CYTHON_COMPILING_IN_PYPY || PyString_GET_SIZE(**argname) == PyString_GET_SIZE(key))\n                             && _PyString_Eq(**argname, key))) {\n                        goto arg_passed_twice;\n                    }\n                    argname++;\n                }\n            }\n        } else\n        #endif\n        if (likely(PyUnicode_Check(key))) {\n            while (*name) {\n                int cmp = (**name == key) ? 0 :\n                #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3\n                    (PyUnicode_GET_SIZE(**name) != PyUnicode_GET_SIZE(key)) ? 1 :\n                #endif\n                    PyUnicode_Compare(**name, key);\n                if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;\n                if (cmp == 0) {\n                    values[name-argnames] = value;\n                    break;\n                }\n                name++;\n            }\n            if (*name) continue;\n            else {\n                PyObject*** argname = argnames;\n                while (argname != first_kw_arg) {\n                    int cmp = (**argname == key) ? 0 :\n                    #if !CYTHON_COMPILING_IN_PYPY && PY_MAJOR_VERSION >= 3\n                        (PyUnicode_GET_SIZE(**argname) != PyUnicode_GET_SIZE(key)) ? 1 :\n                    #endif\n                        PyUnicode_Compare(**argname, key);\n                    if (cmp < 0 && unlikely(PyErr_Occurred())) goto bad;\n                    if (cmp == 0) goto arg_passed_twice;\n                    argname++;\n                }\n            }\n        } else\n            goto invalid_keyword_type;\n        if (kwds2) {\n            if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad;\n        } else {\n            goto invalid_keyword;\n        }\n    }\n    return 0;\narg_passed_twice:\n    __Pyx_RaiseDoubleKeywordsError(function_name, key);\n    goto bad;\ninvalid_keyword_type:\n    PyErr_Format(PyExc_TypeError,\n        \"%.200s() keywords must be strings\", function_name);\n    goto bad;\ninvalid_keyword:\n    PyErr_Format(PyExc_TypeError,\n    #if PY_MAJOR_VERSION < 3\n        \"%.200s() got an unexpected keyword argument '%.200s'\",\n        function_name, PyString_AsString(key));\n    #else\n        \"%s() got an unexpected keyword argument '%U'\",\n        function_name, key);\n    #endif\nbad:\n    return -1;\n}\n\nstatic void __Pyx_RaiseArgumentTypeInvalid(const char* name, PyObject *obj, PyTypeObject *type) {\n    PyErr_Format(PyExc_TypeError,\n        \"Argument '%.200s' has incorrect type (expected %.200s, got %.200s)\",\n        name, type->tp_name, Py_TYPE(obj)->tp_name);\n}\nstatic CYTHON_INLINE int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed,\n    const char *name, int exact)\n{\n    if (unlikely(!type)) {\n        PyErr_SetString(PyExc_SystemError, \"Missing type object\");\n        return 0;\n    }\n    if (none_allowed && obj == Py_None) return 1;\n    else if (exact) {\n        if (likely(Py_TYPE(obj) == type)) return 1;\n        #if PY_MAJOR_VERSION == 2\n        else if ((type == &PyBaseString_Type) && likely(__Pyx_PyBaseString_CheckExact(obj))) return 1;\n        #endif\n    }\n    else {\n        if (likely(PyObject_TypeCheck(obj, type))) return 1;\n    }\n    __Pyx_RaiseArgumentTypeInvalid(name, obj, type);\n    return 0;\n}\n\nstatic CYTHON_INLINE int __Pyx_IsLittleEndian(void) {\n  unsigned int n = 1;\n  return *(unsigned char*)(&n) != 0;\n}\nstatic void __Pyx_BufFmt_Init(__Pyx_BufFmt_Context* ctx,\n                              __Pyx_BufFmt_StackElem* stack,\n                              __Pyx_TypeInfo* type) {\n  stack[0].field = &ctx->root;\n  stack[0].parent_offset = 0;\n  ctx->root.type = type;\n  ctx->root.name = \"buffer dtype\";\n  ctx->root.offset = 0;\n  ctx->head = stack;\n  ctx->head->field = &ctx->root;\n  ctx->fmt_offset = 0;\n  ctx->head->parent_offset = 0;\n  ctx->new_packmode = '@';\n  ctx->enc_packmode = '@';\n  ctx->new_count = 1;\n  ctx->enc_count = 0;\n  ctx->enc_type = 0;\n  ctx->is_complex = 0;\n  ctx->is_valid_array = 0;\n  ctx->struct_alignment = 0;\n  while (type->typegroup == 'S') {\n    ++ctx->head;\n    ctx->head->field = type->fields;\n    ctx->head->parent_offset = 0;\n    type = type->fields->type;\n  }\n}\nstatic int __Pyx_BufFmt_ParseNumber(const char** ts) {\n    int count;\n    const char* t = *ts;\n    if (*t < '0' || *t > '9') {\n      return -1;\n    } else {\n        count = *t++ - '0';\n        while (*t >= '0' && *t < '9') {\n            count *= 10;\n            count += *t++ - '0';\n        }\n    }\n    *ts = t;\n    return count;\n}\nstatic int __Pyx_BufFmt_ExpectNumber(const char **ts) {\n    int number = __Pyx_BufFmt_ParseNumber(ts);\n    if (number == -1) /* First char was not a digit */\n        PyErr_Format(PyExc_ValueError,\\\n                     \"Does not understand character buffer dtype format string ('%c')\", **ts);\n    return number;\n}\nstatic void __Pyx_BufFmt_RaiseUnexpectedChar(char ch) {\n  PyErr_Format(PyExc_ValueError,\n               \"Unexpected format string character: '%c'\", ch);\n}\nstatic const char* __Pyx_BufFmt_DescribeTypeChar(char ch, int is_complex) {\n  switch (ch) {\n    case 'c': return \"'char'\";\n    case 'b': return \"'signed char'\";\n    case 'B': return \"'unsigned char'\";\n    case 'h': return \"'short'\";\n    case 'H': return \"'unsigned short'\";\n    case 'i': return \"'int'\";\n    case 'I': return \"'unsigned int'\";\n    case 'l': return \"'long'\";\n    case 'L': return \"'unsigned long'\";\n    case 'q': return \"'long long'\";\n    case 'Q': return \"'unsigned long long'\";\n    case 'f': return (is_complex ? \"'complex float'\" : \"'float'\");\n    case 'd': return (is_complex ? \"'complex double'\" : \"'double'\");\n    case 'g': return (is_complex ? \"'complex long double'\" : \"'long double'\");\n    case 'T': return \"a struct\";\n    case 'O': return \"Python object\";\n    case 'P': return \"a pointer\";\n    case 's': case 'p': return \"a string\";\n    case 0: return \"end\";\n    default: return \"unparseable format string\";\n  }\n}\nstatic size_t __Pyx_BufFmt_TypeCharToStandardSize(char ch, int is_complex) {\n  switch (ch) {\n    case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1;\n    case 'h': case 'H': return 2;\n    case 'i': case 'I': case 'l': case 'L': return 4;\n    case 'q': case 'Q': return 8;\n    case 'f': return (is_complex ? 8 : 4);\n    case 'd': return (is_complex ? 16 : 8);\n    case 'g': {\n      PyErr_SetString(PyExc_ValueError, \"Python does not define a standard format string size for long double ('g')..\");\n      return 0;\n    }\n    case 'O': case 'P': return sizeof(void*);\n    default:\n      __Pyx_BufFmt_RaiseUnexpectedChar(ch);\n      return 0;\n    }\n}\nstatic size_t __Pyx_BufFmt_TypeCharToNativeSize(char ch, int is_complex) {\n  switch (ch) {\n    case 'c': case 'b': case 'B': case 's': case 'p': return 1;\n    case 'h': case 'H': return sizeof(short);\n    case 'i': case 'I': return sizeof(int);\n    case 'l': case 'L': return sizeof(long);\n    #ifdef HAVE_LONG_LONG\n    case 'q': case 'Q': return sizeof(PY_LONG_LONG);\n    #endif\n    case 'f': return sizeof(float) * (is_complex ? 2 : 1);\n    case 'd': return sizeof(double) * (is_complex ? 2 : 1);\n    case 'g': return sizeof(long double) * (is_complex ? 2 : 1);\n    case 'O': case 'P': return sizeof(void*);\n    default: {\n      __Pyx_BufFmt_RaiseUnexpectedChar(ch);\n      return 0;\n    }\n  }\n}\ntypedef struct { char c; short x; } __Pyx_st_short;\ntypedef struct { char c; int x; } __Pyx_st_int;\ntypedef struct { char c; long x; } __Pyx_st_long;\ntypedef struct { char c; float x; } __Pyx_st_float;\ntypedef struct { char c; double x; } __Pyx_st_double;\ntypedef struct { char c; long double x; } __Pyx_st_longdouble;\ntypedef struct { char c; void *x; } __Pyx_st_void_p;\n#ifdef HAVE_LONG_LONG\ntypedef struct { char c; PY_LONG_LONG x; } __Pyx_st_longlong;\n#endif\nstatic size_t __Pyx_BufFmt_TypeCharToAlignment(char ch, CYTHON_UNUSED int is_complex) {\n  switch (ch) {\n    case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1;\n    case 'h': case 'H': return sizeof(__Pyx_st_short) - sizeof(short);\n    case 'i': case 'I': return sizeof(__Pyx_st_int) - sizeof(int);\n    case 'l': case 'L': return sizeof(__Pyx_st_long) - sizeof(long);\n#ifdef HAVE_LONG_LONG\n    case 'q': case 'Q': return sizeof(__Pyx_st_longlong) - sizeof(PY_LONG_LONG);\n#endif\n    case 'f': return sizeof(__Pyx_st_float) - sizeof(float);\n    case 'd': return sizeof(__Pyx_st_double) - sizeof(double);\n    case 'g': return sizeof(__Pyx_st_longdouble) - sizeof(long double);\n    case 'P': case 'O': return sizeof(__Pyx_st_void_p) - sizeof(void*);\n    default:\n      __Pyx_BufFmt_RaiseUnexpectedChar(ch);\n      return 0;\n    }\n}\n/* These are for computing the padding at the end of the struct to align\n   on the first member of the struct. This will probably the same as above,\n   but we don't have any guarantees.\n */\ntypedef struct { short x; char c; } __Pyx_pad_short;\ntypedef struct { int x; char c; } __Pyx_pad_int;\ntypedef struct { long x; char c; } __Pyx_pad_long;\ntypedef struct { float x; char c; } __Pyx_pad_float;\ntypedef struct { double x; char c; } __Pyx_pad_double;\ntypedef struct { long double x; char c; } __Pyx_pad_longdouble;\ntypedef struct { void *x; char c; } __Pyx_pad_void_p;\n#ifdef HAVE_LONG_LONG\ntypedef struct { PY_LONG_LONG x; char c; } __Pyx_pad_longlong;\n#endif\nstatic size_t __Pyx_BufFmt_TypeCharToPadding(char ch, CYTHON_UNUSED int is_complex) {\n  switch (ch) {\n    case '?': case 'c': case 'b': case 'B': case 's': case 'p': return 1;\n    case 'h': case 'H': return sizeof(__Pyx_pad_short) - sizeof(short);\n    case 'i': case 'I': return sizeof(__Pyx_pad_int) - sizeof(int);\n    case 'l': case 'L': return sizeof(__Pyx_pad_long) - sizeof(long);\n#ifdef HAVE_LONG_LONG\n    case 'q': case 'Q': return sizeof(__Pyx_pad_longlong) - sizeof(PY_LONG_LONG);\n#endif\n    case 'f': return sizeof(__Pyx_pad_float) - sizeof(float);\n    case 'd': return sizeof(__Pyx_pad_double) - sizeof(double);\n    case 'g': return sizeof(__Pyx_pad_longdouble) - sizeof(long double);\n    case 'P': case 'O': return sizeof(__Pyx_pad_void_p) - sizeof(void*);\n    default:\n      __Pyx_BufFmt_RaiseUnexpectedChar(ch);\n      return 0;\n    }\n}\nstatic char __Pyx_BufFmt_TypeCharToGroup(char ch, int is_complex) {\n  switch (ch) {\n    case 'c':\n        return 'H';\n    case 'b': case 'h': case 'i':\n    case 'l': case 'q': case 's': case 'p':\n        return 'I';\n    case 'B': case 'H': case 'I': case 'L': case 'Q':\n        return 'U';\n    case 'f': case 'd': case 'g':\n        return (is_complex ? 'C' : 'R');\n    case 'O':\n        return 'O';\n    case 'P':\n        return 'P';\n    default: {\n      __Pyx_BufFmt_RaiseUnexpectedChar(ch);\n      return 0;\n    }\n  }\n}\nstatic void __Pyx_BufFmt_RaiseExpected(__Pyx_BufFmt_Context* ctx) {\n  if (ctx->head == NULL || ctx->head->field == &ctx->root) {\n    const char* expected;\n    const char* quote;\n    if (ctx->head == NULL) {\n      expected = \"end\";\n      quote = \"\";\n    } else {\n      expected = ctx->head->field->type->name;\n      quote = \"'\";\n    }\n    PyErr_Format(PyExc_ValueError,\n                 \"Buffer dtype mismatch, expected %s%s%s but got %s\",\n                 quote, expected, quote,\n                 __Pyx_BufFmt_DescribeTypeChar(ctx->enc_type, ctx->is_complex));\n  } else {\n    __Pyx_StructField* field = ctx->head->field;\n    __Pyx_StructField* parent = (ctx->head - 1)->field;\n    PyErr_Format(PyExc_ValueError,\n                 \"Buffer dtype mismatch, expected '%s' but got %s in '%s.%s'\",\n                 field->type->name, __Pyx_BufFmt_DescribeTypeChar(ctx->enc_type, ctx->is_complex),\n                 parent->type->name, field->name);\n  }\n}\nstatic int __Pyx_BufFmt_ProcessTypeChunk(__Pyx_BufFmt_Context* ctx) {\n  char group;\n  size_t size, offset, arraysize = 1;\n  if (ctx->enc_type == 0) return 0;\n  if (ctx->head->field->type->arraysize[0]) {\n    int i, ndim = 0;\n    if (ctx->enc_type == 's' || ctx->enc_type == 'p') {\n        ctx->is_valid_array = ctx->head->field->type->ndim == 1;\n        ndim = 1;\n        if (ctx->enc_count != ctx->head->field->type->arraysize[0]) {\n            PyErr_Format(PyExc_ValueError,\n                         \"Expected a dimension of size %zu, got %zu\",\n                         ctx->head->field->type->arraysize[0], ctx->enc_count);\n            return -1;\n        }\n    }\n    if (!ctx->is_valid_array) {\n      PyErr_Format(PyExc_ValueError, \"Expected %d dimensions, got %d\",\n                   ctx->head->field->type->ndim, ndim);\n      return -1;\n    }\n    for (i = 0; i < ctx->head->field->type->ndim; i++) {\n      arraysize *= ctx->head->field->type->arraysize[i];\n    }\n    ctx->is_valid_array = 0;\n    ctx->enc_count = 1;\n  }\n  group = __Pyx_BufFmt_TypeCharToGroup(ctx->enc_type, ctx->is_complex);\n  do {\n    __Pyx_StructField* field = ctx->head->field;\n    __Pyx_TypeInfo* type = field->type;\n    if (ctx->enc_packmode == '@' || ctx->enc_packmode == '^') {\n      size = __Pyx_BufFmt_TypeCharToNativeSize(ctx->enc_type, ctx->is_complex);\n    } else {\n      size = __Pyx_BufFmt_TypeCharToStandardSize(ctx->enc_type, ctx->is_complex);\n    }\n    if (ctx->enc_packmode == '@') {\n      size_t align_at = __Pyx_BufFmt_TypeCharToAlignment(ctx->enc_type, ctx->is_complex);\n      size_t align_mod_offset;\n      if (align_at == 0) return -1;\n      align_mod_offset = ctx->fmt_offset % align_at;\n      if (align_mod_offset > 0) ctx->fmt_offset += align_at - align_mod_offset;\n      if (ctx->struct_alignment == 0)\n          ctx->struct_alignment = __Pyx_BufFmt_TypeCharToPadding(ctx->enc_type,\n                                                                 ctx->is_complex);\n    }\n    if (type->size != size || type->typegroup != group) {\n      if (type->typegroup == 'C' && type->fields != NULL) {\n        size_t parent_offset = ctx->head->parent_offset + field->offset;\n        ++ctx->head;\n        ctx->head->field = type->fields;\n        ctx->head->parent_offset = parent_offset;\n        continue;\n      }\n      if ((type->typegroup == 'H' || group == 'H') && type->size == size) {\n      } else {\n          __Pyx_BufFmt_RaiseExpected(ctx);\n          return -1;\n      }\n    }\n    offset = ctx->head->parent_offset + field->offset;\n    if (ctx->fmt_offset != offset) {\n      PyErr_Format(PyExc_ValueError,\n                   \"Buffer dtype mismatch; next field is at offset %\" CYTHON_FORMAT_SSIZE_T \"d but %\" CYTHON_FORMAT_SSIZE_T \"d expected\",\n                   (Py_ssize_t)ctx->fmt_offset, (Py_ssize_t)offset);\n      return -1;\n    }\n    ctx->fmt_offset += size;\n    if (arraysize)\n      ctx->fmt_offset += (arraysize - 1) * size;\n    --ctx->enc_count; /* Consume from buffer string */\n    while (1) {\n      if (field == &ctx->root) {\n        ctx->head = NULL;\n        if (ctx->enc_count != 0) {\n          __Pyx_BufFmt_RaiseExpected(ctx);\n          return -1;\n        }\n        break; /* breaks both loops as ctx->enc_count == 0 */\n      }\n      ctx->head->field = ++field;\n      if (field->type == NULL) {\n        --ctx->head;\n        field = ctx->head->field;\n        continue;\n      } else if (field->type->typegroup == 'S') {\n        size_t parent_offset = ctx->head->parent_offset + field->offset;\n        if (field->type->fields->type == NULL) continue; /* empty struct */\n        field = field->type->fields;\n        ++ctx->head;\n        ctx->head->field = field;\n        ctx->head->parent_offset = parent_offset;\n        break;\n      } else {\n        break;\n      }\n    }\n  } while (ctx->enc_count);\n  ctx->enc_type = 0;\n  ctx->is_complex = 0;\n  return 0;\n}\nstatic CYTHON_INLINE PyObject *\n__pyx_buffmt_parse_array(__Pyx_BufFmt_Context* ctx, const char** tsp)\n{\n    const char *ts = *tsp;\n    int i = 0, number;\n    int ndim = ctx->head->field->type->ndim;\n;\n    ++ts;\n    if (ctx->new_count != 1) {\n        PyErr_SetString(PyExc_ValueError,\n                        \"Cannot handle repeated arrays in format string\");\n        return NULL;\n    }\n    if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n    while (*ts && *ts != ')') {\n        switch (*ts) {\n            case ' ': case '\\f': case '\\r': case '\\n': case '\\t': case '\\v':  continue;\n            default:  break;  /* not a 'break' in the loop */\n        }\n        number = __Pyx_BufFmt_ExpectNumber(&ts);\n        if (number == -1) return NULL;\n        if (i < ndim && (size_t) number != ctx->head->field->type->arraysize[i])\n            return PyErr_Format(PyExc_ValueError,\n                        \"Expected a dimension of size %zu, got %d\",\n                        ctx->head->field->type->arraysize[i], number);\n        if (*ts != ',' && *ts != ')')\n            return PyErr_Format(PyExc_ValueError,\n                                \"Expected a comma in format string, got '%c'\", *ts);\n        if (*ts == ',') ts++;\n        i++;\n    }\n    if (i != ndim)\n        return PyErr_Format(PyExc_ValueError, \"Expected %d dimension(s), got %d\",\n                            ctx->head->field->type->ndim, i);\n    if (!*ts) {\n        PyErr_SetString(PyExc_ValueError,\n                        \"Unexpected end of format string, expected ')'\");\n        return NULL;\n    }\n    ctx->is_valid_array = 1;\n    ctx->new_count = 1;\n    *tsp = ++ts;\n    return Py_None;\n}\nstatic const char* __Pyx_BufFmt_CheckString(__Pyx_BufFmt_Context* ctx, const char* ts) {\n  int got_Z = 0;\n  while (1) {\n    switch(*ts) {\n      case 0:\n        if (ctx->enc_type != 0 && ctx->head == NULL) {\n          __Pyx_BufFmt_RaiseExpected(ctx);\n          return NULL;\n        }\n        if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n        if (ctx->head != NULL) {\n          __Pyx_BufFmt_RaiseExpected(ctx);\n          return NULL;\n        }\n                return ts;\n      case ' ':\n      case 10:\n      case 13:\n        ++ts;\n        break;\n      case '<':\n        if (!__Pyx_IsLittleEndian()) {\n          PyErr_SetString(PyExc_ValueError, \"Little-endian buffer not supported on big-endian compiler\");\n          return NULL;\n        }\n        ctx->new_packmode = '=';\n        ++ts;\n        break;\n      case '>':\n      case '!':\n        if (__Pyx_IsLittleEndian()) {\n          PyErr_SetString(PyExc_ValueError, \"Big-endian buffer not supported on little-endian compiler\");\n          return NULL;\n        }\n        ctx->new_packmode = '=';\n        ++ts;\n        break;\n      case '=':\n      case '@':\n      case '^':\n        ctx->new_packmode = *ts++;\n        break;\n      case 'T': /* substruct */\n        {\n          const char* ts_after_sub;\n          size_t i, struct_count = ctx->new_count;\n          size_t struct_alignment = ctx->struct_alignment;\n          ctx->new_count = 1;\n          ++ts;\n          if (*ts != '{') {\n            PyErr_SetString(PyExc_ValueError, \"Buffer acquisition: Expected '{' after 'T'\");\n            return NULL;\n          }\n          if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n          ctx->enc_type = 0; /* Erase processed last struct element */\n          ctx->enc_count = 0;\n          ctx->struct_alignment = 0;\n          ++ts;\n          ts_after_sub = ts;\n          for (i = 0; i != struct_count; ++i) {\n            ts_after_sub = __Pyx_BufFmt_CheckString(ctx, ts);\n            if (!ts_after_sub) return NULL;\n          }\n          ts = ts_after_sub;\n          if (struct_alignment) ctx->struct_alignment = struct_alignment;\n        }\n        break;\n      case '}': /* end of substruct; either repeat or move on */\n        {\n          size_t alignment = ctx->struct_alignment;\n          ++ts;\n          if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n          ctx->enc_type = 0; /* Erase processed last struct element */\n          if (alignment && ctx->fmt_offset % alignment) {\n            ctx->fmt_offset += alignment - (ctx->fmt_offset % alignment);\n          }\n        }\n        return ts;\n      case 'x':\n        if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n        ctx->fmt_offset += ctx->new_count;\n        ctx->new_count = 1;\n        ctx->enc_count = 0;\n        ctx->enc_type = 0;\n        ctx->enc_packmode = ctx->new_packmode;\n        ++ts;\n        break;\n      case 'Z':\n        got_Z = 1;\n        ++ts;\n        if (*ts != 'f' && *ts != 'd' && *ts != 'g') {\n          __Pyx_BufFmt_RaiseUnexpectedChar('Z');\n          return NULL;\n        }        /* fall through */\n      case 'c': case 'b': case 'B': case 'h': case 'H': case 'i': case 'I':\n      case 'l': case 'L': case 'q': case 'Q':\n      case 'f': case 'd': case 'g':\n      case 'O': case 's': case 'p':\n        if (ctx->enc_type == *ts && got_Z == ctx->is_complex &&\n            ctx->enc_packmode == ctx->new_packmode) {\n          ctx->enc_count += ctx->new_count;\n        } else {\n          if (__Pyx_BufFmt_ProcessTypeChunk(ctx) == -1) return NULL;\n          ctx->enc_count = ctx->new_count;\n          ctx->enc_packmode = ctx->new_packmode;\n          ctx->enc_type = *ts;\n          ctx->is_complex = got_Z;\n        }\n        ++ts;\n        ctx->new_count = 1;\n        got_Z = 0;\n        break;\n      case ':':\n        ++ts;\n        while(*ts != ':') ++ts;\n        ++ts;\n        break;\n      case '(':\n        if (!__pyx_buffmt_parse_array(ctx, &ts)) return NULL;\n        break;\n      default:\n        {\n          int number = __Pyx_BufFmt_ExpectNumber(&ts);\n          if (number == -1) return NULL;\n          ctx->new_count = (size_t)number;\n        }\n    }\n  }\n}\nstatic CYTHON_INLINE void __Pyx_ZeroBuffer(Py_buffer* buf) {\n  buf->buf = NULL;\n  buf->obj = NULL;\n  buf->strides = __Pyx_zeros;\n  buf->shape = __Pyx_zeros;\n  buf->suboffsets = __Pyx_minusones;\n}\nstatic CYTHON_INLINE int __Pyx_GetBufferAndValidate(\n        Py_buffer* buf, PyObject* obj,  __Pyx_TypeInfo* dtype, int flags,\n        int nd, int cast, __Pyx_BufFmt_StackElem* stack)\n{\n  if (obj == Py_None || obj == NULL) {\n    __Pyx_ZeroBuffer(buf);\n    return 0;\n  }\n  buf->buf = NULL;\n  if (__Pyx_GetBuffer(obj, buf, flags) == -1) goto fail;\n  if (buf->ndim != nd) {\n    PyErr_Format(PyExc_ValueError,\n                 \"Buffer has wrong number of dimensions (expected %d, got %d)\",\n                 nd, buf->ndim);\n    goto fail;\n  }\n  if (!cast) {\n    __Pyx_BufFmt_Context ctx;\n    __Pyx_BufFmt_Init(&ctx, stack, dtype);\n    if (!__Pyx_BufFmt_CheckString(&ctx, buf->format)) goto fail;\n  }\n  if ((unsigned)buf->itemsize != dtype->size) {\n    PyErr_Format(PyExc_ValueError,\n      \"Item size of buffer (%\" CYTHON_FORMAT_SSIZE_T \"d byte%s) does not match size of '%s' (%\" CYTHON_FORMAT_SSIZE_T \"d byte%s)\",\n      buf->itemsize, (buf->itemsize > 1) ? \"s\" : \"\",\n      dtype->name, (Py_ssize_t)dtype->size, (dtype->size > 1) ? \"s\" : \"\");\n    goto fail;\n  }\n  if (buf->suboffsets == NULL) buf->suboffsets = __Pyx_minusones;\n  return 0;\nfail:;\n  __Pyx_ZeroBuffer(buf);\n  return -1;\n}\nstatic CYTHON_INLINE void __Pyx_SafeReleaseBuffer(Py_buffer* info) {\n  if (info->buf == NULL) return;\n  if (info->suboffsets == __Pyx_minusones) info->suboffsets = NULL;\n  __Pyx_ReleaseBuffer(info);\n}\n\nstatic PyObject *__Pyx_GetBuiltinName(PyObject *name) {\n    PyObject* result = __Pyx_PyObject_GetAttrStr(__pyx_b, name);\n    if (unlikely(!result)) {\n        PyErr_Format(PyExc_NameError,\n#if PY_MAJOR_VERSION >= 3\n            \"name '%U' is not defined\", name);\n#else\n            \"name '%.200s' is not defined\", PyString_AS_STRING(name));\n#endif\n    }\n    return result;\n}\n\nstatic CYTHON_INLINE PyObject *__Pyx_GetModuleGlobalName(PyObject *name) {\n    PyObject *result;\n#if CYTHON_COMPILING_IN_CPYTHON\n    result = PyDict_GetItem(__pyx_d, name);\n    if (result) {\n        Py_INCREF(result);\n    } else {\n#else\n    result = PyObject_GetItem(__pyx_d, name);\n    if (!result) {\n        PyErr_Clear();\n#endif\n        result = __Pyx_GetBuiltinName(name);\n    }\n    return result;\n}\n\n#if CYTHON_COMPILING_IN_CPYTHON\nstatic CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg, PyObject *kw) {\n    PyObject *result;\n    ternaryfunc call = func->ob_type->tp_call;\n    if (unlikely(!call))\n        return PyObject_Call(func, arg, kw);\n#if PY_VERSION_HEX >= 0x02060000\n    if (unlikely(Py_EnterRecursiveCall((char*)\" while calling a Python object\")))\n        return NULL;\n#endif\n    result = (*call)(func, arg, kw);\n#if PY_VERSION_HEX >= 0x02060000\n    Py_LeaveRecursiveCall();\n#endif\n    if (unlikely(!result) && unlikely(!PyErr_Occurred())) {\n        PyErr_SetString(\n            PyExc_SystemError,\n            \"NULL result without error in PyObject_Call\");\n    }\n    return result;\n}\n#endif\n\nstatic CYTHON_INLINE int __Pyx_TypeTest(PyObject *obj, PyTypeObject *type) {\n    if (unlikely(!type)) {\n        PyErr_SetString(PyExc_SystemError, \"Missing type object\");\n        return 0;\n    }\n    if (likely(PyObject_TypeCheck(obj, type)))\n        return 1;\n    PyErr_Format(PyExc_TypeError, \"Cannot convert %.200s to %.200s\",\n                 Py_TYPE(obj)->tp_name, type->tp_name);\n    return 0;\n}\n\nstatic void __Pyx_RaiseBufferIndexError(int axis) {\n  PyErr_Format(PyExc_IndexError,\n     \"Out of bounds on buffer access (axis %d)\", axis);\n}\n\nstatic CYTHON_INLINE PyObject* __Pyx_PyObject_GetSlice(\n        PyObject* obj, Py_ssize_t cstart, Py_ssize_t cstop,\n        PyObject** _py_start, PyObject** _py_stop, PyObject** _py_slice,\n        int has_cstart, int has_cstop, CYTHON_UNUSED int wraparound) {\n#if CYTHON_COMPILING_IN_CPYTHON\n    PyMappingMethods* mp;\n#if PY_MAJOR_VERSION < 3\n    PySequenceMethods* ms = Py_TYPE(obj)->tp_as_sequence;\n    if (likely(ms && ms->sq_slice)) {\n        if (!has_cstart) {\n            if (_py_start && (*_py_start != Py_None)) {\n                cstart = __Pyx_PyIndex_AsSsize_t(*_py_start);\n                if ((cstart == (Py_ssize_t)-1) && PyErr_Occurred()) goto bad;\n            } else\n                cstart = 0;\n        }\n        if (!has_cstop) {\n            if (_py_stop && (*_py_stop != Py_None)) {\n                cstop = __Pyx_PyIndex_AsSsize_t(*_py_stop);\n                if ((cstop == (Py_ssize_t)-1) && PyErr_Occurred()) goto bad;\n            } else\n                cstop = PY_SSIZE_T_MAX;\n        }\n        if (wraparound && unlikely((cstart < 0) | (cstop < 0)) && likely(ms->sq_length)) {\n            Py_ssize_t l = ms->sq_length(obj);\n            if (likely(l >= 0)) {\n                if (cstop < 0) {\n                    cstop += l;\n                    if (cstop < 0) cstop = 0;\n                }\n                if (cstart < 0) {\n                    cstart += l;\n                    if (cstart < 0) cstart = 0;\n                }\n            } else {\n                if (PyErr_ExceptionMatches(PyExc_OverflowError))\n                    PyErr_Clear();\n                else\n                    goto bad;\n            }\n        }\n        return ms->sq_slice(obj, cstart, cstop);\n    }\n#endif\n    mp = Py_TYPE(obj)->tp_as_mapping;\n    if (likely(mp && mp->mp_subscript))\n#endif\n    {\n        PyObject* result;\n        PyObject *py_slice, *py_start, *py_stop;\n        if (_py_slice) {\n            py_slice = *_py_slice;\n        } else {\n            PyObject* owned_start = NULL;\n            PyObject* owned_stop = NULL;\n            if (_py_start) {\n                py_start = *_py_start;\n            } else {\n                if (has_cstart) {\n                    owned_start = py_start = PyInt_FromSsize_t(cstart);\n                    if (unlikely(!py_start)) goto bad;\n                } else\n                    py_start = Py_None;\n            }\n            if (_py_stop) {\n                py_stop = *_py_stop;\n            } else {\n                if (has_cstop) {\n                    owned_stop = py_stop = PyInt_FromSsize_t(cstop);\n                    if (unlikely(!py_stop)) {\n                        Py_XDECREF(owned_start);\n                        goto bad;\n                    }\n                } else\n                    py_stop = Py_None;\n            }\n            py_slice = PySlice_New(py_start, py_stop, Py_None);\n            Py_XDECREF(owned_start);\n            Py_XDECREF(owned_stop);\n            if (unlikely(!py_slice)) goto bad;\n        }\n#if CYTHON_COMPILING_IN_CPYTHON\n        result = mp->mp_subscript(obj, py_slice);\n#else\n        result = PyObject_GetItem(obj, py_slice);\n#endif\n        if (!_py_slice) {\n            Py_DECREF(py_slice);\n        }\n        return result;\n    }\n    PyErr_Format(PyExc_TypeError,\n        \"'%.200s' object is unsliceable\", Py_TYPE(obj)->tp_name);\nbad:\n    return NULL;\n}\n\nstatic void __Pyx_RaiseBufferFallbackError(void) {\n  PyErr_SetString(PyExc_ValueError,\n     \"Buffer acquisition failed on assignment; and then reacquiring the old buffer failed too!\");\n}\n\nstatic CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb) {\n#if CYTHON_COMPILING_IN_CPYTHON\n    PyObject *tmp_type, *tmp_value, *tmp_tb;\n    PyThreadState *tstate = PyThreadState_GET();\n    tmp_type = tstate->curexc_type;\n    tmp_value = tstate->curexc_value;\n    tmp_tb = tstate->curexc_traceback;\n    tstate->curexc_type = type;\n    tstate->curexc_value = value;\n    tstate->curexc_traceback = tb;\n    Py_XDECREF(tmp_type);\n    Py_XDECREF(tmp_value);\n    Py_XDECREF(tmp_tb);\n#else\n    PyErr_Restore(type, value, tb);\n#endif\n}\nstatic CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb) {\n#if CYTHON_COMPILING_IN_CPYTHON\n    PyThreadState *tstate = PyThreadState_GET();\n    *type = tstate->curexc_type;\n    *value = tstate->curexc_value;\n    *tb = tstate->curexc_traceback;\n    tstate->curexc_type = 0;\n    tstate->curexc_value = 0;\n    tstate->curexc_traceback = 0;\n#else\n    PyErr_Fetch(type, value, tb);\n#endif\n}\n\n#if PY_MAJOR_VERSION < 3\nstatic void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb,\n                        CYTHON_UNUSED PyObject *cause) {\n    Py_XINCREF(type);\n    if (!value || value == Py_None)\n        value = NULL;\n    else\n        Py_INCREF(value);\n    if (!tb || tb == Py_None)\n        tb = NULL;\n    else {\n        Py_INCREF(tb);\n        if (!PyTraceBack_Check(tb)) {\n            PyErr_SetString(PyExc_TypeError,\n                \"raise: arg 3 must be a traceback or None\");\n            goto raise_error;\n        }\n    }\n    #if PY_VERSION_HEX < 0x02050000\n    if (PyClass_Check(type)) {\n    #else\n    if (PyType_Check(type)) {\n    #endif\n#if CYTHON_COMPILING_IN_PYPY\n        if (!value) {\n            Py_INCREF(Py_None);\n            value = Py_None;\n        }\n#endif\n        PyErr_NormalizeException(&type, &value, &tb);\n    } else {\n        if (value) {\n            PyErr_SetString(PyExc_TypeError,\n                \"instance exception may not have a separate value\");\n            goto raise_error;\n        }\n        value = type;\n        #if PY_VERSION_HEX < 0x02050000\n        if (PyInstance_Check(type)) {\n            type = (PyObject*) ((PyInstanceObject*)type)->in_class;\n            Py_INCREF(type);\n        } else {\n            type = 0;\n            PyErr_SetString(PyExc_TypeError,\n                \"raise: exception must be an old-style class or instance\");\n            goto raise_error;\n        }\n        #else\n        type = (PyObject*) Py_TYPE(type);\n        Py_INCREF(type);\n        if (!PyType_IsSubtype((PyTypeObject *)type, (PyTypeObject *)PyExc_BaseException)) {\n            PyErr_SetString(PyExc_TypeError,\n                \"raise: exception class must be a subclass of BaseException\");\n            goto raise_error;\n        }\n        #endif\n    }\n    __Pyx_ErrRestore(type, value, tb);\n    return;\nraise_error:\n    Py_XDECREF(value);\n    Py_XDECREF(type);\n    Py_XDECREF(tb);\n    return;\n}\n#else /* Python 3+ */\nstatic void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause) {\n    PyObject* owned_instance = NULL;\n    if (tb == Py_None) {\n        tb = 0;\n    } else if (tb && !PyTraceBack_Check(tb)) {\n        PyErr_SetString(PyExc_TypeError,\n            \"raise: arg 3 must be a traceback or None\");\n        goto bad;\n    }\n    if (value == Py_None)\n        value = 0;\n    if (PyExceptionInstance_Check(type)) {\n        if (value) {\n            PyErr_SetString(PyExc_TypeError,\n                \"instance exception may not have a separate value\");\n            goto bad;\n        }\n        value = type;\n        type = (PyObject*) Py_TYPE(value);\n    } else if (PyExceptionClass_Check(type)) {\n        PyObject *instance_class = NULL;\n        if (value && PyExceptionInstance_Check(value)) {\n            instance_class = (PyObject*) Py_TYPE(value);\n            if (instance_class != type) {\n                if (PyObject_IsSubclass(instance_class, type)) {\n                    type = instance_class;\n                } else {\n                    instance_class = NULL;\n                }\n            }\n        }\n        if (!instance_class) {\n            PyObject *args;\n            if (!value)\n                args = PyTuple_New(0);\n            else if (PyTuple_Check(value)) {\n                Py_INCREF(value);\n                args = value;\n            } else\n                args = PyTuple_Pack(1, value);\n            if (!args)\n                goto bad;\n            owned_instance = PyObject_Call(type, args, NULL);\n            Py_DECREF(args);\n            if (!owned_instance)\n                goto bad;\n            value = owned_instance;\n            if (!PyExceptionInstance_Check(value)) {\n                PyErr_Format(PyExc_TypeError,\n                             \"calling %R should have returned an instance of \"\n                             \"BaseException, not %R\",\n                             type, Py_TYPE(value));\n                goto bad;\n            }\n        }\n    } else {\n        PyErr_SetString(PyExc_TypeError,\n            \"raise: exception class must be a subclass of BaseException\");\n        goto bad;\n    }\n#if PY_VERSION_HEX >= 0x03030000\n    if (cause) {\n#else\n    if (cause && cause != Py_None) {\n#endif\n        PyObject *fixed_cause;\n        if (cause == Py_None) {\n            fixed_cause = NULL;\n        } else if (PyExceptionClass_Check(cause)) {\n            fixed_cause = PyObject_CallObject(cause, NULL);\n            if (fixed_cause == NULL)\n                goto bad;\n        } else if (PyExceptionInstance_Check(cause)) {\n            fixed_cause = cause;\n            Py_INCREF(fixed_cause);\n        } else {\n            PyErr_SetString(PyExc_TypeError,\n                            \"exception causes must derive from \"\n                            \"BaseException\");\n            goto bad;\n        }\n        PyException_SetCause(value, fixed_cause);\n    }\n    PyErr_SetObject(type, value);\n    if (tb) {\n        PyThreadState *tstate = PyThreadState_GET();\n        PyObject* tmp_tb = tstate->curexc_traceback;\n        if (tb != tmp_tb) {\n            Py_INCREF(tb);\n            tstate->curexc_traceback = tb;\n            Py_XDECREF(tmp_tb);\n        }\n    }\nbad:\n    Py_XDECREF(owned_instance);\n    return;\n}\n#endif\n\nstatic CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected) {\n    PyErr_Format(PyExc_ValueError,\n                 \"too many values to unpack (expected %\" CYTHON_FORMAT_SSIZE_T \"d)\", expected);\n}\n\nstatic CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) {\n    PyErr_Format(PyExc_ValueError,\n                 \"need more than %\" CYTHON_FORMAT_SSIZE_T \"d value%.1s to unpack\",\n                 index, (index == 1) ? \"\" : \"s\");\n}\n\nstatic CYTHON_INLINE void __Pyx_RaiseNoneNotIterableError(void) {\n    PyErr_SetString(PyExc_TypeError, \"'NoneType' object is not iterable\");\n}\n\n#if PY_MAJOR_VERSION < 3\nstatic int __Pyx_GetBuffer(PyObject *obj, Py_buffer *view, int flags) {\n  #if PY_VERSION_HEX >= 0x02060000\n    if (PyObject_CheckBuffer(obj)) return PyObject_GetBuffer(obj, view, flags);\n  #endif\n        if (PyObject_TypeCheck(obj, __pyx_ptype_5numpy_ndarray)) return __pyx_pw_5numpy_7ndarray_1__getbuffer__(obj, view, flags);\n  #if PY_VERSION_HEX < 0x02060000\n    if (obj->ob_type->tp_dict) {\n        PyObject *getbuffer_cobj = PyObject_GetItem(\n            obj->ob_type->tp_dict, __pyx_n_s_pyx_getbuffer);\n        if (getbuffer_cobj) {\n            getbufferproc func = (getbufferproc) PyCObject_AsVoidPtr(getbuffer_cobj);\n            Py_DECREF(getbuffer_cobj);\n            if (!func)\n                goto fail;\n            return func(obj, view, flags);\n        } else {\n            PyErr_Clear();\n        }\n    }\n  #endif\n    PyErr_Format(PyExc_TypeError, \"'%.200s' does not have the buffer interface\", Py_TYPE(obj)->tp_name);\n#if PY_VERSION_HEX < 0x02060000\nfail:\n#endif\n    return -1;\n}\nstatic void __Pyx_ReleaseBuffer(Py_buffer *view) {\n    PyObject *obj = view->obj;\n    if (!obj) return;\n  #if PY_VERSION_HEX >= 0x02060000\n    if (PyObject_CheckBuffer(obj)) {\n        PyBuffer_Release(view);\n        return;\n    }\n  #endif\n        if (PyObject_TypeCheck(obj, __pyx_ptype_5numpy_ndarray)) { __pyx_pw_5numpy_7ndarray_3__releasebuffer__(obj, view); return; }\n  #if PY_VERSION_HEX < 0x02060000\n    if (obj->ob_type->tp_dict) {\n        PyObject *releasebuffer_cobj = PyObject_GetItem(\n            obj->ob_type->tp_dict, __pyx_n_s_pyx_releasebuffer);\n        if (releasebuffer_cobj) {\n            releasebufferproc func = (releasebufferproc) PyCObject_AsVoidPtr(releasebuffer_cobj);\n            Py_DECREF(releasebuffer_cobj);\n            if (!func)\n                goto fail;\n            func(obj, view);\n            return;\n        } else {\n            PyErr_Clear();\n        }\n    }\n  #endif\n    goto nofail;\n#if PY_VERSION_HEX < 0x02060000\nfail:\n#endif\n    PyErr_WriteUnraisable(obj);\nnofail:\n    Py_DECREF(obj);\n    view->obj = NULL;\n}\n#endif /*  PY_MAJOR_VERSION < 3 */\n\n\n        static PyObject *__Pyx_Import(PyObject *name, PyObject *from_list, int level) {\n    PyObject *empty_list = 0;\n    PyObject *module = 0;\n    PyObject *global_dict = 0;\n    PyObject *empty_dict = 0;\n    PyObject *list;\n    #if PY_VERSION_HEX < 0x03030000\n    PyObject *py_import;\n    py_import = __Pyx_PyObject_GetAttrStr(__pyx_b, __pyx_n_s_import);\n    if (!py_import)\n        goto bad;\n    #endif\n    if (from_list)\n        list = from_list;\n    else {\n        empty_list = PyList_New(0);\n        if (!empty_list)\n            goto bad;\n        list = empty_list;\n    }\n    global_dict = PyModule_GetDict(__pyx_m);\n    if (!global_dict)\n        goto bad;\n    empty_dict = PyDict_New();\n    if (!empty_dict)\n        goto bad;\n    #if PY_VERSION_HEX >= 0x02050000\n    {\n        #if PY_MAJOR_VERSION >= 3\n        if (level == -1) {\n            if (strchr(__Pyx_MODULE_NAME, '.')) {\n                #if PY_VERSION_HEX < 0x03030000\n                PyObject *py_level = PyInt_FromLong(1);\n                if (!py_level)\n                    goto bad;\n                module = PyObject_CallFunctionObjArgs(py_import,\n                    name, global_dict, empty_dict, list, py_level, NULL);\n                Py_DECREF(py_level);\n                #else\n                module = PyImport_ImportModuleLevelObject(\n                    name, global_dict, empty_dict, list, 1);\n                #endif\n                if (!module) {\n                    if (!PyErr_ExceptionMatches(PyExc_ImportError))\n                        goto bad;\n                    PyErr_Clear();\n                }\n            }\n            level = 0; /* try absolute import on failure */\n        }\n        #endif\n        if (!module) {\n            #if PY_VERSION_HEX < 0x03030000\n            PyObject *py_level = PyInt_FromLong(level);\n            if (!py_level)\n                goto bad;\n            module = PyObject_CallFunctionObjArgs(py_import,\n                name, global_dict, empty_dict, list, py_level, NULL);\n            Py_DECREF(py_level);\n            #else\n            module = PyImport_ImportModuleLevelObject(\n                name, global_dict, empty_dict, list, level);\n            #endif\n        }\n    }\n    #else\n    if (level>0) {\n        PyErr_SetString(PyExc_RuntimeError, \"Relative import is not supported for Python <=2.4.\");\n        goto bad;\n    }\n    module = PyObject_CallFunctionObjArgs(py_import,\n        name, global_dict, empty_dict, list, NULL);\n    #endif\nbad:\n    #if PY_VERSION_HEX < 0x03030000\n    Py_XDECREF(py_import);\n    #endif\n    Py_XDECREF(empty_list);\n    Py_XDECREF(empty_dict);\n    return module;\n}\n\n#define __PYX_VERIFY_RETURN_INT(target_type, func_type, func)             \\\n    {                                                                     \\\n        func_type value = func(x);                                        \\\n        if (sizeof(target_type) < sizeof(func_type)) {                    \\\n            if (unlikely(value != (func_type) (target_type) value)) {     \\\n                func_type zero = 0;                                       \\\n                PyErr_SetString(PyExc_OverflowError,                      \\\n                    (is_unsigned && unlikely(value < zero)) ?             \\\n                    \"can't convert negative value to \" #target_type :     \\\n                    \"value too large to convert to \" #target_type);       \\\n                return (target_type) -1;                                  \\\n            }                                                             \\\n        }                                                                 \\\n        return (target_type) value;                                       \\\n    }\n\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n  #include \"longintrepr.h\"\n #endif\n#endif\nstatic CYTHON_INLINE npy_int32 __Pyx_PyInt_As_npy_int32(PyObject *x) {\n    const npy_int32 neg_one = (npy_int32) -1, const_zero = 0;\n    const int is_unsigned = neg_one > const_zero;\n#if PY_MAJOR_VERSION < 3\n    if (likely(PyInt_Check(x))) {\n        if (sizeof(npy_int32) < sizeof(long)) {\n            __PYX_VERIFY_RETURN_INT(npy_int32, long, PyInt_AS_LONG)\n        } else {\n            long val = PyInt_AS_LONG(x);\n            if (is_unsigned && unlikely(val < 0)) {\n                PyErr_SetString(PyExc_OverflowError,\n                                \"can't convert negative value to npy_int32\");\n                return (npy_int32) -1;\n            }\n            return (npy_int32) val;\n        }\n    } else\n#endif\n    if (likely(PyLong_Check(x))) {\n        if (is_unsigned) {\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n            if (sizeof(digit) <= sizeof(npy_int32)) {\n                switch (Py_SIZE(x)) {\n                    case  0: return 0;\n                    case  1: return (npy_int32) ((PyLongObject*)x)->ob_digit[0];\n                }\n            }\n #endif\n#endif\n            if (unlikely(Py_SIZE(x) < 0)) {\n                PyErr_SetString(PyExc_OverflowError,\n                                \"can't convert negative value to npy_int32\");\n                return (npy_int32) -1;\n            }\n            if (sizeof(npy_int32) <= sizeof(unsigned long)) {\n                __PYX_VERIFY_RETURN_INT(npy_int32, unsigned long, PyLong_AsUnsignedLong)\n            } else if (sizeof(npy_int32) <= sizeof(unsigned long long)) {\n                __PYX_VERIFY_RETURN_INT(npy_int32, unsigned long long, PyLong_AsUnsignedLongLong)\n            }\n        } else {\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n            if (sizeof(digit) <= sizeof(npy_int32)) {\n                switch (Py_SIZE(x)) {\n                    case  0: return 0;\n                    case  1: return +(npy_int32) ((PyLongObject*)x)->ob_digit[0];\n                    case -1: return -(npy_int32) ((PyLongObject*)x)->ob_digit[0];\n                }\n            }\n #endif\n#endif\n            if (sizeof(npy_int32) <= sizeof(long)) {\n                __PYX_VERIFY_RETURN_INT(npy_int32, long, PyLong_AsLong)\n            } else if (sizeof(npy_int32) <= sizeof(long long)) {\n                __PYX_VERIFY_RETURN_INT(npy_int32, long long, PyLong_AsLongLong)\n            }\n        }\n        {\n#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)\n            PyErr_SetString(PyExc_RuntimeError,\n                            \"_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers\");\n#else\n            npy_int32 val;\n            PyObject *v = __Pyx_PyNumber_Int(x);\n #if PY_MAJOR_VERSION < 3\n            if (likely(v) && !PyLong_Check(v)) {\n                PyObject *tmp = v;\n                v = PyNumber_Long(tmp);\n                Py_DECREF(tmp);\n            }\n #endif\n            if (likely(v)) {\n                int one = 1; int is_little = (int)*(unsigned char *)&one;\n                unsigned char *bytes = (unsigned char *)&val;\n                int ret = _PyLong_AsByteArray((PyLongObject *)v,\n                                              bytes, sizeof(val),\n                                              is_little, !is_unsigned);\n                Py_DECREF(v);\n                if (likely(!ret))\n                    return val;\n            }\n#endif\n            return (npy_int32) -1;\n        }\n    } else {\n        npy_int32 val;\n        PyObject *tmp = __Pyx_PyNumber_Int(x);\n        if (!tmp) return (npy_int32) -1;\n        val = __Pyx_PyInt_As_npy_int32(tmp);\n        Py_DECREF(tmp);\n        return val;\n    }\n}\n\nstatic CYTHON_INLINE PyObject* __Pyx_PyInt_From_int(int value) {\n    const int neg_one = (int) -1, const_zero = 0;\n    const int is_unsigned = neg_one > const_zero;\n    if (is_unsigned) {\n        if (sizeof(int) < sizeof(long)) {\n            return PyInt_FromLong((long) value);\n        } else if (sizeof(int) <= sizeof(unsigned long)) {\n            return PyLong_FromUnsignedLong((unsigned long) value);\n        } else if (sizeof(int) <= sizeof(unsigned long long)) {\n            return PyLong_FromUnsignedLongLong((unsigned long long) value);\n        }\n    } else {\n        if (sizeof(int) <= sizeof(long)) {\n            return PyInt_FromLong((long) value);\n        } else if (sizeof(int) <= sizeof(long long)) {\n            return PyLong_FromLongLong((long long) value);\n        }\n    }\n    {\n        int one = 1; int little = (int)*(unsigned char *)&one;\n        unsigned char *bytes = (unsigned char *)&value;\n        return _PyLong_FromByteArray(bytes, sizeof(int),\n                                     little, !is_unsigned);\n    }\n}\n\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) {\n      return ::std::complex< float >(x, y);\n    }\n  #else\n    static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) {\n      return x + y*(__pyx_t_float_complex)_Complex_I;\n    }\n  #endif\n#else\n    static CYTHON_INLINE __pyx_t_float_complex __pyx_t_float_complex_from_parts(float x, float y) {\n      __pyx_t_float_complex z;\n      z.real = x;\n      z.imag = y;\n      return z;\n    }\n#endif\n\n#if CYTHON_CCOMPLEX\n#else\n    static CYTHON_INLINE int __Pyx_c_eqf(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n       return (a.real == b.real) && (a.imag == b.imag);\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_sumf(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n        __pyx_t_float_complex z;\n        z.real = a.real + b.real;\n        z.imag = a.imag + b.imag;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_difff(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n        __pyx_t_float_complex z;\n        z.real = a.real - b.real;\n        z.imag = a.imag - b.imag;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_prodf(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n        __pyx_t_float_complex z;\n        z.real = a.real * b.real - a.imag * b.imag;\n        z.imag = a.real * b.imag + a.imag * b.real;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_quotf(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n        __pyx_t_float_complex z;\n        float denom = b.real * b.real + b.imag * b.imag;\n        z.real = (a.real * b.real + a.imag * b.imag) / denom;\n        z.imag = (a.imag * b.real - a.real * b.imag) / denom;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_negf(__pyx_t_float_complex a) {\n        __pyx_t_float_complex z;\n        z.real = -a.real;\n        z.imag = -a.imag;\n        return z;\n    }\n    static CYTHON_INLINE int __Pyx_c_is_zerof(__pyx_t_float_complex a) {\n       return (a.real == 0) && (a.imag == 0);\n    }\n    static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_conjf(__pyx_t_float_complex a) {\n        __pyx_t_float_complex z;\n        z.real =  a.real;\n        z.imag = -a.imag;\n        return z;\n    }\n    #if 1\n        static CYTHON_INLINE float __Pyx_c_absf(__pyx_t_float_complex z) {\n          #if !defined(HAVE_HYPOT) || defined(_MSC_VER)\n            return sqrtf(z.real*z.real + z.imag*z.imag);\n          #else\n            return hypotf(z.real, z.imag);\n          #endif\n        }\n        static CYTHON_INLINE __pyx_t_float_complex __Pyx_c_powf(__pyx_t_float_complex a, __pyx_t_float_complex b) {\n            __pyx_t_float_complex z;\n            float r, lnr, theta, z_r, z_theta;\n            if (b.imag == 0 && b.real == (int)b.real) {\n                if (b.real < 0) {\n                    float denom = a.real * a.real + a.imag * a.imag;\n                    a.real = a.real / denom;\n                    a.imag = -a.imag / denom;\n                    b.real = -b.real;\n                }\n                switch ((int)b.real) {\n                    case 0:\n                        z.real = 1;\n                        z.imag = 0;\n                        return z;\n                    case 1:\n                        return a;\n                    case 2:\n                        z = __Pyx_c_prodf(a, a);\n                        return __Pyx_c_prodf(a, a);\n                    case 3:\n                        z = __Pyx_c_prodf(a, a);\n                        return __Pyx_c_prodf(z, a);\n                    case 4:\n                        z = __Pyx_c_prodf(a, a);\n                        return __Pyx_c_prodf(z, z);\n                }\n            }\n            if (a.imag == 0) {\n                if (a.real == 0) {\n                    return a;\n                }\n                r = a.real;\n                theta = 0;\n            } else {\n                r = __Pyx_c_absf(a);\n                theta = atan2f(a.imag, a.real);\n            }\n            lnr = logf(r);\n            z_r = expf(lnr * b.real - theta * b.imag);\n            z_theta = theta * b.real + lnr * b.imag;\n            z.real = z_r * cosf(z_theta);\n            z.imag = z_r * sinf(z_theta);\n            return z;\n        }\n    #endif\n#endif\n\n#if CYTHON_CCOMPLEX\n  #ifdef __cplusplus\n    static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) {\n      return ::std::complex< double >(x, y);\n    }\n  #else\n    static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) {\n      return x + y*(__pyx_t_double_complex)_Complex_I;\n    }\n  #endif\n#else\n    static CYTHON_INLINE __pyx_t_double_complex __pyx_t_double_complex_from_parts(double x, double y) {\n      __pyx_t_double_complex z;\n      z.real = x;\n      z.imag = y;\n      return z;\n    }\n#endif\n\n#if CYTHON_CCOMPLEX\n#else\n    static CYTHON_INLINE int __Pyx_c_eq(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n       return (a.real == b.real) && (a.imag == b.imag);\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_sum(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n        __pyx_t_double_complex z;\n        z.real = a.real + b.real;\n        z.imag = a.imag + b.imag;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_diff(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n        __pyx_t_double_complex z;\n        z.real = a.real - b.real;\n        z.imag = a.imag - b.imag;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_prod(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n        __pyx_t_double_complex z;\n        z.real = a.real * b.real - a.imag * b.imag;\n        z.imag = a.real * b.imag + a.imag * b.real;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_quot(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n        __pyx_t_double_complex z;\n        double denom = b.real * b.real + b.imag * b.imag;\n        z.real = (a.real * b.real + a.imag * b.imag) / denom;\n        z.imag = (a.imag * b.real - a.real * b.imag) / denom;\n        return z;\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_neg(__pyx_t_double_complex a) {\n        __pyx_t_double_complex z;\n        z.real = -a.real;\n        z.imag = -a.imag;\n        return z;\n    }\n    static CYTHON_INLINE int __Pyx_c_is_zero(__pyx_t_double_complex a) {\n       return (a.real == 0) && (a.imag == 0);\n    }\n    static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_conj(__pyx_t_double_complex a) {\n        __pyx_t_double_complex z;\n        z.real =  a.real;\n        z.imag = -a.imag;\n        return z;\n    }\n    #if 1\n        static CYTHON_INLINE double __Pyx_c_abs(__pyx_t_double_complex z) {\n          #if !defined(HAVE_HYPOT) || defined(_MSC_VER)\n            return sqrt(z.real*z.real + z.imag*z.imag);\n          #else\n            return hypot(z.real, z.imag);\n          #endif\n        }\n        static CYTHON_INLINE __pyx_t_double_complex __Pyx_c_pow(__pyx_t_double_complex a, __pyx_t_double_complex b) {\n            __pyx_t_double_complex z;\n            double r, lnr, theta, z_r, z_theta;\n            if (b.imag == 0 && b.real == (int)b.real) {\n                if (b.real < 0) {\n                    double denom = a.real * a.real + a.imag * a.imag;\n                    a.real = a.real / denom;\n                    a.imag = -a.imag / denom;\n                    b.real = -b.real;\n                }\n                switch ((int)b.real) {\n                    case 0:\n                        z.real = 1;\n                        z.imag = 0;\n                        return z;\n                    case 1:\n                        return a;\n                    case 2:\n                        z = __Pyx_c_prod(a, a);\n                        return __Pyx_c_prod(a, a);\n                    case 3:\n                        z = __Pyx_c_prod(a, a);\n                        return __Pyx_c_prod(z, a);\n                    case 4:\n                        z = __Pyx_c_prod(a, a);\n                        return __Pyx_c_prod(z, z);\n                }\n            }\n            if (a.imag == 0) {\n                if (a.real == 0) {\n                    return a;\n                }\n                r = a.real;\n                theta = 0;\n            } else {\n                r = __Pyx_c_abs(a);\n                theta = atan2(a.imag, a.real);\n            }\n            lnr = log(r);\n            z_r = exp(lnr * b.real - theta * b.imag);\n            z_theta = theta * b.real + lnr * b.imag;\n            z.real = z_r * cos(z_theta);\n            z.imag = z_r * sin(z_theta);\n            return z;\n        }\n    #endif\n#endif\n\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n  #include \"longintrepr.h\"\n #endif\n#endif\nstatic CYTHON_INLINE int __Pyx_PyInt_As_int(PyObject *x) {\n    const int neg_one = (int) -1, const_zero = 0;\n    const int is_unsigned = neg_one > const_zero;\n#if PY_MAJOR_VERSION < 3\n    if (likely(PyInt_Check(x))) {\n        if (sizeof(int) < sizeof(long)) {\n            __PYX_VERIFY_RETURN_INT(int, long, PyInt_AS_LONG)\n        } else {\n            long val = PyInt_AS_LONG(x);\n            if (is_unsigned && unlikely(val < 0)) {\n                PyErr_SetString(PyExc_OverflowError,\n                                \"can't convert negative value to int\");\n                return (int) -1;\n            }\n            return (int) val;\n        }\n    } else\n#endif\n    if (likely(PyLong_Check(x))) {\n        if (is_unsigned) {\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n            if (sizeof(digit) <= sizeof(int)) {\n                switch (Py_SIZE(x)) {\n                    case  0: return 0;\n                    case  1: return (int) ((PyLongObject*)x)->ob_digit[0];\n                }\n            }\n #endif\n#endif\n            if (unlikely(Py_SIZE(x) < 0)) {\n                PyErr_SetString(PyExc_OverflowError,\n                                \"can't convert negative value to int\");\n                return (int) -1;\n            }\n            if (sizeof(int) <= sizeof(unsigned long)) {\n                __PYX_VERIFY_RETURN_INT(int, unsigned long, PyLong_AsUnsignedLong)\n            } else if (sizeof(int) <= sizeof(unsigned long long)) {\n                __PYX_VERIFY_RETURN_INT(int, unsigned long long, PyLong_AsUnsignedLongLong)\n            }\n        } else {\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n            if (sizeof(digit) <= sizeof(int)) {\n                switch (Py_SIZE(x)) {\n                    case  0: return 0;\n                    case  1: return +(int) ((PyLongObject*)x)->ob_digit[0];\n                    case -1: return -(int) ((PyLongObject*)x)->ob_digit[0];\n                }\n            }\n #endif\n#endif\n            if (sizeof(int) <= sizeof(long)) {\n                __PYX_VERIFY_RETURN_INT(int, long, PyLong_AsLong)\n            } else if (sizeof(int) <= sizeof(long long)) {\n                __PYX_VERIFY_RETURN_INT(int, long long, PyLong_AsLongLong)\n            }\n        }\n        {\n#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)\n            PyErr_SetString(PyExc_RuntimeError,\n                            \"_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers\");\n#else\n            int val;\n            PyObject *v = __Pyx_PyNumber_Int(x);\n #if PY_MAJOR_VERSION < 3\n            if (likely(v) && !PyLong_Check(v)) {\n                PyObject *tmp = v;\n                v = PyNumber_Long(tmp);\n                Py_DECREF(tmp);\n            }\n #endif\n            if (likely(v)) {\n                int one = 1; int is_little = (int)*(unsigned char *)&one;\n                unsigned char *bytes = (unsigned char *)&val;\n                int ret = _PyLong_AsByteArray((PyLongObject *)v,\n                                              bytes, sizeof(val),\n                                              is_little, !is_unsigned);\n                Py_DECREF(v);\n                if (likely(!ret))\n                    return val;\n            }\n#endif\n            return (int) -1;\n        }\n    } else {\n        int val;\n        PyObject *tmp = __Pyx_PyNumber_Int(x);\n        if (!tmp) return (int) -1;\n        val = __Pyx_PyInt_As_int(tmp);\n        Py_DECREF(tmp);\n        return val;\n    }\n}\n\nstatic CYTHON_INLINE PyObject* __Pyx_PyInt_From_long(long value) {\n    const long neg_one = (long) -1, const_zero = 0;\n    const int is_unsigned = neg_one > const_zero;\n    if (is_unsigned) {\n        if (sizeof(long) < sizeof(long)) {\n            return PyInt_FromLong((long) value);\n        } else if (sizeof(long) <= sizeof(unsigned long)) {\n            return PyLong_FromUnsignedLong((unsigned long) value);\n        } else if (sizeof(long) <= sizeof(unsigned long long)) {\n            return PyLong_FromUnsignedLongLong((unsigned long long) value);\n        }\n    } else {\n        if (sizeof(long) <= sizeof(long)) {\n            return PyInt_FromLong((long) value);\n        } else if (sizeof(long) <= sizeof(long long)) {\n            return PyLong_FromLongLong((long long) value);\n        }\n    }\n    {\n        int one = 1; int little = (int)*(unsigned char *)&one;\n        unsigned char *bytes = (unsigned char *)&value;\n        return _PyLong_FromByteArray(bytes, sizeof(long),\n                                     little, !is_unsigned);\n    }\n}\n\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n  #include \"longintrepr.h\"\n #endif\n#endif\nstatic CYTHON_INLINE long __Pyx_PyInt_As_long(PyObject *x) {\n    const long neg_one = (long) -1, const_zero = 0;\n    const int is_unsigned = neg_one > const_zero;\n#if PY_MAJOR_VERSION < 3\n    if (likely(PyInt_Check(x))) {\n        if (sizeof(long) < sizeof(long)) {\n            __PYX_VERIFY_RETURN_INT(long, long, PyInt_AS_LONG)\n        } else {\n            long val = PyInt_AS_LONG(x);\n            if (is_unsigned && unlikely(val < 0)) {\n                PyErr_SetString(PyExc_OverflowError,\n                                \"can't convert negative value to long\");\n                return (long) -1;\n            }\n            return (long) val;\n        }\n    } else\n#endif\n    if (likely(PyLong_Check(x))) {\n        if (is_unsigned) {\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n            if (sizeof(digit) <= sizeof(long)) {\n                switch (Py_SIZE(x)) {\n                    case  0: return 0;\n                    case  1: return (long) ((PyLongObject*)x)->ob_digit[0];\n                }\n            }\n #endif\n#endif\n            if (unlikely(Py_SIZE(x) < 0)) {\n                PyErr_SetString(PyExc_OverflowError,\n                                \"can't convert negative value to long\");\n                return (long) -1;\n            }\n            if (sizeof(long) <= sizeof(unsigned long)) {\n                __PYX_VERIFY_RETURN_INT(long, unsigned long, PyLong_AsUnsignedLong)\n            } else if (sizeof(long) <= sizeof(unsigned long long)) {\n                __PYX_VERIFY_RETURN_INT(long, unsigned long long, PyLong_AsUnsignedLongLong)\n            }\n        } else {\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n            if (sizeof(digit) <= sizeof(long)) {\n                switch (Py_SIZE(x)) {\n                    case  0: return 0;\n                    case  1: return +(long) ((PyLongObject*)x)->ob_digit[0];\n                    case -1: return -(long) ((PyLongObject*)x)->ob_digit[0];\n                }\n            }\n #endif\n#endif\n            if (sizeof(long) <= sizeof(long)) {\n                __PYX_VERIFY_RETURN_INT(long, long, PyLong_AsLong)\n            } else if (sizeof(long) <= sizeof(long long)) {\n                __PYX_VERIFY_RETURN_INT(long, long long, PyLong_AsLongLong)\n            }\n        }\n        {\n#if CYTHON_COMPILING_IN_PYPY && !defined(_PyLong_AsByteArray)\n            PyErr_SetString(PyExc_RuntimeError,\n                            \"_PyLong_AsByteArray() not available in PyPy, cannot convert large numbers\");\n#else\n            long val;\n            PyObject *v = __Pyx_PyNumber_Int(x);\n #if PY_MAJOR_VERSION < 3\n            if (likely(v) && !PyLong_Check(v)) {\n                PyObject *tmp = v;\n                v = PyNumber_Long(tmp);\n                Py_DECREF(tmp);\n            }\n #endif\n            if (likely(v)) {\n                int one = 1; int is_little = (int)*(unsigned char *)&one;\n                unsigned char *bytes = (unsigned char *)&val;\n                int ret = _PyLong_AsByteArray((PyLongObject *)v,\n                                              bytes, sizeof(val),\n                                              is_little, !is_unsigned);\n                Py_DECREF(v);\n                if (likely(!ret))\n                    return val;\n            }\n#endif\n            return (long) -1;\n        }\n    } else {\n        long val;\n        PyObject *tmp = __Pyx_PyNumber_Int(x);\n        if (!tmp) return (long) -1;\n        val = __Pyx_PyInt_As_long(tmp);\n        Py_DECREF(tmp);\n        return val;\n    }\n}\n\nstatic int __Pyx_check_binary_version(void) {\n    char ctversion[4], rtversion[4];\n    PyOS_snprintf(ctversion, 4, \"%d.%d\", PY_MAJOR_VERSION, PY_MINOR_VERSION);\n    PyOS_snprintf(rtversion, 4, \"%s\", Py_GetVersion());\n    if (ctversion[0] != rtversion[0] || ctversion[2] != rtversion[2]) {\n        char message[200];\n        PyOS_snprintf(message, sizeof(message),\n                      \"compiletime version %s of module '%.100s' \"\n                      \"does not match runtime version %s\",\n                      ctversion, __Pyx_MODULE_NAME, rtversion);\n        #if PY_VERSION_HEX < 0x02050000\n        return PyErr_Warn(NULL, message);\n        #else\n        return PyErr_WarnEx(NULL, message, 1);\n        #endif\n    }\n    return 0;\n}\n\n#ifndef __PYX_HAVE_RT_ImportModule\n#define __PYX_HAVE_RT_ImportModule\nstatic PyObject *__Pyx_ImportModule(const char *name) {\n    PyObject *py_name = 0;\n    PyObject *py_module = 0;\n    py_name = __Pyx_PyIdentifier_FromString(name);\n    if (!py_name)\n        goto bad;\n    py_module = PyImport_Import(py_name);\n    Py_DECREF(py_name);\n    return py_module;\nbad:\n    Py_XDECREF(py_name);\n    return 0;\n}\n#endif\n\n#ifndef __PYX_HAVE_RT_ImportType\n#define __PYX_HAVE_RT_ImportType\nstatic PyTypeObject *__Pyx_ImportType(const char *module_name, const char *class_name,\n    size_t size, int strict)\n{\n    PyObject *py_module = 0;\n    PyObject *result = 0;\n    PyObject *py_name = 0;\n    char warning[200];\n    Py_ssize_t basicsize;\n#ifdef Py_LIMITED_API\n    PyObject *py_basicsize;\n#endif\n    py_module = __Pyx_ImportModule(module_name);\n    if (!py_module)\n        goto bad;\n    py_name = __Pyx_PyIdentifier_FromString(class_name);\n    if (!py_name)\n        goto bad;\n    result = PyObject_GetAttr(py_module, py_name);\n    Py_DECREF(py_name);\n    py_name = 0;\n    Py_DECREF(py_module);\n    py_module = 0;\n    if (!result)\n        goto bad;\n    if (!PyType_Check(result)) {\n        PyErr_Format(PyExc_TypeError,\n            \"%.200s.%.200s is not a type object\",\n            module_name, class_name);\n        goto bad;\n    }\n#ifndef Py_LIMITED_API\n    basicsize = ((PyTypeObject *)result)->tp_basicsize;\n#else\n    py_basicsize = PyObject_GetAttrString(result, \"__basicsize__\");\n    if (!py_basicsize)\n        goto bad;\n    basicsize = PyLong_AsSsize_t(py_basicsize);\n    Py_DECREF(py_basicsize);\n    py_basicsize = 0;\n    if (basicsize == (Py_ssize_t)-1 && PyErr_Occurred())\n        goto bad;\n#endif\n    if (!strict && (size_t)basicsize > size) {\n        PyOS_snprintf(warning, sizeof(warning),\n            \"%s.%s size changed, may indicate binary incompatibility\",\n            module_name, class_name);\n        #if PY_VERSION_HEX < 0x02050000\n        if (PyErr_Warn(NULL, warning) < 0) goto bad;\n        #else\n        if (PyErr_WarnEx(NULL, warning, 0) < 0) goto bad;\n        #endif\n    }\n    else if ((size_t)basicsize != size) {\n        PyErr_Format(PyExc_ValueError,\n            \"%.200s.%.200s has the wrong size, try recompiling\",\n            module_name, class_name);\n        goto bad;\n    }\n    return (PyTypeObject *)result;\nbad:\n    Py_XDECREF(py_module);\n    Py_XDECREF(result);\n    return NULL;\n}\n#endif\n\nstatic int __pyx_bisect_code_objects(__Pyx_CodeObjectCacheEntry* entries, int count, int code_line) {\n    int start = 0, mid = 0, end = count - 1;\n    if (end >= 0 && code_line > entries[end].code_line) {\n        return count;\n    }\n    while (start < end) {\n        mid = (start + end) / 2;\n        if (code_line < entries[mid].code_line) {\n            end = mid;\n        } else if (code_line > entries[mid].code_line) {\n             start = mid + 1;\n        } else {\n            return mid;\n        }\n    }\n    if (code_line <= entries[mid].code_line) {\n        return mid;\n    } else {\n        return mid + 1;\n    }\n}\nstatic PyCodeObject *__pyx_find_code_object(int code_line) {\n    PyCodeObject* code_object;\n    int pos;\n    if (unlikely(!code_line) || unlikely(!__pyx_code_cache.entries)) {\n        return NULL;\n    }\n    pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line);\n    if (unlikely(pos >= __pyx_code_cache.count) || unlikely(__pyx_code_cache.entries[pos].code_line != code_line)) {\n        return NULL;\n    }\n    code_object = __pyx_code_cache.entries[pos].code_object;\n    Py_INCREF(code_object);\n    return code_object;\n}\nstatic void __pyx_insert_code_object(int code_line, PyCodeObject* code_object) {\n    int pos, i;\n    __Pyx_CodeObjectCacheEntry* entries = __pyx_code_cache.entries;\n    if (unlikely(!code_line)) {\n        return;\n    }\n    if (unlikely(!entries)) {\n        entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Malloc(64*sizeof(__Pyx_CodeObjectCacheEntry));\n        if (likely(entries)) {\n            __pyx_code_cache.entries = entries;\n            __pyx_code_cache.max_count = 64;\n            __pyx_code_cache.count = 1;\n            entries[0].code_line = code_line;\n            entries[0].code_object = code_object;\n            Py_INCREF(code_object);\n        }\n        return;\n    }\n    pos = __pyx_bisect_code_objects(__pyx_code_cache.entries, __pyx_code_cache.count, code_line);\n    if ((pos < __pyx_code_cache.count) && unlikely(__pyx_code_cache.entries[pos].code_line == code_line)) {\n        PyCodeObject* tmp = entries[pos].code_object;\n        entries[pos].code_object = code_object;\n        Py_DECREF(tmp);\n        return;\n    }\n    if (__pyx_code_cache.count == __pyx_code_cache.max_count) {\n        int new_max = __pyx_code_cache.max_count + 64;\n        entries = (__Pyx_CodeObjectCacheEntry*)PyMem_Realloc(\n            __pyx_code_cache.entries, new_max*sizeof(__Pyx_CodeObjectCacheEntry));\n        if (unlikely(!entries)) {\n            return;\n        }\n        __pyx_code_cache.entries = entries;\n        __pyx_code_cache.max_count = new_max;\n    }\n    for (i=__pyx_code_cache.count; i>pos; i--) {\n        entries[i] = entries[i-1];\n    }\n    entries[pos].code_line = code_line;\n    entries[pos].code_object = code_object;\n    __pyx_code_cache.count++;\n    Py_INCREF(code_object);\n}\n\n#include \"compile.h\"\n#include \"frameobject.h\"\n#include \"traceback.h\"\nstatic PyCodeObject* __Pyx_CreateCodeObjectForTraceback(\n            const char *funcname, int c_line,\n            int py_line, const char *filename) {\n    PyCodeObject *py_code = 0;\n    PyObject *py_srcfile = 0;\n    PyObject *py_funcname = 0;\n    #if PY_MAJOR_VERSION < 3\n    py_srcfile = PyString_FromString(filename);\n    #else\n    py_srcfile = PyUnicode_FromString(filename);\n    #endif\n    if (!py_srcfile) goto bad;\n    if (c_line) {\n        #if PY_MAJOR_VERSION < 3\n        py_funcname = PyString_FromFormat( \"%s (%s:%d)\", funcname, __pyx_cfilenm, c_line);\n        #else\n        py_funcname = PyUnicode_FromFormat( \"%s (%s:%d)\", funcname, __pyx_cfilenm, c_line);\n        #endif\n    }\n    else {\n        #if PY_MAJOR_VERSION < 3\n        py_funcname = PyString_FromString(funcname);\n        #else\n        py_funcname = PyUnicode_FromString(funcname);\n        #endif\n    }\n    if (!py_funcname) goto bad;\n    py_code = __Pyx_PyCode_New(\n        0,            /*int argcount,*/\n        0,            /*int kwonlyargcount,*/\n        0,            /*int nlocals,*/\n        0,            /*int stacksize,*/\n        0,            /*int flags,*/\n        __pyx_empty_bytes, /*PyObject *code,*/\n        __pyx_empty_tuple, /*PyObject *consts,*/\n        __pyx_empty_tuple, /*PyObject *names,*/\n        __pyx_empty_tuple, /*PyObject *varnames,*/\n        __pyx_empty_tuple, /*PyObject *freevars,*/\n        __pyx_empty_tuple, /*PyObject *cellvars,*/\n        py_srcfile,   /*PyObject *filename,*/\n        py_funcname,  /*PyObject *name,*/\n        py_line,      /*int firstlineno,*/\n        __pyx_empty_bytes  /*PyObject *lnotab*/\n    );\n    Py_DECREF(py_srcfile);\n    Py_DECREF(py_funcname);\n    return py_code;\nbad:\n    Py_XDECREF(py_srcfile);\n    Py_XDECREF(py_funcname);\n    return NULL;\n}\nstatic void __Pyx_AddTraceback(const char *funcname, int c_line,\n                               int py_line, const char *filename) {\n    PyCodeObject *py_code = 0;\n    PyObject *py_globals = 0;\n    PyFrameObject *py_frame = 0;\n    py_code = __pyx_find_code_object(c_line ? c_line : py_line);\n    if (!py_code) {\n        py_code = __Pyx_CreateCodeObjectForTraceback(\n            funcname, c_line, py_line, filename);\n        if (!py_code) goto bad;\n        __pyx_insert_code_object(c_line ? c_line : py_line, py_code);\n    }\n    py_globals = PyModule_GetDict(__pyx_m);\n    if (!py_globals) goto bad;\n    py_frame = PyFrame_New(\n        PyThreadState_GET(), /*PyThreadState *tstate,*/\n        py_code,             /*PyCodeObject *code,*/\n        py_globals,          /*PyObject *globals,*/\n        0                    /*PyObject *locals*/\n    );\n    if (!py_frame) goto bad;\n    py_frame->f_lineno = py_line;\n    PyTraceBack_Here(py_frame);\nbad:\n    Py_XDECREF(py_code);\n    Py_XDECREF(py_frame);\n}\n\nstatic int __Pyx_InitStrings(__Pyx_StringTabEntry *t) {\n    while (t->p) {\n        #if PY_MAJOR_VERSION < 3\n        if (t->is_unicode) {\n            *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL);\n        } else if (t->intern) {\n            *t->p = PyString_InternFromString(t->s);\n        } else {\n            *t->p = PyString_FromStringAndSize(t->s, t->n - 1);\n        }\n        #else  /* Python 3+ has unicode identifiers */\n        if (t->is_unicode | t->is_str) {\n            if (t->intern) {\n                *t->p = PyUnicode_InternFromString(t->s);\n            } else if (t->encoding) {\n                *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL);\n            } else {\n                *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1);\n            }\n        } else {\n            *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1);\n        }\n        #endif\n        if (!*t->p)\n            return -1;\n        ++t;\n    }\n    return 0;\n}\n\nstatic CYTHON_INLINE PyObject* __Pyx_PyUnicode_FromString(char* c_str) {\n    return __Pyx_PyUnicode_FromStringAndSize(c_str, strlen(c_str));\n}\nstatic CYTHON_INLINE char* __Pyx_PyObject_AsString(PyObject* o) {\n    Py_ssize_t ignore;\n    return __Pyx_PyObject_AsStringAndSize(o, &ignore);\n}\nstatic CYTHON_INLINE char* __Pyx_PyObject_AsStringAndSize(PyObject* o, Py_ssize_t *length) {\n#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT\n    if (\n#if PY_MAJOR_VERSION < 3 && __PYX_DEFAULT_STRING_ENCODING_IS_ASCII\n            __Pyx_sys_getdefaultencoding_not_ascii &&\n#endif\n            PyUnicode_Check(o)) {\n#if PY_VERSION_HEX < 0x03030000\n        char* defenc_c;\n        PyObject* defenc = _PyUnicode_AsDefaultEncodedString(o, NULL);\n        if (!defenc) return NULL;\n        defenc_c = PyBytes_AS_STRING(defenc);\n#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII\n        {\n            char* end = defenc_c + PyBytes_GET_SIZE(defenc);\n            char* c;\n            for (c = defenc_c; c < end; c++) {\n                if ((unsigned char) (*c) >= 128) {\n                    PyUnicode_AsASCIIString(o);\n                    return NULL;\n                }\n            }\n        }\n#endif /*__PYX_DEFAULT_STRING_ENCODING_IS_ASCII*/\n        *length = PyBytes_GET_SIZE(defenc);\n        return defenc_c;\n#else /* PY_VERSION_HEX < 0x03030000 */\n        if (PyUnicode_READY(o) == -1) return NULL;\n#if __PYX_DEFAULT_STRING_ENCODING_IS_ASCII\n        if (PyUnicode_IS_ASCII(o)) {\n            *length = PyUnicode_GET_DATA_SIZE(o);\n            return PyUnicode_AsUTF8(o);\n        } else {\n            PyUnicode_AsASCIIString(o);\n            return NULL;\n        }\n#else /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */\n        return PyUnicode_AsUTF8AndSize(o, length);\n#endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII */\n#endif /* PY_VERSION_HEX < 0x03030000 */\n    } else\n#endif /* __PYX_DEFAULT_STRING_ENCODING_IS_ASCII  || __PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT */\n#if !CYTHON_COMPILING_IN_PYPY\n#if PY_VERSION_HEX >= 0x02060000\n    if (PyByteArray_Check(o)) {\n        *length = PyByteArray_GET_SIZE(o);\n        return PyByteArray_AS_STRING(o);\n    } else\n#endif\n#endif\n    {\n        char* result;\n        int r = PyBytes_AsStringAndSize(o, &result, length);\n        if (unlikely(r < 0)) {\n            return NULL;\n        } else {\n            return result;\n        }\n    }\n}\nstatic CYTHON_INLINE int __Pyx_PyObject_IsTrue(PyObject* x) {\n   int is_true = x == Py_True;\n   if (is_true | (x == Py_False) | (x == Py_None)) return is_true;\n   else return PyObject_IsTrue(x);\n}\nstatic CYTHON_INLINE PyObject* __Pyx_PyNumber_Int(PyObject* x) {\n  PyNumberMethods *m;\n  const char *name = NULL;\n  PyObject *res = NULL;\n#if PY_MAJOR_VERSION < 3\n  if (PyInt_Check(x) || PyLong_Check(x))\n#else\n  if (PyLong_Check(x))\n#endif\n    return Py_INCREF(x), x;\n  m = Py_TYPE(x)->tp_as_number;\n#if PY_MAJOR_VERSION < 3\n  if (m && m->nb_int) {\n    name = \"int\";\n    res = PyNumber_Int(x);\n  }\n  else if (m && m->nb_long) {\n    name = \"long\";\n    res = PyNumber_Long(x);\n  }\n#else\n  if (m && m->nb_int) {\n    name = \"int\";\n    res = PyNumber_Long(x);\n  }\n#endif\n  if (res) {\n#if PY_MAJOR_VERSION < 3\n    if (!PyInt_Check(res) && !PyLong_Check(res)) {\n#else\n    if (!PyLong_Check(res)) {\n#endif\n      PyErr_Format(PyExc_TypeError,\n                   \"__%.4s__ returned non-%.4s (type %.200s)\",\n                   name, name, Py_TYPE(res)->tp_name);\n      Py_DECREF(res);\n      return NULL;\n    }\n  }\n  else if (!PyErr_Occurred()) {\n    PyErr_SetString(PyExc_TypeError,\n                    \"an integer is required\");\n  }\n  return res;\n}\n#if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n #if CYTHON_USE_PYLONG_INTERNALS\n  #include \"longintrepr.h\"\n #endif\n#endif\nstatic CYTHON_INLINE Py_ssize_t __Pyx_PyIndex_AsSsize_t(PyObject* b) {\n  Py_ssize_t ival;\n  PyObject *x;\n#if PY_MAJOR_VERSION < 3\n  if (likely(PyInt_CheckExact(b)))\n      return PyInt_AS_LONG(b);\n#endif\n  if (likely(PyLong_CheckExact(b))) {\n    #if CYTHON_COMPILING_IN_CPYTHON && PY_MAJOR_VERSION >= 3\n     #if CYTHON_USE_PYLONG_INTERNALS\n       switch (Py_SIZE(b)) {\n       case -1: return -(sdigit)((PyLongObject*)b)->ob_digit[0];\n       case  0: return 0;\n       case  1: return ((PyLongObject*)b)->ob_digit[0];\n       }\n     #endif\n    #endif\n  #if PY_VERSION_HEX < 0x02060000\n    return PyInt_AsSsize_t(b);\n  #else\n    return PyLong_AsSsize_t(b);\n  #endif\n  }\n  x = PyNumber_Index(b);\n  if (!x) return -1;\n  ival = PyInt_AsSsize_t(x);\n  Py_DECREF(x);\n  return ival;\n}\nstatic CYTHON_INLINE PyObject * __Pyx_PyInt_FromSize_t(size_t ival) {\n#if PY_VERSION_HEX < 0x02050000\n   if (ival <= LONG_MAX)\n       return PyInt_FromLong((long)ival);\n   else {\n       unsigned char *bytes = (unsigned char *) &ival;\n       int one = 1; int little = (int)*(unsigned char*)&one;\n       return _PyLong_FromByteArray(bytes, sizeof(size_t), little, 0);\n   }\n#else\n   return PyInt_FromSize_t(ival);\n#endif\n}\n\n\n#endif /* Py_PYTHON_H */\n"
  },
  {
    "path": "lib/nms/gpu_nms.hpp",
    "content": "void _nms(int* keep_out, int* num_out, const float* boxes_host, int boxes_num,\n          int boxes_dim, float nms_overlap_thresh, int device_id);\n"
  },
  {
    "path": "lib/nms/gpu_nms.pyx",
    "content": "# --------------------------------------------------------\n# Faster R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\nimport numpy as np\ncimport numpy as np\n\nassert sizeof(int) == sizeof(np.int32_t)\n\ncdef extern from \"gpu_nms.hpp\":\n    void _nms(np.int32_t*, int*, np.float32_t*, int, int, float, int)\n\ndef gpu_nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh,\n            np.int32_t device_id=0):\n    cdef int boxes_num = dets.shape[0]\n    cdef int boxes_dim = dets.shape[1]\n    cdef int num_out\n    cdef np.ndarray[np.int32_t, ndim=1] \\\n        keep = np.zeros(boxes_num, dtype=np.int32)\n    cdef np.ndarray[np.float32_t, ndim=1] \\\n        scores = dets[:, 4]\n    cdef np.ndarray[np.int_t, ndim=1] \\\n        order = scores.argsort()[::-1]\n    cdef np.ndarray[np.float32_t, ndim=2] \\\n        sorted_dets = dets[order, :]\n    _nms(&keep[0], &num_out, &sorted_dets[0, 0], boxes_num, boxes_dim, thresh, device_id)\n    keep = keep[:num_out]\n    return list(order[keep])\n"
  },
  {
    "path": "lib/nms/nms_kernel.cu",
    "content": "// ------------------------------------------------------------------\n// Faster R-CNN\n// Copyright (c) 2015 Microsoft\n// Licensed under The MIT License [see fast-rcnn/LICENSE for details]\n// Written by Shaoqing Ren\n// ------------------------------------------------------------------\n\n#include \"gpu_nms.hpp\"\n#include <vector>\n#include <iostream>\n\n#define CUDA_CHECK(condition) \\\n  /* Code block avoids redefinition of cudaError_t error */ \\\n  do { \\\n    cudaError_t error = condition; \\\n    if (error != cudaSuccess) { \\\n      std::cout << cudaGetErrorString(error) << std::endl; \\\n    } \\\n  } while (0)\n\n#define DIVUP(m,n) ((m) / (n) + ((m) % (n) > 0))\nint const threadsPerBlock = sizeof(unsigned long long) * 8;\n\n__device__ inline float devIoU(float const * const a, float const * const b) {\n  float left = max(a[0], b[0]), right = min(a[2], b[2]);\n  float top = max(a[1], b[1]), bottom = min(a[3], b[3]);\n  float width = max(right - left + 1, 0.f), height = max(bottom - top + 1, 0.f);\n  float interS = width * height;\n  float Sa = (a[2] - a[0] + 1) * (a[3] - a[1] + 1);\n  float Sb = (b[2] - b[0] + 1) * (b[3] - b[1] + 1);\n  return interS / (Sa + Sb - interS);\n}\n\n__global__ void nms_kernel(const int n_boxes, const float nms_overlap_thresh,\n                           const float *dev_boxes, unsigned long long *dev_mask) {\n  const int row_start = blockIdx.y;\n  const int col_start = blockIdx.x;\n\n  // if (row_start > col_start) return;\n\n  const int row_size =\n        min(n_boxes - row_start * threadsPerBlock, threadsPerBlock);\n  const int col_size =\n        min(n_boxes - col_start * threadsPerBlock, threadsPerBlock);\n\n  __shared__ float block_boxes[threadsPerBlock * 5];\n  if (threadIdx.x < col_size) {\n    block_boxes[threadIdx.x * 5 + 0] =\n        dev_boxes[(threadsPerBlock * col_start + threadIdx.x) * 5 + 0];\n    block_boxes[threadIdx.x * 5 + 1] =\n        dev_boxes[(threadsPerBlock * col_start + threadIdx.x) * 5 + 1];\n    block_boxes[threadIdx.x * 5 + 2] =\n        dev_boxes[(threadsPerBlock * col_start + threadIdx.x) * 5 + 2];\n    block_boxes[threadIdx.x * 5 + 3] =\n        dev_boxes[(threadsPerBlock * col_start + threadIdx.x) * 5 + 3];\n    block_boxes[threadIdx.x * 5 + 4] =\n        dev_boxes[(threadsPerBlock * col_start + threadIdx.x) * 5 + 4];\n  }\n  __syncthreads();\n\n  if (threadIdx.x < row_size) {\n    const int cur_box_idx = threadsPerBlock * row_start + threadIdx.x;\n    const float *cur_box = dev_boxes + cur_box_idx * 5;\n    int i = 0;\n    unsigned long long t = 0;\n    int start = 0;\n    if (row_start == col_start) {\n      start = threadIdx.x + 1;\n    }\n    for (i = start; i < col_size; i++) {\n      if (devIoU(cur_box, block_boxes + i * 5) > nms_overlap_thresh) {\n        t |= 1ULL << i;\n      }\n    }\n    const int col_blocks = DIVUP(n_boxes, threadsPerBlock);\n    dev_mask[cur_box_idx * col_blocks + col_start] = t;\n  }\n}\n\nvoid _set_device(int device_id) {\n  int current_device;\n  CUDA_CHECK(cudaGetDevice(&current_device));\n  if (current_device == device_id) {\n    return;\n  }\n  // The call to cudaSetDevice must come before any calls to Get, which\n  // may perform initialization using the GPU.\n  CUDA_CHECK(cudaSetDevice(device_id));\n}\n\nvoid _nms(int* keep_out, int* num_out, const float* boxes_host, int boxes_num,\n          int boxes_dim, float nms_overlap_thresh, int device_id) {\n  _set_device(device_id);\n\n  float* boxes_dev = NULL;\n  unsigned long long* mask_dev = NULL;\n\n  const int col_blocks = DIVUP(boxes_num, threadsPerBlock);\n\n  CUDA_CHECK(cudaMalloc(&boxes_dev,\n                        boxes_num * boxes_dim * sizeof(float)));\n  CUDA_CHECK(cudaMemcpy(boxes_dev,\n                        boxes_host,\n                        boxes_num * boxes_dim * sizeof(float),\n                        cudaMemcpyHostToDevice));\n\n  CUDA_CHECK(cudaMalloc(&mask_dev,\n                        boxes_num * col_blocks * sizeof(unsigned long long)));\n\n  dim3 blocks(DIVUP(boxes_num, threadsPerBlock),\n              DIVUP(boxes_num, threadsPerBlock));\n  dim3 threads(threadsPerBlock);\n  nms_kernel<<<blocks, threads>>>(boxes_num,\n                                  nms_overlap_thresh,\n                                  boxes_dev,\n                                  mask_dev);\n\n  std::vector<unsigned long long> mask_host(boxes_num * col_blocks);\n  CUDA_CHECK(cudaMemcpy(&mask_host[0],\n                        mask_dev,\n                        sizeof(unsigned long long) * boxes_num * col_blocks,\n                        cudaMemcpyDeviceToHost));\n\n  std::vector<unsigned long long> remv(col_blocks);\n  memset(&remv[0], 0, sizeof(unsigned long long) * col_blocks);\n\n  int num_to_keep = 0;\n  for (int i = 0; i < boxes_num; i++) {\n    int nblock = i / threadsPerBlock;\n    int inblock = i % threadsPerBlock;\n\n    if (!(remv[nblock] & (1ULL << inblock))) {\n      keep_out[num_to_keep++] = i;\n      unsigned long long *p = &mask_host[0] + i * col_blocks;\n      for (int j = nblock; j < col_blocks; j++) {\n        remv[j] |= p[j];\n      }\n    }\n  }\n  *num_out = num_to_keep;\n\n  CUDA_CHECK(cudaFree(boxes_dev));\n  CUDA_CHECK(cudaFree(mask_dev));\n}\n"
  },
  {
    "path": "lib/nms/py_cpu_nms.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\nimport numpy as np\n\ndef py_cpu_nms(dets, thresh):\n    \"\"\"Pure Python NMS baseline.\"\"\"\n    x1 = dets[:, 0]\n    y1 = dets[:, 1]\n    x2 = dets[:, 2]\n    y2 = dets[:, 3]\n    scores = dets[:, 4]\n\n    areas = (x2 - x1 + 1) * (y2 - y1 + 1)\n    order = scores.argsort()[::-1]\n\n    keep = []\n    while order.size > 0:\n        i = order[0]\n        keep.append(i)\n        xx1 = np.maximum(x1[i], x1[order[1:]])\n        yy1 = np.maximum(y1[i], y1[order[1:]])\n        xx2 = np.minimum(x2[i], x2[order[1:]])\n        yy2 = np.minimum(y2[i], y2[order[1:]])\n\n        w = np.maximum(0.0, xx2 - xx1 + 1)\n        h = np.maximum(0.0, yy2 - yy1 + 1)\n        inter = w * h\n        ovr = inter / (areas[i] + areas[order[1:]] - inter)\n\n        inds = np.where(ovr <= thresh)[0]\n        order = order[inds + 1]\n\n    return keep\n"
  },
  {
    "path": "lib/roi_data_layer/__init__.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n"
  },
  {
    "path": "lib/roi_data_layer/layer.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick and Xinlei Chen\n# --------------------------------------------------------\n\n\"\"\"The data layer used during training to train a Fast R-CNN network.\n\nRoIDataLayer implements a Caffe Python layer.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nfrom model.config import cfg\nfrom roi_data_layer.minibatch import get_minibatch\nimport numpy as np\nimport time\n\nclass RoIDataLayer(object):\n  \"\"\"Fast R-CNN data layer used for training.\"\"\"\n\n  def __init__(self, roidb, num_classes, random=False):\n    \"\"\"Set the roidb to be used by this layer during training.\"\"\"\n    self._roidb = roidb\n    self._num_classes = num_classes\n    # Also set a random flag\n    self._random = random\n    self._shuffle_roidb_inds()\n\n  def _shuffle_roidb_inds(self):\n    \"\"\"Randomly permute the training roidb.\"\"\"\n    # If the random flag is set, \n    # then the database is shuffled according to system time\n    # Useful for the validation set\n    if self._random:\n      st0 = np.random.get_state()\n      millis = int(round(time.time() * 1000)) % 4294967295\n      np.random.seed(millis)\n    \n    if cfg.TRAIN.ASPECT_GROUPING:\n      widths = np.array([r['width'] for r in self._roidb])\n      heights = np.array([r['height'] for r in self._roidb])\n      horz = (widths >= heights)\n      vert = np.logical_not(horz)\n      horz_inds = np.where(horz)[0]\n      vert_inds = np.where(vert)[0]\n      inds = np.hstack((\n          np.random.permutation(horz_inds),\n          np.random.permutation(vert_inds)))\n      inds = np.reshape(inds, (-1, 2))\n      row_perm = np.random.permutation(np.arange(inds.shape[0]))\n      inds = np.reshape(inds[row_perm, :], (-1,))\n      self._perm = inds\n    else:\n      self._perm = np.random.permutation(np.arange(len(self._roidb)))\n    # Restore the random state\n    if self._random:\n      np.random.set_state(st0)\n      \n    self._cur = 0\n\n  def _get_next_minibatch_inds(self):\n    \"\"\"Return the roidb indices for the next minibatch.\"\"\"\n    \n    if self._cur + cfg.TRAIN.IMS_PER_BATCH >= len(self._roidb):\n      self._shuffle_roidb_inds()\n\n    #print (len(self._perm))\n\n    db_inds = self._perm[self._cur:self._cur + cfg.TRAIN.IMS_PER_BATCH]\n    self._cur += cfg.TRAIN.IMS_PER_BATCH\n\n    return db_inds\n\n  def _get_next_minibatch(self):\n    \"\"\"Return the blobs to be used for the next minibatch.\n\n    If cfg.TRAIN.USE_PREFETCH is True, then blobs will be computed in a\n    separate process and made available through self._blob_queue.\n    \"\"\"\n    db_inds = self._get_next_minibatch_inds()\n    minibatch_db = [self._roidb[i] for i in db_inds]\n    return get_minibatch(minibatch_db, self._num_classes)\n      \n  def forward(self):\n    \"\"\"Get blobs and copy them into this layer's top blob vector.\"\"\"\n    blobs = self._get_next_minibatch()\n    return blobs\n"
  },
  {
    "path": "lib/roi_data_layer/minibatch.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick and Xinlei Chen\n# --------------------------------------------------------\n\n\"\"\"Compute minibatch blobs for training a Fast R-CNN network.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\nimport numpy.random as npr\nimport cv2\nfrom model.config import cfg\nfrom utils.blob import prep_im_for_blob, im_list_to_blob\n\ndef get_minibatch(roidb, num_classes):\n  \"\"\"Given a roidb, construct a minibatch sampled from it.\"\"\"\n  num_images = len(roidb)\n  # Sample random scales to use for each image in this batch\n  random_scale_inds = npr.randint(0, high=len(cfg.TRAIN.SCALES),\n                  size=num_images)\n  assert(cfg.TRAIN.BATCH_SIZE % num_images == 0), \\\n    'num_images ({}) must divide BATCH_SIZE ({})'. \\\n    format(num_images, cfg.TRAIN.BATCH_SIZE)\n\n  # Get the input image blob, formatted for caffe\n  im_blob, im_scales = _get_image_blob(roidb, random_scale_inds)\n\n  blobs = {'data': im_blob}\n\n  assert len(im_scales) == 1, \"Single batch only\"\n  assert len(roidb) == 1, \"Single batch only\"\n  \n  # gt boxes: (x1, y1, x2, y2, cls)\n  if cfg.TRAIN.USE_ALL_GT:\n    # Include all ground truth boxes\n    gt_inds = np.where(roidb[0]['gt_classes'] != 0)[0]\n  else:\n    # For the COCO ground truth boxes, exclude the ones that are ''iscrowd'' \n    gt_inds = np.where(roidb[0]['gt_classes'] != 0 & np.all(roidb[0]['gt_overlaps'].toarray() > -1.0, axis=1))[0]\n  gt_boxes = np.empty((len(gt_inds), 5), dtype=np.float32)\n  gt_boxes[:, 0:4] = roidb[0]['boxes'][gt_inds, :] * im_scales[0]\n  gt_boxes[:, 4] = roidb[0]['gt_classes'][gt_inds]\n  blobs['gt_boxes'] = gt_boxes\n  blobs['im_info'] = np.array(\n    [[im_blob.shape[1], im_blob.shape[2], im_scales[0]]],\n    dtype=np.float32)\n\n  return blobs\n\ndef _get_image_blob(roidb, scale_inds):\n  \"\"\"Builds an input blob from the images in the roidb at the specified\n  scales.\n  \"\"\"\n  num_images = len(roidb)\n  processed_ims = []\n  im_scales = []\n  for i in range(num_images):\n    im = cv2.imread(roidb[i]['image'])\n    if roidb[i]['flipped']:\n      im = im[:, ::-1, :]\n    target_size = cfg.TRAIN.SCALES[scale_inds[i]]\n    im, im_scale = prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size,\n                    cfg.TRAIN.MAX_SIZE)\n    im_scales.append(im_scale)\n    processed_ims.append(im)\n\n  # Create a blob to hold the input images\n  blob = im_list_to_blob(processed_ims)\n\n  return blob, im_scales\n"
  },
  {
    "path": "lib/roi_data_layer/minibatch.py~",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick and Xinlei Chen\n# --------------------------------------------------------\n\n\"\"\"Compute minibatch blobs for training a Fast R-CNN network.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\nimport numpy.random as npr\nimport cv2\nfrom model.config import cfg\nfrom utils.blob import prep_im_for_blob, im_list_to_blob\n\ndef get_minibatch(roidb, num_classes):\n  \"\"\"Given a roidb, construct a minibatch sampled from it.\"\"\"\n  num_images = len(roidb)\n  # Sample random scales to use for each image in this batch\n  random_scale_inds = npr.randint(0, high=len(cfg.TRAIN.SCALES),\n                  size=num_images)\n  print (num_images)\n  print cfg.TRAIN.BATCH_SIZE\n  assert(cfg.TRAIN.BATCH_SIZE % num_images == 0), \\\n    'num_images ({}) must divide BATCH_SIZE ({})'. \\\n    format(num_images, cfg.TRAIN.BATCH_SIZE)\n\n  # Get the input image blob, formatted for caffe\n  im_blob, im_scales = _get_image_blob(roidb, random_scale_inds)\n\n  blobs = {'data': im_blob}\n\n  assert len(im_scales) == 1, \"Single batch only\"\n  assert len(roidb) == 1, \"Single batch only\"\n  \n  # gt boxes: (x1, y1, x2, y2, cls)\n  if cfg.TRAIN.USE_ALL_GT:\n    # Include all ground truth boxes\n    gt_inds = np.where(roidb[0]['gt_classes'] != 0)[0]\n  else:\n    # For the COCO ground truth boxes, exclude the ones that are ''iscrowd'' \n    gt_inds = np.where(roidb[0]['gt_classes'] != 0 & np.all(roidb[0]['gt_overlaps'].toarray() > -1.0, axis=1))[0]\n  gt_boxes = np.empty((len(gt_inds), 5), dtype=np.float32)\n  gt_boxes[:, 0:4] = roidb[0]['boxes'][gt_inds, :] * im_scales[0]\n  gt_boxes[:, 4] = roidb[0]['gt_classes'][gt_inds]\n  blobs['gt_boxes'] = gt_boxes\n  blobs['im_info'] = np.array(\n    [[im_blob.shape[1], im_blob.shape[2], im_scales[0]]],\n    dtype=np.float32)\n\n  return blobs\n\ndef _get_image_blob(roidb, scale_inds):\n  \"\"\"Builds an input blob from the images in the roidb at the specified\n  scales.\n  \"\"\"\n  num_images = len(roidb)\n  processed_ims = []\n  im_scales = []\n  for i in range(num_images):\n    im = cv2.imread(roidb[i]['image'])\n    if roidb[i]['flipped']:\n      im = im[:, ::-1, :]\n    target_size = cfg.TRAIN.SCALES[scale_inds[i]]\n    im, im_scale = prep_im_for_blob(im, cfg.PIXEL_MEANS, target_size,\n                    cfg.TRAIN.MAX_SIZE)\n    im_scales.append(im_scale)\n    processed_ims.append(im)\n\n  # Create a blob to hold the input images\n  blob = im_list_to_blob(processed_ims)\n\n  return blob, im_scales\n"
  },
  {
    "path": "lib/roi_data_layer/roidb.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"Transform a roidb into a trainable roidb by adding a bunch of metadata.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\nfrom model.config import cfg\nfrom model.bbox_transform import bbox_transform\nfrom utils.cython_bbox import bbox_overlaps\nimport PIL\n\ndef prepare_roidb(imdb):\n  \"\"\"Enrich the imdb's roidb by adding some derived quantities that\n  are useful for training. This function precomputes the maximum\n  overlap, taken over ground-truth boxes, between each ROI and\n  each ground-truth box. The class with maximum overlap is also\n  recorded.\n  \"\"\"\n  roidb = imdb.roidb\n  if not (imdb.name.startswith('coco')):\n    sizes = [PIL.Image.open(imdb.image_path_at(i)).size\n         for i in range(imdb.num_images)]\n  for i in range(len(imdb.image_index)):\n    roidb[i]['image'] = imdb.image_path_at(i)\n    if not (imdb.name.startswith('coco')):\n      roidb[i]['width'] = sizes[i][0]\n      roidb[i]['height'] = sizes[i][1]\n    # need gt_overlaps as a dense array for argmax\n    gt_overlaps = roidb[i]['gt_overlaps'].toarray()\n    # max overlap with gt over classes (columns)\n    max_overlaps = gt_overlaps.max(axis=1)\n    # gt class that had the max overlap\n    max_classes = gt_overlaps.argmax(axis=1)\n    roidb[i]['max_classes'] = max_classes\n    roidb[i]['max_overlaps'] = max_overlaps\n    # sanity checks\n    # max overlap of 0 => class should be zero (background)\n    zero_inds = np.where(max_overlaps == 0)[0]\n    assert all(max_classes[zero_inds] == 0)\n    # max overlap > 0 => class should not be zero (must be a fg class)\n    nonzero_inds = np.where(max_overlaps > 0)[0]\n    assert all(max_classes[nonzero_inds] != 0)\n"
  },
  {
    "path": "lib/setup.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\nimport os\nfrom os.path import join as pjoin\nimport numpy as np\nfrom distutils.core import setup\nfrom distutils.extension import Extension\nfrom Cython.Distutils import build_ext\n\ndef find_in_path(name, path):\n    \"Find a file in a search path\"\n    #adapted fom http://code.activestate.com/recipes/52224-find-a-file-given-a-search-path/\n    for dir in path.split(os.pathsep):\n        binpath = pjoin(dir, name)\n        if os.path.exists(binpath):\n            return os.path.abspath(binpath)\n    return None\n\ndef locate_cuda():\n    \"\"\"Locate the CUDA environment on the system\n\n    Returns a dict with keys 'home', 'nvcc', 'include', and 'lib64'\n    and values giving the absolute path to each directory.\n\n    Starts by looking for the CUDAHOME env variable. If not found, everything\n    is based on finding 'nvcc' in the PATH.\n    \"\"\"\n\n    # first check if the CUDAHOME env variable is in use\n    if 'CUDAHOME' in os.environ:\n        home = os.environ['CUDAHOME']\n        nvcc = pjoin(home, 'bin', 'nvcc')\n    else:\n        # otherwise, search the PATH for NVCC\n        default_path = pjoin(os.sep, 'usr', 'local', 'cuda', 'bin')\n        nvcc = find_in_path('nvcc', os.environ['PATH'] + os.pathsep + default_path)\n        if nvcc is None:\n            raise EnvironmentError('The nvcc binary could not be '\n                'located in your $PATH. Either add it to your path, or set $CUDAHOME')\n        home = os.path.dirname(os.path.dirname(nvcc))\n\n    cudaconfig = {'home':home, 'nvcc':nvcc,\n                  'include': pjoin(home, 'include'),\n                  'lib64': pjoin(home, 'lib64')}\n    for k, v in cudaconfig.items():\n        if not os.path.exists(v):\n            raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))\n\n    return cudaconfig\nCUDA = locate_cuda()\n\n# Obtain the numpy include directory.  This logic works across numpy versions.\ntry:\n    numpy_include = np.get_include()\nexcept AttributeError:\n    numpy_include = np.get_numpy_include()\n\ndef customize_compiler_for_nvcc(self):\n    \"\"\"inject deep into distutils to customize how the dispatch\n    to gcc/nvcc works.\n\n    If you subclass UnixCCompiler, it's not trivial to get your subclass\n    injected in, and still have the right customizations (i.e.\n    distutils.sysconfig.customize_compiler) run on it. So instead of going\n    the OO route, I have this. Note, it's kindof like a wierd functional\n    subclassing going on.\"\"\"\n\n    # tell the compiler it can processes .cu\n    self.src_extensions.append('.cu')\n\n    # save references to the default compiler_so and _comple methods\n    default_compiler_so = self.compiler_so\n    super = self._compile\n\n    # now redefine the _compile method. This gets executed for each\n    # object but distutils doesn't have the ability to change compilers\n    # based on source extension: we add it.\n    def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts):\n        print(extra_postargs)\n        if os.path.splitext(src)[1] == '.cu':\n            # use the cuda for .cu files\n            self.set_executable('compiler_so', CUDA['nvcc'])\n            # use only a subset of the extra_postargs, which are 1-1 translated\n            # from the extra_compile_args in the Extension class\n            postargs = extra_postargs['nvcc']\n        else:\n            postargs = extra_postargs['gcc']\n\n        super(obj, src, ext, cc_args, postargs, pp_opts)\n        # reset the default compiler_so, which we might have changed for cuda\n        self.compiler_so = default_compiler_so\n\n    # inject our redefined _compile method into the class\n    self._compile = _compile\n\n# run the customize_compiler\nclass custom_build_ext(build_ext):\n    def build_extensions(self):\n        customize_compiler_for_nvcc(self.compiler)\n        build_ext.build_extensions(self)\n\next_modules = [\n    Extension(\n        \"utils.cython_bbox\",\n        [\"utils/bbox.pyx\"],\n        extra_compile_args={'gcc': [\"-Wno-cpp\", \"-Wno-unused-function\"]},\n        include_dirs = [numpy_include]\n    ),\n    Extension(\n        \"utils.cython_nms\",\n        [\"utils/nms.pyx\"],\n        extra_compile_args={'gcc': [\"-Wno-cpp\", \"-Wno-unused-function\"]},\n        include_dirs = [numpy_include]\n    ),\n    Extension(\n        \"nms.cpu_nms\",\n        [\"nms/cpu_nms.pyx\"],\n        extra_compile_args={'gcc': [\"-Wno-cpp\", \"-Wno-unused-function\"]},\n        include_dirs = [numpy_include]\n    ),\n    Extension('nms.gpu_nms',\n        ['nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],\n        library_dirs=[CUDA['lib64']],\n        libraries=['cudart'],\n        language='c++',\n        runtime_library_dirs=[CUDA['lib64']],\n        # this syntax is specific to this build system\n        # we're only going to use certain compiler args with nvcc and not with gcc\n        # the implementation of this trick is in customize_compiler() below\n        extra_compile_args={'gcc': [\"-Wno-unused-function\"],\n                            'nvcc': ['-arch=sm_52',\n                                     '--ptxas-options=-v',\n                                     '-c',\n                                     '--compiler-options',\n                                     \"'-fPIC'\"]},\n        include_dirs = [numpy_include, CUDA['include']]\n    )\n]\n\nsetup(\n    name='tf_faster_rcnn',\n    ext_modules=ext_modules,\n    # inject our custom trigger\n    cmdclass={'build_ext': custom_build_ext},\n)\n"
  },
  {
    "path": "lib/setup.py~",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\nimport os\nfrom os.path import join as pjoin\nimport numpy as np\nfrom distutils.core import setup\nfrom distutils.extension import Extension\nfrom Cython.Distutils import build_ext\n\ndef find_in_path(name, path):\n    \"Find a file in a search path\"\n    #adapted fom http://code.activestate.com/recipes/52224-find-a-file-given-a-search-path/\n    for dir in path.split(os.pathsep):\n        binpath = pjoin(dir, name)\n        if os.path.exists(binpath):\n            return os.path.abspath(binpath)\n    return None\n\ndef locate_cuda():\n    \"\"\"Locate the CUDA environment on the system\n\n    Returns a dict with keys 'home', 'nvcc', 'include', and 'lib64'\n    and values giving the absolute path to each directory.\n\n    Starts by looking for the CUDAHOME env variable. If not found, everything\n    is based on finding 'nvcc' in the PATH.\n    \"\"\"\n\n    # first check if the CUDAHOME env variable is in use\n    if 'CUDAHOME' in os.environ:\n        home = os.environ['CUDAHOME']\n        nvcc = pjoin(home, 'bin', 'nvcc')\n    else:\n        # otherwise, search the PATH for NVCC\n        default_path = pjoin(os.sep, 'usr', 'local', 'cuda', 'bin')\n        nvcc = find_in_path('nvcc', os.environ['PATH'] + os.pathsep + default_path)\n        if nvcc is None:\n            raise EnvironmentError('The nvcc binary could not be '\n                'located in your $PATH. Either add it to your path, or set $CUDAHOME')\n        home = os.path.dirname(os.path.dirname(nvcc))\n\n    cudaconfig = {'home':home, 'nvcc':nvcc,\n                  'include': pjoin(home, 'include'),\n                  'lib64': pjoin(home, 'lib64')}\n    for k, v in cudaconfig.items():\n        if not os.path.exists(v):\n            raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))\n\n    return cudaconfig\nCUDA = locate_cuda()\n\n# Obtain the numpy include directory.  This logic works across numpy versions.\ntry:\n    numpy_include = np.get_include()\nexcept AttributeError:\n    numpy_include = np.get_numpy_include()\n\ndef customize_compiler_for_nvcc(self):\n    \"\"\"inject deep into distutils to customize how the dispatch\n    to gcc/nvcc works.\n\n    If you subclass UnixCCompiler, it's not trivial to get your subclass\n    injected in, and still have the right customizations (i.e.\n    distutils.sysconfig.customize_compiler) run on it. So instead of going\n    the OO route, I have this. Note, it's kindof like a wierd functional\n    subclassing going on.\"\"\"\n\n    # tell the compiler it can processes .cu\n    self.src_extensions.append('.cu')\n\n    # save references to the default compiler_so and _comple methods\n    default_compiler_so = self.compiler_so\n    super = self._compile\n\n    # now redefine the _compile method. This gets executed for each\n    # object but distutils doesn't have the ability to change compilers\n    # based on source extension: we add it.\n    def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts):\n        print(extra_postargs)\n        if os.path.splitext(src)[1] == '.cu':\n            # use the cuda for .cu files\n            self.set_executable('compiler_so', CUDA['nvcc'])\n            # use only a subset of the extra_postargs, which are 1-1 translated\n            # from the extra_compile_args in the Extension class\n            postargs = extra_postargs['nvcc']\n        else:\n            postargs = extra_postargs['gcc']\n\n        super(obj, src, ext, cc_args, postargs, pp_opts)\n        # reset the default compiler_so, which we might have changed for cuda\n        self.compiler_so = default_compiler_so\n\n    # inject our redefined _compile method into the class\n    self._compile = _compile\n\n# run the customize_compiler\nclass custom_build_ext(build_ext):\n    def build_extensions(self):\n        customize_compiler_for_nvcc(self.compiler)\n        build_ext.build_extensions(self)\n\next_modules = [\n    Extension(\n        \"utils.cython_bbox\",\n        [\"utils/bbox.pyx\"],\n        extra_compile_args={'gcc': [\"-Wno-cpp\", \"-Wno-unused-function\"]},\n        include_dirs = [numpy_include]\n    ),\n    Extension(\n        \"utils.cython_nms\",\n        [\"utils/nms.pyx\"],\n        extra_compile_args={'gcc': [\"-Wno-cpp\", \"-Wno-unused-function\"]},\n        include_dirs = [numpy_include]\n    ),\n    Extension(\n        \"nms.cpu_nms\",\n        [\"nms/cpu_nms.pyx\"],\n        extra_compile_args={'gcc': [\"-Wno-cpp\", \"-Wno-unused-function\"]},\n        include_dirs = [numpy_include]\n    ),\n    Extension('nms.gpu_nms',\n        ['nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],\n        library_dirs=[CUDA['lib64']],\n        libraries=['cudart'],\n        language='c++',\n        runtime_library_dirs=[CUDA['lib64']],\n        # this syntax is specific to this build system\n        # we're only going to use certain compiler args with nvcc and not with gcc\n        # the implementation of this trick is in customize_compiler() below\n        extra_compile_args={'gcc': [\"-Wno-unused-function\"],\n                            'nvcc': ['-arch=sm_52',\n                                     '--ptxas-options=-v',\n                                     '-c',\n                                     '--compiler-options',\n                                     \"'-fPIC'\"]},\n        include_dirs = [numpy_include, CUDA['include']]\n    )\n]\n\nsetup(\n    name='tf_faster_rcnn',\n    ext_modules=ext_modules,\n    # inject our custom trigger\n    cmdclass={'build_ext': custom_build_ext},\n)\n"
  },
  {
    "path": "lib/utils/.gitignore",
    "content": "*.c\n*.cpp\n*.h\n*.hpp\n"
  },
  {
    "path": "lib/utils/__init__.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n"
  },
  {
    "path": "lib/utils/bbox.pyx",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Sergey Karayev\n# --------------------------------------------------------\n\ncimport cython\nimport numpy as np\ncimport numpy as np\n\nDTYPE = np.float\nctypedef np.float_t DTYPE_t\n\ndef bbox_overlaps(\n        np.ndarray[DTYPE_t, ndim=2] boxes,\n        np.ndarray[DTYPE_t, ndim=2] query_boxes):\n    \"\"\"\n    Parameters\n    ----------\n    boxes: (N, 4) ndarray of float\n    query_boxes: (K, 4) ndarray of float\n    Returns\n    -------\n    overlaps: (N, K) ndarray of overlap between boxes and query_boxes\n    \"\"\"\n    cdef unsigned int N = boxes.shape[0]\n    cdef unsigned int K = query_boxes.shape[0]\n    cdef np.ndarray[DTYPE_t, ndim=2] overlaps = np.zeros((N, K), dtype=DTYPE)\n    cdef DTYPE_t iw, ih, box_area\n    cdef DTYPE_t ua\n    cdef unsigned int k, n\n    for k in range(K):\n        box_area = (\n            (query_boxes[k, 2] - query_boxes[k, 0] + 1) *\n            (query_boxes[k, 3] - query_boxes[k, 1] + 1)\n        )\n        for n in range(N):\n            iw = (\n                min(boxes[n, 2], query_boxes[k, 2]) -\n                max(boxes[n, 0], query_boxes[k, 0]) + 1\n            )\n            if iw > 0:\n                ih = (\n                    min(boxes[n, 3], query_boxes[k, 3]) -\n                    max(boxes[n, 1], query_boxes[k, 1]) + 1\n                )\n                if ih > 0:\n                    ua = float(\n                        (boxes[n, 2] - boxes[n, 0] + 1) *\n                        (boxes[n, 3] - boxes[n, 1] + 1) +\n                        box_area - iw * ih\n                    )\n                    overlaps[n, k] = iw * ih / ua\n    return overlaps\n\ndef bbox_overlaps_self(\n        np.ndarray[DTYPE_t, ndim=2] boxes,\n        np.ndarray[DTYPE_t, ndim=2] query_boxes):\n    \"\"\"\n    Parameters\n    ----------\n    boxes: (N, 4) ndarray of float\n    query_boxes: (K, 4) ndarray of float\n    Returns\n    -------\n    overlaps: (N, K) ndarray of overlap between boxes and query_boxes\n    \"\"\"\n    cdef unsigned int N = boxes.shape[0]\n    cdef unsigned int K = query_boxes.shape[0]\n    cdef np.ndarray[DTYPE_t, ndim=2] overlaps = np.zeros((N, K), dtype=DTYPE)\n    cdef DTYPE_t iw, ih, box_area\n    cdef DTYPE_t ua\n    cdef unsigned int k, n\n    for k in range(K):\n        box_area = (\n            (query_boxes[k, 2] - query_boxes[k, 0] + 1) *\n            (query_boxes[k, 3] - query_boxes[k, 1] + 1)\n        )\n        for n in range(N):\n            iw = (\n                min(boxes[n, 2], query_boxes[k, 2]) -\n                max(boxes[n, 0], query_boxes[k, 0]) + 1\n            )\n            if iw > 0:\n                ih = (\n                    min(boxes[n, 3], query_boxes[k, 3]) -\n                    max(boxes[n, 1], query_boxes[k, 1]) + 1\n                )\n                if ih > 0:\n                    ua = float(box_area)\n                    overlaps[n, k] = iw * ih / ua\n    return overlaps"
  },
  {
    "path": "lib/utils/blob.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"Blob helper functions.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\nimport cv2\n\n\ndef im_list_to_blob(ims):\n  \"\"\"Convert a list of images into a network input.\n\n  Assumes images are already prepared (means subtracted, BGR order, ...).\n  \"\"\"\n  max_shape = np.array([im.shape for im in ims]).max(axis=0)\n  num_images = len(ims)\n  blob = np.zeros((num_images, max_shape[0], max_shape[1], 3),\n                  dtype=np.float32)\n  for i in range(num_images):\n    im = ims[i]\n    blob[i, 0:im.shape[0], 0:im.shape[1], :] = im\n\n  return blob\n\n\ndef prep_im_for_blob(im, pixel_means, target_size, max_size):\n  \"\"\"Mean subtract and scale an image for use in a blob.\"\"\"\n  im = im.astype(np.float32, copy=False)\n  im -= pixel_means\n  im_shape = im.shape\n  im_size_min = np.min(im_shape[0:2])\n  im_size_max = np.max(im_shape[0:2])\n  im_scale = float(target_size) / float(im_size_min)\n  # Prevent the biggest axis from being more than MAX_SIZE\n  if np.round(im_scale * im_size_max) > max_size:\n    im_scale = float(max_size) / float(im_size_max)\n  im = cv2.resize(im, None, None, fx=im_scale, fy=im_scale,\n                  interpolation=cv2.INTER_LINEAR)\n\n  return im, im_scale\n"
  },
  {
    "path": "lib/utils/boxes_grid.py",
    "content": "# --------------------------------------------------------\n# Subcategory CNN\n# Copyright (c) 2015 CVGL Stanford\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Yu Xiang\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport numpy as np\nimport math\nfrom model.config import cfg\n\n\ndef get_boxes_grid(image_height, image_width):\n  \"\"\"\n  Return the boxes on image grid.\n  \"\"\"\n\n  # height and width of the heatmap\n  if cfg.NET_NAME == 'CaffeNet':\n    height = np.floor((image_height * max(cfg.TRAIN.SCALES) - 1) / 4.0 + 1)\n    height = np.floor((height - 1) / 2.0 + 1 + 0.5)\n    height = np.floor((height - 1) / 2.0 + 1 + 0.5)\n\n    width = np.floor((image_width * max(cfg.TRAIN.SCALES) - 1) / 4.0 + 1)\n    width = np.floor((width - 1) / 2.0 + 1 + 0.5)\n    width = np.floor((width - 1) / 2.0 + 1 + 0.5)\n  elif cfg.NET_NAME == 'VGGnet':\n    height = np.floor(image_height * max(cfg.TRAIN.SCALES) / 2.0 + 0.5)\n    height = np.floor(height / 2.0 + 0.5)\n    height = np.floor(height / 2.0 + 0.5)\n    height = np.floor(height / 2.0 + 0.5)\n\n    width = np.floor(image_width * max(cfg.TRAIN.SCALES) / 2.0 + 0.5)\n    width = np.floor(width / 2.0 + 0.5)\n    width = np.floor(width / 2.0 + 0.5)\n    width = np.floor(width / 2.0 + 0.5)\n  else:\n    assert (1), 'The network architecture is not supported in utils.get_boxes_grid!'\n\n  # compute the grid box centers\n  h = np.arange(height)\n  w = np.arange(width)\n  y, x = np.meshgrid(h, w, indexing='ij')\n  centers = np.dstack((x, y))\n  centers = np.reshape(centers, (-1, 2))\n  num = centers.shape[0]\n\n  # compute width and height of grid box\n  area = cfg.TRAIN.KERNEL_SIZE * cfg.TRAIN.KERNEL_SIZE\n  aspect = cfg.TRAIN.ASPECTS  # height / width\n  num_aspect = len(aspect)\n  widths = np.zeros((1, num_aspect), dtype=np.float32)\n  heights = np.zeros((1, num_aspect), dtype=np.float32)\n  for i in range(num_aspect):\n    widths[0, i] = math.sqrt(area / aspect[i])\n    heights[0, i] = widths[0, i] * aspect[i]\n\n  # construct grid boxes\n  centers = np.repeat(centers, num_aspect, axis=0)\n  widths = np.tile(widths, num).transpose()\n  heights = np.tile(heights, num).transpose()\n\n  x1 = np.reshape(centers[:, 0], (-1, 1)) - widths * 0.5\n  x2 = np.reshape(centers[:, 0], (-1, 1)) + widths * 0.5\n  y1 = np.reshape(centers[:, 1], (-1, 1)) - heights * 0.5\n  y2 = np.reshape(centers[:, 1], (-1, 1)) + heights * 0.5\n\n  boxes_grid = np.hstack((x1, y1, x2, y2)) / cfg.TRAIN.SPATIAL_SCALE\n\n  return boxes_grid, centers[:, 0], centers[:, 1]\n"
  },
  {
    "path": "lib/utils/nms.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\nimport numpy as np\n\ndef nms(dets, thresh):\n    x1 = dets[:, 0]\n    y1 = dets[:, 1]\n    x2 = dets[:, 2]\n    y2 = dets[:, 3]\n    scores = dets[:, 4]\n\n    areas = (x2 - x1 + 1) * (y2 - y1 + 1)\n    order = scores.argsort()[::-1]\n\n    keep = []\n    while order.size > 0:\n        i = order[0]\n        keep.append(i)\n        xx1 = np.maximum(x1[i], x1[order[1:]])\n        yy1 = np.maximum(y1[i], y1[order[1:]])\n        xx2 = np.minimum(x2[i], x2[order[1:]])\n        yy2 = np.minimum(y2[i], y2[order[1:]])\n\n        w = np.maximum(0.0, xx2 - xx1 + 1)\n        h = np.maximum(0.0, yy2 - yy1 + 1)\n        inter = w * h\n        ovr = inter / (areas[i] + areas[order[1:]] - inter)\n\n        inds = np.where(ovr <= thresh)[0]\n        order = order[inds + 1]\n\n    return keep\n"
  },
  {
    "path": "lib/utils/nms.pyx",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\nimport numpy as np\ncimport numpy as np\n\ncdef inline np.float32_t max(np.float32_t a, np.float32_t b):\n    return a if a >= b else b\n\ncdef inline np.float32_t min(np.float32_t a, np.float32_t b):\n    return a if a <= b else b\n\ndef nms(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):\n    cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]\n    cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n    cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]\n    cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]\n    cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4]\n\n    cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1)\n    cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1]\n\n    cdef int ndets = dets.shape[0]\n    cdef np.ndarray[np.int_t, ndim=1] suppressed = \\\n            np.zeros((ndets), dtype=np.int)\n\n    # nominal indices\n    cdef int _i, _j\n    # sorted indices\n    cdef int i, j\n    # temp variables for box i's (the box currently under consideration)\n    cdef np.float32_t ix1, iy1, ix2, iy2, iarea\n    # variables for computing overlap with box j (lower scoring box)\n    cdef np.float32_t xx1, yy1, xx2, yy2\n    cdef np.float32_t w, h\n    cdef np.float32_t inter, ovr\n\n    keep = []\n    for _i in range(ndets):\n        i = order[_i]\n        if suppressed[i] == 1:\n            continue\n        keep.append(i)\n        ix1 = x1[i]\n        iy1 = y1[i]\n        ix2 = x2[i]\n        iy2 = y2[i]\n        iarea = areas[i]\n        for _j in range(_i + 1, ndets):\n            j = order[_j]\n            if suppressed[j] == 1:\n                continue\n            xx1 = max(ix1, x1[j])\n            yy1 = max(iy1, y1[j])\n            xx2 = min(ix2, x2[j])\n            yy2 = min(iy2, y2[j])\n            w = max(0.0, xx2 - xx1 + 1)\n            h = max(0.0, yy2 - yy1 + 1)\n            inter = w * h\n            ovr = inter / (iarea + areas[j] - inter)\n            if ovr >= thresh:\n                suppressed[j] = 1\n\n    return keep\n\ndef nms_new(np.ndarray[np.float32_t, ndim=2] dets, np.float thresh):\n    cdef np.ndarray[np.float32_t, ndim=1] x1 = dets[:, 0]\n    cdef np.ndarray[np.float32_t, ndim=1] y1 = dets[:, 1]\n    cdef np.ndarray[np.float32_t, ndim=1] x2 = dets[:, 2]\n    cdef np.ndarray[np.float32_t, ndim=1] y2 = dets[:, 3]\n    cdef np.ndarray[np.float32_t, ndim=1] scores = dets[:, 4]\n\n    cdef np.ndarray[np.float32_t, ndim=1] areas = (x2 - x1 + 1) * (y2 - y1 + 1)\n    cdef np.ndarray[np.int_t, ndim=1] order = scores.argsort()[::-1]\n\n    cdef int ndets = dets.shape[0]\n    cdef np.ndarray[np.int_t, ndim=1] suppressed = \\\n            np.zeros((ndets), dtype=np.int)\n\n    # nominal indices\n    cdef int _i, _j\n    # sorted indices\n    cdef int i, j\n    # temp variables for box i's (the box currently under consideration)\n    cdef np.float32_t ix1, iy1, ix2, iy2, iarea\n    # variables for computing overlap with box j (lower scoring box)\n    cdef np.float32_t xx1, yy1, xx2, yy2\n    cdef np.float32_t w, h\n    cdef np.float32_t inter, ovr\n\n    keep = []\n    for _i in range(ndets):\n        i = order[_i]\n        if suppressed[i] == 1:\n            continue\n        keep.append(i)\n        ix1 = x1[i]\n        iy1 = y1[i]\n        ix2 = x2[i]\n        iy2 = y2[i]\n        iarea = areas[i]\n        for _j in range(_i + 1, ndets):\n            j = order[_j]\n            if suppressed[j] == 1:\n                continue\n            xx1 = max(ix1, x1[j])\n            yy1 = max(iy1, y1[j])\n            xx2 = min(ix2, x2[j])\n            yy2 = min(iy2, y2[j])\n            w = max(0.0, xx2 - xx1 + 1)\n            h = max(0.0, yy2 - yy1 + 1)\n            inter = w * h\n            ovr = inter / (iarea + areas[j] - inter)\n            ovr1 = inter / iarea\n            ovr2 = inter / areas[j]\n            if ovr >= thresh or ovr1 > 0.95 or ovr2 > 0.95:\n                suppressed[j] = 1\n\n    return keep\n"
  },
  {
    "path": "lib/utils/timer.py",
    "content": "# --------------------------------------------------------\n# Fast R-CNN\n# Copyright (c) 2015 Microsoft\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Ross Girshick\n# --------------------------------------------------------\n\nimport time\n\nclass Timer(object):\n    \"\"\"A simple timer.\"\"\"\n    def __init__(self):\n        self.total_time = 0.\n        self.calls = 0\n        self.start_time = 0.\n        self.diff = 0.\n        self.average_time = 0.\n\n    def tic(self):\n        # using time.time instead of time.clock because time time.clock\n        # does not normalize for multithreading\n        self.start_time = time.time()\n\n    def toc(self, average=True):\n        self.diff = time.time() - self.start_time\n        self.total_time += self.diff\n        self.calls += 1\n        self.average_time = self.total_time / self.calls\n        if average:\n            return self.average_time\n        else:\n            return self.diff\n"
  },
  {
    "path": "tools/_init_paths.py",
    "content": "import os.path as osp\nimport sys\n\ndef add_path(path):\n    if path not in sys.path:\n        sys.path.insert(0, path)\n\nthis_dir = osp.dirname(__file__)\n\n# Add lib to PYTHONPATH\nlib_path = osp.join(this_dir, '..', 'lib')\nadd_path(lib_path)\n\ncoco_path = osp.join(this_dir, '..', 'data', 'coco', 'PythonAPI')\nadd_path(coco_path)\n"
  },
  {
    "path": "tools/demo.py~",
    "content": "#!/usr/bin/env python\n\n# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen, based on code from Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"\nDemo script showing detections in sample images.\n\nSee README.md for installation instructions before running.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport _init_paths\nfrom model.config import cfg\nfrom model.test import im_detect\nfrom model.nms_wrapper import nms\n\nfrom utils.timer import Timer\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os, cv2\nimport argparse\n\nfrom nets.vgg16 import vgg16\nfrom nets.resnet_v1 import resnetv1\n\nCLASSES = ('__background__',\n           'aeroplane', 'bicycle', 'bird', 'boat',\n           'bottle', 'bus', 'car', 'cat', 'chair',\n           'cow', 'diningtable', 'dog', 'horse',\n           'motorbike', 'person', 'pottedplant',\n           'sheep', 'sofa', 'train', 'tvmonitor')\n\nNETS = {'vgg16': ('vgg16_faster_rcnn_iter_70000.ckpt',),'res101': ('res101_faster_rcnn_iter_110000.ckpt',)}\nDATASETS= {'pascal_voc': ('voc_2007_trainval',),'pascal_voc_0712': ('voc_2007_trainval+voc_2012_trainval',)}\n\ndef vis_detections(im, class_name, dets, thresh=0.5):\n    \"\"\"Draw detected bounding boxes.\"\"\"\n    inds = np.where(dets[:, -1] >= thresh)[0]\n    if len(inds) == 0:\n        return\n\n    im = im[:, :, (2, 1, 0)]\n    fig, ax = plt.subplots(figsize=(12, 12))\n    ax.imshow(im, aspect='equal')\n    for i in inds:\n        bbox = dets[i, :4]\n        score = dets[i, -1]\n\n        ax.add_patch(\n            plt.Rectangle((bbox[0], bbox[1]),\n                          bbox[2] - bbox[0],\n                          bbox[3] - bbox[1], fill=False,\n                          edgecolor='red', linewidth=3.5)\n            )\n        ax.text(bbox[0], bbox[1] - 2,\n                '{:s} {:.3f}'.format(class_name, score),\n                bbox=dict(facecolor='blue', alpha=0.5),\n                fontsize=14, color='white')\n\n    ax.set_title(('{} detections with '\n                  'p({} | box) >= {:.1f}').format(class_name, class_name,\n                                                  thresh),\n                  fontsize=14)\n    plt.axis('off')\n    plt.tight_layout()\n    plt.draw()\n\ndef demo(sess, net, image_name):\n    \"\"\"Detect object classes in an image using pre-computed object proposals.\"\"\"\n\n    # Load the demo image\n    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)\n    im = cv2.imread(im_file)\n\n    # Detect all object classes and regress object bounds\n    timer = Timer()\n    timer.tic()\n    scores, boxes = im_detect(sess, net, im)\n    timer.toc()\n    print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))\n\n    # Visualize detections for each class\n    CONF_THRESH = 0.8\n    NMS_THRESH = 0.3\n    for cls_ind, cls in enumerate(CLASSES[1:]):\n        cls_ind += 1 # because we skipped background\n        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]\n        cls_scores = scores[:, cls_ind]\n        dets = np.hstack((cls_boxes,\n                          cls_scores[:, np.newaxis])).astype(np.float32)\n        keep = nms(dets, NMS_THRESH)\n        dets = dets[keep, :]\n        vis_detections(im, cls, dets, thresh=CONF_THRESH)\n\ndef parse_args():\n    \"\"\"Parse input arguments.\"\"\"\n    parser = argparse.ArgumentParser(description='Tensorflow Faster R-CNN demo')\n    parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16 res101]',\n                        choices=NETS.keys(), default='res101')\n    parser.add_argument('--dataset', dest='dataset', help='Trained dataset [pascal_voc pascal_voc_0712]',\n                        choices=DATASETS.keys(), default='pascal_voc_0712')\n    args = parser.parse_args()\n\n    return args\n\nif __name__ == '__main__':\n    cfg.TEST.HAS_RPN = True  # Use RPN for proposals\n    args = parse_args()\n\n    # model path\n    demonet = args.demo_net\n    dataset = args.dataset\n    tfmodel = os.path.join('output', demonet, DATASETS[dataset][0], 'default',\n                              NETS[demonet][0])\n\n\n    if not os.path.isfile(tfmodel + '.meta'):\n        raise IOError(('{:s} not found.\\nDid you download the proper networks from '\n                       'our server and place them properly?').format(tfmodel + '.meta'))\n\n    # set config\n    tfconfig = tf.ConfigProto(allow_soft_placement=True)\n    tfconfig.gpu_options.allow_growth=True\n\n    # init session\n    sess = tf.Session(config=tfconfig)\n    # load network\n    if demonet == 'vgg16':\n        net = vgg16(batch_size=1)\n    elif demonet == 'res101':\n        net = resnetv1(batch_size=1, num_layers=101)\n    else:\n        raise NotImplementedError\n    net.create_architecture(sess, \"TEST\", 21,\n                          tag='default', anchor_scales=[8, 16, 32])\n    saver = tf.train.Saver()\n    saver.restore(sess, tfmodel)\n\n    print('Loaded network {:s}'.format(tfmodel))\n\n    im_names = ['0010.png','0012.png','0027.png','0038.png','0039.png','000456.jpg', '000542.jpg', '001150.jpg',\n                '001763.jpg', '004545.jpg']\n    for im_name in im_names:\n        print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')\n        print('Demo for data/demo/{}'.format(im_name))\n        demo(sess, net, im_name)\n\n    plt.show()\n"
  },
  {
    "path": "tools/demo_graspRGD.py",
    "content": "#!/usr/bin/env python\n\n# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen, based on code from Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"\nDemo script showing detections in sample images.\n\nSee README.md for installation instructions before running.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport _init_paths\nfrom model.config import cfg\nfrom model.test import im_detect\nfrom model.nms_wrapper import nms\n\nfrom utils.timer import Timer\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os, cv2\nimport argparse\n\nfrom nets.vgg16 import vgg16\nfrom nets.resnet_v1 import resnetv1\nimport scipy\nfrom shapely.geometry import Polygon\n\npi     = scipy.pi\ndot    = scipy.dot\nsin    = scipy.sin\ncos    = scipy.cos\nar     = scipy.array\n\nCLASSES = ('__background__',\n           'angle_01', 'angle_02', 'angle_03', 'angle_04', 'angle_05',\n           'angle_06', 'angle_07', 'angle_08', 'angle_09', 'angle_10',\n           'angle_11', 'angle_12', 'angle_13', 'angle_14', 'angle_15',\n           'angle_16', 'angle_17', 'angle_18', 'angle_19')\n\nNETS = {'vgg16': ('vgg16_faster_rcnn_iter_70000.ckpt',),'res101': ('res101_faster_rcnn_iter_110000.ckpt',),'res50': ('res50_faster_rcnn_iter_240000.ckpt',)}\nDATASETS= {'pascal_voc': ('voc_2007_trainval',),'pascal_voc_0712': ('voc_2007_trainval+voc_2012_trainval',),'grasp': ('train',)}\n\ndef Rotate2D(pts,cnt,ang=scipy.pi/4):\n    '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''\n    return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt\n\ndef vis_detections(ax, image_name, im, class_name, dets, thresh=0.5):\n    \"\"\"Draw detected bounding boxes.\"\"\"\n    inds = np.where(dets[:, -1] >= thresh)[0]\n    if len(inds) == 0:\n        return\n\n    im = im[:, :, (2, 1, 0)]\n    #fig, ax = plt.subplots(figsize=(12, 12))\n    ax.imshow(im, aspect='equal')\n    for i in inds:\n        bbox = dets[i, :4]\n        score = dets[i, -1]\n\n        #ax.add_patch(\n        #    plt.Rectangle((bbox[0], bbox[1]),\n        #                  bbox[2] - bbox[0],\n        #                  bbox[3] - bbox[1], fill=False,\n        #                  edgecolor='red', linewidth=3.5)\n        #    )\n\n        # plot rotated rectangles\n        pts = ar([[bbox[0],bbox[1]], [bbox[2], bbox[1]], [bbox[2], bbox[3]], [bbox[0], bbox[3]]])\n        cnt = ar([(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2])\n        angle = int(class_name[6:])\n        r_bbox = Rotate2D(pts, cnt, -pi/2-pi/20*(angle-1))\n        pred_label_polygon = Polygon([(r_bbox[0,0],r_bbox[0,1]), (r_bbox[1,0], r_bbox[1,1]), (r_bbox[2,0], r_bbox[2,1]), (r_bbox[3,0], r_bbox[3,1])])\n        pred_x, pred_y = pred_label_polygon.exterior.xy\n\n        plt.plot(pred_x[0:2],pred_y[0:2], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[1:3],pred_y[1:3], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[2:4],pred_y[2:4], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[3:5],pred_y[3:5], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n\n        #ax.text(bbox[0], bbox[1] - 2,\n        #        '{:s} {:.3f}'.format(class_name, score),\n        #        bbox=dict(facecolor='blue', alpha=0.5),\n        #        fontsize=14, color='white')\n\n    #ax.set_title(('{} detections with '\n    #              'p({} | box) >= {:.1f}').format(class_name, class_name,\n    #                                              thresh),\n    #              fontsize=14)\n    #plt.axis('off')\n    #plt.tight_layout()\n\n    #save result\n    #savepath = './data/demo/results/' + str(image_name) + str(class_name) + '.png'\n    #plt.savefig(savepath)\n\n    #plt.draw()\n\ndef demo(sess, net, image_name):\n    \"\"\"Detect object classes in an image using pre-computed object proposals.\"\"\"\n\n    # Load the demo image\n    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)\n    im = cv2.imread(im_file)\n    #print(im)\n\n    # Detect all object classes and regress object bounds\n    timer = Timer()\n    timer.tic()\n    scores, boxes = im_detect(sess, net, im)\n\n    #scores_max = scores[:,1:-1].max(axis=1)\n    #scores_max_idx = np.argmax(scores_max)\n    #scores = scores[scores_max_idx:scores_max_idx+1,:]\n    #boxes = boxes[scores_max_idx:scores_max_idx+1, :]\n\n    #im = cv2.imread('/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps_ivalab/rgb_cropped320/rgb_0076Cropped320.png')\n    timer.toc()\n    print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))\n\n    fig, ax = plt.subplots(figsize=(12, 12))\n    # Visualize detections for each class\n    CONF_THRESH = 0.1\t\n    NMS_THRESH = 0.3\n    for cls_ind, cls in enumerate(CLASSES[1:]):\n        cls_ind += 1 # because we skipped background\n        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]\n        cls_scores = scores[:, cls_ind]\n        dets = np.hstack((cls_boxes,\n                          cls_scores[:, np.newaxis])).astype(np.float32)\n        keep = nms(dets, NMS_THRESH)\n        dets = dets[keep, :]\n        vis_detections(ax, image_name, im, cls, dets, thresh=CONF_THRESH)\n        #tmp = max(cls_scores)\n\n    plt.axis('off')\n    plt.tight_layout()\n\n    #cv2.imshow('deepGrasp_top_score', im)\n    #choice = cv2.waitKey(100)\n    \n    #save result\n    savepath = './data/demo/results_all_cls/' + str(image_name) + '.png'\n    plt.savefig(savepath)\n\n    plt.draw()\n\ndef parse_args():\n    \"\"\"Parse input arguments.\"\"\"\n    parser = argparse.ArgumentParser(description='Tensorflow Faster R-CNN demo')\n    parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16 res101]',\n                        choices=NETS.keys(), default='res101')\n    parser.add_argument('--dataset', dest='dataset', help='Trained dataset [pascal_voc pascal_voc_0712]',\n                        choices=DATASETS.keys(), default='pascal_voc_0712')\n    args = parser.parse_args()\n\n    return args\n\nif __name__ == '__main__':\n    cfg.TEST.HAS_RPN = True  # Use RPN for proposals\n    args = parse_args()\n\n    # model path\n    demonet = args.demo_net\n    dataset = args.dataset\n    tfmodel = os.path.join('output', demonet, DATASETS[dataset][0], 'default',\n                              NETS[demonet][0])\n\n\n    if not os.path.isfile(tfmodel + '.meta'):\n        raise IOError(('{:s} not found.\\nDid you download the proper networks from '\n                       'our server and place them properly?').format(tfmodel + '.meta'))\n\n    # set config\n    tfconfig = tf.ConfigProto(allow_soft_placement=True)\n    tfconfig.gpu_options.allow_growth=True\n\n    # init session\n    sess = tf.Session(config=tfconfig)\n    # load network\n    if demonet == 'vgg16':\n        net = vgg16(batch_size=1)\n    elif demonet == 'res101':\n        net = resnetv1(batch_size=1, num_layers=101)\n    elif demonet == 'res50':\n        net = resnetv1(batch_size=1, num_layers=50)\n    else:\n        raise NotImplementedError\n    net.create_architecture(sess, \"TEST\", 20,\n                          tag='default', anchor_scales=[8, 16, 32])\n    saver = tf.train.Saver()\n    saver.restore(sess, tfmodel)\n\n    print('Loaded network {:s}'.format(tfmodel))\n\n    #im_names = ['rgd_0076Cropped320.png','rgd_0095.png','pcd0122r_rgd_preprocessed_1.png','pcd0875r_rgd_preprocessed_1.png','resized_0875_2.png']\n    im_names = ['pcd0100r_rgd_preprocessed_1.png','pcd0266r_rgd_preprocessed_1.png','pcd0882r_rgd_preprocessed_1.png','rgd_0000Cropped320.png']\n    for im_name in im_names:\n        print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')\n        print('Demo for data/demo/{}'.format(im_name))\n        demo(sess, net, im_name)\n\n    plt.show()\n"
  },
  {
    "path": "tools/demo_graspRGD.py~",
    "content": "#!/usr/bin/env python\n\n# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen, based on code from Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"\nDemo script showing detections in sample images.\n\nSee README.md for installation instructions before running.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport _init_paths\nfrom model.config import cfg\nfrom model.test import im_detect\nfrom model.nms_wrapper import nms\n\nfrom utils.timer import Timer\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os, cv2\nimport argparse\n\nfrom nets.vgg16 import vgg16\nfrom nets.resnet_v1 import resnetv1\nimport scipy\nfrom shapely.geometry import Polygon\n\npi     = scipy.pi\ndot    = scipy.dot\nsin    = scipy.sin\ncos    = scipy.cos\nar     = scipy.array\n\nCLASSES = ('__background__',\n           'angle_01', 'angle_02', 'angle_03', 'angle_04', 'angle_05',\n           'angle_06', 'angle_07', 'angle_08', 'angle_09', 'angle_10',\n           'angle_11', 'angle_12', 'angle_13', 'angle_14', 'angle_15',\n           'angle_16', 'angle_17', 'angle_18', 'angle_19')\n\nNETS = {'vgg16': ('vgg16_faster_rcnn_iter_70000.ckpt',),'res101': ('res101_faster_rcnn_iter_110000.ckpt',),'res50': ('res50_faster_rcnn_iter_240000.ckpt',)}\nDATASETS= {'pascal_voc': ('voc_2007_trainval',),'pascal_voc_0712': ('voc_2007_trainval+voc_2012_trainval',),'grasp': ('train',)}\n\ndef Rotate2D(pts,cnt,ang=scipy.pi/4):\n    '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''\n    return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt\n\ndef vis_detections(ax, image_name, im, class_name, dets, thresh=0.5):\n    \"\"\"Draw detected bounding boxes.\"\"\"\n    inds = np.where(dets[:, -1] >= thresh)[0]\n    if len(inds) == 0:\n        return\n\n    im = im[:, :, (2, 1, 0)]\n    #fig, ax = plt.subplots(figsize=(12, 12))\n    ax.imshow(im, aspect='equal')\n    for i in inds:\n        bbox = dets[i, :4]\n        score = dets[i, -1]\n\n        #ax.add_patch(\n        #    plt.Rectangle((bbox[0], bbox[1]),\n        #                  bbox[2] - bbox[0],\n        #                  bbox[3] - bbox[1], fill=False,\n        #                  edgecolor='red', linewidth=3.5)\n        #    )\n\n        # plot rotated rectangles\n        pts = ar([[bbox[0],bbox[1]], [bbox[2], bbox[1]], [bbox[2], bbox[3]], [bbox[0], bbox[3]]])\n        cnt = ar([(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2])\n        angle = int(class_name[6:])\n        r_bbox = Rotate2D(pts, cnt, -pi/2-pi/20*(angle-1))\n        pred_label_polygon = Polygon([(r_bbox[0,0],r_bbox[0,1]), (r_bbox[1,0], r_bbox[1,1]), (r_bbox[2,0], r_bbox[2,1]), (r_bbox[3,0], r_bbox[3,1])])\n        pred_x, pred_y = pred_label_polygon.exterior.xy\n\n        plt.plot(pred_x[0:2],pred_y[0:2], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[1:3],pred_y[1:3], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[2:4],pred_y[2:4], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[3:5],pred_y[3:5], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n\n        #ax.text(bbox[0], bbox[1] - 2,\n        #        '{:s} {:.3f}'.format(class_name, score),\n        #        bbox=dict(facecolor='blue', alpha=0.5),\n        #        fontsize=14, color='white')\n\n    #ax.set_title(('{} detections with '\n    #              'p({} | box) >= {:.1f}').format(class_name, class_name,\n    #                                              thresh),\n    #              fontsize=14)\n    #plt.axis('off')\n    #plt.tight_layout()\n\n    #save result\n    #savepath = './data/demo/results/' + str(image_name) + str(class_name) + '.png'\n    #plt.savefig(savepath)\n\n    #plt.draw()\n\ndef demo(sess, net, image_name):\n    \"\"\"Detect object classes in an image using pre-computed object proposals.\"\"\"\n\n    # Load the demo image\n    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)\n    im = cv2.imread(im_file)\n    #print(im)\n\n    # Detect all object classes and regress object bounds\n    timer = Timer()\n    timer.tic()\n    scores, boxes = im_detect(sess, net, im)\n\n    #scores_max = scores[:,1:-1].max(axis=1)\n    #scores_max_idx = np.argmax(scores_max)\n    #scores = scores[scores_max_idx:scores_max_idx+1,:]\n    #boxes = boxes[scores_max_idx:scores_max_idx+1, :]\n\n    #im = cv2.imread('/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps_ivalab/rgb_cropped320/rgb_0076Cropped320.png')\n    timer.toc()\n    print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))\n\n    fig, ax = plt.subplots(figsize=(12, 12))\n    # Visualize detections for each class\n    CONF_THRESH = 0.1\t\n    NMS_THRESH = 0.3\n    for cls_ind, cls in enumerate(CLASSES[1:]):\n        cls_ind += 1 # because we skipped background\n        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]\n        cls_scores = scores[:, cls_ind]\n        dets = np.hstack((cls_boxes,\n                          cls_scores[:, np.newaxis])).astype(np.float32)\n        keep = nms(dets, NMS_THRESH)\n        dets = dets[keep, :]\n        vis_detections(ax, image_name, im, cls, dets, thresh=CONF_THRESH)\n        #tmp = max(cls_scores)\n\n    plt.axis('off')\n    plt.tight_layout()\n\n    #cv2.imshow('deepGrasp_top_score', im)\n    #choice = cv2.waitKey(100)\n    \n    #save result\n    savepath = './data/demo/results_all_cls/' + str(image_name) + '.png'\n    plt.savefig(savepath)\n\n    plt.draw()\n\ndef parse_args():\n    \"\"\"Parse input arguments.\"\"\"\n    parser = argparse.ArgumentParser(description='Tensorflow Faster R-CNN demo')\n    parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16 res101]',\n                        choices=NETS.keys(), default='res101')\n    parser.add_argument('--dataset', dest='dataset', help='Trained dataset [pascal_voc pascal_voc_0712]',\n                        choices=DATASETS.keys(), default='pascal_voc_0712')\n    args = parser.parse_args()\n\n    return args\n\nif __name__ == '__main__':\n    cfg.TEST.HAS_RPN = True  # Use RPN for proposals\n    args = parse_args()\n\n    # model path\n    demonet = args.demo_net\n    dataset = args.dataset\n    tfmodel = os.path.join('output', demonet, DATASETS[dataset][0], 'default',\n                              NETS[demonet][0])\n\n\n    if not os.path.isfile(tfmodel + '.meta'):\n        raise IOError(('{:s} not found.\\nDid you download the proper networks from '\n                       'our server and place them properly?').format(tfmodel + '.meta'))\n\n    # set config\n    tfconfig = tf.ConfigProto(allow_soft_placement=True)\n    tfconfig.gpu_options.allow_growth=True\n\n    # init session\n    sess = tf.Session(config=tfconfig)\n    # load network\n    if demonet == 'vgg16':\n        net = vgg16(batch_size=1)\n    elif demonet == 'res101':\n        net = resnetv1(batch_size=1, num_layers=101)\n    elif demonet == 'res50':\n        net = resnetv1(batch_size=1, num_layers=50)\n    else:\n        raise NotImplementedError\n    net.create_architecture(sess, \"TEST\", 20,\n                          tag='default', anchor_scales=[8, 16, 32])\n    saver = tf.train.Saver()\n    saver.restore(sess, tfmodel)\n\n    print('Loaded network {:s}'.format(tfmodel))\n\n    #im_names = ['rgd_0076Cropped320.png','rgd_0095.png','pcd0122r_rgd_preprocessed_1.png','pcd0875r_rgd_preprocessed_1.png','resized_0875_2.png']\n    im_names = ['0010.png','0012.png','0027.png','0038.png','0039.png','pcd0875r_rgd_preprocessed_1.png','pic_0010.png']\n    for im_name in im_names:\n        print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')\n        print('Demo for data/demo/{}'.format(im_name))\n        demo(sess, net, im_name)\n\n    plt.show()\n"
  },
  {
    "path": "tools/demo_graspRGD_socket.py",
    "content": "#!/usr/bin/env python\n\n# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen, based on code from Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"\nDemo script showing detections in sample images.\n\nSee README.md for installation instructions before running.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport _init_paths\nfrom model.config import cfg\nfrom model.test import im_detect\nfrom model.nms_wrapper import nms\n\nfrom utils.timer import Timer\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os, cv2\nimport argparse\n\nimport threading\nimport socket\nimport struct\nimport time\nimport cv2.aruco as aruco\n\n\nfrom nets.vgg16 import vgg16\nfrom nets.resnet_v1 import resnetv1\nimport scipy\nfrom shapely.geometry import Polygon\n\npi     = scipy.pi\ndot    = scipy.dot\nsin    = scipy.sin\ncos    = scipy.cos\nar     = scipy.array\n\nframe_current=[]\npred_x, pred_y = np.asarray([1,50,100,200]), np.asarray([50, 100, 200, 400])\nCLASSES = ('__background__',\n           'angle_01', 'angle_02', 'angle_03', 'angle_04', 'angle_05',\n           'angle_06', 'angle_07', 'angle_08', 'angle_09', 'angle_10',\n           'angle_11', 'angle_12', 'angle_13', 'angle_14', 'angle_15',\n           'angle_16', 'angle_17', 'angle_18', 'angle_19')\n\nNETS = {'vgg16': ('vgg16_faster_rcnn_iter_70000.ckpt',),'res101': ('res101_faster_rcnn_iter_110000.ckpt',),'res50': ('res50_faster_rcnn_iter_240000.ckpt',)}\nDATASETS= {'pascal_voc': ('voc_2007_trainval',),'pascal_voc_0712': ('voc_2007_trainval+voc_2012_trainval',),'grasp': ('train',)}\n\nANGLES = (-1000,\n           0, -10, -20, -30, -40,\n           -50, -60, -70, -80, -90,\n           80, 70, 60, 50, 40,\n           30, 20, 10, 0)\n\n\ndef Rotate2D(pts,cnt,ang=scipy.pi/4):\n    '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''\n    return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt\n\ndef vis_detections(ax, im, class_name, dets, thresh=0.5):\n    \"\"\"Draw detected bounding boxes.\"\"\"\n    inds = np.where(dets[:, -1] >= thresh)[0]\n    if len(inds) == 0:\n        return\n\n    im = im[:, :, (2, 1, 0)]\n    #fig, ax = plt.subplots(figsize=(12, 12))\n    ax.imshow(im, aspect='equal')\n    for i in inds:\n        bbox = dets[i, :4]\n        score = dets[i, -1]\n\n        #ax.add_patch(\n        #    plt.Rectangle((bbox[0], bbox[1]),\n        #                  bbox[2] - bbox[0],\n        #                  bbox[3] - bbox[1], fill=False,\n        #                  edgecolor='red', linewidth=3.5)\n        #    )\n\n        # plot rotated rectangles\n        pts = ar([[bbox[0],bbox[1]], [bbox[2], bbox[1]], [bbox[2], bbox[3]], [bbox[0], bbox[3]]])\n        cnt = ar([(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2])\n        angle = int(class_name[6:])\n        r_bbox = Rotate2D(pts, cnt, -pi/2-pi/20*(angle-1))\n        pred_label_polygon = Polygon([(r_bbox[0,0],r_bbox[0,1]), (r_bbox[1,0], r_bbox[1,1]), (r_bbox[2,0], r_bbox[2,1]), (r_bbox[3,0], r_bbox[3,1])])\n        pred_x, pred_y = pred_label_polygon.exterior.xy\n\n        plt.plot(pred_x[0:2],pred_y[0:2], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[1:3],pred_y[1:3], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[2:4],pred_y[2:4], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[3:5],pred_y[3:5], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n\n        #ax.text(bbox[0], bbox[1] - 2,\n        #        '{:s} {:.3f}'.format(class_name, score),\n        #        bbox=dict(facecolor='blue', alpha=0.5),\n        #        fontsize=14, color='white')\n\n    #ax.set_title(('{} detections with '\n    #              'p({} | box) >= {:.1f}').format(class_name, class_name,\n    #                                              thresh),\n    #              fontsize=14)\n    #plt.axis('off')\n    #plt.tight_layout()\n\n    #save result\n    #savepath = './data/demo/results/' + str(image_name) + str(class_name) + '.png'\n    #plt.savefig(savepath)\n\n    #plt.draw()\n\ndef compute_imgRot(frame):\n    # aim to find a ARUCO marker and compute the camera rotation on XY plane\n\n    # camera matrix\n    markerLength = 0.06\n\n    # old camera matrix used by Yufeng\n    # cameraMatrix = np.array([[297.47608, 0.0, 320], [0.0, 297.14815, 240], [0.0, 0.0, 1.0]])  # camera frame from socket\n    # distCoeffs = np.array([0.15190073, -0.8267655, 0.00985276, -0.00435892, 1.58437205])  # fake value\n\n    # new camera matrix obtained at 01/2018\n    cameraMatrix = np.array(\n        [[592.90077, 0.0, 327.06503], [0.0, 591.07515, 239.40367], [0.0, 0.0, 1.0]])  # camera frame from socket\n    distCoeffs = np.array([-0.02067, 0.06351, -0.00285, 0.00083, 0.00000])  # fake value\n\n    # find ARUCO marker\n    gray = frame * 255\n    # print(gray)\n    gray = gray.astype(np.uint8)\n    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)\n    # print (gray)\n    # gray = gray.astype(np.uint8)\n\n    aruco_dict = aruco.Dictionary_get(aruco.DICT_ARUCO_ORIGINAL)\n    parameters = aruco.DetectorParameters_create()  #P_reshape = np.reshape(P, (7*7*2*2))\n  #top = P_reshape.argsort()[-10:][::-1]\n  #index = top[3]\n    corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)\n    print('detected aruco mark (table angle compensation): ' + str(ids))\n    gray = aruco.drawDetectedMarkers(gray, corners)\n\n    dst = 0\n    tvec = 0\n    T = 0\n    angle = 0.0\n    if ids is not None:\n        point0 = corners[0][0][0]\n        point3 = corners[0][0][3]\n        #if point0[1] >= point3[1]: # rotate counter-clock wise\n        angle = np.arctan((point0[1] - point3[1]) / (point3[0] - point0[0]))\n        print('counter-clock rotate angle in degree: ' + str(angle/3.14*180)) \n        print('(simply added to detected degree to compensate the table)')\n        return angle/3.14*180\n        # else: # rotate clock wise\n        #     angle = np.arctan((point3[1] - point0[1]) / (point3[0] - point0[0]))\n        #     print('clock')\n        #     print(angle/3.14*180)\n    else:\n         return angle\n\ndef coordinate_img2table(frame, u, v, rot):\n    \"\"\"project found u v coordinate on image to x y coordinate on table with ARUCO marker.\"\"\"\n\n    # camera matrix\n    markerLength = 0.06\n\n    # old camera matrix used by Yufeng\n    #cameraMatrix = np.array([[297.47608, 0.0, 320], [0.0, 297.14815, 240], [0.0, 0.0, 1.0]])  # camera frame from socket\n    #distCoeffs = np.array([0.15190073, -0.8267655, 0.00985276, -0.00435892, 1.58437205])  # fake value\n\n    # new camera matrix obtained at 01/2018\n    cameraMatrix = np.array([[592.90077, 0.0, 327.06503], [0.0, 591.07515, 239.40367], [0.0, 0.0, 1.0]])  # camera frame from socket\n    distCoeffs = np.array([-0.02067,   0.06351,   -0.00285,   0.00083, 0.00000])  # fake value\n\n    # find ARUCO marker\n    gray = frame*255\n    #print(gray)\n    gray = gray.astype(np.uint8)\n    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)\n    #print (gray)\n    #gray = gray.astype(np.uint8)\n\n    aruco_dict = aruco.Dictionary_get(aruco.DICT_ARUCO_ORIGINAL)\n    parameters = aruco.DetectorParameters_create()\n    corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)\n    print('detected aruco mark (table location): ' + str(ids))\n    gray = aruco.drawDetectedMarkers(gray, corners)\n\n    dst = 0\n    tvec = 0\n    T = 0\n    point_new = np.zeros((3, 1))\n    if ids is not None:\n        rvec, tvec, _ = aruco.estimatePoseSingleMarkers(corners[0], markerLength, cameraMatrix, distCoeffs)\n\n        dst, jacobian = cv2.Rodrigues(rvec)\n        T = np.zeros((4, 4))\n        T[:3, :3] = dst\n        T[:3, 3] = tvec\n        T[3, :] = np.array([0, 0, 0, 1])\n\n        # projection\n        imagePts = np.array([u, v, 1])\n        normal_old = np.array([0, 0, 1])\n        ray_center = np.array([0, 0, 0])\n        distance_old = 0\n\n        normal_new = np.dot(dst, normal_old) #(3,)\n        normal_new = np.expand_dims(normal_new, 1)  #(3,1)\n        translation_old = tvec #(1,1,3)\n        translation_old = np.squeeze(translation_old, 0) #(1,3)\n        distance_new = -(distance_old + np.dot(translation_old, normal_new))\n\n        ray = np.dot(np.linalg.inv(cameraMatrix), imagePts) #(3,)\n        t = -(np.dot(normal_new.transpose(), ray_center) + distance_new) / np.dot(normal_new.transpose(), ray) # (1,1)\n        intersection = np.multiply(ray, t) #(1,3)\n        intersection_homo = np.array([intersection[0,0], intersection[0,1], intersection[0,2], 1])\n\n        point_new = np.dot(np.linalg.inv(T), intersection_homo)\n\n    #print(point_new[0] + 0.36)#0.3\n    #print(point_new[1] + 0.35)#0.4#\n    print('location on table: ' + str(point_new[0] + 0.30))#0.3\n    print('location on table: ' + str(point_new[1] + 0.30))#0.4#\n    return point_new[0] + 0.30, point_new[1] + 0.30, rot\n\ndef demo_process(sess, net):\n    \"\"\"Detect object classes in an image using pre-computed object proposals.\"\"\"\n    count = 0\n    bbs_array = np.array([], dtype=np.float32).reshape(0, 5)\n    im = []\n    tmp_g = []\n    scores= []\n    boxes = []\n    #fig, ax = plt.subplots(figsize=(12, 12))\n    while True:\n      if frame_current != []:\n        print('\\n')\n        print('############ Start detection on a new frame ############')\n        bbs_array = np.array([], dtype=np.float32).reshape(0, 5)\n        # Load the demo image\n        #im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)\n        #im = cv2.imread(im_file)\n        #print (frame_current)\n\n        im = frame_current\n        tmp_g = im[:,:,1]\n        im[:,:,1] = im[:,:,2] \n        im[:,:,2] = tmp_g  \n        im = im*255\n\n        #img = im.astype('uint8')\n        #ax.imshow(img)\n\n        # Detect all object classes and regress object bounds\n        timer = Timer()\n        timer.tic()\n        scores, boxes = im_detect(sess, net, im)\n\n        #scores_max = scores[:,1:-1].max(axis=1)\n        #scores_max_idx = np.argmax(scores_max)\n        #scores = scores[scores_max_idx:scores_max_idx+1,:]\n        #boxes = boxes[scores_max_idx:scores_max_idx+1, :]\n\n        timer.toc()\n        print('deepGrasp detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))\n\n        #fig, ax = plt.subplots(figsize=(12, 12)) #uncommand this when writing files into disk\n        # Visualize detections for each class\n        CONF_THRESH = 0.001\n        CONF_THRESH = 0.1 # demo for spoon 2018/08\n        NMS_THRESH = 0.3\n \n        top_score = 0;\n        top_cls = 0;\n        angle_compensated = 0.0\n        top_boxes = np.zeros(2)\n        top_boxes_coor = np.zeros(4)\n        for cls_ind, cls in enumerate(CLASSES[1:]):\n            cls_ind += 1 # because we skipped background\n            cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]\n            cls_scores = scores[:, cls_ind]\n            dets = np.hstack((cls_boxes,\n                              cls_scores[:, np.newaxis])).astype(np.float32)\n            keep = nms(dets, NMS_THRESH)\n            dets = dets[keep, :]\n\n            # stack all det > threshold\n            dets_th = np.delete(dets, np.where(dets[:, -1] < CONF_THRESH)[0], axis=0)\n            bbs_array = np.vstack((bbs_array, dets_th))\n\n            #vis_detections(ax, im, cls, dets, thresh=CONF_THRESH) #uncommand if you want to visualize\n              \n            if ( max(cls_scores) > top_score):\n              #print (max(cls_scores))\n              #print(np.amax(cls_scores))\n              #print (np.argmax(cls_scores))\n              #print (cls_boxes[np.argmax(cls_scores),:])\n\n              top_boxes_coor = cls_boxes[np.argmax(cls_scores),:]\n              top_boxes[0] = (top_boxes_coor[0] + top_boxes_coor[2]) / 2\n              top_boxes[1] = (top_boxes_coor[1] + top_boxes_coor[3]) / 2\n              top_score = max(cls_scores)\n              top_cls = cls_ind\n\n        x_table = 0\n        y_table = 0\n\n        if bbs_array.shape[0] != 0:\n          print('candadiates grasp above threshold: ' + str(CONF_THRESH) + ' are found!')\n          bbs_array_cnt = np.transpose(np.vstack(( bbs_array[:,0]/2+bbs_array[:,2]/2,  bbs_array[:,1]/2+bbs_array[:,3]/2)))\n          bbs_array_cnt_mean = bbs_array_cnt.mean(axis=0, keepdims=True)\n          bbs_array_dist = np.sum(np.square(bbs_array_cnt - bbs_array_cnt_mean), axis=1)\n          bbs_array_ins = np.argmin(bbs_array_dist)\n          #circle2 = plt.Circle((bbs_array[bbs_array_ins,0]/2+bbs_array[bbs_array_ins,2]/2, bbs_array[bbs_array_ins,1]/2+bbs_array[bbs_array_ins,3]/2), 2, color='g')\n          #ax.add_artist(circle2)\n\n          USE_AVERAGE = False\n          if USE_AVERAGE:\n              # if need top 10, use this:\n              x_table, y_table, _ = coordinate_img2table(frame_current, bbs_array[bbs_array_ins,0]/2+bbs_array[bbs_array_ins,2]/2, bbs_array[bbs_array_ins,1]/2+bbs_array[bbs_array_ins,3]/2, top_cls)\n              bbox = top_boxes_coor\n          else:\n              # if need top 1, use this\n              x_table, y_table, _ = coordinate_img2table(frame_current, top_boxes[0], top_boxes[1], top_cls)\n              bbox = bbs_array[bbs_array_ins, :4] # (1, 5) = (x_min, y_min, x_max, y_max, score)\n          \n          angle_compensated = compute_imgRot(frame_current)\n\n          ## plot grasp, need to use change global pred_x and pred_y\n          pts = ar([[bbox[0],bbox[1]], [bbox[2], bbox[1]], [bbox[2], bbox[3]], [bbox[0], bbox[3]]])\n          cnt = ar([(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2])\n          r_bbox = Rotate2D(pts, cnt, -pi/2-pi/20*(top_cls-1))\n          pred_label_polygon = Polygon([(r_bbox[0,0],r_bbox[0,1]), (r_bbox[1,0], r_bbox[1,1]), (r_bbox[2,0], r_bbox[2,1]), (r_bbox[3,0], r_bbox[3,1])])\n          global pred_x\n          global pred_y\n          pred_x, pred_y = pred_label_polygon.exterior.xy\n\n        else:\n            print('no candidates above threshold: ' + str(CONF_THRESH) + 'found, lower down threshold')\n\n        print(\"top class is: \" + str(top_cls))\n        print(\"top class angle is: \" + str(ANGLES[top_cls] + angle_compensated))\n        print(\"top class location is: \" + str(top_boxes[0]) + \" \" + str(top_boxes[1]))\n\n        x_table_center, y_table_center, _ = coordinate_img2table(frame_current, 320, 240, 0)\n        print('camera is facing to '+str(x_table_center)+' '+str(y_table_center)+' on the table') \n\n        #circle1 = plt.Circle((top_boxes[0], top_boxes[1]), 2, color='y')\n        #ax.add_artist(circle1)\n\n        #plt.axis('off')\n        #plt.tight_layout()\n\n        #save result\n        count = count +1\n        #savepath = './data/demo/results_all_cls/' + str(count) + '.png'\n        #plt.savefig(savepath)\n        #plt.cla()\n\n    \n        file = open('/home/fujenchu/projects/robotArm/toy-opencv-mat-socket-server-master_pcl/bbs/rotation.txt', \"w\")\n        file.write(str(x_table) + '\\n')\n        file.write(str(y_table) + '\\n')\n        file.write(str(ANGLES[top_cls] + angle_compensated) + '\\n')\n        file.close()\n\n        file = open('/home/fujenchu/projects/robotArm/toy-opencv-mat-socket-server-master_pcl/bbs/cam_facing.txt', \"w\")\n        file.write(str(x_table_center) + '\\n')\n        file.write(str(y_table_center) + '\\n')\n        #file.write(str(ANGLES[top_cls] + angle_compensated) + '\\n')\n        file.close()\n\n\n        #plt.draw()\n        #plt.show()\n        #plt.clf()\n\n        #cv2.imshow('deepGrasp_top_score', frame_current)\n        #choice = cv2.waitKey(20)\n        #if choice == 27:\n        #    break\n\n\ndef parse_args():\n    \"\"\"Parse input arguments.\"\"\"\n    parser = argparse.ArgumentParser(description='Tensorflow Faster R-CNN demo')\n    parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16 res101]',\n                        choices=NETS.keys(), default='res101')\n    parser.add_argument('--dataset', dest='dataset', help='Trained dataset [pascal_voc pascal_voc_0712]',\n                        choices=DATASETS.keys(), default='pascal_voc_0712')\n    args = parser.parse_args()\n\n    return args\n\n\n\nif __name__ == '__main__':\n    cfg.TEST.HAS_RPN = True  # Use RPN for proposals\n    args = parse_args()\n\n    # model path\n    demonet = args.demo_net\n    dataset = args.dataset\n    tfmodel = os.path.join('output', demonet, DATASETS[dataset][0], 'default',\n                              NETS[demonet][0])\n\n\n    if not os.path.isfile(tfmodel + '.meta'):\n        raise IOError(('{:s} not found.\\nDid you download the proper networks from '\n                       'our server and place them properly?').format(tfmodel + '.meta'))\n\n    # set config\n    tfconfig = tf.ConfigProto(allow_soft_placement=True)\n    tfconfig.gpu_options.allow_growth=True\n\n    #tfconfig = tf.ConfigProto(device_count={'GPU': 0})\n\n    # init session\n    sess = tf.Session(config=tfconfig)\n    # load network\n    if demonet == 'vgg16':\n        net = vgg16(batch_size=1)\n    elif demonet == 'res101':\n        net = resnetv1(batch_size=1, num_layers=101)\n    elif demonet == 'res50':\n        net = resnetv1(batch_size=1, num_layers=50)\n    else:\n        raise NotImplementedError\n    net.create_architecture(sess, \"TEST\", 20,\n                          tag='default', anchor_scales=[8, 16, 32])\n    saver = tf.train.Saver()\n    saver.restore(sess, tfmodel)\n\n    print('Loaded network {:s}'.format(tfmodel))\n\n    demo_process = threading.Thread(target=demo_process, args=(sess,net))\n    demo_process.start()\n\n    # TCP/IP\n    HOST=''\n    PORT=2330\n    #PORT = 2325\n\n    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\n    print('Socket created')\n\n    s.bind((HOST,PORT))\n    print('Socket bind complete')\n    s.listen(10)\n    print('Socket now listening')\n\n    client,addr=s.accept()\n\n    #count = 0\n    frame = np.zeros((480, 640, 3))\n    while True:\n        start = time.time()\n        frame = np.zeros((480, 640, 3))\n        show_frame = np.zeros((480, 640, 3))\n        frame = np.reshape(frame, (480*640*3))\n        i=0\n        while i < 480*640*3:\n            data = client.recv(640*480*3 - i)\n            datalen = len(data)\n            #if len(data) != 1024:\n            datalen_str = str(len(data))\n            datalen_str = datalen_str + 'B'\n            data_up = struct.unpack(datalen_str, data)\n            data_up_np = np.asarray(data_up)\n            frame[i:i + len(data)] = data_up_np\n\n            i += len(data)\n\n        # frame_depth = np.zeros((240, 320, 1))\n        # show_frame_depth = np.zeros((240, 320, 1))\n        # frame_depth = np.reshape(frame_depth, (320*240*1))\n        # i=0\n        # while i < 240*640*1:\n        #     data = client.recv(240*640*1 - i)\n        #     # datalen = len(data)\n        #     # print(datalen)\n        #     # #if len(data) != 1024:\n        #     #datalen_str = str(len(data)/2)\n        #     #datalen_str = datalen_str + 'H'\n        #     #data_up = struct.unpack(datalen_str, data)\n        #     #data_up_np = np.asarray(data_up)\n        #     data_up_np = np.fromstring(data, dtype='>H')\n        #     frame_depth[i:i + 76800] = data_up_np\n        #\n        #     i += len(data)\n\n        ########## save images for calibration #######################\n        # if cv2.waitKey(10) == ord('s'):\n        #   count = count +1\n        #   savepath = './data/demo/live/' + str(count) + '.png'\n        #   cv2.imwrite(savepath, np.reshape(frame, (480, 640, 3)))\n        #\n        #   savepath = './data/demo/live/' + str(count) + '_d.png'\n        #   cv2.imwrite(savepath, np.reshape(frame_depth, (240, 320, 1))/ np.max(frame_depth))\n        ##############################################################\n\n        frame_current = np.reshape(frame, (480, 640, 3))/255.0\n        #frame_current_depth = 1-(np.reshape(frame_depth, (240, 320, 1)) / np.max(frame_depth))\n\n        duration = time.time()-start\n        #print(\"processed time main =\" + str(duration))\n\n        color = (255/255.0, 255/255.0, 255/255.0) \n        thickness = 1\n\n        start_point, end_point = (int(pred_x[0]), int(pred_y[0])), (int(pred_x[1]), int(pred_y[1])) \n        cv2.line(frame_current, start_point, end_point, color, thickness) \n\n        start_point, end_point = (int(pred_x[2]), int(pred_y[2])), (int(pred_x[3]), int(pred_y[3])) \n        cv2.line(frame_current, start_point, end_point, color, thickness) \n\n        color = (0, 0, 255/255.0) \n        thickness = 2\n\n        start_point, end_point = (int(pred_x[1]), int(pred_y[1])), (int(pred_x[2]), int(pred_y[2])) \n        cv2.line(frame_current, start_point, end_point, color, thickness) \n\n        start_point, end_point = (int(pred_x[3]), int(pred_y[3])), (int(pred_x[0]), int(pred_y[0])) \n        cv2.line(frame_current, start_point, end_point, color, thickness) \n\n        cv2.imshow('frame',frame_current)\n        #cv2.imshow('frame_depth', frame_current_depth)\n        cv2.waitKey(1)\n\n\n\n    #im_names = ['rgd_0076Cropped320.png','rgd_0095.png','pcd0122r_rgd_preprocessed_1.png','pcd0875r_rgd_preprocessed_1.png','resized_0875_2.png']\n    #im_names = ['pcd0875r_rgd_preprocessed_1.png','pic_0010.png']\n    #for im_name in im_names:\n    #    print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')\n    #    print('Demo for data/demo/{}'.format(im_name))\n    #    demo(sess, net, im_name)\n\n    #plt.show()\n"
  },
  {
    "path": "tools/demo_graspRGD_socket.py~",
    "content": "#!/usr/bin/env python\n\n# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen, based on code from Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"\nDemo script showing detections in sample images.\n\nSee README.md for installation instructions before running.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport _init_paths\nfrom model.config import cfg\nfrom model.test import im_detect\nfrom model.nms_wrapper import nms\n\nfrom utils.timer import Timer\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os, cv2\nimport argparse\n\nimport threading\nimport socket\nimport struct\nimport time\nimport cv2.aruco as aruco\n\n\nfrom nets.vgg16 import vgg16\nfrom nets.resnet_v1 import resnetv1\nimport scipy\nfrom shapely.geometry import Polygon\n\npi     = scipy.pi\ndot    = scipy.dot\nsin    = scipy.sin\ncos    = scipy.cos\nar     = scipy.array\n\nframe_current=[]\nCLASSES = ('__background__',\n           'angle_01', 'angle_02', 'angle_03', 'angle_04', 'angle_05',\n           'angle_06', 'angle_07', 'angle_08', 'angle_09', 'angle_10',\n           'angle_11', 'angle_12', 'angle_13', 'angle_14', 'angle_15',\n           'angle_16', 'angle_17', 'angle_18', 'angle_19')\n\nNETS = {'vgg16': ('vgg16_faster_rcnn_iter_70000.ckpt',),'res101': ('res101_faster_rcnn_iter_110000.ckpt',),'res50': ('res50_faster_rcnn_iter_240000.ckpt',)}\nDATASETS= {'pascal_voc': ('voc_2007_trainval',),'pascal_voc_0712': ('voc_2007_trainval+voc_2012_trainval',),'grasp': ('train',)}\n\nANGLES = (-1000,\n           0, -10, -20, -30, -40,\n           -50, -60, -70, -80, -90,\n           80, 70, 60, 50, 40,\n           30, 20, 10, 0)\n\n\ndef Rotate2D(pts,cnt,ang=scipy.pi/4):\n    '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''\n    return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt\n\ndef vis_detections(ax, im, class_name, dets, thresh=0.5):\n    \"\"\"Draw detected bounding boxes.\"\"\"\n    inds = np.where(dets[:, -1] >= thresh)[0]\n    if len(inds) == 0:\n        return\n\n    im = im[:, :, (2, 1, 0)]\n    #fig, ax = plt.subplots(figsize=(12, 12))\n    ax.imshow(im, aspect='equal')\n    for i in inds:\n        bbox = dets[i, :4]\n        score = dets[i, -1]\n\n        #ax.add_patch(\n        #    plt.Rectangle((bbox[0], bbox[1]),\n        #                  bbox[2] - bbox[0],\n        #                  bbox[3] - bbox[1], fill=False,\n        #                  edgecolor='red', linewidth=3.5)\n        #    )\n\n        # plot rotated rectangles\n        pts = ar([[bbox[0],bbox[1]], [bbox[2], bbox[1]], [bbox[2], bbox[3]], [bbox[0], bbox[3]]])\n        cnt = ar([(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2])\n        angle = int(class_name[6:])\n        r_bbox = Rotate2D(pts, cnt, -pi/2-pi/20*(angle-1))\n        pred_label_polygon = Polygon([(r_bbox[0,0],r_bbox[0,1]), (r_bbox[1,0], r_bbox[1,1]), (r_bbox[2,0], r_bbox[2,1]), (r_bbox[3,0], r_bbox[3,1])])\n        pred_x, pred_y = pred_label_polygon.exterior.xy\n\n        plt.plot(pred_x[0:2],pred_y[0:2], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[1:3],pred_y[1:3], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[2:4],pred_y[2:4], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[3:5],pred_y[3:5], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n\n        #ax.text(bbox[0], bbox[1] - 2,\n        #        '{:s} {:.3f}'.format(class_name, score),\n        #        bbox=dict(facecolor='blue', alpha=0.5),\n        #        fontsize=14, color='white')\n\n    #ax.set_title(('{} detections with '\n    #              'p({} | box) >= {:.1f}').format(class_name, class_name,\n    #                                              thresh),\n    #              fontsize=14)\n    #plt.axis('off')\n    #plt.tight_layout()\n\n    #save result\n    #savepath = './data/demo/results/' + str(image_name) + str(class_name) + '.png'\n    #plt.savefig(savepath)\n\n    #plt.draw()\n\ndef compute_imgRot(frame):\n    # aim to find a ARUCO marker and compute the camera rotation on XY plane\n\n    # camera matrix\n    markerLength = 0.06\n\n    # old camera matrix used by Yufeng\n    # cameraMatrix = np.array([[297.47608, 0.0, 320], [0.0, 297.14815, 240], [0.0, 0.0, 1.0]])  # camera frame from socket\n    # distCoeffs = np.array([0.15190073, -0.8267655, 0.00985276, -0.00435892, 1.58437205])  # fake value\n\n    # new camera matrix obtained at 01/2018\n    cameraMatrix = np.array(\n        [[592.90077, 0.0, 327.06503], [0.0, 591.07515, 239.40367], [0.0, 0.0, 1.0]])  # camera frame from socket\n    distCoeffs = np.array([-0.02067, 0.06351, -0.00285, 0.00083, 0.00000])  # fake value\n\n    # find ARUCO marker\n    gray = frame * 255\n    # print(gray)\n    gray = gray.astype(np.uint8)\n    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)\n    # print (gray)\n    # gray = gray.astype(np.uint8)\n\n    aruco_dict = aruco.Dictionary_get(aruco.DICT_ARUCO_ORIGINAL)\n    parameters = aruco.DetectorParameters_create()  #P_reshape = np.reshape(P, (7*7*2*2))\n  #top = P_reshape.argsort()[-10:][::-1]\n  #index = top[3]\n    corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)\n    print(ids)\n    gray = aruco.drawDetectedMarkers(gray, corners)\n\n    dst = 0\n    tvec = 0\n    T = 0\n    angle = 0.0\n    if ids is not None:\n        point0 = corners[0][0][0]\n        point3 = corners[0][0][3]\n        #if point0[1] >= point3[1]: # rotate counter-clock wise\n        angle = np.arctan((point0[1] - point3[1]) / (point3[0] - point0[0]))\n        print('counter-clock rotate angle in degree: (just added to detected degree to compensate the table)')\n        print(angle/3.14*180)\n        return angle/3.14*180\n        # else: # rotate clock wise\n        #     angle = np.arctan((point3[1] - point0[1]) / (point3[0] - point0[0]))\n        #     print('clock')\n        #     print(angle/3.14*180)\n    else:\n         return angle\n\ndef coordinate_img2table(frame, u, v, rot):\n    \"\"\"project found u v coordinate on image to x y coordinate on table with ARUCO marker.\"\"\"\n\n    # camera matrix\n    markerLength = 0.06\n\n    # old camera matrix used by Yufeng\n    #cameraMatrix = np.array([[297.47608, 0.0, 320], [0.0, 297.14815, 240], [0.0, 0.0, 1.0]])  # camera frame from socket\n    #distCoeffs = np.array([0.15190073, -0.8267655, 0.00985276, -0.00435892, 1.58437205])  # fake value\n\n    # new camera matrix obtained at 01/2018\n    cameraMatrix = np.array([[592.90077, 0.0, 327.06503], [0.0, 591.07515, 239.40367], [0.0, 0.0, 1.0]])  # camera frame from socket\n    distCoeffs = np.array([-0.02067,   0.06351,   -0.00285,   0.00083, 0.00000])  # fake value\n\n    # find ARUCO marker\n    gray = frame*255\n    #print(gray)\n    gray = gray.astype(np.uint8)\n    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)\n    #print (gray)\n    #gray = gray.astype(np.uint8)\n\n    aruco_dict = aruco.Dictionary_get(aruco.DICT_ARUCO_ORIGINAL)\n    parameters = aruco.DetectorParameters_create()\n    corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)\n    print(ids)\n    gray = aruco.drawDetectedMarkers(gray, corners)\n\n    dst = 0\n    tvec = 0\n    T = 0\n    point_new = np.zeros((3, 1))\n    if ids is not None:\n        rvec, tvec, _ = aruco.estimatePoseSingleMarkers(corners[0], markerLength, cameraMatrix, distCoeffs)\n\n        dst, jacobian = cv2.Rodrigues(rvec)\n        T = np.zeros((4, 4))\n        T[:3, :3] = dst\n        T[:3, 3] = tvec\n        T[3, :] = np.array([0, 0, 0, 1])\n\n        # projection\n        imagePts = np.array([u, v, 1])\n        normal_old = np.array([0, 0, 1])\n        ray_center = np.array([0, 0, 0])\n        distance_old = 0\n\n        normal_new = np.dot(dst, normal_old) #(3,)\n        normal_new = np.expand_dims(normal_new, 1)  #(3,1)\n        translation_old = tvec #(1,1,3)\n        translation_old = np.squeeze(translation_old, 0) #(1,3)\n        distance_new = -(distance_old + np.dot(translation_old, normal_new))\n\n        ray = np.dot(np.linalg.inv(cameraMatrix), imagePts) #(3,)\n        t = -(np.dot(normal_new.transpose(), ray_center) + distance_new) / np.dot(normal_new.transpose(), ray) # (1,1)\n        intersection = np.multiply(ray, t) #(1,3)\n        intersection_homo = np.array([intersection[0,0], intersection[0,1], intersection[0,2], 1])\n\n        point_new = np.dot(np.linalg.inv(T), intersection_homo)\n\n    #print(point_new[0] + 0.36)#0.3\n    #print(point_new[1] + 0.35)#0.4#\n    print(point_new[0] + 0.30)#0.3\n    print(point_new[1] + 0.30)#0.4#\n    return point_new[0] + 0.30, point_new[1] + 0.30, rot\n\ndef demo_process(sess, net):\n    \"\"\"Detect object classes in an image using pre-computed object proposals.\"\"\"\n    count = 0\n    bbs_array = np.array([], dtype=np.float32).reshape(0, 5)\n    im = []\n    tmp_g = []\n    scores= []\n    boxes = []\n    fig, ax = plt.subplots(figsize=(12, 12))\n    while True:\n      if frame_current != []:\n        print('flag')\n        bbs_array = np.array([], dtype=np.float32).reshape(0, 5)\n        # Load the demo image\n        #im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)\n        #im = cv2.imread(im_file)\n        #print (frame_current)\n\n        im = frame_current\n        tmp_g = im[:,:,1]\n        im[:,:,1] = im[:,:,2] \n        im[:,:,2] = tmp_g  \n        im = im*255\n\n        img = im.astype('uint8')\n        ax.imshow(img)\n\n        # Detect all object classes and regress object bounds\n        timer = Timer()\n        timer.tic()\n        scores, boxes = im_detect(sess, net, im)\n\n        #scores_max = scores[:,1:-1].max(axis=1)\n        #scores_max_idx = np.argmax(scores_max)\n        #scores = scores[scores_max_idx:scores_max_idx+1,:]\n        #boxes = boxes[scores_max_idx:scores_max_idx+1, :]\n\n        timer.toc()\n        print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))\n\n        #fig, ax = plt.subplots(figsize=(12, 12)) #uncommand this when writing files into disk\n        # Visualize detections for each class\n        CONF_THRESH = 0.001\n        CONF_THRESH = 0.5 # demo for spoon 2018/08\n        NMS_THRESH = 0.3\n \n        top_score = 0;\n        top_cls = 0;\n        angle_compensated = 0.0\n        top_boxes = np.zeros(2)\n        for cls_ind, cls in enumerate(CLASSES[1:]):\n            cls_ind += 1 # because we skipped background\n            cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]\n            cls_scores = scores[:, cls_ind]\n            dets = np.hstack((cls_boxes,\n                              cls_scores[:, np.newaxis])).astype(np.float32)\n            keep = nms(dets, NMS_THRESH)\n            dets = dets[keep, :]\n\n            # stack all det > threshold\n            dets_th = np.delete(dets, np.where(dets[:, -1] < CONF_THRESH)[0], axis=0)\n            bbs_array = np.vstack((bbs_array, dets_th))\n\n            #vis_detections(ax, im, cls, dets, thresh=CONF_THRESH) #uncommand if you want to visualize\n              \n            if ( max(cls_scores) > top_score):\n              #print (max(cls_scores))\n              #print(np.amax(cls_scores))\n              #print (np.argmax(cls_scores))\n              #print (cls_boxes[np.argmax(cls_scores),:])\n\n              tmp_top_boxes = cls_boxes[np.argmax(cls_scores),:]\n              top_boxes[0] = (tmp_top_boxes[0] + tmp_top_boxes[2]) / 2\n              top_boxes[1] = (tmp_top_boxes[1] + tmp_top_boxes[3]) / 2\n              top_score = max(cls_scores)\n              top_cls = cls_ind\n\n        x_table = 0\n        y_table = 0\n        if bbs_array.shape[0] != 0:\n          print('array got~')\n          bbs_array_cnt = np.transpose(np.vstack(( bbs_array[:,0]/2+bbs_array[:,2]/2,  bbs_array[:,1]/2+bbs_array[:,3]/2)))\n          bbs_array_cnt_mean = bbs_array_cnt.mean(axis=0, keepdims=True)\n          bbs_array_dist = np.sum(np.square(bbs_array_cnt - bbs_array_cnt_mean), axis=1)\n          bbs_array_ins = np.argmin(bbs_array_dist)\n          circle2 = plt.Circle((bbs_array[bbs_array_ins,0]/2+bbs_array[bbs_array_ins,2]/2, bbs_array[bbs_array_ins,1]/2+bbs_array[bbs_array_ins,3]/2), 2, color='g')\n          ax.add_artist(circle2)\n\n          # if need top 10, use this:\n          #x_table, y_table, _ = coordinate_img2table(frame_current, bbs_array[bbs_array_ins,0]/2+bbs_array[bbs_array_ins,2]/2, bbs_array[bbs_array_ins,1]/2+bbs_array[bbs_array_ins,3]/2, top_cls)\n          # if need top 1, use this\n          x_table, y_table, _ = coordinate_img2table(frame_current, top_boxes[0], top_boxes[1], top_cls)\n\n          angle_compensated = compute_imgRot(frame_current)\n        else:\n            print('no array!!')\n\n        print(\"top class is: \" + str(top_cls))\n        print(\"top class angle is: \" + str(ANGLES[top_cls] + angle_compensated))\n        print(\"top class location is: \" + str(top_boxes[0]) + \" \" + str(top_boxes[1]))\n\n        #coordinate_img2table(frame_current, top_boxes[0], top_boxes[1], top_cls)\n\n        circle1 = plt.Circle((top_boxes[0], top_boxes[1]), 2, color='y')\n        ax.add_artist(circle1)\n\n        plt.axis('off')\n        plt.tight_layout()\n\n        #save result\n        count = count +1\n        #savepath = './data/demo/results_all_cls/' + str(count) + '.png'\n        #plt.savefig(savepath)\n        #plt.cla()\n\n    \n        file = open('/home/fujenchu/projects/robotArm/toy-opencv-mat-socket-server-master_pcl/bbs/rotation.txt', \"w\")\n        file.write(str(x_table) + '\\n')\n        file.write(str(y_table) + '\\n')\n        file.write(str(ANGLES[top_cls] + angle_compensated) + '\\n')\n        file.close()\n\n\n        #plt.draw()\n        plt.show()\n        #plt.clf()\n\n        #cv2.imshow('deepGrasp_top_score', frame_current)\n        #choice = cv2.waitKey(20)\n        #if choice == 27:\n        #    break\n\ndef parse_args():\n    \"\"\"Parse input arguments.\"\"\"\n    parser = argparse.ArgumentParser(description='Tensorflow Faster R-CNN demo')\n    parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16 res101]',\n                        choices=NETS.keys(), default='res101')\n    parser.add_argument('--dataset', dest='dataset', help='Trained dataset [pascal_voc pascal_voc_0712]',\n                        choices=DATASETS.keys(), default='pascal_voc_0712')\n    args = parser.parse_args()\n\n    return args\n\n\n\nif __name__ == '__main__':\n    cfg.TEST.HAS_RPN = True  # Use RPN for proposals\n    args = parse_args()\n\n    # model path\n    demonet = args.demo_net\n    dataset = args.dataset\n    tfmodel = os.path.join('output', demonet, DATASETS[dataset][0], 'default',\n                              NETS[demonet][0])\n\n\n    if not os.path.isfile(tfmodel + '.meta'):\n        raise IOError(('{:s} not found.\\nDid you download the proper networks from '\n                       'our server and place them properly?').format(tfmodel + '.meta'))\n\n    # set config\n    tfconfig = tf.ConfigProto(allow_soft_placement=True)\n    tfconfig.gpu_options.allow_growth=True\n\n    #tfconfig = tf.ConfigProto(device_count={'GPU': 0})\n\n    # init session\n    sess = tf.Session(config=tfconfig)\n    # load network\n    if demonet == 'vgg16':\n        net = vgg16(batch_size=1)\n    elif demonet == 'res101':\n        net = resnetv1(batch_size=1, num_layers=101)\n    elif demonet == 'res50':\n        net = resnetv1(batch_size=1, num_layers=50)\n    else:\n        raise NotImplementedError\n    net.create_architecture(sess, \"TEST\", 20,\n                          tag='default', anchor_scales=[8, 16, 32])\n    saver = tf.train.Saver()\n    saver.restore(sess, tfmodel)\n\n    print('Loaded network {:s}'.format(tfmodel))\n\n    demo_process = threading.Thread(target=demo_process, args=(sess,net))\n    demo_process.start()\n\n    # TCP/IP\n    HOST=''\n    PORT=2330\n    #PORT = 2325\n\n    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\n    print('Socket created')\n\n    s.bind((HOST,PORT))\n    print('Socket bind complete')\n    s.listen(10)\n    print('Socket now listening')\n\n    client,addr=s.accept()\n\n    #count = 0\n    frame = np.zeros((480, 640, 3))\n    while True:\n        start = time.time()\n        frame = np.zeros((480, 640, 3))\n        show_frame = np.zeros((480, 640, 3))\n        frame = np.reshape(frame, (480*640*3))\n        i=0\n        while i < 480*640*3:\n            data = client.recv(640*480*3 - i)\n            datalen = len(data)\n            #if len(data) != 1024:\n            datalen_str = str(len(data))\n            datalen_str = datalen_str + 'B'\n            data_up = struct.unpack(datalen_str, data)\n            data_up_np = np.asarray(data_up)\n            frame[i:i + len(data)] = data_up_np\n\n            i += len(data)\n\n        # frame_depth = np.zeros((240, 320, 1))\n        # show_frame_depth = np.zeros((240, 320, 1))\n        # frame_depth = np.reshape(frame_depth, (320*240*1))\n        # i=0\n        # while i < 240*640*1:\n        #     data = client.recv(240*640*1 - i)\n        #     # datalen = len(data)\n        #     # print(datalen)\n        #     # #if len(data) != 1024:\n        #     #datalen_str = str(len(data)/2)\n        #     #datalen_str = datalen_str + 'H'\n        #     #data_up = struct.unpack(datalen_str, data)\n        #     #data_up_np = np.asarray(data_up)\n        #     data_up_np = np.fromstring(data, dtype='>H')\n        #     frame_depth[i:i + 76800] = data_up_np\n        #\n        #     i += len(data)\n\n        ########## save images for calibration #######################\n        # if cv2.waitKey(10) == ord('s'):\n        #   count = count +1\n        #   savepath = './data/demo/live/' + str(count) + '.png'\n        #   cv2.imwrite(savepath, np.reshape(frame, (480, 640, 3)))\n        #\n        #   savepath = './data/demo/live/' + str(count) + '_d.png'\n        #   cv2.imwrite(savepath, np.reshape(frame_depth, (240, 320, 1))/ np.max(frame_depth))\n        ##############################################################\n\n        frame_current = np.reshape(frame, (480, 640, 3))/255.0\n        #frame_current_depth = 1-(np.reshape(frame_depth, (240, 320, 1)) / np.max(frame_depth))\n\n        duration = time.time()-start\n        #print(\"processed time main =\" + str(duration))\n\n        cv2.imshow('frame',frame_current)\n        #cv2.imshow('frame_depth', frame_current_depth)\n        cv2.waitKey(1)\n\n\n    #im_names = ['rgd_0076Cropped320.png','rgd_0095.png','pcd0122r_rgd_preprocessed_1.png','pcd0875r_rgd_preprocessed_1.png','resized_0875_2.png']\n    #im_names = ['pcd0875r_rgd_preprocessed_1.png','pic_0010.png']\n    #for im_name in im_names:\n    #    print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')\n    #    print('Demo for data/demo/{}'.format(im_name))\n    #    demo(sess, net, im_name)\n\n    #plt.show()\n"
  },
  {
    "path": "tools/demo_graspRGD_socket_drawer.py~",
    "content": "#!/usr/bin/env python\n\n# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen, based on code from Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"\nDemo script showing detections in sample images.\n\nSee README.md for installation instructions before running.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport _init_paths\nfrom model.config import cfg\nfrom model.test import im_detect\nfrom model.nms_wrapper import nms\n\nfrom utils.timer import Timer\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os, cv2\nimport argparse\n\nimport threading\nimport socket\nimport struct\nimport time\nimport cv2.aruco as aruco\n\n\nfrom nets.vgg16 import vgg16\nfrom nets.resnet_v1 import resnetv1\nimport scipy\nfrom shapely.geometry import Polygon\n\npi     = scipy.pi\ndot    = scipy.dot\nsin    = scipy.sin\ncos    = scipy.cos\nar     = scipy.array\n\nframe_current=[]\nCLASSES = ('__background__',\n           'angle_01', 'angle_02', 'angle_03', 'angle_04', 'angle_05',\n           'angle_06', 'angle_07', 'angle_08', 'angle_09', 'angle_10',\n           'angle_11', 'angle_12', 'angle_13', 'angle_14', 'angle_15',\n           'angle_16', 'angle_17', 'angle_18', 'angle_19')\n\nNETS = {'vgg16': ('vgg16_faster_rcnn_iter_70000.ckpt',),'res101': ('res101_faster_rcnn_iter_110000.ckpt',),'res50': ('res50_faster_rcnn_iter_240000.ckpt',)}\nDATASETS= {'pascal_voc': ('voc_2007_trainval',),'pascal_voc_0712': ('voc_2007_trainval+voc_2012_trainval',),'grasp': ('train',)}\n\nANGLES = (-1000,\n           0, -10, -20, -30, -40,\n           -50, -60, -70, -80, -90,\n           80, 70, 60, 50, 40,\n           30, 20, 10, 0)\n\n\ndef Rotate2D(pts,cnt,ang=scipy.pi/4):\n    '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''\n    return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt\n\ndef vis_detections(ax, im, class_name, dets, thresh=0.5):\n    \"\"\"Draw detected bounding boxes.\"\"\"\n    inds = np.where(dets[:, -1] >= thresh)[0]\n    if len(inds) == 0:\n        return\n\n    im = im[:, :, (2, 1, 0)]\n    #fig, ax = plt.subplots(figsize=(12, 12))\n    ax.imshow(im, aspect='equal')\n    for i in inds:\n        bbox = dets[i, :4]\n        score = dets[i, -1]\n\n        #ax.add_patch(\n        #    plt.Rectangle((bbox[0], bbox[1]),\n        #                  bbox[2] - bbox[0],\n        #                  bbox[3] - bbox[1], fill=False,\n        #                  edgecolor='red', linewidth=3.5)\n        #    )\n\n        # plot rotated rectangles\n        pts = ar([[bbox[0],bbox[1]], [bbox[2], bbox[1]], [bbox[2], bbox[3]], [bbox[0], bbox[3]]])\n        cnt = ar([(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2])\n        angle = int(class_name[6:])\n        r_bbox = Rotate2D(pts, cnt, -pi/2-pi/20*(angle-1))\n        pred_label_polygon = Polygon([(r_bbox[0,0],r_bbox[0,1]), (r_bbox[1,0], r_bbox[1,1]), (r_bbox[2,0], r_bbox[2,1]), (r_bbox[3,0], r_bbox[3,1])])\n        pred_x, pred_y = pred_label_polygon.exterior.xy\n\n        plt.plot(pred_x[0:2],pred_y[0:2], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[1:3],pred_y[1:3], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[2:4],pred_y[2:4], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[3:5],pred_y[3:5], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n\n        #ax.text(bbox[0], bbox[1] - 2,\n        #        '{:s} {:.3f}'.format(class_name, score),\n        #        bbox=dict(facecolor='blue', alpha=0.5),\n        #        fontsize=14, color='white')\n\n    #ax.set_title(('{} detections with '\n    #              'p({} | box) >= {:.1f}').format(class_name, class_name,\n    #                                              thresh),\n    #              fontsize=14)\n    #plt.axis('off')\n    #plt.tight_layout()\n\n    #save result\n    #savepath = './data/demo/results/' + str(image_name) + str(class_name) + '.png'\n    #plt.savefig(savepath)\n\n    #plt.draw()\n\ndef compute_imgRot(frame):\n    # aim to find a ARUCO marker and compute the camera rotation on XY plane\n\n    # camera matrix\n    markerLength = 0.06\n\n    # old camera matrix used by Yufeng\n    # cameraMatrix = np.array([[297.47608, 0.0, 320], [0.0, 297.14815, 240], [0.0, 0.0, 1.0]])  # camera frame from socket\n    # distCoeffs = np.array([0.15190073, -0.8267655, 0.00985276, -0.00435892, 1.58437205])  # fake value\n\n    # new camera matrix obtained at 01/2018\n    cameraMatrix = np.array(\n        [[592.90077, 0.0, 327.06503], [0.0, 591.07515, 239.40367], [0.0, 0.0, 1.0]])  # camera frame from socket\n    distCoeffs = np.array([-0.02067, 0.06351, -0.00285, 0.00083, 0.00000])  # fake value\n\n    # find ARUCO marker\n    gray = frame * 255\n    # print(gray)\n    gray = gray.astype(np.uint8)\n    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)\n    # print (gray)\n    # gray = gray.astype(np.uint8)\n\n    aruco_dict = aruco.Dictionary_get(aruco.DICT_ARUCO_ORIGINAL)\n    parameters = aruco.DetectorParameters_create()\n    corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)\n    print(ids)\n    gray = aruco.drawDetectedMarkers(gray, corners)\n\n    dst = 0\n    tvec = 0\n    T = 0\n    angle = 0.0\n    if ids is not None:\n        point0 = corners[0][0][0]\n        point3 = corners[0][0][3]\n        #if point0[1] >= point3[1]: # rotate counter-clock wise\n        angle = np.arctan((point0[1] - point3[1]) / (point3[0] - point0[0]))\n        print('counter-clock rotate angle in degree: (just added to detected degree to compensate the table)')\n        print(angle/3.14*180)\n        return angle/3.14*180\n        # else: # rotate clock wise\n        #     angle = np.arctan((point3[1] - point0[1]) / (point3[0] - point0[0]))\n        #     print('clock')\n        #     print(angle/3.14*180)\n    else:\n         return angle\n\n\ndef coordinate_drawerTop2table(frame):\n    \"\"\"find the marker on the drawer and its location relative to robot base (and marker on the table)\"\"\"\n    gray = frame*255\n    gray = gray.astype(np.uint8)\n    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)\n\n    # shared camera info\n    cameraMatrix = np.array([[592.90077, 0.0, 327.06503], [0.0, 591.07515, 239.40367], [0.0, 0.0, 1.0]])  # camera frame from socket\n    distCoeffs = np.array([-0.02067,   0.06351,   -0.00285,   0.00083, 0.00000])  # fake value\n\n    # find marker on the table (T_o1_c)\n    aruco_dict = aruco.Dictionary_get(aruco.DICT_ARUCO_ORIGINAL)\n    parameters = aruco.DetectorParameters_create()\n    corners_o1, ids_o1, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)\n    markerLength_o1 = 0.06\n\n    T_o1_c = np.random.rand(4,4)\n    if ids_o1 is not None and ids_o1[0] == 466:\n      print(ids_o1)\n      rvec, tvec, _ = aruco.estimatePoseSingleMarkers(corners_o1[0], markerLength_o1, cameraMatrix, distCoeffs)\n      dst, jacobian = cv2.Rodrigues(rvec)\n      T_o1_c[:3, :3] = dst\n      T_o1_c[:3, 3] = tvec\n      T_o1_c[3, :] = np.array([0, 0, 0, 1])\n      aruco.drawAxis(gray, cameraMatrix, distCoeffs, rvec, tvec, markerLength_o1)\n\n    # find marker on the drawer (T_o2_c)\n    aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)\n    parameters = aruco.DetectorParameters_create()\n    corners_o2, ids_o2, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)\n    markerLength_o2 = 0.03\n\n    T_o2_c = np.random.rand(4,4)\n    if ids_o2 is not None and ids_o2[0] == 4:\n      print(ids_o2)\n      rvec, tvec, _ = aruco.estimatePoseSingleMarkers(corners_o2[0], markerLength_o2, cameraMatrix, distCoeffs)\n      dst, jacobian = cv2.Rodrigues(rvec)\n      T_o2_c[:3, :3] = dst\n      T_o2_c[:3, 3] = tvec\n      T_o2_c[3, :] = np.array([0, 0, 0, 1])\n      aruco.drawAxis(gray, cameraMatrix, distCoeffs, rvec, tvec, markerLength_o2)\n\n    if ids_o1 is not None and ids_o2 is not None:\n    # find q_o1 = T_o2_o1 * q_o2 = T_c_o1 * T_o2_c * q_o2 = T_o1_c' * T_o2_c * q_o2 (with q_o2 = [0,0,0,1]')\n      q_o2 = np.array((0.0, 0.0, 0.0, 1.0))\n      q_o1 = np.dot(np.linalg.inv(T_o1_c), np.dot(T_o2_c, q_o2))\n      q_o1 = q_o1/q_o1[3]\n\n      print(q_o1[0] + 0.40)\n      print(q_o1[1] + 0.30)\n      print(q_o1[2] - 0.033)\n\n      file = open('/home/fujenchu/projects/robotArm/toy-opencv-mat-socket-server-master_pcl/bbs/3Dlocations_drawer.txt', \"w\")\n      file.write(str(class_name))\n      file.write(\"\\n\")\n      file.write(str(xmin))\n      file.write(\"\\n\")\n      file.write(str(ymin))\n      file.write(\"\\n\")\n      file.write(str(xmax))\n      file.write(\"\\n\")\n      file.write(str(ymax))\n      file.write(\"\\n\")\n      file.close()\n\n      angle = 0.0\n      vector_o1 = corners_o1[0][0][3] - corners_o1[0][0][1]\n      vector_o2 = corners_o2[0][0][3] - corners_o2[0][0][1]\n      L_o1 = np.sqrt(vector_o1.dot(vector_o1))\n      L_o2 = np.sqrt(vector_o2.dot(vector_o2))\n\n      cos_angle = vector_o1.dot(vector_o2)/(L_o1*L_o2)\n      angle = np.arccos(cos_angle)/3.14*180\n      print(angle)\n\n\n\n\n\n\ndef coordinate_img2table(frame, u, v, rot):\n    \"\"\"project found u v coordinate on image to x y coordinate on table with ARUCO marker.\"\"\"\n\n    # camera matrix\n    markerLength = 0.06\n\n    # old camera matrix used by Yufeng\n    #cameraMatrix = np.array([[297.47608, 0.0, 320], [0.0, 297.14815, 240], [0.0, 0.0, 1.0]])  # camera frame from socket\n    #distCoeffs = np.array([0.15190073, -0.8267655, 0.00985276, -0.00435892, 1.58437205])  # fake value\n\n    # new camera matrix obtained at 01/2018\n    cameraMatrix = np.array([[592.90077, 0.0, 327.06503], [0.0, 591.07515, 239.40367], [0.0, 0.0, 1.0]])  # camera frame from socket\n    distCoeffs = np.array([-0.02067,   0.06351,   -0.00285,   0.00083, 0.00000])  # fake value\n\n    # find ARUCO marker\n    gray = frame*255\n    #print(gray)\n    gray = gray.astype(np.uint8)\n    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)\n    #print (gray)\n    #gray = gray.astype(np.uint8)\n\n    aruco_dict = aruco.Dictionary_get(aruco.DICT_ARUCO_ORIGINAL)\n    parameters = aruco.DetectorParameters_create()\n    corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)\n    print(ids)\n    gray = aruco.drawDetectedMarkers(gray, corners)\n\n    dst = 0\n    tvec = 0\n    T = 0\n    point_new = np.zeros((3, 1))\n    if ids is not None:\n        rvec, tvec, _ = aruco.estimatePoseSingleMarkers(corners[0], markerLength, cameraMatrix, distCoeffs)\n\n        dst, jacobian = cv2.Rodrigues(rvec)\n        T = np.zeros((4, 4))\n        T[:3, :3] = dst\n        T[:3, 3] = tvec\n        T[3, :] = np.array([0, 0, 0, 1])\n\n        # projection\n        imagePts = np.array([u, v, 1])\n        normal_old = np.array([0, 0, 1])\n        ray_center = np.array([0, 0, 0])\n        distance_old = 0\n\n        normal_new = np.dot(dst, normal_old) #(3,)\n        normal_new = np.expand_dims(normal_new, 1)  #(3,1)\n        translation_old = tvec #(1,1,3)\n        translation_old = np.squeeze(translation_old, 0) #(1,3)\n        distance_new = -(distance_old + np.dot(translation_old, normal_new))\n\n        ray = np.dot(np.linalg.inv(cameraMatrix), imagePts) #(3,)\n        t = -(np.dot(normal_new.transpose(), ray_center) + distance_new) / np.dot(normal_new.transpose(), ray) # (1,1)\n        intersection = np.multiply(ray, t) #(1,3)\n        intersection_homo = np.array([intersection[0,0], intersection[0,1], intersection[0,2], 1])\n\n        point_new = np.dot(np.linalg.inv(T), intersection_homo)\n\n    #print(point_new[0] + 0.36)#0.3\n    #print(point_new[1] + 0.35)#0.4#\n    print(point_new[0] + 0.40)#0.3\n    print(point_new[1] + 0.30)#0.4#\n    return point_new[0] + 0.40, point_new[1] + 0.30, rot\n\ndef demo_process(sess, net):\n    \"\"\"Detect object classes in an image using pre-computed object proposals.\"\"\"\n    count = 0\n    bbs_array = np.array([], dtype=np.float32).reshape(0, 5)\n    im = []\n    tmp_g = []\n    scores= []\n    boxes = []\n    fig, ax = plt.subplots(figsize=(12, 12))\n    while True:\n      if frame_current != []:\n        print('flag')\n        bbs_array = np.array([], dtype=np.float32).reshape(0, 5)\n        # Load the demo image\n        #im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)\n        #im = cv2.imread(im_file)\n        #print (frame_current)\n\n        im = frame_current\n        tmp_g = im[:,:,1]\n        im[:,:,1] = im[:,:,2] \n        im[:,:,2] = tmp_g  \n        im = im*255\n\n        # Detect all object classes and regress object bounds\n        timer = Timer()\n        timer.tic()\n        scores, boxes = im_detect(sess, net, im)\n\n        #scores_max = scores[:,1:-1].max(axis=1)\n        #scores_max_idx = np.argmax(scores_max)\n        #scores = scores[scores_max_idx:scores_max_idx+1,:]\n        #boxes = boxes[scores_max_idx:scores_max_idx+1, :]\n\n        timer.toc()\n        print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))\n\n        #fig, ax = plt.subplots(figsize=(12, 12)) #uncommand this when writing files into disk\n        # Visualize detections for each class\n        CONF_THRESH = 0.00005\n        NMS_THRESH = 0.3\n \n        top_score = 0;\n        top_cls = 0;\n        angle_compensated = 0.0\n        top_boxes = np.zeros(2)\n        for cls_ind, cls in enumerate(CLASSES[1:]):\n            cls_ind += 1 # because we skipped background\n            cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]\n            cls_scores = scores[:, cls_ind]\n            dets = np.hstack((cls_boxes,\n                              cls_scores[:, np.newaxis])).astype(np.float32)\n            keep = nms(dets, NMS_THRESH)\n            dets = dets[keep, :]\n\n            # stack all det > threshold\n            dets_th = np.delete(dets, np.where(dets[:, -1] < CONF_THRESH)[0], axis=0)\n            bbs_array = np.vstack((bbs_array, dets_th))\n\n            #vis_detections(ax, im, cls, dets, thresh=CONF_THRESH) #uncommand if you want to visualize\n              \n            if ( max(cls_scores) > top_score):\n              #print (max(cls_scores))\n              #print(np.amax(cls_scores))\n              #print (np.argmax(cls_scores))\n              #print (cls_boxes[np.argmax(cls_scores),:])\n\n              tmp_top_boxes = cls_boxes[np.argmax(cls_scores),:]\n              top_boxes[0] = (tmp_top_boxes[0] + tmp_top_boxes[2]) / 2\n              top_boxes[1] = (tmp_top_boxes[1] + tmp_top_boxes[3]) / 2\n              top_score = max(cls_scores)\n              top_cls = cls_ind\n\n        x_table = 0\n        y_table = 0\n        if bbs_array.shape[0] != 0:\n          print('array got~')\n          bbs_array_cnt = np.transpose(np.vstack(( bbs_array[:,0]/2+bbs_array[:,2]/2,  bbs_array[:,1]/2+bbs_array[:,3]/2)))\n          bbs_array_cnt_mean = bbs_array_cnt.mean(axis=0, keepdims=True)\n          bbs_array_dist = np.sum(np.square(bbs_array_cnt - bbs_array_cnt_mean), axis=1)\n          bbs_array_ins = np.argmin(bbs_array_dist)\n          circle2 = plt.Circle((bbs_array[bbs_array_ins,0]/2+bbs_array[bbs_array_ins,2]/2, bbs_array[bbs_array_ins,1]/2+bbs_array[bbs_array_ins,3]/2), 2, color='g')\n          ax.add_artist(circle2)\n\n          # if need top 10, use this:\n          x_table, y_table, _ = coordinate_img2table(frame_current, bbs_array[bbs_array_ins,0]/2+bbs_array[bbs_array_ins,2]/2, bbs_array[bbs_array_ins,1]/2+bbs_array[bbs_array_ins,3]/2, top_cls)\n          # if need top 1, use this\n          x_table, y_table, _ = coordinate_img2table(frame_current, top_boxes[0], top_boxes[1], top_cls)\n\n          angle_compensated = compute_imgRot(frame_current)\n        else:\n            print('no array!!')\n\n        print(\"top class is: \" + str(top_cls))\n        print(\"top class angle is: \" + str(ANGLES[top_cls] + angle_compensated))\n        print(\"top class location is: \" + str(top_boxes[0]) + \" \" + str(top_boxes[1]))\n\n        #coordinate_img2table(frame_current, top_boxes[0], top_boxes[1], top_cls)\n\n        circle1 = plt.Circle((top_boxes[0], top_boxes[1]), 2, color='y')\n        ax.add_artist(circle1)\n\n        plt.axis('off')\n        plt.tight_layout()\n\n        #save result\n        count = count +1\n        #savepath = './data/demo/results_all_cls/' + str(count) + '.png'\n        #plt.savefig(savepath)\n\n        coordinate_drawerTop2table(frame_current)\n        file = open('/home/fujenchu/projects/robotArm/toy-opencv-mat-socket-server-master_pcl/bbs/rotation.txt', \"w\")\n        file.write(str(x_table) + '\\n')\n        file.write(str(y_table) + '\\n')\n        file.write(str(ANGLES[top_cls] + angle_compensated) + '\\n')\n        file.close()\n\n        #plt.draw()\n        #plt.show()\n        #cv2.imshow('deepGrasp_top_score', frame_current)\n        #choice = cv2.waitKey(20)\n        #if choice == 27:\n        #    break\n\ndef parse_args():\n    \"\"\"Parse input arguments.\"\"\"\n    parser = argparse.ArgumentParser(description='Tensorflow Faster R-CNN demo')\n    parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16 res101]',\n                        choices=NETS.keys(), default='res101')\n    parser.add_argument('--dataset', dest='dataset', help='Trained dataset [pascal_voc pascal_voc_0712]',\n                        choices=DATASETS.keys(), default='pascal_voc_0712')\n    args = parser.parse_args()\n\n    return args\n\n\n\nif __name__ == '__main__':\n    cfg.TEST.HAS_RPN = True  # Use RPN for proposals\n    args = parse_args()\n\n    # model path\n    demonet = args.demo_net\n    dataset = args.dataset\n    tfmodel = os.path.join('output', demonet, DATASETS[dataset][0], 'default',\n                              NETS[demonet][0])\n\n\n    if not os.path.isfile(tfmodel + '.meta'):\n        raise IOError(('{:s} not found.\\nDid you download the proper networks from '\n                       'our server and place them properly?').format(tfmodel + '.meta'))\n\n    # set config\n    tfconfig = tf.ConfigProto(allow_soft_placement=True)\n    tfconfig.gpu_options.allow_growth=True\n\n    #tfconfig = tf.ConfigProto(device_count={'GPU': 0})\n\n    # init session\n    sess = tf.Session(config=tfconfig)\n    # load network\n    if demonet == 'vgg16':\n        net = vgg16(batch_size=1)\n    elif demonet == 'res101':\n        net = resnetv1(batch_size=1, num_layers=101)\n    elif demonet == 'res50':\n        net = resnetv1(batch_size=1, num_layers=50)\n    else:\n        raise NotImplementedError\n    net.create_architecture(sess, \"TEST\", 20,\n                          tag='default', anchor_scales=[8, 16, 32])\n    saver = tf.train.Saver()\n    saver.restore(sess, tfmodel)\n\n    print('Loaded network {:s}'.format(tfmodel))\n\n    demo_process = threading.Thread(target=demo_process, args=(sess,net))\n    demo_process.start()\n\n    # TCP/IP\n    HOST=''\n    PORT=2330\n    #PORT = 2325\n\n    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\n    print('Socket created')\n\n    s.bind((HOST,PORT))\n    print('Socket bind complete')\n    s.listen(10)\n    print('Socket now listening')\n\n    client,addr=s.accept()\n\n    #count = 0\n    frame = np.zeros((480, 640, 3))\n    while True:\n        start = time.time()\n        frame = np.zeros((480, 640, 3))\n        show_frame = np.zeros((480, 640, 3))\n        frame = np.reshape(frame, (480*640*3))\n        i=0\n        while i < 480*640*3:\n            data = client.recv(640*480*3 - i)\n            datalen = len(data)\n            #if len(data) != 1024:\n            datalen_str = str(len(data))\n            datalen_str = datalen_str + 'B'\n            data_up = struct.unpack(datalen_str, data)\n            data_up_np = np.asarray(data_up)\n            frame[i:i + len(data)] = data_up_np\n\n            i += len(data)\n\n        # frame_depth = np.zeros((240, 320, 1))\n        # show_frame_depth = np.zeros((240, 320, 1))\n        # frame_depth = np.reshape(frame_depth, (320*240*1))\n        # i=0\n        # while i < 240*640*1:\n        #     data = client.recv(240*640*1 - i)\n        #     # datalen = len(data)\n        #     # print(datalen)\n        #     # #if len(data) != 1024:\n        #     #datalen_str = str(len(data)/2)\n        #     #datalen_str = datalen_str + 'H'\n        #     #data_up = struct.unpack(datalen_str, data)\n        #     #data_up_np = np.asarray(data_up)\n        #     data_up_np = np.fromstring(data, dtype='>H')\n        #     frame_depth[i:i + 76800] = data_up_np\n        #\n        #     i += len(data)\n\n        ########## save images for calibration #######################\n        # if cv2.waitKey(10) == ord('s'):\n        #   count = count +1\n        #   savepath = './data/demo/live/' + str(count) + '.png'\n        #   cv2.imwrite(savepath, np.reshape(frame, (480, 640, 3)))\n        #\n        #   savepath = './data/demo/live/' + str(count) + '_d.png'\n        #   cv2.imwrite(savepath, np.reshape(frame_depth, (240, 320, 1))/ np.max(frame_depth))\n        ##############################################################\n\n        frame_current = np.reshape(frame, (480, 640, 3))/255.0\n        #frame_current_depth = 1-(np.reshape(frame_depth, (240, 320, 1)) / np.max(frame_depth))\n\n        duration = time.time()-start\n        #print(\"processed time main =\" + str(duration))\n\n        cv2.imshow('frame',frame_current)\n        #cv2.imshow('frame_depth', frame_current_depth)\n        cv2.waitKey(1)\n\n\n    #im_names = ['rgd_0076Cropped320.png','rgd_0095.png','pcd0122r_rgd_preprocessed_1.png','pcd0875r_rgd_preprocessed_1.png','resized_0875_2.png']\n    #im_names = ['pcd0875r_rgd_preprocessed_1.png','pic_0010.png']\n    #for im_name in im_names:\n    #    print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')\n    #    print('Demo for data/demo/{}'.format(im_name))\n    #    demo(sess, net, im_name)\n\n    #plt.show()\n"
  },
  {
    "path": "tools/demo_graspRGD_socket_save_to_rgbd.py~",
    "content": "#!/usr/bin/env python\n\n# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen, based on code from Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"\nDemo script showing detections in sample images.\n\nSee README.md for installation instructions before running.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport _init_paths\nfrom model.config import cfg\nfrom model.test import im_detect\nfrom model.nms_wrapper import nms\n\nfrom utils.timer import Timer\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os, cv2\nimport argparse\n\nimport threading\nimport socket\nimport struct\nimport time\nimport cv2.aruco as aruco\n\n\nfrom nets.vgg16 import vgg16\nfrom nets.resnet_v1 import resnetv1\nimport scipy\nfrom shapely.geometry import Polygon\n\npi     = scipy.pi\ndot    = scipy.dot\nsin    = scipy.sin\ncos    = scipy.cos\nar     = scipy.array\n\nframe_current=[]\nCLASSES = ('__background__',\n           'angle_01', 'angle_02', 'angle_03', 'angle_04', 'angle_05',\n           'angle_06', 'angle_07', 'angle_08', 'angle_09', 'angle_10',\n           'angle_11', 'angle_12', 'angle_13', 'angle_14', 'angle_15',\n           'angle_16', 'angle_17', 'angle_18', 'angle_19')\n\nNETS = {'vgg16': ('vgg16_faster_rcnn_iter_70000.ckpt',),'res101': ('res101_faster_rcnn_iter_110000.ckpt',),'res50': ('res50_faster_rcnn_iter_240000.ckpt',)}\nDATASETS= {'pascal_voc': ('voc_2007_trainval',),'pascal_voc_0712': ('voc_2007_trainval+voc_2012_trainval',),'grasp': ('train',)}\n\nANGLES = (-1000,\n           0, -10, -20, -30, -40,\n           -50, -60, -70, -80, -90,\n           80, 70, 60, 50, 40,\n           30, 20, 10, 0)\n\n\ndef Rotate2D(pts,cnt,ang=scipy.pi/4):\n    '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''\n    return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt\n\ndef vis_detections(ax, im, class_name, dets, thresh=0.5):\n    \"\"\"Draw detected bounding boxes.\"\"\"\n    inds = np.where(dets[:, -1] >= thresh)[0]\n    if len(inds) == 0:\n        return\n\n    im = im[:, :, (2, 1, 0)]\n    #fig, ax = plt.subplots(figsize=(12, 12))\n    ax.imshow(im, aspect='equal')\n    for i in inds:\n        bbox = dets[i, :4]\n        score = dets[i, -1]\n\n        #ax.add_patch(\n        #    plt.Rectangle((bbox[0], bbox[1]),\n        #                  bbox[2] - bbox[0],\n        #                  bbox[3] - bbox[1], fill=False,\n        #                  edgecolor='red', linewidth=3.5)\n        #    )\n\n        # plot rotated rectangles\n        pts = ar([[bbox[0],bbox[1]], [bbox[2], bbox[1]], [bbox[2], bbox[3]], [bbox[0], bbox[3]]])\n        cnt = ar([(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2])\n        angle = int(class_name[6:])\n        r_bbox = Rotate2D(pts, cnt, -pi/2-pi/20*(angle-1))\n        pred_label_polygon = Polygon([(r_bbox[0,0],r_bbox[0,1]), (r_bbox[1,0], r_bbox[1,1]), (r_bbox[2,0], r_bbox[2,1]), (r_bbox[3,0], r_bbox[3,1])])\n        pred_x, pred_y = pred_label_polygon.exterior.xy\n\n        plt.plot(pred_x[0:2],pred_y[0:2], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[1:3],pred_y[1:3], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[2:4],pred_y[2:4], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[3:5],pred_y[3:5], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n\n        #ax.text(bbox[0], bbox[1] - 2,\n        #        '{:s} {:.3f}'.format(class_name, score),\n        #        bbox=dict(facecolor='blue', alpha=0.5),\n        #        fontsize=14, color='white')\n\n    #ax.set_title(('{} detections with '\n    #              'p({} | box) >= {:.1f}').format(class_name, class_name,\n    #                                              thresh),\n    #              fontsize=14)\n    #plt.axis('off')\n    #plt.tight_layout()\n\n    #save result\n    #savepath = './data/demo/results/' + str(image_name) + str(class_name) + '.png'\n    #plt.savefig(savepath)\n\n    #plt.draw()\n\ndef compute_imgRot(frame):\n    # aim to find a ARUCO marker and compute the camera rotation on XY plane\n\n    # camera matrix\n    markerLength = 0.06\n\n    # old camera matrix used by Yufeng\n    # cameraMatrix = np.array([[297.47608, 0.0, 320], [0.0, 297.14815, 240], [0.0, 0.0, 1.0]])  # camera frame from socket\n    # distCoeffs = np.array([0.15190073, -0.8267655, 0.00985276, -0.00435892, 1.58437205])  # fake value\n\n    # new camera matrix obtained at 01/2018\n    cameraMatrix = np.array(\n        [[592.90077, 0.0, 327.06503], [0.0, 591.07515, 239.40367], [0.0, 0.0, 1.0]])  # camera frame from socket\n    distCoeffs = np.array([-0.02067, 0.06351, -0.00285, 0.00083, 0.00000])  # fake value\n\n    # find ARUCO marker\n    gray = frame * 255\n    # print(gray)\n    gray = gray.astype(np.uint8)\n    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)\n    # print (gray)\n    # gray = gray.astype(np.uint8)\n\n    aruco_dict = aruco.Dictionary_get(aruco.DICT_ARUCO_ORIGINAL)\n    parameters = aruco.DetectorParameters_create()\n    corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)\n    print(ids)\n    gray = aruco.drawDetectedMarkers(gray, corners)\n\n    dst = 0\n    tvec = 0\n    T = 0\n    angle = 0.0\n    if ids is not None:\n        point0 = corners[0][0][0]\n        point3 = corners[0][0][3]\n        #if point0[1] >= point3[1]: # rotate counter-clock wise\n        angle = np.arctan((point0[1] - point3[1]) / (point3[0] - point0[0]))\n        print('counter-clock rotate angle in degree: (just added to detected degree to compensate the table)')\n        print(angle/3.14*180)\n        return angle/3.14*180\n        # else: # rotate clock wise\n        #     angle = np.arctan((point3[1] - point0[1]) / (point3[0] - point0[0]))\n        #     print('clock')\n        #     print(angle/3.14*180)\n    else:\n         return angle\n\ndef coordinate_img2table(frame, u, v, rot):\n    \"\"\"project found u v coordinate on image to x y coordinate on table with ARUCO marker.\"\"\"\n\n    # camera matrix\n    markerLength = 0.06\n\n    # old camera matrix used by Yufeng\n    #cameraMatrix = np.array([[297.47608, 0.0, 320], [0.0, 297.14815, 240], [0.0, 0.0, 1.0]])  # camera frame from socket\n    #distCoeffs = np.array([0.15190073, -0.8267655, 0.00985276, -0.00435892, 1.58437205])  # fake value\n\n    # new camera matrix obtained at 01/2018\n    cameraMatrix = np.array([[592.90077, 0.0, 327.06503], [0.0, 591.07515, 239.40367], [0.0, 0.0, 1.0]])  # camera frame from socket\n    distCoeffs = np.array([-0.02067,   0.06351,   -0.00285,   0.00083, 0.00000])  # fake value\n\n    # find ARUCO marker\n    gray = frame*255\n    #print(gray)\n    gray = gray.astype(np.uint8)\n    gray = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)\n    #print (gray)\n    #gray = gray.astype(np.uint8)\n\n    aruco_dict = aruco.Dictionary_get(aruco.DICT_ARUCO_ORIGINAL)\n    parameters = aruco.DetectorParameters_create()\n    corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)\n    print(ids)\n    gray = aruco.drawDetectedMarkers(gray, corners)\n\n    dst = 0\n    tvec = 0\n    T = 0\n    point_new = np.zeros((3, 1))\n    if ids is not None:\n        rvec, tvec, _ = aruco.estimatePoseSingleMarkers(corners[0], markerLength, cameraMatrix, distCoeffs)\n\n        dst, jacobian = cv2.Rodrigues(rvec)\n        T = np.zeros((4, 4))\n        T[:3, :3] = dst\n        T[:3, 3] = tvec\n        T[3, :] = np.array([0, 0, 0, 1])\n\n        # projection\n        imagePts = np.array([u, v, 1])\n        normal_old = np.array([0, 0, 1])\n        ray_center = np.array([0, 0, 0])\n        distance_old = 0\n\n        normal_new = np.dot(dst, normal_old) #(3,)\n        normal_new = np.expand_dims(normal_new, 1)  #(3,1)\n        translation_old = tvec #(1,1,3)\n        translation_old = np.squeeze(translation_old, 0) #(1,3)\n        distance_new = -(distance_old + np.dot(translation_old, normal_new))\n\n        ray = np.dot(np.linalg.inv(cameraMatrix), imagePts) #(3,)\n        t = -(np.dot(normal_new.transpose(), ray_center) + distance_new) / np.dot(normal_new.transpose(), ray) # (1,1)\n        intersection = np.multiply(ray, t) #(1,3)\n        intersection_homo = np.array([intersection[0,0], intersection[0,1], intersection[0,2], 1])\n\n        point_new = np.dot(np.linalg.inv(T), intersection_homo)\n\n    #print(point_new[0] + 0.36)#0.3\n    #print(point_new[1] + 0.35)#0.4#\n    print(point_new[0] + 0.40)#0.3\n    print(point_new[1] + 0.30)#0.4#\n    return point_new[0] + 0.40, point_new[1] + 0.30, rot\n\ndef demo_process(sess, net):\n    \"\"\"Detect object classes in an image using pre-computed object proposals.\"\"\"\n    count = 0\n    bbs_array = np.array([], dtype=np.float32).reshape(0, 5)\n    im = []\n    tmp_g = []\n    scores= []\n    boxes = []\n    fig, ax = plt.subplots(figsize=(12, 12))\n    while True:\n      if frame_current != []:\n        print('flag')\n        bbs_array = np.array([], dtype=np.float32).reshape(0, 5)\n        # Load the demo image\n        #im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)\n        #im = cv2.imread(im_file)\n        #print (frame_current)\n\n        im = frame_current\n        tmp_g = im[:,:,1]\n        im[:,:,1] = im[:,:,2] \n        im[:,:,2] = tmp_g  \n        im = im*255\n\n        # Detect all object classes and regress object bounds\n        timer = Timer()\n        timer.tic()\n        scores, boxes = im_detect(sess, net, im)\n\n        #scores_max = scores[:,1:-1].max(axis=1)\n        #scores_max_idx = np.argmax(scores_max)\n        #scores = scores[scores_max_idx:scores_max_idx+1,:]\n        #boxes = boxes[scores_max_idx:scores_max_idx+1, :]\n\n        timer.toc()\n        print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))\n\n        #fig, ax = plt.subplots(figsize=(12, 12)) #uncommand this when writing files into disk\n        # Visualize detections for each class\n        CONF_THRESH = 0.5\n        NMS_THRESH = 0.3\n \n        top_score = 0;\n        top_cls = 0;\n        angle_compensated = 0.0\n        top_boxes = np.zeros(2)\n        for cls_ind, cls in enumerate(CLASSES[1:]):\n            cls_ind += 1 # because we skipped background\n            cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]\n            cls_scores = scores[:, cls_ind]\n            dets = np.hstack((cls_boxes,\n                              cls_scores[:, np.newaxis])).astype(np.float32)\n            keep = nms(dets, NMS_THRESH)\n            dets = dets[keep, :]\n\n            # stack all det > threshold\n            dets_th = np.delete(dets, np.where(dets[:, -1] < CONF_THRESH)[0], axis=0)\n            bbs_array = np.vstack((bbs_array, dets_th))\n\n            vis_detections(ax, im, cls, dets, thresh=CONF_THRESH) #uncommand if you want to visualize\n              \n            if ( max(cls_scores) > top_score):\n              #print (max(cls_scores))\n              #print(np.amax(cls_scores))\n              #print (np.argmax(cls_scores))\n              #print (cls_boxes[np.argmax(cls_scores),:])\n\n              tmp_top_boxes = cls_boxes[np.argmax(cls_scores),:]\n              top_boxes[0] = (tmp_top_boxes[0] + tmp_top_boxes[2]) / 2\n              top_boxes[1] = (tmp_top_boxes[1] + tmp_top_boxes[3]) / 2\n              top_score = max(cls_scores)\n              top_cls = cls_ind\n\n        x_table = 0\n        y_table = 0\n        if bbs_array.shape[0] != 0:\n          bbs_array_cnt = np.transpose(np.vstack(( bbs_array[:,0]/2+bbs_array[:,2]/2,  bbs_array[:,1]/2+bbs_array[:,3]/2)))\n          bbs_array_cnt_mean = bbs_array_cnt.mean(axis=0, keepdims=True)\n          bbs_array_dist = np.sum(np.square(bbs_array_cnt - bbs_array_cnt_mean), axis=1)\n          bbs_array_ins = np.argmin(bbs_array_dist)\n          circle2 = plt.Circle((bbs_array[bbs_array_ins,0]/2+bbs_array[bbs_array_ins,2]/2, bbs_array[bbs_array_ins,1]/2+bbs_array[bbs_array_ins,3]/2), 2, color='g')\n          ax.add_artist(circle2)\n          x_table, y_table, _ = coordinate_img2table(frame_current, bbs_array[bbs_array_ins,0]/2+bbs_array[bbs_array_ins,2]/2, bbs_array[bbs_array_ins,1]/2+bbs_array[bbs_array_ins,3]/2, top_cls)\n          angle_compensated = compute_imgRot(frame_current)\n\n        print(\"top class is: \" + str(top_cls))\n        print(\"top class angle is: \" + str(ANGLES[top_cls] + angle_compensated))\n        print(\"top class location is: \" + str(top_boxes[0]) + \" \" + str(top_boxes[1]))\n\n        #coordinate_img2table(frame_current, top_boxes[0], top_boxes[1], top_cls)\n\n        circle1 = plt.Circle((top_boxes[0], top_boxes[1]), 2, color='y')\n        ax.add_artist(circle1)\n\n        plt.axis('off')\n        plt.tight_layout()\n\n        #save result\n        count = count +1\n        savepath = './data/demo/results_all_cls/' + str(count) + '.png'\n        plt.savefig(savepath)\n\n    \n        file = open('/home/fujenchu/projects/robotArm/toy-opencv-mat-socket-server-master_pcl/bbs/rotation.txt', \"w\")\n        file.write(str(x_table) + '\\n')\n        file.write(str(y_table) + '\\n')\n        file.write(str(ANGLES[top_cls] + angle_compensated) + '\\n')\n        file.close()\n\n        #plt.draw()\n        #plt.show()\n        #cv2.imshow('deepGrasp_top_score', frame_current)\n        #choice = cv2.waitKey(20)\n        #if choice == 27:\n        #    break\n\ndef parse_args():\n    \"\"\"Parse input arguments.\"\"\"\n    parser = argparse.ArgumentParser(description='Tensorflow Faster R-CNN demo')\n    parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16 res101]',\n                        choices=NETS.keys(), default='res101')\n    parser.add_argument('--dataset', dest='dataset', help='Trained dataset [pascal_voc pascal_voc_0712]',\n                        choices=DATASETS.keys(), default='pascal_voc_0712')\n    args = parser.parse_args()\n\n    return args\n\n\n\nif __name__ == '__main__':\n    cfg.TEST.HAS_RPN = True  # Use RPN for proposals\n    args = parse_args()\n\n    # model path\n    demonet = args.demo_net\n    dataset = args.dataset\n    tfmodel = os.path.join('output', demonet, DATASETS[dataset][0], 'default',\n                              NETS[demonet][0])\n\n\n    if not os.path.isfile(tfmodel + '.meta'):\n        raise IOError(('{:s} not found.\\nDid you download the proper networks from '\n                       'our server and place them properly?').format(tfmodel + '.meta'))\n\n    # set config\n    tfconfig = tf.ConfigProto(allow_soft_placement=True)\n    tfconfig.gpu_options.allow_growth=True\n\n    tfconfig = tf.ConfigProto(device_count={'GPU': 0})\n\n    # init session\n    sess = tf.Session(config=tfconfig)\n    # load network\n    if demonet == 'vgg16':\n        net = vgg16(batch_size=1)\n    elif demonet == 'res101':\n        net = resnetv1(batch_size=1, num_layers=101)\n    elif demonet == 'res50':\n        net = resnetv1(batch_size=1, num_layers=50)\n    else:\n        raise NotImplementedError\n    net.create_architecture(sess, \"TEST\", 20,\n                          tag='default', anchor_scales=[8, 16, 32])\n    saver = tf.train.Saver()\n    saver.restore(sess, tfmodel)\n\n    print('Loaded network {:s}'.format(tfmodel))\n\n    demo_process = threading.Thread(target=demo_process, args=(sess,net))\n    demo_process.start()\n\n    # TCP/IP\n    HOST=''\n    #PORT=2330\n    PORT = 2325\n\n    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)\n    print('Socket created')\n\n    s.bind((HOST,PORT))\n    print('Socket bind complete')\n    s.listen(10)\n    print('Socket now listening')\n\n    client,addr=s.accept()\n\n    count = 100\n    frame = np.zeros((480, 640, 3))\n    while True:\n        start = time.time()\n        frame = np.zeros((480, 640, 3))\n        show_frame = np.zeros((480, 640, 3))\n        frame = np.reshape(frame, (480*640*3))\n        i=0\n        while i < 480*640*3:\n            data = client.recv(640*480*3 - i)\n            datalen = len(data)\n            #if len(data) != 1024:\n            datalen_str = str(len(data))\n            datalen_str = datalen_str + 'B'\n            data_up = struct.unpack(datalen_str, data)\n            data_up_np = np.asarray(data_up)\n            frame[i:i + len(data)] = data_up_np\n\n            i += len(data)\n\n        frame_depth = np.zeros((240, 320, 1))\n        show_frame_depth = np.zeros((240, 320, 1))\n        frame_depth = np.reshape(frame_depth, (320*240*1))\n        i=0\n        while i < 240*640*1:\n            data = client.recv(240*640*1 - i)\n            # datalen = len(data)\n            # print(datalen)\n            # #if len(data) != 1024:\n            #datalen_str = str(len(data)/2)\n            #datalen_str = datalen_str + 'H'\n            #data_up = struct.unpack(datalen_str, data)\n            #data_up_np = np.asarray(data_up)\n            data_up_np = np.fromstring(data, dtype='>H')\n            frame_depth[i:i + 76800] = data_up_np\n\n            i += len(data)\n\n        ########## save images for calibration #######################\n        if cv2.waitKey(10) == ord('s'):\n          count = count +1\n          savepath = './data/demo/live/' + str(count) + '.png'\n          cv2.imwrite(savepath, np.reshape(frame, (480, 640, 3)))\n\n          savepath = './data/demo/live/' + str(count) + '_d.png'\n          cv2.imwrite(savepath, np.reshape(frame_depth, (240, 320, 1))/ np.max(frame_depth))\n        ##############################################################\n\n        frame_current = np.reshape(frame, (480, 640, 3))/255.0\n        #frame_current_depth = 1-(np.reshape(frame_depth, (240, 320, 1)) / np.max(frame_depth))\n\n        duration = time.time()-start\n        #print(\"processed time main =\" + str(duration))\n\n        cv2.imshow('frame',frame_current)\n        #cv2.imshow('frame_depth', frame_current_depth)\n        cv2.waitKey(1)\n\n\n    #im_names = ['rgd_0076Cropped320.png','rgd_0095.png','pcd0122r_rgd_preprocessed_1.png','pcd0875r_rgd_preprocessed_1.png','resized_0875_2.png']\n    #im_names = ['pcd0875r_rgd_preprocessed_1.png','pic_0010.png']\n    #for im_name in im_names:\n    #    print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')\n    #    print('Demo for data/demo/{}'.format(im_name))\n    #    demo(sess, net, im_name)\n\n    #plt.show()\n"
  },
  {
    "path": "tools/demo_graspRGD_vis_mask.py",
    "content": "#!/usr/bin/env python\n\n# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen, based on code from Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"\nDemo script showing detections in sample images.\n\nSee README.md for installation instructions before running.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport _init_paths\nfrom model.config import cfg\nfrom model.test import im_detect\nfrom model.nms_wrapper import nms\n\nfrom utils.timer import Timer\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os, cv2\nimport argparse\n\nfrom nets.vgg16 import vgg16\nfrom nets.resnet_v1 import resnetv1\nimport scipy\nfrom shapely.geometry import Polygon\n\npi     = scipy.pi\ndot    = scipy.dot\nsin    = scipy.sin\ncos    = scipy.cos\nar     = scipy.array\n\nCLASSES = ('__background__',\n           'angle_01', 'angle_02', 'angle_03', 'angle_04', 'angle_05',\n           'angle_06', 'angle_07', 'angle_08', 'angle_09', 'angle_10',\n           'angle_11', 'angle_12', 'angle_13', 'angle_14', 'angle_15',\n           'angle_16', 'angle_17', 'angle_18', 'angle_19')\n\nNETS = {'vgg16': ('vgg16_faster_rcnn_iter_70000.ckpt',),'res101': ('res101_faster_rcnn_iter_110000.ckpt',),'res50': ('res50_faster_rcnn_iter_240000.ckpt',)}\nDATASETS= {'pascal_voc': ('voc_2007_trainval',),'pascal_voc_0712': ('voc_2007_trainval+voc_2012_trainval',),'grasp': ('train',)}\n\ndef Rotate2D(pts,cnt,ang=scipy.pi/4):\n    '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''\n    return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt\n\ndef vis_detections(ax, image_name, im, class_name, dets, thresh=0.5):\n    \"\"\"Draw detected bounding boxes.\"\"\"\n    inds = np.where(dets[:, -1] >= thresh)[0]\n    if len(inds) == 0:\n        return\n\n    im = im[:, :, (2, 1, 0)]\n    #fig, ax = plt.subplots(figsize=(12, 12))\n    ax.imshow(im, aspect='equal')\n\n    for i in inds:\n        bbox = dets[i, :4]\n        score = dets[i, -1]\n\n        #ax.add_patch(\n        #    plt.Rectangle((bbox[0], bbox[1]),\n        #                  bbox[2] - bbox[0],\n        #                  bbox[3] - bbox[1], fill=False,\n        #                  edgecolor='red', linewidth=3.5)\n        #    )\n\n        # plot rotated rectangles\n        pts = ar([[bbox[0],bbox[1]], [bbox[2], bbox[1]], [bbox[2], bbox[3]], [bbox[0], bbox[3]]])\n        cnt = ar([(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2])\n        angle = int(class_name[6:])\n        r_bbox = Rotate2D(pts, cnt, -pi/2-pi/20*(angle-1))\n        pred_label_polygon = Polygon([(r_bbox[0,0],r_bbox[0,1]), (r_bbox[1,0], r_bbox[1,1]), (r_bbox[2,0], r_bbox[2,1]), (r_bbox[3,0], r_bbox[3,1])])\n        pred_x, pred_y = pred_label_polygon.exterior.xy\n\n        plt.plot(pred_x[0:2],pred_y[0:2], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[1:3],pred_y[1:3], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[2:4],pred_y[2:4], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[3:5],pred_y[3:5], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n\n        #ax.text(bbox[0], bbox[1] - 2,\n        #        '{:s} {:.3f}'.format(class_name, score),\n        #        bbox=dict(facecolor='blue', alpha=0.5),\n        #        fontsize=14, color='white')\n\n    #ax.set_title(('{} detections with '\n    #              'p({} | box) >= {:.1f}').format(class_name, class_name,\n    #                                              thresh),\n    #              fontsize=14)\n    #plt.axis('off')\n    #plt.tight_layout()\n\n    #save result\n    #savepath = './data/demo/results/' + str(image_name) + str(class_name) + '.png'\n    #plt.savefig(savepath)\n\n    #plt.draw()\n\ndef demo(sess, net, image_name, mask_name):\n    \"\"\"Detect object classes in an image using pre-computed object proposals.\"\"\"\n\n    # Load the demo image\n    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)\n    mask_file = os.path.join(cfg.DATA_DIR, 'demo', mask_name)\n    im = cv2.imread(im_file)\n    mask = cv2.imread(mask_file,0)\n    #im = im*np.stack([mask,mask,mask], axis=2)\n    im = cv2.bitwise_and(im,im,mask = mask)\n\n    \n    # Detect all object classes and regress object bounds\n    timer = Timer()\n    timer.tic()\n    scores, boxes = im_detect(sess, net, im)\n\n    #scores_max = scores[:,1:-1].max(axis=1)\n    #scores_max_idx = np.argmax(scores_max)\n    #scores = scores[scores_max_idx:scores_max_idx+1,:]\n    #boxes = boxes[scores_max_idx:scores_max_idx+1, :]\n\n    #im = cv2.imread('/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps_ivalab/rgb_cropped320/rgb_0076Cropped320.png')\n    timer.toc()\n    print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))\n\n    fig, ax = plt.subplots(figsize=(12, 12))\n    # Visualize detections for each class\n    CONF_THRESH = 0.1\t\n    NMS_THRESH = 0.3\n    for cls_ind, cls in enumerate(CLASSES[1:]):\n        cls_ind += 1 # because we skipped background\n        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]\n        cls_scores = scores[:, cls_ind]\n        dets = np.hstack((cls_boxes,\n                          cls_scores[:, np.newaxis])).astype(np.float32)\n        keep = nms(dets, NMS_THRESH)\n        dets = dets[keep, :]\n        vis_detections(ax, image_name, im, cls, dets, thresh=CONF_THRESH)\n        #tmp = max(cls_scores)\n\n    plt.axis('off')\n    plt.tight_layout()\n\n    #cv2.imshow('deepGrasp_top_score', im)\n    #choice = cv2.waitKey(100)\n    \n    #save result\n    savepath = './data/demo/results_all_cls/' + str(image_name) + '.png'\n    plt.savefig(savepath)\n\n    plt.draw()\n\ndef parse_args():\n    \"\"\"Parse input arguments.\"\"\"\n    parser = argparse.ArgumentParser(description='Tensorflow Faster R-CNN demo')\n    parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16 res101]',\n                        choices=NETS.keys(), default='res101')\n    parser.add_argument('--dataset', dest='dataset', help='Trained dataset [pascal_voc pascal_voc_0712]',\n                        choices=DATASETS.keys(), default='pascal_voc_0712')\n    args = parser.parse_args()\n\n    return args\n\nif __name__ == '__main__':\n    cfg.TEST.HAS_RPN = True  # Use RPN for proposals\n    args = parse_args()\n\n    # model path\n    demonet = args.demo_net\n    dataset = args.dataset\n    tfmodel = os.path.join('output', demonet, DATASETS[dataset][0], 'default',\n                              NETS[demonet][0])\n\n\n    if not os.path.isfile(tfmodel + '.meta'):\n        raise IOError(('{:s} not found.\\nDid you download the proper networks from '\n                       'our server and place them properly?').format(tfmodel + '.meta'))\n\n    # set config\n    tfconfig = tf.ConfigProto(allow_soft_placement=True)\n    tfconfig.gpu_options.allow_growth=True\n\n    # init session\n    sess = tf.Session(config=tfconfig)\n    # load network\n    if demonet == 'vgg16':\n        net = vgg16(batch_size=1)\n    elif demonet == 'res101':\n        net = resnetv1(batch_size=1, num_layers=101)\n    elif demonet == 'res50':\n        net = resnetv1(batch_size=1, num_layers=50)\n    else:\n        raise NotImplementedError\n    net.create_architecture(sess, \"TEST\", 20,\n                          tag='default', anchor_scales=[8, 16, 32])\n    saver = tf.train.Saver()\n    saver.restore(sess, tfmodel)\n\n    print('Loaded network {:s}'.format(tfmodel))\n\n    #im_names = ['rgd_0076Cropped320.png','rgd_0095.png','pcd0122r_rgd_preprocessed_1.png','pcd0875r_rgd_preprocessed_1.png','resized_0875_2.png']\n    im_names = ['rgd_0000Cropped320.png']\n    mask_name = 'mask.jpg'\n    \n    for im_name in im_names:\n        print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')\n        print('Demo for data/demo/{}'.format(im_name))\n        demo(sess, net, im_name, mask_name)\n\n    plt.show()\n"
  },
  {
    "path": "tools/demo_graspRGD_vis_select.py",
    "content": "#!/usr/bin/env python\n\n# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen, based on code from Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"\nDemo script showing detections in sample images.\n\nSee README.md for installation instructions before running.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport _init_paths\nfrom model.config import cfg\nfrom model.test import im_detect\nfrom model.nms_wrapper import nms\n\nfrom utils.timer import Timer\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os, cv2\nimport argparse\n\nfrom nets.vgg16 import vgg16\nfrom nets.resnet_v1 import resnetv1\nimport scipy\nfrom shapely.geometry import Polygon\n\npi     = scipy.pi\ndot    = scipy.dot\nsin    = scipy.sin\ncos    = scipy.cos\nar     = scipy.array\n\nCLASSES = ('__background__',\n           'angle_01', 'angle_02', 'angle_03', 'angle_04', 'angle_05',\n           'angle_06', 'angle_07', 'angle_08', 'angle_09', 'angle_10',\n           'angle_11', 'angle_12', 'angle_13', 'angle_14', 'angle_15',\n           'angle_16', 'angle_17', 'angle_18', 'angle_19')\n\nglobal SELECTION_COUNT \nSELECTION_COUNT = 0\nNETS = {'vgg16': ('vgg16_faster_rcnn_iter_70000.ckpt',),'res101': ('res101_faster_rcnn_iter_110000.ckpt',),'res50': ('res50_faster_rcnn_iter_240000.ckpt',)}\nDATASETS= {'pascal_voc': ('voc_2007_trainval',),'pascal_voc_0712': ('voc_2007_trainval+voc_2012_trainval',),'grasp': ('train',)}\n\n\ndef Rotate2D(pts,cnt,ang=scipy.pi/4):\n    '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''\n    return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt\n\ndef vis_detections(ax, image_name, im, class_name, dets, thresh=0.5):\n    \"\"\"Draw detected bounding boxes.\"\"\"\n    inds = np.where(dets[:, -1] >= thresh)[0]\n    if len(inds) == 0:\n        return\n\n    im = im[:, :, (2, 1, 0)]\n    #fig, ax = plt.subplots(figsize=(12, 12))\n    ax.imshow(im, aspect='equal')\n    for i in inds:\n        bbox = dets[i, :4]\n        score = dets[i, -1]\n\n        #ax.add_patch(\n        #    plt.Rectangle((bbox[0], bbox[1]),\n        #                  bbox[2] - bbox[0],\n        #                  bbox[3] - bbox[1], fill=False,\n        #                  edgecolor='red', linewidth=3.5)\n        #    )\n\n        # plot rotated rectangles\n        pts = ar([[bbox[0],bbox[1]], [bbox[2], bbox[1]], [bbox[2], bbox[3]], [bbox[0], bbox[3]]])\n        cnt = ar([(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2])\n        angle = int(class_name[6:])\n        r_bbox = Rotate2D(pts, cnt, -pi/2-pi/20*(angle-1))\n        pred_label_polygon = Polygon([(r_bbox[0,0],r_bbox[0,1]), (r_bbox[1,0], r_bbox[1,1]), (r_bbox[2,0], r_bbox[2,1]), (r_bbox[3,0], r_bbox[3,1])])\n        pred_x, pred_y = pred_label_polygon.exterior.xy\n\n        plt.plot(pred_x[0:2],pred_y[0:2], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[1:3],pred_y[1:3], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[2:4],pred_y[2:4], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[3:5],pred_y[3:5], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n\n        #ax.text(bbox[0], bbox[1] - 2,\n        #        '{:s} {:.3f}'.format(class_name, score),\n        #        bbox=dict(facecolor='blue', alpha=0.5),\n        #        fontsize=14, color='white')\n        global SELECTION_COUNT\n        ax.text(bbox[0], bbox[1] - 2,\n                '{:s}  {:.3f}'.format(str(SELECTION_COUNT), score),\n                bbox=dict(facecolor='blue', alpha=0.5),\n                fontsize=14, color='white')\n        SELECTION_COUNT =  SELECTION_COUNT + 1\n\n    #ax.set_title(('{} detections with '\n    #              'p({} | box) >= {:.1f}').format(class_name, class_name,\n    #                                              thresh),\n    #              fontsize=14)\n    #plt.axis('off')\n    #plt.tight_layout()\n\n    #save result\n    #savepath = './data/demo/results/' + str(image_name) + str(class_name) + '.png'\n    #plt.savefig(savepath)\n\n    #plt.draw()\n\ndef demo(sess, net, image_name):\n    \"\"\"Detect object classes in an image using pre-computed object proposals.\"\"\"\n\n    # Load the demo image\n    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)\n    im = cv2.imread(im_file)\n    #print(im)\n\n    # Detect all object classes and regress object bounds\n    timer = Timer()\n    timer.tic()\n    scores, boxes = im_detect(sess, net, im)\n\n    #scores_max = scores[:,1:-1].max(axis=1)\n    #scores_max_idx = np.argmax(scores_max)\n    #scores = scores[scores_max_idx:scores_max_idx+1,:]\n    #boxes = boxes[scores_max_idx:scores_max_idx+1, :]\n\n    #im = cv2.imread('/home/fujenchu/projects/deepLearning/tensorflow-finetune-flickr-style-master/data/grasps_ivalab/rgb_cropped320/rgb_0076Cropped320.png')\n    timer.toc()\n    print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))\n\n    fig, ax = plt.subplots(figsize=(12, 12))\n    # Visualize detections for each class\n    CONF_THRESH = 0.1\t\n    NMS_THRESH = 0.3\n    global SELECTION_COUNT \n    SELECTION_COUNT = 0\n    for cls_ind, cls in enumerate(CLASSES[1:]):\n        cls_ind += 1 # because we skipped background\n        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]\n        cls_scores = scores[:, cls_ind]\n        dets = np.hstack((cls_boxes,\n                          cls_scores[:, np.newaxis])).astype(np.float32)\n        keep = nms(dets, NMS_THRESH)\n        dets = dets[keep, :]\n        vis_detections(ax, image_name, im, cls, dets, thresh=CONF_THRESH)\n        #tmp = max(cls_scores)\n\n    plt.axis('off')\n    plt.tight_layout()\n\n    #cv2.imshow('deepGrasp_top_score', im)\n    #choice = cv2.waitKey(100)\n    \n    #save result\n    savepath = './data/demo/results_all_cls/' + str(image_name) + '.png'\n    plt.savefig(savepath)\n\n    plt.draw()\n\ndef parse_args():\n    \"\"\"Parse input arguments.\"\"\"\n    parser = argparse.ArgumentParser(description='Tensorflow Faster R-CNN demo')\n    parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16 res101]',\n                        choices=NETS.keys(), default='res101')\n    parser.add_argument('--dataset', dest='dataset', help='Trained dataset [pascal_voc pascal_voc_0712]',\n                        choices=DATASETS.keys(), default='pascal_voc_0712')\n    args = parser.parse_args()\n\n    return args\n\nif __name__ == '__main__':\n    cfg.TEST.HAS_RPN = True  # Use RPN for proposals\n    args = parse_args()\n\n    # model path\n    demonet = args.demo_net\n    dataset = args.dataset\n    tfmodel = os.path.join('output', demonet, DATASETS[dataset][0], 'default',\n                              NETS[demonet][0])\n\n\n    if not os.path.isfile(tfmodel + '.meta'):\n        raise IOError(('{:s} not found.\\nDid you download the proper networks from '\n                       'our server and place them properly?').format(tfmodel + '.meta'))\n\n    # set config\n    tfconfig = tf.ConfigProto(allow_soft_placement=True)\n    tfconfig.gpu_options.allow_growth=True\n\n    # init session\n    sess = tf.Session(config=tfconfig)\n    # load network\n    if demonet == 'vgg16':\n        net = vgg16(batch_size=1)\n    elif demonet == 'res101':\n        net = resnetv1(batch_size=1, num_layers=101)\n    elif demonet == 'res50':\n        net = resnetv1(batch_size=1, num_layers=50)\n    else:\n        raise NotImplementedError\n    net.create_architecture(sess, \"TEST\", 20,\n                          tag='default', anchor_scales=[8, 16, 32])\n    saver = tf.train.Saver()\n    saver.restore(sess, tfmodel)\n\n    print('Loaded network {:s}'.format(tfmodel))\n\n    #im_names = ['rgd_0076Cropped320.png','rgd_0095.png','pcd0122r_rgd_preprocessed_1.png','pcd0875r_rgd_preprocessed_1.png','resized_0875_2.png']\n    im_names = ['pcd0100r_rgd_preprocessed_1.png','pcd0266r_rgd_preprocessed_1.png','pcd0882r_rgd_preprocessed_1.png','rgd_0000Cropped320.png']\n    for im_name in im_names:\n        print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')\n        print('Demo for data/demo/{}'.format(im_name))\n        demo(sess, net, im_name)\n\n    plt.show()\n"
  },
  {
    "path": "tools/eval_graspRGD.py~",
    "content": "#!/usr/bin/env python\n\n# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Xinlei Chen, based on code from Ross Girshick\n# --------------------------------------------------------\n\n\"\"\"\nDemo script showing detections in sample images.\n\nSee README.md for installation instructions before running.\n\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport _init_paths\nfrom model.config import cfg\nfrom model.test import im_detect\nfrom model.nms_wrapper import nms\n\nfrom utils.timer import Timer\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os, cv2\nimport argparse\n\nfrom nets.vgg16 import vgg16\nfrom nets.resnet_v1 import resnetv1\nimport scipy\nfrom shapely.geometry import Polygon\nfrom math import acos\n\n\npi     = scipy.pi\ndot    = scipy.dot\nsin    = scipy.sin\ncos    = scipy.cos\nar     = scipy.array\n\ntotal_found = 0\ntotal_count = 0\ntotal_predicted = 0\ntotal_predicted_and_matched = 0\n\nCLASSES = ('__background__',\n           'angle_01', 'angle_02', 'angle_03', 'angle_04', 'angle_05',\n           'angle_06', 'angle_07', 'angle_08', 'angle_09', 'angle_10',\n           'angle_11', 'angle_12', 'angle_13', 'angle_14', 'angle_15',\n           'angle_16', 'angle_17', 'angle_18', 'angle_19')\n\nNETS = {'vgg16': ('vgg16_faster_rcnn_iter_70000.ckpt',),'res101': ('res101_faster_rcnn_iter_110000.ckpt',),'res50': ('res50_faster_rcnn_iter_159000.ckpt',)}\nDATASETS= {'pascal_voc': ('voc_2007_trainval',),'pascal_voc_0712': ('voc_2007_trainval+voc_2012_trainval',),'grasp': ('train',)}\n\ndef dotproduct(a,b):\n\treturn sum([a[i]*b[i] for i in range(len(a))])\n \n#Calculates the size of a vector\ndef veclength(a):\n\treturn sum([a[i]**2 for i in range(len(a))])**.5\n \n#Calculates the angle between two vector\ndef ange(a,b):\n    dp=dotproduct(a,b)\n    la=veclength(a)\n    lb=veclength(b)\n    costheta=dp/(la*lb)\n    degree = acos(costheta)/pi*180\n    if degree > 90:\n        degree = 180 - degree\n    return degree\n\n\ndef Rotate2D(pts,cnt,ang=scipy.pi/4):\n    '''pts = {} Rotates points(nx2) about center cnt(2) by angle ang(1) in radian'''\n    return dot(pts-cnt,ar([[cos(ang),sin(ang)],[-sin(ang),cos(ang)]]))+cnt\n\ndef vis_detections(gt_file, ax, image_name, im, class_name, dets, thresh=0.1):\n    \"\"\"Draw detected bounding boxes.\"\"\"\n    overlap_found = False\n    inds = np.where(dets[:, -1] >= thresh)[0]\n    if len(inds) == 0:\n        missed = True\n        return overlap_found, 0, 0\n\n    gt_x_ar = []\n    gt_y_ar = []\n    with open(gt_file) as f:\n       for line in f:\n         data = line.split()\n         gt_x_ar.append(float(data[0]))\n         gt_y_ar.append(float(data[1]))\n\n    gt_label_polygon_list = []\n    for i in range(0,len(gt_x_ar),4):\n         gt_label_polygon = Polygon([(gt_x_ar[i], gt_y_ar[i]), (gt_x_ar[i+1], gt_y_ar[i+1]), (gt_x_ar[i+2], gt_y_ar[i+2]), (gt_x_ar[i+3], gt_y_ar[i+3])])\n         gt_label_polygon_list.append(gt_label_polygon)\n         #gt_x, gt_y = gt_label_polygon.exterior.xy\n         #plt.plot(gt_x,gt_y, color='r', alpha = 0.7, linewidth=5, solid_capstyle='round', zorder=2)\n\n    im = im[:, :, (2, 1, 0)]\n    #fig, ax = plt.subplots(figsize=(12, 12))\n    ax.imshow(im, aspect='equal')\n    predicted_and_matched_num = 0\n    for i in inds:\n        inds_found = False\n        bbox = dets[i, :4]\n        score = dets[i, -1]\n\n        pts = ar([[bbox[0],bbox[1]], [bbox[2], bbox[1]], [bbox[2], bbox[3]], [bbox[0], bbox[3]]])\n        cnt = ar([(bbox[0] + bbox[2])/2, (bbox[1] + bbox[3])/2])\n        angle = int(class_name[6:])\n        r_bbox = Rotate2D(pts, cnt, -pi/2-pi/20*(angle-1))\n        pred_label_polygon = Polygon([(r_bbox[0,0],r_bbox[0,1]), (r_bbox[1,0], r_bbox[1,1]), (r_bbox[2,0], r_bbox[2,1]), (r_bbox[3,0], r_bbox[3,1])])\n        pred_x, pred_y = pred_label_polygon.exterior.xy\n        plt.plot(pred_x[0:2],pred_y[0:2], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[1:3],pred_y[1:3], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[2:4],pred_y[2:4], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n        plt.plot(pred_x[3:5],pred_y[3:5], color='r', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n\n        for gt_label_polygon_ind in gt_label_polygon_list:\n           intersection = pred_label_polygon.intersection(gt_label_polygon_ind)\n           union = pred_label_polygon.union(gt_label_polygon_ind)\n           if intersection.area/union.area > 0.25:\n              gt_x, gt_y = gt_label_polygon_ind.exterior.xy\n              degree = ange(np.array([pred_x[0] - pred_x[1], pred_y[0] - pred_y[1]]), np.array([gt_x[0] - gt_x[1], gt_y[0] - gt_y[1]]))\n              if degree < 10:\n                 overlap_found = True\n                 inds_found = True\n                 #plt.plot(gt_x,gt_y, color='b', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n                 #plt.plot(gt_x[0:2],gt_y[0:2], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n                 #plt.plot(gt_x[1:3],gt_y[1:3], color='g', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n                 #plt.plot(gt_x[2:4],gt_y[2:4], color='k', alpha = 0.7, linewidth=1, solid_capstyle='round', zorder=2)\n                 #plt.plot(gt_x[3:5],gt_y[3:5], color='g', alpha = 0.7, linewidth=3, solid_capstyle='round', zorder=2)\n        if inds_found is True: predicted_and_matched_num = predicted_and_matched_num+1\n        #ax.text(bbox[0], bbox[1] - 2,\n        #        '{:s} {:.3f}'.format(class_name, score),\n        #        bbox=dict(facecolor='blue', alpha=0.5),\n        #        fontsize=14, color='white')\n\n    ax.set_title(('{} detections with '\n                  'p({} | box) >= {:.1f}').format(class_name, class_name,\n                                                  thresh),\n                  fontsize=14)\n    return overlap_found, len(inds), predicted_and_matched_num\n\ndef demo(sess, net, image_name):\n    \"\"\"Detect object classes in an image using pre-computed object proposals.\"\"\"\n    global total_found \n    global total_count \n    global total_missed\n\n    global total_predicted\n    global total_predicted_and_matched\n   \n    # Load the demo image\n    im_file = image_name\n    gt_file_base = image_name[:-36]\n    im = cv2.imread(im_file)\n    image_name = os.path.basename(im_file)\n    image_name = os.path.splitext(image_name)[0]\n    gt_file = gt_file_base + '/' + image_name[:7] + 'cposCropped320.txt'\n    print ('GT file: ' + gt_file) \n\n    # Detect all object classes and regress object bounds\n    timer = Timer()\n    timer.tic()\n    scores, boxes = im_detect(sess, net, im)\n\n    # need rgb for visualization?\n    im_rgb_file = gt_file_base + '/' + image_name[:7] + 'rCropped320.png'\n    im = cv2.imread(im_rgb_file)\n\n    # only consider one bbox with highest score?\n    scores_max = scores[:,1:-1].max(axis=1)\n    scores_max_idx = np.argmax(scores_max)\n    scores = scores[scores_max_idx:scores_max_idx+1,:]\n    boxes = boxes[scores_max_idx:scores_max_idx+1, :]\n    scores[0,0]=-1 # make sure background wont be the max\n    scores_max_idx = np.argmax(scores)\n\n\n    timer.toc()\n    print (('Detection took {:.3f}s for '\n           '{:d} object proposals').format(timer.total_time, boxes.shape[0]))\n\n    fig, ax = plt.subplots(figsize=(12, 12))\n    # Visualize detections for each class\n    CONF_THRESH = 0.0000001\n    NMS_THRESH = 0.7\n\n    overall_overlap_found = False\n    overall_missed = True\n    for cls_ind, cls in enumerate(CLASSES[1:]):\n        cls_ind += 1 # because we skipped background\n        if scores_max_idx != cls_ind:\n            continue\n\n        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]\n        cls_scores = scores[:, cls_ind]\n        dets = np.hstack((cls_boxes,\n                          cls_scores[:, np.newaxis])).astype(np.float32)\n        keep = nms(dets, NMS_THRESH)\n        dets = dets[keep, :]\n        tmp_overlap_found, tmp_predictedNum, tmp_predictedMatchedNum = vis_detections(gt_file, ax, image_name, im, cls, dets, thresh=CONF_THRESH)\n        #print tmp_overlap_found\n        if tmp_overlap_found == True: overall_overlap_found = True\n        total_predicted = total_predicted + tmp_predictedNum\n        total_predicted_and_matched = total_predicted_and_matched + tmp_predictedMatchedNum\n\n    total_count = total_count + 1\n    if overall_overlap_found == True: total_found = total_found + 1\n    print ('recall = ' + str(total_found) + '/' + str(total_count) + ' (the rate to find one grasp in one image)')\n    print ('precision = ' + str(total_predicted_and_matched) + '/' + str(total_predicted) + ' (the rate per predicted grasp passed Jaccard)')\n    plt.axis('off')\n    plt.tight_layout() \n\n    #save result\n    savepath = './data/demo/results_pure/results_pure_pred/' + str(image_name) + '.png'    \n    plt.savefig(savepath)    \n\n    #plt.draw()\n\ndef parse_args():\n    \"\"\"Parse input arguments.\"\"\"\n    parser = argparse.ArgumentParser(description='Tensorflow Faster R-CNN demo')\n    parser.add_argument('--net', dest='demo_net', help='Network to use [vgg16 res101]',\n                        choices=NETS.keys(), default='res101')\n    parser.add_argument('--dataset', dest='dataset', help='Trained dataset [pascal_voc pascal_voc_0712]',\n                        choices=DATASETS.keys(), default='pascal_voc_0712')\n    args = parser.parse_args()\n\n    return args\n\nif __name__ == '__main__':\n    cfg.TEST.HAS_RPN = True  # Use RPN for proposals\n    args = parse_args()\n\n    # model path\n    demonet = args.demo_net\n    dataset = args.dataset\n    tfmodel = os.path.join('output', demonet, DATASETS[dataset][0], 'default',\n                              NETS[demonet][0])\n\n\n    if not os.path.isfile(tfmodel + '.meta'):\n        raise IOError(('{:s} not found.\\nDid you download the proper networks from '\n                       'our server and place them properly?').format(tfmodel + '.meta'))\n\n    # set config\n    tfconfig = tf.ConfigProto(allow_soft_placement=True)\n    tfconfig.gpu_options.allow_growth=True\n\n    # init session\n    sess = tf.Session(config=tfconfig)\n    # load network\n    if demonet == 'vgg16':\n        net = vgg16(batch_size=1)\n    elif demonet == 'res101':\n        net = resnetv1(batch_size=1, num_layers=101)\n    elif demonet == 'res50':\n        net = resnetv1(batch_size=1, num_layers=50)\n    else:\n        raise NotImplementedError\n    net.create_architecture(sess, \"TEST\", 20,\n                          tag='default', anchor_scales=[8, 16, 32])\n    saver = tf.train.Saver()\n    saver.restore(sess, tfmodel)\n\n    print('Loaded network {:s}'.format(tfmodel))\n\n    print ('reading images..')\n    im_names = []\n    #with open('/home/fujenchu/projects/deepLearning/faster-rcnn-resnet/data/testCrop320rgd.txt') as f:\n    with open('/media/fujenchu/home3/fasterrcnn_grasp/rgd_multibbs_5_5_5_object_tf/data/ImageSets/testfull.txt') as f:\n       for line in f:\n         im_path = \"\"\n         for char in line:\n           if char == '\\n': break\n           im_path = im_path + str(char)\n         im_names.append(im_path)\n\n    print ('total images: ' + str(len(im_names)))\n\n    for im_name in im_names:\n        print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')\n        print('evaluating {}'.format(im_name))\n        demo(sess, net, im_name)\n\n    #plt.show()\n"
  },
  {
    "path": "tools/mask_gen.py",
    "content": "import numpy as np    \nmask = np.zeros((227,227))\nmask[70:120,90:150]=1\nimport scipy.miscscipy.misc.imsave('mask.jpg', mask)\n"
  },
  {
    "path": "tools/trainval_net.py",
    "content": "# --------------------------------------------------------\n# Tensorflow Faster R-CNN\n# Licensed under The MIT License [see LICENSE for details]\n# Written by Zheqi He, Xinlei Chen, based on code from Ross Girshick\n# --------------------------------------------------------\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport _init_paths\nfrom model.train_val import get_training_roidb, train_net\nfrom model.config import cfg, cfg_from_file, cfg_from_list, get_output_dir, get_output_tb_dir\nfrom datasets.factory import get_imdb\nimport datasets.imdb\nimport argparse\nimport pprint\nimport numpy as np\nimport sys\n\nimport tensorflow as tf\nfrom nets.vgg16 import vgg16\nfrom nets.resnet_v1 import resnetv1\n\ndef parse_args():\n  \"\"\"\n  Parse input arguments\n  \"\"\"\n  parser = argparse.ArgumentParser(description='Train a Fast R-CNN network')\n  parser.add_argument('--cfg', dest='cfg_file',\n                      help='optional config file',\n                      default=None, type=str)\n  parser.add_argument('--weight', dest='weight',\n                      help='initialize with pretrained model weights',\n                      type=str)\n  parser.add_argument('--imdb', dest='imdb_name',\n                      help='dataset to train on',\n                      default='voc_2007_trainval', type=str)\n  parser.add_argument('--imdbval', dest='imdbval_name',\n                      help='dataset to validate on',\n                      default='voc_2007_test', type=str)\n  parser.add_argument('--iters', dest='max_iters',\n                      help='number of iterations to train',\n                      default=70000, type=int)\n  parser.add_argument('--tag', dest='tag',\n                      help='tag of the model',\n                      default=None, type=str)\n  parser.add_argument('--net', dest='net',\n                      help='vgg16, res50, res101, res152',\n                      default='res50', type=str)\n  parser.add_argument('--set', dest='set_cfgs',\n                      help='set config keys', default=None,\n                      nargs=argparse.REMAINDER)\n\n  if len(sys.argv) == 1:\n    parser.print_help()\n    sys.exit(1)\n\n  args = parser.parse_args()\n  return args\n\n\ndef combined_roidb(imdb_names):\n  \"\"\"\n  Combine multiple roidbs\n  \"\"\"\n\n  def get_roidb(imdb_name):\n    imdb = get_imdb(imdb_name)\n    print('Loaded dataset `{:s}` for training'.format(imdb.name))\n    imdb.set_proposal_method(cfg.TRAIN.PROPOSAL_METHOD)\n    print('Set proposal method: {:s}'.format(cfg.TRAIN.PROPOSAL_METHOD))\n    roidb = get_training_roidb(imdb)\n    return roidb\n\n  roidbs = [get_roidb(s) for s in imdb_names.split('+')]\n  roidb = roidbs[0]\n  if len(roidbs) > 1:\n    for r in roidbs[1:]:\n      roidb.extend(r)\n    tmp = get_imdb(imdb_names.split('+')[1])\n    imdb = datasets.imdb.imdb(imdb_names, tmp.classes)\n  else:\n    imdb = get_imdb(imdb_names)\n  return imdb, roidb\n\n\nif __name__ == '__main__':\n  args = parse_args()\n\n  print('Called with args:')\n  print(args)\n\n  if args.cfg_file is not None:\n    cfg_from_file(args.cfg_file)\n  if args.set_cfgs is not None:\n    cfg_from_list(args.set_cfgs)\n\n  print('Using config:')\n  pprint.pprint(cfg)\n\n  np.random.seed(cfg.RNG_SEED)\n\n  # train set\n  imdb, roidb = combined_roidb(args.imdb_name)\n  print('{:d} roidb entries'.format(len(roidb)))\n\n  # output directory where the models are saved\n  output_dir = get_output_dir(imdb, args.tag)\n  print('Output will be saved to `{:s}`'.format(output_dir))\n\n  # tensorboard directory where the summaries are saved during training\n  tb_dir = get_output_tb_dir(imdb, args.tag)\n  print('TensorFlow summaries will be saved to `{:s}`'.format(tb_dir))\n\n  # also add the validation set, but with no flipping images\n  orgflip = cfg.TRAIN.USE_FLIPPED\n  cfg.TRAIN.USE_FLIPPED = False\n  _, valroidb = combined_roidb(args.imdbval_name)\n  print('{:d} validation roidb entries'.format(len(valroidb)))\n  cfg.TRAIN.USE_FLIPPED = orgflip\n\n  # load network\n  if args.net == 'vgg16':\n    net = vgg16(batch_size=cfg.TRAIN.IMS_PER_BATCH)\n  elif args.net == 'res50':\n    net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=50)\n  elif args.net == 'res101':\n    net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=101)\n  elif args.net == 'res152':\n    net = resnetv1(batch_size=cfg.TRAIN.IMS_PER_BATCH, num_layers=152)\n  else:\n    raise NotImplementedError\n    \n  train_net(net, imdb, roidb, valroidb, output_dir, tb_dir,\n            pretrained_model=args.weight,\n            max_iters=args.max_iters)\n"
  }
]