Learn to train and predict model under MXNet
Prepare the input data
Because the input data’s formt of MXNet is rec,so we must turn the image into the .rec format,as folow:
First, we prepare some face images data stored in ‘test_face’ file:
step1:build the txt or lst format from the images
Now,we have so many images in hand.Next,we make some changes to them that generate a list or a txt about the images.As for the list or txt,its format as folllows:
To see explicitly,we can see an image below:
Of course, we still need the test.lst or test.txt.
step2:generate the .rec from txt or lst
Under our MXNet root diretory,we can see
so,we can build the rec doc by using ‘im2rec’.
The comman as folows:
Here,we can see three parameters.They are:
the first param: the path of your lst or txt have been build;
the second one: the path of your images;
the third one: the path of your .rec.
OK,so far we have the rec doc of train data,but we still lack the rec of test data.In that,we can generate the test.rec with the same method.Finally,you can get four files:
Good luck!
Construct the net
In this step,we can construct the net ourselves,but at first best do not.Because we can use the off-the-shelf models or net to achieve more higher use.And furthermore,it can save twists and turns at first.And it is so-called ‘standing on the shoulders of giants’.Hence,the simplest way is to make a little change on the off-th-shelf net.
As for the off-the-shelf examples,we can find under ‘mxnet/examples/image-classification/‘ and ‘mxnet/tests/python/‘ .
step1:New a folder named ‘MyTrainMxnet’;the folder path is freewill;
step2:Copy four files below to th new folder ‘MyTrainMxnet’;
Attention:Here we choose the ineption-bn-28-small net,of course, you can choose others,e.g alexnet..
step3:Let’s make some changes.
1.Rename the ‘symbol_inception-bn-28-small.py’ as ‘my-inception.py’;
2.Rename the ‘train_mnist.py’ as ‘train_face.py’;
3.Open ‘train_face.py’ and do some changes;
Delete the ‘symbol’,otherwise your model file must begin with ‘symbol’.
##Train the model
Of course we can choose not do these changes.Instead,we can add ‘–network’ and ‘–data-dir’ in the command,as folows:
In addition, we can add other params if we need in the command above.
Attention:If you want to save the trained-well model after every epoch,please must add ‘–model-prefix’.And you will get it when making prediction afterwards.
Ok,the training step may need much time when cpu-only.So we can wait in patience.
Make a prediction
When you complete the training work,you will find:
Now,we can make a prediction using the test data.
First,we need write a prediction file named ‘predict_face.py’,its contents as follow:
Attention:Remember to change the path into yours.
Ok,hereto it’s over.
If yu want to make predition using new data,please keep attention.
Be continued!