Skip to content

Commit

Permalink
Merge pull request #1 from StoneY1/stone_dev
Browse files Browse the repository at this point in the history
Changes made to Dataloader and train_test.py
  • Loading branch information
harryrobotics committed Nov 30, 2020
2 parents d27345f + e04854d commit f3c4296
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 51 deletions.
57 changes: 36 additions & 21 deletions dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import torchnet as tnt
import torchvision.datasets as datasets
import torchvision.transforms as transforms
# from Places205 import Places205
import numpy as np
import random
from torch.utils.data.dataloader import default_collate
Expand Down Expand Up @@ -63,8 +62,12 @@ def __init__(self, dataset_name, split, random_sized_crop=False,
###CIFAR100

#this mean and std is from CIFAR10
self.mean_pix = [x/255.0 for x in [125.3, 123.0, 113.9]]
self.std_pix = [x/255.0 for x in [63.0, 62.1, 66.7]]
#self.mean_pix = [x/255.0 for x in [125.3, 123.0, 113.9]]
#self.std_pix = [x/255.0 for x in [63.0, 62.1, 66.7]]

# For CIFAR-100
self.mean_pix = (0.5071, 0.4867, 0.4408)
self.std_pix = (0.2675, 0.2565, 0.2761)

if self.random_sized_crop:
raise ValueError('The random size crop option is not supported for the CIFAR dataset')
Expand All @@ -73,8 +76,8 @@ def __init__(self, dataset_name, split, random_sized_crop=False,

if (split != 'test'): #If load training data, the perform augmentation
# transform.append(transforms.RandomCrop(32, padding=4))
transform.append(transforms.RandomHorizontalFlip())

#transform.append(transforms.RandomHorizontalFlip())
pass
transform.append(lambda x: np.asarray(x))

self.transform = transforms.Compose(transform)
Expand Down Expand Up @@ -148,7 +151,6 @@ def rotate_img(img, rot):
else:
raise ValueError('rotation should be 0, 90, 180, or 270 degrees')


class DataLoader(object):
"""If flag is set to unsupervised, will generate the rotations and rotation-labels during training"""
def __init__(self,
Expand All @@ -164,33 +166,46 @@ def __init__(self,
self.batch_size = batch_size
self.unsupervised = unsupervised
self.num_workers = num_workers
self.split = self.dataset.split

mean_pix = self.dataset.mean_pix
std_pix = self.dataset.std_pix

my_transformations = [
# transforms.ToPILImage(),
# transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0),
# transforms.RandomGrayscale(p=0.1),
# transforms.RandomCrop(32, padding=4),
# transforms.RandomResizedCrop(32, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=2),
# transforms.RandomHorizontalFlip(),
transforms.ToTensor()
transforms.ToPILImage(),
transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2),
transforms.RandomGrayscale(p=0.2),
transforms.RandomCrop(32, padding=4, padding_mode='reflect'),
transforms.RandomResizedCrop(32, scale=(0.2, 1.0), ratio=(0.75, 1.3333333333333333), interpolation=2),
transforms.RandomVerticalFlip(),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=mean_pix, std=std_pix)
]

# If testing we won't use any transforms
self.passthrough_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=mean_pix, std=std_pix)
])

if self.unsupervised:
self.transform = transforms.Compose([
transforms.ToTensor()
# transforms.Normalize(mean=mean_pix, std=std_pix)
transforms.ToTensor(),
transforms.Normalize(mean=mean_pix, std=std_pix)
])
else:
print("Compse transofrom supervised mode")
self.transform = transforms.Compose(my_transformations)
self.inv_transform = transforms.Compose([
Denormalize(mean_pix, std_pix),
lambda x: x.numpy() * 255.0,
lambda x: x.transpose(1,2,0).astype(np.uint8),
])
if self.split == "test":
print("Testing set has no transforms")
self.transform = self.passthrough_transform
else:
self.transform = transforms.Compose(my_transformations)
self.inv_transform = transforms.Compose([
Denormalize(mean_pix, std_pix),
lambda x: x.numpy() * 255.0,
lambda x: x.transpose(1,2,0).astype(np.uint8),
])

def get_iterator(self, epoch=0):
# print("get iterator")
Expand Down
76 changes: 46 additions & 30 deletions train_test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@



from __future__ import print_function
import argparse
import copy
import os
import imp
from dataloader import DataLoader, GenericDataset
import matplotlib.pyplot as plt

#from model import BowNet
from model import BowNet2 as BowNet
from model import BowNet
#from model import BowNet2 as BowNet
#from model import BowNet3 as BowNet
from tqdm import tqdm
import torch
import torch.nn as nn
Expand Down Expand Up @@ -52,8 +51,9 @@ def accuracy(output, target, topk=(1,)):
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
correct_preds = copy.deepcopy(correct_k)
res.append(correct_k.mul_(100.0 / batch_size))
return res
return res, correct_preds.int().item()

dataset_train = GenericDataset(
dataset_name=data_train_opt['dataset_name'],
Expand Down Expand Up @@ -81,14 +81,17 @@ def accuracy(output, target, topk=(1,)):
num_workers=4,
shuffle=False)



device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

num_epochs = 200
bownet = BowNet(num_classes=4).to(device)
criterion = nn.CrossEntropyLoss().to(device)
optimizer = optim.SGD(bownet.parameters(), lr=0.1, momentum=0.9,weight_decay= 5e-4)
#optimizer = optim.SGD(bownet.parameters(), lr=0.01, momentum=0.9, weight_decay=5e-4) # used for BowNet2 and 3
#optimizer = optim.Adam(bownet.parameters())
optimizer = optim.SGD(bownet.parameters(), lr=0.01, momentum=0.9, weight_decay=5e-3)
#lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.2)
#lr_scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=60, gamma=0.2)
lr_scheduler = optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', factor=0.5, patience=5)

# tensors = {}
# tensors['dataX'] = torch.FloatTensor()
Expand All @@ -97,14 +100,16 @@ def accuracy(output, target, topk=(1,)):
for epoch in range(num_epochs): # loop over the dataset multiple times

print()
print("TRAINING")
print(f"TRAINING Epoch {epoch+1}")
running_loss = 0.0
loss_100 = 0.0
total_correct = 0
total_samples = 0

print("number of batch: ",len(dloader_train))
start_epoch = time.time()
accs = []

bownet.train()
for idx, batch in enumerate(tqdm(dloader_train(epoch))): #We feed epoch in dloader_train to get a deterministic batch

start_time = time.time()
Expand All @@ -113,14 +118,12 @@ def accuracy(output, target, topk=(1,)):

check_input = inputs[0].permute(1, 2, 0)


#Load data to GPU
inputs, labels = inputs.cuda(), labels.cuda()
time_load_data = time.time() - start_time

# print(labels)


# zero the parameter gradients
optimizer.zero_grad()

Expand All @@ -130,8 +133,7 @@ def accuracy(output, target, topk=(1,)):
# print(preds[:,0])

#Compute loss
loss = criterion(logits, labels)

loss = criterion(logits[:, 0], labels)

#Back Prop and Optimize
loss.backward()
Expand All @@ -141,43 +143,51 @@ def accuracy(output, target, topk=(1,)):
running_loss += loss.item()

loss_100 += loss.item()

accs.append(accuracy(preds[:,0].data, labels, topk=(1,))[0].item())

acc_batch, batch_correct_preds = accuracy(preds[:,0].data, labels, topk=(1,))
accs.append(acc_batch[0].item())
total_correct += batch_correct_preds
total_samples += preds.size(0)

'''
if idx % 100 == 99:
print('[%d, %5d] loss: %.3f' %
(epoch , idx, loss_100/100))
loss_100 = 0.0
acc = accuracy(preds[:,0].data, labels, topk=(1,))[0].item()
acc, correct = accuracy(preds[:,0].data, labels, topk=(1,))[0].item()
print("accuracy 100 batch: ",acc)
print("Time to finish 100 batch", time.time() - start_time)

'''

# plt.imshow(check_input)
# plt.savefig("imag" + str(epoch) + ".png")
accs = np.array(accs)
print("epoch training accuracy: ",accs.mean())
#print("epoch training accuracy: ",accs.mean())
#lr_scheduler.step()
print("epoch training accuracy: ", 100*total_correct/total_samples)

print("Time to load the data", time_load_data)
print("Time to finish an epoch ", time.time() - start_epoch)
print('[%d, %5d] epoches loss: %.3f' %
(epoch, len(dloader_train), running_loss / len(dloader_train)))

PATH = "bownet_checkpoint.pt"
PATH = "bownet_checkpoint1.pt"
#PATH = "bownet_checkpoint2.pt"
torch.save({
'epoch': epoch,
'model_state_dict': bownet.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'loss': loss,
}, PATH)

torch.cuda.empty_cache()
print()
print("EVALUATION")

print(f"EVALUATION Epoch {epoch+1}")
bownet.eval()
print("number of batch: ",len(dloader_test))
start_epoch = time.time()
running_loss = 0.0
accs = []
test_correct = 0
test_total = 0
for idx, batch in enumerate(tqdm(dloader_test())): #We don't feed epoch to dloader_test because we want a random batch
start_time = time.time()
# get the inputs; data is a list of [inputs, labels]
Expand All @@ -194,26 +204,32 @@ def accuracy(output, target, topk=(1,)):
# print(preds[:,0])

#Compute loss
loss = criterion(preds[:,0], labels)
loss = criterion(logits[:,0], labels)


# print statistics
running_loss += loss.item()

accs.append(accuracy(preds[:,0].data, labels, topk=(1,))[0].item())
acc_batch, batch_correct_preds = accuracy(preds[:,0].data, labels, topk=(1,))
accs.append(acc_batch[0].item())
test_correct += batch_correct_preds
test_total += preds.size(0)
#accs.append(accuracy(preds[:,0].data, labels, topk=(1,))[0].item())


# plt.imshow(check_input)
# plt.savefig("imag" + str(epoch) + ".png")

# lr scheduler will monitor test loss
lr_scheduler.step(running_loss/len(dloader_test))
accs = np.array(accs)
print("epoche test accuracy: ",accs.mean())
#print("epoche test accuracy: ",accs.mean())
print("epoch test accuracy: ", 100*test_correct/test_total)

print("Time to load the data", time_load_data)
print("Time to finish an epoch ", time.time() - start_epoch)
print('[%d, %5d] epoches loss: %.3f' %
(epoch, len(dloader_test), running_loss / len(dloader_test)))




print('Finished Training')

0 comments on commit f3c4296

Please sign in to comment.