Last active
November 25, 2015 13:25
-
-
Save kylemcdonald/b5814bdb82e0d53add1f to your computer and use it in GitHub Desktop.
Generating fc7-layer codes from one-hot prob-layer activations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"cellView": "both", | |
"colab_type": "code", | |
"collapsed": false, | |
"id": "i9hkSm1IOZNR" | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"data: 1505280 = 10x3x224x224\n", | |
"conv1_1: 32112640 = 10x64x224x224\n", | |
"conv1_2: 32112640 = 10x64x224x224\n", | |
"pool1: 8028160 = 10x64x112x112\n", | |
"conv2_1: 16056320 = 10x128x112x112\n", | |
"conv2_2: 16056320 = 10x128x112x112\n", | |
"pool2: 4014080 = 10x128x56x56\n", | |
"conv3_1: 8028160 = 10x256x56x56\n", | |
"conv3_2: 8028160 = 10x256x56x56\n", | |
"conv3_3: 8028160 = 10x256x56x56\n", | |
"conv3_4: 8028160 = 10x256x56x56\n", | |
"pool3: 2007040 = 10x256x28x28\n", | |
"conv4_1: 4014080 = 10x512x28x28\n", | |
"conv4_2: 4014080 = 10x512x28x28\n", | |
"conv4_3: 4014080 = 10x512x28x28\n", | |
"conv4_4: 4014080 = 10x512x28x28\n", | |
"pool4: 1003520 = 10x512x14x14\n", | |
"conv5_1: 1003520 = 10x512x14x14\n", | |
"conv5_2: 1003520 = 10x512x14x14\n", | |
"conv5_3: 1003520 = 10x512x14x14\n", | |
"conv5_4: 1003520 = 10x512x14x14\n", | |
"pool5: 250880 = 10x512x7x7\n", | |
"fc6: 40960 = 10x4096x1x1\n", | |
"fc6_fc6_0_split_0: 40960 = 10x4096x1x1\n", | |
"fc6_fc6_0_split_1: 40960 = 10x4096x1x1\n", | |
"fc6_fc6_0_split_2: 40960 = 10x4096x1x1\n", | |
"relu6: 40960 = 10x4096x1x1\n", | |
"drop6: 40960 = 10x4096x1x1\n", | |
"fc7: 40960 = 10x4096x1x1\n", | |
"relu7: 40960 = 10x4096x1x1\n", | |
"drop7: 40960 = 10x4096x1x1\n", | |
"fc8: 10000 = 10x1000x1x1\n", | |
"prob: 10000 = 10x1000x1x1\n" | |
] | |
} | |
], | |
"source": [ | |
"import numpy as np\n", | |
"from google.protobuf import text_format\n", | |
"import caffe\n", | |
"\n", | |
"# load network\n", | |
"# the prototxt has force_backward: true, and fc7 is separated into multiple blobs\n", | |
"model_name = 'vgg_ilsvrc_19'\n", | |
"model_path = '../caffe/models/' + model_name + '/'\n", | |
"net_fn = model_path + 'deploy-expanded.prototxt'\n", | |
"param_fn = model_path + 'net.caffemodel'\n", | |
"net = caffe.Classifier(net_fn, param_fn)\n", | |
"\n", | |
"# print blob names and sizes\n", | |
"for end in net.blobs.keys():\n", | |
" cur = net.blobs[end]\n", | |
" print end + ': {} = {}x{}x{}x{}'.format(cur.count, cur.num, cur.channels, cur.width, cur.height)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def optimize(net,\n", | |
" hot=0,\n", | |
" step_size=.01,\n", | |
" iter_n=100,\n", | |
" mu=.9,\n", | |
" basename='fc7',\n", | |
" start='relu7',\n", | |
" end='prob'):\n", | |
" base = net.blobs[basename]\n", | |
" first = net.blobs[start]\n", | |
" last = net.blobs[end]\n", | |
" base.data[0] = np.random.normal(.5, .1, base.data[0].shape)\n", | |
" base.diff[0] = 0.\n", | |
" velocity = np.zeros_like(base.data[0])\n", | |
" velocity_previous = np.zeros_like(base.data[0])\n", | |
" for i in range(iter_n):\n", | |
" net.forward(start=start, end=end)\n", | |
" target = np.zeros_like(last.data[0])\n", | |
" target.flat[hot] = 1.\n", | |
" error = target - last.data[0]\n", | |
" last.diff[0] = error\n", | |
" net.backward(start=end, end=start)\n", | |
" grad = base.diff[0]\n", | |
" learning_rate = (step_size / np.abs(grad).mean())\n", | |
" velocity_previous = velocity\n", | |
" velocity = mu * velocity + learning_rate * grad\n", | |
" base.data[0] += -mu * velocity_previous + (1 + mu) * velocity\n", | |
" base.data[0] = np.clip(base.data[0], 0, +1)\n", | |
" return base.data[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"in: [ 1. 0. 1. 0. 0. 0. 0. 0.]\n", | |
"out: [ 9.91814137e-01 4.00208119e-05 9.94682705e-06 9.07168669e-06\n", | |
" 1.13390133e-05 1.70246039e-05 1.00811658e-05 3.26871736e-06]\n" | |
] | |
} | |
], | |
"source": [ | |
"print 'in:', optimize(net, hot=0)[0:8]\n", | |
"print 'out:', net.blobs['prob'].data[0,0:8]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"100% (1000 of 1000) |#####################| Elapsed Time: 0:07:12 Time: 0:07:12\n" | |
] | |
} | |
], | |
"source": [ | |
"from progressbar import ProgressBar\n", | |
"vectors = []\n", | |
"pbar = ProgressBar()\n", | |
"for i in pbar(range(1000)):\n", | |
" vectors.append(optimize(net, hot=i).copy())\n", | |
"np.savetxt('vectors', vectors, fmt='%.2f', delimiter='\\t')" | |
] | |
} | |
], | |
"metadata": { | |
"colabVersion": "0.3.1", | |
"default_view": {}, | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.10" | |
}, | |
"views": {} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment