Skip to content

Commit

Permalink
Add Core ML test files (#193) (#1149)
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Sep 10, 2023
1 parent 8a1a43d commit ad25b2f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
16 changes: 11 additions & 5 deletions source/coreml.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,21 @@ coreml.ModelFactory = class {
}
};
walkModel(model);
const weights = new Map();
if (weightPaths.size > 0) {
const folder = path.replace(/\/[^/]*$/, '');
const keys = Array.from(weightPaths);
const paths = keys.map((path) => path.replace(/^@model_path\//, folder + '/'));
try {
const streams = await Promise.all(paths.map((path) => context.request(path, null)));
const weights = new Map();
for (let i = 0; i < keys.length; i++) {
weights.set(keys[i], streams[i]);
}
return new coreml.Model(metadata, format, model, weights);
} catch (error) {
return new coreml.Model(metadata, format, model, new Map());
// continue regardless of error
}
}
return new coreml.Model(metadata, format, model, new Map());
return new coreml.Model(metadata, format, model, weights);
};
const openManifest = async (obj, context, path) => {
const entries = Object.values(obj.itemInfoEntries).filter((entry) => entry.path.toLowerCase().endsWith('.mlmodel'));
Expand Down Expand Up @@ -229,11 +228,14 @@ coreml.Graph = class {
// TODO: need to handle functions other than main?
const main = program.functions.main;
// TODO: need to handle more than one block specialization?
const block = main.block_specializations.CoreML5 || main.block_specializations.CoreML6;
const block_specializations = main.block_specializations;
const key = Object.keys(block_specializations).filter((key) => key.startsWith('CoreML')).shift();
const block = block_specializations[key];
const convertValue = (value) => {
switch (value.value) {
case 'immediateValue': {
const tensor = value.immediateValue.tensor;
const type = coreml.Utility.valueType(value.type);
let values = null;
switch (tensor.value) {
case 'ints':
Expand All @@ -254,6 +256,9 @@ coreml.Graph = class {
default:
throw new coreml.Error("Unsupported tensor value '" + tensor.value + "'.");
}
if (type.shape.dimensions.length === 0) {
values = values[0];
}
return values;
}
case 'blobFileValue': {
Expand All @@ -280,6 +285,7 @@ coreml.Graph = class {
break;
}
case 'float16':
case 'int8':
case 'uint8': {
data = stream.read(size);
break;
Expand Down
15 changes: 11 additions & 4 deletions test/models.json
Original file line number Diff line number Diff line change
Expand Up @@ -1089,14 +1089,14 @@
"target": "EfficientNetB0.mlpackage,EfficientNetB0.mlpackage/Manifest.json,EfficientNetB0.mlpackage/Data/com.apple.CoreML/model.mlmodel,EfficientNetB0.mlpackage/Data/com.apple.CoreML/weights/weight.bin",
"source": "https://github.com/lutzroeder/netron/files/6636195/EfficientNetB0.mlpackage.zip[.,EfficientNetB0.mlpackage/Manifest.json,EfficientNetB0.mlpackage/Data/com.apple.CoreML/model.mlmodel,EfficientNetB0.mlpackage/Data/com.apple.CoreML/weights/weight.bin]",
"format": "Core ML Package v6",
"link": "https://github.com/lutzroeder/netron/issues/751"
"link": "https://github.com/lutzroeder/netron/issues/193"
},
{
"type": "coreml",
"target": "EfficientNetB0.mlpackage.zip",
"source": "https://github.com/lutzroeder/netron/files/6636195/EfficientNetB0.mlpackage.zip",
"format": "Core ML Package v6",
"link": "https://github.com/lutzroeder/netron/issues/751"
"link": "https://github.com/lutzroeder/netron/issues/193"
},
{
"type": "coreml",
Expand Down Expand Up @@ -1478,9 +1478,16 @@
{
"type": "coreml",
"target": "sentiment_model.mlmodel",
"source": "https://github.com/lutzroeder/netron/files/2591618/sentiment_model.mlmodel.zip[sentiment_model.mlmodel]",
"source": "https://github.com/lutzroeder/netron/files/12569013/sentiment_model.mlmodel.zip[sentiment_model.mlmodel]",
"format": "Core ML v1",
"link": "https://github.com/lutzroeder/netron/issues/149"
"link": "https://github.com/lutzroeder/netron/issues/193"
},
{
"type": "coreml",
"target": "simple_activation_quantized_model.mlpackage.zip",
"source": "https://github.com/lutzroeder/netron/files/12568886/simple_activation_quantized_model.mlpackage.zip",
"format": "Core ML Package v8",
"link": "https://github.com/lutzroeder/netron/issues/193"
},
{
"type": "coreml",
Expand Down

0 comments on commit ad25b2f

Please sign in to comment.