-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
87 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import math | ||
import pytorch_lightning as pl | ||
from torch.utils.data import DataLoader, random_split | ||
from disent.dataset import DisentDataset | ||
from disent.dataset.data import XYObjectData | ||
from disent.frameworks.vae import BetaVae | ||
from disent.metrics import metric_dci, metric_mig | ||
from disent.model import AutoEncoder | ||
from disent.model.ae import DecoderConv64, EncoderConv64 | ||
from disent.dataset.transform import ToImgTensorF32 | ||
from disent.util import is_test_run # you can ignore and remove this | ||
|
||
# make the ground-truth data | ||
gt_data = XYObjectData() | ||
# split the data using built-in functions (no longer ground-truth datasets, but subsets) | ||
data_train, data_val = random_split(gt_data, [ | ||
int(math.floor(len(gt_data)*0.7)), | ||
int(math.ceil(len(gt_data)*0.3)), | ||
]) | ||
# create the disent datasets | ||
gt_dataset = DisentDataset(gt_data, transform=ToImgTensorF32()) # .is_ground_truth == True | ||
dataset_train = DisentDataset(data_train, transform=ToImgTensorF32()) # .is_ground_truth == False | ||
dataset_val = DisentDataset(data_val, transform=ToImgTensorF32()) # .is_ground_truth == False | ||
# create the data loaders | ||
dataloader_train = DataLoader(dataset=dataset_train, batch_size=4, shuffle=True, num_workers=0) | ||
dataloader_val = DataLoader(dataset=dataset_val, batch_size=4, shuffle=True, num_workers=0) | ||
|
||
# create the pytorch lightning system | ||
module: pl.LightningModule = BetaVae( | ||
model=AutoEncoder( | ||
encoder=EncoderConv64(x_shape=gt_data.x_shape, z_size=6, z_multiplier=2), | ||
decoder=DecoderConv64(x_shape=gt_data.x_shape, z_size=6), | ||
), | ||
cfg=BetaVae.cfg(optimizer='adam', optimizer_kwargs=dict(lr=1e-3), loss_reduction='mean_sum', beta=4) | ||
) | ||
|
||
# train the model | ||
trainer = pl.Trainer(logger=False, checkpoint_callback=False, fast_dev_run=is_test_run()) | ||
trainer.fit(module, dataloader_train, dataloader_val) | ||
|
||
# compute metrics | ||
# - we cannot guarantee which device the representation is on | ||
get_repr = lambda x: module.encode(x.to(module.device)) | ||
# - We cannot compute disentanglement metrics over the split datasets `dataset_train` & `dataset_val` | ||
# because they are no longer ground-truth datasets, we can only use `gt_dataset` | ||
print(metric_dci(gt_dataset, get_repr, num_train=10 if is_test_run() else 1000, num_test=5 if is_test_run() else 500)) | ||
print(metric_mig(gt_dataset, get_repr, num_train=20 if is_test_run() else 2000)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters