Skip to content

Commit 7d54efa

Browse files
committed
preparing settings for model merge
updated naming in experiments.py small changes to reporting of classifier.py
1 parent cfbcd2e commit 7d54efa

File tree

8 files changed

+46
-32
lines changed

8 files changed

+46
-32
lines changed

DeepLabStream.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import pandas as pd
1818
import click
1919

20-
from utils.configloader import RESOLUTION, FRAMERATE, OUT_DIR, MODEL, MULTI_CAM, STACK_FRAMES, \
20+
from utils.configloader import RESOLUTION, FRAMERATE, OUT_DIR, MODEL_NAME, MULTI_CAM, STACK_FRAMES, \
2121
ANIMALS_NUMBER, STREAMS, STREAMING_SOURCE
2222
from utils.poser import load_deeplabcut, get_pose, find_local_peaks_new, calculate_skeletons
2323
from utils.plotter import plot_bodyparts, plot_metadata_frame
@@ -722,7 +722,7 @@ def show_benchmark_statistics():
722722

723723
if benchmark_enabled:
724724
import re
725-
short_model = re.split('[-_]', MODEL)
725+
short_model = re.split('[-_]', MODEL_NAME)
726726
short_model = short_model[0] + '_' + short_model[2]
727727
np.savetxt(f'{OUT_DIR}/{short_model}_framerate_{FRAMERATE}_resolution_{RESOLUTION[0]}_{RESOLUTION[1]}.txt', np.transpose([fps_data, whole_loop_time_data]))
728728

experiments/custom/classifier.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ def bsoid_classifier_pool_run(input_q: mp.Queue,output_q: mp.Queue):
279279
output_q.put((last_prob,feature_id))
280280
end_time = time.time()
281281
print("Classification time: {:.2f} msec".format((end_time-start_time)*1000))
282+
print("Feature ID: "+ feature_id)
283+
282284
else:
283285
pass
284286

experiments/custom/experiments.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
from collections import Counter
1313
from experiments.custom.stimulus_process import ClassicProtocolProcess, SimpleProtocolProcess,Timer, ExampleProtocolProcess
1414
from experiments.custom.triggers import ScreenTrigger, RegionTrigger, OutsideTrigger, DirectionTrigger, SpeedTrigger,\
15-
SimbaThresholdBehaviorPoolTrigger, BsoidClassBehaviorTriggerPool
15+
SimbaThresholdBehaviorPoolTrigger, BsoidClassBehaviorPoolTrigger
1616
from utils.plotter import plot_triggers_response
1717
from utils.analysis import angle_between_vectors
1818
from experiments.custom.stimulation import show_visual_stim_img,laser_switch
19-
from experiments.custom.classifier import SimbaClassifier_Process, SimbaProcessPool, BsoidProcessPool
19+
from experiments.custom.classifier import SimbaProcessPool, BsoidProcessPool
2020

2121

2222
from utils.configloader import THRESHOLD, POOL_SIZE
2323

24+
2425
""" experimental classification experiment using Simba trained classifiers in a pool"""
25-
class SimbaBehaviorExperimentPool:
26+
class SimbaBehaviorPoolExperiment:
2627
"""
2728
Test experiment for Simba classification
2829
Simple class to contain all of the experiment properties and includes classification
@@ -130,7 +131,7 @@ def get_info(self):
130131

131132
""" experimental classification experiment using BSOID trained classifiers in a pool"""
132133

133-
class BsoidBehaviorExperimentPool:
134+
class BsoidBehaviorPoolExperiment:
134135
"""
135136
Test experiment for Simba classification
136137
Simple class to contain all of the experiment properties and includes classification
@@ -144,7 +145,7 @@ def __init__(self):
144145
self._process_pool = BsoidProcessPool(POOL_SIZE)
145146
#pass classifier to trigger, so that check_skeleton is the only function that passes skeleton
146147
#initiate in experiment, so that process can be started with start_experiment
147-
self._behaviortrigger = BsoidClassBehaviorTriggerPool(target_class= THRESHOLD,
148+
self._behaviortrigger = BsoidClassBehaviorPoolTrigger(target_class= THRESHOLD,
148149
class_process_pool = self._process_pool)
149150
self._event = None
150151
#is not fully utilized in this experiment but is usefull to keep for further adaptation

experiments/custom/triggers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ def get_time_window_len(self):
636636
return self._time_window_len
637637

638638

639-
class BsoidClassBehaviorTriggerPool:
639+
class BsoidClassBehaviorPoolTrigger:
640640
"""
641641
Trigger to check if animal's behavior is classified as specific motif with BSOID trained classifier.
642642
"""

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
numba
12
gpiozero
23
pigpio
34
pyserial

settings.ini

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ CAMERA_SOURCE = 0
88
#you can use "camera", "ipwebcam" or "video" to select your input source
99
STREAMING_SOURCE = video
1010

11-
[DeepLabCut]
12-
DLC_PATH = DLC_PATH
13-
MODEL = MODEL_NAME
11+
[Pose Estimation]
12+
#possible origins are: DLC
13+
MODEL_ORIGIN = ORIGIN
14+
MODEL_PATH = PATH_TO_MODEL
15+
MODEL_NAME = MODEL_NAME
16+
; Currently used to translate pose estimation into postures with bodypart names ... not necessary for classic DLC networks
17+
ALL_BODYPARTS = bp1, bp2, bp3, bp4
1418

1519
[Experiment]
1620
#Available parameters are "CUSTOM" and "BASE"
@@ -22,13 +26,14 @@ RECORD_EXP = True
2226

2327
[Classification]
2428
PATH_TO_CLASSIFIER = CLASSIFIER_PATH
25-
ALL_BODYPARTS = Ear_left_1, Ear_right_1, Nose_1,Center_1, Lateral_left_1, Lateral_right_1, Tail_base_1, Ear_left_2,Ear_right_2, Nose_2, Center_2, Lateral_left_2, Lateral_right_2, Tail_base_2
2629
#time window used for feature extraction (currently only works with 15)
2730
TIME_WINDOW = 15
2831
#number of parallel classifiers to run, this is dependent on your performance time. You need at least 1 more classifier then your average classification time.
2932
POOL_SIZE = 4
30-
#threshold to accept a classification probability as positive detection
33+
#threshold to accept a classification probability as positive detection (SIMBA + )
3134
THRESHOLD = 0.5
35+
# class/category of identified behavior to use as trigger (only used for B-SOID)
36+
TRIGGER = 5
3237
#feature extraction currently works with millimeter not px, so be sure to enter the factor (as in simba).
3338
PIXPERMM = 6.132
3439

utils/configloader.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ def get_script_path():
2727
adv_cfg_path = os.path.join(os.path.dirname(__file__), 'advanced_settings.ini')
2828
with open(adv_cfg_path) as adv_cfg_file:
2929
adv_dsc_config.read_file(adv_cfg_file)
30-
# DeepLabCut
31-
deeplabcut_config = dict(dsc_config.items('DeepLabCut'))
30+
31+
#poseestimation
32+
MODEL_ORIGIN = dsc_config['Pose Estimation'].get('MODEL_ORIGIN')
33+
MODEL_PATH = dsc_config['Pose Estimation'].get('MODEL_PATH')
34+
MODEL_NAME = dsc_config['Pose Estimation'].get('MODEL_NAME')
35+
ALL_BODYPARTS = tuple(part for part in dsc_config['Pose Estimation'].get('ALL_BODYPARTS').split(','))
3236

3337
# Streaming items
3438
try:
@@ -37,7 +41,7 @@ def get_script_path():
3741
print('Incorrect resolution in config!\n'
3842
'Using default value "RESOLUTION = 848, 480"')
3943
RESOLUTION = (848, 480)
40-
MODEL = dsc_config['Streaming'].get('MODEL')
44+
4145
FRAMERATE = dsc_config['Streaming'].getint('FRAMERATE')
4246
OUT_DIR = dsc_config['Streaming'].get('OUTPUT_DIRECTORY')
4347
CAMERA_SOURCE = dsc_config['Streaming'].get('CAMERA_SOURCE')
@@ -59,7 +63,6 @@ def get_script_path():
5963
#Classification
6064
PATH_TO_CLASSIFIER = dsc_config['Classification'].get('PATH_TO_CLASSIFIER')
6165
PIXPERMM = dsc_config['Classification'].getfloat('PIXPERMM')
62-
ALL_BODYPARTS = [str(part).strip() for part in dsc_config['Classification'].get('ALL_BODYPARTS').split(',')]
6366
THRESHOLD = dsc_config['Classification'].getfloat('THRESHOLD')
6467
POOL_SIZE = dsc_config['Classification'].getint('POOL_SIZE')
6568
TIME_WINDOW = dsc_config['Classification'].getint('TIME_WINDOW')

utils/poser.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@
1616
from scipy.ndimage.measurements import label, maximum_position
1717
from scipy.ndimage.morphology import generate_binary_structure, binary_erosion
1818
from scipy.ndimage.filters import maximum_filter
19-
from utils.configloader import deeplabcut_config
19+
from utils.configloader import MODEL_ORIGIN, MODEL_NAME, MODEL_PATH
2020

21-
MODEL = deeplabcut_config['model']
22-
DLC_PATH = deeplabcut_config['dlc_path']
2321

2422
# trying importing functions using deeplabcut module, if DLC 2 is installed correctly
25-
try:
26-
import deeplabcut.pose_estimation_tensorflow.nnet.predict as predict
27-
from deeplabcut.pose_estimation_tensorflow.config import load_config
28-
models_folder = 'pose_estimation_tensorflow/models/'
29-
# if not DLC 2 is not installed, try import from DLC 1 the old way
30-
except ImportError:
31-
# adding DLC posing path and loading modules from it
32-
sys.path.insert(0, DLC_PATH + "/pose-tensorflow")
33-
from config import load_config
34-
from nnet import predict
35-
models_folder = 'pose-tensorflow/models/'
23+
if MODEL_ORIGIN == 'DLC':
24+
try:
25+
import deeplabcut.pose_estimation_tensorflow.nnet.predict as predict
26+
from deeplabcut.pose_estimation_tensorflow.config import load_config
27+
from deeplabcut.pose_estimation_tensorflow.nnet import predict_multianimal
28+
29+
models_folder = 'pose_estimation_tensorflow/models/'
30+
# if not DLC 2 is not installed, try import from DLC 1 the old way
31+
except ImportError:
32+
# adding DLC posing path and loading modules from it
33+
sys.path.insert(0,MODEL_PATH + "/pose-tensorflow")
34+
from config import load_config
35+
from nnet import predict
36+
models_folder = 'pose-tensorflow/models/'
3637

3738

3839
def load_deeplabcut():
@@ -41,7 +42,7 @@ def load_deeplabcut():
4142
4243
:return: tuple of DeepLabCut config, TensorFlow session, inputs and outputs
4344
"""
44-
model = os.path.join(DLC_PATH, models_folder, MODEL)
45+
model = os.path.join(MODEL_PATH, models_folder, MODEL_NAME)
4546
cfg = load_config(os.path.join(model, 'test/pose_cfg.yaml'))
4647
snapshots = sorted([sn.split('.')[0] for sn in os.listdir(model + '/train/') if "index" in sn])
4748
cfg['init_weights'] = model + '/train/' + snapshots[-1]
@@ -85,6 +86,7 @@ def find_local_peaks_new(scoremap: np.ndarray, local_reference: np.ndarray, anim
8586
stride = config['stride']
8687
# filtering scoremap
8788
scoremap[scoremap < 0.1] = 0
89+
8890
for joint_num, joint in enumerate(all_joints_names):
8991
all_peaks[joint] = []
9092
# selecting the joint in scoremap and locref

0 commit comments

Comments
 (0)