コード例 #1
0
def main(params):
    # Initialize some variables to track progress
    accs = []

    # Initialize the model
    model = ConvLSTM(params=params).to(device)

    # Use parallel computing if available
    if device.type == 'cuda' and n_gpus > 1:
        model = nn.DataParallel(model, list(range(n_gpus)))

    # Loss Function and Optimizer (can use weight=class_weights if it is a disbalanced dataset)
    loss_function = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=0.001)

    # Get train and validation loaders
    train_loader, val_loader = get_train_val_loader(params,
                                                    val_pct=params.val_pct)

    # Train the model
    model = train(model, train_loader, loss_function, optimizer, params)
    save_model(model)

    # Get training accuracy
    preds, actual, acc = test(model, train_loader)
    build_test_stats(preds, actual, acc, params)

    # Validate the model
    preds, actual, acc = test(model, val_loader)
    build_test_stats(preds, actual, acc, params)
コード例 #2
0
ファイル: main.py プロジェクト: arnav1993k/SpeedPrediction
def main(config_file,mode,distributed):
    config = check_params(config_file)
    if mode in ["Train","train"]:
        train_dataset = Dataset(config["train_params"]["input_path"],config["train_params"]["imsize"])
        if distributed:
            import horovod as hvd
            hvd.init()
            if hvd.rank()==0:
                writer = setup_tensorboard(get_params(config["train_params"],"tensorboard_location","./summary/"))
            train_sampler = torch.utils.data.distributed.DistributedSampler(train_dataset, num_replicas=hvd.size(),
                                                                            rank=hvd.rank())

            train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=config["train_params"]["batch_size"],
                                                       sampler=train_sampler, shuffle=True)
            model = ConvLSTM(**config["model_params"])
            optimizer = hvd.DistributedOptimizer(model.optimizer, named_parameters=model.named_parameters())
            hvd.broadcast_parameters(model.state_dict(), root_rank=0)
            train_distributed(model,train_loader,optimizer,config,writer)
        else:
            writer = setup_tensorboard(get_params(config["train_params"], "tensorboard_location", "./summary/"))
            train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=config["train_params"]["batch_size"],
                                                     shuffle = True)
            model = ConvLSTM(**config["model_params"])
            train(model,train_loader,model.optimizer,config,writer)
    elif mode in ["infer","Infer"]:
        model = ConvLSTM(**config["model_params"])
        model.load_state_dict(config["infer_params"]["model_save_path"])
        output_file = open(config["infer_params"]["output_path"])
コード例 #3
0
 def __init__(self, data_pth, model_name):
     self.input_data = np.loadtxt(data_pth).astype(np.float32).reshape(
         -1, 1, 17, 2)  #(seq_l, 1, 17, 2)
     self.model_name = os.path.join(model_dir, model_name)
     structre_num = int(model_name.split('_')[1][6:])
     self.model = ConvLSTM(input_size=(17, 2),
                           input_dim=1,
                           hidden_dim=params_ls[structre_num][0],
                           kernel_size=params_ls[structre_num][1],
                           num_layers=len(params_ls[structre_num][0]),
                           num_classes=2,
                           batch_size=2,
                           batch_first=True,
                           bias=True,
                           return_all_layers=False,
                           attention=params_ls[structre_num][2]).cuda()
     self.model.load_state_dict(torch.load(self.model_name))
     self.model.eval()
コード例 #4
0
class prediction(object):
    def __init__(self, data_pth, model_name):
        self.input_data = np.loadtxt(data_pth).astype(np.float32).reshape(
            -1, 1, 17, 2)  #(seq_l, 1, 17, 2)
        self.model_name = os.path.join(model_dir, model_name)
        structre_num = int(model_name.split('_')[1][6:])
        self.model = ConvLSTM(input_size=(17, 2),
                              input_dim=1,
                              hidden_dim=params_ls[structre_num][0],
                              kernel_size=params_ls[structre_num][1],
                              num_layers=len(params_ls[structre_num][0]),
                              num_classes=2,
                              batch_size=2,
                              batch_first=True,
                              bias=True,
                              return_all_layers=False,
                              attention=params_ls[structre_num][2]).cuda()
        self.model.load_state_dict(torch.load(self.model_name))
        self.model.eval()

    def get_input_data(self, input_data):
        data = torch.from_numpy(input_data)
        data = data.unsqueeze(0).to(device=device)

        return data  #(1, 30, 1, 17, 2)

    def predict_pre_second(self, data):
        output = self.model(data)
        #print('output:',output)
        pred = output.data.max(1, keepdim=True)[1]
        #print('pred:',pred)
        return pred

    def predict(self):
        preds = []
        for i in range(0, self.input_data.shape[0], 30):
            data = self.get_input_data(self.input_data[i:i + 30, :, :, :])
            if data.size(1) < 30:
                break
            pred = self.predict_pre_second(data)
            print('pred:', pred)
            preds.append(pred)
        return pred
コード例 #5
0
def load_test_model(params, model_path):
    model = ConvLSTM(params=params).to(device)
    # Use this to fix keyError in the model when using DataParallel while training
    if device.type == 'cuda' and n_gpus > 1:
        model = nn.DataParallel(model, list(range(n_gpus)))
    train_loader, val_loader = get_train_val_loader(params, val_pct=0.2)
    model.load_state_dict(torch.load(model_path))
    model.eval()  # To set dropout and batchnormalization OFF
    preds, actual, acc = test(model, val_loader)
    build_test_stats(preds, actual, acc, params)
コード例 #6
0
ファイル: finetune.py プロジェクト: MdAsifKhan/cudavision
    ]))

trainloader = torch.utils.data.DataLoader(trainset,
                                          batch_size=opt.batch_size,
                                          shuffle=True,
                                          num_workers=opt.workers)
testloader = torch.utils.data.DataLoader(testset,
                                         batch_size=opt.batch_size,
                                         shuffle=False,
                                         num_workers=opt.workers)

nc = 1
model = SweatyNet1(nc)
if opt.use_gpu:
    model = model.cuda()

epoch = 20  #Checkpoint model to load
model_name = opt.model_root + 'Model_lr_{}_opt_{}_epoch_{}'.format(
    opt.lr, opt.optimizer, epoch)
model.load_state_dict(load_model(model_name, key='state_dict_model'))

nc = 10  #Number of Sequence
map_size = [120, 160]
model.layer18 = ConvLSTM(nc, map_size)

opt.lr = 0.00001

modeleval = ModelEvaluator(model)
modeleval.evaluator(seq_trainloader, seq_testloader)
modeleval.plot_loss()
コード例 #7
0
ファイル: main.py プロジェクト: ThanThoai/Action_Recognition
def main(config):
    if config.model == 'c3d':
        model, params = C3D(config)
    elif config.model == 'convlstm':
        model, params = ConvLSTM(config)
    elif config.model == 'densenet':
        model, params = densenet(config)
    elif config.model == 'densenet_lean':
        model, params = densenet_lean(config)
    elif config.model == 'resnext':
        model, params = resnext(config)
    else:
        model, params = densenet_lean(config)

    dataset = config.dataset
    sample_size = config.sample_size
    stride = config.stride
    sample_duration = config.sample_duration

    cv = config.num_cv

    # crop_method = GroupRandomScaleCenterCrop(size=sample_size)
    crop_method = MultiScaleRandomCrop(config.scales, config.sample_size[0])
    # norm = Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])

    norm = Normalize([114.7748, 107.7354, 99.475], [1, 1, 1])
    # spatial_transform = Compose(
    #     [crop_method,
    #      GroupRandomHorizontalFlip(),
    #      ToTensor(1), norm])
    spatial_transform = Compose([
        RandomHorizontalFlip(), crop_method,
        ToTensor(config.norm_value), norm
    ])
    # temporal_transform = RandomCrop(size=sample_duration, stride=stride)
    temporal_transform = TemporalRandomCrop(config.sample_duration,
                                            config.downsample)
    target_transform = Label()

    train_batch = config.train_batch
    train_data = RWF2000('/content/RWF_2000/frames/',
                         g_path + '/RWF-2000.json', 'training',
                         spatial_transform, temporal_transform,
                         target_transform, dataset)
    train_loader = DataLoader(train_data,
                              batch_size=train_batch,
                              shuffle=True,
                              num_workers=4,
                              pin_memory=True)

    crop_method = GroupScaleCenterCrop(size=sample_size)
    norm = Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    spatial_transform = Compose([crop_method, ToTensor(), norm])
    temporal_transform = CenterCrop(size=sample_duration, stride=stride)
    target_transform = Label()

    val_batch = config.val_batch

    val_data = RWF2000('/content/RWF_2000/frames/', g_path + '/RWF-2000.json',
                       'validation', spatial_transform, temporal_transform,
                       target_transform, dataset)
    val_loader = DataLoader(val_data,
                            batch_size=val_batch,
                            shuffle=False,
                            num_workers=4,
                            pin_memory=True)

    if not os.path.exists('{}/pth'.format(config.output)):
        os.mkdir('{}/pth'.format(config.output))
    if not os.path.exists('{}/log'.format(config.output)):
        os.mkdir('{}/log'.format(config.output))

    batch_log = Log(
        '{}/log/{}_fps{}_{}_batch{}.log'.format(
            config.output,
            config.model,
            sample_duration,
            dataset,
            cv,
        ), ['epoch', 'batch', 'iter', 'loss', 'acc', 'lr'])
    epoch_log = Log(
        '{}/log/{}_fps{}_{}_epoch{}.log'.format(config.output, config.model,
                                                sample_duration, dataset, cv),
        ['epoch', 'loss', 'acc', 'lr'])
    val_log = Log(
        '{}/log/{}_fps{}_{}_val{}.log'.format(config.output, config.model,
                                              sample_duration, dataset, cv),
        ['epoch', 'loss', 'acc'])

    criterion = nn.CrossEntropyLoss().to(device)
    # criterion = nn.BCELoss().to(device)

    learning_rate = config.learning_rate
    momentum = config.momentum
    weight_decay = config.weight_decay

    optimizer = torch.optim.SGD(params=params,
                                lr=learning_rate,
                                momentum=momentum,
                                weight_decay=weight_decay,
                                dampening=False,
                                nesterov=False)

    # optimizer = torch.optim.Adam(params=params, lr = learning_rate, weight_decay= weight_decay)

    scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
        optimizer, verbose=True, factor=config.factor, min_lr=config.min_lr)

    acc_baseline = config.acc_baseline
    loss_baseline = 1

    for p in range(1, config.num_prune):
        if p > 0:
            model = torch.load('{}/pth/prune_{}.pth'.format(
                config.output, p - 1))
        print(f"Prune {p}/{config.num_prune}")
        params = sum([np.prod(p.size()) for p in model.parameters()])
        print("Number of Parameters: %.1fM" % (params / 1e6))
        model = prune_model(model)
        params = sum([np.prod(p.size()) for p in model.parameters()])
        print("Number of Parameters: %.1fM" % (params / 1e6))
        model.to(config.device)
        acc_baseline = 0
        for i in range(5):
            train(i, train_loader, model, criterion, optimizer, device,
                  batch_log, epoch_log)
            val_loss, val_acc = val(i, val_loader, model, criterion, device,
                                    val_log)
            scheduler.step(val_loss)
            if val_acc > acc_baseline or (val_acc >= acc_baseline
                                          and val_loss < loss_baseline):
                # torch.save(
                # model.state_dict(),
                # '{}/pth/prune_{}_{}_fps{}_{}{}_{}_{:.4f}_{:.6f}.pth'.format(
                #     config.output, p, config.model, sample_duration, dataset, cv, i, val_acc,
                #     val_loss))
                torch.save(model,
                           '{}/pth/prune_{}.pth'.format(config.output, p))
コード例 #8
0
            NB_SENSOR_CHANNELS))  #inputs (46495, 1, 24, 113), targets (46495,)
        X_train.astype(np.float32), y_train.reshape(len(y_train)).astype(
            np.uint8)
        # X_train = X_train[1:300]
        # y_train = y_train[1:300]
        # X_test = X_test[1:300]
        # y_test = y_test[1:300]
        print(" after reshape: inputs {0}, targets {1}".format(
            X_train.shape, y_train.shape))
    else:
        trainset = OPPORTUNITY(
            'D:/Research/DeepConvLSTM/OPPORTUNITY/gestures.data', train=True)
        testset = OPPORTUNITY(
            'D:/Research/DeepConvLSTM/OPPORTUNITY/gestures.data', train=False)

    model = ConvLSTM()
    if torch.cuda.is_available():
        model.cuda()
        print("Model on gpu")

    # If use CrossEntropyLoss,softmax wont be used in the final layer
    loss_function = nn.CrossEntropyLoss()
    optimizer = optim.RMSprop(model.parameters(), lr=10e-5)

    if DATAFORMAT:
        if CONTAIN_NULLCLASS:
            X_train = list(X_train)
            y_train = list(y_train)
            training_set = []
            for i in range(len(y_train)):
                x = X_train[i]
コード例 #9
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--log_dir',
                        type=str,
                        default='logs',
                        help='output log directory')
    parser.add_argument('--feature',
                        type=str,
                        choices=['melgram', 'mfcc'],
                        default='mfcc',
                        help='feature')
    parser.add_argument('--model_type',
                        type=str,
                        choices=['alex1d', 'alex2d', 'lstm', 'resnet'],
                        default='alex2d',
                        help='convolution type')
    parser.add_argument('--batch_size',
                        type=int,
                        default=128,
                        help='training and valid batch size')
    parser.add_argument('--valid_ratio',
                        type=float,
                        default=0.1,
                        help='the ratio of validation data')
    parser.add_argument('--epochs',
                        type=int,
                        default=32,
                        help='number of epochs to train')
    parser.add_argument('--lr',
                        type=float,
                        default=0.001,
                        help='learning rate')
    parser.add_argument('--seed', type=int, default=1234, help='random seed')

    args = parser.parse_args()

    print('log_dir:', args.log_dir)
    print('feature:', args.feature)
    print('model_type:', args.model_type)
    print('batch_size:', args.batch_size)
    print('valid_ratio:', args.valid_ratio)
    print('epochs:', args.epochs)
    print('lr:', args.lr)
    print('seed:', args.seed)

    # seed
    np.random.seed(args.seed)
    torch.manual_seed(args.seed)
    if cuda:
        torch.cuda.manual_seed(args.seed)

    # データリストをDataFrameとしてロード
    train_df = pd.read_csv('./data/train.csv')
    test_df = pd.read_csv('./data/sample_submission.csv')

    # DataFrameのラベルをインデックスに変換
    le = LabelEncoder()
    le.fit(np.unique(train_df.label))
    train_df['label_idx'] = le.transform(train_df['label'])
    num_classes = len(le.classes_)

    # Datasetをロード
    # test=Trueにするとラベルは読み込まれない
    train_dataset = AudioDataset(train_df,
                                 './data/audio_train',
                                 feature=args.feature,
                                 model_type=args.model_type)

    test_dataset = AudioDataset(test_df,
                                './data/audio_test',
                                test=True,
                                feature=args.feature,
                                model_type=args.model_type)

    # 訓練データを訓練とバリデーションにランダムに分割
    # あとでCVによるEnsembleできるようにシードを指定する
    num_train = len(train_dataset)

    indices = list(range(num_train))
    split = int(args.valid_ratio * num_train)
    np.random.shuffle(indices)
    train_idx, valid_idx = indices[split:], indices[:split]

    train_sampler = SubsetRandomSampler(train_idx)
    valid_sampler = SubsetRandomSampler(valid_idx)

    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               args.batch_size,
                                               sampler=train_sampler,
                                               num_workers=num_workers)

    # バリデーションデータはtrain_datasetの一部を使う
    val_loader = torch.utils.data.DataLoader(train_dataset,
                                             args.batch_size,
                                             sampler=valid_sampler,
                                             num_workers=num_workers)

    # テストデータはDataFrameの順番のまま読み込みたいため
    # shuffle=Falseとする
    test_loader = torch.utils.data.DataLoader(test_dataset,
                                              args.batch_size,
                                              shuffle=False)

    # build model
    if args.model_type == 'alex2d':
        model = AlexNet2d(num_classes).to(device)
    elif args.model_type == 'alex1d':
        model = AlexNet1d(num_classes).to(device)
    elif args.model_type == 'lstm':
        model = ConvLSTM(num_classes).to(device)
    elif args.model_type == 'resnet':
        model = ResNet([2, 2, 2, 2]).to(device)
    else:
        print('Invalid model_type: %s' % args.model_type)
        exit(1)

    print(model)

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=args.lr)

    scheduler = CyclicLR(optimizer,
                         base_lr=0.0001,
                         max_lr=0.01,
                         step_size=10,
                         mode="exp_range")

    # 学習率の履歴を保存(可視化用)
    lr_list = []

    best_acc = 0.0
    best_model = None
    writer = SummaryWriter(args.log_dir)

    for epoch in range(1, args.epochs + 1):
        loss, acc = train(train_loader, model, criterion, optimizer)
        val_loss, val_acc = valid(val_loader, model, criterion)

        lr_list.append(scheduler.get_lr()[0])
        scheduler.step()

        # logging
        writer.add_scalar('train/loss', loss, epoch)
        writer.add_scalar('train/acc', acc, epoch)
        writer.add_scalar('valid/loss', val_loss, epoch)
        writer.add_scalar('valid/acc', val_acc, epoch)

        print(
            'Epoch [%d/%d] loss: %.5f acc: %.5f val_loss: %.5f val_acc: %.5f' %
            (epoch, args.epochs, loss, acc, val_loss, val_acc))

        if val_acc > best_acc:
            print('val_acc improved from %.5f to %.5f' % (best_acc, val_acc))
            best_acc = val_acc

            # remove the old model file
            if best_model is not None:
                os.remove(best_model)

            best_model = os.path.join(
                args.log_dir,
                'epoch%03d-%.3f-%.3f.pth' % (epoch, val_loss, val_acc))
            torch.save(model.state_dict(), best_model)

    # ベストモデルでテストデータを評価
    # あとでEnsembleできるようにモデルの出力値も保存しておく
    print('best_model:', best_model)
    model.load_state_dict(
        torch.load(best_model, map_location=lambda storage, loc: storage))
    predictions = test(test_loader, model)
    np.save(os.path.join(args.log_dir, 'predictions.npy'),
            predictions.cpu().numpy())

    # Top3の出力を持つラベルに変換
    _, indices = predictions.topk(3)  # (N, 3)
    # ラベルに変換
    predicted_labels = le.classes_[indices]
    predicted_labels = [' '.join(lst) for lst in predicted_labels]
    test_df['label'] = predicted_labels
    test_df.to_csv(os.path.join(args.log_dir, 'submission.csv'), index=False)
コード例 #10
0
def main():
    os.environ['CUDA_VISIBLE_DEVICES'] = FLAGS.gpu
    # preprocessing class
    pre_process = MinMaxNormalization01()
    print('load train, validate, test data...')
    split = [43824, 8760, 8760]
    data, train_data, val_data, test_data = load_data(filename=['data/taxi/p_map.mat', 'data/taxi/d_map.mat'],
                                                      split=split)
    # data: [num, row, col, channel]
    print('preprocess train data...')
    pre_process.fit(train_data)

    if 'ResNet' in FLAGS.model:
        pre_index = max(FLAGS.closeness * 1, FLAGS.period * 7, FLAGS.trend * 7 * 24)
        all_timestamps = gen_timestamps(['2009', '2010', '2011', '2012', '2013', '2014', '2015'])
        data = pre_process.transform(data)
        # train_data = train_data
        train_data = data[:split[0]]
        val_data = data[split[0] - pre_index:split[0] + split[1]]
        test_data = data[split[0] + split[1] - pre_index:split[0] + split[1] + split[2]]
        del data
        # get train, validate, test timestamps
        train_timestamps = all_timestamps[:split[0]]
        val_timestamps = all_timestamps[split[0] - pre_index:split[0] + split[1]]
        test_timestamps = all_timestamps[split[0] + split[1] - pre_index:split[0] + split[1] + split[2]]
        # get x, y
        train_x, train_y = batch_data_cpt_ext(train_data, train_timestamps,
                                              batch_size=FLAGS.batch_size, close=FLAGS.closeness, period=FLAGS.period,
                                              trend=FLAGS.trend)
        val_x, val_y = batch_data_cpt_ext(val_data, val_timestamps,
                                          batch_size=FLAGS.batch_size, close=FLAGS.closeness, period=FLAGS.period,
                                          trend=FLAGS.trend)
        test_x, test_y = batch_data_cpt_ext(test_data, test_timestamps,
                                            batch_size=FLAGS.batch_size, close=FLAGS.closeness, period=FLAGS.period,
                                            trend=FLAGS.trend)
        train = {'x': train_x, 'y': train_y}
        val = {'x': val_x, 'y': val_y}
        test = {'x': test_x, 'y': test_y}
        nb_flow = train_data.shape[-1]
        row = train_data.shape[1]
        col = train_data.shape[2]
        if FLAGS.model == 'AttResNet':
            print('k-means to cluster...')
            model_path = 'taxi-results/model_save/AttResNet/'
            log_path = 'taxi-results/log/AttResNet/'
            if FLAGS.pre_saved_cluster:
                cluster_centroid = np.load(model_path + 'cluster_centroid.npy')
            else:
                vector_data = np.reshape(train_data, (train_data.shape[0], -1))
                kmeans = KMeans(n_clusters=FLAGS.cluster_num, init='random', n_init=FLAGS.kmeans_run_num,
                                tol=0.00000001).fit(vector_data)
                cluster_centroid = kmeans.cluster_centers_
                cluster_centroid = np.reshape(cluster_centroid,
                                              (-1, train_data.shape[1], train_data.shape[2], train_data.shape[3]))
                if not os.path.exists(model_path):
                    os.makedirs(model_path)
                if not os.path.exists(log_path):
                    os.makedirs(log_path)
                np.save(model_path + 'cluster_centroid.npy', cluster_centroid)
            print('build AttResNet model...')
            model = AttResNet(input_conf=[[FLAGS.closeness, nb_flow, row, col], [FLAGS.period, nb_flow, row, col],
                                          [FLAGS.trend, nb_flow, row, col], [8]],
                              att_inputs=cluster_centroid, att_nodes=FLAGS.att_nodes,
                              att_layer=['conv', 'conv'],
                              att_layer_param=[[[3, 3], [1, 1, 1, 1], 8], [[3, 3], [1, 1, 1, 1], 2]],
                              batch_size=FLAGS.batch_size,
                              layer=['conv', 'res_net', 'conv'],
                              layer_param=[[[3, 3], [1, 1, 1, 1], 64],
                                           [3, [[[3, 3], [1, 1, 1, 1], 64], [[3, 3], [1, 1, 1, 1], 64]]],
                                           [[3, 3], [1, 1, 1, 1], 2]]
                              )
        else:
            print('build ResNet model...')
            model_path = 'taxi-results/model_save/ResNet/'
            log_path = 'taxi-results/log/ResNet/'
            model = ResNet(input_conf=[[FLAGS.closeness, nb_flow, row, col], [FLAGS.period, nb_flow, row, col],
                                       [FLAGS.trend, nb_flow, row, col], [8]], batch_size=FLAGS.batch_size,
                           layer=['conv', 'res_net', 'conv'],
                           layer_param=[[[3, 3], [1, 1, 1, 1], 64],
                                        [3, [[[3, 3], [1, 1, 1, 1], 64], [[3, 3], [1, 1, 1, 1], 64]]],
                                        [[3, 3], [1, 1, 1, 1], 2]]
                           )
        print('model solver...')
        solver = ModelSolver(model, train, val, preprocessing=pre_process,
                             n_epochs=FLAGS.n_epochs,
                             batch_size=FLAGS.batch_size,
                             update_rule=FLAGS.update_rule,
                             learning_rate=FLAGS.lr, save_every=FLAGS.save_every,
                             pretrained_model=FLAGS.pretrained_model, model_path=model_path,
                             test_model='taxi-results/model_save/ResNet/model-' + str(FLAGS.n_epochs),
                             log_path=log_path,
                             cross_val=False, cpt_ext=True)
        if FLAGS.train:
            print('begin training...')
            test_n = {'data': test_data, 'timestamps': test_timestamps}
            _, test_prediction = solver.train(test, test_n, output_steps=FLAGS.output_steps)
            # get test_target and test_prediction
            i = pre_index
            test_target = []
            while i < len(test_data) - FLAGS.output_steps:
                test_target.append(test_data[i:i + FLAGS.output_steps])
                i += 1
            test_target = np.asarray(test_target)
        if FLAGS.test:
            print('begin testing for predicting next 1 step')
            solver.test(test)
            print('begin testing for predicting next' + str(FLAGS.output_steps) + 'steps')
            test_n = {'data': test_data, 'timestamps': test_timestamps}
            solver.test_1_to_n(test_n)
    else:
        train_data = pre_process.transform(train_data)
        train_x, train_y = batch_data(data=train_data, batch_size=FLAGS.batch_size,
                                      input_steps=FLAGS.input_steps, output_steps=FLAGS.output_steps)
        val_data = pre_process.transform(val_data)
        val_x, val_y = batch_data(data=val_data, batch_size=FLAGS.batch_size,
                                  input_steps=FLAGS.input_steps, output_steps=FLAGS.output_steps)
        test_data = pre_process.transform(test_data)
        test_x, test_y = batch_data(data=test_data, batch_size=FLAGS.batch_size,
                                    input_steps=FLAGS.input_steps, output_steps=FLAGS.output_steps)
        train = {'x': train_x, 'y': train_y}
        val = {'x': val_x, 'y': val_y}
        test = {'x': test_x, 'y': test_y}
        input_dim = [train_data.shape[1], train_data.shape[2], train_data.shape[3]]
        if FLAGS.model == 'ConvLSTM':
            print('build ConvLSTM model...')
            model = ConvLSTM(input_dim=input_dim, batch_size=FLAGS.batch_size,
                             layer={'encoder': ['conv', 'conv', 'conv_lstm', 'conv_lstm'],
                                    'decoder': ['conv_lstm', 'conv_lstm', 'conv', 'conv']},
                             layer_param={'encoder': [[[3, 3], [1, 2, 2, 1], 8],
                                                      [[3, 3], [1, 2, 2, 1], 16],
                                                      [[16, 16], [3, 3], 64],
                                                      [[16, 16], [3, 3], 64]],
                                          'decoder': [[[16, 16], [3, 3], 64],
                                                      [[16, 16], [3, 3], 64],
                                                      [[3, 3], [1, 2, 2, 1], 8],
                                                      [[3, 3], [1, 2, 2, 1], 2]]},
                             input_steps=10, output_steps=10)
            print('model solver...')
            solver = ModelSolver(model, train, val, preprocessing=pre_process,
                                 n_epochs=FLAGS.n_epochs,
                                 batch_size=FLAGS.batch_size,
                                 update_rule=FLAGS.update_rule,
                                 learning_rate=FLAGS.lr, save_every=FLAGS.save_every,
                                 pretrained_model=FLAGS.pretrained_model,
                                 model_path='taxi-results/model_save/ConvLSTM/',
                                 test_model='taxi-results/model_save/ConvLSTM/model-' + str(FLAGS.n_epochs),
                                 log_path='taxi-results/log/ConvLSTM/')
        elif 'AttConvLSTM' in FLAGS.model:
            # train_data: [num, row, col, channel]
            if FLAGS.use_ae:
                # auto-encoder to cluster train_data
                print('auto-encoder to cluster...')
                model_path = 'taxi-results/model_save/AEAttConvLSTM/'
                log_path = 'taxi-results/log/AEAttConvLSTM/'
                if FLAGS.pre_saved_cluster:
                    cluster_centroid = np.load(model_path + 'cluster_centroid.npy')
                else:
                    ae = AutoEncoder(input_dim=input_dim, z_dim=[16, 16, 16],
                                     layer={'encoder': ['conv', 'conv'],
                                            'decoder': ['conv', 'conv']},
                                     layer_param={'encoder': [[[3, 3], [1, 2, 2, 1], 8],
                                                              [[3, 3], [1, 2, 2, 1], 16]],
                                                  'decoder': [[[3, 3], [1, 2, 2, 1], 8],
                                                              [[3, 3], [1, 2, 2, 1], 2]]},
                                     model_save_path=model_path,
                                     batch_size=FLAGS.batch_size)
                    if FLAGS.ae_train:
                        ae.train(train_data, batch_size=FLAGS.batch_size, learning_rate=FLAGS.lr, n_epochs=20,
                                 pretrained_model=FLAGS.ae_pretrained_model)
                    train_z_data = ae.get_z(train_data, pretrained_model=FLAGS.ae_pretrained_model)
                    print(train_z_data.shape)
                    # k-means to cluster train_z_data
                    vector_data = np.reshape(train_z_data, (train_z_data.shape[0], -1))
                    kmeans = KMeans(n_clusters=FLAGS.cluster_num, init='random', n_init=FLAGS.kmeans_run_num,
                                    tol=0.00000001).fit(vector_data)
                    cluster_centroid = kmeans.cluster_centers_
                    print(np.array(cluster_centroid).shape)
                    # reshape to [cluster_num, row, col, channel]
                    cluster_centroid = np.reshape(cluster_centroid,
                                                  (-1, train_z_data.shape[1], train_z_data.shape[2],
                                                   train_z_data.shape[3]))
                    # decoder to original space
                    cluster_centroid = ae.get_y(cluster_centroid, pretrained_model=FLAGS.ae_pretrained_model)
                    print(cluster_centroid.shape)
                    np.save(model_path + 'cluster_centroid.npy', cluster_centroid)
            else:
                # k-means to cluster train_data
                print('k-means to cluster...')
                model_path = 'taxi-results/model_save/' + FLAGS.model + '/'
                log_path = 'taxi-results/log/' + FLAGS.model + '/'
                if not os.path.exists(model_path):
                    os.makedirs(model_path)
                if not os.path.exists(log_path):
                    os.makedirs(log_path)
                if FLAGS.pre_saved_cluster:
                    cluster_centroid = np.load(model_path + 'cluster_centroid.npy')
                else:
                    vector_data = np.reshape(train_data, (train_data.shape[0], -1))
                    # init_vectors = vector_data[:FLAGS.cluster_num, :]
                    # cluster_centroid = init_vectors
                    kmeans = KMeans(n_clusters=FLAGS.cluster_num, init='random', n_init=FLAGS.kmeans_run_num,
                                    tol=0.00000001).fit(vector_data)
                    cluster_centroid = kmeans.cluster_centers_
                    # reshape to [cluster_num, row, col, channel]
                    cluster_centroid = np.reshape(cluster_centroid,
                                                  (-1, train_data.shape[1], train_data.shape[2], train_data.shape[3]))
                    np.save(model_path + 'cluster_centroid.npy', cluster_centroid)
            # build model
            print('build ' + FLAGS.model + ' model...')
            if FLAGS.model == 'AttConvLSTM':
                model = AttConvLSTM(input_dim=input_dim,
                                    att_inputs=cluster_centroid, att_nodes=FLAGS.att_nodes,
                                    batch_size=FLAGS.batch_size,
                                    layer={'encoder': ['conv', 'conv', 'conv_lstm', 'conv_lstm'],
                                           'decoder': ['conv_lstm', 'conv_lstm', 'conv', 'conv'],
                                           'attention': ['conv', 'conv']},
                                    layer_param={'encoder': [[[3, 3], [1, 2, 2, 1], 8],
                                                             [[3, 3], [1, 2, 2, 1], 16],
                                                             [[16, 16], [3, 3], 64],
                                                             [[16, 16], [3, 3], 64]],
                                                 'decoder': [[[16, 16], [3, 3], 64],
                                                             [[16, 16], [3, 3], 64],
                                                             [[3, 3], [1, 2, 2, 1], 8],
                                                             [[3, 3], [1, 2, 2, 1], 2]],
                                                 'attention': [[[3, 3], [1, 2, 2, 1], 8],
                                                               [[3, 3], [1, 2, 2, 1], 16]]},
                                    input_steps=10, output_steps=10)
            elif FLAGS.model == 'MultiAttConvLSTM':
                model = MultiAttConvLSTM(input_dim=input_dim,
                                         att_inputs=cluster_centroid, att_nodes=FLAGS.att_nodes,
                                         batch_size=FLAGS.batch_size,
                                         layer={'encoder': ['conv', 'conv', 'conv_lstm', 'conv_lstm'],
                                                'decoder': ['conv_lstm', 'conv_lstm', 'conv', 'conv'],
                                                'attention': ['conv', 'conv']},
                                         layer_param={'encoder': [[[3, 3], [1, 2, 2, 1], 8],
                                                                  [[3, 3], [1, 2, 2, 1], 16],
                                                                  [[16, 16], [3, 3], 64],
                                                                  [[16, 16], [3, 3], 64]],
                                                      'decoder': [[[16, 16], [3, 3], 64],
                                                                  [[16, 16], [3, 3], 64],
                                                                  [[3, 3], [1, 2, 2, 1], 8],
                                                                  [[3, 3], [1, 2, 2, 1], 2]],
                                                      'attention': [[[3, 3], [1, 2, 2, 1], 8],
                                                                    [[3, 3], [1, 2, 2, 1], 16]]},
                                         input_steps=10, output_steps=10)
            print('model solver...')
            solver = ModelSolver(model, train, val, preprocessing=pre_process,
                                 n_epochs=FLAGS.n_epochs,
                                 batch_size=FLAGS.batch_size,
                                 update_rule=FLAGS.update_rule,
                                 learning_rate=FLAGS.lr, save_every=FLAGS.save_every,
                                 pretrained_model=FLAGS.pretrained_model, model_path=model_path,
                                 test_model=model_path + 'model-' + str(FLAGS.n_epochs), log_path=log_path)
        if FLAGS.train:
            print('begin training...')
            test_prediction, _ = solver.train(test)
            test_target = np.asarray(test_y)
        if FLAGS.test:
            print('test trained model...')
            solver.test_model = solver.model_path + FLAGS.pretrained_model
            test_prediction = solver.test(test)
            test_target = np.asarray(test_y)
    np.save('taxi-results/results/'+FLAGS.model+'/test_target.npy', test_target)
    np.save('taxi-results/results/'+FLAGS.model+'/test_prediction.npy', test_prediction)
    print(test_prediction.shape)
コード例 #11
0
def main(args):
    dataset = Dataset(args.data_path, args.offset, args.seq, args.batch_size,
                      args.batch_per_video)
    optimizer = keras.optimizers.Adam(lr=1e-3)
    if (args.model_type == 0):
        model = ConvLSTM(optimizer, args.layer_num, args.channel_num)
    elif (args.model_type == 1):
        model = CConvLSTM(optimizer, args.layer_num, args.channel_num)

    if args.train == 'train':
        dataload = dataset.train_loader()
        validationload = dataset.validation_loader()
        train(dataload, validationload, model, args.epochs,
              args.steps_per_epoch, args.save_path)

        # save model
        model_json = model.to_json()
        with open('{}.json'.format(args.save_path), 'w') as json_file:
            json_file.write(model_json)
        model.save_weights('{}.h5'.format(args.save_path))

        # check trained well
        x, y = next(dataload)
        pred = model.predict(x)
        make_image(pred, y)

    elif args.train == 'test':

        cm = []
        tp = 0
        fn = 0
        fp = 0
        tn = 0
        # ped1 -> 36, ped2 -> 12
        # ped1 Test017 142 frame 이상
        for i in range(36):
            if (i != 16):
                x, y, video = dataset.test_loader(i)
                try:
                    with open('{}.json'.format(args.load_path), 'r') as f:
                        test_model = model_from_json(f.read())
                except:
                    test_model = model

                test_model.load_weights('{}.h5'.format(args.load_path))
                pred = test(test_model, x, y, args.batch_size)
                abnormal, mse, detect, threshold = abnormal_test(pred, y)
                mse = np.array(mse)
                mse.tofile("mse" + str(i + 1) + ".csv", sep=',')

                # check groundtruth
                gtfilename = args.data_path + 'gt2/Test' + str(i +
                                                               1) + '_gt.csv'
                print(gtfilename)
                f = open(gtfilename, 'r')
                reader = csv.reader(f)
                gt = []

                for j in reader:
                    gt.append(j)

                f.close()

                gt = [int(m) for n in gt for m in n]

                # Make a figure of score with gt
                make_score_figure(mse, gt, threshold, i + 1)

                detect = [int(i) for i in detect]
                # Make a confusion matrix
                cmtemp = confusion_matrix(gt, detect, labels=[1, 0])
                cm.append(cmtemp)
                detect = np.array(detect)
                detect.tofile("detect_" + str(i + 1) + ".csv", sep=',')
                make_ab_video(len(pred), y, abnormal, str(i + 1))
                make_pred_video(pred, str(i + 1))

        for i in cm:
            tp = tp + i[0][0]
            fn = fn + i[0][1]
            fp = fp + i[1][0]
            tn = tn + i[1][1]

        # TP: 비정상을 비정상으로 정확하게 예측
        # TN: 정상을 정상으로 정확하게 예측
        # FP: 비정상을 정상으로 잘못 예측
        # FN: 정상을 비정상으로 잘못 예측

        score = np.array([[tp, fn], [fp, tn]])
        print(score)
        score.tofile("Confusion matrix.csv", sep=',')
コード例 #12
0
        num_workers=cfg["num_workers"],
    )
    val_dataloader = torch.utils.data.DataLoader(
        val_dataset,
        batch_size=cfg["batch_size"],
        shuffle=False,
        num_workers=cfg["num_workers"],
    )
    dataloaders = {}
    dataloaders["train"] = train_dataloader
    dataloaders["val"] = val_dataloader

    # Define Model
    device = torch.device("cuda")

    model = ConvLSTM(cfg, bias=True, batch_first=True)

    if cfg["resume"]:
        model.load_state_dict(torch.load(cfg["weights"]))

    model.to(device)

    if cfg["optimizer"] == "sgd":
        optimizer = optim.SGD(
            model.parameters(),
            lr=cfg["lr"],
            momentum=0.9,
            weight_decay=cfg["weight_decay"],
        )
    else:
        optimizer = RAdam(
コード例 #13
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('log_dir', type=str, help='input log directory')
    parser.add_argument('model_file', type=str, help='input model file')
    parser.add_argument('--feature',
                        type=str,
                        choices=['melgram', 'mfcc'],
                        default='mfcc',
                        help='feature')
    parser.add_argument('--model_type',
                        type=str,
                        choices=['alex1d', 'alex2d', 'lstm', 'resnet'],
                        default='alex2d',
                        help='convolution type of the model')
    args = parser.parse_args()

    print('log_dir:', args.log_dir)
    print('model_file:', args.model_file)
    print('feature:', args.feature)
    print('model_type:', args.model_type)

    # load dataset
    train_df = pd.read_csv('./data/train.csv')
    test_df = pd.read_csv('./data/sample_submission.csv')

    le = LabelEncoder()
    le.fit(np.unique(train_df.label))
    train_df['label_idx'] = le.transform(train_df['label'])
    num_classes = len(le.classes_)

    test_dataset = AudioDataset(test_df,
                                './data/audio_test',
                                test=True,
                                feature=args.feature,
                                model_type=args.model_type)

    test_loader = torch.utils.data.DataLoader(test_dataset, 128, shuffle=False)

    # load model
    if args.model_type == 'alex2d':
        model = AlexNet2d(num_classes).to(device)
    elif args.model_type == 'alex1d':
        model = AlexNet1d(num_classes).to(device)
    elif args.model_type == 'lstm':
        model = ConvLSTM(num_classes).to(device)
    elif args.model_type == 'resnet':
        model = ResNet([2, 2, 2, 2]).to(device)
    else:
        print('Invalid model_type: %s' % args.model_type)
        exit(1)

    print(model)

    # 学習済みモデルをロード
    model.load_state_dict(
        torch.load(args.model_file, map_location=lambda storage, loc: storage))

    # test time augmentation
    tta_predictions = test_time_augmentation(test_loader, model, num_aug=5)
    np.save(os.path.join(args.log_dir, 'tta_predictions.npy'),
            tta_predictions.cpu().numpy())

    # Top3の出力を持つラベルに変換
    _, indices = tta_predictions.topk(3)
    predicted_labels = le.classes_[indices]
    predicted_labels = [' '.join(lst) for lst in predicted_labels]
    test_df['label'] = predicted_labels
    test_df.to_csv(os.path.join(args.log_dir, 'tta_submission.csv'),
                   index=False)
コード例 #14
0
### DATALOADER ###
ds = PoolDataset(seq_len=seq_len,
                 heatup_seq_len=heatup_seq_len,
                 sum_channels=True,
                 transform=lambda frames: (frames / frames.max() - 0.5) * 2)
dataloader = DataLoader(
    ds,
    batch_size=batch_size,
    shuffle=True,
    num_workers=num_workers,
    #collate_fn=collate_fn,
    drop_last=True,
    pin_memory=True)

### MODELS ###
lstm = nn.Sequential(ConvLSTM(1, 32, 5), ConvLSTM(32, 1, 1))

if os.path.isfile(weight_path_lstm):
    w = torch.load(weight_path_lstm)
    lstm.load_state_dict(w['lstm'])
    del w
lstm = lstm.cuda()

### OPTIM ###
loss_func = nn.MSELoss()
optimizer = optim.Adam(
    list(lstm.parameters()),
    lr=lr,
    #weight_decay=1e-5
)
if os.path.isfile(weight_path_lstm):