High accuracy for each epoch

air_plane: <p>hello everyone here is my train code and I  am trying to get train accuracy but outputs are too high for each epoch .Could you help me ?<br>
import torch<br>
from torch.utils.data import DataLoader<br>
import time<br>
import argparse<br>
from progress.bar import IncrementalBar<br>
import numpy as np<br>
import logging</p>
<p>from dataset import Tissue<br>
from dataset import transforms as T<br>
from gan.generator import UnetGenerator<br>
from gan.discriminator import ConditionalDiscriminator<br>
from gan.criterion import GeneratorLoss, DiscriminatorLoss<br>
from gan.utils import Logger</p>
<p>parser = argparse.ArgumentParser(prog=‘top’, description=‘Train Pix2Pix’)<br>
parser.add_argument(“–epochs”, type=int, default=100, help=“Number of epochs”)<br>
parser.add_argument(“–dataset”, type=str, default=“tissue”, help=“Name of the dataset: [‘tissue’]”)<br>
parser.add_argument(“–batch_size”, type=int, default=32, help=“Size of the batches”)<br>
parser.add_argument(“–lr”, type=float, default=0.02, help=“Adams learning rate”)<br>
args = parser.parse_args()</p>
<p>device = ‘cuda’ if torch.cuda.is_available() else ‘cpu’</p>
<p>transforms = T.Compose([T.ToTensor(),<br>
T.Normalize(mean=[0.5, 0.5, 0.5],<br>
std=[0.5, 0.5, 0.5])])</p>
<h1><a name="models-1" class="anchor" href="#models-1"></a>models</h1>
<p>print(‘Defining models!’)<br>
generator = UnetGenerator().to(device)<br>
discriminator = ConditionalDiscriminator().to(device)</p>
<h1><a name="optimizers-2" class="anchor" href="#optimizers-2"></a>optimizers</h1>
<p>g_optimizer = torch.optim.Adam(generator.parameters(), lr=args.lr)<br>
d_optimizer = torch.optim.Adam(discriminator.parameters(), lr=args.lr)</p>
<h1><a name="loss-functions-3" class="anchor" href="#loss-functions-3"></a>loss functions</h1>
<p>g_criterion = GeneratorLoss(alpha=100)<br>
d_criterion = DiscriminatorLoss()</p>
<h1><a name="dataset-4" class="anchor" href="#dataset-4"></a>dataset</h1>
<p>print(f’Downloading “{args.dataset.upper()}” dataset!‘)<br>
dataset = Tissue(root=’.', transform=transforms, download=False, mode=‘train’)</p>
<p>dataloader = DataLoader(dataset, batch_size=args.batch_size, shuffle=False)</p>
<h1><a name="start-of-training-process-5" class="anchor" href="#start-of-training-process-5"></a>Start of training process</h1>
<p>print(‘Start of training process!’)<br>
logger = Logger(filename=args.dataset)</p>
<p>total_correct = 0<br>
total_images = 0</p>
<p>for epoch in range(args.epochs):<br>
ge_loss = 0.<br>
de_loss = 0.<br>
start = time.time()<br>
bar = IncrementalBar(f’[Epoch {epoch + 1}/{args.epochs}]', max=len(dataloader))</p>
<pre><code>epoch_correct = 0
epoch_images = 0

for x, real in dataloader:
    x = x.to(device)
    real = real.to(device)

    # Generator`s loss
    fake = generator(x)
    fake_pred = discriminator(fake, x)
    g_loss = g_criterion(fake, real, fake_pred)

    # Discriminator`s loss
    fake = generator(x).detach()
    fake_pred = discriminator(fake, x)
    real_pred = discriminator(real, x)
    d_loss = d_criterion(fake_pred, real_pred)

    # Generator`s params update
    g_optimizer.zero_grad()
    g_loss.backward()
    g_optimizer.step()

    # Discriminator`s params update
    d_optimizer.zero_grad()
    d_loss.backward()
    d_optimizer.step()

    # add batch losses
    ge_loss += g_loss.item()
    de_loss += d_loss.item()
    bar.next()

    # Calculate accuracy per batch
    with torch.no_grad():
        generator.eval()
        outputs = generator(x)
        _, predicted = torch.max(outputs.data, 1)
        total = real.size(0)
        _, labels_indices = torch.max(real, 1)
        correct = (predicted == labels_indices).sum().item()
        epoch_correct += correct
        epoch_images += total

bar.finish()

# obtain per epoch losses
g_loss_avg = ge_loss / len(dataloader)
d_loss_avg = de_loss / len(dataloader)

# Log losses
logger.add_scalar('generator_loss', g_loss_avg, epoch + 1)
logger.add_scalar('discriminator_loss', d_loss_avg, epoch + 1)
logger.save_weights(generator.state_dict(), 'generator')
logger.save_weights(discriminator.state_dict(), 'discriminator')

# Log accuracy per epoch
epoch_accuracy = (epoch_correct / epoch_images) * 100
print("Epoch {}: Accuracy of the network on the {} Train images: {:.2f} %".format(epoch + 1, epoch_images,
                                                                                  epoch_accuracy))

# accumulate correct predictions and total images processed across all epochs
total_correct += epoch_correct
total_images += epoch_images

# count timeframe
end = time.time()
tm = (end - start)
</code></pre>
<h1><a name="calculate-overall-accuracy-6" class="anchor" href="#calculate-overall-accuracy-6"></a>Calculate overall accuracy</h1>
<p>overall_accuracy = (total_correct / total_images) * 100<br>
print(“Overall Accuracy of the network on the {} Train images: {:.2f} %”.format(total_images, overall_accuracy))</p>
<p>logger.close()<br>
print(‘End of training process!’)</p>
<p>Defining models!<br>
Downloading “TISSUE” dataset!<br>
Start of training process!<br>
Epoch 1: Accuracy of the network on the 400 Train images: 3140211.25 %<br>
Epoch 2: Accuracy of the network on the 400 Train images: 3021189.50 %<br>
Epoch 3: Accuracy of the network on the 400 Train images: 2870963.25 %<br>
Epoch 4: Accuracy of the network on the 400 Train images: 2798101.00 %<br>
Epoch 5: Accuracy of the network on the 400 Train images: 2781629.50 %<br>
Epoch 6: Accuracy of the network on the 400 Train images: 2778091.50 %<br>
Epoch 7: Accuracy of the network on the 400 Train images: 2777310.75 %<br>
Epoch 8: Accuracy of the network on the 400 Train images: 2777132.50 %<br>
Epoch 9: Accuracy of the network on the 400 Train images: 2777086.50 %<br>
Epoch 10: Accuracy of the network on the 400 Train images: 2777074.00 %<br>
Epoch 11: Accuracy of the network on the 400 Train images: 2777070.00 %<br>
Epoch 12: Accuracy of the network on the 400 Train images: 2777069.00 %<br>
Epoch 13: Accuracy of the network on the 400 Train images: 2777067.50 %<br>
Epoch 14: Accuracy of the network on the 400 Train images: 2777068.75 %</p>

mhm2020: <p>what is the dimension of the real and outputs.data?<br>
it sounds that the output of the generator is not 1D, so the sum function in the line “correct = (predicted == labels_indices).sum().item()” return the sum of all elements in (predicted == labels_indices), while total is only the number of images per current batch.<br>
That is why the correct is higher than total in your calculation and consequently getting large accuracy numbers.</p>