Skip to content

Commit

Permalink
added comments to traintestloop.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ilennaj committed Sep 1, 2020
1 parent fd35f95 commit 580051d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 30 deletions.
63 changes: 48 additions & 15 deletions custompackage/.ipynb_checkpoints/traintestloop-checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import Optimizer
from pytorchtools import EarlyStopping



def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, randorder=False, patience=60):
Expand All @@ -30,14 +32,17 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran
# to track the average validation loss per epoch as the model trains
avg_valid_losses = []

#
# if randorder == True, generate the randomizer index array for randomizing the input image pixel order
if randorder == True:
ordering = torch.randperm(len(trainloader.dataset.tensors[0][0]))

# Initialize early stopping object
early_stopping = EarlyStopping(patience=patience, verbose=False)

for epoch in range(epochs): # loop over the dataset multiple times

######################
# train the model #
######################
running_loss = 0.0
running_acc = 0.0
model.train()
Expand All @@ -46,6 +51,7 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran
# get the inputs; data is a list of [inputs, labels]
inputs, labels, _ = data
if randorder == True:
# Randomize pixel order
inputs = inputs[:,ordering].cuda()
else:
inputs = inputs.cuda()
Expand All @@ -72,18 +78,24 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran
# print statistics
running_loss += loss.item()
running_acc += (torch.round(outputs) == labels.float().reshape(-1,1)).sum().item()/trainloader.batch_size
if (i % 4) == 3: # print every 80 mini-batches
loss_curve.append(running_loss/3)
acc_curve.append(running_acc/3)
# Generate loss and accuracy curves by saving average every 4th minibatch
if (i % 4) == 3:
loss_curve.append(running_loss/4)
acc_curve.append(running_acc/4)
running_loss = 0.0
running_acc = 0.0

######################
# validate the model #
######################
model.eval() # prep model for evaluation
for _, data in enumerate(validloader):
inputs, labels, _ = data
inputs = inputs.cuda()
if randorder == True:
# Randomize pixel order
inputs = inputs[:,ordering].cuda()
else:
inputs = inputs.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
output = model(inputs)
Expand All @@ -95,7 +107,7 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran
valid_loss = np.average(valid_losses)


# early_stopping needs the validation loss to check if it has decresed,
# early_stopping needs the validation loss to check if it has decreased,
# and if it has, it will make a checkpoint of the current model
early_stopping(valid_loss, model)

Expand All @@ -108,22 +120,29 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran

print('Finished Training, %d epochs' % (epoch+1))

######################
# test the model #
######################
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels, _ = data
if randorder == True:
images = images[:,ordering].cuda()
# Randomize pixel order
inputs = inputs[:,ordering].cuda()
else:
images = images.cuda()
inputs = inputs.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
outputs = model(images)
# calculate the loss
loss = criterion(outputs, labels.float().reshape(-1,1))
# Sum up correct labelings
predicted = torch.round(outputs)
total += labels.size(0)
correct += (predicted == labels.float().reshape(-1,1)).sum().item()

# Calculate test accuracy
accuracy = correct/total

print('Accuracy of the network on the test images: %2f %%' % (
Expand All @@ -135,6 +154,12 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran
return(loss_curve, acc_curve, loss, accuracy, model)

def train_test_fc(model, trainloader, validloader, testloader, epochs=10, patience=60):
'''
Trains and tests fcnn models
Inputs: model, trainloader, validloader, testloader, epochs, patience
Outputs: train loss_curve, train acc_curve, test ave_loss, test accuracy, trained model
'''
# Initialize loss function and optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

Expand All @@ -144,15 +169,18 @@ def train_test_fc(model, trainloader, validloader, testloader, epochs=10, patien
# to track the average validation loss per epoch as the model trains
avg_valid_losses = []


# to track training loss and accuracy as model trains
loss_curve = []
acc_curve = []

# Initialize early stopping object
early_stopping = EarlyStopping(patience=patience, verbose=False)


for epoch in range(epochs): # loop over the dataset multiple times

######################
# train the model #
######################
running_loss = 0.0
running_acc = 0.0
model.train()
Expand All @@ -176,9 +204,9 @@ def train_test_fc(model, trainloader, validloader, testloader, epochs=10, patien
# print statistics
running_loss += loss.item()
running_acc += (torch.round(outputs) == labels.float().reshape(-1,1)).sum().item()/trainloader.batch_size
if i % 4 == 3: # print every 80 mini-batches
loss_curve.append(running_loss/3)
acc_curve.append(running_acc/3)
if i % 4 == 3: # Generate loss and accuracy curves by saving average every 4th minibatch
loss_curve.append(running_loss/4)
acc_curve.append(running_acc/4)
running_loss = 0.0
running_acc = 0.0

Expand Down Expand Up @@ -221,13 +249,18 @@ def train_test_fc(model, trainloader, validloader, testloader, epochs=10, patien
images, labels, _ = data
images = images.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
outputs = model(images)
# calculate the loss
loss = criterion(outputs, labels.float().reshape(-1,1))
# Sum up correct labelings
predicted = torch.round(outputs)
total += labels.size(0)
correct += (predicted == labels.float().reshape(-1,1)).sum().item()
all_loss += loss
# Calculate test accuracy
accuracy = correct/total
# Calculate average loss
ave_loss = all_loss.item()/total

print('Accuracy of the network on the 10000 test images: %d %%' % (
Expand Down
Binary file modified custompackage/__pycache__/traintestloop.cpython-36.pyc
Binary file not shown.
63 changes: 48 additions & 15 deletions custompackage/traintestloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import Optimizer
from pytorchtools import EarlyStopping



def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, randorder=False, patience=60):
Expand All @@ -30,14 +32,17 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran
# to track the average validation loss per epoch as the model trains
avg_valid_losses = []

#
# if randorder == True, generate the randomizer index array for randomizing the input image pixel order
if randorder == True:
ordering = torch.randperm(len(trainloader.dataset.tensors[0][0]))

# Initialize early stopping object
early_stopping = EarlyStopping(patience=patience, verbose=False)

for epoch in range(epochs): # loop over the dataset multiple times

######################
# train the model #
######################
running_loss = 0.0
running_acc = 0.0
model.train()
Expand All @@ -46,6 +51,7 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran
# get the inputs; data is a list of [inputs, labels]
inputs, labels, _ = data
if randorder == True:
# Randomize pixel order
inputs = inputs[:,ordering].cuda()
else:
inputs = inputs.cuda()
Expand All @@ -72,18 +78,24 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran
# print statistics
running_loss += loss.item()
running_acc += (torch.round(outputs) == labels.float().reshape(-1,1)).sum().item()/trainloader.batch_size
if (i % 4) == 3: # print every 80 mini-batches
loss_curve.append(running_loss/3)
acc_curve.append(running_acc/3)
# Generate loss and accuracy curves by saving average every 4th minibatch
if (i % 4) == 3:
loss_curve.append(running_loss/4)
acc_curve.append(running_acc/4)
running_loss = 0.0
running_acc = 0.0

######################
# validate the model #
######################
model.eval() # prep model for evaluation
for _, data in enumerate(validloader):
inputs, labels, _ = data
inputs = inputs.cuda()
if randorder == True:
# Randomize pixel order
inputs = inputs[:,ordering].cuda()
else:
inputs = inputs.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
output = model(inputs)
Expand All @@ -95,7 +107,7 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran
valid_loss = np.average(valid_losses)


# early_stopping needs the validation loss to check if it has decresed,
# early_stopping needs the validation loss to check if it has decreased,
# and if it has, it will make a checkpoint of the current model
early_stopping(valid_loss, model)

Expand All @@ -108,22 +120,29 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran

print('Finished Training, %d epochs' % (epoch+1))

######################
# test the model #
######################
correct = 0
total = 0
with torch.no_grad():
for data in testloader:
images, labels, _ = data
if randorder == True:
images = images[:,ordering].cuda()
# Randomize pixel order
inputs = inputs[:,ordering].cuda()
else:
images = images.cuda()
inputs = inputs.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
outputs = model(images)
# calculate the loss
loss = criterion(outputs, labels.float().reshape(-1,1))
# Sum up correct labelings
predicted = torch.round(outputs)
total += labels.size(0)
correct += (predicted == labels.float().reshape(-1,1)).sum().item()

# Calculate test accuracy
accuracy = correct/total

print('Accuracy of the network on the test images: %2f %%' % (
Expand All @@ -135,6 +154,12 @@ def train_test_ktree(model, trainloader, validloader, testloader, epochs=10, ran
return(loss_curve, acc_curve, loss, accuracy, model)

def train_test_fc(model, trainloader, validloader, testloader, epochs=10, patience=60):
'''
Trains and tests fcnn models
Inputs: model, trainloader, validloader, testloader, epochs, patience
Outputs: train loss_curve, train acc_curve, test ave_loss, test accuracy, trained model
'''
# Initialize loss function and optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)

Expand All @@ -144,15 +169,18 @@ def train_test_fc(model, trainloader, validloader, testloader, epochs=10, patien
# to track the average validation loss per epoch as the model trains
avg_valid_losses = []


# to track training loss and accuracy as model trains
loss_curve = []
acc_curve = []

# Initialize early stopping object
early_stopping = EarlyStopping(patience=patience, verbose=False)


for epoch in range(epochs): # loop over the dataset multiple times

######################
# train the model #
######################
running_loss = 0.0
running_acc = 0.0
model.train()
Expand All @@ -176,9 +204,9 @@ def train_test_fc(model, trainloader, validloader, testloader, epochs=10, patien
# print statistics
running_loss += loss.item()
running_acc += (torch.round(outputs) == labels.float().reshape(-1,1)).sum().item()/trainloader.batch_size
if i % 4 == 3: # print every 80 mini-batches
loss_curve.append(running_loss/3)
acc_curve.append(running_acc/3)
if i % 4 == 3: # Generate loss and accuracy curves by saving average every 4th minibatch
loss_curve.append(running_loss/4)
acc_curve.append(running_acc/4)
running_loss = 0.0
running_acc = 0.0

Expand Down Expand Up @@ -221,13 +249,18 @@ def train_test_fc(model, trainloader, validloader, testloader, epochs=10, patien
images, labels, _ = data
images = images.cuda()
labels = labels.cuda()
# forward pass: compute predicted outputs by passing inputs to the model
outputs = model(images)
# calculate the loss
loss = criterion(outputs, labels.float().reshape(-1,1))
# Sum up correct labelings
predicted = torch.round(outputs)
total += labels.size(0)
correct += (predicted == labels.float().reshape(-1,1)).sum().item()
all_loss += loss
# Calculate test accuracy
accuracy = correct/total
# Calculate average loss
ave_loss = all_loss.item()/total

print('Accuracy of the network on the 10000 test images: %d %%' % (
Expand Down

0 comments on commit 580051d

Please sign in to comment.