Skip to content

Commit

Permalink
release: v1.4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHauschildt committed Feb 7, 2024
1 parent c808d4e commit 2e9fa86
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 40 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"watch": "concurrently \"npm run watch --prefix packages/web\" \"npm run watch --prefix packages/node\"",
"build": "npm run build --workspaces --if-present",
"lint": "npx prettier --write --ignore-unknown .",
"publish:latest": "npm run publish:latest --workspaces --if-present",
"publish:next": "npm run publish:next --workspaces --if-present",
"publish:latest": "npm run publish:latest --workspaces --if-present && npm run deploy-assets",
"publish:next": "npm run publish:next --workspaces --if-present && npm run deploy-assets",
"package:version": "node ./scripts/package-version.mjs",
"deploy-assets": "node -r dotenv/config ./scripts/deploy-assets.mjs",
"tag": "git tag $npm_package_version & git push origin $npm_package_version",
Expand Down
41 changes: 21 additions & 20 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,28 @@
"package:pack": "npm run build && npm pack . --pack-destination ../../releases"
},
"dependencies": {
"onnxruntime-node": "^1.15.1",
"sharp": "^0.32.4",
"@types/lodash": "^4.14.195",
"@types/node": "^20.3.1",
"lodash": "^4.17.21",
"ndarray": "^1.0.19",
"zod": "^3.21.4"
"onnxruntime-node": "~1.17.0",
"sharp": "~0.32.4",
"@types/lodash": "~4.14.195",
"@types/node": "~20.3.1",
"@types/ndarray": "~1.0.14",
"lodash": "~4.17.21",
"ndarray": "~1.0.19",
"zod": "~3.21.4"
},
"devDependencies": {
"assert": "^2.0.0",
"esbuild": "^0.18.18",
"npm-dts": "^1.3.12",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"stream-browserify": "^3.0.0",
"ts-loader": "^9.4.3",
"tslib": "^2.5.3",
"typescript": "^5.1.6",
"util": "^0.12.5",
"webpack": "^5.85.1",
"webpack-cli": "^5.1.4"
"assert": "~2.0.0",
"esbuild": "~0.18.18",
"npm-dts": "~1.3.12",
"os-browserify": "~0.3.0",
"path-browserify": "~1.0.1",
"process": "~0.11.10",
"stream-browserify": "~3.0.0",
"ts-loader": "~9.4.3",
"tslib": "~2.5.3",
"typescript": "~5.1.6",
"util": "~0.12.5",
"webpack": "~5.85.1",
"webpack-cli": "~5.1.4"
}
}
17 changes: 14 additions & 3 deletions packages/node/src/codecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ async function imageDecode(blob: Blob): Promise<NdArray<Uint8Array>> {
case 'image/x-alpha8': {
const width = parseInt(mime.params['width']);
const height = parseInt(mime.params['height']);
return ndarray(await blob.arrayBuffer(), [height, width, 1]);
return ndarray(new Uint8Array(await blob.arrayBuffer()), [
height,
width,
1
]);
}
case 'image/x-rgba8': {
const width = parseInt(mime.params['width']);
const height = parseInt(mime.params['height']);
return ndarray(await blob.arrayBuffer(), [height, width, 4]);
return ndarray(new Uint8Array(await blob.arrayBuffer()), [
height,
width,
4
]);
}
case 'application/octet-stream':
case `image/png`:
Expand Down Expand Up @@ -55,7 +63,10 @@ async function imageEncode(
switch (type) {
case 'image/x-alpha8':
case 'image/x-rgba8': {
const mime = MimeType.create(type, { width, height });
const mime = MimeType.create(type, {
width: width.toString(),
height: height.toString()
});
return new Blob([imageTensor.data], { type: mime.toString() });
}
case `image/png`:
Expand Down
6 changes: 3 additions & 3 deletions packages/node/src/inference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ async function runInference(
);

let alphamask = ndarray(predictionsDict[0].data, [resolution, resolution, 1]);
alphamask = convertFloat32ToUint8(alphamask);
alphamask = tensorResizeBilinear(alphamask, srcWidth, srcHeight);
let alphamaskU8 = convertFloat32ToUint8(alphamask);
alphamaskU8 = tensorResizeBilinear(alphamaskU8, srcWidth, srcHeight);

if (config.progress) config.progress('compute:inference', 1, 1);
return alphamask;
return alphamaskU8;
}
2 changes: 1 addition & 1 deletion packages/node/src/onnx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function runOnnxSession(

for (const key of outputs) {
const output: ort.Tensor = outputData[key];
const shape: Number[] = output.dims as number[];
const shape: number[] = output.dims as number[];
const data: Float32Array = output.data as Float32Array;
const tensor = ndarray(data, shape);
outputKVPairs.push(tensor);
Expand Down
10 changes: 8 additions & 2 deletions packages/node/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ import { loadFromURI } from './resource';
import ndarray, { NdArray } from 'ndarray';
import { imageDecode, imageEncode } from './codecs';

type ImageSource = ArrayBuffer | Uint8Array | Blob | URL | string;
type ImageSource =
| ArrayBuffer
| Uint8Array
| Blob
| URL
| string
| NdArray<Uint8Array>;

function tensorResizeBilinear(
imageTensor: NdArray<Uint8Array>,
Expand Down Expand Up @@ -69,7 +75,7 @@ function tensorResizeBilinear(
}

function tensorHWCtoBCHW(
imageTensor: NdArray<Uint32Array>,
imageTensor: NdArray<Uint8Array>,
mean: number[] = [128, 128, 128],
std: number[] = [256, 256, 256]
): NdArray<Float32Array> {
Expand Down
23 changes: 19 additions & 4 deletions packages/web/src/codecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,32 @@ async function imageDecode(blob: Blob): Promise<NdArray<Uint8Array>> {
case 'image/x-alpha8': {
const width = parseInt(mime.params['width']);
const height = parseInt(mime.params['height']);
return ndarray(await blob.arrayBuffer(), [height, width, 1]);
return ndarray(new Uint8Array(await blob.arrayBuffer()), [
height,
width,
1
]);
}
case 'image/x-rgba8': {
const width = parseInt(mime.params['width']);
const height = parseInt(mime.params['height']);
return ndarray(await blob.arrayBuffer(), [height, width, 4]);
return ndarray(new Uint8Array(await blob.arrayBuffer()), [
height,
width,
4
]);
}
case 'application/octet-stream': // this is an unknwon type
case `image/png`:
case `image/jpeg`:
case `image/webp`: {
const imageBitmap = await createImageBitmap(blob);
const imageData = imageBitmapToImageData(imageBitmap);
return ndarray(imageData.data, [imageData.height, imageData.width, 4]);
return ndarray(new Uint8Array(imageData.data), [
imageData.height,
imageData.width,
4
]);
}
default:
throw new Error(
Expand All @@ -41,7 +53,10 @@ async function imageEncode(
switch (format) {
case 'image/x-alpha8':
case 'image/x-rgba8': {
const mime = MimeType.create(format, { width, height });
const mime = MimeType.create(format, {
width: width.toString(),
height: height.toString()
});
return new Blob([imageTensor.data], { type: mime.toString() });
}
case `image/png`:
Expand Down
6 changes: 3 additions & 3 deletions packages/web/src/inference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ async function runInference(
);

let alphamask = ndarray(predictionsDict[0].data, [resolution, resolution, 1]);
alphamask = convertFloat32ToUint8(alphamask);
alphamask = tensorResizeBilinear(alphamask, srcWidth, srcHeight);
let alphamaskU8 = convertFloat32ToUint8(alphamask);
alphamaskU8 = tensorResizeBilinear(alphamaskU8, srcWidth, srcHeight);

if (config.progress) config.progress('compute:inference', 1, 1);
return alphamask;
return alphamaskU8;
}
2 changes: 1 addition & 1 deletion packages/web/src/onnx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async function runOnnxSession(

for (const key of outputs) {
const output: ort.Tensor = outputData[key];
const shape: Number[] = output.dims as number[];
const shape: number[] = output.dims as number[];
const data: Float32Array = output.data as Float32Array;
const tensor = ndarray(data, shape);
outputKVPairs.push(tensor);
Expand Down
9 changes: 8 additions & 1 deletion packages/web/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ import { imageDecode, imageEncode } from './codecs';
import { ensureAbsoluteURI } from './url';
import { Config } from './schema';

type ImageSource = ImageData | ArrayBuffer | Uint8Array | Blob | URL | string;
type ImageSource =
| ImageData
| ArrayBuffer
| Uint8Array
| Blob
| URL
| string
| NdArray<Uint8Array>;

function imageBitmapToImageData(imageBitmap: ImageBitmap): ImageData {
var canvas = createCanvas(imageBitmap.width, imageBitmap.height);
Expand Down

0 comments on commit 2e9fa86

Please sign in to comment.