def exp(in_path,out_path,in_dict="rank_rev"): out_path="%s/%s" % (out_path,in_dict) files.make_dir(out_path) raw_path="%s/raw"% out_path reformat(in_path,raw_path,in_dict) scale_path="%s/frames" % out_path tools.rescale_imgs(raw_path,scale_path,dim_x=80,dim_y=128)
def binary_feats(seq_path, feats_path=None): feats_path = get_dirs(seq_path, "feats") files.make_dir(feats_path) for i, in_i in enumerate(files.top_files(seq_path)): print(i) out_i = feats_path + '/' + in_i.split('/')[-1] feats.compute_feats(in_i, out_i)
def make_feats(in_path, out_path, n_epochs=350, n_cats=20): files.make_dir(out_path) spline_path = out_path + '/spline' spline.ens_upsample(in_path, spline_path) model_path = out_path + '/models' fun = ts.make_binary_model ens.train_template(fun, spline_path, out_path, n_epochs, n_cats)
def convert(in_path, out_path): paths = files.top_files(in_path) files.make_dir(out_path) for path_i in paths: out_i = out_path + '/' + path_i.split('/')[-1] frames = from_binary(path_i) to_imgs(frames, out_i)
def transform_action_img(in_path, out_path, fun): files.make_dir(out_path) for in_i in files.top_files(in_path): out_i = "%s/%s" % (out_path, in_i.split('/')[-1]) img_i = cv2.imread(in_i, cv2.IMREAD_GRAYSCALE) new_img_i = fun(img_i) cv2.imwrite(out_i, new_img_i)
def create_test_workspace(sdk, output, workspace_info): # Remove any existing output. shutil.rmtree(output, True) # Copy the base tests. copy_tree(os.path.join(SCRIPT_DIR, 'tests', 'common'), output) if workspace_info.with_cc: copy_tree(os.path.join(SCRIPT_DIR, 'tests', 'cc'), output) if workspace_info.with_dart: copy_tree(os.path.join(SCRIPT_DIR, 'tests', 'dart'), output) # WORKSPACE file. write_file( os.path.join(output, 'WORKSPACE'), 'workspace', { 'sdk_path': os.path.relpath(sdk, output), 'with_cc': workspace_info.with_cc, 'with_dart': workspace_info.with_dart, }) if workspace_info.with_cc: # Generate test to verify that headers compile fine. headers = workspace_info.headers header_base = os.path.join(output, 'headers') write_file(make_dir(os.path.join( header_base, 'BUILD')), 'headers_build', { 'deps': list(filter(lambda k: headers[k], headers.keys())), }) write_file(make_dir(os.path.join(header_base, 'headers.cc')), 'headers', { 'headers': headers, }) return True
def concat_frames(in_path1, in_path2, out_path): seq1, seq2 = read_frames(in_path1, True), read_frames(in_path2, True) files.make_dir(out_path) for name_i in seq1.keys(): img0, img1 = seq1[name_i], seq2[name_i] new_img_i = np.concatenate([img0, img1], axis=0) cv2.imwrite(out_path + '/' + name_i + ".png", new_img_i)
def transform_template(fun, in_path, out_path, info=None, dir_ensemble=True): if (not is_ens(in_path) and dir_ensemble): return apply_fun(fun, in_path, out_path, info) files.make_dir(out_path) for in_i in files.top_files(in_path): out_i = '%s/%s' % (out_path, in_i.split("/")[-1]) apply_fun(fun, in_i, out_i, info)
def make_bagset(in_path, out_path, k=7): seq_dict = files.get_seqs(in_path) files.make_dir(out_path) for i in range(k): dataset_i = resample(seq_dict) out_i = "%s/bag%d" % (out_path, i) files.save_seqs(dataset_i, out_i) files.save_seqs(seq_dict, "%s/full" % out_path)
def ensemble1D(in_path, out_name, n_epochs=1000, size=64, atten=False): input_paths = files.top_files("%s/seqs" % in_path) out_path = "%s/%s" % (in_path, out_name) files.make_dir(out_path) train, extract = get_train(atten) ensemble = ens.ts_ensemble(train, extract) arg_dict = {'size': size, 'n_epochs': n_epochs} ensemble(input_paths, out_path, arg_dict)
def agum_feats(in_path, n_epochs=1000): paths = files.prepare_dirs(in_path, "agum", ["spline", "agum_seqs", "models", "feats"]) spline.ens_upsample(in_path, paths["spline"]) files.make_dir(paths["agum_seqs"]) agum.ens_agum(paths["spline"], paths["agum_seqs"]) basic.ts.ens_train(paths["agum_seqs"], paths["models"], n_epochs) basic.ts.ens_extract(paths["spline"], paths["models"], paths["feats"])
def extract_ensemble(img_path, dst_dir=None): model_path, seq_path = get_dirs(img_path, ['models', 'seqs'], dst_dir) files.make_dir(seq_path) for i, model_i in enumerate(files.top_files(model_path)): seq_i = "%s/nn%d" % (seq_path, i) model_i = "%s/nn%d" % (model_path, i) # resnet.extract_feats(seq_path,model_i,feat_i) sim.frames.extract_feats(img_path, model_i, seq_i)
def save(self, out_path, as_dir=False): if (as_dir): files.make_dir(out_path) for i, result_i in enumerate(self.results): result_i.save("%s/%d" % (out_path, i)) else: with open(out_path, 'wb') as out_file: pickle.dump(self, out_file)
def convert_inert(in_path, out_path): paths = files.top_files(in_path) files.make_dir(out_path) for path_i in paths: out_i = out_path + '/' + path_i.split('/')[-1] mat_i = scipy.io.loadmat(path_i) mat_i = mat_i['d_iner'] np.savetxt(out_i, mat_i, delimiter=',')
def get_img_file_path(url, img_url): domain = get_domain_from_url(url) img_dir = files.join_path('bin', domain) files.make_dir(img_dir) file_name = files.get_base_name(img_url) file_path = files.join_path(img_dir, file_name) return file_path
def cat_histogram(in_path, out_path): data = feats.read_single(in_path, as_dict=False) files.make_dir(out_path) for i in range(data.n_cats()): binary_i = data.binary(i) out_i = "%s/cat%d" % (out_path, i) result = mutual_info_classif(binary_i.X, binary_i.get_labels()) make_plot(result, out_i)
def ensemble_agum(in_path, out_path, n_epochs=1000, size=128): model = ts_cnn.TS_CNN(dropout=0.5, optim_alg=deep.Nestrov()) train, extract = ts_cnn.get_train(model) ensemble = ens.ts_ensemble(train, extract, preproc=norm_partial) input_paths = files.top_files(in_path) arg_dict = {"n_epochs": n_epochs, "size": size} files.make_dir(out_path) ensemble(input_paths, out_path, arg_dict)
def compute(in_path, out_path, upsample=False): seq_dict = imgs.read_seqs(in_path) files.make_dir(out_path) for name_i, seq_i in seq_dict.items(): feat_seq_i = extract(seq_i) name_i = name_i.split('.')[0] + '.txt' out_i = out_path + '/' + name_i np.savetxt(out_i, feat_seq_i, delimiter=',')
def show_all(in_path, out_path): files.make_dir(out_path) common, binary = exp.find_path(in_path) paths = common + binary for in_i in paths: print(in_i) out_i = "%s/%s" % (out_path, in_i.split('/')[-2]) make_plot(in_i, out_i)
def action_img(in_path, out_path, action_fun): if (type(action_fun) == list): action_fun = Pipeline(action_fun) files.make_dir(out_path) for in_i in files.top_files(in_path): frames = read_frames(in_i) action_img_i = action_fun(frames) out_i = out_path + '/' + in_i.split('/')[-1] + ".png" cv2.imwrite(out_i, action_img_i)
def save(self, out_path): files.make_dir(out_path) for name_i, seq_i in self.items(): out_i = "%s/%s" % (out_path, name_i) if (len(self.dims()) == 3 and self.dims()[-1] != 1): seq_i = [ np.concatenate(frame_j.T, axis=0) for frame_j in seq_i ] save_frames(out_i, seq_i)
def extract_template(fun, frame_path, model_path, out_path): if (not is_ens(frame_path)): return fun(frame_path, model_path, out_path) files.make_dir(out_path) for in_i in files.top_files(frame_path): model_i = '%s/%s' % (model_path, in_i.split("/")[-1]) out_i = '%s/%s' % (out_path, in_i.split("/")[-1]) print(in_i) fun(in_i, model_i, out_i)
def agum_exp(input_path, out_path, n_epochs=1000): files.make_dir(out_path) paths = files.get_paths(out_path, ["spline", "agum", "nn", "feats"]) spline.upsample(input_path, paths["spline"], size=64) agum = Agum([ts_scale, magnitude_warp]) agum(paths["spline"], paths["agum"]) train, extract = ts_cnn.get_train() train(paths["agum"], paths["nn"], n_epochs) extract(paths["agum"], paths["nn"], paths["feats"])
def binary_extract(frame_path,model_path,seq_path): files.make_dir(seq_path) paths=model_path if(type(model_path)==list) else files.top_files(model_path) frame_dict=imgs.read_seqs(frame_path) for i,in_i in enumerate(paths): print(i) out_i= seq_path+'/'+in_i.split('/')[-1] extract.extract_features(frame_dict,in_i,out_i) gc.collect()
def acc_hist(common_path, binary_path, clf="LR", out_path=None, title='3DHOI'): votes = full_train(common_path, binary_path, clf) files.make_dir(out_path) fun = simple_histogram for i, result_i in enumerate(votes.results): hist_i = result_i.indv_acc() title_i = "%s_%d" % (title, i) plot_i = fun(hist_i, title=title_i, cumsum=False, show=False) plot_i.savefig("%s/%d" % (out_path, i))
def train_template(fun, in_path, out_path, n_epochs=5, n_cats=20): files.make_dir(out_path) if (type(n_cats) == tuple): cat_iter = range(n_cats[0], n_cats[1]) else: cat_iter = range(n_cats) for i in cat_iter: out_i = '%s/nn%d' % (out_path, i) print(out_i) fun(in_path, out_i, n_epochs, i)
def select_data(in_path,out_path,prefix="bin"): files.make_dir(out_path) all_files=files.get_files(in_path) prefix_files=[f for f in all_files if files.extract_prefix(f)==prefix] in_prefix_files=files.append_path(in_path,prefix_files) out_prefix_files=files.append_path(out_path,prefix_files) for in_file,out_file in zip(in_prefix_files,out_prefix_files): shutil.copyfile(in_file, out_file) print(in_file)
def make_plot(in_path, out_path): if (not os.path.isdir(in_path)): plot_i = tsne_plot(in_path, show=False) plot_i.savefig(out_path) else: files.make_dir(out_path) for in_i in files.top_files(in_path): out_i = "%s/%s" % (out_path, in_i.split('/')[-1]) plot_i = tsne_plot(in_i, show=False) plot_i.savefig(out_i)
def proj_template(in_path,out_path,proj_funcs): files.make_dir(out_path) paths=files.top_files(in_path) for seq_path_i in paths: print(seq_path_i) seq_i=imgs.read_frames(seq_path_i,as_dict=False) proj_seq_i=[proj_i(seq_i) for proj_i in proj_funcs] new_imgs=np.concatenate(proj_seq_i,axis=1) out_i="%s/%s" % (out_path,seq_path_i.split("/")[-1]) imgs.save_frames(out_i,new_imgs)
def __call__(self, ens_path, n_cats, arg_dict=None, use_binary=True): files.make_dir(ens_path) nn_path = "%s/%s" % (ens_path, self.nn) paths = ["%s/%d" % (nn_path, i) for i in range(n_cats)] if (use_binary): files.make_dir(nn_path) for i, path_i in enumerate(paths): self.binary_gen(path_i, i) if (self.ens): self.ens(paths, ens_path, arg_dict)
def reformat(in_path,out_path,in_dict="rank_rev"): pattern="%s/%i/image2/"+in_dict files.make_dir(out_path) for i in range(1,21): in_i=pattern % (in_path,i) for in_ij in files.top_files(in_i): print(in_ij) name_ij=in_ij.split('/')[-1] out_ij="%s/%d_%s" %(out_path,i,name_ij) print(out_ij) shutil.copyfile(in_ij, out_ij)
def files_list_view(self): if not 'password' in self.request.POST and self.check(): print self.request.POST return HTTPFound(location='/login') url = USB_LOCATION + get_url(self.request.matchdict['list']) url_parsed = '/' for i in range(len(url.split('/')) - 3): url_parsed += url.split('/')[i + 1] + '/' action = url.split('/')[-2] filename = str(url_parsed.split('/')[-2]) if 'password' in self.request.POST: if self.request.POST['password'] != '': password = self.request.POST['password'] # print password content = encrypt(self.request.POST['notecontent'], password) write('/'.join(url.split('/')[:-2]), content) return HTTPFound(location='/files' + url[:-1]) # return Response() elif 'file' in self.request.POST: filename = self.request.POST['file'].filename print filename input_file = self.request.POST['file'].file upload(input_file, url, filename) print '/files' + url return HTTPFound(location='/files' + url) elif 'dir_name' in self.request.POST: dirname = self.request.POST['dir_name'] make_dir(dirname, url) return HTTPFound(location='/files' + url) elif 'note_name' in self.request.POST: write(url + self.request.POST['note_name'], '') return HTTPFound(location='/files' + url) elif 'notecontent' in self.request.POST: content = encrypt(self.request.POST['notecontent'], decrypt(self.request.session['enpass'], self.request.cookies['r'])) write('/'.join(url.split('/')[:-2]), content) return HTTPFound(location='/files' + url) elif action == 'edit': content = decrypt(read(url_parsed[:-1]), decrypt(self.request.session['enpass'], self.request.cookies['r'])) re = { 'page_title' : 'Notes', 'edit' : True, 'contents' : content, 'url' : url } re.update(self.get_header()) return re elif action == 'rename': # file_old = str(url_parsed.split('/')[-3]) file_old = '/'.join(url_parsed.split('/')[:-2]) if not is_file('/'.join(url_parsed.split('/')[:-2])) and not is_dir('/'.join(url_parsed.split('/')[:-2])): print 'not filename' return HTTPFound(location='/files') rename(file_old, filename) return HTTPFound('/files' + '/'.join(url.split('/')[:-4])) elif is_file(url_parsed[:-1]): if action == 'download': re = FileResponse(url_parsed[:-1]) re.headers['Content-Disposition'] = 'attachment; filename="' + filename + '"' re.headers['Content-Type'] = 'application/force-download' # re.headers['filename'] = filename re.headers['Accept-Ranges'] = 'bytes' return re elif action == 'delete': delete(url_parsed[:-1]) return HTTPFound(self.request.url[:-7-len(filename.replace(' ', '%20'))]) elif is_dir(url[:-7]) and action == 'delete': delete(url[:-7]) return HTTPFound(self.request.url[:-7-len(filename)]) elif not is_dir(url) or len(url.split('/')) < 5: return HTTPFound(location='/files') temp = [str(part) for part in list_dir(url)] temp.sort(lambda x, y: cmp(x.lower(),y.lower())) item_list = [ { 'url' : '/files/' + url[1:] + part if is_dir(url + part) else '/files/' + url[1:] + part + '/download', 'url_ren' : '/files/' + url[1:] + part, 'url_del' : '/files/' + url[1:] + part + '/delete', 'name' : part, 'is_file' : is_file(url + part), 'size' : size(url + part) } for part in temp ] re = {'page_title': LINKS['files_list']['title'], 'list' : item_list, 'up_dir' : '/files/' + url_parsed[1:], 'url' : url, 'edit' : False } re.update(self.get_header()) return re