Loss always 0 Accuracy always 100%

Yoich1: <p>I am working on the face recognition project of the fer-2013 dataset. The output loss is always 0 and the accuracy is always 100. How can I solve it</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.optim as optim<br>
import torch.nn.functional as F<br>
import torchvision<br>
import torchvision.transforms as transforms<br>
from tqdm import tqdm</p>
<p>BATCH_SIZE = 128<br>
LR = 0.01<br>
EPOCH = 60<br>
DEVICE = torch.device(‘cpu’)</p>
<p>path_train = ‘face_images/train_set’<br>
path_vaild = ‘face_images/vaild_set’</p>
<p>transforms_train = transforms.Compose([<br>
transforms.Grayscale(),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ColorJitter(brightness=0.5, contrast=0.5),<br>
transforms.ToTensor()<br>
])<br>
transforms_vaild = transforms.Compose([<br>
transforms.Grayscale(),<br>
transforms.ToTensor()<br>
])</p>
<p>data_train = torchvision.datasets.ImageFolder(root=path_train,transform=transforms_train)<br>
data_vaild = torchvision.datasets.ImageFolder(root=path_vaild,transform=transforms_vaild)</p>
<p>train_set = torch.utils.data.DataLoader(dataset=data_train,batch_size=BATCH_SIZE,shuffle=True)<br>
vaild_set = torch.utils.data.DataLoader(dataset=data_vaild,batch_size=BATCH_SIZE,shuffle=False)</p>
<p>class VGG(nn.Module):<br>
def <strong>init</strong>(self, *args):<br>
super(VGG, self).<strong>init</strong>()</p>
<pre><code>def forward(self, x):
    return x.view(x.shape[0],-1)
</code></pre>
<p>def vgg_block(num_convs, in_channels, out_channels):<br>
blk = []<br>
for i in range(num_convs):<br>
if i == 0:<br>
blk.append(nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1))<br>
else:<br>
blk.append(nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1))<br>
blk.append(nn.ReLU())<br>
blk.append(nn.MaxPool2d(kernel_size=2, stride=2)) # 这里会使宽高减半<br>
return nn.Sequential(*blk)</p>
<p>conv_arch = ((2, 1, 32), (3, 32, 64), (3, 64, 128))</p>
<p>fc_features = 128 * 6* 6 # c * w * h<br>
fc_hidden_units = 4096 # 任意</p>
<p>def vgg(conv_arch, fc_features, fc_hidden_units):<br>
net = nn.Sequential()</p>
<pre><code>for i, (num_convs, in_channels, out_channels) in enumerate(conv_arch):
    
    net.add_module("vgg_block_" + str(i+1), vgg_block(num_convs, in_channels, out_channels))

net.add_module("fc", nn.Sequential(
                             VGG(),
                             nn.Linear(fc_features, fc_hidden_units),
                             nn.ReLU(),
                             nn.Dropout(0.5),
                             nn.Linear(fc_hidden_units, fc_hidden_units),
                             nn.ReLU(),
                             nn.Dropout(0.5),
                             nn.Linear(fc_hidden_units, 7)
                            ))
return net
</code></pre>
<p>model = vgg(conv_arch, fc_features, fc_hidden_units)<br>
model.to(DEVICE)<br>
optimizer = optim.SGD(model.parameters(),lr=LR,momentum=0.9)<br>
<span class="hashtag">#optim</span>.Adam(model.parameters())<br>
criterion = nn.CrossEntropyLoss()</p>
<p>train_loss = []<br>
train_ac = []<br>
vaild_loss = []<br>
vaild_ac = []<br>
y_pred = []</p>
<p>def train(model,device,dataset,optimizer,epoch):<br>
model.train()<br>
correct = 0<br>
for i,(x,y) in tqdm(enumerate(dataset)):<br>
x , y  = x.to(device), y.to(device)<br>
optimizer.zero_grad()<br>
output = model(x)<br>
pred = output.max(1,keepdim=True)[1]<br>
correct += pred.eq(y.view_as(pred)).sum().item()<br>
loss = criterion(output,y)<br>
loss.backward()<br>
optimizer.step()</p>
<h1>
<a name="train_acappendcorrectlendata_train-1" class="anchor" href="#train_acappendcorrectlendata_train-1"></a>train_ac.append(correct/len(data_train))</h1>
<pre><code>#train_loss.append(loss.item())
print("Epoch {} Loss {:.4f} Accuracy {}/{} ({:.0f}%)".format(epoch,loss,correct,len(data_train),100*correct/len(data_train)))
</code></pre>
<p>def vaild(model,device,dataset):<br>
model.eval()<br>
correct = 0<br>
with torch.no_grad():<br>
for i,(x,y) in tqdm(enumerate(dataset)):<br>
x,y = x.to(device) ,y.to(device)<br>
output = model(x)<br>
loss = criterion(output,y)<br>
pred = output.max(1,keepdim=True)[1]<br>
global  y_pred<br>
y_pred += pred.view(pred.size()[0]).cpu().numpy().tolist()<br>
correct += pred.eq(y.view_as(pred)).sum().item()</p>
<pre><code>#vaild_ac.append(correct/len(data_vaild))
</code></pre>
<h1>
<a name="vaild_lossappendlossitem-2" class="anchor" href="#vaild_lossappendlossitem-2"></a>vaild_loss.append(loss.item())</h1>
<pre><code>print("Test Loss {:.4f} Accuracy {}/{} ({:.0f}%)".format(loss,correct,len(data_vaild),100.*correct/len(data_vaild)))
</code></pre>
<p>def RUN():<br>
for epoch in range(1,EPOCH+1):<br>
‘’‘if epoch==15 :<br>
LR = 0.1<br>
optimizer=optimizer = optim.SGD(model.parameters(),lr=LR,momentum=0.9)<br>
if(epoch&gt;30 and epoch%15==0):<br>
LR*=0.1<br>
optimizer=optimizer = optim.SGD(model.parameters(),lr=LR,momentum=0.9)<br>
‘’’</p>
<pre><code>    train(model,device=DEVICE,dataset=train_set,optimizer=optimizer,epoch=epoch)
    vaild(model,device=DEVICE,dataset=vaild_set)
    
    torch.save(model,'model/model_vgg.pkl')
</code></pre>
<p>if <strong>name</strong> == ‘<strong>main</strong>’:<br>
RUN()</p>

ptrblck: <p>As a quick test could you check the model predictions and compare them against the targets?<br>
It would be interesting to see if you are dealing with a heavily imbalanced dataset (e.g. all samples belong to a single class) and if your model is thus overfitting this class.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.</p>