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

Add object detection sample #228

Merged
merged 6 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 4 additions & 1 deletion PyTorch/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ traces/
*.xlsx
data/cifar-10-python
checkpoints
coco128
coco128
data/PennFudanPed
data/PennFudanPed.zip
objectDetection/PennFudanPed
1 change: 1 addition & 0 deletions PyTorch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The following sample models are included in this repo to help you get started. T

* [squeezenet - a small image classification model](./squeezenet)
* [resnet50 - an image classification model](./resnet50)
* [maskrcnn - an object detection model](./objectDetection/maskrcnn/)
* *more coming soon*

## External Links
Expand Down
24 changes: 0 additions & 24 deletions PyTorch/data/cifar.py

This file was deleted.

47 changes: 47 additions & 0 deletions PyTorch/data/dataset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python
# Copyright (c) Microsoft Corporation. All rights reserved.

import argparse
import pathlib
import os
from torchvision import datasets
import wget
import zipfile

def get_current_dir():
return str(pathlib.Path(__file__).parent.resolve())

def download_cifar_dataset():
path = os.path.join(get_current_dir(), 'cifar-10-python')
datasets.CIFAR10(root=path, download=True)

def download_pennfudanped_dataset():
path = get_current_dir()
if (os.path.exists(os.path.join(path, 'PennFudanPed'))):
print ("PennFundaPed dataset already downloaded and verified")
return

url='https://www.cis.upenn.edu/~jshi/ped_html/PennFudanPed.zip'
print("Downloading PennFundaPed dataset\n")
dataset_path = wget.download(url, out=path)
try:
with zipfile.ZipFile(os.path.join(path, dataset_path)) as z:
z.extractall(path=path)
print("\nExtracted PennFundaPed dataset")
except:
print("Invalid file")

if __name__ == "__main__":
parser = argparse.ArgumentParser(__doc__)
parser.add_argument("--dataset", help="datasets: cifar10 or pennfudanped.", default="all")
args = parser.parse_args()

if args.dataset.lower() == 'all':
download_cifar_dataset()
download_pennfudanped_dataset()
elif args.dataset.lower() == 'cifar10':
download_cifar_dataset()
elif args.dataset.lower() == 'pennfudanped':
download_pennfudanped_dataset()
else:
raise Exception(f"Model {args.dataset} is not supported yet!")
51 changes: 51 additions & 0 deletions PyTorch/objectDetection/maskrcnn/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# maskrcnn Model <!-- omit in toc -->

Sample scripts for training the [Mask R-CNN](https://arxiv.org/abs/1703.06870) model in the [Penn-Fudan Database for Pedestrian Detection and Segmentation](https://www.cis.upenn.edu/~jshi/ped_html/) using PyTorch on DirectML

These scripts are collected from the tutorial [here](https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html)

- [Setup](#setup)
- [Prepare Data](#prepare-data)
- [Training](#training)

## Setup
Install the following prerequisites:
```
pip install -r pytorch\objectDetection\maskrcnn\requirements.txt
```

## Prepare Data

After installing the PyTorch on DirectML package (see [GPU accelerated ML training](..\readme.md)), open a console to the `root` directory and run the setup script to download and convert data:

```
python pytorch\data\dataset.py
```

Running `dataset.py` should take at least a minute or so, since it downloads the CIFAR-10 dataset. The output of running it should look similar to the following:

```
>python pytorch\data\dataset.py
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to E:\work\dml\PyTorch\data\cifar-10-python\cifar-10-python.tar.gz
Failed download. Trying https -> http instead. Downloading http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to E:\work\dml\PyTorch\data\cifar-10-python\cifar-10-python.tar.gz
170499072it [00:32, 5250164.09it/s]
Extracting E:\work\dml\PyTorch\data\cifar-10-python\cifar-10-python.tar.gz to E:\work\dml\PyTorch\data\cifar-10-python
```

## Training

A helper script exists to train Mask R-CNN with PennFudanPed data:

```
cd pytorch\objectdetection\maskrcnn
python .\maskrcnn.py
```

The first few lines of output should look similar to the following (exact numbers may change):
```
>python .\maskrcnn.py
python .\maskrcnn.py
Epoch: [0] [ 0/60] eta: 0:38:26 lr: 0.000090 loss: 2.9777 (2.9777) loss_classifier: 0.7217 (0.7217) loss_box_reg: 0.0754 (0.0754) loss_mask: 1.6228 (1.6228) loss_objectness: 0.4175 (0.4175) loss_rpn_box_reg: 0.1404 (0.1404) time: 38.4439 data: 1.0955
Epoch: [0] [10/60] eta: 0:29:44 lr: 0.000936 loss: 2.4268 (2.4919) loss_classifier: 0.4056 (0.4158) loss_box_reg: 0.1691 (0.3631) loss_mask: 1.1679 (1.1600) loss_objectness: 0.1162 (0.3120) loss_rpn_box_reg: 0.1257 (0.2410) time: 35.6972 data: 0.1034
Epoch: [0] [20/60] eta: 0:23:14 lr: 0.001783 loss: 1.2172 (1.6717) loss_classifier: 0.0669 (0.2410) loss_box_reg: 0.1331 (0.2466) loss_mask: 0.5935 (0.8376) loss_objectness: 0.0565 (0.1873) loss_rpn_box_reg: 0.0574 (0.1593) time: 34.6860 data: 0.0042
```
Loading