{"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":"# 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-06-30T05:40:03.950512Z","iopub.execute_input":"2022-06-30T05:40:03.951278Z","iopub.status.idle":"2022-06-30T05:40:03.990521Z","shell.execute_reply.started":"2022-06-30T05:40:03.951143Z","shell.execute_reply":"2022-06-30T05:40:03.988891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport PIL\nimport tensorflow as tf\nfrom keras.layers.core import Dense\nfrom keras.layers.core import Flatten\nfrom keras import layers\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.optimizers import Adam","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:40:03.99414Z","iopub.execute_input":"2022-06-30T05:40:03.995184Z","iopub.status.idle":"2022-06-30T05:40:12.9229Z","shell.execute_reply.started":"2022-06-30T05:40:03.995023Z","shell.execute_reply":"2022-06-30T05:40:12.921281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnist_train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\nmnist_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:40:12.92787Z","iopub.execute_input":"2022-06-30T05:40:12.928553Z","iopub.status.idle":"2022-06-30T05:40:16.863672Z","shell.execute_reply.started":"2022-06-30T05:40:12.928506Z","shell.execute_reply":"2022-06-30T05:40:16.862356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = mnist_train.iloc[:, 1:]\ny_train = mnist_train.iloc[:, 0]","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:40:16.867292Z","iopub.execute_input":"2022-06-30T05:40:16.867997Z","iopub.status.idle":"2022-06-30T05:40:16.874026Z","shell.execute_reply.started":"2022-06-30T05:40:16.867949Z","shell.execute_reply":"2022-06-30T05:40:16.87263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:40:16.875912Z","iopub.execute_input":"2022-06-30T05:40:16.876735Z","iopub.status.idle":"2022-06-30T05:40:16.894183Z","shell.execute_reply.started":"2022-06-30T05:40:16.876683Z","shell.execute_reply":"2022-06-30T05:40:16.892928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = np.array(x_train).reshape(-1,28,28)\nY_train = np.array(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:40:16.896838Z","iopub.execute_input":"2022-06-30T05:40:16.897654Z","iopub.status.idle":"2022-06-30T05:40:17.01056Z","shell.execute_reply.started":"2022-06-30T05:40:16.897595Z","shell.execute_reply":"2022-06-30T05:40:17.009237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape, Y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:40:17.012377Z","iopub.execute_input":"2022-06-30T05:40:17.013598Z","iopub.status.idle":"2022-06-30T05:40:17.02215Z","shell.execute_reply.started":"2022-06-30T05:40:17.013538Z","shell.execute_reply":"2022-06-30T05:40:17.020358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Channel Stacking","metadata":{}},{"cell_type":"code","source":"X_train = np.stack((X_train,)*3, axis=-1)\nprint(X_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:40:17.024419Z","iopub.execute_input":"2022-06-30T05:40:17.025937Z","iopub.status.idle":"2022-06-30T05:40:17.579755Z","shell.execute_reply.started":"2022-06-30T05:40:17.025888Z","shell.execute_reply":"2022-06-30T05:40:17.578207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> to use resnet model, we have to import images from directory so we have to use flow_from_directory, we will convert the array format to image","metadata":{}},{"cell_type":"code","source":"from fastai import *\nfrom fastai.vision import *\nfrom fastai.vision.all import *\nimport imageio\n\ndef save_images(path:Path, data, labels):\n    for label in np.unique(labels):\n        (path/str(label)).mkdir(parents=True,exist_ok=True)\n    for i in range(len(data)):\n        if(len(labels)!=0):\n            imageio.imsave( str( path/str(labels[i])/(str(i)+'.jpg') ), data[i])\n        else:\n            imageio.imsave( str( path/(str(i)+'.jpg') ), data[i])","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:40:17.585748Z","iopub.execute_input":"2022-06-30T05:40:17.586066Z","iopub.status.idle":"2022-06-30T05:40:21.510372Z","shell.execute_reply.started":"2022-06-30T05:40:17.586036Z","shell.execute_reply":"2022-06-30T05:40:21.509004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_images(Path('/data/digits'),X_train,y_train)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:40:21.516796Z","iopub.execute_input":"2022-06-30T05:40:21.520214Z","iopub.status.idle":"2022-06-30T05:41:25.25848Z","shell.execute_reply.started":"2022-06-30T05:40:21.52017Z","shell.execute_reply":"2022-06-30T05:41:25.257076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Checking Directory","metadata":{}},{"cell_type":"code","source":"print('Total Number of Classes :', len(os.listdir('/data/digits')))\nprint('Images with label 1: ', len(os.listdir('/data/digits/1')))\n\nprint('Image names with label 1')\nprint(os.listdir('/data/digits/1')[:5])","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:41:25.264823Z","iopub.execute_input":"2022-06-30T05:41:25.267998Z","iopub.status.idle":"2022-06-30T05:41:25.308599Z","shell.execute_reply.started":"2022-06-30T05:41:25.267949Z","shell.execute_reply":"2022-06-30T05:41:25.307077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.resnet50 import preprocess_input\nimport tensorflow as tf\n\ntrain_datagen=tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=preprocess_input, validation_split=0.15)\ntrain_generator = train_datagen.flow_from_directory('/data/digits', class_mode='categorical', subset='training')\nvalid_generator = train_datagen.flow_from_directory('/data/digits', class_mode='categorical', subset='validation')","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:41:25.31457Z","iopub.execute_input":"2022-06-30T05:41:25.31812Z","iopub.status.idle":"2022-06-30T05:41:29.677381Z","shell.execute_reply.started":"2022-06-30T05:41:25.318071Z","shell.execute_reply":"2022-06-30T05:41:29.676016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x, y = train_generator[0]\nprint(x.shape)\nprint(y.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:41:29.683052Z","iopub.execute_input":"2022-06-30T05:41:29.686108Z","iopub.status.idle":"2022-06-30T05:41:29.773461Z","shell.execute_reply.started":"2022-06-30T05:41:29.686061Z","shell.execute_reply":"2022-06-30T05:41:29.772025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model = Sequential()\n\npretrained_model= tf.keras.applications.ResNet50(include_top=False,\n                   input_shape=(32,32,3),\n                   pooling='avg',classes=10,\n                   weights='imagenet')\n\nfor layer in pretrained_model.layers:\n        layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:41:29.779341Z","iopub.execute_input":"2022-06-30T05:41:29.782456Z","iopub.status.idle":"2022-06-30T05:41:45.361279Z","shell.execute_reply.started":"2022-06-30T05:41:29.782408Z","shell.execute_reply":"2022-06-30T05:41:45.359789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.add(pretrained_model)\nresnet_model.add(Dense(512, activation='relu'))\nresnet_model.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:41:45.368375Z","iopub.execute_input":"2022-06-30T05:41:45.371635Z","iopub.status.idle":"2022-06-30T05:41:46.160858Z","shell.execute_reply.started":"2022-06-30T05:41:45.37157Z","shell.execute_reply":"2022-06-30T05:41:46.159375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:41:46.167044Z","iopub.execute_input":"2022-06-30T05:41:46.170163Z","iopub.status.idle":"2022-06-30T05:41:46.204399Z","shell.execute_reply.started":"2022-06-30T05:41:46.170115Z","shell.execute_reply":"2022-06-30T05:41:46.203056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:41:46.21025Z","iopub.execute_input":"2022-06-30T05:41:46.213891Z","iopub.status.idle":"2022-06-30T05:41:46.240909Z","shell.execute_reply.started":"2022-06-30T05:41:46.213829Z","shell.execute_reply":"2022-06-30T05:41:46.239626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.fit_generator(generator=train_generator,epochs=15,validation_data=valid_generator)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T05:41:46.24645Z","iopub.execute_input":"2022-06-30T05:41:46.249706Z","iopub.status.idle":"2022-06-30T06:22:09.986348Z","shell.execute_reply.started":"2022-06-30T05:41:46.249657Z","shell.execute_reply":"2022-06-30T06:22:09.983998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnist_test = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\nmnist_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-30T06:22:09.98856Z","iopub.execute_input":"2022-06-30T06:22:09.989046Z","iopub.status.idle":"2022-06-30T06:22:12.997774Z","shell.execute_reply.started":"2022-06-30T06:22:09.989001Z","shell.execute_reply":"2022-06-30T06:22:12.996492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mnist_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-30T06:28:59.748717Z","iopub.execute_input":"2022-06-30T06:28:59.749335Z","iopub.status.idle":"2022-06-30T06:28:59.760005Z","shell.execute_reply.started":"2022-06-30T06:28:59.749264Z","shell.execute_reply":"2022-06-30T06:28:59.758635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = mnist_test\nX_test = np.array(X_test).reshape(-1,28,28)\nX_test = np.stack((X_test,)*3, axis=-1)\n\nprint(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T07:10:58.426053Z","iopub.execute_input":"2022-06-30T07:10:58.426907Z","iopub.status.idle":"2022-06-30T07:10:59.993004Z","shell.execute_reply.started":"2022-06-30T07:10:58.426865Z","shell.execute_reply":"2022-06-30T07:10:59.990996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_test_images(path:Path, data):\n    j='1'\n    (path/j).mkdir(parents=True,exist_ok=True)\n    \n    for i in range(len(data)):\n        imageio.imsave( str( path/j/(str(i)+'.jpg') ), data[i])","metadata":{"execution":{"iopub.status.busy":"2022-06-30T07:17:13.128252Z","iopub.execute_input":"2022-06-30T07:17:13.128911Z","iopub.status.idle":"2022-06-30T07:17:13.144511Z","shell.execute_reply.started":"2022-06-30T07:17:13.128863Z","shell.execute_reply":"2022-06-30T07:17:13.142941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_test_images(Path('/data/test'),X_test)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T07:20:08.324429Z","iopub.execute_input":"2022-06-30T07:20:08.325128Z","iopub.status.idle":"2022-06-30T07:20:56.03725Z","shell.execute_reply.started":"2022-06-30T07:20:08.325078Z","shell.execute_reply":"2022-06-30T07:20:56.035836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Total Number of test image :', len(os.listdir('/data/test')))\nprint(os.listdir('/data/test/1')[:6])","metadata":{"execution":{"iopub.status.busy":"2022-06-30T07:21:12.438747Z","iopub.execute_input":"2022-06-30T07:21:12.439322Z","iopub.status.idle":"2022-06-30T07:21:12.485198Z","shell.execute_reply.started":"2022-06-30T07:21:12.43926Z","shell.execute_reply":"2022-06-30T07:21:12.483787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = train_datagen.flow_from_directory('/data/test', class_mode='categorical')","metadata":{"execution":{"iopub.status.busy":"2022-06-30T07:21:24.739482Z","iopub.execute_input":"2022-06-30T07:21:24.740045Z","iopub.status.idle":"2022-06-30T07:21:26.005859Z","shell.execute_reply.started":"2022-06-30T07:21:24.739999Z","shell.execute_reply":"2022-06-30T07:21:26.004376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"STEP_SIZE_TEST= test_generator.n \ntest_generator.reset()\npredictions = resnet_model.predict_generator(test_generator,steps=STEP_SIZE_TEST, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T07:22:11.128546Z","iopub.execute_input":"2022-06-30T07:22:11.129157Z","iopub.status.idle":"2022-06-30T07:23:44.25876Z","shell.execute_reply.started":"2022-06-30T07:22:11.129107Z","shell.execute_reply":"2022-06-30T07:23:44.25747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_class_indices=np.argmax(predictions,axis=1)","metadata":{"execution":{"iopub.status.busy":"2022-06-30T07:25:37.52932Z","iopub.execute_input":"2022-06-30T07:25:37.529968Z","iopub.status.idle":"2022-06-30T07:25:37.540508Z","shell.execute_reply.started":"2022-06-30T07:25:37.529921Z","shell.execute_reply":"2022-06-30T07:25:37.538916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_class_indices[:10]","metadata":{"execution":{"iopub.status.busy":"2022-06-30T07:25:46.071776Z","iopub.execute_input":"2022-06-30T07:25:46.072392Z","iopub.status.idle":"2022-06-30T07:25:46.085135Z","shell.execute_reply.started":"2022-06-30T07:25:46.072343Z","shell.execute_reply":"2022-06-30T07:25:46.083569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Id = [x for x in range(1,28001)]","metadata":{"execution":{"iopub.status.busy":"2022-06-30T07:31:51.927411Z","iopub.execute_input":"2022-06-30T07:31:51.928987Z","iopub.status.idle":"2022-06-30T07:31:51.938767Z","shell.execute_reply.started":"2022-06-30T07:31:51.928938Z","shell.execute_reply":"2022-06-30T07:31:51.936984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.DataFrame({'ImageId': Id, 'Label': predicted_class_indices})\noutput.to_csv('final_predictions.csv', index=False)\nprint(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2022-06-30T07:32:01.070168Z","iopub.execute_input":"2022-06-30T07:32:01.070788Z","iopub.status.idle":"2022-06-30T07:32:01.175794Z","shell.execute_reply.started":"2022-06-30T07:32:01.070741Z","shell.execute_reply":"2022-06-30T07:32:01.174071Z"},"trusted":true},"execution_count":null,"outputs":[]}]}