Skip to content

Commit 90eaf01

Browse files
committed
feat: most of the test tests are passing with tfjs backend
Except * perspective transform * affine transform
1 parent 7a159e6 commit 90eaf01

File tree

14 files changed

+67
-22
lines changed

14 files changed

+67
-22
lines changed

lib/abstract.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const debug = require('debug')('image-augment');
44

55
class AbstractImageAugment {
66
constructor(opts) {
7-
this.backend = opts.backend ? backends.get(opts.backend) : backends.getDefault();
7+
this.setBackend(opts.backend)
88
this._name = this.constructor.name;
99
}
1010

@@ -19,6 +19,10 @@ class AbstractImageAugment {
1919
static isAugmenter(o) {
2020
return (typeof (o) === 'object' && o._augmenter);
2121
}
22+
23+
setBackend(key){
24+
this.backend = key ? backends.get(key) : backends.getDefault();
25+
}
2226

2327
toSize2(opt) {
2428
const fn = function (size) {

lib/augmenters/abstract.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class AbstractAugmenter extends Abstract{
5151
} else if (this.backend.isImage(runOpts)) {
5252
params1 = {image: runOpts};
5353
} else {
54+
console.log(runOpts.image, this.backend.key)
5455
throw(new Error('runOnce must have an image in it'));
5556
}
5657
const metadata = this.backend.getMetadata(params1.image);

lib/augmenters/background.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class BackgroundAugmenter extends AbstractAugmenter {
2828
buildParams({width, height, channels, boxes}) {
2929
const contextName = 'background';
3030
debug('start generator')
31+
this.generator.setBackend(this.backend.key)
3132
const res = h.object({
3233
background: this.generator.build({width, height, channels, boxes})
3334
}, {
@@ -37,10 +38,10 @@ class BackgroundAugmenter extends AbstractAugmenter {
3738
return res;
3839
}
3940

40-
augmentImage({image}, {background}) {
41+
augmentImage({image, width, height, channels}, {background}) {
4142
// console.log(image.getDataAsArray()[100].slice(50, 60))
4243
debug('start overlay')
43-
const res = this.backend.overlay({foreground: image, background: background.image, width: image.cols, height: image.rows, channels: image.channels});
44+
const res = this.backend.overlay({foreground: image, background: background.image, width, height, channels});
4445
debug('overlay done')
4546
// console.log(res.getDataAsArray()[100].slice(50, 60))
4647
return res;

lib/augmenters/sequential.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Sequential extends AbstractAugmenter {
3030
augment(o, steps, runOpts) {
3131
let promise = Promise.resolve(o)
3232
steps.forEach(s => {
33+
s.setBackend(this.backend.key)
3334
//console.log({s})
3435
promise = promise.then(current => {
3536
if(!this.backend.isImage(current.image)){

lib/backend/opencv-backend.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const debug = require('debug')('image-augment:backend:opencv4nodejs');
33
class OpenCVBackend {
44
constructor() {
55
this._cv = require('opencv4nodejs');
6+
this.key = 'opencv4nodejs';
67
}
78

89
point(x, y) {

lib/backend/tensorflow-backend.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const notImplemented = function(str){
88
class TensorflowBackend {
99
constructor() {
1010
this._tf = require('@tensorflow/tfjs-node');
11+
this.key = 'tfjs';
1112
}
1213

1314
point(x, y) {
@@ -91,7 +92,7 @@ class TensorflowBackend {
9192
}
9293

9394
addWeighted(img, img2, alpha) {
94-
return img.mul(alpha).add(imag2.mul((1-alpha)));
95+
return img.mul(alpha).add(img2.mul((1-alpha)));
9596
}
9697

9798
blur(img, size) {
@@ -166,19 +167,19 @@ class TensorflowBackend {
166167
}
167168

168169
if (channels === 4) {
169-
const channels = foreground.splitChannels();
170-
const a = channels[3].cvtColor(this._cv.COLOR_GRAY2BGR).convertTo(this._cv.CV_32FC3, 1 / 255);
171-
const white = new this._cv.Mat(height, width, this._cv.CV_8UC3, [1,1,1]).convertTo(this._cv.CV_32FC3);
172-
const inverseA = white.sub(a);
173-
const b = foreground.cvtColor(this._cv.COLOR_BGRA2BGR).convertTo(this._cv.CV_32FC3);
174-
const foreW = b.hMul(a);
170+
const channels = this._tf.split(foreground, 4, 3);
175171

176-
const backW = background.cvtColor(this._cv.COLOR_BGRA2BGR).convertTo(this._cv.CV_8UC3).convertTo(this._cv.CV_32FC3).hMul(inverseA);
172+
const a = channels[3].div(this._tf.scalar(255)).tile([1,1,1,4]);
173+
const white = this._tf.onesLike(foreground);
174+
const inverseA = white.sub(a);
175+
//const b = foreground.cvtColor(this._cv.COLOR_BGRA2BGR).convertTo(this._cv.CV_32FC3);
176+
const foreW = foreground.mul(a);
177+
const backW = background.mul(inverseA);
177178
// console.log("overlay a", a.getDataAsArray()[100].slice(50, 60))
178179
// console.log("overlay inverseA", inverseA.getDataAsArray()[100].slice(50, 60))
179180
// console.log("overlay back", backW.getDataAsArray()[100].slice(50, 60))
180181

181-
const res = foreW.add(backW).convertTo(this._cv.CV_8UC3);
182+
const res = foreW.add(backW);
182183
return res;
183184
}
184185

lib/generators/abstract-noise.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ class AbstractNoise extends AbstractGenerator {
3131
contextName: 'image',
3232
value: h.if(this.perChannel,
3333
h.array({
34-
size: channels,
35-
value: hasardPixelValue
34+
values: channels === 4 ? [hasardPixelValue, hasardPixelValue, hasardPixelValue, 255] : [hasardPixelValue, hasardPixelValue, hasardPixelValue]
3635
}),
3736
h.array({
38-
size: channels,
39-
value: hasardPixelValueRef,
37+
values: channels === 4 ? [hasardPixelValueRef, hasardPixelValueRef, hasardPixelValueRef, 255] : [hasardPixelValueRef, hasardPixelValueRef, hasardPixelValueRef],
4038
contextName: 'colorPixel'
4139
})
4240
)

test/augmenters/add-weighted.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const test = require('ava');
33
const AddWeighted = require('../../lib/augmenters/add-weighted');
44
const macroAugmenter = require('../macros/augmenter');
55

6-
test('blur kernel 3', macroAugmenter, AddWeighted, {
6+
test('add weighted', macroAugmenter, AddWeighted, {
77
inputFilename: 'lenna.png',
88
outputFilename: 'lenna-light.png',
99
//debugOutput: path.join(__dirname, '..', 'data/lenna-light.png'),

test/augmenters/additive-poisson-noise.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ test('additivePoissonNoise not perChannel', macroAugmenter, AdditivePoissonNoise
2121

2222
let count = 0;
2323
// Console.log(diff.getDataAsArray().slice(0,30).map(v => v.slice(440, 450)))
24-
const m2 = mat2.getDataAsArray();
24+
const m2 = backend.imageToArray(mat2);
2525
backend.forEachPixel(absdiff, ([b, g, r], rowIndex, colIndex) => {
26-
if (m2[rowIndex][colIndex].indexOf(255) === -1 && m2[rowIndex][colIndex].indexOf(0) === -1 && (r !== g || g !== b)) {
26+
if (m2[rowIndex][colIndex].slice(0,3).indexOf(255) === -1 && m2[rowIndex][colIndex].slice(0,3).indexOf(0) === -1 && (r !== g || g !== b)) {
2727
count++;
2828
}
2929
});
@@ -37,16 +37,17 @@ test('additivePoissonNoise not perChannel', macroAugmenter, AdditivePoissonNoise
3737

3838
test('additivePoissonNoiseperChannel', macroAugmenter, AdditivePoissonNoise, {
3939
inputFilename: 'lenna.png',
40+
backends: ['tfjs'],
4041
expectImg(t, mat1, mat2, backend) {
4142
const diff = backend.absdiff(mat1, mat2);
4243
const metadata = backend.getMetadata(mat1);
4344
const norm = backend.normL1(diff) / (metadata.width * metadata.height * metadata.channels);
4445
t.true(norm > lambda * 2 / 3);
4546
t.true(norm < lambda * 4 / 3);
4647
let count = 0;
47-
const m2 = mat2.getDataAsArray();
48+
const m2 = backend.imageToArray(mat2);
4849
backend.forEachPixel(diff, ([b, g, r], rowIndex, colIndex) => {
49-
if (m2[rowIndex][colIndex].indexOf(255) === -1 && m2[rowIndex][colIndex].indexOf(0) === -1 && (r !== g || g !== b)) {
50+
if (m2[rowIndex][colIndex].slice(0,3).indexOf(255) === -1 && m2[rowIndex][colIndex].slice(0,3).indexOf(0) === -1 && (r !== g || g !== b)) {
5051
count++;
5152
}
5253
});

test/augmenters/affine-transform.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const macroAugmenter = require('../macros/augmenter');
66
test('affine scale 0.5 replicate', macroAugmenter, AffineTransform, {
77
inputFilename: 'lenna.png',
88
outputFilename: 'lenna-affine-scale-0.5.png',
9+
backends: [
10+
'opencv4nodejs'
11+
],
912
// InputPoints: [[0, 0], [1, 0], [0, 1], [1, 1]],
1013
// outputPoints: [[0.25, 0.25], [0.75, 0.25], [0.25, 0.75], [0.75, 0.75]],
1114
options: {
@@ -18,6 +21,9 @@ test('affine scale 0.5 replicate', macroAugmenter, AffineTransform, {
1821
test('affine scale 2 replicate', macroAugmenter, AffineTransform, {
1922
inputFilename: 'lenna.png',
2023
outputFilename: 'lenna-affine-scale-2.png',
24+
backends: [
25+
'opencv4nodejs'
26+
],
2127
inputPoints: [[0, 0], [1, 0], [0, 1], [1, 1]],
2228
outputPoints: [[-0.5, -0.5], [1.5, -0.5], [-0.5, 1.5], [1.5, 1.5]],
2329
options: {
@@ -30,6 +36,9 @@ test('affine scale 2 replicate', macroAugmenter, AffineTransform, {
3036
test('affine translatePercent 0.1', macroAugmenter, AffineTransform, {
3137
inputFilename: 'lenna.png',
3238
outputFilename: 'lenna-affine-translate-10.png',
39+
backends: [
40+
'opencv4nodejs'
41+
],
3342
options: {
3443
translatePercent: 0.1,
3544
borderValue: [255, 0, 0],
@@ -40,6 +49,9 @@ test('affine translatePercent 0.1', macroAugmenter, AffineTransform, {
4049
test('affine translatePercent 0.1, -0.2', macroAugmenter, AffineTransform, {
4150
inputFilename: 'lenna.png',
4251
outputFilename: 'lenna-affine-translate-10-20.png',
52+
backends: [
53+
'opencv4nodejs'
54+
],
4355
inputPoints: [[0, 0], [0, 1]],
4456
outputPoints: [[0.1 + 0.25, -0.2 + 0.25], [0.1 + 0.25, 0.75 - 0.2]],
4557
options: {
@@ -53,6 +65,9 @@ test('affine translatePercent 0.1, -0.2', macroAugmenter, AffineTransform, {
5365
test('affine rotate 10', macroAugmenter, AffineTransform, {
5466
inputFilename: 'lenna.png',
5567
outputFilename: 'lenna-rotate-10.png',
68+
backends: [
69+
'opencv4nodejs'
70+
],
5671
options: {
5772
rotate: 10,
5873
borderValue: [255, 0, 0],
@@ -63,6 +78,9 @@ test('affine rotate 10', macroAugmenter, AffineTransform, {
6378
test('affine shear -25', macroAugmenter, AffineTransform, {
6479
inputFilename: 'lenna.png',
6580
outputFilename: 'lenna-shear-25.png',
81+
backends: [
82+
'opencv4nodejs'
83+
],
6684
options: {
6785
shear: -25,
6886
borderValue: [255, 0, 0],
@@ -72,6 +90,9 @@ test('affine shear -25', macroAugmenter, AffineTransform, {
7290
test('affine all', macroAugmenter, AffineTransform, {
7391
inputFilename: 'lenna.png',
7492
outputFilename: 'lenna-affine-all.png',
93+
backends: [
94+
'opencv4nodejs'
95+
],
7596
options: {
7697
scale: 0.5,
7798
translatePercent: [0.1, 0.2],
@@ -84,6 +105,9 @@ test('affine all', macroAugmenter, AffineTransform, {
84105

85106
test('affine transparent image to transparent border', macroAugmenter, AffineTransform, {
86107
inputFilename: 'lenna-with-alpha.png',
108+
backends: [
109+
'opencv4nodejs'
110+
],
87111
options: {
88112
rotate: 10,
89113
borderType: 'transparent',

0 commit comments

Comments
 (0)