|
| 1 | +import { jest } from '@jest/globals' |
| 2 | +jest.retryTimes(3) |
| 3 | + |
| 4 | +import * as ort from 'onnxruntime-web' |
| 5 | +ort.env.wasm.numThreads = 1 |
| 6 | + |
| 7 | +import ONNXExporter from '../../../../../../lib/model/nns/onnx/onnx_exporter.js' |
| 8 | +import lrn from '../../../../../../lib/model/nns/onnx/layer/lrn.js' |
| 9 | +import LRNLayer from '../../../../../../lib/model/nns/layer/lrn.js' |
| 10 | +import Tensor from '../../../../../../lib/util/tensor.js' |
| 11 | + |
| 12 | +describe('export', () => { |
| 13 | + test.each([ |
| 14 | + { input: 'x', channel_dim: -1, n: 3 }, |
| 15 | + { input: ['x'], n: 3 }, |
| 16 | + ])('last channel %p', param => { |
| 17 | + const model = ONNXExporter.createONNXModel() |
| 18 | + lrn.export(model, { type: 'lrn', ...param }, { x: { size: [null, 10, 3] } }) |
| 19 | + const nodes = model.getGraph().getNodeList() |
| 20 | + expect(nodes).toHaveLength(3) |
| 21 | + expect(nodes[0].getOpType()).toBe('Transpose') |
| 22 | + expect(nodes[1].getOpType()).toBe('Transpose') |
| 23 | + expect(nodes[2].getOpType()).toBe('LRN') |
| 24 | + }) |
| 25 | + |
| 26 | + test('first channel', () => { |
| 27 | + const model = ONNXExporter.createONNXModel() |
| 28 | + lrn.export(model, { type: 'lrn', input: 'x', channel_dim: 1, n: 3 }, { x: { size: [null, 10, 3] } }) |
| 29 | + const nodes = model.getGraph().getNodeList() |
| 30 | + expect(nodes).toHaveLength(1) |
| 31 | + expect(nodes[0].getOpType()).toBe('LRN') |
| 32 | + }) |
| 33 | + |
| 34 | + test('invalid channel dim', () => { |
| 35 | + const model = ONNXExporter.createONNXModel() |
| 36 | + expect(() => |
| 37 | + lrn.export(model, { type: 'lrn', input: ['x'], channel_dim: 0, n: 3 }, { x: { size: [null, 10, 3] } }) |
| 38 | + ).toThrow("Not implemented value of attribute 'channel_dim' 0") |
| 39 | + }) |
| 40 | + |
| 41 | + test('require n', () => { |
| 42 | + const model = ONNXExporter.createONNXModel() |
| 43 | + expect(() => |
| 44 | + lrn.export(model, { type: 'lrn', input: ['x'], channel_dim: -1 }, { x: { size: [null, 10, 3] } }) |
| 45 | + ).toThrow("Require attribute 'n'") |
| 46 | + }) |
| 47 | +}) |
| 48 | + |
| 49 | +describe('runtime', () => { |
| 50 | + let session |
| 51 | + afterEach(async () => { |
| 52 | + await session?.release() |
| 53 | + session = null |
| 54 | + }) |
| 55 | + |
| 56 | + test.each([ |
| 57 | + [{ channel_dim: 1, n: 3 }, [null, 4, 3, 3], [1, 4, 3, 3]], |
| 58 | + [{ n: 5 }, [null, 4, 4, 10], [1, 4, 4, 10]], |
| 59 | + [{ alpha: 0.0002, beta: 0.7, k: 2, n: 5 }, [null, 3, 3, 5], [1, 3, 3, 5]], |
| 60 | + ])('lrn %p %p %p', async (param, inSize, actualSize) => { |
| 61 | + const buf = ONNXExporter.dump([{ type: 'input', size: inSize }, { type: 'lrn', ...param }, { type: 'output' }]) |
| 62 | + session = await ort.InferenceSession.create(buf) |
| 63 | + |
| 64 | + const x = Tensor.randn(actualSize) |
| 65 | + const xten = new ort.Tensor('float32', x.value, x.sizes) |
| 66 | + const out = await session.run({ _input: xten }) |
| 67 | + const yten = out._lrn |
| 68 | + expect(yten.dims).toEqual(actualSize) |
| 69 | + const y = await yten.getData(true) |
| 70 | + |
| 71 | + const t = new LRNLayer(param).calc(x) |
| 72 | + expect(yten.dims).toEqual(t.sizes) |
| 73 | + for (let i = 0; i < y.length; i++) { |
| 74 | + expect(y[i]).toBeCloseTo(t.value[i]) |
| 75 | + } |
| 76 | + }) |
| 77 | +}) |
0 commit comments