{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image\nfrom tensorflow.keras.utils import to_categorical","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_dir = \"/kaggle/input/Kannada-MNIST/\"\nmodel_dir = '/kaggle/input/mobilenet-v2-tf-kernels-96-no-top/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_96_no_top.h5'","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"# data_dir = \"/kaggle/input/Kannada-MNIST/\"\ntrain = pd.read_csv(data_dir + \"train.csv\")\ntrain.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test = pd.read_csv(data_dir + \"test.csv\")\ntest.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train_ = train.iloc[:,1:]\ny_train_ = train.iloc[:,0]\nX_test_ = test.iloc[:,1:]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def oneD_to_threeD(images, labels, row_number, shape=28):\n    images = images.iloc[0:row_number,:].values #取出部分数据（此处500）并转为uint8格式方便CV处理\n    labels = labels.iloc[0:row_number]\n    train_data = []\n    for i in range(row_number):\n        img = images[i,:]\n        img = img.reshape(28, 28) #reshape为2维矩阵\n        img = Image.fromarray(img.astype('uint8')).convert('RGB') #转化为image方便resize并转化为三通道图像\n        resized_img = img.resize((shape,shape), Image.ANTIALIAS) #resize到需要的尺寸\n        resized_img = np.array(resized_img).astype('float32') #重新转化为array以输入神经网络\n        train_data.append(resized_img)\n    train_data=np.array(train_data).astype('float32') #转为float32格式方便神经网络识别\n    labels = to_categorical(labels) #标签化label\n    return train_data, labels","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train, y_train= oneD_to_threeD(X_train_, y_train_, 20000, 96)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test, _ = oneD_to_threeD(X_test_, pd.DataFrame(range(len(X_test_))), len(X_test_), 96)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train = X_train/255 \nX_test = X_test/255","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Dropout, MaxPooling2D, GlobalAveragePooling2D\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.callbacks import ReduceLROnPlateau\n\nimport matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nfrom tensorflow import keras","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_vali, y_train, y_vali = train_test_split(X_train,\n                                                   y_train,\n                                                   test_size = 0.2)\n\nprint(X_train.shape, X_vali.shape, y_train.shape, y_vali.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# reconstructed_model = keras.models.load_model(model_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model = keras.applications.MobileNetV2(weights = model_dir, input_shape = (96, 96, 3), include_top = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential([\n    base_model,\n    GlobalAveragePooling2D(), \n    Dropout(0.4), \n    Dense(10, activation = 'softmax')])\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"optimizer = keras.optimizers.Adam(learning_rate = 0.001, beta_1 = 0.9, beta_2 = 0.999)\nmodel.compile(optimizer=optimizer,\n              loss=['categorical_crossentropy'],\n              metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range=10,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    zoom_range=0.1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen.fit(X_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epochs = 30\nbatch_size = 64\nhistory = model.fit(datagen.flow(X_train, y_train, batch_size=batch_size), epochs=epochs, \n                    validation_data=(X_vali, y_vali))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_model.trainable = True\nmodel.compile(\n    optimizer = keras.optimizers.Adam(1e-5),\n    loss = ['categorical_crossentropy'],\n    metrics = ['accuracy'],\n    )\nmodel.summary()\n\nepochs_fine = 30\nmodel.fit(datagen.flow(X_train, y_train, batch_size=batch_size), epochs=epochs_fine, \n                    validation_data=(X_vali, y_vali))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['accuracy'], label='accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\n# plt.ylim([0.5, 1])\nplt.legend(loc='lower right')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predicted = model.predict(X_test)\npredicted = np.argmax(predicted, axis = 1)\n\nindex = np.array(test.index)\nres = pd.DataFrame(np.concatenate([index[:,np.newaxis],predicted[:,np.newaxis]], axis = 1), columns = ['id', 'label'])\n\nres.to_csv('submission.csv', index = False)","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":4}