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: use lazy import for torch #1338

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/imagery/i.eodag/i.eodag.py
Original file line number Diff line number Diff line change
Expand Up @@ -1325,7 +1325,7 @@ def main():
from eodag.utils.exceptions import MisconfiguredError
import eodag
except ImportError:
gs.fatal(_("Cannot import eodag. Please intall the library first."))
gs.fatal(_("Cannot import eodag. Please install the library first."))

# To disable eodag logs, set DEBUG to 0
# with " g.gisenv 'set=DEBUG=0' "
Expand Down
8 changes: 4 additions & 4 deletions src/imagery/i.sam2/i.sam2.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ <h2>EXAMPLES</h2>
</pre>
</div>

<img src="./i_sam2_trees.jpg" height="600" alt="i.sam2 example" />
<img src="i_sam2_trees.jpg" height="600" alt="i.sam2 example">

<h2>NOTES</h2>
The first time use will be longer as the model needs to be downloaded. Subsequent runs will be faster.
Additionally, Cuda is required for GPU acceleration. If you do not have a GPU, you can use the CPU by setting the environment variable `CUDA_VISIBLE_DEVICES` to `-1`.
Additionally, CUDA is required for GPU acceleration. If you do not have a GPU, you can use the CPU by setting the environment variable `CUDA_VISIBLE_DEVICES` to `-1`.

<h2>REFERENCES</h2>
<ul>
<li>Wu, Q., & Osco, L. (2023). samgeo: A Python package for segmenting geospatial data with the Segment Anything Model (SAM). Journal of Open Source Software, 8(89), 5663. <a href="https://doi.org/10.21105/joss.05663">https://doi.org/10.21105/joss.05663</a></li>
<li>Wu, Q., &amp; Osco, L. (2023). samgeo: A Python package for segmenting geospatial data with the Segment Anything Model (SAM). Journal of Open Source Software, 8(89), 5663. <a href="https://doi.org/10.21105/joss.05663">https://doi.org/10.21105/joss.05663</a></li>
<li>Osco, L. P., Wu, Q., de Lemos, E. L., Gonçalves, W. N., Ramos, A. P. M., Li, J., & Junior, J. M. (2023). The Segment Anything Model (SAM) for remote sensing applications: From zero to one shot. International Journal of Applied Earth Observation and Geoinformation, 124, 103540. <a href="https://doi.org/10.1016/j.jag.2023.103540">https://doi.org/10.1016/j.jag.2023.103540</a></li>
</ul>

Expand All @@ -47,4 +47,4 @@ <h2>SEE ALSO</h2>
</em>

<h2>AUTHOR</h2>
Corey T. White (NCSU GeoForAll Lab & OpenPlains Inc.)
Corey T. White (NCSU GeoForAll Lab &amp; OpenPlains Inc.)
2 changes: 1 addition & 1 deletion src/imagery/i.sam2/i.sam2.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Segment orthoimagery using SamGeo2:

```

![image-alt](./i_sam2_trees.jpg)
![i.sam2: trees detected in an aerial image with samgeo](i_sam2_trees.jpg)

## NOTES

Expand Down
6 changes: 5 additions & 1 deletion src/imagery/i.sam2/i.sam2.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
import os
import sys
import grass.script as gs
import torch
import numpy as np
from PIL import Image
from grass.script import array as garray
Expand Down Expand Up @@ -229,6 +228,11 @@ def main():
text_threshold = float(options.get("text_threshold"))
box_threshold = float(options.get("box_threshold"))

try:
import torch
except ImportError:
gs.fatal(_("Cannot import torch. Please install python3-torch first."))

input_image_np = read_raster_group(group)
rgb_array = normalize_rgb_array(np.stack(input_image_np, axis=-1))
np_image = Image.fromarray(rgb_array[:, :, :3])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,6 @@ def main():

gs.find_program("i.eodag", "--help")
except ImportError:
gs.fatal(_("Addon i.eodag not found. Please intall it with g.extension."))
gs.fatal(_("Addon i.eodag not found. Please install it with g.extension."))

sys.exit(main())
2 changes: 1 addition & 1 deletion src/raster/r.green/r.green.install/r.green.install.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h2>NOTES</h2>
<h3>Installation on Microsoft Windows</h3>
You should install <a href="https://grass.osgeo.org/download/windows/">GRASS GIS</a> with administrator
rights; either the stand-alone installer or via OSGeo4W package, in a directory with full control of
permissions, to be able to intall the <em>r.green</em> modules.
permissions, to be able to install the <em>r.green</em> modules.

<center>
<img src="r_green_install_permissions.png" alt="Microsoft Windows Permissions"><br>
Expand Down
2 changes: 1 addition & 1 deletion src/raster/r.green/r.green.install/r.green.install.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ post-installation troubles that sometimes may occur.
You should install [GRASS
GIS](https://grass.osgeo.org/download/windows/) with administrator
rights; either the stand-alone installer or via OSGeo4W package, in a
directory with full control of permissions, to be able to intall the
directory with full control of permissions, to be able to install the
*r.green* modules.

![image-alt](r_green_install_permissions.png)
Expand Down