Spaces:
Sleeping
Sleeping
ra1425
commited on
Commit
Β·
9af0f61
1
Parent(s):
6290586
FEAT: Complete full data preparation pipeline
Browse files- data_preparation.py +53 -36
data_preparation.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
# --- Standard Python Library ---
|
|
|
|
| 2 |
import random
|
| 3 |
|
| 4 |
# --- Data Handling & Analysis ---
|
|
@@ -16,7 +17,7 @@ from torchvision import transforms
|
|
| 16 |
from torch.utils.data import DataLoader
|
| 17 |
|
| 18 |
# --- Experiment Tracking ---
|
| 19 |
-
from clearml import Task,
|
| 20 |
|
| 21 |
|
| 22 |
# Setting up the SEED to be able to repeat experiments
|
|
@@ -29,6 +30,7 @@ if torch.cuda.is_available():
|
|
| 29 |
|
| 30 |
|
| 31 |
# Initialising a task on ClearML
|
|
|
|
| 32 |
task = Task.init(project_name= 'smallGroupProject', task_name = 'data_prep')
|
| 33 |
task.set_random_seed(SEED)
|
| 34 |
clearml_logger = task.get_logger()
|
|
@@ -168,7 +170,48 @@ print("β
Checkpoint: Plot with classes distributions is created and saved")
|
|
| 168 |
|
| 169 |
|
| 170 |
# --------------- Data Splits ------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
def get_prototype_loaders(batch_size=32):
|
|
|
|
|
|
|
|
|
|
| 172 |
# Calling function to define pipelines
|
| 173 |
normalisation_pipeline, augmentation_pipeline = get_transform_pipelines()
|
| 174 |
|
|
@@ -189,7 +232,7 @@ def get_prototype_loaders(batch_size=32):
|
|
| 189 |
proto_val_split = split_2_dict['train']
|
| 190 |
proto_test_split = split_2_dict['test']
|
| 191 |
|
| 192 |
-
print("β
Checkpoint: Dataset splitted")
|
| 193 |
|
| 194 |
# -- Putting splits through pipelines --
|
| 195 |
proto_train_split.set_transform(augmentation_pipeline)
|
|
@@ -201,16 +244,18 @@ def get_prototype_loaders(batch_size=32):
|
|
| 201 |
proto_val_loader = DataLoader(dataset = proto_val_split, batch_size = batch_size, shuffle = False )
|
| 202 |
proto_test_loader = DataLoader(dataset = proto_test_split, batch_size = batch_size, shuffle = False )
|
| 203 |
|
| 204 |
-
print("β
Checkpoint: DataLoaders are set")
|
| 205 |
return proto_train_loader, proto_val_loader, proto_test_loader
|
| 206 |
|
| 207 |
|
| 208 |
-
|
| 209 |
def get_final_loaders(batch_size=32):
|
|
|
|
|
|
|
|
|
|
| 210 |
# Calling function to define pipelines
|
| 211 |
normalisation_pipeline, augmentation_pipeline = get_transform_pipelines()
|
| 212 |
|
| 213 |
-
# -- Split the
|
| 214 |
# This returns a dictionary: {'train': 70%, 'test': 30%}
|
| 215 |
split_1_dict = data_plants.train_test_split(test_size=0.3, seed=SEED)
|
| 216 |
|
|
@@ -227,49 +272,21 @@ def get_final_loaders(batch_size=32):
|
|
| 227 |
val_split = split_2_dict['train']
|
| 228 |
test_split = split_2_dict['test']
|
| 229 |
|
| 230 |
-
print("β
Checkpoint: Dataset splitted")
|
| 231 |
|
| 232 |
# -- Putting splits through pipelines --
|
| 233 |
train_split.set_transform(augmentation_pipeline)
|
| 234 |
val_split.set_transform(normalisation_pipeline)
|
| 235 |
test_split.set_transform(normalisation_pipeline)
|
| 236 |
|
| 237 |
-
# -- Creating the
|
| 238 |
train_loader = DataLoader(dataset = train_split, batch_size = batch_size, shuffle = True )
|
| 239 |
val_loader = DataLoader(dataset = val_split, batch_size = batch_size, shuffle = False )
|
| 240 |
test_loader = DataLoader(dataset = test_split, batch_size = batch_size, shuffle = False )
|
| 241 |
|
| 242 |
-
print("β
Checkpoint: DataLoaders are set")
|
| 243 |
return train_loader, val_loader, test_loader
|
| 244 |
|
| 245 |
-
|
| 246 |
-
def get_transform_pipelines():
|
| 247 |
-
"""
|
| 248 |
-
Defines and returns the normalization and augmentation pipelines.
|
| 249 |
-
"""
|
| 250 |
-
IMAGENET_MEAN = [0.485, 0.456, 0.406]
|
| 251 |
-
IMAGENET_STD = [0.229, 0.224, 0.225]
|
| 252 |
-
|
| 253 |
-
# Defining pipeline for Val/Test
|
| 254 |
-
normalisation_pipeline = transforms.Compose([
|
| 255 |
-
transforms.ToTensor(),
|
| 256 |
-
transforms.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD)
|
| 257 |
-
])
|
| 258 |
-
|
| 259 |
-
# Augmentation pipeline for Train
|
| 260 |
-
augmentation_pipeline = transforms.Compose([
|
| 261 |
-
transforms.RandomRotation(degrees=30),
|
| 262 |
-
transforms.ColorJitter(brightness=0.2, saturation=0.2),
|
| 263 |
-
transforms.GaussianBlur(kernel_size=3),
|
| 264 |
-
transforms.ToTensor(),
|
| 265 |
-
transforms.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD)
|
| 266 |
-
])
|
| 267 |
-
|
| 268 |
-
print("β
Checkpoint: Transform pipelines created")
|
| 269 |
-
|
| 270 |
-
# Return both pipelines
|
| 271 |
-
return normalisation_pipeline, augmentation_pipeline
|
| 272 |
-
|
| 273 |
# ----------------------------------------------------------------------
|
| 274 |
if __name__ == "__main__":
|
| 275 |
|
|
|
|
| 1 |
# --- Standard Python Library ---
|
| 2 |
+
import os
|
| 3 |
import random
|
| 4 |
|
| 5 |
# --- Data Handling & Analysis ---
|
|
|
|
| 17 |
from torch.utils.data import DataLoader
|
| 18 |
|
| 19 |
# --- Experiment Tracking ---
|
| 20 |
+
from clearml import Task, Logger
|
| 21 |
|
| 22 |
|
| 23 |
# Setting up the SEED to be able to repeat experiments
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
# Initialising a task on ClearML
|
| 33 |
+
# UPDATE CLEARML
|
| 34 |
task = Task.init(project_name= 'smallGroupProject', task_name = 'data_prep')
|
| 35 |
task.set_random_seed(SEED)
|
| 36 |
clearml_logger = task.get_logger()
|
|
|
|
| 170 |
|
| 171 |
|
| 172 |
# --------------- Data Splits ------------
|
| 173 |
+
def get_transform_pipelines():
|
| 174 |
+
"""
|
| 175 |
+
Defines and returns the normalization and augmentation pipelines.
|
| 176 |
+
"""
|
| 177 |
+
# Standard ImageNet mean and std
|
| 178 |
+
# These values are used to normalize the tensors
|
| 179 |
+
IMAGENET_MEAN = [0.485, 0.456, 0.406]
|
| 180 |
+
IMAGENET_STD = [0.229, 0.224, 0.225]
|
| 181 |
+
|
| 182 |
+
# Defining pipeline to ensure that images are consistently formatted (for Val/Test)
|
| 183 |
+
normalisation_pipeline = transforms.Compose([
|
| 184 |
+
# Convert PIL Image to a PyTorch Tensor
|
| 185 |
+
# This also scales pixel values from [0, 255] to [0.0, 1.0]
|
| 186 |
+
transforms.ToTensor(),
|
| 187 |
+
|
| 188 |
+
# Normalise the Tensor; Standartises pixel values
|
| 189 |
+
transforms.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD)
|
| 190 |
+
])
|
| 191 |
+
print("β
Checkpoint: Transform pipeline created")
|
| 192 |
+
|
| 193 |
+
# Augmentation pipeline (to change some parameters of the pictures to create "new" ones)
|
| 194 |
+
augmentation_pipeline = transforms.Compose([
|
| 195 |
+
# Randomly changing some parameters of pictures to enrich dataset
|
| 196 |
+
transforms.RandomRotation(degrees=30),
|
| 197 |
+
transforms.ColorJitter(brightness=0.2, saturation=0.2),
|
| 198 |
+
transforms.GaussianBlur(kernel_size=3),
|
| 199 |
+
|
| 200 |
+
# Convert to Tensor and Normalise
|
| 201 |
+
transforms.ToTensor(),
|
| 202 |
+
transforms.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD)
|
| 203 |
+
])
|
| 204 |
+
|
| 205 |
+
print("β
Checkpoint: Augmentation pipeline created")
|
| 206 |
+
|
| 207 |
+
# Return both pipelines
|
| 208 |
+
return normalisation_pipeline, augmentation_pipeline
|
| 209 |
+
|
| 210 |
+
|
| 211 |
def get_prototype_loaders(batch_size=32):
|
| 212 |
+
"""
|
| 213 |
+
Creates and returns DataLoaders for the 25% PROTOTYPE dataset.
|
| 214 |
+
"""
|
| 215 |
# Calling function to define pipelines
|
| 216 |
normalisation_pipeline, augmentation_pipeline = get_transform_pipelines()
|
| 217 |
|
|
|
|
| 232 |
proto_val_split = split_2_dict['train']
|
| 233 |
proto_test_split = split_2_dict['test']
|
| 234 |
|
| 235 |
+
print("β
Checkpoint: Prototype Dataset splitted")
|
| 236 |
|
| 237 |
# -- Putting splits through pipelines --
|
| 238 |
proto_train_split.set_transform(augmentation_pipeline)
|
|
|
|
| 244 |
proto_val_loader = DataLoader(dataset = proto_val_split, batch_size = batch_size, shuffle = False )
|
| 245 |
proto_test_loader = DataLoader(dataset = proto_test_split, batch_size = batch_size, shuffle = False )
|
| 246 |
|
| 247 |
+
print("β
Checkpoint: Prototype DataLoaders are set")
|
| 248 |
return proto_train_loader, proto_val_loader, proto_test_loader
|
| 249 |
|
| 250 |
|
|
|
|
| 251 |
def get_final_loaders(batch_size=32):
|
| 252 |
+
"""
|
| 253 |
+
Creates and returns DataLoaders for the 100% FINAL dataset.
|
| 254 |
+
"""
|
| 255 |
# Calling function to define pipelines
|
| 256 |
normalisation_pipeline, augmentation_pipeline = get_transform_pipelines()
|
| 257 |
|
| 258 |
+
# -- Split the FULL dataset --
|
| 259 |
# This returns a dictionary: {'train': 70%, 'test': 30%}
|
| 260 |
split_1_dict = data_plants.train_test_split(test_size=0.3, seed=SEED)
|
| 261 |
|
|
|
|
| 272 |
val_split = split_2_dict['train']
|
| 273 |
test_split = split_2_dict['test']
|
| 274 |
|
| 275 |
+
print("β
Checkpoint: Final Dataset splitted")
|
| 276 |
|
| 277 |
# -- Putting splits through pipelines --
|
| 278 |
train_split.set_transform(augmentation_pipeline)
|
| 279 |
val_split.set_transform(normalisation_pipeline)
|
| 280 |
test_split.set_transform(normalisation_pipeline)
|
| 281 |
|
| 282 |
+
# -- Creating the final dataloaders --
|
| 283 |
train_loader = DataLoader(dataset = train_split, batch_size = batch_size, shuffle = True )
|
| 284 |
val_loader = DataLoader(dataset = val_split, batch_size = batch_size, shuffle = False )
|
| 285 |
test_loader = DataLoader(dataset = test_split, batch_size = batch_size, shuffle = False )
|
| 286 |
|
| 287 |
+
print("β
Checkpoint: Final DataLoaders are set")
|
| 288 |
return train_loader, val_loader, test_loader
|
| 289 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
# ----------------------------------------------------------------------
|
| 291 |
if __name__ == "__main__":
|
| 292 |
|