Ejemplo n.º 1
0
    for i in range(1, len(bbb)):
        imidx = imidx + "." + bbb[i]
    tra_lbl_name_list.append(data_dir + tra_label_dir + imidx + label_ext)

print("---")
print("train images: ", len(tra_img_name_list))
print("train labels: ", len(tra_lbl_name_list))
print("---")

train_num = len(tra_img_name_list)

salobj_dataset = SalObjDataset(img_name_list=tra_img_name_list,
                               lbl_name_list=tra_lbl_name_list,
                               transform=transforms.Compose([
                                   RescaleT(320),
                                   RandomCrop(288),
                                   ToTensorLab(flag=0)
                               ]))
salobj_dataloader = DataLoader(salobj_dataset,
                               batch_size=batch_size_train,
                               shuffle=True,
                               num_workers=1)

net = U2NET(3, 1)
if torch.cuda.is_available():
    net.cuda()
    net = nn.DataParallel(net)
# ------- 4. define optimizer --------
print("---define optimizer...")
optimizer = optim.Adam(net.parameters(),
                       lr=0.001,
Ejemplo n.º 2
0
vgg_model = VGGNet(freeze_max=False)
net = FCN8s(vgg_model)

checkpoint = torch.load('baseline.pth')

net.load_state_dict(checkpoint['model_state_dict'])

device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device(
    'cpu')

net.to(device)

# train dataloader
scale = Rescale(int(1.5 * 230))
crop = RandomCrop(224)
rotate = RandomRotate(20.0)
norm = Normalize()
tupled = ToTupleTensor()
tupled_with_roi_align = ToRoIAlignTensor()

composed_for_tracking = transforms.Compose(
    [Rescale(224), norm, tupled_with_roi_align])

dataset = SISSDataset(num_slices=153,
                      num_scans=2,
                      root_dir=Path.cwd().parents[0],
                      transform=composed_for_tracking,
                      train=True)

dataloader = torch.utils.data.DataLoader(dataset,
Ejemplo n.º 3
0
def main():
    # ------- 2. set the directory of training dataset --------
    model_name = 'u2net'  #'u2netp'

    data_dir = 'C:\datasets'
    tra_image_dir = '\DUTS\DUTS-TR\DUTS-TR-Image\\'
    tra_label_dir = '\DUTS\DUTS-TR\DUTS-TR-Mask\\'

    image_ext = '.jpg'
    label_ext = '.png'

    model_dir = './saved_models/' + model_name + '/'

    epoch_num = 100000
    batch_size_train = 4
    batch_size_val = 1
    train_num = 0
    val_num = 0

    tra_img_name_list = glob.glob(data_dir + tra_image_dir + '*')
    print(tra_img_name_list)

    tra_lbl_name_list = []
    for img_path in tra_img_name_list:
        img_name = img_path.split("\\")[-1]

        aaa = img_name.split(".")
        bbb = aaa[0:-1]
        #去除后缀的图片名
        imidx = bbb[0]
        # print(imidx)
        for i in range(1, len(bbb)):
            imidx = imidx + "." + bbb[i]
            print(imidx, "**********")

        tra_lbl_name_list.append(data_dir + tra_label_dir + imidx + label_ext)

    print("---")
    print("train images: ", len(tra_img_name_list))
    print("train labels: ", len(tra_lbl_name_list))
    print("---")

    train_num = len(tra_img_name_list)

    salobj_dataset = SalObjDataset(img_name_list=tra_img_name_list,
                                   lbl_name_list=tra_lbl_name_list,
                                   transform=transforms.Compose([
                                       RescaleT(320),
                                       RandomCrop(288),
                                       ToTensorLab(flag=0)
                                   ]))
    salobj_dataloader = DataLoader(salobj_dataset,
                                   batch_size=batch_size_train,
                                   shuffle=True,
                                   num_workers=1)

    # ------- 3. define model --------
    # define the net
    if (model_name == 'u2net'):
        net = U2NET(3, 1)
    elif (model_name == 'u2netp'):
        net = U2NETP(3, 1)

    if torch.cuda.is_available():
        net.cuda()

    # ------- 4. define optimizer --------
    print("---define optimizer...")
    optimizer = optim.Adam(net.parameters(),
                           lr=0.001,
                           betas=(0.9, 0.999),
                           eps=1e-08,
                           weight_decay=0)
    net.pool34.parameters()
    # ------- 5. training process --------
    print("---start training...")
    ite_num = 0
    running_loss = 0.0
    running_tar_loss = 0.0
    ite_num4val = 0
    save_frq = 2000  # save the model every 2000 iterations

    for epoch in range(0, epoch_num):
        net.train()

        for i, data in enumerate(salobj_dataloader):
            ite_num = ite_num + 1
            ite_num4val = ite_num4val + 1

            inputs, labels = data['image'], data['label']

            inputs = inputs.type(torch.FloatTensor)
            labels = labels.type(torch.FloatTensor)

            # wrap them in Variable
            if torch.cuda.is_available():
                # A = nn.Parameter(inputs,requires_grad=True)

                inputs_v, labels_v = Variable(inputs.cuda(),
                                              requires_grad=False), Variable(
                                                  labels.cuda(),
                                                  requires_grad=False)
            else:
                inputs_v, labels_v = Variable(
                    inputs, requires_grad=False), Variable(labels,
                                                           requires_grad=False)

            # y zero the parameter gradients
            optimizer.zero_grad()

            # forward + backward + optimize
            d0, d1, d2, d3, d4, d5, d6 = net(inputs_v)
            loss2, loss = muti_bce_loss_fusion(d0, d1, d2, d3, d4, d5, d6,
                                               labels_v)

            loss.backward()
            optimizer.step()

            # # print statistics
            running_loss += loss.item()
            running_tar_loss += loss2.item()

            # delete temporary outputs and loss
            del d0, d1, d2, d3, d4, d5, d6, loss2, loss

            print(
                "[epoch: %3d/%3d, batch: %5d/%5d, ite: %d] train loss: %3f, tar: %3f "
                % (epoch + 1, epoch_num,
                   (i + 1) * batch_size_train, train_num, ite_num,
                   running_loss / ite_num4val, running_tar_loss / ite_num4val))

            if ite_num % save_frq == 0:

                # torch.save(net.state_dict(), model_dir + model_name+"_bce_itr_%d_train_%3f_tar_%3f.pth" % (ite_num, running_loss / ite_num4val, running_tar_loss / ite_num4val))
                running_loss = 0.0
                running_tar_loss = 0.0
                net.train()  # resume train
                ite_num4val = 0