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

Jb/july24 #148

Merged
merged 63 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
1210e48
parallelise tiling
PatBall1 Jul 9, 2024
2eda758
parallelise tiling
PatBall1 Jul 10, 2024
7f2a6c7
update readme
PatBall1 Jul 10, 2024
4e10abe
test partial reading of raster
PatBall1 Jul 16, 2024
1ded0d8
test partial reading of raster
PatBall1 Jul 16, 2024
7c42140
add logger for tiling
PatBall1 Jul 16, 2024
aa8ab37
change back to masking
PatBall1 Jul 16, 2024
5320a50
change back to masking
PatBall1 Jul 16, 2024
8e5c68a
change back to masking
PatBall1 Jul 16, 2024
3fd5799
img_path instead of reader for multithread
PatBall1 Jul 16, 2024
93f2c18
img_path instead of reader for multithread
PatBall1 Jul 16, 2024
617b733
img_path instead of reader for multithread
PatBall1 Jul 16, 2024
24b5d74
explode reintroduced
PatBall1 Jul 16, 2024
bb63e3c
refactor tile processing
PatBall1 Jul 16, 2024
b422916
refactor tiling
PatBall1 Jul 17, 2024
c226750
refactor tiling
PatBall1 Jul 17, 2024
eb163be
ms tiling
PatBall1 Aug 8, 2024
6441ae7
tiling mode added
PatBall1 Aug 9, 2024
dc8e0a3
remove lfs
PatBall1 Aug 9, 2024
e71135e
ms bands
PatBall1 Aug 20, 2024
a5e7088
ms bands
PatBall1 Aug 20, 2024
2042c48
ms bands
PatBall1 Aug 20, 2024
f3d1c89
ms bands
PatBall1 Aug 21, 2024
c8803bd
dataset mapper updates
PatBall1 Aug 22, 2024
8b417e8
ms training
PatBall1 Aug 23, 2024
f02fdb0
inhertited processes
PatBall1 Aug 26, 2024
925c3de
inhertited processes
PatBall1 Aug 26, 2024
5027a49
augmentations scaling
PatBall1 Aug 28, 2024
065ad16
augmentations scaling
PatBall1 Aug 28, 2024
5682e75
ms functions
PatBall1 Sep 2, 2024
b5f6216
tutorial updates
PatBall1 Sep 3, 2024
3d16378
tutorial updates
PatBall1 Sep 3, 2024
148cb50
multiclass
PatBall1 Sep 6, 2024
40beaaf
multiclass
PatBall1 Sep 6, 2024
774a15f
multiclass
PatBall1 Sep 6, 2024
993fedc
tutorial updates
PatBall1 Sep 7, 2024
65921f9
classes fix
PatBall1 Sep 9, 2024
92b0853
classes cfg
PatBall1 Sep 9, 2024
d98f69f
class handling
PatBall1 Sep 9, 2024
555cf06
class handling
PatBall1 Sep 9, 2024
7fadcc7
test loader
PatBall1 Sep 9, 2024
516d38f
class columns reduced
PatBall1 Sep 16, 2024
09ae19a
class columns reduced
PatBall1 Sep 16, 2024
db5c4b8
class columns reduced
PatBall1 Sep 16, 2024
acc90e3
tutorial updates
PatBall1 Sep 16, 2024
a6ccbb9
tutorial updates
PatBall1 Sep 16, 2024
f26e251
update test
PatBall1 Sep 16, 2024
0fea6b4
update test
PatBall1 Sep 16, 2024
f9c06de
update test
PatBall1 Sep 16, 2024
77b5f2e
mypy
PatBall1 Sep 16, 2024
31d4000
mypy
PatBall1 Sep 16, 2024
e753c1a
mypy
PatBall1 Sep 16, 2024
bcc7344
mypy
PatBall1 Sep 16, 2024
534e530
mypy
PatBall1 Sep 16, 2024
c569c7c
mypy
PatBall1 Sep 16, 2024
39d5b1c
mypy
PatBall1 Sep 16, 2024
7fdf072
mypy
PatBall1 Sep 16, 2024
9061812
mypy
PatBall1 Sep 16, 2024
c9c6765
mypy
PatBall1 Sep 16, 2024
fc1cfe0
isort
PatBall1 Sep 16, 2024
dfe68e2
flake8
PatBall1 Sep 17, 2024
7a174ca
tutorial
PatBall1 Sep 17, 2024
1c436c3
pep8
PatBall1 Sep 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
*.pth filter=lfs diff=lfs merge=lfs -text
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@
<!-- <a href="https://github.com/hhatto/autopep8"><img alt="Code style: autopep8" src="https://img.shields.io/badge/code%20style-autopep8-000000.svg"></a> -->


Python package for automatic tree crown delineation based on Mask R-CNN. Pre-trained models can be picked in the [`model_garden`](https://github.com/PatBall1/detectree2/tree/master/model_garden).
A tutorial on how to prepare data, train models and make predictions is available [here](https://patball1.github.io/detectree2/tutorial.html). For questions, collaboration proposals and requests for data email [James Ball](mailto:ball.jgc@gmail.com). Some example data is available for download [here](https://doi.org/10.5281/zenodo.8136161).
Python package for automatic tree crown delineation in aerial RGB and multispectral imagery based on Mask R-CNN. Pre-trained models can be picked in the [`model_garden`](https://github.com/PatBall1/detectree2/tree/master/model_garden).
A tutorial on how to prepare data, train models and make predictions is available [here](https://patball1.github.io/detectree2/tutorial.html). For questions, collaboration proposals and requests for data email [James Ball](mailto:ball.jgc@gmail.com). Some example data is available to download [here](https://doi.org/10.5281/zenodo.8136161).

Detectree2是一个基于Mask R-CNN的自动树冠检测与分割的Python包。您可以在[`model_garden`](https://github.com/PatBall1/detectree2/tree/master/model_garden)中选择预训练模型。[这里](https://patball1.github.io/detectree2/tutorial.html)提供了如何准备数据、训练模型和进行预测的教程。如果有任何问题,合作提案或者需要样例数据,可以邮件联系[James Ball](mailto:ball.jgc@gmail.com)。一些示例数据可以在[这里](https://doi.org/10.5281/zenodo.8136161)下载。

| <a href="https://www.conservation.cam.ac.uk/"><img src="./report/cam_logo.png" width="140"></a> | <sup> Code developed by James Ball, Seb Hickman, Thomas Koay, Oscar Jiang, Luran Wang, Panagiotis Ioannou, James Hinton and Matthew Archer in the [Forest Ecology and Conservation Group](https://coomeslab.org/) at the University of Cambridge. The Forest Ecology and Conservation Group is led by Professor David Coomes and is part of the University of Cambridge [Conservation Research Institute](https://www.conservation.cam.ac.uk/). </sup>|
| :---: | :--- |

<br/><br/>
> [!NOTE]
> To save bandwidth, trained models have been moved to [Zenodo](https://zenodo.org/records/10522461). Download models directly with `wget` or equivalent.


## Citation

Expand Down
74 changes: 74 additions & 0 deletions detectree2/data_loading/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import cv2
import detectron2.data.transforms as T
import numpy as np
import rasterio
import torch
from detectron2.structures import BitMasks, BoxMode, Instances
from torch.utils.data import Dataset


class CustomTIFFDataset(Dataset):
def __init__(self, annotations, transforms=None):
"""
Args:
annotations (list): List of dictionaries containing image file paths and annotations.
transforms (callable, optional): Optional transform to be applied on a sample.
"""
self.annotations = annotations
self.transforms = transforms

def __len__(self):
return len(self.annotations)

def __getitem__(self, idx):
# Load the TIFF image
img_info = self.annotations[idx]
with rasterio.open(img_info['file_name']) as src:
# Read all bands (assuming they are all needed)
image = src.read()
# Normalize or rescale if necessary
image = image.astype(np.float32) / 255.0 # Example normalization
# If the number of bands is not 3, reduce to 3 or handle accordingly
#if image.shape[0] > 3:
# image = image[:3, :, :] # Taking the first 3 bands (e.g., RGB)
# Convert to HWC format expected by Detectron2
#image = np.transpose(image, (1, 2, 0))

# Prepare annotations (this part needs to be adapted to your specific annotations)
target = {
"image_id": idx,
"annotations": img_info["annotations"],
"width": img_info["width"],
"height": img_info["height"],
}

if self.transforms is not None:
augmentations = T.AugmentationList(self.transforms)
image, target = augmentations(image, target)

# Convert to Detectron2-compatible format
image = torch.as_tensor(image.astype("float32").transpose(2, 0, 1))
instances = self.get_detectron_instances(target)

return image, instances

def get_detectron_instances(self, target):
"""
Converts annotations into Detectron2's format.
This example assumes annotations are in COCO format, and you'll need to adapt it for your needs.
"""
boxes = [obj["bbox"] for obj in target["annotations"]]
boxes = torch.as_tensor(boxes, dtype=torch.float32)
boxes = BoxMode.convert(boxes, BoxMode.XYWH_ABS, BoxMode.XYXY_ABS)

# Create BitMasks from the binary mask data (assuming the mask is a binary numpy array)
masks = [obj["segmentation"] for obj in target["annotations"]] # Replace with actual mask loading
masks = BitMasks(torch.stack([torch.from_numpy(mask) for mask in masks]))

instances = Instances(
image_size=(target["height"], target["width"]),
gt_boxes=boxes,
gt_classes=torch.tensor([obj["category_id"] for obj in target["annotations"]], dtype=torch.int64),
gt_masks=masks
)
return instances
Loading
Loading