Exemple #1
0
def main():

    torch.manual_seed(2)

    ct = CenterLoss(10, 2)
    ct = ct.cuda()
    print(list(ct.parameters()))

    # print ct.centers.grad

    y = Variable(torch.Tensor([0, 0, 0, 1])).cuda()
    # print y
    feat = Variable(torch.randn([4, 2]), requires_grad=True).cuda()
    # print feat

    out = ct(y, feat)
    out.backward()
    # print ct.centers.grad

    rp = RepulsiveLoss(10, 2, margin=2)
    rp.update_centers(ct.centers)
    rp = rp.cuda()

    outie = rp(y, feat)
    print('output: {}'.format(outie))
    outie.backward()
    print('gradient of the features: {}'.format(feat.grad))
def main():
    dataroot = args.dataroot
    lfw_dataroot = args.lfw
    lfw_batch_size = args.lfw_batch_size
    lfw_validation_epoch_interval = args.lfw_validation_epoch_interval
    model_architecture = args.model
    epochs = args.epochs
    resume_path = args.resume_path
    batch_size = args.batch_size
    num_workers = args.num_workers
    validation_dataset_split_ratio = args.valid_split
    embedding_dimension = args.embedding_dim
    pretrained = args.pretrained
    optimizer = args.optimizer
    learning_rate = args.lr
    learning_rate_center_loss = args.center_loss_lr
    center_loss_weight = args.center_loss_weight
    start_epoch = 0

    # Define image data pre-processing transforms
    #   ToTensor() normalizes pixel values between [0, 1]
    #   Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) normalizes pixel values between [-1, 1]

    #  Size 182x182 RGB image -> Center crop size 160x160 RGB image for more model generalization
    data_transforms = transforms.Compose([
        transforms.RandomCrop(size=160),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
    ])
    # Size 160x160 RGB image
    lfw_transforms = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
    ])

    # Load the dataset
    dataset = torchvision.datasets.ImageFolder(root=dataroot,
                                               transform=data_transforms)

    # Subset the dataset into training and validation datasets
    num_classes = len(dataset.classes)
    print("\nNumber of classes in dataset: {}".format(num_classes))
    num_validation = int(num_classes * validation_dataset_split_ratio)
    num_train = num_classes - num_validation
    indices = list(range(num_classes))
    np.random.seed(420)
    np.random.shuffle(indices)
    train_indices = indices[:num_train]
    validation_indices = indices[num_train:]

    train_dataset = Subset(dataset=dataset, indices=train_indices)
    validation_dataset = Subset(dataset=dataset, indices=validation_indices)

    print("Number of classes in training dataset: {}".format(
        len(train_dataset)))
    print("Number of classes in validation dataset: {}".format(
        len(validation_dataset)))

    # Define the dataloaders
    train_dataloader = DataLoader(dataset=train_dataset,
                                  batch_size=batch_size,
                                  num_workers=num_workers,
                                  shuffle=False)

    validation_dataloader = DataLoader(dataset=validation_dataset,
                                       batch_size=batch_size,
                                       num_workers=num_workers,
                                       shuffle=False)

    lfw_dataloader = torch.utils.data.DataLoader(dataset=LFWDataset(
        dir=lfw_dataroot,
        pairs_path='datasets/LFW_pairs.txt',
        transform=lfw_transforms),
                                                 batch_size=lfw_batch_size,
                                                 num_workers=num_workers,
                                                 shuffle=False)

    # Instantiate model
    if model_architecture == "resnet18":
        model = Resnet18Center(num_classes=num_classes,
                               embedding_dimension=embedding_dimension,
                               pretrained=pretrained)
    elif model_architecture == "resnet34":
        model = Resnet34Center(num_classes=num_classes,
                               embedding_dimension=embedding_dimension,
                               pretrained=pretrained)
    elif model_architecture == "resnet50":
        model = Resnet50Center(num_classes=num_classes,
                               embedding_dimension=embedding_dimension,
                               pretrained=pretrained)
    elif model_architecture == "resnet101":
        model = Resnet101Center(num_classes=num_classes,
                                embedding_dimension=embedding_dimension,
                                pretrained=pretrained)
    elif model_architecture == "inceptionresnetv2":
        model = InceptionResnetV2Center(
            num_classes=num_classes,
            embedding_dimension=embedding_dimension,
            pretrained=pretrained)
    print("\nUsing {} model architecture.".format(model_architecture))

    # Load model to GPU or multiple GPUs if available
    flag_train_gpu = torch.cuda.is_available()
    flag_train_multi_gpu = False

    if flag_train_gpu and torch.cuda.device_count() > 1:
        model = nn.DataParallel(model)
        model.cuda()
        flag_train_multi_gpu = True
        print('Using multi-gpu training.')
    elif flag_train_gpu and torch.cuda.device_count() == 1:
        model.cuda()
        print('Using single-gpu training.')

    # Set loss functions
    criterion_crossentropy = nn.CrossEntropyLoss().cuda()
    criterion_centerloss = CenterLoss(num_classes=num_classes,
                                      feat_dim=embedding_dimension).cuda()

    # Set optimizers
    if optimizer == "sgd":
        optimizer_model = torch.optim.SGD(model.parameters(), lr=learning_rate)
        optimizer_centerloss = torch.optim.SGD(
            criterion_centerloss.parameters(), lr=learning_rate_center_loss)

    elif optimizer == "adagrad":
        optimizer_model = torch.optim.Adagrad(model.parameters(),
                                              lr=learning_rate)
        optimizer_centerloss = torch.optim.Adagrad(
            criterion_centerloss.parameters(), lr=learning_rate_center_loss)

    elif optimizer == "rmsprop":
        optimizer_model = torch.optim.RMSprop(model.parameters(),
                                              lr=learning_rate)
        optimizer_centerloss = torch.optim.RMSprop(
            criterion_centerloss.parameters(), lr=learning_rate_center_loss)

    elif optimizer == "adam":
        optimizer_model = torch.optim.Adam(model.parameters(),
                                           lr=learning_rate)
        optimizer_centerloss = torch.optim.Adam(
            criterion_centerloss.parameters(), lr=learning_rate_center_loss)

    # Set learning rate decay scheduler
    learning_rate_scheduler = optim.lr_scheduler.MultiStepLR(
        optimizer=optimizer_model, milestones=[150, 225], gamma=0.1)

    # Optionally resume from a checkpoint
    if resume_path:

        if os.path.isfile(resume_path):
            print("\nLoading checkpoint {} ...".format(resume_path))

            checkpoint = torch.load(resume_path)
            start_epoch = checkpoint['epoch']

            # In order to load state dict for optimizers correctly, model has to be loaded to gpu first
            if flag_train_multi_gpu:
                model.module.load_state_dict(checkpoint['model_state_dict'])
            else:
                model.load_state_dict(checkpoint['model_state_dict'])

            optimizer_model.load_state_dict(
                checkpoint['optimizer_model_state_dict'])
            optimizer_centerloss.load_state_dict(
                checkpoint['optimizer_centerloss_state_dict'])
            learning_rate_scheduler.load_state_dict(
                checkpoint['learning_rate_scheduler_state_dict'])

            print(
                "\nCheckpoint loaded: start epoch from checkpoint = {}\nRunning for {} epochs.\n"
                .format(start_epoch, epochs - start_epoch))
        else:
            print(
                "WARNING: No checkpoint found at {}!\nTraining from scratch.".
                format(resume_path))

    # Start Training loop
    print(
        "\nTraining using cross entropy loss with center loss starting for {} epochs:\n"
        .format(epochs - start_epoch))

    total_time_start = time.time()
    start_epoch = start_epoch
    end_epoch = start_epoch + epochs

    for epoch in range(start_epoch, end_epoch):
        epoch_time_start = time.time()

        flag_validate_lfw = (epoch +
                             1) % lfw_validation_epoch_interval == 0 or (
                                 epoch + 1) % epochs == 0
        train_loss_sum = 0
        validation_loss_sum = 0

        # Training the model
        model.train()
        learning_rate_scheduler.step()
        progress_bar = enumerate(tqdm(train_dataloader))

        for batch_index, (data, labels) in progress_bar:
            data, labels = data.cuda(), labels.cuda()

            # Forward pass
            if flag_train_multi_gpu:
                embedding, logits = model.module.forward_training(data)
            else:
                embedding, logits = model.forward_training(data)

            # Calculate losses
            cross_entropy_loss = criterion_crossentropy(
                logits.cuda(), labels.cuda())
            center_loss = criterion_centerloss(embedding, labels)
            loss = (center_loss * center_loss_weight) + cross_entropy_loss

            # Backward pass
            optimizer_centerloss.zero_grad()
            optimizer_model.zero_grad()
            loss.backward()
            optimizer_centerloss.step()
            optimizer_model.step()

            # Remove center_loss_weight impact on the learning of center vectors
            for param in criterion_centerloss.parameters():
                param.grad.data *= (1. / center_loss_weight)

            # Update training loss sum
            train_loss_sum += loss.item() * data.size(0)

        # Validating the model
        model.eval()
        correct, total = 0, 0

        with torch.no_grad():

            progress_bar = enumerate(tqdm(validation_dataloader))

            for batch_index, (data, labels) in progress_bar:

                data, labels = data.cuda(), labels.cuda()

                # Forward pass
                if flag_train_multi_gpu:
                    embedding, logits = model.module.forward_training(data)
                else:
                    embedding, logits = model.forward_training(data)

                # Calculate losses
                cross_entropy_loss = criterion_crossentropy(
                    logits.cuda(), labels.cuda())
                center_loss = criterion_centerloss(embedding, labels)
                loss = (center_loss * center_loss_weight) + cross_entropy_loss

                # Update average validation loss
                validation_loss_sum += loss.item() * data.size(0)

                # Calculate training performance metrics
                predictions = logits.data.max(1)[1]
                total += labels.size(0)
                correct += (predictions == labels.data).sum()

        # Calculate average losses in epoch
        avg_train_loss = train_loss_sum / len(train_dataloader.dataset)
        avg_validation_loss = validation_loss_sum / len(
            validation_dataloader.dataset)

        # Calculate training performance statistics in epoch
        classification_accuracy = correct * 100. / total
        classification_error = 100. - classification_accuracy

        epoch_time_end = time.time()

        # Print training and validation statistics and add to log
        print(
            'Epoch {}:\t Average Training Loss: {:.4f}\tAverage Validation Loss: {:.4f}\tClassification Accuracy: {:.2f}%\tClassification Error: {:.2f}%\tEpoch Time: {:.3f} hours'
            .format(epoch + 1, avg_train_loss, avg_validation_loss,
                    classification_accuracy, classification_error,
                    (epoch_time_end - epoch_time_start) / 3600))
        with open('logs/{}_log_center.txt'.format(model_architecture),
                  'a') as f:
            val_list = [
                epoch + 1, avg_train_loss, avg_validation_loss,
                classification_accuracy.item(),
                classification_error.item()
            ]
            log = '\t'.join(str(value) for value in val_list)
            f.writelines(log + '\n')

        try:
            # Plot plot for Cross Entropy Loss and Center Loss on training and validation sets
            plot_training_validation_losses_center(
                log_dir="logs/{}_log_center.txt".format(model_architecture),
                epochs=epochs,
                figure_name="plots/training_validation_losses_{}_center.png".
                format(model_architecture))
        except Exception as e:
            print(e)

        # Validating on LFW dataset using KFold based on Euclidean distance metric
        if flag_validate_lfw:

            model.eval()
            with torch.no_grad():
                l2_distance = PairwiseDistance(2).cuda()
                distances, labels = [], []

                print("Validating on LFW! ...")
                progress_bar = enumerate(tqdm(lfw_dataloader))

                for batch_index, (data_a, data_b, label) in progress_bar:
                    data_a, data_b, label = data_a.cuda(), data_b.cuda(
                    ), label.cuda()

                    output_a, output_b = model(data_a), model(data_b)
                    distance = l2_distance.forward(
                        output_a, output_b)  # Euclidean distance

                    distances.append(distance.cpu().detach().numpy())
                    labels.append(label.cpu().detach().numpy())

                labels = np.array(
                    [sublabel for label in labels for sublabel in label])
                distances = np.array([
                    subdist for distance in distances for subdist in distance
                ])

                true_positive_rate, false_positive_rate, precision, recall, accuracy, roc_auc, best_distances, \
                    tar, far = evaluate_lfw(
                        distances=distances,
                        labels=labels
                     )

                # Print statistics and add to log
                print(
                    "Accuracy on LFW: {:.4f}+-{:.4f}\tPrecision {:.4f}+-{:.4f}\tRecall {:.4f}+-{:.4f}\tROC Area Under Curve: {:.4f}\tBest distance threshold: {:.2f}+-{:.2f}\tTAR: {:.4f}+-{:.4f} @ FAR: {:.4f}"
                    .format(np.mean(accuracy), np.std(accuracy),
                            np.mean(precision), np.std(precision),
                            np.mean(recall), np.std(recall), roc_auc,
                            np.mean(best_distances), np.std(best_distances),
                            np.mean(tar), np.std(tar), np.mean(far)))
                with open(
                        'logs/lfw_{}_log_center.txt'.format(
                            model_architecture), 'a') as f:
                    val_list = [
                        epoch + 1,
                        np.mean(accuracy),
                        np.std(accuracy),
                        np.mean(precision),
                        np.std(precision),
                        np.mean(recall),
                        np.std(recall), roc_auc,
                        np.mean(best_distances),
                        np.std(best_distances),
                        np.mean(tar)
                    ]
                    log = '\t'.join(str(value) for value in val_list)
                    f.writelines(log + '\n')

            try:
                # Plot ROC curve
                plot_roc_lfw(
                    false_positive_rate=false_positive_rate,
                    true_positive_rate=true_positive_rate,
                    figure_name="plots/roc_plots/roc_{}_epoch_{}_center.png".
                    format(model_architecture, epoch + 1))
                # Plot LFW accuracies plot
                plot_accuracy_lfw(
                    log_dir="logs/lfw_{}_log_center.txt".format(
                        model_architecture),
                    epochs=epochs,
                    figure_name="plots/lfw_accuracies_{}_center.png".format(
                        model_architecture))
            except Exception as e:
                print(e)

        # Save model checkpoint
        state = {
            'epoch':
            epoch + 1,
            'num_classes':
            num_classes,
            'embedding_dimension':
            embedding_dimension,
            'batch_size_training':
            batch_size,
            'model_state_dict':
            model.state_dict(),
            'model_architecture':
            model_architecture,
            'optimizer_model_state_dict':
            optimizer_model.state_dict(),
            'optimizer_centerloss_state_dict':
            optimizer_centerloss.state_dict(),
            'learning_rate_scheduler_state_dict':
            learning_rate_scheduler.state_dict()
        }

        # For storing data parallel model's state dictionary without 'module' parameter
        if flag_train_multi_gpu:
            state['model_state_dict'] = model.module.state_dict()

        # For storing best euclidean distance threshold during LFW validation
        if flag_validate_lfw:
            state['best_distance_threshold'] = np.mean(best_distances)

        # Save model checkpoint
        torch.save(
            state,
            'Model_training_checkpoints/model_{}_center_epoch_{}.pt'.format(
                model_architecture, epoch + 1))

    # Training loop end
    total_time_end = time.time()
    total_time_elapsed = total_time_end - total_time_start
    print("\nTraining finished: total time elapsed: {:.2f} hours.".format(
        total_time_elapsed / 3600))
Exemple #3
0
def run(trainr,testdr, name,cls_num,idx):
    batch_size = 8
    data_loader, imagenet_data,new_lbs = load_dat(batch_size, trainr)

    model = se_resnext50_32x4d(num_classes=1000,pretrained=None)
    model.load_state_dict(torch.load('/home/dsl/all_check/se_resnext50_32x4d-a260b3a4.pth'), strict=False)
    model.fc1 = nn.Linear(2048, 4)
    model.fc2 = nn.Linear(4, cls_num)
    #model.load_state_dict(torch.load('/media/dsl/20d6b919-92e1-4489-b2be-a092290668e4/dsl/check/1009_res_total.pth'), strict=False)
    model.cuda()
    state = {'learning_rate': 0.01, 'momentum': 0.9, 'decay': 0.0005}
    optimizer = torch.optim.SGD(model.parameters(), state['learning_rate'], momentum=state['momentum'],
                                weight_decay=state['decay'], nesterov=True)
    state['label_ix'] = imagenet_data.class_to_idx


    state['cls_name'] = name
    centerloss = CenterLoss(cls_num, 4)
    centerloss.cuda()
    optimzer_center = torch.optim.SGD(centerloss.parameters(), lr=0.3)
    state['best_accuracy'] = 0
    sch = lr_scheduler.ReduceLROnPlateau(optimizer=optimizer, factor=0.5, patience=5)
    ll = len(data_loader.dataset)
    focal_loss = FocalLoss(gamma=2)
    focal_loss.cuda()
    state['train_accuracy'] =0
    def train():
        model.train()
        loss_avg = 0.0
        progress = ProgressBar()
        ip1_loader = []
        idx_loader = []
        correct = 0
        for (data, target) in progress(data_loader):
            data.detach().numpy()
            if data.size(0) != batch_size:
                break
            data, target = torch.autograd.Variable(data.cuda()), torch.autograd.Variable(target.cuda())
            f1, output = model(data)
            pred = output.data.max(1)[1]
            correct += float(pred.eq(target.data).sum())

            optimizer.zero_grad()
            optimzer_center.zero_grad()

            loss = focal_loss(output, target)+ centerloss(target, f1)*0.3
            loss.backward()
            optimizer.step()
            optimzer_center.step()


            loss_avg = loss_avg * 0.2 + float(loss) * 0.8
            print(correct, ll, loss_avg)
        state['train_accuracy'] = correct / len(data_loader.dataset)


        state['train_loss'] = loss_avg
    def test():
        with torch.no_grad():
            model.eval()
            loss_avg = 0.0
            correct = 0
            for k in glob.glob(os.path.join(testdr,'*.jpg')):
                imag = Image.open(k)
                ig = data_transforms['val'](imag)
                ig = ig.unsqueeze(0)
                ig = torch.autograd.Variable(ig.cuda())
                f1, output = model(ig)
                output = F.softmax(output, dim=1)
                pred = output.data.squeeze(dim=0).cpu().numpy()
                score = np.asarray(pred)
                score = np.sum(score, axis=0)
                pred_lb = np.argmax(score)
                sc = np.max(score)
                print(k)
                lbs = new_lbs[pred_lb]
                if sc>0.66:
                    shutil.copy(k,os.path.join(train_dr, lbs))
                else:
                    try:
                        nn_name = k.split('/')[-1]
                        os.remove(os.path.join(train_dr, lbs, nn_name))
                    except:
                        pass

    best_accuracy = 0.0
    for epoch in range(100):
        state['epoch'] = epoch
        train()
        test()
        data_loader, imagenet_data, new_lbs = load_dat(batch_size, trainr)
        sch.step(state['train_accuracy'])

        if best_accuracy < state['train_accuracy']:
            state['best_accuracy'] =  state['train_accuracy']
            torch.save(model.state_dict(), os.path.join('/media/dsl/20d6b919-92e1-4489-b2be-a092290668e4/dsl/check', idx+'.pth'))
        with open(os.path.join('/media/dsl/20d6b919-92e1-4489-b2be-a092290668e4/dsl/check', idx+'.json'),'w') as f:
            f.write(json.dumps(state))
            f.flush()
        print(state)
        print("Best accuracy: %f" % state['best_accuracy'])
        if best_accuracy == 1.0:
            break
Exemple #4
0
def run(trainr, test_dr, name, cls_num, idx):
    batch_size = 2
    imagenet_data = ImageFolder(trainr, transform=data_transforms['train'])
    test_data = ImageFolder(test_dr, transform=data_transforms['val'])
    data_loader = DataLoader(imagenet_data,
                             batch_size=batch_size,
                             shuffle=True)
    test_data_loader = DataLoader(test_data, batch_size=1, shuffle=True)
    model = se_resnext50_32x4d(num_classes=1000, pretrained=None)
    model.load_state_dict(
        torch.load('/home/dsl/all_check/se_resnext50_32x4d-a260b3a4.pth'),
        strict=False)
    model.fc1 = nn.Linear(2048, 2)
    model.fc2 = nn.Linear(2, cls_num)
    #model.load_state_dict(torch.load('/media/dsl/20d6b919-92e1-4489-b2be-a092290668e4/dsl/check/1009_res_total.pth'), strict=False)
    model.cuda()
    state = {'learning_rate': 0.01, 'momentum': 0.9, 'decay': 0.0005}
    optimizer = torch.optim.SGD(model.parameters(),
                                state['learning_rate'],
                                momentum=state['momentum'],
                                weight_decay=state['decay'],
                                nesterov=True)
    state['label_ix'] = imagenet_data.class_to_idx
    state['cls_name'] = name
    centerloss = CenterLoss(cls_num, 2)
    centerloss.cuda()
    optimzer_center = torch.optim.SGD(centerloss.parameters(), lr=0.3)
    state['best_accuracy'] = 0
    sch = lr_scheduler.ReduceLROnPlateau(optimizer=optimizer,
                                         factor=0.5,
                                         patience=5)
    ll = len(data_loader.dataset)
    focal_loss = FocalLoss(gamma=2)
    focal_loss.cuda()

    def train():
        model.train()
        loss_avg = 0.0
        progress = ProgressBar()
        ip1_loader = []
        idx_loader = []
        correct = 0
        for (data, target) in progress(data_loader):
            data.detach().numpy()
            if data.size(0) != batch_size:
                break
            data, target = torch.autograd.Variable(
                data.cuda()), torch.autograd.Variable(target.cuda())
            f1, output = model(data)
            pred = output.data.max(1)[1]
            correct += float(pred.eq(target.data).sum())

            optimizer.zero_grad()
            optimzer_center.zero_grad()

            loss = focal_loss(output, target) + centerloss(target, f1) * 0.0
            loss.backward()
            optimizer.step()
            optimzer_center.step()

            ip1_loader.append(f1)
            idx_loader.append((target))

            loss_avg = loss_avg * 0.2 + float(loss) * 0.8
            print(correct, ll, loss_avg)

        state['train_accuracy'] = correct / len(data_loader.dataset)
        feat = torch.cat(ip1_loader, 0)
        labels = torch.cat(idx_loader, 0)
        visualize(feat.data.cpu().numpy(),
                  labels.data.cpu().numpy(), epoch, cls_num)
        state['train_loss'] = loss_avg

    def test():
        with torch.no_grad():
            model.eval()
            loss_avg = 0.0
            correct = 0
            for batch_idx, (data, target) in enumerate(test_data_loader):
                data, target = torch.autograd.Variable(
                    data.cuda()), torch.autograd.Variable(target.cuda())
                f1, output = model(data)
                loss = F.cross_entropy(output, target)
                pred = output.data.max(1)[1]
                correct += float(pred.eq(target.data).sum())
                loss_avg += float(loss)
                state['test_loss'] = loss_avg / len(test_data_loader)
                state['test_accuracy'] = correct / len(
                    test_data_loader.dataset)
            print(state['test_accuracy'])

    best_accuracy = 0.0
    for epoch in range(30):
        state['epoch'] = epoch
        train()
        #test()
        sch.step(state['train_accuracy'])

        if state['best_accuracy'] < state['train_accuracy']:
            state['best_accuracy'] = state['train_accuracy']
            torch.save(
                model.state_dict(),
                os.path.join(
                    '/media/dsl/20d6b919-92e1-4489-b2be-a092290668e4/dsl/check',
                    idx + '.pth'))
        with open(
                os.path.join(
                    '/media/dsl/20d6b919-92e1-4489-b2be-a092290668e4/dsl/check',
                    idx + '.json'), 'w') as f:
            f.write(json.dumps(state))
            f.flush()
        print(state)
        print("Best accuracy: %f" % state['best_accuracy'])
        if best_accuracy == 1.0:
            break
Exemple #5
0
def run(trainr,name,cls_num,idx):

    imagenet_data = ImageFolder(trainr,
                                transform=data_transforms['train'])
    test_data = ImageFolder('D:/deep_learn_data/luntai/pred/other',
                                transform=data_transforms['val'])
    data_loader = DataLoader(imagenet_data, batch_size=6, shuffle=True)
    test_data_loader = DataLoader(test_data, batch_size=6, shuffle=True)
    model = inceptionv4(num_classes=1001, pretrained=None)
    #model.load_state_dict(torch.load('D:/deep_learn_data/check/inceptionv4-8e4777a0.pth'), strict=False)
    model.last_linear = nn.Linear(1536, cls_num)
    #model.fc2 = nn.Linear(4, cls_num)
    model.load_state_dict(torch.load('log/1006_iv_other.pth'), strict=False)
    model.cuda()
    state = {'learning_rate': 0.01, 'momentum': 0.9, 'decay': 0.0005}
    optimizer = torch.optim.SGD(model.parameters(), state['learning_rate'], momentum=state['momentum'],
                                weight_decay=state['decay'], nesterov=True)
    state['label_ix'] = imagenet_data.class_to_idx
    state['cls_name'] = name
    centerloss = CenterLoss(cls_num, 4)
    centerloss.cuda()
    optimzer_center = torch.optim.SGD(centerloss.parameters(), lr=0.3)
    state['best_accuracy'] = 0
    sch = lr_scheduler.ReduceLROnPlateau(optimizer=optimizer, factor=0.5, patience=5)
    ll = len(data_loader.dataset)
    focal_loss = FocalLoss(gamma=2)
    focal_loss.cuda()

    def train():
        model.train()
        loss_avg = 0.0
        progress = ProgressBar()
        ip1_loader = []
        idx_loader = []
        correct = 0
        for (data, target) in progress(data_loader):
            data.detach().numpy()
            if data.size(0) != 6:
                break
            data, target = torch.autograd.Variable(data.cuda()), torch.autograd.Variable(target.cuda())
            output = model(data)
            pred = output.data.max(1)[1]
            correct += float(pred.eq(target.data).sum())

            optimizer.zero_grad()
            optimzer_center.zero_grad()

            loss = focal_loss(output, target)
            loss.backward()
            optimizer.step()
            optimzer_center.step()
            loss_avg = loss_avg * 0.2 + float(loss) * 0.8
            print(correct, ll, loss_avg)

        state['train_accuracy'] = correct / len(data_loader.dataset)
        state['train_loss'] = loss_avg
    def test():
        with torch.no_grad():
            model.eval()
            loss_avg = 0.0
            correct = 0
            for batch_idx, (data, target) in enumerate(test_data_loader):
                data, target = torch.autograd.Variable(data.cuda()), torch.autograd.Variable(target.cuda())
                output = model(data)
                loss = F.cross_entropy(output, target)
                pred = output.data.max(1)[1]
                correct += float(pred.eq(target.data).sum())
                loss_avg += float(loss)
                state['test_loss'] = loss_avg / len(test_data_loader)
                state['test_accuracy'] = correct / len(test_data_loader.dataset)
            print(state['test_accuracy'])

    best_accuracy = 0.0
    for epoch in range(100):
        state['epoch'] = epoch
        train()
        test()
        sch.step(state['train_accuracy'])

        if best_accuracy < state['test_accuracy']:
            state['best_accuracy'] =  state['test_accuracy']
            torch.save(model.state_dict(), os.path.join('./log', idx+'.pth'))
        with open(os.path.join('./log', idx+'.json'),'w') as f:
            f.write(json.dumps(state))
            f.flush()
        print(state)
        print("Best accuracy: %f" % state['best_accuracy'])
        if best_accuracy == 1.0:
            break
Exemple #6
0
    ]),
}
imagenet_data = ImageFolder(
    '/media/dsl/20d6b919-92e1-4489-b2be-a092290668e4/AIChallenger2018/new/step3/桃子/桃子疮痂病',
    transform=data_transforms['train'])

data_loader = DataLoader(imagenet_data, batch_size=8, shuffle=True)

# Model
model = Net().cuda()

# NLLLoss
nllloss = nn.NLLLoss().cuda()  #CrossEntropyLoss = log_softmax + NLLLoss
# CenterLoss
loss_weight = 1
centerloss = CenterLoss(2, 2).cuda()

# optimzer4nn
optimizer4nn = optim.SGD(model.parameters(),
                         lr=0.001,
                         momentum=0.9,
                         weight_decay=0.0005)
sheduler = lr_scheduler.StepLR(optimizer4nn, 20, gamma=0.8)

# optimzer4center
optimzer4center = optim.SGD(centerloss.parameters(), lr=0.5)

for epoch in range(100):
    sheduler.step()
    # print optimizer4nn.param_groups[0]['lr']
    train(epoch + 1)
Exemple #7
0
def run(train_sets,valid_sets, cls_num,idx):
    batch_size = 16
    train_gen = get_batch(batch_size= batch_size,data_set=train_sets, image_size=train_sets.image_size)
    valid_gen = get_batch(batch_size= 1,data_set=valid_sets, image_size=train_sets.image_size)
    model = resnet18(num_classes=1000,pretrained=None)
    model.load_state_dict(torch.load('/home/dsl/all_check/resnet18-5c106cde.pth'), strict=False)
    model.fc = nn.Linear(512,1)
    model.cuda()
    state = {'learning_rate': 0.01, 'momentum': 0.9, 'decay': 0.0005}
    optimizer = torch.optim.SGD(model.parameters(), state['learning_rate'], momentum=state['momentum'],
                                weight_decay=state['decay'], nesterov=True)
    state['label_ix'] = train_sets.cls_map
    state['cls_name'] = idx
    centerloss = CenterLoss(cls_num,2)
    centerloss.cuda()
    optimzer_center = torch.optim.SGD(centerloss.parameters(), lr=0.3)
    state['best_accuracy'] = 0
    sch = lr_scheduler.ReduceLROnPlateau(optimizer=optimizer, factor=0.5, patience=5)
    ll = train_sets.len()
    focal_loss = FocalLoss(gamma=2)
    focal_loss.cuda()

    bc_loss = nn.BCEWithLogitsLoss()

    def train():
        model.train()
        loss_avg = 0.0
        progress = ProgressBar()
        ip1_loader = []
        idx_loader = []
        correct = 0
        for b in range(int(train_sets.len()/batch_size)):
            images, labels = next(train_gen)
            images = np.transpose(images,[0,3,1,2])
            images = torch.from_numpy(images)
            labels = torch.from_numpy(labels).float()
            data, target = torch.autograd.Variable(images.cuda()), torch.autograd.Variable(labels.cuda())
            output = model(data)

            output = output.squeeze()
            ot = F.sigmoid(output)
            pred = ot.ge(0.5).float()

            correct += float(pred.eq(target.data).sum())

            optimizer.zero_grad()

            loss = bc_loss(output, target)
            loss.backward()
            optimizer.step()



            loss_avg = loss_avg * 0.2 + float(loss) * 0.8
            print(correct, ll, loss_avg)
        state['train_accuracy'] = correct / train_sets.len()
        state['train_loss'] = loss_avg
    def test():
        with torch.no_grad():
            model.eval()
            loss_avg = 0.0
            correct = 0
            for i in range(valid_sets.len()):
                images, labels = next(valid_gen)
                images = np.transpose(images, [0,3,1,2])
                images = torch.from_numpy(images)
                labels = torch.from_numpy(labels).float()
                data, target = torch.autograd.Variable(images.cuda()), torch.autograd.Variable(labels.cuda())
                output = model(data)
                output = output.squeeze()
                ot = F.sigmoid(output)
                pred = ot.ge(0.5).float()
                correct += float(pred.eq(target.data).sum())
                state['test_accuracy'] = correct / valid_sets.len()
            print(state['test_accuracy'])

    best_accuracy = 0.0
    for epoch in range(40):
        state['epoch'] = epoch
        train()
        test()
        sch.step(state['train_accuracy'])
        best_accuracy = (state['train_accuracy']+state['test_accuracy'])/2

        if best_accuracy > state['best_accuracy']:
            state['best_accuracy'] =  best_accuracy
            torch.save(model.state_dict(), os.path.join('/media/dsl/20d6b919-92e1-4489-b2be-a092290668e4/AIChallenger2018/zuixin/be', idx+'.pth'))
            with open(os.path.join('/media/dsl/20d6b919-92e1-4489-b2be-a092290668e4/AIChallenger2018/zuixin/be', idx+'.json'),'w') as f:
                f.write(json.dumps(state))
                f.flush()
        print(state)
        print("Best accuracy: %f" % state['best_accuracy'])

        if state['train_accuracy']-state['test_accuracy']>0.06 and epoch>30:
            break
Exemple #8
0
def run(trainr, name, cls_num, idx):

    imagenet_data = ImageFolder(trainr, transform=data_transforms['train'])
    data_loader = DataLoader(imagenet_data, batch_size=6, shuffle=True)
    model = inception_v3(num_classes=1000, pretrained=None, aux_logits=False)
    model.load_state_dict(torch.load(
        'D:/deep_learn_data/check/inception_v3_google-1a9a5a14.pth'),
                          strict=False)
    model.fc1 = nn.Linear(2048, 4)
    model.fc2 = nn.Linear(4, 12)
    #model.load_state_dict(torch.load('D:/deep_learn_data/luntai/check/1.pth'), strict=False)
    model.cuda()
    state = {'learning_rate': 0.01, 'momentum': 0.9, 'decay': 0.0005}
    optimizer = torch.optim.SGD(model.parameters(),
                                state['learning_rate'],
                                momentum=state['momentum'],
                                weight_decay=state['decay'],
                                nesterov=True)
    state['label_ix'] = imagenet_data.class_to_idx
    state['cls_name'] = name
    centerloss = CenterLoss(cls_num, 4)
    centerloss.cuda()
    optimzer_center = torch.optim.SGD(centerloss.parameters(), lr=0.3)
    state['best_accuracy'] = 0
    sch = lr_scheduler.ReduceLROnPlateau(optimizer=optimizer,
                                         factor=0.5,
                                         patience=5)
    ll = len(data_loader.dataset)
    focal_loss = FocalLoss(gamma=2)
    focal_loss.cuda()

    def train():
        model.train()
        loss_avg = 0.0
        progress = ProgressBar()
        ip1_loader = []
        idx_loader = []
        correct = 0
        for (data, target) in progress(data_loader):
            data.detach().numpy()
            if data.size(0) != 6:
                break
            data, target = torch.autograd.Variable(
                data.cuda()), torch.autograd.Variable(target.cuda())
            f1, output = model(data)
            pred = output.data.max(1)[1]
            correct += float(pred.eq(target.data).sum())

            optimizer.zero_grad()
            optimzer_center.zero_grad()

            loss = focal_loss(output, target) + centerloss(target, f1) * 0.3
            loss.backward()
            optimizer.step()
            optimzer_center.step()

            ip1_loader.append(f1)
            idx_loader.append((target))

            loss_avg = loss_avg * 0.2 + float(loss) * 0.8
            print(correct, ll, loss_avg)

        state['train_accuracy'] = correct / len(data_loader.dataset)
        feat = torch.cat(ip1_loader, 0)
        labels = torch.cat(idx_loader, 0)
        visualize(feat.data.cpu().numpy(),
                  labels.data.cpu().numpy(), epoch, cls_num)
        state['train_loss'] = loss_avg

    best_accuracy = 0.0
    for epoch in range(100):
        state['epoch'] = epoch
        train()
        sch.step(state['train_accuracy'])

        if best_accuracy < state['train_accuracy']:
            state['best_accuracy'] = state['train_accuracy']
            torch.save(model.state_dict(), os.path.join('./log', idx + '.pth'))
        with open(os.path.join('./log', idx + '.json'), 'w') as f:
            f.write(json.dumps(state))
            f.flush()
        print(state)
        print("Best accuracy: %f" % state['best_accuracy'])
        if best_accuracy == 1.0 or state['train_accuracy'] > 0.99:
            break
def main():

    ########################################################################
    # Set-up
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    print(sys.executable)

    torch.manual_seed(TORCH_SEED)
    torch.cuda.manual_seed(TORCH_SEED)
    np.random.seed(NUMPY_SEED)

    use_gpu = torch.cuda.is_available()
    # use_gpu = False

    device = torch.device("cuda" if use_gpu else "cpu")
    print('GPU id: {}, name: {}'.format(
        GPU_ID, torch.cuda.get_device_name(torch.cuda.current_device())))

    trainloader, testloader, trainset, testset, num_classes = load_dataset(
        dataset_choice, batch_size_train, batch_size_test)

    classes = np.arange(0, 10)
    # classes = [1,2,3,4,5,6,7,8,9,0]

    if train:

        since = time.time()
        # Define a Convolution Neural Network
        net = MnistModel(embedding_dim)
        if dataset_choice == 'ONLINE_PRODUCTS':
            net = Net(embedding_dim)

        net = net.to(device)

        # Define a Loss function and optimizer
        # cross_entropy = nn.CrossEntropyLoss()
        cross_entropy = nn.NLLLoss()

        center_loss_weight = cl_weight
        center_loss_module = CenterLoss(num_classes, embedding_dim,
                                        center_loss_weight)
        center_loss_module = center_loss_module.to(device)
        if use_gpu:
            center_loss_module = center_loss_module.cuda()

        repulsive_loss_weight = rl_weight
        repulsive_loss_margin = rl_margin
        repulsive_loss_module = RepulsiveLoss(num_classes, embedding_dim,
                                              repulsive_loss_margin,
                                              repulsive_loss_weight)
        repulsive_loss_module = repulsive_loss_module.to(device)
        if use_gpu:
            repulsive_loss_module = repulsive_loss_module.cuda()

        criterion = [cross_entropy, center_loss_module, repulsive_loss_module]

        optimizer_net = optim.Adam(net.parameters(), lr=cross_entropy_lr)
        optimizer_center = optim.SGD(center_loss_module.parameters(),
                                     lr=center_loss_lr)
        optimizer = [optimizer_net, optimizer_center]

        for epoch in range(num_epochs):  # loop over the dataset multiple times
            _, centers = train_epoch(net, trainloader, criterion, optimizer,
                                     epoch, num_classes, batch_size_train,
                                     device, use_gpu, show_plots,
                                     embedding_dim)

        print('Finished Training')
        time_elapsed = time.time() - since
        print('Training complete in {:.0f}m {:.0f}s'.format(
            time_elapsed // 60, time_elapsed % 60))
        torch.save(net.state_dict(), model_save_path)

    else:

        net = MnistModel()
        if use_gpu:
            net = net.cuda()
        net.load_state_dict(torch.load(model_save_path))

    ########################################################################
    # Run the network on the test data
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    # Test for one batch:
    embeddings_one_batch, labels_one_batch = test_one_batch(
        net, testloader, classes, use_gpu, Show=show_misclassified)

    # Test on the whole dataset:
    accuracy = test(net, testloader, device, use_gpu)

    # Classes that performed well, and the classes that did not:
    test_classwise(net, testloader, classes, device, use_gpu)

    # Test for retrieval
    k = 3
    test_retrieval(net, testloader, device, k, use_gpu)

    ########################################################################
    # Show embeddings
    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    # load up data
    x_data = embeddings_one_batch.data.cpu().numpy()
    y_data = labels_one_batch.cpu().numpy()

    # convert image data to float64 matrix. float64 is need for bh_sne
    x_data = np.asarray(x_data).astype('float64')
    x_data = x_data.reshape((x_data.shape[0], -1))

    # perform t-SNE embedding
    # vis_data = tsne(x_data)
    vis_data = pca(x_data, 2)

    # plot the result
    if show_plots:
        visualize_better(vis_data, y_data)

    # logging
    if log:
        from utils.my_logging import save_run_info, prepare_log_dir
        log_dir = prepare_log_dir('logs')
        log_string = 'Dataset: {}\tEpochs: {}\tBatch size: {}\tEmbedding dim: {}\tCenter loss weigth: {:.3f}' \
                     '\tRepulsive loss weigth: {:.3f}\tCross entropy learning rate: {:.5f}\t' \
                     'Center loss learning rate: {:.4f}\tRepulsive loss margin: {:.2f}\tAccuracy: {:.3f}'. \
            format(dataset_choice, num_epochs, batch_size_train, embedding_dim, cl_weight, rl_weight, cross_entropy_lr, center_loss_lr,
                   rl_margin, accuracy)
        save_run_info(log_string, log_dir)
        plot_data_better(vis_data, y_data, log_dir=log_dir)