separated train and test transformation

This commit is contained in:
2026-05-01 23:36:25 +02:00
parent 9285ede90a
commit 07e8563c5d
4 changed files with 56 additions and 22 deletions

29
Data.py
View File

@@ -2,12 +2,18 @@ from torchvision import datasets, transforms, models
import torch import torch
import numpy as np import numpy as np
# transform images to size # train set transform
def transform(res): def train_transform(res):
return transforms.Compose([ return transforms.Compose([
# ResNet expects 224 x 224 res # ResNet expects 224 x 224 res
# Inception expects 299 x 299
transforms.Resize((res, res)), transforms.Resize((res, res)),
transforms.RandomHorizontalFlip(), transforms.RandomHorizontalFlip(p=0.5),
transforms.ColorJitter(
brightness=0.2,
contrast=0.2,
saturation=0.1
),
transforms.ToTensor(), transforms.ToTensor(),
# normalise to # normalise to
transforms.Normalize( transforms.Normalize(
@@ -16,14 +22,26 @@ def transform(res):
) )
]) ])
# test set transform
def test_transform(res):
return transforms.Compose([
# Just standard resize to 224x224
transforms.Resize((res, res)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
# Load data with 'identity' as target and transform it # Load data with 'identity' as target and transform it
def get_set(res): def get_set():
return datasets.CelebA( return datasets.CelebA(
root='./data', root='./data',
split='all', split='all',
target_type='identity', target_type='identity',
download=True, download=True,
transform=transform(res) transform=None
) )
@@ -66,7 +84,6 @@ def get_indices(dataset, identities, split_at):
test_indices = [] test_indices = []
#training_sample = int(sample_size * training_ratio) #training_sample = int(sample_size * training_ratio)
for person_id in identities: for person_id in identities:
# Get all indices for this specific person # Get all indices for this specific person
indices = torch.where(dataset.identity == person_id)[0].numpy() indices = torch.where(dataset.identity == person_id)[0].numpy()

View File

@@ -2,12 +2,19 @@
import torch import torch
class IdentitySubset(torch.utils.data.Dataset): class IdentitySubset(torch.utils.data.Dataset):
def __init__(self, full_ds, indices, id_mapping): def __init__(self, dataset, indices, id_mapping, transform=None):
self.full_ds = full_ds self.dataset = dataset
self.indices = indices self.indices = indices
self.id_mapping = id_mapping self.id_mapping = id_mapping
self.transform = transform
def __getitem__(self, idx): def __getitem__(self, idx):
img, old_id = self.full_ds[self.indices[idx]] img, old_id = self.dataset[self.indices[idx]]
if self.transform:
img = self.transform(img)
return img, self.id_mapping[old_id.item()] return img, self.id_mapping[old_id.item()]
def __len__(self): def __len__(self):
return len(self.indices) return len(self.indices)

35
Tune.py
View File

@@ -8,15 +8,15 @@ from IdentitySubset import IdentitySubset
from architectures.Model import Model, Architecture from architectures.Model import Model, Architecture
# numbre of classes # numbre of classes
CLASS_SIZE = 30 CLASS_SIZE = 20
# batch # batch
BATCH_SIZE = 16 BATCH_SIZE = 8
# size of images per class trainset + testset # size of images per class trainset + testset
# 30 works best, more than that and we dont have enough data # 30 works best, more than that and we dont have enough data
SAMPLE_SIZE = 30 SAMPLE_SIZE = 30
# this is then full sample - test sample # this is then (full_sample - test_sample)
TRAINING_SMPLE = 28 TRAINING_SMPLE = 28
# learning rate # learning rate
@@ -26,14 +26,18 @@ EPOCHS = 20
# depends on model architecture # depends on model architecture
# ResNet, DenseNet = 224 # ResNet, DenseNet = 224
# Inception = 299 # Inception = 299
RESOLUTION = 299 RESOLUTION = 224
# model architecture # model architecture options are
arch = Architecture.INCEPTION # - RESNET18
# - RESNET50
# - DENSENET121
# - INCEPTION
arch = Architecture.RESNET18
# DATA PREPARATION # DATA PREPARATION
# load data set and prepare # load data set and prepare
dataset = get_set(res = RESOLUTION) dataset = get_set()
# select identities for experiment # select identities for experiment
selected_identities = select_ids( selected_identities = select_ids(
dataset = dataset, dataset = dataset,
@@ -56,10 +60,12 @@ id_map = {old_id: new_id for new_id, old_id in enumerate(selected_identities)}
# we remap identities because crossEntropyLoss requires in indices 0 -> (n-1) # we remap identities because crossEntropyLoss requires in indices 0 -> (n-1)
# where n = class size. # where n = class size.
tr_transform = train_transform(res = RESOLUTION)
train_data = IdentitySubset( train_data = IdentitySubset(
dataset, dataset=dataset,
train_indices, indices=train_indices,
id_map) id_mapping=id_map,
transform=tr_transform)
train_loader = DataLoader( train_loader = DataLoader(
train_data, train_data,
@@ -94,11 +100,14 @@ torch.save(
print('Model saved!') print('Model saved!')
# EVALUATE # EVALUATE
te_transform = test_transform(RESOLUTION)
# Testing # Testing
test_data = IdentitySubset( test_data = IdentitySubset(
dataset, dataset = dataset,
test_indices, indices=test_indices,
id_map) id_mapping=id_map,
transform=te_transform)
test_loader = DataLoader( test_loader = DataLoader(
test_data, test_data,

1
lib64 Symbolic link
View File

@@ -0,0 +1 @@
lib