Beispiel #1
0
def get_datasets(dataset_name, noise_type, noise_rate):
    if (dataset_name == "MNIST"):
        train_dataset = MNIST(root='./data/',
                              download=True,
                              train=True,
                              transform=transforms.ToTensor(),
                              noise_type=noise_type,
                              noise_rate=noise_rate)
        test_dataset = MNIST(root='./data/',
                             download=True,
                             train=False,
                             transform=transforms.ToTensor(),
                             noise_type=noise_type,
                             noise_rate=noise_rate)
    elif (dataset_name == "CIFAR10"):
        train_dataset = CIFAR10(root='./data/',
                                download=True,
                                train=True,
                                transform=transforms.ToTensor(),
                                noise_type=noise_type,
                                noise_rate=noise_rate)
        test_dataset = CIFAR10(root='./data/',
                               download=True,
                               train=False,
                               transform=transforms.ToTensor(),
                               noise_type=noise_type,
                               noise_rate=noise_rate)
    elif (dataset_name == "CIFAR100"):
        train_dataset = CIFAR100(root='./data/',
                                 download=True,
                                 train=True,
                                 transform=transforms.ToTensor(),
                                 noise_type=noise_type,
                                 noise_rate=noise_rate)
        test_dataset = CIFAR100(root='./data/',
                                download=True,
                                train=False,
                                transform=transforms.ToTensor(),
                                noise_type=noise_type,
                                noise_rate=noise_rate)
    # elif(dataset_name == "Clothes1M"):
    # 	train_dataset, test_dataset =
    return train_dataset, test_dataset
Beispiel #2
0
def get_val_dataset(p, transform=None, to_neighbors_dataset=False):
    # Base dataset
    if p['val_db_name'] == 'cifar-10':
        from data.cifar import CIFAR10
        dataset = CIFAR10(train=False, transform=transform, download=True)

    elif p['val_db_name'] == 'cifar-20':
        from data.cifar import CIFAR20
        dataset = CIFAR20(train=False, transform=transform, download=True)

    elif p['train_db_name'] == 'imbalanced-cifar-10':
        from data.imbalanced_cifar import IMBALANCECIFAR10
        dataset = IMBALANCECIFAR10(train=False, transform=transform)

    elif p['train_db_name'] == 'imbalanced-cifar-100':
        from data.imbalanced_cifar import IMBALANCECIFAR100
        dataset = IMBALANCECIFAR100(train=False, transform=transform)

    elif p['val_db_name'] == 'stl-10':
        from data.stl import STL10
        dataset = STL10(split='test', transform=transform, download=True)

    elif p['val_db_name'] == 'imagenet':
        from data.imagenet import ImageNet
        dataset = ImageNet(split='val', transform=transform)

    elif p['val_db_name'] in ['imagenet_50', 'imagenet_100', 'imagenet_200']:
        from data.imagenet import ImageNetSubset
        subset_file = './data/imagenet_subsets/%s.txt' % (p['val_db_name'])
        dataset = ImageNetSubset(subset_file=subset_file,
                                 split='val',
                                 transform=transform)

    elif p['train_db_name'] == 'tinyimagenet':
        from data.tinyimagenet import TinyImageNet
        dataset = TinyImageNet(split='val', transform=transform)

    elif p['train_db_name'] == "mnist":
        from data.mnist import MNIST
        dataset = MNIST(root='../data/mnist/',
                        train=False,
                        transform=transform,
                        download=True)

    else:
        raise ValueError('Invalid validation dataset {}'.format(
            p['val_db_name']))

    # Wrap into other dataset (__getitem__ changes)
    if to_neighbors_dataset:  # Dataset returns an image and one of its nearest neighbors.
        from data.custom_dataset import NeighborsDataset
        indices = np.load(p['topk_neighbors_val_path'])
        dataset = NeighborsDataset(dataset, indices, 5)  # Only use 5

    return dataset
Beispiel #3
0
def get_train_dataset(p,
                      transform,
                      to_augmented_dataset=False,
                      to_neighbors_dataset=False,
                      split=None):
    # Base dataset
    if p['train_db_name'] == 'cifar-10':
        from data.cifar import CIFAR10
        dataset = CIFAR10(train=True, transform=transform, download=True)

    elif p['train_db_name'] == 'cifar-20':
        from data.cifar import CIFAR20
        dataset = CIFAR20(train=True, transform=transform, download=True)

    elif p['train_db_name'] == 'stl-10':
        from data.stl import STL10
        dataset = STL10(split=split, transform=transform, download=True)

    elif p['train_db_name'] == 'mnist':
        from data.mnist import MNIST
        dataset = MNIST(train=True, transform=transform, download=True)

    elif p['train_db_name'] == 'imagenet':
        from data.imagenet import ImageNet
        dataset = ImageNet(split='train', transform=transform)

    elif p['train_db_name'] in ['imagenet_50', 'imagenet_100', 'imagenet_200']:
        from data.imagenet import ImageNetSubset
        subset_file = './data/imagenet_subsets/%s.txt' % (p['train_db_name'])
        dataset = ImageNetSubset(subset_file=subset_file,
                                 split='train',
                                 transform=transform)

    # added birds train dataset
    elif p['train_db_name'] == 'bird':
        from data.bird import Birds
        dataset = Birds(split='train', transform=transform)

    else:
        raise ValueError('Invalid train dataset {}'.format(p['train_db_name']))

    # Wrap into other dataset (__getitem__ changes)
    if to_augmented_dataset:  # Dataset returns an image and an augmentation of that image.
        from data.custom_dataset import AugmentedDataset
        dataset = AugmentedDataset(dataset)

    if to_neighbors_dataset:  # Dataset returns an image and one of its nearest neighbors.
        from data.custom_dataset import NeighborsDataset
        indices = np.load(p['topk_neighbors_train_path'])
        dataset = NeighborsDataset(dataset, indices, p['num_neighbors'])

    return dataset
Beispiel #4
0
    def getData(self):
        mnist = MNIST()
        mnist.load()
        mnist.preprocess_dataset()

        self.x_train = mnist.x_train
        self.x_test = mnist.x_test
        self.y_train = mnist.y_train
        self.y_test = mnist.y_test
def load_dataset(config, augment):
    if config.dataset == "mnist":
        data_provider = MNIST(config, augment)
    elif config.dataset == "imagenette":
        data_provider = Imagenette(config, augment)
    elif config.dataset == "svhn":
        data_provider = Svhn(config, augment)
    elif config.dataset == "cifar":
        data_provider = Cifar(config, augment)
        
    else:
        raise Exception("Unknown dataset %s" % config.dataset)

    return data_provider.load_dataset()
Beispiel #6
0
def main():
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')

    parser.add_argument('--result_dir',
                        type=str,
                        help='dir to save result txt files',
                        default='results/')
    parser.add_argument('--noise_rate',
                        type=float,
                        help='corruption rate, should be less than 1',
                        default=0.5)
    parser.add_argument('--forget_rate',
                        type=float,
                        help='forget rate',
                        default=None)
    parser.add_argument('--noise_type',
                        type=str,
                        help='[pairflip, symmetric]',
                        default='symmetric')
    parser.add_argument(
        '--num_gradual',
        type=int,
        default=10,
        help=
        'how many epochs for linear drop rate, can be 5, 10, 15. This parameter is equal to Tk for R(T) in Co-teaching paper.'
    )
    parser.add_argument(
        '--exponent',
        type=float,
        default=1,
        help=
        'exponent of the forget rate, can be 0.5, 1, 2. This parameter is equal to c in Tc for R(T) in Co-teaching paper.'
    )
    parser.add_argument('--top_bn', action='store_true')
    parser.add_argument('--dataset',
                        type=str,
                        help='mnist, cifar10, or cifar100',
                        default='mnist')
    parser.add_argument('--n_epoch', type=int, default=10)
    parser.add_argument('--seed', type=int, default=1)
    parser.add_argument('--print_freq', type=int, default=50)
    parser.add_argument('--num_workers',
                        type=int,
                        default=4,
                        help='how many subprocesses to use for data loading')
    parser.add_argument('--num_iter_per_epoch', type=int, default=400)
    parser.add_argument('--epoch_decay_start', type=int, default=80)
    parser.add_argument('--eps', type=float, default=9.9)
    parser.add_argument('--load_model', type=str, default="")

    parser.add_argument('--batch-size',
                        type=int,
                        default=1000,
                        metavar='N',
                        help='input batch size for training (default: 256)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=4000,
                        metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.9,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=100,
        metavar='N',
        help='how many batches to wait before logging training status')

    parser.add_argument('--save-model',
                        action='store_true',
                        default=False,
                        help='For Saving the current Model')
    args = parser.parse_args()

    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    batch_size = args.batch_size

    if args.dataset == 'mnist':
        input_channel = 1
        num_classes = 10
        args.top_bn = False
        args.epoch_decay_start = 80
        args.n_epoch = 200
        train_dataset = MNIST(root='./data/',
                              download=True,
                              train=True,
                              transform=transforms.ToTensor(),
                              noise_type=args.noise_type,
                              noise_rate=args.noise_rate)

        test_dataset = MNIST(root='./data/',
                             download=True,
                             train=False,
                             transform=transforms.ToTensor(),
                             noise_type=args.noise_type,
                             noise_rate=args.noise_rate)

    if args.dataset == 'cifar10':
        input_channel = 3
        num_classes = 10
        args.top_bn = False
        args.epoch_decay_start = 80
        args.n_epoch = 200
        train_dataset = CIFAR10(root='./data/',
                                download=True,
                                train=True,
                                transform=transforms.ToTensor(),
                                noise_type=args.noise_type,
                                noise_rate=args.noise_rate)

        test_dataset = CIFAR10(root='./data/',
                               download=True,
                               train=False,
                               transform=transforms.ToTensor(),
                               noise_type=args.noise_type,
                               noise_rate=args.noise_rate)
    if args.dataset == 'openimages':
        input_channel = 3
        num_classes = 16
        args.top_bn = False
        args.epoch_decay_start = 80
        openimages_transform = transforms.Compose([
            transforms.Resize((299, 299)),
            transforms.ToTensor(),
            transforms.Normalize([0, 0, 0], [1, 1, 1])
        ])
        train_dataset = datasets.ImageFolder(
            "/home/paul/noisy_labels/OIDv4_ToolKit/OID/Dataset_nl/train",
            transform=openimages_transform)
        test_dataset = datasets.ImageFolder(
            "/home/paul/noisy_labels/OIDv4_ToolKit/OID/Dataset_nl/test",
            transform=openimages_transform)
        print("Train Dataset: {}\n Test Dataset: {}".format(
            len(train_dataset), len(test_dataset)))
        # test_dataset, _ = torch.utils.data.random_split(test_dataset, [100, len(test_dataset)-100])
        # test_dataset = datasets.ImageFolder("/Users/cherry/Documents/OIDv4_ToolKit/OID/Dataset_nl/train", transform=openimages_transform)

    if args.forget_rate is None:
        forget_rate = args.noise_rate
    else:
        forget_rate = args.forget_rate

    # noise_or_not = train_dataset.noise_or_not
    # Data Loader (Input Pipeline)
    print('loading dataset...')
    train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                               batch_size=args.batch_size,
                                               num_workers=args.num_workers,
                                               drop_last=True,
                                               shuffle=True)

    test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                              batch_size=batch_size,
                                              num_workers=args.num_workers,
                                              drop_last=True,
                                              shuffle=False)
    # Define models
    #print('building model...')
    #cnn1 = CNN(input_channel=input_channel, n_outputs=num_classes+1)
    #cnn1.cuda()
    #print(cnn1.parameters)
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    print("using {}".format(device))
    # cnn1 = Net().to(device)
    cnn1 = inception.inception_v3(pretrained=False,
                                  num_classes=num_classes + 1,
                                  aux_logits=False).to(device)
    #cnn1=nn.DataParallel(cnn1,device_ids=[0,1,2,3]).cuda()
    #print(model.parameters)
    #optimizer1 = torch.optim.SGD(cnn1.parameters(), lr=learning_rate)
    #optimizer = torch.optim.Adam(cnn1.parameters(), lr=args.lr)

    optimizer = torch.optim.Adam(cnn1.parameters(), lr=args.lr)
    #optimizer = nn.DataParallel(optimizer, device_ids=[0,1,2,3])

    acc = []
    loss = []
    loss_pure = []
    loss_corrupt = []
    out = []
    eee = 1 - args.noise_rate
    criteria = (-1) * (eee * np.log(eee) + (1 - eee) * np.log(
        (1 - eee) / (args.eps - 1)))
    name = str(args.dataset) + "_" + str(args.noise_type) + "_" + str(
        args.noise_rate) + "_" + str(args.eps) + "_" + str(args.seed)

    if (args.load_model != ""):
        checkpoint = torch.load(args.load_model)
        cnn1.load_state_dict(checkpoint['model_state_dict'])
        optimizer.load_state_dict(checkpoint['optimizer_state_dict'])

        acc = list(checkpoint['test_acc'])
        loss = list(checkpoint['loss'])

    for epoch in range(1, args.n_epoch + 1):
        l1, out10 = train(args,
                          cnn1,
                          device,
                          train_loader,
                          optimizer,
                          epoch,
                          eps=args.eps,
                          nums=num_classes)
        loss.append(l1)
        out.append(out10)
        acc.append(test(args, cnn1, device, test_loader, num_classes))
        print(l1, criteria)
        # if l1<criteria:
        #     break;
        torch.save(
            {
                'model_state_dict': cnn1.state_dict(),
                'optimizer_state_dict': optimizer.state_dict(),
                'loss': loss,
                'test_acc': acc
            }, "early_stopping/" + name + "_model.npy")
    save_dir = "early_stoppig"
    if not os.path.exists(save_dir):
        os.system('mkdir -p %s' % save_dir)

    print(name)
    np.save("early_stopping/" + name + "_acc.npy", acc)
    np.save("early_stopping/" + name + "_loss.npy", loss)
Beispiel #7
0
def main():
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')

    parser.add_argument('--result_dir',
                        type=str,
                        help='dir to save result txt files',
                        default='results/')
    parser.add_argument('--noise_rate',
                        type=float,
                        help='corruption rate, should be less than 1',
                        default=0.5)
    parser.add_argument('--forget_rate',
                        type=float,
                        help='forget rate',
                        default=None)
    parser.add_argument('--noise_type',
                        type=str,
                        help='[pairflip, symmetric]',
                        default='symmetric')
    parser.add_argument(
        '--num_gradual',
        type=int,
        default=10,
        help=
        'how many epochs for linear drop rate, can be 5, 10, 15. This parameter is equal to Tk for R(T) in Co-teaching paper.'
    )
    parser.add_argument(
        '--exponent',
        type=float,
        default=1,
        help=
        'exponent of the forget rate, can be 0.5, 1, 2. This parameter is equal to c in Tc for R(T) in Co-teaching paper.'
    )
    parser.add_argument('--top_bn', action='store_true')
    parser.add_argument('--dataset',
                        type=str,
                        help='mnist, cifar10, or cifar100',
                        default='mnist')
    parser.add_argument('--n_epoch', type=int, default=10)
    parser.add_argument('--seed', type=int, default=1)
    parser.add_argument('--print_freq', type=int, default=50)
    parser.add_argument('--num_workers',
                        type=int,
                        default=4,
                        help='how many subprocesses to use for data loading')
    parser.add_argument('--num_iter_per_epoch', type=int, default=400)
    parser.add_argument('--epoch_decay_start', type=int, default=80)
    parser.add_argument('--eps', type=float, default=9.9)

    parser.add_argument('--batch-size',
                        type=int,
                        default=1000,
                        metavar='N',
                        help='input batch size for training (default: 256)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=4000,
                        metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.005,
                        metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.9,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=100,
        metavar='N',
        help='how many batches to wait before logging training status')

    parser.add_argument('--save-model',
                        action='store_true',
                        default=False,
                        help='For Saving the current Model')
    args = parser.parse_args()

    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    batch_size = args.batch_size

    if args.dataset == 'mnist':
        input_channel = 1
        num_classes = 10
        args.top_bn = False
        args.epoch_decay_start = 80
        args.n_epoch = 200
        train_dataset = MNIST(root='./data/',
                              download=True,
                              train=True,
                              transform=transforms.ToTensor(),
                              noise_type=args.noise_type,
                              noise_rate=args.noise_rate)

        test_dataset = MNIST(root='./data/',
                             download=True,
                             train=False,
                             transform=transforms.ToTensor(),
                             noise_type=args.noise_type,
                             noise_rate=args.noise_rate)

    if args.dataset == 'cifar10':
        input_channel = 3
        num_classes = 10
        args.top_bn = False
        args.epoch_decay_start = 80
        args.n_epoch = 200
        train_dataset = CIFAR10(root='./data/',
                                download=True,
                                train=True,
                                transform=transforms.ToTensor(),
                                noise_type=args.noise_type,
                                noise_rate=args.noise_rate)

        test_dataset = CIFAR10(root='./data/',
                               download=True,
                               train=False,
                               transform=transforms.ToTensor(),
                               noise_type=args.noise_type,
                               noise_rate=args.noise_rate)

    if args.forget_rate is None:
        forget_rate = args.noise_rate
    else:
        forget_rate = args.forget_rate

    noise_or_not = train_dataset.noise_or_not
    # Data Loader (Input Pipeline)
    print('loading dataset...')
    train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                               batch_size=args.batch_size,
                                               num_workers=args.num_workers,
                                               drop_last=True,
                                               shuffle=True)

    test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                              batch_size=batch_size,
                                              num_workers=args.num_workers,
                                              drop_last=True,
                                              shuffle=False)
    # Define models
    #print('building model...')
    #cnn1 = CNN(input_channel=input_channel, n_outputs=num_classes+1)
    #cnn1.cuda()
    #print(cnn1.parameters)
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    cnn1 = Net().to(device)
    #cnn1=nn.DataParallel(cnn1,device_ids=[0,1,2,3]).cuda()
    #print(model.parameters)
    #optimizer1 = torch.optim.SGD(cnn1.parameters(), lr=learning_rate)
    #optimizer = torch.optim.Adam(cnn1.parameters(), lr=args.lr)

    optimizer = torch.optim.SGD(cnn1.parameters(),
                                lr=args.lr,
                                momentum=args.momentum)
    #optimizer = nn.DataParallel(optimizer, device_ids=[0,1,2,3])

    acc = []
    loss = []
    loss_pure = []
    loss_corrupt = []
    out = []
    eee = 1 - args.noise_rate
    criteria = (-1) * (eee * np.log(eee) + (1 - eee) * np.log(
        (1 - eee) / (args.eps - 1)))
    for epoch in range(1, args.n_epoch + 1):
        l1, out10 = train(args,
                          cnn1,
                          device,
                          train_loader,
                          optimizer,
                          epoch,
                          eps=args.eps,
                          nums=num_classes)
        loss.append(l1)
        out.append(out10)
        acc.append(test(args, cnn1, device, test_loader, num_classes))
        print(l1, criteria)
        if l1 < criteria:
            break
    save_dir = "early_stoppig"
    if not os.path.exists(save_dir):
        os.system('mkdir -p %s' % save_dir)

    name = str(args.dataset) + " " + str(args.noise_type) + " " + str(
        args.noise_rate) + " " + str(args.eps) + " " + str(args.seed)
    np.save("early_stopping/" + name + " acc.npy", acc)
    np.save("early_stopping/" + name + " loss.npy", loss)
def training(args):
    args.use_gpu = torch.cuda.is_available()
    #os.environ["CUDA_VISIBLE_DEVICES"]=args.cuda_devices
    torch.manual_seed('1')

    args.save_dir = osp.join(args.save_dir, args.data, args.model,
                             str(args.custom_conv) + 'tiny')
    print(args.save_dir)
    sys.stdout = Logger(osp.join(args.save_dir, 'log_' + '.txt'))
    print_config(args)

    if args.use_gpu:
        print("Currently using GPU: {}".format(args.cuda_devices))
        cudnn.benchmark = True
        torch.cuda.manual_seed_all('0')
    else:
        print("Currently using CPU")
    print("Creating dataset: {}".format(args.data))

    if args.data == 'mnist':
        dataset = MNIST(args.batch_size, args.use_gpu, args.workers)
    elif args.data == 'cifar10':
        dataset = CIFAR10(args.batch_size, args.use_gpu, args.workers)
    trainloader, testloader = dataset.trainloader, dataset.testloader

    args.num_classes = dataset.num_classes

    if args.model == 'lenetpp':
        model = LeNetpp(args.num_classes, custom_conv=args.custom_conv)
    elif args.model == 'mobilenetv2':
        model = MobileNetV2(num_classes=args.num_classes,
                            custom=args.custom_conv)
    print("Creating model: {}".format(args.model))
    #model.load_state_dict(torch.load('work_dir/adaptive_3/cifar10/mobilenetv2/True1/weights/LeNet_epoch_100.pth'))
    if args.use_gpu:
        model = model.cuda()

    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(),
                                lr=args.lr,
                                weight_decay=5e-04,
                                momentum=0.9)

    if args.stepsize > 0:
        scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                    step_size=args.stepsize,
                                                    gamma=args.gamma)

    acc, err = test(args, model, trainloader, 0)
    print("Accuracy (%): {}\t Error rate(%): {}".format(acc, err))

    start_time = time.time()
    for epoch in range(args.max_epoch):
        print("==> Epoch {}/{}".format(epoch + 1, args.max_epoch))
        train(args, model, criterion, optimizer, trainloader, epoch)

        if args.stepsize > 0:
            scheduler.step()

        if args.eval_freq > 0 and (epoch + 1) % args.eval_freq == 0 or (
                epoch + 1) == args.max_epoch:
            print("==> Train")
            acc, err = test(args, model, trainloader, epoch)
            print("Accuracy (%): {}\t Error rate(%): {}".format(acc, err))
            print("==> Test")
            acc, err = test(args, model, testloader, epoch)
            print("Accuracy (%): {}\t Error rate(%): {}".format(acc, err))
            save_model(model, epoch, name='LeNet_', save_dir=args.save_dir)

    elapsed = round(time.time() - start_time)
    elapsed = str(datetime.timedelta(seconds=elapsed))
    print("Finished. Total elapsed time (h:m:s): {}".format(elapsed))
Beispiel #9
0
def get_datasets():
	train_dataset = MNIST(root='./data/',download=True,  train=True, transform=transforms.ToTensor(),noise_type=noise_type,noise_rate=noise_rate)
	test_dataset = MNIST(root='./data/',download=True,  train=False, transform=transforms.ToTensor(),noise_type=noise_type,noise_rate=noise_rate)
	return train_dataset, test_dataset
Beispiel #10
0
def main():
    parser = argparse.ArgumentParser(
        description='PyTorch Gambler\'s Loss Runner')

    parser.add_argument('--result_dir',
                        type=str,
                        help='directory to save result txt files',
                        default='results')
    parser.add_argument('--gpu', type=int, help='gpu index', default=0)
    parser.add_argument('--noise_rate',
                        type=float,
                        help='corruption rate, should be less than 1',
                        default=0.5)
    parser.add_argument('--noise_type',
                        type=str,
                        help='[pairflip, symmetric]',
                        default='symmetric')
    parser.add_argument('--dataset',
                        type=str,
                        help='mnist, cifar10, or imdb',
                        default='mnist')
    parser.add_argument('--n_epoch', type=int, default=10)
    parser.add_argument('--seed', type=int, default=1)
    parser.add_argument('--num_workers',
                        type=int,
                        default=4,
                        help='how many subprocesses to use for data loading')
    parser.add_argument('--epoch_decay_start', type=int, default=80)
    parser.add_argument('--load_model', type=str, default="")

    parser.add_argument('--batch_size',
                        type=int,
                        default=128,
                        metavar='N',
                        help='input batch size for training (default: 128)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=100,
        metavar='N',
        help=
        'how many batches to wait before logging training status (default: 100)'
    )

    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        metavar='LR',
                        help='learning rate (default: 0.001)')
    parser.add_argument('--start_method',
                        type=int,
                        help='number of epochs before starting method',
                        default=0)

    # label smoothing args
    parser.add_argument('--smoothing',
                        type=float,
                        default=1.0,
                        help='smoothing parameter (default: 1)')
    parser.add_argument('--optimal_smoothing',
                        action='store_true',
                        default=False)
    parser.add_argument('--scale_lr',
                        type=float,
                        default=0.0,
                        help='exponent to scale learning rate by')

    parser.add_argument('--method',
                        type=str,
                        help='[nll, smoothing, fsmoothing]',
                        default="nll")

    args = parser.parse_args()

    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    if args.dataset == 'mnist':
        input_channel = 1
        num_classes = 10
        train_dataset = MNIST(root='./data/',
                              download=True,
                              train=True,
                              transform=transforms.ToTensor(),
                              noise_type=args.noise_type,
                              noise_rate=args.noise_rate)
        test_dataset = MNIST(root='./data/',
                             download=True,
                             train=False,
                             transform=transforms.ToTensor(),
                             noise_type=args.noise_type,
                             noise_rate=args.noise_rate)
        print('loading dataset...')
        train_loader = torch.utils.data.DataLoader(
            dataset=train_dataset,
            batch_size=args.batch_size,
            num_workers=args.num_workers,
            drop_last=True,
            shuffle=True)
        test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                                  batch_size=args.batch_size,
                                                  num_workers=args.num_workers,
                                                  drop_last=True,
                                                  shuffle=False)

    if args.dataset == 'cifar10':
        input_channel = 3
        num_classes = 10
        transform_train = transforms.Compose([
            transforms.RandomCrop(32, padding=4),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize((0.4914, 0.4822, 0.4465),
                                 (0.2023, 0.1994, 0.2010)),
        ])

        transform_test = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.4914, 0.4822, 0.4465),
                                 (0.2023, 0.1994, 0.2010)),
        ])
        train_dataset = CIFAR10(root='./data/',
                                download=True,
                                train=True,
                                transform=transform_train,
                                noise_type=args.noise_type,
                                noise_rate=args.noise_rate)
        test_dataset = CIFAR10(root='./data/',
                               download=True,
                               train=False,
                               transform=transform_test,
                               noise_type=args.noise_type,
                               noise_rate=args.noise_rate)
        print('loading dataset...')
        train_loader = torch.utils.data.DataLoader(
            dataset=train_dataset,
            batch_size=args.batch_size,
            num_workers=args.num_workers,
            drop_last=True,
            shuffle=True)
        test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                                  batch_size=args.batch_size,
                                                  num_workers=args.num_workers,
                                                  drop_last=True,
                                                  shuffle=False)

    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda:{}".format(args.gpu) if (
        use_cuda and args.gpu >= 0) else "cpu")
    print("using {}".format(device))

    args.lr *= ((0.9 / (args.smoothing - 0.1))**args.scale_lr)
    print("learning rate scaled to {}".format(args.lr))

    print('building model...')
    if args.dataset == 'mnist':
        model = cnns.CNN_basic(num_classes=num_classes).to(device)
        optimizer = torch.optim.SGD(model.parameters(),
                                    lr=args.lr,
                                    momentum=0.9)
    if args.dataset == 'cifar10':
        model = resnet.ResNet18().to(device)
        optimizer = torch.optim.SGD(model.parameters(),
                                    lr=args.lr,
                                    momentum=0.9)

    test_accs = []
    train_losses = []
    test_losses = []
    out = []

    if args.optimal_smoothing:
        clean_rate = 1 - args.noise_rate
        args.smoothing = (1 - (2 * clean_rate) + clean_rate * clean_rate *
                          num_classes) / (num_classes - 1)
        print("Using smoothing parameter {:.2f} for clean rate {:.2f}".format(
            args.smoothing, clean_rate))

    name = "{}_{}_{}_{:.2f}_{:.2f}_{}".format(args.dataset, args.method,
                                              args.noise_type, args.smoothing,
                                              args.noise_rate, args.seed)

    if not os.path.exists(args.result_dir):
        os.system('mkdir -p %s' % args.result_dir)

    for epoch in range(1, args.n_epoch + 1):
        for param_group in optimizer.param_groups:
            print(epoch, param_group['lr'])
        print(name)
        train_loss = train(args,
                           model,
                           device,
                           train_loader,
                           optimizer,
                           epoch,
                           num_classes=num_classes,
                           use_method=(epoch >= args.start_method),
                           text=(args.dataset == 'imdb'))
        train_losses.append(train_loss)
        test_acc, test_loss = test(args,
                                   model,
                                   device,
                                   test_loader,
                                   num_classes,
                                   text=(args.dataset == 'imdb'))
        test_accs.append(test_acc)
        test_losses.append(test_loss)

    save_data = {
        "command": " ".join(sys.argv),
        "train_loss": train_losses,
        "test_loss": test_losses,
        "test_acc": test_accs
    }
    save_file = args.result_dir + "/" + name + ".json"
    json.dump(save_data, open(save_file, 'w'))
def main():
    parser = argparse.ArgumentParser(
        description='PyTorch Gambler\'s Loss Runner')

    parser.add_argument('--result_dir',
                        type=str,
                        help='directory to save result txt files',
                        default='results')
    parser.add_argument('--noise_rate',
                        type=float,
                        help='corruption rate, should be less than 1',
                        default=0.5)
    parser.add_argument('--noise_type',
                        type=str,
                        help='[pairflip, symmetric]',
                        default='symmetric')
    parser.add_argument('--dataset',
                        type=str,
                        help='mnist, cifar10, or imdb',
                        default='mnist')
    parser.add_argument('--n_epoch', type=int, default=10)
    parser.add_argument('--seed', type=int, default=1)
    parser.add_argument('--num_workers',
                        type=int,
                        default=4,
                        help='how many subprocesses to use for data loading')
    parser.add_argument('--epoch_decay_start', type=int, default=80)
    parser.add_argument('--load_model', type=str, default="")
    parser.add_argument('--model', type=str, default='default')
    parser.add_argument('--batch_size',
                        type=int,
                        default=128,
                        metavar='N',
                        help='input batch size for training (default: 128)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=100,
        metavar='N',
        help=
        'how many batches to wait before logging training status (default: 100)'
    )

    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        metavar='LR',
                        help='learning rate (default: 0.001)')
    parser.add_argument('--eps',
                        type=float,
                        help='set lambda for lambda type \'gmblers\' only',
                        default=1000.0)
    parser.add_argument('--lambda_type',
                        type=str,
                        help='[nll, euc, mid, exp, gmblers]',
                        default="euc")
    parser.add_argument('--start_gamblers',
                        type=int,
                        help='number of epochs before starting gamblers',
                        default=0)

    # label smoothing args
    parser.add_argument('--smoothing',
                        type=float,
                        default=1.0,
                        help='smoothing parameter (default: 1)')

    args = parser.parse_args()
    args.use_scheduler = False

    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    if args.dataset == 'mnist':
        input_channel = 1
        num_classes = 10
        train_dataset = MNIST(root='./data/',
                              download=True,
                              train=True,
                              transform=transforms.ToTensor(),
                              noise_type=args.noise_type,
                              noise_rate=args.noise_rate)
        test_dataset = MNIST(root='./data/',
                             download=True,
                             train=False,
                             transform=transforms.ToTensor(),
                             noise_type=args.noise_type,
                             noise_rate=args.noise_rate)
        print('loading dataset...')
        train_loader = torch.utils.data.DataLoader(
            dataset=train_dataset,
            batch_size=args.batch_size,
            num_workers=args.num_workers,
            drop_last=True,
            shuffle=True)
        test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                                  batch_size=args.batch_size,
                                                  num_workers=args.num_workers,
                                                  drop_last=True,
                                                  shuffle=False)

    if args.dataset == 'cifar10':
        input_channel = 3
        num_classes = 10
        transform_train = transforms.Compose([
            transforms.RandomCrop(32, padding=4),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize((0.4914, 0.4822, 0.4465),
                                 (0.2023, 0.1994, 0.2010)),
        ])

        transform_test = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.4914, 0.4822, 0.4465),
                                 (0.2023, 0.1994, 0.2010)),
        ])
        train_dataset = CIFAR10(root='./data/',
                                download=True,
                                train=True,
                                transform=transform_train,
                                noise_type=args.noise_type,
                                noise_rate=args.noise_rate)
        test_dataset = CIFAR10(root='./data/',
                               download=True,
                               train=False,
                               transform=transform_test,
                               noise_type=args.noise_type,
                               noise_rate=args.noise_rate)
        print('loading dataset...')
        train_loader = torch.utils.data.DataLoader(
            dataset=train_dataset,
            batch_size=args.batch_size,
            num_workers=args.num_workers,
            drop_last=True,
            shuffle=True)
        test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                                  batch_size=args.batch_size,
                                                  num_workers=args.num_workers,
                                                  drop_last=True,
                                                  shuffle=False)

    if args.dataset == 'cifar100':
        input_channel = 3
        num_classes = 100
        transform_train = transforms.Compose([
            transforms.RandomCrop(32, padding=4),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize((0.4914, 0.4822, 0.4465),
                                 (0.2023, 0.1994, 0.2010)),
        ])

        transform_test = transforms.Compose([
            transforms.ToTensor(),
            transforms.Normalize((0.4914, 0.4822, 0.4465),
                                 (0.2023, 0.1994, 0.2010)),
        ])
        train_dataset = CIFAR100(root='./data/',
                                 download=True,
                                 train=True,
                                 transform=transform_train,
                                 noise_type=args.noise_type,
                                 noise_rate=args.noise_rate)
        test_dataset = CIFAR100(root='./data/',
                                download=True,
                                train=False,
                                transform=transform_test,
                                noise_type=args.noise_type,
                                noise_rate=args.noise_rate)
        print('loading dataset...')
        train_loader = torch.utils.data.DataLoader(
            dataset=train_dataset,
            batch_size=args.batch_size,
            num_workers=args.num_workers,
            drop_last=True,
            shuffle=True)
        test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                                  batch_size=args.batch_size,
                                                  num_workers=args.num_workers,
                                                  drop_last=True,
                                                  shuffle=False)

    if args.dataset == 'imdb':
        num_classes = 2
        embedding_length = 300
        hidden_size = 256
        print('loading dataset...')
        TEXT, vocab_size, word_embeddings, train_loader, valid_iter, test_loader = load_data.load_dataset(
            rate=args.noise_rate, batch_size=args.batch_size)

    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    print("using {}".format(device))

    print('building model...')
    if args.dataset == 'mnist':
        model = CNN_basic(num_classes=num_classes).to(device)
        optimizer = torch.optim.SGD(model.parameters(), lr=args.lr)
    if args.dataset == 'cifar10':
        if args.model == 'small':
            model = CNN_small(num_classes=num_classes).to(device)
            optimizer = torch.optim.SGD(model.parameters(), lr=args.lr)
        else:
            model = resnet.ResNet18(num_classes=num_classes).to(device)
            change_lr = lambda epoch: 0.1 if epoch >= 50 else 1.0
            optimizer = LaProp(filter(lambda p: p.requires_grad,
                                      model.parameters()),
                               lr=4e-4)
            scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer,
                                                          lr_lambda=change_lr)
            args.use_scheduler = True
    if args.dataset == 'cifar100':
        if args.model == 'small':
            model = CNN_small(num_classes=num_classes).to(device)
            optimizer = torch.optim.SGD(model.parameters(), lr=args.lr)
        else:
            model = resnet.ResNet18(num_classes=num_classes).to(device)
            change_lr = lambda epoch: 0.1 if epoch >= 50 else 1.0
            optimizer = LaProp(filter(lambda p: p.requires_grad,
                                      model.parameters()),
                               lr=4e-4)
            scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer,
                                                          lr_lambda=change_lr)
            args.use_scheduler = True
    if args.dataset == 'imdb':
        model = LSTMClassifier(args.batch_size, num_classes, hidden_size,
                               vocab_size, embedding_length,
                               word_embeddings).to(device)
        optimizer = LaProp(filter(lambda p: p.requires_grad,
                                  model.parameters()),
                           lr=args.lr)

    test_accs = []
    train_losses = []
    test_losses = []
    out = []

    name = "{}_{}_{:.2f}_{:.2f}_{}_{}".format(args.dataset, args.noise_type,
                                              args.smoothing, args.noise_rate,
                                              args.eps, args.seed)

    if not os.path.exists(args.result_dir):
        os.system('mkdir -p %s' % args.result_dir)

    save_file = args.result_dir + "/" + name + ".json"
    if os.path.exists(save_file):
        print('case processed')
        exit()

    for epoch in range(1, args.n_epoch + 1):
        for param_group in optimizer.param_groups:
            print(epoch, param_group['lr'])
        print(name)
        train_loss = train(args,
                           model,
                           device,
                           train_loader,
                           optimizer,
                           epoch,
                           num_classes=num_classes,
                           use_gamblers=(epoch >= args.start_gamblers),
                           text=(args.dataset == 'imdb'))
        train_losses.append(train_loss)
        test_acc, test_loss = test(args,
                                   model,
                                   device,
                                   test_loader,
                                   num_classes,
                                   text=(args.dataset == 'imdb'))
        test_accs.append(test_acc)
        test_losses.append(test_loss)

        if (args.use_scheduler):
            scheduler.step()

        # torch.save({
        #     'model_state_dict': model.state_dict(),
        #     'optimizer_state_dict': optimizer.state_dict(),
        #     'loss': loss,
        #     'test_acc': acc
        # }, args.result_dir + "/" + name + "_model.npy")

    save_data = {
        "train_loss": train_losses,
        "test_loss": test_losses,
        "test_acc": test_accs
    }
    json.dump(save_data, open(save_file, 'w'))
def main():
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')

    parser.add_argument('--result_dir',
                        type=str,
                        help='dir to save result txt files',
                        default='results/')
    parser.add_argument('--noise_rate',
                        type=float,
                        help='corruption rate, should be less than 1',
                        default=0.5)
    parser.add_argument('--forget_rate',
                        type=float,
                        help='forget rate',
                        default=None)
    parser.add_argument('--noise_type',
                        type=str,
                        help='[pairflip, symmetric]',
                        default='symmetric')
    parser.add_argument(
        '--num_gradual',
        type=int,
        default=10,
        help=
        'how many epochs for linear drop rate, can be 5, 10, 15. This parameter is equal to Tk for R(T) in Co-teaching paper.'
    )
    parser.add_argument(
        '--exponent',
        type=float,
        default=1,
        help=
        'exponent of the forget rate, can be 0.5, 1, 2. This parameter is equal to c in Tc for R(T) in Co-teaching paper.'
    )
    parser.add_argument('--top_bn', action='store_true')
    parser.add_argument('--dataset',
                        type=str,
                        help='mnist, cifar10, or cifar100',
                        default='mnist')
    parser.add_argument('--n_epoch', type=int, default=200)
    parser.add_argument('--seed', type=int, default=1)
    parser.add_argument('--print_freq', type=int, default=50)
    parser.add_argument('--num_workers',
                        type=int,
                        default=4,
                        help='how many subprocesses to use for data loading')
    parser.add_argument('--num_iter_per_epoch', type=int, default=400)
    parser.add_argument('--epoch_decay_start', type=int, default=80)
    parser.add_argument('--eps', type=float, default=9.3)

    parser.add_argument('--batch-size',
                        type=int,
                        default=600,
                        metavar='N',
                        help='input batch size for training (default: 256)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=4000,
                        metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.005,
                        metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.9,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=100,
        metavar='N',
        help='how many batches to wait before logging training status')

    parser.add_argument('--save-model',
                        action='store_true',
                        default=False,
                        help='For Saving the current Model')
    args = parser.parse_args()

    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    batch_size = args.batch_size

    if args.dataset == 'mnist':
        input_channel = 1
        num_classes = 10
        args.top_bn = False
        args.epoch_decay_start = 80
        args.n_epoch = 200
        train_dataset = MNIST(root='./data/',
                              download=True,
                              train=True,
                              transform=transforms.ToTensor(),
                              noise_type=args.noise_type,
                              noise_rate=args.noise_rate)

        test_dataset = MNIST(root='./data/',
                             download=True,
                             train=False,
                             transform=transforms.ToTensor(),
                             noise_type=args.noise_type,
                             noise_rate=args.noise_rate)

    if args.dataset == 'cifar10':
        input_channel = 3
        num_classes = 10
        args.top_bn = False
        args.epoch_decay_start = 80
        args.n_epoch = 200
        train_dataset = CIFAR10(root='./data/',
                                download=True,
                                train=True,
                                transform=transforms.ToTensor(),
                                noise_type=args.noise_type,
                                noise_rate=args.noise_rate)

        test_dataset = CIFAR10(root='./data/',
                               download=True,
                               train=False,
                               transform=transforms.ToTensor(),
                               noise_type=args.noise_type,
                               noise_rate=args.noise_rate)

    if args.dataset == 'cifar100':
        input_channel = 3
        num_classes = 100
        args.top_bn = False
        args.epoch_decay_start = 100
        args.n_epoch = 200
        train_dataset = CIFAR100(root='./data/',
                                 download=True,
                                 train=True,
                                 transform=transforms.ToTensor(),
                                 noise_type=args.noise_type,
                                 noise_rate=args.noise_rate)

        test_dataset = CIFAR100(root='./data/',
                                download=True,
                                train=False,
                                transform=transforms.ToTensor(),
                                noise_type=args.noise_type,
                                noise_rate=args.noise_rate)

    if args.forget_rate is None:
        forget_rate = args.noise_rate
    else:
        forget_rate = args.forget_rate

    noise_or_not = train_dataset.noise_or_not
    # Data Loader (Input Pipeline)
    print('loading dataset...')
    train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                               batch_size=batch_size,
                                               num_workers=args.num_workers,
                                               drop_last=True,
                                               shuffle=True)

    test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                              batch_size=batch_size,
                                              num_workers=args.num_workers,
                                              drop_last=True,
                                              shuffle=False)
    # Define models
    print('building model...')
    cnn1 = CNN(input_channel=input_channel, n_outputs=num_classes + 1)
    cnn1.cuda()
    print(cnn1.parameters)
    #optimizer1 = torch.optim.SGD(cnn1.parameters(), lr=learning_rate)
    optimizer = torch.optim.SGD(cnn1.parameters(),
                                lr=args.lr,
                                momentum=args.momentum)
    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")

    save_dir = args.result_dir + '/' + args.dataset + '/new_loss/'

    if not os.path.exists(save_dir):
        os.system('mkdir -p %s' % save_dir)

    model_str = args.dataset + '_new_loss_' + args.noise_type + '_' + str(
        args.noise_rate) + '_' + str(args.eps) + ' '
    nowTime = datetime.datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
    txtfile = save_dir + "/" + model_str + nowTime + ".txt"
    if os.path.exists(txtfile):
        os.system('mv %s %s' % (txtfile, txtfile + ".bak-%s" % nowTime))
    acc = []
    loss = []
    loss_pure = []
    loss_corrupt = []
    for epoch in range(1, args.n_epoch + 1):
        #         if epoch<20:
        #             loss.append(train(args, model, device, train_loader, optimizer, epoch, eps=9.5))
        #         else:
        #             loss.append(train(args, model, device, train_loader, optimizer, epoch, eps=5))
        #loss.append(train(args, model, device, train_loader, optimizer, epoch, eps=9.3))
        l1 = train(args,
                   cnn1,
                   device,
                   train_loader,
                   optimizer,
                   epoch,
                   eps=args.eps)
        loss.append(l1)
        acc.append(test(args, cnn1, device, test_loader))
        with open(txtfile, "a") as myfile:
            myfile.write(str(int(epoch)) + ': ' + str(acc[-1]) + "\n")

    name = str(args.dataset) + " " + str(args.noise_type) + " " + str(
        args.noise_rate)
    np.save(name + "acc.npy", acc)
    np.save(name + "loss.npy", loss)
Beispiel #13
0
    input_channel = 1
    num_classes = 10
    init_epoch = 0
    filter_outlier = True
    args.epoch_decay_start = 80
    args.model_type = "mlp"
    # args.n_epoch = 200
    transform1 = torchvision.transforms.Compose([
        # torchvision.transforms.RandomPerspective(),
        # torchvision.transforms.ColorJitter(0.2, 0.75, 0.25, 0.04),
        torchvision.transforms.ToTensor(),
    ])
    train_dataset = MNIST(root='./../Co-correcting_plus/data/mnist/',
                          download=False,
                          train=True,
                          transform=transform1,
                          noise_type=args.noise_type,
                          noise_rate=args.noise_rate
                          )

    test_dataset = MNIST(root='./../Co-correcting_plus/data/mnist/',
                         download=False,
                         train=False,
                         transform=transforms.ToTensor(),
                         noise_type=args.noise_type,
                         noise_rate=args.noise_rate
                         )
    # train_dataset = MNIST(root='./../../dataset/MNIST/',
    #                       download=False,
    #                       train=True,
    #                       transform=transforms.ToTensor(),
Beispiel #14
0
                    action='store_true',
                    default=True,
                    help='For Saving the current Model')
parser.add_argument('--noise_rate', '-nr', type=float, default=0.2)
parser.add_argument('--noise_type', '-nt', type=str, default='symmetry')
parser.add_argument('--dataset', type=str, default='mnist')
parser.add_argument('--gpu', type=str, default=0)
parser.add_argument('--target_epoch', '-te', type=int, default=1)
args = parser.parse_args()
nclass = 0
transform = transforms.ToTensor()
# transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
if args.dataset == 'mnist':
    train_dataset = MNIST(root='/home/datasets/Datasets/mnist/',
                          download=True,
                          train=True,
                          transform=transform,
                          noise_type=args.noise_type,
                          noise_rate=args.noise_rate)
    test_dataset = MNIST(root='/home/datasets/Datasets/mnist/',
                         download=True,
                         train=False,
                         transform=transform)
    nclass = 10
elif args.dataset == 'cifar10':
    train_dataset = CIFAR10(root='./data/',
                            download=True,
                            train=True,
                            transform=transform,
                            noise_type=args.noise_type,
                            noise_rate=args.noise_rate)
def main():
    parser = argparse.ArgumentParser(
        description='PyTorch Gambler\'s Loss Runner')

    parser.add_argument(
        '--result_dir',
        type=str,
        help='directory to save result txt files',
        default='results')
    parser.add_argument(
        '--noise_rate',
        type=float,
        help='corruption rate, should be less than 1',
        default=0.5)
    parser.add_argument(
        '--noise_type',
        type=str,
        help='[pairflip, symmetric]',
        default='symmetric')
    parser.add_argument(
        '--dataset', type=str, help='mnist, cifar10, or imdb', default='mnist')
    parser.add_argument('--n_epoch', type=int, default=10)
    parser.add_argument('--seed', type=int, default=1)
    parser.add_argument(
        '--num_workers',
        type=int,
        default=4,
        help='how many subprocesses to use for data loading')
    parser.add_argument('--epoch_decay_start', type=int, default=80)
    parser.add_argument('--load_model', type=str, default="")

    parser.add_argument(
        '--batch_size',
        type=int,
        default=128,
        metavar='N',
        help='input batch size for training (default: 128)')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=100,
        metavar='N',
        help='how many batches to wait before logging training status (default: 100)'
    )

    parser.add_argument(
        '--lr',
        type=float,
        default=0.001,
        metavar='LR',
        help='learning rate (default: 0.001)')
    parser.add_argument(
        '--early_stopping',
        action='store_true',
        default=False,
        help='enables early stopping criterion for only symmetric datasets')
    parser.add_argument('--eps', type=float, help='set lambda for lambda type \'gmblers\' only', default=1000.0)
    parser.add_argument(
        '--lambda_type', type=str, help='[nll, euc, mid, exp, gmblers]', default="euc")
    parser.add_argument(
        '--start_gamblers', type=int, help='number of epochs before starting gamblers', default=0)

    args = parser.parse_args()

    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    if args.dataset == 'mnist':
        input_channel = 1
        num_classes = 10
        train_dataset = MNIST(
            root='./data/',
            download=True,
            train=True,
            transform=transforms.ToTensor(),
            noise_type=args.noise_type,
            noise_rate=args.noise_rate)
        test_dataset = MNIST(
            root='./data/',
            download=True,
            train=False,
            transform=transforms.ToTensor(),
            noise_type=args.noise_type,
            noise_rate=args.noise_rate)
        print('loading dataset...')
        train_loader = torch.utils.data.DataLoader(
            dataset=train_dataset,
            batch_size=args.batch_size,
            num_workers=args.num_workers,
            drop_last=True,
            shuffle=True)
        test_loader = torch.utils.data.DataLoader(
            dataset=test_dataset,
            batch_size=args.batch_size,
            num_workers=args.num_workers,
            drop_last=True,
            shuffle=False)

    if args.dataset == 'cifar10':
        input_channel = 3
        num_classes = 10
        train_dataset = CIFAR10(
            root='./data/',
            download=True,
            train=True,
            transform=transforms.ToTensor(),
            noise_type=args.noise_type,
            noise_rate=args.noise_rate)
        test_dataset = CIFAR10(
            root='./data/',
            download=True,
            train=False,
            transform=transforms.ToTensor(),
            noise_type=args.noise_type,
            noise_rate=args.noise_rate)
        print('loading dataset...')
        train_loader = torch.utils.data.DataLoader(
            dataset=train_dataset,
            batch_size=args.batch_size,
            num_workers=args.num_workers,
            drop_last=True,
            shuffle=True)
        test_loader = torch.utils.data.DataLoader(
            dataset=test_dataset,
            batch_size=args.batch_size,
            num_workers=args.num_workers,
            drop_last=True,
            shuffle=False)

    if args.dataset == 'imdb':
        num_classes = 2
        embedding_length = 300
        hidden_size = 256
        print('loading dataset...')
        TEXT, vocab_size, word_embeddings, train_loader, valid_iter, test_loader = load_data.load_dataset(
            rate=args.noise_rate, batch_size=args.batch_size)

    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    print("using {}".format(device))

    print('building model...')
    if args.dataset == 'mnist':
        model = CNN_basic(num_classes=num_classes + 1).to(device)
        optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
    if args.dataset == 'cifar10':
        model = CNN_small(num_classes=num_classes + 1).to(device)
        optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)
    if args.dataset == 'imdb':
        model = LSTMClassifier(args.batch_size, num_classes + 1, hidden_size,
                               vocab_size, embedding_length, word_embeddings).to(device)
        optimizer = LaProp(filter(lambda p: p.requires_grad, model.parameters()), lr=args.lr)

    acc = []
    loss = []
    out = []

    eee = 1 - args.noise_rate
    criteria = (-1) * (eee * np.log(eee) + (1 - eee) * np.log(
        (1 - eee) / (args.eps - 1)))

    name = str(args.dataset) + "_" + str(args.noise_type) + "_" + str(
        args.noise_rate) + "_" + str(args.eps) + "_" + str(args.seed)

    if not os.path.exists(args.result_dir):
        os.system('mkdir -p %s' % args.result_dir)

    for epoch in range(1, args.n_epoch + 1):
        l1 = train(
            args,
            model,
            device,
            train_loader,
            optimizer,
            epoch,
            num_classes=num_classes,
            use_gamblers=(epoch >= args.start_gamblers),
            text=(args.dataset == 'imdb'))
        loss.append(l1)
        acc.append(test(args, model, device, test_loader, num_classes, text=(args.dataset == 'imdb')))
        if l1 < criteria and epoch >= args.start_gamblers and args.early_stopping:
            print('epoch: {}, loss fulfilled early stopping criteria: {} < {}'.format(epoch, l1, criteria))
            break
        torch.save({
            'model_state_dict': model.state_dict(),
            'optimizer_state_dict': optimizer.state_dict(),
            'loss': loss,
            'test_acc': acc
        }, args.result_dir + "/" + name + "_model.npy")

    print(name)
    np.save(args.result_dir + "/" + name + "_acc.npy", acc)
    np.save(args.result_dir + "/" + name + "_loss.npy", loss)
Beispiel #16
0
def get_datasets(dataset_name, noise_type=None, noise_rate=None):
    val_dataset = None
    if (dataset_name == "MNIST"):
        train_dataset = MNIST(root='./data/',
                              download=True,
                              train=True,
                              transform=transforms.ToTensor(),
                              noise_type=noise_type,
                              noise_rate=noise_rate)
        test_dataset = MNIST(root='./data/',
                             download=True,
                             train=False,
                             transform=transforms.ToTensor(),
                             noise_type=noise_type,
                             noise_rate=noise_rate)
    elif (dataset_name == "CIFAR10"):
        train_dataset = CIFAR10(root='./data/',
                                download=True,
                                train=True,
                                transform=get_transform(),
                                noise_type=noise_type,
                                noise_rate=noise_rate)
        test_dataset = CIFAR10(root='./data/',
                               download=True,
                               train=False,
                               transform=transforms.ToTensor(),
                               noise_type=noise_type,
                               noise_rate=noise_rate)
    elif (dataset_name == "CIFAR100"):
        train_dataset = CIFAR100(root='./data/',
                                 download=True,
                                 train=True,
                                 transform=get_transform(),
                                 noise_type=noise_type,
                                 noise_rate=noise_rate)
        test_dataset = CIFAR100(root='./data/',
                                download=True,
                                train=False,
                                transform=transforms.ToTensor(),
                                noise_type=noise_type,
                                noise_rate=noise_rate)
    elif (dataset_name == "Clothes1M"):
        img_sz = 224
        train_dataset = Clothing(data_dir='../clothing1M/',
                                 train=True,
                                 val=False,
                                 test=False,
                                 transform=clothing_transform(img_sz))
        val_dataset = Clothing(data_dir='../clothing1M/',
                               train=False,
                               val=True,
                               test=False,
                               transform=clothing_transform(img_sz))
        test_dataset = Clothing(data_dir='../clothing1M/',
                                train=False,
                                val=False,
                                test=True,
                                transform=clothing_transform(img_sz))
    elif (dataset_name == "Food101N"):
        img_sz = 224
        train_dataset = Food101N(data_dir='../Food-101N_release/',
                                 train=True,
                                 val=False,
                                 test=False,
                                 transform=food_transform(img_sz))
        val_dataset = Food101N(data_dir='../Food-101N_release/',
                               train=False,
                               val=True,
                               test=False,
                               transform=food_transform(img_sz))
        test_dataset = Food101N(data_dir='../Food-101N_release/',
                                train=False,
                                val=False,
                                test=True,
                                transform=food_transform(img_sz))

    return train_dataset, val_dataset, test_dataset
Beispiel #17
0
import operations
import numpy as np
import tensorflow as tf
import PIL.Image as Image
import keras.layers as layers
import keras.models as models
import keras.initializers as initializers
import keras.backend as backend

from data.mnist import MNIST
from models.Mnist_Model import MNISTModel

tf_session = tf.Session()
backend.set_session(tf_session)

dataset = MNIST(
    r'C:/Users/Eduardo Montesuma/PycharmProjects/AutoAugment/data/MNIST')
reduced_num_train = dataset.num_train // 10
rand_samples = np.random.randint(low=0,
                                 high=dataset.num_train,
                                 size=reduced_num_train)

Xtr = dataset.x_train_rgb
Ytr = dataset.y_train

Xtr = Xtr[rand_samples]
Ytr = Ytr[rand_samples]

Xts = dataset.x_test_rgb
Yts = dataset.y_test

transformations = operations.get_transformations(Xtr)
# Seed
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)

# Hyper Parameters
batch_size = 128
learning_rate = args.lr

# load dataset
num_classes = 10
args.epoch_decay_start = 200
args.n_epoch = 200
train_dataset = MNIST(root='./data/',
                      download=True,
                      train=True,
                      transform=transforms.ToTensor(),
                      noise_type=args.noise_type,
                      noise_rate=args.noise_rate)

test_dataset = MNIST(root='./data/',
                     download=True,
                     train=False,
                     transform=transforms.ToTensor(),
                     noise_type=args.noise_type,
                     noise_rate=args.noise_rate)

if args.forget_rate is None:
    forget_rate = args.noise_rate
else:
    forget_rate = args.forget_rate
Beispiel #19
0
def main():
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')

    parser.add_argument('--result_dir',
                        type=str,
                        help='dir to save result txt files',
                        default='results/')
    parser.add_argument('--noise_rate',
                        type=float,
                        help='corruption rate, should be less than 1',
                        default=0.5)
    parser.add_argument('--forget_rate',
                        type=float,
                        help='forget rate',
                        default=None)
    parser.add_argument('--noise_type',
                        type=str,
                        help='[pairflip, symmetric]',
                        default='symmetric')
    parser.add_argument(
        '--num_gradual',
        type=int,
        default=10,
        help=
        'how many epochs for linear drop rate, can be 5, 10, 15. This parameter is equal to Tk for R(T) in Co-teaching paper.'
    )
    parser.add_argument(
        '--exponent',
        type=float,
        default=1,
        help=
        'exponent of the forget rate, can be 0.5, 1, 2. This parameter is equal to c in Tc for R(T) in Co-teaching paper.'
    )
    parser.add_argument('--top_bn', action='store_true')
    parser.add_argument('--dataset',
                        type=str,
                        help='mnist, cifar10, or cifar100',
                        default='mnist')
    parser.add_argument('--n_epoch', type=int, default=10)
    parser.add_argument('--seed', type=int, default=1)
    parser.add_argument('--print_freq', type=int, default=50)
    parser.add_argument('--num_workers',
                        type=int,
                        default=4,
                        help='how many subprocesses to use for data loading')
    parser.add_argument('--num_iter_per_epoch', type=int, default=400)
    parser.add_argument('--epoch_decay_start', type=int, default=80)
    parser.add_argument('--eps', type=float, default=9.9)

    parser.add_argument('--batch-size',
                        type=int,
                        default=1000,
                        metavar='N',
                        help='input batch size for training (default: 256)')
    parser.add_argument('--test-batch-size',
                        type=int,
                        default=4000,
                        metavar='N',
                        help='input batch size for testing (default: 1000)')
    parser.add_argument('--lr',
                        type=float,
                        default=0.005,
                        metavar='LR',
                        help='learning rate (default: 0.01)')
    parser.add_argument('--momentum',
                        type=float,
                        default=0.9,
                        metavar='M',
                        help='SGD momentum (default: 0.5)')
    parser.add_argument('--no-cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument(
        '--log-interval',
        type=int,
        default=100,
        metavar='N',
        help='how many batches to wait before logging training status')

    parser.add_argument('--save-model',
                        action='store_true',
                        default=False,
                        help='For Saving the current Model')
    args = parser.parse_args()

    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    batch_size = args.batch_size

    if args.dataset == 'mnist':
        input_channel = 1
        num_classes = 10
        args.top_bn = False
        args.epoch_decay_start = 80
        args.n_epoch = 200
        train_dataset = MNIST(root='./data/',
                              download=True,
                              train=True,
                              transform=transforms.ToTensor(),
                              noise_type=args.noise_type,
                              noise_rate=args.noise_rate)
        train_size = int(0.9 * len(train_dataset))
        valid_size = len(train_dataset) - train_size
        train_dataset, valid_dataset = random_split(train_dataset,
                                                    [train_size, valid_size])

        test_dataset = MNIST(root='./data/',
                             download=True,
                             train=False,
                             transform=transforms.ToTensor(),
                             noise_type=args.noise_type,
                             noise_rate=args.noise_rate)

    if args.dataset == 'cifar10':
        input_channel = 3
        num_classes = 10
        args.top_bn = False
        args.epoch_decay_start = 80
        args.n_epoch = 200
        train_dataset = CIFAR10(root='./data/',
                                download=True,
                                train=True,
                                transform=transforms.ToTensor(),
                                noise_type=args.noise_type,
                                noise_rate=args.noise_rate)

        test_dataset = CIFAR10(root='./data/',
                               download=True,
                               train=False,
                               transform=transforms.ToTensor(),
                               noise_type=args.noise_type,
                               noise_rate=args.noise_rate)

    if args.dataset == 'cifar100':
        input_channel = 3
        num_classes = 100
        args.top_bn = False
        args.epoch_decay_start = 100
        args.n_epoch = 200
        train_dataset = CIFAR100(root='./data/',
                                 download=True,
                                 train=True,
                                 transform=transforms.ToTensor(),
                                 noise_type=args.noise_type,
                                 noise_rate=args.noise_rate)

        test_dataset = CIFAR100(root='./data/',
                                download=True,
                                train=False,
                                transform=transforms.ToTensor(),
                                noise_type=args.noise_type,
                                noise_rate=args.noise_rate)

    if args.forget_rate is None:
        forget_rate = args.noise_rate
    else:
        forget_rate = args.forget_rate

    #noise_or_not = train_dataset.noise_or_not

    print('loading dataset...')
    train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                               batch_size=args.batch_size,
                                               num_workers=args.num_workers,
                                               drop_last=True,
                                               shuffle=True)

    valid_loader = torch.utils.data.DataLoader(dataset=valid_dataset,
                                               batch_size=args.batch_size,
                                               num_workers=args.num_workers,
                                               drop_last=True,
                                               shuffle=True)

    test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                              batch_size=batch_size,
                                              num_workers=args.num_workers,
                                              drop_last=True,
                                              shuffle=False)

    use_cuda = torch.cuda.is_available()
    device = torch.device("cuda" if use_cuda else "cpu")
    cnn1 = Net().to(device)
    optimizer = torch.optim.SGD(cnn1.parameters(),
                                lr=args.lr,
                                momentum=args.momentum)

    acc = []
    loss = []
    loss_pure = []
    loss_corrupt = []
    out = []
    eee = 1 - args.noise_rate
    criteria = (-1) * (eee * np.log(eee) + (1 - eee) * np.log(
        (1 - eee) / (args.eps - 1)))
    last = float("inf")
    count = 0

    for epoch in range(1, 101):
        l1, out10 = train(args,
                          cnn1,
                          device,
                          train_loader,
                          optimizer,
                          epoch,
                          eps=args.eps,
                          nums=num_classes)
        cur, out101 = train(args,
                            cnn1,
                            device,
                            valid_loader,
                            optimizer,
                            epoch,
                            eps=args.eps,
                            nums=num_classes)
        #if cur>last:
        #    count+=1
        #else:
        #    last=cur
        #    count=0
        #if count >= 4:
        #    break;
        loss.append(cur)
        out.append(out10)
        acc.append(test(args, cnn1, device, test_loader, num_classes))

    name = str(args.dataset) + " " + str(args.noise_type) + " " + str(
        args.noise_rate) + " " + str(args.eps) + " " + str(args.seed)
    np.save("vl_" + name + " acc.npy", acc)
    np.save("vl_" + name + " loss.npy", loss)