Exemple #1
0
    def run(self):
        # set enviornment
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = str(self.gpuid)
        print("InferenceWorker init, GPU ID: {}".format(self.gpuid))

        # load models
        model = load_model(get_best_model(backbone_type),
                           custom_objects={'triplet_loss': triplet_loss})
        # sgd = keras.optimizers.SGD(lr=1e-5, momentum=0.9, nesterov=True, decay=1e-6)
        # adam = keras.optimizers.Adam(lr=0.001)
        # model.compile(optimizer=sgd, loss=triplet_loss)

        while True:
            try:
                sample = {}
                try:
                    sample['a'] = self.in_queue.get(block=False)
                    sample['p'] = self.in_queue.get(block=False)
                    sample['n'] = self.in_queue.get(block=False)

                except queue.Empty:
                    break

                batch_inputs = np.empty((3, 1, img_size, img_size, channel),
                                        dtype=np.float32)

                for j, role in enumerate(['a', 'p', 'n']):
                    image_name = sample[role]
                    filename = os.path.join(manga_dir, 'test', image_name)
                    image = cv.imread(filename)
                    image = image[:, :, ::-1]  # RGB

                    image = cv.resize(image, (img_size, img_size),
                                      cv.INTER_CUBIC)

                    batch_inputs[j, 0] = preprocess_input(image)

                y_pred = model.predict(
                    [batch_inputs[0], batch_inputs[1], batch_inputs[2]])
                a = y_pred[0, 0:128]
                p = y_pred[0, 128:256]
                n = y_pred[0, 256:384]

                self.out_queue.put({'image_name': sample['a'], 'embedding': a})
                self.out_queue.put({'image_name': sample['p'], 'embedding': p})
                self.out_queue.put({'image_name': sample['n'], 'embedding': n})
                self.signal_queue.put(SENTINEL)

                if self.in_queue.qsize() == 0:
                    break
            except Exception as e:
                print(e)

        import keras.backend as K
        K.clear_session()
        print('InferenceWorker done, GPU ID {}'.format(self.gpuid))
Exemple #2
0
def load_pretrained_models(args, dataset):

    models = []
    if args.pretrained_dir is not None:
        for p in args.pretrained_dir.iterdir():
            if "cnn" in p.name:
                args.model = "CNN"
            else:
                args.model = "LSTM"
            path = utils.get_best_model(p)[0]
            checkpoint = torch.load(path, map_location="cpu")
            lstm_model, _ = create_model_and_optimizer(
                args, dataset, params=checkpoint["model"])
            models.append(lstm_model)
            LOGGER.info(f"Loaded: {path}")

    else:
        if args.pretrained_lstm is not None:
            assert len(args.pretrained_lstm) > 0
            args.model = "LSTM"

            for path in args.pretrained_lstm:
                # model is put to gpu when constructed
                path = utils.get_best_model(path)[0]
                checkpoint = torch.load(path, map_location="cpu")
                lstm_model, _ = create_model_and_optimizer(
                    args, dataset, params=checkpoint["model"])
                models.append(lstm_model)
                LOGGER.info("Loaded: {path}")

        if args.pretrained_cnn is not None:
            assert len(args.pretrained_cnn) > 0
            args.model = "CNN"

            for path in args.pretrained_cnn:
                path = utils.get_best_model(path)[0]
                checkpoint = torch.load(path, map_location="cpu")
                cnn_model, _ = create_model_and_optimizer(
                    args, dataset, params=checkpoint["model"])
                models.append(cnn_model)
                LOGGER.info("Loaded: {path}")

    return models
def main():
    """Task 0 - Split training and evaluation data"""
    # Read data
    X, y = utils.read_documents('../input/all_sentiment_shuffled.txt')

    count_vect = CountVectorizer(analyzer=lambda x: x)
    X_vectorized = count_vect.fit_transform(X)

    # Split point between training and evaluation
    X_train, X_test, y_train, y_test, indices_train, indices_test = train_test_split(X_vectorized, y, np.arange(1, len(X)+1), test_size=0.2)

    """Task 1 - Plot label distribution"""
    # Get label distribution
    train_label_distribution = utils.get_label_distribution(y_train, save_plot_as='training_label_distribution')
    test_label_distribution = utils.get_label_distribution(y_test, save_plot_as='test_label_distribution')

    """Task 2 - Naive Bayes Classifier"""
    multiNB = MultinomialNB()
    multiNB.fit(X_train, y_train)
    multiNB_pred = multiNB.predict(X_test)
    multiNB_pred_prob = multiNB.predict_proba(X_test)
    utils.generate_wrong_pred("NaiveBayes", indices_test, y_test, multiNB_pred, multiNB.classes_, multiNB_pred_prob)
    utils.save_graphical_confusion_matrix(multiNB, X_test, y_test, "NB")

    """Task 2 - Base-DT"""
    baseDT = DecisionTreeClassifier(criterion='entropy', random_state=0)
    baseDT.fit(X_train, y_train)
    baseDT_pred = baseDT.predict(X_test)
    utils.save_graphical_confusion_matrix(baseDT, X_test, y_test, "DT-base")

    """Task 2 - Best-DT"""
    # The following function Analyses the relationship between the ccp_alphas parameter and accuracy 
    # for Decision Trees. It is not active when we run the code because it takes to long to complete. 
    # It is present here to show where it would be used and for completeness only. 

    # utils.analyze_ccp_alpha(X_train, X_test, y_train, y_test)

    bestDT = DecisionTreeClassifier(random_state=0)
    bestDT = utils.get_best_model(X_train, y_train, bestDT, {"criterion": ["entropy", "gini"], "splitter": ["best", "random"]})
    bestDT.fit(X_train, y_train)
    bestDT_pred = bestDT.predict(X_test)
    utils.save_graphical_confusion_matrix(bestDT, X_test, y_test, "DT-best")

    """Task 3 - Generate output"""
    labels = sorted(set(y))
    models = [('NaiveBayes', multiNB_pred),
            ('BaseDT', baseDT_pred),
            ('BestDT', bestDT_pred)]

    utils.generate_output(indices_test, y_test, labels, models)
Exemple #4
0
    def run(self):
        # set enviornment
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = str(self.gpuid)
        print("InferenceWorker init, GPU ID: {}".format(self.gpuid))

        from model import build_model

        # load models
        model = build_model()
        model.load_weights(get_best_model())

        while True:
            try:
                sample = {}
                try:
                    sample['a'] = self.in_queue.get(block=False)
                    sample['p'] = self.in_queue.get(block=False)
                    sample['n'] = self.in_queue.get(block=False)
                except queue.Empty:
                    break

                batch_inputs = np.empty((3, 1, img_size, img_size, channel), dtype=np.float32)

                for j, role in enumerate(['a', 'p', 'n']):
                    image_name = sample[role]
                    filename = os.path.join(image_folder, image_name)
                    image_bgr = cv.imread(filename)
                    image_bgr = cv.resize(image_bgr, (img_size, img_size), cv.INTER_CUBIC)
                    image_rgb = cv.cvtColor(image_bgr, cv.COLOR_BGR2RGB)
                    batch_inputs[j, 0] = preprocess_input(image_rgb)

                y_pred = model.predict([batch_inputs[0], batch_inputs[1], batch_inputs[2]])
                a = y_pred[0, 0:128]
                p = y_pred[0, 128:256]
                n = y_pred[0, 256:384]

                self.out_queue.put({'image_name': sample['a'], 'embedding': a})
                self.out_queue.put({'image_name': sample['p'], 'embedding': p})
                self.out_queue.put({'image_name': sample['n'], 'embedding': n})
                self.signal_queue.put(SENTINEL)

                if self.in_queue.qsize() == 0:
                    break
            except Exception as e:
                print(e)

        import keras.backend as K
        K.clear_session()
        print('InferenceWorker done, GPU ID {}'.format(self.gpuid))
Exemple #5
0
def restore_weights(model):
    """
    Restores weights from the checkpoint file if exists or
    preloads the first layers with VGG19 weights
    :param weights_best_file:
    :return: epoch number to use to continue training. last epoch + 1 or 0
    """
    # load previous weights or vgg19 if this is the first run
    if get_best_model():
        print("Loading the best weights...")

        model.load_weights(get_best_model())
    else:
        print("Loading vgg19 weights...")

        vgg_model = VGG19(include_top=False, weights='imagenet')

        for layer in model.layers:
            if layer.name in from_vgg:
                vgg_layer_name = from_vgg[layer.name]
                layer.set_weights(
                    vgg_model.get_layer(vgg_layer_name).get_weights())
                print("Loaded VGG19 layer: " + vgg_layer_name)
def main(hparams):
    """ Main Function """
    model = ImageCaptioning(hparams)

    checkpoint_callback = ModelCheckpoint(filepath="models/{epoch}-{val_loss:.2f}")
    trainer = pl.Trainer(
        checkpoint_callback=checkpoint_callback,
        max_epochs=hparams.epochs,
        gpus=hparams.gpus,
        distributed_backend=hparams.distributed_backend,
        fast_dev_run=True,
    )
    if not hparams.test:
        trainer.fit(model)
        # trainer.test(model)
    else:
        checkpoint_path = get_best_model("models/")
        model = ImageCaptioning.load_from_checkpoint(checkpoint_path=checkpoint_path)
        trainer.test(model)
Exemple #7
0
def main(params):

    log_dir = os.path.join(MAIN_LOG_DIR, params.log_dir)
    if tf.gfile.Exists(log_dir):
        tf.gfile.DeleteRecursively(log_dir)
    tf.gfile.MakeDirs(log_dir)

    tf.set_random_seed(seed=42)

    print("... creating a TensorFlow session ...\n")
    config = tf.ConfigProto()
    config.allow_soft_placement = True
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    print("... loading CIFAR10 dataset ...")
    (x_train, y_train), (x_test, y_test) = load_data(data_dir)
    y_train = np.squeeze(y_train)
    y_test = np.squeeze(y_test)

    x_train, y_train = shuffle(x_train, y_train)
    x_train, x_val, y_train, y_val = train_test_split(x_train, y_train,
                                                      test_size=0.2,
                                                      stratify=y_train,
                                                      random_state=51)
    # cast samples and labels
    x_train = x_train.astype(np.float32)
    x_val = x_val.astype(np.float32)
    x_test = x_test.astype(np.float32)
    y_train = y_train.astype(np.int32)
    y_val = y_val.astype(np.int32)
    y_test = y_test.astype(np.int32)

    print("\tTRAIN - images {} | {}  - labels {} - {}".format(x_train.shape, x_train.dtype, y_train.shape, y_train.dtype))
    print("\tVAL - images {} | {}  - labels {} - {}".format(x_val.shape, x_val.dtype, y_val.shape, y_val.dtype))
    print("\tTEST - images {} | {}  - labels {} - {}\n".format(x_test.shape, x_test.dtype, y_test.shape, y_test.dtype))

    print('... creating TensorFlow datasets ...\n')
    batch_x, batch_y, handle, handle_train, handle_val, handle_test \
        = build_dataset(sess, x_train, x_val, x_test, y_train, y_val, y_test,
                        batch_size=params.batch_size,
                        buffer_size=x_train.shape[0])

    nb_batches_per_epoch_train = int(ceil(x_train.shape[0]/params.batch_size))
    nb_batches_per_epoch_val = int(ceil(x_val.shape[0]/params.batch_size))
    nb_batches_per_epoch_test = int(ceil(x_test.shape[0]/params.batch_size))
    print('\tnb_batches_per_epoch_train : {}'.format(nb_batches_per_epoch_train))
    print('\tnb_batches_per_epoch_val : {}'.format(nb_batches_per_epoch_val))
    print('\tnb_batches_per_epoch_test : {}\n'.format(nb_batches_per_epoch_test))

    print('... building model ...\n')
    with tf.name_scope('INPUTS'):
        learning_rate = tf.placeholder(shape=[], dtype=tf.float32, name='learning_rate')
        is_training_bn = tf.placeholder(shape=[], dtype=tf.bool, name='is_training_bn')
        use_moving_statistics = tf.placeholder(shape=[], dtype=tf.bool, name='use_moving_statistics')
        global_step = tf.train.get_or_create_global_step()

    logits = build_model(batch_x, is_training_bn=is_training_bn)
    model_vars = tf.trainable_variables()

    with tf.name_scope('LOSS'):
        loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=batch_y))
        acc = tf.reduce_mean(tf.cast(tf.equal(batch_y, tf.argmax(logits, axis=1)), dtype=tf.float32))

    with tf.name_scope('OPTIMIZER'):
        if params.opt == "adam":
            opt = tf.train.AdamOptimizer(learning_rate=learning_rate)
        elif params.opt == "momentum":
            opt = tf.train.MomentumOptimizer(learning_rate=learning_rate, momentum=params.momentum)
        elif params.opt == "adamW":
            opt = tf.contrib.opt.AdamWOptimizer(weight_decay=params.weight_decay, learning_rate=learning_rate,
                                                beta1=0.9, beta2=0.999, epsilon=1e-8)
        elif params.opt == "momentumW":
            opt = tf.contrib.opt.MomentumWOptimizer(weight_decay=params.weight_decay, learning_rate=learning_rate,
                                                    momentum=params.momentum)
        else:
            raise ValueError('Invalid --opt argument : {}'.format(params.opt))

        if 'W' in params.opt:
            # when using AdamW or MomentumW
            if params.weight_decay_on == "all":
                decay_var_list = tf.trainable_variables()
            elif params.weight_decay_on == "kernels":
                decay_var_list = []
                for var in tf.trainable_variables():
                    if 'kernel' in var.name:
                        decay_var_list.append(var)
            else:
                raise ValueError('Invalid --weight_decay_on : {}'.format(params.weight_decay_on))

        # force updates of moving averages in BN before optimizing the network
        with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
            grads_and_vars = opt.compute_gradients(loss, var_list=tf.trainable_variables())
            if 'W' in params.opt:
                # add decay_var_list argument for decoupled optimizers
                train_op = opt.apply_gradients(grads_and_vars, global_step=global_step,
                                               decay_var_list=decay_var_list, name='train_op')
            else:
                # without weight decay
                train_op = opt.apply_gradients(grads_and_vars, global_step=global_step, name='train_op')

    with tf.name_scope('METRICS'):
        acc_mean, acc_update_op = tf.metrics.mean(acc)
        loss_mean, loss_update_op = tf.metrics.mean(loss)

    # summaries which track loss/acc per batch
    acc_summary = tf.summary.scalar('TRAIN/acc', acc)
    loss_summary = tf.summary.scalar('TRAIN/loss', loss)

    # summaries which track accumulated loss/acc
    acc_mean_summary = tf.summary.scalar('MEAN/acc', acc_mean)
    loss_mean_summary = tf.summary.scalar('MEAN/loss', loss_mean)

    # summaries to plot at each epoch
    summaries_mean = tf.summary.merge([acc_mean_summary, loss_mean_summary], name='summaries_mean')

    # summaries to plot regularly
    summaries = [acc_summary, loss_summary]

    summaries = tf.summary.merge(summaries, name='summaries')

    with tf.name_scope('INIT_OPS'):
        # local init_ops contains operations to reset to zero accumulators of 'acc' and 'loss'
        global_init_op = tf.global_variables_initializer()
        local_init_op = tf.local_variables_initializer()

        sess.run(global_init_op)
        sess.run(local_init_op)

    with tf.name_scope('SAVERS'):
        best_saver = tf.train.Saver(tf.global_variables(), max_to_keep=1)

    with tf.name_scope('FILE_WRITERS'):
        writer_train = tf.summary.FileWriter(os.path.join(log_dir, 'train'), graph=sess.graph)
        writer_val = tf.summary.FileWriter(os.path.join(log_dir, 'val'))
        writer_val_bn = tf.summary.FileWriter(os.path.join(log_dir, 'val_bn'))
        writer_test = tf.summary.FileWriter(os.path.join(log_dir, 'test'))

    if params.strategy_lr == "constant":
        def get_learning_rate(step, epoch, steps_per_epoch):
            return params.init_lr
    else:
        raise ValueError('Invalid --strategy_lr : {}'.format(params.strategy_lr))

    def inference(epoch, step, best_acc, best_step, best_epoch):
        sess.run(local_init_op)
        feed_dict = {is_training_bn: False, handle: handle_val}

        for _ in tqdm(range(nb_batches_per_epoch_val),
                      desc='VALIDATION @ EPOCH {}'.format(epoch)):
            sess.run([acc_update_op, loss_update_op], feed_dict=feed_dict)

        acc_v, loss_v, s = sess.run([acc_mean, loss_mean, summaries_mean])

        writer_val.add_summary(s, global_step=step)
        writer_val.flush()

        if acc_v > best_acc:
            color = COLORS['green']
            best_acc = acc_v
            best_step = step
            best_epoch = epoch
            ckpt_path = os.path.join(log_dir, 'best_model.ckpt')
            best_saver.save(sess, ckpt_path, global_step=step)
        else:
            color = COLORS['red']

        print("VALIDATION @ EPOCH {} : {}acc={:.4f}{}  loss={:.5f}".format(epoch, color[0], acc_v, color[1], loss_v))

        return best_acc, best_step, best_epoch

    feed_dict_train = {is_training_bn: True, handle: handle_train}

    best_acc = 0.
    best_step = 0
    best_epoch = 0

    step = -1

    # inference with trained variables
    best_acc, best_step, best_epoch = inference(0, 0, best_acc, best_step, best_epoch)

    for epoch in range(1, params.epochs+1):
        # ####################################### TRAIN 1 EPOCH ######################################################
        # re-initialize local variables
        sess.run(local_init_op)

        for _ in tqdm(range(nb_batches_per_epoch_train), desc='TRAIN @ EPOCH {}'.format(epoch)):
            step += 1

            feed_dict_train[learning_rate] = get_learning_rate(step, epoch, nb_batches_per_epoch_train)

            if step % PLOT_PERIOD == 0:
                _, s, _, _ = sess.run([train_op, summaries, acc_update_op, loss_update_op], feed_dict=feed_dict_train)
                writer_train.add_summary(s, global_step=step)

            else:
                sess.run([train_op, acc_update_op, loss_update_op], feed_dict=feed_dict_train)

        acc_v, loss_v, s = sess.run([acc_mean, loss_mean, summaries_mean])
        writer_train.add_summary(s, global_step=step)
        writer_train.flush()
        print("TRAIN @ EPOCH {} | : acc={:.4f}  loss={:.5f}".format(epoch, acc_v, loss_v))
        # ############################################################################################################

        # ###################################### INFERENCE ###########################################################
        # perform inference with trained variables and moving statistics
        best_acc, best_step, best_epoch = inference(epoch, step, best_acc, best_step, best_epoch)

    # Inference on test set with trained weights
    if best_acc > 0.:

        print("Load best model |  ACC={:.5f} form epoch={}".format(best_acc, best_epoch))
        model_to_restore = get_best_model(log_dir, model='best_model')
        if model_to_restore is not None:
            best_saver.restore(sess, model_to_restore)
        else:
            print("Impossible to load best model .... ")

        sess.run(local_init_op)
        feed_dict = {is_training_bn: False, handle: handle_test}
        for _ in tqdm(range(nb_batches_per_epoch_test), desc='TEST'):
            sess.run([acc_update_op, loss_update_op], feed_dict=feed_dict)

        acc_v, loss_v, s = sess.run([acc_mean, loss_mean, summaries_mean])
        writer_test.add_summary(s, global_step=best_step)
        writer_test.flush()
        print("TEST @ EPOCH {} : acc={:.4f}  loss={:.5f}".format(best_epoch, acc_v, loss_v))

    sess.close()
Exemple #8
0
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, required=True, help='path of config file')
opt = parser.parse_args()

# get config
config = utils.get_config(opt.config)

if torch.cuda.is_available() and not config['cuda']['using_cuda']:
    print("WARNING: You have a CUDA device, so you should probably run with --cuda")

# set device
cuda_str = 'cuda:' + str(config['cuda']['gpu_id'])
device = torch.device(cuda_str if config['cuda']['using_cuda'] else "cpu")

# get best model
best_model_path = utils.get_best_model(config['model']['exp_path'])

if best_model_path is None:
    raise FileNotFoundError('Not found ckpt file')

# set output path
ckpt_filename = best_model_path.split("/")[-1]
result_dir = best_model_path.replace(ckpt_filename, 'results')
correct_dir = os.path.join(result_dir, 'correct')
incorrect_dir = os.path.join(result_dir, 'incorrect')

if not os.path.exists(result_dir):
    os.mkdir(result_dir)
    os.mkdir(correct_dir)
    os.mkdir(incorrect_dir)
Exemple #9
0
from data_generator import MangaDataGenSequence
from model import build_model
from utils import get_available_gpus, get_available_cpus, ensure_folder, triplet_loss, get_smallest_loss, get_best_model

if __name__ == '__main__':
    os.environ["CUDA_VISIBLE_DEVICES"] = "3"

    # Parse arguments
    ap = argparse.ArgumentParser()
    ap.add_argument('--backbone', default='vgg19', type=str, required=False)
    args = ap.parse_args()
    if args.backbone:
        backbone_type = args.backbone

    checkpoint_models_path = backbone_type + '/models/'
    pretrained_path = get_best_model(backbone_type)
    ensure_folder(backbone_type + '/models/')

    # Callbacks
    tensor_board = keras.callbacks.TensorBoard(log_dir='./' + backbone_type +
                                               '/logs',
                                               histogram_freq=0,
                                               write_graph=True,
                                               write_images=True)
    model_names = checkpoint_models_path + 'model.{epoch:02d}-{val_loss:.4f}.hdf5'
    model_checkpoint = ModelCheckpoint(model_names,
                                       monitor='val_loss',
                                       mode='min',
                                       verbose=1,
                                       save_best_only=True,
                                       save_weights_only=False)
# import the necessary packages
import json
import os
import random

import cv2 as cv
import keras.backend as K
import numpy as np
from keras.applications.inception_resnet_v2 import preprocess_input

from config import train_data, test_a_image_folder, img_height, img_width
from model import build_model
from utils import get_best_model

if __name__ == '__main__':
    best_model, epoch = get_best_model()
    model = build_model()
    model.load_weights(best_model)

    labels = [folder for folder in os.listdir(train_data) if os.path.isdir(os.path.join(train_data, folder))]

    test_images = [f for f in os.listdir(test_a_image_folder) if
                   os.path.isfile(os.path.join(test_a_image_folder, f)) and f.endswith('.jpg')]
    num_samples = 20
    samples = random.sample(test_images, num_samples)

    if not os.path.exists('images'):
        os.makedirs('images')

    results = []
    for i in range(len(samples)):
Exemple #11
0
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
from keras.utils import multi_gpu_model

from config import patience, epochs, batch_size
from data_generator import train_gen, valid_gen
from model import build_model
from utils import get_available_gpus, ensure_folder, get_example_numbers, get_best_model

if __name__ == '__main__':
    # Parse arguments
    ap = argparse.ArgumentParser()
    args = vars(ap.parse_args())
    checkpoint_models_path = 'models/'
    ensure_folder(checkpoint_models_path)

    filename, epoch = get_best_model()
    if filename is None:
        initial_epoch = 0
    else:
        initial_epoch = epoch + 1

    # Callbacks
    tensor_board = keras.callbacks.TensorBoard(log_dir='./logs',
                                               histogram_freq=0,
                                               write_graph=True,
                                               write_images=True)
    model_names = checkpoint_models_path + 'model.{epoch:02d}-{val_loss:.4f}.hdf5'
    model_checkpoint = ModelCheckpoint(model_names,
                                       monitor='val_loss',
                                       verbose=1,
                                       save_best_only=True)
Exemple #12
0
def run():
    args = LMArguments()

    # Seed RNGs for reproducibility
    if args.seed > 0:
        print(f"Random seed set to {args.seed}")
        random.seed(args.seed)
        np.random.seed(args.seed)
        torch.manual_seed(args.seed)
        if args.cuda:
            torch.cuda.manual_seed(args.seed)

    # Configure logging
    if args.save:
        logfile = utils.create_exp_dir(args.exp,
                                       args.script,
                                       overwrite=args.overwrite)
    else:
        logfile = None

    # must init logging before SummaryWriter, otherwise it adds handler to root logger so basicConfig does not work
    logging.basicConfig(
        datefmt="%m-%d %H:%M:%S",
        format="%(asctime)s %(levelname)s %(name)s: %(message)s",
        level=logging.getLevelName(args.logging_level),
        filename=logfile,
    )

    # Print out all the arguments set.
    LOGGER.info("Arguments passed: " + args.to_string(max_width=80))
    print(args.exp, flush=True)

    LOGGER.info(f"Running the model on {'GPU (CUDA)' if args.cuda else 'CPU'}")
    if args.cuda:
        props = torch.cuda.get_device_properties(0)
        LOGGER.info(
            f"GPU name: {props.name}, CUDA version {props.major}.{props.minor}, "
            f"available memory: {props.total_memory / 1024 / 1024:.2f}MB.")

    # Create dataset
    dataset = create_dataset(args)

    # Create model
    model, optimizer = create_model_and_optimizer(args, dataset)

    # Print model parameter info
    n_params = sum(p.nelement() for p in model.parameters())
    LOGGER.info(f"Model parameters: {n_params}")
    LOGGER.info(f"Model structure:\n{str(model)}")

    if args.repl:
        # REPL mode
        repl(dataset, model)
        sys.exit(0)

    if args.mode == "train":
        # Training mode
        try:
            train_model(model, dataset, optimizer, args, writer=None)
        except KeyboardInterrupt:
            LOGGER.info("Training halted.")

        # load best model
        best_path, best_epoch = utils.get_best_model(args.exp)
        model.load_state_dict(torch.load(best_path)['model'])
        LOGGER.info(f"Loaded best model (epoch {best_epoch})")

        # Evaluate AND dump the avg logprobs of each word
        evaluate_model(model, dataset, args, split="train", writer=None)
        evaluate_model(model, dataset, args, split="valid", writer=None)
        evaluate_model(model, dataset, args, split="test", writer=None)

        postprocess_probdump(dataset, args.exp)

    else:
        # Evaluation mode
        for split in ['train', 'valid', 'test']:
            evaluate_model(model, dataset, args, split=split, writer=None)
Exemple #13
0
def main(args):

    # Seed RNGs for reproducibility
    if args.seed > 0:
        print(f"Random seed set to {args.seed}")
        random.seed(args.seed)
        np.random.seed(args.seed)
        torch.manual_seed(args.seed)
        if args.cuda:
            torch.cuda.manual_seed(args.seed)

    # Configure logging
    if args.save:
        logfile = utils.create_exp_dir(args.exp,
                                       args.script,
                                       overwrite=args.overwrite)
    else:
        logfile = None

    logging.basicConfig(
        datefmt="%m-%d %H:%M:%S",
        format="%(asctime)s %(levelname)s: %(message)s",
        level=logging.getLevelName(args.logging_level),
        filename=logfile,
    )

    n_gpus = torch.cuda.device_count()
    LOGGER.debug("Running the model on " +
                 (f"CUDA with {n_gpus} GPU(s)" if args.cuda else "CPU"))
    for device in range(n_gpus):
        props = torch.cuda.get_device_properties(device)
        LOGGER.debug(
            f"GPU ({device}) name: {props.name}, CUDA version {props.major}.{props.minor}, "
            f"available memory: {props.total_memory / 1024 / 1024:.2f}MB.")

    if args.eval or args.output is not None:
        evaluate(args)

    else:
        # Create dataset
        dataset = IEDataset(args)

        # Create model
        model, optimizer = create_model_and_optimizer(args, dataset)

        # Print model parameter info
        n_params = sum(p.nelement() for p in model.parameters())
        LOGGER.info(f"Model parameters: {n_params}")
        LOGGER.info(f"Model structure:\n{str(model)}")

        if args.pretrained:
            path, _ = utils.get_best_model(args.pretrained)
            states = torch.load(path, map_location="cuda")
            model.load_state_dict(states["model"])

        try:
            train_model(args, dataset, model, optimizer)
        except KeyboardInterrupt:
            LOGGER.info("Training halted.")

        path, _ = utils.get_best_model(args.exp)
        states = torch.load(path, map_location="cuda")
        model.load_state_dict(states["model"])

        test_model(args, dataset, model)
Exemple #14
0
# import the necessary packages
import os
import random

import cv2 as cv
import keras.backend as K
import numpy as np

from config import img_rows, img_cols, num_classes, test_folder, original_images_key, gray_values
from model import build_model
from utils import get_best_model, preprocess_input

if __name__ == '__main__':
    model = build_model()
    model.load_weights(get_best_model())

    print(model.summary())

    test_dir = os.path.join(test_folder, original_images_key)
    test_dir = os.path.join(
        test_dir,
        'P0089_MacularCube512x128_4-25-2013_9-32-13_OD_sn2218_cube_z.img')
    test_images = [
        os.path.join(test_dir, f) for f in os.listdir(test_dir)
        if f.lower().endswith('.bmp')
    ]
    samples = random.sample(test_images, 10)

    for i, filename in enumerate(samples):
        print('Start processing image: {}'.format(filename))
Exemple #15
0
    def run(self):
        # set enviornment
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = str(self.gpuid)
        print("InferenceWorker init, GPU ID: {}".format(self.gpuid))

        from model import build_model

        # load models
        model = build_model()
        model.load_weights(get_best_model())

        while True:
            try:
                sample = {}
                try:
                    sample['a'] = self.in_queue.get(block=False)
                    sample['p'] = self.in_queue.get(block=False)
                    sample['n'] = self.in_queue.get(block=False)

                except queue.Empty:
                    break

                batch_inputs = np.empty((3, 1, img_size, img_size, channel),
                                        dtype=np.float32)

                for j, role in enumerate(['a', 'p', 'n']):
                    image_name = sample[role]
                    filename = os.path.join(lfw_folder, image_name)
                    image = cv.imread(filename)
                    image = image[:, :, ::-1]  # RGB
                    dets = self.detector(image, 1)

                    num_faces = len(dets)
                    if num_faces > 0:
                        # Find the 5 face landmarks we need to do the alignment.
                        faces = dlib.full_object_detections()
                        for detection in dets:
                            faces.append(self.sp(image, detection))
                        image = dlib.get_face_chip(image,
                                                   faces[0],
                                                   size=img_size)
                    else:
                        image = cv.resize(image, (img_size, img_size),
                                          cv.INTER_CUBIC)

                    batch_inputs[j, 0] = preprocess_input(image)

                y_pred = model.predict(
                    [batch_inputs[0], batch_inputs[1], batch_inputs[2]])
                a = y_pred[0, 0:128]
                p = y_pred[0, 128:256]
                n = y_pred[0, 256:384]

                self.out_queue.put({'image_name': sample['a'], 'embedding': a})
                self.out_queue.put({'image_name': sample['p'], 'embedding': p})
                self.out_queue.put({'image_name': sample['n'], 'embedding': n})
                self.signal_queue.put(SENTINEL)

                if self.in_queue.qsize() == 0:
                    break
            except Exception as e:
                print(e)

        import keras.backend as K
        K.clear_session()
        print('InferenceWorker done, GPU ID {}'.format(self.gpuid))
Exemple #16
0
from data_generator import train_gen, valid_gen
from model import build_model
from utils import get_available_cpus, get_available_gpus, get_highest_acc, get_best_model, focal_loss

if __name__ == '__main__':
    # Parse arguments
    ap = argparse.ArgumentParser()
    ap.add_argument("-p",
                    "--pretrained",
                    help="path to save pretrained model files")
    args = vars(ap.parse_args())
    pretrained_path = args["pretrained"]
    checkpoint_models_path = 'models/'

    if pretrained_path is None:
        pretrained_path = get_best_model()

    # Callbacks
    tensor_board = keras.callbacks.TensorBoard(log_dir='./logs',
                                               histogram_freq=0,
                                               write_graph=True,
                                               write_images=True)
    model_names = checkpoint_models_path + 'model.{epoch:02d}-{val_loss:.4f}.hdf5'
    model_checkpoint = ModelCheckpoint(model_names,
                                       monitor='val_loss',
                                       verbose=1,
                                       save_best_only=True)
    early_stop = EarlyStopping('val_loss', patience=patience)
    reduce_lr = ReduceLROnPlateau('val_loss',
                                  factor=0.1,
                                  patience=int(patience / 4),
Exemple #17
0
import utils

predictors, response = utils.get_data()

x_train, x_test, y_train, y_test = utils.split_data(predictors, response)

# Models comparison
utils.get_best_model(x_train, x_test, y_train, y_test)