summaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/auto_encoder.py14
-rw-r--r--models/layers.py10
-rw-r--r--models/model.py54
-rw-r--r--models/rgb_part_net.py6
4 files changed, 44 insertions, 40 deletions
diff --git a/models/auto_encoder.py b/models/auto_encoder.py
index 9bfb365..61622eb 100644
--- a/models/auto_encoder.py
+++ b/models/auto_encoder.py
@@ -1,3 +1,5 @@
+from typing import Tuple
+
import torch
import torch.nn as nn
import torch.nn.functional as F
@@ -11,9 +13,9 @@ class Encoder(nn.Module):
def __init__(
self,
in_channels: int = 3,
- frame_size: tuple[int, int] = (64, 48),
+ frame_size: Tuple[int, int] = (64, 48),
feature_channels: int = 64,
- output_dims: tuple[int, int, int] = (128, 128, 64)
+ output_dims: Tuple[int, int, int] = (128, 128, 64)
):
super().__init__()
self.feature_channels = feature_channels
@@ -74,9 +76,9 @@ class Decoder(nn.Module):
def __init__(
self,
- input_dims: tuple[int, int, int] = (128, 128, 64),
+ input_dims: Tuple[int, int, int] = (128, 128, 64),
feature_channels: int = 64,
- feature_size: tuple[int, int] = (4, 3),
+ feature_size: Tuple[int, int] = (4, 3),
out_channels: int = 3,
):
super().__init__()
@@ -124,9 +126,9 @@ class AutoEncoder(nn.Module):
self,
num_class: int,
channels: int = 3,
- frame_size: tuple[int, int] = (64, 48),
+ frame_size: Tuple[int, int] = (64, 48),
feature_channels: int = 64,
- embedding_dims: tuple[int, int, int] = (128, 128, 64)
+ embedding_dims: Tuple[int, int, int] = (128, 128, 64)
):
super().__init__()
self.encoder = Encoder(channels, frame_size,
diff --git a/models/layers.py b/models/layers.py
index 5306769..1da79ef 100644
--- a/models/layers.py
+++ b/models/layers.py
@@ -1,4 +1,4 @@
-from typing import Union
+from typing import Union, Tuple
import torch.nn as nn
import torch.nn.functional as F
@@ -9,7 +9,7 @@ class BasicConv2d(nn.Module):
self,
in_channels: int,
out_channels: int,
- kernel_size: Union[int, tuple[int, int]],
+ kernel_size: Union[int, Tuple[int, int]],
**kwargs
):
super().__init__()
@@ -28,7 +28,7 @@ class VGGConv2d(BasicConv2d):
self,
in_channels: int,
out_channels: int,
- kernel_size: Union[int, tuple[int, int]] = 3,
+ kernel_size: Union[int, Tuple[int, int]] = 3,
padding: int = 1,
**kwargs
):
@@ -46,7 +46,7 @@ class BasicConvTranspose2d(nn.Module):
self,
in_channels: int,
out_channels: int,
- kernel_size: Union[int, tuple[int, int]],
+ kernel_size: Union[int, Tuple[int, int]],
**kwargs
):
super().__init__()
@@ -65,7 +65,7 @@ class DCGANConvTranspose2d(BasicConvTranspose2d):
self,
in_channels: int,
out_channels: int,
- kernel_size: Union[int, tuple[int, int]] = 4,
+ kernel_size: Union[int, Tuple[int, int]] = 4,
stride: int = 2,
padding: int = 1,
is_last_layer: bool = False,
diff --git a/models/model.py b/models/model.py
index d976f5a..ebaaaf1 100644
--- a/models/model.py
+++ b/models/model.py
@@ -1,7 +1,7 @@
import copy
import os
import random
-from typing import Union, Optional
+from typing import Union, Optional, Tuple, List, Dict, Set
import numpy as np
import torch
@@ -56,11 +56,11 @@ class Model:
self.is_train: bool = True
self.num_class: Optional[int] = None
self.in_channels: int = 3
- self.in_size: tuple[int, int] = (64, 48)
+ self.in_size: Tuple[int, int] = (64, 48)
self.batch_size: Optional[int] = None
- self._gallery_dataset_meta: Optional[dict[str, list]] = None
- self._probe_datasets_meta: Optional[dict[str, dict[str, list]]] = None
+ self._gallery_dataset_meta: Optional[Dict[str, List]] = None
+ self._probe_datasets_meta: Optional[Dict[str, Dict[str, List]]] = None
self._model_name: str = self.meta.get('name', 'RGB-GaitPart')
self._hp_sig: str = self._make_signature(self.hp)
@@ -109,8 +109,8 @@ class Model:
def fit_all(
self,
dataset_config: DatasetConfiguration,
- dataset_selectors: dict[
- str, dict[str, Union[ClipClasses, ClipConditions, ClipViews]]
+ dataset_selectors: Dict[
+ str, Dict[str, Union[ClipClasses, ClipConditions, ClipViews]]
],
dataloader_config: DataloaderConfiguration,
):
@@ -158,7 +158,7 @@ class Model:
))
# Prepare for model, optimizer and scheduler
model_hp: dict = self.hp.get('model', {}).copy()
- optim_hp: dict = self.hp.get('optimizer', {}).copy()
+ optim_hp: Dict = self.hp.get('optimizer', {}).copy()
sched_hp = self.hp.get('scheduler', {})
self.rgb_pn = RGBPartNet(
@@ -291,10 +291,10 @@ class Model:
def transform(
self,
- iters: tuple[int],
+ iters: Tuple[int],
dataset_config: DatasetConfiguration,
- dataset_selectors: dict[
- str, dict[str, Union[ClipClasses, ClipConditions, ClipViews]]
+ dataset_selectors: Dict[
+ str, Dict[str, Union[ClipClasses, ClipConditions, ClipViews]]
],
dataloader_config: DataloaderConfiguration,
is_train: bool = False
@@ -344,7 +344,7 @@ class Model:
return 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, condition, view, clip = sample.values()
with torch.no_grad():
feature_c, feature_p = self.rgb_pn(clip.to(self.device))
@@ -357,12 +357,12 @@ 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]]
+ dataset_selectors: Dict[
+ str, Dict[str, Union[ClipClasses, ClipConditions, ClipViews]]
]
- ) -> dict[str, str]:
+ ) -> Dict[str, str]:
checkpoints = {}
for (iter_, total_iter, (condition, selector)) in zip(
iters, self.total_iters, dataset_selectors.items()
@@ -381,7 +381,7 @@ class Model:
dataset_config: DatasetConfiguration,
dataloader_config: DataloaderConfiguration,
is_train: bool = False
- ) -> tuple[DataLoader, dict[str, DataLoader]]:
+ ) -> Tuple[DataLoader, Dict[str, DataLoader]]:
dataset_name = dataset_config.get('name', 'CASIA-B')
if dataset_name == 'CASIA-B':
self.is_train = is_train
@@ -439,7 +439,7 @@ class Model:
dataset_config,
popped_keys=['root_dir', 'cache_on']
)
- config: dict = dataset_config.copy()
+ config: Dict = dataset_config.copy()
name = config.pop('name', 'CASIA-B')
if name == 'CASIA-B':
return CASIAB(**config, is_train=self.is_train)
@@ -453,7 +453,7 @@ class Model:
dataset: Union[CASIAB],
dataloader_config: DataloaderConfiguration
) -> DataLoader:
- config: dict = dataloader_config.copy()
+ config: Dict = dataloader_config.copy()
self.batch_size = config.pop('batch_size', 16)
if self.is_train:
dis_sampler = DisentanglingSampler(dataset, self.batch_size)
@@ -466,9 +466,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 batch_size * 2 samples to 2 dicts each containing
@@ -481,8 +481,8 @@ class Model:
return default_collate(batch_0), default_collate(batch_1)
def _make_signature(self,
- config: dict,
- popped_keys: Optional[list] = None) -> str:
+ config: Dict,
+ popped_keys: Optional[List] = None) -> str:
_config = config.copy()
if popped_keys:
for key in popped_keys:
@@ -490,16 +490,16 @@ class Model:
return self._gen_sig(list(_config.values()))
- def _gen_sig(self, values: Union[tuple, list, set, str, int, float]) -> str:
+ def _gen_sig(self, values: Union[Tuple, List, Set, 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))
- elif isinstance(v, set):
+ elif isinstance(v, Set):
strings.append(self._gen_sig(sorted(list(v))))
- elif isinstance(v, dict):
+ elif isinstance(v, Dict):
strings.append(self._gen_sig(list(v.values())))
else:
strings.append(str(v))
diff --git a/models/rgb_part_net.py b/models/rgb_part_net.py
index d5eb619..6be6b0a 100644
--- a/models/rgb_part_net.py
+++ b/models/rgb_part_net.py
@@ -1,3 +1,5 @@
+from typing import Tuple
+
import torch
import torch.nn as nn
import torch.nn.functional as F
@@ -10,9 +12,9 @@ class RGBPartNet(nn.Module):
self,
num_class: int,
ae_in_channels: int = 3,
- ae_in_size: tuple[int, int] = (64, 48),
+ ae_in_size: Tuple[int, int] = (64, 48),
ae_feature_channels: int = 64,
- f_a_c_p_dims: tuple[int, int, int] = (128, 128, 64),
+ f_a_c_p_dims: Tuple[int, int, int] = (128, 128, 64),
image_log_on: bool = False
):
super().__init__()