mirror of
https://github.com/wassname/keras-js.git
synced 2026-09-09 11:25:25 +08:00
update Model/Layer gpu togglin, update demos
This commit is contained in:
@@ -119,7 +119,7 @@ export const MnistCnn = Vue.extend({
|
||||
methods: {
|
||||
|
||||
toggleGpu: function () {
|
||||
this.model.gpu = !this.useGpu
|
||||
this.model.toggleGpu(!this.useGpu)
|
||||
},
|
||||
|
||||
clear: function (e) {
|
||||
|
||||
@@ -92,7 +92,7 @@ export const MnistVae = Vue.extend({
|
||||
methods: {
|
||||
|
||||
toggleGpu: function () {
|
||||
this.model.gpu = !this.useGpu
|
||||
this.model.toggleGpu(!this.useGpu)
|
||||
},
|
||||
|
||||
activateCrosshairs: function (e) {
|
||||
|
||||
@@ -81,8 +81,6 @@
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
justify-content: center;
|
||||
user-select: none;
|
||||
cursor: default;
|
||||
|
||||
.output-class {
|
||||
display: flex;
|
||||
|
||||
@@ -119,7 +119,7 @@ export const ResNet50 = Vue.extend({
|
||||
methods: {
|
||||
|
||||
toggleGpu: function () {
|
||||
this.model.gpu = !this.useGpu
|
||||
this.model.toggleGpu(!this.useGpu)
|
||||
},
|
||||
|
||||
imageURLInputChanged: function (e) {
|
||||
|
||||
@@ -38,6 +38,20 @@ export default class Layer {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle GPU mode on/off
|
||||
* weblas must be available
|
||||
* @param {boolean} mode - on/off
|
||||
*/
|
||||
toggleGpu (mode) {
|
||||
const newMode = typeof mode === 'undefined' ? !this._useWeblas : mode
|
||||
if (newMode && weblas) {
|
||||
this._useWeblas = true
|
||||
} else {
|
||||
this._useWeblas = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for layer computational logic
|
||||
* @param {Tensor} x
|
||||
|
||||
@@ -287,6 +287,22 @@ export default class Model {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle GPU mode on/off
|
||||
* Iterate through all layers and set `gpu` attribute
|
||||
* @param {boolean} mode - on/off
|
||||
*/
|
||||
toggleGpu (mode) {
|
||||
if (typeof mode === 'undefined') {
|
||||
this.gpu = !this.gpu
|
||||
} else {
|
||||
this.gpu = mode
|
||||
}
|
||||
for (let layer of this.modelLayersMap.values()) {
|
||||
layer.toggleGpu(this.gpu)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Async function for recursively traversing the DAG
|
||||
* Graph object is stored in `this.modelDAG`, keyed by layer name.
|
||||
|
||||
@@ -204,6 +204,9 @@ export default class Convolution2D extends Layer {
|
||||
x.tensor = x.tensor.transpose(1, 2, 0)
|
||||
}
|
||||
|
||||
let logging = false
|
||||
if (['conv1', 'res2c_branch2a', 'res4e_branch2b', 'res3d_branch2b', 'res2b_branch2a', 'res4d_branch2c', 'res3c_branch2c'].includes(this.name)) logging = true
|
||||
|
||||
this._calcOutputShape(x)
|
||||
this._padInput(x)
|
||||
|
||||
@@ -213,7 +216,7 @@ export default class Convolution2D extends Layer {
|
||||
imColsMat.createWeblasTensor()
|
||||
}
|
||||
let endTime = performance.now()
|
||||
if (this.name === 'conv1') console.log('imColsMat', endTime - startTime)
|
||||
if (logging) console.log(this.name, 'imColsMat', endTime - startTime)
|
||||
|
||||
startTime = performance.now()
|
||||
const nbFilter = this.kernelShape[0]
|
||||
@@ -239,7 +242,7 @@ export default class Convolution2D extends Layer {
|
||||
gemm(matMul.tensor, imColsMat.tensor, this._wRowsMat.tensor, 1, 1)
|
||||
}
|
||||
endTime = performance.now()
|
||||
if (this.name === 'conv1') console.log('gemm', endTime - startTime)
|
||||
if (logging) console.log(this.name, 'gemm', endTime - startTime)
|
||||
|
||||
startTime = performance.now()
|
||||
let output = new Tensor([], this.outputShape)
|
||||
@@ -252,12 +255,10 @@ export default class Convolution2D extends Layer {
|
||||
}
|
||||
x.tensor = output.tensor
|
||||
endTime = performance.now()
|
||||
if (this.name === 'conv1') console.log('createOutput', endTime - startTime)
|
||||
if (logging) console.log(this.name, 'nbFilter', nbFilter)
|
||||
if (logging) console.log(this.name, 'createOutput', endTime - startTime)
|
||||
|
||||
startTime = performance.now()
|
||||
this.activation(x)
|
||||
endTime = performance.now()
|
||||
if (this.name === 'conv1') console.log('activation', endTime - startTime)
|
||||
|
||||
// convert back to th ordering if necessary
|
||||
if (this.dimOrdering === 'th') {
|
||||
|
||||
Reference in New Issue
Block a user