这篇文章是上一篇的延申。
!nvidia-smi
!pip install --extra-index-url https://developer.download.nvidia.com/compute/redist --upgrade nvidia-dali-cuda120
# import dependencies
from nvidia.dali.pipeline import Pipeline
from nvidia.dali import pipeline_def
import nvidia.dali.fn as fn
import nvidia.dali.types as types
import warnings
# 在代码执行期间禁用警告信息的显示
warnings.filterwarnings("ignore")
batch_size=4
@pipeline_def
def simple_pipeline():
# use fn.readers.file to read encoded images and labels from the hard drive
pngs, labels=fn.readers.file(file_root=image_dir)
# use the fn.decoders.image operation to decode images from png to RGB
images=fn.decoders.image(pngs, device='mixed')
# specify which of the intermediate variables should be returned as the outputs of the pipeline
return images, labels
# create and build pipeline
pipe=simple_pipeline(batch_size=batch_size, num_threads=4, device_id=0)
pipe.build()
# run the pipeline
simple_pipe_output=pipe.run()
images, labels=simple_pipe_output
print("Images is_dense_tensor: " + str(images.is_dense_tensor()))
print("Labels is_dense_tensor: " + str(labels.is_dense_tensor()))
# define a function display images
def show_images(image_batch):
columns=4
rows=1
# create plot
# 窗口的宽度是固定的,而高度根据列数和行数来自动计算,以确保图像按照指定的布局显示。
fig=plt.figure(figsize=(15, (15 // columns) * rows))
gs=gridspec.GridSpec(rows, columns)
for idx in range(rows*columns):
plt.subplot(gs[idx])
plt.axis("off")
plt.imshow(image_batch.at(idx))
plt.tight_layout()
show_images(images.as_cpu())
import random
@pipeline_def
def augmentation_pipeline():
# use fn.readers.file to read encoded images and labels from the hard drive
image_pngs, _=fn.readers.file(file_root=image_dir)
# use the fn.decoders.image operation to decode images from png to RGB
images=fn.decoders.image(image_pngs, device='cpu')
# the same augmentation needs to be performed on the associated masks
mask_pngs, _=fn.readers.file(file_root=mask_dir)
masks=fn.decoders.image(mask_pngs, device='cpu')
image_size=512
roi_size=image_size*.5
roi_start_x=image_size*random.uniform(0, 0.5)
roi_start_y=image_size*random.uniform(0, 0.5)
# use fn.resize to investigate an roi, region of interest
resized_images=fn.resize(images, size=[512, 512], roi_start=[roi_start_x, roi_start_y], roi_end=[roi_start_x+roi_size, roi_start_y+roi_size])
resized_masks=fn.resize(masks, size=[512, 512], roi_start=[roi_start_x, roi_start_y], roi_end=[roi_start_x+roi_size, roi_start_y+roi_size])
# use fn.resize to flip the image
flipped_images=fn.resize(images, size=[-512, -512])
flipped_masks=fn.resize(masks, size=[-512, -512])
return images, resized_images, flipped_images, masks, resized_masks, flipped_masks
# 创建数据增强的数据处理流水线
pipe=augmentation_pipeline(batch_size=batch_size, num_threads=4, device_id=0)
# 构建数据处理流水线
pipe.build()
# 执行数据处理流水线
augmentation_pipe_output=pipe.run()
# define a function display images
augmentation=['original', 'resized', 'flipped']
def show_augmented_images(pipe_output):
image_batch, resized_image_batch, flipped_image_batch, mask_batch, resized_mask_batch, flipped_mask_batch=pipe_output
columns=6
rows=batch_size
# create plot
fig=plt.figure(figsize=(15, (15 // columns) * rows))
gs=gridspec.GridSpec(rows, columns)
grid_data=[image_batch, resized_image_batch, flipped_image_batch, mask_batch, resized_mask_batch, flipped_mask_batch]
# grid 变量用于追踪要显示的图像数据在 grid_data 列表中的索引。
grid=0
for row_idx in range(rows):
for col_idx in range(columns):
plt.subplot(gs[grid])
plt.axis('off')
plt.title(augmentation[col_idx%3])
plt.imshow(grid_data[col_idx].at(row_idx))
grid+=1
plt.tight_layout()
show_augmented_images(augmentation_pipe_output)
show_augmented_images(pipe.run())
@pipeline_def
def rotate_pipeline():
images, _=fn.readers.file(file_root=image_dir)
masks, _=fn.readers.file(file_root=mask_dir)
images=fn.decoders.image(images, device='cpu')
masks=fn.decoders.image(masks, device='cpu')
angle=fn.random.uniform(range=(-30.0, 30.0))
rotated_images = fn.rotate(images.gpu(), angle=angle, fill_value=0, keep_size=True, device='gpu')
rotated_masks = fn.rotate(masks.gpu(), angle=angle, fill_value=0, keep_size=True, device='gpu')
return rotated_images, rotated_masks
random.uniform
并使用 rotate
进行旋转,由此生成随机角度。gpu
,并通过调用 .gpu()
确保其输入传输到 GPU。rotate_pipeline
会在 GPU 上执行旋转pipe=rotate_pipeline(batch_size=batch_size, num_threads=4, device_id=0)
pipe.build()
rotate_pipe_output=pipe.run()
# define a function display images
def show_rotate_images(pipe_output):
image_batch, rotated_batch=pipe_output
columns=batch_size
rows=2
fig=plt.figure(figsize=(15, (15 // columns) * rows))
gs=gridspec.GridSpec(rows, columns)
grid_data=[image_batch.as_cpu(), rotated_batch.as_cpu()]
grid=0
for row_idx in range(rows):
for col_idx in range(columns):
plt.subplot(gs[grid])
plt.axis('off')
plt.imshow(grid_data[row_idx].at(col_idx))
grid+=1
plt.tight_layout()
Pipeline.run()
返回的对象调用 as_cpu()
来实现。show_rotate_images(rotate_pipe_output)