{ "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 = torch.transpose(img, 0, 2)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "batch_size = 100\n", "batch = torch.zeros(100, 3, 256, 256, dtype=torch.uint8)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "data_dir = '../data/p1ch4/image-cats/'\n", "filenames = [name for name in os.listdir(data_dir) if os.path.splitext(name) == '.png']\n", "for i, filename in enumerate(filenames):\n", " img_arr = imageio.imread(filename)\n", " batch[i] = torch.transpose(torch.from_numpy(img_arr), 0, 2)" ] }, { "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }