Пример #1
0
def main(train_files,
         eval_files,
         job_dir,
         epochs,
         batch_size,
         num_classes):
  """Main program"""
  (x_train, y_train), (x_test, y_test) = utils.load_data(train_files, eval_files)
  if K.image_data_format() == 'channes_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
  else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

  x_train = x_train.astype('float32')
  x_test = x_test.astype('float32')
  x_train /= 255
  x_test /= 255

  y_train = keras.utils.to_categorical(y_train, num_classes)
  y_test = keras.utils.to_categorical(y_test, num_classes)
  mnist_model = model.build_model(input_shape, num_classes)
  mnist_model.fit(x_train, y_train,
                  batch_size=batch_size,
                  epochs=epochs,
                  verbose=1,
                  validation_data=(x_test, y_test))

  # Convert the Keras model to TensorFlow SavedModel
  model.to_savedmodel(mnist_model, os.path.join(job_dir, 'export'))
Пример #2
0
def dispatch(train_files, eval_files, job_dir, train_steps, eval_steps,
             train_batch_size, eval_batch_size, learning_rate, eval_frequency,
             first_layer_size, num_layers, scale_factor, eval_num_epochs,
             num_epochs, checkpoint_epochs):
    census_model = model.model_fn(INPUT_SIZE, CLASS_SIZE)

    try:
        os.makedirs(job_dir)
    except:
        pass

    # Unhappy hack to work around h5py not being able to write to GCS.
    # Force snapshots and saves to local filesystem, then copy them over to GCS.
    checkpoint_path = FILE_PATH
    if not job_dir.startswith("gs://"):
        checkpoint_path = os.path.join(job_dir, checkpoint_path)

    # Model checkpoint callback
    checkpoint = keras.callbacks.ModelCheckpoint(checkpoint_path,
                                                 monitor='val_loss',
                                                 verbose=1,
                                                 period=checkpoint_epochs,
                                                 mode='max')

    # Continuous eval callback
    evaluation = ContinuousEval(eval_frequency,
                                eval_files,
                                learning_rate,
                                job_dir,
                                steps=train_steps)

    # Tensorboard logs callback
    tblog = keras.callbacks.TensorBoard(log_dir=os.path.join(job_dir, 'logs'),
                                        histogram_freq=0,
                                        write_graph=True,
                                        embeddings_freq=0)

    callbacks = [checkpoint, evaluation, tblog]

    census_model.fit_generator(model.generator_input(train_files,
                                                     chunk_size=CHUNK_SIZE),
                               steps_per_epoch=train_steps,
                               epochs=num_epochs,
                               callbacks=callbacks)

    # Unhappy hack to work around h5py not being able to write to GCS.
    # Force snapshots and saves to local filesystem, then copy them over to GCS.
    if job_dir.startswith("gs://"):
        census_model.save(CENSUS_MODEL)
        copy_file_to_gcs(job_dir, CENSUS_MODEL)
    else:
        census_model.save(os.path.join(job_dir, CENSUS_MODEL))

    # Convert the Keras model to TensorFlow SavedModel
    model.to_savedmodel(census_model, os.path.join(job_dir, 'export'))
Пример #3
0
def dispatch(train_files, eval_files, job_dir, train_steps, eval_steps,
             train_batch_size, eval_batch_size, learning_rate, eval_frequency,
             first_layer_size, num_layers, scale_factor, eval_num_epochs,
             num_epochs, checkpoint_epochs):
    census_model = model.model_fn(INPUT_SIZE, CLASS_SIZE)

    try:
        os.makedirs(job_dir)
    except:
        pass

    # Model checkpoint callback
    checkpoint = keras.callbacks.ModelCheckpoint(os.path.join(
        job_dir, FILE_PATH),
                                                 monitor='val_loss',
                                                 verbose=1,
                                                 period=checkpoint_epochs,
                                                 mode='max')

    # Continuous eval callback
    evaluation = ContinuousEval(eval_frequency, eval_files, learning_rate,
                                job_dir)

    # Tensorboard logs callback
    tblog = keras.callbacks.TensorBoard(log_dir=os.path.join(job_dir, 'logs'),
                                        histogram_freq=0,
                                        write_graph=True,
                                        embeddings_freq=0)

    #TODO: This needs to be fixed in h5py so that writes to GCS are possible
    # Don't attempt to create checkpoints on Cloud ML Engine for now because
    # h5py doesn't come with native GCS write capability
    if job_dir.startswith('gs://'):
        callbacks = [evaluation, tblog]
    else:
        callbacks = [checkpoint, evaluation, tblog]

    census_model.fit_generator(model.generator_input(train_files,
                                                     chunk_size=CHUNK_SIZE),
                               steps_per_epoch=train_steps,
                               epochs=num_epochs,
                               callbacks=callbacks)

    census_model.save(os.path.join(job_dir, CENSUS_MODEL))

    # Convert the Keras model to TensorFlow SavedModel
    model.to_savedmodel(census_model, os.path.join(job_dir, 'export'))
def dispatch(data_file, job_dir, num_epochs):

    timestamp = str(time.time())
    job_dir = job_dir + "/run" + timestamp

    x_train, y_train, maxlen, nb_chars, char_indices, indices_char = model.get_training_data(
        data_file[0])

    embedding_dims = 128
    filters = 250
    kernel_size = 3
    hidden_dims = 8
    batch_size = 32
    dropout_rate = 0.3
    epochs = num_epochs

    sentiment_model = model.model_fn(nb_chars=nb_chars,
                                     maxlen=maxlen,
                                     embedding_dims=embedding_dims,
                                     hidden_dims=hidden_dims,
                                     dropout_rate=dropout_rate,
                                     filters=filters,
                                     kernel_size=kernel_size)

    try:
        os.makedirs(job_dir)
    except:
        pass

    # Unhappy hack to work around h5py not being able to write to GCS.
    # Force snapshots and saves to local filesystem, then copy them over to GCS.
    checkpoint_path = FILE_PATH
    if not job_dir.startswith("gs://"):
        checkpoint_path = os.path.join(job_dir, checkpoint_path)

    # Model checkpoint callback
    checkpoint = keras.callbacks.ModelCheckpoint(checkpoint_path,
                                                 monitor='val_loss',
                                                 verbose=1,
                                                 save_best_only=True,
                                                 mode='min')

    timestamp = str(time.time())

    # Tensorboard logs callback
    tblog = keras.callbacks.TensorBoard(log_dir=os.path.join(job_dir, 'logs'),
                                        write_graph=True,
                                        embeddings_freq=0)

    callbacks = [checkpoint, tblog]

    sentiment_model = model.compile_model(sentiment_model)
    sentiment_model.fit(x_train,
                        y_train,
                        epochs=epochs,
                        validation_split=0.3,
                        batch_size=batch_size,
                        callbacks=callbacks)

    # Unhappy hack to work around h5py not being able to write to GCS.
    # Force snapshots and saves to local filesystem, then copy them over to GCS.
    if job_dir.startswith("gs://"):
        sentiment_model.save(SENTIMENT_MODEL)
        copy_file_to_gcs(job_dir, SENTIMENT_MODEL)
    else:
        sentiment_model.save(os.path.join(job_dir, SENTIMENT_MODEL))

    # Convert the Keras model to TensorFlow SavedModel
    model.to_savedmodel(sentiment_model, os.path.join(job_dir, 'export'))