import torch from torch.utils.data import DataLoader from sklearn.metrics import classification_report import SetUp from Data import * from IdentitySubset import IdentitySubset # models from architectures.Model import Model, Architecture # numbre of classes CLASS_SIZE = 30 # batch BATCH_SIZE = 16 # size of images per class trainset + testset # 30 works best, more than that and we dont have enough data SAMPLE_SIZE = 30 # this is then full sample - test sample TRAINING_SMPLE = 28 # learning rate LR_RATE = 0.0001 EPOCHS = 20 # depends on model architecture # ResNet, DenseNet = 224 # Inception = 299 RESOLUTION = 299 # model architecture arch = Architecture.INCEPTION # DATA PREPARATION # load data set and prepare dataset = get_set(res = RESOLUTION) # select identities for experiment selected_identities = select_ids( dataset = dataset, sample_size = SAMPLE_SIZE, class_size = CLASS_SIZE ) print(f'> Selected {CLASS_SIZE} random identity classes from CelebA dataset.') print(f'> A class has {TRAINING_SMPLE} train and {SAMPLE_SIZE-TRAINING_SMPLE} test samples') # split class images to train/test indices train_indices, test_indices = get_indices( dataset = dataset, identities = selected_identities, split_at = TRAINING_SMPLE ) # helps map class id to index 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) # where n = class size. train_data = IdentitySubset( dataset, train_indices, id_map) train_loader = DataLoader( train_data, batch_size = BATCH_SIZE, shuffle = True) print(f"> Total training images: {len(train_data)}") print(f'> Constants : Classes = {CLASS_SIZE}, Batch = {BATCH_SIZE}, epochs = {EPOCHS}') # MODEL PREPARATION # cuda if exists (it does here) device = SetUp.get_device() # Create model using Factory model = Model.create( arch = arch, device = device, size = CLASS_SIZE) # FINETUNING model.train( epochs = EPOCHS, loader = train_loader, rate = LR_RATE) # save. torch.save( model.get().state_dict(), f'{arch.name}.pth') # done tuning print('Model saved!') # EVALUATE # Testing test_data = IdentitySubset( dataset, test_indices, id_map) test_loader = DataLoader( test_data, batch_size=BATCH_SIZE, shuffle=False) print(f"Total test images for these {CLASS_SIZE} classes: {len(test_data)}") # Evaluate model.evaluate( loader = test_loader)