diff --git a/Notebook.py b/Notebook.py new file mode 100644 index 0000000..cd8149d --- /dev/null +++ b/Notebook.py @@ -0,0 +1,21 @@ +# Funcion para crear directorios, es necesario e abspath? +##### se agregó a utils +import os + +def create_dir(child_dir_str): + + try: + pardir = os.path.abspath('static/') + original_umask = os.umask(0o000) + os.makedirs(pardir, exist_ok=True) + child_dir = os.path.join(pardir,child_dir_str) + child_dir = os.path.abspath('static/') + os.makedirs(child_dir, exist_ok=True) + finally: + os.umask(original_umask) + print('static folder created:', os.path.isdir(pardir)) + print('img folder created: ', os.path.isdir(child_dir)) + +#create_dir('img') + +####################################################### \ No newline at end of file diff --git a/ShuffleMNIST b/ShuffleMNIST new file mode 160000 index 0000000..e4aa4da --- /dev/null +++ b/ShuffleMNIST @@ -0,0 +1 @@ +Subproject commit e4aa4da84c2cb772867a1b142472890dca80e17e diff --git a/__pycache__/config.cpython-39.pyc b/__pycache__/config.cpython-39.pyc new file mode 100644 index 0000000..b30e3a3 Binary files /dev/null and b/__pycache__/config.cpython-39.pyc differ diff --git a/core/__init__.py b/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/__pycache__/__init__.cpython-39.pyc b/core/__pycache__/__init__.cpython-39.pyc new file mode 100644 index 0000000..98d8712 Binary files /dev/null and b/core/__pycache__/__init__.cpython-39.pyc differ diff --git a/core/__pycache__/anchors.cpython-39.pyc b/core/__pycache__/anchors.cpython-39.pyc new file mode 100644 index 0000000..eca4b23 Binary files /dev/null and b/core/__pycache__/anchors.cpython-39.pyc differ diff --git a/core/__pycache__/dataset.cpython-39.pyc b/core/__pycache__/dataset.cpython-39.pyc new file mode 100644 index 0000000..4fbadec Binary files /dev/null and b/core/__pycache__/dataset.cpython-39.pyc differ diff --git a/core/__pycache__/model.cpython-39.pyc b/core/__pycache__/model.cpython-39.pyc new file mode 100644 index 0000000..5f5a7ea Binary files /dev/null and b/core/__pycache__/model.cpython-39.pyc differ diff --git a/core/__pycache__/resnet.cpython-39.pyc b/core/__pycache__/resnet.cpython-39.pyc new file mode 100644 index 0000000..c4f2cea Binary files /dev/null and b/core/__pycache__/resnet.cpython-39.pyc differ diff --git a/core/__pycache__/utils.cpython-39.pyc b/core/__pycache__/utils.cpython-39.pyc new file mode 100644 index 0000000..76a5266 Binary files /dev/null and b/core/__pycache__/utils.cpython-39.pyc differ diff --git a/core/anchors.py b/core/anchors.py old mode 100644 new mode 100755 diff --git a/core/dataset.py b/core/dataset.py old mode 100644 new mode 100755 index 948849d..266ffde --- a/core/dataset.py +++ b/core/dataset.py @@ -1,5 +1,6 @@ import numpy as np -import scipy.misc +#import scipy.misc +import imageio import os from PIL import Image from torchvision import transforms @@ -25,11 +26,12 @@ def __init__(self, root, is_train=True, data_len=None): train_file_list = [x for i, x in zip(train_test_list, img_name_list) if i] test_file_list = [x for i, x in zip(train_test_list, img_name_list) if not i] if self.is_train: - self.train_img = [scipy.misc.imread(os.path.join(self.root, 'images', train_file)) for train_file in + #se cambió scipy.misc.imread por imageio.imread + self.train_img = [imageio.imread(os.path.join(self.root, 'images', train_file)) for train_file in train_file_list[:data_len]] self.train_label = [x for i, x in zip(train_test_list, label_list) if i][:data_len] if not self.is_train: - self.test_img = [scipy.misc.imread(os.path.join(self.root, 'images', test_file)) for test_file in + self.test_img = [imageio.imread(os.path.join(self.root, 'images', test_file)) for test_file in test_file_list[:data_len]] self.test_label = [x for i, x in zip(train_test_list, label_list) if not i][:data_len] @@ -53,6 +55,7 @@ def __getitem__(self, index): img = transforms.Resize((600, 600), Image.BILINEAR)(img) img = transforms.CenterCrop(INPUT_SIZE)(img) img = transforms.ToTensor()(img) + img = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(img) return img, target diff --git a/core/model.py b/core/model.py old mode 100644 new mode 100755 diff --git a/core/resnet.py b/core/resnet.py old mode 100644 new mode 100755 diff --git a/core/utils.py b/core/utils.py old mode 100644 new mode 100755 index 536c166..2ff95b6 --- a/core/utils.py +++ b/core/utils.py @@ -4,7 +4,20 @@ import time import logging +import time + +def timer(func): + def wrapper(*args, **kwargs): + start = time.time() + rv = func(*args, **kwargs) + total = time.time() - start + print(f'Total: {total}') + return rv + return wrapper + + _, term_width = os.popen('stty size', 'r').read().split() +#term_width = 80 term_width = int(term_width) TOTAL_BAR_LENGTH = 40. @@ -100,5 +113,19 @@ def init_log(output_dir): logging.getLogger('').addHandler(console) return logging +#se agrega para crear las carpetas con los permisos correspondientes +def create_dir(dir_str): + + try: + pardir = os.path.abspath(f'{dir_str}') + original_umask = os.umask(0o000) + os.makedirs(pardir,exist_ok=True) + + finally: + os.umask(original_umask) + print(f'{dir_str} folder created:', os.path.isdir(pardir)) + + return os.path.abspath('./save_dir') + if __name__ == '__main__': pass diff --git a/train.py b/train.py old mode 100644 new mode 100755 index 47a5524..0666a3b --- a/train.py +++ b/train.py @@ -1,28 +1,77 @@ +#from datasets.ShuffleMNIST.dataset import ShuffleMNIST import os +import numpy as np import torch.utils.data from torch.nn import DataParallel from datetime import datetime from torch.optim.lr_scheduler import MultiStepLR from config import BATCH_SIZE, PROPOSAL_NUM, SAVE_FREQ, LR, WD, resume, save_dir from core import model, dataset -from core.utils import init_log, progress_bar +from core.utils import init_log, progress_bar, create_dir, timer + +import torchvision +from torchvision.utils import save_image +from torchvision import transforms + +from ShuffleMNIST import dataset as Shuffdata + +from PIL import Image + os.environ['CUDA_VISIBLE_DEVICES'] = '0,1,2,3' start_epoch = 1 save_dir = os.path.join(save_dir, datetime.now().strftime('%Y%m%d_%H%M%S')) if os.path.exists(save_dir): raise NameError('model dir exists!') -os.makedirs(save_dir) + +#se agrego la funcion create_dir en utils +save_dir = create_dir('save_dir') logging = init_log(save_dir) _print = logging.info # read dataset -trainset = dataset.CUB(root='./CUB_200_2011', is_train=True, data_len=None) -trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, - shuffle=True, num_workers=8, drop_last=False) -testset = dataset.CUB(root='./CUB_200_2011', is_train=False, data_len=None) -testloader = torch.utils.data.DataLoader(testset, batch_size=BATCH_SIZE, - shuffle=False, num_workers=8, drop_last=False) +#trainset = dataset.CUB(root='./CUB_200_2011', is_train=True, data_len=None) +#trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, +# shuffle=True, num_workers=8, drop_last=False) +#testset = dataset.CUB(root='./CUB_200_2011', is_train=False, data_len=None) +#testloader = torch.utils.data.DataLoader(testset, batch_size=BATCH_SIZE, +# shuffle=False, num_workers=8, drop_last=False) + +#read dataset +batch_size_train = 64 +batch_size_test = 1000 + + +dataset_train = torchvision.datasets.MNIST('/home/alessio/alonso/datasets', train=True, download=True, + transform=torchvision.transforms.ToTensor()) + +dataset_test = torchvision.datasets.MNIST('/home/alessio/alonso/datasets', train=False, + download=True,transform=torchvision.transforms.ToTensor()) + +train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=batch_size_train,drop_last=True, shuffle = True) +test_loader = torch.utils.data.DataLoader(dataset_test,batch_size=batch_size_test, shuffle = True,drop_last=True) + +shuffled_train = Shuffdata.ShuffleMNIST(train_loader, anchors = [], num=4, radius = 42, wall_shape = 600, sum = True,is_train=True) +shuffled_test = Shuffdata.ShuffleMNIST(test_loader, anchors = [], num=4, radius = 42, wall_shape = 600, sum = True, is_train = False) + +print('There are {} images and {} labels in the train set.'.format(len(shuffled_train.train_img), + len(shuffled_train.train_label))) +print('There are {} images and {} labels in the test set.'.format(len(shuffled_test.test_img), + len(shuffled_test.test_label))) + +#Configuring shuffled DataLoader +from torch.utils.data.sampler import RandomSampler + +#se cambian estos nombres a train loader para que sean los que se llaman en la red +train_sampler = RandomSampler(shuffled_train, replacement=True, num_samples= 51200, generator=None) +test_sampler = RandomSampler(shuffled_test, replacement=True, num_samples= 5760, generator=None) + +trainloader = torch.utils.data.DataLoader(shuffled_train, batch_size=batch_size_train + ,drop_last=False, sampler = train_sampler) + +testloader = torch.utils.data.DataLoader(shuffled_test, batch_size=batch_size_train + ,drop_last=False, sampler = test_sampler) + # define model net = model.attention_net(topN=PROPOSAL_NUM) if resume: @@ -47,62 +96,126 @@ MultiStepLR(partcls_optimizer, milestones=[60, 100], gamma=0.1)] net = net.cuda() net = DataParallel(net) +device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') + +# Eevitar warning y que si se concidere el primer valor de la taza de aprendizaje +# lr_scheduler.MultiStepLR() -for epoch in range(start_epoch, 500): - for scheduler in schedulers: - scheduler.step() +@timer +def entrenamiento(start_epoch, _print, trainloader, testloader, net, creterion, raw_optimizer, concat_optimizer, part_optimizer, partcls_optimizer, schedulers, device): + for epoch in range(start_epoch, 500): + for scheduler in schedulers: + scheduler.step() # begin training - _print('--' * 50) - net.train() - for i, data in enumerate(trainloader): - img, label = data[0].cuda(), data[1].cuda() - batch_size = img.size(0) - raw_optimizer.zero_grad() - part_optimizer.zero_grad() - concat_optimizer.zero_grad() - partcls_optimizer.zero_grad() - - raw_logits, concat_logits, part_logits, _, top_n_prob = net(img) - part_loss = model.list_loss(part_logits.view(batch_size * PROPOSAL_NUM, -1), + _print('--' * 50) + net.train() + for batch_idx, (data_, target_) in enumerate(trainloader): + #cambiamos sintaxis de esta parte + img, label = data_, target_.to(device) + batch_size = img.size(0) + raw_optimizer.zero_grad() + part_optimizer.zero_grad() + concat_optimizer.zero_grad() + partcls_optimizer.zero_grad() + + + #transformacion de los datos + ##if len(img.shape) == 3: + ## img = np.stack([img] * 3, 2) + + ##img = np.transpose(img, (0,1,2,3)) + #img = Image.fromarray(img, mode='RGB') + #img = Image.fromarray(img) + #img = transforms.Resize((600, 600), Image.BILINEAR)(img) + ##img = torch.as_tensor(img) + #img = transforms.ToTensor()(img) + ##data_ = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(img) + + + if len(img.shape) == 2: + img = np.stack([img] * 3, 2) + img = img.numpy() + img = Image.fromarray(img, mode='RGB') + img = transforms.Resize((600, 600), Image.BILINEAR)(img) + #img = transforms.RandomCrop(INPUT_SIZE)(img) + img = transforms.RandomHorizontalFlip()(img) + img = transforms.ToTensor()(img) + data_ = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(img) + + + + img = data_.to(device) + + raw_logits, concat_logits, part_logits, _, top_n_prob = net(img) + part_loss = model.list_loss(part_logits.view(batch_size * PROPOSAL_NUM, -1), label.unsqueeze(1).repeat(1, PROPOSAL_NUM).view(-1)).view(batch_size, PROPOSAL_NUM) - raw_loss = creterion(raw_logits, label) - concat_loss = creterion(concat_logits, label) - rank_loss = model.ranking_loss(top_n_prob, part_loss) - partcls_loss = creterion(part_logits.view(batch_size * PROPOSAL_NUM, -1), + raw_loss = creterion(raw_logits, label) + concat_loss = creterion(concat_logits, label) + rank_loss = model.ranking_loss(top_n_prob, part_loss) + partcls_loss = creterion(part_logits.view(batch_size * PROPOSAL_NUM, -1), label.unsqueeze(1).repeat(1, PROPOSAL_NUM).view(-1)) - total_loss = raw_loss + rank_loss + concat_loss + partcls_loss - total_loss.backward() - raw_optimizer.step() - part_optimizer.step() - concat_optimizer.step() - partcls_optimizer.step() - progress_bar(i, len(trainloader), 'train') - - if epoch % SAVE_FREQ == 0: - train_loss = 0 - train_correct = 0 - total = 0 - net.eval() - for i, data in enumerate(trainloader): - with torch.no_grad(): - img, label = data[0].cuda(), data[1].cuda() - batch_size = img.size(0) - _, concat_logits, _, _, _ = net(img) + total_loss = raw_loss + rank_loss + concat_loss + partcls_loss + total_loss.backward() + raw_optimizer.step() + part_optimizer.step() + concat_optimizer.step() + partcls_optimizer.step() + progress_bar(i, len(trainloader), 'train') + + if epoch % SAVE_FREQ == 0: + train_loss = 0 + train_correct = 0 + total = 0 + net.eval() + for batch_idx_t, (data_t, target_t) in enumerate(trainloader): + with torch.no_grad(): + img, label = data_t, target_t.to(device) + + #transformacion de los datos + img = np.array(data_t) + #print(img.shape) + ##if len(img.shape) == 3: + ## img = np.stack([img] * 3, 2) + + ##img = np.transpose(img, (0,2,1,3)) + #img = Image.fromarray(img, mode='RGB') + #img = Image.fromarray(img) + #img = transforms.Resize((600, 600), Image.BILINEAR)(img) + ##img = torch.as_tensor(img) + #img = transforms.ToTensor()(img) + #data_t = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(img) + ##data_t = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(img) + + if len(img.shape) == 2: + img = np.stack([img] * 3, 2) + img = img.numpy() + img = Image.fromarray(img, mode='RGB') + img = transforms.Resize((600, 600), Image.BILINEAR)(img) + #img = transforms.RandomCrop(INPUT_SIZE)(img) + img = transforms.RandomHorizontalFlip()(img) + img = transforms.ToTensor()(img) + data_t = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(img) + + + img = data_t.to(device) + + batch_size = img.size(0) + _, concat_logits, _, _, _ = net(img) # calculate loss - concat_loss = creterion(concat_logits, label) + concat_loss = creterion(concat_logits, label) # calculate accuracy - _, concat_predict = torch.max(concat_logits, 1) - total += batch_size - train_correct += torch.sum(concat_predict.data == label.data) - train_loss += concat_loss.item() * batch_size - progress_bar(i, len(trainloader), 'eval train set') + _, concat_predict = torch.max(concat_logits, 1) + total += batch_size + train_correct += torch.sum(concat_predict.data == label.data) + train_loss += concat_loss.item() * batch_size + progress_bar(i, len(trainloader), 'eval train set') - train_acc = float(train_correct) / total - train_loss = train_loss / total + train_acc = float(train_correct) / total + train_loss = train_loss / total - _print( + _print( 'epoch:{} - train loss: {:.3f} and train acc: {:.3f} total sample: {}'.format( epoch, train_loss, @@ -110,26 +223,26 @@ total)) # evaluate on test set - test_loss = 0 - test_correct = 0 - total = 0 - for i, data in enumerate(testloader): - with torch.no_grad(): - img, label = data[0].cuda(), data[1].cuda() - batch_size = img.size(0) - _, concat_logits, _, _, _ = net(img) + test_loss = 0 + test_correct = 0 + total = 0 + for i, data in enumerate(testloader): + with torch.no_grad(): + img, label = data[0].cuda(), data[1].cuda() + batch_size = img.size(0) + _, concat_logits, _, _, _ = net(img) # calculate loss - concat_loss = creterion(concat_logits, label) + concat_loss = creterion(concat_logits, label) # calculate accuracy - _, concat_predict = torch.max(concat_logits, 1) - total += batch_size - test_correct += torch.sum(concat_predict.data == label.data) - test_loss += concat_loss.item() * batch_size - progress_bar(i, len(testloader), 'eval test set') - - test_acc = float(test_correct) / total - test_loss = test_loss / total - _print( + _, concat_predict = torch.max(concat_logits, 1) + total += batch_size + test_correct += torch.sum(concat_predict.data == label.data) + test_loss += concat_loss.item() * batch_size + progress_bar(i, len(testloader), 'eval test set') + + test_acc = float(test_correct) / total + test_loss = test_loss / total + _print( 'epoch:{} - test loss: {:.3f} and test acc: {:.3f} total sample: {}'.format( epoch, test_loss, @@ -137,10 +250,10 @@ total)) # save model - net_state_dict = net.module.state_dict() - if not os.path.exists(save_dir): - os.mkdir(save_dir) - torch.save({ + net_state_dict = net.module.state_dict() + if not os.path.exists(save_dir): + os.mkdir(save_dir) + torch.save({ 'epoch': epoch, 'train_loss': train_loss, 'train_acc': train_acc, @@ -149,4 +262,6 @@ 'net_state_dict': net_state_dict}, os.path.join(save_dir, '%03d.ckpt' % epoch)) +entrenamiento(start_epoch, _print, trainloader, testloader, net, creterion, raw_optimizer, concat_optimizer, part_optimizer, partcls_optimizer, schedulers, device) + print('finishing training')