Skip to content

Instantly share code, notes, and snippets.

@takotab
Last active February 26, 2019 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takotab/b2437cd93f2eeaad875dc3ffc479053f to your computer and use it in GitHub Desktop.
Save takotab/b2437cd93f2eeaad875dc3ffc479053f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"from fastai import *\n",
"import matplotlib.pyplot as plt\n",
"from fastai.vision import *\n",
"from fastai.callbacks.hooks import num_features_model\n",
"import cv2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"from fastai.callbacks.hooks import num_features_model\n",
"from fastai.vision import create_head"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from motion.detect_human import BBoxDataset"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dataset has 1150 samples.\n",
"Dataset has 225 samples.\n",
"(3, 500, 500) (4,)\n"
]
}
],
"source": [
"SZ = 500\n",
"bbox_ds = BBoxDataset(\"coco/val2017_one_human.csv\",type = 'val', size = SZ)\n",
"bbox_ds_val = BBoxDataset(\"coco/val2017_one_human_val.csv\",type='valid', size = SZ)\n",
"print(bbox_ds_val[0][0].shape,bbox_ds_val[0][1].shape)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"BS = 8\n",
"\n",
"train_dl = DataLoader(bbox_ds, BS)\n",
"valid_dl = DataLoader(bbox_ds_val, BS, \n",
" shuffle=False, num_workers=2, pin_memory=True)\n",
"\n",
"data_bunch = DataBunch(train_dl, valid_dl)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"class FlukeDetector2(nn.Module):\n",
" def __init__(self, arch=models.resnet18):\n",
" super().__init__() \n",
" self.cnn = create_body(arch)\n",
" self.head = create_head(num_features_model(self.cnn) * 2, 4)\n",
" \n",
" def forward(self, im):\n",
" x = self.cnn(im)\n",
" x = self.head(x)\n",
" return x.sigmoid_()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Without loss function"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"learn = Learner(data_bunch, FlukeDetector2(arch=models.resnet50))\n",
"learn = learn.load('fastai_bbox_detect_humans_val3')"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"preds, targs = learn.get_preds()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1.345302, 1.258236, 2.051251, 2.114838], dtype=float32)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"predicted_bboxes = ((preds) ).numpy()\n",
"targets = ((targs) ).numpy()\n",
"predicted_bboxes[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note the result is above 1 while the model end with a sigmoid function."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## With loss function"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from torch.nn import L1Loss\n",
"learn = Learner(data_bunch, FlukeDetector2(arch=models.resnet50), loss_func=L1Loss())\n",
"learn = learn.load('fastai_bbox_detect_humans_val3')"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"preds, targs = learn.get_preds()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.296618, 0.229711, 0.71845 , 0.748978], dtype=float32)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"predicted_bboxes = ((preds) ).numpy()\n",
"targets = ((targs) ).numpy()\n",
"predicted_bboxes[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Different results! "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.548, 0.236, 0.802, 0.868], dtype=float32)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"targets[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"and this time they seem fine what I expected."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"```text\n",
"=== Software === \n",
"python : 3.7.2\n",
"fastai : 1.0.42\n",
"fastprogress : 0.1.18\n",
"torch : 1.0.1.post2\n",
"torch cuda : 10.0.130 / is available\n",
"torch cudnn : 7402 / is enabled\n",
"\n",
"=== Hardware === \n",
"torch devices : 1\n",
" - gpu0 : GeForce GTX 1080\n",
"\n",
"=== Environment === \n",
"platform : Linux-4.15.0-45-generic-x86_64-with-debian-buster-sid\n",
"distro : #48-Ubuntu SMP Tue Jan 29 16:28:13 UTC 2019\n",
"conda env : Unknown\n",
"python : /home/tako/devtools/furry-geras/env/bin/python\n",
"sys.path : /home/tako/devtools/furry-geras\n",
"/home/tako/devtools/furry-geras/env/lib/python37.zip\n",
"/home/tako/devtools/furry-geras/env/lib/python3.7\n",
"/home/tako/devtools/furry-geras/env/lib/python3.7/lib-dynload\n",
"/usr/local/lib/python3.7\n",
"\n",
"/home/tako/devtools/furry-geras/env/lib/python3.7/site-packages\n",
"/home/tako/devtools/furry-geras/env/lib/python3.7/site-packages/IPython/extensions\n",
"/home/tako/.ipython\n",
"no nvidia-smi is found\n",
"```\n",
"\n",
"Please make sure to include opening/closing ``` when you paste into forums/github to make the reports appear formatted as code sections.\n",
"\n",
"Optional package(s) to enhance the diagnostics can be installed with:\n",
"pip install distro\n",
"Once installed, re-run this utility to get the additional information\n"
]
}
],
"source": [
"from fastai.utils.collect_env import *\n",
"show_install()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "env-furry-geras",
"language": "python",
"name": "env-furry-geras"
},
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment