www.xbdev.net
xbdev - software development
Friday February 6, 2026
Home | Contact | Support | WebGPU Graphics and Compute ... | Neural Networks and WebGPU Compute.. Learning from Data.....
     
 

Neural Networks and WebGPU Compute..

Learning from Data.....

 


Image to Grayscale (Neural Network)


Show the neural network two images - one color and the other grayscale. Looks at groups of pixels and learns show to convert any other image pixel in the same way.




Complete Code



// Image to Grayscale

await xinitialize( {layers:[27,8,3], build:'cpu'learningrate:0.2} );


let div document.createElement('div');
document.body.appendChilddiv );
div.innerHTML = `
document.createOriginal training images (input/output)<br>

<img id="catOrig" src="https://webgpulab.xbdev.net/var/images/cat.png"></img>
<img id="catGray" src="https://webgpulab.xbdev.net/var/images/cat_grayscale.png"></img>

<br><br>
Test images (input/generated output from neural network)<br>
<img id="catTest" src="https://webgpulab.xbdev.net/var/images/cat_original.png"></img>
<canvas id="mycanvas" width="125" height="125" style="border:1px solid #ff0000;"> </canvas>

<!-- Display how many iterations -->
<br><br>
<div id="counter">Iteration:</div>
`;


console.log('Starting Filter Demo');

var 
canvas     null;
var 
context    null;
var 
size       125 125;
var 
iteration  0;
var 
px         null;    

function 
getData(imageObj){
    
canvas canvas || document.getElementById('mycanvas');
    
context context || canvas.getContext('2d');
    
context.drawImage(imageObj00);
    var 
imageData context.getImageData(00125125);
    return 
imageData.data;
}

function 
getPixel(data,oxoy){

    
ox Math.min(Math.max(ox0), 124);
    
oy Math.min(Math.max(oy0), 124);
    
    
let red   data[((125 oy) + ox) * 4];
    
let green data[((125 oy) + ox) * 1];
    
let blue  data[((125 oy) + ox) * 2];

    return [
red 255green 255blue 255];
}

async function preview(){
    
// console.log('func: preview');
    
iteration++;
    
    var 
elem document.getElementById('counter');
    
elem.innerHTML "Iteration: " iteration;
    
    var 
imageData context.getImageData(00125125);
    for (var 
0125x++)
    {
        for(var 
0125y++)
        {
            var 
px getPixel(imgTestDatax+0y+0);
            
px px.concat(getPixel(imgTestDatax-1y-1));
            
px px.concat(getPixel(imgTestDatax+0y-1));
            
px px.concat(getPixel(imgTestDatax+1y-1));
            
px px.concat(getPixel(imgTestDatax-1y+0));
            
px px.concat(getPixel(imgTestDatax+1y+0));
            
px px.concat(getPixel(imgTestDatax-1y+1));
            
px px.concat(getPixel(imgTestDatax+0y+1));
            
px px.concat(getPixel(imgTestDatax+1y+1));
            var 
rgb await xactivate(px);
            
            
let index y*125;
            
imageData.data[index 4] = (rgb[0] )* 255;
            
imageData.data[index 1] = (rgb[1] ) * 255;
            
imageData.data[index 2] = (rgb[2] ) * 255;
        }
    }
    
context.putImageData(imageData,0,0);
    
    
//requestAnimationFrame( iterate );
}

async function iterate(){
    for (var 
0125x++)
    {
        for(var 
0125y++)
        {
            
px getPixel(imgColDatax+0y+0);
            
px px.concat(getPixel(imgColDatax-1y-1));
            
px px.concat(getPixel(imgColDatax+0y-1));
            
px px.concat(getPixel(imgColDatax+1y-1));
            
px px.concat(getPixel(imgColDatax-1y+0));
            
px px.concat(getPixel(imgColDatax+1y+0));
            
px px.concat(getPixel(imgColDatax-1y+1));
            
px px.concat(getPixel(imgColDatax+0y+1));
            
px px.concat(getPixel(imgColDatax+1y+1));
            
await xactivate(px);
            
await xpropagategetPixel(imgGraData,x,y) );
        }
    }
    
await preview();
}
    
setIntervaliterate1000 );

// **** deep neural network

var imgColData   getData(document.getElementById('catOrig'));
var 
imgGraData   getData(document.getElementById('catGray'));
var 
imgTestData  getData(document.getElementById('catTest'));    


preview();



Things to Try


• Try other grayscale images (use different filter patterns - washed out grayscale)
• Instead of grayscale, create an edge detection filter (original filter and an image that has been put through an edge detection filter, like Sobel filter)
• Train for other filters that focus on different information (removing only the red or emphasising more blue)
• Try bluring or sharpening (sharpen or blur image for the training)


Resources and Links


• WebGPU Lab Demo [LINK]












WebGPU by Example: Fractals, Image Effects, Ray-Tracing, Procedural Geometry, 2D/3D, Particles, Simulations WebGPU Compute graphics and animations using the webgpu api 12 week course kenwright learn webgpu api kenwright programming compute and graphics applications with html5 and webgpu api kenwright real-time 3d graphics with webgpu kenwright webgpu api develompent a quick start guide kenwright webgpu by example 2022 kenwright webgpu gems kenwright webgpu interactive compute and graphics visualization cookbook kenwright wgsl webgpu shading language cookbook kenwright wgsl webgpugems shading language cookbook kenwright



 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.