Skip to content
Snippets Groups Projects
Commit 2bf871b1 authored by Eirik Martin Danielsen's avatar Eirik Martin Danielsen :speech_balloon:
Browse files

deeplabcut runs in a different process

parent ea1a06f7
Branches
No related tags found
1 merge request!20Resolve "ImageAI gender recognition."
...@@ -4,7 +4,8 @@ from tensorflow.keras import Input ...@@ -4,7 +4,8 @@ from tensorflow.keras import Input
import cv2 import cv2
import numpy as np import numpy as np
import os import os
import multiprocessing from multiprocessing import Process
from multiprocessing import Manager
...@@ -14,6 +15,7 @@ def run_estimation(detection_model_path: str = None, configuration_json: str = N ...@@ -14,6 +15,7 @@ def run_estimation(detection_model_path: str = None, configuration_json: str = N
display_percentage_probability: bool = True, display_object_name: bool = True, display_percentage_probability: bool = True, display_object_name: bool = True,
thread_safe: bool = False, debug_img_name: str = "", debug_create_pred_img: bool = False, ret_dictionary: list = None): thread_safe: bool = False, debug_img_name: str = "", debug_create_pred_img: bool = False, ret_dictionary: list = None):
import tensorflow as tf import tensorflow as tf
from tensorflow.keras.backend import clear_session
# add to the top of your code under import tensorflow as tf # add to the top of your code under import tensorflow as tf
config = tf.compat.v1.ConfigProto() config = tf.compat.v1.ConfigProto()
...@@ -145,7 +147,7 @@ def estimate_image(detection_model_path: str = None, configuration_json: str = N ...@@ -145,7 +147,7 @@ def estimate_image(detection_model_path: str = None, configuration_json: str = N
:return detected_objects_image_array: :return detected_objects_image_array:
""" """
manager = multiprocessing.Manager() manager = Manager()
return_dict = manager.list() return_dict = manager.list()
# return_dict = multiprocessing.Value("d", [], lock=False) # return_dict = multiprocessing.Value("d", [], lock=False)
args = (detection_model_path, configuration_json, input_image, args = (detection_model_path, configuration_json, input_image,
...@@ -153,7 +155,7 @@ def estimate_image(detection_model_path: str = None, configuration_json: str = N ...@@ -153,7 +155,7 @@ def estimate_image(detection_model_path: str = None, configuration_json: str = N
nms_treshold, nms_treshold,
display_percentage_probability, display_object_name, display_percentage_probability, display_object_name,
thread_safe, debug_img_name, debug_create_pred_img, return_dict) thread_safe, debug_img_name, debug_create_pred_img, return_dict)
p = multiprocessing.Process(target=run_estimation, args=args) p = Process(target=run_estimation, args=args)
p.start() p.start()
p.join() p.join()
return return_dict[0], return_dict[1], return_dict[2], return_dict[3] return return_dict[0], return_dict[1], return_dict[2], return_dict[3]
......
...@@ -3,13 +3,12 @@ import numpy as np ...@@ -3,13 +3,12 @@ import numpy as np
from scipy.interpolate import interp1d from scipy.interpolate import interp1d
import time import time
import math import math
from path_constants import abs_path_dlc_config
from tensorflow.python.keras.layers import Minimum from tensorflow.python.keras.layers import Minimum
import algorithm.train_src.predict_salamander_abdomen as psa import algorithm.train_src.predict_salamander_abdomen as psa
from threading import Semaphore from threading import Semaphore
DEEPLABCUT_CONFIG_PATH = "./algorithm/train_src/dlc_model/config.yaml"
######################################## ########################################
# CONSTANTS # CONSTANTS
######################################## ########################################
...@@ -73,7 +72,7 @@ def straighten(image): ...@@ -73,7 +72,7 @@ def straighten(image):
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)
prediction, _, x, y = psa.get_prediction_dlc(config=DEEPLABCUT_CONFIG_PATH, image=cropped_image, gputouse=GPUID) prediction, _, x, y = psa.get_prediction_dlc(config=abs_path_dlc_config, image=cropped_image, gputouse=GPUID)
# points is an array of coordinates to use to straighten the image: # points is an array of coordinates to use to straighten the 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.
......
import os.path import os.path
from deeplabcutcore.pose_estimation_tensorflow.nnet import predict
from deeplabcutcore.pose_estimation_tensorflow.config import load_config
from deeplabcutcore.pose_estimation_tensorflow.dataset.pose_dataset import data_to_input
import time import time
import pandas as pd
import numpy as np import numpy as np
import os
import argparse
from pathlib import Path from pathlib import Path
from tqdm import tqdm
import tensorflow as tf
from deeplabcutcore.utils import auxiliaryfunctions
from deeplabcutcore.pose_estimation_tensorflow.predict_videos import GetPosesofFrames
import cv2
from skimage.util import img_as_ubyte from skimage.util import img_as_ubyte
from multiprocessing import Process
from multiprocessing import Manager
from deeplabcutcore.pose_estimation_tensorflow.nnet.net_factory import pose_net from deeplabcutcore.pose_estimation_tensorflow.nnet.net_factory import pose_net
def get_poses_deeplabcut_model(dlc_cfg, sess, inputs, outputs,image): # def get_poses_deeplabcut_model(dlc_cfg, sess, inputs, outputs,image):
''' Batchwise prediction of pose for frame list in directory''' # ''' Batchwise prediction of pose for frame list in directory'''
#from skimage.io import imread # #from skimage.io import imread
from deeplabcutcore.utils.auxfun_videos import imread # from deeplabcutcore.utils.auxfun_videos import imread
print("Starting to extract posture") # print("Starting to extract posture")
ny,nx,nc=np.shape(image) # ny,nx,nc=np.shape(image)
nframes = 1 # nframes = 1
print("Overall # of frames: ", nframes," found with (before cropping) frame dimensions: ", nx,ny) # 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']))) # PredictedData = np.zeros((nframes, dlc_cfg['num_outputs'] * 3 * len(dlc_cfg['all_joints_names'])))
print(PredictedData) # print(PredictedData)
#
# change from int: # # change from int:
frame = img_as_ubyte(image) # frame = img_as_ubyte(image)
print(inputs) # print(inputs)
print(np.shape(frame)) # print(np.shape(frame))
pose = predict.getpose(frame, dlc_cfg, sess, inputs, outputs) # pose = predict.getpose(frame, dlc_cfg, sess, inputs, outputs)
PredictedData[0, :] = pose.flatten() # PredictedData[0, :] = pose.flatten()
#
return PredictedData,nframes,nx,ny # return PredictedData,nframes,nx,ny
def get_prediction_dlc(config,image,shuffle=1, def run_prediction_dlc(config: str,image: np.ndarray,shuffle: int =1,
trainingsetindex=0,gputouse=None): trainingsetindex: int =0,gputouse: int =None,return_dict: list = None):
from deeplabcutcore.pose_estimation_tensorflow.nnet import predict
from deeplabcutcore.pose_estimation_tensorflow.config import load_config
""" from tensorflow.python.framework.ops import reset_default_graph
Analyzed all images (of type = frametype) in a folder and stores the output in one file. from deeplabcutcore.utils import auxiliaryfunctions
You can crop the frames (before analysis), by changing 'cropping'=True and setting 'x1','x2','y1','y2' in the config file.
Output: The labels are stored as MultiIndex Pandas Array, which contains the name of the network, body part name, (x, y) label position \n
in pixels, and the likelihood for each frame per body part. These arrays are stored in an efficient Hierarchical Data Format (HDF) \n
in the same directory, where the video is stored. However, if the flag save_as_csv is set to True, the data can also be exported in \n
comma-separated values format (.csv), which in turn can be imported in many programs, such as MATLAB, R, Prism, etc.
Parameters
----------
config : string
Full path of the config.yaml file as a string.
directory: string
Full path to directory containing the frames that shall be analyzed
frametype: string, optional
Checks for the file extension of the frames. Only images with this extension are analyzed. The default is ``.png``
shuffle: int, optional
An integer specifying the shuffle index of the training dataset used for training the network. The default is 1.
trainingsetindex: int, optional
Integer specifying which TrainingsetFraction to use. By default the first (note that TrainingFraction is a list in config.yaml).
gputouse: int, optional. Natural number indicating the number of your GPU (see number in nvidia-smi). If you do not have a GPU put None.
See: https://nvidia.custhelp.com/app/answers/detail/a_id/3751/~/useful-nvidia-smi-queries
save_as_csv: bool, optional
Saves the predictions in a .csv file. The default is ``False``; if provided it must be either ``True`` or ``False``
rbg: bool, optional.
Whether to load image as rgb; Note e.g. some tiffs do not alow that option in imread, then just set this to false.
Examples
--------
If you want to analyze all frames in /analysis/project/timelapseexperiment1
>>> deeplabcutcore.analyze_videos('/analysis/project/reaching-task/config.yaml','/analysis/project/timelapseexperiment1')
--------
If you want to analyze all frames in /analysis/project/timelapseexperiment1
>>> deeplabcutcore.analyze_videos('/analysis/project/reaching-task/config.yaml','/analysis/project/timelapseexperiment1')
--------
Note: for test purposes one can extract all frames from a video with ffmeg, e.g. ffmpeg -i testvideo.avi thumb%04d.png
"""
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 del os.environ['TF_CUDNN_USE_AUTOTUNE'] #was potentially set during training
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()
start_path=os.getcwd() #record cwd to return to this directory in the end start_path=os.getcwd() #record cwd to return to this directory in the end
cfg = auxiliaryfunctions.read_config(config) cfg = auxiliaryfunctions.read_config(config)
...@@ -162,7 +109,20 @@ def get_prediction_dlc(config,image,shuffle=1, ...@@ -162,7 +109,20 @@ def get_prediction_dlc(config,image,shuffle=1,
# start = time.time() # start = time.time()
# Predicting data: # Predicting data:
print("predicting: ", sess) print("predicting: ", 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)
nframes = 1
print("Frame dimensions: ", nx,ny)
PredictedData = np.zeros((nframes, dlc_cfg['num_outputs'] * 3 * len(dlc_cfg['all_joints_names'])))
# change from int:
frame = img_as_ubyte(image)
pose = predict.getpose(frame, dlc_cfg, sess, inputs, outputs)
PredictedData[0, :] = pose.flatten()
stop = time.time() stop = time.time()
print("PredictedData done:\n") print("PredictedData done:\n")
...@@ -173,5 +133,37 @@ def get_prediction_dlc(config,image,shuffle=1, ...@@ -173,5 +133,37 @@ def get_prediction_dlc(config,image,shuffle=1,
# tf.Graph.reset_default_graph() # tf.Graph.reset_default_graph()
sess.close() sess.close()
return PredictedData[0], nframes, nx, ny return_dict.append(PredictedData[0])
return_dict.append(nframes)
return_dict.append(nx)
return_dict.append(ny)
# return PredictedData[0], nframes, nx, ny
def get_prediction_dlc(config,image,shuffle=1,
trainingsetindex=0,gputouse=None):
"""
Output: Predictions of the salamander.
Parameters
----------
config : string
Full path of the config.yaml file as a string.
shuffle: int, optional
An integer specifying the shuffle index of the training dataset used for training the network. The default is 1.
trainingsetindex: int, optional
Integer specifying which TrainingsetFraction to use. By default the first (note that TrainingFraction is a list in config.yaml).
gputouse: int, optional. Natural number indicating the number of your GPU (see number in nvidia-smi). If you do not have a GPU put None.
"""
return_dict = Manager().list()
# return_dict = multiprocessing.Value("d", [], lock=False)
args = (config, image, shuffle,
trainingsetindex, gputouse, return_dict)
p = Process(target=run_prediction_dlc, args=args)
p.start()
p.join()
return return_dict[0], return_dict[1], return_dict[2], return_dict[3]
import new_straightening as straighten import algorithm.train_src.new_straightening as straighten
# CROPPING IMAGE # CROPPING IMAGE
import cv2 import cv2
import os import os
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from path_constants import abs_path_dlc_config
from path_constants import abs_path_img_analyze
import time
def test():
config_path= "dlc_model/config.yaml"
directory_path= "../../../salamander-api/img_analyze"
image_to_predict = "img40.png"
config = os.path.abspath(config_path)
os_directory_path = os.path.abspath(directory_path) cur_dir = os.getcwd()
image_to_predict = "aspectTest.jpg"
print(abs_path_dlc_config)
print(abs_path_img_analyze)
print(image_to_predict)
config = os.path.abspath(abs_path_dlc_config)
os_directory_path = os.path.abspath(abs_path_img_analyze)
image_type = image_to_predict[-4:] image_type = image_to_predict[-4:]
img_directory = os_directory_path + '\\' + image_to_predict img_directory = os_directory_path + '/' + image_to_predict
smallpath = os_directory_path + '/' + 'small' + image_to_predict smallpath = os_directory_path + '/' + 'small' + image_to_predict
...@@ -20,11 +27,12 @@ img = cv2.imread(img_directory) ...@@ -20,11 +27,12 @@ img = cv2.imread(img_directory)
# You will have to convert the color if you use OpenCV. # You will have to convert the color if you use OpenCV.
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print("starting in 10s")
time.sleep(10)
str_image, img, points, shoulder_points, _ = straighten.straighten(image = img) str_image, img, points, shoulder_points, _ = straighten.straighten(image = img)
if str_image is not None: if str_image is not None:
str_image = cv2.cvtColor(str_image, cv2.COLOR_BGR2RGB) str_image = cv2.cvtColor(str_image, cv2.COLOR_BGR2RGB)
cv2.imwrite(directory_path + '/' + image_to_predict[0:-4] + '_str.png', str_image) cv2.imwrite(abs_path_img_analyze + '/' + image_to_predict[0:-4] + '_str.png', str_image)
# data points # data points
implot = plt.imshow(img) implot = plt.imshow(img)
print(points[0]) print(points[0])
...@@ -36,4 +44,6 @@ if str_image is not None: ...@@ -36,4 +44,6 @@ if str_image is not None:
plt.scatter(shoulder_points[0][0], shoulder_points[0][1], c='y', s=40, ) plt.scatter(shoulder_points[0][0], shoulder_points[0][1], c='y', s=40, )
plt.scatter(shoulder_points[1][0], shoulder_points[1][1], c='y', s=40, ) plt.scatter(shoulder_points[1][0], shoulder_points[1][1], c='y', s=40, )
plt.savefig(directory_path + '/' + image_to_predict[0:-4] + '_points.png') plt.savefig(abs_path_img_analyze + '/' + image_to_predict[0:-4] + '_points.png')
\ No newline at end of file print("done in 10 s (so you can CHECK GPU USAGE)")
time.sleep(10)
...@@ -2,6 +2,7 @@ from flask import request, jsonify ...@@ -2,6 +2,7 @@ from flask import request, jsonify
from flask_restful import Resource from flask_restful import Resource
from flask_jwt_extended import get_jwt_identity, jwt_required from flask_jwt_extended import get_jwt_identity, jwt_required
from algorithm.train_src.new_straightening import straighten from algorithm.train_src.new_straightening import straighten
from algorithm.train_src.estimate_gender import estimate_image
import cv2 import cv2
import os import os
from api import limiter from api import limiter
......
img_analyze/aspectTest.jpg

1.4 MiB

# This file contains consts to path and the program
# uses these for accessing.
import os
abs_path_work_dir = os.getcwd()
abs_path_dlc_model = os.getcwd() + "\\algorithm\\train_src\\dlc_model"
abs_path_dlc_config = abs_path_dlc_model + "\\config.yaml"
abs_path_img_analyze = os.getcwd() + "\\img_analyze"
from api import app #from api import app
if __name__ == "__main__": if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0') # app.run(debug=True)
# app.run(debug=True, host='0.0.0.0')
import algorithm.train_src.test_straightening as poopi
poopi.test()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment