Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i.sam2: SamGeo2 model #1244

Open
wants to merge 7 commits into
base: grass8
Choose a base branch
from
Open

i.sam2: SamGeo2 model #1244

wants to merge 7 commits into from

Conversation

cwhite911
Copy link
Contributor

A new module i.sam2 for segmenting orthoimagery using text prompts with the SAMGeo model (Segment Anything Model Geospatial).

i.sam2 group=rgb_255 output=tree_mask text_prompt="trees"

@ecodiv
Copy link
Contributor

ecodiv commented Nov 16, 2024

Cool, need to try this out.

@cwhite911
Copy link
Contributor Author

I've submitted PR: opengeos/segment-geospatial#370 to fix an upstream issue in the segment-geospatial library.

@neteler
Copy link
Member

neteler commented Nov 18, 2024

Would you mind to add "trees.png" in a higher resolution (perhaps as JPG), at time it is only 640 x 480.

The code for a clickable image figure is found at
https://github.com/OSGeo/grass/blob/main/doc/development/style_guide.md#documentation
-> code it at the end of this section

Please also change the name to i_sam2_trees.jpg. Thanks!

@veroandreo
Copy link
Contributor

So cool, @cwhite911! Would you mind adding the requirements and how to install it within the manual page as in here, for example. It would be more visible for users and clear, too.

# %option
# % key: text_prompt
# % type: string
# % description: Optional text prompt to guide segmentation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text prompt is not optional (required is set to 'yes')

@ecodiv
Copy link
Contributor

ecodiv commented Dec 3, 2024

I tried to run this on Windows 10, GRAS GIS 8.4, installed via OSGeo4W. I am getting the following errors:

ERROR: Could not install packages due to an OSError:
[WinError 5] Access is denied:
'C:\\OSGeo4W\\apps\\Python312\\Lib\\site-
packages\\cv2\\cv2.pyd'
Consider using the `--user` option or check the permissions.
Installing GroundingDINO...
Collecting groundingdino-py
Using cached groundingdino_py-0.4.0-py2.py3-none-any.whl
Requirement already satisfied: torch in c:\osgeo4w\apps\python312\lib\site-packages (from groundingdino-py) (2.5.1)
Requirement already satisfied: torchvision in c:\osgeo4w\apps\python312\lib\site-packages (from groundingdino-py) (0.20.1)
Collecting transformers (from groundingdino-py)
Using cached transformers-4.46.3-py3-none-any.whl.metadata (44 kB)
Requirement already satisfied: addict in c:\osgeo4w\apps\python312\lib\site-packages (from groundingdino-py) (2.4.0)
Requirement already satisfied: yapf in c:\osgeo4w\apps\python312\lib\site-packages (from groundingdino-py) (0.43.0)
Requirement already satisfied: timm in c:\osgeo4w\apps\python312\lib\site-packages (from groundingdino-py) (1.0.11)
Requirement already satisfied: numpy in c:\osgeo4w\apps\python312\lib\site-packages (from groundingdino-py) (1.26.4)
Collecting opencv-python (from groundingdino-py)
Using cached opencv_python-4.10.0.84-cp37-abi3-win_amd64.whl.metadata (20 kB)
Collecting supervision==0.6.0 (from groundingdino-py)
Using cached supervision-0.6.0-py3-none-any.whl.metadata (6.2 kB)
Requirement already satisfied: pycocotools in c:\osgeo4w\apps\python312\lib\site-packages (from groundingdino-py) (2.0.8)
Requirement already satisfied: matplotlib in c:\osgeo4w\apps\python312\lib\site-packages (from supervision==0.6.0->groundingdino-py) (3.8.4)
Requirement already satisfied: pyyaml in c:\osgeo4w\apps\python312\lib\site-packages (from timm->groundingdino-py) (6.0.1)
Requirement already satisfied: huggingface_hub in c:\osgeo4w\apps\python312\lib\site-packages (from timm->groundingdino-py) (0.26.3)
Requirement already satisfied: safetensors in c:\osgeo4w\apps\python312\lib\site-packages (from timm->groundingdino-py) (0.4.5)
Requirement already satisfied: filelock in c:\osgeo4w\apps\python312\lib\site-packages (from torch->groundingdino-py) (3.16.1)
Requirement already satisfied: typing-extensions>=4.8.0 in c:\osgeo4w\apps\python312\lib\site-packages (from torch->groundingdino-py) (4.12.2)
Requirement already satisfied: networkx in c:\osgeo4w\apps\python312\lib\site-packages (from torch->groundingdino-py) (3.4.2)
Requirement already satisfied: jinja2 in c:\osgeo4w\apps\python312\lib\site-packages (from torch->groundingdino-py) (3.1.3)
Requirement already satisfied: fsspec in c:\osgeo4w\apps\python312\lib\site-packages (from torch->groundingdino-py) (2024.10.0)
Requirement already satisfied: setuptools in c:\osgeo4w\apps\python312\lib\site-packages (from torch->groundingdino-py) (69.2.0)
Requirement already satisfied: sympy==1.13.1 in c:\osgeo4w\apps\python312\lib\site-packages (from torch->groundingdino-py) (1.13.1)
Requirement already satisfied: mpmath<1.4,>=1.1.0 in c:\osgeo4w\apps\python312\lib\site-packages (from sympy==1.13.1->torch->groundingdino-py) (1.3.0)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in c:\osgeo4w\apps\python312\lib\site-packages (from torchvision->groundingdino-py) (11.0.0)
Requirement already satisfied: packaging>=20.0 in c:\osgeo4w\apps\python312\lib\site-packages (from transformers->groundingdino-py) (24.0)
Requirement already satisfied: regex!=2019.12.17 in c:\osgeo4w\apps\python312\lib\site-packages (from transformers->groundingdino-py) (2024.11.6)
Requirement already satisfied: requests in c:\osgeo4w\apps\python312\lib\site-packages (from transformers->groundingdino-py) (2.31.0)
Collecting tokenizers<0.21,>=0.20 (from transformers->groundingdino-py)
Using cached tokenizers-0.20.3-cp312-none-win_amd64.whl.metadata (6.9 kB)
Requirement already satisfied: tqdm>=4.27 in c:\osgeo4w\apps\python312\lib\site-packages (from transformers->groundingdino-py) (4.67.1)
Requirement already satisfied: platformdirs>=3.5.1 in c:\osgeo4w\apps\python312\lib\site-packages (from yapf->groundingdino-py) (4.3.6)
Requirement already satisfied: contourpy>=1.0.1 in c:\osgeo4w\apps\python312\lib\site-packages (from matplotlib->supervision==0.6.0->groundingdino-py) (1.2.1)
Requirement already satisfied: cycler>=0.10 in c:\osgeo4w\apps\python312\lib\site-packages (from matplotlib->supervision==0.6.0->groundingdino-py) (0.12.1)
Requirement already satisfied: fonttools>=4.22.0 in c:\osgeo4w\apps\python312\lib\site-packages (from matplotlib->supervision==0.6.0->groundingdino-py) (4.51.0)
Requirement already satisfied: kiwisolver>=1.3.1 in c:\osgeo4w\apps\python312\lib\site-packages (from matplotlib->supervision==0.6.0->groundingdino-py) (1.4.5)
Requirement already satisfied: pyparsing>=2.3.1 in c:\osgeo4w\apps\python312\lib\site-packages (from matplotlib->supervision==0.6.0->groundingdino-py) (3.1.2)
Requirement already satisfied: python-dateutil>=2.7 in c:\osgeo4w\apps\python312\lib\site-packages (from matplotlib->supervision==0.6.0->groundingdino-py) (2.9.0.post0)
Requirement already satisfied: colorama in c:\osgeo4w\apps\python312\lib\site-packages (from tqdm>=4.27->transformers->groundingdino-py) (0.4.6)
Requirement already satisfied: MarkupSafe>=2.0 in c:\osgeo4w\apps\python312\lib\site-packages (from jinja2->torch->groundingdino-py) (2.1.5)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\osgeo4w\apps\python312\lib\site-packages (from requests->transformers->groundingdino-py) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in c:\osgeo4w\apps\python312\lib\site-packages (from requests->transformers->groundingdino-py) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\osgeo4w\apps\python312\lib\site-packages (from requests->transformers->groundingdino-py) (2.2.1)
Requirement already satisfied: certifi>=2017.4.17 in c:\osgeo4w\apps\python312\lib\site-packages (from requests->transformers->groundingdino-py) (2024.2.2)
Requirement already satisfied: six>=1.5 in c:\osgeo4w\apps\python312\lib\site-packages (from python-dateutil>=2.7->matplotlib->supervision==0.6.0->groundingdino-py) (1.16.0)
Using cached supervision-0.6.0-py3-none-any.whl (31 kB)
Using cached opencv_python-4.10.0.84-cp37-abi3-win_amd64.whl (38.8 MB)
Using cached transformers-4.46.3-py3-none-any.whl (10.0 MB)
Using cached tokenizers-0.20.3-cp312-none-win_amd64.whl (2.4 MB)
Installing collected packages: opencv-python, tokenizers, supervision, transformers, groundingdino-py
Please restart the kernel and run the notebook again.
Group <landsat_test> references the following raster maps:
WARNING: Illegal filename <landsat
>. Character <
> not allowed.
ERROR: Raster map <lsat5_1987_10@landsat
> not found
Traceback (most recent call last):
  File "C:\Users\brp\AppData\Roaming\GRASS8\addons/scripts\i
.sam2.py", line 187, in <module>
    sys.exit(main())
             ^^^^^^
  File "C:\Users\brp\AppData\Roaming\GRASS8\addons/scripts\i
.sam2.py", line 106, in main
    input_image_np = list([garray.array(raster,
dtype=np.uint8) for raster in rasters])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\OSGeo4W\apps\grass\grass84\etc\python\grass\scrip
t\array.py", line 161, in __new__
    gcore.run_command(
  File "C:\OSGeo4W\apps\grass\grass84\etc\python\grass\scrip
t\core.py", line 487, in run_command
    return handle_errors(returncode, result=None, args=args,
kwargs=kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^
  File "C:\OSGeo4W\apps\grass\grass84\etc\python\grass\scrip
t\core.py", line 366, in handle_errors
    raise CalledModuleError(module=module, code=code,
returncode=returncode)
grass.exceptions.CalledModuleError: Module run `r.out.bin
--o --q -i input=lsat5_1987_10@landsat
 output=D:\GRASStest\nc_spm_08_grass7\landsat\.tmp/unknown\1
5608.1 bytes=1` ended with an error.
The subprocess ended with a non-zero return code: 1. See
errors above the traceback or in the error output.

temp_output_path = tmp_dir
guide_input_path = None

rasters = gs.read_command("i.group", group=group, flags="lg").strip().split("\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In windows at least, the elements in the resulting list 'rasters' have a trailing '\r', for example: ['lsat5_1987_10@landsat\r', 'lsat5_1987_20@landsat\r', 'lsat5_1987_30@landsat']

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cwhite911 The issue mentioned above can be resolved, I think, by replacing line 105 by:

rasters = gs.read_command("i.group", group=group, flags="lg")
rasters = [raster.strip() for raster in rasters.splitlines()]

Copy link
Contributor

@ecodiv ecodiv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cwhite911 Please see feedback in code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants