Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a warning if progress is provided but no patch size is provided #221

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,18 @@ upscaler.upscale('/path/to/img', {
})
```

### Progress Specified Without Patch Size

If you've specified a `progress` callback but are not specifying `patchSize` in the call to `upscale`, the `progress` callback will never be called. `progress` callbacks only occur when `patchSize` is provided.

In order to have your `progress` callback be called, provide explicit patch sizes:

```
upscaler.upscale('/path/to/img', {
patchSize: 64,
progress: ...
})
```
## Contributions

Contributions are welcome! Please follow the existing conventions, use the linter, add relevant tests, and add relevant documentation.
Expand Down
24 changes: 23 additions & 1 deletion packages/upscalerjs/src/upscale.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import upscale, {
getCopyOfInput,
getProcessedPixels,
concatTensors,
WARNING_PROGRESS_WITHOUT_PATCH_SIZE,
WARNING_UNDEFINED_PADDING,
} from './upscale';
import * as tensorAsBase from 'tensor-as-base64';
import * as image from './image.generated';
Expand Down Expand Up @@ -1122,6 +1124,7 @@ describe('predict', () => {
});

it('should callback with progress on patchSize', async () => {
console.warn = jest.fn();
const img: tf.Tensor4D = tf.ones([4, 4, 3,]).expandDims(0);
const scale = 2;
const patchSize = 2;
Expand All @@ -1142,6 +1145,7 @@ describe('predict', () => {
expect(progress).toHaveBeenCalledWith(0.5);
expect(progress).toHaveBeenCalledWith(0.75);
expect(progress).toHaveBeenCalledWith(1);
expect(console.warn).not.toHaveBeenCalled();
});

it('should warn if provided a patch size without a padding', async () => {
Expand All @@ -1159,7 +1163,7 @@ describe('predict', () => {
await predict(model, img, { scale, } as IModelDefinition, {
patchSize,
});
expect(console.warn).toHaveBeenCalled();
expect(console.warn).toHaveBeenCalledWith(WARNING_UNDEFINED_PADDING);
});
});

Expand Down Expand Up @@ -1209,4 +1213,22 @@ describe('upscale', () => {
}
expect(result.dataSync()).toEqual(upscaledTensor.dataSync());
});

it('should warn if provided a progress callback without patchSize', async () => {
console.warn = jest.fn();
const img: tf.Tensor4D = tf.ones([4, 4, 3,]).expandDims(0);
const scale = 2;
const patchSize = 2;
const model = {
predict: jest.fn((pixel) => {
return tf
.fill([patchSize * scale, patchSize * scale, 3,], pixel.dataSync()[0])
.expandDims(0);
}),
} as unknown as tf.LayersModel;
await predict(model, img, { scale, } as IModelDefinition, {
progress: () => {},
});
expect(console.warn).toHaveBeenCalledWith(WARNING_PROGRESS_WITHOUT_PATCH_SIZE);
});
});
32 changes: 22 additions & 10 deletions packages/upscalerjs/src/upscale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,25 @@ import tensorAsBase64 from 'tensor-as-base64';
import { warn, isTensor, } from './utils';
import type { GetImageAsTensorInput, } from './image.generated';

const ERROR_UNDEFINED_PADDING =
const WARNING_UNDEFINED_PADDING_URL =
'https://thekevinscott.github.io/UpscalerJS/#/?id=padding-is-undefined';

export const WARNING_UNDEFINED_PADDING = [
'"padding" is undefined, but "patchSize" is explicitly defined.',
'Without padding, patches of images often have visible artifacting at the seams. Defining an explicit padding will resolve the artifacting.',
`For more information, see ${WARNING_UNDEFINED_PADDING_URL}.`,
'To hide this warning, pass an explicit padding of "0".',
].join('\n');

const WARNING_PROGRESS_WITHOUT_PATCH_SIZE_URL =
'https://thekevinscott.github.io/UpscalerJS/#/?id=progress-specified-without-patch-size';

export const WARNING_PROGRESS_WITHOUT_PATCH_SIZE = [
'The "progress" callback was provided but "patchSize" was not defined.',
'Without a "patchSize", the "progress" callback will never be called.',
`For more information, see ${WARNING_PROGRESS_WITHOUT_PATCH_SIZE_URL}.`,
].join('\n');

const getWidthAndHeight = (tensor: tf.Tensor3D | tf.Tensor4D) => {
if (tensor.shape.length === 4) {
return tensor.shape.slice(1, 3);
Expand Down Expand Up @@ -212,12 +228,7 @@ export const predict = async (

if (patchSize) {
if (padding === undefined) {
warn([
'"padding" is undefined, but "patchSize" is explicitly defined.',
'Without padding, patches of images often have visible artifacting at the seams. Defining an explicit padding will resolve the artifacting.',
`For more information, see ${ERROR_UNDEFINED_PADDING}.`,
'To hide this warning, pass an explicit padding of "0".',
]);
warn(WARNING_UNDEFINED_PADDING);
}
const channels = 3;
const [height, width,] = pixels.shape.slice(1);
Expand Down Expand Up @@ -291,11 +302,12 @@ export const predict = async (
return squeezedTensor;
}

if (progress) {
warn(WARNING_PROGRESS_WITHOUT_PATCH_SIZE);
}

return tf.tidy(() => {
const pred = model.predict(pixels) as tf.Tensor4D;
if (progress) {
progress(1);
}
/* eslint-disable @typescript-eslint/no-unnecessary-type-assertion */
return pred.squeeze() as tf.Tensor3D;
});
Expand Down