Skip to content

Commit 46174b9

Browse files
committed
make retinaface compatible with tf2.16 and later
1 parent 059fcdf commit 46174b9

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

retinaface/RetinaFace.py

+8
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33
import logging
44
from typing import Union, Any, Optional, Dict
55

6+
# this has to be set before importing tf
7+
os.environ["TF_USE_LEGACY_KERAS"] = "1"
8+
9+
# pylint: disable=wrong-import-position
610
import numpy as np
711
import tensorflow as tf
812

913
from retinaface import __version__
1014
from retinaface.model import retinaface_model
1115
from retinaface.commons import preprocess, postprocess
1216
from retinaface.commons.logger import Logger
17+
from retinaface.commons import package_utils
18+
19+
# users should install tf_keras package if they are using tf 2.16 or later versions
20+
package_utils.validate_for_keras3()
1321

1422
logger = Logger(module="retinaface/RetinaFace.py")
1523

retinaface/commons/package_utils.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 3rd party dependencies
2+
import tensorflow as tf
3+
4+
# project dependencies
5+
from retinaface.commons.logger import Logger
6+
7+
logger = Logger(module="retinaface/commons/package_utils.py")
8+
9+
10+
def validate_for_keras3():
11+
tf_major = int(tf.__version__.split(".", maxsplit=1)[0])
12+
tf_minor = int(tf.__version__.split(".", maxsplit=-1)[1])
13+
14+
# tf_keras is a must dependency after tf 2.16
15+
if tf_major == 1 or (tf_major == 2 and tf_minor < 16):
16+
return
17+
18+
try:
19+
import tf_keras
20+
21+
logger.debug(f"tf_keras is already available - {tf_keras.__version__}")
22+
except ImportError as err:
23+
# you may consider to install that package here
24+
raise ValueError(
25+
f"You have tensorflow {tf.__version__} and this requires "
26+
"tf-keras package. Please run `pip install tf-keras` "
27+
"or downgrade your tensorflow."
28+
) from err

0 commit comments

Comments
 (0)