|
280 | 280 | {
|
281 | 281 | "cell_type": "markdown",
|
282 | 282 | "id": "fdaad7cd-5835-4ae5-9fa1-f8ba3d4e10c4",
|
283 |
| - "metadata": {}, |
| 283 | + "metadata": { |
| 284 | + "jp-MarkdownHeadingCollapsed": true, |
| 285 | + "tags": [] |
| 286 | + }, |
284 | 287 | "source": [
|
285 | 288 | "### Inference on a single image\n",
|
286 | 289 | "We can use these models in fastdup in a few lines of code.\n",
|
|
427 | 430 | {
|
428 | 431 | "cell_type": "markdown",
|
429 | 432 | "id": "aaddc03a-d330-4bb3-ac4e-a3e8730282ad",
|
430 |
| - "metadata": {}, |
| 433 | + "metadata": { |
| 434 | + "jp-MarkdownHeadingCollapsed": true, |
| 435 | + "tags": [] |
| 436 | + }, |
431 | 437 | "source": [
|
432 | 438 | "### Inference on a DataFrame of images\n",
|
433 | 439 | "\n",
|
|
778 | 784 | {
|
779 | 785 | "cell_type": "markdown",
|
780 | 786 | "id": "41feec30-7b9d-4b94-94c4-dca8cef14466",
|
781 |
| - "metadata": {}, |
| 787 | + "metadata": { |
| 788 | + "jp-MarkdownHeadingCollapsed": true, |
| 789 | + "tags": [] |
| 790 | + }, |
782 | 791 | "source": [
|
783 | 792 | "### Inference on single image\n",
|
784 | 793 | "fastdup provides an easy way to load the Grounding DINO model and run an inference.\n",
|
|
1093 | 1102 | {
|
1094 | 1103 | "cell_type": "markdown",
|
1095 | 1104 | "id": "9fb0bdfd-cc1a-420c-bb16-c4e13b75bcda",
|
1096 |
| - "metadata": {}, |
| 1105 | + "metadata": { |
| 1106 | + "jp-MarkdownHeadingCollapsed": true, |
| 1107 | + "tags": [] |
| 1108 | + }, |
1097 | 1109 | "source": [
|
| 1110 | + "### Inference on a DataFrame of images\n", |
| 1111 | + "\n", |
1098 | 1112 | "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 | 1113 | "\n",
|
1100 | 1114 | "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."
|
|
1399 | 1413 | {
|
1400 | 1414 | "cell_type": "markdown",
|
1401 | 1415 | "id": "8c114568-abeb-4159-bbdb-22ee1d1f4b3e",
|
1402 |
| - "metadata": {}, |
| 1416 | + "metadata": { |
| 1417 | + "jp-MarkdownHeadingCollapsed": true, |
| 1418 | + "tags": [] |
| 1419 | + }, |
1403 | 1420 | "source": [
|
1404 | 1421 | "### Searching for Specific Objects with Custom Text Prompt\n",
|
1405 | 1422 | "\n",
|
|
1921 | 1938 | {
|
1922 | 1939 | "cell_type": "markdown",
|
1923 | 1940 | "id": "79a1d14c-4075-424f-abda-b640c3630bd9",
|
1924 |
| - "metadata": {}, |
| 1941 | + "metadata": { |
| 1942 | + "tags": [] |
| 1943 | + }, |
1925 | 1944 | "source": [
|
1926 |
| - "## Zero-Shot Segmentation with SAM" |
| 1945 | + "## Zero-Shot Segmentation with SAM\n", |
| 1946 | + "\n", |
| 1947 | + "In addition to the zer-shot classification and detection modes, fastdup also supports zero-shot segmentation using the [Segment Anything Model (SAM)](https://github.com/facebookresearch/segment-anything) from MetaAI.\n", |
| 1948 | + "\n", |
| 1949 | + "SAM produces high quality object masks from input prompts such as points or boxes, and it can be used to generate masks for all objects in an image." |
1927 | 1950 | ]
|
1928 | 1951 | },
|
1929 | 1952 | {
|
1930 | 1953 | "cell_type": "markdown",
|
1931 | 1954 | "id": "dc58c743-d8e3-45b8-ae32-7cfc6474afd1",
|
1932 |
| - "metadata": {}, |
| 1955 | + "metadata": { |
| 1956 | + "jp-MarkdownHeadingCollapsed": true, |
| 1957 | + "tags": [] |
| 1958 | + }, |
1933 | 1959 | "source": [
|
1934 |
| - "For single image and single bounding box." |
| 1960 | + "### Inference on a single image\n", |
| 1961 | + "\n", |
| 1962 | + "To run an inference using the SAM model, import the `SegmentAnythingModel` class and provide an image-bounding box pair as the input." |
1935 | 1963 | ]
|
1936 | 1964 | },
|
1937 | 1965 | {
|
|
1958 | 1986 | "result = model.run_inference(image_path=\"coco_minitrain_25k/images/val2017/000000449996.jpg\", bboxes=torch.tensor((1.47, 1.45, 638.46, 241.37)))"
|
1959 | 1987 | ]
|
1960 | 1988 | },
|
| 1989 | + { |
| 1990 | + "cell_type": "markdown", |
| 1991 | + "id": "9072cc27-98ab-486e-b559-a02f2be0a64c", |
| 1992 | + "metadata": { |
| 1993 | + "tags": [] |
| 1994 | + }, |
| 1995 | + "source": [ |
| 1996 | + "The result is a list of binary masks. " |
| 1997 | + ] |
| 1998 | + }, |
1961 | 1999 | {
|
1962 | 2000 | "cell_type": "markdown",
|
1963 | 2001 | "id": "249b9531-77ca-47a9-a94a-304c85a192dc",
|
1964 | 2002 | "metadata": {},
|
1965 | 2003 | "source": [
|
1966 |
| - "Load other variants of SAM. Checkpoint can be downloaded from the [official SAM repo](https://github.com/facebookresearch/segment-anything)." |
| 2004 | + "You can also load other variants of SAM from the [official SAM repo](https://github.com/facebookresearch/segment-anything) or even your own custom model.\n", |
| 2005 | + "\n", |
| 2006 | + "To do so, download the `sam_vit_b` [weights](https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth) and the `sam_vit_l` [weights](https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth) from into your local folder and load them into the constructor as follows." |
1967 | 2007 | ]
|
1968 | 2008 | },
|
1969 | 2009 | {
|
|
2011 | 2051 | {
|
2012 | 2052 | "cell_type": "markdown",
|
2013 | 2053 | "id": "d137da5d-ac81-4af1-9b5a-1b4d7b79464d",
|
2014 |
| - "metadata": {}, |
| 2054 | + "metadata": { |
| 2055 | + "tags": [] |
| 2056 | + }, |
2015 | 2057 | "source": [
|
2016 |
| - "For multiple images and multiple bounding boxes in a DataFrame." |
| 2058 | + "### Inference on a DataFrame of images\n", |
| 2059 | + "\n", |
| 2060 | + "Similar to all previous examples, you can use the `enrich` method to add masks to your DataFrame of images.\n", |
| 2061 | + "\n", |
| 2062 | + "In the following code snippet, we load the SAM model and specify `input_col='grounding_dino_bboxes'` to allow SAM to use the bounding boxes as inputs." |
2017 | 2063 | ]
|
2018 | 2064 | },
|
2019 | 2065 | {
|
|
2036 | 2082 | "df = fd.enrich(task='zero-shot-segmentation', model='segment-anything', input_df=df, input_col='grounding_dino_bboxes')"
|
2037 | 2083 | ]
|
2038 | 2084 | },
|
| 2085 | + { |
| 2086 | + "cell_type": "markdown", |
| 2087 | + "id": "16cbf4be-29c0-4854-a8e0-025f43d55ec0", |
| 2088 | + "metadata": {}, |
| 2089 | + "source": [ |
| 2090 | + "Next, drop rows in the DataFrame without masks for the purpose of visualization." |
| 2091 | + ] |
| 2092 | + }, |
2039 | 2093 | {
|
2040 | 2094 | "cell_type": "code",
|
2041 | 2095 | "execution_count": 33,
|
|
2048 | 2102 | "df.dropna(subset=['sam_masks'], inplace=True)"
|
2049 | 2103 | ]
|
2050 | 2104 | },
|
| 2105 | + { |
| 2106 | + "cell_type": "markdown", |
| 2107 | + "id": "130ba407-ae28-417d-993c-10efd008b431", |
| 2108 | + "metadata": {}, |
| 2109 | + "source": [ |
| 2110 | + "Plot the images with bounding boxes and masks." |
| 2111 | + ] |
| 2112 | + }, |
2051 | 2113 | {
|
2052 | 2114 | "cell_type": "code",
|
2053 | 2115 | "execution_count": 34,
|
|
2076 | 2138 | "id": "0f4b137a-25ea-44ab-b44e-ed734428de86",
|
2077 | 2139 | "metadata": {},
|
2078 | 2140 | "source": [
|
2079 |
| - "## Convert Annotations to COCO Format" |
| 2141 | + "## Convert Annotations to COCO Format\n", |
| 2142 | + "\n", |
| 2143 | + "Once the enrichment is complete, you can also conveniently export the DataFrame into the COCO .json annotation format. For now, only the bounding boxes and labels are exported. Masks will be added in a future release." |
2080 | 2144 | ]
|
2081 | 2145 | },
|
2082 | 2146 | {
|
|
2098 | 2162 | "metadata": {},
|
2099 | 2163 | "source": [
|
2100 | 2164 | "## Run fastdup\n",
|
2101 |
| - "\n", |
2102 |
| - "Now let's load the embeddings into fastdup and run an analysis to surface dataset issues." |
| 2165 | + "You can optionally analyze the exported annotations in fastdup to evalute the quality of the annotations." |
2103 | 2166 | ]
|
2104 | 2167 | },
|
2105 | 2168 | {
|
|
4713 | 4776 | "metadata": {},
|
4714 | 4777 | "source": [
|
4715 | 4778 | "## Wrap Up\n",
|
4716 |
| - "In this tutorial, we showed how you can compute embeddings on your dataset using TIMM and run fastdup on top of it to surface dataset issues.\n", |
| 4779 | + "In this tutorial, we showed how you can run zero-shot models to enrich your dataset.\n", |
4717 | 4780 | "\n",
|
4718 | 4781 | "Questions about this tutorial? Reach out to us on our [Slack channel](https://visuallayer.slack.com/)!\n",
|
4719 | 4782 | "\n",
|
|
0 commit comments