File size: 5,076 Bytes
3962e6f
 
 
 
 
 
 
 
 
 
 
 
 
d0c4233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8daf244
d0c4233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8daf244
d0c4233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85cea18
d0c4233
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---

license: apache-2.0
language: en
library_name: tensorflowjs
tags:
- real-cugan
- super-resolution
- image-upscaling
- anime
- tensorflowjs
- image-to-image
---


# Real-CUGAN Models for TensorFlow.js

[![Hugging Face Repo](https://img.shields.io/badge/πŸ€—%20Hugging%20Face-Repo-yellow)](https://huggingface.co/shammisw/real-cugan-tensorflowjs)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

This repository provides pre-converted models of **Real-CUGAN** (Real-World-Oriented Cascaded U-Net for Anime Image Super-Resolution) in the **TensorFlow.js GraphModel format**, ready for use in web browsers and Node.js environments.

These models are optimized for upscaling anime-style images and illustrations with high fidelity, speed, and reduced noise.

## ✨ Features

* **High-Quality Anime Upscaling:** Specifically trained for cartoons and anime, preserving sharp lines and details.
* **Web Ready:** Run directly in the browser with TensorFlow.js for client-side image processing.
* **Multiple Scales & Models:** Includes various models for different upscaling factors and noise reduction levels.
* **Lightweight & Fast:** CUGAN is designed to be more efficient than many larger GAN-based upscalers.

---

## πŸš€ Usage Example

To use these models, you will need to have TensorFlow.js set up in your project.

```bash

# Using npm

npm install @tensorflow/tfjs



# Using yarn

yarn add @tensorflow/tfjs

```

Here is a basic example of how to load and run a model in JavaScript:

```javascript

import * as tf from '@tensorflow/tfjs';



// The URL to the model.json file in this repository

const MODEL_URL = '[https://huggingface.co/shammisw/real-cugan-tensorflowjs/resolve/main/real-cugan-models/realcugan/4x-conservative-64/model.json](https://huggingface.co/shammisw/real-cugan-tensorflowjs/resolve/main/real-cugan-models/realcugan/4x-conservative-64/model.json)';



async function upscaleImage(imageElement) {

  try {

    // 1. Load the model

    console.log('Loading model...');

    const model = await tf.loadGraphModel(MODEL_URL);

    console.log('Model loaded.');



    // 2. Prepare the input tensor from an HTMLImageElement

    // Models are trained on float32 tensors, normalized to the [0, 1] range.

    const inputTensor = tf.browser.fromPixels(imageElement)

      .toFloat()

      .div(255.0)

      .expandDims(0); // Add batch dimension: [h, w, c] -> [1, h, w, c]



    // 3. Run inference

    console.log('Running inference...');

    const outputTensor = model.execute(inputTensor);



    // 4. Process the output and display it on a canvas

    const outputCanvas = document.getElementById('output-canvas');

    await tf.browser.toPixels(outputTensor.squeeze(), outputCanvas);

    console.log('Upscaling complete!');



    // 5. Clean up tensors

    tf.dispose([inputTensor, outputTensor]);



  } catch (error) {

    console.error('Failed to upscale image:', error);

  }

}



// Find your input image element and pass it to the function

const myImage = document.getElementById('my-input-image');

upscaleImage(myImage);

```

---

## πŸ“‚ Available Models

This repository contains the following converted models. The number in the model name (e.g., `-64`) refers to the tile size used during conversion, which can affect performance and memory usage.

| Model Type       | Scale | Denoise Level | Path                                               |
| :--------------- | :---: | :-----------: | :------------------------------------------------- |
| **Conservative** |  2x   |       -       | `real-cugan-models/realcugan/2x-conservative-64/`  |
| **Conservative** |  4x   |       -       | `real-cugan-models/realcugan/4x-conservative-64/`  |
| *More models can be added here as they are converted.* |       |               |                                                    |

---

## πŸ™ Acknowledgements & Credits

This repository only contains the converted models. All credit for the research and training of the original models goes to their respective creators.

* **Original Real-CUGAN Models:** The foundational research and PyTorch models were developed by **Bilibili AI Lab**. Their incredible work made this possible.
    * **GitHub Repository:** [bilibili/ailab/Real-CUGAN](https://github.com/bilibili/ailab/tree/main/Real-CUGAN)

* **TensorFlow.js Conversion:** The methodology for converting these models to TensorFlow.js format was adapted from the excellent **[web-realesrgan](https://github.com/ts-ai/web-realesrgan)** project, which provided a clear path for on-device super-resolution in the browser.

---

## πŸ“œ License

The code and configuration in this repository are released under the **Apache-2.0**.

The original Real-CUGAN models are subject to their own license terms as specified in the [official Real-CUGAN repository](https://github.com/bilibili/ailab/tree/main/Real-CUGAN). Please ensure compliance with their license if you use these models.