{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Deep Learning Models -- A collection of various deep learning architectures, models, and tips for TensorFlow and PyTorch in Jupyter Notebooks.\n", "- Author: Sebastian Raschka\n", "- GitHub Repository: https://github.com/rasbt/deeplearning-models" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sebastian Raschka \n", "\n", "CPython 3.6.8\n", "IPython 7.2.0\n", "\n", "torch 1.1.0\n" ] } ], "source": [ "%load_ext watermark\n", "%watermark -a 'Sebastian Raschka' -v -p torch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Runs on CPU or GPU (if available)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Model Zoo -- Ordinal Regression CNN -- CORAL" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Implementation of a method for ordinal regression, CORAL [1] (COnsistent RAnk Logits) applied to predicting age from face images in the AFAD [2] (Asian Face) dataset using a simple ResNet-34 [3] convolutional network architecture.\n", "\n", "Note that in order to reduce training time, only a subset of AFAD (AFAD-Lite) is being used.\n", "\n", "- [1] Cao, Wenzhi, Vahid Mirjalili, and Sebastian Raschka. \"[Consistent Rank Logits for Ordinal Regression with Convolutional Neural Networks](https://arxiv.org/abs/1901.07884).\" arXiv preprint arXiv:1901.07884 (2019).\n", "- [2] Niu, Zhenxing, Mo Zhou, Le Wang, Xinbo Gao, and Gang Hua. \"[Ordinal regression with multiple output cnn for age estimation](https://ieeexplore.ieee.org/document/7780901/).\" In Proceedings of the IEEE conference on computer vision and pattern recognition, pp. 4920-4928. 2016.\n", "- [3] He, Kaiming, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. \"[Deep residual learning for image recognition](http://openaccess.thecvf.com/content_cvpr_2016/html/He_Deep_Residual_Learning_CVPR_2016_paper.html).\" In Proceedings of the IEEE conference on computer vision and pattern recognition, pp. 770-778. 2016." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Imports" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import time\n", "import numpy as np\n", "import pandas as pd\n", "import os\n", "\n", "import torch.nn as nn\n", "import torch.nn.functional as F\n", "import torch\n", "\n", "from torch.utils.data import Dataset\n", "from torch.utils.data import DataLoader\n", "from torchvision import transforms\n", "from PIL import Image\n", "\n", "\n", "if torch.cuda.is_available():\n", " torch.backends.cudnn.deterministic = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Downloading the Dataset" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cloning into 'tarball-lite'...\n", "remote: Enumerating objects: 37, done.\u001b[K\n", "remote: Total 37 (delta 0), reused 0 (delta 0), pack-reused 37\u001b[K\n", "Unpacking objects: 100% (37/37), done.\n", "Checking out files: 100% (30/30), done.\n" ] } ], "source": [ "!git clone https://github.com/afad-dataset/tarball-lite.git" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "!cat tarball-lite/AFAD-Lite.tar.xz* > tarball-lite/AFAD-Lite.tar.xz" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "!tar xf tarball-lite/AFAD-Lite.tar.xz" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "rootDir = 'AFAD-Lite'\n", "\n", "files = [os.path.relpath(os.path.join(dirpath, file), rootDir)\n", " for (dirpath, dirnames, filenames) in os.walk(rootDir) \n", " for file in filenames if file.endswith('.jpg')]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "59344" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(files)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "d = {}\n", "\n", "d['age'] = []\n", "d['gender'] = []\n", "d['file'] = []\n", "d['path'] = []\n", "\n", "for f in files:\n", " age, gender, fname = f.split('/')\n", " if gender == '111':\n", " gender = 'male'\n", " else:\n", " gender = 'female'\n", " \n", " d['age'].append(age)\n", " d['gender'].append(gender)\n", " d['file'].append(fname)\n", " d['path'].append(f)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | age | \n", "gender | \n", "file | \n", "path | \n", "
---|---|---|---|---|
0 | \n", "39 | \n", "female | \n", "474596-0.jpg | \n", "39/112/474596-0.jpg | \n", "
1 | \n", "39 | \n", "female | \n", "397477-0.jpg | \n", "39/112/397477-0.jpg | \n", "
2 | \n", "39 | \n", "female | \n", "576466-0.jpg | \n", "39/112/576466-0.jpg | \n", "
3 | \n", "39 | \n", "female | \n", "399405-0.jpg | \n", "39/112/399405-0.jpg | \n", "
4 | \n", "39 | \n", "female | \n", "410524-0.jpg | \n", "39/112/410524-0.jpg | \n", "