Skip to content

Commit 4267bef

Browse files
committed
fix: check dispose in unit tests
1 parent 21f5e7b commit 4267bef

37 files changed

+720
-513
lines changed

README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ const tf = require('@tensorflow/tfjs-node');
3434
const ia = require('image-augment')(tf);
3535

3636
// create an augmentation pipeline
37-
const basicAugmentation = new ia.Sequential([
37+
const basicAugmentation = ia.sequential([
3838
// add a noise with a standard deviation of 15
39-
new ia.AdditiveNoise(15),
39+
ia.additiveNoise(15),
4040
// rotate 30°
41-
new ia.AffineTransform({ rotate: 30 }),
41+
ia.affineTransform({ rotate: 30 }),
4242
// add a blur kernel of 3 pixel
43-
new ia.Blur(3)
43+
ia.blur(3)
4444
]);
4545

4646
// 2x2 image in tfjs tensor4d format
@@ -77,11 +77,11 @@ const ia = require('image-augment')(tf);
7777
const h = require('hasard');
7878

7979
// create an augmentation pipeline
80-
const basicAugmentation = new ia.Sequential([
80+
const basicAugmentation = ia.sequential([
8181
// add a noise
82-
new ia.AdditiveNoise(15),
82+
ia.additiveNoise(15),
8383
// add a random affine transform
84-
new ia.AffineTransform({
84+
ia.affineTransform({
8585
// shear from -15 to 15°
8686
shear: h.number(-15, 15),
8787
// rotate from -30 to 30°
@@ -90,7 +90,7 @@ const basicAugmentation = new ia.Sequential([
9090
translatePercent: h.array({size: 2, value: h.number(-0.1, 0.1)}),
9191
}),
9292
// add a blur kernel between 0 and 5
93-
new ia.Blur(h.integer(0, 5))
93+
ia.blur(h.integer(0, 5))
9494
]);
9595

9696
// load images in tensorflow using Jimp and fs
@@ -120,13 +120,13 @@ const cv = require('opencv4nodejs');
120120
const ia = require('image-augment')(cv);
121121

122122
// create an augmentation pipeline
123-
const basicAugmentation = new ia.Sequential([
123+
const basicAugmentation = ia.sequential([
124124
// add a noise with a standard deviation of 15
125-
new ia.AdditiveNoise(15),
125+
ia.additiveNoise(15),
126126
// rotate 30°
127-
new ia.AffineTransform({ rotate: 30 }),
127+
ia.affineTransform({ rotate: 30 }),
128128
// add a blur kernel of 3 pixel
129-
new ia.Blur(3)
129+
ia.blur(3)
130130
]);
131131

132132
const img = cv.imread('lenna.png');
@@ -139,10 +139,9 @@ const {images} = basicAugmentation.run({images : [img]})
139139
[] Add benchmark test to measure the speed
140140
[x] Faster random generator using [tensorflow js truncated normal](https://js.tensorflow.org/api/1.0.0/#truncatedNormal)
141141
[x] Get affine and perspective transform to work with tensorflow backend
142-
[] Add unit test and examples for cropToBox
142+
[] Add unit test and examples for cropToBox and Draw boxes
143143
[] Generate documentation on github
144144
[] add examples/explanations/benchmark in the README.md
145145
[] create a demo app running in the browser with tfjs + webgl
146146
[] Implement perspective Transform using tensorflowjs backend
147-
[] Draw boxes for tensorflowjs
148147
[] Run all unit tests on Travis

lib/abstract.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ class AbstractImageAugment {
66
constructor(opts, imageAugmenter) {
77
if (opts.backendLib) {
88
this.setBackend(opts.backendLib);
9-
} else if(imageAugmenter){
9+
} else if (imageAugmenter) {
1010
this.setBackend(imageAugmenter.backendLib);
11+
} else {
12+
console.log('Warning : no backend library configured for this image-augment object');
1113
}
1214

1315
this._name = this.constructor.name;

lib/augmenters/abstract.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ augmenter.run({images: [img], points: [[[25, 90], [12, 32]]]})
3939
// => { images : [Mat], points: [[<first point new position>, <second point new position>]] }
4040
**/
4141
class AbstractAugmenter extends Abstract {
42-
constructor(opts) {
43-
super(opts);
42+
constructor(opts, ia) {
43+
super(opts, ia);
4444
this._augmenter = true;
4545
}
4646
/**
@@ -71,8 +71,6 @@ class AbstractAugmenter extends Abstract {
7171

7272
const metadata = this.backend.getMetadata(params1.images);
7373

74-
debug(`buildHasard ${this._name}`);
75-
7674
const o2 = Object.assign({}, metadata, params1);
7775
const {nImages} = metadata;
7876

@@ -81,23 +79,31 @@ class AbstractAugmenter extends Abstract {
8179
if (typeof (this.buildAllImagesHasard) === 'function') {
8280
resolved = this.buildAllImagesHasard(o2).runOnce(runOpts);
8381
} else {
84-
debug(`runOnce ${this._name}`);
82+
debug(`buildHasard ${this._name} (${this.backend._tf && this.backend._tf.memory().numTensors})`);
8583
const params = this.buildHasard(o2);
84+
debug(`runOnce ${this._name} (${this.backend._tf && this.backend._tf.memory().numTensors})`);
85+
8686
// Every image hasard is generated independantly
8787
for (let i = 0; i < nImages; i++) {
8888
const resolvedParams = hasard.isHasard(params) ? params.runOnce(runOpts) : params;
8989
resolved.push(resolvedParams);
9090
}
9191
}
9292

93-
// Console.log({resolved, params})
94-
debug(`augment ${this._name}`);
93+
94+
debug(`augment ${this._name} (${this.backend._tf && this.backend._tf.memory().numTensors})`);
95+
9596
return Promise.resolve(this.augment(o2, resolved, runOpts));
9697
}
9798

9899
fromFilenames({filenames}) {
99100
return this.backend.readImages(filenames).then(images => {
100-
this.runAugmenter({images});
101+
return this.runAugmenter({images}).then(res => {
102+
images.forEach(im => {
103+
this.backend.dispose(im)
104+
})
105+
return res;
106+
});
101107
});
102108
}
103109

@@ -128,11 +134,12 @@ class AbstractAugmenter extends Abstract {
128134
// }
129135
// }
130136
augment(attrs, opts, runOpts) {
137+
debug(`start augment ${this._name} (${this.backend._tf && this.backend._tf.memory().numTensors})`);
138+
131139
opts.forEach(o => {
132140
this.checkParams(o);
133141
});
134-
135-
const res = this.backend.splitImages(attrs.images).map((image, index) => {
142+
const res = this.backend.splitImages(attrs.images, false).map((image, index) => {
136143
const points = ((attrs.points && attrs.points[index]) || []).map(p => {
137144
if (Array.isArray(p)) {
138145
return this.backend.point(p[0], p[1]);
@@ -148,26 +155,35 @@ class AbstractAugmenter extends Abstract {
148155
{points},
149156
{boxes: (attrs.boxes && attrs.boxes[index]) || []}
150157
);
151-
return this.augmentOne(
158+
const res = this.augmentOne(
152159
newAttrs,
153160
opts[index],
154161
runOpts
155162
);
163+
this.backend.dispose(image);
164+
return res;
156165
});
157-
158-
return {
159-
images: this.backend.mergeImages(res.map(r => r.image)),
166+
const res2 = {
167+
images: this.backend.mergeImages(res.map(r => r.image), true),
160168
boxes: res.map(r => r.boxes),
161169
points: res.map(r => r.points)
162170
};
171+
debug(`end augment ${this._name} (${this.backend._tf && this.backend._tf.memory().numTensors})`);
172+
return res2;
163173
}
164174

165175
augmentOne(attr, opts, runOpts) {
166-
return {
176+
debug(`start augmentOne ${this._name} (${this.backend._tf && this.backend._tf.memory().numTensors})`);
177+
178+
const res = {
167179
image: this.augmentImage(attr, opts, runOpts),
168180
boxes: this.augmentBoxes(attr, opts, runOpts),
169181
points: this.augmentPoints(attr, opts, runOpts)
170182
};
183+
184+
debug(`end augmentOne ${this._name} (${this.backend._tf && this.backend._tf.memory().numTensors})`);
185+
186+
return res;
171187
}
172188

173189
augmentImage({images}, opts, runOpts) {

lib/augmenters/add-weighted.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ new ia.AddWeighted({
1616
*/
1717

1818
class AddAugmenter extends AbstractAugmenter {
19-
constructor(opts) {
20-
super(opts);
19+
constructor(opts, ia) {
20+
super(opts, ia);
2121
const {value, alpha = 1} = opts;
2222
this.value = this.toColor(value);
2323
this.alpha = alpha;
@@ -36,6 +36,7 @@ class AddAugmenter extends AbstractAugmenter {
3636

3737
augmentImage({image}, {alpha, mat}) {
3838
const res = this.backend.addWeighted(image, mat, alpha);
39+
this.backend.dispose(mat);
3940
return res;
4041
}
4142
}

lib/augmenters/add.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ new ia.Add([12, 0, 0]);
1111
*/
1212

1313
class AddAugmenter extends AbstractAugmenter {
14-
constructor(opts) {
14+
constructor(opts, ia) {
1515
let o;
1616
if (typeof (opts) === 'number' || Array.isArray(opts) || h.isHasard(opts)) {
1717
o = {kernel: opts};
1818
} else {
1919
o = opts;
2020
}
2121

22-
super(opts);
22+
super(opts, ia);
2323
const {value} = o;
2424
this.value = this.toColor(value);
2525
}
@@ -33,7 +33,9 @@ class AddAugmenter extends AbstractAugmenter {
3333
}
3434

3535
augmentImage({image}, im) {
36-
return this.backend.addNoiseOne(image, im);
36+
const res = this.backend.addNoiseOne(image, im);
37+
this.backend.dispose(im);
38+
return res;
3739
}
3840
}
3941

lib/augmenters/additive-gaussian-noise.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ new ia.AdditiveGaussianNoise({
3434
*/
3535

3636
class AdditiveGaussianNoiseAugmenter extends AdditiveAugmenter {
37-
constructor(opts) {
37+
constructor(opts, ia) {
3838
let o;
3939
if (typeof (opts) === 'number' || h.isHasard(opts)) {
4040
o = {sigma: opts};
@@ -43,7 +43,7 @@ class AdditiveGaussianNoiseAugmenter extends AdditiveAugmenter {
4343
}
4444

4545
o.Generator = GaussianNoiseGenerator;
46-
super(o);
46+
super(o, ia);
4747
}
4848
}
4949

lib/augmenters/additive-poisson-noise.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ new ia.AdditivePoissonNoise({
3131
*/
3232

3333
class AdditivePoissonNoiseAugmenter extends AdditiveAugmenter {
34-
constructor(opts) {
34+
constructor(opts, ia) {
3535
let o;
3636
if (typeof (opts) === 'number' || h.isHasard(opts)) {
3737
o = {lambda: opts};
@@ -40,7 +40,7 @@ class AdditivePoissonNoiseAugmenter extends AdditiveAugmenter {
4040
}
4141

4242
o.Generator = PoissonNoiseGenerator;
43-
super(o);
43+
super(o, ia);
4444
}
4545
}
4646

lib/augmenters/additive-truncated-normal-noise.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const AdditiveAugmenter = require('./additive');
1414
* @example
1515
// Simple usage
1616
new ia.Noise(3);
17-
// alias of
17+
// alias of
1818
new ia.AdditiveTruncatedNormalNoise(3);
1919
* @example
2020
//Simple usage with random variable
@@ -38,7 +38,7 @@ new ia.AdditiveTruncatedNormalNoise({
3838
*/
3939

4040
class AdditiveTruncatedNormalNoiseAugmenter extends AdditiveAugmenter {
41-
constructor(opts) {
41+
constructor(opts, ia) {
4242
let o;
4343
if (typeof (opts) === 'number' || h.isHasard(opts)) {
4444
o = {sigma: opts};
@@ -47,7 +47,7 @@ class AdditiveTruncatedNormalNoiseAugmenter extends AdditiveAugmenter {
4747
}
4848

4949
o.Generator = TruncatedNormalNoiseGenerator;
50-
super(o);
50+
super(o, ia);
5151
}
5252
}
5353

lib/augmenters/additive.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,27 @@ const h = require('hasard');
22
const AbstractAugmenter = require('./abstract');
33

44
class AdditiveAugmenter extends AbstractAugmenter {
5-
constructor(opts) {
5+
constructor(opts, ia) {
66
if (typeof (opts.Generator) !== 'function') {
77
throw (new TypeError('Generator class is required in AdditiveAugmenter'));
88
}
99

10-
super(opts);
10+
super(opts, ia);
1111
const opts2 = Object.assign({}, opts);
1212
const {Generator} = opts2;
1313
delete opts2.Generator;
14-
this.generator = new Generator(opts2);
14+
this.generator = new Generator(opts2, ia);
1515
}
1616

1717
buildAllImagesHasard({nImages, width, height, channels}) {
18-
return this.generator.build({nImages, width, height, channels});
18+
this.generator.setBackend(this.backend.backendLib);
19+
const res = this.generator.build({nImages, width, height, channels});
20+
return res;
1921
}
2022

21-
augment(attrs, params, runOpts) {
23+
augment(attrs, params, runOpts) {
2224
const images = this.backend.addNoise(attrs.images, params.images);
23-
// Console.log('augment', {images}, images.dataSync().slice(0,10), attrs.images.dataSync().slice(0, 10))
25+
this.backend.dispose(params.images)
2426
return {images};
2527
}
2628
}

0 commit comments

Comments
 (0)