Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions python/basic/advanced_img_io.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -97,6 +97,7 @@
}
],
"source": [
"# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n",
"# opencv\n",
"N = 1000\n",
"tic = time.time()\n",
Expand Down Expand Up @@ -133,6 +134,7 @@
}
],
"source": [
"# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n",
"# mx.image\n",
"tic = time.time()\n",
"for i in range(N):\n",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -273,7 +276,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"version": "2.7.6"
}
},
"nbformat": 4,
Expand Down
6 changes: 3 additions & 3 deletions python/basic/image_io.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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'"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion python/basic/mixed.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,6 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"language": "python",
Expand Down
5 changes: 4 additions & 1 deletion python/basic/ndarray.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -1121,7 +1125,6 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"language": "python",
Expand Down
6 changes: 3 additions & 3 deletions python/basic/record_io.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -82,7 +82,7 @@
" item = record.read()\n",
" if not item:\n",
" break\n",
" print item\n",
" print (item)\n",
"record.close()"
]
},
Expand Down
5 changes: 2 additions & 3 deletions python/basic/symbol.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -1478,7 +1478,6 @@
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"language": "python",
Expand Down
25 changes: 20 additions & 5 deletions python/cnn/mnist.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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.\""
]
},
{
Expand Down Expand Up @@ -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()"
Expand Down Expand Up @@ -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.\""
]
},
{
Expand Down Expand Up @@ -489,6 +495,7 @@
}
],
"source": [
"# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n",
"# run hand drawing test\n",
"from IPython.display import HTML\n",
"\n",
Expand Down Expand Up @@ -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",
Expand All @@ -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.\""
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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.\""
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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.\""
]
},
{
Expand Down
11 changes: 5 additions & 6 deletions python/how_to/predict.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
18 changes: 12 additions & 6 deletions python/tutorials/linear-regression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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))"
]
Expand Down Expand Up @@ -326,6 +327,7 @@
}
],
"source": [
"# @@@ AUTOTEST_OUTPUT_IGNORED_CELL\n",
"#Inference\n",
"model.predict(eval_iter).asnumpy()"
]
Expand Down Expand Up @@ -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.\""
]
},
{
Expand Down Expand Up @@ -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.\""
]
},
{
Expand Down Expand Up @@ -430,7 +436,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"version": "2.7.6"
}
},
"nbformat": 4,
Expand Down
Loading