Ejemplo n.º 1
0
        # Plot!
        plt.show()
        exit()


if __name__ == '__main__':
    """ Tests CNN_LSTM on SortOfCLEVR"""

    # "Loaded parameters".
    from miprometheus.utils.param_interface import ParamInterface
    from miprometheus.utils.app_state import AppState
    app_state = AppState()
    app_state.visualize = True
    from miprometheus.problems.image_text_to_class.sort_of_clevr import SortOfCLEVR
    problem_params = ParamInterface()
    problem_params.add_config_params({
        'data_folder': '~/data/sort-of-clevr/',
        'split': 'train',
        'regenerate': False,
        'dataset_size': 10000,
        'img_size': 128
    })

    # create problem
    sortofclevr = SortOfCLEVR(problem_params)

    batch_size = 64

    # wrap DataLoader on top of this Dataset subclass
    from torch.utils.data import DataLoader
Ejemplo n.º 2
0
        self.plotWindow.update(fig, frames)
        return self.plotWindow.is_closed


if __name__ == "__main__":
    # Set logging level.
    logger = logging.getLogger('NTM-Module')
    logging.basicConfig(level=logging.DEBUG)

    # Set visualization.
    from miprometheus.utils.app_state import AppState
    AppState().visualize = True

    # "Loaded parameters".
    from miprometheus.utils.param_interface import ParamInterface
    params = ParamInterface()
    params.add_default_params({
        # controller parameters
        'controller': {
            'name': 'GRUController',
            'hidden_state_size': 5,
            'num_layers': 1,
            'non_linearity': 'none',
            'output_size': 5
        },
        # interface parameters
        'interface': {
            'num_read_heads': 2,
            'shift_size': 3
        },
        # memory parameters
Ejemplo n.º 3
0
        self.use_mask = params["use_mask"]

    def evaluate_loss(self, data_dict, logits):
        """ Calculates accuracy equal to mean number of correct predictions in a given batch.
        WARNING: Applies mask to both logits and targets!

        :param data_dict: DataDict({'sequences', 'sequences_length', 'targets', 'mask'}).

        :param logits: Predictions being output of the model.

        """
        # Check if mask should be is used - if so, use the correct loss
        # function.
        if self.use_mask:
            loss = self.loss_function(
                logits, data_dict['targets'], data_dict['masks'])
        else:
            loss = self.loss_function(logits, data_dict['targets'])

        return loss


if __name__ == '__main__':

    from miprometheus.utils.param_interface import ParamInterface

    sample = SeqToSeqProblem(ParamInterface())[0]
    # equivalent to ImageTextToClassProblem(params={}).__getitem__(index=0)

    print(repr(sample))
Ejemplo n.º 4
0
        target = targets[sequence_number]
        label = labels[sequence_number]

        # Reshape image.
        if image.shape[0] == 1:
            # This is a single channel image - get rid of this dimension
            image = np.squeeze(image, axis=0)
        else:
            # More channels - move channels to axis2, according to matplotilb documentation.
            # (X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4))
            image = image.transpose(1, 2, 0)

        # show data.
        plt.xlabel('num_columns')
        plt.ylabel('num_rows')
        plt.title('Target: {} ({}), {}th in Sequence, Question: {}'.format(
            label, target, sequence_number, question))
        plt.imshow(image, interpolation='nearest', aspect='auto')

        # Plot!
        plt.show()


if __name__ == '__main__':

    from miprometheus.utils.param_interface import ParamInterface

    sample = VQAProblem(ParamInterface())[0]

    print(repr(sample))
Ejemplo n.º 5
0
    def __init__(self, name="GridWorker", use_gpu=False):
        """
        Base constructor for all grid workers:

            - Initializes the AppState singleton:

                >>> self.app_state = AppState()

            - Initializes the Parameter Registry:

                >>> self.params = ParamInterface()

            - Defines the logger:

                >>> self.logger = logging.getLogger(name=self.name)

            - Creates parser and adds default worker command line arguments (you can display them with ``--h``).

        :param name: Name of the worker (DEFAULT: "GridWorker").
        :type name: str

        :param use_gpu: Indicates whether the worker should use GPU or not. Value coming from the subclasses \
         (e.g. ``GridTrainerCPU`` vs ``GridTrainerGPU``) (DEFAULT: False).
        :type use_gpu: bool

        """
        # Call base constructor.
        super(GridWorker, self).__init__()

        # Set worker name.
        self.name = name

        # Initialize the application state singleton.
        self.app_state = AppState()
        self.app_state.use_CUDA = use_gpu

        # Initialize parameter interface/registry.
        self.params = ParamInterface()

        # Load the default logger configuration.
        logger_config = {
            'version': 1,
            'disable_existing_loggers': False,
            'formatters': {
                'simple': {
                    'format':
                    '[%(asctime)s] - %(levelname)s - %(name)s >>> %(message)s',
                    'datefmt': '%Y-%m-%d %H:%M:%S'
                }
            },
            'handlers': {
                'console': {
                    'class': 'logging.StreamHandler',
                    'level': 'INFO',
                    'formatter': 'simple',
                    'stream': 'ext://sys.stdout'
                }
            },
            'root': {
                'level': 'DEBUG',
                'handlers': ['console']
            }
        }

        logging.config.dictConfig(logger_config)

        # Create the Logger, set its label and logging level.
        self.logger = logging.getLogger(name=self.name)

        # Create parser with a list of runtime arguments.
        self.parser = argparse.ArgumentParser(
            formatter_class=argparse.RawTextHelpFormatter)

        # Add arguments to the specific parser.
        # These arguments will be shared by all grid workers.
        self.parser.add_argument(
            '--outdir',
            dest='outdir',
            type=str,
            default="./experiments",
            help=
            'Path to the global output directory where the experiments folders '
            'will be / are stored. Affects all grid experiments.'
            ' (DEFAULT: ./experiments)')

        self.parser.add_argument(
            '--savetag',
            dest='savetag',
            type=str,
            default='',
            help='Additional tag for the global output directory.')

        self.parser.add_argument(
            '--ll',
            action='store',
            dest='log_level',
            type=str,
            default='INFO',
            choices=[
                'CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'
            ],
            help="Log level for the experiments. (Default: INFO)")

        self.parser.add_argument(
            '--li',
            dest='logging_interval',
            default=100,
            type=int,
            help=
            'Statistics logging interval. Will impact logging to the logger and exporting to '
            'TensorBoard for the experiments. Do not affect the grid worker. '
            'Writing to the csv file is not impacted (interval of 1).'
            ' (Default: 100, i.e. logs every 100 episodes).')

        self.parser.add_argument(
            '--agree',
            dest='confirm',
            action='store_true',
            help='Request user confirmation before starting the grid experiment.'
            '  (Default: False)')
Ejemplo n.º 6
0
        # Plot figure and list of frames.
        self.plotWindow.update(fig, frames)


if __name__ == '__main__':
    """Unit test of the SequentialModel"""

    from miprometheus.utils.param_interface import ParamInterface
    from miprometheus.utils.app_state import AppState

    # Set visualization.
    AppState().visualize = True

    # Test sequential model.
    sequential_model = SequentialModel(ParamInterface())

    # Set logging level.
    import logging
    logging.basicConfig(level=logging.DEBUG)

    while True:
        # Generate new sequence.
        x = np.random.binomial(1, 0.5, (1, 8, 15))
        y = np.random.binomial(1, 0.5, (1, 8, 15))
        z = np.random.binomial(1, 0.5, (1, 8, 15))

        # Transform to PyTorch.
        x = torch.from_numpy(x).type(torch.FloatTensor)
        y = torch.from_numpy(y).type(torch.FloatTensor)
        z = torch.from_numpy(z).type(torch.FloatTensor)
Ejemplo n.º 7
0
    def __init__(self, name, add_default_parser_args=True):
        """
        Base constructor for all workers:

            - Initializes the AppState singleton:

                >>> self.app_state = AppState()

            - Initializes the Parameter Registry:

                >>> self.params = ParamInterface()

            - Defines the logger:

                >>> self.logger = logging.getLogger(name=self.name)

            - Creates parser and adds default worker command line arguments.

        :param name: Name of the worker.
        :type name: str

        :param add_default_parser_args: If set, adds default parser arguments (DEFAULT: True).
        :type add_default_parser_args: bool

        """
        # Call base constructor.
        super(Worker, self).__init__()

        # Set worker name.
        self.name = name

        # Initialize the application state singleton.
        self.app_state = AppState()

        # Initialize parameter interface/registry.
        self.params = ParamInterface()

        # Initialize logger using the configuration.
        self.initialize_logger()

        # Create parser with a list of runtime arguments.
        self.parser = argparse.ArgumentParser(
            formatter_class=argparse.RawTextHelpFormatter)

        # Add arguments to the specific parser.
        if add_default_parser_args:
            # These arguments will be shared by all basic workers.
            self.parser.add_argument(
                '--config',
                dest='config',
                type=str,
                default='',
                help='Name of the configuration file(s) to be loaded. '
                'If specifying more than one file, they must be separated with coma ",".'
            )

            self.parser.add_argument(
                '--model',
                type=str,
                default='',
                dest='model',
                help='Path to the file containing the saved parameters'
                ' of the model to load (model checkpoint, should end with a .pt extension.)'
            )

            self.parser.add_argument(
                '--gpu',
                dest='use_gpu',
                action='store_true',
                help=
                'The current worker will move the computations on GPU devices, if available '
                'in the system. (Default: False)')

            self.parser.add_argument(
                '--expdir',
                dest='expdir',
                type=str,
                default="./experiments",
                help=
                'Path to the directory where the experiment(s) folders are/will be stored.'
                ' (DEFAULT: ./experiments)')

            self.parser.add_argument('--savetag',
                                     dest='savetag',
                                     type=str,
                                     default='',
                                     help='Tag for the save directory.')

            self.parser.add_argument('--ll',
                                     action='store',
                                     dest='log_level',
                                     type=str,
                                     default='INFO',
                                     choices=[
                                         'CRITICAL', 'ERROR', 'WARNING',
                                         'INFO', 'DEBUG', 'NOTSET'
                                     ],
                                     help="Log level. (Default: INFO)")

            self.parser.add_argument(
                '--li',
                dest='logging_interval',
                default=100,
                type=int,
                help=
                'Statistics logging interval. Will impact logging to the logger and '
                'exporting to TensorBoard. Writing to the csv file is not impacted '
                '(interval of 1).(Default: 100, i.e. logs every 100 episodes).'
            )

            self.parser.add_argument(
                '--agree',
                dest='confirm',
                action='store_true',
                help=
                'Request user confirmation just after loading the settings, '
                'before starting training. (Default: False)')
        if image.shape[0] == 1:
            # This is a single channel image - get rid of this dimension
            image = np.squeeze(image, axis=0)
        else:
            # More channels - move channels to axis2, according to matplotilb documentation.
            # (X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4))
            image = image.transpose(1, 2, 0)

        # show data.
        plt.xlabel('num_columns')
        plt.ylabel('num_rows')
        plt.title('Target class: {} ({}), {}th in Sequence'.format(
            label, target, sequence_number))
        plt.imshow(image,
                   interpolation='nearest',
                   aspect='auto',
                   cmap='gray_r')

        # Plot!
        plt.show()


if __name__ == '__main__':

    from miprometheus.utils.param_interface import ParamInterface

    sample = VideoToClassProblem(ParamInterface())[0]
    # equivalent to ImageToClassProblem(params={}).__getitem__(index=0)

    print(repr(sample))
Ejemplo n.º 9
0
                "The specified class '{}' is not derived from the nn.Module class"
                .format(name))
            exit(-1)

        # Ok, proceed.
        logger.info('Loading the {} controller from {}'.format(
            name, controller_class.__module__))

        # return the instantiated controller class
        return controller_class(params)


if __name__ == "__main__":
    """
    Tests ControllerFactory.
    """
    from miprometheus.utils.param_interface import ParamInterface

    controller_params = ParamInterface()
    controller_params.add_default_params({
        'name': 'RNNController',
        'input_size': 11,
        'output_size': 11,
        'hidden_state_size': 20,
        'num_layers': 1,
        'non_linearity': 'sigmoid'
    })

    controller = ControllerFactory.build_controller(controller_params)
    print(type(controller))
Ejemplo n.º 10
0
            for key, value in zip(
                self.data_definitions.keys(),
                super(CIFAR10, self).collate_fn(batch).values())
        })


if __name__ == "__main__":
    """ Tests sequence generator - generates and displays a random sample"""

    # set the seeds
    np.random.seed(0)
    torch.manual_seed(0)

    # Load parameters.
    from miprometheus.utils.param_interface import ParamInterface
    params = ParamInterface()  # using the default values

    batch_size = 64

    # Create problem.
    cifar10 = CIFAR10(params)

    # get a sample
    sample = cifar10[0]
    print('__getitem__ works.\n')

    # wrap DataLoader on top of this Dataset subclass
    from torch.utils.data import DataLoader

    dataloader = DataLoader(dataset=cifar10,
                            collate_fn=cifar10.collate_fn,
Ejemplo n.º 11
0
        # Get sample.
        image = images[sample_number].cpu().detach().numpy()
        target = targets[sample_number].cpu().detach().numpy()
        label = labels[sample_number]

        # Reshape image.
        if image.shape[0] == 1:
            # This is a single channel image - get rid of this dimension
            image = np.squeeze(image, axis=0)
        else:
            # More channels - move channels to axis2, according to matplotilb documentation.
            # (X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4))
            image = image.transpose(1, 2, 0)

        # Show data.
        plt.title('Target class: {} ({})'.format(label, target))
        plt.imshow(image, interpolation='nearest', aspect='auto')

        # Plot!
        plt.show()


if __name__ == '__main__':

    from miprometheus.utils.param_interface import ParamInterface

    sample = ImageToClassProblem(ParamInterface(), 'ImageToClassProblem')[0]
    # equivalent to ImageToClassProblem(params={}).__getitem__(index=0)

    print(repr(sample))
Ejemplo n.º 12
0
            exit(-1)

        # Ok, proceed.
        logger.info('Loading the {} model from {}'.format(
            name, model_class.__module__))
        # return the instantiated model class
        return model_class(params, problem_default_values_)


if __name__ == "__main__":
    """
    Tests ModelFactory.
    """
    from miprometheus.utils.param_interface import ParamInterface

    model_params = ParamInterface()
    model_params.add_default_params({
        'name': 'MAES',
        'num_control_bits': 3,
        'num_data_bits': 8,
        'encoding_bit': 0,
        'solving_bit': 1,
        'controller': {
            'name': 'rnn',
            'hidden_state_size': 20,
            'num_layers': 1,
            'non_linearity': 'sigmoid'
        },
        'mae_interface': {
            'shift_size': 3
        },
        """
        Add accuracy statistic to ``StatisticsCollector``.

        :param stat_col: ``StatisticsCollector``.

        """
        stat_col.add_statistic('acc', '{:12.10f}')

    def collect_statistics(self, stat_col, data_dict, logits):
        """
        Collects accuracy.

        :param stat_col: ``StatisticsCollector``.

        :param data_dict: DataDict containing the targets and the mask.
        :type data_dict: DataDict

        :param logits: Predictions of the model.

        """
        stat_col['acc'] = self.calculate_accuracy(data_dict, logits)


if __name__ == '__main__':

    from miprometheus.utils.param_interface import ParamInterface

    sample = ImageTextToClassProblem(ParamInterface())[0]
    # equivalent to ImageTextToClassProblem(params={}).__getitem__(index=0)

    print(repr(sample))
Ejemplo n.º 14
0
    def __init__(self, name="Worker"):
        """
        Base constructor for all workers:

            - Initializes the AppState singleton:

                >>> self.app_state = AppState()

            - Initializes the Parameter Registry:

                >>> self.params = ParamInterface()

            - Defines the logger:

                >>> self.logger = logging.getLogger(name=self.name)

            - Creates parser and adds default worker command line arguments.

        :param name: Name of the worker (DEFAULT: "Worker").

        """
        # Call base constructor.
        super(Worker, self).__init__()

        # Set worker name.
        self.name = name

        # Initialize the application state singleton.
        self.app_state = AppState()

        # Initialize parameter interface/registry.
        self.params = ParamInterface()

        # Load the default logger configuration.
        logger_config = {'version': 1,
                         'disable_existing_loggers': False,
                         'formatters': {
                             'simple': {
                                 'format': '[%(asctime)s] - %(levelname)s - %(name)s >>> %(message)s',
                                 'datefmt': '%Y-%m-%d %H:%M:%S'}},
                         'handlers': {
                             'console': {
                                 'class': 'logging.StreamHandler',
                                 'level': 'INFO',
                                 'formatter': 'simple',
                                 'stream': 'ext://sys.stdout'}},
                         'root': {'level': 'DEBUG',
                                  'handlers': ['console']}}

        logging.config.dictConfig(logger_config)

        # Create the Logger, set its label and logging level.
        self.logger = logging.getLogger(name=self.name)

        # Create parser with a list of runtime arguments.
        self.parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)

        # Add arguments to the specific parser.
        # These arguments will be shared by all basic workers.
        self.parser.add_argument('--config',
                                 dest='config',
                                 type=str,
                                 default='',
                                 help='Name of the configuration file(s) to be loaded. '
                                      'If specifying more than one file, they must be separated with coma ",".')

        self.parser.add_argument('--model',
                                 type=str,
                                 default='',
                                 dest='model',
                                 help='Path to the file containing the saved parameters'
                                      ' of the model to load (model checkpoint, should end with a .pt extension.)')

        self.parser.add_argument('--gpu',
                                 dest='use_gpu',
                                 action='store_true',
                                 help='The current worker will move the computations on GPU devices, if available in '
                                      'the system. (Default: False)')

        self.parser.add_argument('--outdir',
                                 dest='outdir',
                                 type=str,
                                 default="./experiments",
                                 help='Path to the output directory where the experiment(s) folders will be stored.'
                                      ' (DEFAULT: ./experiments)')

        self.parser.add_argument('--savetag',
                                 dest='savetag',
                                 type=str,
                                 default='',
                                 help='Tag for the save directory')

        self.parser.add_argument('--ll',
                                 action='store',
                                 dest='log_level',
                                 type=str,
                                 default='INFO',
                                 choices=['CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', 'NOTSET'],
                                 help="Log level. (Default: INFO)")

        self.parser.add_argument('--li',
                                 dest='logging_interval',
                                 default=100,
                                 type=int,
                                 help='Statistics logging interval. Will impact logging to the logger and exporting to '
                                      'TensorBoard. Writing to the csv file is not impacted (interval of 1).'
                                      ' (Default: 100, i.e. logs every 100 episodes).')

        self.parser.add_argument('--agree',
                                 dest='confirm',
                                 action='store_true',
                                 help='Request user confirmation just after loading the settings, '
                                      'before starting training. (Default: False)')