Skip to content

Commit 1e65d33

Browse files
Gorkem-Encordeloy-encordEncord-davids
authored
Fix (maskrcnn-example): improvements on maskrcnn-example (#89)
* feat: make training log optional * fix: skip non-positive width/height * Update examples/maskrcnn-example/utils/encord_dataset.py Co-authored-by: Eloy Pérez Torres <[email protected]> Signed-off-by: Gorkem Polat <[email protected]> * feat: use logging to give message to the user * fix: formatting * Feat (maskrcnn-example): improving labels workflow (#92) * feat: add label improvement workflow * fix: typo * fix: add more explanation * fix: check if there answers before accessing the first one (#96) * fix: check if there answers before accessing the first one * fix: generate embedding only if needed and ignore checkbox classifications * fix: don't run irrelevant object metrics Signed-off-by: Gorkem Polat <[email protected]> Co-authored-by: David Sapiro <[email protected]> * Feat (maskrcnn-example): Save last epoch (#93) * feat: add label improvement workflow * fix: typo * fix: add more explanation * feat: Add saving last epoch * feat: maskrcnn nms argument * fix: black formattiing Signed-off-by: Gorkem Polat <[email protected]> Co-authored-by: Eloy Pérez Torres <[email protected]> Co-authored-by: David Sapiro <[email protected]>
1 parent 9b9ac0c commit 1e65d33

File tree

5 files changed

+84
-8
lines changed

5 files changed

+84
-8
lines changed

examples/maskrcnn-example/readme.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ conda activate encord-maskrcnn
2525
```
2626

2727
## Training
28-
1. Create a config.ini file by looking at the example_config.ini
29-
2. You can use resize the imagens and corresponding annotations via utils/downscle_dataset.py.
30-
2. For the training, the only required fields are [DATA], [LOGGING], and [TRAIN] sections
31-
3. Activate the environment and run `python train.py`
32-
4. You can track the progress of the training on wandb platformdd
28+
1. You can use Encord-Active's Actions tab to create COCO annotations.
29+
2. Create a config.ini file by looking at the example_config.ini
30+
3. You can resize the images and corresponding annotations via utils/downscale_dataset.py.
31+
4. For the training, the only required fields are [DATA], [LOGGING], and [TRAIN] sections
32+
5. Activate the environment and run `python train.py`
33+
6. You can track the progress of the training on wandb platform.
3334

3435

3536
## Importing Encord-Active predictions
@@ -42,4 +43,4 @@ conda activate encord-maskrcnn
4243
encord-active import predictions /path/to/predictions.pkl -t /path/to/project
4344
```
4445

45-
5. Now you can see the model performance on the __Model Quality__ tab.
46+
5. Open the app. Now you can see the model performance on the __Model Quality__ tab.

examples/maskrcnn-example/train.py

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def main(params):
5858
setup_reproducibility(35)
5959

6060
best_map = 0
61+
last_epoch = 0
6162
early_stop_counter = 0
6263

6364
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
@@ -123,6 +124,7 @@ def main(params):
123124
val_map_metric = MeanAveragePrecision(iou_type="segm").to(device)
124125

125126
for epoch in range(params.train.max_epoch):
127+
last_epoch = epoch
126128
print(f"Epoch: {epoch}")
127129
train_one_epoch(model, device, data_loader, optimizer, log_freq=10)
128130

@@ -170,6 +172,14 @@ def main(params):
170172
print("Early stopping at: " + str(epoch))
171173
break
172174

175+
if params.logging.wandb_enabled:
176+
torch.save(
177+
model.state_dict(),
178+
os.path.join(wandb.run.dir, f"epoch_{last_epoch}_maskrcnn.ckpt"),
179+
)
180+
else:
181+
torch.save(model.state_dict(), f"weights/epoch_{last_epoch}_maskrcnn.ckpt")
182+
173183
print("Training finished")
174184

175185

examples/maskrcnn-example/utils/encord_dataset.py

+12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from pathlib import Path
33

44
sys.path.append(Path(__file__).parent.as_posix())
5+
import logging
6+
57
import torch
68
import torchvision
79
from provider import convert_coco_poly_to_mask
@@ -21,6 +23,16 @@ def __getitem__(self, idx):
2123

2224
boxes, labels, area, iscrowd = [], [], [], []
2325
for target_item in target:
26+
if target_item["bbox"][2] <= 0 or target_item["bbox"][3] <= 0:
27+
logging.warning(
28+
f"ERROR: Target bbox for the following image \n"
29+
f'title: {img_metadata[0]["image_title"]} \n'
30+
f'label_hash: {img_metadata[0]["label_hash"]} \n'
31+
f'data_hash: {img_metadata[0]["data_hash"]} \n'
32+
f'has non-positive width/height => [x,y,w,h]: {target_item["bbox"]}. \n'
33+
f"Therefore, skipping this annotation."
34+
)
35+
continue
2436
boxes.append(
2537
[
2638
target_item["bbox"][0],

examples/maskrcnn-example/utils/model_libs.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
from torchvision.models.detection.mask_rcnn import MaskRCNNPredictor
55

66

7-
def get_model_instance_segmentation(num_classes, fine_tuning=False):
7+
def get_model_instance_segmentation(num_classes, fine_tuning=False, box_nms_thresh=0.5):
88
# load an instance segmentation model pre-trained on COCO
9-
model = torchvision.models.detection.maskrcnn_resnet50_fpn(weights=MaskRCNN_ResNet50_FPN_Weights.DEFAULT)
9+
model = torchvision.models.detection.maskrcnn_resnet50_fpn(
10+
weights=MaskRCNN_ResNet50_FPN_Weights.DEFAULT, box_nms_thresh=box_nms_thresh
11+
)
1012

1113
# get number of input features for the classifier
1214
in_features = model.roi_heads.box_predictor.cls_score.in_features
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Improving the labels using Encord-Active
2+
3+
1. Copy the project on the Encord-Annotate platform (we do not want to lose the original labels).
4+
5+
On Encord-Annotate, open the project, go to Settings => Copy project. Select everything (labels, images etc.) when copying the project.
6+
7+
2. Import the new project to Encord-Active.
8+
9+
See documentation: https://encord-active-docs.web.app/cli/import-encord-project
10+
11+
3. Open the Encord-Active app.
12+
13+
```shell
14+
(encord-active-environment) > encord-active visualise
15+
```
16+
17+
4. Go to **Label Quality => Explore**, and choose **Object annotation quality** metric.
18+
19+
5. Fix labels via Encord Annotate by clicking the editor button.
20+
21+
6. Once the fixing operation is finished, the local project should be updated again because labels have changed
22+
on the Encord Annotate side. So delete the local project and import it again.
23+
24+
7. Open the App. Go to **Actions => Filter & Export**, click **Generate COCO file** and when activated
25+
click **Download filtered data** to download COCO file.
26+
27+
8. Fill in **config.ini** file.
28+
29+
9. Train the model
30+
31+
```shell
32+
# inside project root folder
33+
(encord-maskrcnn) > python train.py
34+
```
35+
36+
10. Import predictions. Check the readme.md file for detailed instructions of importing Encord predictions.
37+
38+
```shell
39+
(encord-maskrcnn) > python generate_ea_predictions.py
40+
```
41+
42+
```shell
43+
(encord-active-environment) > encord-active import predictions /path/to/pickle/file -t /path/to/project
44+
```
45+
46+
11. Open the app and check the **Model quality** tab for the details.
47+
48+
49+
50+
51+

0 commit comments

Comments
 (0)