Coder Social home page Coder Social logo

torchnvjpeg's Introduction

Decode JPEG image on GPU using PyTorch api

Install

  • python setup.py bdist_wheel
  • in dist directory, pip install torchnvjpeg-0.1.0-cp36-cp36m-linux_x86_64.whl

How to use

single decode

import torch
import torchnvjpeg
decoder = torchnvjpeg.Decoder()

image_data = open("images/cat.jpg", 'rb').read()

image_tensor = decoder.decode(image_data)  # run on GPU
assert image_tensor.is_cuda

import torchvision
transform = torchvision.transform.Resize((224, 224))
resized_tensor = transform(image_tensor.permute((2, 0, 1))) # run on GPU

batch decode

import torch
import torchnvjpeg
batch_size = 8
max_cpu_threads = 8
device_id = 0
max_image_size = 3840 * 2160 * 3
decoder = torchnvjpeg.Decoder(0, 0, True, device_id, batch_size, max_cpu_threads, max_image_size, torch.cuda.current_stream(device_id))

image_path = "images/cat.jpg"
data = open(image_path, 'rb').read()
data_list = [data for _ in range(batch_size)]

image_tensor_list = decoder.batch_decode(data_list)

parallel decode

import torch
import torchnvjpeg
from multiprocessing.pool import ThreadPool

batch_size = 8
image_path = "images/cat.jpg"
data = open(image_path, 'rb').read()
data_list = [data for _ in range(batch_size)]

decoder_list = [torchnvjpeg.Decoder() for _ in range(batch_size)]

cpu_threads = 4
pool = ThreadPool(cpu_threads)

def run(args):
    decoder, data = args
    return decoder.decode(data)

image_tensor_list = pool.map(run, zip(decoder_list, data_list))

Train

import py/train/gpu_preprocess.py file, wrap data_loader (torch.utils.data.DataLoader) with gpu_loader

gpu_data_loader = gpu_loader(cpu_loader, data_transform)

torchnvjpeg's People

Contributors

itsliupeng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.