Friday, August 24, 2018

Rebuild MNIST Number Image from the Vector

IDE 
pyCharm CE

Source 
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

#Load data
X_train, y_train),(X_test, y_test) = tf.keras.datasets.mnist.load_data()

#60000, which is the value of the 0th dimention i.e. shape[0]
image_record_numbers = X_train.shape[0]

#record from the mnist train data to be plotted, total 60000
image_record_number = 0

#calculate number of pixels for each image, X_train.shape[1] returns 28 & X_train.shape[2] returns 28
number_of_pixels = X_train.shape[1] * X_train.shape[2];

#get a vector (784 pixels), representsation of the image with 28 x 28
X_train_record_vectors = X_train.reshape(image_record_numbers, number_of_pixels).astype('float32')

#Convert back a vector (784 pixels), into the Image of 28 x 28 pixels
X_train_record_matrix = X_train_record_vectors[image_record_number].reshape((28,28))

#Plot the image using reshaped vector
plt.imshow(X_train_record_matrix,cmap=plt.get_cmap('gray'))
plt.show()

Output 



MNIST with KERAS

IDE 
pyCharm CE

Source 
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

#Load data
(X_train, y_train),(X_test, y_test) = tf.keras.datasets.mnist.load_data()

#plot with 3 rows and 3 columns grid, and locate each subplot using the sequence number 1,2 ...plt.subplot(331)
plt.imshow(X_train[0],cmap=plt.get_cmap('gray'))
plt.subplot(332)
plt.imshow(X_train[1],cmap=plt.get_cmap('gray'))
plt.subplot(333)
plt.imshow(X_train[2],cmap=plt.get_cmap('gray'))
plt.subplot(334)
plt.imshow(X_train[3],cmap=plt.get_cmap('gray'))
plt.subplot(335)
plt.imshow(X_train[4],cmap=plt.get_cmap('gray'))
plt.subplot(336)
plt.imshow(X_train[5],cmap=plt.get_cmap('gray'))
plt.subplot(337)
plt.imshow(X_train[6],cmap=plt.get_cmapƄ('gray'))
plt.subplot(338)
plt.imshow(X_train[7],cmap=plt.get_cmap('gray'))
plt.subplot(339)
plt.imshow(X_train[8],cmap=plt.get_cmap('gray'))
plt.show()


Output 

Roughwork

1. Install python3
brew install python3

2. Install pip3
brew postinstall python3

3. Install Jupyter
brew install jupyter

4. Install TensorFlow
pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.9.0-py3-none-any.whl

5. Install Virtual Environment
pip3 install --upgrade virtualenv 

6. Create Virtual Environment
virtualenv --system-site-packages -p python3 <TARGET DIRECTORY>

7. Active the Virtual Environment
cd <TARGET DIRECTORY>
source ./bin/activate  

8. Uninstall python3
brew uninstall python3

9. Install Anaconda
pyenv install anaconda3-4.0.0

Installation of Brew, Python3, Jupyter, Tensorflow

1. Install Homebrew on Mac OSX
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Reference:

2. Install pyenv
brew install pyenv

3. Install python 3.6.6 
(using pyenv install specific version of the python, the latest is 3.7 but this python version is still incompatible with current tensor flow version 1.9) 

pyenv install 3.6.0

pyenv global 3.6.0

python --version

4. Install PyCharm
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null ; brew install caskroom/cask/brew-cask 2> /dev/null

brew cask install pycharm

4.1 Set Python interpretor for PyCharm














4.2 Install additional packages (such as matplotlib) into the pyCharm
https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html

4.3 Fix matplotlib exception at runtime
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. 
Process finished with exit code 1

Solution
touch ~/.matplotlib/matplotlibrc

Add below line
backend: TkAgg

5. Install anaconda
pyenv install --list | grep anaconda
install anaconda3-5.2.0


Installation of Tensorflow within Python Env and Jupyter

1. Download Anaconda: https://www.anaconda.com/distribution/#macos Error while creating a Session with Tensorflow (command line) >...