In [1]:
%matplotlib inline
#%matplotlib
In [110]:
import matplotlib.pyplot as plt
from matplotlib import image as mpimg
from scipy import sqrt, pi, arctan2, cos, sin, ndimage, fftpack, stats
from skimage import exposure, measure, feature
from pandas import read_csv, DataFrame
from pandas import concat as pd_concat
from PIL import Image
import cStringIO
import urllib2
from numpy.random import rand
from numpy import ones, zeros, concatenate
from sklearn.ensemble import ExtraTreesClassifier
from sklearn import svm
In [46]:
feat_df = pd.read_csv("tmp/HogFeatures.csv")
full_df = pd.read_csv("machine_learn/HOG/csv_features/hog_features_9_NewTraining_Faces_everyones.csv")
In [142]:
def save_ImageOfHog(grey_img,ImageOfHog,rgb_img):
f,ax=plt.subplots(1,3,figsize=(20,20))
ax[0].imshow(rgb_img)
ax[1].imshow(grey_img,cmap='gray')
ax[2].imshow(ImageOfHog,cmap='gray',interpolation='nearest')
titles=["RGB Image","Greyscale input for HOG","Image of Histogram Oriented Gradients (HOG)"]
for cnt,axis in enumerate(ax):
axis.set_title(titles[cnt],fontdict={'fontsize':20.})
[label.set_visible(False) for label in axis.get_xticklabels()]
[label.set_visible(False) for label in axis.get_yticklabels()]
plt.savefig("tmp/image_of_hog.png",dpi=200)
def CalcHog_FeaturesAndImage_ForOneImage(grey_img,image_name,rgb_img):
feat = zeros((1,900)) #People_All_9.csv Food_All_9.csv
#get hog features
blocks = feature.hog(grey_img, orientations=9, pixels_per_cell=(100,100), cells_per_block=(5,5), visualise=False, normalise=True) #People_All_9.csv Food_All_9.csv
#slightly diff params for better hog visualization
junk_block,ImageOfHog=feature.hog(grey_img, pixels_per_cell=(10,10), cells_per_block=(30,30),visualise=True,normalise=True)
if(len(blocks) == 900): #People_All_9.csv Food_All_9.csv
feat[0] = blocks
name_df=DataFrame()
name_df["image_name"]= image_name
feat_df= DataFrame(feat)
final_df=pd_concat([name_df,feat_df],axis=1)
final_df.to_csv("tmp/HogFeatures.csv")
save_ImageOfHog(grey_img,ImageOfHog,rgb_img)
def Hog_predict_UploadImage(grey_img,image_name,rgb_img):
CalcHog_FeaturesAndImage_ForOneImage(grey_img,image_name,rgb_img)
feat_df= read_csv("tmp/HogFeatures.csv")
feat_vals= feat_df.ix[:,2:].values
root= "machine_learn/HOG/csv_features/"
Food_df = read_csv(root+"hog_features_9_NewTraining_Food_everyones.csv")
People_df = read_csv(root+"hog_features_9_NewTraining_Faces_everyones.csv")
cTrainF = rand(len(Food_df)) > .5
cTestF = ~cTrainF
cTrainP = rand(len(People_df)) > .5
cTestP = ~cTrainP
TrainX_df = pd_concat([People_df[cTrainP], Food_df[cTrainF]],axis=0)
TrainX= TrainX_df.ix[:,2:].values
TrainY = concatenate([ones(len(People_df[cTrainP])), zeros(len(Food_df[cTrainF]))])
ET_classifier = ExtraTreesClassifier(n_estimators=50, max_depth=None, min_samples_split=1, random_state=0)
ET_classifier.fit(TrainX,TrainY)
ET_prediction = ET_classifier.predict(feat_vals)
LinSVC_classifier = svm.LinearSVC()
LinSVC_classifier.fit(TrainX,TrainY)
LinSVC_prediction = LinSVC_classifier.predict(feat_vals)
return ET_prediction, LinSVC_prediction
In [363]:
root="machine_learn/blob_hog_predict_common_url_set/"
fin=open(root+'FacesAndLimbs_shuffled_blob_features.pickle',"r")
feat_df= pickle.load(fin)
fin.close()
fin=open(root+'FacesAndLimbs_shuffled_url_answer.pickle',"r")
url_df= pickle.load(fin)
fin.close()
In [399]:
root="machine_learn/blob_hog_predict_common_url_set/"
f_faces=root+"FacesAndLimbs_shuffled_hog_features.pickle"
fin=open(f_faces)
faces_df= pickle.load(fin)
faces_df.shape
Out[399]:
In [443]:
root="machine_learn/blob_hog_predict_common_url_set/"
f_hog=root+"FacesAndLimbs_shuffled_hog_predict.pickle"
f_blob=root+"FacesAndLimbs_shuffled_blob_predict.pickle"
fin=open(f_hog,"r")
hog_df= pickle.load(fin)
fin.close()
fin=open(f_blob,"r")
blob_df= pickle.load(fin)
fin.close()
In [444]:
a=set(blob_df.url.values.flatten())
b=set(hog_df.url.values.flatten())
c=a.intersection(b)
common_urls=np.array(list(c))
In [459]:
hog_df.head()
Out[459]:
In [462]:
fin=open(root+'NoLims_shuffled_blob_stats.pickle')
blob_stats=pickle.load(fin)
fin.close()
In [466]:
from random import randrange
print randrange(10)
In [442]:
np.array(list(c))[0]
Out[442]:
In [394]:
len(c),url_df.URL.values.size,hog_df.URL.values.size
Out[394]:
In [48]:
#testing
# import matplotlib.image as mpimg
# file='uploads/engage_1ps.jpg'
# img= mpimg.imread(file)
print img.shape
plt.imshow(img)
Out[48]:
In [ ]:
In [48]:
In [146]:
# file="machine_learn/training_image_urls/NewTraining_Food_everyones.txt"
# urls=np.loadtxt(file,dtype="str")
# url=urls[11]
# read= urllib2.urlopen(url).read()
# obj = Image.open( cStringIO.StringIO(read) )
# rgb_img= np.array(obj)
# # grey_img = np.array(obj.convert('L'))
# grey_img = np.array(.convert('L'))
file= 'uploads/engage_1ps.jpg'
# import matplotlib.image as mpimg
# image= mpimg.imread(file)
obj = Image.open( file )
rgb_img= np.array(obj)
grey_img = np.array(obj.convert('L'))
(et,svc)=Hog_predict_UploadImage(grey_img,file,rgb_img)
In [150]:
def interpret_int_predict(int_predict):
food_code=0
people_code=1
if int_predict == food_code: return "Contains: Food"
else: return "Contains: People"
ans=interpret_int_predict(et[0].astype('int'))
print ans
In [153]:
a=1.2344423
s= "%.1f" % a
print s
In [197]:
from numpy.random import rand
from numpy import ones, zeros, concatenate
import numpy as np
from pandas import read_csv, DataFrame, concat
from sklearn.ensemble import RandomForestClassifier
In [293]:
class FacesAndLimbsTrainingSet():
def __init__(self,Food_df,Faces_df,SkinNoFaces_df):
self.Food= Food_df.ix[:251,:].copy()
self.People= self.Food.copy()
for i in np.arange(0,134):
self.People.ix[i,:]= Faces_df.ix[i,:].copy()
cnt=0
for i in np.arange(134,250):
self.People.ix[i,:]= SkinNoFaces_df.ix[cnt,:].copy()
cnt+=1
class NoLimbsTrainingSet():
def __init__(self,Food_df,Faces_df):
self.Food= Food_df.ix[:251,:].copy()
self.People= Faces_df.ix[:251,:].copy()
class NoFacesTrainingSet():
def __init__(self,Food_df,SkinNoFaces_df):
self.Food= Food_df.ix[:117,:].copy()
self.People= SkinNoFaces_df.ix[:117,:].copy()
class Team_or_Kay_Features():
def __init__(self,Food_df,Faces_df,SkinNoFaces_df):
self.FacesAndLimbs= FacesAndLimbsTrainingSet(Food_df,Faces_df,SkinNoFaces_df)
self.NoLims= NoLimbsTrainingSet(Food_df,Faces_df)
self.NoFaces= NoFacesTrainingSet(Food_df,SkinNoFaces_df)
class AddTeamCols():
def __init__(self, Food_KayF,Faces_KayF,SkinNoFaces_KayF,
Food_TeamF,Faces_TeamF,SkinNoFaces_TeamF):
self.Food= Food_KayF.copy()
cols= Food_TeamF.columns[2:12]
for col in cols:
self.Food[col]= Food_TeamF[col]
self.Faces= Faces_KayF.copy()
cols= Faces_TeamF.columns[2:12]
for col in cols:
self.Faces[col]= Faces_TeamF[col]
self.SkinNoFaces= SkinNoFaces_KayF.copy()
cols= SkinNoFaces_TeamF.columns[2:12]
for col in cols:
self.SkinNoFaces[col]= SkinNoFaces_TeamF[col]
Food_KayF = read_csv('machine_learn/Blob/csv_features/NewTraining_Food_everyones_KFeat_Toddmap.csv')
Faces_KayF = read_csv('machine_learn/Blob/csv_features/NewTraining_Faces_everyones_KFeat_Toddmap.csv')
SkinNoFaces_KayF = read_csv('machine_learn/Blob/csv_features/NewTraining_SkinNoFaces_everyones_KFeat_Toddmap.csv')
Food_TeamF = read_csv('machine_learn/Blob/csv_features/NewTraining_Food_everyones_TeamFeat_Toddmap.csv')
Faces_TeamF = read_csv('machine_learn/Blob/csv_features/NewTraining_Faces_everyones_TeamFeat_Toddmap.csv')
SkinNoFaces_TeamF = read_csv('machine_learn/Blob/csv_features/NewTraining_SkinNoFaces_everyones_TeamFeat_Toddmap.csv')
#team feature numbers for different definitions of Food,People
team= Team_or_Kay_Features(Food_TeamF,Faces_TeamF,SkinNoFaces_TeamF)
#kay feature numbers for different definitions of Food,People
kay= Team_or_Kay_Features(Food_KayF,Faces_KayF,SkinNoFaces_KayF)
#kay feature numbers + team feature number for skin maps for different definitions of Food,People
extend= AddTeamCols(Food_KayF,Faces_KayF,SkinNoFaces_KayF,
Food_TeamF,Faces_TeamF,SkinNoFaces_TeamF)
kay_extend= Team_or_Kay_Features(extend.Food,extend.Faces,extend.SkinNoFaces)
##
#make training and test sets
Food_all = kay_extend.NoLims.Food
People_all= kay_extend.NoLims.People
In [294]:
kay_extend.FacesAndLimbs.Food.shape,kay_extend.FacesAndLimbs.People.shape
Out[294]:
In [205]:
pd.DataFrame?
In [296]:
ans_people=pd.DataFrame(np.ones(250),columns=["answer"])
ans_food= pd.DataFrame(np.zeros(250),columns=["answer"])
FacesAndLimbs_food= pd.concat([ans_food,kay_extend.FacesAndLimbs.Food.ix[:,1:]],axis=1)
FacesAndLimbs_people= pd.concat([ans_people,kay_extend.FacesAndLimbs.People.ix[:,1:]],axis=1)
FacesAndLimbs_food.shape, FacesAndLimbs_people.shape
Out[296]:
In [298]:
FacesAndLimbs= pd.concat([FacesAndLimbs_food,FacesAndLimbs_people],axis=0)
inds=np.arange(0,FacesAndLimbs.shape[0])
FacesAndLimbs.index= inds
FacesAndLimbs.shape
Out[298]:
In [299]:
def shuffle_DataFrame_rows(df):
from random import shuffle
inds=np.arange(0,df.shape[0])
shf_inds= inds.copy()
shuffle(shf_inds)
shf_df= df.ix[shf_inds,:]
shf_df.index= inds
return shf_df
shf_FacesAndLimbs= shuffle_DataFrame_rows(FacesAndLimbs)
In [300]:
shf_FacesAndLimbs_url_answer= shf_FacesAndLimbs.ix[:,0:2]
shf_FacesAndLimbs_blob_features= shf_FacesAndLimbs.ix[:,2:]
In [304]:
name="FacesAndLimbs_shuffled_url_answer.pickle"
fout = open(name, 'w')
pickle.dump(shf_FacesAndLimbs_url_answer, fout)
fout.close()
In [176]:
import pickle
In [190]:
fin=open('machine_learn/blob_hog_predict_common_url_set/FacesAndLimbs_Food_urls.pickle',"r")
test=pickle.load(fin)
fin.close()
In [308]:
fin=open('machine_learn/blob_hog_predict_common_url_set/FacesAndLimbs_shuffled_url_answer.pickle',"r")
df= pickle.load(fin)
fin.close()
In [310]:
df.ix[[0,1,3],:].head()
Out[310]:
In [400]:
root="machine_learn/blob_hog_predict_common_url_set/"
fin=open(root+'FacesAndLimbs_shuffled_hog_features.pickle',"r")
df= pickle.load(fin)
fin.close()
In [406]:
fin=open(root+'NoLims_shuffled_url_answer.pickle',"r")
ans_url_df= pickle.load(fin)
fin.close()
ans_url_df.head()
Out[406]:
In [356]:
root= "machine_learn/blob_hog_predict_common_url_set/"
f_url= root+"NoLims_shuffled_url_answer.pickle"
f_feat= root+"NoLims_shuffled_blob_features.pickle"
fin=open(f_url,"r")
url_df= pickle.load(fin)
fin.close()
fin=open(f_feat,"r")
feat_df= pickle.load(fin)
fin.close()
In [407]:
fin=open(root+'FacesAndLimbs_shuffled_hog_features.pickle',"r")
feat_df= pickle.load(fin)
fin.close()
In [419]:
feat_df.head()
Out[419]:
In [421]:
feat_df.values[0:300,2:]
Out[421]:
In [351]:
url_df_2= url_df.dropna()
url_df_2.shape,url_df.shape
Out[351]:
In [342]:
bad="http://scontent-b.cdninstagram.com/hphotos-xpa1/t51.2885-15/925501_1420836204853800_1357191103_n.jpg"
ind_gd= np.where(url_df.URL.values != bad)[0]
url_gd_df= url_df.ix[ind_gd,:]
feat_gd_df= feat_df.ix[ind_gd,:]
In [343]:
np.where(url_df.URL.values == bad)[0]
Out[343]:
In [340]:
fout = open(f_feat, 'w')
pickle.dump(feat_gd_df, fout)
fout.close()
In [312]:
def get_indices_urls_that_exist(urls):
inds_exist=[]
cnt=-1
for url in df.URL.values:
cnt+=1
print "cnt= %d" % cnt
try:
read= urllib2.urlopen(url).read()
inds_exist.append(cnt)
except urllib2.URLError:
continue
return np.array(inds_exist)
root= "machine_learn/blob_hog_predict_common_url_set/"
url_1= root+"NoLims_shuffled_url_answer.pickle"
fin=open(url_1,"r")
url_df= pickle.load(fin)
fin.close()
url_1_inds= get_indices_urls_that_exist(url_df.URL.values)
In [320]:
path=root+"NoLims_shuffled_blob_features.pickle"
fin=open(path,"r")
blob_feat_df= pickle.load(fin)
fin.close()
df_exist= df.ix[url_1_inds,:]
blob_feat_df_exist= blob_feat_df.ix[url_1_inds,:]
df_exist.shape,df.shape,blob_feat_df_exist.shape,blob_feat_df.shape
Out[320]:
In [322]:
fout = open("NoLims_shuffled_url_answer.pickle", 'w')
pickle.dump(df_exist, fout)
fout.close()
In [323]:
url_2= root+"FacesAndLimbs_shuffled_url_answer.pickle"
fin=open(url_2,"r")
url_df= pickle.load(fin)
fin.close()
url_2_inds= get_indices_urls_that_exist(url_df.URL.values)
In [326]:
path=root+"FacesAndLimbs_shuffled_blob_features.pickle"
fin=open(path,"r")
blob_feat_df= pickle.load(fin)
fin.close()
url_df_exist= url_df.ix[url_2_inds,:]
blob_feat_df_exist= blob_feat_df.ix[url_2_inds,:]
url_df_exist.shape,url_df.shape,blob_feat_df_exist.shape,blob_feat_df.shape
Out[326]:
In [328]:
fout = open("FacesAndLimbs_shuffled_url_answer.pickle", 'w')
pickle.dump(url_df_exist, fout)
fout.close()
In [69]:
file="machine_learn/training_image_urls/NewTraining_Faces_everyones.txt"
urls=np.loadtxt(file,dtype="str")
url=urls[11]
read= urllib2.urlopen(url).read()
obj = Image.open( cStringIO.StringIO(read) )
img = np.array(obj.convert('L'))
print img.shape
plt.imshow(img)
Out[69]:
In [102]:
block,ImageOfHog=feature.hog(img, pixels_per_cell=(10,10), cells_per_block=(30,30),visualise=True)#,normalise=True)
In [140]:
file="machine_learn/training_image_urls/NewTraining_Food_everyones.txt"
urls=np.loadtxt(file,dtype="str")
url=urls[11]
read= urllib2.urlopen(url).read()
obj = Image.open( cStringIO.StringIO(read) )
rgb_img= np.array(obj)
grey_img = np.array(obj.convert('L'))
f,ax=plt.subplots(1,3,figsize=(20,20))
# ax= axis.flatten()
ax[0].imshow(rgb_img)
# ax[1].imshow(np.zeros((10,10)),cmap='gray_r')
ax[1].imshow(grey_img,cmap='gray')
ax[2].imshow(ImageOfHog,cmap='gray',interpolation='nearest')
titles=["RGB Image","Greyscale input for HOG","Image of Histogram Oriented Gradients (HOG)"]
for cnt,axis in enumerate(ax):
axis.set_title(titles[cnt],fontdict={'fontsize':20.})
[label.set_visible(False) for label in axis.get_xticklabels()]
[label.set_visible(False) for label in axis.get_yticklabels()]
plt.savefig("tmp/image_of_hog.png",dpi=200)
In [104]:
block,ImageOfHog=feature.hog(img, pixels_per_cell=(10,10), cells_per_block=(30,30),visualise=True,normalise=True)
plt.imshow(ImageOfHog)
Out[104]:
In [52]:
feat_df= read_csv("tmp/HogFeatures.csv")
feat_vals= feat_df.ix[:,2:].values
In [55]:
feature.hog?
In [ ]: