{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Operators\n", "This example shows you how to run custom Python code by using the family of DALI `python_function` operators to prototype new augmentations or debug the pipeline.\n", "The idea behind these operators is to help you to execute the Python code that operates on DALI's tensors' data in the pipeline execution. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Defining an Operation\n", "The operator that we will use first is `python_function`, which wraps a regular Python function and runs it in a DALI Pipeline.\n", "\n", "We define this function as an example and call it `edit_images`." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from nvidia.dali.pipeline import pipeline_def\n", "import nvidia.dali.fn as fn\n", "import nvidia.dali.types as types\n", "import numpy as np\n", "\n", "\n", "def edit_images(image1, image2):\n", " assert image1.shape == image2.shape\n", " h, w, c = image1.shape\n", " y, x = np.ogrid[0:h, 0:w]\n", " mask = (x - w / 2) ** 2 + (y - h / 2) ** 2 > h * w / 9\n", " result1 = np.copy(image1)\n", " result1[mask] = image2[mask]\n", " result2 = np.copy(image2)\n", " result2[mask] = image1[mask]\n", " return result1, result2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* In this case, it takes two arrays as inputs and returns two outputs. \n", "* The code creates a circular mask and uses it to swap those circular parts between two inputs. \n", "\n", "`python_function` uses NumPy arrays as the data format for the CPU, and [CuPy](https://cupy.chainer.org/) arrays for GPU. \n", "\n", "**Note:** Both input images are copied, because the input data should not be modified." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "