{"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":"markdown","source":"**Importing of Relevant Libraries**","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras.utils import to_categorical\nimport os\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    \n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-27T07:18:39.22375Z","iopub.execute_input":"2023-08-27T07:18:39.225379Z","iopub.status.idle":"2023-08-27T07:18:56.322919Z","shell.execute_reply.started":"2023-08-27T07:18:39.22533Z","shell.execute_reply":"2023-08-27T07:18:56.321692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data Preparation**","metadata":{}},{"cell_type":"code","source":"#Loading of data\ndata_raw = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndata_val  = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:19:25.241698Z","iopub.execute_input":"2023-08-27T07:19:25.242504Z","iopub.status.idle":"2023-08-27T07:19:31.574256Z","shell.execute_reply.started":"2023-08-27T07:19:25.242455Z","shell.execute_reply":"2023-08-27T07:19:31.573142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Separating Samples and Labels\nlabel = data_raw['label'].copy()\ndata_raw = data_raw.drop('label', axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:19:32.606746Z","iopub.execute_input":"2023-08-27T07:19:32.607155Z","iopub.status.idle":"2023-08-27T07:19:32.707556Z","shell.execute_reply.started":"2023-08-27T07:19:32.607121Z","shell.execute_reply":"2023-08-27T07:19:32.706023Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = label.values\ndata_raw = data_raw.values","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:19:34.4759Z","iopub.execute_input":"2023-08-27T07:19:34.476346Z","iopub.status.idle":"2023-08-27T07:19:34.481184Z","shell.execute_reply.started":"2023-08-27T07:19:34.476314Z","shell.execute_reply":"2023-08-27T07:19:34.480046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating Training and Validation sets\nX_train, X_val, y_train, y_val = train_test_split(data_raw, label, test_size=0.1, random_state=28, shuffle=True, stratify=label)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:19:37.855201Z","iopub.execute_input":"2023-08-27T07:19:37.85561Z","iopub.status.idle":"2023-08-27T07:19:38.259313Z","shell.execute_reply.started":"2023-08-27T07:19:37.855576Z","shell.execute_reply":"2023-08-27T07:19:38.258272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reshaping the pixels to form the Image \nX_train = X_train.reshape(-1, 28, 28, 1) / 255.0\nX_val = X_val.reshape(-1, 28, 28, 1) / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:19:39.16196Z","iopub.execute_input":"2023-08-27T07:19:39.162366Z","iopub.status.idle":"2023-08-27T07:19:39.326834Z","shell.execute_reply.started":"2023-08-27T07:19:39.162335Z","shell.execute_reply":"2023-08-27T07:19:39.324474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess(image, label):\n    rgb_image = tf.image.grayscale_to_rgb(tf.convert_to_tensor(image)) #Converting the Images to RGB from Grayscale\n    resized_image = tf.image.resize(rgb_image, (224, 224)) #Resizing the Images for use in Resnet50\n    final_image = keras.applications.resnet50.preprocess_input(resized_image)\n    return final_image, label","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:23:49.856409Z","iopub.execute_input":"2023-08-27T07:23:49.856771Z","iopub.status.idle":"2023-08-27T07:23:49.864141Z","shell.execute_reply.started":"2023-08-27T07:23:49.856738Z","shell.execute_reply":"2023-08-27T07:23:49.862999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32\nepochs = 20","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:22:12.081346Z","iopub.execute_input":"2023-08-27T07:22:12.081741Z","iopub.status.idle":"2023-08-27T07:22:12.086941Z","shell.execute_reply.started":"2023-08-27T07:22:12.081708Z","shell.execute_reply":"2023-08-27T07:22:12.085646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## initialise the Dataset class, the requirement is that X_train and y_train are same length \n## and they are arrays/list where each element is the x or y of the datapoint\ntrain_dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))\n\n## shuffle is to ensure that there is random perm each epoch, buffer is how much your computer\n## will store in memory for faster access\ntrain_dataset = train_dataset.shuffle(buffer_size=len(X_train))\n\n## batching, drop_remainder means that the last incomplete batch will not run,\n## can toggle to true also\ntrain_dataset = train_dataset.batch(batch_size, drop_remainder=True)\n\n## map is to add the map function which they will dynamically run the function on each element\n## when they internally load the batch\ntrain_dataset = train_dataset.map(preprocess, num_parallel_calls=1)\n\n## dont care about the repeat for now\n# train_dataset = train_dataset.repeat(-1)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:32:14.442957Z","iopub.execute_input":"2023-08-27T07:32:14.443697Z","iopub.status.idle":"2023-08-27T07:32:14.930706Z","shell.execute_reply.started":"2023-08-27T07:32:14.443643Z","shell.execute_reply":"2023-08-27T07:32:14.929665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# everything is the same as train_dataset except no need for shuffling\nval_dataset = tf.data.Dataset.from_tensor_slices((X_val, y_val))\nval_dataset = val_dataset.map(preprocess, num_parallel_calls=1)\nval_dataset = val_dataset.batch(batch_size, drop_remainder=True)\n# val_dataset = val_dataset.repeat(-1)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:32:14.935474Z","iopub.execute_input":"2023-08-27T07:32:14.935797Z","iopub.status.idle":"2023-08-27T07:32:14.992528Z","shell.execute_reply.started":"2023-08-27T07:32:14.93577Z","shell.execute_reply":"2023-08-27T07:32:14.991494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Creating a model with Resnet50 as the Body**","metadata":{}},{"cell_type":"code","source":"base_model = keras.applications.resnet50.ResNet50(weights=\"imagenet\", include_top=False)\navg = keras.layers.GlobalAveragePooling2D()(base_model.output)\nx = keras.layers.Dense(256, activation='relu')(avg)\noutput = keras.layers.Dense(10, activation=\"softmax\")(x)\nmodel = keras.Model(inputs=base_model.input, outputs=output)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:30:14.89785Z","iopub.execute_input":"2023-08-27T07:30:14.898367Z","iopub.status.idle":"2023-08-27T07:30:17.818212Z","shell.execute_reply.started":"2023-08-27T07:30:14.898324Z","shell.execute_reply":"2023-08-27T07:30:17.817275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Freezing the layers in Resnet50\nfor layer in base_model.layers:\n    layer.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:30:24.525032Z","iopub.execute_input":"2023-08-27T07:30:24.525438Z","iopub.status.idle":"2023-08-27T07:30:24.540978Z","shell.execute_reply.started":"2023-08-27T07:30:24.525404Z","shell.execute_reply":"2023-08-27T07:30:24.537553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Compiling the model with a Adam optimizer with a relatively higher Learning Rate\noptimizer = keras.optimizers.Adam(learning_rate=0.001)\nmodel.compile(loss=\"sparse_categorical_crossentropy\", optimizer=optimizer,\n metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:30:26.431557Z","iopub.execute_input":"2023-08-27T07:30:26.432435Z","iopub.status.idle":"2023-08-27T07:30:26.465184Z","shell.execute_reply.started":"2023-08-27T07:30:26.432389Z","shell.execute_reply":"2023-08-27T07:30:26.463478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating Callbacks \ncheckpoint_cb = keras.callbacks.ModelCheckpoint(\n    filepath='model_weights_epoch{epoch:02d}.h5', \n    save_weights_only=True,  \n    save_freq='epoch')\nlr_scheduler = keras.callbacks.ReduceLROnPlateau(factor=0.5, patience=3)\nearly_stopping_cb = keras.callbacks.EarlyStopping(patience=5, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:30:51.797908Z","iopub.execute_input":"2023-08-27T07:30:51.798322Z","iopub.status.idle":"2023-08-27T07:30:51.805124Z","shell.execute_reply.started":"2023-08-27T07:30:51.798291Z","shell.execute_reply":"2023-08-27T07:30:51.804004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 20","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:31:21.553162Z","iopub.execute_input":"2023-08-27T07:31:21.553558Z","iopub.status.idle":"2023-08-27T07:31:21.558199Z","shell.execute_reply.started":"2023-08-27T07:31:21.553526Z","shell.execute_reply":"2023-08-27T07:31:21.557237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_dataset, epochs=epochs, validation_data=val_dataset, callbacks=[lr_scheduler,early_stopping_cb,checkpoint_cb])","metadata":{"execution":{"iopub.status.busy":"2023-08-27T07:32:21.572118Z","iopub.execute_input":"2023-08-27T07:32:21.572521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for layer in base_model.layers:\n    layer.trainable = True","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.876941Z","iopub.status.idle":"2023-08-27T04:55:23.878053Z","shell.execute_reply.started":"2023-08-27T04:55:23.877831Z","shell.execute_reply":"2023-08-27T04:55:23.877853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#optimizer = keras.optimizers.Adam(learning_rate=0.0001)\n#model.compile(loss=\"sparse_categorical_crossentropy\", optimizer=optimizer,\n#metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.879306Z","iopub.status.idle":"2023-08-27T04:55:23.879716Z","shell.execute_reply.started":"2023-08-27T04:55:23.879514Z","shell.execute_reply":"2023-08-27T04:55:23.879534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.fit(train_generator, epochs=epochs, steps_per_epoch=steps_per_epoch, validation_data=val_generator, validation_steps=val_steps_per_epoch, callbacks=[lr_scheduler,early_stopping_cb,checkpoint_cb])","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.881703Z","iopub.status.idle":"2023-08-27T04:55:23.883297Z","shell.execute_reply.started":"2023-08-27T04:55:23.882887Z","shell.execute_reply":"2023-08-27T04:55:23.88292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.load_weights('/kaggle/working/model_weights_epoch04.h5')","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.88457Z","iopub.status.idle":"2023-08-27T04:55:23.884939Z","shell.execute_reply.started":"2023-08-27T04:55:23.884776Z","shell.execute_reply":"2023-08-27T04:55:23.884792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.886455Z","iopub.status.idle":"2023-08-27T04:55:23.886798Z","shell.execute_reply.started":"2023-08-27T04:55:23.886647Z","shell.execute_reply":"2023-08-27T04:55:23.886662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#data_reshaped = data_val.values.reshape(-1, 28, 28, 1) / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.888282Z","iopub.status.idle":"2023-08-27T04:55:23.888612Z","shell.execute_reply.started":"2023-08-27T04:55:23.888468Z","shell.execute_reply":"2023-08-27T04:55:23.888483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''''\nbatch_size = 32\nnum_samples = len(data_reshaped) \n\nall_predictions = []\n\n\nfor batch_start in range(0, num_samples, batch_size):\n    batch_end = batch_start + batch_size\n    batch_data = data_reshaped[batch_start:batch_end]  \n\n    preprocessed_batch = batch_preprocess_data(batch_data)\n\n    batch_predictions = model.predict(preprocessed_batch)\n\n    all_predictions.append(batch_predictions)\n\nall_predictions = np.concatenate(all_predictions, axis=0)\n'''''","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.889843Z","iopub.status.idle":"2023-08-27T04:55:23.89017Z","shell.execute_reply.started":"2023-08-27T04:55:23.890027Z","shell.execute_reply":"2023-08-27T04:55:23.890041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predictions = all_predictions.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.892224Z","iopub.status.idle":"2023-08-27T04:55:23.89257Z","shell.execute_reply.started":"2023-08-27T04:55:23.892402Z","shell.execute_reply":"2023-08-27T04:55:23.892418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predictions","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.893824Z","iopub.status.idle":"2023-08-27T04:55:23.894143Z","shell.execute_reply.started":"2023-08-27T04:55:23.894003Z","shell.execute_reply":"2023-08-27T04:55:23.894017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predictions = [x.index(max(x)) for x in predictions]","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.895147Z","iopub.status.idle":"2023-08-27T04:55:23.895443Z","shell.execute_reply.started":"2023-08-27T04:55:23.895302Z","shell.execute_reply":"2023-08-27T04:55:23.895315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predictions[:10]","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.896424Z","iopub.status.idle":"2023-08-27T04:55:23.896707Z","shell.execute_reply.started":"2023-08-27T04:55:23.896578Z","shell.execute_reply":"2023-08-27T04:55:23.896591Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#dict = {'ImageId': [x for x in range(1,len(data_val) + 1)] , 'Label': predictions}","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.897894Z","iopub.status.idle":"2023-08-27T04:55:23.898202Z","shell.execute_reply.started":"2023-08-27T04:55:23.89806Z","shell.execute_reply":"2023-08-27T04:55:23.898074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission = pd.DataFrame(dict)","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.89914Z","iopub.status.idle":"2023-08-27T04:55:23.899423Z","shell.execute_reply.started":"2023-08-27T04:55:23.899288Z","shell.execute_reply":"2023-08-27T04:55:23.8993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.900558Z","iopub.status.idle":"2023-08-27T04:55:23.900855Z","shell.execute_reply.started":"2023-08-27T04:55:23.900706Z","shell.execute_reply":"2023-08-27T04:55:23.900721Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#submission.to_csv('mnist_submission.csv', index=False)\n#print(\"Your submission was successfully saved!\")","metadata":{"execution":{"iopub.status.busy":"2023-08-27T04:55:23.901822Z","iopub.status.idle":"2023-08-27T04:55:23.902119Z","shell.execute_reply.started":"2023-08-27T04:55:23.901961Z","shell.execute_reply":"2023-08-27T04:55:23.901975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}