oidn-web 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,2 +1,145 @@
1
- # oidn-web
2
-
1
+ # Open Image Denoise on the Web
2
+
3
+ This library brings the state-of-the-art AI-based denoising library [Open Image Denoise](https://github.com/RenderKit/oidn) to the web.
4
+
5
+ Many thanks to Max Liani for his series https://maxliani.wordpress.com/2023/03/17/dnnd-1-a-deep-neural-network-dive/. My work is mostly inspired by it.
6
+
7
+ ## How it Works.
8
+
9
+ It uses [tfjs](https://github.com/tensorflow/tfjs) to build the UNet model used by the OIDN. Then use the model to do prediction with a WebGPU backend from the image data.
10
+
11
+ Currently it's only available on the browser with WebGPU enabled.
12
+
13
+ ## How to Use
14
+
15
+ ### Install
16
+
17
+ ```shell
18
+ npm i oidn-web
19
+ ```
20
+
21
+ The TZA weights files are not included in the package. You can find them in this repo or [oidn-weights](https://github.com/RenderKit/oidn-weights).
22
+
23
+ ### Denoise a noisy LDR image
24
+
25
+ ```ts
26
+ import { UNet, initUNetFromURL } from 'oidn-web';
27
+ initUNetFromURL('./weights/rt_ldr.tza').then((unet) => {
28
+ // Read the image data.
29
+ const noisyImageData = noisyCanvas
30
+ .getContext('2d')
31
+ .getImageData(0, 0, width, height);
32
+
33
+ // Tile execute the denoising.
34
+ // If the resolution is high. It will split the input into tiles and execute one tile per frame.
35
+ const abortDenoising = unet.tileExecute({
36
+ // The color input for LDR image is 4 channels.
37
+ // In the format of Uint8ClampedArray or Uint8Array.
38
+ color: { data: noisyImageData, width, height },
39
+ done(denoised) {
40
+ console.log('Finished');
41
+ },
42
+ progress(denoised, tileData, tile) {
43
+ // Put the denoised tile on the output canvas
44
+ outputCtx.putImageData(tileData, tile.x, tile.y);
45
+ }
46
+ });
47
+ });
48
+ ```
49
+
50
+ ### Denoise a noisy HDR image
51
+
52
+ ```ts
53
+ import { UNet, initUNetFromURL } from 'oidn-web';
54
+ initUNetFromURL('./weights/rt_hdr.tza', undefined, {
55
+ // It's hdr input.
56
+ hdr: true
57
+ }).then((unet) => {
58
+ const abortDenoising = unet.tileExecute({
59
+ // The color input for HDR image is 4 channels.
60
+ // In the format of Float32Array.
61
+ color: { data: noisyColor, width, height },
62
+ done(denoised) {
63
+ console.log('Finished');
64
+ },
65
+ progress(denoised, tileData, tile) {
66
+ // The denoised data and tileData has same format with the input.
67
+ }
68
+ });
69
+ });
70
+ ```
71
+
72
+ ### Using AUX Input
73
+
74
+ ```ts
75
+ import { UNet, initUNetFromURL } from 'oidn-web';
76
+ initUNetFromURL('./weights/rt_hdr_alb_nrm.tza', undefined, {
77
+ aux: true,
78
+ hdr: true
79
+ }).then((unet) => {
80
+ const abortDenoising = unet.tileExecute({
81
+ // Same as examples before. noisyColor of HDR image is Float32Array. LDR image is Uint8ClampedArray.
82
+ color: { data: noisyColor, width, height },
83
+ // Normal and albedo are both 4 channels in Uint8ClampedArray.
84
+ normal: { data: normalData, width, height },
85
+ albedo: { data: albedoData, width, height },
86
+
87
+ done(denoised) {
88
+ console.log('Finished');
89
+ },
90
+ progress(denoised, tileData, tile) {
91
+ ///...
92
+ }
93
+ });
94
+ });
95
+ ```
96
+
97
+ ### Integrate into your WebGPU Pipeline
98
+
99
+ If you already have a WebGPU path tracer. You can integrate the oidn-web into your pipeline. It supports input/output gpu buffers to avoid the cost of syncing between CPU and GPU.
100
+
101
+ ```ts
102
+ initUNetFromURL(
103
+ './weights/rt_hdr_alb_nrm.tza',
104
+ {
105
+ // Share GPUDevice and GPUAdapterInfo to the TFJS WebGPU backend
106
+ device,
107
+ adapterInfo
108
+ },
109
+ {
110
+ aux: true,
111
+ hdr: true
112
+ }
113
+ ).then((unet) => {
114
+ const abortDenoising = unet.tileExecute({
115
+ // Inputs are all GPUBuffer
116
+ color: { data: colorBuffer, width, height },
117
+ normal: { data: normalBuffer, width, height },
118
+ albedo: { data: albedoBuffer, width, height },
119
+
120
+ done(denoised) {
121
+ console.log('Finished');
122
+ },
123
+ progress(denoised) {
124
+ // Denoised data is also a GPUBuffer.
125
+ // tileData is undefined if using GPUBuffer as input/output
126
+ }
127
+ });
128
+ });
129
+ ```
130
+
131
+ ### Use smaller and larger weights.
132
+
133
+ OIDN also provides a large weights file, which provides a better quality, and a small weights file, which provides a better performance.
134
+
135
+ ```ts
136
+ // Change the weights file to large and nothing else needs to do.
137
+ initUNetFromURL('./weights/rt_hdr_calb_cnrm_large.tza', ...);
138
+ ```
139
+
140
+ ```ts
141
+ // Change the weights file to small and nothing else needs to do.
142
+ initUNetFromURL('./weights/rt_hdr_alb_nrm_small.tza', ...);
143
+ ```
144
+
145
+ Other combinations can be found in the [oidn-weights](https://github.com/RenderKit/oidn-weights)