def get_model(model_string, **kwargs): """Retrieves model from library. """ if model_string == 'resnet18': return models.resnet18(**kwargs) elif model_string == 'resnet34': return models.resnet34(**kwargs) elif model_string == 'resnet50': return models.resnet50(**kwargs) elif model_string == 'resnet101': return models.resnet101(**kwargs) elif model_string == 'resnet152': return models.resnet152(**kwargs) elif model_string == 'resnet20': return resnet_cifar.resnet20(**kwargs) elif model_string == 'resnet32': return resnet_cifar.resnet32(**kwargs) elif model_string == 'resnet44': return resnet_cifar.resnet44(**kwargs) elif model_string == 'resnet56': return resnet_cifar.resnet56(**kwargs) elif model_string == 'resnet110': return resnet_cifar.resnet110(**kwargs) elif model_string == 'resnet1202': return resnet_cifar.resnet1202(**kwargs) else: raise NameError('{} is not recognized.'.format(model_string))
# because we want to compare our method with the theoretical case where all the training samples are stored prototypes = np.zeros( (args.num_classes, dictionary_size, X_train_total.shape[1], X_train_total.shape[2], X_train_total.shape[3])) for orde in range(args.num_classes): prototypes[orde, :, :, :, :] = X_train_total[np.where( Y_train_total == order[orde])] start_iter = int(args.nb_cl_fg / args.nb_cl) - 1 for iteration in range(start_iter, int(args.num_classes / args.nb_cl)): #init model if iteration == start_iter: ############################################################ last_iter = 0 ############################################################ tg_model = resnet_cifar.resnet32(num_classes=args.nb_cl_fg) ref_model = None else: ############################################################ last_iter = iteration ############################################################ #increment classes ref_model = copy.deepcopy(tg_model) in_features = tg_model.fc.in_features out_features = tg_model.fc.out_features new_fc = nn.Linear(in_features, out_features + args.nb_cl) new_fc.weight.data[:out_features] = tg_model.fc.weight.data new_fc.bias.data[:out_features] = tg_model.fc.bias.data tg_model.fc = new_fc # Prepare the training data for the current batch of classes
def load_architecture(arch_id: str, dataset_name: str) -> nn.Module: # ====== fully-connected networks ======= if arch_id == 'fc-relu': return fully_connected_net(dataset_name, [200, 200], 'relu', bias=True) elif arch_id == 'fc-elu': return fully_connected_net(dataset_name, [200, 200], 'elu', bias=True) elif arch_id == 'fc-tanh': return fully_connected_net(dataset_name, [200, 200], 'tanh', bias=True) elif arch_id == 'fc-hardtanh': return fully_connected_net(dataset_name, [200, 200], 'hardtanh', bias=True) elif arch_id == 'fc-softplus': return fully_connected_net(dataset_name, [200, 200], 'softplus', bias=True) # ====== convolutional networks ======= elif arch_id == 'cnn-relu': return convnet(dataset_name, [32, 32], activation='relu', pooling='max', bias=True) elif arch_id == 'cnn-elu': return convnet(dataset_name, [32, 32], activation='elu', pooling='max', bias=True) elif arch_id == 'cnn-tanh': return convnet(dataset_name, [32, 32], activation='tanh', pooling='max', bias=True) elif arch_id == 'cnn-avgpool-relu': return convnet(dataset_name, [32, 32], activation='relu', pooling='average', bias=True) elif arch_id == 'cnn-avgpool-elu': return convnet(dataset_name, [32, 32], activation='elu', pooling='average', bias=True) elif arch_id == 'cnn-avgpool-tanh': return convnet(dataset_name, [32, 32], activation='tanh', pooling='average', bias=True) # ====== convolutional networks with BN ======= elif arch_id == 'cnn-bn-relu': return convnet_bn(dataset_name, [32, 32], activation='relu', pooling='max', bias=True) elif arch_id == 'cnn-bn-elu': return convnet_bn(dataset_name, [32, 32], activation='elu', pooling='max', bias=True) elif arch_id == 'cnn-bn-tanh': return convnet_bn(dataset_name, [32, 32], activation='tanh', pooling='max', bias=True) # ====== real networks on CIFAR-10 ======= elif arch_id == 'resnet32': return resnet32() elif arch_id == 'vgg11': return vgg11_nodropout() elif arch_id == 'vgg11-bn': return vgg11_nodropout_bn() # ====== additional networks ======== # elif arch_id == 'transformer': # return TransformerModelFixed() elif arch_id == 'deeplinear': return make_deeplinear(20, 50) elif arch_id == 'regression': return make_one_layer_network(h=100, activation='tanh') # ======= vary depth ======= elif arch_id == 'fc-tanh-depth1': return fully_connected_net(dataset_name, [200], 'tanh', bias=True) elif arch_id == 'fc-tanh-depth2': return fully_connected_net(dataset_name, [200, 200], 'tanh', bias=True) elif arch_id == 'fc-tanh-depth3': return fully_connected_net(dataset_name, [200, 200, 200], 'tanh', bias=True) elif arch_id == 'fc-tanh-depth4': return fully_connected_net(dataset_name, [200, 200, 200, 200], 'tanh', bias=True)
def incremental_learning(): # Define transforms for training phase transform_train = transforms.Compose([ transforms.RandomCrop(32, padding=4), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) transform_test = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) net = resnet32(num_classes=NUM_CLASSES) new_acc_train_list = [] new_loss_train_list = [] new_acc_test_list = [] new_loss_test_list = [] all_acc_list = [] for i in range(int(NUM_CLASSES / CLASSES_BATCH)): print('-' * 30) print(f'**** ITERATION {i+1} ****') print('-' * 30) print('Loading the Datasets ...') print('-' * 30) train_dataset = Cifar100(classes=range(i * 10, (i + 1) * 10), train=True, transform=transform_train) test_dataset = Cifar100(classes=range(i * 10, (i + 1) * 10), train=False, transform=transform_test) print('-' * 30) print('Training ...') print('-' * 30) # Prepare Dataloaders train_dataloader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True, drop_last=True, num_workers=4) test_dataloader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False, drop_last=False, num_workers=4) net, train_accuracies, train_losses, test_accuracies, test_losses = train( net, train_dataloader, test_dataloader) new_acc_train_list.append(train_accuracies) new_loss_train_list.append(train_losses) new_acc_test_list.append(test_accuracies) new_loss_test_list.append(test_losses) print('Testing ...') print('-' * 30) # Creating dataset for test on previous classes previous_classes = np.array([]) all_classes_dataset = Cifar100(classes=range(0, (i + 1) * 10), train=False, transform=transform_test) # Prepare Dataloader test_all_dataloader = DataLoader(all_classes_dataset, batch_size=BATCH_SIZE, shuffle=False, drop_last=False, num_workers=4) print('All classes') all_acc_list.append(test(net, test_all_dataloader)) print('-' * 30) return new_acc_train_list, new_loss_train_list, new_acc_test_list, new_loss_test_list, all_acc_list