SatelliteShorelines / CoastSeg

An interactive toolbox for downloading satellite imagery, applying image segmentation models, mapping shoreline positions and more. The mapping extension for CoastSat and Zoo.
https://satelliteshorelines.github.io/CoastSeg/
GNU General Public License v3.0
47 stars 10 forks source link

Update conda recipe to include transformers #136

Closed 2320sharon closed 1 year ago

2320sharon commented 1 year ago

The doodleverse will be changing to accomodate the new .keras model format, and a new set of Transformer models will soon be published to Zenodo. To use these new models tensorflow 2.12 and transformers need to be installed .

A conda install -c conda-forge transformers in this new coastseg env should work to add the transformers package.

2320sharon commented 1 year ago

Dan is working on finishing the Gym update first, then make all the zenodo releases, then next week we can add the dependency and test

2320sharon commented 1 year ago

Here is the latest progress on finding a conda recipe that works with the coastseg environment. A common issue I've discovered is that the transformers library messes with the way the fiona library loads which means that geopandas can't load geojson files. I hypothesize this is due to the h5py library because when the h5py is uninstalled and reinstalled with conda forge everything works correctly.

conda create -n coastseg_transformers2 python=3.10 -y
conda activate coastseg_transformers2
pip install tensorflow 
conda install -c conda-forge transformers==4.29.2
pip install chardet 
conda install -c conda-forge geopandas -y 
conda install -c conda-forge jupyterlab -y 
pip install coastseg
pip uninstall h5py
conda install -c conda-forge h5py -y 
2320sharon commented 1 year ago

I'm testing on this environment on a windows 11 machine

2320sharon commented 1 year ago

I was able to get this recipe to work on a windows 11 machine. I noticed that it took a very long time to resolve conflicting dependencies at the conda install -c conda-forge geopandas -y step.

When I ran the notebook with the unet models it took about 4 times longer than normal to load in coastseg with the transformers.

2320sharon commented 1 year ago
2320sharon commented 1 year ago

Here is my latest recipe that worked. I'm going to be trying this out with the new models in #152

conda create -n coastseg_transformers7 python=3.10 -y
conda activate coastseg_transformers7
conda install -c conda-forge transformers==4.29.2 
conda install -c conda-forge geopandas jupyterlab -y 
pip install tensorflow chardet coastseg
pip uninstall h5py
conda install -c conda-forge h5py -y 
2320sharon commented 1 year ago

I was able to get the segformer_RGB_4class_7933015 to run in this environment

2320sharon commented 1 year ago

Dan was able to get this recipe to work on miniconda

conda create -n coastseg_transformers7 python=3.10 -y
conda activate coastseg_transformers7
conda install -c conda-forge transformers==4.29.2 
conda install -c conda-forge geopandas jupyterlab -y 
pip install tensorflow chardet coastseg
pip uninstall h5py
conda install -c conda-forge h5py -y 
pip uninstall transformers
pip install seg2map

We need to figure out what package is installing transformers from pip which is overriding the transformers we installed from conda

2320sharon commented 1 year ago

Dan is in the process of trying this recipe to simply the conda recipe

conda create -n coastseg_transformers8 python=3.10 -y
conda activate coastseg_transformers8
conda install -c conda-forge geopandas jupyterlab -y 
pip install tensorflow chardet coastseg
pip uninstall h5py
conda install -c conda-forge h5py -y 
pip uninstall transformers
pip install seg2map
2320sharon commented 1 year ago

I have confirmed that the tensorflow from pip does not install the transformers library

dbuscombe-usgs commented 1 year ago

I got this far

conda create -n coastseg_transformers8 python=3.10 -y
conda activate coastseg_transformers8
conda install -c conda-forge geopandas jupyterlab -y 
pip install tensorflow chardet coastseg
pip uninstall h5py
conda install -c conda-forge h5py -y 

then decided to test at this point to see what was installed. TF is installed: python -c "import tensorflow as tf" but transformers is not installed (python -c "import transformers"), so I confirm the observation that TF does not install transformers

At this point, I do not need the line pip uninstall transformers

Completing the installation with pip install seg2map, I can now test the transformers library with python -c "from transformers import TFSegformerForSemanticSegmentation". Success!!!

dbuscombe-usgs commented 1 year ago
conda create -n coastseg_transformers8 python=3.10 -y
conda activate coastseg_transformers8
conda install -c conda-forge geopandas jupyterlab -y 
pip install tensorflow chardet coastseg
pip uninstall h5py
conda install -c conda-forge h5py -y 
pip install seg2map

its dumb, but works. my only nervousness was that by installing seg2map after coastseg, we somehow messed up the coastseg functions, but a quick runthrough of the coastseg notebooks made me think that everything was ok. I was able to download images and extract shorelines using the coastsat classifier notebook, at least. and the unet notebook rendered ok, but I didnt test the model

2320sharon commented 1 year ago

Thank you for trying this out. This makes me think that we can add transformers as a dependency to coastseg in the pyproject.toml file because transformers doesn't automatically install tensorflow.

Here is what I'm going to try

  1. modify pyproect.toml and add transformers and chardet as new dependencies
    
    [build-system]
    requires = ["setuptools>=61.0"]
    build-backend = "setuptools.build_meta"

[project] name = "coastseg" dynamic = ["readme"] version = "0.0.70" authors = [ { name=" Sharon Fitzpatrick", email="sharon.fitzpatrick23@gmail.com" }, ]

finddirective withincludeorexclude`

description = "An interactive jupyter notebook for downloading satellite imagery" dependencies = [ "coastsat-package>=0.1.13", "area", "doodleverse-utils>=0.0.29", "ipyfilechooser>=0.6.0", "tqdm", "leafmap>=0.14.0", "geojson", "aiohttp", "nest-asyncio", "tensorflow", "dask", "ipywidgets>=8.0.6", "transformers", "chardet" ]

"pyTMD",

"xarray"]

license = { file="LICENSE" } requires-python = ">=3.8" classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3 :: Only", "Topic :: Scientific/Engineering :: GIS", ]

tells setup tools to include the code in the coastseg directory within the src directory

[tool.setuptools.packages.find] where = ["src"]

[project.urls] "Homepage" = "https://github.com/SatelliteShorelines/CoastSeg" "Bug Tracker" = "https://github.com/SatelliteShorelines/CoastSeg/issues"

2. Try out this conda recipe

conda create -n coastseg_transformers9 python=3.10 -y conda activate coastseg_transformers9 conda install -c conda-forge geopandas jupyterlab -y pip install coastseg tensorflow pip uninstall h5py conda install -c conda-forge h5py -y

edit: I messed up and didn't run pip install -e .
so after I installed this recipe I ran the following

cd coastseg pip install -e .



3. Try running 
`from transformers import TFSegformerForSemanticSegmentation`
4. This worked!!
2320sharon commented 1 year ago

I've got some great news! I was able to apply the segformer model segformer_RGB_4class_7933015 to the imagery in this environment! It's pretty fast too!

2320sharon commented 1 year ago

I'm going to double check then make these changes official

2320sharon commented 1 year ago

I tried this conda recipe on the add_new_segformer_model branch and it worked.

I already modified the pyproject.toml file so all you should need to do is

git fetch
git switch add_new_segformer_model
conda create -n coastseg_transformers9 python=3.10 -y
conda activate coastseg_transformers9
conda install -c conda-forge geopandas jupyterlab -y 
pip install  tensorflow
cd coastseg
pip install -e .
pip uninstall h5py
conda install -c conda-forge h5py -y 

Then select the segformer model from under the RGB section

2320sharon commented 1 year ago

The new segformer model has been added and the installation instructions have been finalized. These changes are active in coastseg==0.0.72.dev6