Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion deeplabcut/pose_estimation_tensorflow/core/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
)
from deeplabcut.pose_estimation_tensorflow.nnets import PoseNetFactory
from deeplabcut.pose_estimation_tensorflow.util.logging import setup_logging
from deeplabcut.utils import auxfun_models


class LearningRate(object):
Expand Down Expand Up @@ -243,7 +244,8 @@ def train(
sess.run(tf.compat.v1.local_variables_initializer())

# Restore variables from disk.
restorer.restore(sess, cfg["init_weights"])
auxfun_models.smart_restore(restorer, sess, cfg["init_weights"], net_type)

if maxiters is None:
max_iter = int(cfg["multi_step"][-1][1])
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
get_optimizer,
LearningRate,
)
from deeplabcut.utils import auxfun_models


def train(
Expand Down Expand Up @@ -157,7 +158,8 @@ def train(
sess.run(tf.compat.v1.global_variables_initializer())
sess.run(tf.compat.v1.local_variables_initializer())

restorer.restore(sess, cfg["init_weights"])
auxfun_models.smart_restore(restorer, sess, cfg["init_weights"], net_type)

if maxiters is None:
max_iter = int(cfg["multi_step"][-1][1])
else:
Expand Down
17 changes: 17 additions & 0 deletions deeplabcut/utils/auxfun_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ def set_visible_devices(gputouse: int):
tf.config.set_visible_devices(physical_devices[gputouse], "GPU")


def smart_restore(restorer, sess, checkpoint_path, net_type):
"Restore pretrained weights, smartly redownloading them if missing."
try:
restorer.restore(sess, checkpoint_path)
except ValueError as e: # The path may be wrong, or the weights no longer exist
dlcparent_path = auxiliaryfunctions.get_deeplabcut_path()
correct_model_path = os.path.join(
dlcparent_path, MODELTYPE_FILEPATH_MAP[net_type],
)
if checkpoint_path == correct_model_path:
# The path is right, hence the weights are missing; we'll download them again.
_ = check_for_weights(net_type, Path(dlcparent_path))
restorer.restore(sess, checkpoint_path)
else:
raise ValueError(e)


# Aliases for backwards-compatibility
Check4Weights = check_for_weights
Downloadweights = download_weights
Expand Down