diff --git a/python/basic/advanced_img_io.ipynb b/python/basic/advanced_img_io.ipynb index ac9405a4c..9b1ed42d8 100644 --- a/python/basic/advanced_img_io.ipynb +++ b/python/basic/advanced_img_io.ipynb @@ -31,10 +31,10 @@ "import time\n", "# set the number of threads you want to use before importing mxnet\n", "os.environ['MXNET_CPU_WORKER_NTHREADS'] = '4'\n", - "import mxnet as mx\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", - "import cv2" + "import cv2\n", + "import mxnet as mx" ] }, { @@ -97,6 +97,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "# opencv\n", "N = 1000\n", "tic = time.time()\n", @@ -133,6 +134,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "# mx.image\n", "tic = time.time()\n", "for i in range(N):\n", @@ -225,6 +227,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "# crop a random w x h region from image\n", "tmp, coord = mx.image.random_crop(img, (150, 200))\n", "print(coord)\n", @@ -273,7 +276,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.12" + "version": "2.7.6" } }, "nbformat": 4, diff --git a/python/basic/image_io.ipynb b/python/basic/image_io.ipynb index ffdb867a5..1d6aaa76c 100644 --- a/python/basic/image_io.ipynb +++ b/python/basic/image_io.ipynb @@ -37,12 +37,12 @@ "%matplotlib inline\n", "import os\n", "import subprocess\n", - "import mxnet as mx\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", + "import mxnet as mx\n", "\n", "# change this to your mxnet location\n", - "MXNET_HOME = '/scratch/mxnet'" + "MXNET_HOME = '/mxnet'" ] }, { @@ -165,7 +165,7 @@ ], "source": [ "data_iter = mx.io.ImageRecordIter(\n", - " path_imgrec=\"./data/caltech_train.rec\", # the target record file\n", + " path_imgrec=\"./data/caltech.rec\", # the target record file\n", " data_shape=(3, 227, 227), # output data shape. An 227x227 region will be cropped from the original image.\n", " batch_size=4, # number of samples per batch\n", " resize=256 # resize the shorter edge to 256 before cropping\n", diff --git a/python/basic/mixed.ipynb b/python/basic/mixed.ipynb index cd6f35c7c..f23c1b80a 100644 --- a/python/basic/mixed.ipynb +++ b/python/basic/mixed.ipynb @@ -821,7 +821,6 @@ } ], "metadata": { - "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 2", "language": "python", diff --git a/python/basic/ndarray.ipynb b/python/basic/ndarray.ipynb index 50cd76a81..9d29a5524 100644 --- a/python/basic/ndarray.ipynb +++ b/python/basic/ndarray.ipynb @@ -765,6 +765,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "def f():\n", " a = mx.nd.ones((100,100))\n", " b = mx.nd.ones((100,100))\n", @@ -833,6 +834,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "a = mx.nd.ones((100,100), mx.cpu())\n", "b = mx.nd.ones((100,100), mx.gpu())\n", "c = mx.nd.ones((100,100), mx.gpu())\n", @@ -907,6 +909,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "a = mx.nd.ones((2,3))\n", "b = mx.nd.ones((5,6)) \n", "mx.nd.save(\"temp.ndarray\", [a,b])\n", @@ -940,6 +943,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "d = {'a':a, 'b':b}\n", "mx.nd.save(\"temp.ndarray\", d)\n", "c = mx.nd.load(\"temp.ndarray\")\n", @@ -1121,7 +1125,6 @@ } ], "metadata": { - "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 2", "language": "python", diff --git a/python/basic/record_io.ipynb b/python/basic/record_io.ipynb index 957f58a4e..f923b18eb 100644 --- a/python/basic/record_io.ipynb +++ b/python/basic/record_io.ipynb @@ -21,9 +21,9 @@ "source": [ "%matplotlib inline\n", "from __future__ import print_function\n", - "import mxnet as mx\n", "import numpy as np\n", - "import matplotlib.pyplot as plt" + "import matplotlib.pyplot as plt\n", + "import mxnet as mx" ] }, { @@ -82,7 +82,7 @@ " item = record.read()\n", " if not item:\n", " break\n", - " print item\n", + " print (item)\n", "record.close()" ] }, diff --git a/python/basic/symbol.ipynb b/python/basic/symbol.ipynb index bf2396a85..52c792f50 100644 --- a/python/basic/symbol.ipynb +++ b/python/basic/symbol.ipynb @@ -271,7 +271,7 @@ } ], "source": [ - "# Output may vary\n", + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "net = mx.sym.Variable('data')\n", "net = mx.sym.FullyConnected(data=net, name='fc1', num_hidden=128)\n", "net = mx.sym.Activation(data=net, name='relu1', act_type=\"relu\")\n", @@ -402,7 +402,7 @@ } ], "source": [ - "# Output may vary\n", + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "def ConvFactory(data, num_filter, kernel, stride=(1,1), pad=(0, 0), name=None, suffix=''):\n", " conv = mx.symbol.Convolution(data=data, num_filter=num_filter, kernel=kernel, stride=stride, pad=pad, name='conv_%s%s' %(name, suffix))\n", " bn = mx.symbol.BatchNorm(data=conv, name='bn_%s%s' %(name, suffix))\n", @@ -1478,7 +1478,6 @@ } ], "metadata": { - "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 2", "language": "python", diff --git a/python/cnn/mnist.ipynb b/python/cnn/mnist.ipynb index f1d4cca9d..8b55aa365 100644 --- a/python/cnn/mnist.ipynb +++ b/python/cnn/mnist.ipynb @@ -374,6 +374,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "model = mx.model.FeedForward(\n", " ctx = mx.gpu(0), # Run on GPU 0\n", " symbol = mlp, # Use the network we just defined\n", @@ -384,7 +385,9 @@ "model.fit(\n", " X=train_iter, # Training data set\n", " eval_data=test_iter, # Testing data set. MXNet computes scores on test set every epoch\n", - " batch_end_callback = mx.callback.Speedometer(batch_size, 200)) # Logging module to print out progress" + " batch_end_callback = mx.callback.Speedometer(batch_size, 200)) # Logging module to print out progress\n", + "assert model.score(train_iter) > 0.98, \"Low training accuracy.\"\n", + "assert model.score(test_iter) > 0.97, \"Low validation accuracy.\"" ] }, { @@ -423,6 +426,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "plt.imshow((X_test[0].reshape((28,28))*255).astype(np.uint8), cmap='Greys_r')\n", "plt.show()\n", "print 'Result:', model.predict(X_test[0:1])[0].argmax()" @@ -452,7 +456,9 @@ } ], "source": [ - "print 'Accuracy:', model.score(test_iter)*100, '%'" + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", + "print 'Accuracy:', model.score(test_iter)*100, '%'\n", + "assert model.score(test_iter) > 0.97, \"Low prediction accuracy.\"" ] }, { @@ -489,6 +495,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "# run hand drawing test\n", "from IPython.display import HTML\n", "\n", @@ -574,6 +581,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "def norm_stat(d):\n", " \"\"\"The statistics you want to see.\n", " We compute the L2 norm here but you can change it to anything you like.\"\"\"\n", @@ -586,7 +594,9 @@ "model = mx.model.FeedForward(ctx = mx.gpu(0), symbol = mlp, num_epoch = 1,\n", " learning_rate = 0.1, momentum = 0.9, wd = 0.00001)\n", "model.fit(X=train_iter, eval_data=test_iter, monitor=mon, # Set the monitor here\n", - " batch_end_callback = mx.callback.Speedometer(100, 100))" + " batch_end_callback = mx.callback.Speedometer(100, 100))\n", + "assert model.score(train_iter) > 0.94, \"Low training accuracy.\"\n", + "assert model.score(test_iter) > 0.94, \"Low validation accuracy.\"" ] }, { @@ -676,6 +686,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "# ==================Binding=====================\n", "# The symbol we created is only a graph description.\n", "# To run it, we first need to allocate memory and create an executor by 'binding' it.\n", @@ -740,7 +751,8 @@ " metric.update(batch.label, exe.outputs)\n", " t += 1\n", " if t % 100 == 0:\n", - " print 'epoch:', epoch, 'iter:', t, 'metric:', metric.get()\n" + " print 'epoch:', epoch, 'iter:', t, 'metric:', metric.get()\n", + "assert metric.get()[1] > 0.98, \"Low training accuracy.\"" ] }, { @@ -786,6 +798,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "# Define custom softmax operator\n", "class NumpySoftmax(mx.operator.NumpyOp):\n", " def __init__(self):\n", @@ -840,7 +853,9 @@ "model = mx.model.FeedForward(ctx = mx.gpu(0), symbol = mlp, num_epoch = 2,\n", " learning_rate = 0.1, momentum = 0.9, wd = 0.00001)\n", "model.fit(X=train_iter, eval_data=test_iter,\n", - " batch_end_callback = mx.callback.Speedometer(100, 100))" + " batch_end_callback = mx.callback.Speedometer(100, 100))\n", + "assert model.score(train_iter) > 0.96, \"Low training accuracy.\"\n", + "assert model.score(test_iter) > 0.95, \"Low validation accuracy.\"" ] }, { diff --git a/python/how_to/predict.ipynb b/python/how_to/predict.ipynb index 9d0e01d9d..e48a2e578 100644 --- a/python/how_to/predict.ipynb +++ b/python/how_to/predict.ipynb @@ -25,6 +25,11 @@ "outputs": [], "source": [ "import os, urllib\n", + "%matplotlib inline\n", + "import matplotlib\n", + "matplotlib.rc(\"savefig\", dpi=100)\n", + "import matplotlib.pyplot as plt\n", + "import cv2\n", "def download(url):\n", " filename = url.split(\"/\")[-1]\n", " if not os.path.exists(filename):\n", @@ -3500,11 +3505,6 @@ } ], "source": [ - "%matplotlib inline\n", - "import matplotlib\n", - "matplotlib.rc(\"savefig\", dpi=100)\n", - "import matplotlib.pyplot as plt\n", - "import cv2\n", "for i in range(0,8):\n", " img = cv2.cvtColor(cv2.imread('val_1000/%d.jpg' % (i,)), cv2.COLOR_BGR2RGB)\n", " plt.subplot(2,4,i+1)\n", @@ -3531,7 +3531,6 @@ "outputs": [], "source": [ "import numpy as np\n", - "import cv2\n", "def get_image(filename):\n", " img = cv2.imread(filename) # read image in b,g,r order\n", " img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # change to r,g,b order\n", diff --git a/python/tutorials/linear-regression.ipynb b/python/tutorials/linear-regression.ipynb index 343964dcb..cd6db65c4 100644 --- a/python/tutorials/linear-regression.ipynb +++ b/python/tutorials/linear-regression.ipynb @@ -48,8 +48,8 @@ "outputs": [], "source": [ "#Training data\n", - "train_data = np.array([[1,2],[3,4],[5,6],[3,2],[7,1],[6,9]])\n", - "train_label = np.array([5,11,17,7,9,24])\n", + "train_data = np.random.uniform(0, 1, [100, 2])\n", + "train_label = np.array([train_data[i][0] + 2 * train_data[i][1] for i in range(100)])\n", "batch_size = 1\n", "\n", "#Evaluation Data\n", @@ -281,8 +281,9 @@ }, "outputs": [], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "model.fit(train_iter, eval_iter,\n", - " optimizer_params={'learning_rate':0.01, 'momentum': 0.9},\n", + " optimizer_params={'learning_rate':0.005, 'momentum': 0.9},\n", " num_epoch=1000,\n", " batch_end_callback = mx.callback.Speedometer(batch_size, 2))" ] @@ -326,6 +327,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "#Inference\n", "model.predict(eval_iter).asnumpy()" ] @@ -356,9 +358,11 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "#Evaluation\n", "metric = mx.metric.MSE()\n", - "model.score(eval_iter, metric)" + "model.score(eval_iter, metric)\n", + "assert model.score(eval_iter, metric)[0][1] < 0.001, \"High mean squared error on evaluation data.\"" ] }, { @@ -389,12 +393,14 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "#Evaluation Data\n", "eval_data = np.array([[7,2],[6,10],[12,2]])\n", "eval_label = np.array([11.1,26.1,16.1]) #Adding 0.1 to each of the values \n", "eval_iter = mx.io.NDArrayIter(eval_data, eval_label, batch_size, shuffle=False)\n", "\n", - "model.score(eval_iter, metric)" + "model.score(eval_iter, metric)\n", + "assert model.score(eval_iter, metric)[0][1] < 0.02, \"High mean squared error on evaluation data with noise.\"" ] }, { @@ -430,7 +436,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", - "version": "2.7.12" + "version": "2.7.6" } }, "nbformat": 4, diff --git a/python/tutorials/predict_imagenet.ipynb b/python/tutorials/predict_imagenet.ipynb index 68baafe5d..be9b82dbc 100644 --- a/python/tutorials/predict_imagenet.ipynb +++ b/python/tutorials/predict_imagenet.ipynb @@ -20,6 +20,12 @@ "outputs": [], "source": [ "import os, urllib\n", + "%matplotlib inline\n", + "import matplotlib\n", + "matplotlib.rc(\"savefig\", dpi=100)\n", + "import matplotlib.pyplot as plt\n", + "import cv2\n", + "import numpy as np\n", "import mxnet as mx\n", "def download(url,prefix=''):\n", " filename = prefix+url.split(\"/\")[-1]\n", @@ -72,12 +78,6 @@ }, "outputs": [], "source": [ - "%matplotlib inline\n", - "import matplotlib\n", - "matplotlib.rc(\"savefig\", dpi=100)\n", - "import matplotlib.pyplot as plt\n", - "import cv2\n", - "import numpy as np\n", "from collections import namedtuple\n", "Batch = namedtuple('Batch', ['data'])\n", "\n", @@ -148,6 +148,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "url = 'http://writm.com/wp-content/uploads/2016/08/Cat-hd-wallpapers.jpg'\n", "predict(get_image(url), mod, synsets)" ] @@ -182,6 +183,7 @@ } ], "source": [ + "# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n", "url = 'https://images-na.ssl-images-amazon.com/images/G/01/img15/pet-products/small-tiles/23695_pets_vertical_store_dogs_small_tile_8._CB312176604_.jpg'\n", "predict(get_image(url), mod, synsets)" ]