"To use TF-Slim for image classification (as we do in this notebook), you also have to install the TF-Slim image models library from [here](https://github.com/tensorflow/models/tree/master/slim). Let's suppose you install this into a directory called TF_MODELS. Then you should change directory to TF_MODELS/slim **before** running this notebook, so that all the files are on the path.\n",
"To use TF-Slim for image classification (as we do in this notebook), you also have to install the TF-Slim image models library from [here](https://github.com/tensorflow/models/tree/master/slim). Let's suppose you install this into a directory called TF_MODELS. Then you should change directory to TF_MODELS/slim **before** running this notebook, so that these files are in your python path.\n",
"\n",
"\n",
"To check you've got these two steps to work, just execute the cell below. It it complains about unknown modules, restart the notebook after moving to the TF-Slim models directory.\n"
"To check you've got these two steps to work, just execute the cell below. If it complains about unknown modules, restart the notebook after moving to the TF-Slim models directory.\n"
"- Occasionally store the model checkpoint in the specified directory. This is useful in case your machine crashes - then you can simply restart from the specified checkpoint."
"- Occasionally store the model checkpoint in the specified directory. This is useful in case your machine crashes - then you can simply restart from the specified checkpoint."
]
]
},
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Everytime we run training, we need to store the model checkpoint in a new directory,\n",
"# in case anything has changed.\n",
"import time\n",
"ts = time.time()\n",
"ckpt_dir = '/tmp/tf/regression_model/model{}'.format(ts) # Place to store the checkpoint.\n",
"print('Saving to {}'.format(ckpt_dir))"
]
},
{
{
"cell_type": "code",
"cell_type": "code",
"execution_count": null,
"execution_count": null,
...
@@ -233,11 +216,12 @@
...
@@ -233,11 +216,12 @@
"outputs": [],
"outputs": [],
"source": [
"source": [
"def convert_data_to_tensors(x, y):\n",
"def convert_data_to_tensors(x, y):\n",
" input_tensor = tf.constant(x)\n",
" inputs = tf.constant(x)\n",
" input_tensor.set_shape([None, 1])\n",
" inputs.set_shape([None, 1])\n",
" output_tensor = tf.constant(y)\n",
" \n",
" output_tensor.set_shape([None, 1])\n",
" outputs = tf.constant(y)\n",
" return input_tensor, output_tensor"
" outputs.set_shape([None, 1])\n",
" return inputs, outputs"
]
]
},
},
{
{
...
@@ -248,32 +232,34 @@
...
@@ -248,32 +232,34 @@
},
},
"outputs": [],
"outputs": [],
"source": [
"source": [
"graph = tf.Graph() # new graph\n",
"# The following snippet trains the regression model using a sum_of_squares loss.\n",
"### Let's compute various evaluation metrics on the test set.\n",
"### Let's compute various evaluation metrics on the test set.\n",
"\n",
"\n",
"In slim termiology, losses are optimized, but metrics (which may not be differentiable, e.g., precision and recall) are just measured.\n",
"In TF-Slim termiology, losses are optimized, but metrics (which may not be differentiable, e.g., precision and recall) are just measured. As an illustration, the code below computes mean squared error and mean absolute error metrics on the test set.\n",
"As an illustration, the code below computes mean squared error and mean absolute error metrics on the test set.\n",
"\n",
"\n",
"Each metric declaration creates several local variables (which must be initialized via tf.initialize_local_variables()) and returns both a value_op and an update_op. When evaluated, the value_op returns the current value of the metric. The update_op loads a new batch of data, runs the model, obtains the predictions and accumulates the metric statistics appropriately before returning the current value of the metric. We store these value nodes and update nodes in 2 dictionaries.\n",
"Each metric declaration creates several local variables (which must be initialized via tf.initialize_local_variables()) and returns both a value_op and an update_op. When evaluated, the value_op returns the current value of the metric. The update_op loads a new batch of data, runs the model, obtains the predictions and accumulates the metric statistics appropriately before returning the current value of the metric. We store these value nodes and update nodes in 2 dictionaries.\n",
"However, this means they must be trained on big datasets. Since this process is slow, we provide various pre-trained models - see the list [here](https://github.com/tensorflow/models/tree/master/slim#pre-trained-models).\n",
"However, this means they must be trained on big datasets. Since this process is slow, we provide various pre-trained models - see the list [here](https://github.com/tensorflow/models/tree/master/slim#pre-trained-models).\n",
"\n",
"\n",
"\n",
"\n",
"You can either use these models as-is, or you can perform \"surgery\" on them, to modify them for some other task. For example, it is common to \"chop off\" the final pre-softmax layer, and replace it with a new set of weights corresponding to some new set of labels. You can then quickly fine tune the new model on a small new dataset. We illustrate this below, using inception-v3 as the base model.\n"
"You can either use these models as-is, or you can perform \"surgery\" on them, to modify them for some other task. For example, it is common to \"chop off\" the final pre-softmax layer, and replace it with a new set of weights corresponding to some new set of labels. You can then quickly fine tune the new model on a small new dataset. We illustrate this below, using inception-v1 as the base model. While models like Inception V3 are more powerful, Inception V1 is used for speed purposes.\n"