def compile_results(args,views=range(2)): all_dist = [[],[]] rims = [[],[]] rlocs = [[],[]] rinfo = [[],[]] rpredlocs = [[],[]] for view in views: for split_num in range(3): args['view'] = view args['split_num'] = split_num curconf = get_conf(args) tf.reset_default_graph() self = PoseUNet.PoseUNet(curconf,name=get_name(args)) dist, ims, preds, predlocs, locs, info = self.classify_val(0) all_dist[view].append(dist) rims[view].append(ims) rlocs[view].append(locs) rinfo[view].append(info) rpredlocs[view].append(predlocs) all_dist[view] = np.concatenate(all_dist[view],0) rims[view] = np.concatenate(rims[view],0) rlocs[view] = np.concatenate(rlocs[view],0) rinfo[view] = np.concatenate(rinfo[view], 0) rpredlocs[view] = np.concatenate(rpredlocs[view], 0) return all_dist, rims, rlocs, rinfo, rpredlocs
def train(args): split_num = int(args['split_num']) view = int(args['view']) split_type = args['split_type'] if 'restore' in args.keys(): restart = ast.literal_eval(args['restore']) else: restart = False conf = get_conf(args) print('split:{}, view:{} split_type:{}'.format(split_num,view,split_type)) self = PoseUNet.PoseUNet(conf,name=get_name(args)) self.train_unet(restart,0)
def __init__(self, conf, name='pose_u_att', unet_name='pose_unet'): PoseCommon.PoseCommon.__init__(self, conf, name) self.dep_nets = PoseUNet.PoseUNet(conf, unet_name) self.net_name = 'pose_u_att' self.dep_nets.keep_prob = 1. self.db_name = '_att' self.min_score = -1 self.att_layers = conf.att_layers self.att_hist = conf.att_hist # amount of history for attention self.joint = True self.debug_layers = None self.sel_layers = None self.sel_layers_ndx = None
def main(argv): parser = argparse.ArgumentParser() parser.add_argument("conffile", help="config file", required=True) parser.add_argument("confname", help="conf name for importing", required=True) parser.add_argument("net_name", help="Name the network to classify with", required=True) parser.add_argument("movie", help="movie to classify", required=True) parser.add_argument("out name", help="file to save results to", required=True) args = parser.parse_args() imp_mod = importlib.import_module(args.conffile) conf = imp_mod.__dict__[args.confname] oname = re.sub('!', '__', conf.getexpname(args.movie)) self = PoseUNet.PoseUNet(conf, args.net_name) sess = self.init_net_meta(0, True) predList = self.classify_movie(args.movie, sess, flipud=True) cap = cv2.VideoCapture(args.movie) height = int(cap.get(cvc.FRAME_HEIGHT)) width = int(cap.get(cvc.FRAME_WIDTH)) rescale = conf.unet_rescale orig_crop_loc = conf.cropLoc[(height, width)] crop_loc = [int(x / rescale) for x in orig_crop_loc] end_pad = [ int((height - conf.imsz[0]) / rescale) - crop_loc[0], int((width - conf.imsz[1]) / rescale) - crop_loc[1] ] pp = [(0, 0), (crop_loc[0], end_pad[0]), (crop_loc[1], end_pad[1]), (0, 0)] predScores = np.pad(predList[1], pp, mode='constant', constant_values=-1.) predLocs = predList[0] predLocs[:, :, 0] += orig_crop_loc[1] predLocs[:, :, 1] += orig_crop_loc[0] hdf5storage.savemat(args.outname, { 'locs': predLocs, 'scores': predScores, 'expname': args.movie }, appendmat=False, truncate_existing=True) print('Done Detecting:%s' % oname)
from stephenHeadConfig import conf as conf import PoseUMDN import tensorflow as tf import PoseTools import math import PoseUNet import cv2 import PoseCommon mov = '/home/mayank/Dropbox (HHMI)/temp/stephen/C002H001S0001_c_2.avi' out = '/home/mayank/temp/C002H001S0001_c_2_res_unet.avi' conf.mdn_min_sigma = 3. conf.mdn_max_sigma = 3. self = PoseUNet.PoseUNet(conf,'pose_unet_droput_0p7') # self = PoseUMDN.PoseUMDN(conf) self.train_unet(False,0) # self.train_umdn(False,0,joint=True) val_dist, val_ims, val_preds, val_predlocs, val_locs= self.classify_val(0,-1) # self.create_pred_movie(mov,out,flipud=True) ## tf.reset_default_graph() self.init_train(0) self.pred = self.create_network() self.create_saver() sess = tf.InteractiveSession() self.init_and_restore(sess,True,['loss','dist']) info= []
def main(argv): # defaulttrackerpath = "/groups/branson/home/bransonk/tracking/code/poseTF/matlab/compute3Dfrom2D/for_redistribution_files_only/run_compute3Dfrom2D.sh" defaulttrackerpath = "/groups/branson/bransonlab/mayank/PoseTF/matlab/compiled/run_compute3Dfrom2D_compiled.sh" # defaultmcrpath = "/groups/branson/bransonlab/projects/olympiad/MCR/v91" defaultmcrpath = "/groups/branson/bransonlab/mayank/MCR/v92" parser = argparse.ArgumentParser() parser.add_argument("-s", dest="sfilename", help="text file with list of side view videos", required=True) parser.add_argument("-f", dest="ffilename", help="text file with list of front view videos. The list of side view videos and front view videos should match up", required=True) parser.add_argument("-d", dest="dltfilename", help="text file with list of DLTs, one per fly as 'flynum,/path/to/dltfile'", required=True) parser.add_argument("-o", dest="outdir", help="temporary output directory to store intermediate computations", required=True) parser.add_argument("-r", dest="redo", help="if specified will recompute everything", action="store_true") parser.add_argument("-rt", dest="redo_tracking", help="if specified will only recompute tracking", action="store_true") parser.add_argument("-gpu", dest='gpunum', help="GPU to use [optional]") parser.add_argument("-makemovie", dest='makemovie', help="if specified will make results movie", action="store_true") parser.add_argument("-trackerpath", dest='trackerpath', help="Absolute path to the compiled MATLAB tracker script run_compute3Dfrom2D.sh", default=defaulttrackerpath) parser.add_argument("-mcrpath", dest='mcrpath', help="Absolute path to MCR", default=defaultmcrpath) parser.add_argument("-ncores", dest="ncores", help="Number of cores to assign to each MATLAB tracker job", default=1) parser.add_argument("-name", dest="name", help="Name of the detector", default='pose_unet') group = parser.add_mutually_exclusive_group() group.add_argument("-only_detect", dest='detect', action="store_true", help="Do only the detection part of tracking which requires GPU") group.add_argument("-only_track", dest='track', action="store_true", help="Do only the tracking part of the tracking which requires MATLAB") args = parser.parse_args() if args.redo is None: args.redo = False if args.redo_tracking is None: args.redo_tracking = False if args.detect is False and args.track is False: args.detect = True args.track = True args.outdir = os.path.abspath(args.outdir) with open(args.sfilename, "r") as text_file: smovies = text_file.readlines() smovies = [x.rstrip() for x in smovies] with open(args.ffilename, "r") as text_file: fmovies = text_file.readlines() fmovies = [x.rstrip() for x in fmovies] print(smovies) print(fmovies) print(len(smovies)) print(len(fmovies)) if args.track: if len(smovies) != len(fmovies): print("Side and front movies must match") raise exit(0) # read in dltfile dltdict = {} f = open(args.dltfilename, 'r') for l in f: lparts = l.split(',') if len(lparts) != 2: print("Error splitting dlt file line %s into two parts" % l) raise exit(0) dltdict[float(lparts[0])] = lparts[1].strip() f.close() # compiled matlab command matscript = args.trackerpath + " " + args.mcrpath if args.detect: import numpy as np import tensorflow as tf from scipy import io from cvc import cvc import localSetup import PoseTools import multiResData import cv2 import PoseUNet import PoseCommon for ff in smovies + fmovies: if not os.path.isfile(ff): print("Movie %s not found" % (ff)) raise exit(0) if args.gpunum is not None: os.environ['CUDA_VISIBLE_DEVICES'] = args.gpunum for view in range(2): # 0 for front and 1 for side if args.detect: tf.reset_default_graph() if view == 1: # For Side from stephenHeadConfig import sideconf as conf extrastr = '_side' valmovies = smovies else: # For FRONT from stephenHeadConfig import conf as conf extrastr = '_front' valmovies = fmovies if args.detect: self = PoseUNet.PoseUNet(conf,args.name) self.create_ph_fd() self.create_fd() self.pred = self.create_network() sess = tf.Session() self.create_saver() self.restore(sess, True) PoseCommon.initialize_remaining_vars(sess) for ndx in range(len(valmovies)): mname, _ = os.path.splitext(os.path.basename(valmovies[ndx])) oname = re.sub('!', '__', conf.getexpname(valmovies[ndx])) pname = os.path.join(args.outdir, oname + extrastr) print(oname) # detect if args.detect and os.path.isfile(valmovies[ndx]) and \ (args.redo or not os.path.isfile(pname + '.mat')): predList = self.classify_movie(valmovies[ndx], sess) if args.makemovie: PoseTools.create_pred_movie(conf, predList, valmovies[ndx], pname + '.avi', outtype) cap = cv2.VideoCapture(valmovies[ndx]) height = int(cap.get(cvc.FRAME_HEIGHT)) width = int(cap.get(cvc.FRAME_WIDTH)) orig_crop_loc = conf.cropLoc[(height, width)] crop_loc = [old_div(x, 4) for x in orig_crop_loc] end_pad = [old_div(height, 4) - crop_loc[0] - old_div(conf.imsz[0], 4), old_div(width, 4) - crop_loc[1] - old_div(conf.imsz[1], 4)] pp = [(0, 0), (crop_loc[0], end_pad[0]), (crop_loc[1], end_pad[1]), (0, 0), (0, 0)] predScores = np.pad(predList[1], pp, mode='constant', constant_values=-1.) predLocs = predList[0] predLocs[:, :, :, 0] += orig_crop_loc[1] predLocs[:, :, :, 1] += orig_crop_loc[0] io.savemat(pname + '.mat', {'locs': predLocs, 'scores': predScores[..., 0], 'expname': valmovies[ndx]}) print('Detecting:%s' % oname) # track if args.track and view == 1: oname_side = re.sub('!', '__', conf.getexpname(smovies[ndx])) oname_front = re.sub('!', '__', conf.getexpname(fmovies[ndx])) pname_side = os.path.join(args.outdir, oname_side + '_side.mat') pname_front = os.path.join(args.outdir, oname_front + '_front.mat') # 3d trajectories basename_front, _ = os.path.splitext(fmovies[ndx]) basename_side, _ = os.path.splitext(smovies[ndx]) savefile = basename_side + '_3Dres.mat' # savefile = os.path.join(args.outdir , oname_side + '_3Dres.mat') trkfile_front = basename_front + '.trk' trkfile_side = basename_side + '.trk' redo_tracking = args.redo or args.redo_tracking if os.path.isfile(savefile) and os.path.isfile(trkfile_front) and \ os.path.isfile(trkfile_side) and not redo_tracking: print("%s, %s, and %s exist, skipping tracking" % (savefile, trkfile_front, trkfile_side)) continue flynum = conf.getflynum(smovies[ndx]) # print "Parsed fly number as %d"%flynum kinematfile = os.path.abspath(dltdict[flynum]) jobid = oname_side scriptfile = os.path.join(args.outdir, jobid + '_track.sh') logfile = os.path.join(args.outdir, jobid + '_track.log') # print "matscript = " + matscript # print "pname_front = " + pname_front # print "pname_side = " + pname_side # print "kinematfile = " + kinematfile # make script to be qsubbed scriptf = open(scriptfile, 'w') scriptf.write('if [ -d %s ]\n' % args.outdir) scriptf.write(' then export MCR_CACHE_ROOT=%s/mcrcache%s\n' % (args.outdir, jobid)) scriptf.write('fi\n') scriptf.write('%s "%s" "%s" "%s" "%s" "%s" "%s"\n' % ( matscript, savefile, pname_front, pname_side, kinematfile, trkfile_front, trkfile_side)) scriptf.close() os.chmod(scriptfile, stat.S_IRUSR | stat.S_IRGRP | stat.S_IWUSR | stat.S_IWGRP | stat.S_IXUSR | stat.S_IXGRP) cmd = "ssh login1 'source /etc/profile; qsub -pe batch %d -N %s -j y -b y -o '%s' -cwd '\"%s\"''" % ( args.ncores, jobid, logfile, scriptfile) print(cmd) call(cmd, shell=True)
#'/groups/branson/home/robiea/Projects_data/Labeler_APT/Austin_labelerprojects_expandedbehaviors/GT/witinAssayData/cx_JRC_SS03500_CsChr_RigB_20150811T114536', #'/groups/branson/home/robiea/Projects_data/Labeler_APT/Austin_labelerprojects_expandedbehaviors/GT/witinAssayData/cx_GMR_SS00243_CsChr_RigD_20150812T155340' #'/groups/branson/home/robiea/Projects_data/Labeler_APT/Austin_labelerprojects_expandedbehaviors/GT/withGenotypeData/cx_GMR_SS00030_CsChr_RigD_20150826T145307' #'/groups/branson/home/robiea/Projects_data/Labeler_APT/Austin_labelerprojects_expandedbehaviors/GT/withGenotypeData/cx_GMR_SS00038_CsChr_RigC_20150908T140450' #'/groups/branson/home/robiea/Projects_data/Labeler_APT/Austin_labelerprojects_expandedbehaviors/GT/withGenotypeData/cx_GMR_SS00168_CsChr_RigC_20150909T110456', #'/groups/branson/home/robiea/Projects_data/Labeler_APT/Austin_labelerprojects_expandedbehaviors/GT/withGenotypeData/cx_GMR_SS00238_CsChr_RigD_20150826T143306', #'/groups/branson/home/robiea/Projects_data/Labeler_APT/Austin_labelerprojects_expandedbehaviors/GT/withGenotypeData/cx_JHS_K_85321_CsChr_RigB_20151021T095119', dirs = [ '/groups/branson/home/robiea/Projects_data/Labeler_APT/cx_GMR_SS00038_CsChr_RigB_20150729T150617', '/groups/branson/home/robiea/Projects_data/Labeler_APT/cx_GMR_SS00030_CsChr_RigC_20150826T144616', '/groups/branson/home/robiea/Projects_data/Labeler_APT/cx_JHS_K_85321_CsChr_RigD_20150909T163219', '/groups/branson/home/robiea/Projects_data/Labeler_APT/cx_GMR_SS00168_CsChr_RigD_20150909T111218', '/groups/branson/home/robiea/Projects_data/Labeler_APT/cx_GMR_SS00238_CsChr_RigC_20151007T150343', ] self = PoseUNet.PoseUNet(conf, name='pose_unet_full_20180521') sess = self.init_net_meta(1) model_file = self.get_latest_model_file() def pred_fn(all_f): bsize = conf.batch_size xs, _ = PoseTools.preprocess_ims(all_f, in_locs=np.zeros( [bsize, self.conf.n_classes, 2]), conf=self.conf, distort=False, scale=self.conf.unet_rescale) self.fd[self.ph['x']] = xs self.fd[self.ph['phase_train']] = False
import PoseTools self = PoseTrain.PoseTrain(conf) val_dist, val_ims, val_preds, val_predlocs, val_locs = self.classify_val_base() kk = np.percentile(val_dist, [80, 90, 95], axis=0) pp = np.transpose(val_ims[0, ...].astype('uint8'), [1, 2, 0]) PoseTools.create_result_image(pp, val_locs[0, ...], kk) ## U-Net. from poseConfig import felipeConfig as conf import PoseUNet conf.mdn_min_sigma = 10. conf.mdn_max_sigma = 15. self = PoseUNet.PoseUNet(conf, name='pose_unet') self.train_unet(restore=False, train_type=0) ## U-Net results from poseConfig import felipeConfig as conf import PoseUNet import os import PoseTools os.environ['CUDA_VISIBLE_DEVICES'] = '' conf.mdn_min_sigma = 10. conf.mdn_max_sigma = 15. self = PoseUNet.PoseUNet(conf, name='pose_unet') val_dist, val_ims, val_preds, val_predlocs, val_locs = self.classify_val()
## for debug.. import os os.environ["CUDA_VISIBLE_DEVICES"] = '0' import PoseUNet from poseConfig import aliceConfig as conf import h5py import multiResData import tensorflow as tf import numpy as np import PoseTools import pickle from scipy import io as sio import math self = PoseUNet.PoseUNet(conf, name='pose_unet_128_bs8') _, dirs, _ = multiResData.load_val_data(conf) for ndx in range(len(dirs)): dirs[ndx] = dirs[ndx].replace('$dataroot', '/home/mayank/work/FlySpaceTime') trx_file = dirs[2].replace('movie.ufmf', 'registered_trx.mat') self.create_pred_movie_trx(dirs[2], '/home/mayank/temp/aliceOut_JHS_fly6_0_300.avi', trx_file, 6, max_frames=300, start_at=0, trace=True) ##