Ejemplo n.º 1
0
Archivo: main.py Proyecto: hsack6/AGATE
def main(opt):
    train_dataset = BADataset(opt.dataroot, opt.L, True, False, False)
    train_dataloader = BADataloader(train_dataset, batch_size=opt.batchSize, \
                                      shuffle=True, num_workers=opt.workers, drop_last=True)

    valid_dataset = BADataset(opt.dataroot, opt.L, False, True, False)
    valid_dataloader = BADataloader(valid_dataset, batch_size=opt.batchSize, \
                                     shuffle=True, num_workers=opt.workers, drop_last=True)

    test_dataset = BADataset(opt.dataroot, opt.L, False, False, True)
    test_dataloader = BADataloader(test_dataset, batch_size=opt.batchSize, \
                                     shuffle=True, num_workers=opt.workers, drop_last=True)

    all_dataset = BADataset(opt.dataroot, opt.L, False, False, False)
    all_dataloader = BADataloader(all_dataset, batch_size=opt.batchSize, \
                                     shuffle=False, num_workers=opt.workers, drop_last=False)

    opt.n_edge_types = train_dataset.n_edge_types
    opt.n_node = train_dataset.n_node
    opt.n_existing_node = all_node_num

    net = GCN(opt, kernel_size=2, n_blocks=1, state_dim_bottleneck=opt.state_dim, annotation_dim_bottleneck=opt.annotation_dim)
    net.double()
    print(net)

    criterion = nn.CosineSimilarity(dim=1, eps=1e-6)

    if opt.cuda:
        net.cuda()
        criterion.cuda()

    optimizer = optim.Adam(net.parameters(), lr=opt.lr)
    early_stopping = EarlyStopping(patience=opt.patience, verbose=True)

    os.makedirs(OutputDir, exist_ok=True)
    train_loss_ls = []
    valid_loss_ls = []
    test_loss_ls = []

    for epoch in range(0, opt.niter):
        train_loss = train(epoch, train_dataloader, net, criterion, optimizer, opt)
        valid_loss = valid(valid_dataloader, net, criterion, opt)
        test_loss = test(test_dataloader, net, criterion, opt)

        train_loss_ls.append(train_loss)
        valid_loss_ls.append(valid_loss)
        test_loss_ls.append(test_loss)

        early_stopping(valid_loss, net, OutputDir)
        if early_stopping.early_stop:
            print("Early stopping")
            break

    df = pd.DataFrame({'epoch':[i for i in range(1, len(train_loss_ls)+1)], 'train_loss': train_loss_ls, 'valid_loss': valid_loss_ls, 'test_loss': test_loss_ls})
    df.to_csv(OutputDir + '/loss.csv', index=False)

    net.load_state_dict(torch.load(OutputDir + '/checkpoint.pt'))
    inference(all_dataloader, net, criterion, opt, OutputDir)
Ejemplo n.º 2
0
def save_output(root_dir, output_dir):
    # dataset
    dataset = save_predict_dataset(root_dir)

    #model
    model = GCN(4, 512)
    if use_gpu:
        model = model.cuda()
        #model = torch.nn.DataParallel(model).cuda()
    model.load_state_dict(torch.load(os.path.join(save_dir, model_name)))
    model.train(False)
    for n in range(int(len(dataset) / (6 * 9))):
        #test
        full_output = np.zeros((4, 2848, 4288), dtype='float32')  #(C, H, W)
        title = ''
        for idx in range(6 * 9 * n, 6 * 9 * (n + 1)):
            image, name = dataset[idx]
            r = int((idx % (6 * 9)) / 9)  #row
            c = (idx % (6 * 9)) % 9  #column
            title = name

            if use_gpu:
                image = image.cuda()
            image = Variable(image, volatile=True)

            #forward
            output = model(image.unsqueeze(0))
            output = F.sigmoid(output)
            output = output[0]

            if c < 8:
                if r == 5:
                    full_output[:, r * 512:r * 512 + 512 - 224,
                                c * 512:c * 512 +
                                512] = output.cpu().data.numpy()[:, :-224, :]
                else:
                    full_output[:, r * 512:r * 512 + 512, c * 512:c * 512 +
                                512] = output.cpu().data.numpy()

        for i, d in enumerate(['MA', 'EX', 'HE', 'SE']):
            if not os.path.exists(os.path.join(output_dir, d)):
                os.makedirs(os.path.join(output_dir, d))
            im = np.expand_dims(full_output[i], axis=0).transpose(1, 2, 0)
            im = full_output[i] * 255
            im = np.uint8(im)
            im = Image.fromarray(im)
            im.save(os.path.join(output_dir, d, title + '.jpg'))
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)

# Load data
adj, features, labels, idx_train, idx_val, idx_test = load_citeseer()
# Model and optimizer
model = GCN(input_size=features.shape[1],
            hidden_size=args.hidden,
            n_classes=labels.max().item() + 1)
optimizer = optim.Adam(model.parameters(),
                       lr=args.lr,
                       weight_decay=args.weight_decay)

model.cuda()
features = features.cuda()
adj = adj.cuda()
labels = labels.cuda()
idx_train = idx_train.cuda()
idx_val = idx_val.cuda()
idx_test = idx_test.cuda()

criterion = nn.CrossEntropyLoss()


def train(epoch):
    t = time.time()
    model.train()
    optimizer.zero_grad()
    output = model(features, adj)
Ejemplo n.º 4
0
        print()

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

    print('Best F1 score: {:.4f}'.format(best_f1))

    # load best model weights
    model.load_state_dict(best_model_wts)
    return model


if __name__ == '__main__':
    # dataset
    dataloaders = make_dataloaders(batch_size=batch_size)

    #model
    model = GCN(4, 512)
    if use_gpu:
        model = model.cuda()
        #model = torch.nn.DataParallel(model).cuda()
    model.load_state_dict(torch.load(os.path.join(save_dir, 'gcn_v5.pth')))
    #training
    optimizer = optim.Adam(model.parameters(), lr=lr)
    scheduler = ReduceLROnPlateau(optimizer, 'min', verbose=True)
    model = train_model(model, num_epochs, dataloaders, optimizer, scheduler)

    #save
    save_model(model, save_dir, model_name)
Ejemplo n.º 5
0
idx_test = range(500, 1500)
idx_train = torch.LongTensor(idx_train)
idx_val = torch.LongTensor(idx_val)
idx_test = torch.LongTensor(idx_test)
# Model and optimizer
model = GCN(nfeat=features.shape[1],
            nhid=args.hidden,
            nclass=labels.max().item() + 1,
            dropout=args.dropout)
optimizer = optim.Adam(model.parameters(),
                       lr=args.lr,
                       weight_decay=args.weight_decay)

# 数据写入cuda,便于后续加速
if args.cuda:
    model.cuda()  # . cuda()会分配到显存里(如果gpu可用)
    features = features.cuda()
    adj = adj.cuda()
    labels = labels.cuda()
    idx_train = idx_train.cuda()
    idx_val = idx_val.cuda()
    idx_test = idx_test.cuda()


def train(epoch):
    t = time.time()  # 返回当前时间
    model.train()
    optimizer.zero_grad()
    # optimizer.zero_grad()意思是把梯度置零,也就是把loss关于weight的导数变成0.
    # pytorch中每一轮batch需要设置optimizer.zero_gra
    output = model(features, adj)
Ejemplo n.º 6
0
def run_statistic(threshold):
    '''
        evaluate on small images result
    '''
    # dataset
    dataset = IDRiD_sub1_dataset(data_dir)
    dataloader = DataLoader(dataset,
                            batch_size=batch_size,
                            shuffle=False,
                            num_workers=4)
    #print('Data: %d'%(len(dataset)))

    #model
    model = GCN(4, 512)
    if use_gpu:
        model = model.cuda()
        #model = torch.nn.DataParallel(model).cuda()
    model.load_state_dict(torch.load(os.path.join(save_dir, model_name)))
    model.train(False)
    for i in range(4):
        y_pred_list = []
        y_true_list = []
        for idx, data in enumerate(dataloader):
            images, masks, names = data

            if use_gpu:
                images = images.cuda()
                masks = masks.cuda()
            images, masks = Variable(images,
                                     volatile=True), Variable(masks,
                                                              volatile=True)

            #forward
            outputs = model(images)

            # statistics
            outputs = F.sigmoid(
                outputs).cpu().data  #remenber to apply sigmoid befor usage
            masks = masks.cpu().data
            #for i in range(len(outputs)):
            y_pred = outputs[i]
            y_true = masks[i]
            y_pred = y_pred.numpy().flatten()
            y_pred = np.where(y_pred > threshold, 1, 0)
            y_true = y_true.numpy().flatten()
            y_pred_list.append(y_pred)
            y_true_list.append(y_true)

            #verbose
            if idx % 5 == 0 and idx != 0:
                print('\r{:.2f}%'.format(100 * idx / len(dataloader)),
                      end='\r')
        #print()
        type_list = ['MA', 'EX', 'HE', 'SE']
        precision, recall, f1, _ = precision_recall_fscore_support(
            np.array(y_true_list).flatten(),
            np.array(y_pred_list).flatten(),
            average='binary')
        print(
            '{}    \nThreshold: {:.2f}\nPrecision: {:.4f}\nRecall: {:.4f}\nF1: {:.4f}'
            .format(type_list[i], threshold, precision, recall, f1))
Ejemplo n.º 7
0
def show_image_sample():
    # dataset
    dataset = IDRiD_sub1_dataset(data_dir)

    #model
    model = GCN(4, 512)
    if use_gpu:
        model = model.cuda()
        #model = torch.nn.DataParallel(model).cuda()
    model.load_state_dict(torch.load(os.path.join(save_dir, model_name)))
    model.train(False)
    for n in range(12):
        #test
        full_image = np.zeros((3, 2848, 4288), dtype='float32')
        full_mask = np.zeros((4, 2848, 4288), dtype='float32')
        full_output = np.zeros((4, 2848, 4288), dtype='float32')  #(C, H, W)
        title = ''
        for idx in range(9 * 6 * n, 9 * 6 * (n + 1)):
            image, mask, name = dataset[idx]
            n = int(idx / (6 * 9))  #image index
            r = int((idx % (6 * 9)) / 9)  #row
            c = (idx % (6 * 9)) % 9  #column
            title = name[:-8]

            if use_gpu:
                image = image.cuda()
                mask = mask.cuda()
            image, mask = Variable(image,
                                   volatile=True), Variable(mask,
                                                            volatile=True)

            #forward
            output = model(image.unsqueeze(0))
            output = F.sigmoid(output)
            output = output[0]
            if c < 8:
                if r == 5:
                    full_output[:, r * 512:r * 512 + 512 - 224,
                                c * 512:c * 512 +
                                512] = output.cpu().data.numpy()[:, :-224, :]
                    full_mask[:, r * 512:r * 512 + 512 - 224, c * 512:c * 512 +
                              512] = mask.cpu().data.numpy()[:, :-224, :]
                    full_image[:, r * 512:r * 512 + 512 - 224,
                               c * 512:c * 512 +
                               512] = image.cpu().data.numpy()[:, :-224, :]

                else:
                    full_output[:, r * 512:r * 512 + 512, c * 512:c * 512 +
                                512] = output.cpu().data.numpy()
                    full_mask[:, r * 512:r * 512 + 512,
                              c * 512:c * 512 + 512] = mask.cpu().data.numpy()
                    full_image[:, r * 512:r * 512 + 512, c * 512:c * 512 +
                               512] = image.cpu().data.numpy()

        full_image = full_image.transpose(1, 2, 0)
        MA = full_output[0]
        EX = full_output[1]
        HE = full_output[2]
        SE = full_output[3]

        plt.figure()
        plt.axis('off')
        plt.suptitle(title)
        plt.subplot(331)
        plt.title('image')
        fig = plt.imshow(full_image)
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)
        plt.subplot(332)
        plt.title('ground truth MA')
        fig = plt.imshow(full_mask[0])
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)
        plt.subplot(333)
        plt.title('ground truth EX')
        fig = plt.imshow(full_mask[1])
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)
        plt.subplot(334)
        plt.title('ground truth HE')
        fig = plt.imshow(full_mask[2])
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)
        plt.subplot(335)
        plt.title('ground truth SE')
        fig = plt.imshow(full_mask[3])
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)
        plt.subplot(336)
        plt.title('predict MA')
        fig = plt.imshow(MA)
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)
        plt.subplot(337)
        plt.title('predict EX')
        fig = plt.imshow(EX)
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)
        plt.subplot(338)
        plt.title('predict HE')
        fig = plt.imshow(HE)
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)
        plt.subplot(339)
        plt.title('predict SE')
        fig = plt.imshow(SE)
        fig.axes.get_xaxis().set_visible(False)
        fig.axes.get_yaxis().set_visible(False)

        plt.show()
Ejemplo n.º 8
0
def main(files_home):
    starttime = datetime.now()
    print('start test model ', starttime)

    number = args.number

    f = open(os.path.join(files_home, files_name['node_index']), 'rb')
    node2index = cPickle.load(f)

    f_train = os.path.join(files_home, files_name['train_file'])
    f_test = os.path.join(files_home, files_name['test_file'])  ###

    #    testset = Sample_Set_Test(f_test,f_train, node2index)
    #   testloader = DataLoader(testset, batch_size=32, shuffle=False)

    node_count = len(node2index)
    node_dim = 128
    n_repr = 128
    gcn = GCN(node_count, node_dim, n_repr, dropout=args.dropout)
    lp = Link_Prediction(n_repr, dropout=args.dropout)
    if args.cuda == True:
        gcn.cuda()
        lp = nn.DataParallel(lp)
        lp.cuda()

    init_input = torch.LongTensor([j for j in range(0, node_count)]).cuda()

    dis2gene_test_true, dis2gene_test_all = get_rank_test_samples(
        f_train, f_test)

    f = open(files_home + '/networks/adj_matrix_%d_full' % (number), 'rb')
    full_adj_matrix = cPickle.load(f)
    full_adj_matrix = sparse_mx_to_torch_sparse_tensor(full_adj_matrix).cuda()

    for epoch in tqdm(range(0, args.epochs)):
        if epoch % 9 != 0 and epoch < args.epochs - 5:
            continue
        gcn.load_state_dict(
            torch.load(files_home + '/networks/GCN_%d_%d.pth' %
                       (number, epoch)))
        lp.load_state_dict(
            torch.load(files_home + '/networks/Link_Prediction_%d_%d.pth' %
                       (number, epoch)))
        gcn.eval()

        feature_matrix = gcn(init_input, full_adj_matrix)
        #       test_link(testloader, feature_matrix, lp)

        if 0:
            print('use gcn to prediction')
            ap, prec, recall, f1score = test_priorization_gcn(
                dis2gene_test_true, dis2gene_test_all, feature_matrix, lp)
        else:
            print('use gcn and word2ver to prediction')
            ap, prec, recall, f1score = test_priorization_word_gcn(
                dis2gene_test_true, dis2gene_test_all, feature_matrix, lp)
        print('Performance for number=%d epoch=%d' % (number, epoch))
        print('AP: ', ap)
        print('Prec: ', prec)
        print('Recall: ', recall)
        print('F1score: ', f1score)

    endtime = datetime.now()
    print('finish test model! run spend ', endtime - starttime)
Ejemplo n.º 9
0
import numpy as np
import torch
import torch.nn.functional as F
from model import GCN as GCN
from graph import build_graph_skeleton as build_graph
from dataLoader import cropCUB as CUB

CD = True

G = build_graph()

net = GCN(2048, [1024, 512], 200)
if CD:
    net = net.cuda(0)

batch_size = 8
start_epoch = 0
if True:
    ckpt = torch.load('/Disk5/junqi/CUB/early_skeleton_93.ckpt')
    net.load_state_dict(ckpt['net_state_dict'])
    start_epoch = ckpt['epoch'] + 1

datas = CUB()
dataLoader = torch.utils.data.DataLoader(datas,
                                         batch_size=batch_size,
                                         shuffle=True,
                                         num_workers=4)
optimizer = torch.optim.SGD(net.parameters(),
                            lr=1e-2,
                            momentum=0.9,
                            weight_decay=1e-4)
Ejemplo n.º 10
0
import pickle
import torch
from graph import build_graph_skeleton as build_graph
from model import GCN as GCN

testset = CUB('test', False)
dataloader = torch.utils.data.DataLoader(testset,
                                         batch_size=8,
                                         shuffle=False,
                                         num_workers=4)

net = GCN(2048, [1024, 512], 200)
G = build_graph()
ckpt = torch.load('/Disk5/junqi/CUB/early_skeleton_104.ckpt')
net.load_state_dict(ckpt['net_state_dict'])
net = net.cuda()
net = torch.nn.DataParallel(net)
creterion = torch.nn.CrossEntropyLoss()

test_correct = 0
test_loss = 0
total = 0
net.eval()

pre, lab = [], []

for i, (inputs, mask, labels) in enumerate(dataloader):
    if i % 10 == 0 and i > 0:
        print('test {}/{}, acc: {:.3f}, loss: {:.3f}'.format(
            i, len(dataloader),
            float(test_correct) / total, test_loss / total))
Ejemplo n.º 11
0
    def train_classification_gcn(self, Adj, features, nfeats, labels, nclasses,
                                 train_mask, val_mask, test_mask, args):

        model = GCN(in_channels=nfeats,
                    hidden_channels=args.hidden,
                    out_channels=nclasses,
                    num_layers=args.nlayers,
                    dropout=args.dropout2,
                    dropout_adj=args.dropout_adj2,
                    Adj=Adj,
                    sparse=args.sparse)
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=args.lr,
                                     weight_decay=args.w_decay)

        bad_counter = 0
        best_val = 0
        best_model = None
        best_loss = 0
        best_train_loss = 0

        if torch.cuda.is_available():
            model = model.cuda()
            train_mask = train_mask.cuda()
            val_mask = val_mask.cuda()
            test_mask = test_mask.cuda()
            features = features.cuda()
            labels = labels.cuda()

        for epoch in range(1, args.epochs + 1):
            model.train()
            loss, accu = self.get_loss_fixed_adj(model, train_mask, features,
                                                 labels)
            optimizer.zero_grad()
            loss.backward()

            optimizer.step()

            if epoch % 10 == 0:
                model.eval()
                val_loss, accu = self.get_loss_fixed_adj(
                    model, val_mask, features, labels)
                if accu > best_val:
                    bad_counter = 0
                    best_val = accu
                    best_model = copy.deepcopy(model)
                    best_loss = val_loss
                    best_train_loss = loss
                else:
                    bad_counter += 1

                if bad_counter >= args.patience:
                    break

        print("Val Loss {:.4f}, Val Accuracy {:.4f}".format(
            best_loss, best_val))
        best_model.eval()
        test_loss, test_accu = self.get_loss_fixed_adj(best_model, test_mask,
                                                       features, labels)
        print("Test Loss {:.4f}, Test Accuracy {:.4f}".format(
            test_loss, test_accu))
        return best_val, test_accu, best_model
Ejemplo n.º 12
0
                    embeddings,
                    freeze_embeddings=True)
gcn_model = GCN(nFeat, nHid, nComm)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad,
                                    model.parameters()),
                             lr=args.learning_rate,
                             weight_decay=5e-4)
gcn_optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad,
                                        gcn_model.parameters()),
                                 lr=args.learning_rate,
                                 weight_decay=5e-4)

if args.use_cuda:
    model.cuda()
    gcn_model.cuda()
    X_train = X_train.cuda()
    X_test = X_test.cuda()
    Y_train = Y_train.cuda()
    Y_test = Y_test.cuda()

epoch_loss = []
epoch_accuracy = []
train_accuracy = []
epochs = []

correct = 0
try:
    for epoch in range(1, args.num_epochs + 1):

        optimizer.zero_grad()
Ejemplo n.º 13
0
Archivo: train.py Proyecto: 63days/GAT
def main(args):
    device = torch.device('cuda' if args.cuda else 'cpu')

    adj, features, labels, idx_train, idx_val, idx_test = load_data(
        args.dataset)

    if args.model == 'gcn':
        model = GCN(nfeat=features.size(1),
                    nhid=args.hidden,
                    nclass=labels.max().item() + 1,
                    dropout=args.dropout)

        print('Model: GCN')

    elif args.model == 'gat':
        model = GAT(nfeat=features.size(1),
                    nhid=args.hidden,
                    nclass=labels.max().item() + 1,
                    dropout=args.dropout,
                    alpha=args.alpha,
                    nheads=args.n_heads)
        print('Model: GAT')

    elif args.model == 'spgcn':
        model = SpGCN(nfeat=features.size(1),
                      nhid=args.hidden,
                      nclass=labels.max().item() + 1,
                      dropout=args.dropout)
        print('Model: SpGCN')

    elif args.model == 'spgat':
        model = SpGAT(nfeat=features.size(1),
                      nhid=args.hidden,
                      nclass=labels.max().item() + 1,
                      dropout=args.dropout,
                      alpha=args.alpha,
                      nheads=args.n_heads)
        print('Model: SpGAT')

    optimizer = optim.Adam(model.parameters(),
                           lr=args.lr,
                           weight_decay=args.weight_decay)

    if args.cuda:
        adj = adj.cuda()
        features = features.cuda()
        labels = labels.cuda()
        idx_train = idx_train.cuda()
        idx_val = idx_val.cuda()
        idx_test = idx_test.cuda()
        model.cuda()
        print(device)

    def train(epoch):
        model.train()
        optimizer.zero_grad()
        output = model(features, adj)
        loss_train = F.nll_loss(output[idx_train], labels[idx_train])
        acc_train = accuracy(output[idx_train], labels[idx_train])
        loss_train.backward()
        optimizer.step()

        if not args.fastmode:
            model.eval()
            output = model(features, adj)

        loss_val = F.nll_loss(output[idx_val], labels[idx_val])
        acc_val = accuracy(output[idx_val], labels[idx_val])
        #         print('Epoch: {:04d}'.format(epoch + 1),
        #               'loss_train: {:.4f}'.format(loss_train.item()),
        #               'acc_train: {:.4f}'.format(acc_train.item()),
        #               'loss_val: {:.4f}'.format(loss_val.item()),
        #               'acc_val: {:.4f}'.format(acc_val.item()))
        pbar.set_description(
            '| epoch: {:4d} | loss_train: {:.4f} | acc_train: {:.4f} |'
            ' loss_val: {:.4f} | acc_val: {:.4f}'.format(
                epoch + 1, loss_train.item(), acc_train.item(),
                loss_val.item(), acc_val.item()))
        return loss_train.item(), loss_val.item()

    def test():
        model.eval()
        output = model(features, adj)
        loss_test = F.nll_loss(output[idx_test], labels[idx_test])
        acc_test = accuracy(output[idx_test], labels[idx_test])
        print("Test set results:", "loss= {:.4f}".format(loss_test.item()),
              "accuracy= {:.4f}".format(acc_test.item()))

    losses = {}
    pbar = tqdm(range(args.epochs))
    for epoch in pbar:
        loss_train, loss_val = train(epoch)

        if epoch % 10 == 0:
            if len(losses) == 0:
                losses['train'] = [loss_train]
                losses['val'] = [loss_val]

            else:
                losses['train'].append(loss_train)
                losses['val'].append(loss_val)

    f, ax = plt.subplots()

    train_loss = ax.plot(losses['train'], label='Train Loss')
    val_loss = ax.plot(losses['val'], label='Validation Loss')

    ax.legend()
    ax.set_xlabel('Epoch / 10')
    ax.set_ylabel('Loss')

    plt.savefig('results/loss_{}_{}.png'.format(args.model, args.dataset),
                dpi=300)

    print('Optimization Finished!')

    test()