Ejemplo n.º 1
0
    def _read_next_input(self):
        logger.debug("Reading input")

        self.features_read = self.parser.next_batch()
        logger.debug("Read labels: \n{}".format(self.features_read['label']))

        # Prepare separate graph for reading input
        graph = tf.Graph()
        with graph.as_default():
            *_, input_model, input_feed_dict = _construct_X_and_feed_dict(
                self.features_read)

            # Required variable in order to use tf.make_template()
            global_step = tf.Variable(0, name='global_step', trainable=False)

            logger.debug('Making sirs model for the input')

            self.input_model = sirs_classifier.create_model(
                None,
                global_step,
                input_model,
                False,
                INPUT_SIZE,
                CONTEXT_SIZE,
                NUM_CLASSES,
                USE_TEXT,
                use_char_autoencoder=USE_AUTOENCODER,
                use_doc2vec=USE_DOC2VEC)

            y_hat = self.input_model['y_hat']
            y_maxes = tf.reduce_max(y_hat, axis=2)
            y_argmax = tf.cast(tf.argmax(y_maxes, axis=1), tf.int32)

            self.input_model['offsets'] = tf.pad(
                tf.expand_dims(y_argmax, axis=1), [[0, 0], [3, 1]])

            with tf.Session() as s:
                s.run([tf.local_variables_initializer()])

                # Create a tf Saver that can be used to restore a pre-trained model below
                saver = tf.train.Saver()
                self.restore_checkpoint(s, saver)

                # Read the next input
                self.input_selection, y_hat = s.run(
                    [self.input_model['offsets'], self.input_model['y_hat']],
                    feed_dict=input_feed_dict)

        # Reduce max sequence length
        self.max_sequence = y_hat.shape[1]

        # Tell the parser that we read a batch
        self.parser.did_read_batch()

        logger.info("Read sample {} with label \n{}".format(
            self.parser.samples_read(), self.features_read['label']))
Ejemplo n.º 2
0
    def create_model(self, features):
        # First update the features stored in this class with the new features
        feature_batch = self.features_batch
        feature_batch['features'] = features

        # Run sirs_classifier to get the model for forward passes
        # Ignoring two first parameters (session and global_step) since session is never used and
        # global_step is only for training
        model = sirs_classifier.create_model(
            None,
            None,
            feature_batch,
            False,
            INPUT_SIZE,
            CONTEXT_SIZE,
            NUM_CLASSES,
            USE_TEXT,
            use_char_autoencoder=USE_AUTOENCODER,
            use_doc2vec=USE_DOC2VEC)

        y_hat_reshaped = tf.reshape(
            model['y_hat'],
            (1, 1, self.batch_size, self.max_sequence, NUM_CLASSES))

        # Each sample has shape (1, 1, max_sequence, num_classes)
        samples = tf.split(y_hat_reshaped, self.batch_size, 2)

        # each sample has shape (1, 1, 1, num_classes)
        to_concatenate = [
            tf.slice(sample, max_slice, [1, 1, 1, 1, NUM_CLASSES])
            for (sample, max_slice) in zip(samples, self.input_selection)
        ]

        # Shape (1, 1, 1, batch_size * num_classes)
        highest_predictions_concatenated = tf.concat(to_concatenate, axis=4)

        # Remove dimension 0, 1, 2
        model['y_hat'] = tf.reshape(highest_predictions_concatenated,
                                    (self.batch_size, NUM_CLASSES))

        return model
Ejemplo n.º 3
0
    def __init__(self, input_features, model, destination, batch_size,
                 **kwargs):
        # Remember the batch_size
        self.batch_size = batch_size
        self.confs = kwargs

        # Remember the file containing the model
        self.model_file = model

        # Dict used to holde the input elements (as tensors)
        self.features_batch = {
            'features': None,
            'context': None,
            'seq_len': None,
            'label:': None,
            'forloeb': None,
        }

        # Result writer used to append benchmark results to files according to destination directory
        self.writer = ResultWriter(destination)

        # Gets all the different configurations
        self.configurations = get_configurations()

        # Count the configurations and add sensitivity analysis and random
        self.num_configurations = len(self.configurations) + 2

        # Prepare separate graph for reading input
        self.input_graph = tf.Graph()
        with self.input_graph.as_default():
            self.parser = FeatureParser(input_features, INPUT_SIZE,
                                        CONTEXT_SIZE, batch_size)
            self.next_batch = self.parser.next_batch()

            # Required variable in order to use tf.make_template()
            global_step = tf.Variable(0, name='global_step', trainable=False)

            logger.debug('Making sirs model for the input')

            self.input_model = sirs_classifier.create_model(
                None,
                global_step,
                self.next_batch,
                False,
                INPUT_SIZE,
                CONTEXT_SIZE,
                NUM_CLASSES,
                USE_TEXT,
                use_char_autoencoder=USE_AUTOENCODER,
                use_doc2vec=USE_DOC2VEC)

            y_hat = self.input_model['y_hat']
            y_maxes = tf.reduce_max(y_hat, axis=2)
            y_argmax = tf.cast(tf.argmax(y_maxes, axis=1), tf.int32)

            self.input_model['offsets'] = tf.pad(
                tf.expand_dims(y_argmax, axis=1), [[0, 0], [3, 1]])

            self.input_session = tf.Session(graph=self.input_graph)
            self.input_session.run([tf.local_variables_initializer()])

            # Create a tf Saver that can be used to restore a pre-trained model below
            saver = tf.train.Saver()
            self.restore_checkpoint(self.input_session, saver)

        logger.info("Testing {} samples from {}".format(
            self.parser.get_record_count(), input_features))
        logger.info("Writing results to folder: {}".format(destination))
        logger.info("Model used: {}".format(model))
        logger.info("Batch size: {}".format(batch_size))
Ejemplo n.º 4
0
def _create_model(features):
    return sirs_classifier.create_model(None, None, features, False,
                                        INPUT_SIZE, CONTEXT_SIZE, NUM_CLASSES,
                                        USE_TEXT)