{"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":"2021-07-29T15:22:10.742128Z","iopub.execute_input":"2021-07-29T15:22:10.742748Z","iopub.status.idle":"2021-07-29T15:22:10.754115Z","shell.execute_reply.started":"2021-07-29T15:22:10.742698Z","shell.execute_reply":"2021-07-29T15:22:10.753040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom pathlib import Path\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport random\n\n# keras\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, Activation, BatchNormalization\nfrom keras import optimizers\nfrom keras import models\nfrom keras import layers\nfrom keras.losses import SparseCategoricalCrossentropy\nfrom keras.preprocessing.image import ImageDataGenerator\n\n# sklearn library\nfrom sklearn.preprocessing import StandardScaler \nfrom sklearn.model_selection import train_test_split \nfrom sklearn.metrics import classification_report ","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:22:27.695244Z","iopub.execute_input":"2021-07-29T15:22:27.695915Z","iopub.status.idle":"2021-07-29T15:22:27.702497Z","shell.execute_reply.started":"2021-07-29T15:22:27.695871Z","shell.execute_reply":"2021-07-29T15:22:27.701592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! unzip \"../input/dogs-vs-cats-redux-kernels-edition/train.zip\" -d train\n! unzip \"../input/dogs-vs-cats-redux-kernels-edition/test.zip\" -d test","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:22:31.238185Z","iopub.execute_input":"2021-07-29T15:22:31.238805Z","iopub.status.idle":"2021-07-29T15:22:52.714130Z","shell.execute_reply.started":"2021-07-29T15:22:31.238764Z","shell.execute_reply":"2021-07-29T15:22:52.712948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# labels\nfile = os.listdir(\"./train/train\")#指定所有目錄下所有的檔案和目錄名稱 列舉出來 \nLabels = list(map(lambda x: x.split('.')[0], file))#取得圖片檔案的主檔案名稱(dog.jpg > dog)\n\n# for filenames\nf = Path(\"./train/train\")#把檔案目錄定義出來\nFile_Path = list(f.glob(r\"**/*.jpg\"))#獲得指定目錄下的所有jpg文件\n\n# dataframe\nFile_Path = pd.Series(File_Path).astype(str)#將路徑轉換成STR[string](文字)\nLabels = pd.Series(Labels)#Series：用來處理時間序列相關的資料(如感測器資料等)，主要為建立索引的一維陣列。\ndf = pd.concat([File_Path,Labels],axis=1)# 使用 concat 合併 axis=0 為直向合併 1就橫向合併\ndf.columns = ['filename', 'category']#指定資料表欄位","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:42:12.298285Z","iopub.execute_input":"2021-07-29T15:42:12.298675Z","iopub.status.idle":"2021-07-29T15:42:12.574728Z","shell.execute_reply.started":"2021-07-29T15:42:12.298609Z","shell.execute_reply":"2021-07-29T15:42:12.573846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"df.head()#使用.head()可以顯示5筆資料（預設是5筆）","metadata":{"execution":{"iopub.status.busy":"2021-07-28T14:20:32.371856Z","iopub.execute_input":"2021-07-28T14:20:32.372116Z","iopub.status.idle":"2021-07-28T14:20:32.400931Z","shell.execute_reply.started":"2021-07-28T14:20:32.372083Z","shell.execute_reply":"2021-07-28T14:20:32.399898Z"}}},{"cell_type":"code","source":"train_set, test_data = train_test_split(df, test_size= 0.2, random_state = 42)#『原始的資料』、『比例』、『Seed』\ntrain_data, val_data = train_test_split(train_set, test_size= 0.2, random_state = 42)\n\n\nprint(train_data.shape)#numpy.shape() 函式可以返回陣列的形狀。所謂形狀，我們指的是它可以幫助找到一個陣列的尺寸。它以元組的形式返回形狀，因為我們不能改變元組，就像我們不能改變陣列的尺寸一樣。\nprint(test_data.shape)\nprint(val_data.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:42:21.192887Z","iopub.execute_input":"2021-07-29T15:42:21.193264Z","iopub.status.idle":"2021-07-29T15:42:21.216150Z","shell.execute_reply.started":"2021-07-29T15:42:21.193230Z","shell.execute_reply":"2021-07-29T15:42:21.214809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train_data.reset_index(drop=True)#reset_index:給予標籤  drop:布林值(boolean)，如果為False，則將替換的索引列添加到數據中。\ntest_data = test_data.reset_index(drop=True)\nval_data = val_data.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:43:23.405276Z","iopub.execute_input":"2021-07-29T15:43:23.405648Z","iopub.status.idle":"2021-07-29T15:43:23.412468Z","shell.execute_reply.started":"2021-07-29T15:43:23.405602Z","shell.execute_reply":"2021-07-29T15:43:23.411736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_size = (128, 128)#圖片大小","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:43:27.665499Z","iopub.execute_input":"2021-07-29T15:43:27.666074Z","iopub.status.idle":"2021-07-29T15:43:27.671187Z","shell.execute_reply.started":"2021-07-29T15:43:27.666026Z","shell.execute_reply":"2021-07-29T15:43:27.670134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_gen = ImageDataGenerator(rescale=1./255, horizontal_flip=True)#ImageDataGenerator來生成一個數據生成器，其中rescale引數指定將影象張量的數字縮放 horizontal_flip 是水平翻轉的意思","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:43:30.127650Z","iopub.execute_input":"2021-07-29T15:43:30.128004Z","iopub.status.idle":"2021-07-29T15:43:30.133021Z","shell.execute_reply.started":"2021-07-29T15:43:30.127974Z","shell.execute_reply":"2021-07-29T15:43:30.131996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#把產生好的格式 塞入資料 阿斯\n#flow_from_dataframe 它允許輸入一個包含文件名的Pandas數據幀（有/無擴展名）列和一個具有類名的列，直接讀取目錄中的圖像並映射各自的類名\ntrain_gen = img_gen.flow_from_dataframe(\n    train_data, \n    x_col='filename',\n    y_col='category',\n    target_size=img_size,\n    class_mode='binary',\n    batch_size=32,\n    shuffle = False\n)\n#1.train_data 圖片集\n#2.x_col 橫列\n#3.y_col 直行\n#4.target_size 指定大小\n#5.class_mode 把他指定成二進制 \"binary\"將是1D二進制標籤\n#6.batch_size 一批數據的大小（默認 32）\n#7.shuffle 是否混洗數據（默認 True） 大洗牌拉\n\nvalidation_gen = img_gen.flow_from_dataframe(\n    val_data, \n    x_col='filename',\n    y_col='category',\n    target_size=img_size,\n    class_mode='binary',\n    batch_size=32,\n    shuffle = False\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T15:58:07.388788Z","iopub.execute_input":"2021-07-29T15:58:07.389142Z","iopub.status.idle":"2021-07-29T15:58:07.665436Z","shell.execute_reply.started":"2021-07-29T15:58:07.389103Z","shell.execute_reply":"2021-07-29T15:58:07.664388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)))\nmodel.add(layers.MaxPool2D((2, 2)))#pool 池化層 將陣列挑起\n\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPool2D((2, 2)))\n\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPool2D((2, 2)))\n\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.MaxPool2D((2, 2)))\n\nmodel.add(layers.Flatten())\nmodel.add(layers.Dropout(0.5))\nmodel.add(layers.Dense(512, activation='relu'))\nmodel.add(layers.Dense(1, activation='sigmoid'))\n\nmodel.compile(loss='binary_crossentropy', optimizer=optimizers.RMSprop(lr=1e-4), metrics=['acc'])\n","metadata":{"execution":{"iopub.status.busy":"2021-07-29T16:29:40.546326Z","iopub.execute_input":"2021-07-29T16:29:40.546758Z","iopub.status.idle":"2021-07-29T16:29:40.767484Z","shell.execute_reply.started":"2021-07-29T16:29:40.546721Z","shell.execute_reply":"2021-07-29T16:29:40.766752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()#顯示目前網路架構","metadata":{"execution":{"iopub.status.busy":"2021-07-29T16:29:52.746857Z","iopub.execute_input":"2021-07-29T16:29:52.747462Z","iopub.status.idle":"2021-07-29T16:29:52.758653Z","shell.execute_reply.started":"2021-07-29T16:29:52.747425Z","shell.execute_reply":"2021-07-29T16:29:52.757663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#方法用於執行訓練過程\nhistory = model.fit(train_gen,\n                    validation_data = validation_gen,\n                    validation_steps=50,\n                    steps_per_epoch=100,\n                    epochs = 50,\n                    verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T17:07:50.328667Z","iopub.execute_input":"2021-07-29T17:07:50.329095Z","iopub.status.idle":"2021-07-29T18:02:56.169332Z","shell.execute_reply.started":"2021-07-29T17:07:50.329060Z","shell.execute_reply":"2021-07-29T18:02:56.167829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#accuracy\n#記錄了運行輸出 將上述欄位紀錄起來\nacc = history.history['acc']\nval_acc = history.history['val_acc']\n\n#loss\nloss = history.history['loss'] \nval_loss = history.history['val_loss']","metadata":{"execution":{"iopub.status.busy":"2021-07-28T14:20:54.376156Z","iopub.status.idle":"2021-07-28T14:20:54.37664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#然後畫出來 \nplt.figure(figsize=(10, 5))\n\n# visualising Accuracy \nplt.subplot(2, 1, 1)\n\nplt.plot(acc, label='Training Accuracy')\nplt.plot(val_acc, label='Validation Accuracy')\n\nplt.ylabel('Accuracy') \nplt.title('Training and Validation Accuracy') \n\n","metadata":{"execution":{"iopub.status.busy":"2021-07-28T14:20:54.377451Z","iopub.status.idle":"2021-07-28T14:20:54.377943Z"},"trusted":true},"execution_count":null,"outputs":[]}]}