Skip to content

Commit

Permalink
fixed multiprocessing and optimized model preloading
Browse files Browse the repository at this point in the history
  • Loading branch information
pospielov committed Mar 24, 2024
1 parent 7cd4ce7 commit de84014
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
6 changes: 4 additions & 2 deletions dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ services:
- MAX_REQUEST_SIZE=${max_request_size}B
- CONNECTION_TIMEOUT=${connection_timeout:-10000}
- READ_TIMEOUT=${read_timeout:-60000}
- DISTANCE_TYPE=${distance_type:-custom}

compreface-fe:
image: ${registry}compreface-fe:${FE_VERSION}
Expand Down Expand Up @@ -100,12 +101,13 @@ services:
environment:
- ML_PORT=3000
- IMG_LENGTH_LIMIT=${max_detect_size}
#- UWSGI_PROCESSES=${uwsgi_processes:-1}
#- UWSGI_THREADS=${uwsgi_threads:-1}
- UWSGI_PROCESSES=${uwsgi_processes:-1}
- UWSGI_THREADS=${uwsgi_threads:-1}
- CUDA_VISIBLE_DEVICES=0 ##
- NVIDIA_VISIBLE_DEVICES=0
- TZ=utc
- DEBIAN_FRONTEND=noninteractive
- DEVICE=cuda:0
healthcheck:
test: curl --fail http://localhost:3000/healthcheck || exit 1
interval: 10s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,21 @@
model_mame:model_path,
}

preloaded = ""

def load_pretrained_model(architecture=model_mame):
# load model and pretrained statedict
assert architecture in adaface_models.keys()
model = net.build_model(architecture)
statedict = torch.load(adaface_models[architecture],map_location =device)['state_dict']
model_statedict = {key[6:]:val for key, val in statedict.items() if key.startswith('model.')}
model.load_state_dict(model_statedict)
model.eval()
return model
global preloaded

if preloaded == "":
# load model and pretrained statedict
assert architecture in adaface_models.keys()
preloaded = net.build_model(architecture)
print("load adaface model")
statedict = torch.load(adaface_models[architecture],map_location =device)['state_dict']
model_statedict = {key[6:]:val for key, val in statedict.items() if key.startswith('model.')}
preloaded.load_state_dict(model_statedict)
preloaded.eval()
return preloaded

def to_input(pil_rgb_image):
np_img = np.array(pil_rgb_image)
Expand Down Expand Up @@ -53,6 +59,7 @@ def inference_detector(image_path):

detected["result"].append(face)

print(f'Processed {len(detected["result"])} faces')
return detected

def inference_scaner(image_path):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,20 +355,34 @@ def warp_and_crop_face(src_img,

return face_img

preloaded = ""

def preload_model(device, cfg):
global preloaded

if preloaded == "":
print("load RetinaFace model")
preloaded = RetinaFace(cfg=cfg, phase = 'test')
preloaded = load_model(preloaded, args.trained_model, args.cpu)
preloaded.eval()
cudnn.benchmark = True

preloaded = preloaded.to(device)
return preloaded

def retina_detector(image_pil, content_type):
torch.set_grad_enabled(False)

cfg = None
if args.network == "mobile0.25":
cfg = cfg_mnet
elif args.network == "resnet50":
cfg = cfg_re50
# net and model
net = RetinaFace(cfg=cfg, phase = 'test')
net = load_model(net, args.trained_model, args.cpu)
net.eval()
cudnn.benchmark = True

device = torch.device("cpu" if args.cpu else "cuda")
net = net.to(device)
# net and model

net = preload_model(device, cfg)

resize = 1

Expand Down
3 changes: 2 additions & 1 deletion embedding-calculator/uwsgi.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
module = src.app:wsgi_app()
uid = www-data
gid = www-data
master = true
#master = true
lazy=true
http-socket = 0.0.0.0:3000
vacuum = true
die-on-term = true
Expand Down

0 comments on commit de84014

Please sign in to comment.