Skip to content

Commit

Permalink
move flow code to cli.py
Browse files Browse the repository at this point in the history
  • Loading branch information
abagshaw committed May 21, 2017
1 parent c90040e commit 3d77fbe
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 82 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ cache:
# command to install dependencies
install:
- pip install -r test/requirements-testing.txt
- coverage run -m pip install -e .
- pip install -e .

# command to run tests
script: pytest -x --cov=./

#Upload code coverage statistics
after_success:
- coverage combine
- codecov
40 changes: 40 additions & 0 deletions darkflow/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from .defaults import argHandler #Import the default arguments
import os

def cliHandler(args):
FLAGS = argHandler()
FLAGS.setDefaults()
FLAGS.parseArgs(args)

from darkflow.net.build import TFNet

# make sure all necessary dirs exist
def get_dir(dirs):
for d in dirs:
this = os.path.abspath(os.path.join(os.path.curdir, d))
if not os.path.exists(this): os.makedirs(this)
get_dir([FLAGS.imgdir, FLAGS.binary, FLAGS.backup, os.path.join(FLAGS.imgdir,'out'), FLAGS.summary])

# fix FLAGS.load to appropriate type
try: FLAGS.load = int(FLAGS.load)
except: pass

tfnet = TFNet(FLAGS)

if FLAGS.profile:
tfnet.framework.profile(tfnet)
exit()

if FLAGS.demo:
tfnet.camera(FLAGS.demo, FLAGS.saveVideo)
exit()

if FLAGS.train:
print('Enter training ...'); tfnet.train()
if not FLAGS.savepb: exit('Training finished')

if FLAGS.savepb:
print('Rebuild a constant version ...')
tfnet.savepb(); exit('Done')

tfnet.predict()
62 changes: 30 additions & 32 deletions darkflow/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,36 @@ class argHandler(dict):
__delattr__ = dict.__delitem__
_descriptions = {"help, -h": "show this super helpful message and exit"}

def setDefaults(self):
self.define("imgdir", "./sample_img/", "path to testing directory with images")
self.define("binary", "./bin/", "path to .weights directory")
self.define("config", "./cfg/", "path to .cfg directory")
self.define("dataset", "../pascal/VOCdevkit/IMG/", "path to dataset directory")
self.define("backup", "./ckpt/", "path to backup folder")
self.define("summary", "./summary/", "path to TensorBoard summaries directory")
self.define("annotation", "../pascal/VOCdevkit/ANN/", "path to annotation directory")
self.define("threshold", -0.1, "detection threshold")
self.define("model", "", "configuration of choice")
self.define("trainer", "rmsprop", "training algorithm")
self.define("momentum", 0.0, "applicable for rmsprop and momentum optimizers")
self.define("verbalise", True, "say out loud while building graph")
self.define("train", False, "train the whole net")
self.define("load", "", "how to initialize the net? Either from .weights or a checkpoint, or even from scratch")
self.define("savepb", False, "save net and weight to a .pb file")
self.define("gpu", 0.0, "how much gpu (from 0.0 to 1.0)")
self.define("gpuName", "/gpu:0", "GPU device name")
self.define("lr", 1e-5, "learning rate")
self.define("keep",20,"Number of most recent training results to save")
self.define("batch", 16, "batch size")
self.define("epoch", 1000, "number of epoch")
self.define("save", 2000, "save checkpoint every ? training examples")
self.define("demo", '', "demo on webcam")
self.define("profile", False, "profile")
self.define("json", False, "Outputs bounding box information in json format.")
self.define("saveVideo", False, "Records video from input video or camera")
self.define("pbLoad", "", "path to .pb protobuf file (metaLoad must also be specified)")
self.define("metaLoad", "", "path to .meta file generated during --savepb that corresponds to .pb file")

def define(self, argName, default, description):
self[argName] = default
self._descriptions[argName] = description
Expand Down Expand Up @@ -63,35 +93,3 @@ def parseArgs(self, args):
print("Try running ./flow --help")
exit()
i += 1

def getDefaults():
defaultFLAGS = argHandler()
defaultFLAGS.define("imgdir", "./sample_img/", "path to testing directory with images")
defaultFLAGS.define("binary", "./bin/", "path to .weights directory")
defaultFLAGS.define("config", "./cfg/", "path to .cfg directory")
defaultFLAGS.define("dataset", "../pascal/VOCdevkit/IMG/", "path to dataset directory")
defaultFLAGS.define("backup", "./ckpt/", "path to backup folder")
defaultFLAGS.define("summary", "./summary/", "path to TensorBoard summaries directory")
defaultFLAGS.define("annotation", "../pascal/VOCdevkit/ANN/", "path to annotation directory")
defaultFLAGS.define("threshold", -0.1, "detection threshold")
defaultFLAGS.define("model", "", "configuration of choice")
defaultFLAGS.define("trainer", "rmsprop", "training algorithm")
defaultFLAGS.define("momentum", 0.0, "applicable for rmsprop and momentum optimizers")
defaultFLAGS.define("verbalise", True, "say out loud while building graph")
defaultFLAGS.define("train", False, "train the whole net")
defaultFLAGS.define("load", "", "how to initialize the net? Either from .weights or a checkpoint, or even from scratch")
defaultFLAGS.define("savepb", False, "save net and weight to a .pb file")
defaultFLAGS.define("gpu", 0.0, "how much gpu (from 0.0 to 1.0)")
defaultFLAGS.define("gpuName", "/gpu:0", "GPU device name")
defaultFLAGS.define("lr", 1e-5, "learning rate")
defaultFLAGS.define("keep",20,"Number of most recent training results to save")
defaultFLAGS.define("batch", 16, "batch size")
defaultFLAGS.define("epoch", 1000, "number of epoch")
defaultFLAGS.define("save", 2000, "save checkpoint every ? training examples")
defaultFLAGS.define("demo", '', "demo on webcam")
defaultFLAGS.define("profile", False, "profile")
defaultFLAGS.define("json", False, "Outputs bounding box information in json format.")
defaultFLAGS.define("saveVideo", False, "Records video from input video or camera")
defaultFLAGS.define("pbLoad", "", "path to .pb protobuf file (metaLoad must also be specified)")
defaultFLAGS.define("metaLoad", "", "path to .meta file generated during --savepb that corresponds to .pb file")
return defaultFLAGS
44 changes: 2 additions & 42 deletions flow
Original file line number Diff line number Diff line change
@@ -1,47 +1,7 @@
#! /usr/bin/env python

import sys
import os
from darkflow.defaults import getDefaults #Import the default arguments (if you want to change/add default arguments do it in /darkflow/defaults.py)
from darkflow.cli import cliHandler

def main(args):
defaultFLAGS = getDefaults()
defaultFLAGS.parseArgs(args)
FLAGS = defaultFLAGS #Handle the arguments passed

from darkflow.net.build import TFNet

# make sure all necessary dirs exist
def get_dir(dirs):
for d in dirs:
this = os.path.abspath(os.path.join(os.path.curdir, d))
if not os.path.exists(this): os.makedirs(this)
get_dir([FLAGS.imgdir, FLAGS.binary, FLAGS.backup, os.path.join(FLAGS.imgdir,'out'), FLAGS.summary])

# fix FLAGS.load to appropriate type
try: FLAGS.load = int(FLAGS.load)
except: pass

tfnet = TFNet(FLAGS)

if FLAGS.profile:
tfnet.framework.profile(tfnet)
exit()

if FLAGS.demo:
tfnet.camera(FLAGS.demo, FLAGS.saveVideo)
exit()

if FLAGS.train:
print('Enter training ...'); tfnet.train()
if not FLAGS.savepb: exit('Training finished')

if FLAGS.savepb:
print('Rebuild a constant version ...')
tfnet.savepb(); exit('Done')

tfnet.predict()

if __name__ == "__main__":
main(sys.argv)
cliHandler(sys.argv)

8 changes: 2 additions & 6 deletions test/test_darkflow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from darkflow.net.build import TFNet
from darkflow.cli import cliHandler
import json
import requests
import cv2
Expand Down Expand Up @@ -46,11 +47,6 @@ def download_file(url, savePath):
else:
print("Found existing " + fileName + " file.")

if os.path.isfile(os.path.join(buildPath, "flow")):
os.rename(os.path.join(buildPath, "flow"), os.path.join(buildPath, "flow.py")) #Change flow to flow.py so we can import it
sys.path.insert(0, buildPath) #Add the buildPath to the PATH
from flow import main

yoloWeightPathV1 = os.path.join(buildPath, "bin", yoloDownloadV1.split("/")[-1])
yoloCfgPathV1 = os.path.join(buildPath, "cfg", "v1", "{0}.cfg".format(os.path.splitext(os.path.basename(yoloWeightPathV1))[0]))

Expand All @@ -70,7 +66,7 @@ def executeCLI(commandString):
print("Executing: {0}".format(commandString))
print()
splitArgs = [item.strip() for item in commandString.split(" ")]
main(splitArgs) #Run the command
cliHandler(splitArgs) #Run the command
print()

def compareSingleObjects(firstObject, secondObject, width, height):
Expand Down

0 comments on commit 3d77fbe

Please sign in to comment.