def main2(): clfP = joblib.load('svmmodels/test/SVMl_clfP.pkl') clfA = joblib.load('svmmodels/test/SVMl_clfA.pkl') Dt, cpt, cat = loadData('Btest') log('testing...') test2(clfP, clfA, Dt, cpt, cat) log('testing plastic') test(clfP, Dt, cpt) log('testing animals') test(clfA, Dt, cat)
def get_model(cls): classifier = RelatedSearchClassifier() classifier.model = joblib.load(os.sep.join([settings.RELATED_SEARCH_MODEL_BASE_DIR, cls.model_file]), mmap_mode='r') mc = get_cache() classifier.classification_dict = mc.get(classification_dict_cache_key) classifier.num_features = mc.get(num_features_key) return classifier
list_of_file_names.append(this_image_file_name) if os.path.isfile(this_image_file_name): file_exists[kk] = 1 # Loop over the poselets for pose_indx in range(num_poselets): bin_dir = "/home/ely/projects/faces/data/performance/binary/poselet_" + str(pose_indx) + "/" score_dir = "/home/ely/projects/faces/data/performance/score/poselet_" + str(pose_indx) + "/" cmd1 = "mkdir " + bin_dir; cmd2 = "mkdir " + score_dir resp = os.system(cmd1); resp = os.system(cmd2); cur_clf_file = clf_name_prefix + str(pose_indx) + clf_name_postfix cur_clf = joblib.load(cur_clf_file) #cur_clf_in = open(cur_clf_file,'r') #cur_clf = pickle.load(cur_clf_in) #cur_clf_in.close() cur_pose_patch = "/home/ely/projects/faces/data/poselets/seed_patches/seed_"+str(pose_indx)+".png" patch_img = imread(cur_pose_patch) patch_height, patch_width = patch_img.shape # Loop over all of the images. num_file_success = 0 num_images_to_test = 10 while num_file_success < num_images_to_test: found_file = 0 while not(found_file): test_indx = random.choice(range(len(url_list)))
def batchtrain(self, njob=1, phase=None, shared_memory='no', verbose='on'): """Batch training which is called for rought training as well as finetuning. """ t0 = time() nnodes = getattr(self, 'nnodes') dlen = getattr(self, 'dlen') dim = getattr(self, 'dim') mapsize = getattr(self, 'mapsize') ############################################# # Seting the parameters initmethod = getattr(self, 'initmethod') mn = np.min(mapsize) if mn == 1: mpd = float(nnodes * 10) / float(dlen) else: mpd = float(nnodes) / float(dlen) ms = max(mapsize[0], mapsize[1]) if mn == 1: ms = ms / 5. # Based on somtoolbox, Matlab # case 'train', sTrain.trainlen = ceil(50 * mpd); # case 'rough', sTrain.trainlen = ceil(10 * mpd); # case 'finetune', sTrain.trainlen = ceil(40 * mpd); if phase == 'rough': # training length trainlen = int(np.ceil(10 * mpd)) # radius for updating if initmethod == 'random': # trainlen = int(np.ceil(15 * mpd)) radiusin = max(1, np.ceil(ms / 2.)) radiusfin = max(1, radiusin / 8.) elif initmethod == 'pca': radiusin = max(1, np.ceil(ms / 8.)) radiusfin = max(1, radiusin / 4.) elif phase == 'finetune': # train lening length trainlen = int(np.ceil(40 * mpd)) # radius for updating if initmethod == 'random': # trainlen = int(np.ceil(50 * mpd)) radiusin = max(1, ms / 8.) # from radius fin in rough training radiusfin = max(1, radiusin / 16.) elif initmethod == 'pca': radiusin = max(1, np.ceil(ms / 8.) / 4) radiusfin = 1 # max(1, ms / 128) radius = np.linspace(radiusin, radiusfin, trainlen) ################################################## UD2 = getattr(self, 'UD2') New_Codebook_V = np.empty((nnodes, dim)) New_Codebook_V = getattr(self, 'codebook') # print 'data is in shared memory?', shared_memory if shared_memory == 'yes': data = getattr(self, 'data') Data_folder = tempfile.mkdtemp() data_name = os.path.join(Data_folder, 'data') dump(data, data_name) data = load(data_name, mmap_mode='r') else: data = getattr(self, 'data') # X2 is part of euclidean distance (x-y)^2 = x^2 +y^2 - 2xy that we use for # each data row in bmu finding. # Since it is a fixed value we can skip it during bmu finding for each data # point, but later we need it calculate quantification error. X2 = np.einsum('ij, ij->i', data, data) if verbose == 'on': print '%s training...' % phase print 'radius_ini: {}, radius_final: {}, trainlen: {}'.format( radiusin, radiusfin, trainlen) for i in range(trainlen): # In case of Guassian neighborhood H = np.exp(-1.0 * UD2 / (2.0 * radius[i]**2)).reshape(nnodes, nnodes) t1 = time() bmu = None bmu = self.para_bmu_find(data, New_Codebook_V, njb=njob) if verbose == 'on': print # Updating the codebook t2 = time() New_Codebook_V = self.update_codebook_voronoi(data, bmu, H, radius) # print 'updating nodes: ', round (time() - t2, 3) if verbose == 'on': print "epoch: {} ---> elapsed time: {}, quantization error: {}".format( i + 1, round(time() - t1, 3), np.mean(np.sqrt(bmu[1] + X2))) self.codebook = New_Codebook_V bmu[1] = np.sqrt(bmu[1] + X2) self.bmu = bmu
rel = ''' import segmenttest2 reload(segmenttest2) from segmenttest2 import * ''' CAFFE_ROOT = '../caffe/' ADATA_FOLDER = '../DATA_AWATER/' BDATA_FOLDER = '../DATA_BWATER/' MODEL_FILE = CAFFE_ROOT + 'models/bvlc_reference_caffenet/deploy.prototxt' PRETRAINED = CAFFE_ROOT + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel' IMGCLASS = [line[:-1] for line in open('imnetclasses.txt')] CLFP = joblib.load('svmmodels/allprob/clfP.pkl') CLFA = joblib.load('svmmodels/allprob/clfA.pkl') caffe.set_mode_cpu() if not 'NET' in locals(): NET = caffe.Classifier( MODEL_FILE, PRETRAINED, mean=np.load(CAFFE_ROOT + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean( 1).mean(1), channel_swap=(2, 1, 0), raw_scale=255, image_dims=(256, 256))
# Loop over the poselets for pose_indx in range(num_poselets): bin_dir = "/home/ely/projects/faces/data/performance/binary/poselet_" + str( pose_indx) + "/" score_dir = "/home/ely/projects/faces/data/performance/score/poselet_" + str( pose_indx) + "/" cmd1 = "mkdir " + bin_dir cmd2 = "mkdir " + score_dir resp = os.system(cmd1) resp = os.system(cmd2) cur_clf_file = clf_name_prefix + str(pose_indx) + clf_name_postfix cur_clf = joblib.load(cur_clf_file) #cur_clf_in = open(cur_clf_file,'r') #cur_clf = pickle.load(cur_clf_in) #cur_clf_in.close() cur_pose_patch = "/home/ely/projects/faces/data/poselets/seed_patches/seed_" + str( pose_indx) + ".png" patch_img = imread(cur_pose_patch) patch_height, patch_width = patch_img.shape # Loop over all of the images. num_file_success = 0 num_images_to_test = 10 while num_file_success < num_images_to_test: found_file = 0 while not (found_file):
from initial import * import classify as cl from scikits.learn import svm from scikits.learn.externals import joblib #import svmtest as st rel = """ import adatatest reload(adatatest) from adatatest import * """ clfP = joblib.load('svmmodels/test/SVMl_clfP.pkl') clfA = joblib.load('svmmodels/test/SVMl_clfA.pkl') def through(imagefolder,imagename): b = cl.through(imagefolder,imagename) dm = b['fc7'].data.mean(axis=0) a = clfA.predict([dm]) p = clfP.predict([dm]) return int(a[0]),int(p[0]) def test(num=100): bg = pg = ag = bf = 0 for image in ADATA_TRAIN[:num]: a,p = through(ADATA_FOLDER,image) [num,rest] = image.split('_') typeR = rest[0:2] at = int(typeR[0]) pt = int(typeR[1]) print a,at,p,pt
rel = ''' import segmenttest reload(segmenttest) from segmenttest import * ''' CAFFE_ROOT = '../caffe/' ADATA_FOLDER = '../DATA_AWATER/' BDATA_FOLDER = '../DATA_BWATER/' MODEL_FILE = CAFFE_ROOT + 'models/bvlc_reference_caffenet/deploy.prototxt' PRETRAINED = CAFFE_ROOT + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel' IMGCLASS = [line[:-1] for line in open('imnetclasses.txt')] CLFP = joblib.load('svmmodels/linearMixed/clfP.pkl') CLFA = joblib.load('svmmodels/linearMixed/clfA.pkl') caffe.set_mode_cpu() if not 'NET' in locals(): NET = caffe.Classifier( MODEL_FILE, PRETRAINED, mean=np.load(CAFFE_ROOT + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean( 1).mean(1), channel_swap=(2, 1, 0), raw_scale=255, image_dims=(256, 256))