UpscalerJS

repository·main·Indexed 21 days ago

https://github.com/thekevinscott/upscalerjs

A JavaScript library that uses AI to enhance images through resolution upscaling, denoising, and deblurring. It is compatible with Browser, Node.js, and Worker environments. The library supports custom TensorFlow.js models, GPU acceleration via Docker, and provides specialized integration guides for Cloudflare Workers and Electron applications.

Tokens
43.5K
Snippets
204
Records
256
Agent score
75%

What's inside UpscalerJS

  1. Overview of UpscalerJS capabilities

    main

    UpscalerJS is an open-source library for enhancing images using AI. It is compatible with Browser, Node (both CPU and GPU), and Workers environments.

    Key features include:

    • Pretrained Models: Support for increasing resolution, denoising, and deblurring.
    • UI-Focused Enhancement: Uses patch-based processing to maintain performance without sacrificing quality.
    • Custom Models: Ability to integrate your own pretrained models.
    • Cross-Device Compatibility: Works consistently across desktops, tablets, and phones.
  2. Introduction to UpscalerJS

    main

    UpscalerJS is an open-source JavaScript tool for enhancing images using AI. It is built on top of TensorFlow.js and is designed to perform super-resolution (increasing image size while maintaining or enhancing quality) using neural networks.

    Key features include:

    • Multi-environment support: Runs in the browser, Node.js, and Worker environments (such as Cloudflare Workers).
    • Out-of-the-box models: Ships with pre-trained models so users can start upscaling immediately without manual model installation.
    • Client-side processing: By running inference on the user's device, it offers improved privacy (images never leave the device), reduced server costs, and lower latency by avoiding server round-trips.
  3. UpscalerJS API Overview

    main
    The UpscalerJS API provides a set of methods for initializing an upscaling instance, managing models, and executing image upscaling tasks. The core lifecycle involves constructing an instance, optionally warming up the model to reduce initial latency, executing the upscale operation, and finally disposing of the instance to free up resources.
  4. What is Super Resolution and why use it in the browser?

    main

    Super Resolution is a machine learning technique used to reconstruct a high-resolution image from a low-resolution one. Unlike standard scaling algorithms like bicubic interpolation, which often result in blurry images, Super Resolution "paints" new pixels to achieve higher fidelity.

    Benefits of Browser-based Upscaling with UpscalerJS:

    • Zero Deployment Overhead: Since it uses Tensorflow.js, models run directly in the user's browser. There is no need to provision expensive GPUs or manage backend infrastructure.
    • Immediate Feedback: Performing inference on the client device eliminates the latency of uploading an image to a server and waiting for a processed response, which is especially beneficial for users on slow connections.
    • Reduced Bandwidth/Storage: You can serve much smaller source images (e.g., reducing a 724kb image to a 9kb version) and upscale them on the client side, significantly reducing data transfer costs.

    Trade-offs:

    • Hardware Constraints: Performance is limited by the user's device. While modern mobile chips (Apple/Google) are increasingly optimized for neural networks, users on older hardware may experience slower processing speeds compared to a dedicated backend GPU.
  5. Understanding MAXIM Experiments models

    main

    The MAXIM Experiments models in this repository are TensorFlow ports of the MAXIM family of models.

    Key Characteristics:

    • Performance: They run significantly faster than the Jax port.
    • Trade-offs: They exhibit more artifacting and have inferior performance compared to the original models.
    • Limitations: They operate only on fixed-size image chunks. Using these models on certain image sizes can introduce severe artifacting.

    If you encounter significant artifacting, refer to the known issues in the UpscalerJS repository.

  6. Model constraints and input requirements for MAXIM Dehazing Outdoor

    main

    The MAXIM Dehazing Outdoor model is unquantized and supports dynamic input sizes.

    Key constraints:

    • Image Sizes: Must be a multiple of 64. UpscalerJS handles padding and trimming automatically.
    • Patch Sizes: If providing a custom patch size, it must be a multiple of 64. If it is not, UpscalerJS will emit a warning and increase the patch size to the nearest multiple of 64.
  7. MAXIM Model constraints and requirements

    main

    The MAXIM models in UpscalerJS have specific requirements regarding input dimensions:

    • Input Size: The model has a dynamic input size. Image dimensions must be a multiple of 64. UpscalerJS handles padding and trimming automatically to satisfy this.
    • Patch Size: If you provide a custom patchSize, it must also be a multiple of 64. If it is not, UpscalerJS will emit a warning and automatically increase the patch size to the nearest multiple of 64 to ensure compatibility.
    • Quantization: These models are unquantized.
  8. Manage Tensor memory in UpscalerJS

    main

    When you provide a tensor as input or request a tensor as output, you take over responsibility for memory management. You must explicitly call .dispose() on these tensors to prevent memory leaks.

    Important: While TensorFlow.js provides tf.tidy() for synchronous operations, most UpscalerJS methods are asynchronous. Therefore, you should manually call .dispose() on tensors once they are no longer needed within your .then() blocks or after await calls.

    // Example of manual memory management
    upscaler.upscale(tensor, {
      output: 'tensor',
    }).then(upscaledTensor => {
      // 1. Dispose of the original input tensor
      tensor.dispose()
    
      // 2. Use the upscaled tensor
      upscaledTensor.print()
    
      // 3. Dispose of the upscaled tensor when finished
      upscaledTensor.dispose()
    })
  9. MAXIM Deblurring model constraints and details

    main

    The standard MAXIM Deblurring model is unquantized and supports dynamic input sizes.

    Requirements:

    • Image Sizes: Must be a multiple of 64. UpscalerJS handles padding and trimming automatically.
    • Patch Sizes: If providing a custom patch size, it must be a multiple of 64. If it is not, UpscalerJS will emit a warning and increase the patch size to the nearest multiple of 64.

    Performance Note: The model is based on the MAXIM (Multi-Axis MLP for Image Processing) architecture, designed for efficient image enhancement tasks like deblurring, denoising, and deraining.

  10. MAXIM Retouching Model Constraints and Details

    main

    The standard MAXIM Retouching model is unquantized and supports dynamic input sizes.

    Key constraints:

    • Image Dimensions: Input image sizes must be a multiple of 64. UpscalerJS handles padding and trimming automatically to satisfy this.
    • Patch Size: If you provide a custom patchSize, it must also be a multiple of 64. If it is not, UpscalerJS will emit a warning and automatically increase the patch size to the nearest multiple of 64.
  11. Use UpscalerJS for model-agnostic image upscaling

    main
    UpscalerJS is an npm package designed to be agnostic to the underlying upscaling model. This abstraction allows the library to support various models, including those tuned for specific use cases like faces or illustrations, which are served via JS CDNs. This architecture allows for model improvements and new model introductions without changing the core consumer implementation.