{"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 pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport keras","metadata":{"id":"pn8Rt-zP03eX","execution":{"iopub.status.busy":"2023-07-07T10:27:50.318357Z","iopub.execute_input":"2023-07-07T10:27:50.319005Z","iopub.status.idle":"2023-07-07T10:27:50.325128Z","shell.execute_reply.started":"2023-07-07T10:27:50.318960Z","shell.execute_reply":"2023-07-07T10:27:50.323874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_X = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"id":"09t7R5j71tRm","outputId":"969d8df0-ed0e-4960-9fe5-33e124ff9e5d","execution":{"iopub.status.busy":"2023-07-07T10:27:50.327572Z","iopub.execute_input":"2023-07-07T10:27:50.328211Z","iopub.status.idle":"2023-07-07T10:27:57.203258Z","shell.execute_reply.started":"2023-07-07T10:27:50.328176Z","shell.execute_reply":"2023-07-07T10:27:57.202001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#手寫字資料集\n數據包含label(圖像數字)和28*28=784個像素數據\n\n數據以灰階表示，0為黑色，255為白色。","metadata":{"id":"45x_FmAz1HIQ"}},{"cell_type":"code","source":"train_X = train.drop(['label'],axis = 1)\ntrain_Y = train['label']","metadata":{"id":"1tQi_-sX210v","execution":{"iopub.status.busy":"2023-07-07T10:27:57.204692Z","iopub.execute_input":"2023-07-07T10:27:57.205059Z","iopub.status.idle":"2023-07-07T10:27:57.333065Z","shell.execute_reply.started":"2023-07-07T10:27:57.205029Z","shell.execute_reply":"2023-07-07T10:27:57.331748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique, count = np.unique(train_Y, return_counts=True)\nprint(\"Train_label_number:\",dict(zip(unique,count)))","metadata":{"id":"HaSGA-DI30AY","outputId":"35e89635-64d0-4a84-db7d-3252d414aeb9","execution":{"iopub.status.busy":"2023-07-07T10:27:57.335735Z","iopub.execute_input":"2023-07-07T10:27:57.336104Z","iopub.status.idle":"2023-07-07T10:27:57.346709Z","shell.execute_reply.started":"2023-07-07T10:27:57.336074Z","shell.execute_reply":"2023-07-07T10:27:57.345217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# 數據預處理\ndataset的數據為0-255(灰階)之間，0為黑色、255為白色\n\n將數值整理再(0，1)之間\n有助於提高模型的穩定性和收斂速度。\n\n減少數值的範圍，有助於避免梯度消失或爆炸的問題。\n\n歸一化後的像素值更易於解釋和比較，因為它們介於 0 到 1 之間。","metadata":{"id":"S8Fu7dRF5GJJ"}},{"cell_type":"code","source":"train_X = train_X / 255\ntest_X = test_X / 255","metadata":{"id":"fLsfsXkK5QU1","execution":{"iopub.status.busy":"2023-07-07T10:27:57.348704Z","iopub.execute_input":"2023-07-07T10:27:57.349267Z","iopub.status.idle":"2023-07-07T10:27:57.476967Z","shell.execute_reply.started":"2023-07-07T10:27:57.349224Z","shell.execute_reply":"2023-07-07T10:27:57.476081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X","metadata":{"id":"C4XI0lQrBj0o","outputId":"7ae6fed3-c7bc-4b63-af04-8a11ca08e876","execution":{"iopub.status.busy":"2023-07-07T10:27:57.477934Z","iopub.execute_input":"2023-07-07T10:27:57.478255Z","iopub.status.idle":"2023-07-07T10:27:57.546186Z","shell.execute_reply.started":"2023-07-07T10:27:57.478227Z","shell.execute_reply":"2023-07-07T10:27:57.545097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train_X 和 test_X 是原始圖像資料集，每個圖像的形狀為 (28, 28)。然而，深度學習模型通常要求輸入為四維陣列，形狀為 (樣本數, 高度, 寬度, 通道數)。n\\n\\\n因此，使用 reshape() 方法將資料集重新塑形。n\\n\\\n樣本數=-1:自動計算樣本數\n通道數：灰度為1，彩色為3","metadata":{"id":"RBrHYaA_QNCz"}},{"cell_type":"code","source":"train_X = train_X.values.reshape(-1, 28, 28, 1)\ntest_X = test_X.values.reshape(-1, 28, 28, 1)","metadata":{"id":"PRJ-Ngqv7XKV","execution":{"iopub.status.busy":"2023-07-07T10:27:57.547531Z","iopub.execute_input":"2023-07-07T10:27:57.547861Z","iopub.status.idle":"2023-07-07T10:27:57.552766Z","shell.execute_reply.started":"2023-07-07T10:27:57.547833Z","shell.execute_reply":"2023-07-07T10:27:57.552013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X","metadata":{"id":"IbznrgljB_C6","outputId":"2f9281ae-e93b-4cca-a5e4-ad926e0ca8db","execution":{"iopub.status.busy":"2023-07-07T10:27:57.553903Z","iopub.execute_input":"2023-07-07T10:27:57.554847Z","iopub.status.idle":"2023-07-07T10:27:57.573221Z","shell.execute_reply.started":"2023-07-07T10:27:57.554803Z","shell.execute_reply":"2023-07-07T10:27:57.572018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##shuffle","metadata":{"id":"if-OnXPNbEmw"}},{"cell_type":"code","source":"permutation = np.random.permutation(len(train_X))\n\ntrain_X = train_X[permutation]\ntrain_Y = train_Y[permutation]","metadata":{"id":"MAaSGuh77vuc","execution":{"iopub.status.busy":"2023-07-07T10:27:57.575041Z","iopub.execute_input":"2023-07-07T10:27:57.576198Z","iopub.status.idle":"2023-07-07T10:27:57.998481Z","shell.execute_reply.started":"2023-07-07T10:27:57.576154Z","shell.execute_reply":"2023-07-07T10:27:57.997265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = 10\ninput_shape = 784\n\ntrain_Y = keras.utils.to_categorical(train_Y, num_classes)##以one-hot encoding表示label，因為(0，9)的標籤數值過於稀疏不適合神經網路預測層","metadata":{"id":"lN1Iei_dOJ-V","execution":{"iopub.status.busy":"2023-07-07T10:27:58.003171Z","iopub.execute_input":"2023-07-07T10:27:58.003566Z","iopub.status.idle":"2023-07-07T10:27:58.010268Z","shell.execute_reply.started":"2023-07-07T10:27:58.003534Z","shell.execute_reply":"2023-07-07T10:27:58.009006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = train_X[:10]\nlabels = train_Y[:10]\n\n# Create a grid of subplots to display the images\nfig, axes = plt.subplots(2, 5, figsize=(6, 3))\naxes = axes.flatten()\n\n# Iterate over the images and plot them\nfor i in range(10):\n    axes[i].imshow(images[i], cmap='gray')\n    axes[i].axis('off')\n    axes[i].set_title('Label: {}'.format(np.argmax(labels[i])))##np.argmax:回傳含有最大值的索引\n\nplt.tight_layout()\nplt.show()","metadata":{"id":"J61ExcYPOgIz","outputId":"dc891ea6-e128-41c4-93b5-272bae7fe9cb","execution":{"iopub.status.busy":"2023-07-07T10:27:58.011790Z","iopub.execute_input":"2023-07-07T10:27:58.012187Z","iopub.status.idle":"2023-07-07T10:27:59.116069Z","shell.execute_reply.started":"2023-07-07T10:27:58.012157Z","shell.execute_reply":"2023-07-07T10:27:59.114739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{"id":"N8dRLIdMv6zk"}},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Activation, Dropout\nfrom keras.utils import to_categorical, plot_model","metadata":{"id":"UUxYwuFWv5xI","execution":{"iopub.status.busy":"2023-07-07T10:27:59.117545Z","iopub.execute_input":"2023-07-07T10:27:59.118043Z","iopub.status.idle":"2023-07-07T10:27:59.124128Z","shell.execute_reply.started":"2023-07-07T10:27:59.117999Z","shell.execute_reply":"2023-07-07T10:27:59.123070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_Y","metadata":{"id":"FWSituh2xPQW","outputId":"7c7e066e-c066-47be-c7a3-8fcb7176debf","execution":{"iopub.status.busy":"2023-07-07T10:27:59.125734Z","iopub.execute_input":"2023-07-07T10:27:59.126737Z","iopub.status.idle":"2023-07-07T10:27:59.141589Z","shell.execute_reply.started":"2023-07-07T10:27:59.126702Z","shell.execute_reply":"2023-07-07T10:27:59.140201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X.shape","metadata":{"id":"ktkYaVPk1zPD","outputId":"11cea423-beea-4325-a503-458ebb415c89","execution":{"iopub.status.busy":"2023-07-07T10:27:59.143355Z","iopub.execute_input":"2023-07-07T10:27:59.144205Z","iopub.status.idle":"2023-07-07T10:27:59.156102Z","shell.execute_reply.started":"2023-07-07T10:27:59.144170Z","shell.execute_reply":"2023-07-07T10:27:59.154919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = train_X.shape[1]\ninput_size = image_size*image_size\ninput_size","metadata":{"id":"hWeH2ch76R23","outputId":"7589e493-5e1d-4aa8-8406-8fdcd1e3c8f2","execution":{"iopub.status.busy":"2023-07-07T10:27:59.157769Z","iopub.execute_input":"2023-07-07T10:27:59.158999Z","iopub.status.idle":"2023-07-07T10:27:59.170093Z","shell.execute_reply.started":"2023-07-07T10:27:59.158753Z","shell.execute_reply":"2023-07-07T10:27:59.168855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = np.reshape(train_X, [-1, input_size])\ntest_X = np.reshape(test_X, [-1, input_size])","metadata":{"id":"VpEF5XhUDdAf","execution":{"iopub.status.busy":"2023-07-07T10:27:59.171972Z","iopub.execute_input":"2023-07-07T10:27:59.172328Z","iopub.status.idle":"2023-07-07T10:27:59.183551Z","shell.execute_reply.started":"2023-07-07T10:27:59.172298Z","shell.execute_reply":"2023-07-07T10:27:59.182168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X.shape","metadata":{"id":"hUMMyxJUDvVU","outputId":"29e52c4c-e0e0-4920-de90-97b0a7d5d54e","execution":{"iopub.status.busy":"2023-07-07T10:27:59.185071Z","iopub.execute_input":"2023-07-07T10:27:59.185396Z","iopub.status.idle":"2023-07-07T10:27:59.198470Z","shell.execute_reply.started":"2023-07-07T10:27:59.185369Z","shell.execute_reply":"2023-07-07T10:27:59.197516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Homemade Modal","metadata":{"id":"gPtUPX-8Rpkn"}},{"cell_type":"code","source":"# network parameters\nbatch_size = 128\nhidden_units = 256\ndropout = 0.45\nepochs = 30","metadata":{"id":"UD5UNDSDRnIj","execution":{"iopub.status.busy":"2023-07-07T10:27:59.199889Z","iopub.execute_input":"2023-07-07T10:27:59.200273Z","iopub.status.idle":"2023-07-07T10:27:59.210708Z","shell.execute_reply.started":"2023-07-07T10:27:59.200239Z","shell.execute_reply":"2023-07-07T10:27:59.209454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tensorflow random seed\nimport tensorflow as tf","metadata":{"id":"PtZL68KsNtNu","execution":{"iopub.status.busy":"2023-07-07T10:27:59.212392Z","iopub.execute_input":"2023-07-07T10:27:59.212911Z","iopub.status.idle":"2023-07-07T10:27:59.224823Z","shell.execute_reply.started":"2023-07-07T10:27:59.212764Z","shell.execute_reply":"2023-07-07T10:27:59.223370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def Special_model(num_columns, num_cols_y,drop_rate = 0.25):\n    inp = tf.keras.layers.Input(shape = (num_columns))\n\n    x = tf.keras.layers.BatchNormalization()(inp)\n    x = tf.keras.layers.Dropout(drop_rate)(x)\n    x = tf.keras.layers.Dense(256,\n                              activation='relu')(x)\n    ##### 極限降維濃縮 ######\n    x1 = tf.keras.layers.BatchNormalization()(inp)\n    #x1 = tf.keras.layers.Dropout(drop_rate)(x1)\n    x1 = tf.keras.layers.Dense(32,activation='relu')(x1)\n    x1 = tf.keras.layers.BatchNormalization()(x1)\n    #x1 = tf.keras.layers.Dropout(drop_rate)(x1)\n    x1 = tf.keras.layers.Dense(16,activation='relu')(x1)\n    x1 = tf.keras.layers.BatchNormalization()(x1)\n    #x1 = tf.keras.layers.Dropout(drop_rate)(x1)\n    x1 = tf.keras.layers.Dense(16,activation='relu')(x1)\n    x1 = tf.keras.layers.BatchNormalization()(x1)\n    #x1 = tf.keras.layers.Dropout(drop_rate)(x1)\n    x1 = tf.keras.layers.Dense(16,activation='relu')(x1)\n    x1 = tf.keras.layers.Dense(4,activation='relu')(x1)\n    ######################\n    x2 = tf.keras.layers.BatchNormalization()(inp)\n    x2 = tf.keras.layers.Dense(112,activation='relu')(x2)\n    x2 = tf.keras.layers.Dense(28,activation='relu')(x2)\n    x2 = tf.keras.layers.Dense(32,activation='relu')(x2)\n    ######################\n    x = tf.keras.layers.BatchNormalization()(x)\n    x = tf.keras.layers.Dropout(drop_rate)(x)\n    x = tf.keras.layers.Dense(256,\n                              activation='relu')(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n    x = tf.keras.layers.Dropout(drop_rate)(x)\n    x = tf.keras.layers.Dense(128,\n                              activation='relu')(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n    x = tf.keras.layers.Dropout(drop_rate)(x)\n    x = tf.keras.layers.Dense(256,\n                              activation='relu')(x)\n    x = tf.keras.layers.BatchNormalization()(x)\n    x = tf.keras.layers.Dropout(drop_rate)(x)\n    x = tf.keras.layers.Dense(64,activation='relu')(x)\n    ########## 終極融合 #########\n    x3 = tf.keras.layers.Concatenate()([x, x1, x2])\n    #============ Final Layer =================\n    x3 = tf.keras.layers.BatchNormalization()(x3)\n    x3 = tf.keras.layers.Dropout(drop_rate)(x3)\n    out = tf.keras.layers.Dense(num_cols_y, activation = 'softmax')(x3)\n    model = tf.keras.models.Model(inputs = inp, outputs = out)\n    model.compile(\n        optimizer=tf.keras.optimizers.Adam(),\n        loss='categorical_crossentropy' ,\n        metrics=['accuracy']\n            )\n    return model","metadata":{"id":"sTrvn9ixLYtb","execution":{"iopub.status.busy":"2023-07-07T10:27:59.227756Z","iopub.execute_input":"2023-07-07T10:27:59.228163Z","iopub.status.idle":"2023-07-07T10:27:59.248977Z","shell.execute_reply.started":"2023-07-07T10:27:59.228132Z","shell.execute_reply":"2023-07-07T10:27:59.248055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"demo_special = Special_model(num_columns = 784, num_cols_y = 10)\ndemo_special.summary()","metadata":{"id":"UZcoItUyNkeC","outputId":"900a0f5a-61eb-4bb0-c48c-7d6ac79b7033","execution":{"iopub.status.busy":"2023-07-07T10:27:59.250396Z","iopub.execute_input":"2023-07-07T10:27:59.250845Z","iopub.status.idle":"2023-07-07T10:28:00.390646Z","shell.execute_reply.started":"2023-07-07T10:27:59.250802Z","shell.execute_reply":"2023-07-07T10:28:00.390010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_model(demo_special, to_file='mlp-mnist.png', show_shapes=True)","metadata":{"id":"iTP-VtTwOJ2I","outputId":"ecf8515a-42f5-4a7b-c072-feba4fe022bf","execution":{"iopub.status.busy":"2023-07-07T10:28:00.391731Z","iopub.execute_input":"2023-07-07T10:28:00.392116Z","iopub.status.idle":"2023-07-07T10:28:00.834840Z","shell.execute_reply.started":"2023-07-07T10:28:00.392085Z","shell.execute_reply":"2023-07-07T10:28:00.833891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential #\nfrom keras.layers import Dense , Activation, Dropout\nfrom keras.optimizers import Adam ,RMSprop\nfrom keras import backend as K","metadata":{"id":"G74Iiej-Po4X","execution":{"iopub.status.busy":"2023-07-07T10:28:00.836051Z","iopub.execute_input":"2023-07-07T10:28:00.837106Z","iopub.status.idle":"2023-07-07T10:28:00.843198Z","shell.execute_reply.started":"2023-07-07T10:28:00.837068Z","shell.execute_reply":"2023-07-07T10:28:00.841901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ReduceLROnPlateau, ModelCheckpoint\nfrom keras.callbacks import EarlyStopping, LearningRateScheduler","metadata":{"id":"gR4GAmlDOR4h","execution":{"iopub.status.busy":"2023-07-07T10:28:00.844792Z","iopub.execute_input":"2023-07-07T10:28:00.845186Z","iopub.status.idle":"2023-07-07T10:28:00.858496Z","shell.execute_reply.started":"2023-07-07T10:28:00.845155Z","shell.execute_reply":"2023-07-07T10:28:00.857101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_path = f'special_model_demo.hdf5'\n\nreduce_lr_loss = ReduceLROnPlateau(monitor='val_loss',\n                                  factor=0.3,  # 0.3 --> 0.2\n                                  min_lr=1e-5,\n                                  patience=5,  # 3 --> 4\n                                  verbose=0,\n                                  mode='min') # ori verbose = 1\ncb_checkpt = ModelCheckpoint(checkpoint_path,\n                            monitor = 'val_loss',\n                            verbose = 0,\n                            save_best_only = True,\n                            save_weights_only = True,\n                            mode = 'min') # ori verbose = 1\nearly = EarlyStopping(monitor=\"val_loss\",\n                      mode=\"min\",\n                      restore_best_weights=True,\n                      min_delta=1e-5,\n                      patience= 20, # 25 --> 30\n                      verbose = 0) # ori verbose = 1\n","metadata":{"id":"EuStNjR_OfM-","execution":{"iopub.status.busy":"2023-07-07T10:28:00.860219Z","iopub.execute_input":"2023-07-07T10:28:00.860822Z","iopub.status.idle":"2023-07-07T10:28:00.873833Z","shell.execute_reply.started":"2023-07-07T10:28:00.860761Z","shell.execute_reply":"2023-07-07T10:28:00.872800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = demo_special.fit(\n        train_X,\n        train_Y,\n        validation_split=0.1,\n        epochs=30, batch_size=2048,# use 64\n        #class_weight = 'auto',\n        callbacks=[reduce_lr_loss,\n                  cb_checkpt,\n                  early]# 透過callbacks 呼叫定義的功能\n        )","metadata":{"id":"EaTNlOLKQDu1","outputId":"5e51dfad-8239-4d12-90b2-16a157407fe5","execution":{"iopub.status.busy":"2023-07-07T10:28:00.875033Z","iopub.execute_input":"2023-07-07T10:28:00.875929Z","iopub.status.idle":"2023-07-07T10:29:29.952974Z","shell.execute_reply.started":"2023-07-07T10:28:00.875896Z","shell.execute_reply":"2023-07-07T10:29:29.951680Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras\nfrom matplotlib import pyplot as plt\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"id":"QmoiBJ29T2Rt","outputId":"fec26ae4-ef8d-4360-a57d-12e3e92a44ea","execution":{"iopub.status.busy":"2023-07-07T10:29:29.954489Z","iopub.execute_input":"2023-07-07T10:29:29.954901Z","iopub.status.idle":"2023-07-07T10:29:30.232734Z","shell.execute_reply.started":"2023-07-07T10:29:29.954869Z","shell.execute_reply":"2023-07-07T10:29:30.231464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = demo_special.predict(test_X)\npredictions = np.argmax(predictions,axis=1)","metadata":{"id":"GD2f5HNQT_tP","outputId":"e8b02bba-f4e3-4ec4-bb5d-cd835a50fc46","execution":{"iopub.status.busy":"2023-07-07T10:29:30.240091Z","iopub.execute_input":"2023-07-07T10:29:30.240960Z","iopub.status.idle":"2023-07-07T10:29:34.167413Z","shell.execute_reply.started":"2023-07-07T10:29:30.240918Z","shell.execute_reply":"2023-07-07T10:29:34.166112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"id":"1j83Vtr9brGH","outputId":"fd298ed9-abe6-4027-cd8f-bcd0e7dc4203","execution":{"iopub.status.busy":"2023-07-07T10:29:34.168946Z","iopub.execute_input":"2023-07-07T10:29:34.169302Z","iopub.status.idle":"2023-07-07T10:29:34.176535Z","shell.execute_reply.started":"2023-07-07T10:29:34.169261Z","shell.execute_reply":"2023-07-07T10:29:34.175339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Label = pd.DataFrame(predictions, columns = ['Label'])","metadata":{"id":"1cW613E8aVIC","execution":{"iopub.status.busy":"2023-07-07T10:29:34.178028Z","iopub.execute_input":"2023-07-07T10:29:34.179014Z","iopub.status.idle":"2023-07-07T10:29:34.189554Z","shell.execute_reply.started":"2023-07-07T10:29:34.178979Z","shell.execute_reply":"2023-07-07T10:29:34.188289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsubmission = df['ImageId']\nsubmission = pd.concat([submission,Label],axis=1)\nprint(submission)","metadata":{"id":"ipZ5jFZ_eV4p","outputId":"18851443-fdb9-435c-a731-69a314edeb83","execution":{"iopub.status.busy":"2023-07-07T10:32:05.679892Z","iopub.execute_input":"2023-07-07T10:32:05.680293Z","iopub.status.idle":"2023-07-07T10:32:05.709346Z","shell.execute_reply.started":"2023-07-07T10:32:05.680260Z","shell.execute_reply":"2023-07-07T10:32:05.708127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_images = 10\npredictions[3]=0\nfig, axes = plt.subplots(2, 5, figsize=(10, 5))\naxes = axes.flatten()\n\nfor i in range(num_images):\n    # Get the predicted label with the highest probability\n    predicted_label = predictions[i]\n\n    axes[i].imshow(test_X[i].reshape(28, 28), cmap='gray')\n    axes[i].axis('off')\n    axes[i].set_title('Predicted: {}'.format(predicted_label))\n\nplt.tight_layout()\nplt.show()","metadata":{"id":"au3140EFUPbW","outputId":"4fa187c5-d63f-4e74-9f66-82406fc285e2","execution":{"iopub.status.busy":"2023-07-07T10:33:35.820025Z","iopub.execute_input":"2023-07-07T10:33:35.820433Z","iopub.status.idle":"2023-07-07T10:33:36.813858Z","shell.execute_reply.started":"2023-07-07T10:33:35.820401Z","shell.execute_reply":"2023-07-07T10:33:36.812418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"demo_special.save('mnist_model.h5')","metadata":{"id":"PYzqBRwqEQ0U","execution":{"iopub.status.busy":"2023-07-07T10:33:43.843460Z","iopub.execute_input":"2023-07-07T10:33:43.843857Z","iopub.status.idle":"2023-07-07T10:33:43.998499Z","shell.execute_reply.started":"2023-07-07T10:33:43.843824Z","shell.execute_reply":"2023-07-07T10:33:43.997060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Gradio","metadata":{"id":"ltBOr9EnDygN"}},{"cell_type":"code","source":"!pip install gradio","metadata":{"id":"uYv1CBTaDxDi","outputId":"40204c97-bf8e-41f4-c980-9e53ffc5b987","execution":{"iopub.status.busy":"2023-07-07T10:33:46.559822Z","iopub.execute_input":"2023-07-07T10:33:46.560211Z","iopub.status.idle":"2023-07-07T10:34:08.318415Z","shell.execute_reply.started":"2023-07-07T10:33:46.560179Z","shell.execute_reply":"2023-07-07T10:34:08.316917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gradio as gr\nimport cv2\nimport numpy as np\nfrom keras.models import load_model","metadata":{"id":"q4gR9ewzD5ss","execution":{"iopub.status.busy":"2023-07-07T10:34:10.590490Z","iopub.execute_input":"2023-07-07T10:34:10.590914Z","iopub.status.idle":"2023-07-07T10:34:10.597690Z","shell.execute_reply.started":"2023-07-07T10:34:10.590877Z","shell.execute_reply":"2023-07-07T10:34:10.595553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = load_model('mnist_model.h5')","metadata":{"id":"zaW2ViIzE1RM","execution":{"iopub.status.busy":"2023-07-07T10:34:10.600278Z","iopub.execute_input":"2023-07-07T10:34:10.600767Z","iopub.status.idle":"2023-07-07T10:34:11.330284Z","shell.execute_reply.started":"2023-07-07T10:34:10.600723Z","shell.execute_reply":"2023-07-07T10:34:11.329368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_image(img):\n    img_resized = cv2.resize(img, (28, 28))  # 調整大小為 28x28\n    img_normalized = img_resized.astype('float32') / 255.0  \n    img_flattened = img_normalized.flatten()  # 將圖片展平為一維向量\n    img_reshaped = img_flattened.reshape(1, 784)  # 調整形狀以符合模型的要求\n\n    \n    prediction = demo_special.predict(img_reshaped)\n    pred = np.argmax(prediction)\n    return str(pred)\n\n","metadata":{"id":"iHevbPRQiulP","execution":{"iopub.status.busy":"2023-07-07T10:34:11.974810Z","iopub.execute_input":"2023-07-07T10:34:11.975629Z","iopub.status.idle":"2023-07-07T10:34:11.983115Z","shell.execute_reply.started":"2023-07-07T10:34:11.975590Z","shell.execute_reply":"2023-07-07T10:34:11.981684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gr.Interface(fn=predict_image,\n             inputs=\"sketchpad\", outputs=\"label\",\n             theme= gr.themes.Soft(),title='Handwritten Digit Recognition').launch()\n","metadata":{"id":"kw3tkqVxFJBJ","outputId":"e96f67c7-bf76-4245-a362-8c468d745ef3","execution":{"iopub.status.busy":"2023-07-07T10:34:57.589902Z","iopub.execute_input":"2023-07-07T10:34:57.590320Z","iopub.status.idle":"2023-07-07T10:35:03.096673Z","shell.execute_reply.started":"2023-07-07T10:34:57.590289Z","shell.execute_reply":"2023-07-07T10:35:03.095748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"model = demo_special.save('my_model.xml')\nsave_path = '/content/gdrive/MyDrive/models/'\nmodel.save(save_path)","metadata":{"id":"B4H3yhDKDwO-"}}]}