Python network.Network() Examples

The following are 27 code examples of network.Network(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module network , or try the search function .
Example #1
Source File: utils.py    From pytorch-vfi-cft with GNU General Public License v3.0 7 votes vote down vote up
def load_model(weight_path, continuous_fine_tuning=False):
    weight_path = os.path.abspath(weight_path)
    if not os.path.exists(weight_path):
        raise RuntimeError("Could not find file: " + weight_path + ". Did you remember to download the pretrained model?")
    if weight_path.endswith('.gz'):
        with gzip.open(weight_path, 'rb') as f_in:
            state = torch.load(f_in, get_device())
    else:
        state = torch.load(weight_path, get_device())
    net = Network()
    net.to(net.device)
    net.load_state_dict(state['network'])

    if continuous_fine_tuning:
        net.cft = CFT(state, net)
    return net 
Example #2
Source File: consensus.py    From research with MIT License 7 votes vote down vote up
def test():
    n = Network(LATENCY * 2)
    nodes = [Node(n, i, i%4==0) for i in range(20)]
    for i in range(30):
        for _ in range(2):
            z = random.randrange(20)
            n.send_to([1000 + i], z, at=5+i*2)
    for i in range(21 * LATENCY):
        n.tick()
        if i % 10 == 0:
            print("Value sets", [sorted(node.seen.keys()) for node in nodes])
    countz = {}
    maxval = ""
    for node in nodes:
        if node.honest:
            k = str(sorted(node.seen.keys()))
            countz[k] = countz.get(k, 0) + 1
            if countz[k] > countz.get(maxval, 0):
                maxval = k
    print("Most popular: %s" % maxval, "with %d agreeing" % countz[maxval]) 
Example #3
Source File: optimizer.py    From mnist-multi-gpu with Apache License 2.0 7 votes vote down vote up
def mutate(self, network):
        """Randomly mutate one part of the network.

        Args:
            network (dict): The network parameters to mutate

        Returns:
            (Network): A randomly mutated network object

        """
        # Choose a random key.
        mutation = random.choice(list(self.nn_param_choices.keys()))

        # Mutate one of the params.
        network.network[mutation] = random.choice(self.nn_param_choices[mutation])

        return network 
Example #4
Source File: optimizer.py    From mnist-multi-gpu with Apache License 2.0 7 votes vote down vote up
def create_population(self, count):
        """Create a population of random networks.

        Args:
            count (int): Number of networks to generate, aka the
                size of the population

        Returns:
            (list): Population of network objects

        """
        pop = []
        for _ in range(0, count):
            # Create a random network.
            network = Network(self.nn_param_choices)
            network.create_random()

            # Add the network to our population.
            pop.append(network)

        return pop 
Example #5
Source File: main.py    From 3DRegNet with MIT License 7 votes vote down vote up
def main(config):

    #Initialize Network
    net = Network(config)

    data = {}

    if config.run_mode == 'train':

        data['train'] = load_data(config, 'train')

        net.train(data)

    if config.run_mode == 'test':

        data['test'] = load_data(config, 'test')

        net.test(data)

    return 0 
Example #6
Source File: run_mlp.py    From Artificial-Neural-Network-THU-2018 with MIT License 7 votes vote down vote up
def three_layer_relu():
    model = Network()
    model.add(Linear('fc1', 784, 256, 0.001))
    model.add(Relu('rl1'))
    model.add(Linear('fc2', 256, 128, 0.001))
    model.add(Relu('rl2'))
    model.add(Linear('fc3', 128, 10, 0.001))
    model.add(Relu('rl3'))
    config = {
        'learning_rate': 0.0001,
        'weight_decay': 0.005,
        'momentum': 0.9,
        'batch_size': 300,
        'max_epoch': 60,
        'disp_freq': 50,
        'test_epoch': 5
    }
    return model, config 
Example #7
Source File: run_mlp.py    From Artificial-Neural-Network-THU-2018 with MIT License 7 votes vote down vote up
def three_layer_sigmoid():
    model = Network()
    model.add(Linear('fc1', 784, 256, 0.001))
    model.add(Sigmoid('sg1'))
    model.add(Linear('fc2', 256, 128, 0.001))
    model.add(Sigmoid('sg2'))
    model.add(Linear('fc3', 128, 10, 0.001))
    model.add(Sigmoid('sg3'))
    config = {
        'learning_rate': 0.01,
        'weight_decay': 0.005,
        'momentum': 0.9,
        'batch_size': 300,
        'max_epoch': 60,
        'disp_freq': 50,
        'test_epoch': 5
    }
    return model, config 
Example #8
Source File: run_mlp.py    From Artificial-Neural-Network-THU-2018 with MIT License 7 votes vote down vote up
def two_layer_relu():
    model = Network()
    model.add(Linear('fc1', 784, 256, 0.001))
    model.add(Relu('rl1'))
    model.add(Linear('fc2', 256, 10, 0.001))
    model.add(Relu('rl2'))
    config = {
        'learning_rate': 0.0001,
        'weight_decay': 0.005,
        'momentum': 0.9,
        'batch_size': 200,
        'max_epoch': 40,
        'disp_freq': 50,
        'test_epoch': 5
    }
    return model, config 
Example #9
Source File: run_mlp.py    From Artificial-Neural-Network-THU-2018 with MIT License 7 votes vote down vote up
def two_layer_sigmoid():
    model = Network()
    model.add(Linear('fc1', 784, 256, 0.001))
    model.add(Sigmoid('sg1'))
    model.add(Linear('fc2', 256, 10, 0.001))
    model.add(Sigmoid('sg2'))
    config = {
        'learning_rate': 0.01,
        'weight_decay': 0.005,
        'momentum': 0.9,
        'batch_size': 100,
        'max_epoch': 20,
        'disp_freq': 50,
        'test_epoch': 5
    }
    return model, config 
Example #10
Source File: eval_lfw.py    From Probabilistic-Face-Embeddings with MIT License 7 votes vote down vote up
def main(args):


    paths = Dataset(args.dataset_path)['abspath']
    print('%d images to load.' % len(paths))
    assert(len(paths)>0)

    # Load model files and config file
    network = Network()
    network.load_model(args.model_dir) 
    images = preprocess(paths, network.config, False)

    # Run forward pass to calculate embeddings
    mu, sigma_sq = network.extract_feature(images, args.batch_size, verbose=True)
    feat_pfe = np.concatenate([mu, sigma_sq], axis=1)
    
    lfwtest = LFWTest(paths)
    lfwtest.init_standard_proto(args.protocol_path)

    accuracy, threshold = lfwtest.test_standard_proto(mu, utils.pair_euc_score)
    print('Euclidean (cosine) accuracy: %.5f threshold: %.5f' % (accuracy, threshold))
    accuracy, threshold = lfwtest.test_standard_proto(feat_pfe, utils.pair_MLS_score)
    print('MLS accuracy: %.5f threshold: %.5f' % (accuracy, threshold)) 
Example #11
Source File: optimizer.py    From neural-network-genetic-algorithm with MIT License 7 votes vote down vote up
def mutate(self, network):
        """Randomly mutate one part of the network.

        Args:
            network (dict): The network parameters to mutate

        Returns:
            (Network): A randomly mutated network object

        """
        # Choose a random key.
        mutation = random.choice(list(self.nn_param_choices.keys()))

        # Mutate one of the params.
        network.network[mutation] = random.choice(self.nn_param_choices[mutation])

        return network 
Example #12
Source File: daemon.py    From encompass with GNU General Public License v3.0 7 votes vote down vote up
def __init__(self, config):
        threading.Thread.__init__(self)
        self.daemon = True
        self.debug = False
        self.config = config
        self.network = Network(config)
        # network sends responses on that queue
        self.network_queue = Queue.Queue()

        self.running = False
        self.lock = threading.RLock()

        # each GUI is a client of the daemon
        self.clients = []
        self.request_id = 0
        self.requests = {} 
Example #13
Source File: optimizer.py    From neural-network-genetic-algorithm with MIT License 6 votes vote down vote up
def breed(self, mother, father):
        """Make two children as parts of their parents.

        Args:
            mother (dict): Network parameters
            father (dict): Network parameters

        Returns:
            (list): Two network objects

        """
        children = []
        for _ in range(2):

            child = {}

            # Loop through the parameters and pick params for the kid.
            for param in self.nn_param_choices:
                child[param] = random.choice(
                    [mother.network[param], father.network[param]]
                )

            # Now create a network object.
            network = Network(self.nn_param_choices)
            network.create_set(child)

            # Randomly mutate some of the children.
            if self.mutate_chance > random.random():
                network = self.mutate(network)

            children.append(network)

        return children 
Example #14
Source File: optimizer.py    From neural-network-genetic-algorithm with MIT License 6 votes vote down vote up
def create_population(self, count):
        """Create a population of random networks.

        Args:
            count (int): Number of networks to generate, aka the
                size of the population

        Returns:
            (list): Population of network objects

        """
        pop = []
        for _ in range(0, count):
            # Create a random network.
            network = Network(self.nn_param_choices)
            network.create_random()

            # Add the network to our population.
            pop.append(network)

        return pop 
Example #15
Source File: nodes.py    From anima with MIT License 5 votes vote down vote up
def __init__(self, nodesName_in):

        self.nodesName = nodesName_in
        self._controllers = []
        self._utilities = []
        self._joints = []
        self._curves = []

        self.network = Network(self.nodesName) 
Example #16
Source File: nodes.py    From anima with MIT License 5 votes vote down vote up
def appendObject(self, nodeType, object_in):
        #appends the given objects to the node Network
        if isinstance(object_in, (list)):
            for obj in object_in:
                if isinstance(obj, (list)):
                    for ob in obj:
                        self.append_to(nodeType, ob)
                else:
                    self.append_to(nodeType, obj)
        else:
            self.append_to(nodeType, object_in) 
Example #17
Source File: run_mlp.py    From Artificial-Neural-Network-THU-2018 with MIT License 5 votes vote down vote up
def one_layer_net():
    model = Network()
    model.add(Linear('fc1', 784, 10, 0.001))
    config = {
        'learning_rate': 0.00001,
        'weight_decay': 0.005,
        'momentum': 0.9,
        'batch_size': 50,
        'max_epoch': 10,
        'disp_freq': 50,
        'test_epoch': 5
    }
    return model, config 
Example #18
Source File: test.py    From glcic with MIT License 5 votes vote down vote up
def test():
    x = tf.placeholder(tf.float32, [BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, 3])
    mask = tf.placeholder(tf.float32, [BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, 1])
    local_x = tf.placeholder(tf.float32, [BATCH_SIZE, LOCAL_SIZE, LOCAL_SIZE, 3])
    global_completion = tf.placeholder(tf.float32, [BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, 3])
    local_completion = tf.placeholder(tf.float32, [BATCH_SIZE, LOCAL_SIZE, LOCAL_SIZE, 3])
    is_training = tf.placeholder(tf.bool, [])

    model = Network(x, mask, local_x, global_completion, local_completion, is_training, batch_size=BATCH_SIZE)
    sess = tf.Session()
    init_op = tf.global_variables_initializer()
    sess.run(init_op)

    saver = tf.train.Saver()
    saver.restore(sess, '../backup/latest')

    x_test = np.load(test_npy)
    np.random.shuffle(x_test)
    x_test = np.array([a / 127.5 - 1 for a in x_test])

    step_num = int(len(x_test) / BATCH_SIZE)

    cnt = 0
    for i in tqdm.tqdm(range(step_num)):
        x_batch = x_test[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]
        _, mask_batch = get_points()
        completion = sess.run(model.completion, feed_dict={x: x_batch, mask: mask_batch, is_training: False})
        for i in range(BATCH_SIZE):
            cnt += 1
            raw = x_batch[i]
            raw = np.array((raw + 1) * 127.5, dtype=np.uint8)
            masked = raw * (1 - mask_batch[i]) + np.ones_like(raw) * mask_batch[i] * 255
            img = completion[i]
            img = np.array((img + 1) * 127.5, dtype=np.uint8)
            dst = './output/{}.jpg'.format("{0:06d}".format(cnt))
            output_image([['Input', masked], ['Output', img], ['Ground Truth', raw]], dst) 
Example #19
Source File: network_proxy.py    From encompass with GNU General Public License v3.0 5 votes vote down vote up
def switch_to_active_chain(self):
#        print("\nNetworkProxy switch to active chain, waiting for lock")
        with self.lock:
#            print(" Got lock")
            self.message_id = 0
            self.unanswered_requests = {}
            self.subscriptions = {}
            self.pending_transactions_for_notifications = []
#            print(" Stopping current network")
            self.network.stop()
            time.sleep(0.3)

            self.network = Network(self.config)
#            print(" Set new network")
            self.pipe = util.QueuePipe(send_queue=self.network.requests_queue)
            self.network.start(self.pipe.get_queue)
#            print(" Started new network")

            self.status = 'connecting'
            self.servers = {}
            self.banner = ''
            self.blockchain_height = 0
            self.server_height = 0
            self.interfaces = []

            for key in ['status','banner','updated','servers','interfaces']:
                value = self.network.get_status_value(key)
                self.pipe.get_queue.put({'method':'network.status', 'params':[key, value]}) 
Example #20
Source File: network_proxy.py    From encompass with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, socket, config=None):

        if config is None:
            config = {}  # Do not use mutables as default arguments!
        threading.Thread.__init__(self)
        self.config = SimpleConfig(config) if type(config) == type({}) else config
        self.message_id = 0
        self.unanswered_requests = {}
        self.subscriptions = {}
        self.debug = False
        self.lock = threading.Lock()
        self.pending_transactions_for_notifications = []
        self.callbacks = {}
        self.running = True
        self.daemon = True

        if socket:
            self.pipe = util.SocketPipe(socket)
            self.network = None
        else:
            self.network = Network(config)
            self.pipe = util.QueuePipe(send_queue=self.network.requests_queue)
            self.network.start(self.pipe.get_queue)
            for key in ['status','banner','updated','servers','interfaces']:
                value = self.network.get_status_value(key)
                self.pipe.get_queue.put({'method':'network.status', 'params':[key, value]})

        # status variables
        self.status = 'connecting'
        self.servers = {}
        self.banner = ''
        self.blockchain_height = 0
        self.server_height = 0
        self.interfaces = [] 
Example #21
Source File: main.py    From gated-pixel-cnn with Apache License 2.0 5 votes vote down vote up
def main(_):
  model_dir = util.get_model_dir(conf, 
      ['data_dir', 'sample_dir', 'max_epoch', 'test_step', 'save_step',
       'is_train', 'random_seed', 'log_level', 'display', 'runtime_base_dir', 
       'occlude_start_row', 'num_generated_images'])
  util.preprocess_conf(conf)
  validate_parameters(conf)

  data = 'mnist' if conf.data == 'color-mnist' else conf.data 
  DATA_DIR = os.path.join(conf.runtime_base_dir, conf.data_dir, data)
  SAMPLE_DIR = os.path.join(conf.runtime_base_dir, conf.sample_dir, conf.data, model_dir)

  util.check_and_create_dir(DATA_DIR)
  util.check_and_create_dir(SAMPLE_DIR)
  
  dataset = get_dataset(DATA_DIR, conf.q_levels)

  with tf.Session() as sess:
    network = Network(sess, conf, dataset.height, dataset.width, dataset.channels)

    stat = Statistic(sess, conf.data, conf.runtime_base_dir, model_dir, tf.trainable_variables())
    stat.load_model()

    if conf.is_train:
      train(dataset, network, stat, SAMPLE_DIR)
    else:
      generate(network, dataset.height, dataset.width, SAMPLE_DIR) 
Example #22
Source File: brute.py    From neural-network-genetic-algorithm with MIT License 5 votes vote down vote up
def generate_network_list(nn_param_choices):
    """Generate a list of all possible networks.

    Args:
        nn_param_choices (dict): The parameter choices

    Returns:
        networks (list): A list of network objects

    """
    networks = []

    # This is silly.
    for nbn in nn_param_choices['nb_neurons']:
        for nbl in nn_param_choices['nb_layers']:
            for a in nn_param_choices['activation']:
                for o in nn_param_choices['optimizer']:

                    # Set the parameters.
                    network = {
                        'nb_neurons': nbn,
                        'nb_layers': nbl,
                        'activation': a,
                        'optimizer': o,
                    }

                    # Instantiate a network object with set parameters.
                    network_obj = Network()
                    network_obj.create_set(network)

                    networks.append(network_obj)

    return networks 
Example #23
Source File: optimizer.py    From mnist-multi-gpu with Apache License 2.0 5 votes vote down vote up
def breed(self, mother, father):
        """Make two children as parts of their parents.

        Args:
            mother (dict): Network parameters
            father (dict): Network parameters

        Returns:
            (list): Two network objects

        """
        children = []
        for _ in range(2):

            child = {}

            # Loop through the parameters and pick params for the kid.
            for param in self.nn_param_choices:
                child[param] = random.choice(
                    [mother.network[param], father.network[param]]
                )

            # Now create a network object.
            network = Network(self.nn_param_choices)
            network.create_set(child)

            children.append(network)

        return children 
Example #24
Source File: eval_ijb.py    From Probabilistic-Face-Embeddings with MIT License 5 votes vote down vote up
def main(args):

    network = Network()
    network.load_model(args.model_dir)
    proc_func = lambda x: preprocess(x, network.config, False)

    testset = Dataset(args.dataset_path)
    if args.protocol == 'ijba':
        tester = IJBATest(testset['abspath'].values)
        tester.init_proto(args.protocol_path)
    elif args.protocol == 'ijbc':
        tester = IJBCTest(testset['abspath'].values)
        tester.init_proto(args.protocol_path)
    else:
        raise ValueError('Unkown protocol. Only accept "ijba" or "ijbc".')


    mu, sigma_sq = network.extract_feature(tester.image_paths, args.batch_size, proc_func=proc_func, verbose=True)
    features = np.concatenate([mu, sigma_sq], axis=1)

    print('---- Average pooling')
    aggregate_templates(tester.verification_templates, features, 'mean')
    TARs, std, FARs = tester.test_verification(force_compare(utils.pair_euc_score))
    for i in range(len(TARs)):
        print('TAR: {:.5} +- {:.5} FAR: {:.5}'.format(TARs[i], std[i], FARs[i]))

    print('---- Uncertainty pooling')
    aggregate_templates(tester.verification_templates, features, 'PFE_fuse')
    TARs, std, FARs = tester.test_verification(force_compare(utils.pair_euc_score))
    for i in range(len(TARs)):
        print('TAR: {:.5} +- {:.5} FAR: {:.5}'.format(TARs[i], std[i], FARs[i]))


    print('---- MLS comparison')
    aggregate_templates(tester.verification_templates, features, 'PFE_fuse_match')
    TARs, std, FARs = tester.test_verification(force_compare(utils.pair_MLS_score))
    for i in range(len(TARs)):
        print('TAR: {:.5} +- {:.5} FAR: {:.5}'.format(TARs[i], std[i], FARs[i])) 
Example #25
Source File: brute.py    From mnist-multi-gpu with Apache License 2.0 5 votes vote down vote up
def generate_network_list(nn_param_choices):
    """Generate a list of all possible networks.

    Args:
        nn_param_choices (dict): The parameter choices

    Returns:
        networks (list): A list of network objects

    """
    networks = []

    # This is silly.
    for nbn in nn_param_choices['nb_neurons']:
        for nbl in nn_param_choices['nb_layers']:
            for a in nn_param_choices['activation']:
                for o in nn_param_choices['optimizer']:

                    # Set the parameters.
                    network = {
                        'nb_neurons': nbn,
                        'nb_layers': nbl,
                        'activation': a,
                        'optimizer': o,
                    }

                    # Instantiate a network object with set parameters.
                    network_obj = Network()
                    network_obj.create_set(network)

                    networks.append(network_obj)

    return networks 
Example #26
Source File: train.py    From Probabilistic-Face-Embeddings with MIT License 4 votes vote down vote up
def main(args):

    # I/O
    config_file = args.config_file
    config = imp.load_source('config', config_file)
    if args.name:
        config.name = args.name

    trainset = Dataset(config.train_dataset_path)

    network = Network()
    network.initialize(config, trainset.num_classes)

    # Initalization for running
    log_dir = utils.create_log_dir(config, config_file)
    summary_writer = tf.summary.FileWriter(log_dir, network.graph)
    if config.restore_model:
        network.restore_model(config.restore_model, config.restore_scopes)

    proc_func = lambda images: preprocess(images, config, True)
    trainset.start_batch_queue(config.batch_format, proc_func=proc_func)


    # Main Loop
    print('\nStart Training\nname: {}\n# epochs: {}\nepoch_size: {}\nbatch_size: {}\n'.format(
            config.name, config.num_epochs, config.epoch_size, config.batch_format['size']))
    global_step = 0
    start_time = time.time()
    for epoch in range(config.num_epochs):

        # Training
        for step in range(config.epoch_size):
            # Prepare input
            learning_rate = utils.get_updated_learning_rate(global_step, config)
            batch = trainset.pop_batch_queue()

            wl, sm, global_step = network.train(batch['image'], batch['label'], learning_rate, config.keep_prob)

            wl['lr'] = learning_rate

            # Display
            if step % config.summary_interval == 0:
                duration = time.time() - start_time
                start_time = time.time()
                utils.display_info(epoch, step, duration, wl)
                summary_writer.add_summary(sm, global_step=global_step)

        # Save the model
        network.save_model(log_dir, global_step) 
Example #27
Source File: frcnn.py    From incremental_detectors with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def eval_network(sess):
    net = Network(num_classes=args.num_classes+args.extend, distillation=False)
    _, _, remain = split_classes()
    loader = get_loader(False, remain)
    is_voc = loader.dataset == 'voc'

    if args.eval_ckpts != '':
        ckpts = args.eval_ckpts.split(',')
    else:
        ckpts = [args.ckpt]
    global_results = {cat: [] for cat in loader.categories}
    global_results[AVERAGE+" 1-10"] = []
    global_results[AVERAGE+" 11-20"] = []
    global_results[AVERAGE+" ALL"] = []
    for ckpt in ckpts:
        if ckpt[-1].lower() == 'k':
            ckpt_num = int(ckpt[:-1])*1000
        else:
            ckpt_num = int(ckpt)
        init_op, init_feed_dict = restore_ckpt(ckpt_num=ckpt_num)
        sess.run(init_op, feed_dict=init_feed_dict)
        log.info("Checkpoint {}".format(ckpt))
        if is_voc:
            results = Evaluation(net, loader, ckpt_num, args.conf_thresh, args.nms_thresh).evaluate_network(args.eval_first_n)
            for cat in loader.categories:
                global_results[cat].append(results[cat] if cat in results else 0.0)
            # TODO add output formating, line after learnt cats
            old_classes = [results.get(k, 0) for k in loader.categories[:10]]
            new_classes = [results.get(k, 0) for k in loader.categories[10:]]
            all_classes = [results.get(k, 0) for k in loader.categories]
            global_results[AVERAGE+" 1-10"].append(np.mean(old_classes))
            global_results[AVERAGE+" 11-20"].append(np.mean(new_classes))
            global_results[AVERAGE+" ALL"].append(np.mean(all_classes))

            headers = ['Category'] + [("mAP (%s, %i img)" % (ckpt, args.eval_first_n)) for ckpt in ckpts]
            table_src = []
            for cat in loader.categories:
                table_src.append([cat] + global_results[cat])
            table_src.append([AVERAGE+" 1-10", ] + global_results[AVERAGE+" 1-10"])
            table_src.append([AVERAGE+" 11-20", ] + global_results[AVERAGE+" 11-20"])
            table_src.append([AVERAGE+" ALL", ] + global_results[AVERAGE+" ALL"])
            out = tabulate(table_src, headers=headers,
                           floatfmt=".1f", tablefmt='orgtbl')
            with open("/home/lear/kshmelko/scratch/logs/results_voc/%s.pkl" % args.run_name, 'wb') as f:
                pickle.dump(global_results, f, pickle.HIGHEST_PROTOCOL)
            log.info("Summary table over %i checkpoints\nExperiment: %s\n%s", len(ckpts), args.run_name, out)
        else:
            results = COCOEval(net, loader, ckpt_num, args.conf_thresh, args.nms_thresh).evaluate_network(args.eval_first_n)