finetune_class #1
|
@ -10,6 +10,8 @@ from torch.amp import autocast, GradScaler
|
||||||
from torch.utils.data import Dataset, DataLoader
|
from torch.utils.data import Dataset, DataLoader
|
||||||
from torchvision import transforms
|
from torchvision import transforms
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
from torch.utils.checkpoint import checkpoint
|
||||||
|
import gc
|
||||||
|
|
||||||
from aiia import AIIABase
|
from aiia import AIIABase
|
||||||
from upsampler import Upsampler
|
from upsampler import Upsampler
|
||||||
|
@ -24,12 +26,10 @@ class EarlyStopping:
|
||||||
self.early_stop = False
|
self.early_stop = False
|
||||||
|
|
||||||
def __call__(self, epoch_loss):
|
def __call__(self, epoch_loss):
|
||||||
# If current loss is lower than the best loss minus min_delta, update best loss and reset counter.
|
|
||||||
if epoch_loss < self.best_loss - self.min_delta:
|
if epoch_loss < self.best_loss - self.min_delta:
|
||||||
self.best_loss = epoch_loss
|
self.best_loss = epoch_loss
|
||||||
self.counter = 0
|
self.counter = 0
|
||||||
else:
|
else:
|
||||||
# No significant improvement: increment counter.
|
|
||||||
self.counter += 1
|
self.counter += 1
|
||||||
if self.counter >= self.patience:
|
if self.counter >= self.patience:
|
||||||
self.early_stop = True
|
self.early_stop = True
|
||||||
|
@ -40,11 +40,9 @@ class UpscaleDataset(Dataset):
|
||||||
def __init__(self, parquet_files: list, transform=None):
|
def __init__(self, parquet_files: list, transform=None):
|
||||||
combined_df = pd.DataFrame()
|
combined_df = pd.DataFrame()
|
||||||
for parquet_file in parquet_files:
|
for parquet_file in parquet_files:
|
||||||
# Load data with head() to limit rows for memory efficiency.
|
|
||||||
df = pd.read_parquet(parquet_file, columns=['image_512', 'image_1024']).head(500)
|
df = pd.read_parquet(parquet_file, columns=['image_512', 'image_1024']).head(500)
|
||||||
combined_df = pd.concat([combined_df, df], ignore_index=True)
|
combined_df = pd.concat([combined_df, df], ignore_index=True)
|
||||||
|
|
||||||
# Validate that each row has proper image formats.
|
|
||||||
self.df = combined_df.apply(self._validate_row, axis=1)
|
self.df = combined_df.apply(self._validate_row, axis=1)
|
||||||
self.transform = transform
|
self.transform = transform
|
||||||
self.failed_indices = set()
|
self.failed_indices = set()
|
||||||
|
@ -69,7 +67,6 @@ class UpscaleDataset(Dataset):
|
||||||
return len(self.df)
|
return len(self.df)
|
||||||
|
|
||||||
def __getitem__(self, idx):
|
def __getitem__(self, idx):
|
||||||
# Skip indices that have previously failed.
|
|
||||||
if idx in self.failed_indices:
|
if idx in self.failed_indices:
|
||||||
return self[(idx + 1) % len(self)]
|
return self[(idx + 1) % len(self)]
|
||||||
try:
|
try:
|
||||||
|
@ -79,7 +76,6 @@ class UpscaleDataset(Dataset):
|
||||||
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
ImageFile.LOAD_TRUNCATED_IMAGES = True
|
||||||
low_res = Image.open(io.BytesIO(low_res_bytes)).convert('RGB')
|
low_res = Image.open(io.BytesIO(low_res_bytes)).convert('RGB')
|
||||||
high_res = Image.open(io.BytesIO(high_res_bytes)).convert('RGB')
|
high_res = Image.open(io.BytesIO(high_res_bytes)).convert('RGB')
|
||||||
# Validate expected sizes
|
|
||||||
if low_res.size != (512, 512) or high_res.size != (1024, 1024):
|
if low_res.size != (512, 512) or high_res.size != (1024, 1024):
|
||||||
raise ValueError(f"Size mismatch: LowRes={low_res.size}, HighRes={high_res.size}")
|
raise ValueError(f"Size mismatch: LowRes={low_res.size}, HighRes={high_res.size}")
|
||||||
if self.transform:
|
if self.transform:
|
||||||
|
@ -91,7 +87,7 @@ class UpscaleDataset(Dataset):
|
||||||
self.failed_indices.add(idx)
|
self.failed_indices.add(idx)
|
||||||
return self[(idx + 1) % len(self)]
|
return self[(idx + 1) % len(self)]
|
||||||
|
|
||||||
# Define any transformations you require (e.g., converting PIL images to tensors)
|
# Define any transformations you require.
|
||||||
transform = transforms.Compose([
|
transform = transforms.Compose([
|
||||||
transforms.ToTensor(),
|
transforms.ToTensor(),
|
||||||
])
|
])
|
||||||
|
@ -100,15 +96,20 @@ transform = transforms.Compose([
|
||||||
pretrained_model_path = "/root/vision/AIIA/AIIA-base-512"
|
pretrained_model_path = "/root/vision/AIIA/AIIA-base-512"
|
||||||
base_model = AIIABase.load(pretrained_model_path)
|
base_model = AIIABase.load(pretrained_model_path)
|
||||||
model = Upsampler(base_model)
|
model = Upsampler(base_model)
|
||||||
|
|
||||||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
||||||
model = model.to(device)
|
# Move model to device using channels_last memory format.
|
||||||
|
model = model.to(device, memory_format=torch.channels_last)
|
||||||
|
|
||||||
|
# Optional: flag to enable gradient checkpointing.
|
||||||
|
use_checkpointing = True
|
||||||
|
|
||||||
# Create the dataset and dataloader.
|
# Create the dataset and dataloader.
|
||||||
dataset = UpscaleDataset([
|
dataset = UpscaleDataset([
|
||||||
"/root/training_data/vision-dataset/image_upscaler.parquet",
|
"/root/training_data/vision-dataset/image_upscaler.parquet",
|
||||||
"/root/training_data/vision-dataset/image_vec_upscaler.parquet"
|
"/root/training_data/vision-dataset/image_vec_upscaler.parquet"
|
||||||
], transform=transform)
|
], transform=transform)
|
||||||
data_loader = DataLoader(dataset, batch_size=1, shuffle=True)
|
data_loader = DataLoader(dataset, batch_size=1, shuffle=True) # Consider adjusting num_workers if needed.
|
||||||
|
|
||||||
# Define loss function and optimizer.
|
# Define loss function and optimizer.
|
||||||
criterion = nn.MSELoss()
|
criterion = nn.MSELoss()
|
||||||
|
@ -124,7 +125,6 @@ with open(csv_file, mode='a', newline='') as file:
|
||||||
if file.tell() == 0:
|
if file.tell() == 0:
|
||||||
writer.writerow(['Epoch', 'Train Loss'])
|
writer.writerow(['Epoch', 'Train Loss'])
|
||||||
|
|
||||||
# Initialize automatic mixed precision scaler and EarlyStopping.
|
|
||||||
scaler = GradScaler()
|
scaler = GradScaler()
|
||||||
early_stopping = EarlyStopping(patience=3, min_delta=0.001)
|
early_stopping = EarlyStopping(patience=3, min_delta=0.001)
|
||||||
|
|
||||||
|
@ -132,16 +132,20 @@ early_stopping = EarlyStopping(patience=3, min_delta=0.001)
|
||||||
for epoch in range(num_epochs):
|
for epoch in range(num_epochs):
|
||||||
epoch_loss = 0.0
|
epoch_loss = 0.0
|
||||||
progress_bar = tqdm(data_loader, desc=f"Epoch {epoch + 1}")
|
progress_bar = tqdm(data_loader, desc=f"Epoch {epoch + 1}")
|
||||||
print(f"Epoch: {epoch}")
|
print(f"Epoch: {epoch + 1}")
|
||||||
for low_res, high_res in progress_bar:
|
for low_res, high_res in progress_bar:
|
||||||
low_res = low_res.to(device, non_blocking=True)
|
# Move data to GPU with channels_last format where possible.
|
||||||
|
low_res = low_res.to(device, non_blocking=True).to(memory_format=torch.channels_last)
|
||||||
high_res = high_res.to(device, non_blocking=True)
|
high_res = high_res.to(device, non_blocking=True)
|
||||||
|
|
||||||
optimizer.zero_grad()
|
optimizer.zero_grad()
|
||||||
|
|
||||||
# Use automatic mixed precision to speed up training on supported hardware.
|
|
||||||
with autocast(device_type=device.type):
|
with autocast(device_type=device.type):
|
||||||
outputs = model(low_res)
|
if use_checkpointing:
|
||||||
|
# Wrap the forward pass with checkpointing to trade compute for memory.
|
||||||
|
outputs = checkpoint(lambda x: model(x), low_res)
|
||||||
|
else:
|
||||||
|
outputs = model(low_res)
|
||||||
loss = criterion(outputs, high_res)
|
loss = criterion(outputs, high_res)
|
||||||
|
|
||||||
scaler.scale(loss).backward()
|
scaler.scale(loss).backward()
|
||||||
|
@ -150,6 +154,13 @@ for epoch in range(num_epochs):
|
||||||
|
|
||||||
epoch_loss += loss.item()
|
epoch_loss += loss.item()
|
||||||
progress_bar.set_postfix({'loss': loss.item()})
|
progress_bar.set_postfix({'loss': loss.item()})
|
||||||
|
|
||||||
|
# Optionally delete variables to free memory.
|
||||||
|
del low_res, high_res, outputs, loss
|
||||||
|
|
||||||
|
# Perform garbage collection and clear GPU cache after each epoch.
|
||||||
|
gc.collect()
|
||||||
|
torch.cuda.empty_cache()
|
||||||
|
|
||||||
print(f"Epoch {epoch + 1}, Loss: {epoch_loss}")
|
print(f"Epoch {epoch + 1}, Loss: {epoch_loss}")
|
||||||
|
|
||||||
|
@ -158,11 +169,10 @@ for epoch in range(num_epochs):
|
||||||
writer = csv.writer(file)
|
writer = csv.writer(file)
|
||||||
writer.writerow([epoch + 1, epoch_loss])
|
writer.writerow([epoch + 1, epoch_loss])
|
||||||
|
|
||||||
# Check early stopping criteria.
|
|
||||||
if early_stopping(epoch_loss):
|
if early_stopping(epoch_loss):
|
||||||
print(f"Early stopping triggered at epoch {epoch + 1} with loss {epoch_loss}")
|
print(f"Early stopping triggered at epoch {epoch + 1} with loss {epoch_loss}")
|
||||||
break
|
break
|
||||||
|
|
||||||
# Optionally, save the finetuned model using your library's save method.
|
# Optionally save the fine-tuned model.
|
||||||
finetuned_model_path = "aiuNN"
|
finetuned_model_path = "aiuNN"
|
||||||
model.save(finetuned_model_path)
|
model.save(finetuned_model_path)
|
||||||
|
|
Loading…
Reference in New Issue