Skip to content

Commit

Permalink
v0.12.0 (#235)
Browse files Browse the repository at this point in the history
* Refactor to generator (#234)

* Refactor predict to be a generator function

* Refactor upscale function into a generator (#236)

* Move yielding to wrapper upscale function (#237)

* Add abort signal to enable cancellation (#238)

* Update version script (#239)

* Update version script to also update dependencies

* Cancel method integration test (#240)

* Migrate progress test to upscale

* Add integration test for cancel

* Refactor upscale method to accept an internal args object (#241)

* Refactor predict function (#243)

* Refactor predict function

* Insist on parameters for sub upscale functions

* Refactor upscaling code to have better types around async generator return values (#242)

* Rearrange upscale functions to be typed better and add a memory leak test

* Set up a generator wrap function (#244)

* Set up a generator wrap function

* Add test for wrapGenerator function

* Strengthen tests around generator wrap

* Remove console logs

* Add test to cover console warn

* Add coverage to isAborted method

* Cancel method memory leak test (#245)

* Add unit test for memory allocation in predict function

* Update docs (#246)

* Fix examples (#247)

* Bump to latest version and add babelrc

* Migrate examples to use esbuild instead of parcel

* Remove yarnrc from repo

* Ignore yarnrc

* Cancel example (#248)

* Add abort signal to enable cancellation

* Initial commit for cancel example

* Merge upstream

* Implement cancel example

* Initialize value as undefined (#249)

* Add top level abort (#250)

* Add a top level abort

* Add an integration test for cancel

* Update documentation

* Fix deepsource issue

* Add exception for consoles (#251)
  • Loading branch information
thekevinscott authored Mar 5, 2022
1 parent abd7a18 commit 073f991
Show file tree
Hide file tree
Showing 51 changed files with 2,580 additions and 1,546 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ upscaled.png
scratch
packages/upscalerjs/src/tfjs.ts
*.generated.ts
.vscode
.yarnrc
27 changes: 27 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ upscaler.upscale('/path/to/image', {
* `padding` (`number`) - Extra padding to be applied to the patch size during inference.
* `progress` (`(amount: number, slice?: src|tensor) => void`) - A progress callback denoting the percentage complete.
* `progressOutput` (`src|tensor`) - An optional value that sets the return type of the second argument of progress
* `signal` (`AbortSignal`) - An optional signal that allows cancellation of an in-flight upscale request

The `progress` callback optionally returns a second argument with the processed slice of the image:

Expand All @@ -262,6 +263,22 @@ upscaler.upscale('/path/to/image', {
```

The `slice` format will be a base64 string or a tensor corresponding to the value of `output`. This can be overridden by providing an additional property, `progressOutput`, of the form `src | tensor` that will override the value set in `output`.

You can cancel an `upscale` request by providing an `AbortSignal`:

javascript```
const abortController = new AbortController();
upscaler.upscale('/path/to/image', {
signal: abortController.signal,
}).catch(err => {
// I have been cancelled.
});
... some time later
abortController.abort();
```
It's worth noting that calls to `model.predict()` in Tensorflow.js cannot be aborted; if you wish to enable the ability to cancel an inflight request, specifying patch sizes will periodically allow the `upscale` request to release and potentially abort.
### `warmup`
If desired, the model can be "warmed up" after instantiation by calling `warmup` directly.
Expand Down Expand Up @@ -310,6 +327,16 @@ Disposes the current model. Must be called to free up memory when the Upscaler i
await upscaler.dispose();
```

### `abort`

Aborts all inflight `upscale` calls.

#### Example

```javascript
await upscaler.abort();
```

## Troubleshooting

### You must provide an explicit scale
Expand Down
9 changes: 3 additions & 6 deletions examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
"description": "Demonstration of basic usage of UpscalerJS",
"main": "index.js",
"scripts": {
"start": "cross-env NODE_ENV=development parcel index.html --no-hmr --open"
"copy": "rm -rf dist && mkdir dist && cp index.html dist && cp flower.png dist",
"start": "yarn copy && esbuild index.js --servedir=dist --outdir=dist --bundle --loader:.png=dataurl"
},
"author": "Kevin Scott",
"license": "MIT",
"devDependencies": {
"cross-env": "^7.0.2",
"parcel": "1.12.3"
},
"dependencies": {
"@tensorflow/tfjs": "^3.13.0",
"upscaler": "0.9.0"
"upscaler": "0.11.0"
},
"browserslist": {
"production": [
Expand Down
5 changes: 5 additions & 0 deletions examples/cancel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Cancel Example

Demonstrates how to cancel an inflight `upscale` request.

[See this live](https://githubbox.com/thekevinscott/upscalerjs/tree/main/examples/cancel).
File renamed without changes
65 changes: 65 additions & 0 deletions examples/cancel/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<html>
<head>
<title>Cancel Example | Upscaler.JS</title>
<style>
body {
padding: 40px;
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}
button {
margin-top: 20px;
display: block;
padding: 10px 40px;
float: left;
}
#info {
clear: both;
display: block;
}
#target {
background: #EEE;
border: 1px solid #DDD;
display: inline-block;
width: 256px;
height: 256px;
}
#flower {
border: 1px solid #DDD;
}
pre {
padding: 10px;
background: #EEE;
border: 1px solid #DDD;
border-radius: 5px;
}
table td {
vertical-align: top;
}
</style>
</head>
<body>
<h1>Cancel Example</h1>
<p>An example demonstrating how to cancel an inflight request of the upscale method.</p>
<p>Click the upscale button (it is purposefully slow) to upscale the photo, and then click cancel to cancel it.</p>
<button id="upscale">Upscale</button>
<button id="cancel">Cancel</button>
<p id="info"></p>
<table>
<thead>
<tr><td>Original</td><td>Upscaled</td></tr>
</thead>
<tbody>
<tr><td>
<img src="/flower.png" id="flower" />
</td>
<td>
<div id="target">

</div>
</td>
</tr>
</tbody>
</table>
<script src="./index.js"></script>
</body>
</html>
39 changes: 39 additions & 0 deletions examples/cancel/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Upscaler from 'upscaler';
import img from './flower.png';
const target = document.getElementById('target');
const upscale = document.getElementById('upscale');
const cancel = document.getElementById('cancel');
const info = document.getElementById('info');

let abortController = new AbortController();
const upscaler = new Upscaler();
upscale.onclick = () => {
info.innerText = 'Upscaling...';
const start = new Date().getTime();
let rate = 0;
upscaler.upscale(img, {
patchSize: 16,
padding: 4,
signal: abortController.signal,
progress: (inProgressRate) => {
rate = inProgressRate.toFixed(2);
info.innerText = `Upscaling (${rate}%)...`;
},
}).then((upscaledImgSrc) => {
const img = document.createElement('img');
img.src = upscaledImgSrc;
target.innerHTML = '';
target.appendChild(img);
const ms = new Date().getTime() - start;
info.innerText = `Upscaled in ${ms} ms`;
}).catch(err => {
console.log('The AbortError:', err);
info.innerText = `Canceled at ${rate}%`;
});
};

cancel.onclick = () => {
console.log('canceled');
abortController.abort();
abortController = new AbortController();
}
28 changes: 28 additions & 0 deletions examples/cancel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "cancel",
"version": "0.11.0",
"description": "Demonstrates how to cancel an inflight upscale request.",
"main": "index.js",
"scripts": {
"copy": "rm -rf dist && mkdir dist && cp index.html dist && cp flower.png dist",
"start": "yarn copy && esbuild index.js --servedir=dist --outdir=dist --bundle --loader:.png=dataurl"
},
"author": "Kevin Scott",
"license": "MIT",
"dependencies": {
"@tensorflow/tfjs": "^3.13.0",
"upscaler": "0.11.0"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
5 changes: 5 additions & 0 deletions examples/cancel/sandbox.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"infiniteLoopProtection": false,
"hardReloadOnChange": true,
"view": "browser"
}
9 changes: 3 additions & 6 deletions examples/comparisons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
"description": "Demonstration of the different models of UpscalerJS",
"main": "index.js",
"scripts": {
"start": "cross-env NODE_ENV=development parcel index.html --no-hmr --open"
"copy": "rm -rf dist && mkdir dist && cp index.html dist && cp flower.png dist",
"start": "yarn copy && esbuild index.js --servedir=dist --outdir=dist --bundle --loader:.png=dataurl"
},
"author": "Kevin Scott",
"license": "MIT",
"devDependencies": {
"cross-env": "^7.0.2",
"parcel": "1.12.3"
},
"dependencies": {
"@tensorflow/tfjs": "^3.13.0",
"upscaler": "0.9.0"
"upscaler": "0.11.0"
},
"browserslist": {
"production": [
Expand Down
2 changes: 1 addition & 1 deletion examples/models/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "^4.0.2",
"upscaler": "0.9.0"
"upscaler": "0.11.0"
},
"scripts": {
"start": "react-scripts start"
Expand Down
2 changes: 1 addition & 1 deletion examples/nodejs-custom-model/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"dependencies": {
"@upscalerjs/models": "0.9.0",
"@tensorflow/tfjs-node": "^3.13.0",
"upscaler": "0.10.0-canary.1",
"upscaler": "0.11.0",
"express": "4.16.4"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"dependencies": {
"@tensorflow/tfjs-node": "^3.13.0",
"upscaler": "0.10.0-canary.1",
"upscaler": "0.11.0",
"express": "4.16.4"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/patch-sizes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"react-input-range": "^1.3.0",
"react-scripts": "^4.0.3",
"tensor-as-base64": "^0.1.1",
"upscaler": "0.9.0"
"upscaler": "0.11.0"
},
"scripts": {
"start": "PORT=1234 react-scripts start"
Expand Down
9 changes: 3 additions & 6 deletions examples/progress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
"description": "Demonstration of upscale progress with UpscalerJS",
"main": "index.js",
"scripts": {
"start": "cross-env NODE_ENV=development parcel index.html --no-hmr --open"
"copy": "rm -rf dist && mkdir dist && cp index.html dist && cp flower.png dist",
"start": "yarn copy && esbuild index.js --servedir=dist --outdir=dist --bundle --loader:.png=dataurl"
},
"author": "Kevin Scott",
"license": "MIT",
"devDependencies": {
"cross-env": "^7.0.2",
"parcel": "1.12.3"
},
"dependencies": {
"@tensorflow/tfjs": "^3.13.0",
"upscaler": "0.9.0"
"upscaler": "0.11.0"
},
"browserslist": {
"production": [
Expand Down
2 changes: 1 addition & 1 deletion examples/react-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"react-dom": "^16.13.1",
"react-dropzone": "^11.0.2",
"react-scripts": "^4.0.2",
"upscaler": "0.9.0"
"upscaler": "0.11.0"
},
"scripts": {
"start": "react-scripts start"
Expand Down
9 changes: 3 additions & 6 deletions examples/tensor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@
"description": "Demonstration of basic usage of UpscalerJS",
"main": "index.js",
"scripts": {
"start": "cross-env NODE_ENV=development parcel index.html --no-hmr --open"
"copy": "rm -rf dist && mkdir dist && cp index.html dist && cp flower.png dist",
"start": "yarn copy && esbuild index.js --servedir=dist --outdir=dist --bundle --loader:.png=dataurl"
},
"author": "Kevin Scott",
"license": "MIT",
"devDependencies": {
"cross-env": "^7.0.2",
"parcel": "1.12.3"
},
"dependencies": {
"@tensorflow/tfjs": "^3.13.0",
"upscaler": "0.9.0"
"upscaler": "0.11.0"
},
"browserslist": {
"production": [
Expand Down
10 changes: 3 additions & 7 deletions examples/upload/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@
"description": "Demonstration of uploading an image to UpscalerJS",
"main": "index.js",
"scripts": {
"start": "cross-env NODE_ENV=development parcel index.html --no-hmr --open",
"build": "cross-env NODE_ENV=production parcel build index.html --no-minify --public-url ./"
"copy": "rm -rf dist && mkdir dist && cp index.html dist",
"start": "yarn copy && esbuild index.js --servedir=dist --outdir=dist --bundle --loader:.png=dataurl"
},
"author": "Kevin Scott",
"license": "MIT",
"devDependencies": {
"cross-env": "^7.0.2",
"parcel": "1.12.3"
},
"dependencies": {
"@tensorflow/tfjs": "^3.13.0",
"upscaler": "0.9.0"
"upscaler": "0.11.0"
},
"browserslist": {
"production": [
Expand Down
10 changes: 3 additions & 7 deletions examples/warmup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@
"description": "Demonstration of warming up UpscalerJS",
"main": "index.js",
"scripts": {
"start": "cross-env NODE_ENV=development parcel index.html --no-hmr --open",
"build": "cross-env NODE_ENV=production parcel build index.html --no-minify --public-url ./"
"copy": "rm -rf dist && mkdir dist && cp index.html dist && cp flower.png dist",
"start": "yarn copy && esbuild index.js --servedir=dist --outdir=dist --bundle --loader:.png=dataurl"
},
"author": "Kevin Scott",
"license": "MIT",
"devDependencies": {
"cross-env": "^7.0.2",
"parcel": "1.12.3"
},
"dependencies": {
"@tensorflow/tfjs": "^3.13.0",
"upscaler": "0.9.0"
"upscaler": "0.11.0"
},
"browserslist": {
"production": [
Expand Down
2 changes: 1 addition & 1 deletion examples/webcam/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h1>Webcam Example</h1>
An example of using a webcam for upscaling with
<a target="_blank" href="https://github.com/thekevinscott/UpscalerJS">UpscalerJS</a>.
</p>
<video autoplay="true" id="video" width="400" height="300"></video>
<video autoplay="true" id="video" width="200" height="150"></video>
<p>Click the upscale button to upscale a frame of video.</p>
<button id="button" disabled>Upscale</button>
<p id="info"></p>
Expand Down
2 changes: 1 addition & 1 deletion examples/webcam/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ navigator.mediaDevices
info.innerText = "Upscaling...";
const start = new Date().getTime();
upscaler.upscale(data, {
patchSize: 64,
patchSize: 16,
padding: 4,
}).then(upscaledImgSrc => {
const img = document.createElement("img");
Expand Down
Loading

0 comments on commit 073f991

Please sign in to comment.