Beispiel #1
0
def train_model():
    use_gpu = True

    if use_gpu and torch.cuda.is_available():
        device = torch.device("cuda")
    else:
        device = torch.device("cpu")

    print("Loading datasets")
    train_df = utils.import_file("train")
    dev_df = utils.import_file("dev")
    test_df = utils.import_file("test")

    print("Building tokenizer")
    tokenizer = Tokenizer()

    batch_size = 32
    hidden_size = 128
    max_length = 200
    epochs = 10
    dropout_p = 0.1

    print("Creating training dataloader")
    dataloader_train = get_data_loader_bilstm(train_df,
                                              tokenizer,
                                              batch_size=batch_size)
    print("Creating dev dataloader")
    dataloader_dev = get_data_loader_bilstm(dev_df,
                                            tokenizer,
                                            batch_size=batch_size)
    print("Creating test dataloader")
    dataloader_test = get_data_loader_bilstm(test_df,
                                             tokenizer,
                                             batch_size=batch_size,
                                             test=True)

    print("Building encoder")
    encoder = comparer_model.EncoderRNN(tokenizer.vocab_size, hidden_size)
    print("Building decoder")
    decoder = comparer_model.AttnDecoderRNN(tokenizer.vocab_size,
                                            hidden_size,
                                            max_length,
                                            dropout_p=dropout_p)

    print("Creating Trainer")
    trainer = TrainerBiLSTM(encoder, decoder, batch_size, device, max_length)
    print("Starting training")
    trainer.train(dataloader_train, epochs)
Beispiel #2
0
def main(args):
    # Get the configuration file
    config = utils.import_file(os.path.join(args.model_dir, 'config.py'), 'config')
    
    # Get the paths of the aligned images
    with open(args.image_list) as f:
        paths = [line.strip() for line in f]
    print('%d images to load.' % len(paths))
    assert(len(paths)>0)
    

    # Pre-process the images
    images = utils.preprocess(paths, config, False)
    switch = np.array([utils.is_typeB(p) for p in paths])


    # Load model files and config file
    if config.use_sibling:
        network = SiblingNetwork()
    else:
        network = BaseNetwork()
    network.load_model(args.model_dir)


    # Run forward pass to calculate embeddings
    if config.use_sibling:
        embeddings = network.extract_feature(images, switch, args.batch_size, verbose=True)
    else:
        embeddings = network.extract_feature(images, args.batch_size, verbose=True)


    # Output the extracted features
    np.save(args.output, embeddings)
Beispiel #3
0
def load(script_name, file_extension=config.g_ratt_file_extension):
    """ Dynamically loads the specifies script. 'script_name' is a string of 
        script's file name WITHOUT a file extension. 'file_extension' appended
        to 'script_name' to complete the script's file name.
        
        The method returns the 'module object' for the loaded script.
    """
    m, e = utils.import_file(script_name + file_extension,
                             config.g_script_paths)
    if (m == None):
        output.writeline("Error loading script: (). [{}]".format(e))
        return None

    return m
Beispiel #4
0
    def load_dataset(self):
        train_df = import_file("train", path=self.dataset_path)
        dev_df = import_file("dev", path=self.dataset_path)
        test_df = import_file("test", path=self.dataset_path)
        train_df_aug = augment_dataset(
            train_df,
            lambda score: score < -1,
            lambda score: score < -0.3,
            lambda score: score > 0.55,
            lambda score: score > 1,
            lambda score: score > 1.3,
        )

        self.dataLoader_train = get_data_loader(train_df, batch_size=32)
        self.dataLoader_train_aug = get_data_loader(train_df_aug, batch_size=32)
        self.dataLoader_dev = get_data_loader(dev_df, batch_size=32)
        self.dataLoader_test = get_data_loader(test_df, batch_size=32, test=True)

        # Masked versions of the dataset (load both into memory to avoid recalculation)
        self.dataLoader_train_masked = get_data_loader_masked(train_df, batch_size=32)
        self.dataLoader_train_aug_masked = get_data_loader_masked(
            train_df_aug, batch_size=32
        )
        self.dataLoader_dev_masked = get_data_loader_masked(dev_df, batch_size=32)
Beispiel #5
0
def main(args):
    # I/O
    config_file = args.config_file
    config = utils.import_file(config_file, 'config')

    #trainset = utils.Dataset(config.train_dataset_path)
    testset = utils.Dataset(config.test_dataset_path)

    network = BaseNetwork()
    network.initialize(config, 0 ) #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 is not None:
        network.restore_model(config.restore_model, config.restore_scopes)

    # Set up LFW test protocol and load images
    print('Loading images...')
    lfwtest = LFWTest(testset.images)
    lfwtest.init_standard_proto(config.lfw_pairs_file)
    lfwtest.images = utils.preprocess(lfwtest.image_paths, config, is_training=False)


    #trainset.start_batch_queue(config, True)


    #
    # Main Loop
    #
    print('\nStart Training\nname: %s\n# epochs: %d\nepoch_size: %d\nbatch_size: %d\n'\
        % (config.name, config.num_epochs, config.epoch_size, config.batch_size))
    global_step = 0

    # Testing on LFW
    print('Testing on Neetis LFW protocol...')
    embeddings = network.extract_feature(lfwtest.images, config.batch_size)
    print(type(embeddings))

    accuracy_embeddings, threshold_embeddings = lfwtest.test_standard_proto(embeddings)
    print('Embeddings Accuracy: %2.4f Threshold %2.3f' % (accuracy_embeddings, threshold_embeddings))

    with open(os.path.join(log_dir,'lfw_result.txt'),'at') as f:
        f.write('%d\t%.5f\n' % (global_step,accuracy_embeddings))
    summary = tf.Summary()
    summary.value.add(tag='lfw/accuracy', simple_value=accuracy_embeddings)
    summary_writer.add_summary(summary, global_step)
Beispiel #6
0
 def test_export_equals_import(self):
     sixteenth_per_beat = 13
     beats_per_bar = 52
     num_bars = 36
     sixteenth_duration = 24
     swing = False
     swing_amount = 0.3
     beat_pattern = []
     is_active = [1,1,1,1,1,1]
     file_name = 'temp.drum'
     utils.export_file(sixteenth_per_beat, beats_per_bar, num_bars, sixteenth_duration, swing, beat_pattern, is_active, swing_amount, file_name)
     s_p_b, b_p_b, n_b, s_d, s, b_p, is_a, s_a = utils.import_file(file_name)
     self.assertEquals(sixteenth_per_beat, s_p_b)
     self.assertEquals(beats_per_bar, b_p_b)
     self.assertEquals(num_bars, n_b)
     self.assertEquals(sixteenth_duration, s_d)
     self.assertEquals(swing, s)
     self.assertEquals(beat_pattern, b_p)
     os.remove(file_name)
Beispiel #7
0
def main(args):
    # Get the configuration file
    config = utils.import_file(os.path.join(args.model_dir, 'config.py'),
                               'config')

    # Get the paths of the aligned images
    with open(args.image_list) as f:
        paths = [line.strip() for line in f]
    print('%d images to load.' % len(paths))

    # Pre-process the images
    images = utils.preprocess(paths, config, False)
    assert (len(paths) > 0)

    # Load model files and config file
    if config.use_sibling:
        network = SiblingNetwork()
    else:
        network = BaseNetwork()
    network.load_model(args.model_dir)

    # Run forward pass to calculate embeddings
    if config.use_sibling:
        print(
            "\033[93mWARNING:\033[0m When using sibling network: we assume the images are in the order template, probe, template, probe, ...!"
        )
        assert len(
            images
        ) % 2 == 0, "The number of images are assumed to be even for using sibling network!"
        switch = utils.zero_one_switch(len(images))
        embeddings = network.extract_feature(images,
                                             switch,
                                             args.batch_size,
                                             verbose=True)
    else:
        embeddings = network.extract_feature(images,
                                             args.batch_size,
                                             verbose=True)

    # Output the extracted features
    np.save(args.output, embeddings)
Beispiel #8
0
def man(script_name, file_extension=config.g_ratt_file_extension):
    """ Displays help for the specified script name and its 'run()' method 
        (which is assumed to be the script's primary/entry function).
    """

    # Look-up/load the script
    filename = script_name + file_extension
    m, e = utils.import_file(filename, config.g_script_paths)
    if (m == None):
        output.writeline("ERROR: {} was not found. [{}]".format(
            script_name + file_extension, e))
        return

    # get the module's location
    modname = os.path.splitext(os.path.split(filename)[1])[0]
    modpath = config.g_utils_import_paths[modname]

    # get module help
    indent = "    "
    modhelp = indent + pydoc.getdoc(m).replace("\n", "\n" + indent)
    try:
        runhelp = indent + pydoc.getdoc(m.run).replace("\n", "\n" + indent)
    except:
        runhelp = None

    # Display the help text
    output.writeline("")
    output.writeline("MODULE NAME:")
    output.writeline(indent + modname)
    output.writeline("")
    output.writeline("LOCATION:")
    output.writeline(indent + modpath)
    output.writeline("")
    output.writeline("DESCRIPTION:")
    output.writeline(modhelp)
    output.writeline("")
    if (runhelp != None):
        output.writeline("RUN() FUNCTION:")
        output.writeline(runhelp)
        output.writeline("")
Beispiel #9
0
def validate_lfw(args):
    config = utils.import_file(args.config_file, 'config')
    testset = utils.Dataset(args.test_dataset_path)

    # Carregando modelo
    print('Loading network model...')
    network = BaseNetwork()
    network.load_model(args.model_dir)

    # Set up LFW test protocol and load images
    print('Loading images...')
    lfwtest = LFWTest(testset.images)
    lfwtest.init_standard_proto(args.lfw_pairs_file)
    lfwtest.images = utils.preprocess(lfwtest.image_paths,
                                      config,
                                      is_training=False)

    # Testing on LFW
    print('Testing on standard LFW protocol...')
    embeddings = network.extract_feature(lfwtest.images, config.batch_size)
    accuracy_embeddings, threshold_embeddings = lfwtest.test_standard_proto(
        embeddings)
    print('Embeddings Accuracy: %2.4f Threshold %2.3f' %
          (accuracy_embeddings, threshold_embeddings))
Beispiel #10
0
def main(args):
    # I/O
    config_file = args.config_file
    config = utils.import_file(config_file, 'config')

    trainset = utils.Dataset(config.train_dataset_path)
    testset = utils.Dataset(config.test_dataset_path)

    network = SiblingNetwork()
    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)

    # Set up test protocol and load images
    print('Loading images...')
    testset.separate_template_and_probes()
    testset.images = utils.preprocess(testset.images, config, is_training=False)


    trainset.start_batch_queue(config, True)


    #
    # Main Loop
    #
    print('\nStart Training\nname: %s\n# epochs: %d\nepoch_size: %d\nbatch_size: %d\n'\
        % (config.name, config.num_epochs, config.epoch_size, config.batch_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)
            image_batch, label_batch = trainset.pop_batch_queue()
        
            switch_batch = utils.zero_one_switch(len(image_batch))
            wl, sm, global_step = network.train(image_batch, label_batch, switch_batch, learning_rate, config.keep_prob)

            # 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)

        # Testing
        print('Testing...')
        switch = utils.zero_one_switch(len(testset.images))
        embeddings = network.extract_feature(testset.images, switch, config.batch_size)
        tars, fars, _ = utils.test_roc(embeddings, FARs=[1e-4, 1e-3, 1e-2])
        with open(os.path.join(log_dir,'result.txt'),'at') as f:
            for i in range(len(tars)):
                print('[%d] TAR: %2.4f FAR %2.3f' % (epoch+1, tars[i], fars[i]))
                f.write('[%d] TAR: %2.4f FAR %2.3f\n' % (epoch+1, tars[i], fars[i]))
                summary = tf.Summary()
                summary.value.add(tag='test/tar_%d'%i, simple_value=tars[i])
                summary_writer.add_summary(summary, global_step)

        # Save the model
        network.save_model(log_dir, global_step)
                print("P {} using {}".format(self.number,
                                             self.message_queue[0]))
                self.registers[self.X] = self.message_queue.pop(0)
            elif command == 'set':
                self.registers[self.X] = self.Y
            elif command == 'add':
                self.registers[self.X] += self.Y
            elif command == 'mul':
                self.registers[self.X] *= self.Y
            elif command == 'mod':
                self.registers[self.X] %= self.Y
            #increment
            self.i += 1


instructions = utils.import_file("Inputs/input18.txt", split=True)
i1 = instructions[:]
i2 = instructions[:]
p1 = Program(0, i1)
p2 = Program(1, i2, target=p1)
p1.set_target(p2)

thread1 = ProgramThread(p1)
thread2 = ProgramThread(p2)
thread1.start()
thread2.start()

threads = []
threads.append(thread1)
threads.append(thread2)
for t in threads:
Beispiel #12
0
def main():

    # Parse command line
    args = docopt(__doc__, version=VERSION, options_first=True)

    # Add the ratt directory to the system path (so module can access the
    # 'utils' package
    sys.path.append(__file__)

    # Enumrate Windoze COM Ports
    if (args['--serialports'] == True):
        ports = utils.get_available_serial_ports(platform="Windows")
        for p in ports:
            print(p)
        sys.exit()

    # Get Newline option
    config.newline = '\r\n' if args['--crlf'] else '\n'

    # Open log file (when not disabled)
    logfile = None
    if (args['--log'] == True or args['--vlog'] or args['--dlog']):
        if (args['--vlog']):
            config.g_verbose_logs = True
        if (args['--dlog']):
            config.g_debug_logs = True
        logfile = open(utils.append_current_time(args['--logfile']), "wb")

    ## Created 'Expected' object for a: Windoze executable UUT
    if (args['--win']):
        config.g_uut = rexpect.ExpectWindowsConsole(
            " ".join(args['<executable>']), logfile)

    # Created 'Expected' object for a: Linux/Wsl executable UUT
    elif (args['--linux']):
        config.g_uut = rexpect.ExpectLinuxConsole(
            " ".join(args['<executable>']), logfile)

    # Created 'Expected' object for a: UUT via a Windoze COM Port
    elif (args['--comport']):
        serial = utils.open_serial_port('com' + args['<comnum>'],
                                        timeout=0,
                                        baudrate=int(args['--baud']),
                                        parity=args['--parity'],
                                        stopbits=int(args['--stopbits']),
                                        bytesize=int(args['--databits']))
        config.g_uut = rexpect.ExpectSerial(serial, logfile)

    # Create 'Expected' object for a: NO UUT
    elif (args['--nouut']):
        config.g_uut = rexpect.ExpectNullConsole(logfile)

    # Enable output
    output.set_verbose_mode(args['-v'])
    output.set_debug_mode(args['--debug'])
    output.set_output_fd(sys.stdout, logfile)

    # Get script paths
    config.g_script_paths.append('.')
    if (args['--path1'] != None):
        config.g_script_paths.append(args['--path1'])
    if (args['--path2'] != None):
        config.g_script_paths.append(args['--path2'])
    if (args['--path3'] != None):
        config.g_script_paths.append(args['--path3'])

    # Check for batch mode
    if (args['--input'] != None):
        input, result = utils.import_file(args['--input'],
                                          config.g_script_paths)
        if (input == None):
            sys.exit(result)

        start_time = time()
        output.writeline(
            "------------ START: Ratt, ver={}. Start time={}".format(
                VERSION, strftime("%Y-%m-%d_%H.%M.%S", localtime(start_time))))
        output.writeline("------------ RUNNING SUITE CASE: {}".format(result))
        passcode = input.main()
        end_time = time()
        if (passcode != 0):
            output.writeline(
                "------------ TEST SUITE FAILED ({}).".format(passcode))
        else:
            output.writeline("------------ TEST SUITE PASSED.")
        output.writeline(
            "------------ END: End time={}, delta={:.2f} mins".format(
                strftime("%Y-%m-%d_%H.%M.%S", localtime(end_time)),
                (end_time - start_time) / 60.0))

        sys.exit(passcode)

    # interactive mode
    else:
        output.writeline("")
        output.writeline(
            "------------ Welcome to Ratt, this is my Kung-Fu and it is strong! ------------"
        )
        output.writeline("                   ver={}. Start time={}".format(
            VERSION, utils.append_current_time("", "")))
        output.writeline("")
        exec('from rattlib import *')
        exec('import config')

        while (True):
            output.write(">")
            line = sys.stdin.readline().rstrip("\r\n")
            output.writeline(line, log_only=True)
            try:
                exec(line)
            except Exception as e:
                output.writeline(str(e))
Beispiel #13
0
            self.velocity[i] + self.acceleration[i]
            for i in range(len(self.velocity))
        ]
        self.position = [
            self.position[i] + self.velocity[i]
            for i in range(len(self.position))
        ]

    def distance(self):
        return sum([abs(i) for i in self.position])

    def get_position(self):
        return self.position


inputs = utils.import_file("Inputs/input20.txt", split=True)
particles = []
for line in inputs:
    values = [s.split(',') for s in re.findall(r'<(.*?)>', line)]
    pos, vel, acc = [list(map(int, v)) for v in values]
    particles.append(Particle(pos, vel, acc))

for i in range(1000):
    [p.step() for p in particles]
    by_position = lambda x: x.get_position()
    particles = sorted(particles, key=by_position)
    for key, group in itertools.groupby(particles, key=by_position):
        if len(list(group)) > 1:
            particles = list(
                filter(lambda x: x.get_position() != key, particles))
Beispiel #14
0
    def initialize(self, config, num_classes):
        '''
            Initialize the graph from scratch according config.
        '''
        with self.graph.as_default():
            with self.sess.as_default():
                # Set up placeholders
                w, h = config.image_size
                channels = config.channels
                image_batch_placeholder = tf.placeholder(
                    tf.float32,
                    shape=[None, h, w, channels],
                    name='image_batch')
                label_batch_placeholder = tf.placeholder(tf.int32,
                                                         shape=[None],
                                                         name='label_batch')
                learning_rate_placeholder = tf.placeholder(
                    tf.float32, name='learning_rate')
                keep_prob_placeholder = tf.placeholder(tf.float32,
                                                       name='keep_prob')
                phase_train_placeholder = tf.placeholder(tf.bool,
                                                         name='phase_train')
                global_step = tf.Variable(0,
                                          trainable=False,
                                          dtype=tf.int32,
                                          name='global_step')

                image_splits = tf.split(image_batch_placeholder,
                                        config.num_gpus)
                label_splits = tf.split(label_batch_placeholder,
                                        config.num_gpus)
                grads_splits = []
                split_dict = {}

                def insert_dict(k, v):
                    if k in split_dict: split_dict[k].append(v)
                    else: split_dict[k] = [v]

                for i in range(config.num_gpus):
                    scope_name = '' if i == 0 else 'gpu_%d' % i
                    with tf.name_scope(scope_name):
                        with tf.variable_scope('', reuse=i > 0):
                            with tf.device('/gpu:%d' % i):
                                images = tf.identity(image_splits[i],
                                                     name='inputs')
                                labels = tf.identity(label_splits[i],
                                                     name='labels')
                                # Save the first channel for testing
                                if i == 0:
                                    self.inputs = images

                                # Build networks
                                if config.localization_net is not None:
                                    localization_net = utils.import_file(
                                        config.localization_net, 'network')
                                    imsize = (112, 112)
                                    images, theta = localization_net.inference(
                                        images,
                                        imsize,
                                        phase_train_placeholder,
                                        weight_decay=0.0)
                                    images = tf.identity(
                                        images, name='transformed_image')
                                    if i == 0:
                                        tf.summary.image(
                                            'transformed_image', images)
                                else:
                                    images = images

                                network = utils.import_file(
                                    config.network, 'network')
                                prelogits = network.inference(
                                    images,
                                    keep_prob_placeholder,
                                    phase_train_placeholder,
                                    bottleneck_layer_size=config.
                                    embedding_size,
                                    weight_decay=config.weight_decay,
                                    model_version=config.model_version)
                                prelogits = tf.identity(prelogits,
                                                        name='prelogits')
                                embeddings = tf.nn.l2_normalize(
                                    prelogits, dim=1, name='embeddings')
                                if i == 0:
                                    self.outputs = tf.identity(embeddings,
                                                               name='outputs')

                                # Build all losses
                                losses = []

                                # Orignal Softmax
                                if 'softmax' in config.losses.keys():
                                    logits = slim.fully_connected(
                                        prelogits,
                                        num_classes,
                                        weights_regularizer=slim.
                                        l2_regularizer(config.weight_decay),
                                        # weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
                                        weights_initializer=slim.
                                        xavier_initializer(),
                                        biases_initializer=tf.
                                        constant_initializer(0.0),
                                        activation_fn=None,
                                        scope='Logits')
                                    cross_entropy = tf.reduce_mean(
                                        tf.nn.
                                        sparse_softmax_cross_entropy_with_logits(
                                            labels=labels, logits=logits),
                                        name='cross_entropy')
                                    losses.append(cross_entropy)
                                    insert_dict('sloss', cross_entropy)
                                # L2-Softmax
                                if 'cosine' in config.losses.keys():
                                    logits, cosine_loss = tflib.cosine_softmax(
                                        prelogits,
                                        labels,
                                        num_classes,
                                        gamma=config.losses['cosine']['gamma'],
                                        weight_decay=config.weight_decay)
                                    losses.append(cosine_loss)
                                    insert_dict('closs', cosine_loss)
                                # A-Softmax
                                if 'angular' in config.losses.keys():
                                    a_cfg = config.losses['angular']
                                    angular_loss = tflib.angular_softmax(
                                        prelogits,
                                        labels,
                                        num_classes,
                                        global_step,
                                        a_cfg['m'],
                                        a_cfg['lamb_min'],
                                        a_cfg['lamb_max'],
                                        weight_decay=config.weight_decay)
                                    losses.append(angular_loss)
                                    insert_dict('aloss', angular_loss)
                                # Split Loss
                                if 'split' in config.losses.keys():
                                    split_losses = tflib.split_softmax(
                                        prelogits,
                                        labels,
                                        num_classes,
                                        global_step,
                                        gamma=config.losses['split']['gamma'],
                                        weight_decay=config.weight_decay)
                                    losses.extend(split_losses)
                                    insert_dict('loss', split_losses[0])

                            # Collect all losses
                                reg_loss = tf.reduce_sum(tf.get_collection(
                                    tf.GraphKeys.REGULARIZATION_LOSSES),
                                                         name='reg_loss')
                                losses.append(reg_loss)
                                insert_dict('reg_loss', reg_loss)

                                total_loss = tf.add_n(losses,
                                                      name='total_loss')
                                grads_split = tf.gradients(
                                    total_loss, tf.trainable_variables())
                                grads_splits.append(grads_split)

                # Merge the splits
                grads = tflib.average_grads(grads_splits)
                for k, v in split_dict.items():
                    v = tflib.average_tensors(v)
                    split_dict[k] = v
                    if 'loss' in k:
                        tf.summary.scalar('losses/' + k, v)
                    else:
                        tf.summary.scalar(k, v)

                # Training Operaters
                apply_gradient_op = tflib.apply_gradient(
                    tf.trainable_variables(), grads, config.optimizer,
                    learning_rate_placeholder,
                    config.learning_rate_multipliers)

                update_global_step_op = tf.assign_add(global_step, 1)

                update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

                train_ops = [apply_gradient_op, update_global_step_op
                             ] + update_ops
                train_op = tf.group(*train_ops)

                tf.summary.scalar('learning_rate', learning_rate_placeholder)
                summary_op = tf.summary.merge_all()

                # Initialize variables
                self.sess.run(tf.local_variables_initializer())
                self.sess.run(tf.global_variables_initializer())
                self.saver = tf.train.Saver(tf.trainable_variables(),
                                            max_to_keep=None)

                # Keep useful tensors
                self.image_batch_placeholder = image_batch_placeholder
                self.label_batch_placeholder = label_batch_placeholder
                self.learning_rate_placeholder = learning_rate_placeholder
                self.keep_prob_placeholder = keep_prob_placeholder
                self.phase_train_placeholder = phase_train_placeholder
                self.global_step = global_step
                self.watch_list = split_dict
                self.train_op = train_op
                self.summary_op = summary_op
#!/usr/bin/env python3

import strcon
import xor
import utils

hex_strings = utils.import_file("Inputs/4.txt", split=True)
byte_strings = [strcon.hexToBytes(s) for s in hex_strings]

for b in byte_strings:
    (result, score, key) = xor.singleByteDecrypt(b)
    if score > 200:
        print('Score: {score}\nPlaintext: {s}\nKey: {key}'.format(score=score,
                                                                  s=result,
                                                                  key=key))
Beispiel #16
0
import utils

utils.import_file('c:\\level.hom')
    Args:
        memory: A list of ints representing memory addresses and their contents.

    Returns:
        A tuple of how many cycles were performed and the cycle length.
    """
    memory = memory[:]
    states = {}
    num_cycles = 0
    while tuple(memory) not in states:
        #keep track of when each state was seen
        states[tuple(memory)] = num_cycles
        cycle(memory)
        num_cycles += 1
    cycle_length = num_cycles - states[tuple(memory)]
    return (num_cycles, cycle_length)


M = [0, 2, 7, 0]
#Part 1
assert reallocate(M)[0] == 5
cycle(M)
assert M == [2, 4, 1, 2]
#Part 2
assert reallocate(M)[1] == 4

memory = [int(i) for i in utils.import_file("Inputs/input6.txt").split()]
num_cycles, cycle_length = reallocate(memory)
print("Part 1: {}".format(num_cycles))
print("Part 2: {}".format(cycle_length))
Beispiel #18
0
import os
import sys
import importlib

from utils import path_to_module, remove_suffix, import_file

dict_ = {'width': width, 'height': height, 'arena_dicts': []}

arena_files = {'./dict_arena_rec_max.py'}

Arena_Params = []

for arena_file in arena_files:
    Arena_Param = import_file(arena_file,
                              start_path=__file__,
                              main_path=sys.path[0])
    Arena_Params.append(Arena_Param)
    arena_dict = Arena_Param.dict_
    '''
    for key, value in arena_dict.items():
        print('%s:%s'%(key, value))
    #arena_type = arena_dict['type']
    '''
    dict_['arena_dicts'].append(arena_dict)
    '''
    if arena_type in ['rec', 'rec_max', 'square']:
        dict_arenas['arena_dicts'].append({  # arena 0
            'type':'rec_max',
            'width': arena_width,
            'height': arena_height, 
Beispiel #19
0
import stream
import utils
import strcon
import block
import xor

key_size = 16
nonce_size = key_size // 2
key = block.generateRandomBytes(key_size)
nonce = bytes([0] * nonce_size)

base64texts = utils.import_file("Inputs/19.txt", split=True)
plaintexts = map(strcon.base64ToBytes, base64texts)
ciphertexts = [stream.CTR_mode(p, key, nonce) for p in plaintexts]

min_len = len(min(ciphertexts, key=len))
transposed_result = []
for i in range(min_len):
    letters = bytes([c[i] for c in ciphertexts])
    transposed_result.append(xor.singleByteDecrypt(letters)[0].decode())
result = [''.join(s) for s in zip(*transposed_result)]
for r in result:
    print(r)
                           evenly divides the other
    """
    for combo in itertools.combinations(l, 2):
        low, high = sorted(combo)
        q, r = divmod(high, low)
        if r == 0: return q
    raise InvalidInputError("The list did not contain two numbers where one"
                              " evenly divides the other")    

def divsum(matrix):
    return sum([even_divisor(row) for row in matrix])

#part 1
assert checksum([[5, 1, 9, 5],
                 [7, 5, 3],
                 [2, 4, 6, 8]]) == 18

#part 2
assert divsum([[5, 9, 2, 8],
               [9, 4, 7, 3],
               [3, 8, 6, 5]]) == 9

input_file = utils.import_file("Inputs/input2.txt")

matrix = []
for row in input_file.split('\n'):
    matrix.append([int(entry) for entry in row.split('\t')])

print("Part 1: {}".format(checksum(matrix)))
print("Part 2: {}".format(divsum(matrix)))
Beispiel #21
0
import numpy as np
import argparse
import utils
import tflib
from network import Network
from tensorflow.contrib.tensorboard.plugins import projector
import evaluate

# Config File
parser = argparse.ArgumentParser()
parser.add_argument('--config_file',
                    help='Path to training configuration file',
                    type=str)
config_file = parser.parse_args().config_file
# I/O
config = utils.import_file(config_file, 'config')

trainset = utils.Dataset(config.splits_path + '/train_' +
                         str(config.fold_number) + '.txt')
trainset.images = utils.preprocess(trainset.images, config, True)

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)

# Load gallery and probe file_list
Beispiel #22
0
def import_file(file_name):
    """Take the json in the given file and import all global constants."""
    global sixteenth_per_beat
    global beats_per_bar
    global num_bars
    global sixteenth_duration
    global swing
    global beat_pattern
    global is_active
    global swing_amount

    if not utils.is_valid_file('saved_beats/' + file_name + '.drum'):
        message.set_message("Error")
        return False

    reset_global_timer()

    sixteenth_per_beat, beats_per_bar, num_bars, sixteenth_duration, swing, beat_pattern, is_active, swing_amount = utils.import_file(
        'saved_beats/' + file_name + '.drum')

    set_graphics()
    message.set_message("Imported")
    resize_window()
    return True
import utils
import strcon
import itertools
import block

block_length = 16

texts = [
    strcon.hexToBytes(t) for t in utils.import_file("Inputs/8.txt", split=True)
]
for text in texts:
    blocks = block.blockSplit(text, block_length)
    for combo in itertools.combinations(blocks, 2):
        if combo[0] == combo[1]:
            print('Detected duplicate! Text was {0}, block was {1}'.format(
                strcon.bytesToHex(text), strcon.bytesToHex(combo[0])))
            break
Beispiel #24
0
arena_files = ['./dict_arena/dict_arena_rec_max.py']

import os
import sys
import importlib

from utils import path_to_module, remove_suffix, import_file

Arena_Params = []

dict_ = {'width': width, 'height': height, 'arena_dicts': []}

for arena_file in arena_files:
    Arena_Param = import_file(path_rel=arena_file,
                              path_start=__file__,
                              path_main=sys.path[0])
    Arena_Params.append(Arena_Param)
    arena_dict = Arena_Param.dict_
    '''
    for key, value in arena_dict.items():
        print('%s:%s'%(key, value))
    #arena_type = arena_dict['type']
    '''
    dict_['arena_dicts'].append(arena_dict)
    '''
    if arena_type in ['rec', 'rec_max', 'square']:
        dict_arenas['arena_dicts'].append({  # arena 0
            'type':'rec_max',
            'width': arena_width,
            'height': arena_height, 
Beispiel #25
0
def validate_tflite(args):

    # open config files
    config = utils.import_file(args.config_file, 'config')
    testset = utils.Dataset(args.test_dataset_path)

    # Set up LFW test protocol and load images
    print('Loading images...')
    lfwtest = LFWTest(testset.images)
    lfwtest.init_standard_proto(args.lfw_pairs_file)
    lfwtest.images = utils.preprocess(lfwtest.image_paths,
                                      config,
                                      is_training=False)

    # Load TFLite model and allocate tensors
    print('Loading network tflite model...')
    interpreter = tf.contrib.lite.Interpreter(args.model_dir)
    interpreter.allocate_tensors()

    # Get Input and output tensors
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    # ---------------------------------
    # Testing on LFW
    num_images = lfwtest.images.shape[0] if type(
        lfwtest.images) == np.ndarray else len(lfwtest.images)
    num_features = output_details[0]['shape'][1]

    result = np.ndarray((num_images, num_features), dtype=np.float32)
    total_time = 0.0

    print("Extracting Embeddings from LFW...")
    #for start_idx in range(0, num_images, config.batch_size):
    for start_idx in range(num_images):

        # get batch images
        #end_idx = min(num_images, start_idx + config.batch_size)
        #inputs = lfwtest.images[start_idx:end_idx]
        inputs = np.expand_dims(lfwtest.images[start_idx], axis=0)
        #print("input should be: ", str(input_details[0]['shape']))
        #print("input: ", str(inputs.shape))
        # run net
        start_time = time.time()
        interpreter.set_tensor(input_details[0]['index'], inputs)
        interpreter.invoke()

        # get batches result
        #result[start_idx:end_idx] = interpreter.get_tensor(output_details[0]['index'])
        result[start_idx] = interpreter.get_tensor(output_details[0]['index'])
        elapsed_time = time.time() - start_time
        total_time += elapsed_time

        if (start_idx % 100 == 0):
            print("Extract " + str(start_idx) + "/" + str(num_images))
        if (start_idx % 1000 == 0):
            print("Tempo de inferência total: " + str(total_time / 1000))
            total_time = 0.0

    # calculating accuracy and threshold
    print("Calculating accuracy...")
    accuracy_embeddings, threshold_embeddings = lfwtest.test_standard_proto(
        result)
    print('Embeddings Accuracy: %2.4f Threshold %2.3f' %
          (accuracy_embeddings, threshold_embeddings))
Beispiel #26
0
thickness = 2
color = (0, 0, 255)
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
time_out = 2

max_fps = 15
count_fps = 0
last_fps_time = time.time()
no_viewer = False

cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX

lines = import_file(constants['samples_map_file'])
mapper = {'0': 'No one'}

for line in lines:
    identifier, name = line[1].split(',')
    mapper[identifier] = name.replace('\n', '')

last_rec = 0
last_rec_time = time.time()

while True:
    time.sleep(1 / max_fps)
    count_fps += 1

    if time.time() - last_fps_time >= 1:
        last_fps_time = time.time()
Beispiel #27
0
import utils

utils.import_file('c:\\level.cform')
import utils
import strcon
import block

key = b'YELLOW SUBMARINE'

text = ''.join(utils.import_file("Inputs/7.txt", split=True)) 
text = strcon.base64ToBytes(text)
print(block.decryptAES_ECB(text, key).decode())
Beispiel #29
0
def main(args):
    # I/O
    config_file = args.config_file
    config = utils.import_file(config_file, 'config')

    trainset = utils.Dataset(config.train_dataset_path)
    testset = utils.Dataset(config.test_dataset_path)

    network = BaseNetwork()
    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 is not None:
        network.restore_model(config.restore_model, config.restore_scopes)

    # Set up LFW test protocol and load images
    print('Loading images...')
    lfwtest = LFWTest(testset.images)
    lfwtest.init_standard_proto(config.lfw_pairs_file)
    lfwtest.images = utils.preprocess(lfwtest.image_paths,
                                      config,
                                      is_training=False)

    trainset.start_batch_queue(config, True)

    #
    # Main Loop
    #
    print('\nStart Training\nname: %s\n# epochs: %d\nepoch_size: %d\nbatch_size: %d\n'\
        % (config.name, config.num_epochs, config.epoch_size, config.batch_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['images'],
                                                batch['labels'], learning_rate,
                                                config.keep_prob)

            # 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)

        # Testing on LFW
        print('Testing on standard LFW protocol...')
        embeddings = network.extract_feature(lfwtest.images, config.batch_size)
        accuracy_embeddings, threshold_embeddings = lfwtest.test_standard_proto(
            embeddings)
        print('Embeddings Accuracy: %2.4f Threshold %2.3f' %
              (accuracy_embeddings, threshold_embeddings))

        with open(os.path.join(log_dir, 'lfw_result.txt'), 'at') as f:
            f.write('%d\t%.5f\n' % (global_step, accuracy_embeddings))
        summary = tf.Summary()
        summary.value.add(tag='lfw/accuracy', simple_value=accuracy_embeddings)
        summary_writer.add_summary(summary, global_step)

        # Save the model
        network.save_model(log_dir, global_step)
    after the last digit is the first digit in the list.

    Args:
        s: A string of the captcha digits of length at least 2.
        shift: An integer number of digits ahead to look for a match. 

    Returns:
        An integer solution to the captcha.
    """
    shifted = s[shift:] + s[0:shift]
    return sum([int(s[i]) for i in range(len(s)) if s[i] == shifted[i]])


#part 1
assert (solve_captcha("1122") == 3)
assert (solve_captcha("1111") == 4)
assert (solve_captcha("1234") == 0)
assert (solve_captcha("91212129") == 9)

#part 2
assert (solve_captcha("1212", shift=2) == 6)
assert (solve_captcha("1221", shift=2) == 0)
assert (solve_captcha("123425", shift=3) == 4)
assert (solve_captcha("123123", shift=3) == 12)
assert (solve_captcha("12131415", shift=4) == 4)

s = utils.import_file("Inputs/input-1-1.txt")

print("Part 1: {}".format(solve_captcha(s)))
print("Part 2: {}".format(solve_captcha(s, shift=len(s) // 2)))