Skip to content

Commit

Permalink
Update view.js (#1240)
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Jul 15, 2024
1 parent b27447d commit d7f5072
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 28 deletions.
71 changes: 53 additions & 18 deletions source/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3361,22 +3361,21 @@ view.TensorSidebar = class extends view.ObjectSidebar {
}
}
}
/*
// Metrics
if (value.initializer) {
if (!tensor.empty) {
if (!this._tensor.empty) {
if (!this._metrics) {
this._metrics = new metrics.Tensor(this._tensor);
}
this.addHeader('Metrics');
const metrics = this._metrics.metrics;
for (const metric of metrics) {
const value = metric.type === 'percentage' ? `${(metric.value * 100).toFixed(1)}%` : metric.value;
this.addProperty(metric.name, [value]);
if (this._metrics.metrics.length > 0) {
this.addHeader('Metrics');
for (const metric of this._metrics.metrics) {
const value = metric.type === 'percentage' ? `${(metric.value * 100).toFixed(1)}%` : metric.value;
this.addProperty(metric.name, [value]);
}
}
}
}
*/
}

activate() {
Expand Down Expand Up @@ -5014,17 +5013,24 @@ metrics.Tensor = class {

constructor(tensor) {
this._tensor = tensor;
this._metrics = null;
}

get metrics() {
if (!this._metrics) {
const tensor = this._tensor;
const data = tensor.value;
this._metrics = Array.from(tensor.metrics || []);
if (this._metrics === null) {
this._metrics = [];
this._metrics = Array.from(this._tensor.metrics || []);
const keys = new Set(this._metrics.map((metrics) => metrics.name));
if (!keys.has('sparsity')) {
const type = this._tensor.type;
const shape = type.shape.dimensions;
const size = shape.reduce((a, b) => a * b, 1);
if (type.dataType.startsWith('float') && size < 0x800000 && (!keys.has('sparsity') || !keys.has('min') || !keys.has('max') && !keys.has('mean') || !keys.has('max') || !keys.has('std'))) {
const data = this._tensor.value;
let zeros = 0;
let parameters = 0;
let min = null;
let max = null;
let sum = 0;
let count = 0;
const stack = [data];
while (stack.length > 0) {
const data = stack.pop();
Expand All @@ -5034,12 +5040,40 @@ metrics.Tensor = class {
}
} else {
zeros += data === 0 || data === 0n || data === '';
parameters += 1;
min = Math.min(data, min === null ? data : min);
max = Math.max(data, max === null ? data : max);
sum += data;
count += 1;
}
}
const mean = sum / count;
if (!keys.has('sparsity')) {
this._metrics.push(new metrics.Argument('min', min, type.dataType));
}
if (!keys.has('max')) {
this._metrics.push(new metrics.Argument('max', max, type.dataType));
}
if (!keys.has('mean')) {
this._metrics.push(new metrics.Argument('mean', mean, type.dataType));
}
if (!keys.has('std')) {
let variance = 0;
const stack = [data];
while (stack.length > 0) {
const data = stack.pop();
if (Array.isArray(data)) {
for (const element of data) {
stack.push(element);
}
} else {
variance += Math.pow(data - mean, 2);
}
}
this._metrics.push(new metrics.Argument('std', Math.sqrt(variance / count)));
}
if (!keys.has('sparsity')) {
this._metrics.push(new metrics.Argument('sparsity', count > 0 ? zeros / count : 0, 'percentage'));
}
const value = parameters > 0 ? zeros / parameters : 0;
const argument = new metrics.Argument('sparsity', value, 'percentage');
this._metrics.push(argument);
}
}
return this._metrics;
Expand Down Expand Up @@ -6135,6 +6169,7 @@ export const View = view.View;
export const ModelFactoryService = view.ModelFactoryService;
export const ModelSidebar = view.ModelSidebar;
export const NodeSidebar = view.NodeSidebar;
export const TensorSidebar = view.TensorSidebar;
export const Documentation = view.Documentation;
export const Formatter = view.Formatter;
export const Tensor = view.Tensor;
Expand Down
12 changes: 2 additions & 10 deletions test/models.json
Original file line number Diff line number Diff line change
Expand Up @@ -5312,6 +5312,7 @@
"source": "https://github.com/lutzroeder/netron/files/11149538/model.ptl.zip[model.ptl]",
"format": "TorchScript v1.6",
"assert": "model.graphs[0].nodes[0].inputs[1].type == 'object'",
"tags": "validation",
"link": "https://github.com/lutzroeder/netron/issues/1067"
},
{
Expand Down Expand Up @@ -5808,7 +5809,7 @@
"source": "https://github.com/lutzroeder/netron/files/9541426/sparse_coo.pth.zip[sparse_coo.pth]",
"format": "PyTorch v1.6",
"assert": "model.graphs[0].nodes[0].inputs[0].value[0].type.layout == 'sparse.coo'",
"tag": "validation",
"tags": "validation",
"link": "https://github.com/lutzroeder/netron/issues/720"
},
{
Expand Down Expand Up @@ -7512,15 +7513,6 @@
"tags": "quantization",
"link": "https://github.com/kosslab-kr/Tizen-NN-Runtime"
},
{
"type": "tnn",
"target": "instance_normalization.tnnproto",
"source": "https://github.com/lutzroeder/netron/files/5124180/instance_normalization.zip[instance_normalization.tnnproto]",
"format": "TNN",
"assert": "model.graphs[0].nodes[0].inputs.length == 1",
"tags": "validation",
"link": "https://github.com/lutzroeder/netron/pull/583"
},
{
"type": "tnn",
"target": "squeezenet_v1.1.tnnmodel,squeezenet_v1.1.tnnproto",
Expand Down
6 changes: 6 additions & 0 deletions test/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,12 @@ export class Target {
for (const value of input.value) {
validateValue(value);
}
if (this.tags.has('validation')) {
if (input.value.length === 1 && input.value[0].initializer) {
const sidebar = new view.TensorSidebar(this.view, input);
sidebar.render();
}
}
}
}
for (const output of node.outputs) {
Expand Down

0 comments on commit d7f5072

Please sign in to comment.