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 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)
Beispiel #3
0
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)
Beispiel #4
0
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 train_network(self,event):

        shuffle = self.shuffles.GetValue()
        trainingsetindex = self.trainingindex.GetValue()
        max_snapshots_to_keep = self.snapshots.GetValue()
        displayiters = self.display_iters.GetValue()
        saveiters = self.save_iters.GetValue()
        maxiters = self.max_iters.GetValue()

        deeplabcut.train_network(self.config,shuffle,
                                 trainingsetindex,gputouse=None,
                                 max_snapshots_to_keep=max_snapshots_to_keep,
                                 autotune=None,
                                 displayiters=displayiters,
                                 saveiters=saveiters,
                                 maxiters=maxiters)
Beispiel #6
0
    def train_network(self, event):
        if self.shuffles.Children:
            shuffle = int(self.shuffles.Children[0].GetValue())
        else:
            shuffle = int(self.shuffles.GetValue())

        if self.trainingindex.Children:
            trainingsetindex = int(self.trainingindex.Children[0].GetValue())
        else:
            trainingsetindex = int(self.trainingindex.GetValue())

        if self.snapshots.Children:
            max_snapshots_to_keep = int(self.snapshots.Children[0].GetValue())
        else:
            max_snapshots_to_keep = int(self.snapshots.GetValue())

        if self.display_iters.Children:
            displayiters = int(self.display_iters.Children[0].GetValue())
        else:
            displayiters = int(self.display_iters.GetValue())

        if self.save_iters.Children:
            saveiters = int(self.save_iters.Children[0].GetValue())
        else:
            saveiters = int(self.save_iters.GetValue())

        if self.max_iters.Children:
            maxiters = int(self.max_iters.Children[0].GetValue())
        else:
            maxiters = int(self.max_iters.GetValue())

        deeplabcut.train_network(
            self.config,
            shuffle,
            trainingsetindex,
            gputouse=None,
            max_snapshots_to_keep=max_snapshots_to_keep,
            autotune=None,
            displayiters=displayiters,
            saveiters=saveiters,
            maxiters=maxiters,
        )
    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
    
    print("DONE WITH ", project," resetting to original path")
)

path_config = '/home/weertman/Documents/Theresa_FHL/Armina_DeepLabCut/Arimina_model_1/'
if os.path.exists(path_config) != True:
    os.mkdir(path_config)

path_config = deeplabcut.create_new_project(project,
                                            experimenter,
                                            videos,
                                            copy_videos=True,
                                            working_directory=path_config)

path_config = '/home/weertman/Documents/Theresa_FHL/Armina_DeepLabCut/Arimina_model_1/Armina_model_1-Theresa-2020-07-01/config.yaml'
'''
deeplabcut.extract_frames(path_config,
                          mode = 'automatic',   
                          algo = 'uniform',
                          userfeedback = False,
                          crop = False)
'''
deeplabcut.label_frames(path_config)

deeplabcut.create_training_dataset(path_config)
deeplabcut.train_network(path_config, maxiters=200000, displayiters=10)
deeplabcut.evaluate_network(path_config)

deeplabcut.analyze_time_lapse_frames(
    path_config,
    '/home/weertman/Documents/Theresa_FHL/Armina_DeepLabCut/left_test_images/',
    save_as_csv=True)
Beispiel #9
0
def write_config(configname,cfg):
    with open(str(configname), 'w') as ymlfile:
                yaml.dump(cfg, ymlfile,default_flow_style=False)

# Loading example data set 
path_config_file = os.path.join(os.getcwd(),'openfield-Pranav-2018-10-30/config.yaml')
deeplabcut.load_demo_data(path_config_file)

cfg=read_config(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=read_config(posefile)
DLC_config['save_iters']=10
DLC_config['display_iters']=2
DLC_config['multi_step']=[[0.005,15001]]
write_config(posefile,DLC_config)

print("TRAIN NETWORK")

deeplabcut.train_network(path_config_file, shuffle=1,saveiters=15000,displayiters=100)

print("EVALUATE")
deeplabcut.evaluate_network(path_config_file,plotting=False)


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])

print("Create Labeled Video")
deeplabcut.create_labeled_video(path_config_file,[videofile_path],save_frames=True)
Beispiel #10
0
base_dlc_folder = join(const.base_save_folder, const.rat_folder, const.date_folders[date],
                       'Analysis', 'Deeplabcut')
base_projects_folder = join(base_dlc_folder, 'projects')

cropped_video_filename = join(base_dlc_folder, 'BonsaiCroping', 'Croped_video.avi')

# ----------------------------------------------------------------------------------------------------------------------
config_path = deeplabcut.create_new_project(project='V1', experimenter='', videos=[cropped_video_filename],
                                            working_directory=base_projects_folder, copy_videos=True)

# Use the line below to 'reload the existing project
config_path = join(base_projects_folder, 'V1--2019-06-30', 'config.yaml')

# Edit the config.yaml file
deeplabcut.extract_frames(config_path, 'manual')

deeplabcut.label_frames(config_path)

deeplabcut.check_labels(config_path)

deeplabcut.create_training_dataset(config_path)

deeplabcut.train_network(config_path, gputouse=1)

deeplabcut.evaluate_network(config_path, plotting=True)

deeplabcut.analyze_videos(config_path, [cropped_video_filename], gputouse=1,
                          shuffle=1, save_as_csv=False, videotype='avi')

deeplabcut.create_labeled_video(config_path, [cropped_video_filename])
        keepdeconvweights=False
    '''
    print("CREATING TRAININGSET", net_type)
    if 'resnet_50' == net_type:  #this tests the default condition...
        deeplabcut.create_training_dataset(path_config_file,
                                           Shuffles=[shuffle],
                                           augmenter_type=augmenter_type)
    else:
        deeplabcut.create_training_dataset(path_config_file,
                                           Shuffles=[shuffle],
                                           net_type=net_type,
                                           augmenter_type=augmenter_type)
    Cuttrainingschedule(path_config_file, shuffle, lastvalue=stoptrain)

    print("TRAIN")
    deeplabcut.train_network(path_config_file, shuffle=shuffle)

    print("EVALUATE")
    deeplabcut.evaluate_network(path_config_file,
                                Shuffles=[shuffle],
                                plotting=True)

    print("CUT SHORT VIDEO AND ANALYZE")
    if shuffle == 0:
        # Make super short video (so the analysis is quick!)
        newvideo = deeplabcut.ShortenVideo(video[0],
                                           start='00:00:00',
                                           stop='00:00:00.4',
                                           outsuffix='short',
                                           outpath=os.path.join(
                                               cfg['project_path'], 'videos'))
                 "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'
]
deeplabcut.auxiliaryfunctions.write_config(configfile, cfg)

print("CREATING-SOME More LABELS FOR THE FRAMES (including the new bodypart!)")
cfg = deeplabcut.auxiliaryfunctions.read_config(configfile)
frames = [
    f for f in os.listdir(
        os.path.join(cfg['project_path'], 'labeled-data', videoname))
    
    
    """

    # ------------------------------------------------------------------
    # Stage VII: training the network
    # Step 9
    # ------------------------------------------------------------------
    """
    CRITICAL It is recommended to train for thousands of iterations (typically >100,000) until the loss
    plateaus. The variables display_iters and save_iters in the pose_cfg.yaml file allow the user to
    alter how often the loss is displayed and how often the (intermediate) weights are stored.
    """
    if "step_9" in stages_to_run or "training_network" in stages_to_run:
        deeplabcut.train_network(config_path, shuffle=1, trainingsetindex=0,
                                 max_snapshots_to_keep=None, displayiters=500, saveiters=5000, maxiters=None,
                                 allow_growth=False, gputouse=0, autotune=False, keepdeconvweights=True)  # gputouse=0
    # saveiters is at 50000 by default

    """
    During training, checkpoints are stored in the subdirectory ‘train’ under the respective iteration
    directory at user-specified iterations (‘save_iters’). If you wish to restart the training from a specific
    checkpoint, specify the full path of the checkpoint for the variable init_weights in the pose_cfg.
    yaml file under the ‘train’ subdirectory before starting to train
    """

    # ------------------------------------------------------------------
    # Stage VIII: evaluation of the trained network
    # Step 10
    # ------------------------------------------------------------------
    """
Beispiel #14
0
def create_training_dataset_CLARA(config,
                                  num_shuffles=1,
                                  Shuffles=None,
                                  windows2linux=False,
                                  trainIndexes=None,
                                  testIndexes=None):
    """
    Creates a training dataset. Labels from all the extracted frames are merged into a single .h5 file.\n
    Only the videos included in the config file are used to create this dataset.\n
    
    [OPTIONAL] Use the function 'add_new_video' at any stage of the project to add more videos to the project.

    Parameter
    ----------
    config : string
        Full path of the config.yaml file as a string.

    num_shuffles : int, optional
        Number of shuffles of training dataset to create, i.e. [1,2,3] for num_shuffles=3. Default is set to 1.

    Shuffles: list of shuffles.
        Alternatively the user can also give a list of shuffles (integers!).

    windows2linux: bool.
        The annotation files contain path formated according to your operating system. If you label on windows 
        but train & evaluate on a unix system (e.g. ubunt, colab, Mac) set this variable to True to convert the paths. 
    
    Example
    --------
    >>> deeplabcut.create_training_dataset('/analysis/project/reaching-task/config.yaml',num_shuffles=1)
    Windows:
    >>> deeplabcut.create_training_dataset('C:\\Users\\Ulf\\looming-task\\config.yaml',Shuffles=[3,17,5])
    --------
    """
    from skimage import io
    import scipy.io as sio
    from deeplabcut.utils import auxiliaryfunctions, auxfun_models

    # Loading metadata from config file:
    cfg = auxiliaryfunctions.read_config(config)
    scorer = cfg['scorer']
    project_path = cfg['project_path']
    # Create path for training sets & store data there
    trainingsetfolder = auxiliaryfunctions.GetTrainingSetFolder(
        cfg)  #Path concatenation OS platform independent
    auxiliaryfunctions.attempttomakefolder(Path(
        os.path.join(project_path, str(trainingsetfolder))),
                                           recursive=True)
    """
    Merges all the h5 files for all labeled-datasets (from individual videos).
    """
    AnnotationData = None
    data_path = Path(os.path.join(project_path, 'labeled-data'))
    videos = cfg['video_sets'].keys()
    video_names = [Path(i).stem for i in videos]
    for i in video_names:
        try:
            data = pd.read_hdf((str(data_path / Path(i)) + '/CollectedData_' +
                                cfg['scorer'] + '.h5'), 'df_with_missing')
            smlData = data.dropna(how='all')
            smlKeys = list(smlData.index.values)
            smlKeyLong = list()
            for sk in smlKeys:
                smlKeyLong.append('labeled-data/' + str(Path(i)) + '/' + sk)
            smlData.index = smlKeyLong
            data = smlData
            if AnnotationData is None:
                AnnotationData = data
            else:
                AnnotationData = pd.concat([AnnotationData, data])

        except FileNotFoundError:
            print((str(data_path / Path(i)) + '/CollectedData_' +
                   cfg['scorer'] + '.h5'),
                  " not found (perhaps not annotated)")

    trainingsetfolder_full = Path(os.path.join(project_path,
                                               trainingsetfolder))
    filename = str(
        str(trainingsetfolder_full) + '/' + '/CollectedData_' + cfg['scorer'])
    AnnotationData.to_hdf(filename + '.h5', key='df_with_missing', mode='w')
    AnnotationData.to_csv(filename + '.csv')  #human readable.
    Data = AnnotationData

    Data = Data[scorer]  #extract labeled data

    #loading & linking pretrained models
    net_type = 'resnet_' + str(cfg['resnet'])
    import deeplabcut
    parent_path = Path(os.path.dirname(deeplabcut.__file__))
    defaultconfigfile = str(parent_path / 'pose_cfg.yaml')

    model_path, num_shuffles = auxfun_models.Check4weights(
        net_type, parent_path, num_shuffles)

    if Shuffles == None:
        Shuffles = range(1, num_shuffles + 1, 1)
    else:
        Shuffles = [i for i in Shuffles if isinstance(i, int)]

    bodyparts = cfg['bodyparts']
    if isinstance(bodyparts, list):
        parts = bodyparts
    else:
        parts = list()
        categories = list()
        for cat in bodyparts.keys():
            categories.append(cat)
        for key in categories:
            for ptname in bodyparts[key]:
                parts.append(ptname)
    bodyparts = parts

    TrainingFraction = cfg['TrainingFraction']
    for shuffle in Shuffles:  # Creating shuffles starting from 1
        for trainFraction in TrainingFraction:
            #trainIndexes, testIndexes = SplitTrials(range(len(Data.index)), trainFraction)
            if trainIndexes is None and testIndexes is None:
                trainIndexes, testIndexes = SplitTrials_CLARA(
                    range(len(Data.index)), trainFraction)
            else:
                print(
                    "You passed a split with the following fraction:",
                    len(trainIndexes) * 1. /
                    (len(testIndexes) + len(trainIndexes)) * 100)

            ####################################################
            # Generating data structure with labeled information & frame metadata (for deep cut)
            ####################################################

            # Make training file!
            data = []
            for jj in trainIndexes:
                H = {}
                # load image to get dimensions:
                filename = Data.index[jj]
                im = io.imread(os.path.join(cfg['project_path'], filename))
                H['image'] = filename

                if np.ndim(im) == 3:
                    H['size'] = np.array(
                        [np.shape(im)[2],
                         np.shape(im)[0],
                         np.shape(im)[1]])
                else:
                    # print "Grayscale!"
                    H['size'] = np.array([1, np.shape(im)[0], np.shape(im)[1]])

                indexjoints = 0
                joints = np.zeros((len(bodyparts), 3)) * np.nan
                for bpindex, bodypart in enumerate(bodyparts):
                    if Data[bodypart]['x'][jj] < np.shape(
                            im)[1] and Data[bodypart]['y'][jj] < np.shape(
                                im)[0]:  #are labels in image?
                        joints[indexjoints, 0] = int(bpindex)
                        joints[indexjoints, 1] = Data[bodypart]['x'][jj]
                        joints[indexjoints, 2] = Data[bodypart]['y'][jj]
                        indexjoints += 1

                joints = joints[np.where(np.prod(
                    np.isfinite(joints),
                    1))[0], :]  # drop NaN, i.e. lines for missing body parts

                assert (np.prod(np.array(joints[:, 2]) < np.shape(im)[0])
                        )  # y coordinate within image?
                assert (np.prod(np.array(joints[:, 1]) < np.shape(im)[1])
                        )  # x coordinate within image?

                H['joints'] = np.array(joints, dtype=int)
                if np.size(joints) > 0:  #exclude images without labels
                    data.append(H)

            if len(trainIndexes) > 0:
                datafilename, metadatafilename = auxiliaryfunctions.GetDataandMetaDataFilenames(
                    trainingsetfolder, trainFraction, shuffle, cfg)
                ################################################################################
                # Saving metadata (Pickle file)
                ################################################################################
                auxiliaryfunctions.SaveMetadata(
                    os.path.join(project_path, metadatafilename), data,
                    trainIndexes, testIndexes, trainFraction)
                ################################################################################
                # Saving data file (convert to training file for deeper cut (*.mat))
                ################################################################################

                DTYPE = [('image', 'O'), ('size', 'O'), ('joints', 'O')]
                MatlabData = np.array(
                    [(np.array([data[item]['image']],
                               dtype='U'), np.array([data[item]['size']]),
                      boxitintoacell_CLARA(data[item]['joints']))
                     for item in range(len(data))],
                    dtype=DTYPE)

                sio.savemat(os.path.join(project_path, datafilename),
                            {'dataset': MatlabData})

                ################################################################################
                # Creating file structure for training &
                # Test files as well as pose_yaml files (containing training and testing information)
                #################################################################################

                modelfoldername = auxiliaryfunctions.GetModelFolder(
                    trainFraction, shuffle, cfg)
                auxiliaryfunctions.attempttomakefolder(
                    Path(config).parents[0] / modelfoldername, recursive=True)
                auxiliaryfunctions.attempttomakefolder(
                    str(Path(config).parents[0] / modelfoldername) + '/' +
                    '/train')
                auxiliaryfunctions.attempttomakefolder(
                    str(Path(config).parents[0] / modelfoldername) + '/' +
                    '/test')

                path_train_config = str(
                    os.path.join(cfg['project_path'], Path(modelfoldername),
                                 'train', 'pose_cfg.yaml'))
                path_test_config = str(
                    os.path.join(cfg['project_path'], Path(modelfoldername),
                                 'test', 'pose_cfg.yaml'))
                #str(cfg['proj_path']+'/'+Path(modelfoldername) / 'test'  /  'pose_cfg.yaml')

                items2change = {
                    "dataset": datafilename,
                    "metadataset": metadatafilename,
                    "num_joints": len(bodyparts),
                    "all_joints": [[i] for i in range(len(bodyparts))],
                    "all_joints_names": [str(bpt) for bpt in bodyparts],
                    "init_weights": model_path,
                    "project_path": str(cfg['project_path']),
                    "net_type": net_type,
                    "crop": 'False'
                }
                trainingdata = MakeTrain_pose_yaml_CLARA(
                    items2change, path_train_config, defaultconfigfile)
                keys2save = [
                    "dataset", "num_joints", "all_joints", "all_joints_names",
                    "net_type", 'init_weights', 'global_scale',
                    'location_refinement', 'locref_stdev'
                ]
                MakeTest_pose_yaml_CLARA(trainingdata, keys2save,
                                         path_test_config)
    deeplabcut.train_network(config)
Beispiel #15
0
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,
                   shuffle=1,
                   save_as_csv=False,
                   videotype='.mp4')
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)
Beispiel #17
0
# 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)

deeplabcut.create_labeled_video(config_path,
                                training_videos,
                                destfolder=labeled_csv_path,
                                draw_skeleton=True)
Beispiel #18
0
import os
os.environ["DLClight"] = "True"
import sys
import deeplabcut as dlc

shuffleindex = 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.train_network(config_path,
                  shuffle=shuffleindex,
                  trainingsetindex=0,
                  max_snapshots_to_keep=None,
                  displayiters=250,
                  saveiters=None,
                  maxiters=None,
                  allow_growth=False,
                  gputouse=gputouse,
                  autotune=False,
                  keepdeconvweights=True)

print("dlc_start_training.py with the call", str(sys.argv), "is done!")
        TRAIN_SIZE, 1, cfg, cfg["project_path"]
    )
    pose_config_path = os.path.join(model_folder, "train", "pose_cfg.yaml")
    edits = {
        "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",
Beispiel #20
0
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")
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])
Beispiel #21
0
 def train_network(self):
     deeplabcut.train_network(self.full_config_path(),
                              shuffle=1,
                              gputouse=0)
Beispiel #22
0
    posefile, _, _ = deeplabcut.return_train_network_path(path_config_file,
                                                          shuffle=shuffle)

    if shuffle % 3 == 1:  # imgaug
        edits = {"rotation": 180, "motion_blur": True}
        DLC_config = deeplabcut.auxiliaryfunctions.edit_config(posefile, edits)

    elif shuffle % 3 == 0:  # Tensorpack:
        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],
Beispiel #23
0
# Loading example data set
path_config_file = os.path.join(os.getcwd(), "openfield-Pranav-2018-10-30/config.yaml")
deeplabcut.load_demo_data(path_config_file)
shuffle = 13

deeplabcut.create_training_dataset(path_config_file, Shuffles=[shuffle])
cfg = deeplabcut.auxiliaryfunctions.read_config(path_config_file)

# example how to set pose config variables:
posefile, _, _ = deeplabcut.return_train_network_path(path_config_file, shuffle=shuffle)
edits = {"save_iters": 15000, "display_iters": 1000, "multi_step": [[0.005, 15001]]}
DLC_config = deeplabcut.auxiliaryfunctions.edit_config(posefile, edits)

print("TRAIN NETWORK")
deeplabcut.train_network(path_config_file, shuffle=shuffle, max_snapshots_to_keep=3)

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
)  # ,videotype='.mp4')

print("Create Labeled Video")
deeplabcut.create_labeled_video(
    path_config_file, [videofile_path], save_frames=False, shuffle=shuffle
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)
Beispiel #25
0
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)

print("TRAIN")
deeplabcut.train_network(path_config_file)

print("EVALUATE")
deeplabcut.evaluate_network(path_config_file, plotting=True)
#deeplabcut.evaluate_network(path_config_file,plotting=True,trainingsetindex=33)
print("CUT SHORT VIDEO AND ANALYZE (with dynamic cropping!)")

# Make super short video (so the analysis is quick!)

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])
    newvideo = deeplabcut.ShortenVideo(video[0],
                                       start='00:00:00',
                                       stop='00:00:00.4',
                                       outsuffix='short',
                                       outpath=os.path.join(
Beispiel #26
0
import deeplabcut
import os

configpath = os.path.join(
    '/home/neudata/Desktop/DeepLabCut/examples/openfield-Pranav-2018-10-30/config.yaml'
)

deeplabcut.load_demo_data(configpath)

deeplabcut.train_network(configpath, maxiters=5)  #trains for 5 iterations

#manually test GUI...
deeplabcut.label_frames(configpath)
Beispiel #27
0
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)

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)
Beispiel #28
0
import os
os.environ["DLClight"] = "True"
import deeplabcut
os.system('echo $$')
os.system('echo imported')
config_path = '/dartfs-hpc/rc/home/7/f002qw7/thesis/ir_test-markt-2020-02-28/config.yaml'
os.system('echo config_path')
deeplabcut.train_network(config_path)
os.system('echo done')
Beispiel #29
0

# 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)


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)
Beispiel #30
0
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()
    train_time = train_end - train_start
    print('maxiters is %d' % maxiters)
    print('training time is %f' % train_time)
    with open(os.path.join(path_prj, 'traintime.csv'), 'w') as csv_file:
        csv_writer = csv.writer(csv_file, delimiter=',')
        csv_writer.writerow(['maxiters', 'training time'])
        csv_writer.writerow([maxiters, train_time])
else:
    print("No need to train!")

if tag_evaluation:
    deeplabcut.evaluate_network(path_config_file, plotting=True)  # evaluation