{"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":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport easyocr as ocr\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport tensorflow as tf\n\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\n#@Credit\n#Image processing used from : https://www.kaggle.com/remekkinas/step-2-find-numbers-no-model-required","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-20T09:56:52.283197Z","iopub.execute_input":"2022-03-20T09:56:52.283543Z","iopub.status.idle":"2022-03-20T09:56:59.410052Z","shell.execute_reply.started":"2022-03-20T09:56:52.283461Z","shell.execute_reply":"2022-03-20T09:56:59.409193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dict = pd.read_csv(\"../input/ultra-mnist/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-03-20T09:57:02.855792Z","iopub.execute_input":"2022-03-20T09:57:02.856187Z","iopub.status.idle":"2022-03-20T09:57:02.889494Z","shell.execute_reply.started":"2022-03-20T09:57:02.856152Z","shell.execute_reply":"2022-03-20T09:57:02.888843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dict[\"id\"]=list(map(lambda x : x+\".jpeg\",train_dict[\"id\"]))","metadata":{"execution":{"iopub.status.busy":"2022-03-20T10:01:08.385245Z","iopub.execute_input":"2022-03-20T10:01:08.385492Z","iopub.status.idle":"2022-03-20T10:01:08.398038Z","shell.execute_reply.started":"2022-03-20T10:01:08.385465Z","shell.execute_reply":"2022-03-20T10:01:08.397363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dict[\"digit_sum\"]=list(map(lambda x : str(x)+\"-1\",train_dict[\"digit_sum\"]))","metadata":{"execution":{"iopub.status.busy":"2022-03-20T10:01:45.256355Z","iopub.execute_input":"2022-03-20T10:01:45.256622Z","iopub.status.idle":"2022-03-20T10:01:45.275760Z","shell.execute_reply.started":"2022-03-20T10:01:45.256592Z","shell.execute_reply":"2022-03-20T10:01:45.275001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dict","metadata":{"execution":{"iopub.status.busy":"2022-03-20T10:01:51.517148Z","iopub.execute_input":"2022-03-20T10:01:51.517930Z","iopub.status.idle":"2022-03-20T10:01:51.530899Z","shell.execute_reply.started":"2022-03-20T10:01:51.517892Z","shell.execute_reply":"2022-03-20T10:01:51.530075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = \"../input/ultra-mnist/train\"\nxc_img_prep = tf.keras.applications.xception.preprocess_input \n\ntrain_datagen = ImageDataGenerator(preprocessing_function = xc_img_prep,validation_split = 0.15)\n\ntrain_generator = train_datagen.flow_from_dataframe(train_dict,train_dir, \n                                                   target_size = (299,299),\n                                                   batch_size = 32,\n                                                   class_mode = \"categorical\", \n                                                   x_col = \"id\",\n                                                   y_col = \"digit_sum\",\n                                                   subset = \"training\")\n\nvalid_datagen = ImageDataGenerator(preprocessing_function = xc_img_prep,validation_split = 0.15)\n\nvalid_generator = valid_datagen.flow_from_dataframe(train_dict,train_dir, \n                                                   target_size = (299,299),\n                                                   batch_size = 32,\n                                                   class_mode = \"categorical\", \n                                                   x_col = \"id\",\n                                                   y_col = \"digit_sum\",\n                                                   subset = \"validation\")","metadata":{"execution":{"iopub.status.busy":"2022-03-20T10:13:30.156406Z","iopub.execute_input":"2022-03-20T10:13:30.157152Z","iopub.status.idle":"2022-03-20T10:13:40.033243Z","shell.execute_reply.started":"2022-03-20T10:13:30.157113Z","shell.execute_reply":"2022-03-20T10:13:40.032501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = tf.keras.applications.Xception(weights = \"imagenet\", include_top = False)\nn_classes = 28","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"avg = tf.keras.layers.GlobalAveragePooling2D()(base_model.output)\noutput = tf.keras.layers.Dense(n_classes, activation = \"softmax\")(avg)\nmodel = tf.keras.Model(inputs = base_model.input, outputs = output)\n\nfor layer in base_model.layers:\n    layer.trainable = False\n    \noptimizer = tf.keras.optimizers.SGD(learning_rate= 0.2, momentum = 0.9, decay = 0.01)\nmodel.compile(loss = \"categorical_crossentropy\", optimizer = optimizer , metrics = [\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-03-20T10:13:45.221630Z","iopub.execute_input":"2022-03-20T10:13:45.222215Z","iopub.status.idle":"2022-03-20T10:13:45.261747Z","shell.execute_reply.started":"2022-03-20T10:13:45.222175Z","shell.execute_reply":"2022-03-20T10:13:45.261067Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator, epochs = 1, validation_data = valid_generator)","metadata":{"execution":{"iopub.status.busy":"2022-03-20T10:13:49.085161Z","iopub.execute_input":"2022-03-20T10:13:49.085954Z","iopub.status.idle":"2022-03-20T10:49:37.325155Z","shell.execute_reply.started":"2022-03-20T10:13:49.085904Z","shell.execute_reply":"2022-03-20T10:49:37.323934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(\"mnist_xception\")","metadata":{"execution":{"iopub.status.busy":"2022-03-20T10:51:10.997463Z","iopub.execute_input":"2022-03-20T10:51:10.998966Z","iopub.status.idle":"2022-03-20T10:51:11.353832Z","shell.execute_reply.started":"2022-03-20T10:51:10.998919Z","shell.execute_reply":"2022-03-20T10:51:11.353082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = \"../input/ultra-mnist/test\"\ntest_datagen = ImageDataGenerator(preprocessing_function = xc_img_prep)\ntest_generator = test_datagen.flow_from_directory(directory = test_dir, \n                                                   target_size = (299,299),\n                                                   batch_size = 32,\n                                                  class_mode = \"categorical\",    \n                                                 shuffle = False,\n                                                 classes = ['.']) ","metadata":{"execution":{"iopub.status.busy":"2022-03-20T10:51:54.359542Z","iopub.execute_input":"2022-03-20T10:51:54.360446Z","iopub.status.idle":"2022-03-20T10:52:25.688255Z","shell.execute_reply.started":"2022-03-20T10:51:54.360402Z","shell.execute_reply":"2022-03-20T10:52:25.687408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict_generator(test_generator, verbose = 1)\n","metadata":{"execution":{"iopub.status.busy":"2022-03-20T11:00:53.947770Z","iopub.execute_input":"2022-03-20T11:00:53.948463Z","iopub.status.idle":"2022-03-20T11:45:25.917637Z","shell.execute_reply.started":"2022-03-20T11:00:53.948430Z","shell.execute_reply":"2022-03-20T11:45:25.916870Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import joblib\n\njoblib.dump(y_pred, \"y_pred_ultra_mnist.pickle\")\n","metadata":{"execution":{"iopub.status.busy":"2022-03-20T12:07:43.878066Z","iopub.execute_input":"2022-03-20T12:07:43.878627Z","iopub.status.idle":"2022-03-20T12:07:43.890357Z","shell.execute_reply.started":"2022-03-20T12:07:43.878588Z","shell.execute_reply":"2022-03-20T12:07:43.889525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_labels = {}\n\nfor a in train_generator.class_indices:\n    class_labels[train_generator.class_indices[a]] = a","metadata":{"execution":{"iopub.status.busy":"2022-03-20T12:17:38.222651Z","iopub.execute_input":"2022-03-20T12:17:38.223110Z","iopub.status.idle":"2022-03-20T12:17:38.226881Z","shell.execute_reply.started":"2022-03-20T12:17:38.223070Z","shell.execute_reply":"2022-03-20T12:17:38.226199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_indices = np.argmax(y_pred,axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-03-20T12:19:13.583456Z","iopub.execute_input":"2022-03-20T12:19:13.583996Z","iopub.status.idle":"2022-03-20T12:19:13.594592Z","shell.execute_reply.started":"2022-03-20T12:19:13.583949Z","shell.execute_reply":"2022-03-20T12:19:13.593593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_labels = [class_labels[a] for a in pred_indices]","metadata":{"execution":{"iopub.status.busy":"2022-03-20T12:20:31.218500Z","iopub.execute_input":"2022-03-20T12:20:31.219235Z","iopub.status.idle":"2022-03-20T12:20:31.237386Z","shell.execute_reply.started":"2022-03-20T12:20:31.219196Z","shell.execute_reply":"2022-03-20T12:20:31.236665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_real_labels = list(map(lambda x : int(x.split(\"-\")[0]),pred_labels)) ","metadata":{"execution":{"iopub.status.busy":"2022-03-20T12:24:55.743618Z","iopub.execute_input":"2022-03-20T12:24:55.743882Z","iopub.status.idle":"2022-03-20T12:24:55.759125Z","shell.execute_reply.started":"2022-03-20T12:24:55.743854Z","shell.execute_reply":"2022-03-20T12:24:55.758428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = list(map(lambda x : x.split(\".\")[1][1:],test_generator.filenames))","metadata":{"execution":{"iopub.status.busy":"2022-03-20T12:23:21.693266Z","iopub.execute_input":"2022-03-20T12:23:21.693524Z","iopub.status.idle":"2022-03-20T12:23:21.712662Z","shell.execute_reply.started":"2022-03-20T12:23:21.693495Z","shell.execute_reply":"2022-03-20T12:23:21.711796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_csv = pd.DataFrame(test_filenames, columns = [\"id\"])","metadata":{"execution":{"iopub.status.busy":"2022-03-20T12:25:58.173401Z","iopub.execute_input":"2022-03-20T12:25:58.173660Z","iopub.status.idle":"2022-03-20T12:25:58.181550Z","shell.execute_reply.started":"2022-03-20T12:25:58.173632Z","shell.execute_reply":"2022-03-20T12:25:58.180723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_csv[\"digit_sum\"] = pred_real_labels","metadata":{"execution":{"iopub.status.busy":"2022-03-20T12:26:18.037564Z","iopub.execute_input":"2022-03-20T12:26:18.037868Z","iopub.status.idle":"2022-03-20T12:26:18.058411Z","shell.execute_reply.started":"2022-03-20T12:26:18.037836Z","shell.execute_reply":"2022-03-20T12:26:18.057606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_csv.to_csv(\"ultra_mnist_sub_1.csv\",index = False)","metadata":{"execution":{"iopub.status.busy":"2022-03-20T12:27:08.242542Z","iopub.execute_input":"2022-03-20T12:27:08.242793Z","iopub.status.idle":"2022-03-20T12:27:08.296053Z","shell.execute_reply.started":"2022-03-20T12:27:08.242766Z","shell.execute_reply":"2022-03-20T12:27:08.295368Z"},"trusted":true},"execution_count":null,"outputs":[]}]}