Host Decoder examples¶
In this notebook we will show usage examples of different flavors of Host Decoder.
Common code¶
[1]:
from __future__ import division
from nvidia.dali.pipeline import Pipeline
import nvidia.dali.ops as ops
import nvidia.dali.types as types
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
%matplotlib inline
seed = 1549361629
def show_images(image_batch):
    columns = 4
    rows = (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))
image_dir = "images"
batch_size = 4
Host decoder¶
‘HostDecoder’ decodes the whole image (no cropping).
[2]:
class HostDecoderPipeline(Pipeline):
    def __init__(self, batch_size, num_threads, device_id):
        super(HostDecoderPipeline, self).__init__(batch_size, num_threads, device_id, seed = seed)
        self.input = ops.FileReader(file_root = image_dir)
        self.decode = ops.HostDecoder(output_type = types.RGB)
    def define_graph(self):
        jpegs, labels = self.input()
        images = self.decode(jpegs)
        return (images, labels)
pipe = HostDecoderPipeline(batch_size, 1, 0)
pipe.build()
images, _ = pipe.run()
show_images( images )
Host Decoder with random cropping window size and anchor¶
‘HostDecoderRandomCrop’ produces a randomly cropped image. The random cropping window is produced based on a given aspect ratio and area distributions.
[3]:
class HostDecoderRandomCropPipeline(Pipeline):
    def __init__(self, batch_size, num_threads, device_id):
        super(HostDecoderRandomCropPipeline, self).__init__(batch_size, num_threads, device_id, seed = seed)
        self.input = ops.FileReader(file_root = image_dir)
        self.decode = ops.HostDecoderRandomCrop(
            device="cpu",
            output_type=types.RGB,
            random_aspect_ratio=[0.8, 1.25],
            random_area=[0.1, 1.0],
            num_attempts=100)
    def define_graph(self):
        jpegs, labels = self.input()
        images = self.decode(jpegs)
        return (images, labels)
pipe = HostDecoderRandomCropPipeline(batch_size, 1, 0)
pipe.build()
images, _ = pipe.run()
show_images( images )
Host Decoder with fixed cropping window size and external anchor¶
‘HostDecoderCrop’ produces a cropped image with a fixed cropping window size and a variable position
[4]:
class HostDecoderCropPipeline(Pipeline):
    def __init__(self, batch_size, num_threads, device_id):
        super(HostDecoderCropPipeline, self).__init__(batch_size, num_threads, device_id, seed = seed)
        self.input = ops.FileReader(file_root = image_dir)
        self.pos_rng_x = ops.Uniform(range = (0.0, 1.0))
        self.pos_rng_y = ops.Uniform(range = (0.0, 1.0))
        self.decode = ops.HostDecoderCrop(output_type = types.RGB, crop = (224, 224))
    def define_graph(self):
        jpegs, labels = self.input()
        pos_x = self.pos_rng_x()
        pos_y = self.pos_rng_y()
        images = self.decode(jpegs, crop_pos_x=pos_x, crop_pos_y=pos_y)
        return (images, labels)
pipe = HostDecoderCropPipeline(batch_size, 1, 0)
pipe.build()
images, _ = pipe.run()
show_images( images )
Host Decoder with external window size and anchor¶
‘HostDecoderSlice’ produces a cropped image based on additional tensor inputs describing the cropping window size and position
[5]:
import numpy as np
class ExternalInputIterator(object):
    def __init__(self, batch_size):
        self.batch_size = batch_size
    def __iter__(self):
        self.i = 0
        self.n = self.batch_size
        return self
    def __next__(self):
        pos = []
        size = []
        for _ in range(self.batch_size):
            pos.append(np.asarray([0.4, 0.2], dtype=np.float32))
            size.append(np.asarray([0.3, 0.5], dtype=np.float32))
            self.i = (self.i + 1) % self.n
        return (pos, size)
    next = __next__
eii = ExternalInputIterator(batch_size)
pos_size_iter = iter(eii)
class HostDecoderSlicePipeline(Pipeline):
    def __init__(self, batch_size, num_threads, device_id):
        super(HostDecoderSlicePipeline, self).__init__(batch_size, num_threads, device_id, seed = seed)
        self.input = ops.FileReader(file_root = image_dir)
        self.input_crop_pos = ops.ExternalSource()
        self.input_crop_size = ops.ExternalSource()
        self.input_crop = ops.ExternalSource()
        self.decode = ops.HostDecoderSlice(output_type = types.RGB)
    def define_graph(self):
        jpegs, labels = self.input()
        self.crop_pos = self.input_crop_pos()
        self.crop_size = self.input_crop_size()
        images = self.decode(jpegs, self.crop_pos, self.crop_size)
        return (images, labels)
    def iter_setup(self):
        (crop_pos, crop_size) = pos_size_iter.next()
        self.feed_input(self.crop_pos, crop_pos)
        self.feed_input(self.crop_size, crop_size)
pipe = HostDecoderSlicePipeline(batch_size, 1, 0)
pipe.build()
images, _ = pipe.run()
show_images( images )
nvJPEG decoder¶
‘nvJPEGDecoder’ decodes the whole image (no cropping) using nvJPEG library.
[6]:
class nvJPEGDecoderPipeline(Pipeline):
    def __init__(self, batch_size, num_threads, device_id):
        super(nvJPEGDecoderPipeline, self).__init__(batch_size, num_threads, device_id, seed = seed)
        self.input = ops.FileReader(file_root = image_dir)
        self.decode = ops.nvJPEGDecoder(device = 'mixed', output_type = types.RGB)
    def define_graph(self):
        jpegs, labels = self.input()
        images = self.decode(jpegs)
        return (images, labels)
pipe = nvJPEGDecoderPipeline(batch_size, 1, 0)
pipe.build()
images, _ = pipe.run()
show_images( images.as_cpu() )
nvJPEG Decoder with random cropping window size and anchor¶
‘nvJPEGDecoderRandomCrop’ produces a randomly cropped image. The random cropping window is produced based on a given aspect ratio and area distributions.
[7]:
class nvJPEGDecoderRandomCropPipeline(Pipeline):
    def __init__(self, batch_size, num_threads, device_id):
        super(nvJPEGDecoderRandomCropPipeline, self).__init__(batch_size, num_threads, device_id, seed = seed)
        self.input = ops.FileReader(file_root = image_dir)
        self.decode = ops.nvJPEGDecoderRandomCrop(
            device="mixed",
            output_type=types.RGB,
            random_aspect_ratio=[0.8, 1.25],
            random_area=[0.1, 1.0],
            num_attempts=100)
    def define_graph(self):
        jpegs, labels = self.input()
        images = self.decode(jpegs)
        return (images, labels)
pipe = nvJPEGDecoderRandomCropPipeline(batch_size, 1, 0)
pipe.build()
images, _ = pipe.run()
show_images( images.as_cpu() )
nvJPEG Decoder with fixed cropping window size and external anchor¶
‘nvJPEGDecoderCrop’ produces a cropped image with a fixed cropping window size and a variable position
[8]:
class nvJPEGDecoderCropPipeline(Pipeline):
    def __init__(self, batch_size, num_threads, device_id):
        super(nvJPEGDecoderCropPipeline, self).__init__(batch_size, num_threads, device_id, seed = seed)
        self.input = ops.FileReader(file_root = image_dir)
        self.pos_rng_x = ops.Uniform(range = (0.0, 1.0))
        self.pos_rng_y = ops.Uniform(range = (0.0, 1.0))
        self.decode = ops.nvJPEGDecoderCrop(device = 'mixed', output_type = types.RGB, crop = (224, 224))
    def define_graph(self):
        jpegs, labels = self.input()
        pos_x = self.pos_rng_x()
        pos_y = self.pos_rng_y()
        images = self.decode(jpegs, crop_pos_x=pos_x, crop_pos_y=pos_y)
        return (images, labels)
pipe = nvJPEGDecoderCropPipeline(batch_size, 1, 0)
pipe.build()
images, _ = pipe.run()
show_images( images.as_cpu() )
nvJPEG Decoder with external window size and anchor¶
‘nvJPEGDecoderSlice’ produces a cropped image based on additional tensor inputs describing the cropping window size and position
[9]:
import numpy as np
class ExternalInputIterator(object):
    def __init__(self, batch_size):
        self.batch_size = batch_size
    def __iter__(self):
        self.i = 0
        self.n = self.batch_size
        return self
    def __next__(self):
        pos = []
        size = []
        for _ in range(self.batch_size):
            pos.append(np.asarray([0.4, 0.2], dtype=np.float32))
            size.append(np.asarray([0.3, 0.5], dtype=np.float32))
            self.i = (self.i + 1) % self.n
        return (pos, size)
    next = __next__
eii = ExternalInputIterator(batch_size)
pos_size_iter = iter(eii)
class nvJPEGDecoderSlicePipeline(Pipeline):
    def __init__(self, batch_size, num_threads, device_id):
        super(nvJPEGDecoderSlicePipeline, self).__init__(batch_size, num_threads, device_id, seed = seed)
        self.input = ops.FileReader(file_root = image_dir)
        self.input_crop_pos = ops.ExternalSource()
        self.input_crop_size = ops.ExternalSource()
        self.input_crop = ops.ExternalSource()
        self.decode = ops.nvJPEGDecoderSlice(device = 'mixed', output_type = types.RGB)
    def define_graph(self):
        jpegs, labels = self.input()
        self.crop_pos = self.input_crop_pos()
        self.crop_size = self.input_crop_size()
        images = self.decode(jpegs, self.crop_pos, self.crop_size)
        return (images, labels)
    def iter_setup(self):
        (crop_pos, crop_size) = pos_size_iter.next()
        self.feed_input(self.crop_pos, crop_pos)
        self.feed_input(self.crop_size, crop_size)
pipe = nvJPEGDecoderSlicePipeline(batch_size, 1, 0)
pipe.build()
images, _ = pipe.run()
show_images( images.as_cpu() )
[ ]: