{"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 glob\nimport cv2\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport random\nimport numpy as np\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-04-07T13:31:17.802323Z","iopub.execute_input":"2022-04-07T13:31:17.802585Z","iopub.status.idle":"2022-04-07T13:31:17.808569Z","shell.execute_reply.started":"2022-04-07T13:31:17.802556Z","shell.execute_reply":"2022-04-07T13:31:17.807727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls","metadata":{"execution":{"iopub.status.busy":"2022-04-07T08:28:04.852246Z","iopub.execute_input":"2022-04-07T08:28:04.8525Z","iopub.status.idle":"2022-04-07T08:28:05.813365Z","shell.execute_reply.started":"2022-04-07T08:28:04.852471Z","shell.execute_reply":"2022-04-07T08:28:05.812551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.getcwd())","metadata":{"execution":{"iopub.status.busy":"2022-04-07T08:28:05.816412Z","iopub.execute_input":"2022-04-07T08:28:05.816974Z","iopub.status.idle":"2022-04-07T08:28:05.82311Z","shell.execute_reply.started":"2022-04-07T08:28:05.816942Z","shell.execute_reply":"2022-04-07T08:28:05.8222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_images = glob.glob(r'../input/sorghum-id-fgvc-9/test/*')\ntrain_images = glob.glob(r'../input/sorghum-id-fgvc-9/train_images/*')\n\nprint(f'Total Training Images : {len(train_images)}')\nprint(f'Total Test Images : {len(test_images)}')","metadata":{"execution":{"iopub.status.busy":"2022-04-07T13:31:21.778787Z","iopub.execute_input":"2022-04-07T13:31:21.779480Z","iopub.status.idle":"2022-04-07T13:31:23.117920Z","shell.execute_reply.started":"2022-04-07T13:31:21.779438Z","shell.execute_reply":"2022-04-07T13:31:23.116397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_mapping_csv = pd.read_csv(r'../input/sorghum-id-fgvc-9/train_cultivar_mapping.csv')\nsample_submission_csv = pd.read_csv(r'../input/sorghum-id-fgvc-9/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-07T13:31:23.119481Z","iopub.execute_input":"2022-04-07T13:31:23.119835Z","iopub.status.idle":"2022-04-07T13:31:23.175921Z","shell.execute_reply.started":"2022-04-07T13:31:23.119795Z","shell.execute_reply":"2022-04-07T13:31:23.175213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_mapping_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T13:31:23.177320Z","iopub.execute_input":"2022-04-07T13:31:23.177585Z","iopub.status.idle":"2022-04-07T13:31:23.193257Z","shell.execute_reply.started":"2022-04-07T13:31:23.177540Z","shell.execute_reply":"2022-04-07T13:31:23.192409Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_names = train_mapping_csv['image'].to_list()\ntrain_labels = train_mapping_csv['cultivar'].to_list()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T13:31:24.849147Z","iopub.execute_input":"2022-04-07T13:31:24.849609Z","iopub.status.idle":"2022-04-07T13:31:24.854522Z","shell.execute_reply.started":"2022-04-07T13:31:24.849573Z","shell.execute_reply":"2022-04-07T13:31:24.853879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_names = np.unique(train_labels).tolist()[:-1]","metadata":{"execution":{"iopub.status.busy":"2022-04-07T13:31:25.223320Z","iopub.execute_input":"2022-04-07T13:31:25.224045Z","iopub.status.idle":"2022-04-07T13:31:25.242050Z","shell.execute_reply.started":"2022-04-07T13:31:25.224000Z","shell.execute_reply":"2022-04-07T13:31:25.241253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_index = {k : v for k,v in zip(label_names,range(len(label_names)))}\nindex_class = {v : k for k,v in zip(label_names,range(len(label_names)))}","metadata":{"execution":{"iopub.status.busy":"2022-04-07T13:31:25.595562Z","iopub.execute_input":"2022-04-07T13:31:25.595802Z","iopub.status.idle":"2022-04-07T13:31:25.600996Z","shell.execute_reply.started":"2022-04-07T13:31:25.595774Z","shell.execute_reply":"2022-04-07T13:31:25.599810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_filepath = []\ntrain_filepath_labels = []\ntrain_filepath_labels_index = []\nfilenames = []\n\nfor i in train_images:\n    im_name = os.path.basename(i)\n    im_label = train_labels[image_names.index(im_name)]\n    train_filepath.append(i)\n    train_filepath_labels.append(im_label)\n    train_filepath_labels_index.append(str(class_index.get(im_label)))\n    filenames.append(im_name)\n\nfilepath_df = pd.DataFrame.from_dict({'Filepaths':train_filepath, 'Labels' : train_filepath_labels, 'Filename' : filenames,'Label_Index': train_filepath_labels_index})","metadata":{"execution":{"iopub.status.busy":"2022-04-07T13:31:28.316782Z","iopub.execute_input":"2022-04-07T13:31:28.317550Z","iopub.status.idle":"2022-04-07T13:31:34.368804Z","shell.execute_reply.started":"2022-04-07T13:31:28.317505Z","shell.execute_reply":"2022-04-07T13:31:34.368092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T13:31:34.370443Z","iopub.execute_input":"2022-04-07T13:31:34.370790Z","iopub.status.idle":"2022-04-07T13:31:34.383068Z","shell.execute_reply.started":"2022-04-07T13:31:34.370752Z","shell.execute_reply":"2022-04-07T13:31:34.382235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def visualize_train_dataset(df,select_label=None):\n\n    if select_label:\n        print(f'Selected Label : {select_label}')\n        df = df[df['Labels'] == select_label]\n        \n    r6_df = df.sample(16)\n    \n    filepaths = r6_df['Filepaths'].to_list()\n    labels = r6_df['Labels'].to_list()\n    \n    plt.figure(figsize=(16,16))\n    for indx,i in enumerate(filepaths):\n        lb = labels[indx]\n        im = cv2.imread(i)\n        plt.subplot(4,4,indx+1)\n        plt.title(lb)\n        plt.imshow(im[:,:,::-1])\n        plt.axis('off')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T08:28:10.16174Z","iopub.status.idle":"2022-04-07T08:28:10.162089Z","shell.execute_reply.started":"2022-04-07T08:28:10.161923Z","shell.execute_reply":"2022-04-07T08:28:10.161945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_train_dataset(filepath_df,select_label = label_names[52])","metadata":{"execution":{"iopub.status.busy":"2022-04-07T08:28:10.163184Z","iopub.status.idle":"2022-04-07T08:28:10.16372Z","shell.execute_reply.started":"2022-04-07T08:28:10.163469Z","shell.execute_reply":"2022-04-07T08:28:10.163494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"visualize_train_dataset(filepath_df,select_label = label_names[28])","metadata":{"execution":{"iopub.status.busy":"2022-04-07T08:28:10.165064Z","iopub.status.idle":"2022-04-07T08:28:10.165686Z","shell.execute_reply.started":"2022-04-07T08:28:10.165432Z","shell.execute_reply":"2022-04-07T08:28:10.165458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing import image\nfrom keras_preprocessing.image import ImageDataGenerator\nimport tensorflow\nimport tensorflow as tf\nfrom tensorflow.keras.applications.xception import preprocess_input","metadata":{"execution":{"iopub.status.busy":"2022-04-07T13:28:49.359327Z","iopub.execute_input":"2022-04-07T13:28:49.359600Z","iopub.status.idle":"2022-04-07T13:28:53.859101Z","shell.execute_reply.started":"2022-04-07T13:28:49.359570Z","shell.execute_reply":"2022-04-07T13:28:53.858214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df, valid_df = train_test_split(filepath_df, test_size=0.2, random_state=42,stratify=filepath_df['Label_Index'], shuffle=True)\ntrain_df.to_csv('TrainDF.csv')\nvalid_df.to_csv('ValidDF.csv')","metadata":{"execution":{"iopub.status.busy":"2022-04-07T08:28:10.169028Z","iopub.status.idle":"2022-04-07T08:28:10.169627Z","shell.execute_reply.started":"2022-04-07T08:28:10.169386Z","shell.execute_reply":"2022-04-07T08:28:10.169411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n                             horizontal_flip=True,\n                             vertical_flip=True,\n                             rotation_range=30,\n                             brightness_range=(0.2, 0.8),\n                             zoom_range=[0.5,1.0],\n                             preprocessing_function = preprocess_input\n                            )\n\nval_datagen = ImageDataGenerator(preprocessing_function = preprocess_input)\n\nimg_height, img_width = 224,224\nbatch_size = 256\n\ntrain_generator = train_datagen.flow_from_dataframe(dataframe=train_df, directory='../input/sorghum-id-fgvc-9/train_images',\n                                              x_col=\"Filename\", \n                                              y_col=\"Label_Index\", \n                                              batch_size=batch_size, \n                                              target_size=(img_height, img_width),\n                                              shuffle=True)\n\nvalid_generator = val_datagen.flow_from_dataframe(dataframe=valid_df, directory='../input/sorghum-id-fgvc-9/train_images',\n                                              x_col=\"Filename\", \n                                              y_col=\"Label_Index\", \n                                              batch_size=batch_size,  \n                                              target_size=(img_height, img_width),\n                                              shuffle=False)\n\nSTEP_SIZE_TRAIN = train_generator.n // train_generator.batch_size\nSTEP_SIZE_VALID = valid_generator.n // valid_generator.batch_size","metadata":{"execution":{"iopub.status.busy":"2022-04-07T08:28:10.174607Z","iopub.status.idle":"2022-04-07T08:28:10.175038Z","shell.execute_reply.started":"2022-04-07T08:28:10.174805Z","shell.execute_reply":"2022-04-07T08:28:10.174841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# https://colab.research.google.com/github/mschuessler/two4two/blob/trainKerasExample/examples/two4two_leNet.ipynb#scrollTo=SNaYUXbBFXJG","metadata":{"execution":{"iopub.status.busy":"2022-04-07T08:28:10.176343Z","iopub.status.idle":"2022-04-07T08:28:10.176752Z","shell.execute_reply.started":"2022-04-07T08:28:10.176526Z","shell.execute_reply":"2022-04-07T08:28:10.176547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n    tensorflow.keras.backend.clear_session()\n    base_model = tensorflow.keras.applications.Xception(weights='imagenet',  \n                                                        input_shape=(img_height, img_width, 3),\n                                                        include_top=False)\n    base_model.trainable = False\n    inputs = tensorflow.keras.Input(shape=(img_height, img_width, 3))\n    x = base_model(inputs, training=False)\n    x = tensorflow.keras.layers.GlobalAveragePooling2D()(x)\n    x = tensorflow.keras.layers.Dense(256,activation='relu')(x)\n    x = tf.keras.layers.Dropout(0.2)(x)\n    x = tensorflow.keras.layers.Dense(128,activation='relu')(x)\n    outputs = tensorflow.keras.layers.Dense(100)(x)\n    model = tensorflow.keras.Model(inputs, outputs)\n    return model","metadata":{"execution":{"iopub.status.busy":"2022-04-07T08:28:10.178082Z","iopub.status.idle":"2022-04-07T08:28:10.178516Z","shell.execute_reply.started":"2022-04-07T08:28:10.178283Z","shell.execute_reply":"2022-04-07T08:28:10.178308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model_x = create_model()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T14:24:15.437510Z","iopub.execute_input":"2022-04-07T14:24:15.437766Z","iopub.status.idle":"2022-04-07T14:24:15.441874Z","shell.execute_reply.started":"2022-04-07T14:24:15.437735Z","shell.execute_reply":"2022-04-07T14:24:15.440874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model_x.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T14:24:15.938103Z","iopub.execute_input":"2022-04-07T14:24:15.938388Z","iopub.status.idle":"2022-04-07T14:24:15.943026Z","shell.execute_reply.started":"2022-04-07T14:24:15.938349Z","shell.execute_reply":"2022-04-07T14:24:15.941501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_x = tf.keras.models.load_model(r'../input/model-1/base_model_1/09-0.191683-0.183594.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-04-07T14:24:16.521745Z","iopub.execute_input":"2022-04-07T14:24:16.522316Z","iopub.status.idle":"2022-04-07T14:24:19.342661Z","shell.execute_reply.started":"2022-04-07T14:24:16.522278Z","shell.execute_reply":"2022-04-07T14:24:19.341909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_x.compile(optimizer=tensorflow.keras.optimizers.Adam(),\n              loss=tensorflow.keras.losses.CategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])\n\nmodel_filepath = \"base_model_2/{epoch:02d}-{accuracy:02f}-{val_accuracy:02f}.hdf5\"\n\nmodel_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(\n    filepath=model_filepath,\n    save_weights_only=False,\n    monitor='val_accuracy',\n    mode='max',\n    save_best_only=True)\n\nhistory = model_x.fit(train_generator,\n            validation_data=valid_generator,\n            validation_steps=STEP_SIZE_VALID,\n            steps_per_epoch=STEP_SIZE_TRAIN,\n            epochs=10,\n            callbacks = [model_checkpoint_callback])","metadata":{"execution":{"iopub.status.busy":"2022-04-07T08:28:10.186427Z","iopub.status.idle":"2022-04-07T08:28:10.187178Z","shell.execute_reply.started":"2022-04-07T08:28:10.18693Z","shell.execute_reply":"2022-04-07T08:28:10.186954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model_test = tf.keras.models.load_model(r'../input/model-1/base_model_1/09-0.191683-0.183594.hdf5')","metadata":{"execution":{"iopub.status.busy":"2022-04-07T13:29:20.072016Z","iopub.execute_input":"2022-04-07T13:29:20.072738Z","iopub.status.idle":"2022-04-07T13:29:25.252747Z","shell.execute_reply.started":"2022-04-07T13:29:20.072696Z","shell.execute_reply":"2022-04-07T13:29:25.251975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import cv2\n# from tqdm.autonotebook import tqdm\n# def generate_predictions():\n#     layer = tf.keras.layers.Softmax()\n#     test_image_folder = r'../input/sorghum-id-fgvc-9/test'\n#     sample_submission_csv = pd.read_csv(r'../input/sorghum-id-fgvc-9/sample_submission.csv')\n#     test_filenames = sample_submission_csv['filename'].to_list()\n#     _filenames = []\n#     _preds = []\n#     for f in tqdm(test_filenames,total=len(test_filenames)):\n        \n#         f = test_image_folder + '/' + f\n#         im = cv2.imread(f)\n#         im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)\n#         im = cv2.resize(im, (224, 224))\n#         im = preprocess_input(im)\n#         #print(im.shape)\n#         #im = im/255.0\n#         im = np.expand_dims(im,0)\n#         y = model_test.predict(im)\n#         y = layer(y).numpy()\n#         y_pred = np.argmax(y)\n#         y_pred_name = index_class.get(y_pred)\n#         _filenames.append(os.path.basename(f))\n#         _preds.append(y_pred_name)\n    \n#     df = pd.DataFrame.from_dict({'filename':_filenames,'cultivar': _preds})\n    \n#     return df","metadata":{"execution":{"iopub.status.busy":"2022-04-07T14:25:44.525757Z","iopub.execute_input":"2022-04-07T14:25:44.526179Z","iopub.status.idle":"2022-04-07T14:25:44.533005Z","shell.execute_reply.started":"2022-04-07T14:25:44.526142Z","shell.execute_reply":"2022-04-07T14:25:44.532314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_df = generate_predictions()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T13:31:35.426149Z","iopub.execute_input":"2022-04-07T13:31:35.426421Z","iopub.status.idle":"2022-04-07T14:09:39.544618Z","shell.execute_reply.started":"2022-04-07T13:31:35.426389Z","shell.execute_reply":"2022-04-07T14:09:39.543881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T14:19:25.842451Z","iopub.execute_input":"2022-04-07T14:19:25.842718Z","iopub.status.idle":"2022-04-07T14:19:25.854405Z","shell.execute_reply.started":"2022-04-07T14:19:25.842689Z","shell.execute_reply":"2022-04-07T14:19:25.853602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_df.to_csv('submission_1.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-07T14:09:39.560349Z","iopub.execute_input":"2022-04-07T14:09:39.560617Z","iopub.status.idle":"2022-04-07T14:09:39.635357Z","shell.execute_reply.started":"2022-04-07T14:09:39.560583Z","shell.execute_reply":"2022-04-07T14:09:39.634746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sample_submission_csv = pd.read_csv(r'../input/sorghum-id-fgvc-9/sample_submission.csv')\n# sample_submission_csv.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-07T08:28:10.198287Z","iopub.status.idle":"2022-04-07T08:28:10.199966Z","shell.execute_reply.started":"2022-04-07T08:28:10.199702Z","shell.execute_reply":"2022-04-07T08:28:10.199727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}