I am trying to develop a transfer learning CNN model using the googlenet CNN and modifying it so that it contains a few hidden layers at the end. The loss function seems to converge to a low value for 10 epochs, however my predictions for the test data are terrible. I know that I should be getting an appropriate result because some of my peers are. Please help me, do you see any problem with my implementation?
import torchvision.models as models
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
torch.manual_seed(12)
#call googlenet CNN
googlenet = models.googlenet(pretrained=True)
#Modify googlenet by adding extra hidden layers
num_features = googlenet.fc.in_features
new_fc_layer = nn.Sequential(
nn.Linear(num_features, 512),
nn.ReLU(),
nn.Linear(512,100),
nn.ReLU(),
nn.Linear(100,1) # replace num_classes with the desired number of output classes
)
googlenet.fc = new_fc_layer
model = googlenet.to(device)
#Parameters
num_epochs = 10
learning_rate = 0.001
batch_size = 10
criterion = nn.MSELoss() #loss function
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate,weight_decay=5e-4)
num_batches = len(X_train) // batch_size
L=[]
model.train()
for epoch in range(num_epochs):
for i in range(0, len(X_train), batch_size):
x_batch = torch.FloatTensor(X_train[i:i+batch_size]).to(device)
y_batch = torch.FloatTensor(Y_train[i:i+batch_size]).to(device)
# Compute the model's predictions and loss on this batch
y_pred = model(x_batch)
loss = criterion(y_pred, y_batch)
# Backpropagate the loss and update the parameters
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Update the training loss
L.append( loss.item())
print('Epoch: ',epoch, 'loss: ',loss.item())
model.eval()
predicted_counts_CNN = []
with torch.no_grad():
for i in range(0, len(X_test), batch_size):
x_test_batch = torch.FloatTensor(X_test[i:i+batch_size]).to(device)
x_test_batch=x_test_batch.to(device)
preds_batch = model(x_test_batch)
predicted_counts_CNN.append(preds_batch.cpu().detach().numpy())