예제 #1
0
    def save(self, location: str):

        import json
        import pickle
        from tensorflow.keras.models import save_model  #type: ignore

        if location[-1] != '/':
            location += '/'

        try:
            mkdir(location)
        except FileExistsError:
            pass

        #Save metadata
        metadata: Dict[str, Any] = {
            'brainType': self.brain_type,
            'maxSequenceLength': self._max_sequence_length,
            'includeCasingInformation': self.include_casing_information,
            'bidirectional': self.bidirectional
        }
        json.dump(metadata, open(location + 'metadata.json', 'w'))

        #Save tokenizer
        pickle.dump(self.tokenizer, open(location + 'tokenizer.pickle', 'wb'))

        #Save model
        save_model(self.model, location + 'model')
예제 #2
0
파일: main.py 프로젝트: yjc-991115/nnom
def train_simple(x_train, y_train, vad_train, batch_size=64, epochs=10, model_name="model.h5"):
    """
    This simple RNN model also can do similar jobs. Compared to the complex RNNoise-like model:
    it take the same input as the other one, but train with only the gains (without VAD)
    it also have a simple straight forward structure (no concatenate).
    """
    input_feature_size = x_train.shape[-1]
    output_feature_size = y_train.shape[-1]
    timestamp_size = batch_size

    input = Input(shape=(1, input_feature_size), batch_size=timestamp_size)

    x = GRU(96, return_sequences=True, stateful=True, recurrent_dropout=0.3)(input)
    x = GRU(96, return_sequences=True, stateful=True, recurrent_dropout=0.3)(x)
    x = GRU(48, return_sequences=True, stateful=True, recurrent_dropout=0.3)(x)
    x = Flatten()(x)
    x = Dense(output_feature_size)(x)
    x = Activation("hard_sigmoid")(x) # use hard sigmoid for better resolution in fixed-point model

    model = Model(inputs=input, outputs=[x])
    model.compile("adam", loss=["MSE"], metrics=[msse])
    model.summary()
    history = model.fit(x_train, y_train,
                        batch_size=timestamp_size, epochs=epochs, verbose=2, shuffle=False, # shuffle must be false
                        callbacks=[reset_state_after_batch()])
    # free the session to avoid nesting naming while we load the best model after.
    save_model(model, model_name)
    del model
    tf.keras.backend.clear_session()
    return history
예제 #3
0
def main():
    #Initialize GPU
    load_device()

    #Paths to image data
    DATADIR = os.path.join(os.getcwd(), 'dataset', 'kaggle')
    paths = [os.path.join(DATADIR + '\\train'), os.path.join(DATADIR+ '\\valid'), os.path.join(DATADIR+ '\\test')]

    #paths to save callbacks for tensorboard and checkpoints
    tb_name = 'fit'
    checkpoint_path = "checkpoint/cp-{epoch:04d}.ckpt"
    checkpoint_dir = os.path.dirname(checkpoint_path)           

    #load Data
    train_batches, valid_batches, test_batches = load_data(paths=paths, dim=(224, 224), 
                                                         class_names=['cats', 'dogs'], batch_size=32)

    #create model
    model = create_model()
    model.summary()

    #start training
    train_model()

    #test model and generate classification report and confusion matrix
    class_names=['cats', 'dogs']
    test_model(class_names)

    #save model
    save_model('./models')

    #test model using saved weights
    model = load_model('./models')
    test_model()
예제 #4
0
def train_and_evaluate(batch_size, epochs, job_dir, output_path):
    # Download the data
    x_train, y_train, x_test, y_test = _download_data()

    # Preprocess the data
    x_train, y_train = _preprocess_data(x_train, y_train)
    x_test, y_test = _preprocess_data(x_test, y_test)

    # Build the model
    model = _build_model()
    model.compile(loss=losses.categorical_crossentropy,
                  optimizer=optimizers.Adam(),
                  metrics=[metrics.categorical_accuracy])

    # Train the model
    # Use tensorboard in the Cloud Shell without slash at the end
    # e.g. tensorboard --logdir gs://BUCKET/tmp/logs  <-- no slash
    logdir = os.path.join(job_dir,
                          "logs/scalars/" + time.strftime("%Y%m%d-%H%M%S"))
    tb_callback = callbacks.TensorBoard(log_dir=logdir)
    model.fit(x_train,
              y_train,
              epochs=epochs,
              batch_size=batch_size,
              callbacks=[tb_callback])

    # Evaluate the model
    loss_value, accuracy = model.evaluate(x_test, y_test)
    LOGGER.info("  *** LOSS VALUE:  %f     ACCURACY: %.4f" %
                (loss_value, accuracy))

    # Save model in TF SavedModel Format
    model_dir = os.path.join(output_path, VERSION)
    models.save_model(model, model_dir, save_format='tf')
예제 #5
0
    def test_maxpool(self, _model_maxpool2D_1_last, _config_last):
        """Test maxpooling."""
        config = _config_last
        path_wd = config.get('paths', 'path_wd')
        model_name = config.get('paths', 'filename_ann')
        models.save_model(_model_maxpool2D_1_last,
                          os.path.join(path_wd, model_name + '.h5'))

        updates = {
            'tools': {
                'evaluate_ann': False,
                'normalize': False
            },
            'simulation': {
                'duration': 100,
                'num_to_test': 100,
                'batch_size': 50
            },
            'output': {
                'log_vars': {'activations_n_b_l', 'spiketrains_n_b_l_t'}
            }
        }

        config.read_dict(updates)

        acc = run_pipeline(config)

        acc_ann = get_ann_acc(config)
        assert acc[0] >= 0.9 * acc_ann

        corr = get_correlations(config)
        assert np.all(corr[:-1] > 0.99)
        assert corr[-1] > 0.90
예제 #6
0
    def test_inisim(self, _model_2, _config):

        path_wd = _config.get('paths', 'path_wd')
        model_name = _config.get('paths', 'filename_ann')
        models.save_model(_model_2, os.path.join(path_wd, model_name + '.h5'))

        updates = {
            'tools': {
                'evaluate_ann': False
            },
            'simulation': {
                'duration': 100,
                'num_to_test': 100,
                'batch_size': 50
            },
            'output': {
                'log_vars': {'activations_n_b_l', 'spiketrains_n_b_l_t'}
            }
        }

        _config.read_dict(updates)

        acc = run_pipeline(_config)

        assert acc[0] >= 0.95

        corr = get_correlations(_config)
        assert np.all(corr[:-1] > 0.99)
        assert corr[-1] > 0.90
예제 #7
0
 def save_to_dir(self, directory: str):
     params = self.get_params()
     with open(path.join(directory, "params.json"), "w") as f:
         json.dump(params, f)
     if hasattr(self, "model") and self.model is not None:
         save_model(
             self.model,
             filepath=path.join(directory, "model.h5"),
             include_optimizer=True,
             save_format="h5",
         )
         if (
             hasattr(self.model, "history")
             and self.model.history.history is not None
         ):
             f_name = path.join(directory, "history.pkl")
             with open(f_name, "wb") as history_file:
                 pickle.dump(
                     (
                         self.model.history.history,
                         self.model.history.params,
                         self.model.history.epoch,
                     ),
                     history_file,
                 )
예제 #8
0
    def on_epoch_end(self, epoch, logs={}):
        val_loss = logs['loss']
        val_acc = logs['acc']
        if val_acc > self.best_acc:
            self.best_acc = val_acc
            self.best_loss = val_loss

            # Filenames
            hdf5Filename = os.path.join(
                self.bestdir, "Bestmodel_{:06d}_{:.4f}_{:.4f}.hdf5".format(
                    epoch + 1, val_acc, val_loss))
            yamlFilename = os.path.join(
                self.bestdir, "Bestmodel_{:06d}_{:.4f}_{:.4f}.yaml".format(
                    epoch + 1, val_acc, val_loss))

            # YAML
            yamlModel = self.model.to_yaml()
            with open(yamlFilename, "w") as yamlFile:
                yamlFile.write(yamlModel)

            # HDF5
            KM.save_model(self.model, hdf5Filename)
            with H.File(hdf5Filename, "r+") as f:
                f.require_dataset("initialEpoch", (), "uint64",
                                  True)[...] = int(epoch + 1)
                f.flush()

            # Print
            L.getLogger("train").info(
                "Saved best model to {:s} at epoch {:5d}".format(
                    hdf5Filename, epoch + 1))
예제 #9
0
 def __export__(self, path):
     """
     Saves a CNN to a .model file
     :param path: path to the .model file to be saved
     :return: void
     """
     save_model(self.model, path)
예제 #10
0
def train(model=model(), checkpoint="latest.hdf5"):
    dataset = build_dataset()
    callback = TensorBoard("_tensor_board")
    callback.set_model(model)
    labels = interested_words
    for i in range(10000):
        save_model(model, "checkpoint.model")
        print("Chunk " + str(i) + " of 10000...")
        X, Y = get_batch(dataset, batchsize=1000)
        for j in range(10):
            logs = model.train_on_batch(np.array(X), np.array(Y))
            print("loss:", logs)
            write_log(callback, ["training loss"], [logs], i * 10 + j)
        X, Y = get_batch(dataset, batchsize=100, batchtype="test")
        results = model.predict(X)
        accuracy = 0
        for result, actual in zip(results, Y):
            #print("running test")
            x = np.argmax(result)
            j = np.argmax(actual)
            try:
                print("expected " + labels[j], " got " + labels[x])
            except:
                pass
            if x == j: accuracy += 1
        write_log(callback, ["test accuracy"], [accuracy], i)
 def main(self, data_file_path, model_file_path):
     data = pd.read_csv(data_file_path)
     train_tweets, val_tweets, train_label, val_label = self.preprocess_data(
         data["text"], data["target"])
     result, model = self.train_model(train_tweets, val_tweets, train_label,
                                      val_label)
     save_model(model, model_file_path)
예제 #12
0
def retrain():
    '''
    Retrain the LSTM
    '''

    X = []
    Y = []

    f = open("assets/data/name_gender.csv")
    data = f.readlines()
    f.close()
    for line in data:
        name, gender = line.split(',')
        if name and gender:
            tensor = extract(name)
            if type(tensor) == np.ndarray:
                X.extend(tensor.tolist())
            Y.append([1, 0] if gender.strip("\n") == 'M' else [0, 1])

    X = np.array(X)
    Y = np.array(Y)

    if X.shape[0] > 0 and Y.shape[0] > 0:
        callback = EarlyStopping(
            monitor='val_loss', min_delta=0, patience=3, verbose=0,
            mode='auto', baseline=None, restore_best_weights=True
        )

        model = load_model("assets/model/LSTMSimple")
        model.fit(X, Y, batch_size=1, epochs=X.shape[0], validation_data=(X, Y),
                            callbacks=[callback])
        save_model(model, "assets/model/LSTMSimple", save_format="h5")

#if __name__ == '__main__':
#    retrain()
예제 #13
0
def create_model():
    MODEL_FILENAME = "build/model/digit_model.h5"

    # Load Data
    mnist = tf.keras.datasets.mnist
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    # Normalize pixel grayscale to be between 0 and 1:
    x_train, x_test = x_train / 255.0, x_test / 255.0

    model = tf.keras.models.Sequential([
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(512, activation=tf.nn.relu),
        tf.keras.layers.Dropout(0.2),
        tf.keras.layers.Dense(10, activation=tf.nn.softmax)
    ])
    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

    model.fit(x_train, y_train, epochs=5)

    #Create Path
    if not os.path.exists(os.path.dirname(MODEL_FILENAME)):
        try:
            os.makedirs(os.path.dirname(MODEL_FILENAME))
        except OSError as exc:  # Guard against race condition
            if exc.errno != errno.EEXIST:
                raise

    save_model(model, MODEL_FILENAME)
    del model  # delete
예제 #14
0
def train(x_train, y_train, x_test, y_test, type, batch_size=64, epochs=100):
    inputs = Input(shape=x_train.shape[1:])
    x = Conv1D(16, kernel_size=5, strides=1)(inputs)
    x = LSTM(32, return_sequences=True)(x)
    x = LSTM(32, return_sequences=True)(x)
    x = Flatten()(x)
    x = Dense(128)(x)
    x = Dropout(0.3)(x)
    x = Dense(type)(x)
    predictions = Softmax()(x)

    model = Model(inputs=inputs, outputs=predictions)

    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

    model.summary()
    history = model.fit(x_train,
                        y_train,
                        batch_size=batch_size,
                        epochs=epochs,
                        verbose=2,
                        validation_data=(x_test, y_test))

    # free the session to avoid nesting naming while we load the best model after.
    save_model(model, model_path)
    del model
    tf.keras.backend.clear_session()
    return history
예제 #15
0
def train_and_evaluate(batch_size, epochs, job_dir, output_path):
    # Donwload the data
    x_train, y_train, x_test, y_test = _download_data()

    # Preprocess the data
    x_train, y_train = _preprocess_data(x_train, y_train)
    x_test, y_test = _preprocess_data(x_test, y_test)

    # Build the model
    model = _build_model()
    model.compile(loss=losses.categorical_crossentropy
          , optimizer=optimizers.Adam()
          , metrics=[metrics.categorical_accuracy])

    # Train the model
    logdir = os.path.join(job_dir, "logs/scalars/" + time.strftime("%Y%m%d-%H%M%S"))
    tb_callback = callbacks.TensorBoard(log_dir=logdir)

    model.fit(x_train, y_train
        , batch_size=batch_size
        , epochs=epochs
        , callbacks=[tb_callback])
      #  , validation_split=0.15) no se utiliza

    # Evaluate the model
    loss_value, accuracy = model.evaluate(x_test, y_test)
    #print(accuracy) -- se puede hacer pero mejor logger
    LOGGER.info(f" **** LOSS VALUE:{loss_value}, ACCURACY:{round(accuracy,4)}")
    
    # Save model in TF scikit - pickle in tensorflow save model
    model_dir = os.path.join(output_path, VERSION)
    models.save_model(model, model_dir)
예제 #16
0
 def save_model_to(self, output_name=None):
     if output_name:
         filepath = os.path.join('models', output_name)
     else:
         filepath = Path(self.model_dir)
     save_model(self.Model, filepath=filepath)
     save_config(self.config, os.path.join(filepath, 'config.json'))
예제 #17
0
def create_model():
    """Create image classification object"""
    file_name = "./resnet50.h5"

    dir_path = os.path.dirname(os.path.realpath(__file__))
    full_path = os.path.join(dir_path, file_name)
    if os.path.exists(full_path):
        model = load_model(full_path, custom_objects=None, compile=True)
    else:
        model = InceptionResNetV2(
            include_top=True,
            weights="imagenet",
            input_tensor=None,
            input_shape=None,
            pooling=None,
            classes=1000,
        )
        save_model(model,
                   full_path,
                   overwrite=True,
                   include_optimizer=True,
                   save_format=None,
                   signatures=None,
                   options=None)

    data = {"model": model}
    return data
예제 #18
0
    def test_maxpool_first(self, _model_maxpool2D_1_first, _config_first):
        """Test that maxpooling fails with data format channels_first."""
        config = _config_first
        path_wd = config.get('paths', 'path_wd')
        model_name = config.get('paths', 'filename_ann')
        models.save_model(_model_maxpool2D_1_first,
                          os.path.join(path_wd, model_name + '.h5'))

        updates = {
            'tools': {
                'evaluate_ann': False,
                'normalize': False
            },
            'simulation': {
                'duration': 100,
                'num_to_test': 100,
                'batch_size': 50
            },
            'output': {
                'log_vars': {'activations_n_b_l', 'spiketrains_n_b_l_t'}
            }
        }

        config.read_dict(updates)

        run_pipeline(config)
예제 #19
0
 def save(self, path):
     """ Saves the current state """
     with ZipFile(path, mode='w') as package:
         print('Creating zip package:', path)
         tmpfile = NamedTemporaryFile(suffix='.h5')
         print('Temporarily saving keras model to', tmpfile.name)
         models.save_model(self.model, tmpfile.name)
         print('Adding', tmpfile.name, 'to package as model.h5')
         package.write(tmpfile.name, 'model.h5')
         print('Adding glove.bin', len(pickle.dumps(self.glove)), 'bytes.')
         package.writestr('glove.bin', pickle.dumps(self.glove))
         print('Adding tfidf_model.bin',
               len(pickle.dumps(self.tfidf_model)), 'bytes.')
         package.writestr('tfidf_model.bin', pickle.dumps(self.tfidf_model))
         print('Adding keyphraseness_model.bin',
               len(pickle.dumps(self.keyphraseness_model)), 'bytes.')
         package.writestr('keyphraseness_model.bin',
                          pickle.dumps(self.keyphraseness_model))
         print('Adding pos_tag_model.bin',
               len(pickle.dumps(self.pos_tag_model)), 'bytes.')
         package.writestr('pos_tag_model.bin',
                          pickle.dumps(self.pos_tag_model))
         print('Adding settings.json',
               len(json.dumps(self.settings, default=lambda x: x.__dict__)),
               'characters.')
         package.writestr(
             'settings.json',
             json.dumps(self.settings, default=lambda x: x.__dict__))
예제 #20
0
    def test_maxpool_fallback(self, _model_maxpool2D_1, _config):
        """Test that maxpooling falls back on average pooling."""
        path_wd = _config.get('paths', 'path_wd')
        model_name = _config.get('paths', 'filename_ann')
        models.save_model(_model_maxpool2D_1,
                          os.path.join(path_wd, model_name + '.h5'))

        updates = {
            'tools': {
                'evaluate_ann': False,
                'normalize': False
            },
            'conversion': {
                'max2avg_pool': True
            },
            'simulation': {
                'duration': 100,
                'num_to_test': 100,
                'batch_size': 50
            },
            'output': {
                'log_vars': {'activations_n_b_l', 'spiketrains_n_b_l_t'}
            }
        }

        _config.read_dict(updates)

        acc = run_pipeline(_config)

        assert acc[0] >= 0.8

        corr = get_correlations(_config)
        assert np.all(corr[:-1] > 0.99)
        assert corr[-1] > 0.90
예제 #21
0
    def on_epoch_end(self, epoch, logs=None):
        train_loss = round(logs.get('loss'), 4)
        val_loss = round(logs.get('val_loss'), 4)

        # Save model as h5 file
        model_filepath = f'{self.model_dir}/model-{epoch + 1}-{train_loss}-{val_loss}.h5'
        save_model(self.model, model_filepath)

        # Save model weights as h5 file
        weights_filepath = f'{self.model_dir}/model-weights-{epoch + 1}-{train_loss}-{val_loss}.h5'
        self.model.save_weights(weights_filepath)

        # Save optimizer state as pickle file
        optimizer_weights_filepath = f'{self.model_dir}/optimizer-weights-{epoch + 1}-{train_loss}-{val_loss}.pkl'
        with open(optimizer_weights_filepath, 'wb') as f:
            pickle.dump(
                K.batch_get_value(getattr(self.model.optimizer, 'weights')), f)

        # Update the checkpoint epoch
        self.checkpoint['epoch'] += 1
        if val_loss < self.best_val_loss:
            self.checkpoint['best_model'] = model_filepath
            self.best_val_loss = val_loss
        self.checkpoint['last_model'] = model_filepath
        self.checkpoint['last_weights'] = weights_filepath
        self.checkpoint['last_optimizer_weights'] = optimizer_weights_filepath
        with open(f"{self.checkpoint['checkpoint_dir']}/checkpoint.json",
                  "w") as f:
            json.dump(self.checkpoint, f, indent=4)
예제 #22
0
    def test_spinnaker_sparse(self, _model_3, _config):

        path_wd = _config.get('paths', 'path_wd')
        model_name = _config.get('paths', 'filename_ann')
        models.save_model(_model_3, os.path.join(path_wd, model_name + '.h5'))

        updates = {
            'tools': {
                'evaluate_ann': False
            },
            'input': {
                'poisson_input': True
            },
            'simulation': {
                'simulator': 'spiNNaker',
                'duration': 100,
                'num_to_test': 1,  # smaller to make more feasible
                'batch_size': 1
            },
            'output': {
                'log_vars': {'activations_n_b_l', 'spiketrains_n_b_l_t'}
            }
        }

        _config.read_dict(updates)

        initialize_simulator(_config)

        acc = run_pipeline(_config)

        assert acc[0] >= 0.95

        corr = get_correlations(_config)
        assert np.all(corr[:-1] > 0.97)
        assert corr[-1] > 0.5
예제 #23
0
def get_measure_tf_lite(model, number_of_measures, tmp_keras_file,
                        tmp_tflite_file, benchmark_loc):
    """given a model, loads that model in tf_lite and benchmarks the time needed for a prediction in C++ using
    the benchmark tool associated with tf-lite (this tool does not return median but only mean so we will use
    that instead)
    :return: the mean of number_of_measures trials"""
    model.compile(optimizer=SGD(), loss='binary_crossentropy')
    save_model(model, tmp_keras_file)

    # Convert to TensorFlow Lite model.
    converter = lite.TFLiteConverter.from_keras_model_file(tmp_keras_file)
    tflite_model = converter.convert()
    with open(tmp_tflite_file, "wb") as file:
        file.write(tflite_model)

    # Loads TFLite model and get measures
    command_line = "{} --graph={} --min_secs=0 --warmup_min_secs=0 --num_runs={} |& tr -d '\n' | awk {}".format(
        benchmark_loc, tmp_tflite_file, number_of_measures,
        "'{print $NF}'")  # tr removes the \n and awk gets
    # the last element of the outputs message, |& is used before tr because we want to pipe strderr and not
    # stdout
    result = float(
        subprocess.check_output(command_line,
                                shell=True,
                                executable='/bin/bash')) / 10**6  # result
    # given in microseconds
    return result
예제 #24
0
def model_train_one_it(df, i, model_path):
    import numpy as np
    import time
    from tensorflow.keras.models import Model, load_model, save_model
    import tensorflow as tf
    config = tf.compat.v1.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.compat.v1.Session(config=config)

    X_inpre = df['X'][i]
    y_inpre = df['y_in'][i]
    y_outpre = df['y'][i]

    characters = y_outpre.shape[0]
    letters = y_outpre.shape[1]

    X_in = np.stack([X_inpre for x in range(characters)])
    y_in = np.array(y_inpre).reshape(characters, 1, letters)
    y_out = np.array(y_outpre).reshape(characters, 1, letters)

    model = load_model(model_path)

    history = model.fit([X_in, y_in],
                        y_out,
                        batch_size=32,
                        max_queue_size=256,
                        use_multiprocessing=True)
    save_model(model, model_path, save_format='h5')

    return history
예제 #25
0
def get_median_measure_tf_lite_python(model, number_of_measures,
                                      tmp_keras_file, tmp_tflite_file):
    """given a model, loads that model in tf_lite and benchmarks the time needed for a prediction in python
    :return: the median of number_of_measures trials"""
    measures = np.zeros(number_of_measures)

    model.compile(optimizer=SGD(), loss='binary_crossentropy')
    save_model(model, tmp_keras_file)

    # Convert to TensorFlow Lite model.
    converter = lite.TFLiteConverter.from_keras_model_file(tmp_keras_file)
    tflite_model = converter.convert()
    with open(tmp_tflite_file, "wb") as file:
        file.write(tflite_model)

    # Load TFLite model and get measures
    interpreter = lite.Interpreter(model_path=tmp_tflite_file)
    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()

    for k in range(number_of_measures):
        # Test model on random input data.
        input_shape = input_details[0]['shape']
        input_data = np.array(np.random.random_sample(input_shape),
                              dtype=np.float32)
        interpreter.set_tensor(input_details[0]['index'], input_data)

        begin = time.perf_counter()
        interpreter.invoke()
        measures[k] = time.perf_counter() - begin

    return np.median(measures)
예제 #26
0
def model_training():
    df = pd.read_csv("results.csv")
    X = df[["date", "home_team", "away_team", "country", "neutral"]]
    y = df[["home_score", "away_score"]]
    X_train, X_valid, y_train, y_valid = train_test_split(X,
                                                          y,
                                                          train_size=0.80)

    preprocessor = ColumnTransformer(
        transformers=[('date_scale', StandardScaler(),
                       ['date']), ('neutral', OrdinalEncoder(), ['neutral']),
                      ('home',
                       OneHotEncoder(handle_unknown='ignore', sparse=False),
                       ['home_team']),
                      ('away',
                       OneHotEncoder(handle_unknown='ignore', sparse=False),
                       ['away_team'])])

    pipeline = Pipeline(steps=[('date', DateTransformer()),
                               ('pre', preprocessor)],
                        verbose=True)

    X_train = pipeline.fit_transform(X_train)
    X_valid = pipeline.transform(X_valid)
    input_shape = [X_train.shape[1]]

    model = models.Sequential([
        layers.Dense(512, activation='relu', input_shape=input_shape),
        layers.Dense(512, activation='relu'),
        layers.Dense(2)
    ])
    model.compile(loss='mse', optimizer=optimizers.Adam(learning_rate=0.01))

    early_stopping = callbacks.EarlyStopping(
        patience=5,
        min_delta=0.001,
        restore_best_weights=True,
    )

    history = model.fit(X_train,
                        y_train,
                        validation_data=(X_valid, y_valid),
                        epochs=500,
                        batch_size=64,
                        callbacks=[early_stopping],
                        verbose=1)

    curr_dir = Path(__file__).resolve().parent
    with open("model_count.txt", "r") as mc:
        cnt = mc.read()
        models.save_model(
            model, Path(curr_dir, 'Saved Neural Network Models',
                        f'model_{cnt}'))
        joblib.dump(
            pipeline,
            Path(curr_dir, 'Saved Fitted Preprocessing Pipelines',
                 f'pipeline_{cnt}.pkl'))
    with open("model_count.txt", "w") as mc:
        new_cnt = str(int(cnt) + 1)
        mc.write(new_cnt)
예제 #27
0
    def on_epoch_end(self, epoch, logs={}):
        if (epoch + 1) % self.period_of_epochs == 0:
            # Filenames
            baseHDF5Filename = "ModelChkpt{:06d}.hdf5".format(epoch + 1)
            baseYAMLFilename = "ModelChkpt{:06d}.yaml".format(epoch + 1)
            hdf5Filename = os.path.join(self.chkptsdir, baseHDF5Filename)
            yamlFilename = os.path.join(self.chkptsdir, baseYAMLFilename)

            # YAML
            yamlModel = self.model.to_yaml()
            with open(yamlFilename, "w") as yamlFile:
                yamlFile.write(yamlModel)

            # HDF5
            KM.save_model(self.model, hdf5Filename)
            with H.File(hdf5Filename, "r+") as f:
                f.require_dataset("initialEpoch", (), "uint64",
                                  True)[...] = int(epoch + 1)
                f.flush()

            # Symlink to new HDF5 file, then atomically rename and replace.
            os.symlink(baseHDF5Filename, self.linkFilename + ".rename")
            os.rename(self.linkFilename + ".rename", self.linkFilename)

            # Print
            L.getLogger("train").info(
                "Saved checkpoint to {:s} at epoch {:5d}".format(
                    hdf5Filename, epoch + 1))
예제 #28
0
def train_conv_net_cpu(train_data, train_labels, val_data, val_labels,
                       conv_net_hyperparameters, num_processors, seed, dtype="float32", inter_op_threads=2,trial=None):
    """
    Train a convolutional neural network on the CPU.
    """
    np.random.seed(seed)
    if "get_visible_devices" in dir(tf.config.experimental):
        gpus = tf.config.experimental.get_visible_devices("GPU")
    else:
        gpus = tf.config.get_visible_devices("GPU")
    if len(gpus) > 0:
        for device in gpus:
            tf.config.experimental.set_memory_growth(device, True)
    tf.config.threading.set_inter_op_parallelism_threads(inter_op_threads)
    tf.config.threading.set_intra_op_parallelism_threads(num_processors)
    if tf.__version__[0] == "1":
        tf.set_random_seed(seed)
    else:
        tf.random.set_seed(seed)
    K.set_floatx(dtype)
    with tf.device("/CPU:0"):
        scn = ResNet(**conv_net_hyperparameters)
        history = scn.fit(train_data, train_labels, val_x=val_data, val_y=val_labels)
        #scn.fit(train_data, train_labels, val_x=val_data, val_y=val_labels)
        time_str = pd.Timestamp.now().strftime("%d/%m/%Y %I:%M:%S")
        with open('AUC_history.csv','w') as f:
            for key in history.history.keys():
                f.write("%s,%s\n"%(key,history.history[key]))
        epoch_times = scn.time_history.times
        batch_loss = np.array(scn.loss_history.losses).ravel().tolist()
        epoch_loss = np.array(scn.loss_history.val_losses).ravel().tolist()
    date_time = str(datetime.now())
    save_model(scn.model, 'goes16ci_model_cpu' + date_time + '.h5', save_format = 'h5')
    return epoch_times, batch_loss, epoch_loss
예제 #29
0
def main(output, ftpath, corpus, kg_url, cpu):
    """ Train a Classification model

    :param output: output path for the TF model
    :param ftpath: path for Fasttext word embedding model
    :param corpus: path to a text file of documents. One sentence per line.
                    Separate documents with an empty line
    :
    """
    logger.setLevel(logging.DEBUG)
    docs = read_corpus(corpus)

    logger.info("Loading fasttext model")
    ft_model = FastText.load(ftpath)
    kg = get_lemmatized_kg(kg_url)

    if cpu:
        sess = nongpu_session()
    else:
        sess = gpu_session()  # non
    with sess.as_default():
        k_model, loss, accuracy, history = make_classifier(ft_model, docs, kg)

        logger.info("Model trained, saving")
        save_model(k_model, output, overwrite=True, include_optimizer=True)

    return output
예제 #30
0
파일: util.py 프로젝트: raghu330/DLWP-CS
 def __getstate__(self):
     model_str = ""
     with tempfile.NamedTemporaryFile(suffix='.hdf5', delete=True) as fd:
         keras_models.save_model(self, fd.name, overwrite=True)
         model_str = fd.read()
     d = {'model_str': model_str}
     return d
예제 #31
0
파일: model.py 프로젝트: arjo129/uSpeech
def train(model = model(),checkpoint = "latest.hdf5"):
    dataset = build_dataset()
    callback = TensorBoard("_tensor_board")
    callback.set_model(model)
    labels = interested_words
    for i in range(10000):
        save_model(model, "checkpoint.model")
        print("Chunk "+str(i)+" of 10000...")
        X,Y = get_batch(dataset,batchsize=1000)
        for j in range(10):
            logs = model.train_on_batch(np.array(X),np.array(Y))
            print("loss:",logs)
            write_log(callback, ["training loss"], [logs], i*10+j)
        X,Y = get_batch(dataset,batchsize=100,batchtype="test")
        results = model.predict(X)
        accuracy = 0
        for result,actual in zip(results,Y):
            #print("running test")
            x =np.argmax(result)
            j =np.argmax(actual)
            try:
                print("expected "+labels[j]," got "+labels[x])
            except:
                pass
            if x == j: accuracy += 1
        write_log(callback,["test accuracy"],[accuracy],i)
def _run(args):
  """ TODO(praneetdutta): Formalize train sub-routine
  """
  with tf.Session() as sess:
    env = gym.make(args[0].environment)
    env = StackFrameEnv(env, args[0].frame_stack,
     args[0].img_height, args[0].img_width)
    state = env.reset()
    num_actions = env.action_space.n
    if args[0].mode == 'Train':
      # Add timestamp; important for HP tuning so models don't clobber each
      # other.
      model_dir = hp_directory(args[0].model_dir)
    else:
      model_dir = args[0].model_dir
    agent = Agent(
        height=args[0].img_height,
        width=args[0].img_width,
        actions=env.action_space.n,
        channels=args[0].frame_stack,
        model_dir=model_dir,
        discount=args[0].discount_factor)
    agent.create_model(lr=args[0].learning_rate)
    print ("STARTING...")

    if not os.path.exists(model_dir) and args[0].save_model:
      os.makedirs(model_dir)
    print("MODEL WILL BE STORED AT: ", model_dir)
    target_agent = Agent(height=args[0].img_height, width=args[0].img_width,
     actions=env.action_space.n, channels=args[0].frame_stack)
    target_agent.create_model(args[0].learning_rate)
    target_network  = target_agent.model
    target_network.set_weights(agent.model.get_weights())
    if args[0].mode != 'Train':
      trained_model_path = args[0].load_model
      try:
        agent.model.load_weights(trained_model_path)
      except:
        print('{} is not a valid .h5 model.'.format(trained_model_path))
    eta = args[0].init_eta
    Buffer = ExpBuffer(max_size=args[0].buffer_size,
      min_size=args[0].start_train)
    episode_reward, episode_number, done = 0, 0, False
    episode_run = True

    for curr_step in range(args[0].steps):
      if (episode_number % DISPLAY_RESULTS == 0 and
          episode_run) or args[0].mode != 'Train':
        episode_reward = agent.play(env, model_dir, args[0].mode)
        print('CURRENT STEP: {}, EPISODE_NUMBER: {}, EPISODE REWARD: {},'
              'EPSILON: {}'.format(curr_step, episode_number, episode_reward,
                                   eta))
        episode_run = False


      if args[0].mode == 'Train':
        eta = anneal_exploration(eta, curr_step, args[0].steps / 10.0,
                                 args[0].start_train, args[0].init_eta,
                                 args[0].min_eta, 'linear')

        if eta > np.random.rand() or curr_step < args[0].start_train:
          action = env.action_space.sample()
        else:
          action = agent.predict_action(state)

        next_state, reward, done, info = env.step(action)
        Buffer.add_exp([state, next_state, reward, action, done])
        ready_to_update_model = curr_step > args[0].start_train and len(
            Buffer.buffer) > Buffer.min_size
        if ready_to_update_model:
          exp_state, exp_next_state, exp_reward, exp_action, exp_done = Buffer.sample_experiences(
              args[0].batch_size)
          agent.batch_train(exp_state, exp_next_state, exp_reward, exp_action,
                            exp_done, target_network, args[0].Q_learning)
          if curr_step % args[0].update_target == 0:
            target_network.set_weights(agent.model.get_weights())
          if curr_step % (SAVE_STEPS_FACTOR *
                          args[0].update_target) == 0 and args[0].save_model:
            print('SAVING MODEL AT STEP: ', curr_step)
            models.save_model(
                agent.model,
                model_dir + 'model_' + str(episode_number) + '_.h5')
        #Resets state
        if done or args[0].mode != 'Train':
          episode_number += 1
          episode_run = True
          state = env.reset()
        else:
          state = next_state