From 98b6e6dc3be6f88abb72e351c8f2da2b23b8ab85 Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Thu, 7 Jan 2021 19:55:00 +0800 Subject: Type hint for python version lower than 3.9 --- models/model.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'models/model.py') diff --git a/models/model.py b/models/model.py index 1dc0f23..4deced0 100644 --- a/models/model.py +++ b/models/model.py @@ -1,5 +1,5 @@ import os -from typing import Union, Optional +from typing import Union, Optional, Tuple, List import numpy as np import torch @@ -195,9 +195,9 @@ class Model: def _batch_splitter( self, - batch: list[dict[str, Union[np.int64, str, torch.Tensor]]] - ) -> tuple[dict[str, Union[list[str], torch.Tensor]], - dict[str, Union[list[str], torch.Tensor]]]: + batch: List[dict[str, Union[np.int64, str, torch.Tensor]]] + ) -> Tuple[dict[str, Union[List[str], torch.Tensor]], + dict[str, Union[List[str], torch.Tensor]]]: """ Disentanglement need two random conditions, this function will split pr * k * 2 samples to 2 dicts each containing pr * k @@ -212,7 +212,7 @@ class Model: def _make_signature(self, config: dict, - popped_keys: Optional[list] = None) -> str: + popped_keys: Optional[List] = None) -> str: _config = config.copy() if popped_keys: for key in popped_keys: @@ -220,12 +220,12 @@ class Model: return self._gen_sig(list(_config.values())) - def _gen_sig(self, values: Union[tuple, list, str, int, float]) -> str: + def _gen_sig(self, values: Union[Tuple, List, str, int, float]) -> str: strings = [] for v in values: if isinstance(v, str): strings.append(v) - elif isinstance(v, (tuple, list)): + elif isinstance(v, (Tuple, List)): strings.append(self._gen_sig(v)) else: strings.append(str(v)) -- cgit v1.2.3 From e5a73abd80578aa5e46d8d444466d1e6346ec6ec Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Thu, 7 Jan 2021 19:55:00 +0800 Subject: Type hint for python version lower than 3.9 --- models/model.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'models/model.py') diff --git a/models/model.py b/models/model.py index 4deced0..725988a 100644 --- a/models/model.py +++ b/models/model.py @@ -1,5 +1,5 @@ import os -from typing import Union, Optional, Tuple, List +from typing import Union, Optional, Tuple, List, Dict import numpy as np import torch @@ -166,7 +166,7 @@ class Model: popped_keys=['root_dir', 'cache_on'] ) self.log_name = '_'.join((self.log_name, self._dataset_sig)) - config: dict = dataset_config.copy() + config: Dict = dataset_config.copy() name = config.pop('name') if name == 'CASIA-B': return CASIAB(**config, is_train=self.is_train) @@ -180,7 +180,7 @@ class Model: dataset: Union[CASIAB], dataloader_config: DataloaderConfiguration ) -> DataLoader: - config: dict = dataloader_config.copy() + config: Dict = dataloader_config.copy() if self.is_train: (self.pr, self.k) = config.pop('batch_size') self.log_name = '_'.join((self.log_name, str(self.pr), str(self.k))) @@ -195,9 +195,9 @@ class Model: def _batch_splitter( self, - batch: List[dict[str, Union[np.int64, str, torch.Tensor]]] - ) -> Tuple[dict[str, Union[List[str], torch.Tensor]], - dict[str, Union[List[str], torch.Tensor]]]: + batch: List[Dict[str, Union[np.int64, str, torch.Tensor]]] + ) -> Tuple[Dict[str, Union[List[str], torch.Tensor]], + Dict[str, Union[List[str], torch.Tensor]]]: """ Disentanglement need two random conditions, this function will split pr * k * 2 samples to 2 dicts each containing pr * k @@ -211,7 +211,7 @@ class Model: return default_collate(_batch[0]), default_collate(_batch[1]) def _make_signature(self, - config: dict, + config: Dict, popped_keys: Optional[List] = None) -> str: _config = config.copy() if popped_keys: -- cgit v1.2.3 From d750dd9dafe3cda3b1331ad2bfecb53c8c2b1267 Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Thu, 21 Jan 2021 23:47:11 +0800 Subject: A type hint fix --- models/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'models/model.py') diff --git a/models/model.py b/models/model.py index bed28a5..f4604c8 100644 --- a/models/model.py +++ b/models/model.py @@ -133,7 +133,7 @@ class Model: dataloader = self._parse_dataloader_config(dataset, dataloader_config) # Prepare for model, optimizer and scheduler model_hp = self.hp.get('model', {}) - optim_hp: dict = self.hp.get('optimizer', {}).copy() + optim_hp: Dict = self.hp.get('optimizer', {}).copy() ae_optim_hp = optim_hp.pop('auto_encoder', {}) pn_optim_hp = optim_hp.pop('part_net', {}) hpm_optim_hp = optim_hp.pop('hpm', {}) -- cgit v1.2.3 From a040400d7caa267d4bfbe8e5520568806f92b3d4 Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Sat, 23 Jan 2021 00:43:20 +0800 Subject: Type hint fixes --- models/model.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'models/model.py') diff --git a/models/model.py b/models/model.py index 8992914..1dd195e 100644 --- a/models/model.py +++ b/models/model.py @@ -220,7 +220,7 @@ class Model: def predict_all( self, - iters: tuple[int], + iters: Tuple[int], dataset_config: DatasetConfiguration, dataset_selectors: Dict[ str, Dict[str, Union[ClipClasses, ClipConditions, ClipViews]] @@ -266,7 +266,7 @@ class Model: return self._evaluate(gallery_samples, probe_samples) - def _get_eval_sample(self, sample: dict[str, Union[list, torch.Tensor]]): + def _get_eval_sample(self, sample: Dict[str, Union[List, torch.Tensor]]): label = sample.pop('label').item() clip = sample.pop('clip').to(self.device) feature = self.rgb_pn(clip).detach() @@ -326,7 +326,7 @@ class Model: def _load_pretrained( self, - iters: tuple[int], + iters: Tuple[int], dataset_config: DatasetConfiguration, dataset_selectors: Dict[ str, Dict[str, Union[ClipClasses, ClipConditions, ClipViews]] -- cgit v1.2.3 From 24b5968bfc5799e44c9bbbc00e3a9be00f4509ac Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Mon, 15 Feb 2021 11:08:52 +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 3aeb754..9748e46 100644 --- a/models/model.py +++ b/models/model.py @@ -182,7 +182,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: @@ -190,21 +190,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() @@ -234,9 +225,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 ) @@ -250,7 +239,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