{"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 pandas as pd\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom pathlib import Path\nimport os\nimport numpy as np\nfrom keras.preprocessing.image import ImageDataGenerator\n#from keras.layers import Dense, GlobalAveragePooling2D, Input\n#from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n#from keras.applications import efficientnet_v2","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-01T08:44:10.509992Z","iopub.execute_input":"2022-05-01T08:44:10.510356Z","iopub.status.idle":"2022-05-01T08:44:15.127577Z","shell.execute_reply.started":"2022-05-01T08:44:10.510262Z","shell.execute_reply":"2022-05-01T08:44:15.126838Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Path('../input/sorghum-id-fgvc-9')","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:44:16.480393Z","iopub.execute_input":"2022-05-01T08:44:16.480975Z","iopub.status.idle":"2022-05-01T08:44:16.484463Z","shell.execute_reply.started":"2022-05-01T08:44:16.480940Z","shell.execute_reply":"2022-05-01T08:44:16.483759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(path/'train_cultivar_mapping.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:47:31.821472Z","iopub.execute_input":"2022-05-01T08:47:31.821805Z","iopub.status.idle":"2022-05-01T08:47:31.864281Z","shell.execute_reply.started":"2022-05-01T08:47:31.821766Z","shell.execute_reply":"2022-05-01T08:47:31.863603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.count()","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:50:25.486966Z","iopub.execute_input":"2022-05-01T08:50:25.487705Z","iopub.status.idle":"2022-05-01T08:50:25.500277Z","shell.execute_reply.started":"2022-05-01T08:50:25.487647Z","shell.execute_reply":"2022-05-01T08:50:25.499374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"actual_images = [img for img in os.listdir(path/'train_images') if img in train_df.image.to_list()]","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:49:38.600389Z","iopub.execute_input":"2022-05-01T08:49:38.600647Z","iopub.status.idle":"2022-05-01T08:49:48.262688Z","shell.execute_reply.started":"2022-05-01T08:49:38.600617Z","shell.execute_reply":"2022-05-01T08:49:48.261942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(actual_images)","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:50:05.911349Z","iopub.execute_input":"2022-05-01T08:50:05.911605Z","iopub.status.idle":"2022-05-01T08:50:05.918005Z","shell.execute_reply.started":"2022-05-01T08:50:05.911578Z","shell.execute_reply":"2022-05-01T08:50:05.917359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df[train_df.image.isin(actual_images)]","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:50:46.771456Z","iopub.execute_input":"2022-05-01T08:50:46.771756Z","iopub.status.idle":"2022-05-01T08:50:46.789031Z","shell.execute_reply.started":"2022-05-01T08:50:46.771717Z","shell.execute_reply":"2022-05-01T08:50:46.788251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:51:06.603247Z","iopub.execute_input":"2022-05-01T08:51:06.603506Z","iopub.status.idle":"2022-05-01T08:51:06.608671Z","shell.execute_reply.started":"2022-05-01T08:51:06.603477Z","shell.execute_reply":"2022-05-01T08:51:06.607895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:51:19.986171Z","iopub.execute_input":"2022-05-01T08:51:19.986894Z","iopub.status.idle":"2022-05-01T08:51:19.996101Z","shell.execute_reply.started":"2022-05-01T08:51:19.986854Z","shell.execute_reply":"2022-05-01T08:51:19.995444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.load_model('../input/sorghum-keras-efficientnetv2b3-model/Sorghum_keras_best_model.h5')","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:44:19.044417Z","iopub.execute_input":"2022-05-01T08:44:19.044998Z","iopub.status.idle":"2022-05-01T08:44:28.703789Z","shell.execute_reply.started":"2022-05-01T08:44:19.044955Z","shell.execute_reply":"2022-05-01T08:44:28.703051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 256\nBATCH_SIZE = 32\nEPOCHS = 30","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:44:59.573539Z","iopub.execute_input":"2022-05-01T08:44:59.574308Z","iopub.status.idle":"2022-05-01T08:44:59.578011Z","shell.execute_reply.started":"2022-05-01T08:44:59.574268Z","shell.execute_reply":"2022-05-01T08:44:59.577216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir(path/'test')\ntest_df = pd.DataFrame({'filename' : test_filenames})\n\ntest_datagen = ImageDataGenerator()\n\ntest_generator = test_datagen.flow_from_dataframe(\n    dataframe = test_df,\n    directory = path/'test',\n    x_col = 'filename',\n    y_col = None,\n    class_mode = None,\n    target_size = (IMG_SIZE, IMG_SIZE),\n    batch_size = BATCH_SIZE,\n    shuffle = False\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:45:00.592084Z","iopub.execute_input":"2022-05-01T08:45:00.592340Z","iopub.status.idle":"2022-05-01T08:45:38.681570Z","shell.execute_reply.started":"2022-05-01T08:45:00.592311Z","shell.execute_reply":"2022-05-01T08:45:38.680052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = list(train_df.cultivar.unique())\nlen(classes)","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:51:25.409860Z","iopub.execute_input":"2022-05-01T08:51:25.410117Z","iopub.status.idle":"2022-05-01T08:51:25.418297Z","shell.execute_reply.started":"2022-05-01T08:51:25.410090Z","shell.execute_reply":"2022-05-01T08:51:25.417493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(test_generator)\npred_idx = np.argmax(preds, axis=1)\npredictions = [classes[i] for i in pred_idx]","metadata":{"execution":{"iopub.status.busy":"2022-05-01T08:51:27.787574Z","iopub.execute_input":"2022-05-01T08:51:27.788191Z","iopub.status.idle":"2022-05-01T09:16:44.553345Z","shell.execute_reply.started":"2022-05-01T08:51:27.788152Z","shell.execute_reply":"2022-05-01T09:16:44.552641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['cultivar'] = predictions","metadata":{"execution":{"iopub.status.busy":"2022-05-01T09:16:44.555842Z","iopub.execute_input":"2022-05-01T09:16:44.556242Z","iopub.status.idle":"2022-05-01T09:16:44.562831Z","shell.execute_reply.started":"2022-05-01T09:16:44.556206Z","shell.execute_reply":"2022-05-01T09:16:44.562142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-01T09:16:44.565240Z","iopub.execute_input":"2022-05-01T09:16:44.565505Z","iopub.status.idle":"2022-05-01T09:16:44.623914Z","shell.execute_reply.started":"2022-05-01T09:16:44.565470Z","shell.execute_reply":"2022-05-01T09:16:44.623306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}