Skip to main content

Real-time PyTorch Tensor Visualisation in CUDA, Eliminating CPU Transfer

Project description

Buy Me A Coffee Twitter PyPI version Downloads

image

cudacanvas : PyTorch Tensor Image Display in CUDA

(Real-time PyTorch Tensor Image Visualisation in CUDA, Eliminating CPU Transfer)

CudaCanvas is a simple Python module that eliminates CPU transfer for Pytorch tensors for displaying and rendering images in the training or evaluation phase, ideal for machine learning scientists and engineers.

Simplified version that directly displays the image without explicit window creation (cudacanvas >= v1.0.1)

import torch
import cudacanvas


#REPLACE THIS with you training loop
while (True):

    #REPLACE THIS with you training code and generation of data
    noise_image = torch.rand((4, 500, 500), device="cuda")

    #Visualise your data in real-time
    cudacanvas.im_show(noise_image)

    #OPTIONAL: Terminate training when the window is closed
    if cudacanvas.should_close():
        cudacanvas.clean_up()
        #end process if the window is closed
        break

You can visualise the latent of Stable Diffusion during sampling in real-time whilst waiting for the steps to finish

import warnings
warnings.filterwarnings("ignore")
from diffusers import StableDiffusionPipeline
import torch
import cudacanvas

def display_tensors(pipe, step, timestep, callback_kwargs):
    latents = callback_kwargs["latents"]

    with torch.no_grad():
        image = pipe.vae.decode(latents / pipe.vae.config.scaling_factor, return_dict=False)[0]
        image = image - image.min()
        image = image / image.max()
    
    cudacanvas.im_show(image.squeeze(0))
    
    if cudacanvas.should_close():
        cudacanvas.clean_up()
        pipe._interrupt = True
    
    return callback_kwargs

pipeline = StableDiffusionPipeline.from_pretrained(
    "stabilityai/stable-diffusion-2-1-base",
    torch_dtype=torch.float16,
    variant="fp16"
).to("cuda")

image = pipeline(
    prompt="A croissant shaped like a cute bear.",
    negative_prompt="Deformed, ugly, bad anatomy",
    callback_on_step_end=display_tensors,
    callback_on_step_end_tensor_inputs=["latents"],
).images[0]

cudacanvas.clean_up()

Installation

Before instllation make sure you have torch with cuda support already installed on your machine

We aligned pytorch and cuda version with our package the supporting packages are torch (2.0.1, 2.1.2 and 2.2.2) and (11.8 and 12.1)

Identify your current torch and cuda version

import torch
torch.__version__

Depending on your torch and cuda you can install the relevant cudacanvas package, for the latest one matching the latest pytorch package you can simply download the latest package

pip install cudacanvas

For other torch and cuda packages put the torch and cuda version after that cudacanvas version for example for 2.1.2+cu118 the Cudacanvas package you require is 1.0.1.post212118

pip install cudacanvas==1.0.1.post212118 --force-reinstall

Support

Also support my channel ☕ ☕ : https://www.buymeacoffee.com/outofai

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

cudacanvas-1.0.1.post260126-cp311-cp311-win_amd64.whl (101.8 kB view details)

Uploaded CPython 3.11Windows x86-64

cudacanvas-1.0.1.post260126-cp310-cp310-win_amd64.whl (100.6 kB view details)

Uploaded CPython 3.10Windows x86-64

cudacanvas-1.0.1.post260126-cp39-cp39-win_amd64.whl (101.1 kB view details)

Uploaded CPython 3.9Windows x86-64

File details

Details for the file cudacanvas-1.0.1.post260126-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260126-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 b68b28e13fbb1957a5e119bac3d7cf5bced6e7abe9c180633e2e7a150eb2f94c
MD5 5af68746f01dbddbea61339d5d81fde0
BLAKE2b-256 8105afc2751ada3c8f23b798e6437c31ed92cb0fb63daeebba655fc450481217

See more details on using hashes here.

File details

Details for the file cudacanvas-1.0.1.post260126-cp311-cp311-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260126-cp311-cp311-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 98c32064358b8a521504f8f47c58e3db5e2768a2162bd7914b31a4cec50a1684
MD5 f287dc0a59d014bd3e4800090031d483
BLAKE2b-256 ad28436338254198d44b0f15328d0a77cfec8b489636b4b82108c3409972b1d6

See more details on using hashes here.

File details

Details for the file cudacanvas-1.0.1.post260126-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260126-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 a988bc720b9d8b3ade47ed05c548182c2d647c74f9dee58258ba5bd5cc5c2911
MD5 434832c64c07dd7de48383c4812ee4a5
BLAKE2b-256 6f9fd7faa4e7a08d04708743ff750a98948a1a2f8cc2a16a6846d0752c1e949f

See more details on using hashes here.

File details

Details for the file cudacanvas-1.0.1.post260126-cp310-cp310-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260126-cp310-cp310-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5ceccd1f2b974f52d0bec887b0a7e6d8817db7c7bb0a94e2c45c5f615e99161d
MD5 8eb7786a5c8c94914e5e48eac71e20d6
BLAKE2b-256 1586aa1436dd89eba94baf2d55d6f2dcc53b0112e32f12f6edc7617851f45e9d

See more details on using hashes here.

File details

Details for the file cudacanvas-1.0.1.post260126-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260126-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 4eabf742a2def065140abbe82e91f7c5c3d6766c23bee09eba71627291f1dc67
MD5 164b955ca7ac605c5b1dff1228bb7f4d
BLAKE2b-256 807f285f9605ea0973b91ffaf50cc8f996b93a0b13fba5b971051637d99c073a

See more details on using hashes here.

File details

Details for the file cudacanvas-1.0.1.post260126-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cudacanvas-1.0.1.post260126-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3187d9218b3c93d5246553c30e7225fb9d14137f7c087d65c15886a573ab824d
MD5 37c0901532fa332af72f94cea6274354
BLAKE2b-256 80165049214f22c7394706315bf51c0096ca93e0b71da7e50568a9b835137971

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page