{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data augmentation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#export\n", "from exp.nb_09 import *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## PIL transforms" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "path = datasets.untar_data(datasets.URLs.IMAGENETTE_160)\n", "tfms = [make_rgb, ResizeFixed(128), to_byte_tensor, to_float_tensor]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from nvidia.dali.pipeline import Pipeline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import nvidia.dali.ops as ops\n", "import nvidia.dali.types as types" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bs=8" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class SimplePipeline(Pipeline):\n", " def __init__(self, batch_size=8, num_threads=8, device_id=0):\n", " super(SimplePipeline, self).__init__(batch_size, num_threads, device_id, seed = 12)\n", " self.input = ops.FileReader(file_root = path/'train')\n", " # self.input = ops.FileReader(file_root = image_dir, file_list = image_dir + '/file_list.txt')\n", " self.resize = ops.Resize(device = \"cpu\", resize_x=128, resize_y=128, image_type = types.RGB,\n", " interp_type = types.INTERP_LINEAR)\n", " self.decode = ops.HostDecoder(output_type = types.RGB)\n", "\n", " def define_graph(self):\n", " jpegs, labels = self.input(name='r')\n", " images = self.decode(jpegs)\n", " images = self.resize(images)\n", " return (images, labels)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pipe = SimplePipeline()\n", "pipe.build()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pipe_out = pipe.run()\n", "print(pipe_out)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "images, labels = pipe_out\n", "images.is_dense_tensor(), labels.is_dense_tensor()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t = images.as_tensor()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from nvidia.dali.plugin.pytorch import DALIGenericIterator,DALIClassificationIterator,feed_ndarray" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "it = DALIGenericIterator(pipe, ['data','label'], pipe.epoch_size('r'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "it = DALIClassificationIterator(pipe, pipe.epoch_size('r'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "its = iter(it)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t = next(it)[0]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t['label'].cuda().long().type()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t['data'].type()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "labels_tensor = labels.as_tensor()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "labels_tensor.shape()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "np.array(labels_tensor)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import matplotlib.gridspec as gridspec" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def show_images(image_batch):\n", " columns = 4\n", " rows = len(image_batch) // (columns)\n", " fig = plt.figure(figsize = (32,(32 // columns) * rows))\n", " gs = gridspec.GridSpec(rows, columns)\n", " for j in range(rows*columns):\n", " plt.subplot(gs[j])\n", " plt.axis(\"off\")\n", " plt.imshow(image_batch.at(j))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show_images(images)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 2 }