Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jabooth/85ee24abbdd7103aa3281487bb99a3e8 to your computer and use it in GitHub Desktop.
Save jabooth/85ee24abbdd7103aa3281487bb99a3e8 to your computer and use it in GitHub Desktop.
Pickling menpofit pertained models
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Imagine this notebook actually trained an AAM. I'll cheat and load one\n",
"# @nontas has trained. Point is it doesn't matter how you get to the\n",
"# menpofit model, the only tricky bit is how to save out the result afterwards\n",
"import menpo.io as mio\n",
"aam = mio.import_pickle('/Users/jab08/Downloads/aam_p34.pkl')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def sampling_for_aam(aam, sampling_step=[2, 2]):\n",
" sampling = []\n",
" for st, ps in zip(sampling_step, aam.patch_shape):\n",
" sampling_grid = np.zeros(ps, dtype=np.bool)\n",
" sampling_grid[::st, ::st] = True\n",
" sampling.append(sampling_grid)\n",
" return sampling\n",
"\n",
"# We *can't* pickle the function above - as it doesn't exist in\n",
"# a package that will exist at run time. We *can* pickle the resulting\n",
"# sampling arrays, so generate them alongside the AAM before saving.\n",
"sampling = sampling_for_aam(aam)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's the bit you need to add to the bottom of your scripts.\n",
"Point is, we build a partial over all the state we need to\n",
"call the fitter constructor. As we have provided all arguments\n",
"and keyword arguments, we only need to call the function without\n",
"arguments to load the fitter we want."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from functools import partial\n",
"from menpofit.aam import LucasKanadeAAMFitter, WibergInverseCompositional\n",
"\n",
"create_fitter = partial(LucasKanadeAAMFitter, aam, \n",
" lk_algorithm_cls=WibergInverseCompositional, \n",
" sampling=sampling)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Save out the immediately invokable function that we will invoke after importing\n",
"to create (at load time) the fitter.\n",
"\n",
"Note that this is fine as we are saving a partial, which only uses:\n",
"\n",
"1. Objects that are pickleable (numpy arrays, numbers etc etc)\n",
"2. Classes/Functions that exist in packages like `menpofit`, and NOT in this notebook (e.g. `menpofit.aam.WibergInverseCompositional` is fine)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mio.export_pickle(create_fitter, './fitter_py3.pkl', protocol=4)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load time\n",
"\n",
"Just load the partial back and immediately invoke it to build the fitter."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import menpo.io as mio\n",
"fitter = mio.import_pickle('./fitter_py3.pkl')()"
]
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment