text:
"""Utilities to save models."""

from __future__ import absolute_import
from __future__ import division
from __future__ import REDACTED
from __future__ import print_function

import os

import REDACTED
from absl import logging
import tensorflow.compat.v2 as tf
import typing

def export_bert_model(model_export_path: typing.Text,
                      model: tf.keras.Model,
                      checkpoint_dir: typing.Optional[typing.Text] = None,
                      restore_model_using_load_weights: bool = False) -> None:
  """Export BERT model for serving which does not include the optimizer.

  Arguments:
      model_export_path: Path to which exported model will be saved.
      model: Keras model object to export.
      checkpoint_dir: Path from which model weights will be loaded, if
        specified.
      restore_model_using_load_weights: Whether to use checkpoint.restore() API
        for custom checkpoint or to use model.load_weights() API.
        There are 2 different ways to save checkpoints. One is using
        tf.train.Checkpoint and another is using Keras model.save_weights().
        Custom training loop implementation uses tf.train.Checkpoint API
        and Keras ModelCheckpoint callback internally uses model.save_weights()
        API. Since these two API's cannot be used toghether, model loading logic
        must be take into account how model checkpoint was saved.

  Raises:
    ValueError when either model_export_path or model is not specified.
  """
  if not model_export_path:
    raise ValueError('model_export_path must be specified.')
  if not isinstance(model, tf.keras.Model):
    raise ValueError('model must be a tf.keras.Model object.')

  if checkpoint_dir:
    # Keras compile/fit() was used to save checkpoint using
    # model.save_weights().
    if restore_model_using_load_weights:
      model_weight_path = os.path.join(checkpoint_dir, 'checkpoint')
      assert tf.io.gfile.exists(model_weight_path)
      model.load_weights(model_weight_path)

    # tf.train.Checkpoint API was used via custom training loop logic.
    else:
      checkpoint = tf.train.Checkpoint(model=model)

      # Restores the model from latest checkpoint.
      latest_checkpoint_file = tf.train.latest_checkpoint(checkpoint_dir)
      assert latest_checkpoint_file
      logging.info('Checkpoint file %s found and restoring from '
                   'checkpoint', latest_checkpoint_file)
      checkpoint.restore(
          latest_checkpoint_file).assert_existing_objects_matched()

  model.save(model_export_path, include_optimizer=False, save_format='tf')

class BertModelCheckpoint(tf.keras.callbacks.Callback):
  """Keras callback that saves model at the end of every epoch."""

  def __init__(self, checkpoint_dir, checkpoint):
    """Initializes BertModelCheckpoint.

    Arguments:
      checkpoint_dir: Directory of the to be saved checkpoint file.
      checkpoint: tf.train.Checkpoint object.
    """
    super(BertModelCheckpoint, self).__init__()
    self.checkpoint_file_name = os.path.join(
        checkpoint_dir, 'bert_training_checkpoint_step_{global_step}.ckpt')
    assert isinstance(checkpoint, tf.train.Checkpoint)
    self.checkpoint = checkpoint

  def on_epoch_end(self, epoch, logs=None):
    global_step = tf.keras.backend.get_value(self.model.optimizer.iterations)
    formatted_file_name = self.checkpoint_file_name.format(
        global_step=global_step)
    saved_path = self.checkpoint.save(formatted_file_name)
    logging.info('Saving model TF checkpoint to : %s', saved_path)


First day back at work, and it was incredibly productive. Six new campaigns launched and two training sessions for our new VP of federal sales. All in all, I was rather pleased.
Michelle and I decided that tonight should be a nice-light dinner, watching Deadliest Catch and Project Runway. For some reason it never occurs to me to do a crab boil on Deadliest Catch night. I'll have to remedy that next season. For something light, I decided against beef, and figured a nice salad would do. However, as easy as a mixed green salad with balsamic is, I figured that it wasn't much more work to actually make something a bit more appealing.
After a swing by Cub on the way home to pick up some chicken, I ended up with a quart of strawberries (they were by the front door, I couldn't resist) as well.
Knowing that I had a bottle of Archer Farms Raspberry-Chipotle grilling sauce in the fridge, I decided to put it to good use along with the strawberries and a dash of Annie's Raspberry Vinaigrette.
Deciding to use chicken strips instead of whole breasts meant that I had to use some foil on the grill, else I would have chicken strip charcoal briquettes dangling between the grill slats. That's not a pleasing thought at all.
Actually, this worked rather nicely, as the browned more evenly, and in the final few minutes of cooking, when brushed with the glaze, they were able to simmer in the drippings rather than the glaze simply charring.
Next step was to pull the chicken and slice the strawberries. So far, we're looking at around 12 minutes from prep to completion at this point, so I'm relatively happy.
Then sprinkled with raw sunflower kernels for good measure before the chicken is placed.
And voila, mixed baby greens with raspberry-chipotle glazed chicken, strawberries, sunflower seeds and raspberry vinaigrette.
We were happy with this dish. Next time, however, we want to try walnuts instead of sunflower kernels and maybe add buffalo mozzarella or Gorgonzola as well.


Wheat growers across southern and central England are responding to the good autumn weather by delaying drilling as long as they dare to combat blackgrass.
Many have held back longer than last autumn in a bid to beat the arable industry's most damaging weed, and have been helped by mild and moist conditions.
These have allowed blackgrass to emerge quickly and then be killed off with glyphosate herbicide before autumn wheat drilling starts.
John Hall, a director at farming group Sentry, said good flushes of blackgrass have allowed them to hold off an extra week with autumn drilling.
"On land with a bad blackgrass problem we have only started drilling this week. It is a conscious decision helped by the season," he told Farmers Weekly.
Areas of heavy land that the group farms in Cambridgeshire and Leicestershire have seen good flushes of blackgrass, then have been sprayed off and sown this week with a direct drill, to minimise any soil disturbance.
"This year we have certainly been helped by a good spell of weather, so we have being drilling about a week later," he said.
On lighter land with no blackgrass burden, the group started drilling wheat on 25 September.
Cambridgeshire grower James Peck is busy drilling wheat this week after a late start, but with a watchful eye on the weather.
"We started drilling wheat on 7 October, whereas five to 10 years ago we would have been finished by 1 October," he said.
At the start of this week he had drilled about 70% of his 600ha of wheat in near perfect conditions, and with continuing good weather he should be finished by the end of the week.
"It is a very risky strategy and we are super-anxious about the weather," he added. Mr Peck farms about 2,400ha of land from his base at Scotland Farm, Dry Drayton, just west of Cambridge.
He said a rotation with plenty of spring cropping is key to controlling blackgrass, as bad weather can often hold up the autumn routine of spraying off the weed, drilling and applying pre-emergence herbicides.
Further east in Cambridgeshire, farm manager George Palmer is leaving wheat drilling as late as early November on bad blackgrass land, and only started drilling wheat on 2 October this season.
"Any field with blackgrass we leave drilling as late as possible, usually until early November, and last year we did not see a huge yield disadvantage," he said.
Seed rates are increased 10-15% for the late drillings and a careful watch is kept on the weather. Earlier this week, half of his 600ha of wheat had been drilled.
Mr Palmer manages 2,000ha of arable land varying from sandy loams to fenland at Northfield Farms just south-east of Ely, on the edge of the Cambridgeshire Fens.
Lincolnshire farm manager Chris Baylis moved to drilling wheat in November 2016 on his worst blackgrass-infested land near the River Humber with very good results.
"Having just walked the land and seen the second flush of blackgrass coming through we know it is the right thing to do," said Mr Baylis, who is Farmers Weekly's farm manager of the year 2017.
All fields on the 3,600ha estate are assessed for blackgrass, and last season he moved to drilling wheat on his most blackgrass-infected land from 20 October to early November.
"We have had very good blackgrass control with this approach using high seed rates and vigorous varieties such as Crispin and Santiago," he said from his base at Hall Farm, Stainton le Vale in north Lincolnshire.