Ejemplo 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
Ejemplo n.º 2
0
    def  __enter__(self):
        ff_profile = webdriver.FirefoxProfile()
        ff_profile.set_preference("network.proxy.type", 2);
        ff_profile.set_preference("network.proxy.autoconfig_url", "http://wpad/wpad.dat")

        my_log.debug("selenium server:" + os.environ['SELENIUM_SERVER'])

        driver = webdriver.Remote(os.environ['SELENIUM_SERVER'],
                          browser_profile=ff_profile,
                          desired_capabilities=webdriver.DesiredCapabilities.FIREFOX.copy())

        driver.implicitly_wait(20) # seconds
        self.driver = driver
        return self.driver
Ejemplo n.º 3
0
def vis(args):
    test_data, test_label = load_data(root, train=False)
    log.info(test_data=test_data.shape, test_label=test_label.shape)

    log.debug('Building Model', args.model_name)
    if args.model_name == 'pointnet':
        num_class = 40
        model = PointNetCls(num_class, args.feature_transform).cuda()
    else:
        model = PointNet2ClsMsg().cuda()

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

    if args.pretrain is None:
        log.err('No pretrain model')
        return

    log.debug('Loading pretrain model...')
    checkpoint = torch.load(args.pretrain)
    model.load_state_dict(checkpoint)
    model.eval()

    log.info('Press space to exit, press Q for next frame')

    for idx in range(test_data.shape[0]):
        point_np = test_data[idx:idx + 1]
        gt = test_label[idx][0]

        points = torch.from_numpy(point_np)
        points = points.transpose(2, 1).cuda()

        pred, trans_feat = model(points)
        pred_choice = pred.data.max(1)[1]
        log.info(gt=class_names[gt],
                 pred_choice=class_names[pred_choice.cpu().numpy().item()])

        point_cloud = open3d.geometry.PointCloud()
        point_cloud.points = open3d.utility.Vector3dVector(point_np[0])

        vis = open3d.visualization.VisualizerWithKeyCallback()
        vis.create_window()
        vis.get_render_option().background_color = np.asarray([0, 0, 0])
        vis.add_geometry(point_cloud)
        vis.register_key_callback(32, lambda vis: exit())
        vis.run()
        vis.destroy_window()
Ejemplo n.º 4
0
    def wav_to_text(self, wav):
        log.debug("Start. wav=" + wav)

        # wavファイルを読み込む
        with open(wav, 'rb') as infile:
            raw_data = infile.read()

        # 認証トークンを取得
        token = self._authorize()

        # Speech to Textを呼び出す
        text = self._speech_to_text(raw_data, token)

        log.debug("End. return " + text)
        return text
Ejemplo n.º 5
0
def evaluate(args):
    cache = _load(root)
    norm = True if args.model_name == 'pointnet' else False
    test_ds = PartNormalDataset(root, cache, npoints=2048, split='test')
    testdataloader = DataLoader(test_ds,
                                batch_size=args.batch_size,
                                shuffle=False,
                                num_workers=int(args.workers))
    log.info("The number of test data is:", len(test_ds))

    log.info('Building Model', args.model_name)
    num_classes = 16
    num_part = 50
    if args.model_name == 'pointnet2':
        model = PointNet2PartSegMsg_one_hot(num_part)
    else:
        model = PointNetDenseCls(cat_num=num_classes, part_num=num_part)

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

    if args.pretrain is None:
        log.err('No pretrain model')
        return

    log.debug('Loading pretrain model...')
    state_dict = torch.load(args.pretrain)
    model.load_state_dict(state_dict)

    log.info('Testing pretrain model...')

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

    log.info('test_hist_acc', len(test_hist_acc))
    log.info(cat_mean_iou)
    log.info('Test Accuracy', '%.5f' % test_metrics['accuracy'])
    log.info('Class avg mIOU:', '%.5f' % test_metrics['class_avg_iou'])
    log.info('Inctance avg mIOU:', '%.5f' % test_metrics['inctance_avg_iou'])
Ejemplo n.º 6
0
def vis(args):
    test_data, test_label = _load(load_train = False)
    test_dataset = S3DISDataLoader(test_data,test_label)
    testdataloader = DataLoader(test_dataset, batch_size=args.batch_size,shuffle=False, num_workers=args.workers)

    log.debug('Building Model', args.model_name)
    num_classes = 13
    if args.model_name == 'pointnet2':
        model = PointNet2SemSeg(num_classes) 
    else:
        model = PointNetSeg(num_classes,feature_transform=True,semseg = True)

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

    if args.pretrain is None:
        log.err('No pretrain model')
        return

    log.debug('Loading pretrain model...')
    checkpoint = torch.load(args.pretrain)
    model.load_state_dict(checkpoint)
    model.eval()

    cmap = plt.cm.get_cmap("hsv", 13)
    cmap = np.array([cmap(i) for i in range(13)])[:, :3]

    for batch_id, (points, target) in enumerate(testdataloader):
        log.info('Press space to exit','press Q for next frame')
        batchsize, num_point, _ = points.size()
        points, target = Variable(points.float()), Variable(target.long())
        points = points.transpose(2, 1)
        points, target = points.cuda(), target.cuda()
        if args.model_name == 'pointnet2':
            pred = model(points)
        else:
            pred, _ = model(points)

        points = points[:, :3, :].transpose(-1, 1)
        pred_choice = pred.data.max(-1)[1]

        for idx in range(batchsize):
            pt, gt, pred = points[idx], target[idx], pred_choice[idx]
            gt_color = cmap[gt.cpu().numpy() - 1, :]
            pred_color = cmap[pred.cpu().numpy() - 1, :]

            point_cloud = open3d.geometry.PointCloud()
            point_cloud.points = open3d.utility.Vector3dVector(pt.cpu().numpy())
            point_cloud.colors = open3d.Vector3dVector(gt_color)

            vis = open3d.visualization.VisualizerWithKeyCallback()
            vis.create_window()
            vis.get_render_option().background_color = np.asarray([0, 0, 0])
            vis.add_geometry(point_cloud)

            vis.register_key_callback(32, lambda vis: exit())
            vis.run()
            vis.destroy_window()
Ejemplo n.º 7
0
def evaluate(args):
    test_data, test_label = _load(load_train = False)
    test_dataset = S3DISDataLoader(test_data,test_label)
    testdataloader = DataLoader(test_dataset, batch_size=args.batch_size,shuffle=True, num_workers=args.workers)

    log.debug('Building Model', args.model_name)
    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 None:
        log.err('No pretrain model')
        return

    log.debug('Loading pretrain model...')
    state_dict = torch.load(args.pretrain)
    model.load_state_dict(state_dict)

    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)
    log.info(Test_accuracy=test_metrics['accuracy'], Test_meanIOU=mean_iou)
Ejemplo n.º 8
0
def _load(root):
    fn_cache = 'experiment/data/shapenetcore_partanno_segmentation_benchmark_v0_normal.h5'
    if not os.path.exists(fn_cache):
        log.debug('Indexing Files...')
        fns_full = []
        fp_h5 = h5py.File(fn_cache, "w")

        for line in open(os.path.join(root, 'synsetoffset2category.txt'), 'r'):
            name, wordnet_id = line.strip().split()
            pt_folder = os.path.join(root, wordnet_id)
            log.info('Building', name, wordnet_id)
            for fn in tqdm(os.listdir(pt_folder)):
                token = fn.split('.')[0]
                fn_full = os.path.join(pt_folder, fn)
                data = np.loadtxt(fn_full).astype(np.float32)

                h5_index = '%s_%s' % (wordnet_id, token)
                fp_h5.create_dataset(h5_index, data=data)

        log.debug('Building cache...')
        fp_h5.close()

    log.debug('Loading from cache...')
    fp_h5 = h5py.File(fn_cache, 'r')
    cache = {}
    for token in fp_h5.keys():
        cache[token] = fp_h5.get(token)[()]
    return cache
Ejemplo n.º 9
0
def evaluate(args):
    test_data, test_label = load_data(
        'experiment/data/modelnet40_ply_hdf5_2048/', train=False)
    testDataset = ModelNetDataLoader(test_data, test_label)
    testDataLoader = torch.utils.data.DataLoader(testDataset,
                                                 batch_size=args.batch_size,
                                                 shuffle=False)

    log.debug('Building Model', args.model_name)
    if args.model_name == 'pointnet':
        num_class = 40
        model = PointNetCls(num_class, args.feature_transform)
    else:
        model = PointNet2ClsMsg()

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

    if args.pretrain is None:
        log.err('No pretrain model')
        return

    log.debug('Loading pretrain model...')
    state_dict = torch.load(args.pretrain)
    model.load_state_dict(state_dict)

    acc = test_clf(model.eval(), testDataLoader)
    log.msg(Test_Accuracy='%.5f' % (acc))
Ejemplo n.º 10
0
async def message(sid, data):
    my_log.debug("action name: " + data['name'] + " " + sid)

    def callback(res):
        app.loop.create_task(
            sio.emit('act.status', data=res, room=sid, namespace='/action'))

    callback({'text': 'action begin:' + data['name']})

    module = importlib.import_module(data['name'].lower())
    """in case I changed the module"""
    module = importlib.reload(module)

    def closesocket():
        callback({'result': 'ok', 'close': True})

    def action1():
        module.perform(data, callback)
        my_log.debug('schedule close socket')
        app.loop.call_later(30, closesocket)

    app.loop.run_in_executor(act_executor, action1)
Ejemplo n.º 11
0
    def add_comment(self, text):
        log.debug("Start. text=" + text)

        auth = self.firebase.auth()
        user = auth.sign_in_with_email_and_password(self.config["email"],
                                                    self.config["password"])
        print(user)

        # Firebase Functions呼び出し
        url = self.config["addCommentURL"]
        headers = {"Authorization": "Bearer " + user["idToken"]}
        data = {"email": self.config["email"], "content": text}
        log.info("Request -------------\n  url=" + url + "\n  headers=" +
                 str(headers) + "\n  data=" + str(data))
        response = requests.post(url, data=data, headers=headers)
        if not response.ok:
            raise response.raise_for_status()

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

        log.debug("End.")
Ejemplo n.º 12
0
    def _authorize(self):
        log.debug("Start.")
        url = self.config["authURL"]
        headers = {
            "Content-type": "application/x-www-form-urlencoded",
            "Content-Length": "0",
            "Ocp-Apim-Subscription-Key": self.config["apiKey"]
        }

        log.info("Request -------------\n  url=" + url + "\n  headers=" +
                 str(headers))
        response = requests.post(url, headers=headers)

        if not response.ok:
            log.debug("Response NG")
            response.raise_for_status()

        log.info("Response OK")
        log.debug("End.")
        return response.text
Ejemplo n.º 13
0
def vis(args):
    cache = _load(root)
    norm = True if args.model_name == 'pointnet' else False
    test_ds = PartNormalDataset(root, cache, npoints=2048, split='test')
    testdataloader = DataLoader(test_ds,
                                batch_size=args.batch_size,
                                shuffle=True,
                                num_workers=int(args.workers))
    log.info("The number of test data is:", len(test_ds))

    log.info('Building Model', args.model_name)
    num_classes = 16
    num_part = 50
    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)
    model.cuda()
    log.debug('Using multi GPU:', args.gpu)

    if args.pretrain is None:
        log.err('No pretrain model')
        return

    log.info('Loading pretrain model...')
    checkpoint = torch.load(args.pretrain)
    model.load_state_dict(checkpoint)

    log.info('Press space to exit, press Q for next frame')
    for batch_id, (points, label, target,
                   norm_plt) in enumerate(testdataloader):
        batchsize, num_point, _ = points.size()
        points, label, target, norm_plt = points.float(), label.long(
        ), target.long(), norm_plt.float()
        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()
        if args.model_name == 'pointnet':
            labels_pred, seg_pred, _ = model(points, to_categorical(label, 16))
        else:
            seg_pred = model(points, norm_plt, to_categorical(label, 16))
        pred_choice = seg_pred.max(-1)[1]
        log.info(seg_pred=seg_pred.shape, pred_choice=pred_choice.shape)
        log.info(seg_pred=seg_pred.shape, pred_choice=pred_choice.shape)

        cmap_plt = plt.cm.get_cmap("hsv", num_part)
        cmap_list = [cmap_plt(i)[:3] for i in range(num_part)]
        np.random.shuffle(cmap_list)
        cmap = np.array(cmap_list)

        #log.info('points',points.shape,'label',label.shape,'target',target.shape,'norm_plt',norm_plt.shape)
        for idx in range(batchsize):
            pt, gt, pred = points[idx].transpose(
                1, 0), target[idx], pred_choice[idx].transpose(-1, 0)
            # log.info('pt',pt.size(),'gt',gt.size(),'pred',pred.shape)

            gt_color = cmap[gt.cpu().numpy() - 1, :]
            pred_color = cmap[pred.cpu().numpy() - 1, :]

            point_cloud = open3d.geometry.PointCloud()
            point_cloud.points = open3d.utility.Vector3dVector(
                pt.cpu().numpy())
            point_cloud.colors = open3d.utility.Vector3dVector(pred_color)

            vis = open3d.visualization.VisualizerWithKeyCallback()
            vis.create_window()
            vis.get_render_option().background_color = np.asarray([0, 0, 0])
            vis.add_geometry(point_cloud)
            vis.register_key_callback(32, lambda vis: exit())
            vis.run()
            vis.destroy_window()
Ejemplo n.º 14
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)
Ejemplo n.º 15
0
def disconnect(sid):
    my_log.debug('disconnect ' + sid)
Ejemplo n.º 16
0
def connect(sid, environ):
    my_log.debug("connect " + sid)
Ejemplo n.º 17
0
    callback({'text': 'action begin:' + data['name']})

    module = importlib.import_module(data['name'].lower())
    """in case I changed the module"""
    module = importlib.reload(module)

    def closesocket():
        callback({'result': 'ok', 'close': True})

    def action1():
        module.perform(data, callback)
        my_log.debug('schedule close socket')
        app.loop.call_later(30, closesocket)

    app.loop.run_in_executor(act_executor, action1)


@sio.on('disconnect', namespace='/action')
def disconnect(sid):
    my_log.debug('disconnect ' + sid)


app.router.add_static('/static', 'static')
app.router.add_get('/', index)

if __name__ == '__main__':

    my_log.debug('server started on 19999...')
    web.run_app(app, port=19999)
Ejemplo n.º 18
0
def train(args):
    experiment_dir = mkdir('./experiment/')
    checkpoints_dir = mkdir('./experiment/clf/%s/' % (args.model_name))
    train_data, train_label, test_data, test_label = load_data(
        'experiment/data/modelnet40_ply_hdf5_2048/')

    trainDataset = ModelNetDataLoader(train_data,
                                      train_label,
                                      data_augmentation=args.augment)
    trainDataLoader = DataLoader(trainDataset,
                                 batch_size=args.batch_size,
                                 shuffle=True)

    testDataset = ModelNetDataLoader(test_data, test_label)
    testDataLoader = torch.utils.data.DataLoader(testDataset,
                                                 batch_size=args.batch_size,
                                                 shuffle=False)

    log.info('Building Model', args.model_name)
    if args.model_name == 'pointnet':
        num_class = 40
        model = PointNetCls(num_class, args.feature_transform).cuda()
    else:
        model = PointNet2ClsMsg().cuda()

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

    if args.pretrain is not None:
        log.info('Use pretrain model...')
        state_dict = torch.load(args.pretrain)
        model.load_state_dict(state_dict)
        init_epoch = int(args.pretrain[:-4].split('-')[-1])
        log.info('start epoch from', init_epoch)
    else:
        log.info('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

    global_epoch = 0
    global_step = 0
    best_tst_accuracy = 0.0

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

        log.debug(job='clf',
                  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 batch_id, data in tqdm(enumerate(trainDataLoader, 0),
                                   total=len(trainDataLoader),
                                   smoothing=0.9):
            points, target = data
            target = target[:, 0]
            points = points.transpose(2, 1)
            points, target = points.cuda(), target.cuda()
            optimizer.zero_grad()
            model = model.train()
            pred, trans_feat = model(points)
            loss = F.nll_loss(pred, target.long())
            if args.feature_transform and args.model_name == 'pointnet':
                loss += feature_transform_reguliarzer(trans_feat) * 0.001
            loss.backward()
            optimizer.step()
            global_step += 1

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

        acc = test_clf(model, testDataLoader)
        log.info(loss='%.5f' % (loss.data))
        log.info(Test_Accuracy='%.5f' % acc)

        if acc >= best_tst_accuracy:
            best_tst_accuracy = acc
            fn_pth = 'clf-%s-%.5f-%04d.pth' % (args.model_name, acc, epoch)
            log.debug('Saving model....', fn_pth)
            torch.save(model.state_dict(),
                       os.path.join(checkpoints_dir, fn_pth))
        global_epoch += 1

    log.info(Best_Accuracy=best_tst_accuracy)
    log.info('End of training...')
Ejemplo n.º 19
0
 def action1():
     module.perform(data, callback)
     my_log.debug('schedule close socket')
     app.loop.call_later(30, closesocket)
Ejemplo n.º 20
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)