def create_training_dataset(self, event): """ """ num_shuffles = self.shuffles.GetValue() userfeedback_option = self.userfeedback.GetStringSelection() if userfeedback_option == 'Yes': userfeedback = True else: userfeedback = False trainindex = self.trainingindex.GetValue() if self.model_comparison_choice.GetStringSelection() == 'No': deeplabcut.create_training_dataset( self.config, num_shuffles, Shuffles=[self.shuffle.GetValue()], userfeedback=userfeedback, net_type=self.net_choice.GetValue(), augmenter_type=self.aug_choice.GetValue()) if self.model_comparison_choice.GetStringSelection() == 'Yes': deeplabcut.create_training_model_comparison( self.config, trainindex=trainindex, num_shuffles=num_shuffles, userfeedback=userfeedback, net_types=self.net_type, augmenter_types=self.aug_type)
def train_dlc(path): os.chdir(path) folders = [name for name in os.listdir(path) if os.path.isdir(name) and name[0].isdigit()] num_folder = len(folders) for i in range(len(folders)): start = time() if os.path.exists(r"%s\%s\labeled-data"%(path, folders[i])) and os.path.exists(r"%s\%s\config.yaml"%(path, folders[i])) \ and os.path.exists(r"%s\%s\dlc-models\iteration-0"%(path, folders[i])) == False and get_size(r"%s\%s\labeled-data"%(path, folders[i])) > 0: path_config_file = r"%s\%s\config.yaml" %(path, folders[i]) deeplabcut.check_labels(path_config_file) deeplabcut.create_training_dataset(path_config_file) deeplabcut.train_network(path_config_file, shuffle = 1, autotune = True, displayiters = 5000, saveiters = 5000, maxiters = 200000) deeplabcut.analyze_videos(path_config_file, r"%s\%s\videos"%(path, folders[i]), videotype = '.mp4', save_as_csv = True) print("%s training has been done, %s left"%(folders[i], num_folder - i)) elif os.path.exists(r"%s\%s\dlc-models\iteration-0"%(path, folders[i])): path_config_file = r"%s\%s\config.yaml" %(path, folders[i]) print("%s model has been trained, do you want to retrain it? y/n"%(folders[i])) feedback = input() if feedback == "y": deeplabcut.train_network(path_config_file, shuffle = 1, autotune = True, displayiters = 5000, saveiters = 5000, maxiters = 200000) # If model was previously trained, read config.yaml to retrieve deeplabcut.analyze_videos(path_config_file, r"%s\%s\videos"%(path, folders[i]), videotype='.mp4', save_as_csv=True) print("%s training has been done, %s left"%(folders[i], num_folder - i)) else: print("labeled-data folder not found OR empty OR config not found") print("Running time for %s is %s sec" %(folders[i], time() - start))
def load_demo_data(config, createtrainingset=True): """ Loads the demo data -- subset from trail-tracking data in Mathis et al. 2018. When loading, it sets paths correctly to run this project on your system Parameter ---------- config : string Full path of the config.yaml file of the provided demo dataset as a string. createtrainingset : bool Boolean variable indicating if a training set shall be created. Example -------- >>> deeplabcut.load_demo_data('config.yaml') -------- """ config = Path(config).resolve() config = str(config) transform_data(config) if createtrainingset: print("Loaded, now creating training data...") deeplabcut.create_training_dataset(config, num_shuffles=1)
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 run_program(path_config_file): # import vars and check path if not os.path.exists(path_config_file): raise ValueError('no config file at %s' % path_config_file) # train the network deeplabcut.create_training_dataset(path_config_file, windows2linux=True) deeplabcut.train_network(path_config_file, gputouse=1, max_snapshots_to_keep=5)
def DLCTrain(config_path, displayiters, saveiters, maxiters): """ Utility to facilitate training a DeepLabCut model from the MesoNet GUI. :param config_path: (required) The path to a DeepLabCut configuration file (.yaml). :param displayiters: (required) The interval between which intervals should be shown. :param saveiters: (required) The interval after which the model should be saved. :param maxiters: (required) The number of iterations after which the model should finish training. """ deeplabcut.create_training_dataset(config_path) deeplabcut.train_network(config_path, displayiters=displayiters, saveiters=saveiters, maxiters=maxiters)
def create_training_dataset(self, event): """ """ num_shuffles = self.shuffles.GetValue() if self.model_comparison_choice.GetStringSelection() == 'No': deeplabcut.create_training_dataset( self.config, num_shuffles, net_type=self.net_choice.GetValue(), augmenter_type=self.aug_choice.GetValue()) if self.model_comparison_choice.GetStringSelection() == 'Yes': deeplabcut.create_training_model_comparison( self.config, num_shuffles, net_types=self.net_type, augmenter_types=self.aug_type)
def create_training_dataset(self, event): """ """ num_shuffles = self.shuffle.GetValue() config_file = auxiliaryfunctions.read_config(self.config) trainindex = self.trainingindex.GetValue() if self.userfeedback.GetStringSelection() == "Yes": userfeedback = True else: userfeedback = False if config_file.get("multianimalproject", False): if self.cropandlabel.GetStringSelection() == "Yes": n_crops, height, width = [ int(text.GetValue()) for _, text in self.crop_widgets ] deeplabcut.cropimagesandlabels( self.config, n_crops, (height, width), userfeedback ) else: random = False deeplabcut.create_multianimaltraining_dataset( self.config, num_shuffles, Shuffles=[self.shuffle.GetValue()], net_type=self.net_choice.GetValue(), ) else: if self.model_comparison_choice.GetStringSelection() == "No": deeplabcut.create_training_dataset( self.config, num_shuffles, Shuffles=[self.shuffle.GetValue()], userfeedback=userfeedback, net_type=self.net_choice.GetValue(), augmenter_type=self.aug_choice.GetValue(), ) if self.model_comparison_choice.GetStringSelection() == "Yes": deeplabcut.create_training_model_comparison( self.config, trainindex=trainindex, num_shuffles=num_shuffles, userfeedback=userfeedback, net_types=self.net_type, augmenter_types=self.aug_type, )
def load_demo_data(config): """ Loads the demo data. Make sure that you are in the same directory where you have downloaded or cloned the deeplabcut. Parameter ---------- config : string Full path of the config.yaml file of the provided demo dataset as a string. Example -------- >>> deeplabcut.load_demo_data('config.yaml') -------- """ config = Path(config).resolve() config = str(config) transform_data(config) print("Loaded, now creating training data...") deeplabcut.create_training_dataset(config, num_shuffles=1)
def create_training_dataset(self, event): """ """ num_shuffles = self.shuffle.GetValue() config_file = auxiliaryfunctions.read_config(self.config) trainindex = self.trainingindex.GetValue() if self.userfeedback.GetStringSelection() == "Yes": userfeedback = True else: userfeedback = False if config_file.get("multianimalproject", False): deeplabcut.create_multianimaltraining_dataset( self.config, num_shuffles, Shuffles=[self.shuffle.GetValue()], net_type=self.net_choice.GetValue(), ) else: if self.model_comparison_choice.GetStringSelection() == "No": deeplabcut.create_training_dataset( self.config, num_shuffles, Shuffles=[self.shuffle.GetValue()], userfeedback=userfeedback, net_type=self.net_choice.GetValue(), augmenter_type=self.aug_choice.GetValue(), ) if self.model_comparison_choice.GetStringSelection() == "Yes": deeplabcut.create_training_model_comparison( self.config, trainindex=trainindex, num_shuffles=num_shuffles, userfeedback=userfeedback, net_types=self.net_type, augmenter_types=self.aug_type, )
dataFrame.to_csv( os.path.join(cfg['project_path'], 'labeled-data', videoname, "CollectedData_" + scorer + ".csv")) dataFrame.to_hdf(os.path.join(cfg['project_path'], 'labeled-data', videoname, "CollectedData_" + scorer + '.h5'), 'df_with_missing', format='table', mode='w') print("Plot labels...") deeplabcut.check_labels(path_config_file) print("CREATING TRAININGSET") deeplabcut.create_training_dataset(path_config_file, net_type=net_type, augmenter_type=augmenter_type) 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'] = numiter DLC_config['display_iters'] = 2 DLC_config['multi_step'] = [[0.001, numiter]] print("CHANGING training parameters to end quickly!") deeplabcut.auxiliaryfunctions.write_plainconfig(posefile, DLC_config)
index=[os.path.join('labeled-data', videoname, fn) for fn in frames]) if index == 0: dataFrame = frame else: dataFrame = pd.concat([dataFrame, frame], axis=1) dataFrame.to_csv( os.path.join(cfg['project_path'], 'labeled-data', videoname, "CollectedData_" + cfg['scorer'] + ".csv")) dataFrame.to_hdf(os.path.join(cfg['project_path'], 'labeled-data', videoname, "CollectedData_" + cfg['scorer'] + '.h5'), 'df_with_missing', format='table', mode='w') deeplabcut.create_training_dataset(configfile, Shuffles=[1]) DLC_config = deeplabcut.auxiliaryfunctions.read_plainconfig(path_train_config) DLC_config['save_iters'] = lastvalue DLC_config['display_iters'] = 1 DLC_config['multi_step'] = [[0.001, lastvalue]] DLC_config['init_weights'] = pretrainedDeeperCutweights.split('.index')[0] deeplabcut.auxiliaryfunctions.write_plainconfig(path_train_config, DLC_config) deeplabcut.train_network(configfile, shuffle=1) print("Adding bodypart!") cfg = deeplabcut.auxiliaryfunctions.read_config(configfile) cfg['bodyparts'] = [ 'ankle1', 'knee1', 'hip1', 'hip2', 'knee2', 'ankle2', 'wrist1', 'elbow1', 'shoulder1', 'shoulder2', 'elbow2', 'wrist2', 'chin', 'forehead', 'plus1more'
def create_training_dataset(self): deeplabcut.create_training_dataset(self.full_config_path())
os.path.join( cfg["project_path"], "labeled-data", vname, "CollectedData_" + scorer + ".h5", ), "df_with_missing", format="table", mode="w", ) print("MERGING") deeplabcut.merge_datasets(config_path) # iteration + 1 print("CREATING TRAININGSET updated training set") deeplabcut.create_training_dataset(config_path, net_type=NET) 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("Analyzing video with auto_track....") deeplabcut.analyze_videos( config_path, [new_video_path], save_as_csv=True,
import os from pathlib import Path os.environ['DLClight']='True' import deeplabcut # Loading example data set path_config_file = os.path.join(os.getcwd(),'openfield-Pranav-2018-10-30/config.yaml') cfg=deeplabcut.auxiliaryfunctions.read_config(path_config_file) deeplabcut.load_demo_data(path_config_file) #create one split and make Shuffle 5 and 6 have the same split. trainIndexes, testIndexes=deeplabcut.mergeandsplit(path_config_file,trainindex=0,uniform=True) deeplabcut.create_training_dataset(path_config_file,Shuffles=[2],trainIndexes=trainIndexes,testIndexes=testIndexes) deeplabcut.create_training_dataset(path_config_file,Shuffles=[3],trainIndexes=trainIndexes,testIndexes=testIndexes) for shuffle in [2,3]: if shuffle==3: 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(shuffle),'train/pose_cfg.yaml') DLC_config=deeplabcut.auxiliaryfunctions.read_plainconfig(posefile) DLC_config['dataset_type']='tensorpack' deeplabcut.auxiliaryfunctions.write_plainconfig(posefile,DLC_config) #otherwise default... print("TRAIN NETWORK", shuffle) deeplabcut.train_network(path_config_file, shuffle=shuffle,saveiters=10000,displayiters=200,maxiters=100000,max_snapshots_to_keep=11) print("EVALUATE")
import os os.environ["DLClight"] = "True" import deeplabcut as dlc 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") dlc.create_training_dataset(config_path, augmenter_type='imgaug') #import tensorflow as tf #hello = tf.constant('Hello, TensorFlow!') #sess = tf.Session() #print(sess.run(hello))
for project in Projects[model]: projectpath=os.path.join(prefix,project) config=os.path.join(projectpath,'config.yaml') 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
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")
print('New config_path: "{}"'.format(config_path)) # Edit the config file to represent your tracking # DLC Cropping deeplabcut.extract_frames(config_path, mode='automatic', algo='uniform', crop=False, userfeedback=False) deeplabcut.label_frames(config_path) deeplabcut.check_labels(config_path) deeplabcut.create_training_dataset(config_path) # 0 is the GPU number, see in nvidia-smi deeplabcut.train_network(config_path, gputouse=0, saveiters=25000, maxiters=250000) deeplabcut.evaluate_network(config_path, plotting=False) # DLC estimation deeplabcut.analyze_videos(config_path, training_videos, gputouse=0, save_as_csv=True, destfolder=labeled_csv_path)
cfg = deeplabcut.auxiliaryfunctions.read_config(path_config_file) maxiters = 10000 deeplabcut.load_demo_data(path_config_file) ## Create one split and make Shuffle 2 and 3 have the same split. ###Note that the new function in DLC 2.1 simplifies network/augmentation comparisons greatly: deeplabcut.create_training_model_comparison( path_config_file, num_shuffles=1, net_types=["resnet_50", "efficientnet-b3"], augmenter_types=["imgaug", "scalecrop", "tensorpack"], ) ## here is an "old way" to do this """ trainIndices, testIndices=deeplabcut.mergeandsplit(path_config_file,trainindex=0,uniform=True) deeplabcut.create_training_dataset(path_config_file,Shuffles=[2],trainIndices=trainIndices,testIndices=testIndices) deeplabcut.create_training_dataset(path_config_file,Shuffles=[3],trainIndices=trainIndices,testIndices=testIndices) for shuffle in [2,3]: if shuffle==3: 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(shuffle),'train/pose_cfg.yaml') DLC_config=deeplabcut.auxiliaryfunctions.read_plainconfig(posefile) DLC_config['dataset_type']='tensorpack' deeplabcut.auxiliaryfunctions.write_plainconfig(posefile,DLC_config) """ for shuffle in 1 + np.arange(6): posefile, _, _ = deeplabcut.return_train_network_path(path_config_file,
import os from pathlib import Path os.environ['DLClight'] = 'True' import deeplabcut # Loading example data set path_config_file = os.path.join(os.getcwd(), 'openfield-Pranav-2018-10-30/config.yaml') cfg = deeplabcut.auxiliaryfunctions.read_config(path_config_file) deeplabcut.load_demo_data(path_config_file) ##create one split and make Shuffle 2 and 3 have the same split. ''' trainIndexes, testIndexes=deeplabcut.mergeandsplit(path_config_file,trainindex=0,uniform=True) deeplabcut.create_training_dataset(path_config_file,Shuffles=[2],trainIndexes=trainIndexes,testIndexes=testIndexes) deeplabcut.create_training_dataset(path_config_file,Shuffles=[3],trainIndexes=trainIndexes,testIndexes=testIndexes) for shuffle in [2,3]: if shuffle==3: 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(shuffle),'train/pose_cfg.yaml') DLC_config=deeplabcut.auxiliaryfunctions.read_plainconfig(posefile) DLC_config['dataset_type']='tensorpack' deeplabcut.auxiliaryfunctions.write_plainconfig(posefile,DLC_config) ''' ###Note that the new function in DLC 2.1 does that much easier... deeplabcut.create_training_model_comparison( path_config_file,
(stage 4) and adjust the location of the labels. """ # ------------------------------------------------------------------ # Stage VI: creation of a training dataset # Step 8 # ------------------------------------------------------------------ """ CRITICAL Combining the labeled datasets from all the videos and splitting them will create train and test datasets. The training data will be used to train the network, whereas the test dataset will be used to test the generalization of the network (during evaluation). The function create_training_dataset performs these steps. """ if "step_8" in stages_to_run or "create_training_dataset" in stages_to_run: deeplabcut.create_training_dataset(config_path, num_shuffles=1, Shuffles=None, windows2linux=False, userfeedback=False, trainIndexes=None, testIndexes=None, net_type=None, augmenter_type=None) """ The set of arguments in the function will shuffle the combined labeled dataset and split it to create a train and a test set. The subdirectory with the suffix ‘iteration-#’ under the directory ‘training-datasets’ stores the dataset and meta information, where the ‘#’ is the value of the iteration variable stored in the project’s configuration file (this number keeps track of how often the dataset is refined; see Stage X). If you wish to benchmark the performance of DeepLabCut, create multiple splits by specifying an integer value in the num_shuffles parameter. Each iteration of the creation of a training dataset will create a .mat file, which contains the address of the images as well as the target postures, and a .pickle file, which contains the meta information about the training dataset. This step also creates a directory for the model, including two subdirectories within ‘dlc-models’ called ‘test’ and ‘train’, each of which has a configuration file called pose_cfg.yaml. Specifically, you can edit the pose_cfg.yaml within the ‘train’ subdirectory before starting the training. These configuration files contain meta information to configure feature
copy_videos=False) # if the project was already created, run this config_path = 'path to yaml file' # extract frame dlc.extract_frames(config_path, 'automatic', 'kmeans') # label the data dlc.label_frames(config_path) # check labels #dlc.check_labels(config_path) # creating training dataset dlc.create_training_dataset(config_path, num_shuffles=1) # start training dlc.train_network(config_path) # evaluate the trained network #dlc.evaluate_network(config_path, plotting=True) full_path_to_videos = [] root = 'add your path' for path in videos: full_path_to_videos.append(root + '/' + path) # video analysis and plotting results dlc.analyze_videos(config_path, full_path_to_videos,
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)
columnindex = pd.MultiIndex.from_product([[scorer], [bodypart], ['x', 'y']],names=['scorer', 'bodyparts', 'coords']) frame = pd.DataFrame(100+np.ones((len(frames),2))*50*index, columns = columnindex, index = [os.path.join('labeled-data',videoname,fn) for fn in frames]) if index==0: dataFrame=frame else: dataFrame = pd.concat([dataFrame, frame],axis=1) dataFrame.to_csv(os.path.join(cfg['project_path'],'labeled-data',videoname,"CollectedData_" + scorer + ".csv")) dataFrame.to_hdf(os.path.join(cfg['project_path'],'labeled-data',videoname,"CollectedData_" + scorer + '.h5'),'df_with_missing',format='table', mode='w') print("Plot labels...") deeplabcut.check_labels(path_config_file) print("CREATING TRAININGSET") deeplabcut.create_training_dataset(path_config_file) 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")
cfg["project_path"], "labeled-data", vname, "CollectedData_" + scorer + ".h5", ), "df_with_missing", format="table", mode="w", ) """ print("MERGING") deeplabcut.merge_datasets(config_path) # iteration + 1 print('CREATING TRAININGSET UPDATED TRAINING SET') deeplabcut.create_training_dataset(config_path, Shuffles=[3], net_type=NET) print('TRAINING NETWORK...') deeplabcut.train_network(config_path, shuffle=3, maxiters=N_ITER) print("NETWORK TRAINED!") print('EVALUATING NETWORK...') deeplabcut.evaluate_network(config_path, Shuffles=[3], plotting=True) print('NETWORK EVALUATED....') print('ANALYZING VIDEO WITH AUTO_TRACK....') deeplabcut.analyze_videos( config_path, [new_video_path], shuffle=3,
dataFrame.to_csv( os.path.join(cfg['project_path'], 'labeled-data', videoname, "CollectedData_" + scorer + ".csv")) dataFrame.to_hdf(os.path.join(cfg['project_path'], 'labeled-data', videoname, "CollectedData_" + scorer + '.h5'), 'df_with_missing', format='table', mode='w') print("Plot labels...") dlc.check_labels(path_config_file) print("CREATING TRAININGSET") dlc.create_training_dataset(path_config_file, net_type=net_type, augmenter_type=augmenter_type) 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 = 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)
deeplabcut.extract_frames(path_config_file, 'automatic', 'uniform', crop=False) else: print("frames have been extracted already!!") if not frameslabeled: print("Need labeled frames") deeplabcut.label_frames(path_config_file) # label frames else: print("Frames have been labeled already!!!!!") if not traindatasetexist: # create training dataset if not exist deeplabcut.check_labels(path_config_file) deeplabcut.create_training_dataset(path_config_file, num_shuffles=1) else: print("Training dataset exists") if tag_train: # train data argments, saveiters, displayiters, maxiters are the same as in pose_cfg.yaml, here are for visualization saveiters = 10000 displayiters = 1000 maxiters = 100000 train_start = time.time() deeplabcut.train_network(path_config_file, shuffle=1, saveiters=saveiters, displayiters=displayiters, maxiters=maxiters) train_end = time.time()
%matplotlib inline path_config_file = '/dlc-blinking/whisk/whisk_only-Guy-2019-02-01/config.yaml' # Enter the path of the config file that was just created from the above step (check the folder) deeplabcut.extract_frames(path_config_file,'automatic','uniform',crop=True, checkcropping=True, opencv=False) #there are other ways to grab frames, such as by clustering 'kmeans'; please see the paper. # changed the cropping dimensions in the config.yaml file %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)
"labeled-data", videoname, "CollectedData_" + scorer + ".h5", ), "df_with_missing", format="table", mode="w", ) print("Plot labels...") deeplabcut.check_labels(path_config_file) print("CREATING TRAININGSET") deeplabcut.create_training_dataset( path_config_file, net_type=net_type, augmenter_type=augmenter_type ) 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", )