In [144]:
root = '/data/vision/torralba/health-habits/other/enes/'

%matplotlib inline
import matplotlib.pyplot as plt

import os
import sys
import random
import json
import math
import time
import fnmatch
import os
import scipy.misc
sys.path.append( root + 'Utils/')

import pandas as pd
import numpy as np
import tensorflow as tf

from PIL import Image
from IPython.display import display
from pprint import pprint
from notebook_utils import *
from skimage import color, io

In [2]:
with open('all_paths.txt') as f:
  all_paths = [line.rstrip('\n') for line in f.readlines()]
print len(all_paths)


1281146

In [4]:
def gaussian( x, var ):
  return np.exp( -(x**2) / (2 * var**2))

In [132]:
quantized_array = np.load('pts_in_hull.npy')

In [197]:
def get_data_new(path):
  lt = time.time()
  img = io.imread(path)
  
  print "A0", (time.time() - lt)
  lt = time.time()
  
  resized_img = scipy.misc.imresize(img, (64,64))
  
  print "A1", (time.time() - lt)
  lt = time.time()
  
  img = color.rgb2lab(img)
  assert img.shape == (256,256,3)

  print "A2", (time.time() - lt)
  lt = time.time()
    
  image = img[:,:,0:1]
  img = color.rgb2lab(resized_img)
  colors = img[:,:,1:3]
  
  print "B", (time.time() - lt)
  lt = time.time()
  
  colors = np.tile( colors.reshape((64,64,1,2)), (1,1,313,1))
  
  print "C", (time.time() - lt)
  lt = time.time()
  
  big_quantized = np.tile( quantized_array, (64,64,1,1))
  
  print "D", (time.time() - lt)
  lt = time.time()
  
  aaa = colors - big_quantized
  
  print "E-1", (time.time() - lt)
  lt = time.time()
  
  aaaa = np.square(aaa)
  
  print "E0", (time.time() - lt)
  lt = time.time()
  
  distances = np.sum(aaaa, axis=3)
  
  print "E1", (time.time() - lt)
  lt = time.time()
  
  d = distances.copy()
  
  print "E2", (time.time() - lt)
  lt = time.time()
  
  distance_cap = 0
  for i in range(5):
    v = np.argmax(d, axis=2)
    d[v] = 0
    print "FAFAFA", 
#   d.sort(axis = 2)
  
  print "E3", (time.time() - lt)
  lt = time.time()
  
  low_values = (distances > np.tile( distance_cap, (1,1,313) ))#d[:,:,4:5]
  
  print "E4", (time.time() - lt)
  lt = time.time()
  
  gaussian_distances = gaussian(distances, 5)
  
  print "F1", (time.time() - lt)
  lt = time.time()
  
  gaussian_distances[low_values] = 0
  
  print "F2", (time.time() - lt)
  lt = time.time()
  
  output = gaussian_distances / np.sum(gaussian_distances, axis = 2).reshape((64,64,1))
  
  print "F3", (time.time() - lt)
  lt = time.time()
  
  return image, output

In [198]:
image,output = get_data_new( all_paths[0] )


A0 0.00635004043579
A1 0.00180983543396
A2 0.0444900989532
B 0.00251007080078
C 0.00651693344116
D 0.00313806533813
E-1 0.00921106338501
E0 0.00483512878418
E1 0.022360086441
E2 0.00134587287903
FAFAFA 282.974609375
FAFAFA 282.974609375
FAFAFA 282.974609375
FAFAFA 282.974609375
FAFAFA 282.974609375
E3 0.00817799568176
E4 0.00270986557007
F1 0.0290699005127
F2 0.00205397605896
F3 0.00380682945251
/afs/csail.mit.edu/u/k/kocabey/.virtualenvs/tensorflow/lib/python2.7/site-packages/ipykernel/__main__.py:80: RuntimeWarning: invalid value encountered in divide

In [148]:
one_hot = np.argmax( output, axis = 2 )
one_hot.shape


Out[148]:
(64, 64)

In [137]:
one_hot[0,0]


Out[137]:
139

In [138]:
colorized = np.zeros( (64, 64, 3) )
colorized[:,:,0] = scipy.misc.imresize( image.reshape((256,256)), (64,64) ) / (255. / 100)

In [139]:
for i in range(64): 
  for j in range(64):
    colorized[i,j,1:3] = quantized_array[ one_hot[i,j] ]

In [140]:
quantized_array[ one_hot[0,0] ]


Out[140]:
array([10,  0])

In [141]:
Image.fromarray( (255 * color.lab2rgb(colorized)).astype(np.uint8) )


Out[141]: