当前位置:网站首页>Accelerating with Dali modules

Accelerating with Dali modules

2022-06-12 23:44:00 Cheap sword

Official documents :https://docs.nvidia.com/deeplearning/dali/user-guide/docs/installation.html

One 、 install

TIPS: My version is CUDA 11.4, I installed 11.0 Version of , It doesn't matter

1.cuda10.2

pip install --extra-index-url https://developer.download.nvidia.com/compute/redist --upgrade nvidia-dali-cuda102  

2.cuda 11.0

pip install --extra-index-url https://developer.download.nvidia.com/compute/redist --upgrade nvidia-dali-cuda110

Two 、Pipeline

1. Basic use

Pipeline yes DALI Core concepts of module processing data , It's defined in nvidia.dali.Pipeline (from nvidia.dali.pipeline import Pipeline) in

  1. Use pipeline_def( Official documents ) Decorators are defined
  2. The defined decorator will add multiple parameters . For more parameters, see Official documents .
from nvidia.dali import pipeline_def
import nvidia.dali.fn as fn
import nvidia.dali.types as types

# Just look for some pictures , to images Create several folders , Just put the pictures in the folder 
image_dir = "data/images"
max_batch_size = 8


@pipeline_def
def simple_pipeline():
    jpegs, labels = fn.readers.file(file_root=image_dir,random_shuffle=True)
	#device Of 'mixed' Refers to the use GPU and CPU Hybrid decoding , This is for large resolution JPEG The image is very effective , You can also use only CPU decode 
    images = fn.decoders.image(jpegs, device='mixed').gpu()

    return images, labels
    
pipe = simple_pipeline(batch_size=max_batch_size, num_threads=1, device_id=0)
pipe.build()

# function , Get one batch Result 
pipe_out = pipe.run()
images, labels = pipe_out

The last step , We got the picture and the corresponding tag , The following will images Save as file .labels The value of is passed print(np.array(labels.as_tensor())) obtain

import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt

def show_images(image_batch):
    columns = 4
    rows = (max_batch_size + 1) // (columns)
    fig = plt.figure(figsize = (32,(32 // columns) * rows))
    gs = gridspec.GridSpec(rows, columns)
    for j in range(rows*columns):
        plt.subplot(gs[j])
        plt.axis("off")
        plt.imshow(image_batch.at(j))
    
    plt.savefig("dummy_name.png")

show_images(images.as_cpu())

open dummy_name.png File to see the picture .
 Insert picture description here

2. Enhancements

Random picture rotation , Fill in the blank 1,( Read more parameters Official documents )

@pipeline_def
def simple_pipeline():
    jpegs, labels = fn.readers.file(file_root=image_dir,random_shuffle=True)
    images = fn.decoders.image(jpegs, device='mixed').gpu()
    angle = fn.random.uniform(range=(-10.0, 10.0))
   
    rotated_images = fn.rotate(images, angle=angle, fill_value=1)

    return rotated_images, labels

 Insert picture description here
3. Tips
DALI The module does not support transferring data from within the pipeline GPU Move to CPU in .

3、 ... and 、 Realization

By inheritance Pipeline Class and rewrite define_graph Method
Make up later

原网站

版权声明
本文为[Cheap sword]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280605175868.html