def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, saved_weights_name): if multi_gpu > 1: with tf.device('/cpu:0'): template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size // multi_gpu, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh) else: template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size // multi_gpu, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh) # load the pretrained weight if exists, otherwise load the backend weight only if os.path.exists(saved_weights_name): print("\nLoading pretrained weights.\n") template_model.load_weights(saved_weights_name) else: template_model.load_weights("backend.h5", by_name=True) if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model return train_model, infer_model
def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, saved_weights_name, lr, scales): if multi_gpu > 1: with tf.device('/cpu:0'): template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size // multi_gpu, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, scales=scales) else: template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, scales=scales) # # Setting the session to allow growth, so it doesn't allocate all GPU memory. # gpu_ops = tf.GPUOptions(allow_growth=True) # config = tf.ConfigProto(gpu_options=gpu_ops) # sess = tf.Session(config=config) # # # Setting this as the default tensorflow session. # keras.backend.tensorflow_backend.set_session(sess) # aerial_model = load_model('aerial_model.h5') # print("aerial_model summary", aerial_model.summary()) # backend_model= load_model('backend.h5') # print("backend_model summary", backend_model.summary()) # load the pretrained weight if exists, otherwise load the backend weight only if os.path.exists(saved_weights_name): # print("\nLoading pretrained weights.\n") # print("saved_weights_name", saved_weights_name) # template_model.load_weights(saved_weights_name) template_model.load_weights("backend.h5", by_name=True) print("summary", template_model.summary()) else: template_model.load_weights("backend.h5", by_name=True) if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model optimizer = Adam(lr=lr, clipnorm=0.001) train_model.compile(loss=dummy_loss, optimizer=optimizer, metrics=['accuracy']) return train_model, infer_model
def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, pretrain_weights, lr, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale): if os.path.exists(pretrain_weights): init = False else: init = True if multi_gpu > 1: with tf.device('/cpu:0'): # template_model, infer_model = create_yolov3_model( template_model, infer_model = create_TinyX5_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size // multi_gpu, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale, init=init) else: # template_model, infer_model = create_yolov3_model( template_model, infer_model = create_TinyX5_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale, init=init) # load the pretrained weight if exists, otherwise load the backend weight only if os.path.exists(pretrain_weights): print("\nLoading pretrained weights.\n") template_model.load_weights(pretrain_weights) if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model optimizer = Adam(lr=lr, clipnorm=0.001) train_model.compile(loss=dummy_loss, optimizer=optimizer) return train_model, infer_model
def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, saved_weights_name, lr, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale): if multi_gpu > 1: with tf.device('/cpu:0'): template_model, infer_model = create_scaled_mobilenetv2_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size // multi_gpu, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) else: template_model, infer_model = create_scaled_mobilenetv2_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) # load the pretrained weight if exists, otherwise load the backend weight only if os.path.exists(saved_weights_name): print("\nLoading pretrained weights.\n") template_model.load_weights(saved_weights_name) # else: # template_model.load_weights("backend.h5", by_name=True) if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model # optimizer = Adam(lr=lr, clipnorm=0.001) optimizer = Adam(lr=1e-4, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0) train_model.compile(loss=dummy_loss, optimizer=optimizer) return train_model, infer_model
def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, saved_weights_name, lr, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale): if multi_gpu > 1: with tf.device('/cpu:0'): template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size // multi_gpu, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) else: template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) # load the pretrained weight if exists, otherwise load the backend weight only if os.path.exists(saved_weights_name): print("\nLoading pretrained weights.\n") template_model.load_weights(saved_weights_name) else: template_model.load_weights( "E:\\EPHEC\\Bac_3\\Q2\\TFE\\experiencor\\keras-yolo3-master\\backend.h5", by_name=True) if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model optimizer = Adam(lr=lr, clipnorm=0.001) train_model.compile(loss=dummy_loss, optimizer=optimizer) return train_model, infer_model
def create_model( nb_class, #this is the total number of classes (labels) anchors, #this is the anchors , from the config file max_box_per_image, #max box per image max_grid, batch_size, #max grid, batch size , from the config warmup_batches, #warmup you start with a small learning rate and then gradually increase it by a constant # for each epoch till it reaches “k times learning rate”. ignore_thresh, #ignore threash the confidence level, if iou is less than this just ignore it and consider it false multi_gpu, #multi gpu saved_weights_name, #saved weights file name lr, # grid_scales, #grid scale obj_scale, #object scale noobj_scale, #no obj scale xywh_scale, #xywh scale class_scale #class scale ): if multi_gpu > 1: # we have multi gpu use the best gpu, O:^) >:^) with tf.device('/cpu:0'): #close the cpu, choose best gpu 3:^) #make a template model (training model )and Inference model ( the final model to predict ) template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size // multi_gpu, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) else: #no gpu >:^( T_T use the cpu template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) #=================================================== weights ======================================================# # load the pretrained weight if exists, otherwise load the backend weight only if os.path.exists(saved_weights_name): #if the weight path exist print("\nLoading pretrained weights.\n") #load this weights template_model.load_weights( saved_weights_name ) #add this weights to the template model (training model) else: #the path doesn't exist, first time train for example template_model.load_weights("backend.h5", by_name=True) #get the backend weights #==================================================================================================================# #=============================================== process ==========================================================# if multi_gpu > 1: # we have multi gpu O:^) train_model = multi_gpu_model( template_model, gpus=multi_gpu) #start train multi gpu template model else: #we don't have multi gpu train_model = template_model # the template is the train model #==================================================================================================================# #=========================================== optmizers ============================================================# optimizer = Adam(lr=lr, clipnorm=0.001) #optmizer type and value train_model.compile(loss=dummy_loss, optimizer=optimizer) #loss type = dummy loss #==================================================================================================================# return train_model, infer_model #return the train model and the infer model (note that we didn't do much on infer
def create_model( nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, saved_weights_name, lr, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale ): if multi_gpu > 1: with tf.device('/cpu:0'): template_model, infer_model = create_yolov3_model( nb_class = nb_class, anchors = anchors, max_box_per_image = max_box_per_image, max_grid = max_grid, batch_size = batch_size//multi_gpu, warmup_batches = warmup_batches, ignore_thresh = ignore_thresh, grid_scales = grid_scales, obj_scale = obj_scale, noobj_scale = noobj_scale, xywh_scale = xywh_scale, class_scale = class_scale ) else: template_model, infer_model = create_yolov3_model( nb_class = nb_class, anchors = anchors, max_box_per_image = max_box_per_image, max_grid = max_grid, batch_size = batch_size, warmup_batches = warmup_batches, ignore_thresh = ignore_thresh, grid_scales = grid_scales, obj_scale = obj_scale, noobj_scale = noobj_scale, xywh_scale = xywh_scale, class_scale = class_scale ) # load the pretrained weight if exists, otherwise load the backend weight only if os.path.exists(saved_weights_name): print("\nLoading pretrained weights.\n") template_model.load_weights(saved_weights_name) else: template_model.load_weights("backend.h5", by_name=True) if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model optimizer = Adam(lr=lr, clipnorm=0.001) train_model.compile(loss=dummy_loss, optimizer=optimizer) return train_model, infer_model
def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, saved_weights_name, lr, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale, optimizer=None): if multi_gpu > 1: logging.info(f"Create model for {multi_gpu} gpus") with tf.device('/cpu:0'): template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size // multi_gpu, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) else: template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) # load the pretrained weight if exists, otherwise load the backend weight only if os.path.exists(saved_weights_name): logging.info("Loading pretrained weights.") template_model.load_weights(saved_weights_name) elif os.path.exists("backend.h5"): logging.info("Loading default backend weights.") template_model.load_weights("backend.h5", by_name=True) else: logging.info("Training from scratch") if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model if not optimizer: optimizer = Adam(lr=lr, clipnorm=0.001) train_model.compile(loss=dummy_loss, optimizer=optimizer) return train_model, infer_model
def create_model( config, max_box_per_image, warmup_batches, multi_gpu, saved_weights_name, lr ): if config["model"]["model_name"] == "yolov3": print('[INFO] YOLOV3 Model Creating...') if multi_gpu > 1: with tf.device('/cpu:0'): yolo_model = YOLOV3( config=config, max_box_per_image=max_box_per_image, batch_size=config["train"]["batch_size"] // multi_gpu, warmup_batches=warmup_batches) template_model, infer_model = yolo_model.model() else: yolo_model = YOLOV3( config=config, max_box_per_image=max_box_per_image, batch_size=config["train"]["batch_size"], warmup_batches=warmup_batches) template_model, infer_model = yolo_model.model() elif config["model"]["model_name"] == "yolov4": print('[INFO] YOLOV4 Model Creating...') if multi_gpu > 1: with tf.device('/cpu:0'): yolo_model = YOLOV4( config=config, max_box_per_image=max_box_per_image, batch_size=config["train"]["batch_size"] // multi_gpu, warmup_batches=warmup_batches) template_model, infer_model = yolo_model.model() else: yolo_model = YOLOV4( config=config, max_box_per_image=max_box_per_image, batch_size=config["train"]["batch_size"], warmup_batches=warmup_batches) template_model, infer_model = yolo_model.model() else: pass # load the pretrained weight if exists, otherwise load the backend weight only if os.path.exists(saved_weights_name): print("[INFO] Find pretrained weights...") print("\n[INFO] Loading pretrained weights...\n") template_model.load_weights(saved_weights_name) # else: # template_model.load_weights("backend.h5", by_name=True) if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model optimizer = Adam(lr=lr, clipnorm=0.001) train_model.compile(loss=dummy_loss, optimizer=optimizer) return train_model, infer_model
def create_model( create_model_func, loss_func, nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, saved_weights_name, pretrained_weights, lr, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale, width, height ): if multi_gpu > 1: with tf.device('/cpu:0'): template_model, infer_model = create_model_func( nb_class = nb_class, anchors = anchors, max_box_per_image = max_box_per_image, max_grid = max_grid, batch_size = batch_size//multi_gpu, warmup_batches = warmup_batches, ignore_thresh = ignore_thresh, grid_scales = grid_scales, obj_scale = obj_scale, noobj_scale = noobj_scale, xywh_scale = xywh_scale, class_scale = class_scale, width = width, height = height ) else: template_model, infer_model = create_model_func( nb_class = nb_class, anchors = anchors, max_box_per_image = max_box_per_image, max_grid = max_grid, batch_size = batch_size, warmup_batches = warmup_batches, ignore_thresh = ignore_thresh, grid_scales = grid_scales, obj_scale = obj_scale, noobj_scale = noobj_scale, xywh_scale = xywh_scale, class_scale = class_scale, width = width, height = height ) # load the pretrained weight if exists, otherwise load the pretrained weights only if saved_weights_name and os.path.exists(saved_weights_name): print("\nLoading previously saved weights.\n") template_model.load_weights(saved_weights_name) elif pretrained_weights and os.path.exists(pretrained_weights): print("\nLoading pretrained weights.\n") by_name = False if pretrained_weights.endswith('yolo-weights.h5') or pretrained_weights.endswith('yolo-tiny-weights.h5'): by_name = True template_model.load_weights(pretrained_weights, by_name=by_name) else: print('\nNo weights to load!\n') if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model optimizer = Adam(lr=lr, clipnorm=0.001) train_model.compile(loss=loss_func, optimizer=optimizer) return train_model, infer_model
def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, saved_weights_name, pre_trained_weights, lr, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale, model_type="full", input_image_size=(None, None, 3)): if multi_gpu > 1: with tf.device('/cpu:0'): template_model, infer_model = yolo.create_yolo_model( model_type, nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size // multi_gpu, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale, input_image_size=(input_image_size[1], input_image_size[0], input_image_size[2])) else: template_model, infer_model = yolo.create_yolo_model( model_type, nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale, input_image_size=(input_image_size[1], input_image_size[0], input_image_size[2])) # load the pretrained weight if exists, otherwise load the backend weight only if os.path.exists(saved_weights_name): print("\nLoading pretrained weights.\n") template_model.load_weights(saved_weights_name) elif pre_trained_weights: template_model.load_weights(pre_trained_weights) else: print("Not using pre trained weights!") if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model optimizer = Adam(lr=lr, clipnorm=0.001) train_model.compile(loss=yolo.dummy_loss, optimizer=optimizer) return train_model, infer_model
def create_model( nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, load_weights_name, lr, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale ): if multi_gpu > 1: with tf.device('/cpu:0'): template_model, infer_model = create_mobilenetv2_yolov3_model( nb_class = nb_class, anchors = anchors, max_box_per_image = max_box_per_image, max_grid = max_grid, batch_size = batch_size//multi_gpu, warmup_batches = warmup_batches, ignore_thresh = ignore_thresh, grid_scales = grid_scales, obj_scale = obj_scale, noobj_scale = noobj_scale, xywh_scale = xywh_scale, class_scale = class_scale ) else: template_model, infer_model = create_mobilenetv2_yolov3_model( nb_class = nb_class, anchors = anchors, max_box_per_image = max_box_per_image, max_grid = max_grid, batch_size = batch_size, warmup_batches = warmup_batches, ignore_thresh = ignore_thresh, grid_scales = grid_scales, obj_scale = obj_scale, noobj_scale = noobj_scale, xywh_scale = xywh_scale, class_scale = class_scale ) # load the pretrained weight if exists, otherwise load the backend weight only if os.path.exists(load_weights_name + ".h5"): print("\n===========Loading pretrained weights.===========") print(load_weights_name + ".h5\n") template_model.load_weights(load_weights_name + ".h5") # else: # template_model.load_weights("backend.h5", by_name=True) if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model optimizer = Adam(lr=lr, clipnorm=0.001) train_model.compile(loss=dummy_loss, optimizer=optimizer) # print(train_model.summary()) return train_model, infer_model
def create_model(nb_class, anchors, max_box_per_image, max_grid, batch_size, warmup_batches, ignore_thresh, multi_gpu, saved_weights_name, lr, grid_scales, obj_scale, noobj_scale, xywh_scale, class_scale, tiny=False): if multi_gpu > 1: if tiny: with tf.device('/cpu:0'): template_model, infer_model = create_yolov3_tiny_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size // multi_gpu, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) else: with tf.device('/cpu:0'): template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size // multi_gpu, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) else: if tiny: template_model, infer_model = create_yolov3_tiny_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) else: template_model, infer_model = create_yolov3_model( nb_class=nb_class, anchors=anchors, max_box_per_image=max_box_per_image, max_grid=max_grid, batch_size=batch_size, warmup_batches=warmup_batches, ignore_thresh=ignore_thresh, grid_scales=grid_scales, obj_scale=obj_scale, noobj_scale=noobj_scale, xywh_scale=xywh_scale, class_scale=class_scale) # load the pretrained weight if exists, otherwise start from scratch if os.path.exists(saved_weights_name): print("\nLoading pretrained weights.\n") try: template_model.load_weights(saved_weights_name) except: print("Weights file failed to load, starting from scratch") else: print("No pre-trained weights available, starting from scratch") if multi_gpu > 1: train_model = multi_gpu_model(template_model, gpus=multi_gpu) else: train_model = template_model optimizer = Adam(lr=lr, clipnorm=0.001) train_model.compile(loss=dummy_loss, optimizer=optimizer) return train_model, infer_model