tensorspace-team / tensorspace

Neural network 3D visualization framework, build interactive and intuitive model in browsers, support pre-trained deep learning models from TensorFlow, Keras, TensorFlow.js
https://tensorspace.org
Apache License 2.0
5.07k stars 444 forks source link

Automatically inject layer metrics if there is a pre-trained model #226

Closed syt123450 closed 5 years ago

syt123450 commented 5 years ago

After load() a pre-trained model, it is possible to get layer metrics from loaded model, for example, kernelSize, filters, strides for Conv2d layer, units for Dense layer. In previous TensorSpace API, it is required to configure layer metrics even if TensorSpace model has already load a pre-trained model.

In new TensorSpace API, if TensorSpace model has already loaded a pre-trained model, there is no need to configure deep learning model related attributes, as TensorSpace Layer will automatically get these metrics from pre-trained model and inject them into its relative attributes. This new feature may simplify the TensorSpace usage and optimize the visualization pipeline.

Let's have a quick look at this new feature and make a comparison:

New TensorSpace Usage.

let model = new TSP.models.Sequential( container );
model.add( new TSP.layers.GreyscaleInput() );
model.add( new TSP.layers.Padding2d() );
model.add( new TSP.layers.Conv2d({
  initStatus: "open"
}) );
model.add( new TSP.layers.Pooling2d() );
model.add( new TSP.layers.Conv2d() );
model.add( new TSP.layers.Pooling2d() );
model.add( new TSP.layers.Dense() );
model.add( new TSP.layers.Dense() );
model.add( new TSP.layers.Output1d({
  outputs: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
}) );
model.load({
  type: "tensorflow",
  url: "model.json"
});
model.init();

Previous Usage.

let model = new TSP.models.Sequential( container );
model.add( new TSP.layers.GreyscaleInput({
  shape: [28, 28, 1] 
}) );
model.add( new TSP.layers.Padding2d({
  padding: [2, 2]
}) );
model.add( new TSP.layers.Conv2d({
  kernelSize: 5,
  filters: 6,
  strides: 1,
  initStatus: "open"
}) );
model.add( new TSP.layers.Pooling2d({
  poolSize: [2, 2],
  strides: [2, 2]
}) );
model.add( new TSP.layers.Conv2d({
  kernelSize: 5,
  filters: 16,
  strides: 1
}) );
model.add( new TSP.layers.Pooling2d({
  poolSize: [2, 2],
  strides: [2, 2]
}) );
model.add( new TSP.layers.Dense({
  units: 120
}) );
model.add( new TSP.layers.Dense({
  units: 84
}) );
model.add( new TSP.layers.Output1d({
  units: 10,
  outputs: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
}) );
model.load({
  type: "tensorflow",
  url: "model.json"
});
model.init();

So how about previous layer metrics configuration? New feature has backward compatibility, previous Layer metric configuration can still well.

Use case for layer metrics configuration? As upcoming TensorSpace version will automatically inject layer metrics based on pre-trained model, layer metrics configuration in TensorSpace can be use to create an empty TensorSpace model ( which means that init() a TensorSpace model without a pre-trained model ).

There is a basic model visualization pipeline: data -> training -> pre-trained model -> visualization, generally, this is a synchronous process. By creating an empty TensorSpace model, model visualization and model training can be asynchronous. For example, I will sometimes build an empty model to find inspiration before model training and an empty TensorSpace model may help TensorSpace model preprocessing.

Build an empty visualization model using layer metrics.

let model = new TSP.models.Sequential( container );
model.add( new TSP.layers.GreyscaleInput({
  shape: [28, 28, 1] 
}) );
model.add( new TSP.layers.Padding2d({
  padding: [2, 2]
}) );
model.add( new TSP.layers.Conv2d({
  kernelSize: 5,
  filters: 6,
  strides: 1,
  initStatus: "open"
}) );
model.add( new TSP.layers.Pooling2d({
  poolSize: [2, 2],
  strides: [2, 2]
}) );
model.add( new TSP.layers.Conv2d({
  kernelSize: 5,
  filters: 16,
  strides: 1
}) );
model.add( new TSP.layers.Pooling2d({
  poolSize: [2, 2],
  strides: [2, 2]
}) );
model.add( new TSP.layers.Dense({
  units: 120
}) );
model.add( new TSP.layers.Dense({
  units: 84
}) );
model.add( new TSP.layers.Output1d({
  units: 10,
  outputs: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
}) );
model.init();

If everything goes well, this feature will be released in next version -- v0.6

Related to #213