{"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 tensorflow as tf               # 載入tensorflow 模組\nimport tensorflow_datasets as tfds    # 載入tensorflow dataset 模組\nimport pandas as pd\nimport numpy as np\n\nbatch_size = 64                       # mini-batch 大小為64\nIMG_SIZE = 224\nNUM_CLASSES = 10\n\ndf_train=pd.read_csv('../input/digit-recognizer/train.csv')# 載入資料集\n\nds_train=df_train.to_numpy()\nprint(ds_train.shape)\n\n\nlabel=ds_train[:,0] \nimage=ds_train[:,1:].reshape((-1,28,28,1)) \nprint(image.shape)\nds_train=tf.data.Dataset.from_tensor_slices((image,label))\nprint(ds_train)\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:12:47.697306Z","iopub.execute_input":"2022-08-05T01:12:47.698186Z","iopub.status.idle":"2022-08-05T01:13:00.921273Z","shell.execute_reply.started":"2022-08-05T01:12:47.698069Z","shell.execute_reply":"2022-08-05T01:13:00.920091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"size = (IMG_SIZE, IMG_SIZE)   # 影像大小設為 224X224\nds_train = ds_train.map(lambda image, label: (tf.image.grayscale_to_rgb(tf.image.resize(tf.cast(image, dtype='int64'), size)), label))\n#image = tf.image.resize(image, size)\nprint(ds_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:13:00.923362Z","iopub.execute_input":"2022-08-05T01:13:00.923783Z","iopub.status.idle":"2022-08-05T01:13:01.019186Z","shell.execute_reply.started":"2022-08-05T01:13:00.923746Z","shell.execute_reply":"2022-08-05T01:13:01.018163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt # 載入matplotlib模組\n\n\n# 顯示圖片內容及標籤\n\nfor i, (image, label) in enumerate(ds_train.take(9)):\n    ax = plt.subplot(3, 3, i + 1)\n    plt.imshow(image.numpy().astype(\"uint8\"))\n    plt.title(\"{}\".format(label))\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:13:01.020520Z","iopub.execute_input":"2022-08-05T01:13:01.021074Z","iopub.status.idle":"2022-08-05T01:13:01.727934Z","shell.execute_reply.started":"2022-08-05T01:13:01.021033Z","shell.execute_reply":"2022-08-05T01:13:01.726791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras import layers\n\nimg_augmentation = Sequential(\n    [\n        layers.RandomRotation(factor=0.15), # 隨機旋轉\n        layers.RandomTranslation(height_factor=0.1, width_factor=0.1), # 隨機平移\n        layers.RandomFlip(), # 隨機翻轉\n        layers.RandomContrast(factor=0.1), #隨機對比增強\n    ],\n    name=\"img_augmentation\",\n)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:13:01.730484Z","iopub.execute_input":"2022-08-05T01:13:01.731264Z","iopub.status.idle":"2022-08-05T01:13:02.625522Z","shell.execute_reply.started":"2022-08-05T01:13:01.731216Z","shell.execute_reply":"2022-08-05T01:13:02.624503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image, label in ds_train.take(1):\n    for i in range(9):  # 隨機產生九張經過 img_augmentation的影像\n        ax = plt.subplot(3, 3, i + 1)\n        aug_img = img_augmentation(tf.expand_dims(image, axis=0))\n        plt.imshow(aug_img[0].numpy().astype(\"uint8\"))\n        plt.title(\"{}\".format(label))\n        plt.axis(\"off\")\n\n","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:13:04.988002Z","iopub.execute_input":"2022-08-05T01:13:04.989125Z","iopub.status.idle":"2022-08-05T01:13:05.974957Z","shell.execute_reply.started":"2022-08-05T01:13:04.989062Z","shell.execute_reply":"2022-08-05T01:13:05.974005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def input_preprocess(image, label):\n    label = tf.one_hot(label, NUM_CLASSES)\n    return image, label\n\n\nds_train = ds_train.map(\n    input_preprocess, num_parallel_calls=tf.data.AUTOTUNE\n)\nds_train = ds_train.batch(batch_size=batch_size, drop_remainder=True)\nds_train = ds_train.prefetch(tf.data.AUTOTUNE)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:13:13.633837Z","iopub.execute_input":"2022-08-05T01:13:13.634220Z","iopub.status.idle":"2022-08-05T01:13:13.680442Z","shell.execute_reply.started":"2022-08-05T01:13:13.634188Z","shell.execute_reply":"2022-08-05T01:13:13.679559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(ds_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:13:16.164052Z","iopub.execute_input":"2022-08-05T01:13:16.164795Z","iopub.status.idle":"2022-08-05T01:13:16.170230Z","shell.execute_reply.started":"2022-08-05T01:13:16.164759Z","shell.execute_reply":"2022-08-05T01:13:16.169246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n\ndef plot_hist(hist):\n    plt.plot(hist.history[\"accuracy\"])\n    plt.plot(hist.history[\"loss\"])\n    plt.title(\"model accuracy\")\n    plt.ylabel(\"accuracy\")\n    plt.xlabel(\"epoch\")\n    plt.legend([\"train\", \"loss\"], loc=\"upper left\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:13:18.423115Z","iopub.execute_input":"2022-08-05T01:13:18.424203Z","iopub.status.idle":"2022-08-05T01:13:18.430862Z","shell.execute_reply.started":"2022-08-05T01:13:18.424158Z","shell.execute_reply":"2022-08-05T01:13:18.429801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB7 \n\ndef build_model(num_classes):\n    inputs = layers.Input(shape=(IMG_SIZE, IMG_SIZE, 3))\n    x = img_augmentation(inputs)\n    model = EfficientNetB7(include_top=False, input_tensor=x, weights=\"imagenet\")\n\n    # Freeze the pretrained weights(骨幹網路不訓練)\n    model.trainable = False\n\n    # Rebuild top\n    x = layers.GlobalAveragePooling2D(name=\"avg_pool\")(model.output)\n    x = layers.BatchNormalization()(x)\n\n    top_dropout_rate = 0.2\n    x = layers.Dropout(top_dropout_rate, name=\"top_dropout\")(x)\n    outputs = layers.Dense(NUM_CLASSES, activation=\"softmax\", name=\"pred\")(x)\n\n    # Compile\n    model = tf.keras.Model(inputs, outputs, name=\"EfficientNet\")\n    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-2)\n    model.compile(\n        optimizer=optimizer, loss=\"categorical_crossentropy\", metrics=[\"accuracy\"]\n    )\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:13:21.895200Z","iopub.execute_input":"2022-08-05T01:13:21.896226Z","iopub.status.idle":"2022-08-05T01:13:21.905638Z","shell.execute_reply.started":"2022-08-05T01:13:21.896181Z","shell.execute_reply":"2022-08-05T01:13:21.904563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model(num_classes=NUM_CLASSES)\n\nepochs = 10  # @param {type: \"slider\", min:8, max:80}\nhist = model.fit(ds_train, epochs=epochs, verbose=2)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:13:28.088393Z","iopub.execute_input":"2022-08-05T01:13:28.088756Z","iopub.status.idle":"2022-08-05T01:57:54.178507Z","shell.execute_reply.started":"2022-08-05T01:13:28.088725Z","shell.execute_reply":"2022-08-05T01:57:54.177487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_hist(hist)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:57:54.180714Z","iopub.execute_input":"2022-08-05T01:57:54.181298Z","iopub.status.idle":"2022-08-05T01:57:54.469393Z","shell.execute_reply.started":"2022-08-05T01:57:54.181262Z","shell.execute_reply":"2022-08-05T01:57:54.465218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def unfreeze_model(model):\n    # We unfreeze the top 20 layers while leaving BatchNorm layers frozen\n    for layer in model.layers[-20:]:\n        if not isinstance(layer, layers.BatchNormalization):\n            layer.trainable = True\n\n    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-4)\n    model.compile(\n        optimizer=optimizer, loss=\"categorical_crossentropy\", metrics=[\"accuracy\"]\n    )\n\n\nunfreeze_model(model)\n\nepochs = 10  # @param {type: \"slider\", min:8, max:50}\nhist = model.fit(ds_train, epochs=epochs, verbose=2)\nplot_hist(hist)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T01:57:54.474352Z","iopub.execute_input":"2022-08-05T01:57:54.476524Z","iopub.status.idle":"2022-08-05T02:49:09.208370Z","shell.execute_reply.started":"2022-08-05T01:57:54.476487Z","shell.execute_reply":"2022-08-05T02:49:09.207060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(\"mnist-efficientNet\", save_format='h5')","metadata":{"execution":{"iopub.status.busy":"2022-08-05T02:49:09.210978Z","iopub.execute_input":"2022-08-05T02:49:09.211331Z","iopub.status.idle":"2022-08-05T02:49:10.776261Z","shell.execute_reply.started":"2022-08-05T02:49:09.211297Z","shell.execute_reply":"2022-08-05T02:49:10.775221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\nFileLink('./mnist-efficientNet')","metadata":{"execution":{"iopub.status.busy":"2022-08-05T02:49:10.778919Z","iopub.execute_input":"2022-08-05T02:49:10.779297Z","iopub.status.idle":"2022-08-05T02:49:10.786357Z","shell.execute_reply.started":"2022-08-05T02:49:10.779261Z","shell.execute_reply":"2022-08-05T02:49:10.785355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \n\nreconstructed_model = tf.keras.models.load_model(\"mnist-efficientNet\")\n\nreconstructed_model.fit(ds_train)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T02:49:10.787841Z","iopub.execute_input":"2022-08-05T02:49:10.788476Z","iopub.status.idle":"2022-08-05T02:54:48.483203Z","shell.execute_reply.started":"2022-08-05T02:49:10.788440Z","shell.execute_reply":"2022-08-05T02:54:48.482151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf               # 載入tensorflow 模組\nimport tensorflow_datasets as tfds    # 載入tensorflow dataset 模組\nimport pandas as pd\nimport numpy as np\n#讀取測試資料集\ntest_array=pd.read_csv('../input/digit-recognizer/test.csv').to_numpy()\nprint(test_array.shape)\n#把形狀轉成二維圖像以及型別轉為float32\ntest_array=test_array.reshape((-1,28,28,1)).astype(np.float32)\nprint(test_array.shape)\nprint(test_array.dtype)","metadata":{"execution":{"iopub.status.busy":"2022-08-05T02:55:17.941766Z","iopub.execute_input":"2022-08-05T02:55:17.942262Z","iopub.status.idle":"2022-08-05T02:55:21.166492Z","shell.execute_reply.started":"2022-08-05T02:55:17.942219Z","shell.execute_reply":"2022-08-05T02:55:21.165316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.transforms import functional as F\nfrom tensorflow.keras.applications.efficientnet import preprocess_input\nfrom tqdm import tqdm\n\n#遞迴產生推論結果\nreconstructed_model = tf.keras.models.load_model(\"mnist-efficientNet\")\nbatch_size = 64                       # mini-batch 大小為64\nIMG_SIZE = 224\nNUM_CLASSES = 10\nsize = (IMG_SIZE, IMG_SIZE) \nanswers={}\n\nfor i in tqdm(range(len(test_array))):\n    img=test_array[i]\n    img=tf.image.resize(tf.cast(img, dtype='int32'), size)\n    img=tf.image.grayscale_to_rgb(img)\n    #print(img.shape) \n    img=np.expand_dims(img,0)\n    img=preprocess_input(img)\n    #print(img.shape) \n    #result_o=reconstructed_model.predict(img)\n    #print(result_o)\n    result=np.argmax(reconstructed_model.predict(img))\n    answers[i+1]=result\n    #print(result)\n    #break","metadata":{"execution":{"iopub.status.busy":"2022-08-05T03:55:41.601067Z","iopub.execute_input":"2022-08-05T03:55:41.602193Z","iopub.status.idle":"2022-08-05T04:33:36.744064Z","shell.execute_reply.started":"2022-08-05T03:55:41.602149Z","shell.execute_reply":"2022-08-05T04:33:36.743135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_rows=['ImageId,Label\\n']\nfor i in tqdm(range(len(test_array))):\n    submission_rows.append('{0},{1}\\n'.format(i+1,answers[i+1]))\n\n    \nwith open('./submission.csv','w',encoding='utf-8-sig') as f:\n    f.writelines(submission_rows)\n","metadata":{"execution":{"iopub.status.busy":"2022-08-05T04:33:36.746053Z","iopub.execute_input":"2022-08-05T04:33:36.746460Z","iopub.status.idle":"2022-08-05T04:33:36.807125Z","shell.execute_reply.started":"2022-08-05T04:33:36.746419Z","shell.execute_reply":"2022-08-05T04:33:36.806111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import FileLink\nFileLink('./submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-08-05T04:33:36.808416Z","iopub.execute_input":"2022-08-05T04:33:36.809208Z","iopub.status.idle":"2022-08-05T04:33:36.817699Z","shell.execute_reply.started":"2022-08-05T04:33:36.809169Z","shell.execute_reply":"2022-08-05T04:33:36.816571Z"},"trusted":true},"execution_count":null,"outputs":[]}]}