{"cells":[{"metadata":{},"cell_type":"markdown","source":"https://www.kaggle.com/ateplyuk/mnist-efficientnet"},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport sys\n# Repository source: https://github.com/qubvel/efficientnet\nsys.path.append(os.path.abspath('../input/efficientnet/efficientnet-master/efficientnet-master/'))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from efficientnet import EfficientNetB3\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten \nfrom keras.models import Model\nfrom keras import optimizers\nfrom keras.utils import np_utils\nfrom keras.preprocessing.image import ImageDataGenerator\nimport cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom pathlib import Path\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%ls ../input/Kannada-MNIST/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"root_path = Path(\"../input/Kannada-MNIST/\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Load the data\ntrain = pd.read_csv(root_path / \"train.csv\")\ntest = pd.read_csv(root_path / \"test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Y_train = train[\"label\"]\n\n# Drop 'label' column\nX_train = train.drop(labels = [\"label\"],axis = 1) \n\ntest.drop('id', axis=1, inplace=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Normilize data\nX_train = X_train.astype('float32')\ntest = test.astype('float32')\nX_train /= 255\ntest /= 255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train3 = np.full((X_train.shape[0], 28, 28, 3), 0.0)\n\nfor i, s in enumerate(X_train):\n    X_train3[i] = cv2.cvtColor(s, cv2.COLOR_GRAY2RGB) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test3 = np.full((test.shape[0], 28, 28, 3), 0.0)\n\nfor i, s in enumerate(test):\n    test3[i] = cv2.cvtColor(s, cv2.COLOR_GRAY2RGB) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train3.shape, test3.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"Y_train = np_utils.to_categorical(Y_train, 10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%ls ../input/efficientnet-keras-weights-b0b5/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Load in EfficientNetB3\nmodel = EfficientNetB3(weights=None,\n                        include_top=False,\n                        input_shape=(28, 28, 3))\nmodel.load_weights('../input/efficientnet-keras-weights-b0b5/efficientnet-b3_imagenet_1000_notop.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = model.output\nx = Flatten()(x)\nx = Dense(1024, activation=\"relu\")(x)\nx = Dropout(0.5)(x)\npredictions = Dense(units = 10, activation=\"softmax\")(x)\nmodel_f = Model(input = model.input, output = predictions)\nmodel_f.compile(optimizers.rmsprop(lr=0.0001, decay=1e-6),\n                loss='categorical_crossentropy',\n                metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(\n        rotation_range= 8,  \n        zoom_range = 0.15,  \n        width_shift_range=0.2, \n        height_shift_range=0.2)\ndatagen.fit(X_train3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val1, y_train, y_val1 = train_test_split(\n    X_train3, Y_train, test_size=0.05, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint\nsize_batch = 60\ncheckpoint = ModelCheckpoint('BWeight.md5',monitor='val_loss',\n                            save_best_only=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model_f.fit_generator(datagen.flow(X_train,y_train, batch_size=size_batch),\n                              epochs = 50,\n                              validation_data = (X_val1,y_val1),\n                              verbose = 2,\n                              steps_per_epoch = X_train.shape[0] // size_batch,\n                              callbacks=[checkpoint])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import json\n\nwith open('history.json', 'w') as f:\n    json.dump(history.history, f)\n\nhistory_df = pd.DataFrame(history.history)\nhistory_df[['loss', 'val_loss']].plot()\nhistory_df[['acc', 'val_acc']].plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%%time\n# Prediction\ntest_predictions = model_f.predict(test3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# select the index with the maximum probability\nresults = np.argmax(test_predictions,axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv('../input/Kannada-MNIST/sample_submission.csv')\nsubmission['label'] = results\nsubmission.to_csv('submission.csv', index=False)\nsubmission.head()","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}