Skip to content

Commit

Permalink
upload
Browse files Browse the repository at this point in the history
  • Loading branch information
quantran committed Jan 6, 2019
0 parents commit f4dba45
Show file tree
Hide file tree
Showing 14 changed files with 8,661 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
__pycache__/
.ipynb_checkpoints

*.py[cod]
*$py.class
.Python
env/
build/

data_draw/
data_crop/
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This is a repo for this detailed blog post on [quantran.xyz/blog/building-an-image-classification-model-from-a-to-z/]. Including Grad-CAM visualization Python module ([code](gradcam.py) and [use cases](gradcam-usecase.ipynb)) and notebooks containing training processes and steps. If you want to trace my process, here is the order (for more details you can visit the blog post):
1. [crop-fix.ipynb]
2. [google-image-download.ipynb]
3. [image-augmentation.ipynb]
4. [resnet-training-with-upsampling.ipynb]
5. [final-training-all-images.ipynb]
6. [live-action-model-check.ipynb]

Tools use: [Fastai library which built on top of Pytorch 1.0](https://github.com/fastai/fastai), Matplotlib, scikit-image, [google-images-download](https://github.com/hardikvasa/google-images-download)
175 changes: 175 additions & 0 deletions crop-fix.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"from PIL import Image\n",
"from pathlib import Path\n",
"from concurrent.futures import ProcessPoolExecutor\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import os"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"data = Path('data')\n",
"data_crop = Path('data_crop')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['pocahontas',\n",
" 'mulan',\n",
" 'kiki',\n",
" 'howl',\n",
" 'hercules',\n",
" 'mermaid',\n",
" 'models',\n",
" 'beauty',\n",
" 'castle',\n",
" 'mononoke',\n",
" 'tarzan']"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"classes=os.listdir(str(data))\n",
"classes"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"classes = ['pocahontas',\n",
" 'mulan',\n",
" 'kiki',\n",
" 'howl',\n",
" 'hercules',\n",
" 'mermaid',\n",
" 'beauty',\n",
" 'castle',\n",
" 'mononoke',\n",
" 'tarzan']"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# for c in classes:\n",
"# os.mkdir(data_crop/c)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"def crop_class(c):\n",
" print(f'Processing {c}...')\n",
" exc=20\n",
" src_path = data/c\n",
" dest_path = data_crop/c\n",
" img_fnames = os.listdir(str(src_path))\n",
" for img_fname in img_fnames:\n",
" img = Image.open(src_path/img_fname)\n",
" cropped = img.crop((exc, exc, img.size[0]-exc, img.size[1]-exc))\n",
" cropped.save(dest_path/img_fname)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with ProcessPoolExecutor(max_workers=4) as executor:\n",
" for c,_ in zip(classes,executor.map(crop_class,classes)):\n",
" continue\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n",
"True\n"
]
}
],
"source": [
"#sanity check\n",
"for c in classes:\n",
" src_path = data/c\n",
" dest_path = data_crop/c\n",
" org = len(os.listdir(str(src_path)))\n",
" fin = len(os.listdir(str(dest_path)))\n",
" print(org==fin)"
]
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit f4dba45

Please sign in to comment.