def train_phase(self): phase_idx = [ i for i in range(len(self.train_steps)) if self.train_steps[i] <= self.current_epoch ][-1] lr = self.lr_steps[phase_idx] upper_epoch = self.train_steps[ phase_idx + 1] if phase_idx < len(self.train_steps) - 1 else self.total_epochs number_of_epochs = upper_epoch - self.current_epoch print("====== Start of a new training phase ======") print("Learning Rate: {}".format(lr)) print("Number of epochs: {}".format(number_of_epochs)) #print("Momentum: {}".format(self.momentum_steps[phase_idx])) print("===========================================") # set args for new phase options = dict(self.base_options) if self.current_epoch > 0: assert 'load' in options options['lr'] = lr #options['momentum'] = self.momentum_steps[phase_idx] options['epoch'] = number_of_epochs # train tfnet = TFNet(options) tfnet.train() self.current_epoch += number_of_epochs
def train_darkflow_model(cfg_obj,load_ckpt): options = darkflow_parse_config(cfg_obj) options['train'] = True if load_ckpt is not None: options['load'] = int(load_ckpt) tfnet = TFNet(options) tfnet.train()
def train_yolo(model_cfg, weights, batch_size, epoch, annotation_path, image_path, label_path, backup_path): options = { "model": model_cfg, "load": weights, "batch": batch_size, "epoch": epoch, "train": True, "verbalise": False, "annotation": annotation_path, "dataset": image_path, "labels": label_path, "backup": backup_path } tfnet = TFNet(options) tfnet.train()
def trainFlowCFG(cfg): ckptPath = 'ckpt' checkpointPath = 'ckpt/checkpoint' if not os.path.exists(ckptPath): os.makedirs(ckptPath) if not os.path.exists(checkpointPath): os.makedirs(checkpointPath) #dataset is the image folder options = {"model": cfg['darkflow']["model"], "load": cfg['darkflow']["starting_weights"], "batch": cfg['darkflow']["batch_size"], "epoch": cfg['darkflow']["epoch"], "gpu": cfg['darkflow']["gpu_usage"], "train": True, "lr": float(cfg['darkflow']["learning_rate"]), "annotation": cfg['darkflow']["training_annotations"], "labels": cfg['darkflow']["labels_file"], "dataset": cfg['darkflow']["training_images"]} tfnet = TFNet(options) tfnet.train() tfnet.savepb() if cfg['meta']['saveRun']: modelPath = cfg['darkflow']['model'] path, filename = os.path.split(modelPath) name, ext = os.path.splitext(filename) pathPB = os.path.join(cfg['temp']['rootDir'],cfg['paths']['darkflow'],'built_graph', name+'.pb') pathMeta = os.path.join(cfg['temp']['rootDir'],cfg['paths']['darkflow'],'built_graph', name+'.meta') savePathPB = os.path.join(cfg['temp']['rootDir'],'runData', cfg['meta']['runName'],cfg['meta']['runName']+'.pb') savePathMeta = os.path.join(cfg['temp']['rootDir'], 'runData', cfg['meta']['runName'],cfg['meta']['runName']+'.meta') shutil.copyfile(pathPB,savePathPB) shutil.copyfile(pathMeta,savePathMeta)
def cliHandler(args): FLAGS = argHandler() FLAGS.setDefaults() FLAGS.parseArgs(args) # 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.demo: tfnet.camera() exit('Demo stopped, exit.') if FLAGS.train: print('Enter training ...') tfnet.train() if not FLAGS.savepb: exit('Training finished, exit.') if FLAGS.savepb: print('Rebuild a constant version ...') tfnet.savepb() exit('Done') tfnet.predict()
class Model: def __init__(self, **kwargs): self.tf_net = TFNet(kwargs) self.resolution = (640, 360) self.fps = 29.9 print('Start time ', datetime.datetime.now()) def train(self): self.tf_net.train() print('Train end time ', datetime.datetime.now()) def draw_box(self, input_video, output_video, limit=300): counter = 0 four_cc = cv2.VideoWriter_fourcc(*'mp4v') video_out = cv2.VideoWriter(output_video, four_cc, self.fps, self.resolution) video_input = cv2.VideoCapture(input_video) while True: success, frame = video_input.read() frame = cv2.resize(frame, self.resolution) if not success or counter > limit: break results = self.tf_net.return_predict(frame) for res in results: if res['label'] == 'person': frame = cv2.rectangle( frame, (res['topleft']['x'], res['topleft']['y']), (res['bottomright']['x'], res['bottomright']['y']), [0, 0, 255], 1 ) counter += 1 video_out.write(frame) video_out.release()
from darkflow.net.build import TFNet #%config InlineBackend.figure_format = 'svg' options = {"model": "cfg/yolo.cfg", "load": "bin/yolov2.weights", "batch": 8, "epoch": 100, "train": True, "annotation": "new_data/annots/", "dataset": "new_data/images/"} # 'gpu': 1.0, tfnet = TFNet(options) tfnet.train() ## si queremos hacerlo por consola # python flow --model cfg/yolo.cfg --load bin/yolov2.weights --train --annotation new_data\annots --dataset new_data\images --epoch 1
from darkflow.net.build import TFNet if __name__ == '__main__': options = { "model": "/media/mensa/Data/Task/EgyALPR/darkflow/cfg/yolo.cfg", "load": "/media/mensa/Data/Task/EgyALPR/darkflow/bin/yolo.weights", "batch": 8, "epoch": 100, "gpu": 0.9, "train": True, "annotation": "/media/mensa/Data/Task/EgyALPR/training_dataset/xml", "dataset": "/media/mensa/Data/Task/EgyALPR/training_dataset/images" } tf_net = TFNet(options) tf_net.train() tf_net.savepb()
import matplotlib.pyplot as plt import numpy as np from darkflow.net.build import TFNet import cv2 # yolov2-new was created by modifying yolov2-new.cfg as per instructions in darkflow repo options = { "model": "cfg/yolov2-voc2012.cfg", "load": "bin/yolov2.weights", "epoch": 2, "train": True, "annotation": "./VOCtrainval_11-May-2012/VOCdevkit/VOC2012/Annotations/", "dataset": "./VOCtrainval_11-May-2012/VOCdevkit/VOC2012/JPEGImages/", "labels": "labels-voc2012.txt" } tfnet = TFNet(options) tfnet.train() # creates automatically a ckpt folder containing the checkpoint tfnet.savepb() # creates the built_graph folder
from darkflow.net.build' import TFNet import tensorflow as tf config=tf.ConfigProto(log_device_placement=True) with tf.Session(config=config) as sess: options={'model':'G:/darkflow/cfg/yolo1.cfg', 'load':'G:/darkflow/bin/yolo.weights', 'epoch':120, 'gpu':0.5, 'train':True, 'batch':4, 'annotation':'G:/chexray/Training_YOLO/annotations', 'dataset':'G:/chexray/Training_YOLO/images'} tfn=TFNet(options) tfn.train() from darkflow.net.build import TFNet import tensorflow as tf config=tf.ConfigProto(log_device_placement=True) with tf.Session(config=config) as sess: options={'model':'G:/darkflow/cfg/yolo1.cfg', 'load':10440, 'gpu':0.5, 'threshold':0.2 } tfn=TFNet(options) import cv2 image=cv2.imread('G:/chexray/images/test.png') result=tfn.return_predict(image) for r in result: tl=r['topleft']['x'],r['topleft']['y']
from darkflow.net.build import TFNet #darkflow is supporting library to build model options = {'model' : 'cfg/yolotiny.cfg', #making dictionary for inputs to feed in model 'load' : 'bin/yolotiny.weights', 'batch' : 2, 'epoch' : 200, 'gpu' : 1.0, 'train' : True, 'annotation' : './xml', 'dataset' : './Images1', 'threshold' : 0.1 } tfnet = TFNet(options) #setting up layers tfnet.train() #train
import matplotlib.pyplot as plt ''' ########################################## Step 1: Create the training model ########################################## ''' #Some variables for training the model options = { "model": "cfg/model_608_coco/yolov2-1c.cfg", #Training the 1 class model # "load": "bin/yolov2-coco-608x608.weights", #Start training on original weights "load": -1, #Resume loading from most recently trained weights "batch": 1, #Oringially 2 "epoch": 20, #Originally 5 "train": True, # "annotation": "../Soccerball/annots/", # "dataset": "../Soccerball/images/", "annotation": "../Kangaroo/annots/", "dataset": "../Kangaroo/images/", "gpu": 1.0, "save": 175 # save/batch is how many steps the model saves. } #Create a tensorflow network object tfnetTrain = TFNet(options) #Train the model with the options it was created with tfnetTrain.train() print("Done training yo.")