{"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\nimport pandas as pd\nfrom tensorflow.keras import Input, Model\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-15T10:44:02.559801Z","iopub.execute_input":"2022-03-15T10:44:02.560469Z","iopub.status.idle":"2022-03-15T10:44:09.147095Z","shell.execute_reply.started":"2022-03-15T10:44:02.560346Z","shell.execute_reply":"2022-03-15T10:44:09.146248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"raw_train = pd.read_csv('../input/digit-recognizer/train.csv')\nraw_test = pd.read_csv('../input/digit-recognizer/test.csv')\nraw_train = raw_train.sample(frac=1)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T10:44:30.401816Z","iopub.execute_input":"2022-03-15T10:44:30.402109Z","iopub.status.idle":"2022-03-15T10:44:36.242286Z","shell.execute_reply.started":"2022-03-15T10:44:30.402077Z","shell.execute_reply":"2022-03-15T10:44:36.241418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Reshape and transform to numpy\nx_train = raw_train.to_numpy()[:, 1:]/255\ny_train = raw_train.to_numpy()[:, 0]\nx_train = x_train.reshape((42000, 28, 28, 1))\nx_train = x_train.astype('float32')\ny_train = y_train.astype('int32')\n\n## Separate data\nx_val = x_train[38000:]\ny_val = y_train[38000:]\nx_train = x_train[:38000]\ny_train = y_train[:38000]\n\nx_test = raw_test.to_numpy()/255\nx_test = x_test.reshape((28000, 28, 28, 1))\nx_test = x_test.astype('float32')","metadata":{"execution":{"iopub.status.busy":"2022-03-15T10:44:41.500320Z","iopub.execute_input":"2022-03-15T10:44:41.501269Z","iopub.status.idle":"2022-03-15T10:44:41.796355Z","shell.execute_reply.started":"2022-03-15T10:44:41.501219Z","shell.execute_reply":"2022-03-15T10:44:41.795386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gen =ImageDataGenerator(rotation_range=8, width_shift_range=0.08, shear_range=0.3, height_shift_range=0.08, zoom_range=0.08)\nbatches = gen.flow(x_train, y_train, batch_size=32)\nval_batches = gen.flow(x_val, y_val, batch_size=32)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T10:44:45.993102Z","iopub.execute_input":"2022-03-15T10:44:45.993585Z","iopub.status.idle":"2022-03-15T10:44:46.000966Z","shell.execute_reply.started":"2022-03-15T10:44:45.993532Z","shell.execute_reply":"2022-03-15T10:44:46.000113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_tensor = Input(shape=(28, 28, 1))\nx = Conv2D(32, (3, 3), activation='relu')(input_tensor)\nx = Conv2D(64, (3, 3), activation='relu')(x)\nx = MaxPooling2D((2, 2))(x)\nx = Dropout(0.25)(x)\nx = Flatten()(x)\nx = Dense(128, activation='relu')(x)\nx = Dropout(0.5)(x)\noutput_tensor = Dense(10, activation='softmax')(x)\n\nmodel = Model(inputs=input_tensor, outputs=output_tensor)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T10:44:48.470058Z","iopub.execute_input":"2022-03-15T10:44:48.470849Z","iopub.status.idle":"2022-03-15T10:44:48.630467Z","shell.execute_reply.started":"2022-03-15T10:44:48.470807Z","shell.execute_reply":"2022-03-15T10:44:48.629472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\nhistory = model.fit_generator(generator=batches, steps_per_epoch=len(x_train)//32, validation_data=val_batches, validation_steps=len(x_val)//32, epochs=4)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T10:45:01.739467Z","iopub.execute_input":"2022-03-15T10:45:01.740097Z","iopub.status.idle":"2022-03-15T10:50:30.165380Z","shell.execute_reply.started":"2022-03-15T10:45:01.740057Z","shell.execute_reply":"2022-03-15T10:50:30.164529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"results = model.predict(x_test)\nsub = []\ni=1\nfor line in results:\n    sub.append([i, np.argmax(results[i-1, :])])\n    i+=1\n\ndf = pd.DataFrame(sub, columns=['ImageId', 'Label'])\ndf.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T10:53:16.006141Z","iopub.execute_input":"2022-03-15T10:53:16.006671Z","iopub.status.idle":"2022-03-15T10:53:24.327745Z","shell.execute_reply.started":"2022-03-15T10:53:16.006637Z","shell.execute_reply":"2022-03-15T10:53:24.327032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from plotnine import ggplot, geom_line, geom_point, aes\nhist_df = pd.DataFrame(history.history)\nhist_df.insert(0, 'Epoch', range(1, 5))\n\n(\nggplot(hist_df, aes(x='Epoch'))\n    + geom_point(aes(y='loss'), color='red')\n    + geom_point(aes(y='val_loss'), color='blue')\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T10:52:55.458977Z","iopub.execute_input":"2022-03-15T10:52:55.459286Z","iopub.status.idle":"2022-03-15T10:52:58.429314Z","shell.execute_reply.started":"2022-03-15T10:52:55.459245Z","shell.execute_reply":"2022-03-15T10:52:58.428516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(\nggplot(hist_df, aes(x='Epoch'))\n    + geom_line(aes(y='accuracy'), color='red')\n    + geom_line(aes(y='val_accuracy'), color='blue')\n)","metadata":{"execution":{"iopub.status.busy":"2022-03-15T10:53:02.523165Z","iopub.execute_input":"2022-03-15T10:53:02.523440Z","iopub.status.idle":"2022-03-15T10:53:02.831735Z","shell.execute_reply.started":"2022-03-15T10:53:02.523412Z","shell.execute_reply":"2022-03-15T10:53:02.830839Z"},"trusted":true},"execution_count":null,"outputs":[]}]}