Skip to content

Instantly share code, notes, and snippets.

@yang-zhang
Last active December 27, 2023 05:28
Show Gist options
  • Select an option

  • Save yang-zhang/ec071ae4775c2125595fd80f40efb0d6 to your computer and use it in GitHub Desktop.

Select an option

Save yang-zhang/ec071ae4775c2125595fd80f40efb0d6 to your computer and use it in GitHub Desktop.

Revisions

  1. yang-zhang revised this gist Nov 8, 2018. 1 changed file with 139 additions and 170 deletions.
    309 changes: 139 additions & 170 deletions multi-face.ipynb
    139 additions, 170 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
  2. yang-zhang revised this gist Nov 8, 2018. 1 changed file with 16 additions and 21 deletions.
    37 changes: 16 additions & 21 deletions multi-face.ipynb
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "This post is a line-by-line example of a multi-task deep learning model, implemented using the *fastai* v1 library for PyTorch. This model takes in an image of a human face and predicts their gender, race, and age. \n\nThis post wants to show:\n1. an example of a multi-task deep learning model;\n2. the multi-task model makes better predictions than the individual model; and\n2. how to use the fastai library to easily implement the model.\n\nThis post is written in a working Jupyter notebook, so you can run and change the code if you like (at least it's fun to randomly sample faces - some of them are famous)."
    "source": "This notebook is a line-by-line example of a multi-task deep learning model, implemented using the *fastai* v1 library for PyTorch. This model takes in an image of a human face and predicts their gender, race, and age. \n\nThis notebook wants to show:\n1. an example of a multi-task deep learning model;\n2. the multi-task model makes better predictions than the individual model; and\n2. how to use the fastai library to easily implement the model.\n\nThis document is written in a working Jupyter notebook, so you can run and change the code if you like (at least it's fun to randomly sample faces - some of them are famous)."
    },
    {
    "metadata": {},
    @@ -18,17 +18,17 @@
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "First, some quick introduction to multi-task learnig."
    "source": "First, some quick introduction to multi-task learning."
    },
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "### What it is \n\n> [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning): Multi-task learning (MTL) is a subfield of machine learning in which *multiple learning tasks are solved at the same time*, while exploiting commonalities and differences across tasks. \n\n In this post, *multiple learning tasks* are the three tasks to predict gender, race (both classification tasks), and age (a regression task), respectively."
    "source": "### What it is \n\n> [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning): Multi-task learning (MTL) is a subfield of machine learning in which *multiple learning tasks are solved at the same time*, while exploiting commonalities and differences across tasks. \n\n In this notebook, *multiple learning tasks* are the three tasks to predict gender, race (both classification tasks), and age (a regression task), respectively."
    },
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "### Why do it\n> [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning): This can result in *improved learning efficiency and prediction accuracy* for the task-specific models, when compared to training the models separately.\n\n In this post,\n - Regarding *improved learning efficiency*: we run the multi-task model once instead of three time for the single-task models (all four models have similar run time). \n - Regarding *improved prediction accuracy*: we will show that the multi-task model cut average age prediction error in half from 10 years to 5 years while holding the gender and race prediction accuracy almost the same. "
    "source": "### Why do it\n> [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning): This can result in *improved learning efficiency and prediction accuracy* for the task-specific models, when compared to training the models separately.\n\n In this notebook,\n - Regarding *improved learning efficiency*: we run the multi-task model once instead of three time for the single-task models (all four models have similar run time). \n - Regarding *improved prediction accuracy*: we will show that the multi-task model cut average age prediction error in half from 10 years to 5 years while holding the gender and race prediction accuracy almost the same. "
    },
    {
    "metadata": {},
    @@ -87,7 +87,6 @@
    },
    {
    "metadata": {
    "scrolled": true,
    "trusted": false
    },
    "cell_type": "code",
    @@ -167,7 +166,7 @@
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "The labels of each face image is embedded in the file name, formated like `[age]_[gender]_[race]_[date&time].jpg`\n\n`[age]` is an integer from 0 to 116, indicating the age\n`[gender]` is either 0 (male) or 1 (female)\n`[race]` is an integer from 0 to 4, denoting White, Black, Asian, Indian, and Others (like Hispanic, Latino, Middle Eastern).\n`[date&time]` is in the format of yyyymmddHHMMSSFFF, showing the date and time an image was collected to UTKFace"
    "source": "The labels of each face image is embedded in the file name, formatted like `[age]_[gender]_[race]_[date&time].jpg`\n\n`[age]` is an integer from 0 to 116, indicating the age\n`[gender]` is either 0 (male) or 1 (female)\n`[race]` is an integer from 0 to 4, denoting White, Black, Asian, Indian, and Others (like Hispanic, Latino, Middle Eastern).\n`[date&time]` is in the format of yyyymmddHHMMSSFFF, showing the date and time an image was collected to UTKFace"
    },
    {
    "metadata": {
    @@ -397,7 +396,6 @@
    },
    {
    "metadata": {
    "scrolled": true,
    "trusted": true
    },
    "cell_type": "code",
    @@ -421,7 +419,6 @@
    },
    {
    "metadata": {
    "scrolled": false,
    "trusted": false
    },
    "cell_type": "code",
    @@ -671,7 +668,8 @@
    },
    {
    "metadata": {
    "trusted": false
    "trusted": false,
    "collapsed": true
    },
    "cell_type": "code",
    "source": "i = np.random.choice(len(df))\ndfi = df.iloc[i]\n\nprint(f'age: {dfi[\"age\"]}, gender: {dfi[\"gender\"]}, race: {dfi[\"race\"]}')\nopen_image(dfi[\"img_name\"])",
    @@ -753,6 +751,7 @@
    },
    {
    "metadata": {
    "collapsed": true,
    "trusted": true
    },
    "cell_type": "code",
    @@ -958,7 +957,8 @@
    },
    {
    "metadata": {
    "trusted": true
    "trusted": true,
    "collapsed": true
    },
    "cell_type": "code",
    "source": "df = pd.read_csv('img2targets.csv', usecols=['img_name', target, 'is_train'])\n\nclasses = sorted(set(df[target]))\n\ndef get_dataset(df, classes=None):\n return ImageClassificationDataset(df.img_name, df[target], classes)\n\ntrain_ds = get_dataset(df[df.is_train==1], classes)\nvalid_ds = get_dataset(df[df.is_train==0], classes)\n\ndef get_data(sz, bs):\n return ImageDataBunch.create(train_ds, valid_ds, path=p, bs=bs, size=sz,\n ds_tfms=get_transforms()).normalize(imagenet_stats)\n\ndata = get_data(sz=200, bs=128)\n\ni = np.random.choice(len(data.valid_ds))\nimg,label = data.valid_ds[i]\n\n# img.show(title=\" \".join(np.array(data.classes)[np.where(labels)[0]]))\nimg.show(title=classes[label])",
    @@ -1059,7 +1059,6 @@
    },
    {
    "metadata": {
    "scrolled": true,
    "trusted": false
    },
    "cell_type": "code",
    @@ -1090,7 +1089,7 @@
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "The age prediction task is a regression problem. As the writing of this post, fastai does yet not have an `ImageRegressionDataset` class. (It's a fast growing library and might have one soon). So we will adapt `ImageClassificationDataset` a little bit into a regression dataset, by\n1. setting `y` to be the value of the target variable (after adding a batch dimension by `[:, None]`;\n2. setting `c=1`, so that the output of the network will be 1-dimension; and\n3. setting `loss_func = F.l1_loss` for the regression problem.\n\nAs we can see it's not hard to change how the library currently works to adapt to your problem."
    "source": "The age prediction task is a regression problem. As the writing of this notebook, fastai does yet not have an `ImageRegressionDataset` class. (It's a fast growing library and might have one soon). So we will adapt `ImageClassificationDataset` a little bit into a regression dataset, by\n1. setting `y` to be the value of the target variable (after adding a batch dimension by `[:, None]`;\n2. setting `c=1`, so that the output of the network will be 1-dimension; and\n3. setting `loss_func = F.l1_loss` for the regression problem.\n\nAs we can see it's not hard to change how the library currently works to adapt to your problem."
    },
    {
    "metadata": {
    @@ -1183,7 +1182,6 @@
    },
    {
    "metadata": {
    "scrolled": false,
    "trusted": false
    },
    "cell_type": "code",
    @@ -1227,7 +1225,6 @@
    },
    {
    "metadata": {
    "scrolled": true,
    "trusted": false
    },
    "cell_type": "code",
    @@ -1263,8 +1260,7 @@
    },
    {
    "metadata": {
    "trusted": true,
    "scrolled": true
    "trusted": true
    },
    "cell_type": "code",
    "source": "Dataset",
    @@ -1448,7 +1444,7 @@
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "Finally we need to set create the `__len__` and `__getitem__` methods which every pytorch `Dataset` must have. \n\n def __getitem__(self,i:int)->Tuple[Image, np.ndarray, np.ndarray]: \n return open_image(self.x[i]), torch.tensor([self.y_gender[i], self.y_race[i], self.y_age[i]]).float()\n"
    "source": "Finally we need to set create the `__len__` and `__getitem__` methods which every pytorch `Dataset` must have. "
    },
    {
    "metadata": {},
    @@ -1480,8 +1476,7 @@
    },
    {
    "metadata": {
    "trusted": true,
    "scrolled": true
    "trusted": true
    },
    "cell_type": "code",
    "source": "valid_ds[0][0].shape, valid_ds[0][1]",
    @@ -1637,7 +1632,6 @@
    },
    {
    "metadata": {
    "scrolled": true,
    "trusted": false
    },
    "cell_type": "code",
    @@ -1677,6 +1671,7 @@
    },
    {
    "metadata": {
    "collapsed": true,
    "trusted": true
    },
    "cell_type": "code",
    @@ -1736,7 +1731,7 @@
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "In this post we showed:\n1. an example of a multi-task deep learning model: predicting gender, race (both classification tasks), and age (a regression task), respectively, based on the face image;\n2. that the multi-task model makes better predictions than the individual model; and\n2. how to use the fastai library to implement the model.\n\nHopefully this convinces you to \n1. try building a multi-task model when related labels are available (it is very common in real life data) \n2. try using fastai library for it as it is easy and provides best practices out of the box.\n\nWe haven't considered a lot of things.\n1. We've only scratched the surface of multi-task learning. There are a large variety of techniques to learn and experiment. Wikipedia and Sebastian Ruder's [article](http://ruder.io/multi-task/) are good reference.\n2. We kept the model building part simple on purpose to focus on the multi-task part. We didn't experiment with model architecture including the pretrained backbone and the top layers, we didn't tune dropout, and we didn't consider progressively increasing image size for training, a effective techique taught by fastai. In a real world application these are worth considering and testing, and fastai library usually makes it easy."
    "source": "In this notebook we showed:\n1. an example of a multi-task deep learning model: predicting gender, race (both classification tasks), and age (a regression task), respectively, based on the face image;\n2. that the multi-task model makes better predictions than the individual model; and\n2. how to use the fastai library to implement the model.\n\nHopefully this convinces you to \n1. try building a multi-task model when related labels are available (it is very common in real life data) \n2. try using fastai library for it as it is easy and provides best practices out of the box.\n\nWe haven't considered a lot of things.\n1. We've only scratched the surface of multi-task learning. There are a large variety of techniques to learn and experiment. Wikipedia and Sebastian Ruder's [article](http://ruder.io/multi-task/) are good reference.\n2. We kept the model building part simple on purpose to focus on the multi-task part. We didn't experiment with model architecture including the pretrained backbone and the top layers, we didn't tune dropout, and we didn't consider progressively increasing image size for training, a effective technique taught by fastai. In a real world application these are worth considering and testing, and fastai library usually makes it easy."
    },
    {
    "metadata": {
  3. yang-zhang revised this gist Nov 8, 2018. 1 changed file with 16 additions and 1 deletion.
    17 changes: 16 additions & 1 deletion multi-face.ipynb
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,22 @@
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "First, some quick introduction to multi-task learnig.\n\nWhat it is according to [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning):\n> Multi-task learning (MTL) is a subfield of machine learning in which *multiple learning tasks are solved at the same time*, while exploiting commonalities and differences across tasks. \n\n In this post, *multiple learning tasks* are the three tasks to predict gender, race (both classification tasks), and age (a regression task), respectively.\n\nWhy do it according to [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning):\n> This can result in *improved learning efficiency and prediction accuracy* for the task-specific models, when compared to training the models separately.\n\n In this post,\n - Regarding *improved learning efficiency*: we run the multi-task model once instead of three time for the single-task models (all four models have similar run time). \n - Regarding *improved prediction accuracy*: we will show that the multi-task model cut average age prediction error in half from 10 years to 5 years while holding the gender and race prediction accuracy almost the same. \n\n\nWhy it works according to [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning): \n> Multi-task learning works because *regularization induced by requiring an algorithm to perform well on a related task* can be superior to regularization that prevents overfitting by penalizing all complexity uniformly. \n \n We will not cover the math here. Wikipedia provides some good resources. "
    "source": "First, some quick introduction to multi-task learnig."
    },
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "### What it is \n\n> [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning): Multi-task learning (MTL) is a subfield of machine learning in which *multiple learning tasks are solved at the same time*, while exploiting commonalities and differences across tasks. \n\n In this post, *multiple learning tasks* are the three tasks to predict gender, race (both classification tasks), and age (a regression task), respectively."
    },
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "### Why do it\n> [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning): This can result in *improved learning efficiency and prediction accuracy* for the task-specific models, when compared to training the models separately.\n\n In this post,\n - Regarding *improved learning efficiency*: we run the multi-task model once instead of three time for the single-task models (all four models have similar run time). \n - Regarding *improved prediction accuracy*: we will show that the multi-task model cut average age prediction error in half from 10 years to 5 years while holding the gender and race prediction accuracy almost the same. "
    },
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "### Why it works \n> [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning): Multi-task learning works because *regularization induced by requiring an algorithm to perform well on a related task* can be superior to regularization that prevents overfitting by penalizing all complexity uniformly. \n \n We will not cover the math here. Wikipedia provides some good resources. "
    },
    {
    "metadata": {},
  4. yang-zhang revised this gist Nov 8, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion multi-face.ipynb
    Original file line number Diff line number Diff line change
    @@ -18,7 +18,7 @@
    {
    "metadata": {},
    "cell_type": "markdown",
    "source": "First, some quick introduction to multi-task learnig.\n\n1. What it is according to [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning):\n> Multi-task learning (MTL) is a subfield of machine learning in which *multiple learning tasks are solved at the same time*, while exploiting commonalities and differences across tasks. \n\n In this post, *multiple learning tasks* are the three tasks to predict gender, race (both classification tasks), and age (a regression task), respectively.\n\n2. Why do it according to [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning):\n> This can result in *improved learning efficiency and prediction accuracy* for the task-specific models, when compared to training the models separately.\n\n In this post,\n - Regarding *improved learning efficiency*: we run the multi-task model once instead of three time for the single-task models (all four models have similar run time). \n - Regarding *improved prediction accuracy*: we will show that the multi-task model cut average age prediction error in half from 10 years to 5 years while holding the gender and race prediction accuracy almost the same. \n\n\n3. Why it works according to [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning): \n> Multi-task learning works because *regularization induced by requiring an algorithm to perform well on a related task* can be superior to regularization that prevents overfitting by penalizing all complexity uniformly. \n \n We will not cover the math here. Wikipedia provides some good resources. "
    "source": "First, some quick introduction to multi-task learnig.\n\nWhat it is according to [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning):\n> Multi-task learning (MTL) is a subfield of machine learning in which *multiple learning tasks are solved at the same time*, while exploiting commonalities and differences across tasks. \n\n In this post, *multiple learning tasks* are the three tasks to predict gender, race (both classification tasks), and age (a regression task), respectively.\n\nWhy do it according to [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning):\n> This can result in *improved learning efficiency and prediction accuracy* for the task-specific models, when compared to training the models separately.\n\n In this post,\n - Regarding *improved learning efficiency*: we run the multi-task model once instead of three time for the single-task models (all four models have similar run time). \n - Regarding *improved prediction accuracy*: we will show that the multi-task model cut average age prediction error in half from 10 years to 5 years while holding the gender and race prediction accuracy almost the same. \n\n\nWhy it works according to [wikipedia](https://en.wikipedia.org/wiki/Multi-task_learning): \n> Multi-task learning works because *regularization induced by requiring an algorithm to perform well on a related task* can be superior to regularization that prevents overfitting by penalizing all complexity uniformly. \n \n We will not cover the math here. Wikipedia provides some good resources. "
    },
    {
    "metadata": {},
  5. yang-zhang revised this gist Nov 8, 2018. 1 changed file with 14 additions and 12 deletions.
    26 changes: 14 additions & 12 deletions multi-face.ipynb
    14 additions, 12 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
  6. yang-zhang created this gist Nov 8, 2018.
    1,805 changes: 1,805 additions & 0 deletions multi-face.ipynb
    1,805 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.