googlecodelabs / tensorflow-for-poets-2

Apache License 2.0
508 stars 463 forks source link

allowing for non-command line use #101

Open CorbanSwain opened 5 years ago

CorbanSwain commented 5 years ago

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)
googlebot commented 5 years ago

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).

:memo: 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 commented 5 years ago

CLA has been signed.

googlebot commented 5 years ago

CLAs look good, thanks!