Skip to content

Commit

Permalink
fix cropping/upsampling/zeropadding layers when following layer where…
Browse files Browse the repository at this point in the history
… output is2DReshaped=true
  • Loading branch information
transcranial committed Dec 30, 2017
1 parent 5bf621d commit 34a1d3f
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 30 deletions.
24 changes: 19 additions & 5 deletions src/layers/convolutional/Cropping2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ export default class Cropping2D extends Layer {
* Creates row/col index mappings to map input texture to output texture
*
* @param {Object} indicesForReshaped
* @param {boolean} is2DReshaped
*/
_createIndexMap(indicesForReshaped) {
_createIndexMap(indicesForReshaped, is2DReshaped) {
if (this.indexMap) {
return
}
Expand All @@ -116,7 +117,11 @@ export default class Cropping2D extends Layer {

ops.assign(this.indexMap.tensor, indices.tensor.hi(...sliceEnd).lo(...sliceStart))

this.indexMap.reshapeTo2DSquare()
if (is2DReshaped) {
this.indexMap.reshapeTo2D()
} else {
this.indexMap.reshapeTo2DSquare()
}
this.indexMap.createGLTexture({ type: '2d', format: 'int' })
}

Expand All @@ -127,6 +132,7 @@ export default class Cropping2D extends Layer {
*/
_callGPU(x) {
if (!x.glTexture) {
// defaults to square reshaped
x.reshapeTo2DSquare()
x.createGLTexture({ type: '2d', format: 'float' })
}
Expand All @@ -144,11 +150,15 @@ export default class Cropping2D extends Layer {
this.inputShape[2]
]

this._createIndexMap(x.indicesForReshaped)
this._createIndexMap(x.indicesForReshaped, x.is2DReshaped)

if (!this.output) {
this.output = new Tensor([], this.outputShape)
this.output.reshapeTo2DSquare()
if (x.is2DReshaped) {
this.output.reshapeTo2D()
} else {
this.output.reshapeTo2DSquare()
}
this.output.createGLTexture({ type: '2d', format: 'float' })
}

Expand All @@ -162,7 +172,11 @@ export default class Cropping2D extends Layer {
// GPU -> CPU data transfer
if (this.outbound.length === 0) {
this.output.transferFromGLTexture()
this.output.reshapeFrom2DSquare()
if (this.output.is2DReshaped) {
this.output.reshapeFrom2D()
} else {
this.output.reshapeFrom2DSquare()
}
}
}
}
24 changes: 19 additions & 5 deletions src/layers/convolutional/Cropping3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ export default class Cropping3D extends Layer {
* Creates row/col index mappings to map input texture to output texture
*
* @param {Object} indicesForReshaped
* @param {boolean} is2DReshaped
*/
_createIndexMap(indicesForReshaped) {
_createIndexMap(indicesForReshaped, is2DReshaped) {
if (this.indexMap) {
return
}
Expand Down Expand Up @@ -132,7 +133,11 @@ export default class Cropping3D extends Layer {

ops.assign(this.indexMap.tensor, indices.tensor.hi(...sliceEnd).lo(...sliceStart))

this.indexMap.reshapeTo2DSquare()
if (is2DReshaped) {
this.indexMap.reshapeTo2D()
} else {
this.indexMap.reshapeTo2DSquare()
}
this.indexMap.createGLTexture({ type: '2d', format: 'int' })
}

Expand All @@ -143,6 +148,7 @@ export default class Cropping3D extends Layer {
*/
_callGPU(x) {
if (!x.glTexture) {
// defaults to square reshaped
x.reshapeTo2DSquare()
x.createGLTexture({ type: '2d', format: 'float' })
}
Expand All @@ -162,11 +168,15 @@ export default class Cropping3D extends Layer {
this.inputShape[3]
]

this._createIndexMap(x.indicesForReshaped)
this._createIndexMap(x.indicesForReshaped, x.is2DReshaped)

if (!this.output) {
this.output = new Tensor([], this.outputShape)
this.output.reshapeTo2DSquare()
if (x.is2DReshaped) {
this.output.reshapeTo2D()
} else {
this.output.reshapeTo2DSquare()
}
this.output.createGLTexture({ type: '2d', format: 'float' })
}

Expand All @@ -180,7 +190,11 @@ export default class Cropping3D extends Layer {
// GPU -> CPU data transfer
if (this.outbound.length === 0) {
this.output.transferFromGLTexture()
this.output.reshapeFrom2DSquare()
if (this.output.is2DReshaped) {
this.output.reshapeFrom2D()
} else {
this.output.reshapeFrom2DSquare()
}
}
}
}
24 changes: 19 additions & 5 deletions src/layers/convolutional/UpSampling2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ export default class UpSampling2D extends Layer {
* Creates row/col index mappings to map input texture to output texture
*
* @param {Object} indicesForReshaped
* @param {boolean} is2DReshaped
*/
_createIndexMap(indicesForReshaped) {
_createIndexMap(indicesForReshaped, is2DReshaped) {
if (this.indexMap) {
return
}
Expand All @@ -102,7 +103,11 @@ export default class UpSampling2D extends Layer {
}
}

this.indexMap.reshapeTo2DSquare()
if (is2DReshaped) {
this.indexMap.reshapeTo2D()
} else {
this.indexMap.reshapeTo2DSquare()
}
this.indexMap.createGLTexture({ type: '2d', format: 'int' })
}

Expand All @@ -113,6 +118,7 @@ export default class UpSampling2D extends Layer {
*/
_callGPU(x) {
if (!x.glTexture) {
// defaults to square reshaped
x.reshapeTo2DSquare()
x.createGLTexture({ type: '2d', format: 'float' })
}
Expand All @@ -122,11 +128,15 @@ export default class UpSampling2D extends Layer {
? [this.inputShape[0], this.inputShape[1] * this.size[0], this.inputShape[2] * this.size[1]]
: [this.inputShape[0] * this.size[0], this.inputShape[1] * this.size[1], this.inputShape[2]]

this._createIndexMap(x.indicesForReshaped)
this._createIndexMap(x.indicesForReshaped, x.is2DReshaped)

if (!this.output) {
this.output = new Tensor([], this.outputShape)
this.output.reshapeTo2DSquare()
if (x.is2DReshaped) {
this.output.reshapeTo2D()
} else {
this.output.reshapeTo2DSquare()
}
this.output.createGLTexture({ type: '2d', format: 'float' })
}

Expand All @@ -140,7 +150,11 @@ export default class UpSampling2D extends Layer {
// GPU -> CPU data transfer
if (this.outbound.length === 0) {
this.output.transferFromGLTexture()
this.output.reshapeFrom2DSquare()
if (this.output.is2DReshaped) {
this.output.reshapeFrom2D()
} else {
this.output.reshapeFrom2DSquare()
}
}
}
}
24 changes: 19 additions & 5 deletions src/layers/convolutional/UpSampling3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ export default class UpSampling3D extends Layer {
* Creates row/col index mappings to map input texture to output texture
*
* @param {Object} indicesForReshaped
* @param {boolean} is2DReshaped
*/
_createIndexMap(indicesForReshaped) {
_createIndexMap(indicesForReshaped, is2DReshaped) {
if (this.indexMap) {
return
}
Expand All @@ -113,7 +114,11 @@ export default class UpSampling3D extends Layer {
}
}

this.indexMap.reshapeTo2DSquare()
if (is2DReshaped) {
this.indexMap.reshapeTo2D()
} else {
this.indexMap.reshapeTo2DSquare()
}
this.indexMap.createGLTexture({ type: '2d', format: 'int' })
}

Expand All @@ -124,6 +129,7 @@ export default class UpSampling3D extends Layer {
*/
_callGPU(x) {
if (!x.glTexture) {
// defaults to square reshaped
x.reshapeTo2DSquare()
x.createGLTexture({ type: '2d', format: 'float' })
}
Expand All @@ -143,11 +149,15 @@ export default class UpSampling3D extends Layer {
this.inputShape[3]
]

this._createIndexMap(x.indicesForReshaped)
this._createIndexMap(x.indicesForReshaped, x.is2DReshaped)

if (!this.output) {
this.output = new Tensor([], this.outputShape)
this.output.reshapeTo2DSquare()
if (x.is2DReshaped) {
this.output.reshapeTo2D()
} else {
this.output.reshapeTo2DSquare()
}
this.output.createGLTexture({ type: '2d', format: 'float' })
}

Expand All @@ -161,7 +171,11 @@ export default class UpSampling3D extends Layer {
// GPU -> CPU data transfer
if (this.outbound.length === 0) {
this.output.transferFromGLTexture()
this.output.reshapeFrom2DSquare()
if (this.output.is2DReshaped) {
this.output.reshapeFrom2D()
} else {
this.output.reshapeFrom2DSquare()
}
}
}
}
24 changes: 19 additions & 5 deletions src/layers/convolutional/ZeroPadding2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ export default class ZeroPadding2D extends Layer {
* Creates row/col index mappings to map input texture to output texture
*
* @param {Object} indicesForReshaped
* @param {boolean} is2DReshaped
*/
_createIndexMap(indicesForReshaped) {
_createIndexMap(indicesForReshaped, is2DReshaped) {
if (this.indexMap) {
return
}
Expand All @@ -117,7 +118,11 @@ export default class ZeroPadding2D extends Layer {
ops.assigns(this.indexMap.tensor, -1)
ops.assign(this.indexMap.tensor.hi(...sliceEnd).lo(...sliceStart), indices.tensor)

this.indexMap.reshapeTo2DSquare()
if (is2DReshaped) {
this.indexMap.reshapeTo2D()
} else {
this.indexMap.reshapeTo2DSquare()
}
this.indexMap.createGLTexture({ type: '2d', format: 'int' })
}

Expand All @@ -128,6 +133,7 @@ export default class ZeroPadding2D extends Layer {
*/
_callGPU(x) {
if (!x.glTexture) {
// defaults to square reshaped
x.reshapeTo2DSquare()
x.createGLTexture({ type: '2d', format: 'float' })
}
Expand All @@ -145,11 +151,15 @@ export default class ZeroPadding2D extends Layer {
this.inputShape[2]
]

this._createIndexMap(x.indicesForReshaped)
this._createIndexMap(x.indicesForReshaped, x.is2DReshaped)

if (!this.output) {
this.output = new Tensor([], this.outputShape)
this.output.reshapeTo2DSquare()
if (x.is2DReshaped) {
this.output.reshapeTo2D()
} else {
this.output.reshapeTo2DSquare()
}
this.output.createGLTexture({ type: '2d', format: 'float' })
}

Expand All @@ -163,7 +173,11 @@ export default class ZeroPadding2D extends Layer {
// GPU -> CPU data transfer
if (this.outbound.length === 0) {
this.output.transferFromGLTexture()
this.output.reshapeFrom2DSquare()
if (this.output.is2DReshaped) {
this.output.reshapeFrom2D()
} else {
this.output.reshapeFrom2DSquare()
}
}
}
}
24 changes: 19 additions & 5 deletions src/layers/convolutional/ZeroPadding3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ export default class ZeroPadding3D extends Layer {
* Creates row/col index mappings to map input texture to output texture
*
* @param {Object} indicesForReshaped
* @param {boolean} is2DReshaped
*/
_createIndexMap(indicesForReshaped) {
_createIndexMap(indicesForReshaped, is2DReshaped) {
if (this.indexMap) {
return
}
Expand Down Expand Up @@ -133,7 +134,11 @@ export default class ZeroPadding3D extends Layer {
ops.assigns(this.indexMap.tensor, -1)
ops.assign(this.indexMap.tensor.hi(...sliceEnd).lo(...sliceStart), indices.tensor)

this.indexMap.reshapeTo2DSquare()
if (is2DReshaped) {
this.indexMap.reshapeTo2D()
} else {
this.indexMap.reshapeTo2DSquare()
}
this.indexMap.createGLTexture({ type: '2d', format: 'int' })
}

Expand All @@ -144,6 +149,7 @@ export default class ZeroPadding3D extends Layer {
*/
_callGPU(x) {
if (!x.glTexture) {
// defaults to square reshaped
x.reshapeTo2DSquare()
x.createGLTexture({ type: '2d', format: 'float' })
}
Expand All @@ -163,11 +169,15 @@ export default class ZeroPadding3D extends Layer {
this.inputShape[3]
]

this._createIndexMap(x.indicesForReshaped)
this._createIndexMap(x.indicesForReshaped, x.is2DReshaped)

if (!this.output) {
this.output = new Tensor([], this.outputShape)
this.output.reshapeTo2DSquare()
if (x.is2DReshaped) {
this.output.reshapeTo2D()
} else {
this.output.reshapeTo2DSquare()
}
this.output.createGLTexture({ type: '2d', format: 'float' })
}

Expand All @@ -181,7 +191,11 @@ export default class ZeroPadding3D extends Layer {
// GPU -> CPU data transfer
if (this.outbound.length === 0) {
this.output.transferFromGLTexture()
this.output.reshapeFrom2DSquare()
if (this.output.is2DReshaped) {
this.output.reshapeFrom2D()
} else {
this.output.reshapeFrom2DSquare()
}
}
}
}
1 change: 1 addition & 0 deletions src/layers/normalization/BatchNormalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export default class BatchNormalization extends Layer {
if (x.tensor.shape.length <= 2) {
x.createGLTexture({ type: '2d', format: 'float', supportsTextureFragments: true })
} else if (x.tensor.shape.length > 2 && !x.is2DReshaped) {
// defaults to square reshaped
x.reshapeTo2DSquare()
x.createGLTexture({ type: '2d', format: 'float', supportsTextureFragments: true })
}
Expand Down

0 comments on commit 34a1d3f

Please sign in to comment.