def main(**args):
    # disable access to the installed CUDA device
    if args['usecpu']:
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

    # a lil hacky way to get the model to restore properly
    network = args['network']
    packet_shape = args['packet_shape']
    item_types = dict.fromkeys(cons.ALL_ITEM_TYPES, True)
    item_shapes = dat.get_data_item_shapes(packet_shape, item_types)
    model = netutils.import_model(network, item_shapes, **args)
    graph = model.network_graph

    logdir = args['logdir']
    filter_slice = args.get('filter_slice', slice(0, None))
    depth_slice = args.get('depth_slice', slice(0, None))
    conv_layers = (name for name, layer in graph.trainable_layers.items()
                   if layer['type'] == 'Conv2D')
    conv_weights = ((layer_name, model.get_layer_weights(layer_name))
                    for layer_name in conv_layers)
    for layer_name, weights in conv_weights:
        weights_sliced = weights[filter_slice, depth_slice]
        fig = visualize_conv_weights(weights_sliced)
        filename = 'weights_of_layer_{}'.format(layer_name)
        fig.savefig(os.path.join(logdir, filename))
        plt.close(fig)
Пример #2
0
def main(**settings):
    # load dataset
    name, srcdir = settings['name'], settings['srcdir']
    item_types = settings['item_types']
    input_handler = io_utils.DatasetFsPersistencyHandler(load_dir=srcdir)
    dataset = input_handler.load_dataset(name, item_types=item_types)

    # create dataset splitter
    fraction = settings['test_items_fraction']
    num_items = settings['test_items_count']
    splitter = netutils.DatasetSplitter('RANDOM',
                                        items_fraction=fraction,
                                        num_items=num_items)

    # import network
    net_module_name = settings['network']
    model = netutils.import_model(net_module_name, dataset.item_shapes,
                                  **settings)
    graph = model.network_graph

    # prepare network trainer
    num_epochs = settings['num_epochs']
    trainer = train_utils.TfModelTrainer(
        splitter.get_data_and_targets(dataset), **settings)

    # main loop
    weights = {
        layer: model.get_layer_weights(layer)
        for layer in graph.trainable_layers
    }
    biases = {
        layer: model.get_layer_biases(layer)
        for layer in graph.trainable_layers
    }
    run_id = 'cval_{}'.format(netutils.get_default_run_id(net_module_name))
    num_crossvals = settings['num_crossvals']
    for run_idx in range(num_crossvals):
        print('Starting run {}'.format(run_idx + 1))
        data_dict = splitter.get_data_and_targets(dataset,
                                                  dict_format='PER_SET')
        tr, te = data_dict['train'], data_dict['test']
        inputs_dict = {
            'train_data': netutils.convert_to_model_inputs_dict(model, tr),
            'train_targets': netutils.convert_to_model_outputs_dict(model, tr),
            'test_data': netutils.convert_to_model_inputs_dict(model, te),
            'test_targets': netutils.convert_to_model_outputs_dict(model, te),
        }
        trainer.train_model(model, data_dict=inputs_dict, run_id=run_id)
        # restore initial weights and biases
        for layer in graph.trainable_layers:
            model.set_layer_weights(layer, weights[layer])
            model.set_layer_biases(layer, biases[layer])
def main(**settings):
    logdir = settings['logdir']

    # disable CUDA device access
    if settings['usecpu']:
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

    # load dataset
    name, srcdir = settings['name'], settings['srcdir']
    item_types = settings['item_types']
    input_handler = dset_io.DatasetFsPersistencyHandler(load_dir=srcdir)
    dataset = input_handler.load_dataset(name, item_types=item_types)
    items_slice = settings.get('items_slice', slice(0, None))
    data = dataset.get_data_as_dict(items_slice)

    # import network model
    net_module_name = settings['network']
    model = netutils.import_model(net_module_name, dataset.item_shapes,
                                  **settings)
    graph = model.network_graph
    for data_path_layers in graph.data_paths.values():
        model.enable_hidden_layer_output(data_path_layers)

    # feed data to network model
    inputs = netutils.convert_dataset_items_to_model_inputs(model, data)
    activations = model.get_hidden_layer_activations(inputs)
    input_layers, hidden_layers = graph.input_layers, graph.hidden_layers
    #data_paths = model.network_graph.data_paths
    for layer_name, layer_activations in activations.items():
        out_dir = os.path.join(logdir, layer_name)
        os.makedirs(out_dir, exist_ok=True)
        layer = hidden_layers[layer_name]['layer']
        if len(layer.shape[1:]) == 3:
            fig_creator = acviz.visualize_3d_activations
        elif len(layer.shape[1:]) == 1:
            fig_creator = acviz.visualize_1d_activations
        print('creating activation figures for layer {}'.format(layer_name))
        for idx in range(len(layer_activations)):
            fig = fig_creator(layer_activations[idx], layer_name)
            savefile = os.path.join(
                out_dir,
                'layer_{}_item_{}.svg'.format(layer_name,
                                              items_slice.start + idx))
            acviz.save_figure(fig, savefile)
Пример #4
0
    # load input dataset
    input_handler = io_utils.DatasetFsPersistencyHandler(load_dir=srcdir)
    dataset = input_handler.load_dataset(name, item_types=item_types)

    #load trained network model
    logdir = cutils.get_config_for_module("model_checker")['default']['logdir']
    if not os.path.exists(logdir):
        os.mkdir(logdir)
    network_module_name, model_file = args.network, args.model_file
    network_module_name = "net.samples." + network_module_name
    run_id = netutils.get_default_run_id(network_module_name)
    tb_dir = os.path.join(logdir, run_id)
    os.mkdir(tb_dir)
    model = netutils.import_model(network_module_name,
                                  dataset.item_shapes,
                                  model_file=model_file,
                                  tb_dir=tb_dir)

    # check (evaluate) model
    log_data = test_utils.evaluate_classification_model(model,
                                                        dataset,
                                                        items_slice=slice(
                                                            args.start_item,
                                                            args.stop_item))

    # output results
    meta_fields = dataset.metadata_fields
    extra_fields = list(meta_fields)
    extra_fields.sort()
    headers = netutils.CLASSIFICATION_FIELDS + extra_fields
    writer = csv.DictWriter(args.outfile, headers, delimiter='\t')
Пример #5
0
    # load dataset
    name, srcdir, item_types = args['name'], args['srcdir'], args['item_types']
    input_handler = io_utils.DatasetFsPersistencyHandler(load_dir=srcdir)
    dataset = input_handler.load_dataset(name, item_types=item_types)

    # create splitter and split dataset into train and test data
    mode = args['split_mode']
    fraction, num_items = args['test_items_fraction'], args['test_items_count']
    splitter = netutils.DatasetSplitter(mode,
                                        items_fraction=fraction,
                                        num_items=num_items)

    # import network model
    network, model_file = 'net.samples.' + args['network'], args['model_file']
    tb_dir = args['tb_dir']
    model = netutils.import_model(network, dataset.item_shapes, **args)

    # prepare network trainer
    data_dict = splitter.get_data_and_targets(dataset, dict_format='PER_SET')
    train, test = data_dict['train'], data_dict['test']
    inputs_dict = {
        'train_data': netutils.convert_to_model_inputs_dict(model, train),
        'train_targets': netutils.convert_to_model_outputs_dict(model, train),
        'test_data': netutils.convert_to_model_inputs_dict(model, test),
        'test_targets': netutils.convert_to_model_outputs_dict(model, test),
    }
    trainer = train_utils.TfModelTrainer(inputs_dict, **args)

    # train model and optionally save if requested
    trainer.train_model(model)
    if args['save']: