{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","collapsed":true,"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":false},"cell_type":"markdown","source":"## Note: Used internet and GPU to first run the experiments. Then used a different kernel to submit submission file"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Convert CSV data to images\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport os\nfrom tqdm import tqdm_notebook as tqdm\n\nos.mkdir(\"images\");\nos.mkdir(\"images/train\");\nos.mkdir(\"images/test\");","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#Training data\ndv = pd.read_csv(\"/kaggle/input/Kannada-MNIST/train.csv\");\nfor i in tqdm(range(60000)):\n    x = dv.iloc[i].to_numpy();\n    label = str(x[0])\n    img = x[1:]\n    img = np.reshape(img, (28, 28));\n    img = np.expand_dims(img, axis=0);\n    img = np.vstack((img, img, img))\n    img = np.swapaxes(img, 0, 1);\n    img = np.swapaxes(img, 1, 2);\n    if(not os.path.isdir(\"images/train/\" + label)):\n        os.mkdir(\"images/train/\" + label);\n    \n    cv2.imwrite(\"images/train/\" + label + \"/\" + str(i) + \".jpg\", img);\n    \n    \n#Testing data\ndv = pd.read_csv(\"/kaggle/input/Kannada-MNIST/test.csv\");\nfor i in tqdm(range(5000)):\n    x = dv.iloc[i].to_numpy();\n    id_ = str(x[0])\n    img = x[1:]\n    img = np.reshape(img, (28, 28));\n    img = np.expand_dims(img, axis=0);\n    img = np.vstack((img, img, img))\n    img = np.swapaxes(img, 0, 1);\n    img = np.swapaxes(img, 1, 2);\n    cv2.imwrite(\"images/test/\" + str(id_) + \".jpg\", img);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Introducing Monk\n- An **open-source low code** transfer learning toolkit that acts as a unified wrapper over deep learning frameworks\n\n- Get More details: https://medium.com/@Abhishek4273/expediting-transfer-learning-the-monks-way-422d7ec40ec6\n\n## Features\n- a. A **syntax invariant wrapper** over pytorch, keras, mxnet-gluon for computer vision\n- b. Write **less code** to quickly prototype and experiment\n- c. **Compare metrics** across experiments."},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!git clone https://github.com/Tessellate-Imaging/monk_v1.git","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Custom dependency installation script for kaggle"},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -r /kaggle/working/monk_v1/installation/requirements_kaggle.txt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import sys\nsys.path.append(\"/kaggle/working/monk_v1/monk/\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Example 1 - Using pyrotch resnet50 to train the classifier in Quick Mode"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Step 0 - Using Pytorch\nfrom pytorch_prototype import prototype","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Step 1 - Create experiment\nptf = prototype(verbose=1);\nptf.Prototype(\"sample-project-1\", \"sample-experiment-1\");","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Invoke [Quick Mode](https://clever-noyce-f9d43f.netlify.com/#/quick_mode/quickmode_pytorch) "},{"metadata":{"trusted":true},"cell_type":"code","source":"# Step 2\nptf.Default(dataset_path=\"images/train/\",\n           model_name=\"resnet50\", freeze_base_network=True,\n           num_epochs=25);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"### Using Monk's [Update MODE](https://clever-noyce-f9d43f.netlify.com/#/update_mode/update_dataset)"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Additional Step\nptf.update_save_intermediate_models(False); \nptf.update_display_progress_realtime(False);\nptf.update_batch_size(32);\nptf.Reload();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Step - 3\nptf.Train();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Example 2 - Using gluon resnet50 to train the classifier in Quick Mode"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Step 0 - Using Gluon\nfrom gluon_prototype import prototype","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Step 1 - Create experiment\nptf = prototype(verbose=1);\nptf.Prototype(\"sample-project-1\", \"sample-experiment-2\");","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Step 2 - Invoke Quick Prototype Default mode\nptf.Default(dataset_path=\"images/train/\",\n           model_name=\"resnet50_v2\", freeze_base_network=True,\n           num_epochs=25);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Using Monk's [Update MODE](https://clever-noyce-f9d43f.netlify.com/#/update_mode/update_dataset)"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Additional Step\nptf.update_save_intermediate_models(False); \nptf.update_display_progress_realtime(False);\nptf.update_batch_size(32);\nptf.Reload();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Step 3 - Train\nptf.Train();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Example 3 - Using keras resnet50 to train the classifier in Quick Mode"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Step 0 - Using Keras\nfrom keras_prototype import prototype","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Step 1 - Create experiment\nptf = prototype(verbose=1);\nptf.Prototype(\"sample-project-1\", \"sample-experiment-3\");","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Step 2 - Invoke Quick Prototype Default mode\nptf.Default(dataset_path=\"images/train/\",\n           model_name=\"resnet50_v2\", freeze_base_network=True,\n           num_epochs=25);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Using Monk's [Update MODE](https://clever-noyce-f9d43f.netlify.com/#/update_mode/update_dataset)"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Additional Step\nptf.update_save_intermediate_models(False); \nptf.update_display_progress_realtime(False);\nptf.update_batch_size(32);\nptf.Reload();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ptf.Train();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Compare the experiments irrespective of backend library\n- Training accuracy curves\n- Training loss curves\n- Validation accuravy curves\n- Validation loss curves\n- GPU memory usages\n- Training times"},{"metadata":{"trusted":true},"cell_type":"code","source":"from compare_prototype import compare\n\nctf = compare(verbose=1);\nctf.Comparison(\"Sample-Comparison-1\");\nctf.Add_Experiment(\"sample-project-1\", \"sample-experiment-1\");\nctf.Add_Experiment(\"sample-project-1\", \"sample-experiment-2\");\nctf.Add_Experiment(\"sample-project-1\", \"sample-experiment-3\");\n\nctf.Generate_Statistics();","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Display comparisons"},{"metadata":{"trusted":true},"cell_type":"code","source":"from IPython.display import Image\nfrom IPython.display import display","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.listdir(\"/kaggle/working/workspace/comparison/Sample-Comparison-1/\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Compare training accuracy and loss curves"},{"metadata":{"trusted":true},"cell_type":"code","source":"x = Image(filename='/kaggle/working/workspace/comparison/Sample-Comparison-1/train_accuracy.png') \ny = Image(filename='/kaggle/working/workspace/comparison/Sample-Comparison-1/train_loss.png') \ndisplay(x, y)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Compare validation accuracy and loss curves"},{"metadata":{"trusted":true},"cell_type":"code","source":"x = Image(filename='/kaggle/working/workspace/comparison/Sample-Comparison-1/val_accuracy.png') \ny = Image(filename='/kaggle/working/workspace/comparison/Sample-Comparison-1/val_loss.png') \ndisplay(x, y)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Compare gpu memory usages and training times"},{"metadata":{"trusted":true},"cell_type":"code","source":"x = Image(filename='/kaggle/working/workspace/comparison/Sample-Comparison-1/stats_training_time.png') \ny = Image(filename='/kaggle/working/workspace/comparison/Sample-Comparison-1/stats_max_gpu_usage.png') \ndisplay(x, y)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"### Select the most suitable experiment for inferencing (As per the results you achieve)"},{"metadata":{"trusted":true},"cell_type":"code","source":"from pytorch_prototype import prototype\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Load the experiment with eval_infer tag"},{"metadata":{"trusted":true},"cell_type":"code","source":"ptf = prototype(verbose=1);\nptf.Prototype(\"sample-project-1\", \"sample-experiment-1\", eval_infer=True);","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Infer using a single command"},{"metadata":{"trusted":true},"cell_type":"code","source":"inference_dataset = \"images/test/\";\noutput = ptf.Infer(img_dir=inference_dataset);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Create submission\nimport pandas as pd\nsub = pd.read_csv(\"/kaggle/input/Kannada-MNIST/sample_submission.csv\");\nfor i in range(len(output)):\n    index = int(sub[sub['id']==int(output[i]['img_name'].split(\".\")[0])].index[0])\n    #print(output[i]);\n    #print(index);\n    sub['label'][index] = int(output[i]['predicted_class'])\n    #print(sub.iloc[index:index+1])\n    #break\nsub.to_csv(\"submission.csv\", index=False);","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"sub.iloc[0:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -r /kaggle/working/monk_v1/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -r images","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":1}