Data Science ——MNIST
1. mnist_deep.py read (where is the training, where is the test?)
2. Find the code for the training and save the model locally, called model.ckpt
Docker:
3. app.py
(1)restore model.ckpt, assuming model f(volume)docker run -v
(2)Given any graph x,f(x) = digit
(3)Deploy f(x) to Flask
(4)Write the current time, file name, and predicted number into Cassandra
Level-0:Implement the above code locally
My test1 always identify wrong, my test2 is not working. I need help to fix these two. And then finish step 3 and level-0
Im using Pycharm, Macbook pro. Please make sure I can run your code in mac environment.
DATA SCIENCE/.DS_Store
__MACOSX/DATA SCIENCE/._.DS_Store
DATA SCIENCE/venv/MNIST_data/t10k-images-idx3-ubyte.gz
DATA SCIENCE/venv/MNIST_data/t10k-images-idx3-ubyte
DATA SCIENCE/venv/MNIST_data/train-images-idx3-ubyte.gz
DATA SCIENCE/venv/MNIST_data/train-images-idx3-ubyte
DATA SCIENCE/venv/MNIST_data/train-labels-idx1-ubyte.gz
DATA SCIENCE/venv/MNIST_data/train-labels-idx1-ubyte
DATA SCIENCE/venv/MNIST_data/t10k-labels-idx1-ubyte.gz
DATA SCIENCE/venv/MNIST_data/t10k-labels-idx1-ubyte
DATA SCIENCE/venv/.DS_Store
__MACOSX/DATA SCIENCE/venv/._.DS_Store
DATA SCIENCE/venv/TEST/.DS_Store
__MACOSX/DATA SCIENCE/venv/TEST/._.DS_Store
DATA SCIENCE/venv/TEST/8.png
DATA SCIENCE/venv/TEST/9.png
DATA SCIENCE/venv/TEST/4.png
DATA SCIENCE/venv/TEST/5.png
DATA SCIENCE/venv/TEST/7.png
__MACOSX/DATA SCIENCE/venv/TEST/._7.png
DATA SCIENCE/venv/TEST/6.png
DATA SCIENCE/venv/TEST/2.png
__MACOSX/DATA SCIENCE/venv/TEST/._2.png
DATA SCIENCE/venv/TEST/3.png
DATA SCIENCE/venv/TEST/1.png
DATA SCIENCE/venv/8.png
__MACOSX/DATA SCIENCE/venv/._8.png
DATA SCIENCE/venv/bin/pip3.7
#!/Users/lenoxye/PycharmProjects/MIT/venv/bin/python # EASY-INSTALL-ENTRY-SCRIPT: 'pip==19.0.3','console_scripts','pip3.7' __requires__ = 'pip==19.0.3' import re import sys from pkg_resources import load_entry_point if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit( load_entry_point('pip==19.0.3', 'console_scripts', 'pip3.7')() )
DATA SCIENCE/venv/bin/python3
DATA SCIENCE/venv/bin/easy_install
#!/Users/lenoxye/PycharmProjects/MIT/venv/bin/python # EASY-INSTALL-ENTRY-SCRIPT: 'setuptools==40.8.0','console_scripts','easy_install' __requires__ = 'setuptools==40.8.0' import re import sys from pkg_resources import load_entry_point if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit( load_entry_point('setuptools==40.8.0', 'console_scripts', 'easy_install')() )
DATA SCIENCE/venv/bin/python
DATA SCIENCE/venv/bin/pip3
#!/Users/lenoxye/PycharmProjects/MIT/venv/bin/python # EASY-INSTALL-ENTRY-SCRIPT: 'pip==19.0.3','console_scripts','pip3' __requires__ = 'pip==19.0.3' import re import sys from pkg_resources import load_entry_point if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit( load_entry_point('pip==19.0.3', 'console_scripts', 'pip3')() )
DATA SCIENCE/venv/bin/activate.fish
# This file must be used with ". bin/activate.fish" *from fish* (http://fishshell.org) # you cannot run it directly function deactivate -d "Exit virtualenv and return to normal shell environment" # reset old environment variables if test -n "$_OLD_VIRTUAL_PATH" set -gx PATH $_OLD_VIRTUAL_PATH set -e _OLD_VIRTUAL_PATH end if test -n "$_OLD_VIRTUAL_PYTHONHOME" set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME set -e _OLD_VIRTUAL_PYTHONHOME end if test -n "$_OLD_FISH_PROMPT_OVERRIDE" functions -e fish_prompt set -e _OLD_FISH_PROMPT_OVERRIDE functions -c _old_fish_prompt fish_prompt functions -e _old_fish_prompt end set -e VIRTUAL_ENV if test "$argv[1]" != "nondestructive" # Self destruct! functions -e deactivate end end # unset irrelevant variables deactivate nondestructive set -gx VIRTUAL_ENV "/Users/lenoxye/PycharmProjects/MIT/venv" set -gx _OLD_VIRTUAL_PATH $PATH set -gx PATH "$VIRTUAL_ENV/bin" $PATH # unset PYTHONHOME if set if set -q PYTHONHOME set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME set -e PYTHONHOME end if test -z "$VIRTUAL_ENV_DISABLE_PROMPT" # fish uses a function instead of an env var to generate the prompt. # save the current fish_prompt function as the function _old_fish_prompt functions -c fish_prompt _old_fish_prompt # with the original prompt function renamed, we can override with our own. function fish_prompt # Save the return status of the last command set -l old_status $status # Prompt override? if test -n "(venv) " printf "%s%s" "(venv) " (set_color normal) else # ...Otherwise, prepend env set -l _checkbase (basename "$VIRTUAL_ENV") if test $_checkbase = "__" # special case for Aspen magic directories # see http://www.zetadev.com/software/aspen/ printf "%s[%s]%s " (set_color -b blue white) (basename (dirname "$VIRTUAL_ENV")) (set_color normal) else printf "%s(%s)%s" (set_color -b blue white) (basename "$VIRTUAL_ENV") (set_color normal) end end # Restore the return status of the previous command. echo "exit $old_status" | . _old_fish_prompt end set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV" end
DATA SCIENCE/venv/bin/easy_install-3.7
#!/Users/lenoxye/PycharmProjects/MIT/venv/bin/python # EASY-INSTALL-ENTRY-SCRIPT: 'setuptools==40.8.0','console_scripts','easy_install-3.7' __requires__ = 'setuptools==40.8.0' import re import sys from pkg_resources import load_entry_point if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit( load_entry_point('setuptools==40.8.0', 'console_scripts', 'easy_install-3.7')() )
DATA SCIENCE/venv/bin/python3.7
DATA SCIENCE/venv/bin/pip
#!/Users/lenoxye/PycharmProjects/MIT/venv/bin/python # EASY-INSTALL-ENTRY-SCRIPT: 'pip==19.0.3','console_scripts','pip' __requires__ = 'pip==19.0.3' import re import sys from pkg_resources import load_entry_point if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0]) sys.exit( load_entry_point('pip==19.0.3', 'console_scripts', 'pip')() )
DATA SCIENCE/venv/bin/activate
# This file must be used with "source bin/activate" *from bash* # you cannot run it directly deactivate () { # reset old environment variables if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then PATH="${_OLD_VIRTUAL_PATH:-}" export PATH unset _OLD_VIRTUAL_PATH fi if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}" export PYTHONHOME unset _OLD_VIRTUAL_PYTHONHOME fi # This should detect bash and zsh, which have a hash command that must # be called to get it to forget past commands. Without forgetting # past commands the $PATH changes we made may not be respected if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then hash -r fi if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then PS1="${_OLD_VIRTUAL_PS1:-}" export PS1 unset _OLD_VIRTUAL_PS1 fi unset VIRTUAL_ENV if [ ! "$1" = "nondestructive" ] ; then # Self destruct! unset -f deactivate fi } # unset irrelevant variables deactivate nondestructive VIRTUAL_ENV="/Users/lenoxye/PycharmProjects/MIT/venv" export VIRTUAL_ENV _OLD_VIRTUAL_PATH="$PATH" PATH="$VIRTUAL_ENV/bin:$PATH" export PATH # unset PYTHONHOME if set # this will fail if PYTHONHOME is set to the empty string (which is bad anyway) # could use `if (set -u; : $PYTHONHOME) ;` in bash if [ -n "${PYTHONHOME:-}" ] ; then _OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}" unset PYTHONHOME fi if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then _OLD_VIRTUAL_PS1="${PS1:-}" if [ "x(venv) " != x ] ; then PS1="(venv) ${PS1:-}" else if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then # special case for Aspen magic directories # see http://www.zetadev.com/software/aspen/ PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1" else PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" fi fi export PS1 fi # This should detect bash and zsh, which have a hash command that must # be called to get it to forget past commands. Without forgetting # past commands the $PATH changes we made may not be respected if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then hash -r fi
DATA SCIENCE/venv/bin/activate.csh
# This file must be used with "source bin/activate.csh" *from csh*. # You cannot run it directly. # Created by Davide Di Blasi . # Ported to Python 3.3 venv by Andrew Svetlov alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate' # Unset irrelevant variables. deactivate nondestructive setenv VIRTUAL_ENV "/Users/lenoxye/PycharmProjects/MIT/venv" set _OLD_VIRTUAL_PATH="$PATH" setenv PATH "$VIRTUAL_ENV/bin:$PATH" set _OLD_VIRTUAL_PROMPT="$prompt" if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then if ("venv" != "") then set env_name = "venv" else if (`basename "VIRTUAL_ENV"` == "__") then # special case for Aspen magic directories # see http://www.zetadev.com/software/aspen/ set env_name = `basename \`dirname "$VIRTUAL_ENV"\`` else set env_name = `basename "$VIRTUAL_ENV"` endif endif set prompt = "[$env_name] $prompt" unset env_name endif alias pydoc python -m pydoc rehash
DATA SCIENCE/venv/Mnistdeep.py
from tensorflow.examples.tutorials.mnist import input_data import tensorflow as tf mnist = input_data.read_data_sets('MNIST_data', one_hot=True) x = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) def weight_variable(shape): initial = tf.truncated_normal(shape,stddev = 0.1) return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1,shape = shape) return tf.Variable(initial) def conv2d(x,W): return tf.nn.conv2d(x, W, strides = [1,1,1,1], padding = 'SAME') def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME') W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) x_image = tf.reshape(x,[-1,28,28,1]) h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2) W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) keep_prob = tf.placeholder("float") h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(20000): batch = mnist.train.next_batch(50) if i % 100 == 0: train_accuracy = accuracy.eval(feed_dict={ x: batch[0], y_: batch[1], keep_prob: 1.0}) print('step %d, training accuracy %g' % (i, train_accuracy)) train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) saver.save(sess, 'tmp/model.ckpt') print('test accuracy %g' % accuracy.eval(feed_dict={ x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
DATA SCIENCE/venv/test1.py
from PIL import Image, ImageFilter import tensorflow as tf import matplotlib.pyplot as plt import numpy as np def image_prepare(image_path): img = Image.open(image_path) im = img.resize((28, 28), Image.ANTIALIAS) im = im.convert('L') tv = list(im.getdata()) tva = [(255 - x) * 1.0 / 255.0 for x in tv] return tva image_path = input('Please enter the image path : ') result = image_prepare(image_path) x = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) def weight_variable(shape): initial = tf.truncated_normal(shape, stddev = 0.1) return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1, shape = shape) return tf.Variable(initial) def conv2d(x, W): return tf.nn.conv2d(x, W, strides = [1,1,1,1], padding = "SAME") def max_pool_2x2(x): return tf.nn.max_pool(x, ksize = [1,2,2,1], strides = [1,2,2,1], padding = "SAME") W_conv1 = weight_variable([5,5,1,32]) b_conv1 = bias_variable([32]) x_image = tf.reshape(x, [-1,28,28,1]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1)+b_conv1) h_pool1 = max_pool_2x2(h_conv1) W_conv2 = weight_variable([5,5,32,64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2)+b_conv2) h_pool2 = max_pool_2x2(h_conv2) W_fc1 = weight_variable([7*7*64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1)+b_fc1) keep_prob = tf.placeholder("float") h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2)+b_fc2) cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) saver.restore(sess, "tmp/model.ckpt") prediction = tf.argmax(y_conv, 1) predict = prediction.eval(feed_dict={x: [result], keep_prob: 1.0}, session=sess) print('recognize result:') print(predict[0])
DATA SCIENCE/venv/pyvenv.cfg
home = /usr/local/bin include-system-site-packages = false version = 3.7.4
DATA SCIENCE/venv/test2.py
from PIL import Image, ImageFilter import tensorflow as tf import matplotlib.pyplot as plt import cv def imageprepare(): file_name='TEST/1.png' im = Image.open(file_name).convert('L') im.save("sample.png") plt.imshow(im) plt.show() tv = list(im.getdata()) #get pixel values #normalize pixels to 0 and 1. 0 is pure white, 1 is pure black. tva = [ (255-x)*1.0/255.0 for x in tv] #print(tva) return tva """ This function returns the predicted integer. The imput is the pixel values from the imageprepare() function. """ # Define the model (same as when creating the model file) result=imageprepare() x = tf.placeholder(tf.float32, [None, 784]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) x_image = tf.reshape(x, [-1,28,28,1]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2) W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) init_op = tf.initialize_all_variables() """ Load the model2.ckpt file file is stored in the same directory as this python script is started Use the model to predict the integer. Integer is returend as list. Based on the documentatoin at https://www.tensorflow.org/versions/master/how_tos/variables/index.html """ saver = tf.train.Saver() with tf.Session() as sess: sess.run(init_op) saver.restore(sess, "tmp/model.ckpt") #print ("Model restored.") prediction=tf.argmax(y_conv,1) predint=prediction.eval(feed_dict={x: [result],keep_prob: 1.0}, session=sess) print(h_conv2) print('recognize result:') print(predint[0])
DATA SCIENCE/venv/123.png
__MACOSX/DATA SCIENCE/venv/._123.png
DATA SCIENCE/venv/sample.png
DATA SCIENCE/venv/lib/.DS_Store
__MACOSX/DATA SCIENCE/venv/lib/._.DS_Store
DATA SCIENCE/venv/lib/python3.7/site-packages/easy-install.pth
./setuptools-40.8.0-py3.7.egg ./pip-19.0.3-py3.7.egg
DATA SCIENCE/venv/lib/python3.7/site-packages/pip-19.0.3-py3.7.egg/EGG-INFO/PKG-INFO