@huggingface/transformers
Version:
State-of-the-art Machine Learning for the web. Run 🤗 Transformers directly in your browser, with no need for a server!
59 lines (51 loc) • 2.06 kB
JavaScript
import { Pipeline, prepareImages } from './_base.js';
import { RawImage } from '../utils/image.js';
/**
* @typedef {import('./_base.js').ImagePipelineConstructorArgs} ImagePipelineConstructorArgs
* @typedef {import('./_base.js').Disposable} Disposable
* @typedef {import('./_base.js').ImageInput} ImageInput
*/
/**
* @typedef {ImagePipelineConstructorArgs & ImageToImagePipelineCallback & Disposable} ImageToImagePipelineType
*/
/**
* @template T
* @typedef {T extends ImageInput[] ? RawImage[] : RawImage} ImageToImagePipelineResult
*/
/**
* @typedef {<T extends ImageInput | ImageInput[]>(images: T) => Promise<ImageToImagePipelineResult<T>>} ImageToImagePipelineCallback
*/
/**
* Image to Image pipeline using any `AutoModelForImageToImage`. This pipeline generates an image based on a previous image input.
*
* **Example:** Super-resolution w/ `Xenova/swin2SR-classical-sr-x2-64`
* ```javascript
* import { pipeline } from '@huggingface/transformers';
*
* const upscaler = await pipeline('image-to-image', 'Xenova/swin2SR-classical-sr-x2-64');
* const url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/butterfly.jpg';
* const output = await upscaler(url);
* // RawImage {
* // data: Uint8Array(786432) [ 41, 31, 24, 43, ... ],
* // width: 512,
* // height: 512,
* // channels: 3
* // }
* ```
*/
export class ImageToImagePipeline
extends /** @type {new (options: ImagePipelineConstructorArgs) => ImageToImagePipelineType} */ (Pipeline)
{
async _call(images) {
const preparedImages = await prepareImages(images);
const inputs = await this.processor(preparedImages);
const outputs = await this.model(inputs);
/** @type {RawImage[]} */
const toReturn = [];
for (const batch of outputs.reconstruction) {
const output = batch.squeeze().clamp_(0, 1).mul_(255).round_().to('uint8');
toReturn.push(RawImage.fromTensor(output));
}
return Array.isArray(images) ? toReturn : toReturn[0];
}
}