<commit_before>from __future__ import absolute_import

import re
import os
from collections import OrderedDict
from functools import partial

import torch
from torch.autograd import Variable
from torch.utils.serialization import load_lua
from .backend.torchlegacy import load_legacy_model, LambdaBase
import numpy as np

from emu.nnadapter import NNAdapter
from emu.docutil import doc_inherit


imagenet_mean = np.array([0.485, 0.456, 0.406])
imagenet_std = np.array([0.229, 0.224, 0.225])


class TorchAdapter(NNAdapter):
    """
    Overrides the NNAdapter to load and read Torch7/pytorch models.
    An installation of pytorch is required.
    """

    def __init__(self, model_fp, mean, std, inputsize, keep_outputs=None, use_gpu=False):
        """
        Initializes the adapter with a pretrained model from a filepath or pytorch-model identifier
        (see https://github.com/pytorch/vision#models).

        Parameters
        ----------
        model_fp : String
            Filepath or model identifier.
        mean : ndarray or String
            Mean definition via array or filepath to .t7 torch or .npy tensor.
        std : ndarray or String
            Standard deviation definition via array or filepath to .t7 torch or .npy tensor.
        inputsize : tuple or list
            Target input data dimensionality of format: (channels, height, width).
            Used for rescaling given data in preprocess step.
        use_gpu : bool
            Flag to enable gpu use. Default: False
        keep_outputs : list, tuple or set
            List of layer identifier strings to keep during a feed forward call to enable later access via
            get_layeroutput().
            By default no layer outputs but the last are kept.
            Consolidate get_layers() to identify layers.
        """
        # self.model = self._load_model_config(model_fp)
        if '.' not in os.path.basename(model_fp):
            import torchvision.models as models
            if model_fp not in models.__dict__:
                raise KeyError('Model {} does not exist in pytorchs model zoo.'.format(model_fp))
            print('Loading model {} from pytorch model zoo'.format(model_fp))<commit_msg>Do not require file extension for loading from file<commit_after>from __future__ import absolute_import

import re
import os
from collections import OrderedDict
from functools import partial

import torch
from torch.autograd import Variable
from torch.utils.serialization import load_lua
from .backend.torchlegacy import load_legacy_model, LambdaBase
import numpy as np

from emu.nnadapter import NNAdapter
from emu.docutil import doc_inherit


imagenet_mean = np.array([0.485, 0.456, 0.406])
imagenet_std = np.array([0.229, 0.224, 0.225])


class TorchAdapter(NNAdapter):
    """
    Overrides the NNAdapter to load and read Torch7/pytorch models.
    An installation of pytorch is required.
    """

    def __init__(self, model_fp, mean, std, inputsize, keep_outputs=None, use_gpu=False):
        """
        Initializes the adapter with a pretrained model from a filepath or pytorch-model identifier
        (see https://github.com/pytorch/vision#models).

        Parameters
        ----------
        model_fp : String
            Filepath or model identifier.
        mean : ndarray or String
            Mean definition via array or filepath to .t7 torch or .npy tensor.
        std : ndarray or String
            Standard deviation definition via array or filepath to .t7 torch or .npy tensor.
        inputsize : tuple or list
            Target input data dimensionality of format: (channels, height, width).
            Used for rescaling given data in preprocess step.
        use_gpu : bool
            Flag to enable gpu use. Default: False
        keep_outputs : list, tuple or set
            List of layer identifier strings to keep during a feed forward call to enable later access via
            get_layeroutput().
            By default no layer outputs but the last are kept.
            Consolidate get_layers() to identify layers.
        """
        # self.model = self._load_model_config(model_fp)
        if not os.path.exists(model_fp):
            import torchvision.models as models
            if model_fp not in models.__dict__:
                raise KeyError('Model {} does not exist in pytorchs model zoo.'.format(model_fp))
            print('Loading model {} from pytorch model zoo'.format(model_fp))
{'fraction_non_alphanumeric': 0.06633685630762312, 'fraction_numerical': 0.012143017764785248, 'mean_word_length': 2.587096774193548, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4839371', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1194, 'n_words': 464}