Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prhbrt/ab544d1834aa41a9e77915d79285a1af to your computer and use it in GitHub Desktop.
Save prhbrt/ab544d1834aa41a9e77915d79285a1af to your computer and use it in GitHub Desktop.
Holistically-Nested Edge Detection Paper Example
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy\n",
"\n",
"%matplotlib notebook\n",
"from matplotlib import pyplot\n",
"\n",
"from skimage.io import imread\n",
"import caffe\n",
"import numpy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"image = imread('paper-example.jpg')\n",
"\n",
"# pyplot.figure()\n",
"# pyplot.imshow(image)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def preprocess_input(X):\n",
" X = numpy.array(X, dtype=numpy.float32)\n",
" X = X[...,::-1]\n",
" X -= numpy.array([[[[104.00698793,116.66876762,122.67891434]]]])\n",
" return X.transpose(0, 3, 1, 2) / 128"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"images = image[None]\n",
"preprocessed = preprocess_input(images)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(-0.95842904, 1.1796329)"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"preprocessed.min(), preprocessed.max()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"net = caffe.Net(\n",
" 'deploy.prototxt',\n",
" 'hed_pretrained_bsds.caffemodel',\n",
" caffe.TEST\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"net.blobs['data'].reshape(*preprocessed.shape)\n",
"net.blobs['data'].data[...] = preprocessed\n",
"\n",
"# run net and take argmax for prediction\n",
"net.forward()\n",
"out1 = net.blobs['sigmoid-dsn1'].data[0][0]\n",
"out2 = net.blobs['sigmoid-dsn2'].data[0][0]\n",
"out3 = net.blobs['sigmoid-dsn3'].data[0][0]\n",
"out4 = net.blobs['sigmoid-dsn4'].data[0][0]\n",
"out5 = net.blobs['sigmoid-dsn5'].data[0][0]\n",
"fuse = net.blobs['sigmoid-fuse'].data[0][0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# pyplot.rcParams['figure.figsize'] = (9, 5)\n",
"# for axis, out, title in zip(\n",
"# pyplot.subplots(2,3)[1].ravel(),\n",
"# [out1, out2, out3, out4, out5, numpy.zeros(out5.shape)],\n",
"# ['out1', 'out2', 'out3', 'out4', 'fusion', ''],\n",
"# ):\n",
"# if out is not None:\n",
"# axis.imshow(out)\n",
"# axis.set_xticks([])\n",
"# axis.set_yticks([])\n",
"# axis.set_xlabel(title)\n",
"# pyplot.tight_layout()"
]
}
],
"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.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment