robustdg/docs/notebooks/helper_plots.ipynb

116 строки
2.6 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Visualizing Rotated MINST samples"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#Common imports\n",
"import os\n",
"import random\n",
"import copy\n",
"import numpy as np\n",
"\n",
"#Pillow\n",
"from PIL import Image \n",
"import PIL \n",
"\n",
"#Matplotlib\n",
"from matplotlib.pyplot import imshow\n",
"\n",
"#Pytorch\n",
"import torch\n",
"import torch.utils.data as data_utils\n",
"from torchvision import datasets, transforms"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"data_obj= datasets.MNIST('../../data/rot_mnist',\n",
" train=True,\n",
" download=False,\n",
" transform=transforms.ToTensor()\n",
" )\n",
"train_loader = torch.utils.data.DataLoader(data_obj,\n",
" batch_size=60000,\n",
" shuffle=False)\n",
"\n",
"\n",
"for i, (x, y) in enumerate(train_loader):\n",
" mnist_imgs = x\n",
" mnist_labels = y"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"to_pil= transforms.Compose([\n",
" transforms.ToPILImage(),\n",
" ])\n",
"angles=[0, 15, 30, 45, 60, 75, 90]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"indice= random.randint(0, mnist_imgs.shape[0])\n",
"mnist_img= mnist_imgs[indice]\n",
"rotated_imgs=[]\n",
"for angle in angles:\n",
" rotated_imgs.append( transforms.functional.rotate( to_pil(mnist_img), angle) )"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"counter=0\n",
"for img in rotated_imgs:\n",
" img.save('../../results/rot_mnist/images/' + str(angles[counter]) + '.jpg')\n",
" counter+=1"
]
}
],
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}