def main(): salmap_gt_mapped = get_calibrated_gtsal() gtsal_path = '../../data_viper/salience_gt.pkl' savefile(gtsal_path, salmap_gt_mapped) pl.figure(1) pl.imshow(immontage(salmap_gt_mapped, [6, 17])) pl.savefig('tmp1.jpg') os.system('open tmp1.jpg')
def main(): ''' pipeline for testing and evaluation ''' # preset parameters save_path = '../data_viper/model/' # load data imL = 48 bs = 100 datafile_viper = '../data_viper/viper_class.pkl' viper = loadfile(datafile_viper) # load model modelfile_viper = '../data_viper/model/model.pkl' model = loadfile(modelfile_viper) # evaluation and testing # test_x = viper.test_x.get_value(borrow=True) test_x = np.asarray(viper.test_ims) test_y = viper.test_y.get_value(borrow=True) n_test = test_x.shape[0] n_batches_test = np.int(1.0*n_test/bs) n_test = n_batches_test * bs test_ypred = [model.test(i)[-1] for i in range(n_batches_test)] test_ypred = np.asarray(test_ypred).flatten() test_ims = test_x[:n_test].reshape((n_test, imL, imL, -1)) # assign predicted scores to images h, w = viper.imgs[0].shape[:2] mh, mw = len(np.unique(viper.yy)), len(np.unique(viper.xx)) msk0 = np.zeros(mh*mw) msks = [msk0.copy() for im in viper.imgs] showlist = [] for i in range(n_test): imgid = viper.test_imgids[i] patid = viper.test_ctrids[i] score = test_ypred[i] msks[imgid][patid] = score # resize predicted salience map to match image size msks_rs = [imresize(msk.reshape((mh, mw)), size=(h, w))/255. for msk in msks] # save salience map for comparison test_imids = np.asarray(np.unique(viper.test_imgids)) salmap_gt = np.asarray(viper.salmsks) #np.asarray([viper.salmsks[imid] for imid in test_imids]) salmap_pred = np.asarray(msks_rs) #np.asarray([msks_rs[imid]/255. for imid in test_imids]) savefile(save_path+'salmaps_comparison.pkl', [test_imids, salmap_gt, salmap_pred]) # quantize to show different test patches kmeans = KMeans(init='k-means++', n_clusters=10, n_init=10) kmeans.fit(test_ypred.reshape(n_test, 1)) # save to result folder for i in range(10): idx = kmeans.labels_== i if any(idx): im = immontage(list(test_ims[idx])) imsave(save_path+'{}.jpg'.format(kmeans.cluster_centers_[i]), im) print 'testing finished' os.system('xdg-open '+save_path)
def main(): ''' pipeline for supervised salience training ''' if os.path.isdir('../data_viper/'): datapath = '../data_viper/' else: datapath = '../data/' save_path = '../data_viper/model_feat/' DATA_OPT = 'feat' # feature type TRAIN_OPT = 'SVR' # training model option TRAIN = True # wheather re-train the model # prepare training data for supervised salience training #======================================================= datafile_viper = datapath + 'viper.pkl' if not os.path.isfile(datafile_viper): viper = DataMan_viper_small() viper.make_data() savefile(datafile_viper, viper) else: viper = loadfile(datafile_viper) viper = preprocess_data(viper, DATA_OPT) # training # ============== modelfile = datapath + 'model_feat/model.pkl' if TRAIN: tic = time.clock() model = train_model(viper, TRAIN_OPT) toc = time.clock() print 'Elapsed training time: {0:.2f} min'.format((toc-tic)/60.) savefile(modelfile, model) os.system('ls -lh ' + modelfile) else: model = loadfile(modelfile) ## validation #========================================= print 'validating trained model' nValid = 5000 valididx = np.random.permutation(viper.valid_feat.shape[0])[:nValid] # valid_ypred = model.predict(viper.valid_feat[valididx]) valid_ypred = predict(model, viper.valid_feat[valididx], viper.yy[viper.valid_ctrids][valididx], viper.imH) #- quantize patches based on testing scores kmeans = KMeans(init='k-means++', n_clusters=10, n_init=10, verbose=1) kmeans.fit(valid_ypred.reshape(nValid, 1)) #- crop patches for testing image valid_patset = np.asarray(viper.get_patchset('valid'))[valididx] #- save to result folder os.system('rm '+save_path+'*.jpg') for i in range(10): idx = kmeans.labels_== i if any(idx): pats = immontage(list(valid_patset[idx])) imsave(save_path+'{}.jpg'.format(kmeans.cluster_centers_[i]), pats) print 'patchset {} saved'.format(i) ### testing #=============== print 'testing' # test_ypred = model.predict(viper.test_feat) test_ypred = predict(model, viper.test_feat, viper.yy_test[viper.test_ctrids], viper.imH) ## assign predicted scores to images h, w = viper.imgs[0].shape[:2] mh, mw = len(np.unique(viper.yy_test)), len(np.unique(viper.xx_test)) msk0 = np.zeros(mh*mw, dtype=np.float32) msks = [msk0.copy() for im in viper.imgs] showlist = [] n_test = len(test_ypred) for i in range(n_test): imgid = viper.test_imgids[i] patid = viper.test_ctrids[i] score = test_ypred[i] msks[imgid][patid] = score # resize predicted salience map to match image size msks_rs = [mapresize(msk.reshape((mw, mh)).T, size=(h, w)) for msk in msks] # msks_rs = msks # save salience map for comparison test_imids = np.asarray(np.unique(viper.test_imgids)) salmap_gt = np.asarray(viper.salmsks) #np.asarray([viper.salmsks[imid] for imid in test_imids]) salmap_pred = np.asarray(msks_rs) #np.asarray([msks_rs[imid]/255. for imid in test_imids]) savefile(save_path+'salmaps_comparison.pkl', [test_imids, salmap_gt, salmap_pred])
def test_knn(): datafile_viper = '../data_viper/viper.pkl' viper = loadfile(datafile_viper) viper = downsample_data(viper) # from sklearn.neighbors import KNeighborsRegressor # model = KNeighborsRegressor(n_neighbors=5, weights='uniform', metric='euclidean') # model.fit(viper.train_feat, viper.train_y) from sklearn.neighbors import KDTree # divide into stripes nStripe = 10 y_max = viper.yy.max() y_min = viper.yy.min() y_len = np.int((y_max - y_min)/10.) y_centers = np.round(np.linspace(y_min+y_len, y_max-y_len, nStripe)) k = 5 y_ctr = y_centers[k] stripe_idx = np.where((viper.yy[viper.train_ctrids] >= y_ctr-y_len) & (viper.yy[viper.train_ctrids] < y_ctr+y_len))[0] model = KDTree(viper.train_feat[stripe_idx, :288], metric='euclidean') train_patset = viper.get_patchset('train') test_patset = viper.get_patchset('test') test_ids = np.where((viper.yy[viper.test_ctrids] >= y_ctr-y_len) & (viper.yy[viper.test_ctrids] < y_ctr+y_len))[0] np.random.shuffle(test_ids) for i in test_ids: get_testrect = lambda i: [viper.xx[viper.test_ctrids[i]] - viper.patL/2, viper.yy[viper.test_ctrids[i]] - viper.patL/2, viper.patL, viper.patL] get_trainrect = lambda i: [viper.xx[viper.train_ctrids[i]] - viper.patL/2, viper.yy[viper.train_ctrids[i]] - viper.patL/2, viper.patL, viper.patL] gray2color = lambda grayim: np.dstack((grayim, grayim, grayim)) imlist = [] patlist = [] maplist = [] patlist.append(imresize(test_patset[i], size=(100, 100))) imlist.append(drawrect(viper.imgs[viper.test_imgids[i]], get_testrect(i))) maplist.append(viper.salmsks[viper.test_imgids[i]]) dist, ind = model.query(viper.test_feat[i, :288], k=30, return_distance=True) print viper.test_y[i] hist = np.histogram(viper.train_y[stripe_idx[ind[0]]]) print hist[0] print hist[1] print dist for id in stripe_idx[ind[0]]: patlist.append(imresize(train_patset[id], size=(100, 100))) imlist.append(drawrect(viper.imgs[viper.train_imgids[id]], get_trainrect(id))) maplist.append(viper.salmsks[viper.train_imgids[id]]) pats = immontage(patlist) imgs = immontage(imlist) maps = immontage(maplist) imsave('tmp1.jpg', pats) imsave('tmp2.jpg', imgs) imsave('tmp3.jpg', maps) raw_input() os.system('xdg-open tmp1.jpg')