def read_frames(seq_path_i, as_dict=False): if (as_dict): return { files.clean_str(path_j): cv2.imread(path_j, cv2.IMREAD_GRAYSCALE) for path_j in files.top_files(seq_path_i) } return [ cv2.imread(path_j, cv2.IMREAD_GRAYSCALE) for path_j in files.top_files(seq_path_i) ]
def read_frame_seqs(in_path, n_split=1): frame_seqs = FrameSeqs() for i, path_i in enumerate(files.top_files(in_path)): name_i = files.Name(path_i.split('/')[-1]).clean() if (len(name_i) == 0): name_i = files.Name(str(i)) frames = [ read_frame(path_j, n_split) for path_j in files.top_files(path_i) ] frame_seqs[name_i] = frames return frame_seqs
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 simple_exp(dataset): if (os.path.isdir(dataset)): dataset = files.top_files(dataset) result = train_model(dataset) result.report() print(result.get_error()) print(result.get_acc())
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 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 read_actions(in_path): actions = ActionImgs() for path_i in files.top_files(in_path): name_i = files.Name(path_i.split('/')[-1]) name_i = name_i.clean() actions[name_i] = cv2.imread(path_i, cv2.IMREAD_GRAYSCALE) return actions
def read_frames(seq_path_i): if (seq_path_i.split(".")[-1] == "npy"): return np.load(seq_path_i) return [ cv2.imread(frame_path_j, cv2.IMREAD_GRAYSCALE) for frame_path_j in files.top_files(seq_path_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 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 ensemble_exp(in_path, out_name, n_epochs=1000, size=64): input_paths = files.top_files("%s/seqs" % in_path) out_path = "%s/%s" % (in_path, out_name) ts_cnn = TS_CNN(dropout=0.5, activ='elu', optim_alg=deep.Nestrov()) train, extract = get_train(ts_cnn) ensemble = ens.ts_ensemble(train, extract) arg_dict = {'size': size, 'n_epochs': n_epochs} ensemble(input_paths, out_path, arg_dict)
def read(in_path): if (type(in_path) == list): return [read_unified(in_path)] if (not os.path.isdir(in_path)): return [from_dict(read_single(in_path))] return [ from_dict(read_single(path_i)) for path_i in files.top_files(in_path) ]
def check_model(in_path): if (os.path.isdir(in_path)): model_path = in_path + "/frame_models" paths = files.top_files(model_path) model = load_model(paths[0]) else: model = load_model(in_path) model.summary()
def read_local_feats(in_path): paths = files.top_files(in_path) feat_dict = {} for path_i in paths: name_i = path_i.split("/")[-1] name_i = data.clean_name(path_i) feat_dict[name_i] = load_seq(path_i) return feat_dict
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 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 read_seqs(in_path): seqs = {} for seq_path_i in files.top_files(in_path): frames = read_frames(seq_path_i) name_i = seq_path_i.split('/')[-1] print(name_i) seqs[name_i] = frames return seqs
def read_seqs(in_path): seqs=Seqs() for path_i in files.top_files(in_path): data_i=np.loadtxt(path_i, delimiter=',') name_i=path_i.split('/')[-1] name_i=files.Name(name_i).clean()#clean(name_i) seqs[name_i]=data_i return seqs
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 check_feature(in_path): all_ts = [] for path_i in files.top_files(in_path): seq_dict_i = data.seqs.read_seqs(path_i) feat_dict_i = seq_dict_i.to_feats(scipy.stats.kurtosis, single=True) X, y = feat_dict_i.to_dataset() all_ts.append(X.flatten()) all_ts = np.array(all_ts).flatten() print(basic_stats(all_ts))
def img_dataset(in_path): img_dict={} for path_i in files.top_files(in_path): name_i=files.clean_str(path_i.split("/")[-1]) img_i=cv2.imread(path_i, cv2.IMREAD_GRAYSCALE) img_i=np.expand_dims(img_i,-1) img_dict[name_i]=img_i print(img_dict.keys()) return img_dict
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 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 read_seqs(in_path): paths = files.top_files(in_path) seq_dict = Seqs() for path_i in paths: data_i = np.load(path_i) name_i = path_i.split('/')[-1] name_i = files.Name(name_i).clean() seq_dict[name_i] = data_i return seq_dict
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 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 read_seqs(in_path): seq_dict = {} for path_i in files.top_files(in_path): postfix = in_path.split(".")[-1] if (postfix == "npy"): seq_i = np.loadtxt(path_i, delimiter=',') else: seq_i = np.load(path_i) seq_dict[path_i.split('/')[-1]] = seq_i return seq_dict
def __call__(self, common_path, binary_path, info): lines = [] for binary_i in files.top_files(binary_path): id_i = binary_i.split('/')[-1] path_i = "%s/feats" % binary_i desc_i = self.fun(common_path, path_i) desc_i = "%s,%s,%s" % (info, id_i, desc_i) print(desc_i) lines.append(str(desc_i)) return lines
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 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)