In [1]:
%pylab
%matplotlib inline


Using matplotlib backend: TkAgg
Populating the interactive namespace from numpy and matplotlib

In [2]:
cd ..


/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-net-work

In [3]:
import sys
import numpy as np
import skimage
import cv2
import sklearn

In [22]:
import time

In [4]:
from holoviews import *


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-008fb02dab24> in <module>()
----> 1 from holoviews import *

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/holoviews/__init__.py in <module>()
      8                             commit="$Format:%h$", reponame='holoviews')
      9 
---> 10 from .core.dimension import Dimension         # pyflakes:ignore (API import)
     11 from .core.boundingregion import BoundingBox  # pyflakes:ignore (API import)
     12 from .core.layout import *                    # pyflakes:ignore (API import)

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/holoviews/core/__init__.py in <module>()
      1 from .boundingregion import *  # pyflakes:ignore (API import)
----> 2 from .dimension import *       # pyflakes:ignore (API import)
      3 from .element import *         # pyflakes:ignore (API import)
      4 from .layout import *          # pyflakes:ignore (API import)
      5 from .operation import *       # pyflakes:ignore (API import)

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/holoviews/core/dimension.py in <module>()
     10 
     11 from ..core.util import valid_identifier
---> 12 from .options import Store
     13 from .pprint import PrettyPrinter
     14 

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/holoviews/core/options.py in <module>()
     66 
     67 
---> 68 class Cycle(param.Parameterized):
     69     """
     70     A simple container class that specifies cyclic options. A typical

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/holoviews/core/options.py in Cycle()
     78 
     79     items = param.List(default=None, allow_None=True,  doc="""
---> 80         If supplied, the explicit list of items to be cycled over.""")
     81 
     82     rckey = param.String(default='axes.color_cycle', doc="""

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/param/__init__.pyc in __init__(self, default, class_, instantiate, bounds, **params)
   1056         self.class_ = class_
   1057         self.bounds = bounds
-> 1058         self._check_bounds(default)
   1059         Parameter.__init__(self,default=default,instantiate=instantiate,
   1060                            **params)

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/param/__init__.pyc in _check_bounds(self, val)
   1074         """
   1075         if not (isinstance(val,list)):
-> 1076             raise ValueError("List '%s' must be a list."%(self._attrib_name))
   1077 
   1078         if self.bounds is not None:

AttributeError: _attrib_name

In [5]:
%load_ext holoviews.ipython


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-f42f741b409a> in <module>()
----> 1 get_ipython().magic(u'load_ext holoviews.ipython')

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in magic(self, arg_s)
   2302         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2303         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2304         return self.run_line_magic(magic_name, magic_arg_s)
   2305 
   2306     #-------------------------------------------------------------------------

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/IPython/core/interactiveshell.pyc in run_line_magic(self, magic_name, line)
   2223                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2224             with self.builtin_trap:
-> 2225                 result = fn(*args,**kwargs)
   2226             return result
   2227 

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/IPython/core/magics/extension.pyc in load_ext(self, module_str)

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/IPython/core/magic.pyc in <lambda>(f, *a, **k)
    191     # but it's overkill for just that one bit of state.
    192     def magic_deco(arg):
--> 193         call = lambda f, *a, **k: f(*a, **k)
    194 
    195         if callable(arg):

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/IPython/core/magics/extension.pyc in load_ext(self, module_str)
     61         if not module_str:
     62             raise UsageError('Missing module name.')
---> 63         res = self.shell.extension_manager.load_extension(module_str)
     64 
     65         if res == 'already loaded':

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/IPython/core/extensions.pyc in load_extension(self, module_str)
     83             if module_str not in sys.modules:
     84                 with prepended_to_syspath(self.ipython_extension_dir):
---> 85                     __import__(module_str)
     86             mod = sys.modules[module_str]
     87             if self._call_load_ipython_extension(mod):

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/holoviews/__init__.py in <module>()
      8                             commit="$Format:%h$", reponame='holoviews')
      9 
---> 10 from .core.dimension import Dimension         # pyflakes:ignore (API import)
     11 from .core.boundingregion import BoundingBox  # pyflakes:ignore (API import)
     12 from .core.layout import *                    # pyflakes:ignore (API import)

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/holoviews/core/__init__.py in <module>()
      1 from .boundingregion import *  # pyflakes:ignore (API import)
----> 2 from .dimension import *       # pyflakes:ignore (API import)
      3 from .element import *         # pyflakes:ignore (API import)
      4 from .layout import *          # pyflakes:ignore (API import)
      5 from .operation import *       # pyflakes:ignore (API import)

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/holoviews/core/dimension.py in <module>()
     10 
     11 from ..core.util import valid_identifier
---> 12 from .options import Store
     13 from .pprint import PrettyPrinter
     14 

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/holoviews/core/options.py in <module>()
     66 
     67 
---> 68 class Cycle(param.Parameterized):
     69     """
     70     A simple container class that specifies cyclic options. A typical

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/holoviews/core/options.py in Cycle()
     78 
     79     items = param.List(default=None, allow_None=True,  doc="""
---> 80         If supplied, the explicit list of items to be cycled over.""")
     81 
     82     rckey = param.String(default='axes.color_cycle', doc="""

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/param/__init__.pyc in __init__(self, default, class_, instantiate, bounds, **params)
   1056         self.class_ = class_
   1057         self.bounds = bounds
-> 1058         self._check_bounds(default)
   1059         Parameter.__init__(self,default=default,instantiate=instantiate,
   1060                            **params)

/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-venv-auto/lib/python2.7/site-packages/param/__init__.pyc in _check_bounds(self, val)
   1074         """
   1075         if not (isinstance(val,list)):
-> 1076             raise ValueError("List '%s' must be a list."%(self._attrib_name))
   1077 
   1078         if self.bounds is not None:

AttributeError: _attrib_name

In [6]:
import neukrill_net.utils
import neukrill_net.highlevelfeatures

In [7]:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

In [8]:
settings = neukrill_net.utils.Settings('settings.json')

In [9]:
X,y = settings.flattened_train_paths(settings.classes)

In [10]:
img = neukrill_net.highlevelfeatures.loadimage(X[0])

In [11]:
imgplot = plt.imshow(img)
imgplot.set_cmap('gray')



In [12]:
import skimage.feature

In [13]:
M = skimage.feature.greycomatrix(img, [1], 0, levels=256, normed=True)

In [14]:
M.shape


Out[14]:
(256, 256, 1, 1)

In [15]:
M.squeeze()


Out[15]:
array([[ 0.        ,  0.        ,  0.        , ...,  0.        ,
         0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        , ...,  0.        ,
         0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        , ...,  0.        ,
         0.        ,  0.        ],
       ..., 
       [ 0.        ,  0.        ,  0.        , ...,  0.0015674 ,
         0.0031348 ,  0.01593521],
       [ 0.        ,  0.        ,  0.        , ...,  0.00365726,
         0.01593521,  0.03605016],
       [ 0.        ,  0.        ,  0.        , ...,  0.01410658,
         0.03761755,  0.76985371]])

In [16]:
%%opts Image style(cmap='coolwarm')
Image((M.squeeze()-M.min())*(M.max()-M.min()))


ERROR: Cell magic `%%opts` not found.

In [17]:
reload(neukrill_net.highlevelfeatures)


Out[17]:
<module 'neukrill_net.highlevelfeatures' from '/afs/inf.ed.ac.uk/user/s11/s1145806/Documents/git/neukrill-net-tools/neukrill_net/highlevelfeatures.pyc'>

In [18]:
hlf = neukrill_net.highlevelfeatures.CoocurProps()

In [19]:
hlf.extract_image(img)


Out[19]:
array([[  8.94109831e+01,   2.33748865e+02,   3.84509414e+02,
          5.26368752e+02,   6.42020154e+02,   7.26356382e+02,
          7.71580354e+02,   7.91133169e+02,   8.07771936e+02,
          8.25750940e+02,   8.46006732e+02,   8.65581185e+02,
          8.88185145e+02,   9.09241280e+02,   9.31630503e+02,
          9.53560952e+02,   9.74185997e+02,   9.96331812e+02],
       [  1.83858521e+00,   2.99505974e+00,   3.94732815e+00,
          4.73317533e+00,   5.33734531e+00,   5.78863418e+00,
          6.07474006e+00,   6.24732499e+00,   6.38570297e+00,
          6.52894236e+00,   6.67213181e+00,   6.79616729e+00,
          6.94389156e+00,   7.07420303e+00,   7.20536685e+00,
          7.29957729e+00,   7.40371459e+00,   7.52496845e+00],
       [  8.40084695e-01,   8.26183165e-01,   8.15821243e-01,
          8.02941013e-01,   7.95434761e-01,   7.88870645e-01,
          7.82232097e-01,   7.75070516e-01,   7.71926731e-01,
          7.65476700e-01,   7.62281847e-01,   7.59127917e-01,
          7.56305787e-01,   7.54961425e-01,   7.50305416e-01,
          7.50588006e-01,   7.49492059e-01,   7.46619366e-01],
       [  5.96495631e-01,   5.79167049e-01,   5.68021035e-01,
          5.50765298e-01,   5.38027011e-01,   5.24626488e-01,
          5.14111233e-01,   5.01112629e-01,   4.95427673e-01,
          4.86848350e-01,   4.80802771e-01,   4.76177174e-01,
          4.72000191e-01,   4.70452396e-01,   4.64396625e-01,
          4.63196895e-01,   4.62578103e-01,   4.58417435e-01],
       [  7.72331281e-01,   7.61030016e-01,   7.53661990e-01,
          7.42131370e-01,   7.33484501e-01,   7.24280265e-01,
          7.17006625e-01,   7.07851751e-01,   7.03822562e-01,
          6.97647562e-01,   6.93274867e-01,   6.89879555e-01,
          6.86788186e-01,   6.85566058e-01,   6.81118767e-01,
          6.80196244e-01,   6.79676649e-01,   6.76613325e-01],
       [  8.74966675e-01,   6.78312406e-01,   4.79365635e-01,
          2.98999447e-01,   1.59252766e-01,   6.49875146e-02,
          2.39117444e-02,   1.67861331e-02,   1.40638698e-02,
          1.05485550e-02,   4.85300532e-03,   8.64317606e-04,
         -5.76297384e-03,  -9.92750856e-03,  -1.48418163e-02,
         -1.86466840e-02,  -1.99869197e-02,  -2.18218335e-02]])

In [20]:
neukrill_net.highlevelfeatures.CoocurProps(num_angles=8).extract_image(img)


Out[20]:
array([[  1.17428038e+02,   1.89596979e+02,   3.71757996e+02,
          5.42268418e+02,   6.73977672e+02,   7.16145786e+02,
          7.80393091e+02,   8.12691427e+02,   8.21010811e+02,
          8.47480913e+02,   8.76248802e+02,   8.86036029e+02,
          9.17234941e+02,   9.49361347e+02,   9.81212789e+02,
          9.92178014e+02,   1.02502690e+03,   1.06022794e+03],
       [  2.08998226e+00,   2.66821952e+00,   3.84167395e+00,
          4.80607179e+00,   5.53040286e+00,   5.75604730e+00,
          6.13952547e+00,   6.40731029e+00,   6.47649928e+00,
          6.69422998e+00,   6.92582249e+00,   6.98784024e+00,
          7.20834929e+00,   7.43979831e+00,   7.64416070e+00,
          7.69126592e+00,   7.88339788e+00,   8.09769751e+00],
       [  8.37396275e-01,   8.30445509e-01,   8.14798875e-01,
          8.00121406e-01,   7.90376200e-01,   7.87094142e-01,
          7.78041077e-01,   7.68082550e-01,   7.66510658e-01,
          7.57246223e-01,   7.49762983e-01,   7.48186018e-01,
          7.42604722e-01,   7.38228020e-01,   7.34011316e-01,
          7.34152610e-01,   7.30819285e-01,   7.27971226e-01],
       [  5.92931976e-01,   5.84267684e-01,   5.65129355e-01,
          5.44692657e-01,   5.28342072e-01,   5.21641810e-01,
          5.07269341e-01,   4.89544438e-01,   4.86701960e-01,
          4.72146952e-01,   4.60854703e-01,   4.58541904e-01,
          4.49853527e-01,   4.44697804e-01,   4.39187093e-01,
          4.38587227e-01,   4.34117951e-01,   4.29891054e-01],
       [  7.70017121e-01,   7.64366488e-01,   7.51743414e-01,
          7.38019356e-01,   7.26826156e-01,   7.22224038e-01,
          7.12204091e-01,   6.99599416e-01,   6.97584821e-01,
          6.86989564e-01,   6.78635794e-01,   6.76938138e-01,
          6.70381602e-01,   6.66400802e-01,   6.62260295e-01,
          6.61799034e-01,   6.58289634e-01,   6.55064829e-01],
       [  8.37376313e-01,   7.39049178e-01,   5.00386801e-01,
          2.89382027e-01,   1.39013267e-01,   9.18806409e-02,
          3.56336332e-02,   2.17996503e-02,   2.04385187e-02,
          1.55523487e-02,   9.47457588e-03,   7.48023202e-03,
          7.15892106e-04,  -5.30179478e-03,  -9.78445250e-03,
         -1.16868864e-02,  -1.49283667e-02,  -1.87186045e-02]])

In [23]:
t0 = time.time()
XF = hlf.transform(X)
print("Computing features took {}".format(time.time()-t0))


Computing features took 12835.581182

In [25]:
sklearn.externals.joblib.dump((hlf,XF,y),'cache/coocurprops.pkl')


Out[25]:
['cache/coocurprops.pkl', 'cache/coocurprops.pkl_01.npy']

In [26]:
XF.shape


Out[26]:
(1, 30336, 108)

Naive Bayes


In [27]:
import sklearn.naive_bayes

In [28]:
clf = sklearn.naive_bayes.GaussianNB()

t0 = time.time()
X_train, X_test, y_train, y_test = sklearn.cross_validation.train_test_split(
    sklearn.preprocessing.StandardScaler().fit_transform(XF.squeeze(0)), y, test_size=0.5, random_state=42)
clf.fit(X_train, y_train)

t1 = time.time()
total = t1-t0
print("Time={}".format(total))

print("Accuracy={}".format(clf.score(X_test, y_test)))
print("Logloss={}".format(sklearn.metrics.log_loss(y_test, clf.predict_proba(X_test))))


Time=0.773290872574
Accuracy=0.20266350211
Logloss=22.9722176476

Logistic Regression


In [29]:
clf = sklearn.linear_model.LogisticRegression(random_state=42)

t0 = time.time()
X_train, X_test, y_train, y_test = sklearn.cross_validation.train_test_split(
    sklearn.preprocessing.StandardScaler().fit_transform(XF.squeeze(0)), y, test_size=0.5, random_state=42)
clf.fit(X_train, y_train)

t1 = time.time()
total = t1-t0
print("Time={}".format(total))

print("Accuracy={}".format(clf.score(X_test, y_test)))
print("Logloss={}".format(sklearn.metrics.log_loss(y_test, clf.predict_proba(X_test))))


Time=105.316153049
Accuracy=0.444290611814
Logloss=2.2076145382