Running a CIFAR 10 image classifier on Windows with pytorch
At the time of running(7/17), running pytorch requires some effort. But, thanks to the effort of the community, it is possible to run it with GPU support. The classifier is a slightly modified version of http://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html, to make it compatible with windows, and to add model saving/loading.
- Get conda installed on your machine-(https://conda.io/docs/install/quick.html)
- Create a python 3.6 environment. With conda this is as simple as:
conda create --name py36 python=3.6activate py36
3. Install pytorch using the following command:
conda install -c peterjc123 pytorch=0.1.124. To run the classifier sample, torchvision is used. Torchvision will also make your life easier when testing with many vision datasets by avoiding a lot of boilerplate code. Let’s install torchvision by running:
pip install torchvision5. Now, all requirements the requirements have been installed. Let’s clone and run the classifier.
git clone https://github.com/mackiem/pytorch-experiments
>python classifier.py
Files already downloaded and verified
Files already downloaded and verified
[1, 2000] loss: 2.220
[1, 4000] loss: 1.867
[1, 6000] loss: 1.707
[1, 8000] loss: 1.589
[1, 10000] loss: 1.551
[1, 12000] loss: 1.493
[2, 2000] loss: 1.420
[2, 4000] loss: 1.398
[2, 6000] loss: 1.349
[2, 8000] loss: 1.334
[2, 10000] loss: 1.301
[2, 12000] loss: 1.293
Total test accuracy : 54 %
Accuracy of plane : 76 %
Accuracy of car : 72 %
Accuracy of bird : 41 %
Accuracy of cat : 24 %
Accuracy of deer : 50 %
Accuracy of dog : 47 %
Accuracy of frog : 64 %
Accuracy of horse : 65 %
Accuracy of ship : 57 %
Accuracy of truck : 46 %6. That’s it! You have run pytorch on windows, trained it on the gpu, and classified the cifar 10 dataset.
Now, let’s identify some changes in the code that allows it to run in windows.
The main change that is in the line:
if __name__ == ‘__main__’:Without explicitly checking for main, windows will give you the following exception, due to it recursively trying to spawn subprocesses (https://stackoverflow.com/questions/18204782/runtimeerror-on-windows-trying-python-multiprocessing).
Without this you should get an exception that looks like this:
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:if __name__ == ‘__main__’:
freeze_support()
…The “freeze_support()” line can be omitted if the program
is not going to be frozen to produce an executable. return DataLoaderIter(self)….
BrokenPipeError: [Errno 32] Broken pipe
The other change that were done is to introduce training on the gpu. This as simple as adding cuda() to the approriate variables in pytorch. You can see this in the code clearly, if you do a simple ‘find’.
The next change is to avoid training the network repeatedly, after training it as defined in the training loop. The model is saved as ‘cifar-10-model’. As a micro exercise, change the variable train from True to False, after running this script once (If you ran the command as listed above, this is already done, and you should see the cifar-10-model file in the directory). Now, you can simply run it to load the saved model, and execute it on the test set.
Hopefully, now you can start coding in pytorch even if it’s on Windows. Have fun!
