-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathCOCODataset.swift
179 lines (167 loc) · 6.97 KB
/
COCODataset.swift
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
177
178
179
// Copyright 2020 The TensorFlow Authors. All Rights Reserved.
//
// 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.
import Foundation
import TensorFlow
public struct COCODataset<Entropy: RandomNumberGenerator> {
/// Type of the collection of non-collated batches.
public typealias Batches = Slices<Sampling<[ObjectDetectionExample], ArraySlice<Int>>>
/// The type of the training data, represented as a sequence of epochs, which
/// are collection of batches.
public typealias Training = LazyMapSequence<
TrainingEpochs<[ObjectDetectionExample], Entropy>,
LazyMapSequence<Batches, [ObjectDetectionExample]>
>
/// The type of the validation data, represented as a collection of batches.
public typealias Validation = LazyMapSequence<Slices<[ObjectDetectionExample]>, [ObjectDetectionExample]>
/// The training epochs.
public let training: Training
/// The validation batches.
public let validation: Validation
/// Creates an instance with `batchSize` on `device` using `remoteBinaryArchiveLocation`.
///
/// - Parameters:
/// - training: The COCO metadata for the training data.
/// - validation: The COCO metadata for the validation data.
/// - includeMasks: Whether to include the segmentation masks when loading the dataset.
/// - batchSize: Number of images provided per batch.
/// - entropy: A source of randomness used to shuffle sample ordering. It
/// will be stored in `self`, so if it is only pseudorandom and has value
/// semantics, the sequence of epochs is deterministic and not dependent
/// on other operations.
/// - device: The Device on which resulting Tensors from this dataset will be placed, as well
/// as where the latter stages of any conversion calculations will be performed.
public init(
training: COCO, validation: COCO, includeMasks: Bool, batchSize: Int,
entropy: Entropy, device: Device,
transform: @escaping (ObjectDetectionExample) -> [ObjectDetectionExample]
) {
let trainingSamples = loadCOCOExamples(
from: training,
includeMasks: includeMasks,
batchSize: batchSize)
self.training = TrainingEpochs(samples: trainingSamples, batchSize: batchSize, entropy: entropy)
.lazy.map { (batches: Batches) -> LazyMapSequence<Batches, [ObjectDetectionExample]> in
return batches.lazy.map {
makeBatch(samples: $0, device: device, transform: transform)
}
}
let validationSamples = loadCOCOExamples(
from: validation,
includeMasks: includeMasks,
batchSize: batchSize)
self.validation = validationSamples.inBatches(of: batchSize).lazy.map {
makeBatch(samples: $0, device: device, transform: transform)
}
}
public static func identity(_ example: ObjectDetectionExample) -> [ObjectDetectionExample] {
return [example]
}
}
extension COCODataset: ObjectDetectionData where Entropy == SystemRandomNumberGenerator {
/// Creates an instance with `batchSize`, using the SystemRandomNumberGenerator.
public init(
training: COCO, validation: COCO, includeMasks: Bool, batchSize: Int,
on device: Device = Device.default,
transform: @escaping (ObjectDetectionExample) -> [ObjectDetectionExample] = COCODataset.identity
) {
self.init(
training: training, validation: validation, includeMasks: includeMasks, batchSize: batchSize,
entropy: SystemRandomNumberGenerator(), device: device, transform: transform)
}
}
func loadCOCOExamples(from coco: COCO, includeMasks: Bool, batchSize: Int)
-> [ObjectDetectionExample]
{
let images = coco.metadata["images"] as! [COCO.Image]
let batchCount: Int = images.count / batchSize + 1
let batches = Array(0..<batchCount)
let examples: [[ObjectDetectionExample]] = batches.map { batchIdx in
var examples: [ObjectDetectionExample] = []
for i in 0..<batchSize {
let idx = batchSize * batchIdx + i
if idx < images.count {
let img = images[idx]
let example = loadCOCOExample(coco: coco, image: img, includeMasks: includeMasks)
examples.append(example)
}
}
return examples
}
let result = Array(examples.joined())
assert(result.count == images.count)
return result
}
func loadCOCOExample(coco: COCO, image: COCO.Image, includeMasks: Bool) -> ObjectDetectionExample {
let imgDir = coco.imagesDirectory
let imgW = image["width"] as! Int
let imgH = image["height"] as! Int
let imgFileName = image["file_name"] as! String
var imgUrl: URL? = nil
if imgDir != nil {
let imgPath = imgDir!.appendingPathComponent(imgFileName).path
imgUrl = URL(string: imgPath)!
}
let imgId = image["id"] as! Int
let img = LazyImage(width: imgW, height: imgH, url: imgUrl)
let annotations: [COCO.Annotation]
if let anns = coco.imageToAnnotations[imgId] {
annotations = anns
} else {
annotations = []
}
var objects: [LabeledObject] = []
objects.reserveCapacity(annotations.count)
for annotation in annotations {
let bb = annotation["bbox"] as! [Double]
let bbX = bb[0]
let bbY = bb[1]
let bbW = bb[2]
let bbH = bb[3]
let xMin = Float(bbX) / Float(imgW)
let xMax = Float(bbX + bbW) / Float(imgW)
let yMin = Float(bbY) / Float(imgH)
let yMax = Float(bbY + bbH) / Float(imgH)
let isCrowd: Int?
if let iscrowd = annotation["iscrowd"] {
isCrowd = iscrowd as? Int
} else {
isCrowd = nil
}
let area = Float(annotation["area"] as! Double)
let classId = annotation["category_id"] as! Int
let classInfo = coco.categories[classId]!
let className = classInfo["name"] as! String
let maskRLE: RLE?
if includeMasks {
maskRLE = coco.annotationToRLE(annotation)
} else {
maskRLE = nil
}
let object = LabeledObject(
xMin: xMin, xMax: xMax,
yMin: yMin, yMax: yMax,
className: className, classId: classId,
isCrowd: isCrowd, area: area, maskRLE: maskRLE)
objects.append(object)
}
return ObjectDetectionExample(image: img, objects: objects)
}
fileprivate func makeBatch<BatchSamples: Collection>(
samples: BatchSamples, device: Device,
transform: (ObjectDetectionExample) -> [ObjectDetectionExample]
) -> [ObjectDetectionExample] where BatchSamples.Element == ObjectDetectionExample {
return samples.reduce([]) {
$0 + transform($1)
}
}