Esempio n. 1
0
def main():

    args = get_parser()

    # Getting data to predict on
    if os.path.isfile(args.images_path):
        data = load_image(args.images_path, args.image_size)
        image_names = [args.images_path]
        data = data.reshape(-1, args.image_size, args.image_size, 1)
    else:
        data, image_names = load_images(args.images_path, args.image_size)

    # Creating model
    model = create_model(image_size=args.image_size,
                         weights_path=args.weights_path)

    # Predicting
    head_root, head_vowel, head_consonant = model.predict(data, verbose=1)

    head_root = np.argmax(head_root, axis=1)
    head_vowel = np.argmax(head_vowel, axis=1)
    head_consonant = np.argmax(head_consonant, axis=1)

    # Creating and saving resulting DataFrame
    result_df = pd.DataFrame()
    result_df['image_name'] = image_names
    result_df['head_root'] = head_root
    result_df['head_vowel'] = head_vowel
    result_df['head_consonant'] = head_consonant
    result_df.to_csv('result.csv', index=False)
Esempio n. 2
0
def main():
    # Read the data
    X, y = read_data(config.dataset_path, genres, song_samples)

    # Transform to a 3-channel image
    X_stack = np.squeeze(np.stack((X, ) * 3, -1))
    X_train, X_test, y_train, y_test = train_test_split(X_stack,
                                                        y,
                                                        test_size=0.3,
                                                        random_state=42,
                                                        stratify=y)

    print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)

    # Histogram for train and test
    values, count = np.unique(np.argmax(y_train, axis=1), return_counts=True)
    plt.bar(values, count)

    values, count = np.unique(np.argmax(y_test, axis=1), return_counts=True)
    plt.bar(values, count)
    plt.savefig(os.path.join(config.save_dir, 'histogram.png'),
                format='png',
                bbox_inches='tight')

    # Training step
    input_shape = X_train[0].shape

    model = create_model(input_shape, num_genres)
    model.summary()

    optimizer = SGDW(momentum=0.9)
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=['accuracy'])

    hist = model.fit(X_train,
                     y_train,
                     batch_size=config.batch_size,
                     epochs=config.num_epochs,
                     validation_data=(X_test, y_test),
                     callbacks=[
                         checkpointer, early_stopper, tensorboard, csv_logger,
                         reduce_lr
                     ])

    # Evaluate
    score = model.evaluate(X_test, y_test, verbose=0)
    # Plot graphs
    save_history(hist, os.path.join(config.save_dir, 'evaluate.png'))

    # Save the confusion Matrix
    preds = np.argmax(model.predict(X_test), axis=1)
    y_orig = np.argmax(y_test, axis=1)
    conf_matrix = confusion_matrix(preds, y_orig)

    keys = OrderedDict(sorted(genres.items(), key=lambda t: t[1])).keys()
    plot_confusion_matrix(os.path.join(config.save_dir, 'cm.png'),
                          conf_matrix,
                          keys,
                          normalize=True)
Esempio n. 3
0
def main():
    args = get_parser()

    train_df = pd.read_csv('train_data/train.csv',
                           dtype={
                               'grapheme_root': 'uint8',
                               'vowel_diacritic': 'uint8',
                               'consonant_diacritic': 'uint8'
                           })
    train_df.drop(['grapheme'], axis=1, inplace=True)

    model = create_model(image_size=args.image_size, pretrained=False)

    histories = []
    for i in range(args.n_parquets):
        print('Loading parquet.')
        parquet = pd.read_parquet(f'train_data/train_image_data_{i}.parquet')
        parquet_images_id = parquet['image_id'].values
        parquet.drop(columns='image_id', inplace=True)
        temp_df = train_df[train_df['image_id'].isin(parquet_images_id)]

        print('Transforming data.')
        all_images = prepare_train_data(parquet)
        Y_train_root = pd.get_dummies(temp_df['grapheme_root']).values
        Y_train_vowel = pd.get_dummies(temp_df['vowel_diacritic']).values
        Y_train_consonant = pd.get_dummies(
            temp_df['consonant_diacritic']).values

        x_train, x_test, y_train_root, y_test_root, y_train_vowel, y_test_vowel, y_train_consonant, y_test_consonant = train_test_split(
            all_images,
            Y_train_root,
            Y_train_vowel,
            Y_train_consonant,
            test_size=0.08,
            random_state=666)

        datagen = create_datagen(x_train)

        print('Training model.')
        history = model.fit_generator(
            datagen.flow(x_train, {
                'dense_2': y_train_root,
                'dense_3': y_train_vowel,
                'dense_4': y_train_consonant
            },
                         batch_size=args.batch_size),
            epochs=args.n_epochs,
            validation_data=(x_test,
                             [y_test_root, y_test_vowel, y_test_consonant]),
            steps_per_epoch=x_train.shape[0] // args.batch_size,
            callbacks=[
                lr_reduction_root, lr_reduction_vowel, lr_reduction_consonant
            ])

        histories.append(history)

    model.save(args.weights_path)
Esempio n. 4
0
 def onMessage(self, payload, isBinary):
     deep_config = json.loads(payload)
     global ds
     ds = create_model(path, deep_config)
Esempio n. 5
0
from twisted.python import log
from twisted.web.server import Site
from twisted.web.static import Data

from utils.parse import process_file
from utils.transcribe import transcribe
from utils.model import create_model
from utils.thread_with_trace import thread_with_trace

from streaming.audiobuffer import AudioBuffer
from streaming.transcriber import StreamTranscriber

from config import path, \
    settings

ds = create_model(path, settings)


class Model(WebSocketServerProtocol):

    def onConnect(self, request):
        print("Client connecting: {0}".format(request.peer))

    def onClose(self, wasClean, code, reason):
        print("WebSocket connection closed: {0}".format(reason))

    def onMessage(self, payload, isBinary):
        deep_config = json.loads(payload)
        global ds
        ds = create_model(path, deep_config)
Esempio n. 6
0
    # comment vocab size
    config['comvocabsize'] = len(vocab.nl2index)
    # ast vocab size
    config['smlvocabsize'] = len(vocab.ast2index)

    # set sequence lengths
    # set sequence length for our input
    # code seq len
    config['tdatlen'] = 100
    # ast seq len
    config['maxastnodes'] = 100
    # comment seq len
    config['comlen'] = 30
    config['batch_size'] = batchsize

    config, _ = create_model(modeltype, config)
    print("MODEL LOADED")
    model = keras.models.load_model(modelfile,
                                    custom_objects={
                                        "tf": tf,
                                        "keras": keras,
                                        'GCNLayer': GCNLayer
                                    })

    config['batch_maker'] = 'graph_multi_1'

    print(model.summary())

    # set up prediction string and output file
    comstart = np.zeros(config['comlen'])
    stk = vocab.nl2index['<SOS>']
Esempio n. 7
0
    # model parameters
    steps = int(len(train_code_data) / batch_size) + 1
    valsteps = int(len(val_code_data) / batch_size) + 1

    # Print information
    print('tdatvocabsize {}'.format(config['tdatvocabsize']))
    print('comvocabsize {}'.format(config['comvocabsize']))
    print('smlvocabsize {}'.format(config['smlvocabsize']))
    print('batch size {}'.format(batch_size))
    print('steps {}'.format(steps))
    print('training data size {}'.format(steps * batch_size))
    print('vaidation data size {}'.format(valsteps * batch_size))
    print('------------------------------------------')

    # create model
    config, model = create_model(modeltype, config)

    print(model.summary())

    # set up data generators
    gen = BatchGen(config, 'train', train_code_data, train_ast_data, train_nl,
                   train_edges, vocab)

    checkpoint = ModelCheckpoint(outdir + "/models/" + modeltype +
                                 "_E{epoch:02d}.h5")

    valgen = BatchGen(config, 'val', val_code_data, val_ast_data, val_nl,
                      val_edges, vocab)
    callbacks = [checkpoint]

    model.fit_generator(gen,
Esempio n. 8
0
def main():
    # global args, best_prec1, cfg
    model = create_model(cfg.model.arch, cfg.model.num_classes,
                         cfg.model.pretrained)

    best_prec1 = 0
    # optionally resume from a checkpoint
    if cfg.resume:
        if os.path.isfile(cfg.resume):
            print("=> loading checkpoint '{}'".format(cfg.resume))
            checkpoint = torch.load(cfg.resume)
            cfg.start_epoch = checkpoint['epoch']
            best_prec1 = checkpoint['best_prec1']
            state_dict = checkpoint['state_dict']

            new_state_dict = OrderedDict()
            for k, v in state_dict.items():
                name = k[7:]
                new_state_dict[name] = v

            model.load_state_dict(new_state_dict)
            print("=> loaded checkpoint '{}' (epoch {})".format(
                cfg.resume, checkpoint['epoch']))
        else:
            print("=> no checkpoint found at '{}'".format(cfg.resume))

    cudnn.benchmark = True

    # Data loading code
    print("Loading data...")
    traindir = os.path.join(cfg.data, 'train')
    valdir = os.path.join(cfg.data, 'val')

    train_loader = torch.utils.data.DataLoader(SceneData(
        txt_file=cfg.annotations.train,
        image_dir=cfg.data,
        mode='train',
        transform=image_transforms['train']),
                                               batch_size=cfg.batch_size,
                                               shuffle=True,
                                               num_workers=cfg.workers,
                                               pin_memory=True)

    val_loader = torch.utils.data.DataLoader(SceneData(
        txt_file=cfg.annotations.val,
        image_dir=cfg.data,
        mode='train',
        transform=image_transforms['val']),
                                             batch_size=cfg.batch_size,
                                             shuffle=False,
                                             num_workers=cfg.workers,
                                             pin_memory=True)

    # define loss function (criterion) and optimizer
    # criterion = nn.CrossEntropyLoss().cuda()
    criterion = FocalLoss(class_num=cfg.model.num_classes)

    optimizer = optim.SGD(model.parameters(),
                          cfg.optimizer.learning_rate,
                          momentum=cfg.optimizer.momentum,
                          weight_decay=cfg.optimizer.weight_decay)

    model = torch.nn.DataParallel(model, device_ids=cfg.device_ids).cuda()
    # model = model.cuda()

    if cfg.evaluate:
        validate(val_loader, model, criterion)
        return

    for epoch in range(cfg.start_epoch, cfg.total_epochs):
        # train for one epoch
        train(train_loader, model, criterion, optimizer, epoch)

        # evaluate on validation set
        prec1 = validate(val_loader, model, criterion)

        # remember best prec@1 and save checkpoint
        is_best = prec1[0] > best_prec1
        best_prec1 = max(prec1[0], best_prec1)
        save_checkpoint(
            {
                'epoch': epoch + 1,
                'arch': cfg.model.arch,
                'state_dict': model.state_dict(),
                'best_prec1': best_prec1,
            }, epoch, is_best, cfg.work_dir)