コード例 #1
0
    def __init__(self, filepath: str):
        """Constructs model by loading pretrained net.
        
        Arguments:
            filepath (str) : the path to the pretrained h5 net

        Raises:
        TypeError: filepath not  string
        FileNotFoundError: filepath not pointing to anything
        NotKerasModelError: filepath not pointing to h5 keras model
        """
        type_check(filepath, str, "filepath")
        file_exists(filepath)
        self.load(filepath)
        super(WafBrainWrapper, self).__init__(self._keras_classifier)
コード例 #2
0
    def create_dataset_from_file(
        self, filepath: str, label: int, limit: int = None, unique_rows=True
    ):
        """Create dataset from fil containing sql queries.
        
        Arguments:
            filepath (str) : path of sql queries dataset
            label (int) : labels to assign to each sample
        
        Keyword Arguments:
            limit (int) : if None, it specifies how many queries to use (default: (None))
            unique_rows (bool) : True for removing all the duplicates (default: (True))
        
        Raises:
            TypeError: params has wrong types
            FileNotFoundError: filepath not pointing to regular file
            TypeError: limit is not None and not int
        
        Returns:
            (numpy ndarray, list) : X and y
        """
        type_check(filepath, str, "filepath")
        type_check(label, int, "label")
        type_check(unique_rows, bool, "unique_rows")
        if limit is not None:
            type_check(limit, int, "limit")

        file_exists(filepath)
        X = []
        with open(filepath, "r") as f:
            i = 0
            for line in f:
                if limit is not None and i > limit:
                    break
                line = line.strip()
                X.append(self.produce_feat_vector(line))
                i += 1
        if unique_rows:
            X = np.unique(X, axis=0)
        else:
            X = np.array(X)
        y = [label for _ in X]
        return X, y
コード例 #3
0
    def load_model(self, filepath, ModelClass):
        """Loads a PyTorch classifier stored in filepath.

        Arguments:
            filepath (string) : The path of the PyTorch classifier.

        Raises:
            TypeError: filepath is not string.
            FileNotFoundError: filepath not pointing to any file.
            NotPyTorchModelError: model can not be loaded.

        Returns:
            self
        """
        type_check(filepath, str, "filepath")
        file_exists(filepath)
        ModelClass.load_state_dict(torch.load(filepath))
        ModelClass.eval()
        self._pytorch_classifier = ModelClass
        return self
コード例 #4
0
    def load(self, filepath):
        """Loads a sklearn classifier stored in filepath.
        
        Arguments:
            filepath (string) : The path of the sklearn classifier.

        Raises:
            TypeError: filepath is not string.
            FileNotFoundError: filepath not pointing to any file.
            NotSklearnModelError: model can not be loaded.

        Returns:
            self
        """
        type_check(filepath, str, "filepath")
        file_exists(filepath)
        try:
            self._sklearn_classifier = joblib.load(filepath)
        except Exception as e:
            raise NotSklearnModelError("Error in loading model.") from e
        return self
コード例 #5
0
    def load(self, filepath):
        """Loads a keras classifier stored in filepath.
        
        Arguments:
            filepath (string) : The path of the keras classifier.
        
        Returns:
            self
        Raises:
            TypeError: filepath is not string.
            FileNotFoundError: filepath not pointing to any file.
            NotKerasModelError: model can not be loaded.
        """
        type_check(filepath, str, "filepath")
        file_exists(filepath)

        try:
            self._keras_classifier = keras.models.load_model(filepath)
        except Exception as e:
            raise NotKerasModelError(
                "Can not load keras model. See inner exception for details."
            ) from e