Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
For details on TensorPack check out:
A Neural Net Training Interface on TensorFlow, with focus on speed + flexibility
https://github.com/tensorpack/tensorpack
"""
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)
"""
# Importing the toolbox (takes several seconds)
import deeplabcut
import os
from pathlib import Path
# 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=11 #>> imageio functions!
shuffle=12
deeplabcut.create_training_dataset(path_config_file,Shuffles=[shuffle])
cfg=deeplabcut.auxiliaryfunctions.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(shuffle),'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.005,15001]]
deeplabcut.auxiliaryfunctions.write_plainconfig(posefile,DLC_config)
print("TRAIN NETWORK")
deeplabcut.train_network(path_config_file, shuffle=shuffle,saveiters=15000,displayiters=1000,max_snapshots_to_keep=15)
print("EVALUATE")
deeplabcut.evaluate_network(path_config_file, Shuffles=[shuffle],plotting=True)
print("RELABELING")
DF=pd.read_hdf(file,'df_with_missing')
DLCscorer=np.unique(DF.columns.get_level_values(0))[0]
DF.columns.set_levels([scorer.replace(DLCscorer,scorer)],level=0,inplace=True)
DF =DF.drop('likelihood',axis=1,level=2)
DF.to_csv(os.path.join(cfg['project_path'],'labeled-data',vname,"CollectedData_" + scorer + ".csv"))
DF.to_hdf(os.path.join(cfg['project_path'],'labeled-data',vname,"CollectedData_" + scorer + '.h5'),'df_with_missing',format='table', mode='w')
print("MERGING")
deeplabcut.merge_datasets(path_config_file)
print("CREATING TRAININGSET")
deeplabcut.create_training_dataset(path_config_file,net_type=net_type,augmenter_type=augmenter_type2)
cfg=deeplabcut.auxiliaryfunctions.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=deeplabcut.auxiliaryfunctions.read_plainconfig(posefile)
DLC_config['save_iters']=numiter
DLC_config['display_iters']=1
DLC_config['multi_step']=[[0.001,numiter]]
print("CHANGING training parameters to end quickly!")
deeplabcut.auxiliaryfunctions.write_config(posefile,DLC_config)
print("TRAIN")
deeplabcut.train_network(path_config_file)
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])
newvideo2=deeplabcut.ShortenVideo(video[0],start='00:00:00',stop='00:00:00.4',outsuffix='short2',outpath=os.path.join(cfg['project_path'],'videos'))
shutil.copyfile(video[0], output1)
shutil.copyfile(video[0], output2)
'''
# checking if 2d test project is available
try:
config = glob.glob(os.path.join(basepath,'TEST*','config.yaml'))[-1]
except:
raise("Please run the testscript.py first before testing for 3d")
dfolder=None
print("CREATING 3-D PROJECT")
path_config_file=deeplabcut.create_new_project_3d(task,scorer,num_cameras)
try:
cfg=deeplabcut.auxiliaryfunctions.read_config(path_config_file)
cfg['config_file_camera-1']=config
cfg['shuffle_camera-1']=1
cfg['config_file_camera-2']=config
cfg['shuffle_camera-2']=2
cfg['skeleton']=[['bodypart1','bodypart2'],['objectA','bodypart3']]
deeplabcut.auxiliaryfunctions.write_config_3d(path_config_file,cfg)
except:
raise("Please delete the project and re-try.") # otherwise the cfg is an empty array!
'''
# Creating the name of the project
date = dt.today()
month = date.strftime("%B")
day = date.day
import pandas as pd
import numpy as np
print("Imported DLC!")
basepath=os.path.dirname(os.path.abspath('testscript.py'))
videoname='reachingvideo1'
video=[os.path.join(basepath,'Reaching-Mackenzie-2018-08-30','videos',videoname+'.avi')]
#to test destination folder:
#dfolder=basepath
dfolder=None
print("CREATING PROJECT")
path_config_file=deeplabcut.create_new_project(task,scorer,video,copy_videos=True)
cfg=deeplabcut.auxiliaryfunctions.read_config(path_config_file)
cfg['numframes2pick']=5
cfg['pcutoff']=0.01
cfg['TrainingFraction']=[.8]
cfg['default_net_type']='resnet_152'
deeplabcut.auxiliaryfunctions.write_config(path_config_file,cfg)
print("EXTRACTING FRAMES")
deeplabcut.extract_frames(path_config_file,mode='automatic',userfeedback=False)
print("CREATING-SOME LABELS FOR THE FRAMES")
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])