In [74]:
import sys
sys.path.append('/home/yuncong/Brain/pipeline_scripts')

import matplotlib
%matplotlib inline
#from IPython.display import display
#matplotlib.use('GTkAgg')
import matplotlib.pyplot as plt
import pickle
import os
from os import path
from sklearn.linear_model import SGDRegressor
from sklearn.linear_model import Perceptron
from sklearn.cluster import KMeans
from sklearn import svm
from numpy.linalg import lstsq
import numpy as np
import sys
import utilities2014
reload(utilities2014)
from utilities2014 import *
from sklearn.grid_search import GridSearchCV
from sklearn.svm import SVC


from sklearn.preprocessing import MinMaxScaler

In [75]:
pcklPath = "/home/iizhaki/oasis/WebStem/pickles/"

mapped = pickle.load(open(pcklPath + "mapped.pck", "rb"))
props = pickle.load(open(pcklPath + "props.pck", "rb"))
hists = pickle.load(open(pcklPath + "hists.pck", "rb"))
refs = pickle.load(open(pcklPath + "refs.pck", "rb"))

In [76]:
tripletsPath = "/home/iizhaki/WebStem/triplets/"
imgsPath = "/home/iizhaki/WebStem/photos/"

In [77]:
os.environ['GORDON_DATA_DIR'] = '/home/yuncong/project/DavidData2014tif/'
os.environ['GORDON_REPO_DIR'] = '/home/yuncong/Brain'
os.environ['GORDON_RESULT_DIR'] = '/home/yuncong/project/DavidData2014results/'
os.environ['GORDON_LABELING_DIR'] = '/home/yuncong/project/DavidData2014labelings/'

In [78]:
def collectPageData(pPath, pData):
    redRef = ""
    blueRef = ""

    with open(pPath) as pFile:
        for pLine in pFile:
            pSplit = pLine.split()
            if len(pSplit) < 6:  # Maybe comment or blank space
                continue
            
            #print pSplit

            pName = pSplit[4]
            pColor = pSplit[5]
            
            # Initialize sets
            if "gold" in pColor:
                boundNum = int(pName.split("_")[3])
                superpixel = int(pName.split("_")[4])
                
                if "red" in pSplit[6]:
                    redRef = boundNum
                    if redRef not in pData:
                        pData[redRef] = set()
                    pData[redRef].add(superpixel)
                elif "blue" in pSplit[6]:
                    blueRef = boundNum
                    if blueRef not in pData:
                        pData[blueRef] = set()
                    pData[blueRef].add(superpixel)
                else:
                    print "ERROR!"
                    return "ERROR!"
                
                continue
            
            boundNum = int(pName.split("_")[2])
            superpixel = int(pName.split("_")[3])

            if "red" in pColor:
                if boundNum == redRef:
                    pData[redRef].add(superpixel)

            if "blue" in pColor:
                if boundNum == blueRef:
                    pData[blueRef].add(superpixel)

    return "Ok"

In [79]:
pagesPath = [(tripletsPath + f) for f in os.listdir(tripletsPath)]

In [80]:
pData = {}

for pageFile in pagesPath:
    print collectPageData(pageFile, pData)
    
print pData


Ok
Ok
Ok
Ok
Ok
Ok
Ok
{11: set([1281, 482, 571, 1125, 668, 748, 1389, 669, 563, 755, 1428, 570, 1243, 1148, 1117, 389]), 12: set([322, 259, 260, 326, 195, 168, 307, 394, 375, 243, 350, 425, 284, 349, 222, 223]), 13: set([928, 609, 1031, 777, 971, 1261, 1198, 1393, 1262, 1110]), 14: set([1056, 964, 997, 681, 878, 690, 821, 965]), 25: set([3171, 3144, 2953, 3415, 3154, 3157, 3241, 3320, 3353, 3068, 3326, 3253]), 26: set([3724, 3474, 3478, 3482, 3867, 3623, 3755, 3633, 3769, 3773, 3649, 3395, 3656, 3402, 3280, 3540, 3285, 3159, 3562, 3820, 3568, 3190]), 27: set([2402, 2470, 2631, 2669, 2638, 2515, 2581, 2550, 2745, 2395, 2670, 2421])}

In [ ]:


In [81]:
# 30 sections in total
collectHists = {}

for section in pData:
    # Initialize array of histograms
    collectHists[section] = []
    
    for superpixel in pData[section]:
        collectHists[section].append(hists[section][superpixel])

In [ ]:
# [(11, 7), (13, 5), (14, 12)], [(12, 12)]
# [(11, 22), (12, 11)], [(13, 25), (14, 17)]

# Some manual work:
predXs = []
predYs = []

for section in [11, 13, 14]:
    for currHist in collectHists[section]:
        predXs.append(currHist)
        predYs.append(1)
        
for section in [12]:
    for currHist in collectHists[section]:
        predXs.append(currHist)
        predYs.append(-1)
        
#predXs = np.array(predXs) * 1.0 / np.max(predXs)

In [ ]:
print predXs[0:2], predYs[0:2]
print predXs[-2:], predYs[-2:]

# print predYs[0][0], predYs[1][0]

In [ ]:
import shutil

allFiles = os.listdir(imgsPath)
print allFiles[0]

In [ ]:
for section in pData:
    spxs = pData[section]
    break ############
    for sp in spxs:
        exp = "ref_" + str(section) + "_" + str(sp)
        for fn in allFiles:
            if exp in fn:
                thedir = "/home/iizhaki/WebStem/temp/" + str(section)
                try:
                    os.stat(thedir)
                except:
                    os.mkdir(thedir)       

                shutil.copy2(imgsPath + "/" + fn, "/home/iizhaki/WebStem/temp/" + str(section))

In [ ]:


In [ ]:
predYs_arr = np.array(predYs)
predXs_arr  = np.array(predXs)
predXs_arr.shape

In [ ]:
plt.scatter(predXs_arr[predYs_arr==1,0], predXs_arr[predYs_arr==1,1], c='r')
plt.scatter(predXs_arr[predYs_arr==-1,0], predXs_arr[predYs_arr==-1,1], c='g')
plt.show()

#plt.savefig("/home/iizhaki/WebStem/temp/img_[(11,22),(12,11)]vs.[(13,25),(14,17)].png")

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [112]:
# Training SVM
X = predXs
y = predYs

X = np.array(X).astype(float)
#scaler = MinMaxScaler().fit(X)
#X = scaler.transform(X)

In [113]:
#X = [np.array(x) * 1.0 / np.max(x) for x in predXs]

#thetax, _, _, _ = np.linalg.lstsq(X, y)

#del clf
#clf = SGDRegressor()
#clf.fit(X, y)

#clf = svm.SVC(verbose = True)
#clf.fit(X, y) 

param_grid = {'C': [1, 1e1, 1e2, 1e3, 5e3, 1e4, 5e4, 1e5],
              'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1, 1, 10], }
#clf = GridSearchCV(SVC(kernel='poly', class_weight='auto', degree = 14), param_grid)
#clf = SVC(verbose = True, kernel='poly', class_weight='auto', degree=4) 
clf = SVC(verbose = True, kernel='poly', class_weight='auto', degree=5) 
clf = clf.fit(X, y)

#clf = Perceptron()
#clf.fit(X, y)


print predYs


[LibSVM][1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1]

In [114]:
def predict(data, theta):
    return np.dot(np.array(data), np.array(theta))

In [115]:
trueYes = 0
totalYes = 0
trueNo = 0
totalNo = 0

predictions = clf.predict(X)
print predictions 

for i in range(len(predictions)):
    #thePred =  predict(predXs[i], thetax)
    thePred = predictions[i]
    if (predYs[i] > 0):
        totalYes += 1
        if (thePred > 0.):
            trueYes += 1
    else:
        totalNo += 1
        if (thePred < 0):
            trueNo += 1
    
print "===================="
print trueYes * 1.0 / totalYes
print trueNo * 1.0 / totalNo


[ 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1 -1 -1 -1 -1 -1 -1 -1 -1]
====================
1.0
1.0

In [ ]:


In [116]:
#print clf.coef_ 
#print clf.intercept_

In [117]:
# [(25, 22), (27, 28)] vs. [(26, 3)]
# [(25, 22), (27, 28)] vs. [(26, 5)]
# [(11, 7), (13, 5), (14, 12)] vs. [(12, 12)]
# [(11, 22), (12, 11)], [(13, 25), (14, 17)]


viss = {}
#for cSection in [25, 26, 27]:
for cSection in [13, 14]:
    pos26 = []
    scores26 = []
    DM = DataManager(generate_hierarchy = False, stack = 'RS141', resol = 'x5', section = cSection)
    cSupporters = DM.load_pipeline_result('nonoverlappingSupporters', 'pkl')
    print pData[cSection]
    #viss[cSection] = DM.visualize_cluster(pData[cSection])

    tuples = []
    for cBoundary in range(len(cSupporters)):        
        total = 0
        truePos = 0
        trueNeg = 0
        
        scores = []
        
        cSample = (cSection, cBoundary)
        pot = []
        for s in cSupporters[cBoundary]:
            total += 1
            hs = hists[cSection][s]
            hs = np.array(hs).astype(float)
            #hs = scaler.transform(hs)
            #print clf.decision_function(hs)
            #if clf.predict(hs)[0] > 0:
            #if cBoundary == 0:
                 #print clf.decision_function(hs)
            
            scores.append(clf.decision_function(hs)[0])
            if clf.decision_function(hs) >= 1:
                truePos += 1
                pot.append(s)

        value = truePos * 100.0 / total
        #if value < 70:
         #   continue
        tuples.append((value, cBoundary))
        
        scores = np.array(scores)
        scores26.append((np.mean(scores), cBoundary))
        if cSection == 26:
            pos26 += pot
        #print cSample, ": Using classifier results:", truePos * 100.0 / total    

    print "Section ", cSection, ": ", sorted(tuples, reverse=True)
    print "============================================"
    scores26 = sorted(scores26, reverse=True)
    for i in range(len(scores26)):
        if scores26[i][0] > 0:
            print "Boudnary: ", scores26[i][1], ", Score: ", scores26[i][0]


set([928, 609, 1031, 777, 971, 1261, 1198, 1393, 1262, 1110])
Section  13 :  [(100.0, 27), (100.0, 26), (100.0, 25), (100.0, 24), (100.0, 22), (100.0, 21), (100.0, 20), (100.0, 19), (100.0, 18), (100.0, 17), (100.0, 13), (100.0, 12), (100.0, 11), (100.0, 10), (100.0, 9), (100.0, 8), (100.0, 6), (100.0, 3), (100.0, 2), (100.0, 0), (96.29629629629629, 7), (95.23809523809524, 4), (94.8051948051948, 5), (92.85714285714286, 16), (92.63157894736842, 1), (88.88888888888889, 14), (86.36363636363636, 15), (75.0, 23)]
============================================
Boudnary:  18 , Score:  2.98967511061
Boudnary:  0 , Score:  2.98967257567
Boudnary:  20 , Score:  2.98961960329
Boudnary:  27 , Score:  2.9896160691
Boudnary:  24 , Score:  2.98958281124
Boudnary:  12 , Score:  2.98956912381
Boudnary:  9 , Score:  2.98867844335
Boudnary:  11 , Score:  2.98821935514
Boudnary:  10 , Score:  2.98698684552
Boudnary:  19 , Score:  2.98645101163
Boudnary:  25 , Score:  2.98134301443
Boudnary:  13 , Score:  2.91943257015
Boudnary:  21 , Score:  2.91042767259
Boudnary:  22 , Score:  2.76096133378
Boudnary:  2 , Score:  2.699459646
Boudnary:  26 , Score:  2.68627322081
Boudnary:  7 , Score:  2.57354321476
Boudnary:  6 , Score:  2.46847858529
Boudnary:  3 , Score:  2.41831042995
Boudnary:  1 , Score:  2.40128135945
Boudnary:  8 , Score:  2.16388494621
Boudnary:  17 , Score:  2.15594789383
Boudnary:  5 , Score:  1.92193726236
Boudnary:  4 , Score:  1.86341337237
Boudnary:  14 , Score:  1.85531120809
Boudnary:  15 , Score:  1.8017815085
Boudnary:  16 , Score:  1.78640270193
Boudnary:  23 , Score:  1.50627263662
set([1056, 964, 997, 681, 878, 690, 821, 965])
Section  14 :  [(100.0, 29), (100.0, 28), (100.0, 27), (100.0, 26), (100.0, 25), (100.0, 24), (100.0, 21), (100.0, 19), (100.0, 17), (100.0, 4), (100.0, 1), (100.0, 0), (99.43820224719101, 30), (98.83040935672514, 20), (97.77777777777777, 5), (92.10526315789474, 2), (88.04347826086956, 10), (80.0, 6), (78.57142857142857, 16), (74.53416149068323, 23), (61.18421052631579, 3), (42.028985507246375, 22), (27.77777777777778, 18), (27.272727272727273, 13), (26.027397260273972, 11), (25.925925925925927, 14), (25.0, 15), (15.053763440860216, 8), (9.090909090909092, 12), (8.0, 7), (4.545454545454546, 31), (0.0, 9)]
============================================
Boudnary:  28 , Score:  2.9896721614
Boudnary:  26 , Score:  2.98906494129
Boudnary:  25 , Score:  2.98766763212
Boudnary:  29 , Score:  2.98741449344
Boudnary:  24 , Score:  2.98544039722
Boudnary:  27 , Score:  2.97664016235
Boudnary:  1 , Score:  2.97085565676
Boudnary:  19 , Score:  2.94586715771
Boudnary:  0 , Score:  2.94448216642
Boudnary:  4 , Score:  2.88021469211
Boudnary:  17 , Score:  2.88020666378
Boudnary:  21 , Score:  2.83999836681
Boudnary:  20 , Score:  2.81995774922
Boudnary:  30 , Score:  2.80248772437
Boudnary:  5 , Score:  2.60421461258
Boudnary:  10 , Score:  2.47579250814
Boudnary:  2 , Score:  2.36597778263
Boudnary:  16 , Score:  1.62909488992
Boudnary:  6 , Score:  1.31516226297
Boudnary:  23 , Score:  0.975997748276
Boudnary:  3 , Score:  0.952611170328

In [73]:
DM = DataManager(generate_hierarchy = False, stack = 'RS141', resol = 'x5', section = 26)
cSupporters = DM.load_pipeline_result('nonoverlappingSupporters', 'pkl')

display(DM.visualize_cluster(pos26))


Out[73]:

In [25]:
DM = DataManager(generate_hierarchy = False, stack = 'RS141', resol = 'x5', section = 26)
matchings = DM.load_pipeline_result('DBoundaries26With27', 'npy')

In [26]:
print matchings[np.argsort(matchings[:, 28]), 28] 
print np.argsort(matchings[:, 28])


[ 0.70596259  0.78489858  0.81210731  0.83241228  0.89783793  0.97528395
  1.07130031  1.09742485  1.11068696  1.36808181  1.41883095  1.90523488
  2.78681277         inf         inf         inf         inf         inf
         inf         inf         inf         inf         inf         inf
         inf         inf         inf         inf         inf         inf
         inf         inf]
[ 3 21 16 18 17 19  0  5  4  2  1 29 28 27 25 24 23 22 20 26 15 14 13 12 11
 10  9  8  7  6 30 31]

In [ ]:


In [27]:
DM = DataManager(generate_hierarchy = False, stack = 'RS141', resol = 'x5', section = 2)
matchings = DM.load_pipeline_result('DBoundaries2With4', 'npy')

print matchings[np.argsort(matchings[:, 8]), 8] 
print np.argsort(matchings[:, 8])


[ 0.66467065  0.66958352  0.8229889   0.85319059  0.87260952  0.88394716
  0.95227919  0.96957002  0.97706937  0.98381796  1.01106212  1.05428913
  1.05961905  1.0682539   1.08313887  1.22246128  1.56025875  1.6679087
  2.01896999  2.2753929          inf         inf         inf         inf
         inf         inf         inf         inf         inf         inf]
[24 12 29 23 10 14 21 27  4 28  1 25 20 22  9 13  3  8  2 16  6 26 17 18  5
 15 11  7 19  0]

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:
groups = [(tripletsPath + f) for f in os.listdir(tripletsPath)]

predXs = [0] * 40
predYs = [0] * 40
predResults = [0] * 40
print groups

In [195]:
for group in groups:
    # Blue is lower than Red
    groupN = ""
    groupBR = [set(), set()]
    
    with open(group) as f:
        for line in f:               
            splitted = line.split()
            
            if len(splitted) < 6:
                continue
                
                
            if groupN == "":
                groupN = imgsPath + splitted[4].split("/")[2]
                print "N: ", groupN
                groupO = ([f for f in os.listdir(groupN) if "img_ref_" in f])
                print "O: ", groupO
                groupY =([fn.split("_")[2] for fn in groupO])
                print "Y: ", groupY
                lower = int(groupO[0].split("_")[3])
                upper = int(groupO[1].split("_")[3])
                 
                if groupY[0] == "26":
                    br = 0
                else:
                    br = 1
                print "Br: ", br
            
            if splitted[5] == "blue":
                if ("ref_" + groupY[0]) in splitted[4]:
                    groupBR[br].add(int(splitted[4].split("_")[3]))
            elif splitted[5] == "red":
                if ("ref_" + groupY[1]) in splitted[4]:
                    groupBR[1-br].add(int(splitted[4].split("_")[3]))
            elif splitted[5] == "uncheck":
                if splitted[6] == "blue" and ("ref_" + groupY[0]) in splitted[4]:
                    groupBR[br].remove(int(splitted[4].split("_")[3]))
                elif splitted[6] == "red" and ("ref_" + groupY[1]) in splitted[4]:
                    groupBR[1-br].remove(int(splitted[4].split("_")[3]))

    # =============================
    y0 = int(groupY[0])
    y1 = int(groupY[1])
    # =============================            
        
    if predXs[br] == 0:
        predXs[br] = []
        predYs[br] = []
        
    predXs[br].append(hists[y0][lower]);
    predYs[br].append(1.0);

    predXs[br].append(hists[y1][upper]);
    predYs[br].append(-1.0);
    
    for b1 in groupBR[br]:
        predXs[br].append(hists[y0][b1])
        predYs[br].append(1.0)    

    for r1 in groupBR[1-br]:
        predXs[br].append(hists[y1][r1])
        predYs[br].append(-1.0)


    # =============================
    if predXs[1-br] == 0:
        predXs[1-br] = []
        predYs[1-br] = []

    predXs[1-br].append(hists[y0][lower]);
    predYs[1-br].append(-1.0);

    predXs[1-br].append(hists[y1][upper]);
    predYs[1-br].append(1.0);
    
    for b1 in groupBR[br]:
        predXs[1-br].append(hists[y0][b1])
        predYs[1-br].append(-1.0)    

    for r1 in groupBR[1-br]:
        predXs[1-br].append(hists[y1][r1])
        predYs[1-br].append(1.0)


N:  /home/iizhaki/WebStem/photos/group_0
O:  ['img_ref_26_2100_703_1535_64.png', 'img_ref_25_3154_1483_1735_85.png']
Y:  ['26', '25']
Br:  0
N:  /home/iizhaki/WebStem/photos/group_1
O:  ['img_ref_27_2631_1298_1821_64.png', 'img_ref_26_2100_703_1535_64.png']
Y:  ['27', '26']
Br:  1

In [ ]:


In [ ]:


In [203]:
for i in range(len(predYs)):
    X = predXs[i]
    y = predYs[i]
    
    if X == 0:
        continue
    
    clf = svm.SVC()
    clf.fit(X, y) 
    
    predResults[i] = clf

In [272]:
for i in range(len(predYs)):
    if predResults[i] != 0:
        print i, predResults[i].support_


0 [30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 15 16 17 18 19 20 21 22 23 24
 25 26 27 28 29]
1 [15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 40 41 42 43 44]

In [273]:
# 0 => Corresponds to 26
# 1 => Corresponds to others

In [274]:
import sys
sys.path.append('/home/yuncong/Brain/pipeline_scripts')
from utilities2014 import *

In [275]:
os.environ['GORDON_DATA_DIR'] = '/home/yuncong/project/DavidData2014tif/'
os.environ['GORDON_REPO_DIR'] = '/home/yuncong/Brain'
os.environ['GORDON_RESULT_DIR'] = '/home/yuncong/project/DavidData2014results/'
os.environ['GORDON_LABELING_DIR'] = '/home/yuncong/project/DavidData2014labelings/'

In [288]:
for smpl1 in [(25, 22), (27, 28)]:
    sec1 = smpl1[0]
    i = smpl1[1]
    
    total = 0
    right = 0
    wrong = 0
    
    dm_prev = DataManager(generate_hierarchy=False, stack='RS141', resol='x5', section=sec1)
    supporters_prev = dm_prev.load_pipeline_result('nonoverlappingSupporters', 'pkl')
    
    for s in supporters_prev[i]:
        total += 1
        if predResults[0].predict(hists[sec1][s])[0] < 0:
            right += 1
        
        if predResults[1].predict(hists[sec1][s])[0] > 0:
            wrong += 1
            
    print sec1, ": Using classifier '26' results", right, "out of", total
    print sec1, ": Using classifier 'other' results", wrong, "out of", total
        


for smpl2 in [(26, 3)]:
    sec2 = smpl2[0]
    j = smpl2[1]
    
    total = 0
    right = 0
    wrong = 0
        
    dm_next = DataManager(generate_hierarchy=False, stack='RS141', resol='x5', section=sec2)
    supporters_next = dm_next.load_pipeline_result('nonoverlappingSupporters', 'pkl')
    
    for s in supporters_next[j]:
        total += 1
        if predResults[1].predict(hists[sec2][s])[0] < 0:
            right += 1
        
        if predResults[0].predict(hists[sec2][s])[0] > 0:
            wrong += 1
            
    print sec2, ": Using classifier 'other' results", right, "out of", total
    print sec2, ": Using classifier '26' results", wrong, "out of", total


loaded /home/yuncong/project/DavidData2014results/RS141/0025/RS141_x5_0025_gabor-blueNisslWide-segm-blueNisslRegular-vq-blueNissl_nonoverlappingSupporters.pkl
25 : Using classifier '26' results 0 out of 39
25 : Using classifier 'other' results 0 out of 39
loaded /home/yuncong/project/DavidData2014results/RS141/0027/RS141_x5_0027_gabor-blueNisslWide-segm-blueNisslRegular-vq-blueNissl_nonoverlappingSupporters.pkl
27 : Using classifier '26' results 0 out of 42
27 : Using classifier 'other' results 0 out of 42
loaded /home/yuncong/project/DavidData2014results/RS141/0026/RS141_x5_0026_gabor-blueNisslWide-segm-blueNisslRegular-vq-blueNissl_nonoverlappingSupporters.pkl
26 : Using classifier 'other' results 34 out of 34
26 : Using classifier '26' results 34 out of 34

In [289]:
sec = 25
dm_next = DataManager(generate_hierarchy=False, stack='RS141', resol='x5', section=sec)
supporters_next = dm_next.load_pipeline_result('nonoverlappingSupporters', 'pkl')

for j in range(len(supporters_next)):
    
    total = 0
    right = 0
    wrong = 0
        
    for s in supporters_next[j]:
        total += 1
        if predResults[1].predict(hists[sec][s])[0] < 0:
            right += 1

        if predResults[0].predict(hists[sec][s])[0] > 0:
            wrong += 1

    print "(", sec, ", ", j, "): Using classifier 'other' results: ", (right * 100.0 / total)
    #print "(26, ", j, "): Using classifier '26' results", wrong, "out of", total


loaded /home/yuncong/project/DavidData2014results/RS141/0025/RS141_x5_0025_gabor-blueNisslWide-segm-blueNisslRegular-vq-blueNissl_nonoverlappingSupporters.pkl
( 25 ,  0 ): Using classifier 'other' results:  100.0
( 25 ,  1 ): Using classifier 'other' results:  100.0
( 25 ,  2 ): Using classifier 'other' results:  100.0
( 25 ,  3 ): Using classifier 'other' results:  100.0
( 25 ,  4 ): Using classifier 'other' results:  100.0
( 25 ,  5 ): Using classifier 'other' results:  100.0
( 25 ,  6 ): Using classifier 'other' results:  100.0
( 25 ,  7 ): Using classifier 'other' results:  100.0
( 25 ,  8 ): Using classifier 'other' results:  100.0
( 25 ,  9 ): Using classifier 'other' results:  100.0
( 25 ,  10 ): Using classifier 'other' results:  100.0
( 25 ,  11 ): Using classifier 'other' results:  100.0
( 25 ,  12 ): Using classifier 'other' results:  100.0
( 25 ,  13 ): Using classifier 'other' results:  100.0
( 25 ,  14 ): Using classifier 'other' results:  100.0
( 25 ,  15 ): Using classifier 'other' results:  100.0
( 25 ,  16 ): Using classifier 'other' results:  100.0
( 25 ,  17 ): Using classifier 'other' results:  100.0
( 25 ,  18 ): Using classifier 'other' results:  100.0
( 25 ,  19 ): Using classifier 'other' results:  100.0
( 25 ,  20 ): Using classifier 'other' results:  100.0
( 25 ,  21 ): Using classifier 'other' results:  100.0
( 25 ,  22 ): Using classifier 'other' results:  100.0
( 25 ,  23 ): Using classifier 'other' results:  100.0
( 25 ,  24 ): Using classifier 'other' results:  100.0
( 25 ,  25 ): Using classifier 'other' results:  100.0
( 25 ,  26 ): Using classifier 'other' results:  100.0
( 25 ,  27 ): Using classifier 'other' results:  100.0
( 25 ,  28 ): Using classifier 'other' results:  100.0
( 25 ,  29 ): Using classifier 'other' results:  100.0

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [122]:
print X[0], X[1], y[0], y[1]


[0, 6, 0, 0, 0, 588, 60, 306, 4, 0, 242, 0, 17, 1429] [0, 0, 0, 0, 2063, 317, 39, 128, 0, 0, 68, 0, 0, 247] 1.0 0.0

In [123]:
prc = Perceptron()
prc.fit(X, y)

clf = svm.SVC()
clf.fit(X, y) 

thetax, _, _, _ = np.linalg.lstsq(X, y)

In [142]:
allImgs = [f for f in os.listdir(groupN) if "ref_" in f and "img" not in f]

total = 0
right = 0

def predict(data, theta):
    theta = np.array(theta)
    prediction = [np.dot(theta, d)  for d in data]
    return prediction

#ptrain = predict(X_train, thetax)

for b1 in groupBR[0]:
    if int(clf.predict(hists[g0][b1])) == 1:
        right += 1
    total += 1
    
print right * 100.0 / total

# ==============================

total = 0
right = 0

for r1 in groupBR[1]:
    if int(clf.predict(hists[g1][r1])) == 0:
           right += 1
    total += 1

print right * 100.0 / total

# ==============================

total = 0
right = 0
           
for img in allImgs:
    cords = img.split("_")[1: 3]
    if cords[0] == "ref":
        continue
        
    a = int(cords[0])
    b = int(cords[1])
        
    # print cords[0], prc.predict(hists[a][b])[0]
    #if int(cords[0]) ==  int(2 - prc.predict(hists[a][b])[0]):
    res =  np.dot(thetax, np.array(hists[a][b]))
    #if int(cords[0]) ==  int(round(2 - res)):
    if int(cords[0]) ==  int(round(2 - clf.predict(hists[a][b])[0])):
        right += 1
    
    total += 1
    
print right * 100.0 / total


100.0
100.0
81.25

In [ ]:


In [ ]:


In [110]:
for group in groups:
    # Blue is lower than Red
    groupN = ""
    groupBR = [set(), set()]
    lower = ""
    upper = ""
    
    with open(group) as f:
        for line in f:               
            splitted = line.split()
            #print splitted
            #break
            
            if len(splitted) < 6:
                continue
            
            if groupN == "":
                groupN = imgsPath + splitted[4].split("/")[2]
                groupY = ([f.split("_")[1] for f in os.listdir(groupN) if "org_" in f])
                #print sorted([f.split("_")[1] for f in os.listdir(groupN) if "org_" in f])
                #print [f.split("_")[1] for f in os.listdir(groupN) if "org_" in f]
                #groupY = ["0", "1"]
                groupO = ([f for f in os.listdir(groupN) if "img_ref_" in f])
                #groupO = [f for f in os.listdir(groupN) if "img_ref_" in f]
                print groupY, groupO
                if ("img_ref_" + groupY[0]) in groupO[0]:
                    lower = int(groupO[0].split("_")[3])
                    upper = int(groupO[1].split("_")[3])
                else:
                    lower = int(groupO[1].split("_")[3])
                    upper = int(groupO[0].split("_")[3])
                print "Group:", groupN, groupY
                        
            
            if splitted[5] == "blue":
                if ("ref_" + groupY[0]) in splitted[4]:
                    groupBR[0].add(int(splitted[4].split("_")[3]))
            elif splitted[5] == "red":
                if ("ref_" + groupY[1]) in splitted[4]:
                    groupBR[1].add(int(splitted[4].split("_")[3]))
            elif splitted[5] == "uncheck":
                if splitted[6] == "blue" and ("ref_" + groupY[0]) in splitted[4]:
                    groupBR[0].remove(int(splitted[4].split("_")[3]))
                elif splitted[6] == "red" and ("ref_" + groupY[1]) in splitted[4]:
                    groupBR[1].remove(int(splitted[4].split("_")[3]))
                    
    print groupBR[0]
    print groupBR[1]
    print lower, upper
    
    
    
    i = 0
    X = []
    y = []

    if "26" in groupO[0]:
        g0 = 0
        g1 = 1
        k0 = int(groupY[0])
        k1 = int(groupY[1])
    else:
        g0 = 1
        g1 = 0
        k0 = int(groupY[1])
        k1 = int(groupY[0])
        
    #g0 = int(groupY[0])
    #g1 = int(groupY[1])
    lowHist = hists[k0][lower]
    highHist = hists[k1][upper]
    lenHist = len(lowHist)

    # =============================
    if predXs[g0] == 0:
        predXs[g0] = []
        predYs[g0] = []
    
    predXs[g0].append(lowHist);
    predYs[g0].append(1.0);

    predXs[g0].append(highHist);
    predYs[g0].append(-1.0);

    for b1 in groupBR[0]:
        predXs[g0].append(hists[k0][b1])
        predYs[g0].append(1.0)    

    for r1 in groupBR[1]:
        predXs[g0].append(hists[k1][r1])
        predYs[g0].append(-1.0)


    # =============================
    if predXs[g1] == 0:
        predXs[g1] = []
        predYs[g1] = []
    
    predXs[g1].append(lowHist);
    predYs[g1].append(-1.0);

    predXs[g1].append(highHist);
    predYs[g1].append(1.0);

    for b1 in groupBR[0]:
        predXs[g1].append(hists[k0][b1])
        predYs[g1].append(-1.0)    

    for r1 in groupBR[1]:
        predXs[g1].append(hists[k1][r1])
        predYs[g1].append(1.0)


['26', '28'] ['img_ref_28_3456_1224_2060_60.png', 'img_ref_26_2848_388_1898_53.png']
Group: /home/iizhaki/WebStem/photos/group_2 ['26', '28']
set([2323, 2223])
set([3729, 2685, 2894])
2848 3456
['26', '25'] ['img_ref_26_2100_703_1535_64.png', 'img_ref_25_3154_1483_1735_85.png']
Group: /home/iizhaki/WebStem/photos/group_0 ['26', '25']
set([2539, 2323, 2219, 2540, 2099, 2613, 2071])
set([3360, 3267, 3244, 3083, 3148, 3152, 3179, 3253, 3320, 3258, 3068, 3157])
2100 3154
['26', '27'] ['img_ref_27_2631_1298_1821_64.png', 'img_ref_26_2100_703_1535_64.png']
Group: /home/iizhaki/WebStem/photos/group_1 ['26', '27']
set([2223])
set([])
2100 2631

In [ ]:


In [ ]: