|
452 | 452 | }
|
453 | 453 | ],
|
454 | 454 | "source": [
|
455 |
| - "NUM_ROWS_TO_ENRICH = 20\n", |
| 455 | + "NUM_ROWS_TO_ENRICH = 20 # For demonstration, only run on 20 rows. \n", |
456 | 456 | "\n",
|
457 | 457 | "df = fd.enrich(task='zero-shot-classification',\n",
|
458 | 458 | " model='recognize-anything-model', \n",
|
|
769 | 769 | "id": "000a9c1d-d499-4200-86d4-16e14eceb679",
|
770 | 770 | "metadata": {},
|
771 | 771 | "source": [
|
772 |
| - "## Zero-Shot Detection with Grounding DINO" |
| 772 | + "## Zero-Shot Detection with Grounding DINO\n", |
| 773 | + "Apart from classification models, fastdup also supports zero-shot detection models like [Grounding DINO](https://github.com/IDEA-Research/GroundingDINO) (and more to come).\n", |
| 774 | + "\n", |
| 775 | + "Grounding DINO is a powerful open-set zero-shot detection model. It accepts image-text pair as inputs and outputs a bounding box." |
773 | 776 | ]
|
774 | 777 | },
|
775 | 778 | {
|
|
812 | 815 | "id": "94a74bdb-78d0-4b95-8f2d-3fd2d2f18e20",
|
813 | 816 | "metadata": {},
|
814 | 817 | "source": [
|
815 |
| - "You'll have to import the module and provide it with an image and text prompt. Text prompts must be separated with `\" . \"`." |
| 818 | + "You'll have to import the module and provide it with an image-text input pair. \n", |
| 819 | + "\n", |
| 820 | + "Note: Text prompts must be separated with `\" . \"`.\n", |
| 821 | + "\n", |
| 822 | + "By default fastdup uses the smaller variant of Grounding DINO (Swin-T backbone)." |
816 | 823 | ]
|
817 | 824 | },
|
818 | 825 | {
|
|
902 | 909 | "results"
|
903 | 910 | ]
|
904 | 911 | },
|
| 912 | + { |
| 913 | + "cell_type": "markdown", |
| 914 | + "id": "e639eb37-8716-4563-80c0-cc269147f440", |
| 915 | + "metadata": {}, |
| 916 | + "source": [ |
| 917 | + "Let's plot the image and results using the `annotate_image` convenience function." |
| 918 | + ] |
| 919 | + }, |
905 | 920 | {
|
906 | 921 | "cell_type": "code",
|
907 | 922 | "execution_count": 17,
|
|
931 | 946 | "id": "55d02a9a-32c1-430d-8ca8-ca10ac4b8712",
|
932 | 947 | "metadata": {},
|
933 | 948 | "source": [
|
934 |
| - "Load another SwinB variant of Grounding DINO. Weights and config can be downloaded from the [official Grounding DINO repo](https://github.com/IDEA-Research/GroundingDINO)." |
| 949 | + "You can optionally load another variant of Grounding DINO (Swin-B backbone) from the [official Grounding DINO repo](https://github.com/IDEA-Research/GroundingDINO).\n", |
| 950 | + "\n", |
| 951 | + "Download the [weights](https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/groundingdino_swinb_cogcoor.pth) and [config](https://github.com/IDEA-Research/GroundingDINO/blob/main/groundingdino/config/GroundingDINO_SwinB_cfg.py) into your local directory and pass them as arguments to the `GroundingDINO` contructor. " |
935 | 952 | ]
|
936 | 953 | },
|
937 | 954 | {
|
|
982 | 999 | " text_threshold=0.25)"
|
983 | 1000 | ]
|
984 | 1001 | },
|
| 1002 | + { |
| 1003 | + "cell_type": "markdown", |
| 1004 | + "id": "3c8dc27f-e5fc-4285-bc44-11334bfac2a2", |
| 1005 | + "metadata": {}, |
| 1006 | + "source": [ |
| 1007 | + "Fine tune the detection output by varying the `box_threshold` and `text_threshold` values.\n", |
| 1008 | + "\n", |
| 1009 | + "The outputs are stored in a Python `dict`. " |
| 1010 | + ] |
| 1011 | + }, |
985 | 1012 | {
|
986 | 1013 | "cell_type": "code",
|
987 | 1014 | "execution_count": 19,
|
|
1068 | 1095 | "id": "9fb0bdfd-cc1a-420c-bb16-c4e13b75bcda",
|
1069 | 1096 | "metadata": {},
|
1070 | 1097 | "source": [
|
1071 |
| - "Outputs the columns grounding_dino bboxes, scores and labels." |
| 1098 | + "To run the enrichment on a DataFrame, use the `.enrich` method and specify `model=grounding-dino`. By default fastdup loads the smaller variant (Swin-T) backbone for enrichment. \n", |
| 1099 | + "\n", |
| 1100 | + "Also specify the DataFrame to run the enrichment on and the name of the column as the input to the Grounding DINO model. In this example, we take the text prompt from the `ram_tags` column which we have computed earlier." |
1072 | 1101 | ]
|
1073 | 1102 | },
|
1074 | 1103 | {
|
|
1134 | 1163 | "df = fd.enrich(task='zero-shot-detection', model='grounding-dino', input_df=df, input_col='ram_tags', device=\"cuda\")"
|
1135 | 1164 | ]
|
1136 | 1165 | },
|
| 1166 | + { |
| 1167 | + "cell_type": "markdown", |
| 1168 | + "id": "59c8e8d0-1c00-403b-84d9-226458b9268a", |
| 1169 | + "metadata": {}, |
| 1170 | + "source": [ |
| 1171 | + "Once, done you'll notice that 3 new columns are appened into the DataFrame namely - `grounding_dino_bboxes`, `grounding_dino_scores`, and `grounding_dino_labels`. " |
| 1172 | + ] |
| 1173 | + }, |
1137 | 1174 | {
|
1138 | 1175 | "cell_type": "code",
|
1139 | 1176 | "execution_count": 22,
|
|
1327 | 1364 | "df"
|
1328 | 1365 | ]
|
1329 | 1366 | },
|
| 1367 | + { |
| 1368 | + "cell_type": "markdown", |
| 1369 | + "id": "abada383-b3a2-42ee-8f9d-df6c27e46fb5", |
| 1370 | + "metadata": {}, |
| 1371 | + "source": [ |
| 1372 | + "Now let's plot the results of the enrichment using the `plot_annotations` function." |
| 1373 | + ] |
| 1374 | + }, |
1330 | 1375 | {
|
1331 | 1376 | "cell_type": "code",
|
1332 | 1377 | "execution_count": 23,
|
|
1356 | 1401 | "id": "8c114568-abeb-4159-bbdb-22ee1d1f4b3e",
|
1357 | 1402 | "metadata": {},
|
1358 | 1403 | "source": [
|
1359 |
| - "### Custom Text Prompt" |
| 1404 | + "### Searching for Specific Objects with Custom Text Prompt\n", |
| 1405 | + "\n", |
| 1406 | + "Let's suppose you'd like to search for specific objects in your dataset, you can create a column in the DataFrame specifying the objects of interest and run the `.enrich` method." |
1360 | 1407 | ]
|
1361 | 1408 | },
|
1362 | 1409 | {
|
|
1577 | 1624 | "df_custom_prompt"
|
1578 | 1625 | ]
|
1579 | 1626 | },
|
| 1627 | + { |
| 1628 | + "cell_type": "markdown", |
| 1629 | + "id": "6ebb9a5a-4e65-4c60-83ef-daae93209e5a", |
| 1630 | + "metadata": {}, |
| 1631 | + "source": [ |
| 1632 | + "Note that we specify `input_col='custom_prompt'` so that the model uses the text from the 'custom_prompt' column." |
| 1633 | + ] |
| 1634 | + }, |
1580 | 1635 | {
|
1581 | 1636 | "cell_type": "code",
|
1582 | 1637 | "execution_count": 26,
|
|
1825 | 1880 | "id": "7a979b19-eaef-422b-944b-0285115e24d6",
|
1826 | 1881 | "metadata": {},
|
1827 | 1882 | "source": [
|
1828 |
| - "Remove rows with empty detection." |
| 1883 | + "Not all images contain \"face\", \"eye\" and \"hair\", let's remove the columns with no detections and plot the colums with detections." |
1829 | 1884 | ]
|
1830 | 1885 | },
|
1831 | 1886 | {
|
|
0 commit comments