This commit is contained in:
luboslenco
2024-02-19 17:07:09 +01:00
parent 3f57cc5330
commit a87a79141b
45 changed files with 1805 additions and 1892 deletions
+56 -57
View File
@@ -106,73 +106,72 @@ class InpaintNode extends LogicNode {
let u8 = new Uint8Array(bytes_img);
let f32mask = new Float32Array(4 * 64 * 64);
data_get_blob("models/sd_vae_encoder.quant.onnx", (vae_encoder_blob: ArrayBuffer) => {
// for (let x = 0; x < Math.floor(image.width / 512); ++x) {
// for (let y = 0; y < Math.floor(image.height / 512); ++y) {
let x = 0;
let y = 0;
let vae_encoder_blob: ArrayBuffer = data_get_blob("models/sd_vae_encoder.quant.onnx");
// for (let x = 0; x < Math.floor(image.width / 512); ++x) {
// for (let y = 0; y < Math.floor(image.height / 512); ++y) {
let x = 0;
let y = 0;
for (let xx = 0; xx < 64; ++xx) {
for (let yy = 0; yy < 64; ++yy) {
// let step = Math.floor(512 / 64);
// let j = (yy * step * mask.width + xx * step) + (y * 512 * mask.width + x * 512);
let step = Math.floor(mask.width / 64);
let j = (yy * step * mask.width + xx * step);
let f = u8[j] / 255.0;
let i = yy * 64 + xx;
f32mask[i ] = f;
f32mask[i + 64 * 64 ] = f;
f32mask[i + 64 * 64 * 2] = f;
f32mask[i + 64 * 64 * 3] = f;
}
for (let xx = 0; xx < 64; ++xx) {
for (let yy = 0; yy < 64; ++yy) {
// let step = Math.floor(512 / 64);
// let j = (yy * step * mask.width + xx * step) + (y * 512 * mask.width + x * 512);
let step = Math.floor(mask.width / 64);
let j = (yy * step * mask.width + xx * step);
let f = u8[j] / 255.0;
let i = yy * 64 + xx;
f32mask[i ] = f;
f32mask[i + 64 * 64 ] = f;
f32mask[i + 64 * 64 * 2] = f;
f32mask[i + 64 * 64 * 3] = f;
}
}
g2_begin(InpaintNode.temp, false);
// g2_drawImage(image, -x * 512, -y * 512);
g2_draw_scaled_image(image, 0, 0, 512, 512);
g2_end();
g2_begin(InpaintNode.temp, false);
// g2_drawImage(image, -x * 512, -y * 512);
g2_draw_scaled_image(image, 0, 0, 512, 512);
g2_end();
let bytes_img = image_get_pixels(InpaintNode.temp);
let u8a = new Uint8Array(bytes_img);
let f32a = new Float32Array(3 * 512 * 512);
for (let i = 0; i < (512 * 512); ++i) {
f32a[i ] = (u8a[i * 4 ] / 255.0) * 2.0 - 1.0;
f32a[i + 512 * 512 ] = (u8a[i * 4 + 1] / 255.0) * 2.0 - 1.0;
f32a[i + 512 * 512 * 2] = (u8a[i * 4 + 2] / 255.0) * 2.0 - 1.0;
}
bytes_img = image_get_pixels(InpaintNode.temp);
let u8a = new Uint8Array(bytes_img);
let f32a = new Float32Array(3 * 512 * 512);
for (let i = 0; i < (512 * 512); ++i) {
f32a[i ] = (u8a[i * 4 ] / 255.0) * 2.0 - 1.0;
f32a[i + 512 * 512 ] = (u8a[i * 4 + 1] / 255.0) * 2.0 - 1.0;
f32a[i + 512 * 512 * 2] = (u8a[i * 4 + 2] / 255.0) * 2.0 - 1.0;
}
let latents_buf = krom_ml_inference(vae_encoder_blob, [f32a.buffer], [[1, 3, 512, 512]], [1, 4, 64, 64], Config.raw.gpu_inference);
let latents = new Float32Array(latents_buf);
for (let i = 0; i < latents.length; ++i) {
latents[i] = 0.18215 * latents[i];
}
let latents_orig = latents.slice(0);
let latents_buf = krom_ml_inference(vae_encoder_blob, [f32a.buffer], [[1, 3, 512, 512]], [1, 4, 64, 64], Config.raw.gpu_inference);
let latents = new Float32Array(latents_buf);
for (let i = 0; i < latents.length; ++i) {
latents[i] = 0.18215 * latents[i];
}
let latents_orig = latents.slice(0);
let noise = new Float32Array(latents.length);
for (let i = 0; i < noise.length; ++i) noise[i] = Math.cos(2.0 * 3.14 * RandomNode.getFloat()) * Math.sqrt(-2.0 * Math.log(RandomNode.getFloat()));
let noise = new Float32Array(latents.length);
for (let i = 0; i < noise.length; ++i) noise[i] = Math.cos(2.0 * 3.14 * RandomNode.getFloat()) * Math.sqrt(-2.0 * Math.log(RandomNode.getFloat()));
let num_inference_steps = 50;
let init_timestep = Math.floor(num_inference_steps * InpaintNode.strength);
let timestep = TextToPhotoNode.timesteps[num_inference_steps - init_timestep];
let alphas_cumprod = TextToPhotoNode.alphas_cumprod;
let sqrt_alpha_prod = Math.pow(alphas_cumprod[timestep], 0.5);
let sqrt_one_minus_alpha_prod = Math.pow(1.0 - alphas_cumprod[timestep], 0.5);
for (let i = 0; i < latents.length; ++i) {
latents[i] = sqrt_alpha_prod * latents[i] + sqrt_one_minus_alpha_prod * noise[i];
}
let num_inference_steps = 50;
let init_timestep = Math.floor(num_inference_steps * InpaintNode.strength);
let timestep = TextToPhotoNode.timesteps[num_inference_steps - init_timestep];
let alphas_cumprod = TextToPhotoNode.alphas_cumprod;
let sqrt_alpha_prod = Math.pow(alphas_cumprod[timestep], 0.5);
let sqrt_one_minus_alpha_prod = Math.pow(1.0 - alphas_cumprod[timestep], 0.5);
for (let i = 0; i < latents.length; ++i) {
latents[i] = sqrt_alpha_prod * latents[i] + sqrt_one_minus_alpha_prod * noise[i];
}
let start = num_inference_steps - init_timestep;
let start = num_inference_steps - init_timestep;
TextToPhotoNode.stableDiffusion(InpaintNode.prompt, (img: image_t) => {
// result.g2_begin(false);
// result.g2_draw_image(img, x * 512, y * 512);
// result.g2_end();
InpaintNode.result = img;
done(img);
}, latents, start, true, f32mask, latents_orig);
// }
TextToPhotoNode.stableDiffusion(InpaintNode.prompt, (img: image_t) => {
// result.g2_begin(false);
// result.g2_draw_image(img, x * 512, y * 512);
// result.g2_end();
InpaintNode.result = img;
done(img);
}, latents, start, true, f32mask, latents_orig);
// }
});
// }
}
static def: zui_node_t = {
+71 -72
View File
@@ -67,87 +67,86 @@ class PhotoToPBRNode extends LogicNode {
f32a[i + PhotoToPBRNode.tileWithBorderW * PhotoToPBRNode.tileWithBorderW * 2] = (u8a[i * 4 + 2] / 255 - 0.5) / 0.5;
}
data_get_blob("models/photo_to_" + PhotoToPBRNode.modelNames[from] + ".quant.onnx", (model_blob: ArrayBuffer) => {
let buf = krom_ml_inference(model_blob, [f32a.buffer], null, null, Config.raw.gpu_inference);
let ar = new Float32Array(buf);
let u8a = new Uint8Array(4 * PhotoToPBRNode.tileW * PhotoToPBRNode.tileW);
let offsetG = (from == ChannelType.ChannelBaseColor || from == ChannelType.ChannelNormalMap) ? PhotoToPBRNode.tileWithBorderW * PhotoToPBRNode.tileWithBorderW : 0;
let offsetB = (from == ChannelType.ChannelBaseColor || from == ChannelType.ChannelNormalMap) ? PhotoToPBRNode.tileWithBorderW * PhotoToPBRNode.tileWithBorderW * 2 : 0;
for (let i = 0; i < (PhotoToPBRNode.tileW * PhotoToPBRNode.tileW); ++i) {
let x = PhotoToPBRNode.borderW + i % PhotoToPBRNode.tileW;
let y = PhotoToPBRNode.borderW + Math.floor(i / PhotoToPBRNode.tileW);
u8a[i * 4 ] = Math.floor((ar[y * PhotoToPBRNode.tileWithBorderW + x ] * 0.5 + 0.5) * 255);
u8a[i * 4 + 1] = Math.floor((ar[y * PhotoToPBRNode.tileWithBorderW + x + offsetG] * 0.5 + 0.5) * 255);
u8a[i * 4 + 2] = Math.floor((ar[y * PhotoToPBRNode.tileWithBorderW + x + offsetB] * 0.5 + 0.5) * 255);
u8a[i * 4 + 3] = 255;
}
tileFloats.push(ar);
let model_blob: ArrayBuffer = data_get_blob("models/photo_to_" + PhotoToPBRNode.modelNames[from] + ".quant.onnx");
let buf = krom_ml_inference(model_blob, [f32a.buffer], null, null, Config.raw.gpu_inference);
let ar = new Float32Array(buf);
u8a = new Uint8Array(4 * PhotoToPBRNode.tileW * PhotoToPBRNode.tileW);
let offsetG = (from == ChannelType.ChannelBaseColor || from == ChannelType.ChannelNormalMap) ? PhotoToPBRNode.tileWithBorderW * PhotoToPBRNode.tileWithBorderW : 0;
let offsetB = (from == ChannelType.ChannelBaseColor || from == ChannelType.ChannelNormalMap) ? PhotoToPBRNode.tileWithBorderW * PhotoToPBRNode.tileWithBorderW * 2 : 0;
for (let i = 0; i < (PhotoToPBRNode.tileW * PhotoToPBRNode.tileW); ++i) {
let x = PhotoToPBRNode.borderW + i % PhotoToPBRNode.tileW;
let y = PhotoToPBRNode.borderW + Math.floor(i / PhotoToPBRNode.tileW);
u8a[i * 4 ] = Math.floor((ar[y * PhotoToPBRNode.tileWithBorderW + x ] * 0.5 + 0.5) * 255);
u8a[i * 4 + 1] = Math.floor((ar[y * PhotoToPBRNode.tileWithBorderW + x + offsetG] * 0.5 + 0.5) * 255);
u8a[i * 4 + 2] = Math.floor((ar[y * PhotoToPBRNode.tileWithBorderW + x + offsetB] * 0.5 + 0.5) * 255);
u8a[i * 4 + 3] = 255;
}
tileFloats.push(ar);
// Use border pixels to blend seams
if (i > 0) {
if (x > 0) {
let ar = tileFloats[i - 1];
for (let yy = 0; yy < PhotoToPBRNode.tileW; ++yy) {
for (let xx = 0; xx < PhotoToPBRNode.borderW; ++xx) {
let i = yy * PhotoToPBRNode.tileW + xx;
let a = u8a[i * 4];
let b = u8a[i * 4 + 1];
let c = u8a[i * 4 + 2];
// Use border pixels to blend seams
if (i > 0) {
if (x > 0) {
let ar = tileFloats[i - 1];
for (let yy = 0; yy < PhotoToPBRNode.tileW; ++yy) {
for (let xx = 0; xx < PhotoToPBRNode.borderW; ++xx) {
let i = yy * PhotoToPBRNode.tileW + xx;
let a = u8a[i * 4];
let b = u8a[i * 4 + 1];
let c = u8a[i * 4 + 2];
let aa = Math.floor((ar[(PhotoToPBRNode.borderW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + xx ] * 0.5 + 0.5) * 255);
let bb = Math.floor((ar[(PhotoToPBRNode.borderW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + xx + offsetG] * 0.5 + 0.5) * 255);
let cc = Math.floor((ar[(PhotoToPBRNode.borderW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + xx + offsetB] * 0.5 + 0.5) * 255);
let aa = Math.floor((ar[(PhotoToPBRNode.borderW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + xx ] * 0.5 + 0.5) * 255);
let bb = Math.floor((ar[(PhotoToPBRNode.borderW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + xx + offsetG] * 0.5 + 0.5) * 255);
let cc = Math.floor((ar[(PhotoToPBRNode.borderW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + xx + offsetB] * 0.5 + 0.5) * 255);
let f = xx / PhotoToPBRNode.borderW;
let invf = 1.0 - f;
a = Math.floor(a * f + aa * invf);
b = Math.floor(b * f + bb * invf);
c = Math.floor(c * f + cc * invf);
let f = xx / PhotoToPBRNode.borderW;
let invf = 1.0 - f;
a = Math.floor(a * f + aa * invf);
b = Math.floor(b * f + bb * invf);
c = Math.floor(c * f + cc * invf);
u8a[i * 4 ] = a;
u8a[i * 4 + 1] = b;
u8a[i * 4 + 2] = c;
}
}
}
if (y > 0) {
let ar = tileFloats[i - tilesX];
for (let xx = 0; xx < PhotoToPBRNode.tileW; ++xx) {
for (let yy = 0; yy < PhotoToPBRNode.borderW; ++yy) {
let i = yy * PhotoToPBRNode.tileW + xx;
let a = u8a[i * 4];
let b = u8a[i * 4 + 1];
let c = u8a[i * 4 + 2];
let aa = Math.floor((ar[(PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + xx ] * 0.5 + 0.5) * 255);
let bb = Math.floor((ar[(PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + xx + offsetG] * 0.5 + 0.5) * 255);
let cc = Math.floor((ar[(PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + xx + offsetB] * 0.5 + 0.5) * 255);
let f = yy / PhotoToPBRNode.borderW;
let invf = 1.0 - f;
a = Math.floor(a * f + aa * invf);
b = Math.floor(b * f + bb * invf);
c = Math.floor(c * f + cc * invf);
u8a[i * 4 ] = a;
u8a[i * 4 + 1] = b;
u8a[i * 4 + 2] = c;
}
u8a[i * 4 ] = a;
u8a[i * 4 + 1] = b;
u8a[i * 4 + 2] = c;
}
}
}
if (y > 0) {
let ar = tileFloats[i - tilesX];
for (let xx = 0; xx < PhotoToPBRNode.tileW; ++xx) {
for (let yy = 0; yy < PhotoToPBRNode.borderW; ++yy) {
let i = yy * PhotoToPBRNode.tileW + xx;
let a = u8a[i * 4];
let b = u8a[i * 4 + 1];
let c = u8a[i * 4 + 2];
///if (krom_metal || krom_vulkan)
if (from == ChannelType.ChannelBaseColor) PhotoToPBRNode.bgraSwap(u8a.buffer);
///end
let aa = Math.floor((ar[(PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + xx ] * 0.5 + 0.5) * 255);
let bb = Math.floor((ar[(PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + xx + offsetG] * 0.5 + 0.5) * 255);
let cc = Math.floor((ar[(PhotoToPBRNode.borderW + PhotoToPBRNode.tileW + yy) * PhotoToPBRNode.tileWithBorderW + PhotoToPBRNode.borderW + xx + offsetB] * 0.5 + 0.5) * 255);
let temp2 = image_from_bytes(u8a.buffer, PhotoToPBRNode.tileW, PhotoToPBRNode.tileW);
g2_begin(PhotoToPBRNode.images[from], false);
g2_draw_image(temp2, x * PhotoToPBRNode.tileW, y * PhotoToPBRNode.tileW);
g2_end();
Base.notifyOnNextFrame(() => {
image_unload(temp2);
});
let f = yy / PhotoToPBRNode.borderW;
let invf = 1.0 - f;
a = Math.floor(a * f + aa * invf);
b = Math.floor(b * f + bb * invf);
c = Math.floor(c * f + cc * invf);
u8a[i * 4 ] = a;
u8a[i * 4 + 1] = b;
u8a[i * 4 + 2] = c;
}
}
}
}
///if (krom_metal || krom_vulkan)
if (from == ChannelType.ChannelBaseColor) PhotoToPBRNode.bgraSwap(u8a.buffer);
///end
let temp2 = image_from_bytes(u8a.buffer, PhotoToPBRNode.tileW, PhotoToPBRNode.tileW);
g2_begin(PhotoToPBRNode.images[from], false);
g2_draw_image(temp2, x * PhotoToPBRNode.tileW, y * PhotoToPBRNode.tileW);
g2_end();
Base.notifyOnNextFrame(() => {
image_unload(temp2);
});
}
+9 -12
View File
@@ -30,20 +30,17 @@ class TextToPhotoNode extends LogicNode {
}
static stableDiffusion = (prompt: string, done: (img: image_t)=>void, inpaintLatents: Float32Array = null, offset = 0, upscale = true, mask: Float32Array = null, latents_orig: Float32Array = null) => {
data_get_blob("models/sd_text_encoder.quant.onnx", (_text_encoder_blob: ArrayBuffer) => {
data_get_blob("models/sd_unet.quant.onnx", (_unet_blob: ArrayBuffer) => {
data_get_blob("models/sd_vae_decoder.quant.onnx", (_vae_decoder_blob: ArrayBuffer) => {
TextToPhotoNode.text_encoder_blob = _text_encoder_blob;
TextToPhotoNode.unet_blob = _unet_blob;
TextToPhotoNode.vae_decoder_blob = _vae_decoder_blob;
TextToPhotoNode.textEncoder(prompt, inpaintLatents, (latents: Float32Array, text_embeddings: Float32Array) => {
TextToPhotoNode.unet(latents, text_embeddings, mask, latents_orig, offset, (latents: Float32Array) => {
TextToPhotoNode.vaeDecoder(latents, upscale, done);
});
let _text_encoder_blob: ArrayBuffer = data_get_blob("models/sd_text_encoder.quant.onnx");
let _unet_blob: ArrayBuffer = data_get_blob("models/sd_unet.quant.onnx");
let _vae_decoder_blob: ArrayBuffer = data_get_blob("models/sd_vae_decoder.quant.onnx");
TextToPhotoNode.text_encoder_blob = _text_encoder_blob;
TextToPhotoNode.unet_blob = _unet_blob;
TextToPhotoNode.vae_decoder_blob = _vae_decoder_blob;
TextToPhotoNode.textEncoder(prompt, inpaintLatents, (latents: Float32Array, text_embeddings: Float32Array) => {
TextToPhotoNode.unet(latents, text_embeddings, mask, latents_orig, offset, (latents: Float32Array) => {
TextToPhotoNode.vaeDecoder(latents, upscale, done);
});
});
});
});
}
static textEncoder = (prompt: string, inpaintLatents: Float32Array, done: (a: Float32Array, b: Float32Array)=>void) => {
+3 -4
View File
@@ -31,10 +31,9 @@ class UpscaleNode extends LogicNode {
}
static loadBlob = (done: ()=>void) => {
data_get_blob("models/esrgan.quant.onnx", (_esrgan_blob: ArrayBuffer) => {
UpscaleNode.esrgan_blob = _esrgan_blob;
done();
});
let _esrgan_blob: ArrayBuffer = data_get_blob("models/esrgan.quant.onnx");
UpscaleNode.esrgan_blob = _esrgan_blob;
done();
}
override getCachedImage = (): image_t => {
+22 -23
View File
@@ -42,31 +42,30 @@ class VarianceNode extends LogicNode {
Console.progress(tr("Processing") + " - " + tr("Variance"));
Base.notifyOnNextFrame(() => {
data_get_blob("models/sd_vae_encoder.quant.onnx", (vae_encoder_blob: ArrayBuffer) => {
let latents_buf = krom_ml_inference(vae_encoder_blob, [f32a.buffer], [[1, 3, 512, 512]], [1, 4, 64, 64], Config.raw.gpu_inference);
let latents = new Float32Array(latents_buf);
for (let i = 0; i < latents.length; ++i) {
latents[i] = 0.18215 * latents[i];
}
let vae_encoder_blob: ArrayBuffer = data_get_blob("models/sd_vae_encoder.quant.onnx");
let latents_buf = krom_ml_inference(vae_encoder_blob, [f32a.buffer], [[1, 3, 512, 512]], [1, 4, 64, 64], Config.raw.gpu_inference);
let latents = new Float32Array(latents_buf);
for (let i = 0; i < latents.length; ++i) {
latents[i] = 0.18215 * latents[i];
}
let noise = new Float32Array(latents.length);
for (let i = 0; i < noise.length; ++i) noise[i] = Math.cos(2.0 * 3.14 * RandomNode.getFloat()) * Math.sqrt(-2.0 * Math.log(RandomNode.getFloat()));
let num_inference_steps = 50;
let init_timestep = Math.floor(num_inference_steps * strength);
let timesteps = TextToPhotoNode.timesteps[num_inference_steps - init_timestep];
let alphas_cumprod = TextToPhotoNode.alphas_cumprod;
let sqrt_alpha_prod = Math.pow(alphas_cumprod[timesteps], 0.5);
let sqrt_one_minus_alpha_prod = Math.pow(1.0 - alphas_cumprod[timesteps], 0.5);
for (let i = 0; i < latents.length; ++i) {
latents[i] = sqrt_alpha_prod * latents[i] + sqrt_one_minus_alpha_prod * noise[i];
}
let t_start = num_inference_steps - init_timestep;
let noise = new Float32Array(latents.length);
for (let i = 0; i < noise.length; ++i) noise[i] = Math.cos(2.0 * 3.14 * RandomNode.getFloat()) * Math.sqrt(-2.0 * Math.log(RandomNode.getFloat()));
let num_inference_steps = 50;
let init_timestep = Math.floor(num_inference_steps * strength);
let timesteps = TextToPhotoNode.timesteps[num_inference_steps - init_timestep];
let alphas_cumprod = TextToPhotoNode.alphas_cumprod;
let sqrt_alpha_prod = Math.pow(alphas_cumprod[timesteps], 0.5);
let sqrt_one_minus_alpha_prod = Math.pow(1.0 - alphas_cumprod[timesteps], 0.5);
for (let i = 0; i < latents.length; ++i) {
latents[i] = sqrt_alpha_prod * latents[i] + sqrt_one_minus_alpha_prod * noise[i];
}
let t_start = num_inference_steps - init_timestep;
TextToPhotoNode.stableDiffusion(VarianceNode.prompt, (_image: image_t) => {
VarianceNode.image = _image;
done(VarianceNode.image);
}, latents, t_start);
});
TextToPhotoNode.stableDiffusion(VarianceNode.prompt, (_image: image_t) => {
VarianceNode.image = _image;
done(VarianceNode.image);
}, latents, t_start);
});
});
}