{"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-11-26T14:50:15.319073Z","iopub.execute_input":"2022-11-26T14:50:15.319508Z","iopub.status.idle":"2022-11-26T14:50:15.350362Z","shell.execute_reply.started":"2022-11-26T14:50:15.319419Z","shell.execute_reply":"2022-11-26T14:50:15.349367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:50:15.358609Z","iopub.execute_input":"2022-11-26T14:50:15.358906Z","iopub.status.idle":"2022-11-26T14:50:21.003621Z","shell.execute_reply.started":"2022-11-26T14:50:15.358880Z","shell.execute_reply":"2022-11-26T14:50:21.002657Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv').astype('float32')\nX, y = train.drop(columns='label'), train.label.values\ndel train","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:50:21.005369Z","iopub.execute_input":"2022-11-26T14:50:21.006038Z","iopub.status.idle":"2022-11-26T14:50:24.121974Z","shell.execute_reply.started":"2022-11-26T14:50:21.006000Z","shell.execute_reply":"2022-11-26T14:50:24.121004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def change_size(image: np.array) -> np.array:\n    from tensorflow.keras.preprocessing.image import load_img, img_to_array, array_to_img\n    \"\"\"Resize the given array to (x, 75, 75, 3)\n    return np.array\n    \"\"\"\n    img = array_to_img(image, scale=False)\n    img = img.resize((75, 75))\n    img = img.convert(mode='RGB')\n    arr = img_to_array(img)\n    \n    return arr.astype(np.float32)\n\ndef reshape_x(X: pd.DataFrame) -> np.array:\n    \"\"\"Reshape X from (x, 784) to (x, 28, 28, 1)\n    \"\"\"\n    return X.values.reshape((-1, 28, 28, 1))","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:50:24.124658Z","iopub.execute_input":"2022-11-26T14:50:24.126016Z","iopub.status.idle":"2022-11-26T14:50:24.134639Z","shell.execute_reply.started":"2022-11-26T14:50:24.125987Z","shell.execute_reply":"2022-11-26T14:50:24.133674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gpu_devices = tf.config.experimental.list_physical_devices('GPU')\nfor device in gpu_devices:\n    tf.config.experimental.set_memory_growth(device, True)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:50:24.139199Z","iopub.execute_input":"2022-11-26T14:50:24.140165Z","iopub.status.idle":"2022-11-26T14:50:24.314928Z","shell.execute_reply.started":"2022-11-26T14:50:24.140122Z","shell.execute_reply":"2022-11-26T14:50:24.311485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = reshape_x(X)\ndel X\nx = np.array(tuple(map(change_size, x)))\ny = tf.keras.utils.to_categorical(y)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:50:24.317944Z","iopub.execute_input":"2022-11-26T14:50:24.318651Z","iopub.status.idle":"2022-11-26T14:50:36.342055Z","shell.execute_reply.started":"2022-11-26T14:50:24.318620Z","shell.execute_reply":"2022-11-26T14:50:36.341046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#split the data using sklearn routine \nX_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=1)\nprint(\"X_train.shape\", X_train.shape, \"y_train.shape\", y_train.shape)\nprint(\"X_test.shape\", X_test.shape, \"y_test.shape\", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:50:36.343625Z","iopub.execute_input":"2022-11-26T14:50:36.344014Z","iopub.status.idle":"2022-11-26T14:50:37.118780Z","shell.execute_reply.started":"2022-11-26T14:50:36.343978Z","shell.execute_reply":"2022-11-26T14:50:37.116868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del x, y","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:50:37.120107Z","iopub.execute_input":"2022-11-26T14:50:37.120489Z","iopub.status.idle":"2022-11-26T14:50:37.131610Z","shell.execute_reply.started":"2022-11-26T14:50:37.120453Z","shell.execute_reply":"2022-11-26T14:50:37.130576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential(\n    [\n        tf.keras.applications.resnet50.ResNet50(\n            input_shape=(75, 75, 3), include_top=False, weights=\"imagenet\", classes=10,\n        ),\n        Flatten(),\n        Dense(units=1024, activation='relu', kernel_regularizer=tf.keras.regularizers.L2(0.001)),\n        Dense(units=10, activation=\"softmax\"),\n              ]\n             )\n        \nfor layer in model.layers[0].layers:\n    if layer.name == 'conv5_block1_0_conv':\n        break\n    layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:50:37.133243Z","iopub.execute_input":"2022-11-26T14:50:37.133590Z","iopub.status.idle":"2022-11-26T14:50:41.480961Z","shell.execute_reply.started":"2022-11-26T14:50:37.133558Z","shell.execute_reply":"2022-11-26T14:50:41.479998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', \n              metrics=['accuracy'],\n              optimizer=tf.keras.optimizers.Adam(0.0005),\n             )","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:50:41.482252Z","iopub.execute_input":"2022-11-26T14:50:41.482590Z","iopub.status.idle":"2022-11-26T14:50:41.499867Z","shell.execute_reply.started":"2022-11-26T14:50:41.482556Z","shell.execute_reply":"2022-11-26T14:50:41.498932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert X_train.shape[0] == y_train.shape[0]\nassert X_test.shape[0] == y_test.shape[0]","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:50:41.503622Z","iopub.execute_input":"2022-11-26T14:50:41.503971Z","iopub.status.idle":"2022-11-26T14:50:41.518169Z","shell.execute_reply.started":"2022-11-26T14:50:41.503945Z","shell.execute_reply":"2022-11-26T14:50:41.517209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    X_train,\n    y_train,\n    epochs=8,\n    validation_data=(X_test, y_test)\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:50:41.519653Z","iopub.execute_input":"2022-11-26T14:50:41.520002Z","iopub.status.idle":"2022-11-26T14:56:15.426275Z","shell.execute_reply.started":"2022-11-26T14:50:41.519976Z","shell.execute_reply":"2022-11-26T14:56:15.425335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del X_train, y_train","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:56:15.428138Z","iopub.execute_input":"2022-11-26T14:56:15.428498Z","iopub.status.idle":"2022-11-26T14:56:15.437576Z","shell.execute_reply.started":"2022-11-26T14:56:15.428459Z","shell.execute_reply":"2022-11-26T14:56:15.436599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score = model.evaluate(X_test, y_test, verbose=0)\nprint(f\"Loss: {score[0]}\\nAccuracy: {score[1]}\")\ndel score, X_test, y_test","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:56:15.439260Z","iopub.execute_input":"2022-11-26T14:56:15.439987Z","iopub.status.idle":"2022-11-26T14:56:20.383304Z","shell.execute_reply.started":"2022-11-26T14:56:15.439951Z","shell.execute_reply":"2022-11-26T14:56:20.382060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def submit(softmax=True):\n    \"\"\"Submit the prediction depends on model loss and last layer activation function\n    if act. function is linear then result is in variable `sub`\n    if act. function is softmax then result is in variable `logit`\n    \"\"\"\n    test = reshape_x(pd.read_csv('../input/digit-recognizer/test.csv').astype('float'))\n    test = np.array(tuple(map(change_size, test)))\n    submission = pd.read_csv('../input/digit-recognizer/sample_submission.csv')\n    logit = model.predict(test)\n    if softmax:\n        submission['Label'] = logit\n    else:\n        sub = np.array([np.argmax(i) for i in logit])\n        submission['Label'] = sub\n    submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:56:20.385157Z","iopub.execute_input":"2022-11-26T14:56:20.385874Z","iopub.status.idle":"2022-11-26T14:56:20.393266Z","shell.execute_reply.started":"2022-11-26T14:56:20.385833Z","shell.execute_reply":"2022-11-26T14:56:20.392165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit(False)","metadata":{"execution":{"iopub.status.busy":"2022-11-26T14:56:20.394997Z","iopub.execute_input":"2022-11-26T14:56:20.395439Z","iopub.status.idle":"2022-11-26T14:56:55.491773Z","shell.execute_reply.started":"2022-11-26T14:56:20.395401Z","shell.execute_reply":"2022-11-26T14:56:55.490661Z"},"trusted":true},"execution_count":null,"outputs":[]}]}