From db7edf86af56aa76eaaa2b30f3b6568e401c7ecd Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Tue, 12 Jan 2021 11:38:14 +0800 Subject: Remove TypeDict for python 3.7 --- models/model.py | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) (limited to 'models/model.py') diff --git a/models/model.py b/models/model.py index 1154d7f..7cf6ed0 100644 --- a/models/model.py +++ b/models/model.py @@ -13,9 +13,6 @@ from torch.utils.tensorboard import SummaryWriter from tqdm import tqdm from models.rgb_part_net import RGBPartNet -from utils.configuration import DataloaderConfiguration, \ - HyperparameterConfiguration, DatasetConfiguration, ModelConfiguration, \ - SystemConfiguration from utils.dataset import CASIAB, ClipConditions, ClipViews, ClipClasses from utils.sampler import TripletSampler @@ -23,9 +20,9 @@ from utils.sampler import TripletSampler class Model: def __init__( self, - system_config: SystemConfiguration, - model_config: ModelConfiguration, - hyperparameter_config: HyperparameterConfiguration + system_config: Dict, + model_config: Dict, + hyperparameter_config: Dict ): self.disable_acc = system_config['disable_acc'] if self.disable_acc: @@ -89,11 +86,11 @@ class Model: def fit_all( self, - dataset_config: DatasetConfiguration, + dataset_config: Dict, dataset_selectors: Dict[ str, Dict[str, Union[ClipClasses, ClipConditions, ClipViews]] ], - dataloader_config: DataloaderConfiguration, + dataloader_config: Dict, ): for (condition, selector) in dataset_selectors.items(): print(f'Training model {condition} ...') @@ -104,8 +101,8 @@ class Model: def fit( self, - dataset_config: DatasetConfiguration, - dataloader_config: DataloaderConfiguration, + dataset_config: Dict, + dataloader_config: Dict, ): self.is_train = True dataset = self._parse_dataset_config(dataset_config) @@ -184,11 +181,11 @@ class Model: def predict_all( self, iter_: int, - dataset_config: DatasetConfiguration, + dataset_config: dict, dataset_selectors: Dict[ str, Dict[str, Union[ClipClasses, ClipConditions, ClipViews]] ], - dataloader_config: DataloaderConfiguration, + dataloader_config: dict, ) -> Dict[str, torch.Tensor]: self.is_train = False # Split gallery and probe dataset @@ -296,7 +293,7 @@ class Model: def _load_pretrained( self, iter_: int, - dataset_config: DatasetConfiguration, + dataset_config: Dict, dataset_selectors: Dict[ str, Dict[str, Union[ClipClasses, ClipConditions, ClipViews]] ] @@ -313,8 +310,8 @@ class Model: def _split_gallery_probe( self, - dataset_config: DatasetConfiguration, - dataloader_config: DataloaderConfiguration, + dataset_config: Dict, + dataloader_config: Dict, ) -> Tuple[DataLoader, Dict[str: DataLoader]]: dataset_name = dataset_config.get('name', 'CASIA-B') if dataset_name == 'CASIA-B': @@ -364,7 +361,7 @@ class Model: def _parse_dataset_config( self, - dataset_config: DatasetConfiguration + dataset_config: Dict ) -> Union[CASIAB]: self.train_size = dataset_config.get('train_size', 74) self.in_channels = dataset_config.get('num_input_channels', 3) @@ -385,7 +382,7 @@ class Model: def _parse_dataloader_config( self, dataset: Union[CASIAB], - dataloader_config: DataloaderConfiguration + dataloader_config: Dict ) -> DataLoader: config: Dict = dataloader_config.copy() (self.pr, self.k) = config.pop('batch_size') -- cgit v1.2.3 From 61f79339fd0168bc200117daf0459764b93d0acb Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Tue, 12 Jan 2021 14:26:03 +0800 Subject: Some type hint fixes --- models/model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'models/model.py') diff --git a/models/model.py b/models/model.py index 617966f..af37bb2 100644 --- a/models/model.py +++ b/models/model.py @@ -182,11 +182,11 @@ class Model: def predict_all( self, iter_: int, - dataset_config: dict, + dataset_config: Dict, dataset_selectors: Dict[ str, Dict[str, Union[ClipClasses, ClipConditions, ClipViews]] ], - dataloader_config: dict, + dataloader_config: Dict, ) -> Dict[str, torch.Tensor]: self.is_train = False # Split gallery and probe dataset -- cgit v1.2.3 From 8ed6492faa7d04c19d15bf137ae4d7ef447fd334 Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Mon, 15 Feb 2021 11:09:25 +0800 Subject: Revert "Memory usage improvement" This reverts commit be508061 --- models/model.py | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) (limited to 'models/model.py') diff --git a/models/model.py b/models/model.py index 199e371..4f5a234 100644 --- a/models/model.py +++ b/models/model.py @@ -179,7 +179,7 @@ class Model: # Training start start_time = datetime.now() running_loss = torch.zeros(5, device=self.device) - print(f"{'Time':^8} {'Iter':^5} {'Loss':^5}", + print(f"{'Time':^8} {'Iter':^5} {'Loss':^6}", f"{'Xrecon':^8} {'CanoCons':^8} {'PoseSim':^8}", f"{'BATripH':^8} {'BATripP':^8} {'LRs':^19}") for (batch_c1, batch_c2) in dataloader: @@ -187,21 +187,12 @@ class Model: # Zero the parameter gradients self.optimizer.zero_grad() # forward + backward + optimize - # Feed data twice in order to reduce memory usage x_c1 = batch_c1['clip'].to(self.device) + x_c2 = batch_c2['clip'].to(self.device) y = batch_c1['label'].to(self.device) # Duplicate labels for each part y = y.unsqueeze(1).repeat(1, self.rgb_pn.num_total_parts) - # Feed condition 1 clips first - losses, images = self.rgb_pn(x_c1, y) - (xrecon_loss, hpm_ba_trip, pn_ba_trip) = losses - x_c2 = batch_c2['clip'].to(self.device) - # Then feed condition 2 clips - cano_cons_loss, pose_sim_loss = self.rgb_pn(x_c2, is_c1=False) - losses = torch.stack(( - xrecon_loss, cano_cons_loss, pose_sim_loss, - hpm_ba_trip, pn_ba_trip - )) + losses, images = self.rgb_pn(x_c1, x_c2, y) loss = losses.sum() loss.backward() self.optimizer.step() @@ -231,9 +222,7 @@ class Model: self.writer.add_images( 'Canonical image', i_c, self.curr_iter ) - for (i, (o, a, p)) in enumerate(zip( - batch_c1['clip'], i_a, i_p - )): + for (i, (o, a, p)) in enumerate(zip(x_c1, i_a, i_p)): self.writer.add_images( f'Original image/batch {i}', o, self.curr_iter ) @@ -247,7 +236,7 @@ class Model: remaining_minute, second = divmod(time_used.seconds, 60) hour, minute = divmod(remaining_minute, 60) print(f'{hour:02}:{minute:02}:{second:02}', - f'{self.curr_iter:5d} {running_loss.sum() / 100:5.3f}', + f'{self.curr_iter:5d} {running_loss.sum() / 100:6.3f}', '{:f} {:f} {:f} {:f} {:f}'.format(*running_loss / 100), '{:.3e} {:.3e}'.format(lrs[0], lrs[1])) running_loss.zero_() -- cgit v1.2.3 From d0edaf066d2c7069bce9af4102ec4c75ee4e2911 Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Thu, 18 Feb 2021 20:57:37 +0800 Subject: Type hint fixes --- models/model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'models/model.py') diff --git a/models/model.py b/models/model.py index a3b6d3a..4bf8dbc 100644 --- a/models/model.py +++ b/models/model.py @@ -279,11 +279,11 @@ class Model: def transform( self, iters: Tuple[int], - dataset_config: DatasetConfiguration, + dataset_config: Dict, dataset_selectors: Dict[ str, Dict[str, Union[ClipClasses, ClipConditions, ClipViews]] ], - dataloader_config: DataloaderConfiguration + dataloader_config: Dict ): self.is_train = False # Split gallery and probe dataset -- cgit v1.2.3