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 --- utils/configuration.py | 18 +++++++++--------- utils/dataset.py | 16 ++++++++-------- utils/sampler.py | 4 ++-- 3 files changed, 19 insertions(+), 19 deletions(-) (limited to 'utils') diff --git a/utils/configuration.py b/utils/configuration.py index f3ae0b3..aa04b32 100644 --- a/utils/configuration.py +++ b/utils/configuration.py @@ -1,4 +1,4 @@ -from typing import TypedDict, Optional, Union +from typing import TypedDict, Optional, Union, Tuple from utils.dataset import ClipClasses, ClipConditions, ClipViews @@ -17,32 +17,32 @@ class DatasetConfiguration(TypedDict): discard_threshold: int selector: Optional[dict[str, Union[ClipClasses, ClipConditions, ClipViews]]] num_input_channels: int - frame_size: tuple[int, int] + frame_size: Tuple[int, int] cache_on: bool class DataloaderConfiguration(TypedDict): - batch_size: tuple[int, int] + batch_size: Tuple[int, int] num_workers: int pin_memory: bool class HyperparameterConfiguration(TypedDict): ae_feature_channels: int - f_a_c_p_dims: tuple[int, int, int] - hpm_scales: tuple[int, ...] + f_a_c_p_dims: Tuple[int, int, int] + hpm_scales: Tuple[int, ...] hpm_use_avg_pool: bool hpm_use_max_pool: bool fpfe_feature_channels: int - fpfe_kernel_sizes: tuple[tuple, ...] - fpfe_paddings: tuple[tuple, ...] - fpfe_halving: tuple[int, ...] + fpfe_kernel_sizes: Tuple[Tuple, ...] + fpfe_paddings: Tuple[Tuple, ...] + fpfe_halving: Tuple[int, ...] tfa_squeeze_ratio: int tfa_num_parts: int embedding_dims: int triplet_margin: float lr: int - betas: tuple[float, float] + betas: Tuple[float, float] class ModelConfiguration(TypedDict): diff --git a/utils/dataset.py b/utils/dataset.py index ded9fd5..0a33693 100644 --- a/utils/dataset.py +++ b/utils/dataset.py @@ -1,7 +1,7 @@ import os import random import re -from typing import Optional, NewType, Union +from typing import Optional, NewType, Union, List, Tuple import numpy as np import torch @@ -30,7 +30,7 @@ class CASIAB(data.Dataset): str, Union[ClipClasses, ClipConditions, ClipViews] ]] = None, num_input_channels: int = 3, - frame_size: tuple[int, int] = (64, 32), + frame_size: Tuple[int, int] = (64, 32), cache_on: bool = False ): """ @@ -75,15 +75,15 @@ class CASIAB(data.Dataset): self.views: np.ndarray[np.str_] # Labels, classes, conditions and views in dataset, # set of three attributes above - self.metadata = dict[str, list[np.int64, str]] + self.metadata = dict[str, List[np.int64, str]] # Dictionaries for indexing frames and frame names by clip name # and chip path when cache is on - self._cached_clips_frame_names: Optional[dict[str, list[str]]] = None + self._cached_clips_frame_names: Optional[dict[str, List[str]]] = None self._cached_clips: Optional[dict[str, torch.Tensor]] = None # Video clip directory names - self._clip_names: list[str] = [] + self._clip_names: List[str] = [] clip_names = sorted(os.listdir(self._root_dir)) if self._is_train: @@ -215,8 +215,8 @@ class CASIAB(data.Dataset): def _load_cached_video( self, clip: torch.Tensor, - frame_names: list[str], - sampled_frame_names: list[str] + frame_names: List[str], + sampled_frame_names: List[str] ) -> torch.Tensor: # Mask the original clip when it is long enough if len(frame_names) >= self._num_sampled_frames: @@ -246,7 +246,7 @@ class CASIAB(data.Dataset): return clip def _sample_frames(self, clip_path: str, - is_caching: bool = False) -> list[str]: + is_caching: bool = False) -> List[str]: if self._cache_on: if is_caching: # Sort frame in advance for loading convenience diff --git a/utils/sampler.py b/utils/sampler.py index cdf1984..734acf9 100644 --- a/utils/sampler.py +++ b/utils/sampler.py @@ -1,6 +1,6 @@ import random from collections.abc import Iterator -from typing import Union +from typing import Union, Tuple import numpy as np from torch.utils import data @@ -12,7 +12,7 @@ class TripletSampler(data.Sampler): def __init__( self, data_source: Union[CASIAB], - batch_size: tuple[int, int] + batch_size: Tuple[int, int] ): super().__init__(data_source) self.metadata_labels = data_source.metadata['labels'] -- 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 --- utils/configuration.py | 4 ++-- utils/dataset.py | 18 +++++++++--------- utils/sampler.py | 2 +- 3 files changed, 12 insertions(+), 12 deletions(-) (limited to 'utils') diff --git a/utils/configuration.py b/utils/configuration.py index aa04b32..455abe8 100644 --- a/utils/configuration.py +++ b/utils/configuration.py @@ -1,4 +1,4 @@ -from typing import TypedDict, Optional, Union, Tuple +from typing import TypedDict, Optional, Union, Tuple, Dict from utils.dataset import ClipClasses, ClipConditions, ClipViews @@ -15,7 +15,7 @@ class DatasetConfiguration(TypedDict): train_size: int num_sampled_frames: int discard_threshold: int - selector: Optional[dict[str, Union[ClipClasses, ClipConditions, ClipViews]]] + selector: Optional[Dict[str, Union[ClipClasses, ClipConditions, ClipViews]]] num_input_channels: int frame_size: Tuple[int, int] cache_on: bool diff --git a/utils/dataset.py b/utils/dataset.py index 0a33693..e691157 100644 --- a/utils/dataset.py +++ b/utils/dataset.py @@ -1,7 +1,7 @@ import os import random import re -from typing import Optional, NewType, Union, List, Tuple +from typing import Optional, NewType, Union, List, Tuple, Set, Dict import numpy as np import torch @@ -11,9 +11,9 @@ from sklearn.preprocessing import LabelEncoder from torch.utils import data from tqdm import tqdm -ClipClasses = NewType('ClipClasses', set[str]) -ClipConditions = NewType('ClipConditions', set[str]) -ClipViews = NewType('ClipViews', set[str]) +ClipClasses = NewType('ClipClasses', Set[str]) +ClipConditions = NewType('ClipConditions', Set[str]) +ClipViews = NewType('ClipViews', Set[str]) class CASIAB(data.Dataset): @@ -26,7 +26,7 @@ class CASIAB(data.Dataset): train_size: int = 74, num_sampled_frames: int = 30, discard_threshold: int = 15, - selector: Optional[dict[ + selector: Optional[Dict[ str, Union[ClipClasses, ClipConditions, ClipViews] ]] = None, num_input_channels: int = 3, @@ -75,12 +75,12 @@ class CASIAB(data.Dataset): self.views: np.ndarray[np.str_] # Labels, classes, conditions and views in dataset, # set of three attributes above - self.metadata = dict[str, List[np.int64, str]] + self.metadata = Dict[str, List[np.int64, str]] # Dictionaries for indexing frames and frame names by clip name # and chip path when cache is on - self._cached_clips_frame_names: Optional[dict[str, List[str]]] = None - self._cached_clips: Optional[dict[str, torch.Tensor]] = None + self._cached_clips_frame_names: Optional[Dict[str, List[str]]] = None + self._cached_clips: Optional[Dict[str, torch.Tensor]] = None # Video clip directory names self._clip_names: List[str] = [] @@ -170,7 +170,7 @@ class CASIAB(data.Dataset): def __getitem__( self, index: int - ) -> dict[str, Union[np.int64, str, torch.Tensor]]: + ) -> Dict[str, Union[np.int64, str, torch.Tensor]]: label = self.labels[index] condition = self.conditions[index] view = self.views[index] diff --git a/utils/sampler.py b/utils/sampler.py index 734acf9..c1bf3dd 100644 --- a/utils/sampler.py +++ b/utils/sampler.py @@ -1,5 +1,5 @@ import random -from collections.abc import Iterator +from collections import Iterator from typing import Union, Tuple import numpy as np -- cgit v1.2.3 From 46e68c1d0168816107fd9997e1d948d3f403f5ee Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Thu, 7 Jan 2021 20:19:36 +0800 Subject: Type hint for python version lower than 3.9 --- utils/sampler.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'utils') diff --git a/utils/sampler.py b/utils/sampler.py index c1bf3dd..0977f94 100644 --- a/utils/sampler.py +++ b/utils/sampler.py @@ -1,6 +1,5 @@ import random -from collections import Iterator -from typing import Union, Tuple +from typing import Union, Tuple, Iterator import numpy as np from torch.utils import data -- cgit v1.2.3 From f6706981fd9e0608f25c6528467394508cfa2afc Mon Sep 17 00:00:00 2001 From: Jordan Gong Date: Wed, 13 Jan 2021 11:05:30 +0800 Subject: Type hint fixes --- utils/configuration.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'utils') diff --git a/utils/configuration.py b/utils/configuration.py index d7ebc5e..17796cd 100644 --- a/utils/configuration.py +++ b/utils/configuration.py @@ -64,8 +64,8 @@ class ModelConfiguration(TypedDict): name: str restore_iter: int total_iter: int - restore_iters: tuple[int, ...] - total_iters: tuple[int, ...] + restore_iters: Tuple[int, ...] + total_iters: Tuple[int, ...] class Configuration(TypedDict): -- cgit v1.2.3