Example #1
0
 def create_model(ema=False):
     # Network definition
     net = UNet(n_channels=3, n_classes=args.num_classes, bilinear=True)
     model = net.to(args.device)
     if ema:
         for param in model.parameters():
             param.detach_()
     return model
Example #2
0
def load_model(model_name, input_shape, classes):
    if model_name == 'FCN32':
        model = FCN32.FCN32(classes, input_shape).build()
    elif model_name == 'UNet':
        model = UNet.UNet(classes).build()
    elif model_name == 'GAN':
        model = GAN.Generator(classes)
    return model
Example #3
0
def create_prm_model(prm):
    """
    Select the desired model

    Input: str, ex: 'UNet'
    Output: function, ex UNet.Unet()
    """
    if prm['model'] == 'UNet':
        prm['model_func'] = UNet.Unet(prm)
    if prm['model'] == 'VCD':
        prm['model_func'] = VCD.VCD(prm)
    return (prm)
def example():
    vid_path = '77.pkl'

    with open(vid_path, 'rb') as file:
        video_list = pickle.load(file)['video']

    dataset = TrainDataset(video_list)
    dataloader = DataLoader(dataset, batch_size=18, shuffle=True)

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

    keypoint_model = UNet(6).to(device)
    reconstruct_model = UNet_Reconstruct(3, 6).to(device)

    train_keypoints(keypoint_model, reconstruct_model, dataloader)
Example #5
0
#    ### PREPRATION OF DATA FOR MODELS TO MATCH THE INPUT/OUTPUT DIMENSSIONS ###
a = train_geny[:, :, :, 0]
b = train_geny[:, :, :, 1]
aV = train_genyV[:, :, :, 0]
bV = train_genyV[:, :, :, 1]
a1 = Y1[:, :, :, 0]
b1 = Y1[:, :, :, 1]
a1V = Y1V[:, :, :, 0]
b1V = Y1V[:, :, :, 1]

M8 = train_geny[:, :, :, :, 0]
M8V = train_genyV[:, :, :, :, 0]
epochs = 40
Adam = optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.99)

model = UNet()
model.summary()
model.compile(optimizer=Adam, loss=dice_coef_loss, metrics=[dice_coef])
model.fit(train_genx,
          M8,
          validation_data=(train_genxV, M8V),
          batch_size=2,
          epochs=epochs)
model.save_weights("Carrot_Unet.h5")

model = segnet()
model.summary()
model.compile(optimizer=Adam, loss=dice_coef_loss, metrics=[dice_coef])
model.fit(train_genx,
          M8,
          validation_data=(train_genxV, M8V),
def main(_):
    writer = SummaryWriter(log_dir=opts.tb_log_dir + str(opts.alpha)  + '/' + opts.exp_name)

    torch.manual_seed(0)
    if opts.category in ['horse', 'tiger']:
        dataset = tf_final.TigDogDataset_Final(opts.root_dir, opts.category, transforms=None, normalize=False,
                                               max_length=None, remove_neck_kp=False, split='train',
                                               img_size=opts.img_size, mirror=False, scale=False, crop=False)

        collate_fn = tf_final.TigDog_collate

    directory = opts.tmp_dir + '/' + opts.category + '/'
    if not osp.exists(directory):
        os.makedirs(directory)

    save_counter = 0
    sample_to_vid = {}
    samples_per_vid = {}
    print('Number of videos for ', opts.category, '-', len(dataset))
    i_sample = 0
    for i_sample, sample in enumerate(dataset):
        num_frames = sample['video'].shape[0]
        for i in range(num_frames):
            new_sample = {}
            for k in sample.keys():
                if k in ['video', 'sfm_poses', 'landmarks', 'segmentations', 'bboxes']:
                    new_sample[k] = sample[k][i]

            pkl.dump(new_sample, open(directory + str(save_counter) + '.pkl', 'wb'))
            sample_to_vid[save_counter] = i_sample
            if i_sample in samples_per_vid:
                samples_per_vid[i_sample].append(save_counter)
            else:
                samples_per_vid[i_sample] = [save_counter]
            save_counter += 1
           # if i >= 5:  # 35:  # TODO:fix this
               # break
        #if i_sample >= 3:  # TODO:fix this
           # break

    training_samples = save_counter
    print('Training samples (frames):', training_samples)
    dataset = tigdog_mf.TigDogDataset_MultiFrame(opts.tmp_dir, opts.category, num_frames=opts.num_frames,
                                                 sample_to_vid=sample_to_vid,
                                                 samples_per_vid=samples_per_vid,
                                                 normalize=True, transforms=True,
                                                 remove_neck_kp=True, split='train', img_size=opts.img_size,
                                                 mirror=True, scale=True, crop=True, v2_crop=True, tight_bboxes=True)
    collate_fn = tigdog_mf.TigDog_collate

    dataloader = DataLoader(dataset, opts.batch_size, drop_last=True, shuffle=True,
                            collate_fn=collate_fn, num_workers=2)
    print('Dataloader:', len(dataloader))

    keypoint_model = UNet(opts.num_kps).cuda()
    reconstruct_model = UNet_Reconstruct(3, opts.num_kps).cuda()
    loss_fn_alex = lpips.LPIPS(net='alex').cuda()
    optimizer = optim.Adam(list(keypoint_model.parameters()) + list(reconstruct_model.parameters()), lr=opts.lr, weight_decay=opts.wd)
    std = opts.std
    n_iter = 0
    affine = RandomAffine(degrees=5, shear=(0.0,0.5))
    for epoch in range(opts.epochs):
        avg_loss = 0
        for sample in dataloader:
            input_img_tensor = sample['img'].type(torch.FloatTensor).clone().cuda()
            mask_3channels = torch.unsqueeze(sample['mask'], 2)
            mask_3channels = mask_3channels.repeat(1,1,3,1,1).clone().cuda()
            frame1 = input_img_tensor[:, 0] * mask_3channels[:,0]
            frame2 = input_img_tensor[:, 1] * mask_3channels[:,1]
            source = frame1
            target = frame2
            target_outputs = keypoint_model(target)
            result_x, result_y = xy_outputs(target_outputs, scaling=True, scale=16)
            result_kps = torch.cat([result_x, result_y], dim=1)
            result_kps_vis = torch.stack([result_x, result_y, torch.ones_like(result_y)], dim=-1)
            reconstruct = reconstruct_model(source, result_kps)
            target_mask = sample['mask'][:,1]
            mask_edt = np.stack([compute_dt(m) for m in target_mask])
            result_kps_xy = torch.dstack((result_x, result_y))
            edts_barrier = torch.tensor(mask_edt).float().unsqueeze(1).cuda()
            loss_mask = texture_dt_loss_v(result_kps_xy, edts_barrier)
            loss_reconstruction = loss_fn_alex.forward(reconstruct, change_range(target)).mean()
            loss = loss_reconstruction + (opts.alpha * loss_mask)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if n_iter % opts.vis_every == 0:
                kp_img = utils.kp2im(result_kps_vis[0].detach().cpu().numpy(), target[0].cpu().numpy(), radius=2) / 255
                kp_img = torch.from_numpy(kp_img).permute(2, 0, 1)[None]
                kp_img = kp_img.to(source.device)
                kp_mask = utils.kp2im(result_kps_vis[0].detach().cpu().numpy(), mask_3channels[0,1].cpu().numpy())
                kp_mask = torch.from_numpy(kp_mask).permute(2, 0, 1)[None]
                kp_mask = kp_mask.to(source.device)
                grid = torch.cat([source[:1], target[:1], kp_img[:1], kp_mask, change_range(reconstruct[:1], to_01=True)], dim=3)[0]
                writer.add_image('iter {n} of image (reconstruction, mask, loss) = ({r},{m},{l})   '.format(r=loss_reconstruction,m=loss_mask,l=loss,n=str(n_iter)), grid, n_iter)
            avg_loss += loss.item()
            writer.add_scalar('Loss/train std : ' + str(opts.std), loss, n_iter)
            n_iter += 1
        avg_loss = avg_loss / len(dataloader)
        print('Epoch ', epoch, ' average loss ', avg_loss)
    torch.save({
        'keypoint_state_dict' : keypoint_model.state_dict(),
        'reconstruct_state_dict' : reconstruct_model.state_dict()
        }, opts.model_state_dir + str(opts.alpha))
    writer.close()
Example #7
0
P1 = '/home/user01/data_ssd/Abbas/PAPER/Deeplab_results/paddy_millet/'

#I4,I8=imgT()
for i in range(40):
    IMG = I8[i, :, :, :].copy()
    IMG[np.where(resultV[i, :, :, 0] == 1)] = [0, 0, 1]
    IMG[np.where(resultV[i, :, :, 1] == 1)] = [1, 0, 0]
    cv2.imwrite(os.path.join(P1, str(i) + ".png"), IMG * 255)

model = fcn_8()
model.load_weights("_FCN_8s.h5")
result = model.predict(I8, batch_size=2)
resultF = np.zeros([I8.shape[0], 896, 896, 2])
resultF[np.where(result[:, :, :, :] >= .5)] = 1

model = UNet()
model.load_weights("_UNET14.h5")
result = model.predict(I8, batch_size=2)
resultU = np.zeros([I8.shape[0], 896, 896, 2])
resultU[np.where(result[:, :, :, :] >= .5)] = 1

model = segnet()
model.load_weights("BoniRob_SegNet.h5")
result = model.predict(I8, batch_size=2)
resultS = np.zeros([I8.shape[0], 896, 896, 2])
resultS[np.where(result[:, :, :, :] >= .5)] = 1

IoU_Crop = Crop_iou(resultS, M8)
IoU_Weed = Weed_iou(resultS, M8)
IoU_Mean = mean_iou(resultS, M8)
def trainUnet(dataset_tag,
              dataset_name,
              data_directory,
              input_dim,
              class_no,
              repeat,
              train_batchsize,
              validate_batchsize,
              num_epochs,
              learning_rate,
              width,
              depth,
              augmentation='all_flip',
              loss_f='dice'):
    """ This is the panel to control the training of baseline U-net.

    Args:
        input_dim: channel number of input image, for example, 3 for RGB
        class_no: number of classes of classification
        repeat: repat the same experiments with different stochastic seeds, we normally run each experiment at least 3 times
        train_batchsize: training batch size, this depends on the GPU memory
        validate_batchsize: we normally set-up as 1
        num_epochs: training epoch length
        learning_rate:
        input_height: resolution of input image
        input_width: resolution of input image
        alpha: regularisation strength hyper-parameter
        width: channel number of first encoder in the segmentation network, for the standard U-net, it is 64
        depth: down-sampling stages of the segmentation network
        data_path: path to where you store your all of your data
        dataset_tag: 'mnist' for MNIST; 'brats' for BRATS 2018; 'lidc' for LIDC lung data set
        label_mode: 'multi' for multi-class of proposed method; 'p_unet' for baseline probabilistic u-net; 'normal' for binary on MNIST; 'binary' for general binary segmentation
        loss_f: 'noisy_label' for our noisy label function, or 'dice' for dice loss
        save_probability_map: if True, we save all of the probability maps of output of networks

    Returns:

    """
    for j in range(1, repeat + 1):
        #
        Exp = UNet(in_ch=input_dim,
                   width=width,
                   depth=depth,
                   class_no=class_no,
                   norm='in',
                   dropout=False,
                   apply_last_layer=True)
        #
        Exp_name = 'UNet' + '_width' + str(width) + \
                   '_depth' + str(depth) + \
                   '_repeat' + str(j)
        #
        # ====================================================================================================================================================================
        trainloader, validateloader, testloader, data_length = getData(
            data_directory, dataset_name, dataset_tag, train_batchsize,
            validate_batchsize, augmentation)
        # ==================
        trainSingleModel(Exp,
                         Exp_name,
                         num_epochs,
                         data_length,
                         learning_rate,
                         dataset_tag,
                         train_batchsize,
                         trainloader,
                         validateloader,
                         testloader,
                         losstag=loss_f,
                         class_no=class_no)