Example #1
0
# Move to GPU
if cuda:
    target_encoder = target_encoder.cuda()
    source_classifier = source_classifier.cuda()


# -------------
#  Predictions
# -------------

print("---> generating predictions...")

# Set model to evaluation mode
target_encoder.eval()
source_classifier.eval()
predictions = []

# Make predictions
with torch.no_grad():   # do not need to calculate information for gradient during eval
    for imgs in dataloader_target_test:
        
        images = make_variable(imgs).cuda()
        preds = source_classifier(target_encoder(images))
        pred_cls = preds.data.max(1)[1]
        predictions.append(pred_cls.cpu())

preds = np.concatenate(predictions)   # numpy vector, size = number of test data


# ------
Example #2
0
acc_arr = []

print("### [Info] Training encoder and classifier for source ###")

for epoch in range(src_num_epochs):
    print("Epoch {}/{}".format(epoch + 1, src_num_epochs))

    # train model on source
    src_encoder.train()
    src_classifier.train()
    trange = tqdm(src_dataloader)
    running_loss, running_corrects = train_src_one_epoch(trange)
    
    # eval model on source
    src_encoder.eval()
    src_classifier.eval()        
    eval_trange = tqdm(val_src_dataloader)
    eval_loss, eval_corrects = eval_src_one_epoch(eval_trange)
    
    epoch_loss = running_loss / len(src_dataset)
    epoch_acc = running_corrects / len(src_dataset)
            
    eval_epoch_loss = eval_loss / len(val_src_dataset)
    eval_epoch_acc = eval_corrects / len(val_src_dataset)
    
    print("Train | Loss: {:.5f} | Accuracy: {:.5f}".format(epoch_loss, epoch_acc))
    print("Val | Loss: {:.5f} | Accuracy: {:.5f}".format(eval_epoch_loss, eval_epoch_acc))
    
    if eval_epoch_acc > best_acc:
        best_acc = eval_epoch_acc
        best_src_encoder = copy.deepcopy(src_encoder.state_dict())
Example #3
0
def main(args):
    # read from args
    test_path = args.test_path
    d_target = args.d_target
    output_predict_path = args.output_predict_path

    ########## Arguments ##########
    batch_size = 128

    # svhn, usps, mnistm
    if d_target == "mnistm":
        d_source = "usps"
    elif d_target == "svhn":
        d_source = "mnistm"
    else:
        d_source = "svhn"

    output_src_classifier_path = "./hw3-4/models/src_classifier_{}_{}.pth".format(
        d_source, d_target)
    output_tgt_encoder_path = "./hw3-4//models/tgt_encoder_{}_{}.pth".format(
        d_source, d_target)

    #############################

    dataset = ReproduceDataset(test_path)
    dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=False)

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    # init models
    tgt_encoder = LeNetEncoder()
    src_classifier = LeNetClassifier()

    # to device
    tgt_encoder.to(device)
    src_classifier.to(device)

    # init weights
    tgt_encoder.load_state_dict(torch.load(
        output_tgt_encoder_path, map_location=device))

    src_classifier.load_state_dict(torch.load(
        output_src_classifier_path, map_location=device))

    tgt_encoder.eval()
    src_classifier.eval()

    all_pred = []

    for idx, targets in enumerate(dataloader):
        target_images = targets.to(device)
        target_bs = target_images.shape[0]

        with torch.no_grad():
            preds = src_classifier(tgt_encoder(target_images))

        # calculate label acc
        _, pred_labels = torch.max(preds, 1)
        all_pred.append(pred_labels)

    # save to predict
    pred = torch.cat(all_pred).cpu().numpy()
    image_names = ['{:05}.png'.format(i) for i in range(len(pred))]

    pd.DataFrame({
        'image_name': image_names,
        'label': pred
    }).to_csv(output_predict_path, index=False)