Spaces:
Running on T4
Running on T4
Add cp11 wheel for faster colab launch
#9
by imgprcsng - opened
Files changed (3)
- app.py+8-21
- notebooks/demo.ipynb+15-56
- requirements.txt+1-4
175 unmodified lines17617717817918018118218318418518618718818919019119219319419519619719819920020120242 unmodified lines245246247248249250251252253172 unmodified lines426427428429430175 unmodified linesreturn inds_to_filterdef get_xy_from_boxes(image, boxes):"""Get box centers and return in image coordinates"""(w, h) = image.sizex = w * boxes[:, 0]y = h * boxes[:, 1]return x, ydef generate_heatmap(image, boxes):# Plot results.(w, h) = image.sizedet_map = np.zeros((h, w))x, y = get_xy_from_boxes(image, boxes)# Box centers are floating point, convert to int and clip them at edge of boxx = np.clip(np.around(x).astype(int), 0, w - 1)y = np.clip(np.around(y).astype(int), 0, h - 1)det_map[y, x] = 1det_map = ndimage.gaussian_filter(det_map, sigma=(w // 200, w // 200), order=0)42 unmodified linesreturn input_image, input_image_exemplar, exemplardef get_boxes_from_prediction(model_output, text, keywords = ""):logits = model_output["pred_logits"].sigmoid()[0][:, :]boxes = model_output["pred_boxes"][0]box_mask = logits.max(dim=-1).values > CONF_THRESHboxes = boxes[box_mask, :].cpu().numpy()logits = logits[box_mask, :].cpu().numpy()return boxes, logits172 unmodified linesclear_btn_main.add([input_image_main, input_text_main, exemplar_image_main, detected_instances_main, pred_count_main])demo.queue().launch(allowed_paths=['back-icon.jpg', 'paste-icon.jpg', 'upload-icon.jpg', 'button-legend.jpg'])#demo.queue().launch(share=True)175 unmodified lines17617717817918018118218318418518642 unmodified lines229230231232233234235236237238239240241172 unmodified lines414415416417175 unmodified linesreturn inds_to_filterdef generate_heatmap(image, boxes):# Plot results.(w, h) = image.sizedet_map = np.zeros((h, w))det_map[(h * boxes[:, 1]).astype(int), (w * boxes[:, 0]).astype(int)] = 1det_map = ndimage.gaussian_filter(det_map, sigma=(w // 200, w // 200), order=0)42 unmodified linesreturn input_image, input_image_exemplar, exemplardef get_boxes_from_prediction(model_output, text, keywords = ""):ind_to_filter = get_ind_to_filter(text, model_output["token"][0].word_ids, keywords)logits = model_output["pred_logits"].sigmoid()[0][:, ind_to_filter]boxes = model_output["pred_boxes"][0]if len(keywords.strip()) > 0:box_mask = (logits > CONF_THRESH).sum(dim=-1) == len(ind_to_filter)else:box_mask = logits.max(dim=-1).values > CONF_THRESHboxes = boxes[box_mask, :].cpu().numpy()logits = logits[box_mask, :].cpu().numpy()return boxes, logits172 unmodified linesclear_btn_main.add([input_image_main, input_text_main, exemplar_image_main, detected_instances_main, pred_count_main])demo.queue().launch(allowed_paths=['back-icon.jpg', 'paste-icon.jpg', 'upload-icon.jpg', 'button-legend.jpg'])
5 unmodified lines678910111276 unmodified lines899091929394959697989910010118 unmodified lines1201211221231241251261271281293 unmodified lines13313413513613713813914014114214314414514614714814915015115215315415550 unmodified lines2062072082092102112122 unmodified lines21521621721821922022167 unmodified lines2892902912922932942951 unmodified line2972982993003013023033043053063073083093103113123135 unmodified lines3193203213223233243253263273283293303313323333343353363373383393403413426 unmodified lines3493503513523533543553565 unmodified lines"id": "yxig5CdZuHb9"},"source": ["# CountGD - Multimodal open-world object counting\n","\n"]},76 unmodified lines"source": ["### Install Dependencies\n","\n","The environment will be setup with the code, models and required dependencies.\n","\n","*Note for Colab users*\n","\n","To reduce the waiting time, you can use the pre-built wheel file available [here](https://drive.google.com/file/d/1Vl_6DAWfnVU7HFX5y_5TqqbkyTcjONbm/view?usp=sharing) - Visit the link and add it as a shortcut to your \"My Drive\" folder or edit the path accordingly below. (Line 28)\n","\n","Alternatively, if you are unable to use google drive, you can download the file to your machine & upload it to the colab runtime when you connect to it and update the path below to install it from there. (Line 28)"]},{18 unmodified lines" git clone \"https://huggingface.co/spaces/nikigoli/countgd\" /content/countgd\n"," fi\n"," cd /content/countgd\n","\n"," # If you are testing out WIP items, uncomment the following and change the pr ref\n"," # git fetch origin refs/pr/10:refs/remotes/origin/pr/10\n"," # git checkout pr/10 && git pull\n","else\n"," # TODO check if cwd is the correct git repo\n"," # If users use vscode, then we set the default start directory to root of the repo\n",3 unmodified lines"# TODO check for gcc-11 or above\n","\n","# Install pip packages\n","pip install --upgrade pip setuptools wheel ninja\n","pip install -r requirements.txt\n","\n","cd models/GroundingDINO/ops\n","if [ \"${RUNNING_IN_COLAB}\" == \"True\" ]; then\n"," export CUDA_HOME=/usr/local/cuda/\n"," if ! pip install \"/content/drive/MyDrive/MultiScaleDeformableAttention-1.0-cp311-cp311-linux_x86_64.whl\"\n"," then\n"," echo \"failed to install wheel, trying to build from source\";\n"," python3 setup.py build\n"," python3 setup.py install\n"," fi\n","else\n"," # We try to build the module as we dont know what environment we are running on\n"," python3 setup.py build\n"," python3 setup.py install\n","fi\n","python3 test.py"]},50 unmodified lines" get_device,\n"," get_args_parser,\n"," generate_heatmap,\n"," get_xy_from_boxes,\n"," predict,\n",")\n","args = get_args_parser().parse_args([])\n",2 unmodified lines"model = model.to(device)\n","\n","run = lambda image, text: predict(model, transform, image, text, None, device)\n","get_output = lambda image, boxes: (len(boxes), get_xy_from_boxes(image, boxes), generate_heatmap(image, boxes))\n"]},{67 unmodified lines},{"cell_type": "code","execution_count": null,"metadata": {"id": "rFXRk-_uuHb_"},1 unmodified line"source": ["from tqdm import tqdm\n","import os\n","import json\n","def convert_xy_to_json(xy: tuple):\n"," x, y = xy\n"," pts = []\n"," for _x, _y in zip(x.tolist(), y.tolist()):\n"," _x, _y = round(_x, 3), round(_y, 3)\n"," pts.append([_x, _y])\n","\n"," # List of [x, y] points\n"," return pts\n","\n","def process_zipfile(input_zipfile: Path, text: str):\n"," if not input_zipfile.exists() or not input_zipfile.is_file() or not os.access(input_zipfile, os.R_OK):\n"," logger.error(f'Cannot open / read zipfile: {input_zipfile}. Please check if it exists')\n",5 unmodified lines"\n"," output_zipfile = input_zipfile.parent / f'{input_zipfile.stem}_countgd.zip'\n"," output_csvfile = input_zipfile.parent / f'{input_zipfile.stem}.csv'\n"," output_xyjson = input_zipfile.parent / f'{input_zipfile.stem}_xy.json'\n","\n"," xy_map = {}\n","\n"," logger.info(f'Writing outputs to {output_zipfile.name} and {output_csvfile.name} in {input_zipfile.parent} folder')\n"," with zipfile_writer(output_zipfile) as add_to_zip, csvfile_writer(output_csvfile) as write_row:\n"," for filename, im in tqdm(images_from_zipfile(input_zipfile)):\n"," try:\n"," boxes, _ = run(im, text)\n"," count, xy, heatmap = get_output(im, boxes)\n"," logger.info(f'Count: {count} - {filename}')\n"," xy_map[filename] = convert_xy_to_json(xy)\n"," write_row({'filename': filename, 'count': count})\n"," add_to_zip(heatmap, filename)\n"," except Exception:\n"," logger.error(f'failed to process {filename}')\n","\n"," output_xyjson.write_text(json.dumps(xy_map))"]},{6 unmodified lines"\n","Use the form on colab to set the parameters, providing the zipfile with input images and a promt text representing the object you want to count.\n","\n","Use the fileupload button to add the zip file to the `countgd` directory or change the path below accordingly.\n","\n","If you are not running on colab, change the values in the next cell\n","\n","Make sure to run the cell once you change the value."5 unmodified lines678910111276 unmodified lines8990919293949518 unmodified lines1141151161171181191201213 unmodified lines12512612712812913013113213313413513613713850 unmodified lines1891901911921931942 unmodified lines19719819920020120220367 unmodified lines2712722732742752762771 unmodified line2792802812822832845 unmodified lines2902912922932942952962972982993003013023036 unmodified lines3103113123133143155 unmodified lines"id": "yxig5CdZuHb9"},"source": ["# CountGD - Multimodela open-world object counting\n","\n"]},76 unmodified lines"source": ["### Install Dependencies\n","\n","The environment will be setup with the code, models and required dependencies."]},{18 unmodified lines" git clone \"https://huggingface.co/spaces/nikigoli/countgd\" /content/countgd\n"," fi\n"," cd /content/countgd\n"," git fetch origin refs/pr/5:refs/remotes/origin/pr/5\n"," git checkout pr/5\n","else\n"," # TODO check if cwd is the correct git repo\n"," # If users use vscode, then we set the default start directory to root of the repo\n",3 unmodified lines"# TODO check for gcc-11 or above\n","\n","# Install pip packages\n","pip install --upgrade pip setuptools wheel\n","pip install -r requirements.txt\n","\n","# Compile modules\n","export CUDA_HOME=/usr/local/cuda/\n","cd models/GroundingDINO/ops\n","python3 setup.py build\n","pip install .\n","python3 test.py"]},50 unmodified lines" get_device,\n"," get_args_parser,\n"," generate_heatmap,\n"," predict,\n",")\n","args = get_args_parser().parse_args([])\n",2 unmodified lines"model = model.to(device)\n","\n","run = lambda image, text: predict(model, transform, image, text, None, device)\n","get_output = lambda image, boxes: (len(boxes), generate_heatmap(image, boxes))\n"]},{67 unmodified lines},{"cell_type": "code","execution_count": 15,"metadata": {"id": "rFXRk-_uuHb_"},1 unmodified line"source": ["from tqdm import tqdm\n","import os\n","def process_zipfile(input_zipfile: Path, text: str):\n"," if not input_zipfile.exists() or not input_zipfile.is_file() or not os.access(input_zipfile, os.R_OK):\n"," logger.error(f'Cannot open / read zipfile: {input_zipfile}. Please check if it exists')\n",5 unmodified lines"\n"," output_zipfile = input_zipfile.parent / f'{input_zipfile.stem}_countgd.zip'\n"," output_csvfile = input_zipfile.parent / f'{input_zipfile.stem}.csv'\n","\n"," logger.info(f'Writing outputs to {output_zipfile.name} and {output_csvfile.name} in {input_zipfile.parent} folder')\n"," with zipfile_writer(output_zipfile) as add_to_zip, csvfile_writer(output_csvfile) as write_row:\n"," for filename, im in tqdm(images_from_zipfile(input_zipfile)):\n"," boxes, _ = run(im, text)\n"," count, heatmap = get_output(im, boxes)\n"," write_row({'filename': filename, 'count': count})\n"," add_to_zip(heatmap, filename)"]},{6 unmodified lines"\n","Use the form on colab to set the parameters, providing the zipfile with input images and a promt text representing the object you want to count.\n","\n","If you are not running on colab, change the values in the next cell\n","\n","Make sure to run the cell once you change the value."
10 unmodified lines1112131415161718192021222310 unmodified linesushlexgradio>=4.0.0,<5gradio_image_prompter-0.1.0-py3-none-any.whlstarlette<1.0.0spacesfiletypetqdm--extra-index-url https://download.pytorch.org/whl/cu121torch<2.6torchvisiontransformers<5huggingface_hub<1pydantic==2.10.610 unmodified lines1112131415161718192010 unmodified linesushlexgradio>=4.0.0,<5gradio_image_prompter-0.1.0-py3-none-any.whlspacesfiletypetqdm--extra-index-url https://download.pytorch.org/whl/cu121torch<2.6torchvisiontransformers
app.py+8-21
notebooks/demo.ipynb+15-56
requirements.txt+1-4