def train(): videofile_path = ['videos/ostrich.mp4' ] #Enter a folder OR a list of videos to analyze. if not os.path.isfile(videofile_path[0]): sys.exit("File {} not found!!".format(videofile_path[0])) path_config_file = '/home/braincreator/Pose_estimation/DeepLabCut/examples/ostrich/Testing-Shan-2019-08-07/config.yaml' print("\n" + "=" * 80 + "\n\t\t\t\tTRAINING\n" + "=" * 80) print("\nCreating Training dataset from labeled frames...\n") deeplabcut.create_training_dataset(path_config_file) print("\nDataset created, Next stop Training!\n\n") deeplabcut.train_network(path_config_file, shuffle=1, saveiters=2000, displayiters=500, gputouse=None, maxiters=50000) print("\nEvauating network:\n" + "=" * 50) deeplabcut.evaluate_network(path_config_file, plotting=True) print("\n" + "=" * 80 + "\n\t\t\t\tANALYZE\n" + "=" * 80) deeplabcut.analyze_videos(path_config_file, videofile_path, gputouse=None, videotype='.mp4') print("\n" + "=" * 80 + "\n\t\t\t\tCreating video\n" + "=" * 80) deeplabcut.create_labeled_video(path_config_file, videofile_path, outputframerate=5, draw_skeleton=True)
def evaluate_network(self, event): # shuffle = self.shuffle.GetValue() trainingsetindex = self.trainingset.GetValue() Shuffles = [self.shuffles.GetValue()] if self.plot_choice.GetStringSelection() == "Yes": plotting = True else: plotting = False if self.plot_scoremaps.GetStringSelection() == "Yes": for shuffle in Shuffles: deeplabcut.extract_save_all_maps(self.config, shuffle=shuffle) if len(self.bodyparts) == 0: self.bodyparts = "all" deeplabcut.evaluate_network( self.config, Shuffles=Shuffles, trainingsetindex=trainingsetindex, plotting=plotting, show_errors=True, comparisonbodyparts=self.bodyparts, )
def evaluate_network(self, event): #shuffle = self.shuffle.GetValue() trainingsetindex = self.trainingset.GetValue() shuffle = [self.shuffles.GetValue()] if self.plot_choice.GetStringSelection() == "Yes": plotting = True else: plotting = False if len(self.bodyparts) == 0: self.bodyparts = 'all' deeplabcut.evaluate_network(self.config, Shuffles=shuffle, trainingsetindex=trainingsetindex, plotting=plotting, show_errors=True, comparisonbodyparts=self.bodyparts, gputouse=None)
"global_scale": 0.5, "batch_size": 1, "save_iters": N_ITER, "display_iters": N_ITER // 2, "crop_size": [200, 200], # "multi_step": [[0.001, N_ITER]], } deeplabcut.auxiliaryfunctions.edit_config(pose_config_path, edits) print("Pose config edited.") print("Training network...") deeplabcut.train_network(config_path, maxiters=N_ITER) print("Network trained.") print("Evaluating network...") deeplabcut.evaluate_network(config_path, plotting=True) print("Network evaluated....") print("Extracting maps...") deeplabcut.extract_save_all_maps(config_path, Indices=[0, 1, 2]) new_video_path = deeplabcut.ShortenVideo( video_path, start="00:00:00", stop="00:00:01", outsuffix="short", outpath=os.path.join(cfg["project_path"], "videos"), ) print("Analyzing video...")
import os os.environ["DLClight"] = "True" import deeplabcut as dlc import sys shuffle = [int(sys.argv[1])] gputouse = int(sys.argv[2]) config_path = "/usr/users/onur.serce/dlc_real-alja_onur-2020-04-06/config.yaml" print("'config_path' is:", config_path) print("'dlc.__version__' is:'", dlc.__version__) print("\n") print("This is the name of the program:", sys.argv[0]) print("str(sys.argv):", str(sys.argv), "\n") dlc.evaluate_network(config=config_path, Shuffles=shuffle, plotting=False, gputouse=gputouse) print("dlc_evaluate_network.py with the call", str(sys.argv), "is done!")
# -*- coding: utf-8 -*- """ Created on Sun Nov 22 17:51:01 2020 @author: wlwee """ import deeplabcut import pathlib from ruamel.yaml import YAML path_config = r"C:\Users\wlwee\Documents\python\follow_cam_models\MODEL\eyemantle-weert-2020-11-17\config.yaml" #deeplabcut.label_frames(path_config) #deeplabcut.create_training_dataset(path_config) #deeplabcut.train_network(path_config, displayiters = 100, maxiters = 600000, saveiters = 50000, max_snapshots_to_keep = int(600000/50000)) for i in range(-12, -1): yaml = YAML() mf = pathlib.Path(path_config) data = yaml.load(mf) data['snapshotindex'] = i yaml.dump(data, mf) deeplabcut.evaluate_network(path_config, comparisonbodyparts=data['bodyparts'][2:])
edits = {"rotation": 180, "noise_sigma": 0.01} DLC_config = deeplabcut.auxiliaryfunctions.edit_config(posefile, edits) print("TRAIN NETWORK", shuffle) deeplabcut.train_network( path_config_file, shuffle=shuffle, saveiters=10000, displayiters=200, maxiters=maxiters, max_snapshots_to_keep=11, ) print("EVALUATE") deeplabcut.evaluate_network(path_config_file, Shuffles=[shuffle], plotting=True) print("Analyze Video") videofile_path = os.path.join(os.getcwd(), "openfield-Pranav-2018-10-30", "videos", "m3v1mp4.mp4") deeplabcut.analyze_videos(path_config_file, [videofile_path], shuffle=shuffle) print("Create Labeled Video and plot") deeplabcut.create_labeled_video(path_config_file, [videofile_path], shuffle=shuffle) deeplabcut.plot_trajectories(path_config_file, [videofile_path], shuffle=shuffle)
posefile=os.path.join(cfg['project_path'],'dlc-models/iteration-'+str(cfg['iteration'])+'/'+ cfg['Task'] + cfg['date'] + '-trainset' + str(int(cfg['TrainingFraction'][0] * 100)) + 'shuffle' + str(1),'train/pose_cfg.yaml') DLC_config=deeplabcut.auxiliaryfunctions.read_plainconfig(posefile) DLC_config['save_iters']=10 DLC_config['display_iters']=2 DLC_config['multi_step']=[[0.001,10]] print("CHANGING training parameters to end quickly!") deeplabcut.auxiliaryfunctions.write_plainconfig(posefile,DLC_config) print("TRAIN") deeplabcut.train_network(path_config_file) print("EVALUATE") deeplabcut.evaluate_network(path_config_file,plotting=True) print("CUT SHORT VIDEO AND ANALYZE") # Make super short video (so the analysis is quick!) vname='brief' newvideo=os.path.join(cfg['project_path'],'videos',vname+'.mp4') try: #you need ffmpeg command line interface subprocess.call(['ffmpeg','-i',video[0],'-ss','00:00:00','-to','00:00:00.4','-c','copy',newvideo]) except: #for windows: from moviepy.editor import VideoFileClip,VideoClip clip = VideoFileClip(video[0]) clip.reader.initialize() def make_frame(t): return clip.get_frame(1)
def evaluate_network(self): deeplabcut.evaluate_network(self.full_config_path(), plotting=False)
import deeplabcut if __name__ == "__main__": deeplabcut.evaluate_network( '/media/mahdi/LaCie/Mahdi/DLC/projects/project_1/VV2Dpose-mahdi-2/ config.yaml', Shuffles=[1], plotting=True, show_errors=True) print('end')
cfg=deeplabcut.auxiliaryfunctions.read_config(config) previous_path=cfg['project_path'] cfg['project_path']=projectpath deeplabcut.auxiliaryfunctions.write_config(config,cfg) print("This is the name of the script: ", sys.argv[0]) print("Shuffle: ", shuffle) print("config: ", config) deeplabcut.create_training_dataset(config, Shuffles=[shuffle],windows2linux=True) deeplabcut.train_network(config, shuffle=shuffle, max_snapshots_to_keep=5, maxiters=Maxiter) print("Evaluating...") deeplabcut.evaluate_network(config, Shuffles=[shuffle],plotting=True) print("Analyzing videos..., switching to last snapshot...") #cfg=deeplabcut.auxiliaryfunctions.read_config(config) #cfg['snapshotindex']=-1 #deeplabcut.auxiliaryfunctions.write_config(config,cfg) for vtype in ['.mp4','.m4v','.mpg']: try: deeplabcut.analyze_videos(config,[str(os.path.join(projectpath,'videos'))],shuffle=shuffle,videotype=vtype,save_as_csv=True) except: pass print("DONE WITH ", project," resetting to original path") cfg['project_path']=previous_path deeplabcut.auxiliaryfunctions.write_config(config,cfg)
def cli_create_project(project_path, video_paths, name='eye_video_demo', experimenter='experimenter', recursive=False, format_='mp4', exclude=[], num_frames=20, train=False, analyze=False, create_videos=False): """run all steps to create a DLC project""" if len(video_paths) == 0: # use provided example video_files = [op.join(op.split(op.realpath(__file__))[0], 'data', 'example_eye_camera_video.mp4')] else: # get video files video_files = [] for vp in list(video_paths): video_files.extend(get_video_files(vp, recursive, exclude, file_format=format_.lower())) # list all video files (and convert to mp4 if required) for i, vf in enumerate(video_files): print("found video file: %s" % vf) if op.splitext(vf)[1] == '.h264': vide_files[i] = convert_h264_to_mp4(vf) # create a new project config_path = deeplabcut.create_new_project(name, experimenter, video_files, working_directory=project_path, copy_videos=False) config = deeplabcut.utils.read_config(config_path) config['bodyparts'] = ['pupil_center', 'nasal_corner', 'temporal_corner'] config['numframes2pick'] = num_frames deeplabcut.utils.write_config(config_path, config) # extract and label frames deeplabcut.extract_frames(config_path, mode='automatic', algo='kmeans', crop=True) deeplabcut.label_frames(config_path) deeplabcut.check_labels(config_path) # create training dataset deeplabcut.create_training_dataset(config_path) if train: # train and evaluate the network deeplabcut.train_network(config_path) deeplabcut.evaluate_network(config_path) if analyze: deeplabcut.analyze_videos(config_path, video_files) if create_videos: # create a video deeplabcut.create_labeled_video(config_path, video_files)
def run_original_dlc_shuffle(task, date, shuffle, allow_growth=True, max_snapshots_to_keep=None, keepdeconvweights=True, maxiters=None): data_info = DataLoader(task) cfg = get_model_config(task, data_info.model_data_dir, scorer=data_info.scorer, date=date) path_config_file = Path(cfg["project_path"]) / "config.yaml" # dlc_cfg = get_train_config(cfg, shuffle=shuffle) # %% print("\nTraining network") deeplabcut.train_network( str(path_config_file), shuffle=shuffle, trainingsetindex=cfg["iteration"], allow_growth=allow_growth, max_snapshots_to_keep=max_snapshots_to_keep, keepdeconvweights=keepdeconvweights, maxiters=maxiters, # inherits from dlc_cfg file being read ) #%% # Evaluate run print("\nEVALUATE") deeplabcut.evaluate_network( str(path_config_file), plotting=False, Shuffles=[shuffle], trainingsetindex=cfg["iteration"], ) # %% print("Make copy of video for evaluation") # Read video video_file = str( Path(cfg["project_path"]) / "videos" / (data_info.vname + data_info.videotype)) # %% dfolder = Path(cfg["project_path"] ) / "videos" / "moriginal_iter_{}_shuffle_{}".format( cfg['iteration'], shuffle) if not os.path.isdir(dfolder): os.makedirs(dfolder) # Make video copy for evaluation newvideo = str(dfolder / (data_info.vname + '.mp4')) newclip = VideoFileClip(video_file) newclip.write_videofile(newvideo) #%% # deeplabcut.analyze_videos(path_config_file, # [newvideo], # save_as_csv=True, # shuffle=shuffle, # trainingsetindex=trainingsetindex, # destfolder=dfolder, # dynamic=(True, .1, 5)) # %% # cannot use dynamic cropping wo locref print("\nanalyze again...") deeplabcut.analyze_videos( str(path_config_file), [newvideo], save_as_csv=True, destfolder=dfolder, shuffle=shuffle, trainingsetindex=cfg["iteration"], ) # %% print("\nCreate Labeled video") deeplabcut.create_labeled_video( str(path_config_file), [newvideo], destfolder=dfolder, shuffle=shuffle, trainingsetindex=cfg["iteration"], save_frames=False, ) # %% # print("\nMaking plots") # deeplabcut.plot_trajectories(str(path_config_file), # [newvideo], # destfolder=dfolder, # shuffle=shuffle, # trainingsetindex=trainingsetindex) # % note when training gails we are not updating the model #%% return
# In[10]: deeplabcut.check_labels(path_config) # In[11]: deeplabcut.create_training_dataset(path_config) # In[13]: deeplabcut.train_network(path_config, saveiters='1000', displayiters='1') # In[14]: deeplabcut.evaluate_network(path_config, plotting=True, show_errors=True) # In[15]: deeplabcut.analyze_videos(path_config, ['/home/amr/Trial_DeepLabCut/mouse.avi'], save_as_csv=True) # In[26]: #to created trailpoints deeplabcut.create_labeled_video(path_config, ['/home/amr/Trial_DeepLabCut/mouse.avi'], trailpoints=10) # In[27]:
"decay_steps": maxiters, "lr_init": 0.0005, } DLC_config = deeplabcut.auxiliaryfunctions.edit_config(posefile, edits) for shuffle in Shuffles: print("TRAIN NETWORK", shuffle) deeplabcut.train_network( path_config_file, shuffle=shuffle, saveiters=saveiters, displayiters=displayiters, maxiters=maxiters, max_snapshots_to_keep=11, ) print("Analyze Video") videofile_path = os.path.join( os.getcwd(), "openfield-Pranav-2018-10-30", "videos", "m3v1mp4.mp4" ) deeplabcut.analyze_videos(path_config_file, [videofile_path], shuffle=shuffle) print("Create Labeled Video and plot") deeplabcut.create_labeled_video(path_config_file, [videofile_path], shuffle=shuffle) deeplabcut.plot_trajectories(path_config_file, [videofile_path], shuffle=shuffle) print("EVALUATE") deeplabcut.evaluate_network(path_config_file, Shuffles=Shuffles, plotting=False)
def usingDeep(path_config, project): exit = False while exit == False: video_path = path_config.split("/") video_path = '/' + video_path[1] + '/' + video_path[ 2] + '/' + video_path[3] + '/' + video_path[4] + '/videos/' print_usage("project") action = input() while action not in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]: try: action = int(action) if action not in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]: print("please type number between 0 and 12") action = input() except ValueError: print("Please enter number") action = input() print("been here") if action == 0: return False elif action == 1: print("do you want to crop the video? yes/no") if input() == "yes" or "y": print( "how many videos you you want to crop? (use number: 1,2,3 etc)" ) crops = int(input()) print("only crop all the video's and than exit") for loop in range(0, crops): deeplabcut.extract_frames(path_config, 'manual', 'kmeans', crop=True) deeplabcut.extract_frames(path_config, 'automatic', 'kmeans', crop=True) elif action == 2: deeplabcut.label_frames(path_config) elif action == 3: deeplabcut.check_labels(path_config) elif action == 4: deeplabcut.create_training_dataset(path_config) elif action == 5: with open("training_network.py") as fp: lines = fp.readlines() lines[3] = lines[3].split("=") lines[3] = lines[3][0] + "= '" + path_config + "'\n" with open("training_network.py", "w") as fp: for line in lines: fp.writelines(line) print("run: sbatch slurm.sh") return True elif action == 6: try: deeplabcut.evaluate_network(path_config, Shuffles=[1], trainingsetindex=0, plotting=None, show_errors=True, comparisonbodyparts='all', gputouse=None, rescale=False) except OSError as e: print("file does not exist") elif action == 7: print("\nType video name in project/videos you want to analyze") video_path = video_path + create_dict_list( path_config[:-11] + "videos/", 1)[0] with open("training_network.py") as fp: lines = fp.readlines() lines[3] = lines[3].split("=") lines[3] = lines[3][0] + "= '" + path_config + "'\n" lines[4] = lines[4].split("=") lines[4] = lines[4][0] + "= '" + video_path + "'\n" with open("training_network.py", "w") as fp: for line in lines: fp.writelines(line) print( "run: sbatch slurm.sh after changing the command in training_network.py" ) return True elif action == 8: print( "\nChoose the video in project/videos you want to plot trajectories from" ) video_path = video_path + create_dict_list( path_config[:-11] + "videos/", 1)[0] print(video_path) deeplabcut.plot_trajectories(path_config, [video_path], filtered=True) elif action == 9: print( "\nChoose the video in project/videos you want to make a labeled video from" ) video_path = video_path + create_dict_list( path_config[:-11] + "videos/", 1)[0] deeplabcut.create_labeled_video(path_config, [video_path], videotype='.mp4', draw_skeleton=True) elif action == 10: print("\nChoose where to upload the video from") video_path = '/data/11012579/videos/' + create_dict_list( '/data/11012579/videos/', 0)[0] print("\nChoose which video to upload") video_path_list = [ video_path + "/" + create_dict_list(video_path, 1)[0] ] while True: print("\nDo you want to add more videos?\nType yes or no") if input() == 'yes': video_path_list.append(video_path + "/" + create_dict_list(video_path, 1)[0]) else: deeplabcut.add_new_videos(path_config, video_path_list, copy_videos=False) break elif action == 11: print("also here") Dlc_results2 = pd.read_hdf( '/data/11012579/videos/vidDLC_resnet50_demo_project_grab2Feb7shuffle1_11500.h5' ) Dlc_results2.plot() else: print_usage("error") print("klaar")
class tutorial(): def __init__(self, algo, crop, userfeedback, shuffle, saveiters, displayiters, angle, center, scale, filenames): self.algo = "automatic" self.crop = False self.userfeedback = False self.shuffle = 1 self.saveiters = 200 self.displayiters = 100 self.angle = -4.5 self.center = None self.scale = 1.0 #Generate all rotating videos self.filenames = glob.glob('*.mp4') #Return the file name with .mp4 extention def rotate(self, image, angle, center=None, scale=1): #scale = 1: original size rows,cols,ch = image.shape if center == None: center = (cols / 2, rows / 2) M = cv2.getRotationMatrix2D(center, angle, scale) #Matrix: Rotate with center by angles dst = cv2.warpAffine(image,M,(cols,rows)) #After rotation return dst def videorotate(self, filenames, output_name, display_video = False): # capture video cap = cv2.VideoCapture(filename) #read video frame by frame #extract original video frame features sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) fps = int(cap.get(cv2.CAP_PROP_FPS)) #Make a directory to store the rotated videos path = "./rotated" try: os.mkdir(path) except OSError: pass else: print ("Successfully created the directory %s " % path) #Automatically name the rotated videos file = "./rotated/" + output_name out = cv2.VideoWriter(file, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), fps, sz) #Integrate all frames to video #Read videos and rotate by certain degrees while(cap.isOpened()): #flip for truning(fliping) frames of video ret,img = cap.read() try: img2 = rotate(img, -4.5) #Flipped Vertically out.write(img2) if display_video == True: cv2.imshow('rotated video',img2) k=cv2.waitKey(30) & 0xff #once you inter Esc capturing will stop if k==27: break except: print (filename, 'successfully rotated!!!' ) break cap.release() out.release() cv2.destroyAllWindows() for i in filenames: output_name = os.path.splitext(i)[0] + " rotated.mp4" cwd = os.chdir("./rotated") #we are using rotated videos cwd = os.getcwd() mp4files = [f for f in listdir(cwd) if isfile(join(cwd, f)) and os.path.splitext(f)[1] == ".mp4"] #Get all mp4 files task='Reaching' # Enter the name of your experiment Task experimenter='Donghan' # Enter the name of the experimenter video=mp4files # Enter the paths of your videos you want to grab frames from. now = datetime.datetime.now() try: path_config_file=deeplabcut.create_new_project(task,experimenter,video, working_directory='/home/donghan/DeepLabCut/data/rotated',copy_videos=True) #change the working directory to where you want the folders created. except: overwrite = input("Do you want to overwrite the folder since it already exists? y/n:") if overwrite == 'y': os.rmdir(task + '-' + experimenter + '-' + str(now.year) + '-' + str(now.month) + '-' + str(now.day)) path_config_file=deeplabcut.create_new_project(task,experimenter,video, working_directory='/home/donghan/DeepLabCut/data/rotated',copy_videos=True) else: continue # The function returns the path, where your project is. # You could also enter this manually (e.g. if the project is already created and you want to pick up, where you stopped...) #path_config_file = '/home/Mackenzie/Reaching/config.yaml' # Enter the path of the config file that was just created from the above step (check the folder) %matplotlib inline deeplabcut.extract_frames(path_config_file,'automatic',crop=False, userfeedback=False) #there are other ways to grab frames, such as by clustering 'kmeans'; please see the paper. #You can change the cropping to false, then delete the checkcropping part! #userfeedback: ask if users would like to continue or stop %gui wx deeplabcut.label_frames(path_config_file) deeplabcut.check_labels(path_config_file) #this creates a subdirectory with the frames + your labels deeplabcut.create_training_dataset(path_config_file) deeplabcut.train_network(path_config_file, shuffle=1, saveiters=200, displayiters=10) #Other parameters include trainingsetindex=0,gputouse=None,max_snapshots_to_keep=5,autotune=False,maxiters=None #Detailed function explanation can be found here https://github.com/AlexEMG/DeepLabCut/blob/efa95129061b1ba1535f7361fe76e9267568a156/deeplabcut/pose_estimation_tensorflow/training.py deeplabcut.evaluate_network(path_config_file) videofile_path = ['1035 SI_A, Aug 15, 13 17 7 rotated.mp4'] #Enter the list of videos to analyze. deeplabcut.analyze_videos(path_config_file,videofile_path) deeplabcut.create_labeled_video(path_config_file,videofile_path)
# import deeplabcut import os from pathlib import Path os.environ["DLClight"] = "True" import deeplabcut # video_path = ['/Users/felix/biteOscope/DLC_aedes/videos/mockVideo01.mp4'] # config_path = deeplabcut.create_new_project('aedes01','felix', video_path, copy_videos=False, videotype='.mp4') config_path = '/home/felix/biteOscope/DLC_aedes/aedes01-felix-2020-01-10/config.yaml' # deeplabcut.label_frames(config_path) # deeplabcut.create_training_dataset(config_path, num_shuffles=2) # deeplabcut.train_network(config_path, shuffle=2) deeplabcut.evaluate_network(config_path, Shuffles=[2], plotting=True, gputouse=0)
def auto_evaluate_model(path_config, num_snapshots, evaluation_dir_to_be_deleted='default', first_bodypart='default', last_bodypart='default'): ''' automatically evaluates deeplabcut model snap shots, allows which body parts to be evaluated to be subset by a given start stop range default being all. if you wish to re-evaluate the model for a different set of body parts you must delete all the previous evaluation results in the current target evaluation folder path_config: String String containing the full path to the config file for the deeplabcut model num_snapshots: Integer Number of snap shots saved when training a model, set by the 'max_snapshots_to_keep' parameter in deeplabcut.train_network() evaluation_dir: String String containing the full path to the target evaluation directory for the models current Iteration, shuffle, and TrainingFraction i.e., 'C:/Users/wlwee/Documents/python/follow_cam_models/MODEL/eyemantle-weert-2020-11-17/evaluation-results/iteration-1/eyemantleNov17-trainset80shuffle1' if left as default no files will be deleted, if path is given the files in it will be deleted and written over with the new bodyparts. first_bodypart: Integer First body part of range of body parts to evaluate, default is 0 last_bodypart: Integer Last body part of a range of body parts to evaluate, default is len(data['bodyparts']) Example -------- for evaluating 12 snapshots of a model with body parts 2 to 5 >>> body_parts_evaluated = auto_evaluate_model(path_config = 'C:/Users/wlwee/Documents/python/follow_cam_models/MODEL/eyemantle-weert-2020-11-17/config.yaml', num_snapshots = 12, evaluation_dir_to_be_deleted = 'C:/Users/wlwee/Documents/python/follow_cam_models/MODEL/eyemantle-weert-2020-11-17/evaluation-results/iteration-1/eyemantleNov17-trainset80shuffle1', first_bodypart = 2, last_bodypart = 5) -------- for evaluating 12 snapshots of a model for all bodyparts >>> body_parts_evaluated = auto_evaluate_model(path_config = 'C:/Users/wlwee/Documents/python/follow_cam_models/MODEL/eyemantle-weert-2020-11-17/config.yaml', num_snapshots = 12, evaluation_dir_to_be_deleted = 'C:/Users/wlwee/Documents/python/follow_cam_models/MODEL/eyemantle-weert-2020-11-17/evaluation-results/iteration-1/eyemantleNov17-trainset80shuffle1') -------- ''' if evaluation_dir_to_be_deleted != 'default': if os.path.exists(evaluation_dir_to_be_deleted) == True: for f in os.listdir(evaluation_dir_to_be_deleted): os.remove(os.path.join(evaluation_dir_to_be_deleted, f)) for i in range(-1 * num_snapshots, 0): yaml = YAML() mf = pathlib.Path(path_config) data = yaml.load(mf) data['snapshotindex'] = i yaml.dump(data, mf) if first_bodypart == 'default': first_bodypart = 0 if last_bodypart == 'default': last_bodypart = len(data['bodyparts']) print(first_bodypart, last_bodypart) print('evaluating following bodyparts: ') body_parts_evaluated = data['bodyparts'][first_bodypart:last_bodypart] print(body_parts_evaluated) deeplabcut.evaluate_network(path_config, comparisonbodyparts=data['bodyparts'] [first_bodypart:last_bodypart]) return body_parts_evaluated
str(int(cfg['TrainingFraction'][0] * 100)) + 'shuffle' + str(1), 'train/pose_cfg.yaml') DLC_config = dlc.auxiliaryfunctions.read_plainconfig(posefile) DLC_config['save_iters'] = numiter DLC_config['display_iters'] = 2 DLC_config['multi_step'] = [[0.001, numiter]] print("CHANGING training parameters to end quickly!") dlc.auxiliaryfunctions.write_plainconfig(posefile, DLC_config) print("TRAIN") dlc.train_network(path_config_file) print("EVALUATE") dlc.evaluate_network(path_config_file, plotting=True) videotest = os.path.join(cfg['project_path'], 'videos', videoname + ".avi") print(videotest) # quicker variant ''' print("VIDEO ANALYSIS") dlc.analyze_videos(path_config_file, [videotest], save_as_csv=True) print("CREATE VIDEO") dlc.create_labeled_video(path_config_file,[videotest], save_frames=False) print("Making plots") dlc.plot_trajectories(path_config_file,[videotest])
# ------------------------------------------------------------------ # Stage VIII: evaluation of the trained network # Step 10 # ------------------------------------------------------------------ """ It is important to evaluate the performance of the trained network. This performance is measured by computing the mean average Euclidean error (MAE; which is proportional to the average root mean square error) between the manual labels and the ones predicted by DeepLabCut. The MAE is saved as a comma-separated file and displayed for all pairs and only likely pairs (>p-cutoff). This helps to exclude, for example, occluded body parts. One of the strengths of DeepLabCut is that, owing to the probabilistic output of the scoremap, it can, if sufficiently trained, also reliably report whether a body part is visible in a given frame (see discussions of fingertips in reaching and the Drosophila legs during 3D behavior in Mathis et al. 12 ). """ if "step_10" in stages_to_run or "evaluate_network" in stages_to_run: deeplabcut.evaluate_network(config_path, Shuffles=[1], plotting=True, trainingsetindex=0, show_errors = True, comparisonbodyparts="all", gputouse=None, rescale=False) # 450000 """ Setting plotting to True plots all the testing and training frames with the manual and predicted labels. You should visually check the labeled test (and training) images that are created in the ‘evaluation-results’ directory. Ideally, DeepLabCut labeled the unseen (test) images according to your required accuracy, and the average train and test errors will be comparable (good generalization). What (numerically) constitutes an acceptable MAE depends on many factors (including the size of the tracked body parts and the labeling variability). Note that the test error can also be larger than the training error because of human variability in labeling (see Fig. 2 in Mathis et al. 12 ). If desired, customize the plots by editing the config.yaml file (i.e., the colormap, marker size (dotsize), and transparency of labels (alphavalue) can be modified). By default, each body part is plotted in a different color (governed by the colormap) and the plot labels indicate their source. Note that, by default, the human labels are plotted as a plus symbol (‘+’) and DeepLabCut’s
%gui wx deeplabcut.label_frames(path_config_file) # Lables have now been created deeplabcut.check_labels(path_config_file) #this creates a subdirectory with the frames + your labels # Reviewed the labels, the seem to be ok # Downloading the ResNets dataset: deeplabcut.create_training_dataset(path_config_file) # Training the dataset deeplabcut.train_network(path_config_file) # Evaluating the results deeplabcut.evaluate_network(path_config_file) # Analyzing video videofile_path = ['dlc-blinking/an3_vid2_full/eyes_only-Guy-2019-01-25/videos/animal_3_video_2_150fps_correct.mp4', 'dlc-blinking/an3_vid2_full/eyes_only-Guy-2019-01-25/videos/march_8_animal_1_video_150fps_correct.mp4'] #Enter the list of videos to analyze. videofile_path = ['dlc-blinking/whisk/whisk_only-Guy-2019-02-01/videos/crush_19_01_07_animal_3.mp4', 'dlc-blinking/whisk/whisk_only-Guy-2019-02-01/videos/animal_3_video_2_150fps_correct.mp4'] deeplabcut.analyze_videos(path_config_file,videofile_path,save_as_csv=True) deeplabcut.create_labeled_video(path_config_file, ['D:\\dlc-blinking\\an3_vid2_full\\whisk_only-Guy-2019-02-01\\videos\\crush_19_01_07_animal_3.mp4'], save_frames=True) deeplabcut.create_labeled_video(path_config_file, ['D:\\dlc-blinking\\whisk\\whisk_only-Guy-2019-02-01\\videos\\animal_3_video_2_150fps_correct.mp4'], save_frames=True) %matplotlib notebook #for making interactive plots. deeplabcut.plot_trajectories(path_config_file,videofile_path)
deeplabcut.extract_frames(config_path,'manual') #label frames deeplabcut.label_frames(config_path) ##opt#check annotated frames deeplabcut.check_labels(config_path) #create training dataset deeplabcut.create_training_dataset(config_path,num_shuffles=1) #train the network --> additional parameters deeplabcut.train_network(config_path, shuffle=1, trainingsetindex=0, gputouse=390.87, max_snapshots_to_keep=5, autotune=False, displayiters=None, saveiters=None) #evaluate the trained network deeplabcut.evaluate_network(config_path,shuffle=[1], plotting=True) #analyze new video deeplabcut.analyze_videos(config_path,['path of test video'],shuffle=1, save_as_csv=True) #create labeled video --> optional parameters deeplabcut.create_labeled_video(config_path,['pathof test video']) #plot trajectory of the extracted poses across the analyzed video deeplabcut.plot_trajectories(‘config_path’,['path of test video']) #extract outlier frames deeplabcut.extract_outlier_frames(‘config_path’,['path of test video']) #refine labels int raining set for outlier condition deeplabcut.refine_labels('config_path')