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

added detectree example notebook #4

Merged
merged 10 commits into from
Oct 11, 2021
Merged

Conversation

shmh40
Copy link
Contributor

@shmh40 shmh40 commented Sep 10, 2021

To do:

  • Read in model weights with intake (is this possible @acocac ?
  • Provide graphical interface with leafmap so users can see predictions and tiff geographically
  • Small tidy possible - extend summary, move comments to markdown

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@shmh40
Copy link
Contributor Author

shmh40 commented Sep 11, 2021

  • Updated notebook with interactive leafmap
  • This currently works for shapefiles, but I cannot make it work for tiffs, even using the example from the documentation

@acocac acocac self-requested a review September 14, 2021 09:08
@shmh40
Copy link
Contributor Author

shmh40 commented Sep 14, 2021

Added code so forest-modelling-detectree-v2 will run on cpu

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:18Z
----------------------------------------------------------------

Great description! I would add the hyperlink to Mask R-CNN and mention it's available through the detectron2 framework.


shmh40 commented on 2021-09-23T09:29:43Z
----------------------------------------------------------------

Added hyperlink

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:19Z
----------------------------------------------------------------

There are some libraries that the notebook doesn't longer need and others I added for interactive plotting. I would suggest to replace the original cells of this section by the following ones:

Cell 1:
## install detectron2
!python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'

install geospatial libraries

!pip -q install geopandas
!pip -q install rasterio
!pip -q install fiona
!pip -q install shapely

install intake plugins

!pip -q install git+https://github.com/ESM-VFC/intake_zenodo_fetcher.git

Cell 2:
import cv2
from PIL import Image
import os
import numpy as np
import urllib.request
import glob

intake library and plugin

import intake
from intake_zenodo_fetcher import download_zenodo_files_for_entry

geospatial libraries

import geopandas as gpd

from rasterio.transform import from_origin
import rasterio.features

import fiona

from shapely.geometry import shape, mapping, box
from shapely.geometry.multipolygon import MultiPolygon

machine learning libraries

from detectron2 import model_zoo
from detectron2.engine import DefaultPredictor
from detectron2.utils.visualizer import Visualizer, ColorMode
from detectron2.config import get_cfg

visualisation

import holoviews as hv
from IPython.display import display
import geoviews.tile_sources as gts

import hvplot.pandas
import hvplot.xarray

hv.extension('bokeh', width=100)



shmh40 commented on 2021-09-23T09:31:15Z
----------------------------------------------------------------

Agreed

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:20Z
----------------------------------------------------------------

remove


shmh40 commented on 2021-09-23T09:31:04Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:20Z
----------------------------------------------------------------

remove


shmh40 commented on 2021-09-23T09:31:27Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:21Z
----------------------------------------------------------------

remove


shmh40 commented on 2021-09-23T09:31:41Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:21Z
----------------------------------------------------------------

remove


shmh40 commented on 2021-09-23T09:31:54Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:22Z
----------------------------------------------------------------

Before the Load data section, I would provide a sort of project folder structure (a python dictionary) into a new section Set Folder Structure. I provide the cell to add into this new suggestion.

# Define the project main folder
data_folder = './detectree'

Set the folder structure

config = {
'in_geotiff': os.path.join(data_folder, 'input','tiff'),
'in_png': os.path.join(data_folder, 'input','png'),
'model': os.path.join(data_folder, 'model'),
'out_geotiff': os.path.join(data_folder, 'output','raster'),
'out_shapefile': os.path.join(data_folder, 'output','vector'),
}

List comprehension for the folder structure code

[os.makedirs(val) for key, val in config.items() if not os.path.exists(val)]



shmh40 commented on 2021-09-23T09:33:16Z
----------------------------------------------------------------

Done - thank you

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:23Z
----------------------------------------------------------------

remove as we install intake zenodo fetcher plugin in the Install and load libraries section. Note we don't need to install intake and intake-array as they're already available in environment.yml file of the Environmental AI book.


@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:23Z
----------------------------------------------------------------

remove as we are importing intake and intake zenodo in the Install and load libraries section. Also, we aren't longer using tempfile due to the proposed folder structure.


@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:24Z
----------------------------------------------------------------

Line #1.    # create a temp dir

remove


shmh40 commented on 2021-09-23T09:58:22Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:25Z
----------------------------------------------------------------

Line #2.    path = tempfile.mkdtemp()

remove


@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:25Z
----------------------------------------------------------------

Line #3.    

add

# write a catalog YAML file



shmh40 commented on 2021-09-23T09:58:13Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:26Z
----------------------------------------------------------------

Line #4.    catalog_file = os.path.join(path, 'catalog.yaml')

change:

catalog_file = os.path.join(data_folder, 'catalog.yaml')

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:26Z
----------------------------------------------------------------

Line #15.          urlpath: "{{ CATALOG_DIR }}/Sep_2014_RGB_602500_646600.tif"

change:

urlpath: "{{ CATALOG_DIR }}/input/tiff/Sep_2014_RGB_602500_646600.tif"

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:27Z
----------------------------------------------------------------

Line #1.    # let's investigate the data-array, what is the shape? Bounds? Bands? CRS?

move this text into the markdown of the subheading,

Let's investigate the data-array object, what is the shape? Bounds? Bands? CRS? 

shmh40 commented on 2021-09-23T09:58:01Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:28Z
----------------------------------------------------------------

add some additional text below the subsection heading, for example:

The Mask R-CNN requires images in png format. Let's export the RGB bands to a png file.

shmh40 commented on 2021-09-23T09:57:54Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:28Z
----------------------------------------------------------------

Line #15.    cv2.imwrite('/home/jovyan/lustre_scratch/sepilok_data/spare_pngs/tile_'+str(minx)+'_'+str(miny)+'.png', rgb_rescaled)

change to:

filepath = config['in_png'] + '/' + 'tile_' + str(minx) + '_' + str(miny) + '.png'

cv2.imwrite(filepath, rgb_rescaled)



shmh40 commented on 2021-09-23T09:57:47Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:29Z
----------------------------------------------------------------

Line #1.    filepath = "/home/jovyan/lustre_scratch/sepilok_data/spare_pngs/tile_602500_646600.png"

remove


shmh40 commented on 2021-09-23T09:57:27Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:29Z
----------------------------------------------------------------

Before this section, we can add a new subsection Download the pretrained model with the following cell:

# define the URL to retrieve the model
fn = 'model_final.pth'
url = f'https://zenodo.org/record/5515408/files/{fn}?download=1'

urllib.request.urlretrieve(url, config['model'] + '/' + fn)



shmh40 commented on 2021-09-23T09:57:19Z
----------------------------------------------------------------

Nice

acocac commented on 2021-09-23T14:45:53Z
----------------------------------------------------------------

(:

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:30Z
----------------------------------------------------------------

Line #1.    from detectron2.utils.visualizer import ColorMode

remove


@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:31Z
----------------------------------------------------------------

Line #2.    from detectron2.engine import DefaultTrainer

remove


@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:31Z
----------------------------------------------------------------

Line #3.    from detectron2.config import get_cfg

remove


@review-notebook-app
Copy link

review-notebook-app bot commented Sep 21, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-21T09:35:32Z
----------------------------------------------------------------

Line #17.    cfg.MODEL.WEIGHTS = '/home/jovyan/detectron2_live/train_test/output/model_final.pth'

change to:

cfg.MODEL.WEIGHTS = config['model'] + '/model_final.pth'



Copy link
Contributor Author

shmh40 commented Sep 23, 2021

Done


View entire conversation on ReviewNB

@shmh40
Copy link
Contributor Author

shmh40 commented Sep 23, 2021

v4 of the notebook now uploaded with desired changes and some minor amendments to mapping - added code to save the interactive map.

Copy link
Member

acocac commented Sep 23, 2021

It seems there are some version problems with recent pytorch and detectron2 installed via !python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'. I would suggest to add an extra line above ## install detectron2:

# install pytorch
!pip -q install torch==1.8.0 
!pip -q install torchvision==0.9.0


View entire conversation on ReviewNB

Copy link
Member

acocac commented Sep 23, 2021

(:


View entire conversation on ReviewNB

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 23, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-23T14:57:34Z
----------------------------------------------------------------

It seems there are some version problems with recent pytorch and detectron2 installed via !python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'. I would suggest to add an extra line above ## install detectron2:

# install pytorch
!pip -q install torch==1.8.0 
!pip -q install torchvision==0.9.0

shmh40 commented on 2021-10-09T11:58:10Z
----------------------------------------------------------------

Done

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 23, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-23T14:57:34Z
----------------------------------------------------------------

Line #10.    plot_rgb = int_arr.astype('uint8').hvplot.rgb(

It might worth to investigate how you can change the info displayed in the hover.... can we add some info from the polygons e.g. probability?


shmh40 commented on 2021-10-09T12:33:00Z
----------------------------------------------------------------

Done!

@review-notebook-app
Copy link

review-notebook-app bot commented Sep 23, 2021

View / edit / reply to this conversation on ReviewNB

acocac commented on 2021-09-23T14:57:35Z
----------------------------------------------------------------

It might be a good to mention why you export as HTML and not PNG. For instance, you can say:

Let's export the combined map to a HTML file, this allows to keep the interactive plotting in a separate window.

shmh40 commented on 2021-10-09T11:57:42Z
----------------------------------------------------------------

Done

@acocac
Copy link
Member

acocac commented Sep 23, 2021

@shmh40 thanks for implementing the suggestions. I just made a minor comments.
FYI, You don't need to create a new notebook file for each version. Please make the minor changes on forest-modelling-detectree-v4.ipynb . Let me know if you succeed to explore further the hover of hvplot. Otherwise, the notebook is ready to publish!

Copy link
Contributor Author

shmh40 commented Oct 9, 2021

Done


View entire conversation on ReviewNB

1 similar comment
Copy link
Contributor Author

shmh40 commented Oct 9, 2021

Done


View entire conversation on ReviewNB

@shmh40
Copy link
Contributor Author

shmh40 commented Oct 9, 2021

Made final changes, and added function to see confidence score in hvplot. Good to go!

Copy link
Contributor Author

shmh40 commented Oct 9, 2021

Done!


View entire conversation on ReviewNB

Copy link
Contributor Author

shmh40 commented Oct 9, 2021

All changes complete!

@acocac acocac merged commit 786c3ef into alan-turing-institute:master Oct 11, 2021
@acocac
Copy link
Member

acocac commented Oct 11, 2021

Thanks @shmh40, it was great to have your contribution for the forest domain. I hope you learnt some useful python packages for handling geospatial datasets. I made some minor style adjustments to the notebook and now it's good to go (:

@acocac acocac added this to Published in Notebooks Nov 17, 2021
@acocac acocac modified the milestone: 0.01 Dec 2, 2021
@acocac acocac added notebook Add/update notebook ready labels Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
medium priority modelling Modelling Notebooks notebook Add/update notebook ready
Projects
Notebooks
Published
Development

Successfully merging this pull request may close these issues.

None yet

2 participants