Example #1
0
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(),
                      lr=args.lr,
                      momentum=0.9,
                      weight_decay=5e-4)
steps_lr = [50, 75, 90]
for batch_idx, (inputs, targets) in enumerate(trainloader):
    if use_cuda:
        inputs, targets = inputs.cuda(), targets.cuda()
    inputs, targets = Variable(inputs), Variable(targets)
    break
net = LSUVinit(net,
               inputs,
               needed_std=1.0,
               std_tol=0.1,
               max_attempts=10,
               do_orthonorm=True,
               cuda=use_cuda)
print(net)


# Training
def train(epoch):
    print('\nEpoch: %d' % epoch)
    net.train()
    train_loss = 0
    correct = 0
    total = 0
    for batch_idx, (inputs, targets) in enumerate(trainloader):
        if use_cuda:
        except:
            pass
    return


from LSUV import LSUVinit
for batch_idx, (inputs, targets) in enumerate(trainloader):
    if use_cuda:
        inputs, targets = inputs.cuda(), targets.cuda()
    inputs, targets = Variable(inputs), Variable(targets)
    break

net = LSUVinit(net,
               inputs,
               needed_std=1.0,
               std_tol=0.1,
               max_attempts=10,
               do_orthonorm=False,
               gamma=True,
               cuda=use_cuda)
net.train()
optimizer1 = optim.SGD(net.parameters(), lr=0.01, momentum=0.9, dampening=0.9)
for i in range(0):
    optimizer1.zero_grad()
    loss = gor_filter_loss(net)
    if i % 100 == 0:
        print(loss)
    loss.backward()
    optimizer1.step()

for epoch in range(0, 100):
    train(epoch)
Example #3
0
import torch
import torchvision.models as models
import numpy as np
from LSUV import LSUVinit
import sys
import os
sys.path.insert(0, '/home/ubuntu/dev/opencv-3.1/build/lib')
import cv2
from torch.autograd import Variable
images_to_process = []
for img_fname in os.listdir('imgs'):
    img = cv2.imread('imgs/' + img_fname)
    print(img.shape)
    if img is not None:
        images_to_process.append(
            np.transpose(cv2.resize(img, (224, 224)), (2, 0, 1)))

data = np.array(images_to_process).astype(np.float32)
data = torch.from_numpy(data)
alexnet = models.densenet121(pretrained=False)
alexnet = LSUVinit(alexnet,
                   data,
                   needed_std=1.0,
                   std_tol=0.1,
                   max_attempts=10,
                   needed_mean=0.,
                   do_orthonorm=False)
Example #4
0
                if q == 0:
                    subepoch = epoch % subepochs
                    epoch = epoch // subepochs

                Or = subepoch % 4

                #MNIST
                #H = torch.repeat_interleave(H, 3, dim=1)

                I = I.float()

                DS = nn.AdaptiveAvgPool2d((256, 256))
                I = DS(I).to(device) / 256
                if epoch == 0 and q == 0 and load == False:
                    print("LSUVINIT")
                    model = LSUVinit(model, I, cuda=True)

                #I = torchvision.transforms.ToTensor()(torchvision.transforms.functional.rotate(torchvision.transforms.ToPILImage()(I), Or*90))
                #I = I.unsqueeze(0).to(device)
                #pdb.set_trace()
                if mem:
                    a = torch.cuda.memory_allocated(device=device)
                    print("Data on Device, PreForward", a / 1e9)

                # zero the parameter gradients

                # forward + backward + optimize
                model.iter += 1
                pred = model.forward(I)
                #pdb.set_trace()
                if mem:
Example #5
0
		torchvision.transforms.ToTensor()
		])
	dataset = torchvision.datasets.ImageFolder("imagenet-a", transforms = transforms)
	loader = torch.utils.DataLoader(dataset, batch_size=32, shuffle=True, sampler=None,
		batch_sampler=None, num_workers=4, collate_fn=None,
		pin_memory=True, drop_last=False, timeout=0,
		worker_init_fn=None)

	# Start Train Loop
	for epoch in range(rf, epochs):
		for q, (data, label) in enumerate(trainloader, 0):
			I, _ = data
			I = I.float().to(device)

			if epoch == 0 and q == 0 and load == False and model.init == False:
				model = LSUVinit(model, I, std_tol= 1e-3, cuda=True)
				model.init = True

			# forward + backward + optimize
			model.iter += 1
			pred = model.forward(I)

			loss = (hl + mse + kl + adS + bowl).mean()
			loss.backward()

			print("Epoch", epoch, "batch", q, "loss", np.round(loss.item(),4), "kl", np.round(kl.mean().item(),4), "mse", np.round(mse.mean().item(),4), "img means", "({}, {})".format(np.round(pred.mean().item(),3), np.round(I.mean().item(),3)))

			if model.iter%100 == 0:
				losses.append([loss.item()])

			optimizer.step()
Example #6
0
                DS = nn.AdaptiveAvgPool2d((256, 256))
                I = DS(I) / 256
                #A, B, C, D, = I[:,:,:128,:128], I[:,:,:128,128:], I[:,:,128:,:128], I[:,:,128:,128:]
                I = torch.cat((I[:, :, :128, :128], I[:, :, :128, 128:],
                               I[:, :, 128:, :128], I[:, :, 128:, 128:]),
                              dim=0)  #.to(device)
                I = torch.cat((I[:, :, :64, :64], I[:, :, :64, 64:],
                               I[:, :, 64:, :64], I[:, :, 64:, 64:]),
                              dim=0).to(device)
                if epoch == 0 and q == 0 and load == False and init == False:
                    print("LSUVINIT")
                    model = LSUVinit(model,
                                     I,
                                     needed_std=I.reshape(
                                         I.size(0), I.size(1),
                                         -1).std(dim=-1).mean().item(),
                                     std_tol=1e-4,
                                     max_attempts=50,
                                     cuda=True)
                    init = True

                #I = torchvision.transforms.ToTensor()(torchvision.transforms.functional.rotate(torchvision.transforms.ToPILImage()(I), Or*90))
                #I = I.unsqueeze(0).to(device)
                #pdb.set_trace()
                if mem:
                    a = torch.cuda.memory_allocated(device=device)
                    print("Data on Device, PreForward", a / 1e9)

                # zero the parameter gradients

                # forward + backward + optimize