make a script to train based on this import os import numpy as np import torch from…
- User
make a script to train based on this
import os import numpy as np import torch from torch.utils.data import Dataset, DataLoader from sklearn.model_selection import train_test_split import re
Define dataset class
class SpectrogramDataset(Dataset): def init(self, low_quality_spectrograms, high_quality_spectrograms): # Assuming that low_quality_spectrograms and high_quality_spectrograms are lists of Tensors self.low_quality_spectrograms = low_quality_spectrograms self.high_quality_spectrograms = high_quality_spectrograms
def __len__(self): return len(self.low_quality_spectrograms) def __getitem__(self, idx): return self.low_quality_spectrograms[idx], self.high_quality_spectrograms[idx]Function to load the dataset
def load_dataset(lq_dir, hq_dir): lq_files = os.listdir(lq_dir) lq_specs, hq_specs = [], []
for lq_file in lq_files: lq_path = os.path.join(lq_dir, lq_file) hq_file = re.sub('lq', 'hq', lq_file) # Replace lq with hq in the filename hq_path = os.path.join(hq_dir, hq_file) if os.path.exists(hq_path): # Ensure the HQ counterpart exists lq_spec = np.load(lq_path) hq_spec = np.load(hq_path) # Convert numpy arrays to tensors and append lq_specs.append(torch.from_numpy(lq_spec).float()) hq_specs.append(torch.from_numpy(hq_spec).float()) else: print(f'Missing HQ file for {lq_file}') # Returning lists of tensors return lq_specs, hq_specsCustom collate function to handle varying shapes within a batch
def custom_collate_fn(batch): # Just return the list of tensors as is, no padding lq_specs, hq_specs = zip(*batch) # Transpose the batch (list of pairs) into pairs of lists return list(lq_specs), list(hq_specs) # In this basic form, DataLoader expects batch to be Tensor
lq_dir = "/content/dataset /processed/LQPRO" # Corrected path
hq_dir = "/content/dataset /processed/HQPRO"
Load the dataset
lq_spectrograms, hq_spectrograms = load_dataset(lq_dir, hq_dir)
Split and prepare the data as in the previous script
Note: Direct splitting lists of tensors now
lq_train, lq_test, hq_train, hq_test = train_test_split(lq_spectrograms, hq_spectrograms, test_size=0.2, random_state=42) lq_train, lq_val, hq_train, hq_val = train_test_split(lq_train, hq_train, test_size=0.25, random_state=42)
Create datasets
train_dataset = SpectrogramDataset(lq_train, hq_train) val_dataset = SpectrogramDataset(lq_val, hq_val) test_dataset = SpectrogramDataset(lq_test, hq_test)
Create dataloaders with custom collate function
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True, collate_fn=custom_collate_fn) val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False, collate_fn=custom_collate_fn) test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False, collate_fn=custom_collate_fn)
- Assistant
- User
can you add a variable called save_dir that the model ckpt will save to
- Assistant
- User
ValueError Traceback (most recent call last) in <cell line: 65>() 63 model = EnhanceNet() 64 loss_function = nn.MSELoss() # Assuming regression-type problem ---> 65 optimizer = torch.optim.Adam(model.parameters(), lr=0.001) 66 67 # Train the model
1 frames /usr/local/lib/python3.10/dist-packages/torch/optim/optimizer.py in init(self, params, defaults) 271 param_groups = list(params) 272 if len(param_groups) == 0: --> 273 raise ValueError("optimizer got an empty parameter list") 274 if not isinstance(param_groups[0], dict): 275 param_groups = [{'params': param_groups}]
ValueError: optimizer got an empty parameter list
- Assistant