Exemple #1
0
from torchvision import datasets
from torchvision import transforms
import torch
import torch.nn as nn
from  torch.utils.data import DataLoader as dt
from torch.optim.lr_scheduler import CosineAnnealingLR
from torch.optim.lr_scheduler import LambdaLR
#define
lr=0.001
batchsize=16
warmup=5
max_epoch=50
device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
root1=r'D:/学习相关/大三下/cv/fd/mouth/train'
root2=r'D:/学习相关/大三下/cv/fd/mouth/test'
transform=transforms.Compose([transforms.Grayscale(),transforms.Resize(40),transforms.RandomCrop(30),transforms.ToTensor(),transforms.Normalize([0.5],[0.5])])
trainset=datasets.ImageFolder(root1,transform=transform)
testset=datasets.ImageFolder(root2,transform=transform)
train_loader=dt(dataset=trainset,batch_size=batchsize,shuffle=True)
test_loader=dt(dataset=testset,batch_size=batchsize,shuffle=False)

class My_ResidualBlock1(nn.Module):
    def __init__(self,channel):
        super().__init__()
        self.c1=nn.Conv2d(channel,channel,kernel_size=3,padding=1)
        self.c2=nn.Conv2d(channel, channel,kernel_size=3, padding=1)
    def forward(self,x):
        x=nn.functional.relu(self.c1(x))
        y=self.c2(x)
        return nn.functional.relu(x+y)
class My_CNN_withRB_mouth(nn.Module):
def load_data(opt):
    """ Load Data

    Args:
        opt ([type]): Argument Parser

    Raises:
        IOError: Cannot Load Dataset

    Returns:
        [type]: dataloader
    """

    ##
    # LOAD DATA SET
    if opt.dataroot == '':
        opt.dataroot = './data/{}'.format(opt.dataset)
    print(opt.dataset)
    ## CIFAR
    if opt.dataset in ['cifar10']:
        transform = transforms.Compose([
            transforms.Resize(opt.isize),
            transforms.ToTensor(),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        ])

        train_ds = CIFAR10(root='./data',
                           train=True,
                           download=True,
                           transform=transform)
        valid_ds = CIFAR10(root='./data',
                           train=False,
                           download=True,
                           transform=transform)
        train_ds, valid_ds = get_cifar_anomaly_dataset(
            train_ds, valid_ds, train_ds.class_to_idx[opt.abnormal_class])

    ## MNIST
    elif opt.dataset in ['mnist']:
        transform = transforms.Compose([
            transforms.Resize(opt.isize),
            transforms.ToTensor(),
            transforms.Normalize((0.1307, ), (0.3081, ))
        ])

        train_ds = MNIST(root='./data',
                         train=True,
                         download=True,
                         transform=transform)
        valid_ds = MNIST(root='./data',
                         train=False,
                         download=True,
                         transform=transform)
        train_ds, valid_ds = get_mnist_anomaly_dataset(train_ds, valid_ds,
                                                       int(opt.abnormal_class))

    # FOLDER
    elif opt.dataset in ['csot']:
        transform = transforms.Compose([
            transforms.Resize(opt.isize),
            transforms.CenterCrop(opt.isize),
            transforms.ToTensor(),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        ])
        train_ds = ImageFolder(os.path.join(opt.dataroot, 'train'), transform)
        valid_ds = ImageFolder(os.path.join(opt.dataroot, 'test'), transform)

    elif opt.dataset in ['yunfangbu']:
        transform = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        ])
        train_ds = ImageFolder(os.path.join(opt.dataroot, 'train'), transform)
        valid_ds = ImageFolder(os.path.join(opt.dataroot, 'test'), transform)

    elif opt.dataset in ['anomaly1']:
        transform = transforms.Compose([
            transforms.Grayscale(num_output_channels=1),
            transforms.Resize(opt.isize),
            transforms.CenterCrop(opt.isize),
            transforms.ToTensor(),
            transforms.Normalize((0.5, ), (0.5, ))
        ])
        train_ds = ImageFolder(os.path.join(opt.dataroot, 'train'), transform)
        valid_ds = ImageFolder(os.path.join(opt.dataroot, 'test'), transform)

    elif opt.dataset in ['anomaly10']:
        transform = transforms.Compose([
            transforms.Grayscale(num_output_channels=1),
            transforms.Resize(opt.isize),
            transforms.ToTensor(),
            transforms.Normalize((0.5, ), (0.5, ))
        ])
        train_ds = ImageFolder(os.path.join(opt.dataroot, 'train'), transform)
        valid_ds = ImageFolder(os.path.join(opt.dataroot, 'test'), transform)

    else:
        transform = transforms.Compose([
            transforms.Resize(opt.isize),
            transforms.CenterCrop(opt.isize),
            transforms.ToTensor(),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        ])
        if opt.nc == 1:
            print('Input is grayscale image')
            transform = transforms.Compose([
                transforms.Grayscale(num_output_channels=1),
                transforms.Resize(opt.isize),
                transforms.CenterCrop(opt.isize),
                transforms.ToTensor(),
                transforms.Normalize((0.5, ), (0.5, ))
            ])

        train_ds = ImageFolder(os.path.join(opt.dataroot, 'train'), transform)
        valid_ds = ImageFolder(os.path.join(opt.dataroot, 'test'), transform)

    ## DATALOADER
    train_dl = DataLoader(dataset=train_ds,
                          batch_size=opt.batchsize,
                          shuffle=True,
                          drop_last=True)
    valid_dl = DataLoader(dataset=valid_ds,
                          batch_size=opt.batchsize,
                          shuffle=False,
                          drop_last=False)

    return Data(train_dl, valid_dl)
Exemple #3
0
def get_transform(opt,
                  params=None,
                  grayscale=False,
                  method=Image.BICUBIC,
                  convert=True):
    transform_list = []
    if grayscale:
        transform_list.append(transforms.Grayscale(1))
    if 'resize' in opt.preprocess:
        osize = [opt.load_size, opt.load_size]
        transform_list.append(transforms.Resize(osize, method))
    elif 'scale_width' in opt.preprocess:
        transform_list.append(
            transforms.Lambda(
                lambda img: __scale_width(img, opt.load_size, method)))

    if 'rotate' in opt.preprocess:
        transform_list.append(transforms.RandomRotation(opt.rot_degree))

    if 'crop' in opt.preprocess:
        if 'center_crop' in opt.preprocess:
            transform_list.append(transforms.CenterCrop(opt.crop_size))
        elif params is None:
            transform_list.append(transforms.RandomCrop(opt.crop_size))
        else:
            transform_list.append(
                transforms.Lambda(lambda img: __crop(img, params['crop_pos'],
                                                     opt.crop_size)))

    if opt.preprocess == 'none':
        transform_list.append(
            transforms.Lambda(
                lambda img: __make_power_2(img, base=4, method=method)))

    if not opt.no_flip:
        if params is None:
            transform_list.append(transforms.RandomHorizontalFlip())
        elif params['flip']:
            transform_list.append(
                transforms.Lambda(lambda img: __flip(img, params['flip'])))

    if 'color_jitter' in opt.preprocess:
        # transform_list.append(transforms.ColorJitter( #TODO: parametarize
        #             brightness=0.25*5,  #args.jitter_brightness,
        #             contrast=0.4*5, #args.jitter_contrast,
        #             saturation=0.2*5, #args.jitter_saturation,
        #             hue=0.05*5 #args.jitter_hue
        #         ))
        transform_list.append(
            transforms.ColorJitter(  #TODO: parametarize
                brightness=0.25,  #args.jitter_brightness,
                contrast=0.4,  #args.jitter_contrast,
                saturation=0.2,  #args.jitter_saturation,
                hue=0.05  #args.jitter_hue
            ))

    transform_list.append(transforms.ToTensor())
    transform_tensor = [transforms.ToPILImage()] + transform_list
    if convert:
        transform_list += [transforms.Normalize((0.5, ), (0.5, ))]

    #cut or erase after normalization
    if 'cutout' in opt.preprocess:
        transform_list.append(Cutout(n_holes=1, length=8))  #TODO: parametarize
        transform_tensor.append(Cutout(n_holes=1,
                                       length=8))  #TODO: parametarize

    if 'erasing' in opt.preprocess:
        transform_list.append(
            RandomErasing(p=0.5, sl=0.02, sh=0.3, r1=0.3,
                          r2=1 / 0.3))  #TODO: parametarize
        transform_tensor.append(
            RandomErasing(p=0.5, sl=0.02, sh=0.3, r1=0.3,
                          r2=1 / 0.3))  #TODO: parametarize

    try:
        if opt.phase == "train" and "multi_transforms" in opt.dataset_mode:
            print("return multi transforms, phase: ", opt.phase)
            non_transform = []
            if grayscale:
                non_transform.append(transforms.Grayscale(1))
            non_transform.extend([
                transforms.Lambda(
                    lambda img: __make_power_2(img, base=4, method=method)),
                transforms.Resize(
                    opt.crop_size, method
                ),  #TODO: adjust size to crop size because it's same if crop is specified
                transforms.ToTensor(),
                transforms.Normalize((0.5, ), (0.5, ))
            ])

            return transforms.Compose(non_transform), transforms.Compose(
                transform_list), transforms.Compose(transform_tensor)
    except:
        pass

    return transforms.Compose(transform_list)
Exemple #4
0
        part =  np.asarray(np_probs[i * (N // splits): (i+1) * (N // splits)], dtype=np.float32)
        kl = part * (np.log(part) - np.log(np.expand_dims(np.mean(part, 0), 0)))
        kl = np.mean(np.sum(kl, 1))
        scores.append(np.exp(kl))
    return np.mean(scores), np.std(scores)

    return np.mean(split_scores), np.std(split_scores)


# Make dataloader for the to-be-classified images
# Get the names of the images
filenames = os.listdir(test_path)

# Transform the images
transform = tf.Compose([
    tf.Grayscale(),
    tf.Resize([224, 224]),
    tf.ToTensor()
])

test_data = torchvision.datasets.ImageFolder(test_path, transform=transform)
test_loader = DataLoader(dataset=test_data, num_workers=threads, batch_size=1, shuffle=False)


# Setup the loader index, hardcoded since we only use the pretrained model
if task == 'initials':
    model = '/model_snapshots/initials.pt'
    class_dictionary = ['M', 'Q', 'C', 'N', 'P', 'O', 'D', 'T', 'H', 'S', 'V', 'B', 'A', 'W', 'I', 'E', 'L', 'F', 'R', 'Z', 'G', 'Y', 'X', 'K', '']
    class_count = 25
    loader_index = 1
elif task == 'countries':
class mnist_three_component(nn.Module):

    NOISE_RGB_AMOUNT = 15  # 15
    BLUR = 2  # 2
    DIM = 64
    F_DIM = 64  # 64

    _transformations = Group([
        tf.Grayscale(num_output_channels=3),
        tf.Resize((DIM, DIM)),
        tf.ToTensor(),
        #  mytransforms.NormalizeGray
    ])

    _train = Group([
        tf.Grayscale(num_output_channels=1),
        #        mytransforms.Add1DNoise(config.BORDER_COLOR_GRAY, NOISE_RGB_AMOUNT),
        #        mytransforms.Gaussian_Blur(BLUR),
    ])

    _test = []

    transformations = {
        'train': tf.Compose(_train + _transformations),
        'test': tf.Compose(_test + _transformations)
    }

    def __init__(self):
        F_DIM = self.F_DIM
        super().__init__()
        self.feats = nn.Sequential(
            nn.Conv2d(3, 32, 3, 1, 1),
            nn.MaxPool2d(2, 2),
            nn.ReLU(True),
            nn.BatchNorm2d(32),
            nn.Conv2d(32, 64, 3, 1, 1),
            nn.ReLU(True),
            nn.BatchNorm2d(64),
            nn.Conv2d(64, 64, 5, 1, 1),
            nn.MaxPool2d(2, 2),
            nn.ReLU(True),
            nn.BatchNorm2d(64),
            nn.Conv2d(64, 64, 5, 1, 1),
            nn.ReLU(True),
            nn.BatchNorm2d(64),
        )

        self.classifier = nn.Sequential(nn.Linear(32448, F_DIM, bias=False),
                                        nn.BatchNorm1d(F_DIM),
                                        nn.ReLU(inplace=True), nn.Dropout(0.5),
                                        nn.Linear(F_DIM, F_DIM, bias=False),
                                        nn.BatchNorm1d(F_DIM),
                                        nn.ReLU(inplace=True), nn.Dropout(0.5),
                                        nn.Linear(F_DIM, 2))

    def forward(self, components):
        n, z, e = components
        nout, zout, eout = self.feats(n), self.feats(z), self.feats(e)
        out = torch.cat((nout, zout, eout), 1)
        #print(out)
        out = out.view(out.size(0), -1)
        out = self.classifier(out)
        return out
    )
    parser.add_argument(
        "--save-file",
        default="data\prepared_data.pkl",
        help="File path with data to check",
    )
    parser.add_argument(
        "--show-example",
        default=False,
        help="Either to show an example of the transformation or not",
    )
    args = parser.parse_args()

    data = read_data(args.file)
    states, actions = map(np.array, zip(*data))
    dataset = CarV0GymDataset(args.file, classification=True)
    if args.show_example:
        selected_state = states[int(len(states) / 2)]
        selected_state = transforms.ToPILImage()(selected_state)
        selected_state.show(title="Original Image")
        selected_state = transforms.Grayscale(1)(selected_state)
        selected_state.show(title="Grayscale")
        selected_state = transforms.Pad((12, 12, 12, 0))(selected_state)
        selected_state.show(title="Padding")
        selected_state = transforms.CenterCrop(84)(selected_state)
        selected_state.show(title="CenterCrop")
        selected_state = transforms.ToTensor()(selected_state)
        selected_state = transforms.Normalize((0,), (1,))(selected_state)
        selected_state = transforms.ToPILImage()(selected_state)
        selected_state.show(title="Final Normalized")
def main(args):
    ### config
    global noise_multiplier
    dataset = args.dataset
    num_discriminators = args.num_discriminators
    noise_multiplier = args.noise_multiplier
    z_dim = args.z_dim
    model_dim = args.model_dim
    batchsize = args.batchsize
    L_gp = args.L_gp
    L_epsilon = args.L_epsilon
    critic_iters = args.critic_iters
    latent_type = args.latent_type
    load_dir = args.load_dir
    save_dir = args.save_dir
    if_dp = (args.dp > 0.)
    gen_arch = args.gen_arch
    num_gpus = args.num_gpus

    ### CUDA
    use_cuda = torch.cuda.is_available()
    devices = [torch.device("cuda:%d" % i if use_cuda else "cpu") for i in range(num_gpus)]
    device0 = devices[0]
    if use_cuda:
        torch.set_default_tensor_type('torch.cuda.FloatTensor')

    ### Random seed
    random.seed(args.random_seed)
    np.random.seed(args.random_seed)
    torch.manual_seed(args.random_seed)

    ### Fix noise for visualization
    if latent_type == 'normal':
        fix_noise = torch.randn(10, z_dim)
    elif latent_type == 'bernoulli':
        p = 0.5
        bernoulli = torch.distributions.Bernoulli(torch.tensor([p]))
        fix_noise = bernoulli.sample((10, z_dim)).view(10, z_dim)
    else:
        raise NotImplementedError

    ### Set up models
    print('gen_arch:' + gen_arch)
    netG = GeneratorDCGAN(z_dim=z_dim, model_dim=model_dim, num_classes=10)

    netGS = copy.deepcopy(netG)
    netD_list = []
    for i in range(num_discriminators):
        netD = DiscriminatorDCGAN()
        netD_list.append(netD)

    ### Load pre-trained discriminators
    print("load pre-training...")
    if load_dir is not None:
        for netD_id in range(num_discriminators):
            print('Load NetD ', str(netD_id))
            network_path = os.path.join(load_dir, 'netD_%d' % netD_id, 'netD.pth')
            netD = netD_list[netD_id]
            netD.load_state_dict(torch.load(network_path))

    netG = netG.to(device0)
    for netD_id, netD in enumerate(netD_list):
        device = devices[get_device_id(netD_id, num_discriminators, num_gpus)]
        netD.to(device)

    ### Set up optimizers
    optimizerD_list = []
    for i in range(num_discriminators):
        netD = netD_list[i]
        optimizerD = optim.Adam(netD.parameters(), lr=1e-4, betas=(0.5, 0.99))
        optimizerD_list.append(optimizerD)
    optimizerG = optim.Adam(netG.parameters(), lr=1e-4, betas=(0.5, 0.99))

    ### Data loaders
    if dataset == 'mnist' or dataset == 'fashionmnist':
        transform_train = transforms.Compose([
        transforms.CenterCrop((28, 28)),
        transforms.ToTensor(),
        #transforms.Grayscale(),
        ])
    elif dataset == 'cifar_100' or dataset == 'cifar_10':
        transform_train = transforms.Compose([
        transforms.CenterCrop((28, 28)),
        transforms.Grayscale(),
        transforms.ToTensor(),
        ])

    if dataset == 'mnist':
        dataloader = datasets.MNIST
        trainset = dataloader(root=os.path.join(DATA_ROOT, 'MNIST'), train=True, download=True,
                              transform=transform_train)
        IMG_DIM = 784
        NUM_CLASSES = 10
    elif dataset == 'fashionmnist':
        dataloader = datasets.FashionMNIST
        trainset = dataloader(root=os.path.join(DATA_ROOT, 'FashionMNIST'), train=True, download=True,
                              transform=transform_train)
    elif dataset == 'cifar_100':
        dataloader = datasets.CIFAR100
        trainset = dataloader(root=os.path.join(DATA_ROOT, 'CIFAR100'), train=True, download=True,
                              transform=transform_train)
        IMG_DIM = 3072
        NUM_CLASSES = 100
    elif dataset == 'cifar_10':
        IMG_DIM = 784
        NUM_CLASSES = 10
        dataloader = datasets.CIFAR10
        trainset = dataloader(root=os.path.join(DATA_ROOT, 'CIFAR10'), train=True, download=True,
                              transform=transform_train)
    else:
        raise NotImplementedError

    print('creat indices file')
    indices_full = np.arange(len(trainset))
    np.random.shuffle(indices_full)
    #indices_full.dump(os.path.join(save_dir, 'indices.npy'))
    trainset_size = int(len(trainset) / num_discriminators)
    print('Size of the dataset: ', trainset_size)

    input_pipelines = []
    for i in range(num_discriminators):
        start = i * trainset_size
        end = (i + 1) * trainset_size
        indices = indices_full[start:end]
        trainloader = DataLoader(trainset, batch_size=args.batchsize, drop_last=False,
                                      num_workers=args.num_workers, sampler=SubsetRandomSampler(indices))
        #input_data = inf_train_gen(trainloader)
        input_pipelines.append(trainloader)
    
    ### not add noise by hook which is middle between G/D
    '''
    if if_dp:
    ### Register hook
    
        global dynamic_hook_function
        for netD in netD_list:
            netD.conv1.register_backward_hook(master_hook_adder)
    '''


    prg_bar = tqdm(range(args.iterations+1))
    for iters in prg_bar:
        #########################
        ### Update D network
        #########################
        netD_id = np.random.randint(num_discriminators, size=1)[0]
        device = devices[get_device_id(netD_id, num_discriminators, num_gpus)]
        netD = netD_list[netD_id]
        optimizerD = optimizerD_list[netD_id]
        input_data = input_pipelines[netD_id]



        for p in netD.parameters():
            p.requires_grad = True

        ### Register hook for add noise to D
        if if_dp:
            for parameter in netD.parameters():
                if parameter.requires_grad:
                    parameter.register_hook(
                        lambda grad: grad + (1 / batchsize) * noise_multiplier * torch.randn(grad.shape) * SENSITIVITY
                        #lambda grad: grad
                    )

        for iter_d in range(critic_iters):
            real_data, real_y = next(iter(input_data))
            real_data = real_data.view(-1, IMG_DIM)
            real_data = real_data.to(device)
            real_y = real_y.to(device)
            real_data_v = autograd.Variable(real_data)

            ### train with real
            dynamic_hook_function = dummy_hook
            netD.zero_grad()
            D_real_score = netD(real_data_v, real_y)
            D_real = -D_real_score.mean()

            ### train with fake
            batchsize = real_data.shape[0]
            if latent_type == 'normal':
                noise = torch.randn(batchsize, z_dim).to(device0)
            elif latent_type == 'bernoulli':
                noise = bernoulli.sample((batchsize, z_dim)).view(batchsize, z_dim).to(device0)
            else:
                raise NotImplementedError
            noisev = autograd.Variable(noise)
            fake = autograd.Variable(netG(noisev, real_y.to(device0)).data)
            inputv = fake.to(device)
            D_fake = netD(inputv, real_y.to(device))
            D_fake = D_fake.mean()

            ### train with gradient penalty
            gradient_penalty = netD.calc_gradient_penalty(real_data_v.data, fake.data, real_y, L_gp, device)
            D_cost = D_fake + D_real + gradient_penalty

            ### train with epsilon penalty
            logit_cost = L_epsilon * torch.pow(D_real_score, 2).mean()
            D_cost += logit_cost

            ### update
            D_cost.backward()
            Wasserstein_D = -D_real - D_fake
            optimizerD.step()

        del real_data, real_y, fake, noise, inputv, D_real, D_fake, logit_cost, gradient_penalty
        torch.cuda.empty_cache()

        ############################
        # Update G network
        ###########################
        if if_dp:
            ### Sanitize the gradients passed to the Generator
            dynamic_hook_function = dp_conv_hook
        else:
            ### Only modify the gradient norm, without adding noise
            dynamic_hook_function = modify_gradnorm_conv_hook

        for p in netD.parameters():
            p.requires_grad = False
        netG.zero_grad()

        ### train with sanitized discriminator output
        if latent_type == 'normal':
            noise = torch.randn(batchsize, z_dim).to(device0)
        elif latent_type == 'bernoulli':
            noise = bernoulli.sample((batchsize, z_dim)).view(batchsize, z_dim).to(device0)
        else:
            raise NotImplementedError
        label = torch.randint(0, NUM_CLASSES, [batchsize]).to(device0)
        noisev = autograd.Variable(noise)
        fake = netG(noisev, label)
        #summary(netG, input_data=[noisev,label])
        fake = fake.to(device)
        label = label.to(device)
        G = netD(fake, label)
        G = - G.mean()

        ### update
        G.backward()
        G_cost = G
        optimizerG.step()

        ### update the exponential moving average
        exp_mov_avg(netGS, netG, alpha=0.999, global_step=iters)

        ############################
        ### Results visualization
        ############################
        prg_bar.set_description('iter:{}, G_cost:{:.2f}, D_cost:{:.2f}, Wasserstein:{:.2f}'.format(iters, G_cost.cpu().data,
                                                                D_cost.cpu().data,
                                                                Wasserstein_D.cpu().data
                                                                ))
        if iters % args.vis_step == 0:
            if dataset == 'mnist':
                generate_image_mnist(iters, netGS, fix_noise, save_dir, device0)
            elif dataset == 'cifar_100':
                generate_image_cifar100(iters, netGS, fix_noise, save_dir, device0)
            elif dataset == 'cifar_10':
                generate_image_mnist(iters, netGS, fix_noise, save_dir, device0)

        if iters % args.save_step == 0:
            ### save model
            torch.save(netGS.state_dict(), os.path.join(save_dir, 'netGS_%d.pth' % iters))

        del label, fake, noisev, noise, G, G_cost, D_cost
        torch.cuda.empty_cache()

        if ((iters+1) % 500 == 0):
            classify_training(netGS, dataset, iters+1)

    ### save model
    torch.save(netG, os.path.join(save_dir, 'netG.pth'))
    torch.save(netGS, os.path.join(save_dir, 'netGS.pth'))
            self.classes.index(x.split("/")[-2]) for x in self.files
        ]

    def __len__(self):
        return len(self.files)

    def __getitem__(self, idx):
        im = Image.open(self.files[idx])
        if self.transform is not None:
            im = self.transform(im)
        label = self.labels[idx]

        return im, label


if __name__ == "__main__":

    pv_transforms = transforms.Compose([
        transforms.Grayscale(1),
        transforms.Resize((64, 64)),
        transforms.ToTensor()
    ])
    dataset = PlantVillageDataset("data/mini-plantvillage",
                                  transform=pv_transforms)
    img, label = dataset[0]
    data_loader = torch.utils.data.DataLoader(dataset,
                                              batch_size=100,
                                              shuffle=True)

    print(len(data_loader))
def create_datasets(data_path, input_size, rgb=False):
    """
    This function creates and returns a training data loader and a
    testing/validation data loader. The dataloader should also perform some
    pre-processing steps on each of the datasets. Most of this function is
    implemented for you, you will only need to add a few additional lines.
    A data loader in pyTorch is a class inherited from the
    torch.utils.data.Dataset abstract class. In this project we will use the
    ImageFolder data loader. See
    http://pytorch.org/docs/master/torchvision/datasets.html#imagefolder for
    details. Although you don't need to for this project, you can also create your
    own custom data loader by inheriting from the abstract class and implementing
    the __len__() and __getitem__() methods as described in
    http://pytorch.org/tutorials/beginner/data_loading_tutorial.html
    As mentioned, the data loader should perform any necessary pre-processing
    steps on the data (images) and targets (labels). In pyTorch, this is done
    with 'transforms', which can be composed (chained together) as shown in
    http://pytorch.org/tutorials/beginner/data_loading_tutorial.html#transforms.
    While that example implements its own transforms, for this project the
    built-in transforms in torchvision.transforms should suffice. See
    http://pytorch.org/docs/master/torchvision/transforms.html for the list of
    available built-in transforms.
    Args:
    - data_path: (string) Path to the directory that contains the 'test' and
        'train' data directories.
    - input_size: (w, h) Size of input image. The images will be resized to
        this size.
    - rgb: (boolean) Flag indicating if input images are RGB or grayscale. If
        False, images will be converted to grayscale.
    Returns:
    - train_dataloader: Dataloader for the training dataset.
    - test_dataloader: Dataloader for the testing/validation dataset.
    """
    train_data_path = osp.join(data_path, 'train')
    test_data_path = osp.join(data_path, 'test')
    # Below variables are provided for your convenience. You may or may not need
    # all of them.
    train_mean, train_std = utils.get_mean_std(train_data_path, input_size,
                                               rgb)
    test_mean, test_std = utils.get_mean_std(test_data_path, input_size, rgb)
    """ TRAIN DATA TRANSFORMS """
    train_data_tforms = []
    train_data_tforms.append(transforms.Resize(size=max(input_size)))
    train_data_tforms.append(transforms.CenterCrop(size=input_size))
    if not rgb:
        train_data_tforms.append(transforms.Grayscale())

    #######################################################################
    #                        TODO: YOUR CODE HERE                         #
    #######################################################################
    # TODO Add a transformation to you train_data_tforms that left-right mirrors
    # the image randomly. Which transformation should you add?
    # pass
    train_data_tforms.append(transforms.RandomHorizontalFlip(p=0.5))
    # train_data_tforms.append(transforms.RandomAffine(degrees=(-30, 30)))
    # Do not move the position of the below line (leave it between the left-right
    # mirroring and normalization transformations.
    train_data_tforms.append(transforms.ToTensor())

    # TODO Add a transformation to your train_data_tforms that normalizes the
    # tensor by subtracting mean and dividing by std. You may use train_mean,
    # test_mean, train_std, or test_std values that are already calculated for
    # you. Which mean and std should you use to normalize the data?
    # pass
    train_data_tforms.append(transforms.Normalize(train_mean, train_std))
    #######################################################################
    #                          END OF YOUR CODE                           #
    #######################################################################
    train_data_tforms = transforms.Compose(train_data_tforms)
    """ TEST/VALIDATION DATA TRANSFORMS """

    test_data_tforms = []
    test_data_tforms.append(transforms.Resize(size=max(input_size)))
    test_data_tforms.append(transforms.CenterCrop(size=input_size))
    if not rgb:
        test_data_tforms.append(transforms.Grayscale())
    test_data_tforms.append(transforms.ToTensor())
    #######################################################################
    #                        TODO: YOUR CODE HERE                         #
    #######################################################################
    # TODO Add a transformation to your test_data_tforms that normalizes the
    # tensor by subtracting mean and dividing by std. You may use train_mean,
    # test_mean, train_std, or test_std values that are already calculated for
    # you. Which mean and std should you use to normalize the data?
    # pass
    test_data_tforms.append(transforms.Normalize(test_mean, test_std))
    #######################################################################
    #                          END OF YOUR CODE                           #
    #######################################################################
    test_data_tforms = transforms.Compose(test_data_tforms)
    """ DATASET LOADERS """
    # Creating dataset loaders using the transformations specified above.
    train_dset = datasets.ImageFolder(root=osp.join(data_path, 'train'),
                                      transform=train_data_tforms)
    test_dset = datasets.ImageFolder(root=osp.join(data_path, 'test'),
                                     transform=test_data_tforms)
    return train_dset, test_dset
Exemple #10
0
def train_model(device,
                model_name,
                classification_model,
                colorization_model,
                dataloaders,
                criterion,
                optimizer,
                save_dir=None,
                save_all_epochs=False,
                num_epochs=25):
    '''
    model: The NN to train
    dataloaders: A dictionary containing at least the keys 
                 'train','val' that maps to Pytorch data loaders for the dataset
    criterion: The Loss function
    optimizer: The algorithm to update weights 
               (Variations on gradient descent)
    num_epochs: How many epochs to train for
    save_dir: Where to save the best model weights that are found, 
              as they are found. Will save to save_dir/weights_best.pt
              Using None will not write anything to disk
    save_all_epochs: Whether to save weights for ALL epochs, not just the best
                     validation error epoch. Will save to save_dir/weights_e{#}.pt
    '''
    since = time.time()

    val_acc_history = []

    best_model_wts = copy.deepcopy(colorization_model.state_dict())
    best_loss = float('inf')

    train_loss = []
    val_loss = []
    start_time = time.perf_counter()
    for epoch in range(num_epochs):

        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
        print('-' * 10)

        # Each epoch has a training and validation phase
        for phase in ['train', 'val']:
            if phase == 'train':
                colorization_model.train()  # Set model to training mode
            else:
                colorization_model.eval()  # Set model to evaluate mode

            running_loss = 0.0

            # Iterate over data.
            # TQDM has nice progress bars
            num_iter = 0
            for inputs, _ in tqdm(dataloaders[phase]):
                if num_iter > MAX_ITER:
                    break
                num_iter += 1
                inputs = inputs.to(device)
                # zero the parameter gradients
                optimizer.zero_grad()

                # forward
                # track history if only in train
                with torch.set_grad_enabled(phase == 'train'):
                    # Get model outputs and calculate loss
                    outputs = forwards(inputs, classification_model,
                                       colorization_model)

                    loss = post_processing(inputs, outputs, criterion)

                    # torch.max outputs the maximum value, and its index
                    # Since the input is batched, we take the max along axis 1
                    # (the meaningful outputs)
                    _, preds = torch.max(outputs, 1)

                    # backprop + optimize only if in training phase
                    if phase == 'train':
                        loss.backward()
                        optimizer.step()

                # statistics
                running_loss += loss.item() * inputs.size(0)
                #running_corrects += torch.sum(preds == labels.data)

            epoch_loss = running_loss / len(dataloaders[phase].dataset)
            #epoch_acc = running_corrects.double() / len(dataloaders[phase].dataset)
            print("epoch_loss", epoch_loss)

            #val_loss = evaluate(classification_model,colorization_model, dataloaders['val'], criterion, is_labelled = True, generate_labels = generate_validation_labels, k = 5)

            if phase == "train":
                train_loss.append(epoch_loss)
                plt.clf()
                plt.plot(train_loss)
                plt.xlabel("Epoch")
                plt.ylabel("Train Loss")
                plt.title(model_name + " Accuracy")
                plt.savefig("plots/" + model_name + '/' + 'TrainAccuracy.png')
                plt.clf()
                pass
            # deep copy the model
            if phase == 'val' and epoch_loss < best_loss:
                best_loss = epoch_loss
                best_model_wts = copy.deepcopy(colorization_model.state_dict())
                torch.save(best_model_wts,
                           os.path.join(save_dir, model_name + '_best.pt'))
            if phase == 'val':
                val_loss.append(epoch_loss)
                plt.clf()
                plt.plot(val_loss)
                plt.xlabel("Epoch")
                plt.ylabel("Val Loss")
                plt.title(model_name + " Accuracy")
                plt.savefig("plots/" + model_name + '/' + 'ValAccuracy.png')
                plt.clf()
                pass
            if save_all_epochs:
                torch.save(colorization_model.state_dict(),
                           os.path.join(save_dir, f'weights_{epoch}.pt'))
        print()
        for inputs, _ in tqdm(dataloaders['example']):
            gray = transforms.Grayscale(num_output_channels=3)(inputs)
            outputs = forwards(inputs, classification_model,
                               colorization_model)
            inputs = np.swapaxes(inputs, 1, 3)
            inputs = np.swapaxes(inputs, 1, 2)
            outputs = np.swapaxes(outputs, 1, 3)
            outputs = np.swapaxes(outputs, 1, 2)
            gray = np.swapaxes(gray, 1, 3)
            gray = np.swapaxes(gray, 1, 2)
            f, ax = plt.subplots(inputs.shape[0], 3)
            ax[0][0].set_title("Original")
            ax[0][1].set_title("Grayscale")
            ax[0][2].set_title("Recolorized")
            for i in range(inputs.shape[0]):
                # use the created array to output your multiple images. In this case I have stacked 4 images vertically
                ax[i][0].imshow(inputs[i])
                remove_axis(ax[i][0])
                remove_axis(ax[i][1])
                remove_axis(ax[i][2])
                #ax[i][1].imshow(outputs[i].detach().numpy())
                ax[i][1].imshow(gray[i])
                lab = color.rgb2lab(inputs[i])
                full_output = np.stack([
                    lab[:, :, 0], outputs[i, :, :, 0].detach().numpy(),
                    outputs[i, :, :, 1].detach().numpy()
                ],
                                       axis=2)
                ax[i][2].imshow(color.lab2rgb(full_output))
            plt.savefig('outputs/' + save_file + '/figure epoch number ' +
                        str(epoch) + '.png')
            break

    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))
    print('Best val Acc: {:4f}'.format(best_loss))

    # save and load best model weights
    torch.save(best_model_wts, os.path.join(save_dir, model_name + '_best.pt'))
    colorization_model.load_state_dict(best_model_wts)

    return colorization_model, val_acc_history
Exemple #11
0
    def __init__(self,
                 training,
                 p_data,
                 dmap_seq_paths,
                 poses,
                 intrin_path,
                 img_size=[1248, 380],
                 digitize=False,
                 d_candi=None,
                 d_candi_up=None,
                 resize_dmap=None,
                 if_process=True,
                 crop_w=384,
                 velodyne_depth=True,
                 pytorch_scaling=False):
        '''
        inputs:

        traning - if for training or not

        p_data - a pykitti.raw object, returned by get_paths()

        dmap_seq_paths - list of dmaps, returned by get_paths()

        poses - list of posesc

        d_candi - the candidate depths
        digitize (Optional; False) - if digitize the depth map using d_candi

        resize_dmap - the scale for re-scaling the dmap the GT dmap size would be self.img_size * resize_dmap

        if_process - if do post process

        crop_w - the cropped image width. We will crop the central region with wdith crop_w

        '''

        assert len(p_data) == len(dmap_seq_paths[0]) == len(poses)
        assert len(p_data) == len(dmap_seq_paths[1]) == len(poses)

        if crop_w is not None:
            assert (img_size[0] - crop_w) % 2 == 0 and crop_w % 4 == 0
            assert resize_dmap is not None

        self.pytorch_scaling = pytorch_scaling
        self.velodyne_depth = velodyne_depth
        self.p_data = p_data
        self.dmap_seq_paths = dmap_seq_paths
        self.poses = poses
        self.training = training
        self.intrin_path = intrin_path  # reserved

        self.to_gray = tfv_transform.Compose(
            [tfv_transform.Grayscale(),
             tfv_transform.ToTensor()])
        self.d_candi = d_candi
        self.digitize = digitize

        self.crop_w = crop_w

        if digitize:
            self.label_min = 0
            self.label_max = len(d_candi) - 1

        # usample in the d dimension #
        self.dup4_candi = d_candi_up
        self.dup4_label_min = 0
        self.dup4_label_max = len(self.dup4_candi) - 1
        ##

        self.resize_dmap = resize_dmap

        self.img_size = img_size  # the raw input image size (used for resizing the input images)

        self.if_preprocess = if_process

        if crop_w is not None:
            self.crop_amt = [self.img_size[0] / self.crop_w, 1.]
            if self.crop_amt[0] != 2: raise Exception('Check this')

        # initialization about the camera intrsinsics, which is the same for all data #
        if crop_w is None:
            left_cam_intrinsics = self.get_cam_intrinsics(None, "left")
            right_cam_intrinsics = self.get_cam_intrinsics(None, "right")
        else:
            width_ = int(crop_w * self.resize_dmap)
            height_ = int(float(self.img_size[1] * self.resize_dmap))
            img_size_ = np.array([width_, height_], dtype=int)
            left_cam_intrinsics = self.get_cam_intrinsics(
                img_size=img_size_, mode="left", crop_amt=self.crop_amt)
            right_cam_intrinsics = self.get_cam_intrinsics(
                img_size=img_size_, mode="right", crop_amt=self.crop_amt)

        self.left_cam_intrinsics = left_cam_intrinsics
        self.right_cam_intrinsics = right_cam_intrinsics
Exemple #12
0
def forwards(inputs, classification_model, colorization_model):
    gray = transforms.Grayscale(num_output_channels=3)(inputs)
    mid_level_features = classification_model(gray)
    return colorization_model(mid_level_features)
Exemple #13
0
def get_dataset(dataset_name):
    normalize = T.Normalize(mean=[0.485, 0.456, 0.406],
                            std=[0.229, 0.224, 0.225])
    transform = T.Compose(
        [T.Resize(256),
         T.CenterCrop(224),
         T.ToTensor(), normalize])
    grey_transform = T.Compose([
        T.Resize(256),
        T.CenterCrop(224),
        T.Grayscale(3),
        T.ToTensor(), normalize
    ])

    data_dir = 'data'
    if not osp.exists(data_dir): os.makedirs(data_dir)

    if dataset_name == 'mnist':
        return datasets.MNIST("./data/mnist",
                              train=True,
                              transform=grey_transform,
                              target_transform=None,
                              download=True)

    if dataset_name == 'fashionmnist':
        return datasets.FashionMNIST("./data/fashionmnist",
                                     train=True,
                                     transform=grey_transform,
                                     target_transform=None,
                                     download=True)

    if dataset_name == 'svhn':
        return datasets.SVHN("./data/svhn",
                             split='train',
                             transform=transform,
                             target_transform=None,
                             download=True)

    if dataset_name == 'cifar':
        return datasets.CIFAR10("./data/cifar",
                                train=True,
                                transform=transform,
                                target_transform=None,
                                download=True)

    if dataset_name == 'cifar_watermarked':
        return datasets.ImageFolder("./data/CIFAR_data/watermarked_ds",
                                    transform=transform)

    if dataset_name == 'pubfig':
        return PUBFIG83(root='./data/pubfig_data/pubfig83-aligned',
                        train=True,
                        num_perclass=108,
                        transform=transform)

    if dataset_name == 'pubfig_watermarked':
        return PUBFIG83(root='./data/pubfig_data/watermarked_ds',
                        train=True,
                        num_perclass=108,
                        transform=transform)

    if dataset_name == 'tinyimagenet':
        return datasets.ImageFolder("./data/tiny-imagenet-200/train",
                                    transform=transform)

    if dataset_name == 'test_custom_mnist':
        return create_custom_mnist_ds("./data/mnistForData/test.npz")

    if dataset_name == 'train_custom_mnist':
        return create_custom_mnist_ds("./data/mnistForData/train.npz")

    if dataset_name == 'prediction_custom_mnist':
        return create_custom_mnist_ds("./data/mnistForData/prediction.npz")

    if dataset_name == 'train_usps_ds':
        return create_usps_ds('./data/usps/usps.h5')

    if dataset_name == 'test_usps_ds':
        return create_usps_ds('./data/usps/usps.h5', train=False)

    raise NotImplementedError(f'dataset = {dataset_name} not implemented')
Exemple #14
0
    def __init__(self, test=None):
        rgb_transform = transforms.Compose(
            [transforms.Resize((384, 128), interpolation=3)])
        gray_transform = transforms.Compose([
            transforms.Resize((384, 128), interpolation=3),
            transforms.Grayscale(3)
        ])
        test_transform = transforms.Compose([
            transforms.Resize((384, 128), interpolation=3),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])
        process_transform = transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225]),
            RandomErasing(probability=0.5, mean=[0.0, 0.0, 0.0])
        ])
        woEr_process_transform = transforms.Compose([
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
        ])

        self.trainset = Market1501(data_path=opt.data_path,
                                   dtype="train",
                                   rgb_trans=rgb_transform,
                                   gray_trans=gray_transform,
                                   process_trans=process_transform)
        self.trainset_woEr = Market1501(data_path=opt.data_path,
                                        dtype="train",
                                        rgb_trans=rgb_transform,
                                        gray_trans=gray_transform,
                                        process_trans=woEr_process_transform)
        if test is None:
            self.testset = Market1501(data_path=opt.data_path,
                                      dtype="test",
                                      test_trans=test_transform)
        else:
            self.testset = Market1501(data_path=opt.data_path + '/' +
                                      str(test),
                                      dtype="test",
                                      test_trans=test_transform)
        self.queryset = Market1501(data_path=opt.data_path,
                                   dtype="query",
                                   test_trans=test_transform)

        self.train_loader = dataloader.DataLoader(
            self.trainset,
            sampler=RandomSampler(self.trainset,
                                  batch_id=opt.batchid,
                                  batch_image=opt.batchimage),
            batch_size=opt.batchid * opt.batchimage,
            num_workers=0,
            pin_memory=True)  # 8
        self.train_loader_woEr = dataloader.DataLoader(
            self.trainset_woEr,
            sampler=RandomSampler(self.trainset_woEr,
                                  batch_id=opt.batchid,
                                  batch_image=opt.batchimage),
            batch_size=opt.batchid * opt.batchimage,
            num_workers=0,
            pin_memory=True)  # 8

        self.test_loader = dataloader.DataLoader(self.testset,
                                                 batch_size=opt.batchtest,
                                                 num_workers=0,
                                                 pin_memory=True)  # 8
        self.query_loader = dataloader.DataLoader(self.queryset,
                                                  batch_size=opt.batchtest,
                                                  num_workers=0,
                                                  pin_memory=True)  # 8
import torch
import torch.utils.data as Data
import torch.nn as nn
import torchvision
import numpy as np
import torchvision.transforms as transforms
import scipy.io as sio

test_data = torchvision.datasets.ImageFolder('./Testing_set', transform=transforms.Compose([
    transforms.Grayscale(num_output_channels=1),transforms.ToTensor()]))

test_loader=Data.DataLoader(dataset=test_data, batch_size=1, shuffle=False)

# NN network
class Autoencoder1(nn.Module):
    def __init__(self):
        super(Autoencoder1, self).__init__()
        self.encoder = nn.Sequential(  # two layers encoder
            nn.Linear(74*74, 8000),
            nn.ReLU(True),  # ReLU, Tanh, etc.
            nn.Linear(8000, 1000),
            nn.ReLU(True),  # input is in (0,1), so select this one
        )
        self.decoder = nn.Sequential(  # two layers decoder
            nn.Linear(1000, 8000),
            nn.ReLU(True),
            nn.Linear(8000, 74*74),
            nn.Sigmoid()
        )

    def forward(self, x):
def load_data(image_size, category_filter, train_test_split, is_gray=False):
    if is_gray:
        train_transform = transforms.Compose([
            transforms.Resize(image_size),
            transforms.RandomHorizontalFlip(),
            transforms.RandomAffine(30, translate=(0.3, 0.3), scale=(1.0, 1.5)),
            ImageNetPolicy(),
            transforms.Grayscale(3),
            transforms.ToTensor()
        ])
        test_transform = transforms.Compose([
            transforms.Resize(image_size),
            transforms.Grayscale(3),
            transforms.ToTensor()
        ])
    else:
        train_transform = transforms.Compose([
            transforms.Resize(image_size),
            transforms.RandomHorizontalFlip(),
            transforms.RandomAffine(30, translate=(0.3, 0.3), scale=(1.0, 1.5)),
            ImageNetPolicy(),
            transforms.ToTensor()
        ])
        test_transform = transforms.Compose([
            transforms.Resize(image_size),
            transforms.ToTensor()
        ])

    root_dir = '/train-data/inaturalist-2019/'
    train_test_set = InaturalistDataset(
        root_dir + 'train2019.json',
        root_dir,
        train_transform,
        category_filter=category_filter
    )
    val_set = InaturalistDataset(
        root_dir + 'val2019.json',
        root_dir,
        test_transform,
        category_filter=category_filter
    )

    if isinstance(train_test_split, float):
        train_size = int(len(train_test_set) * train_test_split)
        test_size = len(train_test_set) - train_size

        train_set, test_set = torch.utils.data.random_split(train_test_set, [train_size, test_size])
    elif isinstance(train_test_split, dict):
        train_indices, test_indices = train_test_set.sample(train_test_split)

        train_set = torch.utils.data.Subset(train_test_set, train_indices)
        test_set = torch.utils.data.Subset(train_test_set, test_indices)

    test_set.__getattribute__('dataset').__setattr__('transform', test_transform)

    print(f'train: {len(train_set)}, val: {len(val_set)}, test: {len(test_set)}')

    train_loader = torch.utils.data.DataLoader(train_set, batch_size=64, shuffle=True, num_workers=32)
    val_loader = torch.utils.data.DataLoader(val_set, batch_size=64, shuffle=True, num_workers=32)
    test_loader = torch.utils.data.DataLoader(test_set, batch_size=64, shuffle=True, num_workers=32)

    return (train_loader, val_loader, test_loader)
def main():
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    wali = create_WALI().to(device)

    optimizerEG = Adam(list(wali.get_encoder_parameters()) +
                       list(wali.get_generator_parameters()),
                       lr=LEARNING_RATE,
                       betas=(BETA1, BETA2))
    optimizerC = Adam(wali.get_critic_parameters(),
                      lr=LEARNING_RATE,
                      betas=(BETA1, BETA2))

    transform = transforms.Compose([
        transforms.Resize(IMAGE_SIZE),
        transforms.Grayscale(1),
        transforms.ToTensor(),
        transforms.Normalize((0.5, ), (0.5, ))
    ])
    mold_img = ImageFolder('data/folder/train', transform)

    loader = data.DataLoader(mold_img, BATCH_SIZE, shuffle=True)

    noise = torch.randn(64, NLAT, 1, 1, device=device)

    EG_losses, C_losses = [], []
    curr_iter = C_iter = EG_iter = 0
    C_update, EG_update = True, False
    print('Training starts...')

    while curr_iter < ITER:
        for batch_idx, (x, _) in enumerate(loader, 1):
            x = x.to(device)

            if curr_iter == 0:
                init_x = x
                curr_iter += 1

            z = torch.randn(x.size(0), NLAT, 1, 1).to(device)
            C_loss, EG_loss = wali(x, z, lamb=LAMBDA)

            if C_update:
                optimizerC.zero_grad()
                C_loss.backward()
                C_losses.append(C_loss.item())
                optimizerC.step()
                C_iter += 1

                if C_iter == C_ITERS:
                    C_iter = 0
                    C_update, EG_update = False, True
                continue

            if EG_update:
                optimizerEG.zero_grad()
                EG_loss.backward()
                EG_losses.append(EG_loss.item())
                optimizerEG.step()
                EG_iter += 1

                if EG_iter == EG_ITERS:
                    EG_iter = 0
                    C_update, EG_update = True, False
                    curr_iter += 1
                else:
                    continue

            # print training statistics
            if curr_iter % 100 == 0:
                print('[%d/%d]\tW-distance: %.4f\tC-loss: %.4f' %
                      (curr_iter, ITER, EG_loss.item(), C_loss.item()))

                # plot reconstructed images and samples
                wali.eval()
                real_x, rect_x = init_x[:32], wali.reconstruct(
                    init_x[:32]).detach_()
                rect_imgs = torch.cat(
                    (real_x.unsqueeze(1), rect_x.unsqueeze(1)), dim=1)
                rect_imgs = rect_imgs.view(64, NUM_CHANNELS, IMAGE_SIZE,
                                           IMAGE_SIZE).cpu()
                genr_imgs = wali.generate(noise).detach_().cpu()
                utils.save_image(rect_imgs * 0.5 + 0.5,
                                 'Runs/fsize/rect%d.png' % curr_iter)
                utils.save_image(genr_imgs * 0.5 + 0.5,
                                 'Runs/fsize/genr%d.png' % curr_iter)
                wali.train()

            # save model
            if curr_iter % (ITER // 10) == 0:
                torch.save(wali.state_dict(), 'Runs/fsize/%d.ckpt' % curr_iter)
Exemple #18
0
def load_dataset(dataset, train_size, valid_size, test_size):
    """Load the dataset passed in argument with the corresponding sizes for the training, validation and testing set."""

    if dataset == 'mnist_012':
        root = './data/mnist'
        num_classes = 3

        trans = transforms.Compose([transforms.Grayscale(num_output_channels=1), transforms.ToTensor(), transforms.Normalize(mean=MNIST_MEAN, std=MNIST_STD)])
        train_valid_set = datasets.MNIST(root=root, train=True, transform=trans)
        test_set = datasets.MNIST(root=root, train=False, transform=trans)

        train_valid_set = MNIST_bis(dataset=train_valid_set, size=train_size+valid_size, digits_to_keep=[0,1,2])
        test_set = MNIST_bis(dataset=test_set, size=test_size, digits_to_keep=[0,1,2])

        train_sampler, valid_sampler = train_valid_split(dataset=train_valid_set, train_size=train_size)

        train_loader = DataLoader(dataset=train_valid_set, batch_size=BATCH_SIZE, sampler=train_sampler, num_workers=4, pin_memory=True, drop_last=True)
        valid_loader = DataLoader(dataset=train_valid_set, batch_size=BATCH_SIZE, sampler=valid_sampler, num_workers=4, pin_memory=True, drop_last=True)
        test_loader = DataLoader(dataset=test_set, batch_size=BATCH_SIZE, num_workers=4, pin_memory=True, drop_last=True)

    elif dataset == 'mnist_rot':
        root = './data/mnist'
        num_classes = 9

        train_trans = transforms.Compose([transforms.Grayscale(num_output_channels=1), transforms.Resize((26,26)), transforms.ToTensor(), transforms.Normalize(mean=MNIST_MEAN, std=MNIST_STD)])
        test_trans = transforms.Compose([transforms.Grayscale(num_output_channels=1), transforms.Resize((26,26)), transforms.RandomRotation((0,360)), transforms.ToTensor(), transforms.Normalize(mean=MNIST_MEAN, std=MNIST_STD)])
        train_valid_set = datasets.MNIST(root=root, train=True, transform=train_trans)
        test_set = datasets.MNIST(root=root, train=False, transform=test_trans)

        train_valid_set_bis = MNIST_bis(dataset=train_valid_set, size=train_size+valid_size, digits_to_keep=[0,1,2,3,4,5,6,7,8])
        test_set = MNIST_bis(dataset=test_set, size=test_size, digits_to_keep=[0,1,2,3,4,5,6,7,8])

        train_sampler, valid_sampler = train_valid_split(dataset=train_valid_set_bis, train_size=train_size)

        train_loader = DataLoader(dataset=train_valid_set_bis, batch_size=BATCH_SIZE, sampler=train_sampler, num_workers=4, pin_memory=True, drop_last=True)
        valid_loader = DataLoader(dataset=train_valid_set_bis, batch_size=BATCH_SIZE, sampler=valid_sampler, num_workers=4, pin_memory=True, drop_last=True)
        test_loader = DataLoader(dataset=test_set, batch_size=BATCH_SIZE, num_workers=4, pin_memory=True, drop_last=True)

    elif dataset == 'mnist_trans':
        root = './data/mnist'
        num_classes = 9

        train_trans = transforms.Compose([transforms.Grayscale(num_output_channels=1), transforms.Resize((26,26)), transforms.ToTensor(), transforms.Normalize(mean=MNIST_MEAN, std=MNIST_STD)])
        test_trans = transforms.Compose([transforms.Grayscale(num_output_channels=1), transforms.Resize((26,26)), RandomTranslation(horizontal=6, vertical=6), transforms.ToTensor(), transforms.Normalize(mean=MNIST_MEAN, std=MNIST_STD)])
        train_valid_set = datasets.MNIST(root=root, train=True, transform=train_trans)
        test_set = datasets.MNIST(root=root, train=False, transform=test_trans)
        
        train_valid_set_bis = MNIST_bis(dataset=train_valid_set, size=train_size+valid_size, digits_to_keep=[0,1,2,3,4,5,6,7,8])
        test_set = MNIST_bis(dataset=test_set, size=test_size, digits_to_keep=[0,1,2,3,4,5,6,7,8])

        train_sampler, valid_sampler = train_valid_split(dataset=train_valid_set_bis, train_size=train_size)

        train_loader = DataLoader(dataset=train_valid_set_bis, batch_size=BATCH_SIZE, sampler=train_sampler, num_workers=4, pin_memory=True, drop_last=True)
        valid_loader = DataLoader(dataset=train_valid_set_bis, batch_size=BATCH_SIZE, sampler=valid_sampler, num_workers=4, pin_memory=True, drop_last=True)
        test_loader = DataLoader(dataset=test_set, batch_size=BATCH_SIZE, num_workers=4, pin_memory=True, drop_last=True)

    elif dataset == 'eth80':
        root = './data/eth80'
        num_classes = 8

        trans = transforms.Compose([transforms.Grayscale(num_output_channels=1), transforms.Resize((50,50)), transforms.ToTensor(), transforms.Normalize(mean=ETH80_MEAN, std=ETH80_STD)])
        complete_set = datasets.ImageFolder(root=root, transform=trans)
        class_names = complete_set.classes

        train_sampler, valid_sampler, test_sampler = train_valid_test_split(dataset=complete_set, train_size=train_size, valid_size=valid_size)
        
        train_loader = DataLoader(dataset=complete_set, batch_size=BATCH_SIZE, sampler=train_sampler, num_workers=4, pin_memory=True, drop_last=True) 
        valid_loader = DataLoader(dataset=complete_set, batch_size=BATCH_SIZE, sampler=valid_sampler, num_workers=4, pin_memory=True, drop_last=True) 
        test_loader = DataLoader(dataset=complete_set, batch_size=BATCH_SIZE, sampler=test_sampler, num_workers=4, pin_memory=True, drop_last=True)

    else:
        raise ValueError('Specified dataset does not exist.')

    logger.debug('Class frequency train loader: {} validation loader: {} test loader: {}'.format(
        count_class_freq(train_loader, num_classes),count_class_freq(valid_loader, num_classes), count_class_freq(test_loader, num_classes))
        )
    logging.info('Loaded {} dataset with the split {}-{}-{} for the [train]-[valid]-[test] setup.'.format(dataset, len(train_loader)*BATCH_SIZE, len(valid_loader)*BATCH_SIZE, len(test_loader)*BATCH_SIZE))


    return train_loader, valid_loader, test_loader, get_dim(train_loader)
def classify_training(netGS, dataset, iter):
    ### Data loaders
    if dataset == 'mnist' or dataset == 'fashionmnist':
        transform_train = transforms.Compose([
        transforms.CenterCrop((28, 28)),
        transforms.ToTensor(),
        #transforms.Grayscale(),
        ])
    elif dataset == 'cifar_100' or dataset == 'cifar_10':
        transform_train = transforms.Compose([
        transforms.CenterCrop((28, 28)),
        transforms.Grayscale(),
        transforms.ToTensor(),
        ])

    if dataset == 'mnist':
        dataloader = datasets.MNIST
        test_set = dataloader(root=os.path.join(DATA_ROOT, 'MNIST'), train=False, download=True,
                              transform=transform_train)
        IMG_DIM = 784
        NUM_CLASSES = 10
    elif dataset == 'fashionmnist':
        dataloader = datasets.FashionMNIST
        test_set = dataloader(root=os.path.join(DATA_ROOT, 'FashionMNIST'), train=False, download=True,
                              transform=transform_train)
    elif dataset == 'cifar_100':
        dataloader = datasets.CIFAR100
        test_set = dataloader(root=os.path.join(DATA_ROOT, 'CIFAR100'), train=False, download=True,
                              transform=transform_train)
        IMG_DIM = 3072
        NUM_CLASSES = 100
    elif dataset == 'cifar_10':
        IMG_DIM = 784
        NUM_CLASSES = 10
        dataloader = datasets.CIFAR10
        test_set = dataloader(root=os.path.join(DATA_ROOT, 'CIFAR10'), train=False, download=True,
                              transform=transform_train)
    else:
        raise NotImplementedError
    test_loader = DataLoader(test_set, batch_size=1000, shuffle=False)

    netGS.eval()
    for i in tqdm(range(25)):
        gen_labels = Variable(LongTensor(np.random.randint(0, 10, 2000)))
        noise = Variable(FloatTensor(np.random.normal(0, 1, (2000, args.z_dim))))
        synthesized = netGS(noise, gen_labels)

        if (i == 0):
            new_data = synthesized.cpu().detach()
            new_label = gen_labels.cpu().detach()
        else:
            new_data = torch.cat((new_data, synthesized.cpu().detach()), 0)
            new_label = torch.cat((new_label, gen_labels.cpu().detach()), 0)

    new_data = torch.clamp(new_data, min=0., max=1.)

    C = Classifier().cuda()
    C.train()
    opt_C = torch.optim.Adam(C.parameters(), lr=1e-3)
    criterion = nn.CrossEntropyLoss()

    gen_set = TensorDataset(new_data.cuda(), new_label.cuda())
    gen_loader = DataLoader(gen_set, batch_size=32, shuffle=True)

    prg_bar = tqdm(range(50))
    for epoch in prg_bar:
        train_acc = 0.0
        train_loss = 0.0
        for i, (data, label) in enumerate(gen_loader):
            pred = C(data)
            loss = criterion(pred, label)

            opt_C.zero_grad()
            loss.backward()
            opt_C.step()

            train_acc += np.sum(np.argmax(pred.cpu().data.numpy(), axis=1) == label.cpu().numpy())
            train_loss += loss.item()
        
        prg_bar.set_description(f'acc: {train_acc/gen_set.__len__():.3f}  loss: {train_loss/gen_set.__len__():.4f}')

    test_acc = 0.0
    C.eval()
    for i, (data, label) in enumerate(test_loader):
        data = Variable(data.type(FloatTensor))
        label = Variable(label.type(LongTensor))
        pred = C(data)
        test_acc += np.sum(np.argmax(pred.cpu().data.numpy(), axis=1) == label.cpu().numpy())

    test_acc /= test_set.__len__()
    test_acc *= 100
    print(f'the final result of test accuracy = {test_acc/100:.3f}')

    for i in range(9):
        if (test_acc > acc_milestone[i] and acc_passed[i] == False):
            acc_passed[i] = True
            torch.save(netGS, os.path.join(args.checkpoint, f'diff_acc/{acc_milestone[i]}.pth'))
            with open(os.path.join(args.checkpoint, f'diff_acc/result.txt'), 'a') as f:
                f.write(f"thres:{acc_milestone[i]}% iter:{iter}, acc:{test_acc:.1f}%\n")

    if (iter in iter_milestone):
        torch.save(netGS, os.path.join(args.checkpoint, f'diff_iter/{iter}.pth'))
        with open(os.path.join(args.checkpoint, f'diff_iter/result.txt'), 'a') as f:
                f.write(f"iter:{iter}, acc:{test_acc:.1f}%\n")

    del C, new_data, new_label, gen_set, gen_loader
    torch.cuda.empty_cache()
        output = self.discriminator_block_1(inputs)
        output = self.discriminator_block_2(output)
        output = self.discriminator_block_3(output)
        output = self.discriminator_block_4(output)
        output = self.discriminator_block_5(output)
        output = self.discriminator_block_6(output)
        output = self.last_layer(output)
        return output

# Configure dataset loader
loading_transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

gray_scale_transform = transforms.Compose(
    [transforms.ToPILImage(), transforms.Grayscale(num_output_channels=1), transforms.ToTensor(),
     transforms.Normalize(mean=[0.5], std=[0.5])])

# Initialize generator and discriminator
generator = Generator('G', training=training)
discriminator = Discriminator('D', training=training)

if cuda:
    generator.cuda(gpu_id)
    discriminator.cuda(gpu_id)
if training:
    # Loss function
    bce_loss_with_logits = torch.nn.BCEWithLogitsLoss()
    if cuda:
        bce_loss_with_logits.cuda(gpu_id)
Exemple #21
0
assert (len(sys.argv) > 1)
save_path = sys.argv[1]

n_epochs = 1500
log_interval = 100
use_cuda = torch.cuda.is_available()
if use_cuda:
    torch.cuda.empty_cache()
    #torch.cuda.set_device(1)

preprocess_train = transforms.Compose([
    # transforms.RandomRotation(30),
    # transforms.RandomResizedCrop(28),
    # transforms.RandomHorizontalFlip(),
    transforms.Resize(224),
    transforms.Grayscale(num_output_channels=3),
    transforms.ToTensor()
])

preprocess_test = transforms.Compose([
    transforms.Resize(224),
    transforms.Grayscale(num_output_channels=3),
    transforms.ToTensor(),
])

trainset = torchvision.datasets.FashionMNIST(root="fashion",
                                             train=True,
                                             download=True,
                                             transform=preprocess_train)
testset = torchvision.datasets.FashionMNIST(root="fashion",
                                            train=False,
Exemple #22
0
def train_model(model, output_dir):
    class IndexedDataset(datasets.ImageFolder):
        def __getitem__(self, index):
            data, target = super(IndexedDataset, self).__getitem__(index)
            return data, target, index

    # Load data
    train_trans = transforms.Compose([
        transforms.Grayscale(1),
        transforms.Resize([image_width, image_width]),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.8], std=[0.2])
    ])
    trainset = IndexedDataset(train_dir, transform=train_trans)
    classes = trainset.classes
    train_loader = DataLoader(dataset=trainset,
                              batch_size=batch_size,
                              shuffle=True)

    # Loss and optimizer
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

    # Train the model
    total_step = len(train_loader)
    loss_list = []
    acc_list = []

    # Visualization object
    visu = Visualization(
        classes
    )  # , VIS_DATA, VIS_TARGET, VIS_PRED, test_vis_data, TEST_TARGET, TEST_PRED, VIS_ACC, TEST_ACC

    for epoch in range(num_epochs):
        # Per batch
        total_totals = 0
        total_correct = 0

        epoch_indices = []
        epoch_vis_data = []
        epoch_vis_target = []
        epoch_vis_predicted = []

        for i, (images, labels, indices) in enumerate(train_loader):
            # print(f"indices = {indices.numpy()}")
            # Batch i
            # Run the forward pass
            outputs, train_vis_data = model(images)
            epoch_indices.extend(indices)
            epoch_vis_data.extend(train_vis_data)
            epoch_vis_target.extend(labels.numpy())
            loss = criterion(outputs, labels)
            loss_list.append(loss.item())

            # Backprop and perform Adam optimisation
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            # Track the accuracy
            total = labels.size(0)
            total_totals += total
            _, predicted = torch.max(outputs.data, 1)
            epoch_vis_predicted.extend(predicted.numpy())
            correct = (predicted == labels).sum().item()
            total_correct += correct
            accuracy = correct / total
            acc_list.append(accuracy)

            if (i + 1) % 20 == 0:
                print(
                    'Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Accuracy: {:.2f}%'
                    .format(epoch + 1, num_epochs, i + 1, total_step,
                            loss.item(), (accuracy) * 100))

        visu.add_data(epoch_indices, epoch_vis_data, epoch_vis_target,
                      epoch_vis_predicted)

        # Add (0,0) point for the test accuracy graph
        if epoch == 0:
            _, _, _, test_acc, _ = test_model(model)
            visu.TEST_ACC.append((epoch + 1, test_acc))

        visu.VIS_ACC.append((epoch + 1, total_correct / total_totals))

        if (epoch + 1) % show_after_epochs == 0:
            visu.TEST_DATA, visu.TEST_TARGET, visu.TEST_PRED, test_acc, images_prediction = test_model(
                model)
            visu.TEST_ACC.append((epoch + 1, test_acc))
            # print(images_prediction)
            for image, pred, label in images_prediction:
                # print(image)
                # print(pred)
                visu.add_class_colour(image, pred, label)
            # visu.make_vis(output_dir, epoch+1)
            visu.make_label_vis(output_dir, epoch + 1)

        visu.clear_after_epoch()
    return classes, train_loader, loss_list, acc_list
    def __init__(self):
        self.config = GlobalConfig()
        os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
        print("torch.distributed.is_available: " +
              str(torch.distributed.is_available()))
        print("Device Count: {0}".format(torch.cuda.device_count()))

        # Creates training set
        train_transform = transforms.Compose([
            transforms.Resize(self.config.Width),
            transforms.RandomCrop(self.config.Width),
            transforms.ToTensor(),
            transforms.Normalize(mean=self.config.mean, std=self.config.std)
        ])
        self.train_loader = torch.utils.data.DataLoader(
            datasets.ImageFolder(self.config.TRAIN_PATH, train_transform),
            batch_size=self.config.train_batch_size,
            num_workers=4,
            pin_memory=True,
            shuffle=True,
            drop_last=True)
        # Creates water set
        train_water_transform = transforms.Compose([
            transforms.Resize(self.config.Water_Width),
            transforms.RandomCrop(self.config.Water_Width),
            transforms.Grayscale(),
            transforms.ToTensor(),
            transforms.Normalize(mean=self.config.mean[0],
                                 std=self.config.std[0])
        ])
        self.train_water_loader = torch.utils.data.DataLoader(
            datasets.ImageFolder(self.config.TRAIN_PATH,
                                 train_water_transform),
            batch_size=self.config.train_batch_size,
            num_workers=4,
            pin_memory=True,
            shuffle=True,
            drop_last=True)

        # Creates test set
        test_transform = transforms.Compose([
            transforms.Resize(self.config.Width),
            transforms.RandomCrop(self.config.Width),
            transforms.ToTensor(),
            transforms.Normalize(mean=self.config.mean, std=self.config.std)
        ])
        self.test_loader = torch.utils.data.DataLoader(datasets.ImageFolder(
            self.config.TEST_PATH, test_transform),
                                                       batch_size=1,
                                                       num_workers=4,
                                                       pin_memory=True,
                                                       shuffle=True,
                                                       drop_last=True)
        # Creates water test set
        test_water_transform = transforms.Compose([
            transforms.Resize(self.config.Water_Width),
            transforms.RandomCrop(self.config.Water_Width),
            transforms.Grayscale(),
            transforms.ToTensor(),
            transforms.Normalize(mean=self.config.mean[0],
                                 std=self.config.std[0])
        ])
        self.test_water_loader = torch.utils.data.DataLoader(
            datasets.ImageFolder(self.config.TEST_PATH, test_water_transform),
            batch_size=1,
            num_workers=4,
            pin_memory=True,
            shuffle=True,
            drop_last=True)

        # self.train_dataset = MyDataset(root=self.config.TRAIN_PATH,filename=self.config.TAG_PATH,mean=self.config.mean,std=self.config.std)
        # self.another_dataset = MyDataset(root=self.config.TRAIN_PATH, filename=self.config.TAG_PATH, grayscale=True, size=64,mean=self.config.mean,std=self.config.std)
        # print(len(self.train_dataset))
        # self.train_loader = data.DataLoader(dataset=self.train_dataset, batch_size=self.config.train_batch_size,
        #                                     shuffle=True, num_workers=4)
        # self.another_loader = data.DataLoader(dataset=self.another_dataset, batch_size=self.config.train_batch_size,
        #                                     shuffle=True, num_workers=4)

        self.net = LinJingZhiNet(config=self.config)
        self.train_cover, self.train_water = None, None
        self.test_cover, self.test_water = None, None
        self.pseudo = np.random.choice(
            [0, 1], (self.config.message_length,
                     self.config.message_length * self.config.message_length))
    pt_transforms.Pad((0, 1, 0, 1)),
    pt_transforms.Lambda(lambda x: my_transform_1(x)),
    # pt_transforms.RandomApply(p=0.85,transforms=[pt_transforms.Resize((20,20))]),
    # pt_transforms.RandomChoice([pt_transforms.ToTensor()]),
    # pt_transforms.ToPILImage(),
    # pt_transforms.RandomOrder([pt_transforms.Resize(20)]),
    # pt_transforms.RandomCrop((20, 20)),
    # pt_transforms.RandomHorizontalFlip(p=0),
    # pt_transforms.RandomVerticalFlip(p=0),
    # pt_transforms.RandomResizedCrop(100, (1,1), (2,2)),
    # pt_transforms.RandomSizedCrop(100, (1,1), (2,2)),
    # pt_transforms.ColorJitter(brightness = 2)
    pt_transforms.RandomRotation((0, 0)),
    pt_transforms.RandomRotation((0, 0)),
    pt_transforms.RandomAffine(0.7),
    pt_transforms.Grayscale(num_output_channels=1),
    pt_transforms.RandomGrayscale(p=0.5),
    # pt_transforms.RandomAffine(degrees=[-45, 45],translate=[0.15, 0.15],scale=[1.0, 1.2])
])
result_pt = transform_pt(img)
print(result_pt)

# Transform TF
transform_tf = tf_transforms.Compose([
    # tf_transforms.FiveCrop(5),
    # tf_transforms.FiveCrop((2,2)),
    # tf_transforms.TenCrop(5),
    # tf_transforms.TenCrop((2,2)),
    tf_transforms.ToTensor(),
    tf_transforms.ToPILImage(),
    tf_transforms.ToTensor(),
    def __init__(self, mom, do, wd, name, index):
        momentum = mom
        dropout = do
        weightdecay = wd
        name = name
        index = index
        learningRate = 0.1
        #momentum = float(sys.argv[1]) #0.5
        #dropout = float(sys.argv[2]) #0.2
        #weightdecay = float(sys.argv[3]) #0

        model = Net(dropout).to(device)
        optimizer = torch.optim.SGD(model.parameters(),
                                    lr=learningRate,
                                    momentum=momentum,
                                    weight_decay=weightdecay)
        criterion = nn.CrossEntropyLoss()

        print(model)

        batch_size = 32
        transform = transforms.Compose([
            transforms.Grayscale(num_output_channels=1),
            transforms.ToTensor()
        ])

        train_dataset = datasets.CIFAR10('./data',
                                         train=True,
                                         download=True,
                                         transform=transform)

        # train_dataset, validation_dataset = torch.utils.data.random_split(train_dataset, [40000, 10000])
        # validation_dataset = torch.utils.data.Subset(train_dataset, [])
        validation_dataset = datasets.CIFAR10('./data',
                                              train=True,
                                              transform=transform)

        testing_dataset = datasets.CIFAR10('./data',
                                           train=False,
                                           transform=transform,
                                           download=True)

        num_train = len(train_dataset)
        indices = list(range(num_train))
        split = 40000

        train_idx, valid_idx = indices[:split], indices[split:]
        train_dataset = Subset(train_dataset, train_idx)
        validation_dataset = Subset(validation_dataset, valid_idx)

        train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                                   batch_size=batch_size)

        validation_loader = torch.utils.data.DataLoader(
            dataset=validation_dataset, batch_size=batch_size)

        testing_loader = torch.utils.data.DataLoader(dataset=testing_dataset,
                                                     batch_size=batch_size)

        for (X_train, y_train) in train_loader:
            print('X_train:', X_train.size(), 'type:', X_train.type())
            print('y_train:', y_train.size(), 'type:', y_train.type())
            break
        print('validationsize: ' + str(len(validation_loader)))
        print('trainsize: ' + str(len(train_loader)))

        def train(epoch, log_interval=200):
            # Set model to training mode
            model.train()

            # Loop over each batch from the training set
            for batch_idx, (data, target) in enumerate(train_loader):
                # Copy data to GPU if needed
                data = data.to(device)
                target = target.to(device)

                # Zero gradient buffers
                optimizer.zero_grad()

                # Pass data through the network
                output = model(data)

                # Calculate loss
                loss = criterion(output, target)

                # Backpropagate
                loss.backward()

                # Update weights
                optimizer.step()

                if batch_idx % log_interval == 0:
                    print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.
                          format(epoch, batch_idx * len(data),
                                 len(train_loader.dataset),
                                 100. * batch_idx / len(train_loader),
                                 loss.data.item()))

        def validate(loss_vector, accuracy_vector, loader):
            model.eval()
            val_loss, correct = 0, 0
            for data, target in loader:
                data = data.to(device)
                target = target.to(device)
                output = model(data)
                val_loss += criterion(output, target).data.item()
                pred = output.data.max(1)[
                    1]  # get the index of the max log-probability
                correct += pred.eq(target.data).cpu().sum()

            val_loss /= len(loader)
            loss_vector.append(val_loss)

            accuracy = 100. * correct.to(torch.float32) / len(loader.dataset)
            accuracy_vector.append(accuracy)

            print(
                '\nValidation set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'
                .format(val_loss, correct, len(loader.dataset), accuracy))

            return accuracy

        #%%time
        epochs = 5

        lossv, accv = [], []
        for epoch in range(1, epochs + 1):
            train(epoch)
            validate(lossv, accv, validation_loader)

        testv, taccv = [], []
        print("THE BELOW IS TEST ACCURACY")
        self.accr = validate(testv, taccv, testing_loader)

        # Generate plot of loss as a function of training epochs
        plt.figure()
        plt.xlabel('Epoch')
        plt.ylabel('Loss')
        plt.plot(np.arange(1, epochs + 1), lossv, label="Loss")
        plt.title('Validation Loss')
        plt.legend(loc='best')
        plt.savefig('q3plot1' + name + str(index))

        # Generate plot of accuracy as a function of training epochs
        plt.figure()
        plt.xlabel('Epoch')
        plt.ylabel('Accuracy Percentage')
        plt.plot(np.arange(1, epochs + 1), accv, label="Accuracy")
        plt.title('Validation Accuracy')
        plt.legend(loc='best')
        plt.savefig('q3plot2' + name + str(index))
Exemple #26
0
def main():
    # # Parsing command line args
    # parser = argparse.ArgumentParser(description='CIFAR10 example')
    # parser.add_argument('--kernel', type=str, default=None,
    #                     help='Kernel type to use: [gaussian, polynomial, sigmoid, DCT] (default: None)')
    #
    # parser.add_argument('--epoch', type=int, default=2, help='Number of epochs (default: 2)')
    # parser.add_argument('--batch_size', type=int, default=4, help='Batch suze (default: 4)')
    # parser.add_argument('--gpu', type=bool, default=True, help='Use GPU? (default: True)')
    #
    # args = parser.parse_args()
    #
    # device = 'cpu'
    # if args.gpu:
    #     device = 'cuda'
    #
    # # Initiating network
    # resnet50 = torchvision.models.resnet50()
    # resnet50._modules['fc'] = torch.nn.Linear(2048, 10, True)
    # net = resnet50
    #
    # if args.kernel == 'gaussian':
    #     kernel_wrapper(net, GaussianKernel())
    # elif args.kernel == 'polynomial':
    #     kernel_wrapper(net, PolynomialKernel())
    # elif args.kernel == 'sigmoid':
    #     kernel_wrapper(net, SigmoidKernel())
    # elif args.kernel == "DCT":
    #     kernel_wrapper(net, SigmoidKernel())
    # elif args.kernel is not None:
    #     raise Exception('Invalid kernel')

    net = SharpnessModelUnfold(num_classes=3)
    net2 = SharpnessModelConv(num_classes=3)

    # net.to(device)

    # Loading datasets
    transform = transforms.Compose([
        transforms.Grayscale(),
        transforms.ToTensor()
        # transforms.Normalize((0.49, 0.48, 0.45), (0.25, 0.24, 0.26))
    ])

    # trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
    # trainloader = torch.utils.data.DataLoader(trainset, batch_size=1, shuffle=True, num_workers=2)
    testset = torchvision.datasets.FakeData(transform=transform)
    # testset = torchvision.datasets.(root='./data', train=False, download=True, transform=transform)
    # testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
    testloader = torch.utils.data.DataLoader(testset,
                                             batch_size=1,
                                             shuffle=False,
                                             num_workers=2)

    img = plt.imread("./resourses/image_blur3.jpg")[:200, :200, 0:1]
    torch_images = torch.Tensor(img).transpose(0, 2).transpose(1,
                                                               2).unsqueeze(0)
    with torch.no_grad():
        net.eval()
        # print(net)
        # for (images, labels) in testloader:
        # images = images.to(device)
        # labels = labels.to(device)
        outputs = net(torch_images)
        outputs2 = net2(torch_images)

    # dct_map_2 = compute_dct_pytorch(img[:, :, 0].astype(np.float))
    # dct_map_2[0][0, 0] = dct_map[0].max()
    # print(np.abs(dct_map).mean())
    # print(np.abs(dct_map_2[0]).mean())
    # print(np.abs(dct_map_conv).mean())

    dct_map = outputs.data.numpy()[0, 0]
    dct_map_conv = outputs2.data.numpy()[0, 0]

    assert outputs.shape == outputs2.shape
    assert np.abs(dct_map_conv).mean() == np.abs(dct_map).mean()
    assert np.abs(dct_map_conv).max() == np.abs(dct_map).max()
Exemple #27
0
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torch.nn.functional as F
import matplotlib.pyplot as plt
import os
from torchvision import datasets, transforms
import time
from PIL import Image
from torchvision import models

data_transforms = {
    'train': transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.Grayscale(),
        transforms.ToTensor(),
        transforms.Lambda(lambda x: torch.cat([x, x, x], 0))
        # transforms.Normalize()
    ]),
    'test': transforms.Compose([
        # transforms.Resize(256),
        transforms.CenterCrop(42),
        transforms.Grayscale(),
        transforms.ToTensor(),
        transforms.Lambda(lambda x: torch.cat([x, x, x], 0))
        # transforms.Normalize()
    ])
}

data_dir = '../Datasets/'
def get_dataloaders(
    dataset,
    root=None,
    seed=0,
    device=torch.device("cpu"),
    train_batch_size=1,
    test_batch_size=1,
    num_workers=0,
    **kwargs,
):
    """
    Args:
        dataset: string, dataset identifier (only 'mnist' supported)
        root: string (optional), folder where the data will be downloaded.
        device: (optional) a torch.device() where the datasets will be located
                (default=torch.device('cpu'))
        seed: int (optional) manual seed used for torch and cudnn
        train_batch_size: int (optional), batch size for training dataloader
                          (default: 1)
        test_batch_size: int (optional), batch size for test dataloader
                          (default: 1)
        num_workers: int (optional), how many subprocesses to use for data 
                     loading. 0 means that the data will be loaded in the 
                     main process. (default: 0)
        **kwargs: allowed arguments include
            augment
            num_channels
            resize_to

    Returns:
        train_loader: pytorch DataLoader, training set
        test_loader: pytorch DataLoader, test set

    Raises:
        ValueError if dataset.lower is not one of {'mnist', 'cifar-10', 
            'cifar-100'}

    Notes:
    Make sure to read the documentation here (https://pytorch.org/docs/stable/data.html) 
    and here (https://pytorch.org/docs/stable/notes/faq.html#dataloader-workers-random-seed) 
    when using `num_workers` != 0, and pay particular attention to the role of random 
    seeds for reproducibility and workers syncing. 

    The training dataset will be shuffled (deterministically, depending on
    seed), while the test set will not be shuffled.
    """
    # set seeds and make sure results are deterministic (at least with
    # `num_workers` set to 0)
    torch.manual_seed(seed)
    if device.type == "cuda":
        torch.cuda.manual_seed_all(seed)
        torch.backends.cudnn.deterministic = True

    # determine transformations to apply depending on dataset nature
    if dataset.lower() == "mnist":
        # basic image standardization
        train_transform = transforms.Compose(
            [
                transforms.ToTensor(),
                transforms.Normalize((0.1307,), (0.3081,)),
            ]
        )
        test_transform = transforms.Compose(
            [
                transforms.ToTensor(),
                transforms.Normalize((0.1307,), (0.3081,)),
            ]
        )

        if ("resize_to" in kwargs) and (kwargs["resize_to"] is not None):
            # TODO: check that resize_to makes sense
            resize_transform = [transforms.Resize(kwargs["resize_to"])]
            train_transform = transforms.Compose(
                resize_transform + train_transform.transforms
            )
            test_transform = transforms.Compose(
                resize_transform + test_transform.transforms
            )

        # data augmentation strategies
        if ("augment" in kwargs) and (kwargs["augment"] == True):
            augment_transforms = [
                transforms.RandomCrop(28, padding=4),
                transforms.RandomHorizontalFlip(),
            ]
            train_transform = transforms.Compose(
                augment_transforms + train_transform.transforms
            )

        # if ("broadcast_channels" in kwargs) and (
        #     kwargs["broadcast_channels"] == True
        # ):
        if ("num_channels" in kwargs) and (kwargs["num_channels"] != 1):
            broadcast_transform = [
                transforms.Grayscale(
                    num_output_channels=kwargs["num_channels"]
                )
            ]
            train_transform = transforms.Compose(
                broadcast_transform + train_transform.transforms
            )
            test_transform = transforms.Compose(
                broadcast_transform + test_transform.transforms
            )

        root = root or "/anonymized/path/"

        try:
            train_dataset = datasets.MNIST(
                root=root,
                train=True,
                download=True,
                transform=train_transform,
            )
            test_dataset = datasets.MNIST(
                root=root,
                train=False,
                download=True,
                transform=test_transform,
            )
        except OSError:
            train_dataset = datasets.MNIST(
                root=os.path.expanduser("~"),
                train=True,
                download=True,
                transform=train_transform,
            )
            test_dataset = datasets.MNIST(
                root=os.path.expanduser("~"),
                train=False,
                download=True,
                transform=test_transform,
            )

    elif dataset.lower() in ["cifar-10", "cifar-100"]:

        # Construct initial transforms
        train_transform = transforms.Compose([transforms.ToTensor()])
        test_transform = transforms.Compose([transforms.ToTensor()])

        if ("resize_to" in kwargs) and (kwargs["resize_to"] is not None):
            print(kwargs["resize_to"])
            resize_transform = [transforms.Resize(kwargs["resize_to"])]
            train_transform = transforms.Compose(
                resize_transform + train_transform.transforms
            )
            test_transform = transforms.Compose(
                resize_transform + test_transform.transforms
            )

        if ("augment" in kwargs) and (kwargs["augment"] == True):
            augment_transforms = [
                transforms.RandomCrop(32, padding=4),
                transforms.RandomHorizontalFlip(),
            ]
            train_transform = transforms.Compose(
                augment_transforms + train_transform.transforms
            )

        # if ("num_channels" in kwargs) and (kwargs["num_channels"] != 3):
        if "num_channels" in kwargs:
            if kwargs["num_channels"] == 3:
                norm_transform = [
                    transforms.Normalize(
                        (0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)
                    )
                ]
                train_transform = transforms.Compose(
                    train_transform.transforms + norm_transform
                )
                test_transform = transforms.Compose(
                    test_transform.transforms + norm_transform
                )
            elif kwargs["num_channels"] == 1:
                broadcast_transform = [
                    transforms.Grayscale(
                        num_output_channels=kwargs["num_channels"]
                    )
                ]
                norm_transform = [transforms.Normalize((0.4790,), (0.2389,))]

                train_transform = transforms.Compose(
                    broadcast_transform
                    + train_transform.transforms
                    + norm_transform
                )
                test_transform = transforms.Compose(
                    broadcast_transform
                    + train_transform.transforms
                    + norm_transform
                )
            else:
                raise ValueError(
                    "Unexpected number of channels: {}".format(
                        kwargs["num_channels"]
                    )
                )

        if dataset.lower() in ["cifar-10"]:
            root = root or "/anonymized/path/"
            logger.info(
                "Data folder for {} dataset: {}".format(dataset, root)
            )

            try:
                train_dataset = datasets.CIFAR10(
                    root=root,
                    train=True,
                    download=True,
                    transform=train_transform,
                )
                test_dataset = datasets.CIFAR10(
                    root=root,
                    train=False,
                    download=True,
                    transform=test_transform,
                )
            except OSError:
                train_dataset = datasets.CIFAR10(
                    root=os.path.expanduser("~"),
                    train=True,
                    download=True,
                    transform=train_transform,
                )
                test_dataset = datasets.CIFAR10(
                    root=os.path.expanduser("~"),
                    train=False,
                    download=True,
                    transform=test_transform,
                )

        elif dataset.lower() == "cifar-100":
            root = root or "/anonymized/path/"
            logger.info(
                "Data folder for {} dataset: {}".format(dataset, root)
            )

            try:
                train_dataset = datasets.CIFAR100(
                    root=root,
                    train=True,
                    download=True,
                    transform=train_transform,
                )
                test_dataset = datasets.CIFAR100(
                    root=root,
                    train=False,
                    download=True,
                    transform=test_transform,
                )

            except OSError:
                train_dataset = datasets.CIFAR100(
                    root=os.path.expanduser("~"),
                    train=True,
                    download=True,
                    transform=train_transform,
                )
                test_dataset = datasets.CIFAR100(
                    root=os.path.expanduser("~"),
                    train=False,
                    download=True,
                    transform=test_transform,
                )

    else:
        raise ValueError(
            '`dataset` must be one of {"mnist, cifar-10, cifar-100"}'
        )

    # set up dataloaders from the datasets above, specifying the device,
    # sampling strategy, number of processes, etc.
    train_loader = torch.utils.data.DataLoader(
        train_dataset,
        batch_size=train_batch_size,
        shuffle=True,
        num_workers=num_workers,
        pin_memory=device.type == "cuda",
    )
    test_loader = torch.utils.data.DataLoader(
        test_dataset,
        batch_size=test_batch_size,
        shuffle=False,
        num_workers=num_workers,
        pin_memory=device.type == "cuda",
    )

    return train_loader, test_loader
Exemple #29
0
        self.interaction_steps = 0

    def select_action(self, state):
        self.interaction_steps += 1
        return torch.tensor([random.sample([0, 1, 2], 1)],
                            device=device,
                            dtype=torch.long)

    def evaluate_action(self, state):
        return torch.tensor([random.sample([0, 1, 2], 1)],
                            device=device,
                            dtype=torch.long)


frame_proc = T.Compose([T.ToPILImage(),
                        T.Grayscale(), \
                        T.Resize((84,84), interpolation=Image.BILINEAR), \
                        T.ToTensor()])


class Atari(object):
    def __init__(self,
                 env_name="FreewayDeterministic-v4",
                 agent_history_length=4):
        self.env = gym.make(env_name)
        self.state = None
        self.agent_history_length = agent_history_length

    def reset(self):
        observation = self.env.reset()
        frame = self.image_proc(observation).to(device)
Exemple #30
0
    plt.ylabel('Reward')
    plt.grid()
    plt.legend()

    plt.savefig('random.png')


def select_action(state):
    global steps_done
    steps_done += 1
    return torch.tensor([[random.randrange(3)]],
                        device=device,
                        dtype=torch.long)


transform1 = T.Compose([T.ToPILImage(), T.CenterCrop(160), T.Grayscale()])
transform2 = T.Compose([T.Resize((84, 84)), T.ToTensor()])


def get_state(obs, prev_state=None):
    state = np.array(obs)
    state = state.transpose((2, 0, 1))
    state = torch.from_numpy(state)
    # print(state.shape)
    state = transform1(state)
    state = T.functional.adjust_contrast(state, 10)
    state = transform2(state)
    if prev_state is None:
        state = torch.cat([state, state, state], 0)
    else:
        state = torch.cat([prev_state.squeeze(0)[-2:], state], 0)