/
ObjectDetectionConfig.scala
176 lines (158 loc) · 5.95 KB
/
ObjectDetectionConfig.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright 2018 Analytics Zoo Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intel.analytics.zoo.models.image.objectdetection
import com.intel.analytics.bigdl.dataset.PaddingParam
import com.intel.analytics.bigdl.numeric.NumericFloat
import com.intel.analytics.bigdl.tensor.Tensor
import com.intel.analytics.bigdl.tensor.TensorNumericMath.TensorNumeric
import com.intel.analytics.bigdl.transform.vision.image.ImageFeature
import com.intel.analytics.zoo.feature.common.Preprocessing
import com.intel.analytics.zoo.feature.image._
import com.intel.analytics.zoo.models.image.common.ImageConfigure
import com.intel.analytics.zoo.models.image.objectdetection.ObjectDetectorDataset.{Coco, Pascal}
import scala.reflect.ClassTag
private[models] object ObjectDetectionConfig {
val models = Set("ssd-vgg16-300x300",
"ssd-vgg16-300x300-quantize",
"ssd-vgg16-512x512",
"ssd-vgg16-512x512-quantize",
"ssd-mobilenet-300x300",
"frcnn-vgg16",
"frcnn-vgg16-compress",
"frcnn-vgg16-quantize",
"frcnn-vgg16-compress-quantize",
"frcnn-pvanet",
"frcnn-pvanet-quantize",
"frcnn-pvanet-compress",
"frcnn-pvanet-compress-quantize",
"frcnn-vgg16-compress-quantize")
def apply[T: ClassTag](model: String, dataset: String, version: String)
(implicit ev: TensorNumeric[T]): ImageConfigure[T] = {
val labelMap = LabelReader(dataset)
model match {
case "ssd-vgg16-300x300" |
"ssd-vgg16-300x300-quantize" =>
ImageConfigure(ObjectDetectionConfig.preprocessSsdVgg(300, dataset, version),
ScaleDetection(),
batchPerPartition = 2,
labelMap = labelMap)
case "ssd-vgg16-512x512" |
"ssd-vgg16-512x512-quantize" =>
ImageConfigure(ObjectDetectionConfig.preprocessSsdVgg(512, dataset, version),
ScaleDetection(),
batchPerPartition = 2,
labelMap = labelMap)
case "ssd-mobilenet-300x300" =>
ImageConfigure(ObjectDetectionConfig.preprocessSsdMobilenet(300, dataset, version),
ScaleDetection(),
batchPerPartition = 2,
labelMap = labelMap
)
case "frcnn-vgg16" |
"frcnn-vgg16-quantize" |
"frcnn-vgg16-compress" |
"frcnn-vgg16-compress-quantize" =>
ImageConfigure(ObjectDetectionConfig.preprocessFrcnnVgg(dataset, version),
DecodeOutput(),
batchPerPartition = 1,
labelMap,
Some(PaddingParam()))
case "frcnn-pvanet" |
"frcnn-pvanet-quantize" |
"frcnn-pvanet-compress" |
"frcnn-pvanet-compress-quantize" =>
ImageConfigure(ObjectDetectionConfig.preprocessFrcnnPvanet(dataset, version),
DecodeOutput(),
batchPerPartition = 1,
labelMap,
Some(PaddingParam()))
}
}
def preprocessSsdVgg(resolution: Int, dataset: String, version: String)
: Preprocessing[ImageFeature, ImageFeature] = {
preprocessSsd(resolution, (123f, 117f, 104f), 1f)
}
def preprocessSsd(resolution: Int, meansRGB: (Float, Float, Float),
scale: Float): Preprocessing[ImageFeature, ImageFeature] = {
ImageResize(resolution, resolution) ->
ImageChannelNormalize(meansRGB._1, meansRGB._2, meansRGB._3, scale, scale, scale) ->
ImageMatToTensor() -> ImageSetToSample()
}
def preprocessSsdMobilenet(resolution: Int, dataset: String, version: String)
: Preprocessing[ImageFeature, ImageFeature] = {
ObjectDetectorDataset(dataset) match {
case Pascal =>
preprocessSsd(resolution, (127.5f, 127.5f, 127.5f), 1 / 0.007843f)
case Coco =>
throw new Exception("coco is not yet supported for Analytics Zoo ssd mobilenet")
}
}
def preprocessFrcnn(resolution: Int, scaleMultipleOf: Int):
Preprocessing[ImageFeature, ImageFeature] = {
ImageAspectScale(resolution, scaleMultipleOf) ->
ImageChannelNormalize(122.7717f, 115.9465f, 102.9801f) ->
ImageMatToTensor() -> ImInfo() -> ImageSetToSample(Array(ImageFeature.imageTensor, "ImInfo"))
}
def preprocessFrcnnVgg(dataset: String, version: String):
Preprocessing[ImageFeature, ImageFeature] = {
ObjectDetectorDataset(dataset) match {
case Pascal =>
preprocessFrcnn(600, 1)
case Coco =>
throw new Exception("coco is not yet supported for Analytics Zoo FrcnnVgg")
}
}
def preprocessFrcnnPvanet(dataset: String, version: String):
Preprocessing[ImageFeature, ImageFeature] = {
ObjectDetectorDataset(dataset) match {
case Pascal =>
preprocessFrcnn(640, 32)
case Coco =>
throw new Exception("coco is not yet supported for BigDL FrcnnPvanet")
}
}
}
sealed trait ObjectDetectorDataset {
val value: String
}
object ObjectDetectorDataset {
def apply(datasetString: String): ObjectDetectorDataset = {
datasetString.toUpperCase match {
case Pascal.value => Pascal
case Coco.value => Coco
}
}
case object Pascal extends ObjectDetectorDataset {
val value = "PASCAL"
}
case object Coco extends ObjectDetectorDataset {
val value: String = "COCO"
}
}
/**
* Generate imInfo
* imInfo is a tensor that contains height, width, scaleInHeight, scaleInWidth
*/
case class ImInfo() extends ImageProcessing {
override def transformMat(feature: ImageFeature): Unit = {
feature("ImInfo") = feature.getImInfo()
}
}
case class DummyGT() extends ImageProcessing {
override def transformMat(feature: ImageFeature): Unit = {
feature("DummyGT") = Tensor[Float](1)
}
}