{"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\n%matplotlib inline\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nimport cv2\nimport os\nimport warnings\nwarnings.filterwarnings('ignore')\nimport tensorflow as tf\nimport random\nimport albumentations as A\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense,Activation,Flatten, Conv2D, MaxPooling2D\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-27T01:46:25.068861Z","iopub.execute_input":"2023-03-27T01:46:25.069219Z","iopub.status.idle":"2023-03-27T01:46:25.080897Z","shell.execute_reply.started":"2023-03-27T01:46:25.069187Z","shell.execute_reply":"2023-03-27T01:46:25.079678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_path = '../input/plant-pathology-2021-fgvc8/train_images'\ntest_image_path = '../input/plant-pathology-2021-fgvc8/test_images'\ntrain_df_path = '../input/plant-pathology-2021-fgvc8/train.csv'\ntest_df_path = '../input/plant-pathology-2021-fgvc8/sample_submission.csv'","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:46:25.129059Z","iopub.execute_input":"2023-03-27T01:46:25.130132Z","iopub.status.idle":"2023-03-27T01:46:25.135977Z","shell.execute_reply.started":"2023-03-27T01:46:25.130066Z","shell.execute_reply":"2023-03-27T01:46:25.134398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(train_df_path)\ndf_test=pd.read_csv(test_df_path)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:46:25.138010Z","iopub.execute_input":"2023-03-27T01:46:25.138506Z","iopub.status.idle":"2023-03-27T01:46:25.186844Z","shell.execute_reply.started":"2023-03-27T01:46:25.138470Z","shell.execute_reply":"2023-03-27T01:46:25.185758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:46:25.188329Z","iopub.execute_input":"2023-03-27T01:46:25.188800Z","iopub.status.idle":"2023-03-27T01:46:25.207231Z","shell.execute_reply.started":"2023-03-27T01:46:25.188757Z","shell.execute_reply":"2023-03-27T01:46:25.206015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.labels.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:46:25.209779Z","iopub.execute_input":"2023-03-27T01:46:25.210344Z","iopub.status.idle":"2023-03-27T01:46:25.222247Z","shell.execute_reply.started":"2023-03-27T01:46:25.210310Z","shell.execute_reply":"2023-03-27T01:46:25.220665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,12))\nlabels = sns.barplot(df_train.labels.value_counts().index,df_train.labels.value_counts())\nfor item in labels.get_xticklabels():\n    item.set_rotation(45)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:46:25.223850Z","iopub.execute_input":"2023-03-27T01:46:25.225270Z","iopub.status.idle":"2023-03-27T01:46:25.265862Z","shell.execute_reply.started":"2023-03-27T01:46:25.225212Z","shell.execute_reply":"2023-03-27T01:46:25.264229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def batch_visualize(df,batch_size,path):\n    sample_df = df_train.sample(9)\n    image_names = sample_df[\"image\"].values\n    labels = sample_df[\"labels\"].values\n    plt.figure(figsize=(16, 12))\n    \n    for image_ind, (image_name, label) in enumerate(zip(image_names, labels)):\n        plt.subplot(3, 3, image_ind + 1)\n        image = cv2.imread(os.path.join(path, image_name))\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        plt.imshow(image)\n        plt.title(f\"{label}\", fontsize=12)\n        plt.axis(\"off\")\n    plt.show()\n    \nbatch_visualize(df_train,9,train_image_path)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:46:25.266701Z","iopub.status.idle":"2023-03-27T01:46:25.267048Z","shell.execute_reply.started":"2023-03-27T01:46:25.266878Z","shell.execute_reply":"2023-03-27T01:46:25.266900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def batch_visualize_with_label(df,batch_size,path,label): \n    sample_df = df_train[df_train[\"labels\"]==label].sample(9)\n    image_names = sample_df[\"image\"].values\n    labels = sample_df[\"labels\"].values\n    plt.figure(figsize=(16, 12))\n    \n    for image_ind, (image_name, label) in enumerate(zip(image_names, labels)):\n        plt.subplot(3, 3, image_ind + 1)\n        image = cv2.imread(os.path.join(path, image_name))\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n        plt.imshow(image)\n        plt.axis(\"off\")\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:46:25.323814Z","iopub.execute_input":"2023-03-27T01:46:25.324193Z","iopub.status.idle":"2023-03-27T01:46:25.331557Z","shell.execute_reply.started":"2023-03-27T01:46:25.324158Z","shell.execute_reply":"2023-03-27T01:46:25.330238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualize_with_label(df_train,9,train_image_path,'healthy')","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:46:25.349385Z","iopub.execute_input":"2023-03-27T01:46:25.349791Z","iopub.status.idle":"2023-03-27T01:46:38.000269Z","shell.execute_reply.started":"2023-03-27T01:46:25.349757Z","shell.execute_reply":"2023-03-27T01:46:37.998910Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualize_with_label(df_train,9,train_image_path,'scab')","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:46:38.002352Z","iopub.execute_input":"2023-03-27T01:46:38.003019Z","iopub.status.idle":"2023-03-27T01:46:50.670796Z","shell.execute_reply.started":"2023-03-27T01:46:38.002984Z","shell.execute_reply":"2023-03-27T01:46:50.669725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualize_with_label(df_train,9,train_image_path,'frog_eye_leaf_spot')","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:46:50.672074Z","iopub.execute_input":"2023-03-27T01:46:50.672359Z","iopub.status.idle":"2023-03-27T01:47:03.311152Z","shell.execute_reply.started":"2023-03-27T01:46:50.672331Z","shell.execute_reply":"2023-03-27T01:47:03.309770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualize_with_label(df_train,9,train_image_path,'rust')","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:47:03.313350Z","iopub.execute_input":"2023-03-27T01:47:03.313858Z","iopub.status.idle":"2023-03-27T01:47:12.948304Z","shell.execute_reply.started":"2023-03-27T01:47:03.313828Z","shell.execute_reply":"2023-03-27T01:47:12.946753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualize_with_label(df_train,9,train_image_path,'complex')","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:47:12.949504Z","iopub.execute_input":"2023-03-27T01:47:12.949848Z","iopub.status.idle":"2023-03-27T01:47:24.165115Z","shell.execute_reply.started":"2023-03-27T01:47:12.949816Z","shell.execute_reply":"2023-03-27T01:47:24.164091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_visualize_with_label(df_train,9,train_image_path,'powdery_mildew')","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:47:24.166293Z","iopub.execute_input":"2023-03-27T01:47:24.166602Z","iopub.status.idle":"2023-03-27T01:47:36.715207Z","shell.execute_reply.started":"2023-03-27T01:47:24.166573Z","shell.execute_reply":"2023-03-27T01:47:36.714186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HEIGHT = 128\nWIDTH=128\nSEED = 45\nBATCH_SIZE= 64\n\ntrain_datagen = ImageDataGenerator(rescale = 1/255.,\n    rotation_range=20,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    horizontal_flip=True,\n    validation_split = 0.2,\n    zoom_range = 0.2,\n    shear_range = 0.2,\n    vertical_flip = False)\n\ntrain_dataset = train_datagen.flow_from_dataframe(\n    df_train,\n    directory = train_image_path,\n    x_col = \"image\",\n    y_col = \"labels\",\n    target_size = (HEIGHT,WIDTH),\n    class_mode='categorical',\n    batch_size = BATCH_SIZE,\n    subset = \"training\",\n    shuffle = True,\n    seed = SEED,\n    validate_filenames = False\n)\n\nvalidation_dataset = train_datagen.flow_from_dataframe(\n    df_train,\n    directory = train_image_path,\n    x_col = \"image\",\n    y_col = \"labels\",\n    target_size = (HEIGHT,WIDTH),\n    class_mode='categorical',\n    batch_size = BATCH_SIZE,\n    subset = \"validation\",\n    shuffle = True,\n    seed = SEED,\n    validate_filenames = False\n)\n\ntest_datagen = ImageDataGenerator(\n    rescale = 1./255\n)\nINPUT_SIZE = (HEIGHT,WIDTH,3)\ntest_dataset=test_datagen.flow_from_dataframe(\n    df_test,\n    directory=test_image_path,\n    x_col='image',\n    y_col=None,\n    class_mode=None,\n    target_size=INPUT_SIZE[:2]\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:47:36.716398Z","iopub.execute_input":"2023-03-27T01:47:36.717374Z","iopub.status.idle":"2023-03-27T01:47:36.829804Z","shell.execute_reply.started":"2023-03-27T01:47:36.717322Z","shell.execute_reply":"2023-03-27T01:47:36.828216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Conv2D(32,(3,3),activation='relu',padding='same',input_shape=(HEIGHT,WIDTH,3)))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Conv2D(64,(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Conv2D(64,(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Conv2D(128,(3,3),activation='relu',padding='same'))\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Flatten())\nmodel.add(Dense(12,activation='softmax'))\n\n","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:47:36.831184Z","iopub.execute_input":"2023-03-27T01:47:36.831582Z","iopub.status.idle":"2023-03-27T01:47:36.931347Z","shell.execute_reply.started":"2023-03-27T01:47:36.831542Z","shell.execute_reply":"2023-03-27T01:47:36.930153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n    loss='categorical_crossentropy',\n    metrics=['accuracy'])\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:47:36.933302Z","iopub.execute_input":"2023-03-27T01:47:36.933739Z","iopub.status.idle":"2023-03-27T01:47:36.977242Z","shell.execute_reply.started":"2023-03-27T01:47:36.933698Z","shell.execute_reply":"2023-03-27T01:47:36.976092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint_path = \"training_1/cp.ckpt\"\ncheckpoint_dir = os.path.dirname(checkpoint_path)\n\n# Create a callback that saves the model's weights\ncp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,\n                                                 save_weights_only=True,\n                                                 verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:47:36.979978Z","iopub.execute_input":"2023-03-27T01:47:36.980294Z","iopub.status.idle":"2023-03-27T01:47:36.986951Z","shell.execute_reply.started":"2023-03-27T01:47:36.980263Z","shell.execute_reply":"2023-03-27T01:47:36.985473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel_history=model.fit_generator(train_dataset,\n                                  validation_data=validation_dataset,\n                                  epochs=5,\n                                  steps_per_epoch=train_dataset.samples//128,\n                                 validation_steps=validation_dataset.samples//128,\n                                 callbacks=[cp_callback]\n                                 )\n","metadata":{"execution":{"iopub.status.busy":"2023-03-27T01:47:36.988297Z","iopub.execute_input":"2023-03-27T01:47:36.988853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.class_indices.items()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(test_dataset)\nprint(preds)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_disease_ind=np.argmax(preds, axis=-1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_disease_ind","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":[]}]}