()
Create the model and start the training.
Create the model and start the training.
Create the model and start the training .
def main():
    """Create the model and start the training."""

    if not os.path.exists(args.snapshot_dir):
        os.makedirs(args.snapshot_dir)

    writer = SummaryWriter(args.snapshot_dir)
    gpus = [int(i) for i in args.gpu.split(',')]
    if not args.gpu == 'None':
        os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu

    h, w = map(int, args.input_size.split(','))
    input_size = [h, w]

    cudnn.enabled = True
    # cudnn related setting
    cudnn.benchmark = True
    torch.backends.cudnn.deterministic = False
    torch.backends.cudnn.enabled = True
 

    deeplab = Res_Deeplab(num_classes=args.num_classes)

    # dump_input = torch.rand((args.batch_size, 3, input_size[0], input_size[1]))
    # writer.add_graph(deeplab.cuda(), dump_input.cuda(), verbose=False)

    saved_state_dict = torch.load(args.restore_from)
    new_params = deeplab.state_dict().copy()
    for i in saved_state_dict:
        i_parts = i.split('.')
        # print(i_parts)
        if not i_parts[0] == 'fc':
            new_params['.'.join(i_parts[0:])] = saved_state_dict[i]

    deeplab.load_state_dict(new_params)
   
    model = DataParallelModel(deeplab)
    model.cuda()

    criterion = CriterionAll()
    criterion = DataParallelCriterion(criterion)
    criterion.cuda()

    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])

    transform = transforms.Compose([
        transforms.ToTensor(),
        normalize,
    ])

    trainloader = data.DataLoader(LIPDataSet(args.data_dir, args.dataset, crop_size=input_size, transform=transform),
                                  batch_size=args.batch_size * len(gpus), shuffle=True, num_workers=2,
                                  pin_memory=True)
    #lip_dataset = LIPDataSet(args.data_dir, 'val', crop_size=input_size, transform=transform)
    #num_samples = len(lip_dataset)
    
    #valloader = data.DataLoader(lip_dataset, batch_size=args.batch_size * len(gpus),
    #                             shuffle=False, pin_memory=True)

    optimizer = optim.SGD(
        model.parameters(),
        lr=args.learning_rate,
        momentum=args.momentum,
        weight_decay=args.weight_decay
    )
    optimizer.zero_grad()

    total_iters = args.epochs * len(trainloader)
    for epoch in range(args.start_epoch, args.epochs):
        model.train()
        for i_iter, batch in enumerate(trainloader):
            i_iter += len(trainloader) * epoch
            lr = adjust_learning_rate(optimizer, i_iter, total_iters)

            images, labels, edges, _ = batch
            labels = labels.long().cuda(non_blocking=True)
            edges = edges.long().cuda(non_blocking=True)

            preds = model(images)

            loss = criterion(preds, [labels, edges])
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            if i_iter % 100 == 0:
                writer.add_scalar('learning_rate', lr, i_iter)
                writer.add_scalar('loss', loss.data.cpu().numpy(), i_iter)

            if i_iter % 500 == 0:

                images_inv = inv_preprocess(images, args.save_num_images)
                labels_colors = decode_parsing(labels, args.save_num_images, args.num_classes, is_pred=False)
                edges_colors = decode_parsing(edges, args.save_num_images, 2, is_pred=False)

                if isinstance(preds, list):
                    preds = preds[0]
                preds_colors = decode_parsing(preds[0][-1], args.save_num_images, args.num_classes, is_pred=True)
                pred_edges = decode_parsing(preds[1][-1], args.save_num_images, 2, is_pred=True)

                img = vutils.make_grid(images_inv, normalize=False, scale_each=True)
                lab = vutils.make_grid(labels_colors, normalize=False, scale_each=True)
                pred = vutils.make_grid(preds_colors, normalize=False, scale_each=True)
                edge = vutils.make_grid(edges_colors, normalize=False, scale_each=True)
                pred_edge = vutils.make_grid(pred_edges, normalize=False, scale_each=True)

                writer.add_image('Images/', img, i_iter)
                writer.add_image('Labels/', lab, i_iter)
                writer.add_image('Preds/', pred, i_iter)
                writer.add_image('Edges/', edge, i_iter)
                writer.add_image('PredEdges/', pred_edge, i_iter)

            print('iter = {} of {} completed, loss = {}'.format(i_iter, total_iters, loss.data.cpu().numpy()))

        torch.save(model.state_dict(), osp.join(args.snapshot_dir, 'LIP_epoch_' + str(epoch) + '.pth'))

        #parsing_preds, scales, centers = valid(model, valloader, input_size,  num_samples, len(gpus))

        #mIoU = compute_mean_ioU(parsing_preds, scales, centers, args.num_classes, args.data_dir, input_size)

        #print(mIoU)
        #writer.add_scalars('mIoU', mIoU, epoch)

    end = timeit.default_timer()
    print(end - start, 'seconds')
def main ( ) : if not os . path . exists ( args . snapshot_dir ) : os . makedirs ( args . snapshot_dir ) writer = SummaryWriter ( args . snapshot_dir ) gpus = [ int ( i ) for i in args . gpu . split ( ',' ) ] if not args . gpu == 'None' : os . environ [ "CUDA_VISIBLE_DEVICES" ] = args . gpu h , w = map ( int , args . input_size . split ( ',' ) ) input_size = [ h , w ] cudnn . enabled = True # cudnn related setting cudnn . benchmark = True torch . backends . cudnn . deterministic = False torch . backends . cudnn . enabled = True deeplab = Res_Deeplab ( num_classes = args . num_classes ) # dump_input = torch.rand((args.batch_size, 3, input_size[0], input_size[1])) # writer.add_graph(deeplab.cuda(), dump_input.cuda(), verbose=False) saved_state_dict = torch . load ( args . restore_from ) new_params = deeplab . state_dict ( ) . copy ( ) for i in saved_state_dict : i_parts = i . split ( '.' ) # print(i_parts) if not i_parts [ 0 ] == 'fc' : new_params [ '.' . join ( i_parts [ 0 : ] ) ] = saved_state_dict [ i ] deeplab . load_state_dict ( new_params ) model = DataParallelModel ( deeplab ) model . cuda ( ) criterion = CriterionAll ( ) criterion = DataParallelCriterion ( criterion ) criterion . cuda ( ) normalize = transforms . Normalize ( mean = [ 0.485 , 0.456 , 0.406 ] , std = [ 0.229 , 0.224 , 0.225 ] ) transform = transforms . Compose ( [ transforms . ToTensor ( ) , normalize , ] ) trainloader = data . DataLoader ( LIPDataSet ( args . data_dir , args . dataset , crop_size = input_size , transform = transform ) , batch_size = args . batch_size * len ( gpus ) , shuffle = True , num_workers = 2 , pin_memory = True ) #lip_dataset = LIPDataSet(args.data_dir, 'val', crop_size=input_size, transform=transform) #num_samples = len(lip_dataset) #valloader = data.DataLoader(lip_dataset, batch_size=args.batch_size * len(gpus), #                             shuffle=False, pin_memory=True) optimizer = optim . SGD ( model . parameters ( ) , lr = args . learning_rate , momentum = args . momentum , weight_decay = args . weight_decay ) optimizer . zero_grad ( ) total_iters = args . epochs * len ( trainloader ) for epoch in range ( args . start_epoch , args . epochs ) : model . train ( ) for i_iter , batch in enumerate ( trainloader ) : i_iter += len ( trainloader ) * epoch lr = adjust_learning_rate ( optimizer , i_iter , total_iters ) images , labels , edges , _ = batch labels = labels . long ( ) . cuda ( non_blocking = True ) edges = edges . long ( ) . cuda ( non_blocking = True ) preds = model ( images ) loss = criterion ( preds , [ labels , edges ] ) optimizer . zero_grad ( ) loss . backward ( ) optimizer . step ( ) if i_iter % 100 == 0 : writer . add_scalar ( 'learning_rate' , lr , i_iter ) writer . add_scalar ( 'loss' , loss . data . cpu ( ) . numpy ( ) , i_iter ) if i_iter % 500 == 0 : images_inv = inv_preprocess ( images , args . save_num_images ) labels_colors = decode_parsing ( labels , args . save_num_images , args . num_classes , is_pred = False ) edges_colors = decode_parsing ( edges , args . save_num_images , 2 , is_pred = False ) if isinstance ( preds , list ) : preds = preds [ 0 ] preds_colors = decode_parsing ( preds [ 0 ] [ - 1 ] , args . save_num_images , args . num_classes , is_pred = True ) pred_edges = decode_parsing ( preds [ 1 ] [ - 1 ] , args . save_num_images , 2 , is_pred = True ) img = vutils . make_grid ( images_inv , normalize = False , scale_each = True ) lab = vutils . make_grid ( labels_colors , normalize = False , scale_each = True ) pred = vutils . make_grid ( preds_colors , normalize = False , scale_each = True ) edge = vutils . make_grid ( edges_colors , normalize = False , scale_each = True ) pred_edge = vutils . make_grid ( pred_edges , normalize = False , scale_each = True ) writer . add_image ( 'Images/' , img , i_iter ) writer . add_image ( 'Labels/' , lab , i_iter ) writer . add_image ( 'Preds/' , pred , i_iter ) writer . add_image ( 'Edges/' , edge , i_iter ) writer . add_image ( 'PredEdges/' , pred_edge , i_iter ) print ( 'iter = {} of {} completed, loss = {}' . format ( i_iter , total_iters , loss . data . cpu ( ) . numpy ( ) ) ) torch . save ( model . state_dict ( ) , osp . join ( args . snapshot_dir , 'LIP_epoch_' + str ( epoch ) + '.pth' ) ) #parsing_preds, scales, centers = valid(model, valloader, input_size,  num_samples, len(gpus)) #mIoU = compute_mean_ioU(parsing_preds, scales, centers, args.num_classes, args.data_dir, input_size) #print(mIoU) #writer.add_scalars('mIoU', mIoU, epoch) end = timeit . default_timer ( ) print ( end - start , 'seconds' )