{"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\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-11-17T13:12:20.617561Z","iopub.execute_input":"2021-11-17T13:12:20.618439Z","iopub.status.idle":"2021-11-17T13:12:20.623407Z","shell.execute_reply.started":"2021-11-17T13:12:20.618387Z","shell.execute_reply":"2021-11-17T13:12:20.622588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport keras\nfrom keras.preprocessing import image\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPool2D, Flatten,Dense,Dropout,BatchNormalization\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import VGG16, InceptionResNetV2, ResNet50, Xception\nimport cv2\nfrom PIL import Image\n\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Activation, Flatten\nfrom tensorflow.keras.optimizers import SGD\n","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:12:24.527245Z","iopub.execute_input":"2021-11-17T13:12:24.527545Z","iopub.status.idle":"2021-11-17T13:12:24.536472Z","shell.execute_reply.started":"2021-11-17T13:12:24.527508Z","shell.execute_reply":"2021-11-17T13:12:24.535628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/plant-pathology-2021-fgvc8/'\ntrain_dir = path + 'train_images/'\ntest_dir = path + 'test_images/'\n#resized images\ntrain_paths = '../input/resized-plant2021/img_sz_256'","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:12:28.689023Z","iopub.execute_input":"2021-11-17T13:12:28.689291Z","iopub.status.idle":"2021-11-17T13:12:28.693563Z","shell.execute_reply.started":"2021-11-17T13:12:28.689263Z","shell.execute_reply":"2021-11-17T13:12:28.692714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:12:34.260320Z","iopub.execute_input":"2021-11-17T13:12:34.260860Z","iopub.status.idle":"2021-11-17T13:12:34.287037Z","shell.execute_reply.started":"2021-11-17T13:12:34.260826Z","shell.execute_reply":"2021-11-17T13:12:34.286214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2=pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:12:36.579206Z","iopub.execute_input":"2021-11-17T13:12:36.579771Z","iopub.status.idle":"2021-11-17T13:12:36.604687Z","shell.execute_reply.started":"2021-11-17T13:12:36.579717Z","shell.execute_reply":"2021-11-17T13:12:36.604043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2['labels'].unique()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:12:39.085407Z","iopub.execute_input":"2021-11-17T13:12:39.085904Z","iopub.status.idle":"2021-11-17T13:12:39.093474Z","shell.execute_reply.started":"2021-11-17T13:12:39.085840Z","shell.execute_reply":"2021-11-17T13:12:39.092835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2['labels'] = df2['labels'].apply(lambda string: string.split(' '))\ndf2","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:12:43.303795Z","iopub.execute_input":"2021-11-17T13:12:43.304270Z","iopub.status.idle":"2021-11-17T13:12:43.328974Z","shell.execute_reply.started":"2021-11-17T13:12:43.304218Z","shell.execute_reply":"2021-11-17T13:12:43.328354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import MultiLabelBinarizer\ns = list(df2['labels'])\nmlb = MultiLabelBinarizer()\ndf2x = pd.DataFrame(mlb.fit_transform(s), columns=mlb.classes_, index=df2.index)\nprint(df2x.columns)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:12:47.090270Z","iopub.execute_input":"2021-11-17T13:12:47.090684Z","iopub.status.idle":"2021-11-17T13:12:47.154322Z","shell.execute_reply.started":"2021-11-17T13:12:47.090643Z","shell.execute_reply":"2021-11-17T13:12:47.153490Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:12:51.745321Z","iopub.execute_input":"2021-11-17T13:12:51.745598Z","iopub.status.idle":"2021-11-17T13:12:51.755826Z","shell.execute_reply.started":"2021-11-17T13:12:51.745567Z","shell.execute_reply":"2021-11-17T13:12:51.755068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.labels.value_counts()\n","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:12:55.483839Z","iopub.execute_input":"2021-11-17T13:12:55.484144Z","iopub.status.idle":"2021-11-17T13:12:55.493686Z","shell.execute_reply.started":"2021-11-17T13:12:55.484110Z","shell.execute_reply":"2021-11-17T13:12:55.493058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['labels'].unique()\n","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:12:59.161191Z","iopub.execute_input":"2021-11-17T13:12:59.161949Z","iopub.status.idle":"2021-11-17T13:12:59.169491Z","shell.execute_reply.started":"2021-11-17T13:12:59.161894Z","shell.execute_reply":"2021-11-17T13:12:59.168631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['labels'] = df['labels'].astype(str)\n","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:13:02.340449Z","iopub.execute_input":"2021-11-17T13:13:02.340746Z","iopub.status.idle":"2021-11-17T13:13:02.346249Z","shell.execute_reply.started":"2021-11-17T13:13:02.340715Z","shell.execute_reply":"2021-11-17T13:13:02.345351Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8,5))\nsns.countplot(data = df,y='labels')","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:13:06.512711Z","iopub.execute_input":"2021-11-17T13:13:06.513015Z","iopub.status.idle":"2021-11-17T13:13:06.843381Z","shell.execute_reply.started":"2021-11-17T13:13:06.512980Z","shell.execute_reply":"2021-11-17T13:13:06.842531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_examples(label):\n    fig, ax = plt.subplots(1, 5, figsize=(25, 5))\n    ax = ax.ravel()\n    for i in range(5):\n        idx = df[df['labels']==label].index[i]\n        image = cv2.imread(train_paths+df.loc[idx, 'image'])\n        \n        image =cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        ax[i].imshow(image)\n        ax[i].set_title(label)\n        ax[i].set_xticklabels([])\n        ax[i].set_yticklabels([])","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:13:11.141537Z","iopub.execute_input":"2021-11-17T13:13:11.141822Z","iopub.status.idle":"2021-11-17T13:13:11.148735Z","shell.execute_reply.started":"2021-11-17T13:13:11.141793Z","shell.execute_reply":"2021-11-17T13:13:11.147952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#for labels in list(df['labels'].unique()):\n #   plot_examples(labels)","metadata":{"execution":{"iopub.status.busy":"2021-11-15T23:21:01.704862Z","iopub.execute_input":"2021-11-15T23:21:01.705327Z","iopub.status.idle":"2021-11-15T23:21:01.70946Z","shell.execute_reply.started":"2021-11-15T23:21:01.705291Z","shell.execute_reply":"2021-11-15T23:21:01.708445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#แปลงให้labelเป็นตัวเลข\ncount_dict = df.labels.value_counts()\nlabel2id = {\n    'scab': 0,\n    'frog_eye_leaf_spot' : 1,\n    'rust' : 2,\n    'complex' : 3,\n    'powdery_mildew' : 4,\n}\nNUM_CLASS = len(label2id)\nid2label = dict([(value, key) for key, value in label2id.items()])\ndf[\"labels\"] = df[\"labels\"].map(lambda x : [i for i in x.split(\" \") if i != \"healthy\"])\ndf[\"labels\"] = df[\"labels\"].map(lambda x : [label2id[i] for i in x])\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:13:15.741678Z","iopub.execute_input":"2021-11-17T13:13:15.742306Z","iopub.status.idle":"2021-11-17T13:13:15.793402Z","shell.execute_reply.started":"2021-11-17T13:13:15.742272Z","shell.execute_reply":"2021-11-17T13:13:15.792844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale = 1./255.,\n                                   rotation_range = 40,\n                                   width_shift_range = 0.2,\n                                   height_shift_range = 0.2,\n                                   shear_range = 0.2,\n                                   zoom_range = 0.2,\n                                   horizontal_flip = True,\n                                   validation_split = 0.2)\ntest_datagen = ImageDataGenerator(rescale = 1./255,\n                                  validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:13:20.450843Z","iopub.execute_input":"2021-11-17T13:13:20.451467Z","iopub.status.idle":"2021-11-17T13:13:20.457305Z","shell.execute_reply.started":"2021-11-17T13:13:20.451424Z","shell.execute_reply":"2021-11-17T13:13:20.456479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(dataframe = df,\n                                                   directory = train_paths,\n                                                   target_size = (256,256),\n                                                   x_col = 'image',\n                                                   y_col = 'labels',\n                                                   batch_size = 128,\n                                                   color_mode = 'rgb',\n                                                   class_mode = 'categorical',\n                                                   subset = 'training')\n\ntest_generator = test_datagen.flow_from_dataframe(dataframe = df,\n                                                 directory = train_paths,\n                                                 target_size = (256,256),\n                                                 x_col = 'image',\n                                                 y_col = 'labels',\n                                                 batch_size = 128,\n                                                 color_mode = 'rgb',\n                                                 class_mode = 'categorical',\n                                                 subset = 'validation')","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:13:24.495479Z","iopub.execute_input":"2021-11-17T13:13:24.496010Z","iopub.status.idle":"2021-11-17T13:13:46.427038Z","shell.execute_reply.started":"2021-11-17T13:13:24.495976Z","shell.execute_reply":"2021-11-17T13:13:46.426048Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    tf.keras.layers.Conv2D(32, (3,3), input_shape=(256,256,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),\n    tf.keras.layers.MaxPooling2D(2,2),\n    tf.keras.layers.Flatten(),\n    tf.keras.layers.Dense(512, activation='relu'),\n    tf.keras.layers.Dense(NUM_CLASS, activation='sigmoid')\n])\nops = SGD(learning_rate=0.1)\nmodel.compile(loss='binary_crossentropy', optimizer=ops,metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:20:42.355573Z","iopub.execute_input":"2021-11-17T13:20:42.356248Z","iopub.status.idle":"2021-11-17T13:20:42.467661Z","shell.execute_reply.started":"2021-11-17T13:20:42.356198Z","shell.execute_reply":"2021-11-17T13:20:42.466767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#history = model.fit_generator(train_generator,validation_data,test_generator)\n#history = model.fit_generator(train_generator,epochs = 15)\n#history = model.fit_generator(train_generator,epochs = 1 ,validation_data = test_generator)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-11-17T13:19:52.393053Z","iopub.execute_input":"2021-11-17T13:19:52.393364Z","iopub.status.idle":"2021-11-17T13:19:52.397568Z","shell.execute_reply.started":"2021-11-17T13:19:52.393330Z","shell.execute_reply":"2021-11-17T13:19:52.396778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping\ncheckpoint_path = \"./muimodel.h5\"\ncheckpoint_dir = os.path.dirname(checkpoint_path)\n\n# Create a callback that saves the model\ncp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,\n                                                 monitor='val_loss',\n                                                 save_best_only=True,\n                                                 verbose=1)","metadata":{"execution":{"iopub.status.busy":"2021-11-17T14:06:40.119144Z","iopub.execute_input":"2021-11-17T14:06:40.119563Z","iopub.status.idle":"2021-11-17T14:06:40.124830Z","shell.execute_reply.started":"2021-11-17T14:06:40.119533Z","shell.execute_reply":"2021-11-17T14:06:40.124054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(\n                        monitor='val_loss',\n                        min_delta=0.0,\n                        patience=5,\n                        verbose=1\n                )","metadata":{"execution":{"iopub.status.busy":"2021-11-17T14:07:27.885647Z","iopub.execute_input":"2021-11-17T14:07:27.885953Z","iopub.status.idle":"2021-11-17T14:07:27.890435Z","shell.execute_reply.started":"2021-11-17T14:07:27.885917Z","shell.execute_reply":"2021-11-17T14:07:27.889628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BATCH_SIZE=128","metadata":{"execution":{"iopub.status.busy":"2021-11-17T14:07:31.439247Z","iopub.execute_input":"2021-11-17T14:07:31.439724Z","iopub.status.idle":"2021-11-17T14:07:31.442913Z","shell.execute_reply.started":"2021-11-17T14:07:31.439695Z","shell.execute_reply":"2021-11-17T14:07:31.442286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fit = model.fit_generator(train_generator,\n                                  validation_data=test_generator,\n                                  epochs=1,\n                                  steps_per_epoch=test_generator.samples//BATCH_SIZE,\n                                  validation_steps=test_generator.samples//BATCH_SIZE,\n                                  callbacks=[cp_callback, early_stopping]\n                                 )","metadata":{"execution":{"iopub.status.busy":"2021-11-17T14:07:35.313904Z","iopub.execute_input":"2021-11-17T14:07:35.314387Z","iopub.status.idle":"2021-11-17T14:14:47.296325Z","shell.execute_reply.started":"2021-11-17T14:07:35.314353Z","shell.execute_reply":"2021-11-17T14:14:47.294394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.plot(history.history['loss'])\n#plt.plot(history.history['val_loss']) \n#plt.title('Model Loss') \n#plt.ylabel('Loss') \n#plt.xlabel('Epochs') \n#plt.legend(['Train', 'Test'], loc='upper right') \n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-15T23:05:57.678612Z","iopub.status.idle":"2021-11-15T23:05:57.67914Z","shell.execute_reply.started":"2021-11-15T23:05:57.678932Z","shell.execute_reply":"2021-11-15T23:05:57.678953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ------------------------------------------------------------","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}}]}