def __init__(self, ds_test, batch_size=1, num_workers=4, loss='JointsMSELoss', checkpoint_path="./weights/pose_hrnet_w48_384x288.pth", model_c=48, model_nof_joints=17, model_bn_momentum=0.1, flip_test_images=True, device=None): """ Initializes a new Test object. The HRNet model is initialized and the saved checkpoint is loaded. The DataLoader and the loss function are defined. Args: ds_test (HumanPoseEstimationDataset): test dataset. batch_size (int): batch size. Default: 1 num_workers (int): number of workers for each DataLoader Default: 4 loss (str): loss function. Valid values are 'JointsMSELoss' and 'JointsOHKMMSELoss'. Default: "JointsMSELoss" checkpoint_path (str): path to a previous checkpoint. Default: None model_c (int): hrnet parameters - number of channels. Default: 48 model_nof_joints (int): hrnet parameters - number of joints. Default: 17 model_bn_momentum (float): hrnet parameters - path to the pretrained weights. Default: 0.1 flip_test_images (bool): flip images during validating. Default: True device (torch.device): device to be used (default: cuda, if available). Default: None """ super(Test, self).__init__() hrnet_m = 'HRNet' hrnet_c = 48 hrnet_j = 17 hrnet_weights = "./weights/pose_hrnet_w48_384x288.pth" hrnet_joints_set = "coco" image_resolution = (384, 288) single_person = False disable_tracking = False max_batch_size = 16 self.ds_test = ds_test self.batch_size = batch_size self.num_workers = num_workers self.loss = loss self.checkpoint_path = checkpoint_path self.model_c = model_c self.model_nof_joints = model_nof_joints self.model_bn_momentum = model_bn_momentum self.flip_test_images = flip_test_images self.epoch = 0 # torch device if device is not None: self.device = device else: if torch.cuda.is_available(): self.device = torch.device('cuda:0') else: self.device = torch.device('cpu') print(self.device) # # load model #self.model = HRNet(c=self.model_c, nof_joints=self.model_nof_joints, # bn_momentum=self.model_bn_momentum).to(self.device) image_resolution = ast.literal_eval(image_resolution) self.model = SimpleHRNet(hrnet_c, hrnet_j, hrnet_weights, model_name=hrnet_m, resolution=image_resolution, multiperson=not single_person, return_bounding_boxes=not disable_tracking, max_batch_size=max_batch_size, device=device) # # define loss if self.loss == 'JointsMSELoss': self.loss_fn = JointsMSELoss().to(self.device) elif self.loss == 'JointsOHKMMSELoss': self.loss_fn = JointsOHKMMSELoss().to(self.device) else: raise NotImplementedError # # load previous checkpoint if os.path.basename( self.checkpoint_path) == "pose_hrnet_w48_384x288.pth": self.starting_epoch = 210 # 1? #self.params = None elif self.checkpoint_path is not None: print('Loading checkpoint %s...' % self.checkpoint_path) if os.path.isdir(self.checkpoint_path): path = os.path.join(self.checkpoint_path, 'checkpoint_last.pth') else: path = self.checkpoint_path self.starting_epoch, self.model, _, self.params = load_checkpoint( path, self.model, device=self.device) else: raise ValueError('checkpoint_path is not defined') # # load test dataset self.dl_test = DataLoader(self.ds_test, batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers) self.len_dl_test = len(self.dl_test) #print("print len_dl_test: ", self.len_dl_test) # # initialize variables self.mean_loss_test = 0. self.mean_acc_test = 0.
def __init__(self, exp_name, ds_train, ds_val, epochs=210, batch_size=16, num_workers=4, loss='JointsMSELoss', lr=0.001, lr_decay=True, lr_decay_steps=(170, 200), lr_decay_gamma=0.1, optimizer='Adam', weight_decay=0., momentum=0.9, nesterov=False, pretrained_weight_path=None, checkpoint_path=None, log_path='./logs', use_tensorboard=True, model_c=48, model_nof_joints=17, model_bn_momentum=0.1, flip_test_images=True, device=None ): """ Initializes a new Train object. The log folder is created, the HRNet model is initialized and optional pre-trained weights or saved checkpoints are loaded. The DataLoaders, the loss function, and the optimizer are defined. Args: exp_name (str): experiment name. ds_train (HumanPoseEstimationDataset): train dataset. ds_val (HumanPoseEstimationDataset): validation dataset. epochs (int): number of epochs. Default: 210 batch_size (int): batch size. Default: 16 num_workers (int): number of workers for each DataLoader Default: 4 loss (str): loss function. Valid values are 'JointsMSELoss' and 'JointsOHKMMSELoss'. Default: "JointsMSELoss" lr (float): learning rate. Default: 0.001 lr_decay (bool): learning rate decay. Default: True lr_decay_steps (tuple): steps for the learning rate decay scheduler. Default: (170, 200) lr_decay_gamma (float): scale factor for each learning rate decay step. Default: 0.1 optimizer (str): network optimizer. Valid values are 'Adam' and 'SGD'. Default: "Adam" weight_decay (float): weight decay. Default: 0. momentum (float): momentum factor. Default: 0.9 nesterov (bool): Nesterov momentum. Default: False pretrained_weight_path (str): path to pre-trained weights (such as weights from pre-train on imagenet). Default: None checkpoint_path (str): path to a previous checkpoint. Default: None log_path (str): path where tensorboard data and checkpoints will be saved. Default: "./logs" use_tensorboard (bool): enables tensorboard use. Default: True model_c (int): hrnet parameters - number of channels. Default: 48 model_nof_joints (int): hrnet parameters - number of joints. Default: 17 model_bn_momentum (float): hrnet parameters - path to the pretrained weights. Default: 0.1 flip_test_images (bool): flip images during validating. Default: True device (torch.device): device to be used (default: cuda, if available). Default: None """ super(Train, self).__init__() self.exp_name = exp_name self.ds_train = ds_train self.ds_val = ds_val self.epochs = epochs self.batch_size = batch_size self.num_workers = num_workers self.loss = loss self.lr = lr self.lr_decay = lr_decay self.lr_decay_steps = lr_decay_steps self.lr_decay_gamma = lr_decay_gamma self.optimizer = optimizer self.weight_decay = weight_decay self.momentum = momentum self.nesterov = nesterov self.pretrained_weight_path = pretrained_weight_path self.checkpoint_path = checkpoint_path self.log_path = os.path.join(log_path, self.exp_name) self.use_tensorboard = use_tensorboard self.model_c = model_c self.model_nof_joints = model_nof_joints self.model_bn_momentum = model_bn_momentum self.flip_test_images = flip_test_images self.epoch = 0 # torch device if device is not None: self.device = device else: if torch.cuda.is_available(): self.device = torch.device('cuda:0') else: self.device = torch.device('cpu') print(self.device) os.makedirs(self.log_path, 0o755, exist_ok=False) # exist_ok=False to avoid overwriting if self.use_tensorboard: self.summary_writer = tb.SummaryWriter(self.log_path) # # write all experiment parameters in parameters.txt and in tensorboard text field self.parameters = [x + ': ' + str(y) + '\n' for x, y in locals().items()] with open(os.path.join(self.log_path, 'parameters.txt'), 'w') as fd: fd.writelines(self.parameters) if self.use_tensorboard: self.summary_writer.add_text('parameters', '\n'.join(self.parameters)) # # load model self.model = HRNet(c=self.model_c, nof_joints=self.model_nof_joints, bn_momentum=self.model_bn_momentum).to(self.device) # # define loss and optimizers if self.loss == 'JointsMSELoss': self.loss_fn = JointsMSELoss().to(self.device) elif self.loss == 'JointsOHKMMSELoss': self.loss_fn = JointsOHKMMSELoss().to(self.device) else: raise NotImplementedError if optimizer == 'SGD': self.optim = SGD(self.model.parameters(), lr=self.lr, weight_decay=self.weight_decay, momentum=self.momentum, nesterov=self.nesterov) elif optimizer == 'Adam': self.optim = Adam(self.model.parameters(), lr=self.lr, weight_decay=self.weight_decay) else: raise NotImplementedError # # load pre-trained weights (such as those pre-trained on imagenet) if self.pretrained_weight_path is not None: self.model.load_state_dict(torch.load(self.pretrained_weight_path, map_location=self.device), strict=True) print('Pre-trained weights loaded.') # # load previous checkpoint if self.checkpoint_path is not None: print('Loading checkpoint %s...' % self.checkpoint_path) if os.path.isdir(self.checkpoint_path): path = os.path.join(self.checkpoint_path, 'checkpoint_last.pth') else: path = self.checkpoint_path self.starting_epoch, self.model, self.optim, self.params = load_checkpoint(path, self.model, self.optim, self.device) else: self.starting_epoch = 0 if lr_decay: self.lr_scheduler = MultiStepLR(self.optim, list(self.lr_decay_steps), gamma=self.lr_decay_gamma, last_epoch=self.starting_epoch if self.starting_epoch else -1) # # load train and val datasets self.dl_train = DataLoader(self.ds_train, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers, drop_last=True) self.len_dl_train = len(self.dl_train) # dl_val = DataLoader(self.ds_val, batch_size=1, shuffle=False, num_workers=num_workers) self.dl_val = DataLoader(self.ds_val, batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers) self.len_dl_val = len(self.dl_val) # # initialize variables self.mean_loss_train = 0. self.mean_acc_train = 0. self.mean_loss_val = 0. self.mean_acc_val = 0. self.mean_mAP_val = 0. self.best_loss = None self.best_acc = None self.best_mAP = None
def __init__(self, ds_test, batch_size=1, num_workers=4, loss='JointsMSELoss', checkpoint_path=None, model_c=48, model_nof_joints=17, model_bn_momentum=0.1, flip_test_images=True, device=None): """ Initializes a new Test object. The HRNet model is initialized and the saved checkpoint is loaded. The DataLoader and the loss function are defined. Args: ds_test (HumanPoseEstimationDataset): test dataset. batch_size (int): batch size. Default: 1 num_workers (int): number of workers for each DataLoader Default: 4 loss (str): loss function. Valid values are 'JointsMSELoss' and 'JointsOHKMMSELoss'. Default: "JointsMSELoss" checkpoint_path (str): path to a previous checkpoint. Default: None model_c (int): hrnet parameters - number of channels. Default: 48 model_nof_joints (int): hrnet parameters - number of joints. Default: 17 model_bn_momentum (float): hrnet parameters - path to the pretrained weights. Default: 0.1 flip_test_images (bool): flip images during validating. Default: True device (torch.device): device to be used (default: cuda, if available). Default: None """ super(Test, self).__init__() self.ds_test = ds_test self.batch_size = batch_size self.num_workers = num_workers self.loss = loss self.checkpoint_path = checkpoint_path self.model_c = model_c self.model_nof_joints = model_nof_joints self.model_bn_momentum = model_bn_momentum self.flip_test_images = flip_test_images self.epoch = 0 # torch device if device is not None: self.device = device else: if torch.cuda.is_available(): self.device = torch.device('cuda:0') else: self.device = torch.device('cpu') print(self.device) # # load model self.model = HRNet(c=self.model_c, nof_joints=self.model_nof_joints, bn_momentum=self.model_bn_momentum).to(self.device) # # define loss if self.loss == 'JointsMSELoss': self.loss_fn = JointsMSELoss().to(self.device) elif self.loss == 'JointsOHKMMSELoss': self.loss_fn = JointsOHKMMSELoss().to(self.device) else: raise NotImplementedError # # load previous checkpoint if self.checkpoint_path is not None: print('Loading checkpoint %s...' % self.checkpoint_path) if os.path.isdir(self.checkpoint_path): path = os.path.join(self.checkpoint_path, 'checkpoint_last.pth') else: path = self.checkpoint_path self.starting_epoch, self.model, _, self.params = load_checkpoint( path, self.model, device=self.device) else: raise ValueError('checkpoint_path is not defined') # # load test dataset self.dl_test = DataLoader(self.ds_test, batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers) self.len_dl_test = len(self.dl_test) # # initialize variables self.mean_loss_test = 0. self.mean_acc_test = 0.
def __init__(self, exp_name, ds_train, ds_val, epochs=210, batch_size=16, num_workers=4, loss='JointsMSELoss', lr=0.001, lr_decay=True, lr_decay_steps=(170, 200), lr_decay_gamma=0.1, optimizer='Adam', weight_decay=0.00001, momentum=0.9, nesterov=False, pretrained_weight_path=None, checkpoint_path=None, log_path='./logs', use_tensorboard=True, model_c=48, model_nof_joints=17, model_bn_momentum=0.1, flip_test_images=True, device=None): """ Inicializa el nuevo objeto Train Se crea el folder de logs, se inicializa el modelo HRNet y se determinan dimensiones pre entrenadas o puntos de control guardos son cargados """ super(Train, self).__init__() self.exp_name = exp_name self.ds_train = ds_train self.ds_val = ds_val self.epochs = epochs self.batch_size = batch_size self.num_workers = num_workers self.loss = loss self.lr = lr self.lr_decay = lr_decay self.lr_decay_steps = lr_decay_steps self.lr_decay_gamma = lr_decay_gamma self.optimizer = optimizer self.weight_decay = weight_decay self.momentum = momentum self.nesterov = nesterov self.pretrained_weight_path = pretrained_weight_path self.checkpoint_path = checkpoint_path self.log_path = os.path.join(log_path, self.exp_name) self.use_tensorboard = use_tensorboard self.model_c = model_c self.model_nof_joints = model_nof_joints self.model_bn_momentum = model_bn_momentum self.flip_test_images = flip_test_images self.epoch = 0 # torch devz if device is not None: self.device = device else: if torch.cuda.is_available(): self.device = torch.device('cuda:0') else: self.device = torch.device('cpu') print(self.device) os.makedirs(self.log_path, 0o755, exist_ok=False) if self.use_tensorboard: self.summary_writer = tb.SummaryWriter(self.log_path) #escribe todos los parametros experimentales en parameters.txt y en campos de texto de tensorboard self.parameters = [ x + ': ' + str(y) + '\n' for x, y in locals().items() ] with open(os.path.join(self.log_path, 'parameters.txt'), 'w') as fd: fd.writelines(self.parameters) if self.use_tensorboard: self.summary_writer.add_text('parameters', '\n'.join(self.parameters)) # # Carga el modelo self.model = HRNet(c=self.model_c, nof_joints=self.model_nof_joints, bn_momentum=self.model_bn_momentum).to(self.device) if self.loss == 'JointsMSELoss': self.loss_fn = JointsMSELoss().to(self.device) elif self.loss == 'JointsOHKMMSELoss': self.loss_fn = JointsOHKMMSELoss().to(self.device) else: raise NotImplementedError if optimizer == 'SGD': self.optim = SGD(self.model.parameters(), lr=self.lr, weight_decay=self.weight_decay, momentum=self.momentum, nesterov=self.nesterov) elif optimizer == 'Adam': self.optim = Adam(self.model.parameters(), lr=self.lr, weight_decay=self.weight_decay) else: raise NotImplementedError # Carga las dimensiones preentrenadas if self.pretrained_weight_path is not None: self.model.load_state_dict(torch.load(self.pretrained_weight_path, map_location=self.device), strict=False) # # carga puntos de control previos if self.checkpoint_path is not None: print('Loading checkpoint %s...' % self.checkpoint_path) if os.path.isdir(self.checkpoint_path): path = os.path.join(self.checkpoint_path, 'checkpoint_last.pth') else: path = self.checkpoint_path self.starting_epoch, self.model, self.optim, self.params = load_checkpoint( path, self.model, self.optim, self.device) else: self.starting_epoch = 0 if lr_decay: self.lr_scheduler = MultiStepLR(self.optim, list(self.lr_decay_steps), gamma=self.lr_decay_gamma, last_epoch=self.starting_epoch) # Carga el entrenamiento y los valores de los datasets self.dl_train = DataLoader(self.ds_train, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers, drop_last=True) self.len_dl_train = len(self.dl_train) self.dl_val = DataLoader(self.ds_val, batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers) self.len_dl_val = len(self.dl_val) # # inicializa las variables self.mean_loss_train = 0. self.mean_acc_train = 0. self.mean_loss_val = 0. self.mean_acc_val = 0. self.mean_mAP_val = 0. self.best_loss = None self.best_acc = None self.best_mAP = None
def __init__(self, ds_test, batch_size=1, num_workers=4, loss='JointsMSELoss', checkpoint_path=None, model_c=48, model_nof_joints=17, model_bn_momentum=0.1, flip_test_images=True, device=None): super(Test, self).__init__() self.ds_test = ds_test self.batch_size = batch_size self.num_workers = num_workers self.loss = loss self.checkpoint_path = checkpoint_path self.model_c = model_c self.model_nof_joints = model_nof_joints self.model_bn_momentum = model_bn_momentum self.flip_test_images = flip_test_images self.epoch = 0 # dispositivo torch if device is not None: self.device = device else: if torch.cuda.is_available(): self.device = torch.device('cuda:0') else: self.device = torch.device('cpu') print(self.device) # cargando modelo self.model = HRNet(c=self.model_c, nof_joints=self.model_nof_joints, bn_momentum=self.model_bn_momentum).to(self.device) #Definiendo perdidas if self.loss == 'JointsMSELoss': self.loss_fn = JointsMSELoss().to(self.device) elif self.loss == 'JointsOHKMMSELoss': self.loss_fn = JointsOHKMMSELoss().to(self.device) else: raise NotImplementedError #cargar punto de control anterior if self.checkpoint_path is not None: print('Loading checkpoint %s...' % self.checkpoint_path) if os.path.isdir(self.checkpoint_path): path = os.path.join(self.checkpoint_path, 'checkpoint_last.pth') else: path = self.checkpoint_path self.starting_epoch, self.model, _, self.params = load_checkpoint( path, self.model, device=self.device) else: raise ValueError('checkpoint_path is not defined') #conjunto de datos de prueba de carga self.dl_test = DataLoader(self.ds_test, batch_size=self.batch_size, shuffle=False, num_workers=self.num_workers) self.len_dl_test = len(self.dl_test) #inicializar variables self.mean_loss_test = 0. self.mean_acc_test = 0.