{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom torch.utils.data import Dataset, DataLoader\nfrom torchvision import transforms, utils","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nfrom pathlib import Path","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom PIL import Image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"INPUT = Path(\"../input/digit-recognizer\")\nos.listdir(INPUT)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv(INPUT/\"train.csv\")\ntrain_df.head(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df = pd.read_csv(INPUT/\"test.csv\")\ntest_df.head(3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Create images from CSV"},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN = Path(\"../train\")\nTEST = Path(\"../test\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create training directory\nfor index in range(10):\n    try:\n        os.makedirs(TRAIN/str(index))\n    except:\n        pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Test whether creating the training directory was successful\nsorted(os.listdir(TRAIN))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Create test directory\ntry:\n    os.makedirs(TEST)\nexcept:\n    pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# import numpy to reshape array from flat (1x784) to square (28x28)\nimport numpy as np\n\n# import PIL to display images and to create images from arrays\nfrom PIL import Image\n\ndef saveDigit(digit, filepath):\n    digit = digit.reshape(28,28)\n    digit = digit.astype(np.uint8)\n\n    img = Image.fromarray(digit)\n    img.save(filepath)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# save training images\nfor index, row in train_df.iterrows():\n    \n    label,digit = row[0], row[1:]\n    \n    folder = TRAIN/str(label)\n    filename = f\"{index}.jpg\"\n    filepath = folder/filename\n    \n    digit = digit.values\n    \n    saveDigit(digit, filepath)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# save testing images\nfor index, digit in test_df.iterrows():\n\n    folder = TEST\n    filename = f\"{index}.jpg\"\n    filepath = folder/filename\n    \n    digit = digit.values\n    \n    saveDigit(digit, filepath)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Create a Map-style dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"#alternativ kann man auch ein DataLoader verwenden\n\n\n# wenn ich mit einer Klasse arbeite sollte ich mit __getitem__ das Bild und das Label erhalten, falls das label existiert, also im training set\n\nclass MNIST(Dataset):\n    \n    def __init__(self, df):\n        self.data = df\n    \n    def __len__(self):\n        length = 0\n        for i in range(10):\n            length += len(os.listdir(TRAIN/str(i)))\n        \n        return length\n    \n    def __getitem__(self, index):\n        image_path = TRAIN/\"1\"/os.listdir(TRAIN/\"1\")[index]\n        image = Image.open(image_path)\n        #image = np.asarray(image)\n        return image","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"mnist = MNIST(train_df)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(5,10))\nfor i in range(1, 6):\n    \n    fig.add_subplot(1, 5, i)\n    \n    sample=mnist[i]\n    plt.imshow(sample)\n    print(sample)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}