{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#setup the environment\n!pip install -q openvino-dev[onnx,tensorflow2]==2022.1.0 &> error_log_openvino.txt\n#!pip install -q tensorflow==2.5.1\n!pip install -q pandas==1.2.4 &> error_log_panda.txt","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:03:42.646577Z","iopub.execute_input":"2022-07-25T23:03:42.647065Z","iopub.status.idle":"2022-07-25T23:04:14.103296Z","shell.execute_reply.started":"2022-07-25T23:03:42.647024Z","shell.execute_reply":"2022-07-25T23:04:14.101998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-25T23:04:14.106025Z","iopub.execute_input":"2022-07-25T23:04:14.106495Z","iopub.status.idle":"2022-07-25T23:04:14.119730Z","shell.execute_reply.started":"2022-07-25T23:04:14.106441Z","shell.execute_reply":"2022-07-25T23:04:14.117725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nmnist = tf.keras.datasets.mnist\n\n(x_train, y_train), (x_test, y_test) = mnist.load_data()\nx_train, x_test = x_train / 255.0, x_test / 255.0","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:04:14.122544Z","iopub.execute_input":"2022-07-25T23:04:14.123020Z","iopub.status.idle":"2022-07-25T23:04:14.729219Z","shell.execute_reply.started":"2022-07-25T23:04:14.122973Z","shell.execute_reply":"2022-07-25T23:04:14.728077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.Sequential([\n  tf.keras.layers.Flatten(input_shape=(28, 28)),\n  tf.keras.layers.Dense(128, activation='relu'),\n  tf.keras.layers.Dropout(0.2),\n  tf.keras.layers.Dense(10)\n])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:04:14.731148Z","iopub.execute_input":"2022-07-25T23:04:14.731504Z","iopub.status.idle":"2022-07-25T23:04:14.805968Z","shell.execute_reply.started":"2022-07-25T23:04:14.731467Z","shell.execute_reply":"2022-07-25T23:04:14.804951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model(x_train[:1]).numpy()\npredictions","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:04:14.807680Z","iopub.execute_input":"2022-07-25T23:04:14.808167Z","iopub.status.idle":"2022-07-25T23:04:14.822465Z","shell.execute_reply.started":"2022-07-25T23:04:14.808122Z","shell.execute_reply":"2022-07-25T23:04:14.820288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.nn.softmax(predictions).numpy()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:04:14.824617Z","iopub.execute_input":"2022-07-25T23:04:14.825089Z","iopub.status.idle":"2022-07-25T23:04:14.834873Z","shell.execute_reply.started":"2022-07-25T23:04:14.825053Z","shell.execute_reply":"2022-07-25T23:04:14.834015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:04:14.836619Z","iopub.execute_input":"2022-07-25T23:04:14.837009Z","iopub.status.idle":"2022-07-25T23:04:14.842729Z","shell.execute_reply.started":"2022-07-25T23:04:14.836976Z","shell.execute_reply":"2022-07-25T23:04:14.841525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss_fn(y_train[:1], predictions).numpy()","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:04:14.846179Z","iopub.execute_input":"2022-07-25T23:04:14.846783Z","iopub.status.idle":"2022-07-25T23:04:14.860764Z","shell.execute_reply.started":"2022-07-25T23:04:14.846731Z","shell.execute_reply":"2022-07-25T23:04:14.859974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=loss_fn,\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:04:14.862929Z","iopub.execute_input":"2022-07-25T23:04:14.863552Z","iopub.status.idle":"2022-07-25T23:04:14.876596Z","shell.execute_reply.started":"2022-07-25T23:04:14.863503Z","shell.execute_reply":"2022-07-25T23:04:14.875559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train, y_train, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:04:14.878438Z","iopub.execute_input":"2022-07-25T23:04:14.879041Z","iopub.status.idle":"2022-07-25T23:05:07.102510Z","shell.execute_reply.started":"2022-07-25T23:04:14.879004Z","shell.execute_reply":"2022-07-25T23:05:07.101160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(x_test,  y_test, verbose=2)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:05:07.103914Z","iopub.execute_input":"2022-07-25T23:05:07.104238Z","iopub.status.idle":"2022-07-25T23:05:07.774076Z","shell.execute_reply.started":"2022-07-25T23:05:07.104207Z","shell.execute_reply":"2022-07-25T23:05:07.773099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probability_model = tf.keras.Sequential([\n  model,\n  tf.keras.layers.Softmax()\n])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:05:07.775387Z","iopub.execute_input":"2022-07-25T23:05:07.775731Z","iopub.status.idle":"2022-07-25T23:05:07.800926Z","shell.execute_reply.started":"2022-07-25T23:05:07.775688Z","shell.execute_reply":"2022-07-25T23:05:07.799898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"probability_model(x_test[:5])","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:05:07.802353Z","iopub.execute_input":"2022-07-25T23:05:07.802690Z","iopub.status.idle":"2022-07-25T23:05:07.816259Z","shell.execute_reply.started":"2022-07-25T23:05:07.802655Z","shell.execute_reply":"2022-07-25T23:05:07.814575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_fname=\"mnist\"\nprobability_model.save(model_fname)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:05:07.817864Z","iopub.execute_input":"2022-07-25T23:05:07.818194Z","iopub.status.idle":"2022-07-25T23:05:08.943173Z","shell.execute_reply.started":"2022-07-25T23:05:07.818162Z","shell.execute_reply":"2022-07-25T23:05:08.941733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from openvino.runtime import Core\nfrom pathlib import Path\nimport json\nimport sys\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n# The paths of the source and converted models\nmodel_name = \"mnist\"\nmodel_path = Path(model_name)\nir_data_type = \"FP16\"\nir_model_name = \"mnist_ir\"\n\n# Get the path to the Model Optimizer script\n\n# Construct the command for Model Optimizer\nmo_command = f\"\"\"mo\n                 --saved_model_dir \"{model_name}\"\n                 --input_shape \"[28,28]\" \n                 --data_type \"{ir_data_type}\" \n                 --output_dir \"{model_path.parent}\"\n                 --model_name \"{ir_model_name}\"\n                 \"\"\"\nmo_command = \" \".join(mo_command.split())\n\n# Run the Model Optimizer (overwrites the older model)\nprint(\"Exporting TensorFlow model to IR... This may take a few minutes.\")\nmo_result = %sx $mo_command\nprint(\"\\n\".join(mo_result))","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:05:08.945591Z","iopub.execute_input":"2022-07-25T23:05:08.946069Z","iopub.status.idle":"2022-07-25T23:05:20.134157Z","shell.execute_reply.started":"2022-07-25T23:05:08.946021Z","shell.execute_reply":"2022-07-25T23:05:20.132906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_xml = \"mnist_ir.xml\"\nmodel_bin = \"mnist_ir.bin\"\n\n# Load network to the plugin\nie = Core()\nmodel = ie.read_model(model=model_xml)\ncompiled_model = ie.compile_model(model=model, device_name=\"CPU\")\n\ninput_layer = compiled_model.input(0)\noutput_layer = compiled_model.output(0)\n\n#test against a few images from the dataset\ninput_list = x_test[:10]\nfor input_image in input_list:\n  res = compiled_model([input_image])[output_layer]\n  X = input_image\n  X = X.reshape([28, 28]);\n  plt.figure()\n  plt.gray()\n  plt.imshow(X)\n  plt.text(0,-1, \"The prediction is \"+str(np.argmax(res[0]))+\" @ \"+str(max(res[0])*100)+\"%\")","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:05:20.136041Z","iopub.execute_input":"2022-07-25T23:05:20.136416Z","iopub.status.idle":"2022-07-25T23:05:22.293212Z","shell.execute_reply.started":"2022-07-25T23:05:20.136374Z","shell.execute_reply":"2022-07-25T23:05:22.292333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Let's Test the Tensorflow Solution on the test.csv!","metadata":{}},{"cell_type":"code","source":"test_data = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nsubmit_data = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:05:22.294718Z","iopub.execute_input":"2022-07-25T23:05:22.295109Z","iopub.status.idle":"2022-07-25T23:05:24.689881Z","shell.execute_reply.started":"2022-07-25T23:05:22.295071Z","shell.execute_reply":"2022-07-25T23:05:24.688788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs = test_data.to_numpy().reshape(28000,1, 28,28)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:05:24.691473Z","iopub.execute_input":"2022-07-25T23:05:24.692119Z","iopub.status.idle":"2022-07-25T23:05:24.698825Z","shell.execute_reply.started":"2022-07-25T23:05:24.692071Z","shell.execute_reply":"2022-07-25T23:05:24.697435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_imgs.shape","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:05:24.700718Z","iopub.execute_input":"2022-07-25T23:05:24.701433Z","iopub.status.idle":"2022-07-25T23:05:24.713754Z","shell.execute_reply.started":"2022-07-25T23:05:24.701384Z","shell.execute_reply":"2022-07-25T23:05:24.712385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = np.zeros((28000,1))\ni=0\nfor test_img in test_imgs:\n    res = compiled_model([test_img[0]])[output_layer]\n    #X = input_image\n    #X = X.reshape([28, 28]);\n    #plt.figure()\n    #plt.gray()\n    #plt.imshow(X)\n    #plt.text(0,-1, \"The prediction is \"+str(np.argmax(res[0]))+\" @ \"+str(max(res[0])*100)+\"%\")\n    pred[i] = np.argmax(res[0])\n    i=i+1\n","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:08:32.921752Z","iopub.execute_input":"2022-07-25T23:08:32.922201Z","iopub.status.idle":"2022-07-25T23:08:41.988916Z","shell.execute_reply.started":"2022-07-25T23:08:32.922162Z","shell.execute_reply":"2022-07-25T23:08:41.987361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ind = pd.DataFrame(np.arange(1,28001), columns=['ImageId'])\n\npred_sub = pd.DataFrame(pred.astype('int32'), columns=['Label'])\nsubmission = [ind , pred_sub]\nsub = pd.concat(submission, axis=1)\n\ncompression_opts = dict(method='zip',\n                        archive_name='submission.csv')  \nsub.to_csv('submission.zip', index=False, compression=compression_opts)","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:07:01.831381Z","iopub.execute_input":"2022-07-25T23:07:01.831870Z","iopub.status.idle":"2022-07-25T23:07:01.953783Z","shell.execute_reply.started":"2022-07-25T23:07:01.831814Z","shell.execute_reply":"2022-07-25T23:07:01.952299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('submission.zip')","metadata":{"execution":{"iopub.status.busy":"2022-07-25T23:07:04.580368Z","iopub.execute_input":"2022-07-25T23:07:04.580887Z","iopub.status.idle":"2022-07-25T23:07:04.611334Z","shell.execute_reply.started":"2022-07-25T23:07:04.580832Z","shell.execute_reply":"2022-07-25T23:07:04.610016Z"},"trusted":true},"execution_count":null,"outputs":[]}]}