import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from avalanche.benchmarks import NCScenario
from avalanche.training.supervised import EWC
train_dataset = torchvision.datasets.ImageFolder(root='data/train', transform=transforms.ToTensor())
test_dataset = torchvision.datasets.ImageFolder(root='data/val', transform=transforms.ToTensor())
class CustomResNet(nn.Module):
def __init__(self, num_classes):
super(CustomResNet, self).__init__()
self.resnet = torchvision.models.resnet18(pretrained=True)
self.resnet.fc = nn.Linear(self.resnet.fc.in_features, num_classes)
def forward(self, x):
return self.resnet(x)
model = CustomResNet(num_classes=len(train_dataset.classes))
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
criterion = nn.CrossEntropyLoss()
scenario = NCScenario(train_dataset, test_dataset, n_experiences=len(train_dataset.classes), task_labels=True)
ewc = EWC(model, optimizer, criterion, ewc_lambda=0.1)
for train_task in scenario.train_stream:
ewc.train(train_task, epochs=5)
I wrote this code, but i don't understand in this avalanche library how can i instead of training each of my labels incrementally, download my model with 34 labels and add just one new label so i want to have 2 experiences.
I also tried to solve this problem by using PyCIL library but i also don't understand this part in there.