Skip to content

Commit cb679b0

Browse files
authored
Merge pull request aws#315 from xzhou33/master
Adding object detection notebook
2 parents 3d6ba23 + d619e96 commit cb679b0

File tree

9 files changed

+2152
-2
lines changed

9 files changed

+2152
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ These examples provide quick walkthroughs to get you up and running with Amazon
4343
- [XGBoost for multi-class classification](introduction_to_amazon_algorithms/xgboost_mnist) uses Amazon SageMaker's implementation of [XGBoost](https://github.com/dmlc/xgboost) to classify handwritten digits from the MNIST dataset as one of the ten digits using a multi-class classifier. Both single machine and distributed use-cases are presented.
4444
- [DeepAR for time series forecasting](introduction_to_amazon_algorithms/deepar_synthetic) illustrates how to use the Amazon SageMaker DeepAR algorithm for time series forecasting on a synthetically generated data set.
4545
- [BlazingText Word2Vec](introduction_to_amazon_algorithms/blazingtext_word2vec_text8) generates Word2Vec embeddings from a cleaned text dump of Wikipedia articles using SageMaker's fast and scalable BlazingText implementation.
46+
- [Object Detection](introduction_to_amazon_algorithms/object_detection_pascalvoc_coco) illustrates how to train an object detector using the Amazon SageMaker Object Detection algorithm with different input formats (RecordIO and image).
4647

4748
### Scientific Details of Algorithms
4849

@@ -65,8 +66,8 @@ These examples that showcase unique functionality available in Amazon SageMaker.
6566
- [Bring Your Own R Algorithm](advanced_functionality/r_bring_your_own) shows how to bring your own algorithm container to Amazon SageMaker using the R language.
6667
- [Installing the R Kernel](advanced_functionality/install_r_kernel) shows how to install the R kernel into an Amazon SageMaker Notebook Instance.
6768
- [Bring Your Own scikit Algorithm](advanced_functionality/scikit_bring_your_own) provides a detailed walkthrough on how to package a scikit learn algorithm for training and production-ready hosting.
68-
- [Bring Your Own MXNet Model](advanced_functionality/mxnet_mnist_byom) shows how to bring a model trained anywhere using MXNet into Amazon SageMaker
69-
- [Bring Your Own TensorFlow Model](advanced_functionality/tensorflow_iris_byom) shows how to bring a model trained anywhere using TensorFlow into Amazon SageMaker
69+
- [Bring Your Own MXNet Model](advanced_functionality/mxnet_mnist_byom) shows how to bring a model trained anywhere using MXNet into Amazon SageMaker.
70+
- [Bring Your Own TensorFlow Model](advanced_functionality/tensorflow_iris_byom) shows how to bring a model trained anywhere using TensorFlow into Amazon SageMaker.
7071

7172
### Amazon SageMaker Pre-Built Deep Learning Framework Containers and the Python SDK
7273

introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/object_detection_image_json_format.ipynb

Lines changed: 676 additions & 0 deletions
Large diffs are not rendered by default.

introduction_to_amazon_algorithms/object_detection_pascalvoc_coco/object_detection_recordio_format.ipynb

Lines changed: 526 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
from imdb import Imdb
19+
import random
20+
21+
class ConcatDB(Imdb):
22+
"""
23+
ConcatDB is used to concatenate multiple imdbs to form a larger db.
24+
It is very useful to combine multiple dataset with same classes.
25+
Parameters
26+
----------
27+
imdbs : Imdb or list of Imdb
28+
Imdbs to be concatenated
29+
shuffle : bool
30+
whether to shuffle the initial list
31+
"""
32+
def __init__(self, imdbs, shuffle):
33+
super(ConcatDB, self).__init__('concatdb')
34+
if not isinstance(imdbs, list):
35+
imdbs = [imdbs]
36+
self.imdbs = imdbs
37+
self._check_classes()
38+
self.image_set_index = self._load_image_set_index(shuffle)
39+
40+
def _check_classes(self):
41+
"""
42+
check input imdbs, make sure they have same classes
43+
"""
44+
try:
45+
self.classes = self.imdbs[0].classes
46+
self.num_classes = len(self.classes)
47+
except AttributeError:
48+
# fine, if no classes is provided
49+
pass
50+
51+
if self.num_classes > 0:
52+
for db in self.imdbs:
53+
assert self.classes == db.classes, "Multiple imdb must have same classes"
54+
55+
def _load_image_set_index(self, shuffle):
56+
"""
57+
get total number of images, init indices
58+
59+
Parameters
60+
----------
61+
shuffle : bool
62+
whether to shuffle the initial indices
63+
"""
64+
self.num_images = 0
65+
for db in self.imdbs:
66+
self.num_images += db.num_images
67+
indices = list(range(self.num_images))
68+
if shuffle:
69+
random.shuffle(indices)
70+
return indices
71+
72+
def _locate_index(self, index):
73+
"""
74+
given index, find out sub-db and sub-index
75+
76+
Parameters
77+
----------
78+
index : int
79+
index of a specific image
80+
81+
Returns
82+
----------
83+
a tuple (sub-db, sub-index)
84+
"""
85+
assert index >= 0 and index < self.num_images, "index out of range"
86+
pos = self.image_set_index[index]
87+
for k, v in enumerate(self.imdbs):
88+
if pos >= v.num_images:
89+
pos -= v.num_images
90+
else:
91+
return (k, pos)
92+
93+
def image_path_from_index(self, index):
94+
"""
95+
given image index, find out full path
96+
97+
Parameters
98+
----------
99+
index: int
100+
index of a specific image
101+
102+
Returns
103+
----------
104+
full path of this image
105+
"""
106+
assert self.image_set_index is not None, "Dataset not initialized"
107+
pos = self.image_set_index[index]
108+
n_db, n_index = self._locate_index(index)
109+
return self.imdbs[n_db].image_path_from_index(n_index)
110+
111+
def label_from_index(self, index):
112+
"""
113+
given image index, return preprocessed ground-truth
114+
115+
Parameters
116+
----------
117+
index: int
118+
index of a specific image
119+
120+
Returns
121+
----------
122+
ground-truths of this image
123+
"""
124+
assert self.image_set_index is not None, "Dataset not initialized"
125+
pos = self.image_set_index[index]
126+
n_db, n_index = self._locate_index(index)
127+
return self.imdbs[n_db].label_from_index(n_index)

0 commit comments

Comments
 (0)