'Getting very low accuracy on my CNN Model
I have a built CNN model in pytorch and i am getting very less accuracy on it.
# model building
class CNN(nn.Module):
def __init__(self):
super().__init__()
#covolution layers
self.conv=nn.Sequential(
nn.Conv2d(3,16,3,1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(16,32,3,1),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32,64,3,1),
nn.ReLU(),
nn.MaxPool2d(2)
)
#fully connected layers
self.fc=nn.Sequential(
nn.Flatten(),
nn.LazyLinear(64),
nn.Linear(64,128),
nn.Linear(128,6)
)
#forward pass
def forward(self,x):
x=self.fc(self.conv(x))
return x
device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model=CNN().to(device)
criterion=nn.CrossEntropyLoss()
optimizer=torch.optim.Adam(model.parameters(),lr=0.01)
#trainig Network
epochs=10
print('Training Started...')
for i in range(epochs):
tr_sample=0
tr_correct=0
for b,(image,label) in enumerate(train_loader):
image=image.to(device)
label=label.to(device)
y_pred=model.forward(image)
loss=criterion(y_pred,label)
_,predicted=torch.max(y_pred,1)
tr_sample+=label.size(0)
tr_correct+=(predicted==label).sum().item()
optimizer.zero_grad()
loss.backward()
optimizer.step()
trn_acc=100*(tr_correct/tr_sample)
with torch.no_grad():
n_samples=0
n_correct=0
for image,label in test_loader:
image=image.to(device)
label=label.to(device)
y_eval=model.forward(image)
_,predicted=torch.max(y_eval,1)
n_samples+=label.size(0)
n_correct+=(predicted==label).sum().item()
acc=100*n_correct/n_samples
print(f'Epoch: {i} | loss: {loss.item()} | train accuracy:{trn_acc:.4f} % | test accuracy: {acc:.4f} %')
print('Training Finished !')
Here is the glimpse of accuracy which I am receiving.

How do i improve the accuracy of my model? I tried already tuning parameters but nothing seems to be working out. I am getting much better accuracy for same layers on tensorflow.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
