{ "metadata": { "name": "02 Compute Gabor Filter Responses" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import numpy\n", "import math\n", "import scipy\n", "import scipy.io\n", "import scipy.misc\n", "import scipy.cluster\n", "import scipy.cluster.vq\n", "import matplotlib\n", "import matplotlib.pyplot\n", "import os\n", "import IPython\n", "import IPython.parallel\n", "import itertools\n", "import random\n", "import sklearn\n", "import sklearn.decomposition\n", "import sklearn.manifold\n", "import sklearn.cluster\n", "import sklearn.feature_extraction\n", "import sklearn.feature_extraction.text\n", "\n", "base_path = \"/u/mlrobert/code/local/2013_drawing_assistant/data/\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "num_thetas = 8\n", "filter_path = base_path+\"filters/gabor/num_thetas=08/\"\n", "\n", "tmp = []\n", "\n", "for theta_index in range(num_thetas):\n", " \n", " current_g_mat = scipy.io.loadmat(filter_path+\"theta=%02d.mat\" % theta_index)\n", " current_g = current_g_mat[\"g\"]\n", " \n", " tmp.append(current_g)\n", "\n", "g = numpy.array(tmp)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "img_path = base_path+\"renderings/bunny2/apparent_ridges.num_latitude_lines=20.num_longitude_lines=20/\"\n", "responses_base_path = base_path+\"filter_responses/bunny2/apparent_ridges.num_latitude_lines=20.num_longitude_lines=20.gabor.num_thetas=08/\"\n", "debug_base_path = base_path+\"debug/filter_responses/bunny2/apparent_ridges.num_latitude_lines=20.num_longitude_lines=20.gabor.num_thetas=08/\"" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "def compute_gabor_filter_responses(img_name_ext):\n", " \n", " import numpy\n", " import scipy\n", " import scipy.io\n", " import scipy.misc\n", " import os\n", " import matplotlib\n", " import matplotlib.pyplot\n", "\n", " img_name = img_name_ext[:-4]\n", " img_ext = img_name_ext[-4:]\n", " \n", " img = scipy.misc.imread(img_path+img_name+img_ext).astype(numpy.float32)[:,:,0]\n", " img = (255.0 - img) / 255.0\n", " \n", " responses_path = responses_base_path+img_name+\"/\"\n", " debug_path = debug_base_path+img_name+\"/\"\n", " \n", " if not os.path.exists(responses_path):\n", " os.makedirs(responses_path)\n", "\n", " if not os.path.exists(debug_path):\n", " os.makedirs(debug_path)\n", " \n", " for theta_index in range(num_thetas):\n", " \n", " F_img = numpy.fft.fft2(img)\n", " F_img_centered = numpy.fft.fftshift(F_img)\n", " F_img_centered_filtered = F_img_centered * g[theta_index]\n", " F_img_centered_filtered_uncentered = numpy.fft.ifftshift(F_img_centered_filtered)\n", " filter_response = numpy.fft.ifft2(F_img_centered_filtered_uncentered)\n", " \n", " scipy.io.savemat(\"%stheta=%02d.mat\" % (responses_path, theta_index), {\"filter_response\":filter_response}, oned_as=\"column\")\n", " \n", " matplotlib.pyplot.clf();\n", " matplotlib.pyplot.imshow(numpy.abs(filter_response), interpolation=\"nearest\");\n", " matplotlib.pyplot.colorbar();\n", " matplotlib.pyplot.savefig(\"%stheta=%02d.png\" % (debug_path, theta_index));\n", " matplotlib.pyplot.clf();" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 46 }, { "cell_type": "code", "collapsed": false, "input": [ "img_names_exts = sorted(os.listdir(img_path))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 47 }, { "cell_type": "code", "collapsed": false, "input": [ "compute_gabor_filter_responses(img_names_exts[0])" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 48 }, { "cell_type": "code", "collapsed": false, "input": [ "client = IPython.parallel.Client()\n", "directView = client[:]\n", "\n", "directView[\"num_thetas\"] = num_thetas\n", "directView[\"img_path\"] = img_path\n", "directView[\"responses_base_path\"] = responses_base_path\n", "directView[\"debug_path\"] = debug_path\n", "directView[\"g\"] = g\n", "\n", "asyncResult = directView.map_async(compute_gabor_filter_responses, img_names_exts)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 49 }, { "cell_type": "code", "collapsed": false, "input": [ "print asyncResult.progress\n", "print asyncResult.ready()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "32\n", "True\n" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "result = asyncResult.get()\n", "print result" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]\n" ] } ], "prompt_number": 35 } ], "metadata": {} } ] }