Skip to content

Commit

Permalink
Update Deeplearning4j test files (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Jul 2, 2023
1 parent 9d409af commit f9a0b41
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 39 deletions.
86 changes: 55 additions & 31 deletions source/dl4j.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dl4j.ModelFactory = class {
}
}
if (identifier === 'coefficients.bin') {
const signature = [ 0x00, 0x07, 0x4A, 0x41, 0x56, 0x41, 0x43, 0x50, 0x50 ];
const signature = [ 0x00, 0x07, 0x4A, 0x41, 0x56, 0x41, 0x43, 0x50, 0x50 ]; // JAVACPP
const stream = context.stream;
if (signature.length <= stream.length && stream.peek(signature.length).every((value, index) => value === signature[index])) {
return 'dl4j.coefficients';
Expand Down Expand Up @@ -71,7 +71,8 @@ dl4j.Graph = class {
this._inputs = [];
this._outputs =[];
this._nodes = [];
const dataType = coefficients ? new dl4j.NDArrayReader(coefficients).dataType : '?';
coefficients = coefficients ? new dl4j.NDArray(coefficients) : null;
const dataType = coefficients ? coefficients.dataType : '?';
const values = new Map();
const value = (name, type, tensor) => {
if (name.length === 0 && tensor) {
Expand Down Expand Up @@ -425,60 +426,77 @@ dl4j.TensorShape = class {
}
};

dl4j.NDArrayReader = class {
dl4j.NDArray = class {

constructor(buffer) {
const reader = new dl4j.BinaryReader(buffer);
const dataTypes = new Map([
[ 'INT', [ 'int32', 4 ] ],
[ 'FLOAT', [ 'float32', 4 ] ],
[ 'DOUBLE', [ 'float64', 8 ] ]
]);
const read = (reader) => {
const header = {};
header.alloc = reader.string();
header.length = 0;
switch (header.alloc) {
const readHeader = (reader) => {
const alloc = reader.string();
let length = 0;
switch (alloc) {
case 'DIRECT':
case 'HEAP':
case 'JAVACPP':
header.length = reader.int32();
length = reader.int32();
break;
case 'LONG_SHAPE':
case 'MIXED_DATA_TYPES':
header.length = reader.int64();
length = reader.int64();
break;
default:
throw new dl4j.Error("Unsupported header alloc '" + header.alloc + "'.");
throw new dl4j.Error("Unsupported header alloc '" + alloc + "'.");
}
const type = reader.string();
if (!dataTypes.has(type)) {
throw new dl4j.Error("Unsupported header type '" + type + "'.");
}
const entry = dataTypes.get(type);
header.type = entry[0];
header.itemsize = entry[1];
header.data = reader.read(header.itemsize * header.length);
return header;
return [ alloc, length, type ];
};
/* let shape = */ read(reader);
const data = read(reader);
this._dataType = data.type;
}

get dataType() {
return this._dataType;
const headerShape = readHeader(reader);
if (headerShape[2] !== 'INT') {
throw new dl4j.Error("Unsupported header shape type '" + headerShape[2] + "'.");
}
const shapeInfo = new Array(headerShape[1]);
for (let i = 0; i < shapeInfo.length; i++) {
shapeInfo[i] = reader.int32();
}
const rank = shapeInfo[0];
const shapeInfoLength = rank * 2 + 4;
this.shape = shapeInfo.slice(1, 1 + rank);
this.strides = shapeInfo.slice(1 + rank, 1 + (rank * 2));
this.order = shapeInfo[shapeInfoLength - 1];
const headerData = readHeader(reader);
const dataTypes = new Map([
[ 'INT', [ 'int32', 4 ] ],
[ 'FLOAT', [ 'float32', 4 ] ],
[ 'DOUBLE', [ 'float64', 8 ] ]
]);
if (!dataTypes.has(headerData[2])) {
throw new dl4j.Error("Unsupported header data type '" + headerShape[2] + "'.");
}
const dataType = dataTypes.get(headerData[2]);
this.dataType = dataType[0];
const size = headerData[1] * dataType[1];
if ((reader.position + size) <= reader.length) {
this.data = reader.read(size);
}
}
};

dl4j.BinaryReader = class {

constructor(buffer) {
this._buffer = buffer;
this._length = buffer.length;
this._position = 0;
this._view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
}

get length() {
return this._length;
}

get position() {
return this._position;
}

read(size) {
const data = this._buffer.subarray(this._position, this._position + size);
this._position += size;
Expand All @@ -503,6 +521,12 @@ dl4j.BinaryReader = class {
this._position += 4;
return this._view.getInt64(position, false).toNumber();
}

float32() {
const position = this._position;
this._position += 4;
return this._view.getFloat32(position, false);
}
};

dl4j.Error = class extends Error {
Expand Down
16 changes: 8 additions & 8 deletions test/models.json
Original file line number Diff line number Diff line change
Expand Up @@ -1870,9 +1870,9 @@
{
"type": "dl4j",
"target": "nasnetmobile_dl4j_inference.v1.zip",
"source": "https://dl4jdata.blob.core.windows.net/models/nasnetmobile_dl4j_inference.v1.zip",
"source": "https://github.com/lutzroeder/netron/files/11928662/nasnetmobile_dl4j_inference.v1.zip",
"format": "Deeplearning4j",
"link": "https://github.com/eclipse/deeplearning4j"
"link": "https://github.com/lutzroeder/netron/issues/303"
},
{
"type": "dl4j",
Expand All @@ -1884,23 +1884,23 @@
{
"type": "dl4j",
"target": "tiny-yolo-voc_dl4j_inference.v2.zip",
"source": "https://dl4jdata.blob.core.windows.net/models/tiny-yolo-voc_dl4j_inference.v2.zip",
"source": "https://github.com/lutzroeder/netron/files/11928661/tiny-yolo-voc_dl4j_inference.v2.zip",
"format": "Deeplearning4j",
"link": "https://github.com/eclipse/deeplearning4j"
"link": "https://github.com/lutzroeder/netron/issues/303"
},
{
"type": "dl4j",
"target": "unet_dl4j_segment_inference.v1.zip",
"source": "https://dl4jdata.blob.core.windows.net/models/unet_dl4j_segment_inference.v1.zip",
"source": "https://github.com/lutzroeder/netron/files/11928659/unet_dl4j_segment_inference.v1.zip",
"format": "Deeplearning4j",
"link": "https://github.com/eclipse/deeplearning4j"
"link": "https://github.com/lutzroeder/netron/issues/303"
},
{
"type": "dl4j",
"target": "xception_dl4j_inference.v2.zip",
"source": "https://dl4jdata.blob.core.windows.net/models/xception_dl4j_inference.v2.zip",
"source": "https://github.com/lutzroeder/netron/files/11928660/xception_dl4j_inference.v2.zip",
"format": "Deeplearning4j",
"link": "https://github.com/eclipse/deeplearning4j"
"link": "https://github.com/lutzroeder/netron/issues/303"
},
{
"type": "dlc",
Expand Down

0 comments on commit f9a0b41

Please sign in to comment.