Exemplo n.º 1
0
    def _speech_to_text(self, raw_data, token, lang="ja-JP", samplerate=16000):
        log.debug("Start.")

        # Bing Speech API呼び出し
        url = self.config["wav2textURL"] + "?" + urllib.parse.urlencode(
            {"language": lang})
        headers = {
            "Content-type":
            "audio/wav; codec=audio/pcm; samplerate={0}".format(samplerate),
            #"Transfer-Encoding": "chunked",
            "Authorization":
            "Bearer " + token
        }
        log.info("Request -------------\n  url=" + url + "\n  headers=" +
                 str(headers))
        response = requests.post(url, data=raw_data, headers=headers)
        if not response.ok:
            raise response.raise_for_status()

        # 文字化け対策のためutf-8で処理する
        response.encoding = "utf-8"
        log.info("Response ------------\n  " + response.text)

        json = response.json()
        if ("Success" != json["RecognitionStatus"]):
            log.warn(json["RecognitionStatus"] + "が発生しました。")
            return ""
        result = response.json()["DisplayText"]

        print("End.")
        return result
Exemplo n.º 2
0
def train(args):
    experiment_dir = mkdir('./experiment/')
    checkpoints_dir = mkdir('./experiment/semseg/%s/'%(args.model_name))
    train_data, train_label, test_data, test_label = _load()

    dataset = S3DISDataLoader(train_data, train_label, data_augmentation = args.augment)
    dataloader = DataLoader(dataset, batch_size=args.batch_size,shuffle=True, num_workers=args.workers)
    
    test_dataset = S3DISDataLoader(test_data, test_label)
    testdataloader = DataLoader(test_dataset, batch_size=args.batch_size,shuffle=True, num_workers=args.workers)

    num_classes = 13
    if args.model_name == 'pointnet':
        model = PointNetSeg(num_classes, feature_transform=True, input_dims = 9)
    else:
        model = PointNet2SemSeg(num_classes, feature_dims = 6)

    torch.backends.cudnn.benchmark = True
    model = torch.nn.DataParallel(model).cuda()
    log.debug('Using gpu:',args.gpu)

    if args.pretrain is not None:
        log.debug('Use pretrain model...')
        model.load_state_dict(torch.load(args.pretrain))
        init_epoch = int(args.pretrain[:-4].split('-')[-1])
        log.debug('start epoch from', init_epoch)
    else:
        log.debug('Training from scratch')
        init_epoch = 0

    if args.optimizer == 'SGD':
        optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
    elif args.optimizer == 'Adam':
        optimizer = torch.optim.Adam(
            model.parameters(),
            lr=args.learning_rate,
            betas=(0.9, 0.999),
            eps=1e-08,
            weight_decay=args.decay_rate)
            
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=20, gamma=0.5)
    LEARNING_RATE_CLIP = 1e-5

    history = {'loss':[]}
    best_acc = 0
    best_meaniou = 0

    for epoch in range(init_epoch,args.epoch):
        scheduler.step()
        lr = max(optimizer.param_groups[0]['lr'],LEARNING_RATE_CLIP)

        log.info(job='semseg',model=args.model_name,gpu=args.gpu,epoch='%d/%s' % (epoch, args.epoch),lr=lr)
        
        for param_group in optimizer.param_groups:
            param_group['lr'] = lr
        
        for points, target in tqdm(dataloader, total=len(dataloader), smoothing=0.9, dynamic_ncols=True):
            points, target = points.float(), target.long()
            points = points.transpose(2, 1)
            points, target = points.cuda(), target.cuda()
            optimizer.zero_grad()
            model = model.train()

            if args.model_name == 'pointnet':
                pred, trans_feat = model(points)
            else:
                pred = model(points)

            pred = pred.contiguous().view(-1, num_classes)
            target = target.view(-1, 1)[:, 0]
            loss = F.nll_loss(pred, target)

            if args.model_name == 'pointnet':
                loss += feature_transform_reguliarzer(trans_feat) * 0.001

            history['loss'].append(loss.cpu().data.numpy())
            loss.backward()
            optimizer.step()
        
        log.debug('clear cuda cache')
        torch.cuda.empty_cache()

        test_metrics, cat_mean_iou = test_semseg(
            model.eval(), 
            testdataloader, 
            label_id_to_name,
            args.model_name,
            num_classes,
        )
        mean_iou = np.mean(cat_mean_iou)

        save_model = False
        if test_metrics['accuracy'] > best_acc:
            best_acc = test_metrics['accuracy']
        
        if mean_iou > best_meaniou:
            best_meaniou = mean_iou
            save_model = True
        
        if save_model:
            fn_pth = 'semseg-%s-%.5f-%04d.pth' % (args.model_name, best_meaniou, epoch)
            log.info('Save model...',fn = fn_pth)
            torch.save(model.state_dict(), os.path.join(checkpoints_dir, fn_pth))
            log.warn(cat_mean_iou)
        else:
            log.info('No need to save model')
            log.warn(cat_mean_iou)

        log.warn('Curr',accuracy=test_metrics['accuracy'], meanIOU=mean_iou)
        log.warn('Best',accuracy=best_acc, meanIOU=best_meaniou)
Exemplo n.º 3
0
def train(args):
    experiment_dir = mkdir('experiment/')
    checkpoints_dir = mkdir('experiment/%s/'%(args.model_name))
    
    kitti_utils = Semantic_KITTI_Utils(ROOT, subset = args.subset)
    class_names = kitti_utils.class_names
    num_classes = kitti_utils.num_classes

    if args.subset == 'inview':
        train_npts = 2000
        test_npts = 2500
    
    if args.subset == 'all':
        train_npts = 50000
        test_npts = 100000

    log.info(subset=args.subset, train_npts=train_npts, test_npts=test_npts)

    dataset = SemKITTI_Loader(ROOT, train_npts, train = True, subset = args.subset)
    dataloader = DataLoader(dataset, batch_size=args.batch_size, shuffle=True, 
                            num_workers=args.workers, pin_memory=True)
    
    test_dataset = SemKITTI_Loader(ROOT, test_npts, train = False, subset = args.subset)
    testdataloader = DataLoader(test_dataset, batch_size=int(args.batch_size/2), shuffle=False, 
                            num_workers=args.workers, pin_memory=True)

    if args.model_name == 'pointnet':
        model = PointNetSeg(num_classes, input_dims = 4, feature_transform = True)
    else:
        model = PointNet2SemSeg(num_classes, feature_dims = 1)

    if args.optimizer == 'SGD':
        optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
    elif args.optimizer == 'Adam':
        optimizer = torch.optim.Adam(
            model.parameters(),
            lr=args.learning_rate,
            betas=(0.9, 0.999),
            eps=1e-08,
            weight_decay=1e-4)

    torch.backends.cudnn.benchmark = True
    model = torch.nn.DataParallel(model)
    #use more than 1 gpu
    model.cuda()
    log.info('Using gpu:',args.gpu)
    
    if args.pretrain is not None:
        log.info('Use pretrain model...')
        model.load_state_dict(torch.load(args.pretrain))
        init_epoch = int(args.pretrain[:-4].split('-')[-1])
        log.info('Restart training', epoch=init_epoch)
    else:
        log.msg('Training from scratch')
        init_epoch = 0

    best_acc = 0
    best_miou = 0

    #->5.12 add
    # to show details of epoch training
    loss_list = []
    miou_list = []
    acc_list = []
    epoch_time = []
    lr_list = []
    #-<5.12 add

    for epoch in range(init_epoch,args.epoch):
        model.train()
        lr = calc_decay(args.learning_rate, epoch)
        log.info(model=args.model_name, gpu=args.gpu, epoch=epoch, lr=lr)
        
        for param_group in optimizer.param_groups:
            param_group['lr'] = lr
        
        for points, target in tqdm(dataloader, total=len(dataloader), smoothing=0.9, dynamic_ncols=True):
            points = points.float().transpose(2, 1).cuda()
            target = target.long().cuda()

            if args.model_name == 'pointnet':
                logits, trans_feat = model(points)
            else:
                logits = model(points)

            #logits = logits.contiguous().view(-1, num_classes)
            #target = target.view(-1, 1)[:, 0]
            #loss = F.nll_loss(logits, target)

            logits = logits.transpose(2, 1)
            loss = nn.CrossEntropyLoss()(logits, target)

            if args.model_name == 'pointnet':
                loss += feature_transform_reguliarzer(trans_feat) * 0.001

          #  loss_list.append(loss.item())

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
        
        torch.cuda.empty_cache()

        acc, miou = test_kitti_semseg(model.eval(), testdataloader,
                                    args.model_name,num_classes,class_names)

        # miou_list.append(np.asscalar(miou))
        # acc_list.append(np.asscalar(acc))
        save_model = False
        if acc > best_acc:
            best_acc = acc
        
        if miou > best_miou:
            best_miou = miou
            save_model = True
        
        #->5.12 add
        loss_list.append(loss.item())
        miou_list.append(np.asscalar(miou))
        acc_list.append(np.asscalar(acc))
        epoch_time.append(epoch)
        lr_list.append(lr)
        #->5.12 add
        if save_model:
            fn_pth = '%s-%.5f-%04d.pth' % (args.model_name, best_miou, epoch)
            log.info('Save model...',fn = fn_pth)
            torch.save(model.state_dict(), os.path.join(checkpoints_dir, fn_pth))
        else:
            log.info('No need to save model')
        # 3.31 add |>
        # show(args)
        # 3.31 add |<
        log.warn('Curr',accuracy=acc, mIOU=miou)
        log.warn('Best',accuracy=best_acc, mIOU=best_miou)
    # 5.15 add
    label_size = {"size":40}
    fig = plt.figure()
    plt.plot(epoch_time,loss_list,label = "loss")    
    plt.plot(epoch_time,miou_list,label = "mIOU")
    plt.plot(epoch_time,acc_list,label = "accuracy")
   # plt.plot(epoch_time,lr_list,label = "learning rate")
    plt.xlabel("epoch time", fontsize=40)
    plt.ylabel("value", fontsize=40)
    plt.title("training trendency", fontsize=60)
    plt.tick_params(labelsize=40)
    plt.legend(prop = label_size)
    plt.show()
Exemplo n.º 4
0
def train(args):
    experiment_dir = mkdir('experiment/')
    checkpoints_dir = mkdir('experiment/%s/' % (args.model_name))

    kitti_utils = Semantic_KITTI_Utils(KITTI_ROOT, subset=args.subset)
    class_names = kitti_utils.class_names
    num_classes = kitti_utils.num_classes

    if args.subset == 'inview':
        train_npts = 8000
        test_npts = 24000

    if args.subset == 'all':
        train_npts = 50000
        test_npts = 100000

    log.info(subset=args.subset, train_npts=train_npts, test_npts=test_npts)

    dataset = SemKITTI_Loader(KITTI_ROOT,
                              train_npts,
                              train=True,
                              subset=args.subset)
    dataloader = DataLoader(dataset,
                            batch_size=args.batch_size,
                            shuffle=True,
                            num_workers=args.workers,
                            pin_memory=True)

    test_dataset = SemKITTI_Loader(KITTI_ROOT,
                                   test_npts,
                                   train=False,
                                   subset=args.subset)
    testdataloader = DataLoader(test_dataset,
                                batch_size=int(args.batch_size / 2),
                                shuffle=False,
                                num_workers=args.workers,
                                pin_memory=True)

    if args.model_name == 'pointnet':
        model = PointNetSeg(num_classes, input_dims=4, feature_transform=True)
    else:
        model = PointNet2SemSeg(num_classes, feature_dims=1)

    if args.optimizer == 'SGD':
        optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
    elif args.optimizer == 'Adam':
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=args.learning_rate,
                                     betas=(0.9, 0.999),
                                     eps=1e-08,
                                     weight_decay=1e-4)

    torch.backends.cudnn.benchmark = True
    model = torch.nn.DataParallel(model)
    model.cuda()
    log.info('Using gpu:', args.gpu)

    if args.pretrain is not None:
        log.info('Use pretrain model...')
        model.load_state_dict(torch.load(args.pretrain))
        init_epoch = int(args.pretrain[:-4].split('-')[-1])
        log.info('Restart training', epoch=init_epoch)
    else:
        log.msg('Training from scratch')
        init_epoch = 0

    best_acc = 0
    best_miou = 0

    for epoch in range(init_epoch, args.epoch):
        model.train()
        lr = calc_decay(args.learning_rate, epoch)
        log.info(subset=args.subset,
                 model=args.model_name,
                 gpu=args.gpu,
                 epoch=epoch,
                 lr=lr)

        for param_group in optimizer.param_groups:
            param_group['lr'] = lr

        for points, target in tqdm(dataloader,
                                   total=len(dataloader),
                                   smoothing=0.9,
                                   dynamic_ncols=True):
            points = points.float().transpose(2, 1).cuda()
            target = target.long().cuda()

            if args.model_name == 'pointnet':
                logits, trans_feat = model(points)
            else:
                logits = model(points)

            #logits = logits.contiguous().view(-1, num_classes)
            #target = target.view(-1, 1)[:, 0]
            #loss = F.nll_loss(logits, target)

            logits = logits.transpose(2, 1)
            loss = nn.CrossEntropyLoss()(logits, target)

            if args.model_name == 'pointnet':
                loss += feature_transform_reguliarzer(trans_feat) * 0.001

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

        torch.cuda.empty_cache()

        acc, miou = test_kitti_semseg(model.eval(), testdataloader,
                                      args.model_name, num_classes,
                                      class_names)

        save_model = False
        if acc > best_acc:
            best_acc = acc

        if miou > best_miou:
            best_miou = miou
            save_model = True

        if save_model:
            fn_pth = '%s-%s-%.5f-%04d.pth' % (args.model_name, args.subset,
                                              best_miou, epoch)
            log.info('Save model...', fn=fn_pth)
            torch.save(model.state_dict(),
                       os.path.join(checkpoints_dir, fn_pth))
        else:
            log.info('No need to save model')

        log.warn('Curr', accuracy=acc, mIOU=miou)
        log.warn('Best', accuracy=best_acc, mIOU=best_miou)
Exemplo n.º 5
0
def train(args):
    experiment_dir = mkdir('./experiment/')
    checkpoints_dir = mkdir('./experiment/partseg/%s/' % (args.model_name))
    cache = _load(root)

    norm = True if args.model_name == 'pointnet' else False
    npoints = 2048
    train_ds = PartNormalDataset(root,
                                 cache,
                                 npoints=npoints,
                                 split='trainval',
                                 data_augmentation=args.augment)
    dataloader = DataLoader(train_ds,
                            batch_size=args.batch_size,
                            shuffle=True,
                            num_workers=int(args.workers))

    test_ds = PartNormalDataset(root, cache, npoints=npoints, split='test')
    testdataloader = DataLoader(test_ds,
                                batch_size=args.batch_size,
                                shuffle=False,
                                num_workers=int(args.workers))

    num_classes = 16
    num_part = 50
    log.info(len_training=len(train_ds), len_testing=len(test_ds))
    log.info(num_classes=num_classes, num_part=num_part)

    if args.model_name == 'pointnet':
        model = PointNetDenseCls(cat_num=num_classes, part_num=num_part)
    else:
        model = PointNet2PartSegMsg_one_hot(num_part)

    torch.backends.cudnn.benchmark = True
    model = torch.nn.DataParallel(model).cuda()
    log.debug('Using gpu:', args.gpu)

    if args.pretrain is not None and args.pretrain != 'None':
        log.debug('Use pretrain model...')
        model.load_state_dict(torch.load(args.pretrain))
        init_epoch = int(args.pretrain[:-4].split('-')[-1])
        log.debug('start epoch from', init_epoch)
    else:
        log.debug('Training from scratch')
        init_epoch = 0

    if args.optimizer == 'SGD':
        optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
    elif args.optimizer == 'Adam':
        optimizer = torch.optim.Adam(model.parameters(),
                                     lr=args.learning_rate,
                                     betas=(0.9, 0.999),
                                     eps=1e-08,
                                     weight_decay=args.decay_rate)

    scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                                step_size=20,
                                                gamma=0.5)

    history = {'loss': []}
    best_acc = 0
    best_class_avg_iou = 0
    best_inctance_avg_iou = 0
    LEARNING_RATE_CLIP = 1e-5

    # criterion = PointNetLoss()
    def feature_transform_reguliarzer(trans):
        d = trans.size()[1]
        I = torch.eye(d)[None, :, :]
        if trans.is_cuda:
            I = I.cuda()
        loss = torch.mean(
            torch.norm(torch.bmm(trans,
                                 trans.transpose(2, 1) - I), dim=(1, 2)))
        return loss

    def PointNet_Loss(labels_pred, label, seg_pred, seg, trans_feat):
        mat_diff_loss_scale = 0.001
        weight = 1
        seg_loss = F.nll_loss(seg_pred, seg)
        mat_diff_loss = feature_transform_reguliarzer(trans_feat)
        label_loss = F.nll_loss(labels_pred, label)
        loss = weight * seg_loss + (
            1 - weight) * label_loss + mat_diff_loss * mat_diff_loss_scale
        return loss, seg_loss, label_loss

    for epoch in range(init_epoch, args.epoch):
        scheduler.step()
        lr = max(optimizer.param_groups[0]['lr'], LEARNING_RATE_CLIP)
        log.info(job='partseg',
                 model=args.model_name,
                 gpu=args.gpu,
                 epoch='%d/%s' % (epoch, args.epoch),
                 lr=lr)

        for param_group in optimizer.param_groups:
            param_group['lr'] = lr

        for i, data in tqdm(enumerate(dataloader, 0),
                            total=len(dataloader),
                            smoothing=0.9):
            points, label, target, norm_plt = data
            points, label, target = points.float(), label.long(), target.long()
            points = points.transpose(2, 1)
            norm_plt = norm_plt.transpose(2, 1)
            points, label, target, norm_plt = points.cuda(), label.squeeze(
            ).cuda(), target.cuda(), norm_plt.cuda()
            optimizer.zero_grad()
            model = model.train()

            if args.model_name == 'pointnet':
                labels_pred, seg_pred, trans_feat = model(
                    points, to_categorical(label, 16))
                seg_pred = seg_pred.contiguous().view(-1, num_part)
                target = target.view(-1, 1)[:, 0]
                # loss, seg_loss, label_loss = criterion(labels_pred, label, seg_pred, target, trans_feat)
                loss, seg_loss, label_loss = PointNet_Loss(
                    labels_pred, label, seg_pred, target, trans_feat)
            else:
                seg_pred = model(points, norm_plt, to_categorical(label, 16))
                seg_pred = seg_pred.contiguous().view(-1, num_part)
                target = target.view(-1, 1)[:, 0]
                loss = F.nll_loss(seg_pred, target)

            history['loss'].append(loss.cpu().data.numpy())
            loss.backward()
            optimizer.step()

        log.debug('clear cuda cache')
        torch.cuda.empty_cache()

        test_metrics, test_hist_acc, cat_mean_iou = test_partseg(
            model.eval(),
            testdataloader,
            label_id_to_name,
            args.model_name,
            num_part,
        )

        save_model = False
        if test_metrics['accuracy'] > best_acc:
            best_acc = test_metrics['accuracy']

        if test_metrics['class_avg_iou'] > best_class_avg_iou:
            best_class_avg_iou = test_metrics['class_avg_iou']

        if test_metrics['inctance_avg_iou'] > best_inctance_avg_iou:
            best_inctance_avg_iou = test_metrics['inctance_avg_iou']
            save_model = True

        if save_model:
            fn_pth = 'partseg-%s-%.5f-%04d.pth' % (
                args.model_name, best_inctance_avg_iou, epoch)
            log.info('Save model...', fn=fn_pth)
            torch.save(model.state_dict(),
                       os.path.join(checkpoints_dir, fn_pth))
            log.info(cat_mean_iou)
        else:
            log.info('No need to save model')

        log.warn('Curr',
                 accuracy=test_metrics['accuracy'],
                 class_avg_mIOU=test_metrics['class_avg_iou'],
                 inctance_avg_mIOU=test_metrics['inctance_avg_iou'])

        log.warn('Best',
                 accuracy=best_acc,
                 class_avg_mIOU=best_class_avg_iou,
                 inctance_avg_mIOU=best_inctance_avg_iou)