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

ImportError: DLL load failed while importing _version #841

Closed
wqfcv opened this issue Nov 28, 2024 · 12 comments
Closed

ImportError: DLL load failed while importing _version #841

wqfcv opened this issue Nov 28, 2024 · 12 comments
Labels
installation wontfix This will not be worked on

Comments

@wqfcv
Copy link

wqfcv commented Nov 28, 2024

Install using standard conda approach:

conda create -n deepforest python=3 pytorch torchvision cudatoolkit=10.2 -c pytorch
conda activate deepforest
conda install deepforest -c conda-forge

Then do some basic stuff. I ran this, not sure exact failure point:

from deepforest import main
from deepforest import get_data
from deepforest import visualize
import matplotlib.pyplot as plt
from deepforest.visualize import plot_predictions

model = main.deepforest()
model.load_model(model_name="weecology/deepforest-tree", revision="main")
raster_path = get_data("result.tif")
predicted_raster = model.predict_tile(raster_path, patch_size=300, patch_overlap=0.25,return_plot=True)
PS C:\Users\18774> & D:/anaconda/envs/deepforest/python.exe d:/treeDetection/tree_identify.py
Traceback (most recent call last):
  File "d:\treeDetection\tree_identify.py", line 1, in <module>
    from deepforest import main
  File "D:\anaconda\envs\deepforest\Lib\site-packages\deepforest\main.py", line 12, in <module>
    from deepforest import utilities
  File "D:\anaconda\envs\deepforest\Lib\site-packages\deepforest\utilities.py", line 10, in <module>
    import rasterio
  File "D:\anaconda\envs\deepforest\Lib\site-packages\rasterio\__init__.py", line 28, in <module>
    from rasterio._version import gdal_version, get_geos_version, get_proj_version
ImportError: DLL load failed while importing _version: 找不到指定的程序。

@bw4sz
Copy link
Collaborator

bw4sz commented Nov 28, 2024

Thanks for reporting, can you print out info from conda list, it will tell us about package versions. Rasterio interacts with gdal, this is upstream of DeepForest and definitely particular to your own install. Start by creating an env, install rasterio https://rasterio.readthedocs.io/en/stable/installation.html and confirm you can achieve that first. I'll leave this open, but i'm not convinced there is anything we can do from DeepForest perspective.

@ethanwhite ethanwhite added the Awaiting author contribution Waiting on the issue author to do something before proceeding label Dec 4, 2024
@wqfcv
Copy link
Author

wqfcv commented Dec 10, 2024

# packages in environment at D:\anaconda\envs\DeepForest:
#
# Name                    Version                   Build  Channel
affine                    2.4.0              pyhd8ed1ab_0    conda-forge
alabaster                 1.0.0              pyhd8ed1ab_0    conda-forge
aom                       3.5.0                h63175ca_0    conda-forge
attrs                     24.2.0             pyh71513ae_0    conda-forge
babel                     2.16.0             pyhd8ed1ab_0    conda-forge
blas                      1.0                         mkl    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
blosc                     1.21.3               h6c2663c_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
boost-cpp                 1.82.0               h59b6b97_2    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
bottleneck                1.4.2           py312h4b0e54e_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
branca                    0.7.2              pyhd8ed1ab_0    conda-forge
brotli                    1.0.9                hcfcfb64_9    conda-forge
brotli-bin                1.0.9                hcfcfb64_9    conda-forge
brotli-python             1.0.9           py312hd77b12b_8    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
bzip2                     1.0.8                h2bbff1b_6    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
ca-certificates           2024.9.24            haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
cairo                     1.16.0            hb19e0ff_1008    conda-forge
certifi                   2024.8.30          pyhd8ed1ab_0    conda-forge
cfitsio                   3.470                h0af3d06_7    conda-forge
charls                    2.2.0                h39d44d4_0    conda-forge
charset-normalizer        3.3.2              pyhd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
click                     8.1.7           win_pyh7428d3b_0    conda-forge
click-plugins             1.1.1                      py_0    conda-forge
cligj                     0.7.2              pyhd8ed1ab_1    conda-forge
colorama                  0.4.6              pyhd8ed1ab_0    conda-forge
commonmark                0.9.1                      py_0    conda-forge
contourpy                 1.3.1           py312h214f63a_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
cudatoolkit               10.2.89              h74a9793_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
cycler                    0.12.1             pyhd8ed1ab_0    conda-forge
dav1d                     1.2.1                hcfcfb64_0    conda-forge
deepforest                1.1.3              pyh8a188c0_0    conda-forge
docutils                  0.21.2             pyhd8ed1ab_0    conda-forge
exceptiongroup            1.2.2              pyhd8ed1ab_0    conda-forge
expat                     2.6.3                h5da7b33_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
filelock                  3.13.1          py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
fiona                     1.9.5           py312hc7c4135_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
folium                    0.18.0             pyhd8ed1ab_0    conda-forge
fontconfig                2.14.1               hb33846d_3    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
fonttools                 4.55.0             pyhe1237c8_0    conda-forge
freetype                  2.12.1               ha860e81_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
freexl                    2.0.0                h8276f4a_0    conda-forge
fsspec                    2024.10.0          pyhff2d567_0    conda-forge
future                    1.0.0              pyhd8ed1ab_0    conda-forge
gdal                      3.6.2           py312h8827949_7    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
geopandas                 0.14.4             pyhd8ed1ab_0    conda-forge
geopandas-base            0.14.4             pyha770c72_0    conda-forge
geos                      3.8.0                he025d50_1    conda-forge
geotiff                   1.7.0                h4545760_3    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
giflib                    5.2.2                h7edc060_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
h5py                      3.12.1          py312h3b2c811_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
hdf4                      4.2.13            h0e5069d_1005    conda-forge
hdf5                      1.12.1               h51c971a_3    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
icc_rt                    2022.1.0             h6049295_2    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
icu                       68.2                 h0e60522_0    conda-forge
idna                      3.7             py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
imagecodecs               2023.1.23       py312hd5bf116_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
imagesize                 1.4.1              pyhd8ed1ab_0    conda-forge
iniconfig                 2.0.0              pyhd8ed1ab_0    conda-forge
intel-openmp              2023.1.0         h59b6b97_46320    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jinja2                    3.1.4           py312haa95532_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
joblib                    1.4.2              pyhd8ed1ab_0    conda-forge
jpeg                      9e                   h827c3e9_3    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
kealib                    1.5.0                hde4a422_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
kiwisolver                1.4.4           py312hd77b12b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
krb5                      1.20.1               heb0366b_0    conda-forge
lcms2                     2.12                 h83e58a3_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
lerc                      3.0                  hd77b12b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libaec                    1.1.3                h63175ca_0    conda-forge
libavif                   0.11.1               ha7b97ba_2    conda-forge
libboost                  1.82.0               h3399ecb_2    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libbrotlicommon           1.0.9                hcfcfb64_9    conda-forge
libbrotlidec              1.0.9                hcfcfb64_9    conda-forge
libbrotlienc              1.0.9                hcfcfb64_9    conda-forge
libcurl                   8.9.1                h0416ee5_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libdeflate                1.17                 h2bbff1b_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libexpat                  2.6.3                he0c23c2_0    conda-forge
libffi                    3.4.4                hd77b12b_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libgdal                   3.6.2                h0e70117_7    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libglib                   2.78.4               ha17d25a_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libiconv                  1.17                 hcfcfb64_2    conda-forge
libjpeg-turbo             2.0.0                h196d8e1_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libkml                    1.3.0                h63940dd_7    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libnetcdf                 4.8.1                h6685c40_4    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libpng                    1.6.39               h8cc25b3_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libpq                     17.0                 h70ee33d_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libspatialindex           1.9.3                h5a68840_5    conda-forge
libspatialite             5.1.0                h25d3e1c_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libssh2                   1.10.0               h9a1e1f7_2    conda-forge
libtiff                   4.5.1                hd77b12b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libuv                     1.48.0               h827c3e9_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libwebp                   1.3.2                hbc33d0d_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libwebp-base              1.3.2                h3d04722_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libxml2                   2.13.5               h24da03e_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libzip                    1.8.0                h519de47_1    conda-forge
libzopfli                 1.0.3                h0e60522_0    conda-forge
lightning-utilities       0.11.9             pyhff2d567_0    conda-forge
lz4-c                     1.9.4                h2bbff1b_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mapclassify               2.8.1              pyhd8ed1ab_0    conda-forge
markupsafe                2.1.3           py312h2bbff1b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
matplotlib-base           3.9.2           py312he19b0ae_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
minizip                   4.0.3                hb68bac4_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mkl                       2023.1.0         h6b88ed4_46358    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mkl-service               2.4.0           py312h2bbff1b_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mkl_fft                   1.3.11          py312h827c3e9_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mkl_random                1.2.8           py312h0158946_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mpmath                    1.3.0           py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
munkres                   1.1.4              pyh9f0ad1d_0    conda-forge
networkx                  3.2.1           py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
numexpr                   2.10.1          py312h4cd664f_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
numpy                     1.26.4          py312hfd52020_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
numpy-base                1.26.4          py312h4dde369_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
numpydoc                  1.8.0              pyhd8ed1ab_0    conda-forge
openjpeg                  2.5.2                hae555c5_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
openssl                   3.4.0                h2466b09_0    conda-forge
packaging                 24.2               pyhff2d567_1    conda-forge
pandas                    2.2.3           py312h5da7b33_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pcre2                     10.42                h0ff8eda_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pillow                    11.0.0          py312hb5480e2_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pip                       24.2            py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pixman                    0.43.4               h63175ca_0    conda-forge
pluggy                    1.5.0              pyhd8ed1ab_0    conda-forge
poppler                   24.09.0              h4f47ea2_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
poppler-data              0.4.12               hd8ed1ab_0    conda-forge
proj                      9.3.1                ha107b6e_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
psutil                    5.9.0           py312h2bbff1b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pygments                  2.18.0             pyhd8ed1ab_0    conda-forge
pyparsing                 3.2.0              pyhd8ed1ab_1    conda-forge
pyproj                    3.6.1           py312h8115771_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pysocks                   1.7.1           py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pytest                    8.3.3              pyhd8ed1ab_0    conda-forge
python                    3.12.7               h14ffc60_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
python-dateutil           2.9.0.post0        pyhff2d567_0    conda-forge
python-tzdata             2024.2             pyhd8ed1ab_0    conda-forge
pytorch                   2.5.1              py3.12_cpu_0    pytorch
pytorch-lightning         2.4.0              pyhd8ed1ab_0    conda-forge
pytorch-mutex             1.0                         cpu    pytorch
pytz                      2024.2             pyhd8ed1ab_0    conda-forge
pyyaml                    6.0.2           py312h827c3e9_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
qhull                     2020.2               hc790b64_5    conda-forge
rasterio                  1.3.10          py312hca534e7_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
recommonmark              0.7.1              pyhd8ed1ab_0    conda-forge
requests                  2.32.3          py312haa95532_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
rtree                     1.0.1           py312h2eaa2aa_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
scikit-learn              1.5.1           py312h0158946_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
scipy                     1.14.1          py312h9d85e7c_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
setuptools                72.1.0          py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
shapely                   2.0.5           py312h4059a34_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
six                       1.16.0             pyh6c4a22f_0    conda-forge
slidingwindow             0.0.14             pyh9f0ad1d_0    conda-forge
snappy                    1.2.1                h23299a8_0    conda-forge
snowballstemmer           2.2.0              pyhd8ed1ab_0    conda-forge
snuggs                    1.4.7              pyhd8ed1ab_1    conda-forge
sphinx                    8.1.3              pyhd8ed1ab_0    conda-forge
sphinxcontrib-applehelp   2.0.0              pyhd8ed1ab_0    conda-forge
sphinxcontrib-devhelp     2.0.0              pyhd8ed1ab_0    conda-forge
sphinxcontrib-htmlhelp    2.1.0              pyhd8ed1ab_0    conda-forge
sphinxcontrib-jsmath      1.0.1              pyhd8ed1ab_0    conda-forge
sphinxcontrib-qthelp      2.0.0              pyhd8ed1ab_0    conda-forge
sphinxcontrib-serializinghtml 1.1.10             pyhd8ed1ab_0    conda-forge
sqlite                    3.45.3               h2bbff1b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
sympy                     1.13.2          py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
tabulate                  0.9.0              pyhd8ed1ab_1    conda-forge
tbb                       2021.8.0             h59b6b97_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
threadpoolctl             3.5.0              pyhc1e730c_0    conda-forge
tiledb                    2.3.3                hd8964de_3    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
tk                        8.6.14               h0416ee5_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
tomli                     2.1.0              pyhff2d567_0    conda-forge
torchmetrics              1.5.2              pyhe5570ce_0    conda-forge
torchvision               0.20.1                py312_cpu    pytorch
tqdm                      4.67.1             pyhd8ed1ab_0    conda-forge
typing-extensions         4.11.0          py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
typing_extensions         4.11.0          py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
tzdata                    2024b                h04d1e81_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
ucrt                      10.0.22621.0         h57928b3_1    conda-forge
unicodedata2              15.1.0          py312h2bbff1b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
uriparser                 0.9.8                h5a68840_0    conda-forge
urllib3                   2.2.3           py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
vc                        14.40                h2eaa2aa_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
vc14_runtime              14.42.34433         he29a5d6_23    conda-forge
vs2015_runtime            14.42.34433         hdffcdeb_23    conda-forge
wheel                     0.44.0          py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
win_inet_pton             1.1.0           py312haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
xerces-c                  3.2.5                he0c23c2_2    conda-forge
xmltodict                 0.14.2             pyhd8ed1ab_0    conda-forge
xyzservices               2024.9.0           pyhd8ed1ab_0    conda-forge
xz                        5.4.6                h8cc25b3_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
yaml                      0.2.5                he774522_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
zfp                       1.0.1                he0c23c2_2    conda-forge
zlib                      1.2.13               h8cc25b3_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
zstd                      1.5.6                h8880b57_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main

@github-actions github-actions bot removed the Awaiting author contribution Waiting on the issue author to do something before proceeding label Dec 10, 2024
@henrykironde
Copy link
Contributor

@wqfcv, thank you for reaching out! Could you please run the following commands and share the output with us? This will help us diagnose the issue more effectively:

python -c "import rasterio; print(rasterio.__version__)"
gdalinfo --version

Looking forward to your response!

@wqfcv
Copy link
Author

wqfcv commented Dec 11, 2024

(deepForest) C:\Users\18774>python -c "import rasterio; print(rasterio.__version__)"
1.3.10

(deepForest) C:\Users\18774>gdalinfo --version
GDAL 3.6.2, released 2023/01/02

@henrykironde
Copy link
Contributor

@wqfcv The issue likely stems from your environment, particularly how GDAL and Rasterio are installed. To resolve this, I recommend updating Conda and creating a fresh environment. Then, install DeepForest using pip instead of Conda. Here's how:

conda create -n new_deepforest python=3.9
conda activate new_deepforest
conda install -c pytorch pytorch torchvision cudatoolkit=10.2
conda install -c conda-forge rasterio gdal
pip install deepforest

Once the setup is complete, try the example provided in the DeepForest documentation:
Predicting Large Tiles Tutorial

Also raster_path = get_data("result.tif") should be used for inbuilt data https://github.com/weecology/DeepForest/tree/main/src/deepforest/data .

@wqfcv
Copy link
Author

wqfcv commented Dec 12, 2024

Thank you for your help. I have successfully installed DeepForest. But there seems to be an issue in the example code. Here's code:

from deepforest import main
from deepforest import get_data
import matplotlib.pyplot as plt


def main_program():
    # Initialize the model class
    model = main.deepforest()

    # Load a pretrained tree detection model from Hugging Face
    model.load_model(model_name="weecology/deepforest-tree", revision="main")

    # Predict on large geospatial tiles using overlapping windows
    raster_path = get_data("OSBS_029.tif")
    predicted_raster = model.predict_tile(raster_path, patch_size=300, patch_overlap=0.25)

    # Optional: Visualize or save the predicted raster (add your plotting or saving logic here)
    print(predicted_raster)
    plot_results(results)


if __name__ == '__main__':
    main_program()

Here's error:

Traceback (most recent call last):
  File "D:\treeDetection\test.py", line 23, in <module>
    main_program()
  File "D:\treeDetection\test.py", line 19, in main_program
    plot_results(results)
NameError: name 'plot_results' is not defined

@henrykironde
Copy link
Contributor

You are using and old version. Use pip uninstall deepforeset and then pip install deepforest -U

@wqfcv
Copy link
Author

wqfcv commented Dec 16, 2024

Thank you for your help. I can now use plot_results normally. But after running the following code, no image was displayed in pycharm.

from deepforest import main
from deepforest import get_data
import matplotlib.pyplot as plt
from deepforest.visualize import plot_results
import rasterio

def main_program():

    savedir = r'D:\\treeDetection\\test.tif'
    # Initialize the model class
    model = main.deepforest()


    # Load a pretrained tree detection model
    model.load_model(model_name="weecology/deepforest-tree", revision="main")

    # Input raster path
    raster_path = get_data("OSBS_029.tif")
    print(f"Raster Path: {raster_path}")

    # Open the raster image using rasterio
    with rasterio.open(raster_path) as src:
        image = src.read([1, 2, 3]).transpose(1, 2, 0)  # Read RGB channels

    # Generate predictions
    predicted_raster = model.predict_tile(raster_path, patch_size=300, patch_overlap=0.25)
    print(predicted_raster)

    # Visualize predictions on the image
    # plot_results(predicted_raster,savedir=savedir)
    plot_results(predicted_raster)
    # plt.show()

if __name__ == '__main__':
    main_program()

@henrykironde
Copy link
Contributor

@wqfcv I don't think PyCharm will display the images. The images usually pop up as their own Gui window trying to look for any extra icon / application on the task bar.

@wqfcv
Copy link
Author

wqfcv commented Dec 18, 2024

But after running the code, no GUI window appeared.
屏幕截图 2024-12-18 090800

@wqfcv
Copy link
Author

wqfcv commented Dec 20, 2024

I can solve my previous problem, but I encountered issues that may be related to multithreading when using the GPU version of PyTorch.
Here is my virtual environment information:

# packages in environment at D:\anaconda\envs\new_deepforest:
#
# Name                    Version                   Build  Channel
accessible-pygments       0.0.5                    pypi_0    pypi
affine                    2.4.0              pyhd8ed1ab_1    conda-forge
aiohappyeyeballs          2.4.4                    pypi_0    pypi
aiohttp                   3.11.10                  pypi_0    pypi
aiolimiter                1.2.1                    pypi_0    pypi
aiosignal                 1.3.1                    pypi_0    pypi
alabaster                 0.7.16                   pypi_0    pypi
albucore                  0.0.21                   pypi_0    pypi
albumentations            1.4.23                   pypi_0    pypi
annotated-types           0.7.0                    pypi_0    pypi
anyio                     4.6.2            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
argon2-cffi               21.3.0             pyhd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
argon2-cffi-bindings      21.2.0           py39h2bbff1b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
asttokens                 3.0.0                    pypi_0    pypi
async-lru                 2.0.4            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
async-timeout             5.0.1                    pypi_0    pypi
attrs                     24.2.0             pyh71513ae_1    conda-forge
autopep8                  2.3.1                    pypi_0    pypi
babel                     2.16.0                   pypi_0    pypi
backcall                  0.2.0              pyhd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
beautifulsoup4            4.12.3           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
blas                      1.0                         mkl    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
bleach                    6.2.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
blosc                     1.21.5               hbd69f2e_1    conda-forge
boost-cpp                 1.78.0               h9f4b32c_4    conda-forge
brotli-python             1.0.9            py39hd77b12b_8    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
bzip2                     1.0.8                h2466b09_7    conda-forge
ca-certificates           2024.12.14           h56e8100_0    conda-forge
cairo                     1.16.0            hd694305_1014    conda-forge
certifi                   2024.12.14         pyhd8ed1ab_0    conda-forge
cffi                      1.17.1           py39h827c3e9_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
cfitsio                   4.2.0                h9ebe7e4_0    conda-forge
charset-normalizer        3.3.2              pyhd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
click                     8.1.7           win_pyh7428d3b_1    conda-forge
click-plugins             1.1.1              pyhd8ed1ab_1    conda-forge
cligj                     0.7.2              pyhd8ed1ab_2    conda-forge
colorama                  0.4.6              pyhd8ed1ab_1    conda-forge
comm                      0.2.1            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
commonmark                0.9.1                    pypi_0    pypi
contourpy                 1.3.0                    pypi_0    pypi
cuda-cccl                 12.4.127                      0    nvidia
cuda-cudart               12.1.105                      0    nvidia
cuda-cudart-dev           12.1.105                      0    nvidia
cuda-cupti                12.1.105                      0    nvidia
cuda-libraries            12.1.0                        0    nvidia
cuda-libraries-dev        12.1.0                        0    nvidia
cuda-nvrtc                12.1.105                      0    nvidia
cuda-nvrtc-dev            12.1.105                      0    nvidia
cuda-nvtx                 12.1.105                      0    nvidia
cuda-opencl               12.4.127                      0    nvidia
cuda-opencl-dev           12.4.127                      0    nvidia
cuda-profiler-api         12.4.127                      0    nvidia
cuda-runtime              12.1.0                        0    nvidia
cudatoolkit               10.2.89              h74a9793_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
curl                      8.9.1                h3f729d1_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
cycler                    0.12.1                   pypi_0    pypi
debugpy                   1.6.7            py39hd77b12b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
decorator                 5.1.1              pyhd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
deepforest                1.4.1                    pypi_0    pypi
defusedxml                0.7.1              pyhd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
docformatter              1.7.5                    pypi_0    pypi
docutils                  0.21.2                   pypi_0    pypi
eval-type-backport        0.2.0                    pypi_0    pypi
exceptiongroup            1.2.2                    pypi_0    pypi
executing                 2.1.0                    pypi_0    pypi
expat                     2.6.4                he0c23c2_0    conda-forge
filelock                  3.13.1           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
font-ttf-dejavu-sans-mono 2.37                 hab24e00_0    conda-forge
font-ttf-inconsolata      3.000                h77eed37_0    conda-forge
font-ttf-source-code-pro  2.038                h77eed37_0    conda-forge
font-ttf-ubuntu           0.83                 h77eed37_3    conda-forge
fontconfig                2.14.2               hbde0cde_0    conda-forge
fonts-conda-ecosystem     1                             0    conda-forge
fonts-conda-forge         1                             0    conda-forge
fonttools                 4.55.3                   pypi_0    pypi
freetype                  2.12.1               ha860e81_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
freexl                    1.0.6                h67ca5e6_1    conda-forge
frozenlist                1.5.0                    pypi_0    pypi
fsspec                    2024.10.0                pypi_0    pypi
gdal                      3.6.2            py39h3be0312_9    conda-forge
geopandas                 1.0.1                    pypi_0    pypi
geos                      3.11.1               h1537add_0    conda-forge
geotiff                   1.7.1                hc256dc0_6    conda-forge
gettext                   0.22.5               h5728263_3    conda-forge
gettext-tools             0.22.5               h5a7288d_3    conda-forge
giflib                    5.2.2                h7edc060_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
glib                      2.78.1               h12be248_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge
glib-tools                2.78.1               h12be248_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge
gmpy2                     2.1.2            py39h7f96b67_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
gst-plugins-base          1.22.3               h23ce68f_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
gstreamer                 1.22.3               h2bbff1b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
h11                       0.14.0           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
hdf4                      4.2.15               h1b1b6ef_5    conda-forge
hdf5                      1.12.2          nompi_h57737ce_101    conda-forge
httpcore                  1.0.2            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
httpx                     0.27.0           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
huggingface-hub           0.26.5                   pypi_0    pypi
icu                       70.1                 h0e60522_0    conda-forge
idna                      3.7              py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
imagesize                 1.4.1                    pypi_0    pypi
importlib-metadata        8.5.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
importlib-resources       6.4.5                    pypi_0    pypi
importlib_metadata        8.5.0                hd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
intel-openmp              2023.1.0         h59b6b97_46320    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
ipykernel                 6.29.5           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
ipython                   8.18.1                   pypi_0    pypi
ipywidgets                8.1.5            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jedi                      0.19.2           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jinja2                    3.1.4            py39haa95532_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jpeg                      9e                   h827c3e9_3    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
json5                     0.9.25           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jsonschema                4.23.0           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jsonschema-specifications 2023.7.1         py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyter                   1.0.0            py39haa95532_9    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyter-lsp               2.2.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyter_client            8.6.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyter_console           6.6.3            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyter_core              5.7.2            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyter_events            0.10.0           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyter_server            2.14.1           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyter_server_terminals  0.4.4            py39haa95532_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyterlab                4.2.5            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyterlab_pygments       0.1.2                      py_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyterlab_server         2.27.3           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
jupyterlab_widgets        3.0.13           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
kealib                    1.5.0                h61be68b_0    conda-forge
kiwisolver                1.4.7                    pypi_0    pypi
krb5                      1.20.1               h5b6d351_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
lcms2                     2.15                 ha5c8aab_0    conda-forge
lerc                      4.0.0                h63175ca_0    conda-forge
libaec                    1.1.3                h63175ca_0    conda-forge
libasprintf               0.22.5               h5728263_3    conda-forge
libasprintf-devel         0.22.5               h5728263_3    conda-forge
libblas                   3.9.0           1_h8933c1f_netlib    conda-forge
libcblas                  3.9.0           8_h719fc58_netlib    conda-forge
libclang                  15.0.7          default_h3a3e6c3_5    https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge
libclang13                15.0.7          default_hf64faad_5    https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge
libcublas                 12.1.0.26                     0    nvidia
libcublas-dev             12.1.0.26                     0    nvidia
libcufft                  11.0.2.4                      0    nvidia
libcufft-dev              11.0.2.4                      0    nvidia
libcurand                 10.3.5.147                    0    nvidia
libcurand-dev             10.3.5.147                    0    nvidia
libcurl                   8.9.1                h0416ee5_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libcusolver               11.4.4.55                     0    nvidia
libcusolver-dev           11.4.4.55                     0    nvidia
libcusparse               12.0.2.55                     0    nvidia
libcusparse-dev           12.0.2.55                     0    nvidia
libdeflate                1.17                 h2bbff1b_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libexpat                  2.6.4                he0c23c2_0    conda-forge
libffi                    3.4.2                h8ffe710_5    conda-forge
libgdal                   3.6.2                h221c0cb_9    conda-forge
libgettextpo              0.22.5               h5728263_3    conda-forge
libgettextpo-devel        0.22.5               h5728263_3    conda-forge
libglib                   2.78.1               he8f3873_0    conda-forge
libiconv                  1.17                 hcfcfb64_2    conda-forge
libintl                   0.22.5               h5728263_3    conda-forge
libintl-devel             0.22.5               h5728263_3    conda-forge
libjpeg-turbo             2.0.0                h196d8e1_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libkml                    1.3.0             hd45a9bc_1016    conda-forge
liblapack                 3.9.0           8_h719fc58_netlib    conda-forge
libnetcdf                 4.9.1           nompi_hc41bf00_101    conda-forge
libnpp                    12.0.2.50                     0    nvidia
libnpp-dev                12.0.2.50                     0    nvidia
libnvjitlink              12.1.105                      0    nvidia
libnvjitlink-dev          12.1.105                      0    nvidia
libnvjpeg                 12.1.1.14                     0    nvidia
libnvjpeg-dev             12.1.1.14                     0    nvidia
libogg                    1.3.5                h2bbff1b_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libpng                    1.6.39               h8cc25b3_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libpq                     15.2                 ha9684e8_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge
librttopo                 1.1.0               he22b5cd_12    conda-forge
libsodium                 1.0.18               h62dcd97_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libspatialite             5.0.1               hfdcade0_23    conda-forge
libsqlite                 3.47.2               h67fdade_0    conda-forge
libssh2                   1.11.1               h2addb87_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libtiff                   4.5.0                hf8721a0_2    conda-forge
libuv                     1.48.0               h827c3e9_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libvorbis                 1.3.7                he774522_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libwebp                   1.3.2                hbc33d0d_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libwebp-base              1.3.2                h3d04722_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
libxml2                   2.12.7               h283a6d9_1    conda-forge
libzip                    1.9.2                h519de47_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge
libzlib                   1.2.13               h2466b09_6    conda-forge
lightning-utilities       0.11.9                   pypi_0    pypi
lz4-c                     1.9.4                h2bbff1b_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
m2w64-gcc-libgfortran     5.3.0                         6    conda-forge
m2w64-gcc-libs            5.3.0                         7    conda-forge
m2w64-gcc-libs-core       5.3.0                         7    conda-forge
m2w64-gmp                 6.1.0                         2    conda-forge
m2w64-libwinpthread-git   5.0.0.4634.697f757               2    conda-forge
markupsafe                2.1.3            py39h2bbff1b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
matplotlib                3.9.3                    pypi_0    pypi
matplotlib-inline         0.1.7                    pypi_0    pypi
mistune                   2.0.4            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mkl                       2023.1.0         h6b88ed4_46358    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mkl-service               2.4.0            py39h2bbff1b_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mkl_fft                   1.3.11           py39h827c3e9_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mkl_random                1.2.8            py39hc64d2fc_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mpc                       1.1.0                h7edee0f_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mpfr                      4.0.2                h62dcd97_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mpir                      3.0.0                hec2e145_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
mpmath                    1.3.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
msys2-conda-epoch         20160418                      1    conda-forge
multidict                 6.1.0                    pypi_0    pypi
nbclient                  0.8.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
nbconvert                 7.16.4           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
nbformat                  5.10.4           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
nbqa                      1.9.1                    pypi_0    pypi
nest-asyncio              1.6.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
networkx                  3.2.1            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
notebook                  7.2.2            py39haa95532_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
notebook-shim             0.2.3            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
numpy                     1.26.0                   pypi_0    pypi
numpy-base                1.26.4           py39h65a83cf_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
opencv-python-headless    4.10.0.84                pypi_0    pypi
openjpeg                  2.5.2                hae555c5_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
openssl                   3.1.7                h2466b09_0    conda-forge
overrides                 7.4.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
packaging                 24.2             py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pandas                    2.2.3                    pypi_0    pypi
pandocfilters             1.5.0              pyhd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
parso                     0.8.4            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pcre2                     10.40                h17e33f8_0    conda-forge
pickleshare               0.7.5           pyhd3eb1b0_1003    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pillow                    11.0.0           py39hb5480e2_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pip                       24.2             py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pixman                    0.44.2               had0cd8c_0    conda-forge
platformdirs              3.10.0           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
ply                       3.11             py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
poppler                   23.03.0              h183ae7b_0    conda-forge
poppler-data              0.4.12               hd8ed1ab_0    conda-forge
postgresql                15.2                 hd87cd2b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge
progressbar2              4.5.0                    pypi_0    pypi
proj                      9.1.1                heca977f_2    conda-forge
prometheus_client         0.21.0           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
prompt-toolkit            3.0.48                   pypi_0    pypi
prompt_toolkit            3.0.43               hd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
propcache                 0.2.1                    pypi_0    pypi
psutil                    6.1.0                    pypi_0    pypi
pure-eval                 0.2.3                    pypi_0    pypi
pure_eval                 0.2.2              pyhd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pycocotools               2.0.8                    pypi_0    pypi
pycodestyle               2.12.1                   pypi_0    pypi
pycparser                 2.21               pyhd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pydantic                  2.10.3                   pypi_0    pypi
pydantic-core             2.27.1                   pypi_0    pypi
pydata-sphinx-theme       0.16.0                   pypi_0    pypi
pygments                  2.18.0                   pypi_0    pypi
pyogrio                   0.10.0                   pypi_0    pypi
pyparsing                 3.2.0              pyhd8ed1ab_2    conda-forge
pyproj                    3.6.1                    pypi_0    pypi
pyqt                      5.15.10          py39hd77b12b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pyqt5-sip                 12.13.0          py39h2bbff1b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pysocks                   1.7.1            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
python                    3.9.21               h8205438_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
python-dateutil           2.9.0post0       py39haa95532_2    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
python-fastjsonschema     2.20.0           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
python-json-logger        3.2.1            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
python-utils              3.9.1                    pypi_0    pypi
python_abi                3.9                      2_cp39    conda-forge
pytorch                   2.5.1           py3.9_cuda12.1_cudnn9_0    pytorch
pytorch-cuda              12.1                 hde6ce7c_6    pytorch
pytorch-lightning         2.4.0                    pypi_0    pypi
pytorch-mutex             1.0                        cuda    pytorch
pytz                      2024.2                   pypi_0    pypi
pywin32                   305              py39h2bbff1b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pywinpty                  2.0.14           py39h72d21ff_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pyyaml                    6.0.2            py39h827c3e9_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
pyzmq                     25.1.2           py39hd77b12b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
qt-main                   5.15.8               h720456b_6    https://mirrors.sjtug.sjtu.edu.cn/anaconda/cloud/conda-forge
qtconsole                 5.6.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
qtpy                      2.4.1            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
rasterio                  1.3.10           py39had1fd89_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
recommonmark              0.7.1                    pypi_0    pypi
referencing               0.30.2           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
requests                  2.32.3           py39haa95532_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
rfc3339-validator         0.1.4            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
rfc3986-validator         0.1.1            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
rpds-py                   0.10.6           py39h636fa0f_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
rtree                     1.3.0                    pypi_0    pypi
safetensors               0.4.5                    pypi_0    pypi
scipy                     1.13.1                   pypi_0    pypi
send2trash                1.8.2            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
setuptools                75.1.0           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
shapely                   2.0.6                    pypi_0    pypi
simsimd                   6.2.1                    pypi_0    pypi
sip                       6.7.12           py39hd77b12b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
six                       1.17.0                   pypi_0    pypi
slidingwindow             0.0.14                   pypi_0    pypi
snappy                    1.2.1                h500f7fa_1    conda-forge
sniffio                   1.3.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
snowballstemmer           2.2.0                    pypi_0    pypi
snuggs                    1.4.7              pyhd8ed1ab_2    conda-forge
soupsieve                 2.6                      pypi_0    pypi
sphinx                    7.4.7                    pypi_0    pypi
sphinxcontrib-applehelp   2.0.0                    pypi_0    pypi
sphinxcontrib-devhelp     2.0.0                    pypi_0    pypi
sphinxcontrib-htmlhelp    2.1.0                    pypi_0    pypi
sphinxcontrib-jsmath      1.0.1                    pypi_0    pypi
sphinxcontrib-qthelp      2.0.0                    pypi_0    pypi
sphinxcontrib-serializinghtml 2.0.0                    pypi_0    pypi
sqlite                    3.45.3               h2bbff1b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
stack-data                0.6.3                    pypi_0    pypi
stack_data                0.2.0              pyhd3eb1b0_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
stringzilla               3.11.1                   pypi_0    pypi
supervision               0.22.0                   pypi_0    pypi
sympy                     1.13.1                   pypi_0    pypi
tbb                       2021.8.0             h59b6b97_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
terminado                 0.17.1           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
tiledb                    2.13.2               h3132609_0    conda-forge
tinycss2                  1.2.1            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
tokenize-rt               6.1.0                    pypi_0    pypi
tomli                     2.2.1                    pypi_0    pypi
torchaudio                2.5.1                    pypi_0    pypi
torchmetrics              1.6.0                    pypi_0    pypi
torchvision               0.20.1                   pypi_0    pypi
tornado                   6.4.2            py39h827c3e9_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
tqdm                      4.67.1                   pypi_0    pypi
traitlets                 5.14.3           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
typing-extensions         4.12.2                   pypi_0    pypi
tzdata                    2024.2                   pypi_0    pypi
ucrt                      10.0.22621.0         h57928b3_1    conda-forge
untokenize                0.1.1                    pypi_0    pypi
uriparser                 0.9.8                h5a68840_0    conda-forge
urllib3                   2.2.3            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
vc                        14.40                haa95532_2    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
vc14_runtime              14.42.34433         he29a5d6_23    conda-forge
vs2015_runtime            14.42.34433         hdffcdeb_23    conda-forge
wcwidth                   0.2.13                   pypi_0    pypi
webencodings              0.5.1            py39haa95532_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
websocket-client          1.8.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
wheel                     0.44.0           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
widgetsnbextension        4.0.13           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
win_inet_pton             1.1.0            py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
winpty                    0.4.3                         4    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
xerces-c                  3.2.5                he0c23c2_2    conda-forge
xmltodict                 0.14.2                   pypi_0    pypi
xz                        5.4.6                h8cc25b3_1    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
yaml                      0.2.5                he774522_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
yarl                      1.18.3                   pypi_0    pypi
zeromq                    4.3.5                hd77b12b_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
zipp                      3.21.0           py39haa95532_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main
zlib                      1.2.13               h2466b09_6    conda-forge
zstd                      1.5.6                h8880b57_0    https://mirrors.sjtug.sjtu.edu.cn/anaconda/pkgs/main

Here's my code:

from deepforest import main
from deepforest import get_data
import matplotlib.pyplot as plt
from deepforest.visualize import plot_results
import rasterio
import os
import matplotlib
from matplotlib.patches import Rectangle
import torch

# matplotlib.use('TkAgg')  # 适合本地图形界面

def initialize_model():
    """
    Initialize and load the pre-trained DeepForest model.
    """
    model = main.deepforest()
    model.load_model(model_name="weecology/deepforest-tree", revision="main")
    return model

def read_raster(raster_path):
    """
    Read the raster file using rasterio and extract RGB channels.
    """
    with rasterio.open(raster_path) as src:
        image = src.read([1, 2, 3]).transpose(1, 2, 0)  # Read RGB channels
    return image



def predict_and_visualize(model, raster_path, savedir=None):
    """
    Generate predictions using the model and visualize the results.
    """
    # Generate predictions
    predicted_raster = model.predict_tile(raster_path, patch_size=300, patch_overlap=0.25)
    print(f"Predicted Raster: \n{predicted_raster}")

    # Read the original raster for background visualization
    with rasterio.open(raster_path) as src:
        image = src.read([1, 2, 3]).transpose(1, 2, 0)  # Read RGB channels

    # Plot the original raster
    fig, ax = plt.subplots(figsize=(12, 12))
    ax.imshow(image)
    ax.set_title("Predicted Raster with Bounding Boxes")

    # Add bounding boxes from the predictions
    for _, row in predicted_raster.iterrows():
        x_min, y_min, x_max, y_max = row['xmin'], row['ymin'], row['xmax'], row['ymax']
        label = row['label']
        confidence = row['score']

        # Draw rectangle
        rect = Rectangle(
            (x_min, y_min),
            x_max - x_min,
            y_max - y_min,
            linewidth=2,
            edgecolor='red',
            facecolor='none'
        )
        ax.add_patch(rect)
        # Add label and confidence
        ax.text(
            x_min, y_min - 5,
            f"{label}: {confidence:.2f}",
            color='red',
            fontsize=8,
            bbox=dict(facecolor='white', alpha=0.5, edgecolor='none')
        )

    # Save results if a valid directory is provided
    if savedir:
        save_directory = os.path.dirname(savedir)
        if not os.path.exists(save_directory):
            os.makedirs(save_directory)
        plt.savefig(savedir)
        print(f"Results saved to {savedir}")

    # Show the plot
    plt.show()

def main_programm():
    """
    Main program function.
    """
    # File paths
    raster_path = get_data(r"D:\\treeDetection\\result_threebands.tif")
    savedir = r'D:\\treeDetection\\output2.png'

    # Initialize model
    model = initialize_model()

    # Read raster and perform predictions
    image = read_raster(raster_path)
    predict_and_visualize(model, raster_path, savedir)

if __name__ == '__main__':
    main_programm()

Here's the error:

Traceback (most recent call last):
  File "D:\treeDetection\test2.py", line 123, in <module>
    main_programm()
  File "D:\treeDetection\test2.py", line 120, in main_programm
    predict_and_visualize(model, raster_path, savedir)
  File "D:\treeDetection\test2.py", line 59, in predict_and_visualize
    predicted_raster = model.predict_tile(raster_path, patch_size=300, patch_overlap=0.25)
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\deepforest\main.py", line 541, in predict_tile
    batched_results = self.trainer.predict(self, self.predict_dataloader(ds))
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 858, in predict
    return call._call_and_handle_interrupt(
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\trainer\call.py", line 47, in _call_and_handle_interrupt
    return trainer_fn(*args, **kwargs)
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 897, in _predict_impl
    results = self._run(model, ckpt_path=ckpt_path)
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 981, in _run
    results = self._run_stage()
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\trainer\trainer.py", line 1020, in _run_stage
    return self.predict_loop.run()
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\loops\utilities.py", line 178, in _decorator
    return loop_run(self, *args, **kwargs)
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\loops\prediction_loop.py", line 107, in run
    self.reset()
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\loops\prediction_loop.py", line 181, in reset
    iter(data_fetcher)  # creates the iterator inside the fetcher
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\loops\fetchers.py", line 104, in __iter__
    super().__iter__()
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\loops\fetchers.py", line 51, in __iter__
    self.iterator = iter(self.combined_loader)
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\utilities\combined_loader.py", line 351, in __iter__
    iter(iterator)
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\utilities\combined_loader.py", line 155, in __iter__
    self._load_current_iterator()
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\pytorch_lightning\utilities\combined_loader.py", line 173, in _load_current_iterator
    self.iterators = [iter(self.iterables[self._iterator_idx])]
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\torch\utils\data\dataloader.py", line 484, in __iter__
    return self._get_iterator()
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\torch\utils\data\dataloader.py", line 415, in _get_iterator
    return _MultiProcessingDataLoaderIter(self)
  File "D:\anaconda\envs\new_deepforest\lib\site-packages\torch\utils\data\dataloader.py", line 1138, in __init__
    w.start()
  File "D:\anaconda\envs\new_deepforest\lib\multiprocessing\process.py", line 121, in start
    self._popen = self._Popen(self)
  File "D:\anaconda\envs\new_deepforest\lib\multiprocessing\context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "D:\anaconda\envs\new_deepforest\lib\multiprocessing\context.py", line 327, in _Popen
    return Popen(process_obj)
  File "D:\anaconda\envs\new_deepforest\lib\multiprocessing\popen_spawn_win32.py", line 93, in __init__
    reduction.dump(process_obj, to_child)
  File "D:\anaconda\envs\new_deepforest\lib\multiprocessing\reduction.py", line 60, in dump
    ForkingPickler(file, protocol).dump(obj)
OSError: [Errno 22] Invalid argument

@bw4sz
Copy link
Collaborator

bw4sz commented Jan 16, 2025

Labeling this wontfix installation, this seems upstream of us and specific to this user, pytorch installation challenges.

@bw4sz bw4sz closed this as completed Jan 16, 2025
@bw4sz bw4sz added wontfix This will not be worked on installation labels Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
installation wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants