Exemplo n.º 1
0
def get_classification_data_loaders():
    x_train = np.random.rand(200, 28, 28, 3)
    y_train = np.random.rand(200, 3)
    x_test = np.random.rand(190, 28, 28, 3)
    y_test = np.random.rand(190, 3)
    data_transformer = Backend.get_image_transformer(x_train, augment=True)
    train_data = data_transformer.transform_train(x_train, y_train)
    test_data = data_transformer.transform_test(x_test, y_test)
    return train_data, test_data
Exemplo n.º 2
0
    def __init__(self, verbose, **kwargs):
        super().__init__(verbose=verbose, **kwargs)
        self.device = Backend.get_device()

        # BERT specific
        self.bert_model = 'bert-base-uncased'
        self.tokenizer = BertTokenizer.from_pretrained(self.bert_model, do_lower_case=True)

        # Labels/classes
        self.num_labels = None
Exemplo n.º 3
0
    def fit(self, x_train):
        """ Train only

        Args:
            x_train: ndarray contained the training data

        Returns:

        """
        # input size stay the same, enable  cudnn optimization
        cudnn.benchmark = True
        self.data_transformer = Backend.get_image_transformer(x_train, augment=self.augment)
        train_dataloader = self.data_transformer.transform_train(x_train)
        GANModelTrainer(self.net_g,
                        self.net_d,
                        train_dataloader,
                        Backend.binary_classification_loss,
                        self.verbose,
                        self.gen_training_result,
                        device=Backend.get_device()).train_model()
Exemplo n.º 4
0
    def predict(self, x_test):
        """Return the predicted labels for the testing data.

        Args:
            x_test: An instance of numpy.ndarray containing the testing data.

        Returns:
            A numpy.ndarray containing the predicted labels for the testing data.
        """
        if Constant.LIMIT_MEMORY:
            pass

        x_test = self.preprocess(x_test)
        test_loader = self.data_transformer.transform_test(x_test)
        model = self.graph.produce_model()
        model.eval()

        output = Backend.predict(model, test_loader)
        return self.inverse_transform_y(output)
Exemplo n.º 5
0
def train(q, graph, train_data, test_data, trainer_args, metric, loss, verbose,
          path):
    """Train the neural architecture."""
    try:
        model = graph.produce_model()
        loss, metric_value = Backend.get_model_trainer(
            model=model,
            path=path,
            train_data=train_data,
            test_data=test_data,
            metric=metric,
            loss_function=loss,
            verbose=verbose).train_model(**trainer_args)
        model.set_weight_to_graph()
        if q:
            q.put((metric_value, loss, model.graph))
        return metric_value, loss, model.graph
    except RuntimeError as e:
        if not re.search('out of memory', str(e)):
            raise e
        if verbose:
            print(
                '\nCurrent model size is too big. Discontinuing training this model to search for other models.'
            )
        Constant.MAX_MODEL_SIZE = graph.size() - 1
        if q:
            q.put((None, None, None))
        return None, None, None
    except TimeoutError as exp:
        logging.warning("TimeoutError occurred at train() : {0}".format(
            str(exp)))
        if q:
            q.put((None, None, None))
        return None, None, None
    except Exception as exp:
        logging.warning("Exception occurred at train() : {0}".format(str(exp)))
        if verbose:
            print("Exception occurred at train() : {0}".format(str(exp)))
        if q:
            q.put((None, None, None))
        return None, None, None
Exemplo n.º 6
0
    def fit(self, x_train, y_train, time_limit=None):
        """Trains the model on the dataset given.

        Args:
            x_train: A numpy.ndarray instance containing the training data,
                or the training data combined with the validation data.
            y_train: A numpy.ndarray instance containing the label of the training data,
                or the label of the training data combined with the validation label.
            time_limit: A dictionary containing the parameters of the ModelTrainer constructor.
        """
        validate_xy(x_train, y_train)
        self.resize_shape = compute_image_resize_params(x_train)
        x_train = self.preprocess(x_train)
        self.y_encoder.fit(y_train)
        y_train = self.transform_y(y_train)
        # Divide training data into training and testing data.
        validation_set_size = int(len(y_train) * Constant.VALIDATION_SET_SIZE)
        validation_set_size = min(validation_set_size, 500)
        validation_set_size = max(validation_set_size, 1)
        x_train_new, x_test, y_train_new, y_test = train_test_split(
            x_train, y_train, test_size=validation_set_size, random_state=42)

        # initialize data_transformer
        self.data_transformer = Backend.get_image_transformer(x_train)
        # Wrap the data into DataLoaders
        train_loader = self.data_transformer.transform_train(
            x_train_new, y_train_new)
        test_loader = self.data_transformer.transform_test(x_test, y_test)

        self.generator = self._init_generator(self.y_encoder.n_classes,
                                              x_train_new.shape[1:])
        graph = self.generator.generate()

        if time_limit is None:
            time_limit = {'max_no_improvement_num': 30}
        _, _1, self.graph = train(None, graph, train_loader, test_loader,
                                  time_limit, self.metric, self.loss,
                                  self.verbose, self.path)
Exemplo n.º 7
0
    def __init__(self, verbose, **kwargs):
        """Initialize the TextClassifier.

        Args:
            verbose: Mode of verbosity.
        """
        super().__init__(verbose=verbose, **kwargs)
        self.device = Backend.get_device()
        self.verbose = verbose

        # BERT specific
        self.bert_model = 'bert-base-uncased'
        self.max_seq_length = 128
        self.tokenizer = BertTokenizer.from_pretrained(self.bert_model, do_lower_case=True)

        # Labels/classes
        self.num_labels = None

        # Output directory
        self.output_model_file = os.path.join(self.path, 'pytorch_model.bin')

        # Evaluation params
        self.eval_batch_size = 32
Exemplo n.º 8
0
 def __init__(self, nz=100, ngf=32, ndf=32, nc=3, verbose=False, gen_training_result=None,
              augment=None):
     """
    Args:
         nz: size of the latent z vector
         ngf: of gen filters in first conv layer
         ndf: of discrim filters in first conv layer
         nc: number of input chanel
         verbose: A boolean of whether the search process will be printed to stdout.
         gen_training_result: A tuple of (path, size) to denote where to output the intermediate result with size
         augment: A boolean value indicating whether the data needs augmentation.
     """
     super().__init__(verbose)
     self.nz = nz
     self.ngf = ngf
     self.ndf = ndf
     self.nc = nc
     self.verbose = verbose
     self.device = Backend.get_device()
     self.gen_training_result = gen_training_result
     self.augment = augment if augment is not None else Constant.DATA_AUGMENTATION
     self.data_transformer = None
     self.net_d = Discriminator(self.nc, self.ndf)
     self.net_g = Generator(self.nc, self.nz, self.ngf)
Exemplo n.º 9
0
 def produce_model(self):
     """Build a new torch model based on the current graph."""
     return Backend.produce_model(self)
Exemplo n.º 10
0
 def init_transformer(self, x):
     if self.data_transformer is None:
         self.data_transformer = Backend.get_image_transformer(x, augment=self.augment)
Exemplo n.º 11
0
    def predict(self, test_loader):
        model = self.best_model.produce_model()
        model.eval()

        return Backend.predict(model, test_loader)
Exemplo n.º 12
0
 def compute(cls, prediction, target):
     return Backend.regression_metric(prediction, target)
Exemplo n.º 13
0
 def compute(cls, prediction, target):
     return Backend.classification_metric(prediction, target)
Exemplo n.º 14
0
def get_classification_train_data_loaders():
    x_train = np.random.rand(200, 32, 32, 3)
    data_transformer = Backend.get_image_transformer(x_train, augment=True)
    train_data = data_transformer.transform_train(x_train)
    return train_data