Skip to content
Snippets Groups Projects
Commit 86c936e0 authored by eirikmda's avatar eirikmda
Browse files

reimplementing reformatting

parent 1eda3995
No related branches found
No related tags found
1 merge request!38Resolve "remove dead code and cleanup"
...@@ -59,10 +59,10 @@ def straighten(image): ...@@ -59,10 +59,10 @@ def straighten(image):
with _ACCESS_TF_AND_GPU_SEMA: with _ACCESS_TF_AND_GPU_SEMA:
start_time = time.time() start_time = time.time()
height, width, _ = image.shape height, width, _ = image.shape
# find the largest dimention: # find the largest dimension:
larger_dim = max(width, height) larger_dim = max(width, height)
cropped_image = image cropped_image = image
# reduce size of image by the largest dimention, if it exceeds a given constant: # reduce size of image by the largest dimension, if it exceeds a given constant:
if larger_dim > MAX_DEEPLABCUT_IMAGE_SIZE: if larger_dim > MAX_DEEPLABCUT_IMAGE_SIZE:
factor = MAX_DEEPLABCUT_IMAGE_SIZE / larger_dim factor = MAX_DEEPLABCUT_IMAGE_SIZE / larger_dim
cropped_image = cv2.resize(cropped_image, None, fx=factor, fy=factor, interpolation=cv2.INTER_CUBIC) cropped_image = cv2.resize(cropped_image, None, fx=factor, fy=factor, interpolation=cv2.INTER_CUBIC)
...@@ -73,22 +73,22 @@ def straighten(image): ...@@ -73,22 +73,22 @@ def straighten(image):
# We use the prediction from deeplabcut for the points. There are two things to take note of. # We use the prediction from deeplabcut for the points. There are two things to take note of.
# The predictions can be negative, so we ensure that negative values are set as 0 with max(0,num) # The predictions can be negative, so we ensure that negative values are set as 0 with max(0,num)
# the structure of prediction is: shape(1,12) content: [bp1_x, bp1_y, bp1_score, bp2_x, bp2_y, bp2_score # the structure of prediction is: shape(1,12) content: [bp1_x, bp1_y, bp1_score, bp2_x, bp2_y, bp2_score
# bp3_x, bp3_y, bp3_score, bp4_x, bp4_y, bp4_score] 'bp' = bodypart. # bp3_x, bp3_y, bp3_score, bp4_x, bp4_y, bp4_score] 'bp' = body_part.
# we are using the score to determine if the image is valid and skipping the score for points: # we are using the score to determine if the image is valid and skipping the score for points:
score = np.array([prediction[2], prediction[5], prediction[8], prediction[11], prediction[14], prediction[17]]) score = np.array([prediction[2], prediction[5], prediction[8], prediction[11], prediction[14], prediction[17]])
if score[0] < LEAST_DEEPLABCUT_SCORE or score[1] < LEAST_DEEPLABCUT_SCORE or \ if score[0] < LEAST_DEEPLABCUT_SCORE or score[1] < LEAST_DEEPLABCUT_SCORE or score[
score[2] < LEAST_DEEPLABCUT_SCORE or score[3] < LEAST_DEEPLABCUT_SCORE: 2] < LEAST_DEEPLABCUT_SCORE or score[3] < LEAST_DEEPLABCUT_SCORE:
print("straighten failed because the score (", score, " found is below ", LEAST_DEEPLABCUT_SCORE) print("straighten failed because the score (", score, " found is below ", LEAST_DEEPLABCUT_SCORE)
return None, None, None, None, score return None, None, None, None, score
# getting new width and height: # getting new width and height:
heigh, width, _ = cropped_image.shape height, width, _ = cropped_image.shape
# ensureing each point is between 0 and width/height: # ensuring each point is between 0 and width/height:
points_spine = np.array([ points_spine = np.array([
(min(width, max(0, int(prediction[0]))), min(heigh, max(0, int(prediction[1])))), (min(width, max(0, int(prediction[0]))), min(height, max(0, int(prediction[1])))),
(min(width, max(0, int(prediction[3]))), min(heigh, max(0, int(prediction[4])))), (min(width, max(0, int(prediction[3]))), min(height, max(0, int(prediction[4])))),
(min(width, max(0, int(prediction[6]))), min(heigh, max(0, int(prediction[7])))), (min(width, max(0, int(prediction[6]))), min(height, max(0, int(prediction[7])))),
(min(width, max(0, int(prediction[9]))), min(heigh, max(0, int(prediction[10])))) (min(width, max(0, int(prediction[9]))), min(height, max(0, int(prediction[10]))))
]) ])
# Used to check if the point 2 is closer to point 1. This will also be used later # Used to check if the point 2 is closer to point 1. This will also be used later
...@@ -112,14 +112,13 @@ def straighten(image): ...@@ -112,14 +112,13 @@ def straighten(image):
points_spine[2] = halfway_between(point1=points_spine[2], point2=points_spine[3]) points_spine[2] = halfway_between(point1=points_spine[2], point2=points_spine[3])
del dist_2_3 del dist_2_3
# Shoulder points are cannot be a part of the spine points as the spine points are sent
# Shoulder points are cannot be a part of the spine points as the spide points are sednt
# to a function that expects all the points to represent a line: # to a function that expects all the points to represent a line:
points_shoulder = np.array([ points_shoulder = np.array([
(min(width, max(0, int(prediction[12]))), min(heigh, max(0, int(prediction[13])))), (min(width, max(0, int(prediction[12]))), min(height, max(0, int(prediction[13])))),
(min(width, max(0, int(prediction[15]))), min(heigh, max(0, int(prediction[16])))) (min(width, max(0, int(prediction[15]))), min(height, max(0, int(prediction[16]))))
]) ])
######### VALIDATING SPINE POINTS: # Validating spine points:
# This would mean that either the first or last point are above each other. # This would mean that either the first or last point are above each other.
# or the image is really small. This is ugly, but we can't just check for duplicates: # or the image is really small. This is ugly, but we can't just check for duplicates:
if np.linalg.norm(points_spine[0] - points_spine[1]) < MINIMUM_MID_POINT_DISTANCE or \ if np.linalg.norm(points_spine[0] - points_spine[1]) < MINIMUM_MID_POINT_DISTANCE or \
...@@ -127,18 +126,16 @@ def straighten(image): ...@@ -127,18 +126,16 @@ def straighten(image):
np.linalg.norm(points_spine[0] - points_spine[3]) < MINIMUM_MID_POINT_DISTANCE or \ np.linalg.norm(points_spine[0] - points_spine[3]) < MINIMUM_MID_POINT_DISTANCE or \
np.linalg.norm(points_spine[3] - points_spine[1]) < MINIMUM_MID_POINT_DISTANCE or \ np.linalg.norm(points_spine[3] - points_spine[1]) < MINIMUM_MID_POINT_DISTANCE or \
np.linalg.norm(points_spine[3] - points_spine[2]) < MINIMUM_MID_POINT_DISTANCE: np.linalg.norm(points_spine[3] - points_spine[2]) < MINIMUM_MID_POINT_DISTANCE:
print("predicted points were not correct" # print("predicted points were not correct and we can't proceed with the straightening")
" and we can't proceed with the straightening")
return None, None, None, None, score return None, None, None, None, score
# checking if the points in the middle needs to be swapped: # checking if the points in the middle needs to be swapped:
dist_2_1 = np.linalg.norm(points_spine[1] - points_spine[0]) dist_2_1 = np.linalg.norm(points_spine[1] - points_spine[0])
dist_3_1 = np.linalg.norm(points_spine[2] - points_spine[0]) dist_3_1 = np.linalg.norm(points_spine[2] - points_spine[0])
if dist_2_1 > dist_3_1: if dist_2_1 > dist_3_1:
points_spine[[1, 2], :] = points_spine[[2, 1], :] points_spine[[1, 2], :] = points_spine[[2, 1], :]
print("Two middle points need to be swapped, ", dist_3_1, " ", dist_2_1) # print("Two middle points need to be swapped, ", dist_3_1, " ", dist_2_1)
del dist_3_1 del dist_3_1
del dist_2_1 del dist_2_1
...@@ -146,39 +143,36 @@ def straighten(image): ...@@ -146,39 +143,36 @@ def straighten(image):
# its shoulders. This will be used to stretch the pattern to the image borders. If # its shoulders. This will be used to stretch the pattern to the image borders. If
# we don't do this a lot of the image will be of either the salamanders side (that # we don't do this a lot of the image will be of either the salamanders side (that
# does not have a pattern) or background: # does not have a pattern) or background:
shoulder_width = math.sqrt((points_shoulder[1][0] - points_shoulder[0][0]) ** 2 \ shoulder_width = math.sqrt(
+ (points_shoulder[1][1] - points_shoulder[0][1]) ** 2) (points_shoulder[1][0] - points_shoulder[0][0]) ** 2 + (points_shoulder[1][1] - points_shoulder[0][1]) ** 2)
print("shoulder width: ", shoulder_width) # print("shoulder width: ", shoulder_width)
if shoulder_width < MINIMUM_SHOULDER_WIDTH: if shoulder_width < MINIMUM_SHOULDER_WIDTH:
shoulder_width = 2 * (math.sqrt((points_shoulder[1][0] - points_spine[0][0]) ** 2 \ shoulder_width = 2 * (math.sqrt(
+ (points_shoulder[1][1] - points_spine[0][1]) ** 2)) (points_shoulder[1][0] - points_spine[0][0]) ** 2 + (points_shoulder[1][1] - points_spine[0][1]) ** 2))
print("correcting: ", shoulder_width) # print("correcting: ", shoulder_width)
if shoulder_width < MINIMUM_SHOULDER_WIDTH: if shoulder_width < MINIMUM_SHOULDER_WIDTH:
shoulder_width = STRAIGTHENED_IMAGE_HEIGHT shoulder_width = STRAIGTHENED_IMAGE_HEIGHT
start = time.time() start = time.time()
curve = get_smooth_curve(points_spine, STRAIGTHENED_IMAGE_WIDTH) curve = get_smooth_curve(points_spine, STRAIGTHENED_IMAGE_WIDTH)
end = time.time() end = time.time()
print("Bicubic interpolation of spine took " + str(end - start) + "s") # print("Bicubic interpolation of spine took " + str(end - start) + "s")
map = generate_map_from_bellycurve(curve, shoulder_width) map = generate_map_from_bellycurve(curve, shoulder_width)
straightened_image = cv2.remap(cropped_image, map[:, :, 0], map[:, :, 1], cv2.INTER_LINEAR) straightened_image = cv2.remap(cropped_image, map[:, :, 0], map[:, :, 1], cv2.INTER_LINEAR)
end_time = time.time() end_time = time.time()
print("Straightening took " + str(end_time - start_time) + "s") # print("Straightening took " + str(end_time - start_time) + "s")
return straightened_image, cropped_image, points_spine, points_shoulder, score return straightened_image, cropped_image, points_spine, points_shoulder, score
def generate_map_from_bellycurve(curve, width=STRAIGTHENED_IMAGE_WIDTH): def generate_map_from_bellycurve(curve, width=STRAIGTHENED_IMAGE_WIDTH):
''' '''
Generates a map that can be passed to cv2.remap to extract the belly pattern Generates a map that can be passed to cv2.remap to extract the belly pattern
''' '''
salamander_length = np.linalg.norm(curve[len(curve) - 1] - curve[0]) salamander_length = np.linalg.norm(curve[len(curve) - 1] - curve[0])
# salamander_width = salamander_length * STRAIGTHENED_IMAGE_ASPECT_RATIO # salamander_width = salamander_length * STRAIGHTENED_IMAGE_ASPECT_RATIO
salamander_width = width + SHOULDER_ESTIMATION_BUFFER salamander_width = width + SHOULDER_ESTIMATION_BUFFER
gradient = np.gradient(curve, axis=0) gradient = np.gradient(curve, axis=0)
...@@ -200,9 +194,6 @@ def generate_map_from_bellycurve(curve, width=STRAIGTHENED_IMAGE_WIDTH): ...@@ -200,9 +194,6 @@ def generate_map_from_bellycurve(curve, width=STRAIGTHENED_IMAGE_WIDTH):
return map.astype('float32') return map.astype('float32')
def get_smooth_curve(points, num_points): def get_smooth_curve(points, num_points):
''' '''
Takes in an array of 2-D points and returns a new set of points Takes in an array of 2-D points and returns a new set of points
...@@ -214,36 +205,19 @@ def get_smooth_curve(points, num_points): ...@@ -214,36 +205,19 @@ def get_smooth_curve(points, num_points):
# print("distance, ", distance.shape) # print("distance, ", distance.shape)
# Accumulate distance to use as parameter # Accumulate distance to use as parameter
accumulated_distance = np.cumsum(distance) accumulated_distance = np.cumsum(distance)
# print("accumulated_distance, ", accumulated_distance.shape)
# Insert starting point # Insert starting point
accumulated_distance = np.insert(accumulated_distance, 0, 0) accumulated_distance = np.insert(accumulated_distance, 0, 0)
# print("accumulated_distance, ", accumulated_distance.shape)
# Make it go from 0 to 1 # Make it go from 0 to 1
accumulated_distance /= accumulated_distance[-1] accumulated_distance /= accumulated_distance[-1]
# print("accumulated_distance, ", accumulated_distance.shape)
alpha = np.linspace(0, 1, num_points) alpha = np.linspace(0, 1, num_points)
# print("alpha, ", alpha.shape)
f = interp1d(accumulated_distance, points, kind='cubic', axis=0) f = interp1d(accumulated_distance, points, kind='cubic', axis=0)
interpoints = f(alpha) inter_points = f(alpha)
# print("interpoint, ", interpoints.shape) return inter_points
return interpoints
# def get_distance(point1, point2):
# '''
# get_distance() calculates the distance between two points:
# '''
# return 2 * (math.sqrt((point1[0] - point2[0]) ** 2 \
# + (point1[1] - point2[1]) ** 2))
#
def halfway_between(point1, point2): def halfway_between(point1, point2):
......
...@@ -6,31 +6,6 @@ from skimage.util import img_as_ubyte ...@@ -6,31 +6,6 @@ from skimage.util import img_as_ubyte
from multiprocessing import Process from multiprocessing import Process
from multiprocessing import Manager from multiprocessing import Manager
from deeplabcutcore.pose_estimation_tensorflow.nnet.net_factory import pose_net
# def get_poses_deeplabcut_model(dlc_cfg, sess, inputs, outputs,image):
# ''' Batchwise prediction of pose for frame list in directory'''
# #from skimage.io import imread
# from deeplabcutcore.utils.auxfun_videos import imread
# print("Starting to extract posture")
# ny,nx,nc=np.shape(image)
# nframes = 1
# print("Overall # of frames: ", nframes," found with (before cropping) frame dimensions: ", nx,ny)
# PredictedData = np.zeros((nframes, dlc_cfg['num_outputs'] * 3 * len(dlc_cfg['all_joints_names'])))
# print(PredictedData)
#
# # change from int:
# frame = img_as_ubyte(image)
# print(inputs)
# print(np.shape(frame))
# pose = predict.getpose(frame, dlc_cfg, sess, inputs, outputs)
# PredictedData[0, :] = pose.flatten()
#
# return PredictedData,nframes,nx,ny
def run_prediction_dlc(config: str,image: np.ndarray,shuffle: int =1, def run_prediction_dlc(config: str,image: np.ndarray,shuffle: int =1,
trainingsetindex: int =0,gputouse: int =None,return_dict: list = None): trainingsetindex: int =0,gputouse: int =None,return_dict: list = None):
from deeplabcutcore.pose_estimation_tensorflow.nnet import predict from deeplabcutcore.pose_estimation_tensorflow.nnet import predict
...@@ -39,27 +14,28 @@ def run_prediction_dlc(config: str,image: np.ndarray,shuffle: int =1, ...@@ -39,27 +14,28 @@ def run_prediction_dlc(config: str,image: np.ndarray,shuffle: int =1,
from deeplabcutcore.utils import auxiliaryfunctions from deeplabcutcore.utils import auxiliaryfunctions
if 'TF_CUDNN_USE_AUTOTUNE' in os.environ: if 'TF_CUDNN_USE_AUTOTUNE' in os.environ:
del os.environ['TF_CUDNN_USE_AUTOTUNE'] #was potentially set during training # was potentially set during training
del os.environ['TF_CUDNN_USE_AUTOTUNE']
if gputouse is not None: #gpu selection if gputouse is not None: #gpu selection
os.environ['CUDA_VISIBLE_DEVICES'] = str(gputouse) os.environ['CUDA_VISIBLE_DEVICES'] = str(gputouse)
# tf.compat.v1.reset_default_graph() # tf.compat.v1.reset_default_graph()
reset_default_graph() reset_default_graph()
start_path=os.getcwd() #record cwd to return to this directory in the end # record cwd to return to this directory in the end:
cfg = auxiliaryfunctions.read_config(config) cfg = auxiliaryfunctions.read_config(config)
trainFraction = cfg['TrainingFraction'][trainingsetindex] train_fraction = cfg['TrainingFraction'][trainingsetindex]
modelfolder=os.path.join(cfg["project_path"],str(auxiliaryfunctions.GetModelFolder(trainFraction,shuffle,cfg))) model_folder=os.path.join(cfg["project_path"],str(auxiliaryfunctions.GetModelFolder(train_fraction,shuffle,cfg)))
path_test_config = Path(modelfolder) / 'test' / 'pose_cfg.yaml' path_test_config = Path(model_folder) / 'test' / 'pose_cfg.yaml'
print(path_test_config) # print(path_test_config)
try: try:
dlc_cfg = load_config(str(path_test_config)) dlc_cfg = load_config(str(path_test_config))
except FileNotFoundError: except FileNotFoundError:
raise FileNotFoundError("It seems the model for shuffle %s and trainFraction %s does not exist."%(shuffle,trainFraction)) raise FileNotFoundError("It seems the model for shuffle %s and trainFraction %s does not exist."%(shuffle,train_fraction))
# Check which snapshots are available and sort them by # iterations # Check which snapshots are available and sort them by # iterations
try: try:
Snapshots = np.array([fn.split('.')[0]for fn in os.listdir(os.path.join(modelfolder , 'train'))if "index" in fn]) snapshots = np.array([fn.split('.')[0]for fn in os.listdir(os.path.join(model_folder , 'train'))if "index" in fn])
except FileNotFoundError: except FileNotFoundError:
raise FileNotFoundError("Snapshots not found!\ raise FileNotFoundError("Snapshots not found!\
It seems the dataset for shuffle %s has not been trained/does not exist.\n \ It seems the dataset for shuffle %s has not been trained/does not exist.\n \
...@@ -67,28 +43,28 @@ def run_prediction_dlc(config: str,image: np.ndarray,shuffle: int =1, ...@@ -67,28 +43,28 @@ def run_prediction_dlc(config: str,image: np.ndarray,shuffle: int =1,
'train_network' to train the network for shuffle %s."%(shuffle,shuffle)) 'train_network' to train the network for shuffle %s."%(shuffle,shuffle))
if cfg['snapshotindex'] == 'all': if cfg['snapshotindex'] == 'all':
print("Snapshotindex is set to 'all' in the config.yaml file.\ # print("Snapshotindex is set to 'all' in the config.yaml file.\
Running video analysis with all snapshots is very costly!\ # Running video analysis with all snapshots is very costly!\
Use the function 'evaluate_network' to choose the best the snapshot.\ # Use the function 'evaluate_network' to choose the best the snapshot.\
For now, changing snapshot index to -1!") # For now, changing snapshot index to -1!")
snapshotindex = -1 snapshot_index = -1
else: else:
snapshotindex=cfg['snapshotindex'] snapshot_index=cfg['snapshotindex']
increasing_indices = np.argsort([int(m.split('-')[1]) for m in Snapshots]) increasing_indices = np.argsort([int(m.split('-')[1]) for m in snapshots])
Snapshots = Snapshots[increasing_indices] snapshots = snapshots[increasing_indices]
print("Using %s" % Snapshots[snapshotindex], "for model", modelfolder) # print("Using %s" % snapshots[snapshot_index], "for model", model_folder)
################################################## ##################################################
# Load and setup CNN part detector # Load and setup CNN part detector
################################################## ##################################################
# Check if data already was generated: # Check if data already was generated:
dlc_cfg['init_weights'] = os.path.join(modelfolder , 'train', Snapshots[snapshotindex]) dlc_cfg['init_weights'] = os.path.join(model_folder , 'train', snapshots[snapshot_index])
trainingsiterations = (dlc_cfg['init_weights'].split(os.sep)[-1]).split('-')[-1] trainingsiterations = (dlc_cfg['init_weights'].split(os.sep)[-1]).split('-')[-1]
#update batchsize (based on parameters in config.yaml) # Update batchsize (based on parameters in config.yaml)
dlc_cfg['batch_size'] = 1 dlc_cfg['batch_size'] = 1
# Name for scorer: # Name for scorer:
...@@ -107,13 +83,13 @@ def run_prediction_dlc(config: str,image: np.ndarray,shuffle: int =1, ...@@ -107,13 +83,13 @@ def run_prediction_dlc(config: str,image: np.ndarray,shuffle: int =1,
# Loading the images # Loading the images
################################################## ##################################################
# Predicting data: # Predicting data:
print("session: ", sess) # print("session: ", sess)
# PredictedData,nframes,nx,ny=get_poses_deeplabcut_model(dlc_cfg, sess, inputs, outputs,image) # PredictedData,nframes,nx,ny=get_poses_deeplabcut_model(dlc_cfg, sess, inputs, outputs,image)
ny,nx,nc=np.shape(image) ny,nx,nc=np.shape(image)
nframes = 1 nframes = 1
print("Frame dimensions: ", nx,ny) # print("Frame dimensions: ", nx,ny)
PredictedData = np.zeros((nframes, dlc_cfg['num_outputs'] * 3 * len(dlc_cfg['all_joints_names']))) PredictedData = np.zeros((nframes, dlc_cfg['num_outputs'] * 3 * len(dlc_cfg['all_joints_names'])))
# change from int: # change from int:
...@@ -123,7 +99,7 @@ def run_prediction_dlc(config: str,image: np.ndarray,shuffle: int =1, ...@@ -123,7 +99,7 @@ def run_prediction_dlc(config: str,image: np.ndarray,shuffle: int =1,
stop = time.time() stop = time.time()
print("PredictedData done:\n") # print("PredictedData done:\n")
# closing the session: # closing the session:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment