Esempio n. 1
0
 def __init__(self, test_name):
     """
     Init some specify data that is different among sub test classes.
     Should be called first by super().__init__ in all sub test classes.
     :param test_name: name of test case.
     """
     self.test_result = TestResult(test_name)
     self.steps = Steps()
     self.logger = Logger(test_name)
Esempio n. 2
0
 def init_data_test(self):
     """
     Init test data.
     If the test case need some extra test date then just override this method.
     """
     test_name = os.path.splitext(os.path.basename(inspect.getfile(self.__class__)))[0]
     self.test_result = TestResult(test_name)
     self.steps = Steps()
     self.logger = Logger(test_name)
def main():
    setpu_seed(2021)
    args = parse_args()
    save_path = os.path.join(args.outf, args.save)

    device = torch.device(
        "cuda" if torch.cuda.is_available() and args.cuda else "cpu")
    cudnn.benchmark = True

    log = Logger(save_path)
    sys.stdout = Print_Logger(os.path.join(save_path, 'train_log.txt'))
    print('The computing device used is: ',
          'GPU' if device.type == 'cuda' else 'CPU')

    # net = models.UNetFamily.U_Net(1,2).to(device)
    net = models.LadderNet(inplanes=1, num_classes=2, layers=3,
                           filters=16).to(device)
    print("Total number of parameters: " + str(count_parameters(net)))

    log.save_graph(
        net,
        torch.randn((1, 1, 64, 64)).to(device).to(
            device=device))  # Save the model structure to the tensorboard file
    # torch.nn.init.kaiming_normal(net, mode='fan_out')      # Modify default initialization method
    # net.apply(weight_init)

    # The training speed of this task is fast, so pre training is not recommended
    if args.pre_trained is not None:
        # Load checkpoint.
        print('==> Resuming from checkpoint..')
        checkpoint = torch.load(args.outf +
                                '%s/latest_model.pth' % args.pre_trained)
        net.load_state_dict(checkpoint['net'])
        optimizer.load_state_dict(checkpoint['optimizer'])
        args.start_epoch = checkpoint['epoch'] + 1

    # criterion = LossMulti(jaccard_weight=0,class_weights=np.array([0.5,0.5]))
    criterion = CrossEntropyLoss2d()  # Initialize loss function

    # create a list of learning rate with epochs
    # lr_epoch = np.array([50, args.N_epochs])
    # lr_value = np.array([0.001, 0.0001])
    # lr_schedule = make_lr_schedule(lr_epoch,lr_value)
    # lr_scheduler = optim.lr_scheduler.StepLR(optimizer,step_size=10,gamma=0.5)
    # optimizer = optim.SGD(net.parameters(),lr=lr_schedule[0], momentum=0.9, weight_decay=5e-4, nesterov=True)
    optimizer = optim.Adam(net.parameters(), lr=args.lr)
    lr_scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer,
                                                        T_max=args.N_epochs,
                                                        eta_min=0)

    train_loader, val_loader = get_dataloader(args)  # create dataloader

    if args.val_on_test:
        print(
            '\033[0;32m===============Validation on Testset!!!===============\033[0m'
        )
        val_tool = Test(args)

    best = {
        'epoch': 0,
        'AUC_roc': 0.5
    }  # Initialize the best epoch and performance(AUC of ROC)
    trigger = 0  # Early stop Counter
    for epoch in range(args.start_epoch, args.N_epochs + 1):
        print('\nEPOCH: %d/%d --(learn_rate:%.6f) | Time: %s' % \
            (epoch, args.N_epochs,optimizer.state_dict()['param_groups'][0]['lr'], time.asctime()))

        # train stage
        train_log = train(train_loader, net, criterion, optimizer, device)
        # val stage
        if not args.val_on_test:
            val_log = val(val_loader, net, criterion, device)
        else:
            val_tool.inference(net)
            val_log = val_tool.val()

        log.update(epoch, train_log, val_log)  # Add log information
        lr_scheduler.step()

        # Save checkpoint of latest and best model.
        state = {
            'net': net.state_dict(),
            'optimizer': optimizer.state_dict(),
            'epoch': epoch
        }
        torch.save(state, os.path.join(save_path, 'latest_model.pth'))
        trigger += 1
        if val_log['val_auc_roc'] > best['AUC_roc']:
            print('\033[0;33mSaving best model!\033[0m')
            torch.save(state, os.path.join(save_path, 'best_model.pth'))
            best['epoch'] = epoch
            best['AUC_roc'] = val_log['val_auc_roc']
            trigger = 0
        print('Best performance at Epoch: {} | AUC_roc: {}'.format(
            best['epoch'], best['AUC_roc']))
        # early stopping
        if not args.early_stop is None:
            if trigger >= args.early_stop:
                print("=> early stopping")
                break
        torch.cuda.empty_cache()
Esempio n. 4
0

def gpu_memory_in_use(gpu='/device:GPU:0', log=None):
    from tensorflow.contrib.memory_stats.python.ops.memory_stats_ops import BytesInUse
    with tf.device(gpu):
        bytes_in_use = BytesInUse()
    with tf.Session() as sess:
        val = sess.run(bytes_in_use)
    if log is not None:
        log.P("  GPU free mem: {:.1f} Gb".format(val / (1024**3)))
    return val


if __name__ == '__main__':
    cfg = 'config/duplex_config.txt'
    log = Logger(lib_name='MGS', config_file=cfg, TF_KERAS=True)

    yolo = log.LoadGraphFromModels('01_1712_y_720_1280_c.pb')
    face = log.LoadGraphFromModels('20_190301_mob_ssd_faces.pb')

    config_proto = tf.compat.v1.ConfigProto()
    config_proto.gpu_options.allow_growth = True
    yolo_sess = tf.compat.v1.Session(graph=yolo, config=config_proto)
    log.P("Created yolo session")

    config_proto = tf.compat.v1.ConfigProto()
    config_proto.gpu_options.allow_growth = True
    face_sess = tf.compat.v1.Session(graph=face, config=config_proto)
    log.P("Created ssd session")

    tf_face_classes = face_sess.graph.get_tensor_by_name("detection_classes:0")
  log.P("*"*50)
  log.P("LocalConfig v{} received config data:".format(__VER__))
  dct_prev = log.load_data_json(fn_config)
  log.P("  Current: {}".format(dct_prev))
  log.save_data_json(dct_config, fn_config )
  dct_new = log.load_data_json(fn_config)
  log.P("  New:     {}".format(dct_config))
  jresponse = flask.jsonify({
            "RECEIVED_CONFIG_UPDATE": dct_new})    
  jresponse.headers["Access-Control-Allow-Origin"] = "*"
  jresponse.headers["Access-Control-Allow-Methods"] = "POST, GET, OPTIONS, DELETE"
  jresponse.headers["Access-Control-Allow-Headers"] = "Content-Type"
  log.P("*"*50)
  return jresponse
  
  

if __name__ == '__main__':
  config_file = 'config/duplex_config.txt'
  log = Logger(lib_name='ODJcfg', config_file=config_file,  TF_KERAS=False)
  log.P("Starting OmniDJ local config server {}".format(__VER__))
  dct_viewer = log.load_data_json(fn_config)
  log.P("Currend config:\n{}".format(dct_viewer))
  
  app = flask.Flask('LocalConfigServer')
  app.add_url_rule(rule='/config', 
                   endpoint="LocalConfig", 
                   view_func=config_update, 
                   methods = ['GET', 'POST','OPTIONS']
                   )  
  app.run(host='127.0.0.1', port=5500)
Esempio n. 6
0
"""
import tensorflow as tf
from libraries.logger import Logger


def combine_graphs(graph1, graph2, name1='g1', name2='g2'):
    gdef_1 = graph1.as_graph_def()
    gdef_2 = graph2.as_graph_def()
    with tf.Graph().as_default() as g_combined:
        tf.import_graph_def(graph_def=gdef_1, name=name1)
        tf.import_graph_def(graph_def=gdef_2, name=name2)
    return g_combined


if __name__ == '__main__':
    log = Logger(lib_name='GC', config_file='config/duplex_config.txt')
    g1 = log.LoadGraphFromModels('01_1712_y_720_1280_c.pb')
    n1 = 'yolo'
    g2 = log.LoadGraphFromModels('20_190301_mob_ssd_faces.pb')
    n2 = 'face'
    gc = combine_graphs(g1, g2, name1=n1, name2=n2)

    config_proto = tf.compat.v1.ConfigProto()
    config_proto.gpu_options.allow_growth = True
    sess = tf.compat.v1.Session(graph=gc, config=config_proto)

    earning_phase = sess.graph.get_tensor_by_name(n1 +
                                                  "/keras_learning_phase:0")
    tf_classes = sess.graph.get_tensor_by_name(n1 + "/YOLO_OUTPUT_CLASSES" +
                                               ":0")
    tf_scores = sess.graph.get_tensor_by_name(n1 + "/YOLO_OUTPUT_SCORES" +