コード例 #1
0
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))
コード例 #2
0
 def check_labelInd(self, event):
     dlg = wx.MessageDialog(
         None,
         "This will now plot the labeled frames afer you have finished labeling!",
     )
     result = dlg.ShowModal()
     deeplabcut.check_labels(self.config, visualizeindividuals=True)
コード例 #3
0
 def check_labelF(self, event):
     dlg = wx.MessageDialog(
         None,
         "This will now plot the labeled frames afer you have finished labeling!"
     )
     result = dlg.ShowModal()
     deeplabcut.check_labels(self.config)
コード例 #4
0
def DLCLabel(config_path):
    """
    Utility to facilitate labelling DeepLabCut frames from the MesoNet GUI.

    :param config_path: (required) The path to a DeepLabCut configuration file (.yaml).
    """
    deeplabcut.extract_frames(config_path, crop=False)
    deeplabcut.label_frames(config_path)
    deeplabcut.check_labels(config_path)
コード例 #5
0
def create_dlc_project(name, author, files, props):
    config_path = deeplabcut.create_new_project(name, author, files)
    update_dlc_config(config_path, props)
    deeplabcut.extract_frames(config_path,
                              'automatic',
                              'kmeans',
                              userfeedback=False)
    deeplabcut.label_frames(config_path)
    deeplabcut.check_labels(config_path)

    return config_path.replace('/config.yaml', '')
コード例 #6
0
def check_labels(projectId):
    config_path = projectRepository.find_one({'_id': ObjectId(projectId)
                                              })['config_path']
    err_folders = deeplabcut.check_labels(config_path)
    if (len(err_folders) > 0):
        return "Some labels were incorrect", 400
    else:
        return "OK", 200
コード例 #7
0
video=['videos/animal_3_video_2_150fps_correct.mp4', 'videos/crush_19_01_07_animal_3.mp4'] # Enter the paths of your videos you want to grab frames from.

deeplabcut.create_new_project(task,experimenter,video, working_directory='dlc-blinking/whisk',copy_videos=True) #change the working directory to where you want the folders created.

%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',
コード例 #8
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...")

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
コード例 #9
0
ファイル: testscript.py プロジェクト: zanecn/DeepLabCut
frames=os.listdir(os.path.join(cfg['project_path'],'labeled-data',videoname))
#As this next step is manual, we update the labels by putting them on the diagonal (fixed for all frames)
for index,bodypart in enumerate(cfg['bodyparts']):
        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")
コード例 #10
0
ファイル: dlc_wrapper.py プロジェクト: jabrantley/dlc_wrapper
 def check_labels(self):
     deeplabcut.check_labels(self.full_config_path())
コード例 #11
0
        os.path.join(rel_folder, image)
        for image in auxiliaryfunctions.grab_files_in_folder(image_folder, "png")
    ]
    fake_data = np.tile(
        np.repeat(50 * np.arange(len(animals_id)) + 50, 2), (len(index), 1)
    )
    df = pd.DataFrame(fake_data, index=index, columns=columns)
    output_path = os.path.join(image_folder, f"CollectedData_{SCORER}.csv")
    df.to_csv(output_path)
    df.to_hdf(
        output_path.replace("csv", "h5"), "df_with_missing", format="table", mode="w"
    )
    print("Artificial data created.")

    print("Checking labels...")
    deeplabcut.check_labels(config_path, draw_skeleton=False)
    print("Labels checked.")

    print("Creating train dataset...")
    deeplabcut.create_multianimaltraining_dataset(
        config_path, net_type=NET, crop_size=(200, 200)
    )
    print("Train dataset created.")

    # Check the training image paths are correctly stored as arrays of strings
    trainingsetfolder = auxiliaryfunctions.GetTrainingSetFolder(cfg)
    datafile, _ = auxiliaryfunctions.GetDataandMetaDataFilenames(
        trainingsetfolder,
        0.8,
        1,
        cfg,
コード例 #12
0
def create_labels(task,
                  date,
                  overwrite_flag=False,
                  check_labels=False,
                  verbose=False,
                  training_fraction=0.95):
    from deepgraphpose.utils_data import local_extract_frames
    from deepgraphpose.PoseDataLoader import DataLoader
    import deeplabcut

    #%%
    #task = 'reach'
    #date ='2030-12-12'
    #%%
    data_info = DataLoader(task)
    # %%
    videoname_ = data_info.vname + data_info.videotype
    scorer = data_info.scorer

    # %%
    assert data_info.raw_data_dir.exists()
    if not data_info.model_data_dir.exists():
        os.makedirs(data_info.model_data_dir)
    # %%
    # The data should be in raw dats dir
    videofile_path = data_info.raw_data_dir / videoname_
    assert videofile_path.exists()
    videos = [videofile_path]

    # %%
    wd = Path(data_info.model_data_dir).resolve()
    project_name = "{pn}-{exp}-{date}".format(pn=task, exp=scorer, date=date)
    project_path = wd / project_name

    print("Project_path: {}\n".format(project_path))
    # %%
    if not project_path.exists():
        path_config_file = deeplabcut.create_new_project(
            project=task,
            experimenter=scorer,
            videos=videos,  # list of video
            working_directory=str(data_info.model_data_dir),
            copy_videos=True,
            videotype=data_info.videotype,
            date=date,
        )
        print("\n Created  new project")

    else:
        if not overwrite_flag:
            raise Exception(
                " Your config file exists, Turn on overwriting flag Goodbye!")
        else:
            path_config_file = project_path / "config.yaml"
            print("\nYou are overwritting your local file!!")
    # %% set config path
    cfg = deeplabcut.auxiliaryfunctions.read_config(str(path_config_file))

    #print("{" + "\n".join("{!r}: {!r},".format(k, v) for k, v in cfg.items()) + "}")
    #%%
    cfg["move2corner"] = data_info.move2corner
    cfg["numframes2pick"] = data_info.numframes2pick
    cfg["TrainingFraction"] = [training_fraction]  #data_info.TrainingFraction
    cfg["cropping"] = data_info.cropping
    cfg["dotsize"] = data_info.dotsize
    cfg["batch_size"] = data_info.batch_size
    # cfg['pos_dist_thresh'] # should be 4 or os for single pixel but need to be large enough to recover all pixels
    cfg["skeleton"] = data_info.skeleton
    cfg["bodyparts"] = data_info.bodyparts
    # %%
    # iteration
    # snapshot index -1
    # rewrite config file
    deeplabcut.auxiliaryfunctions.write_config(path_config_file, cfg)

    # %%
    # Load curated datasets
    # #### pick curated datasets instead of automatic
    #print("Loading labeled frames and indices")
    frames_index_keep = data_info.get_labeled_frames()

    # %%
    #print("CREATING-SOME LABELS FOR THE FRAMES")
    # directory called on frame extractor
    frames_dir = project_path / "labeled-data" / data_info.vname
    frames = os.listdir(frames_dir)

    #print("Number of training frames {}".format(len(frames)))
    #%%
    if len(frames) == 0:
        assert len(np.unique(frames_index_keep)) >= cfg["numframes2pick"]
        frames2pick = np.sort(
            np.random.choice(frames_index_keep,
                             cfg["numframes2pick"],
                             replace=False))
        local_extract_frames(path_config_file, frames2pick)
        frames = os.listdir(frames_dir)

    #%%
    csv_file = (project_path / "labeled-data" / data_info.vname /
                Path("CollectedData_" + scorer + ".csv"))
    hdf_file = (project_path / "labeled-data" / data_info.vname /
                Path("CollectedData_" + scorer + ".h5"))

    # %%
    if not (csv_file.exists() and hdf_file.exists()):
        # num_frames = len(frames)
        # num_bodyparts = len(cfg["bodyparts"])

        # clip = VideoFileClip(str(videofile_path))
        data = np.load(str(videofile_path).split(".")[0] + ".npy",
                       allow_pickle=True)[()]
        xr = data["xr"]
        yr = data["yr"]

        # Extracts the frame number
        extract_frame_num = lambda x: int(x.split("/")[-1].split(".")[0][3:])

        # As this next step is manual, we update the labels by putting them on the diagonal (fixed for all frames)
        for index, bodypart in enumerate(cfg["bodyparts"]):
            columnindex = pd.MultiIndex.from_product(
                [[scorer], [bodypart], ["x", "y"]],
                names=["scorer", "bodyparts", "coords"],
            )
            #print(columnindex)
            frame_index_name = [
                os.path.join("labeled-data", data_info.vname, fn)
                for fn in frames
            ]
            frame_indices = np.asarray(
                [extract_frame_num(frame_) for frame_ in frame_index_name])

            frame = pd.DataFrame(
                np.vstack((xr[index, frame_indices], yr[index,
                                                        frame_indices])).T,
                columns=columnindex,
                index=frame_index_name,
            )
            if index == 0:
                dataFrame = frame
            else:
                dataFrame = pd.concat([dataFrame, frame], axis=1)

        dataFrame.to_csv(str(csv_file))
        dataFrame.to_hdf(str(hdf_file),
                         "df_with_missing",
                         format="table",
                         mode="w")

        #print("Plot labels...")
        if check_labels:
            deeplabcut.check_labels(path_config_file)
    else:
        print('FIle exists')
    return path_config_file
コード例 #13
0
ファイル: home.py プロジェクト: jessegroot/DeepLabCut
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")
コード例 #14
0
        the user can call the function label_frames and will be asked if he or she wants to display only
        the new labels or all labels before loading the frames. Saving the labels after all the images are
        labeled will append the new labels to the existing labeled dataset.
        """

    # ------------------------------------------------------------------
    # Stage V: (optional) checking of annotated frames
    # Step 7
    # ------------------------------------------------------------------
    if "step_7" in stages_to_run or "checking_annotated_frames" in stages_to_run:
        """
        CRITICAL Checking whether the labels were created and stored correctly is beneficial for training, as
        labeling is one of the most critical parts of a supervised learning algorithm such as DeepLabCut.
        Nevertheless, this section is optional.
        """
        deeplabcut.check_labels(config_path)
        """
        For each directory in ‘labeled-data’, this function creates a subdirectory with ‘labeled’ as a suffix.
        These directories contain the frames plotted with the annotated body parts. You can then double-
        check whether the body parts are labeled correctly. If they are not correct, use the labeling GUI
        (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
コード例 #15
0
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)
コード例 #16
0
 def clicked_check():
     deeplabcut.check_labels(self.path_config_file)
コード例 #17
0
def create_labels_md(config_path,
                     video_path,
                     scorer,
                     overwrite_flag=False,
                     check_labels=False,
                     verbose=False):
    from deepgraphpose.utils_data import local_extract_frames_md
    from deeplabcut.utils import auxiliaryfunctions

    cfg = auxiliaryfunctions.read_config(config_path)

    project_path = Path(str(Path(config_path).parent))
    numframes2pick = []
    for video in video_path:
        print(video)
        try:
            # For windows os.path.realpath does not work and does not link to the real video. [old: rel_video_path = os.path.realpath(video)]
            rel_video_path = str(Path.resolve(Path(video)))
        except:
            rel_video_path = os.readlink(str(video))

        rel_video_path_parent = Path(
            rel_video_path).parent / 'dlc_labels_check.npy'
        videoname = Path(rel_video_path).name
        frames_index_keep = np.load(str(rel_video_path_parent),
                                    allow_pickle=True)[()]['keep']

        print("Number of training frames {} for video {}".format(
            len(frames_index_keep), videoname))

        #%
        if cfg["numframes2pick"] is not None:
            assert len(np.unique(frames_index_keep)) >= cfg["numframes2pick"]
            frames2pick = np.sort(
                np.random.choice(frames_index_keep,
                                 cfg["numframes2pick"],
                                 replace=False))
        else:
            frames2pick = frames_index_keep
        numframes2pick += [len(frames2pick)]

        # extract frames for one video
        local_extract_frames_md(config_path, frames2pick, video)

        #%
        vname = videoname.split(".")[0]
        csv_file = (project_path / "labeled-data" / vname /
                    Path("CollectedData_" + scorer + ".csv"))
        hdf_file = (project_path / "labeled-data" / vname /
                    Path("CollectedData_" + scorer + ".h5"))

        frames_dir = project_path / "labeled-data" / vname
        frames = os.listdir(frames_dir)

        # %
        if not (csv_file.exists() and hdf_file.exists()):
            # num_frames = len(frames)
            # num_bodyparts = len(cfg["bodyparts"])

            # clip = VideoFileClip(str(videofile_path))
            data = np.load(str(rel_video_path).split(".")[0] + ".npy",
                           allow_pickle=True)[()]
            xr = data["xr"]
            yr = data["yr"]

            # Extracts the frame number
            extract_frame_num = lambda x: int(
                x.split("/")[-1].split(".")[0][3:])

            # As this next step is manual, we update the labels by putting them on the diagonal (fixed for all frames)
            for index, bodypart in enumerate(cfg["bodyparts"]):
                columnindex = pd.MultiIndex.from_product(
                    [[scorer], [bodypart], ["x", "y"]],
                    names=["scorer", "bodyparts", "coords"],
                )
                #print(columnindex)
                frame_index_name = [
                    os.path.join("labeled-data", vname, fn) for fn in frames
                ]
                frame_indices = np.asarray(
                    [extract_frame_num(frame_) for frame_ in frame_index_name])

                frame = pd.DataFrame(
                    np.vstack((xr[index, frame_indices], yr[index,
                                                            frame_indices])).T,
                    columns=columnindex,
                    index=frame_index_name,
                )
                if index == 0:
                    dataFrame = frame
                else:
                    dataFrame = pd.concat([dataFrame, frame], axis=1)

            dataFrame.to_csv(str(csv_file))
            dataFrame.to_hdf(str(hdf_file),
                             "df_with_missing",
                             format="table",
                             mode="w")

            #print("Plot labels...")
            if check_labels:
                deeplabcut.check_labels(config_path)
        else:
            print('FIle exists')
    #    return path_config_file

    return numframes2pick
コード例 #18
0
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)
コード例 #19
0
ファイル: random.py プロジェクト: tochenan/data_analyais_DLC
import deeplabcut
import tensorflow as tf

'''deeplabcut.create_new_project('1_2_4_chamber','MC',['C:\\Users\\analysis\\Desktop\\Mingran\\analysed_trimmed_data\\BRAC3477.2b\\05_08_2019\\BRAC34772b top_left 05_08_2019 12_40_54 1_trimmed.mp4'],
working_directory='C:\\Users\\analysis\\Desktop')'''

videopath =['C:\\Users\\analysis\\Desktop\\Mingran\\analysed_trimmed_data\\BRAC3477.2b\\13_08_2019\\BRAC34772b top_right 13_08_2019 14_39_52 2_trimmed.mp4',
'C:\\Users\\analysis\\Desktop\\Mingran\\analysed_trimmed_data\\BRAC3583.3e\\23_07_2019\\BRAC35833e 23_07_2019 13_35_06 4_trimmed.mp4',
'C:\\Users\\analysis\\Desktop\\Mingran\\analysed_trimmed_data\\BRAC3616.3c\\23_07_2019\\BRAC36163c 23_07_2019 12_41_11 3_trimmed.mp4']
config = 'C:\\Users\\analysis\\Desktop\\1_2_4_chamber-MC-2019-08-23\\config.yaml'


'''deeplabcut.add_new_videos(config,videopath)
deeplabcut.extract_frames(config,'automatic','kmeans')'''
deeplabcut.label_frames(config)

deeplabcut.check_labels(config)
deeplabcut.create_training_dataset(config)
deeplabcut.train_network(config)


deeplabcut.extract_outlier_frames(config,videopath)
deeplabcut.refine_labels(config)
deeplabcut.merge_datasets(config)
deeplabcut.create_training_dataset(config)
deeplabcut.train_network(config)
コード例 #20
0
deeplabcut.extract_frames(path_config, 'automatic', 'kmeans')


# In[9]:


deeplabcut.label_frames(path_config, Screens=2)


# Now, a GUI will open for you to label the body part you want

# 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]:
コード例 #21
0
ファイル: label.py プロジェクト: hicsail/neuro-circuit
def label(path_config_file):
    dlc.label_frames(path_config_file)
    print("Checking labels")
    dlc.check_labels(path_config_file)