예제 #1
0
    def __init__(self,
                 state_size,
                 action_size,
                 hidden_layer=256,
                 hidden_layer1=256,
                 w_init=3e-3):
        super(Critic, self).__init__()

        self.cnn = nn.Sequential(
            nn.Conv2d(9, 32, 4, stride=2,
                      padding=1),  # batch_size * 32 * 46 * 46
            nn.ReLU(True),
            nn.Conv2d(32, 64, 4, stride=2, padding=1),
            nn.BatchNorm2d(64),
            nn.ReLU(True),
            nn.Conv2d(64, 128, 4, stride=2, padding=1),
            nn.BatchNorm2d(128),
            nn.ReLU(True),
            nn.Conv2d(128, 256, 4, stride=2, padding=1),
            nn.ReLU(True),
            Flatten())

        self.linear = nn.Linear(256 * 3 * 3 + action_size, hidden_layer)
        self.linear1 = nn.Linear(hidden_layer, hidden_layer1)
        self.linear2 = nn.Linear(hidden_layer1, action_size)

        self.linear2.weight.data.uniform_(-w_init, w_init)
        self.linear2.bias.data.uniform_(-w_init, w_init)
def main():
    # model = ResNet18(5).to(device)
    # trained_model = resnest50(pretrained=True)
    # trained_model =resnet101(pretrained=True)
    # print('children', *list(trained_model.children())[:-1])
    trained_model = torch.hub.load('zhanghang1989/ResNeSt',
                                   'resnest269',
                                   pretrained=True)
    model = nn.Sequential(
        *list(trained_model.children())[:-1],  # [b, 512, 1, 1]   [32,2048]
        #                           list(trained_model.children())[-2].view(32, 2048,1,1),
        # nn.BatchNorm2d(2048),
        Flatten(),  # [b, 512, 1, 1] => [b, 512]
        # torch.nn.Dropout(0.3),

        #                           nn.Linear(2048, 1000),
        #                           nn.Dropout(0.1),
        #                           nn.Linear(1000,500),
        #                           nn.Dropout(0.1),
        #                           nn.Linear(500, 1)
        # nn.ReLU(inplace=True),
        # torch.nn.Dropout(0.5),
        # nn.Linear(1024, 1),
        # nn.ReLU(inplace=True)
        # nn.ReLU6(inplace=True)
        nn.Linear(2048, 1),
        #                         nn.Linear(1000, 2000),

        #                           nn.Linear(2000, 1),
        # nn.LeakyReLU(inplace=True),
    ).to(device)
    model.load_state_dict(torch.load('best06301936v0.mdl'))

    print('loaded from ckpt!')

    names, val_y_true, val_preds = evalute(model, val_loader)
    #     names=list(names)
    #     val_y_true=list(val_y_true)
    #     val_preds=list(val_preds)
    from pandas.core.frame import DataFrame

    c = {
        "names": names,
        "val_y_true": val_y_true,
        "val_preds": val_preds
    }  # 将列表a,b转换成字典
    data = DataFrame(c)  # 将字典转换成为数据框
    print(data)
    # a.to_excel("val_values07011427.xls")
    # data.to_excel("val_values07020812.xls")
    # data.to_excel("val_values07020812.xls")
    # data.to_excel("val_values07021000.xls")  #最好的结果
    data.to_excel("val_values07061911.xls")
예제 #3
0
def main():
    trained_model = resnest50(pretrained=True)
    # #     trained_model = resnet18(pretrained=True)
    model = nn.Sequential(
        *list(trained_model.children())[:-1],  # [b, 512, 1, 1]
        Flatten(),  # [b, 512, 1, 1] => [b, 512]
        # nn.Linear(2048, 512),
        # nn.Linear(512, 2),
        nn.BatchNorm1d(2048, affine=False),
        nn.Dropout(0.29),
        nn.Linear(2048, 2),
        # nn.BatchNorm1d(2048, affine=False),
        # nn.Linear(248, 2),
        nn.Softmax(dim=1)).to(device)
    # model.load_state_dict(torch.load('Resnest50_0205_1112.mdl'))  #Resnest50_0205_1112
    model.load_state_dict(torch.load('Resnest50_0308.mdl'))
    # print("model",model)

    # Inhosp_Nos,EXAM_NOs, y_trues,y_probs,y_preds,confusionmatrix=Test(model,val_loader,0.9)
    # Inhosp_Nos,EXAM_NOs, y_trues,y_probs,y_preds,confusionmatrix=Train_Test(model,train_loader,0.9)
    Inhosp_Nos, EXAM_NOs, y_trues, y_probs, y_preds, confusionmatrix = Test(
        model, test_loader, 0.2)

    #     train_AUC=confusion_matrixes(model,train_loader)
    #     print("train_AUC=",train_AUC)
    AUC = confusion_matrixes(model, test_loader)
    print("train_AUC=", AUC)

    from pandas.core.frame import DataFrame

    c = {
        "Inhosp_Nos": Inhosp_Nos,
        "EXAM_NOs": EXAM_NOs,
        "y_trues": y_trues,
        "y_probs": y_probs,
        "y_preds": y_preds
    }  # 将列表a,b转换成字典
    data = DataFrame(c)  # 将字典转换成为数据框
    # data.to_csv("data_train_0214.csv") 使用
    # data.to_csv("data_val_0214.csv")  # 使用
    data.to_csv("data_test_resnest50_2021_0308.csv")  # 使用
    print("=========文预测结束==============")
    print(data)
예제 #4
0
def discriminator(opts, output_size):
    """
    From https://arxiv.org/abs/1511.06434.pdf
    """
    model = nn.Sequential(
        nn.Conv2d(1, 32, 4, 2, 1), #1
        nn.InstanceNorm2d(32),
        nn.LeakyReLU(0.2, True),
        nn.Dropout3d(opts.dropout),
        nn.Conv2d(32, 32, 4, 2, 1), #2
        nn.InstanceNorm2d(32),
        nn.LeakyReLU(0.2, True),
        nn.Dropout3d(opts.dropout),
        nn.Conv2d(32, 64, 4, 2, 1), #3
        nn.InstanceNorm2d(64),
        nn.LeakyReLU(0.2, True),
        nn.Dropout3d(opts.dropout),
        nn.Conv2d(64, 64, 4, 2, 1), #4
        nn.InstanceNorm2d(64),
        nn.LeakyReLU(0.2, True),
        nn.Dropout3d(opts.dropout),
        nn.Conv2d(64, 128, 4, 2, 1), #5
        nn.InstanceNorm2d(128),
        nn.LeakyReLU(0.2, True),
        nn.Dropout3d(opts.dropout),
        nn.Conv2d(128, 128, 5,1, 1),
        nn.InstanceNorm2d(128),
        nn.LeakyReLU(0.2, True),
        nn.Dropout3d(opts.dropout),

        Flatten(),
        nn.Linear(128 * 29, 32 * 29),
        nn.LeakyReLU(0.2, True),
        nn.Dropout(opts.dropout),
        nn.Linear(32 * 29, 16 * 29),
        nn.LeakyReLU(0.2, True),
        nn.Dropout(opts.dropout),
        nn.Linear(16 * 29, output_size),
    )

    return model
def main():
    trained_model = resnest50(pretrained=True)
    # #     trained_model = resnet18(pretrained=True)
    model = nn.Sequential(
        *list(trained_model.children())[:-1],  # [b, 512, 1, 1]
        Flatten(),  # [b, 512, 1, 1] => [b, 512]
        # nn.Linear(2048, 512),
        # nn.Linear(512, 2),
        nn.BatchNorm1d(2048, affine=False),
        nn.Dropout(0.29),
        nn.Linear(2048, 2),
        # nn.BatchNorm1d(2048, affine=False),
        # nn.Linear(248, 2),
        nn.Softmax(dim=1)).to(device)
    # model.load_state_dict(torch.load('Resnest50_0205_1112.mdl'))  #Resnest50_0205_1112
    model.load_state_dict(torch.load('Resnest50_0308.mdl'))

    print("*******************计算验证集的混淆矩阵*****************************")
    optimal_threshold, point, Inhosp_Nos_val, EXAM_NOs_val, y_trues_val, y_probs_val, preds_need = Val_optimal_threshold(
        model, val_loader)
    # val_AUC = confusion_matrixes(model, val_loader)

    # print("optimal_threshold=",optimal_threshold)
    # print("point=", point)
    print("验证集样本预测中....")
    c = {
        "Inhosp_Nos": Inhosp_Nos_val,
        "EXAM_NOs": EXAM_NOs_val,
        "y_trues": y_trues_val,
        "y_probs": y_probs_val,
        "y_preds": preds_need
    }  # 将列表a,b转换成字典
    data = DataFrame(c)  # 将字典转换成为数据框
    data.to_csv("data_val_resnest50_2021_0309.csv")  # 使用
    print("=========验证集所有样本预测结束==============")

    print("*********样本筛选中******************")

    ###训练集的样本预测开始
    Inhosp_Nos_train, EXAM_NOs_train, y_trues_train, y_probs_train, y_preds_train, confusionmatrix_train = Train_Test(
        model, train_loader, optimal_threshold)
    train_AUC = Train_confusion_matrixes(model, train_loader)
    print("train_AUC=", train_AUC)

    c = {
        "Inhosp_Nos": Inhosp_Nos_train,
        "EXAM_NOs": EXAM_NOs_train,
        "y_trues": y_trues_train,
        "y_probs": y_probs_train,
        "y_preds": y_preds_train
    }  # 将列表a,b转换成字典
    data = DataFrame(c)  # 将字典转换成为数据框
    # data.to_csv("data_train_0214.csv") 使用
    # data.to_csv("data_val_0214.csv")  # 使用
    data.to_csv("data_train_resnest50_2021_0309.csv")  # 使用
    print("=========训练集所有样本预测结束==============")

    print("*********计算测试集的预测结果******************")
    Inhosp_Nos_test, EXAM_NOs_test, y_trues_test, y_probs_test, y_preds_test, confusionmatrix_test = Test(
        model, test_loader, optimal_threshold)
    test_AUC = confusion_matrixes(model, test_loader)
    print("test_AUC=", test_AUC)

    c = {
        "Inhosp_Nos": Inhosp_Nos_test,
        "EXAM_NOs": EXAM_NOs_test,
        "y_trues": y_trues_test,
        "y_probs": y_probs_test,
        "y_preds": y_preds_test
    }  # 将列表a,b转换成字典
    data = DataFrame(c)  # 将字典转换成为数据框
    data.to_csv("data_test_resnest50_2021_0309.csv")  # 使用

    print("**********End**********")
예제 #6
0
def main():
    # model = ResNet18(5).to(device)
    trained_model = resnest50(pretrained=True)
    # trained_model =resnet101(pretrained=True)
    # print('children', *list(trained_model.children())[:-1])

    model = nn.Sequential(*list(trained_model.children())[:-2],  # [b, 512, 1, 1]   [32,2048]
                          #                           list(trained_model.children())[-2].view(32, 2048,1,1),
                          nn.BatchNorm2d(2048),
                          Flatten(),  # [b, 512, 1, 1] => [b, 512]
                          # torch.nn.Dropout(0.5),

                          nn.Linear(2048 * 49, 1),
                          # nn.ReLU(inplace=True),
                          # torch.nn.Dropout(0.5),
                          # nn.Linear(1024, 1),
                          nn.ReLU(inplace=True)
                          ).to(device)

    model.train()

    # optimizer = optim.Adam(model.parameters(), lr=lr)
    optimizer = optim.Adam(model.parameters(), lr=lr)
    # optimizer=optim.SGD(model.parameters(),lr=lr,momentum=0.99,weight_decay=0.01)
    # scheduler=torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=10, verbose=False, threshold=0.0001, threshold_mode='rel', cooldown=0, min_lr=0, eps=1e-08)

    #     criteon = nn.CrossEopyLoss()
    criteon = torch.nn.MSELoss()

    # criteon = nn.L1Loss()

    #     for t in range(200):
    #     prediction = model(x)
    #     loss = loss_func(prediction, y)

    #     optimizer.zero_grad()
    #     loss.backward()
    #     optimizer.step()

    #     if t%5 == 0:
    #         plt.cla()
    #         plt.scatter(x.data.numpy(), y.data.numpy()) # 画散点图
    #         plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5) # 画拟合曲线
    #         plt.text(0.5, 0, 'Loss=%.4f' % loss.data[0], fontdict={'size':20,'color':'red'}) # 显示损失数值
    #         plt.pause(0.1)

    # # 如果在脚本中使用ion()命令开启了交互模式,没有使用ioff()关闭的话,则图像会一闪而过,并不会常留。要想防止这种情况,需要在plt.show()之前加上ioff()命令。
    # plt.ioff()
    # plt.show()

    best_loss, best_epoch = 5, 0
    global_step = 0
    viz.line([1], [-1], win='loss', opts=dict(title='loss'))
    viz.line([10], [-1], win='loss_val', opts=dict(title='loss_val'))
    #     train_loss1=[]
    #     val_loss1=[]

    for epoch in range(epochs):

        for step, (x, y) in enumerate(train_loader):
            # print("Enter"+str(epoch)+'time'+str(step))
            print("Enter: {} step {}".format(epoch, step))

            # x: [b, 3, 224, 224], y: [b]
            x, y = x.to(device), y.to(device)

            model.train()
            logits = model(x)
            loss = criteon(logits, y)
            # train_loss1.append(loss)

            # loss = criteon(logits, y)
            # loss=F.smooth_l1_loss(logits, y,size_average=False)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            viz.line([loss.item()], [global_step], win='loss', update='append')
            global_step += 1

        if epoch % 1 == 0:

            for x, y in val_loader:
                x, y = x.to(device), y.to(device)
                with torch.no_grad():
                    logits_val = model(x)

                    #                     print('logits_val',logits_val)
                    #                     print("y_true",y)
                    loss_val = criteon(logits_val, y)
                    # val_loss1.append(loss_val)
                    # scheduler.step(loss_val)
                    #                     r2=r2_score(logits_val, y)
                    #                     mean_absolute_error=mean_absolute_error(logits_val, y)
                    #                     mean_squared_error=mean_squared_error(logits_val, y)
                    #                     print("r2",r2)
                    #                     print("mean_absolute_error",mean_absolute_error)
                    #                     print("mean_squared_error",mean_squared_error)
                    # loss_val = criteon(logits_val, y)
                    # loss_val =F.smooth_l1_loss(logits_val, y,size_average=False)

                    if loss_val.item() < best_loss:
                        best_epoch = epoch
                        best_loss = loss_val

                        torch.save(model.state_dict(), 'best.mdl')

                        viz.line([loss_val.item()], [global_step], win='loss_val', update='append')

    print('best loss:', best_loss, 'best epoch:', best_epoch)

    model.load_state_dict(torch.load('best.mdl'))
    print('loaded from ckpt!')
    # train_db = Data('./result1', 224, mode='train')
    # val_db = Data('./result1', 224, mode='val')

    train_loader1 = DataLoader(train_db, batch_size=140, shuffle=True,
                               num_workers=0)
    val_loader1 = DataLoader(val_db, batch_size=60, num_workers=0)
    for x, y in train_loader1:
        criteon = torch.nn.MSELoss()
        x, y = x.to(device), y.to(device)
        print(x.shape)
        with torch.no_grad():
            logits_train = model(x)
            # print('logit_train',logits_train)
            logits_train = logits_train.squeeze(1)
            logits_train = logits_train.cpu().numpy()
            y = y.cpu().numpy()
            r2 = r2_score(y, logits_train)
            print("r2", r2)
            mse = mean_squared_error(y, logits_train)
            # loss = criteon(y, logits_train)
            # print("loss",loss)
            print("mse", mse)
            print('logits_train', logits_train)
            # logits_train=pd.DataFrame(logits_train)
            print('train_y', y)

    for x, y in val_loader1:
        x, y = x.to(device), y.to(device)
        print(x.shape)
        with torch.no_grad():
            logits_val = model(x)
            logits_val = logits_val.squeeze(1)
            logits_val = logits_val.cpu().numpy()
            y = y.cpu().numpy()
            r2 = r2_score(y, logits_val)
            print("r2", r2)
            mse = mean_squared_error(y, logits_val)
            print("mse", mse)
            print('logits_val', logits_val)
            # print('logits_train',logits_train)
            # logits_train=pd.DataFrame(logits_train)
            print('test_y', y)
def main():
    trained_model = resnest50(pretrained=True)
    # #     trained_model = resnet18(pretrained=True)
    model = nn.Sequential(
        *list(trained_model.children())[:-1],  # [b, 512, 1, 1]
        Flatten(),  # [b, 512, 1, 1] => [b, 512]
        # nn.Linear(2048, 512),
        # nn.Linear(512, 2),
        nn.BatchNorm1d(2048, affine=False),
        nn.Dropout(0.29),
        nn.Linear(2048, 2),
        # nn.BatchNorm1d(2048, affine=False),
        # nn.Linear(248, 2),
        nn.Softmax(dim=1)).to(device)
    # model.load_state_dict(torch.load('Resnest50_0205_1112.mdl'))  #Resnest50_0205_1112
    model.load_state_dict(torch.load('Resnest50_0308.mdl'))

    thresholds = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]

    print("*******************计算验证集的混淆矩阵*****************************")
    Inhosp_Nos_val, y_trues_val, y_probs_val, y_preds_val = Test(
        model, val_loader)

    sensitivities = []
    specificities = []
    for i, item in enumerate(thresholds):
        # Yuedens=0
        print('第{}th thresholds{}'.format(i, item))

        preds = []
        for j in y_probs_val:

            if j > item:
                preds.append(1)

            else:
                preds.append(0)
        confusion_matrix0 = confusion_matrix(y_trues_val, preds)
        print(confusion_matrix0)
        sensitivity = confusion_matrix0[1, 1] / (confusion_matrix0[1, 0] +
                                                 confusion_matrix0[1, 1])
        specificity = confusion_matrix0[0, 0] / (confusion_matrix0[0, 0] +
                                                 confusion_matrix0[0, 1])

        sensitivities.append(sensitivity)
        specificities.append(specificity)
    sensitivities = np.array(sensitivities)
    specificities = np.array(specificities)
    optimal_threshold, point = Find_Optimal_Cutoff(sensitivities,
                                                   specificities, thresholds)

    print('optimal_threshold=', optimal_threshold)
    print("point=", point)

    preds_val = []
    for j in y_probs_val:

        if j > optimal_threshold:
            preds_val.append(1)

        else:
            preds_val.append(0)

    val_AUC = confusion_matrixes(model, val_loader)
    print("val_AUC=", val_AUC)

    confusionmatrix_val = confusion_matrix(y_trues_val, preds_val)
    print('confusionmatrix_val', confusionmatrix_val)

    print("*********计算训练集的混淆矩阵******************")
    ###训练集的混淆矩阵
    Inhosp_Nos_train, y_trues_train, y_probs_train, y_preds_train = Train_Test(
        model, train_loader)

    preds_train = []
    for j in y_probs_train:

        if j > optimal_threshold:
            preds_train.append(1)

        else:
            preds_train.append(0)

    confusionmatrix_train = confusion_matrix(y_trues_train, preds_train)

    print('Confusionmatrix_train', confusionmatrix_train)

    train_AUC = Train_confusion_matrixes(model, train_loader)
    print("train_AUC=", train_AUC)

    print("*********计算测试集的混淆矩阵******************")

    test_AUC = confusion_matrixes(model, test_loader)
    print("test_AUC=", test_AUC)

    ###训练集的混淆矩阵
    Inhosp_Nos_test, y_trues_test, y_probs_test, y_preds_test = Test(
        model, test_loader)

    preds_test = []
    for j in y_probs_test:

        if j > optimal_threshold:
            preds_test.append(1)

        else:
            preds_test.append(0)

    confusionmatrix_test = confusion_matrix(y_trues_test, preds_test)
    print('confusionmatrix_test', confusionmatrix_test)
def main():
    trained_model = resnest50(pretrained=True)
    # #     trained_model = resnet18(pretrained=True)
    model = nn.Sequential(*list(trained_model.children())[:-1],  # [b, 512, 1, 1]
                          Flatten(),  # [b, 512, 1, 1] => [b, 512]
                          # nn.Linear(2048, 512),
                          # nn.Linear(512, 2),
                          nn.BatchNorm1d(2048, affine=False),
                          nn.Dropout(0.29),
                          nn.Linear(2048, 2),
                          # nn.BatchNorm1d(2048, affine=False),
                          # nn.Linear(248, 2),
                          nn.Softmax(dim=1)
                          ).to(device)

    optimizer = optim.Adam(model.parameters(), lr=0.008, betas=(0.9, 0.999), eps=1e-08, weight_decay=0.04,
                           amsgrad=False)
    lr_list = []
    for epoch in range(epochs):
        if epoch % 1 == 0:
            for p in optimizer.param_groups:
                p['lr'] *= 0.9
        lr_list.append(optimizer.state_dict()['param_groups'][0]['lr'])

    #
    # scheduler=torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1, patience=2,verbose=False, threshold=0.0001, threshold_mode='rel', cooldown=0, min_lr=0, eps=1e-08)
    # optimizer = optim.Adam(model.parameters(), lr=lr)
    criteon = nn.CrossEntropyLoss()
    # criteon = nn.BCELoss()

    best_acc, best_epoch = 0, 0
    global_step = 0
    viz.line([0], [-1], win='loss', opts=dict(title='loss'))
    viz.line([0], [-1], win='val_acc', opts=dict(title='val_acc'))
    train_losses = []
    train_accs = []
    val_losses = []
    val_accs = []

    start = time.time()
    for epoch in range(epochs):
        train_loss = 0.0

        for step, (Inhosp_No, EXAM_NO, x, y) in enumerate(train_loader):
            #             y_trains.extend(y)

            print("********Enter {} 的{} step ***********".format(epoch, step))
            # x: [b, 3, 224, 224], y: [b]
            x, y = x.to(device), y.to(device)

            model.train()
            logits = model(x)
            loss = criteon(logits, y)
            #             train_losses.append(loss.item())

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            viz.line([loss.item()], [global_step], win='loss', update='append')
            global_step += 1
            train_loss += loss.item() * x.size(0)
        train_loss = train_loss / len(train_loader.dataset)
        train_losses.append(train_loss)
        train_acc = evalute(model, train_loader)
        train_accs.append(train_acc)

        if epoch % 1 == 0:
            val_loss = 0.0
            for Inhosp_No, EXAM_NO, x, y in val_loader:
                # x, y = x.to(device), y.unsqueeze(1).to(device)
                x, y = x.to(device), y.to(device)
                #                 ys.extend(y)
                with torch.no_grad():
                    logits_val = model(x)
                    loss_val = criteon(logits_val, y)
                    # scheduler.step(loss_val)
                    # scheduler.step(loss_val)
                    val_loss += loss_val.item() * x.size(0)
            val_loss = val_loss / len(val_loader.dataset)
            # scheduler.step(val_loss)
            # print("val_loss",val_loss)
            val_losses.append(val_loss)
            val_acc = evalute(model, val_loader)
            val_accs.append(val_acc)

            if val_acc > best_acc:
                best_epoch = epoch
                best_acc = val_acc

                torch.save(model.state_dict(), 'Resnest50_0310.mdl')

                viz.line([val_acc], [global_step], win='val_acc', update='append')

    print("val_losses", val_losses)
    print("train_losses", train_losses)
    print('best acc:', best_acc, 'best epoch:', best_epoch)
    x1 = range(0, epochs)
    x2 = range(0, epochs)
    y1 = train_accs
    # y1 = Accuracy_list
    y2 = val_accs
    y3 = train_losses
    y4 = val_losses
    # y2 = Loss_list
    plt.subplot(2, 2, 1)
    plt.plot(x1, y1, color='lime')
    plt.plot(x1, y2, color='r')
    # plt.title('Cross_validation curve')
    plt.ylabel('Accuracy')
    # plt.xlabel('Epochs')
    plt.subplot(2, 2, 3)

    plt.plot(x2, y3, color='lime')
    plt.plot(x2, y4, color='r')
    plt.xlabel('Epochs')
    plt.ylabel('Cross-Entrotopy Loss')

    plt.subplot(2, 2, 4)
    plt.plot(range(epochs), lr_list, color='r')

    plt.ylabel('lr')

    plt.subplot(2, 2, 2)
    plt.plot(lr_list, val_losses, color='r')
    plt.ylabel('val_losses')

    plt.savefig("Resnest50_0310_accuracy_loss.pdf")
    plt.savefig("Resnest50_0310_accuracy_loss.svg")
    plt.show()

    model.load_state_dict(torch.load('Resnest50_0310.mdl'))
    print('loaded from ckpt!')

    test_acc = evalute(model, test_loader)
    print('test acc:', test_acc)
    torch.cuda.synchronize()
    end = time.time()
    GPU_time = end - start
    print("GPU_TIME=", GPU_time)
    num_params = sum(param.numel() for param in model.parameters())
    print('num_params', num_params)

    AUC_train = confusion_matrixes(model, train_loader)
    print("confusion_train", AUC_train)
    AUC_val = confusion_matrixes(model, val_loader)
    print("confusion_val", AUC_val)
    AUC_test = confusion_matrixes(model, test_loader)
    print("confusion_test", AUC_test)
예제 #9
0
import numpy as np
import argparse
import numpy as np
import matplotlib.pyplot as plt
from resnest.torch import resnest50
# net = resnest50(pretrained=True)
import torch.nn.functional as F

trained_model = torch.hub.load('zhanghang1989/ResNeSt',
                               'resnest269',
                               pretrained=True)
model = nn.Sequential(
    *list(trained_model.children())[:-1],  # [b, 512, 1, 1]   [32,2048]
    #                           list(trained_model.children())[-2].view(32, 2048,1,1),
    # nn.BatchNorm2d(2048),
    Flatten(),  # [b, 512, 1, 1] => [b, 512]
    # torch.nn.Dropout(0.3),

    #                           nn.Linear(2048, 1000),
    #                           nn.Dropout(0.1),
    #                           nn.Linear(1000,500),
    #                           nn.Dropout(0.1),
    #                           nn.Linear(500, 1)
    # nn.ReLU(inplace=True),
    # torch.nn.Dropout(0.5),
    # nn.Linear(1024, 1),
    # nn.ReLU(inplace=True)
    # nn.ReLU6(inplace=True)
    nn.Linear(2048, 1000),
    nn.Linear(1000, 500),
    nn.Linear(500, 1)
예제 #10
0
def main():
    # model = ResNet18(5).to(device)

    # trained_model = resnet101(pretrained=True)
    # # print('children', *list(trained_model.children())[:-1])
    #
    # model = nn.Sequential(*list(trained_model.children())[:-1],  # [b, 512, 1, 1]
    #                       Flatten(),  # [b, 512, 1, 1] => [b, 512]
    #                       nn.Linear(2048, 2)
    #                       ).to(device)
    trained_model = torch.hub.load('zhanghang1989/ResNeSt',
                                   'resnest269',
                                   pretrained=True)
    model = nn.Sequential(
        *list(trained_model.children())[:-1],  # [b, 512, 1, 1]   [32,2048]
        #                           list(trained_model.children())[-2].view(32, 2048,1,1),
        # nn.BatchNorm2d(2048),
        Flatten(),  # [b, 512, 1, 1] => [b, 512]
        # torch.nn.Dropout(0.3),

        #                           nn.Linear(2048, 1000),
        #                           nn.Dropout(0.1),
        #                           nn.Linear(1000,500),
        #                           nn.Dropout(0.1),
        #                           nn.Linear(500, 1)
        # nn.ReLU(inplace=True),
        # torch.nn.Dropout(0.5),
        # nn.Linear(1024, 1),
        # nn.ReLU(inplace=True)
        # nn.ReLU6(inplace=True)
        nn.Linear(2048, 2),
        #                         nn.Linear(1000, 2000),

        #                           nn.Linear(2000, 1),
        # nn.LeakyReLU(inplace=True),
    ).to(device)
    model.train()

    optimizer = optim.Adam(model.parameters(), lr=lr)
    criteon = nn.CrossEntropyLoss()

    best_acc, best_epoch = 0, 0
    global_step = 0
    viz.line([0], [-1], win='loss', opts=dict(title='loss'))
    viz.line([0], [-1], win='val_acc', opts=dict(title='val_acc'))
    for epoch in range(epochs):

        for step, (x, y) in enumerate(train_loader):
            # print("Enter"+str(epoch)+'time'+str(step))
            print("Enter: {} step {}".format(epoch, step))

            # x: [b, 3, 224, 224], y: [b]
            x, y = x.to(device), y.to(device)

            model.train()
            logits = model(x)
            loss = criteon(logits, y)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            viz.line([loss.item()], [global_step], win='loss', update='append')
            global_step += 1

        if epoch % 1 == 0:

            val_acc = evalute(model, val_loader)
            if val_acc > best_acc:
                best_epoch = epoch
                best_acc = val_acc

                torch.save(model.state_dict(), 'best.mdl')

                viz.line([val_acc], [global_step],
                         win='val_acc',
                         update='append')

    print('best acc:', best_acc, 'best epoch:', best_epoch)

    model.load_state_dict(torch.load('best.mdl'))
    print('loaded from ckpt!')

    test_acc = evalute(model, test_loader)
    print('test acc:', test_acc)

    confusion, report, AUC = test_confusion(model, test_loader)
    print('confusion', confusion)
    print('classification_report', report)
    print('AUC', AUC)