summaryrefslogtreecommitdiff
path: root/models/model.py
diff options
context:
space:
mode:
authorJordan Gong <jordan.gong@protonmail.com>2021-02-20 14:26:31 +0800
committerJordan Gong <jordan.gong@protonmail.com>2021-02-20 14:29:31 +0800
commit850c525772969823eef6083e8018ac43a1e87e4c (patch)
tree16f1d48561dead30d31ab2b72918fb1e9fef5665 /models/model.py
parenta31eb135c8cc3a8737fabca54fe3d5791f293753 (diff)
parent50eedae4f320c446544772fb2b0abcbce1be7590 (diff)
Merge branch 'master' into data_parallel
# Conflicts: # models/model.py
Diffstat (limited to 'models/model.py')
-rw-r--r--models/model.py24
1 files changed, 19 insertions, 5 deletions
diff --git a/models/model.py b/models/model.py
index cac165e..a69a9b0 100644
--- a/models/model.py
+++ b/models/model.py
@@ -18,6 +18,7 @@ from utils.configuration import DataloaderConfiguration, \
SystemConfiguration
from utils.dataset import CASIAB, ClipConditions, ClipViews, ClipClasses
from utils.sampler import TripletSampler
+from utils.triplet_loss import JointBatchAllTripletLoss
class Model:
@@ -67,6 +68,7 @@ class Model:
self._dataset_sig: str = 'undefined'
self.rgb_pn: Optional[RGBPartNet] = None
+ self.ba_triplet_loss: Optional[JointBatchAllTripletLoss] = None
self.optimizer: Optional[optim.Adam] = None
self.scheduler: Optional[optim.lr_scheduler.StepLR] = None
self.writer: Optional[SummaryWriter] = None
@@ -140,7 +142,8 @@ class Model:
dataset = self._parse_dataset_config(dataset_config)
dataloader = self._parse_dataloader_config(dataset, dataloader_config)
# Prepare for model, optimizer and scheduler
- model_hp = self.hp.get('model', {})
+ model_hp: dict = self.hp.get('model', {}).copy()
+ triplet_margins = model_hp.pop('triplet_margins', (0.2, 0.2))
optim_hp: dict = self.hp.get('optimizer', {}).copy()
start_iter = optim_hp.pop('start_iter', 0)
ae_optim_hp = optim_hp.pop('auto_encoder', {})
@@ -150,9 +153,14 @@ class Model:
sched_hp = self.hp.get('scheduler', {})
self.rgb_pn = RGBPartNet(self.in_channels, self.in_size, **model_hp,
image_log_on=self.image_log_on)
+ self.ba_triplet_loss = JointBatchAllTripletLoss(
+ self.rgb_pn.hpm_num_parts, triplet_margins
+ )
# Try to accelerate computation using CUDA or others
self.rgb_pn = nn.DataParallel(self.rgb_pn)
self.rgb_pn = self.rgb_pn.to(self.device)
+ self.ba_triplet_loss = nn.DataParallel(self.ba_triplet_loss)
+ self.ba_triplet_loss = self.ba_triplet_loss.to(self.device)
self.optimizer = optim.Adam([
{'params': self.rgb_pn.module.ae.parameters(), **ae_optim_hp},
{'params': self.rgb_pn.module.pn.parameters(), **pn_optim_hp},
@@ -194,12 +202,18 @@ class Model:
# forward + backward + optimize
x_c1 = batch_c1['clip'].to(self.device)
x_c2 = batch_c2['clip'].to(self.device)
+ feature, ae_losses, images = self.rgb_pn(x_c1, x_c2)
y = batch_c1['label'].to(self.device)
# Duplicate labels for each part
- y = y.unsqueeze(1).repeat(1, self.rgb_pn.module.num_total_parts)
- losses, images = self.rgb_pn(x_c1, x_c2, y)
- # Combine losses from different data splits
- losses = losses.mean()
+ y = y.repeat(self.rgb_pn.num_total_parts, 1)
+ triplet_loss = self.ba_triplet_loss(feature, y)
+ losses = torch.cat((
+ ae_losses.mean(0),
+ torch.stack((
+ triplet_loss[:self.rgb_pn.hpm_num_parts].mean(),
+ triplet_loss[self.rgb_pn.hpm_num_parts:].mean()
+ ))
+ ))
loss = losses.sum()
loss.backward()
self.optimizer.step()