Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allowing for non-command line use #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

CorbanSwain
Copy link

@CorbanSwain CorbanSwain commented Dec 5, 2018

This fixes an issure for me where I wanted to call retrain from within another script. This PR is now enables this functionality:

# call_retrain.py

# We're assuming the project directory has been renamed to tffp2

# Some other python script in the same parent directory as the tffp2 directory
# parent_dir
#    + - call_retrain.py
#    + tffp2
#    + tf_files
#    + flower_images
#    + logs


from tffp2.scripts import retrain
import os

def retrain_on_flowers(log_dir):
    model_arch = 'mobilenet_0.5_224'
    summaries_dir = os.path.join(log_dir, model_arch)
    retrain_args = dict(
        bottleneck_dir='tf_files/bottlenecks',
        model_dir='tf_files/models/%s' % model_arch,
        summaries_dir=summaries_dir,
        output_graph='tf_files/retrained_graph.pb',
        output_labels='tf_files/retrained_labels.txt',
        architecture=model_arch,
        image_dir='flower_images',
    )
    retrain(**retrain_args)


def start_tensorboard(pth):
    import tensorboard as tb
    import tensorboard.main
    import tensorboard.program
    from threading import Thread

    tb.program.FLAGS.logdir = pth
    thread = Thread(target=tb.main.main, args=tuple())
    thread.start()
    return thread


if __name__ == "__main__":
    tf_logdir = 'logs'
    start_tensorboard(tf_logdir)
    retrain_on_flowers(tf_logdir)
  • Adds a wrapper function init_parser() to generate the argument parser in the scripts/retrain.py script.
  • Adds a function run_with_args(**kwargs) which takes in keyword arguments in the scripts/retrain.py script.
    1. formats keyword arguments to be passed as command-line-like arguments
    2. passes the command-line like arguments to the parser to generate the flags
    3. runs the main(_) function.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here (e.g. I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

@CorbanSwain
Copy link
Author

CLA has been signed.

@googlebot
Copy link

CLAs look good, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants