PatBall1 / detectree2

Python package for automatic tree crown delineation based on the Detectron2 implementation of Mask R-CNN
https://patball1.github.io/detectree2/
MIT License
148 stars 35 forks source link

PIL.Image attribute error when importing from detectree2.models.train #110

Closed JDtroles closed 11 months ago

JDtroles commented 1 year ago
from detectree2.preprocessing.tiling import tile_data_train, to_traintest_folders
from detectree2.models.train import register_train_data, MyTrainer, setup_cfg
import rasterio
import geopandas as gpd
import shutil

throws Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[14], line 2
      1 from detectree2.preprocessing.tiling import tile_data_train, to_traintest_folders
----> 2 from detectree2.models.train import register_train_data, MyTrainer, setup_cfg
      4 import rasterio
      5 import geopandas as gpd

File ~/anaconda3/envs/detectree2/lib/python3.8/site-packages/detectree2/models/train.py:17
     14 from typing import Any, Dict, List
     16 import cv2
---> 17 import detectron2.data.transforms as T  # noqa:N812
     18 import detectron2.utils.comm as comm
     19 import numpy as np

File ~/anaconda3/envs/detectree2/lib/python3.8/site-packages/detectron2/data/__init__.py:2
      1 # Copyright (c) Facebook, Inc. and its affiliates.
----> 2 from . import transforms  # isort:skip
      4 from .build import (
      5     build_batch_data_loader,
      6     build_detection_test_loader,
   (...)
     10     print_instances_class_histogram,
     11 )
     12 from .catalog import DatasetCatalog, MetadataCatalog, Metadata

File ~/anaconda3/envs/detectree2/lib/python3.8/site-packages/detectron2/data/transforms/__init__.py:4
      2 from fvcore.transforms.transform import Transform, TransformList  # order them first
      3 from fvcore.transforms.transform import *
----> 4 from .transform import *
      5 from .augmentation import *
      6 from .augmentation_impl import *

File ~/anaconda3/envs/detectree2/lib/python3.8/site-packages/detectron2/data/transforms/transform.py:36
     25     pass
     27 __all__ = [
     28     "ExtentTransform",
     29     "ResizeTransform",
   (...)
     32     "PILColorTransform",
     33 ]
---> 36 class ExtentTransform(Transform):
     37     """
     38     Extracts a subregion from the source image and scales it to the output size.
     39 
   (...)
     43     See: https://pillow.readthedocs.io/en/latest/PIL.html#PIL.ImageTransform.ExtentTransform
     44     """
     46     def __init__(self, src_rect, output_size, interp=Image.LINEAR, fill=0):

File ~/anaconda3/envs/detectree2/lib/python3.8/site-packages/detectron2/data/transforms/transform.py:46, in ExtentTransform()
     36 class ExtentTransform(Transform):
     37     """
     38     Extracts a subregion from the source image and scales it to the output size.
     39 
   (...)
     43     See: https://pillow.readthedocs.io/en/latest/PIL.html#PIL.ImageTransform.ExtentTransform
     44     """
---> 46     def __init__(self, src_rect, output_size, interp=Image.LINEAR, fill=0):
     47         """
     48         Args:
     49             src_rect (x0, y0, x1, y1): src coordinates
   (...)
     52             fill: Fill color used when src_rect extends outside image
     53         """
     54         super().__init__()

AttributeError: module 'PIL.Image' has no attribute 'LINEAR'

Help is greatly appreciated.

Thank you for your work!

PatBall1 commented 1 year ago

@JDtroles thanks for raising this issue. I believe that it comes from this issue with detectron2: https://github.com/facebookresearch/detectron2/issues/5010

In short, the newest Pillow (10.0.0) seems to be causing problems because LINEAR no longer is equated to BILINEAR. Based on the above linked issue it should be fixed very soon so I will not implement a work around on this repo.

If things are urgent, a temporary solution would be to pin the previous version of Pillow when installing detectron2:

First clone locally:

git clone https://github.com/facebookresearch/detectron2.git
cd detectron2

Then edit the setup.py to fix the detectron2 version:

# other bits of setup script...
setup(
   # other setup instructions
    install_requires=[
       [...]
        "Pillow==9.5.0",     # Replacing "Pillow>=7.1" which attempts to get the latest version
       # other dependencies...
    ],
)

Then install the package located in the current directory

pip install .

Then, assuming the other dependencies are correctly set up, you should be able to install detectree2 with the usual method:

cd ..
pip install git+https://github.com/PatBall1/detectree2.git

Please let me know if you have any questions or other thoughts.

JDtroles commented 1 year ago

@PatBall1 thank you for the really fast answer! It fixed the issue with Pillow. Sadly I am now running into another issue:

ERROR 1: PROJ: proj_create_from_database: /home/usr/anaconda3/envs/env-name/share/proj/proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation.

But probably I will just wait for the fix in detectron. Or is this totally unrelated?

Thanks again!

PatBall1 commented 12 months ago

@JDtroles this seems to be a different issue deriving from a conflict with your installed version of the PROJ library. This problem usually arises when there are multiple versions of PROJ installed in your environment and the system is using an incompatible or incorrect version.

Here are steps to troubleshoot this problem:

  1. Check the version of PROJ: Run the following command to check the current version of PROJ.
proj

The version of the PROJ library should be displayed.

  1. Find the location of PROJ: Use the following command to find where PROJ is installed in your system.
which proj

This will return the path where proj is installed.

  1. Check for Multiple Installations: It's possible that multiple versions of the PROJ library are installed in your system. Run the following command to check this:
sudo find / -name "proj.db"

This will return all paths where proj.db is located. If you see multiple paths, that means you have multiple versions of the PROJ library installed. If you're using conda, you should see a path similar to /home/usr/anaconda3/envs/env-name/share/proj/proj.db.

  1. Remove or Update the Incompatible Version: If there are multiple installations, you need to remove the incompatible one or update it to a compatible version. You can use conda or pip to update the PROJ library, depending on how you installed it.

For conda:

conda install -c conda-forge proj

For pip:

pip install --upgrade pyproj
  1. Set the PROJ_LIB environment variable: If there are multiple installations and you can't remove the incompatible one, or if the problem persists after updating, you can set the PROJ_LIB environment variable to point to the correct proj.db path.
export PROJ_LIB=/path/to/proj.db

Replace /path/to/proj.db with the correct path you found earlier.

Note that this command will only set the PROJ_LIB variable for the current session. If you open a new terminal window or restart your computer, you will have to set it again. To set it permanently, you can add the command to your shell's startup script (like ~/.bashrc or ~/.bash_profile for bash).

You'll need to restart your terminal or source your bash file (source ~/.bashrc) after exporting the variable. Please let me know how you get on.