{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import torch\n", "torch.set_printoptions(edgeitems=2, threshold=50)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(720, 1280, 3)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import imageio\n", "\n", "img_arr = imageio.imread('../data/p1ch4/image-dog/bobby.jpg')\n", "img_arr.shape" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "img = torch.from_numpy(img_arr)\n", "out = img.permute(2, 0, 1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "batch_size = 3\n", "batch = torch.zeros(batch_size, 3, 256, 256, dtype=torch.uint8)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [], "source": [ "import os\n", "\n", "data_dir = '../data/p1ch4/image-cats/'\n", "filenames = [name for name in os.listdir(data_dir)\n", " if os.path.splitext(name)[-1] == '.png']\n", "for i, filename in enumerate(filenames):\n", " img_arr = imageio.imread(os.path.join(data_dir, filename))\n", " img_t = torch.from_numpy(img_arr)\n", " img_t = img_t.permute(2, 0, 1)\n", " img_t = img_t[:3] # <1>\n", " batch[i] = img_t" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "batch = batch.float()\n", "batch /= 255.0" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "n_channels = batch.shape[1]\n", "for c in range(n_channels):\n", " mean = torch.mean(batch[:, c])\n", " std = torch.std(batch[:, c])\n", " batch[:, c] = (batch[:, c] - mean) / std" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 2 }