| 94 | |
| 95 | |
| 96 | def get_optimizer(loss_op, cfg): |
| 97 | tstep = tf.compat.v1.placeholder(tf.int32, shape=[], name="tstep") |
| 98 | if "efficientnet" in cfg["net_type"]: |
| 99 | print("Switching to cosine decay schedule with adam!") |
| 100 | cfg["optimizer"] = "adam" |
| 101 | learning_rate = tf.compat.v1.train.cosine_decay(cfg["lr_init"], tstep, cfg["decay_steps"], alpha=cfg["alpha_r"]) |
| 102 | else: |
| 103 | learning_rate = tf.compat.v1.placeholder(tf.float32, shape=[]) |
| 104 | |
| 105 | if cfg["optimizer"] == "sgd": |
| 106 | optimizer = tf.compat.v1.train.MomentumOptimizer(learning_rate=learning_rate, momentum=0.9) |
| 107 | elif cfg["optimizer"] == "adam": |
| 108 | optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate) |
| 109 | else: |
| 110 | raise ValueError("unknown optimizer {}".format(cfg["optimizer"])) |
| 111 | train_op = slim.learning.create_train_op(loss_op, optimizer) |
| 112 | |
| 113 | return learning_rate, train_op, tstep |
| 114 | |
| 115 | |
| 116 | def get_optimizer_with_freeze(loss_op, cfg): |