def main(nJobs = 1): path = '/local/attale00/GoodPose/extracted_alpha/grayScale64' fileNames = utils.getAllFiles(path); labs=utils.parseLabelFiles('/local/attale00/GoodPose'+'/mouth_labels','mouth',fileNames,cutoffSeq='_0.png',suffix='_face0.labels') print('-----computing Features-----') roi2 = (0,32,0,64) mouthSet = fg.dataContainer(labs) #load the mask for the mouth room pixels and dilate it eM=np.load('/home/attale00/Desktop/mouthMask.npy') m=cv2.resize(np.uint8(eM),(256,256)); strel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) dil = cv2.dilate(m,strel) m=dil>0; #get the features fg.getHogFeature(mouthSet,roi2,path=path+'/',ending=None,extraMask = None) #map the string labels to numbers (required by sklearn) #change the mapping here for different classifiers mouthSet.targetNum=map(utils.mapMouthLabels2Two,mouthSet.target) n_estimators = 100 min_split = 10 max_depth = 20 max_features = np.sqrt(len(mouthSet.data[0])) rf = classifierUtils.standardRF(max_features = max_features) rf2=classifierUtils.standardRF(max_features=max_features) score=classifierUtils.standardCrossvalidation(rf2,mouthSet) rf.fit(mouthSet.data,mouthSet.targetNum) pickle.dump(rf,open('/home/attale00/Desktop/classifiers/RandomForestMouthclassifier_12','w')) f=open('/home/attale00/Desktop/classifiers/RandomForestMouthclassifier_12.txt','w') f.write('Trained on aflw\n') f.write('Attribute: mouth' ) f.write('Features: getHogFeature(mouthSet,roi2,path=path,ending=None,extraMask = m) on 64*64 grayScale 3 direction bins \n') f.write('ROI:(0,32,0,64)\n') f.write('labels: closed, narrow: 0, open, wideOpen: 1\n') f.write('CV Score: {}\n'.format(score)) f.close()
def main(mode): path = '/local/attale00/AFLW_ALL/' path_ea = '/local/attale00/AFLW_cropped/cropped3/' # fileNames = utils.getAllFiles(path_ea); # minr = 10000; # for f in fileNames: # im = cv2.imread(path_ea+f,-1) # if im.shape[0]!=40 or im.shape[1]!=120: # print f # print im.shape # minr = minr if im.shape[0]>= minr else im.shape[0] # # print minr # labs=utils.parseLabelFiles(path+'/labels/labels','mouth_opening',fileNames,cutoffSeq='.png',suffix='_face0.labels') testSet = fg.dataContainer(labs) roi=(0,37,0,115) roi=None filters = None meanI = None #roi=(44,84,88,168) # eM=np.load('/home/attale00/Desktop/mouthMask.npy') # m=cv2.resize(np.uint8(eM),(256,256)); # strel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) # dil = cv2.dilate(m,strel) # # m=dil>0; X=fg.getAllImagesFlat(path_ea,testSet.fileNames,(40,120),roi=roi) # X=fg.getAllImagesFlat(path_ea,testSet.fileNames,(120,40),roi=roi,resizeFactor = .5) # # perform ICA if mode not in ['s','v']: ica = FastICA(n_components=100,whiten=True) ica.fit(X) meanI=np.mean(X,axis=0) X1=X-meanI data=ica.transform(X1) filters=ica.components_ elif mode in ['s','v']: W=np.load('/home/attale00/Desktop/classifiers/patches/filterMP1.npy') m=np.load('/home/attale00/Desktop/classifiers/patches/meanIMP1.npy') X1=X-m data=np.dot(X1,W.T) for i in range(len(fileNames)): testSet.data[i].extend(data[i,:]) #strel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)) fg.getHogFeature(testSet,roi,path=path_ea,ending='.png',extraMask = None,orientations = 5, cells_per_block=(3,3),pixels_per_cell=(24,8),maskFromAlpha=False) fg.getColorHistogram(testSet,(0,40,40,80),path=path_ea,ending='.png',colorspace='lab',bins=20) #fg.getImagePatchStat(testSet,path=path_ea,patchSize=(4,12)) #fg.getImagePatchStat(testSet,path='/local/attale00/AFLW_cropped/mouth_img_error/',patchSize=(4,12)) #pca # n_samples, n_features = X.shape # # mean_ = np.mean(X, axis=0) # X -= mean_ # U, S, V = linalg.svd(X) # explained_variance_ = (S ** 2) / n_samples # explained_variance_ratio_ = (explained_variance_ /explained_variance_.sum()) # K=V / S[:, np.newaxis] * np.sqrt(n_samples) # filters=K[:100] # data=np.dot(X,filters.T) testSet.targetNum=map(utils.mapMouthLabels2Two,testSet.target) rf=classifierUtils.standardRF(max_features = 23,min_split=12,max_depth=45) #rf = svm.NuSVC() #rf = linear_model.SGDClassifier(loss='perceptron', eta0=1, learning_rate='constant', penalty=None) if mode in ['s','v']: print 'Classifying with loaded classifier' _classifyWithOld(path,testSet,mode) elif mode in ['c']: print 'cross validation of data' classifierUtils.dissectedCV(rf,testSet) s=classifierUtils.standardCrossvalidation(rf,testSet) print s elif mode in ['save']: print 'saving new classifier' _saveRF(testSet,rf,filters=filters,meanI=meanI) else: print 'not doing anything'
def _cross_validate(testSet): rf=classifierUtils.standardRF(max_features = np.sqrt(len(testSet.data[0]))) print 'Scores' print classifierUtils.standardCrossvalidation(rf,testSet) print '----' return
def main(mode): path = '/local/attale00/AFLW_ALL/' path_ea = '/local/attale00/AFLW_cropped/mouth_img_error_multiPie/' allLabelFiles = utils.getAllFiles('/local/attale00/a_labels') labeledImages = [i[0:16]+'.png' for i in allLabelFiles] #labs=utils.parseLabelFiles(path+'/Multi-PIE/labels','mouth',labeledImages,cutoffSeq='.png',suffix='_face0.labels') labs=utils.parseLabelFiles('/local/attale00/a_labels','mouth',labeledImages,cutoffSeq='.png',suffix='_face0.labels') # fileNames = labeledImages; # minr = 10000; # for f in fileNames: # im = cv2.imread(path_ea+f,-1) # if im.shape[0]!=40 or im.shape[1]!=120: # print f # print im.shape # minr = minr if im.shape[0]>= minr else im.shape[0] # # print minr # # testSet = fg.dataContainer(labs) roi=(0,37,0,115) roi=None fg.getImagePatchStat(testSet,path=path_ea,patchSize=(4,12),overlap = 2) #pca # n_samples, n_features = X.shape # # mean_ = np.mean(X, axis=0) # X -= mean_ # U, S, V = linalg.svd(X) # explained_variance_ = (S ** 2) / n_samples # explained_variance_ratio_ = (explained_variance_ /explained_variance_.sum()) # K=V / S[:, np.newaxis] * np.sqrt(n_samples) # filters=K[:100] # data=np.dot(X,filters.T) testSet.targetNum=map(utils.mapMouthLabels2Two,testSet.target) rf=classifierUtils.standardRF(max_features = 27,min_split=13,max_depth=40) #rf = svm.NuSVC() #rf = linear_model.SGDClassifier(loss='perceptron', eta0=1, learning_rate='constant', penalty=None) if mode in ['s','v']: print 'Classifying with loaded classifier' classifierUtils.classifyWithOld(path,testSet,mode,clfPath = '/home/attale00/Desktop/classifiers/errorpatches/rferror') elif mode in ['c']: print 'cross validation of data' classifierUtils.dissectedCV(rf,testSet) print classifierUtils.standardCrossvalidation(rf,testSet) elif mode in ['save']: print 'saving new classifier' _saveRF(testSet,rf,filters=filters,meanI=meanI) else: print 'not doing anything'
def main(mode): path = '/local/attale00/AFLW_ALL/' path_ea = '/local/attale00/AFLW_cropped/mouth_img_error_multiPie/' allLabelFiles = utils.getAllFiles('/local/attale00/a_labels') labeledImages = [i[0:16]+'.png' for i in allLabelFiles] #labs=utils.parseLabelFiles(path+'/Multi-PIE/labels','mouth',labeledImages,cutoffSeq='.png',suffix='_face0.labels') labs=utils.parseLabelFiles('/local/attale00/a_labels','mouth',labeledImages,cutoffSeq='.png',suffix='_face0.labels') labs=dict((k,v) for (k,v) in labs.iteritems() if not v.startswith('narr')) # fileNames = labeledImages; # roi=None testSet = fg.dataContainer(labs) X=fg.getAllImagesFlat(path_ea,testSet.fileNames,(40,120),roi=roi) fgmode = 0 #fg.getImagePatchStat(testSet,path=path_ea,patchSize=(8,24),overlap = 2,mode=fgmode) roi=None orientations = 9 #fg.getHogFeature(testSet,roi,path=path_ea,ending='.png',extraMask = None,orientations = orientations, cells_per_block=(3,3),pixels_per_cell=(24,8),maskFromAlpha=False) # perform ICA if mode not in ['s','v']: ica = FastICA(n_components=100,whiten=True) ica.fit(X) meanI=np.mean(X,axis=0) X1=X-meanI data=ica.transform(X1) filters=ica.components_ elif mode in ['s','v']: W=np.load('/home/attale00/Desktop/classifiers/thesis/errorpatches/filter1.npy') m=np.load('/home/attale00/Desktop/classifiers/thesis/errorpatches/meanI1.npy') X1=X-m data=np.dot(X1,W.T) for i in range(len(testSet.fileNames)): testSet.data[i].extend(data[i,:]) testSet.targetNum=map(utils.mapMouthLabels2Two,testSet.target) rf=classifierUtils.standardRF(max_features = 27,min_split=13,max_depth=40) #rf = svm.NuSVC() #rf = linear_model.SGDClassifier(loss='perceptron', eta0=1, learning_rate='constant', penalty=None) if mode in ['s','v']: print 'Classifying with loaded classifier' #r=classifierUtils.classifyWithOld(path,testSet,mode,clfPath = '/home/attale00/Desktop/classifiers/thesis/errorpatches/mode{}'.format(fgmode)) r=classifierUtils.classifyWithOld(path,testSet,mode,clfPath='/home/attale00/Desktop/classifiers/thesis/errorpatches/errorpatch_ica') pickle.dump(r,open('errorpatch_test_ica'.format(fgmode),'w')) elif mode in ['c']: print 'cross validation of data' classifierUtils.dissectedCV(rf,testSet) print classifierUtils.standardCrossvalidation(rf,testSet) elif mode in ['save']: print 'saving new classifier' _saveRF(testSet,rf,filters=filters,meanI=meanI) else: print 'not doing anything'