Hello everybody,
I am new to Pytorch and I am have a problem, it is probably something basic. I have just trained a simple model for FashionMNIST (as an example), then transfer the learning to a new CIFAR problem. The code for FashionMNIST is:
CNN(
(conv1): Conv2d(3, 16, kernel_size=(5, 5), stride=(1, 1))
(conv2): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1))
(conv3): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1))
(fc): Linear(in_features=512, out_features=64, bias=True)
(classifier): Linear(in_features=64, out_features=10, bias=True)
)
class_names = trainset.classes
dataiter = iter(trainloader)
inputs, classes = dataiter.next()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model_ft = best_model_FashionMNIST
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, 64)
model_ft = model_ft.to(device)
criterion = nn.CrossEntropyLoss()
optimizer_ft = optim.Adam(model_ft.parameters(), lr=0.0001)
model_ft = train_model(model_ft, criterion, optimizer_ft, device, num_epochs=20)
I train the new probem with parameters from FashionMNIST:
model_ft.eval()
classes = testset.classes
with tqdm.notebook.tqdm(total=len(testloader), unit='batch', desc=f'Evaluation',
position=100, leave=True) as pbar:
for X, Y in testloader:
X = X.to(device)
Y = Y.to(device)
_, Y_ = torch.max(model_ft(X).data, 1)
for y, y_ in zip(Y, Y):
if y == y_:
correct[y] += 1
total[y] += 1
acc[y] = correct[y]/total[y]
pbar.set_postfix(accuracy=sum(acc)/len(classes))
pbar.update()
for class_name,class_acc in zip(classes,acc):
print(f'Accuracy for class {class_name:5s}: {class_acc:.2f}')
print(f'Accuracy: {sum(acc)/len(classes):.2f}')
When trying to solve, I do not understand the reason that I get only two classes in the prediction, could someone tell me why it is due please?
The results is:
Y_:
tensor([0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1,
0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0,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