blob: f771e339363119d96919b2d578bde27bedabf2d6 [file] [log] [blame]
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Caffe2 Basic Concepts - Operators & Nets\n",
"\n",
"In this tutorial we will go through a set of Caffe2 basics: the basic concepts including how operators and nets are being written.\n",
"\n",
"First, let's import caffe2. `core` and `workspace` are usually the two that you need most. If you want to manipulate protocol buffers generated by caffe2, you probably also want to import `caffe2_pb2` from `caffe2.proto`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# We'll also import a few standard python libraries\n",
"from matplotlib import pyplot\n",
"import numpy as np\n",
"import time\n",
"\n",
"# These are the droids you are looking for.\n",
"from caffe2.python import core, workspace\n",
"from caffe2.proto import caffe2_pb2\n",
"\n",
"# Let's show all plots inline.\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You might see a warning saying that caffe2 does not have GPU support. That means you are running a CPU-only build. Don't be alarmed - anything CPU is still runnable without problem."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Workspaces\n",
"\n",
"Let's cover workspaces first, where all the data reside.\n",
"\n",
"If you are familiar with Matlab, workspace consists of blobs you create and store in memory. For now, consider a blob to be a N-dimensional Tensor similar to numpy's ndarray, but is contiguous. Down the road, we will show you that a blob is actually a typed pointer that can store any type of C++ objects, but Tensor is the most common type stored in a blob. Let's show what the interface looks like.\n",
"\n",
"`Blobs()` prints out all existing blobs in the workspace. \n",
"`HasBlob()` queries if a blob exists in the workspace. For now, we don't have anything yet."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Current blobs in the workspace: {}\".format(workspace.Blobs()))\n",
"print(\"Workspace has blob 'X'? {}\".format(workspace.HasBlob(\"X\")))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can feed blobs into the workspace using `FeedBlob()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X = np.random.randn(2, 3).astype(np.float32)\n",
"print(\"Generated X from numpy:\\n{}\".format(X))\n",
"workspace.FeedBlob(\"X\", X)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, let's take a look what blobs there are in the workspace."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"print(\"Current blobs in the workspace: {}\".format(workspace.Blobs()))\n",
"print(\"Workspace has blob 'X'? {}\".format(workspace.HasBlob(\"X\")))\n",
"print(\"Fetched X:\\n{}\".format(workspace.FetchBlob(\"X\")))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's verify that the arrays are equal."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"np.testing.assert_array_equal(X, workspace.FetchBlob(\"X\"))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also, if you are trying to access a blob that does not exist, an error will be thrown:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" workspace.FetchBlob(\"invincible_pink_unicorn\")\n",
"except RuntimeError as err:\n",
" print(err)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One thing that you might not use immediately: you can have multiple workspaces in Python using different names, and switch between them. Blobs in different workspaces are separate from each other. You can query the current workspace using `CurrentWorkspace`. Let's try switching the workspace by name (gutentag) and creating a new one if it doesn't exist."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Current workspace: {}\".format(workspace.CurrentWorkspace()))\n",
"print(\"Current blobs in the workspace: {}\".format(workspace.Blobs()))\n",
"\n",
"# Switch the workspace. The second argument \"True\" means creating \n",
"# the workspace if it is missing.\n",
"workspace.SwitchWorkspace(\"gutentag\", True)\n",
"\n",
"# Let's print the current workspace. Note that there is nothing in the\n",
"# workspace yet.\n",
"print(\"Current workspace: {}\".format(workspace.CurrentWorkspace()))\n",
"print(\"Current blobs in the workspace: {}\".format(workspace.Blobs()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's switch back to the default workspace."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"workspace.SwitchWorkspace(\"default\")\n",
"print(\"Current workspace: {}\".format(workspace.CurrentWorkspace()))\n",
"print(\"Current blobs in the workspace: {}\".format(workspace.Blobs()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, `ResetWorkspace()` clears anything that is in the current workspace."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"workspace.ResetWorkspace()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Operators\n",
"\n",
"Operators in Caffe2 are kind of like functions. From the C++ side, they all derive from a common interface, and are registered by type, so that we can call different operators during runtime. The interface of operators is defined in `caffe2/proto/caffe2.proto`. Basically, it takes in a bunch of inputs, and produces a bunch of outputs.\n",
"\n",
"Remember, when we say \"create an operator\" in Caffe2 Python, nothing gets run yet. All it does is to create the protocol buffere that specifies what the operator should be. At a later time it will be sent to the C++ backend for execution. If you are not familiar with protobuf, it is a json-like serialization tool for structured data. Find more about protocol buffers [here](https://developers.google.com/protocol-buffers/).\n",
"\n",
"Let's see an actual example."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create an operator.\n",
"op = core.CreateOperator(\n",
" \"Relu\", # The type of operator that we want to run\n",
" [\"X\"], # A list of input blobs by their names\n",
" [\"Y\"], # A list of output blobs by their names\n",
")\n",
"# and we are done!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As we mentioned, the created op is actually a protobuf object. Let's show the content."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"print(\"Type of the created op is: {}\".format(type(op)))\n",
"print(\"Content:\\n\")\n",
"print(str(op))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"OK, let's run the operator. We first feed in the input X to the workspace. \n",
"Then the simplest way to run an operator is to do `workspace.RunOperatorOnce(operator)`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"workspace.FeedBlob(\"X\", np.random.randn(2, 3).astype(np.float32))\n",
"workspace.RunOperatorOnce(op)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After execution, let's see if the operator is doing the right thing, which is our neural network's activation function ([Relu](https://en.wikipedia.org/wiki/Rectifier_(neural_networks))) in this case."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Current blobs in the workspace: {}\\n\".format(workspace.Blobs()))\n",
"print(\"X:\\n{}\\n\".format(workspace.FetchBlob(\"X\")))\n",
"print(\"Y:\\n{}\\n\".format(workspace.FetchBlob(\"Y\")))\n",
"print(\"Expected:\\n{}\\n\".format(np.maximum(workspace.FetchBlob(\"X\"), 0)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is working if your Expected output matches your Y output in this example.\n",
"\n",
"Operators also take optional arguments if needed. They are specified as key-value pairs. Let's take a look at one simple example, which takes a tensor and fills it with Gaussian random variables."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"op = core.CreateOperator(\n",
" \"GaussianFill\",\n",
" [], # GaussianFill does not need any parameters.\n",
" [\"Z\"],\n",
" shape=[100, 100], # shape argument as a list of ints.\n",
" mean=1.0, # mean as a single float\n",
" std=1.0, # std as a single float\n",
")\n",
"print(\"Content of op:\\n\")\n",
"print(str(op))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's run it and see if things are as intended."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"workspace.RunOperatorOnce(op)\n",
"temp = workspace.FetchBlob(\"Z\")\n",
"pyplot.hist(temp.flatten(), bins=50)\n",
"pyplot.title(\"Distribution of Z\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you see a bell shaped curve then it worked!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Nets\n",
"\n",
"Nets are essentially computation graphs. We keep the name `Net` for backward consistency (and also to pay tribute to neural nets). A Net is composed of multiple operators just like a program written as a sequence of commands. Let's take a look.\n",
"\n",
"When we talk about nets, we will also talk about BlobReference, which is an object that wraps around a string so we can do easy chaining of operators.\n",
"\n",
"Let's create a network that is essentially the equivalent of the following python math:\n",
"```\n",
"X = np.random.randn(2, 3)\n",
"W = np.random.randn(5, 3)\n",
"b = np.ones(5)\n",
"Y = X * W^T + b\n",
"```\n",
"We'll show the progress step by step. Caffe2's `core.Net` is a wrapper class around a NetDef protocol buffer."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When creating a network, its underlying protocol buffer is essentially empty other than the network name. Let's create the net and then show the proto content."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"net = core.Net(\"my_first_net\")\n",
"print(\"Current network proto:\\n\\n{}\".format(net.Proto()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's create a blob called X, and use GaussianFill to fill it with some random data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"X = net.GaussianFill([], [\"X\"], mean=0.0, std=1.0, shape=[2, 3], run_once=0)\n",
"print(\"New network proto:\\n\\n{}\".format(net.Proto()))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"You might have observed a few differences from the earlier `core.CreateOperator` call. Basically, when we have a net, you can direct create an operator *and* add it to the net at the same time using Python tricks: essentially, if you call `net.SomeOp` where SomeOp is a registered type string of an operator, this essentially gets translated to\n",
"```\n",
"op = core.CreateOperator(\"SomeOp\", ...)\n",
"net.Proto().op.append(op)\n",
"```\n",
"\n",
"Also, you might be wondering what X is. X is a `BlobReference` which basically records two things:\n",
"- what its name is. You can access the name by str(X)\n",
"- which net it gets created from. It is recorded by an internal variable `_from_net`, but most likely\n",
"you won't need that.\n",
"\n",
"Let's verify it. Also, remember, we are not actually running anything yet, so X contains nothing but a symbol. Don't expect to get any numerical values out of it right now :)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"print(\"Type of X is: {}\".format(type(X)))\n",
"print(\"The blob name is: {}\".format(str(X)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's continue to create W and b."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"W = net.GaussianFill([], [\"W\"], mean=0.0, std=1.0, shape=[5, 3], run_once=0)\n",
"b = net.ConstantFill([], [\"b\"], shape=[5,], value=1.0, run_once=0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, one simple code sugar: since the BlobReference objects know what net it is generated from, in addition to creating operators from net, you can also create operators from BlobReferences. Let's create the FC operator in this way."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Y = X.FC([W, b], [\"Y\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Under the hood, `X.FC(...)` simply delegates to `net.FC` by inserting `X` as the first input of the corresponding operator, so what we did above is equivalent to\n",
"```\n",
"Y = net.FC([X, W, b], [\"Y\"])\n",
"```\n",
"\n",
"Let's take a look at the current network."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Current network proto:\\n\\n{}\".format(net.Proto()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Too verbose huh? Let's try to visualize it as a graph. Caffe2 ships with a very minimal graph visualization tool for this purpose. Let's show that in ipython."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from caffe2.python import net_drawer\n",
"from IPython import display\n",
"graph = net_drawer.GetPydotGraph(net, rankdir=\"LR\")\n",
"display.Image(graph.create_png(), width=800)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So we have defined a `Net`, but nothing gets executed yet. Remember that the net above is essentially a protobuf that holds the definition of the network. When we actually want to run the network, what happens under the hood is:\n",
"- Instantiate a C++ net object from the protobuf;\n",
"- Call the instantiated net's Run() function.\n",
"\n",
"Before we do anything, we should clear any earlier workspace variables with `ResetWorkspace()`.\n",
"\n",
"Then there are two ways to run a net from Python. We will do the first option in the example below.\n",
"\n",
"1. Using `workspace.RunNetOnce()`, which instantiates, runs and immediately destructs the network. \n",
"2. A little bit more complex and involves two steps: \n",
" (a) call `workspace.CreateNet()` to create the C++ net object owned by the workspace, and\n",
" (b) use `workspace.RunNet()` by passing the name of the network to it.\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"workspace.ResetWorkspace()\n",
"print(\"Current blobs in the workspace: {}\".format(workspace.Blobs()))\n",
"workspace.RunNetOnce(net)\n",
"print(\"Blobs in the workspace after execution: {}\".format(workspace.Blobs()))\n",
"# Let's dump the contents of the blobs\n",
"for name in workspace.Blobs():\n",
" print(\"{}:\\n{}\".format(name, workspace.FetchBlob(name)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's try the second way to create the net, and run it. First clear the variables with `ResetWorkspace()`, create the net with the workspace's net object you created earlier `CreateNet(net_object)`, and then run the net by name with `RunNet(net_name)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"workspace.ResetWorkspace()\n",
"print(\"Current blobs in the workspace: {}\".format(workspace.Blobs()))\n",
"workspace.CreateNet(net)\n",
"workspace.RunNet(net.Proto().name)\n",
"print(\"Blobs in the workspace after execution: {}\".format(workspace.Blobs()))\n",
"for name in workspace.Blobs():\n",
" print(\"{}:\\n{}\".format(name, workspace.FetchBlob(name)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There are a few differences between `RunNetOnce` and `RunNet`, but probably the main difference is the computation time overhead. Since `RunNetOnce` involves serializing the protobuf to pass between Python and C and instantiating the network, it may take longer to run. Let's see in this case what the overhead is."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# It seems that %timeit magic does not work well with\n",
"# C++ extensions so we'll basically do for loops\n",
"start = time.time()\n",
"for i in range(1000):\n",
" workspace.RunNetOnce(net)\n",
"end = time.time()\n",
"print('Run time per RunNetOnce: {}'.format((end - start) / 1000))\n",
"\n",
"start = time.time()\n",
"for i in range(1000):\n",
" workspace.RunNet(net.Proto().name)\n",
"end = time.time()\n",
"print('Run time per RunNet: {}'.format((end - start) / 1000))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"OK, so above are a few key components if you would like to use Caffe2 from the python side. We are going to add more to the tutorial as we find more needs. For now, kindly check out the rest of the tutorials!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "KERNEL_NAME"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 1
}