
How we deployed a 10MB image classification model that runs entirely in the browser using TensorFlow.js and WebAssembly.

## Why run ML in the browser?

Training happens on powerful servers, but what about inference? Running models in the browser offers unique advantages:

- Privacy - user data never leaves their device
- Speed - no network round-trip to a server
- Cost - zero inference costs on your infrastructure
- Offline - works without internet connection

We built an image classification app that identifies 1000+ objects in real-time, entirely client-side.

## Choosing the right model

Not all models work well in browsers. We needed:
- Small file size (< 10MB)
- Fast inference (< 100ms)
- Good accuracy (> 75% top-5)

### Model options

MobileNetV2
- Size: 14MB
- Speed: 45ms
- Accuracy: 88% top-5
- Our choice

ResNet50
- Size: 98MB
- Speed: 250ms
- Accuracy: 93% top-5
- Too large and slow

Tiny YOLO
- Size: 35MB
- Speed: 120ms
- Accuracy: good for object detection
- Overkill for simple classification

## Converting the model

TensorFlow.js requires models in a specific format. Here's how to convert:

### 1. Export from Python/TensorFlow

```python
import tensorflow as tf
import tensorflowjs as tfjs

# Load your trained model
model = tf.keras.models.load_model('model.h5')

# Convert to TensorFlow.js format
tfjs.converters.save_keras_model(model, 'tfjs_model')
```

This creates:
- `model.json` - model architecture and weights metadata
- `group1-shard1of1.bin` - binary weight data

### 2. Optimize for size

```bash
# Quantize weights from float32 to uint16 (50% size reduction)
tensorflowjs_converter \
  --input_format=keras \
  --output_format=tfjs_graph_model \
  --quantization_bytes=2 \
  model.h5 \
  tfjs_model_quantized
```

Result: 14MB to 7MB with minimal accuracy loss

## Loading the model in JavaScript

```javascript
import * as tf from '@tensorflow/tfjs'

// Enable WebGL backend for GPU acceleration
await tf.setBackend('webgl')

// Load model
const model = await tf.loadGraphModel('tfjs_model/model.json')

console.log('Model loaded successfully!')
```

### Preloading for better UX

Don't make users wait. Load the model on page load:

```javascript
let model = null

// Start loading immediately
const modelPromise = tf.loadGraphModel('tfjs_model/model.json')

// Use when needed
async function classify(image) {
  if (!model) {
    model = await modelPromise
  }

  return predict(model, image)
}
```

## Preprocessing images

Neural networks expect specific input formats:

```javascript
function preprocessImage(imgElement) {
  return tf.tidy(() => {
    // Convert HTML image to tensor
    let tensor = tf.browser.fromPixels(imgElement)

    // Resize to 224x224 (MobileNet input size)
    tensor = tf.image.resizeBilinear(tensor, [224, 224])

    // Normalize pixel values to [-1, 1]
    tensor = tensor.div(127.5).sub(1)

    // Add batch dimension
    tensor = tensor.expandDims(0)

    return tensor
  })
}
```

Important: use `tf.tidy()` to prevent memory leaks.

## Running inference

```javascript
async function classifyImage(imgElement) {
  // Preprocess
  const input = preprocessImage(imgElement)

  // Run inference
  const predictions = await model.predict(input)

  // Get top 5 predictions
  const top5 = await getTop5(predictions)

  // Clean up tensors
  input.dispose()
  predictions.dispose()

  return top5
}

async function getTop5(predictions) {
  const values = await predictions.data()
  const indices = Array.from(values)
    .map((prob, index) => ({ prob, index }))
    .sort((a, b) => b.prob - a.prob)
    .slice(0, 5)
    .map(x => ({
      className: IMAGENET_CLASSES[x.index],
      probability: x.prob
    }))

  return indices
}
```

## Performance optimization

### 1. Use WebGL backend

WebGL uses the GPU for calculations:

```javascript
// Check which backend is being used
console.log(tf.getBackend()) // Should be 'webgl'

// Manually set if needed
await tf.setBackend('webgl')
```

WebGL is 10-100x faster than CPU.

### 2. Warm up the model

The first inference is always slower. Warm it up:

```javascript
async function warmUpModel(model) {
  const warmupTensor = tf.zeros([1, 224, 224, 3])
  await model.predict(warmupTensor)
  warmupTensor.dispose()
}

// After loading model
await warmUpModel(model)
```

### 3. Batch predictions

Processing multiple images? Use batching:

```javascript
async function batchPredict(images) {
  // Stack multiple images into one tensor
  const batch = tf.tidy(() => {
    const tensors = images.map(preprocessImage)
    return tf.stack(tensors)
  })

  const predictions = await model.predict(batch)

  batch.dispose()
  return predictions
}
```

### 4. Use Web Workers

Don't block the main thread:

```javascript
// worker.js
import * as tf from '@tensorflow/tfjs'

let model

self.addEventListener('message', async (e) => {
  const { type, data } = e.data

  if (type === 'load') {
    model = await tf.loadGraphModel(data.modelUrl)
    self.postMessage({ type: 'loaded' })
  }

  if (type === 'predict') {
    const result = await classify(data.image)
    self.postMessage({ type: 'result', data: result })
  }
})

// main.js
const worker = new Worker('worker.js')

worker.postMessage({
  type: 'load',
  data: { modelUrl: 'model.json' }
})

worker.addEventListener('message', (e) => {
  if (e.data.type === 'result') {
    console.log('Predictions:', e.data.data)
  }
})
```

## Real-time video classification

Classify webcam frames in real-time:

```javascript
async function setupCamera() {
  const video = document.getElementById('webcam')

  const stream = await navigator.mediaDevices.getUserMedia({
    video: { width: 640, height: 480 }
  })

  video.srcObject = stream

  return new Promise(resolve => {
    video.onloadedmetadata = () => resolve(video)
  })
}

async function detectFrame(video) {
  const predictions = await classifyImage(video)

  // Display results
  displayPredictions(predictions)

  // Request next frame
  requestAnimationFrame(() => detectFrame(video))
}

// Start
const video = await setupCamera()
await video.play()
detectFrame(video)
```

### Throttling for performance

Don't classify every frame:

```javascript
let lastPredictionTime = 0
const PREDICTION_INTERVAL = 200 // ms

async function detectFrame(video) {
  const now = Date.now()

  if (now - lastPredictionTime > PREDICTION_INTERVAL) {
    const predictions = await classifyImage(video)
    displayPredictions(predictions)
    lastPredictionTime = now
  }

  requestAnimationFrame(() => detectFrame(video))
}
```

## Memory management

TensorFlow.js uses GPU memory. Leaks cause crashes.

### Manual disposal

```javascript
const tensor = tf.tensor([1, 2, 3])
// Use tensor...
tensor.dispose() // Free memory
```

### Use tf.tidy()

Automatically cleans up intermediate tensors:

```javascript
const result = tf.tidy(() => {
  const a = tf.tensor([1, 2, 3])
  const b = tf.tensor([4, 5, 6])
  const c = a.add(b) // Intermediate tensor
  return c.mean() // Return value is NOT disposed
})

// Only 'result' exists, a/b/c were cleaned up
```

### Monitor memory

```javascript
console.log('Num tensors:', tf.memory().numTensors)
console.log('Num bytes:', tf.memory().numBytes)

// Watch for leaks
setInterval(() => {
  const mem = tf.memory()
  console.log(`Tensors: ${mem.numTensors}, Bytes: ${mem.numBytes}`)
}, 5000)
```

## Handling edge cases

### Mobile performance

Mobile GPUs are slower:

```javascript
function isMobile() {
  return /Android|iPhone|iPad/i.test(navigator.userAgent)
}

const config = isMobile()
  ? { inputSize: 192, maxFPS: 15 }  // Smaller, slower
  : { inputSize: 224, maxFPS: 30 }  // Full quality
```

### Fallback for unsupported browsers

```javascript
async function setupTensorFlow() {
  try {
    await tf.setBackend('webgl')
    await tf.ready()
  } catch (e) {
    console.warn('WebGL not available, falling back to CPU')
    await tf.setBackend('cpu')
  }
}
```

### Progressive loading

Load a tiny model first, then upgrade:

```javascript
// Load tiny model (1MB)
let model = await tf.loadGraphModel('tiny_model/model.json')
console.log('Tiny model ready!')

// Upgrade to full model in background
tf.loadGraphModel('full_model/model.json').then(fullModel => {
  model = fullModel
  console.log('Full model ready!')
})
```

## Bundle size optimization

TensorFlow.js is large. Optimize your bundle:

### Tree shaking

Import only what you need:

```javascript
// Imports everything (500KB)
import * as tf from '@tensorflow/tfjs'

// Import specific modules (150KB)
import '@tensorflow/tfjs-backend-webgl'
import { loadGraphModel } from '@tensorflow/tfjs-converter'
import { browser, tidy, image } from '@tensorflow/tfjs-core'
```

### Code splitting

Lazy load TensorFlow.js:

```javascript
async function loadModel() {
  const tf = await import('@tensorflow/tfjs')
  return tf.loadGraphModel('model.json')
}
```

### Use a CDN

Let browsers cache TensorFlow.js:

```html
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.0.0"></script>
```

## Production checklist

Before deploying:

- [ ] Quantize model weights (50% size reduction)
- [ ] Warm up model on load
- [ ] Use `tf.tidy()` everywhere
- [ ] Monitor memory usage
- [ ] Add loading indicators
- [ ] Test on mobile devices
- [ ] Add error handling
- [ ] Consider Web Worker for long predictions
- [ ] Implement progressive loading
- [ ] Use WASM backend as fallback

## Results

Our production app:

| Metric | Value |
|--------|-------|
| Model size | 7MB (quantized) |
| Load time | 1.2s (3G network) |
| Inference time | 45ms (desktop GPU) |
| Inference time | 180ms (mobile GPU) |
| Memory usage | ~150MB |
| Accuracy | 87% top-5 |

## Lessons learned

1. Quantization is essential - 50% size reduction with <1% accuracy loss
2. Memory leaks are easy - always use `tf.tidy()` or manual disposal
3. Mobile is much slower - plan for 4-8x slower inference
4. WebGL isn't always available - have a CPU fallback
5. Bundle size matters - tree-shake and code-split TensorFlow.js

## Resources

- [TensorFlow.js Documentation](https://www.tensorflow.org/js)
- [Model Conversion Guide](https://www.tensorflow.org/js/guide/conversion)
- [Pre-trained Models](https://www.tensorflow.org/js/models)
- [TFJS Examples](https://github.com/tensorflow/tfjs-examples)