In [1]:
import mxnet as mx 
from importlib import import_module
import cv2
import matplotlib.pyplot as plt 
import numpy as np

In [2]:
# 1. data reading
# labels in order: 
"""
0 airplane
1 automobile
2 bird
3 cat
4 deer
5 dog
6 frog
7 horse
8 ship
9 truck
"""

def get_cifar10():
    # TODO fill all the blanks 
    # Hint
    # rgb mean default: '123.68,116.779,103.939',
    # pad size: whatever you think is valid, pad to 32 is good 
    # image shape:  the image shape feed into the network, e.g. (3,224,224)')
    # num-classes: the number of classes
    # num-examples:  the number of training examples, 50000 :)
    # data-nthreads: default=4,number of threads for data decoding, how many cpus do you have
    # dtype: default is float32, can be data type: float32 or float16')
    train = mx.io.ImageRecordIter(
            path_imgrec         = "./cifar10_val.rec",
            label_width         = 1,
            mean_r              = 123.68,
            mean_g              = 116.779,
            mean_b              = 103.939,
            data_name           = 'data',
            label_name          = 'softmax_label',
            data_shape          = (3,32,32),
            batch_size          = 20,
            pad                 = 4,
            fill_value          = 127,
            preprocess_threads  = 4,
            shuffle             = True)
    val = mx.io.ImageRecordIter(
            path_imgrec         = "./cifar10_val.rec",
            label_width         = 1,
            mean_r              = 123.68,
            mean_g              = 116.779,
            mean_b              = 103.939,
            data_name           = 'data',
            label_name          = 'softmax_label',
            data_shape          = (3,32,32),
            batch_size          = 20,
            pad                 = 4,
            fill_value          = 127,
            preprocess_threads  = 4,
            shuffle             = False)
    return train, val

train, val = get_cifar10()
# TODO write the script to look what is inside train and val
# Check the image size, and label 
# Question? check MXNET_course/mxnet-week3/cifar10/step_by_step_debug.ipynb
print (type(train))
print (type(val))
# print train.label
train.reset()
train.next()
val.reset()
val.next()
print (train.getdata()[0])
image_0 = train.getdata()[0].asnumpy()
plt.imshow(image_0.transpose(2,1,0))
plt.show()
plt.imshow(val.getdata()[0].asnumpy().transpose(2,1,0))
plt.show()


<class 'mxnet.io.MXDataIter'>
<class 'mxnet.io.MXDataIter'>
<NDArray 3x32x32 @cpu(0)>

In [ ]:
# 2 model getting 
# TODO read through resnet.py file for understanding
def get_resnet():
    net = import_module('resnet')
    sym = net.get_symbol(10,20,"3,28,28")
    return sym 
sym = get_resnet()

# TODO 
# 1. Plot and visualize the network. Put your comments about its architecture (why bottlenet)
# 2. List all weight and output (Question? check MXNET_course/mxnet-week3/cifar10/step_by_step_debug.ipynb)
mx.viz.plot_network(sym,hide_weights=False,save_format='pdf',title='resnet8')
print sym.list_arguments()
print sym.list_outputs()


['data', 'bn_data_gamma', 'bn_data_beta', 'conv0_weight', 'stage1_unit1_bn1_gamma', 'stage1_unit1_bn1_beta', 'stage1_unit1_conv1_weight', 'stage1_unit1_bn2_gamma', 'stage1_unit1_bn2_beta', 'stage1_unit1_conv2_weight', 'stage1_unit1_sc_weight', 'stage1_unit2_bn1_gamma', 'stage1_unit2_bn1_beta', 'stage1_unit2_conv1_weight', 'stage1_unit2_bn2_gamma', 'stage1_unit2_bn2_beta', 'stage1_unit2_conv2_weight', 'stage1_unit3_bn1_gamma', 'stage1_unit3_bn1_beta', 'stage1_unit3_conv1_weight', 'stage1_unit3_bn2_gamma', 'stage1_unit3_bn2_beta', 'stage1_unit3_conv2_weight', 'stage2_unit1_bn1_gamma', 'stage2_unit1_bn1_beta', 'stage2_unit1_conv1_weight', 'stage2_unit1_bn2_gamma', 'stage2_unit1_bn2_beta', 'stage2_unit1_conv2_weight', 'stage2_unit1_sc_weight', 'stage2_unit2_bn1_gamma', 'stage2_unit2_bn1_beta', 'stage2_unit2_conv1_weight', 'stage2_unit2_bn2_gamma', 'stage2_unit2_bn2_beta', 'stage2_unit2_conv2_weight', 'stage2_unit3_bn1_gamma', 'stage2_unit3_bn1_beta', 'stage2_unit3_conv1_weight', 'stage2_unit3_bn2_gamma', 'stage2_unit3_bn2_beta', 'stage2_unit3_conv2_weight', 'stage3_unit1_bn1_gamma', 'stage3_unit1_bn1_beta', 'stage3_unit1_conv1_weight', 'stage3_unit1_bn2_gamma', 'stage3_unit1_bn2_beta', 'stage3_unit1_conv2_weight', 'stage3_unit1_sc_weight', 'stage3_unit2_bn1_gamma', 'stage3_unit2_bn1_beta', 'stage3_unit2_conv1_weight', 'stage3_unit2_bn2_gamma', 'stage3_unit2_bn2_beta', 'stage3_unit2_conv2_weight', 'stage3_unit3_bn1_gamma', 'stage3_unit3_bn1_beta', 'stage3_unit3_conv1_weight', 'stage3_unit3_bn2_gamma', 'stage3_unit3_bn2_beta', 'stage3_unit3_conv2_weight', 'bn1_gamma', 'bn1_beta', 'fc1_weight', 'fc1_bias', 'softmax_label']
['softmax_output']

In [ ]:
# 3 sanity check random image inference
img1 = cv2.imread("frog.jpg")
img1 = cv2.resize(img1,(32,32))# you need to pad it if you do padding for you nework
plt.imshow(img1)
plt.show()
print type(img1)
img1 = img1.transpose((2,0,1)).reshape((1,3,32,32))
print img1.shape

img2 = cv2.imread("frog2.jpg")
img2 = cv2.resize(img2,(32,32))# you need to pad it if you do padding for you nework
plt.imshow(img2)
plt.show()
print type(img2)
img2 = img2.transpose((2,0,1)).reshape((1,3,32,32))
print img2.shape

img = np.vstack([img1,img2])
print "The very small training dataset contain: ", img.shape

# TODO: figure out how to convert numpy array to mx.nd.array
img_mxnd = mx.nd.array(source_array=img,ctx=mx.cpu())
label_mxnd = mx.nd.array(ctx=mx.cpu(), source_array=np.asarray([6,6])) # 6 is frog
img_itr = mx.io.NDArrayIter(data=img_mxnd, data_name='data', label_name ='softmax_label', label=label_mxnd, batch_size=1) # Hint the name should "data" and softmax_label
print "small dataset is: ", type(img_itr)
for each in img_itr:
    print each

# TODO bind the random img to network 
# question? check mxnet-week3/cifar10/train_cifar10.py
mod = mx.mod.Module(sym)
mod.bind(img_itr.provide_data,img_itr.provide_label)
mod.init_params(mx.init.Xavier(rnd_type='gaussian', factor_type='in', magnitude=2)) 

# run forward perdiction 
# TODO fill the mod.predict 
# check mod.predict
out = mod.predict(eval_data=img_itr,num_batch=2,reset=True)


<type 'numpy.ndarray'>
(1, 3, 32, 32)
<type 'numpy.ndarray'>
(1, 3, 32, 32)
The very small training dataset contain:  (2, 3, 32, 32)
small dataset is:  <class 'mxnet.io.NDArrayIter'>
DataBatch: data shapes: [(1L, 3L, 32L, 32L)] label shapes: [(1L,)]
DataBatch: data shapes: [(1L, 3L, 32L, 32L)] label shapes: [(1L,)]

In [ ]:
print np.argmax(out.asnumpy(),axis=1)

In [ ]:
# 4 overfit small dataset 
# TODO fill all ???
mod.init_params(???) 
mod.init_optimizer(???) 
# run forward perdiction
metric = mx.metric.create('acc')

for epoch in range(5):
    img_itr.reset()
    metric.reset()
    for batch in img_itr:
        mod.forward(batch, is_train=True)
        mod.update_metric(metric, batch.label)
        mod.backward()
        mod.update()
    print ('Epoch {}, Train {}'.format(epoch, metric.get()))
    # You should get 100% accuacy on these two images

In [ ]:
# 5 (optional) train cifar10 on resnet(~epoch) if you have GPU. Build the training script from week2

In [ ]:
# 6 (optional) Wild test on your model