{"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 sklearn.model_selection import train_test_split\nfrom pathlib import Path","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:42:27.248337Z","iopub.execute_input":"2022-04-30T13:42:27.249078Z","iopub.status.idle":"2022-04-30T13:42:32.423316Z","shell.execute_reply.started":"2022-04-30T13:42:27.248956Z","shell.execute_reply":"2022-04-30T13:42:32.422546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:42:32.424849Z","iopub.execute_input":"2022-04-30T13:42:32.425165Z","iopub.status.idle":"2022-04-30T13:42:32.614289Z","shell.execute_reply.started":"2022-04-30T13:42:32.425064Z","shell.execute_reply":"2022-04-30T13:42:32.612077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.version","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:42:40.861388Z","iopub.execute_input":"2022-04-30T13:42:40.861642Z","iopub.status.idle":"2022-04-30T13:42:40.869649Z","shell.execute_reply.started":"2022-04-30T13:42:40.861615Z","shell.execute_reply":"2022-04-30T13:42:40.868957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.config.list_physical_devices('GPU')","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:42:47.688580Z","iopub.execute_input":"2022-04-30T13:42:47.689173Z","iopub.status.idle":"2022-04-30T13:42:47.694735Z","shell.execute_reply.started":"2022-04-30T13:42:47.689138Z","shell.execute_reply":"2022-04-30T13:42:47.693903Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path  = Path('../input/small-jpegs-fgvc')\npath","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:43:07.154235Z","iopub.execute_input":"2022-04-30T13:43:07.154487Z","iopub.status.idle":"2022-04-30T13:43:07.160994Z","shell.execute_reply.started":"2022-04-30T13:43:07.154460Z","shell.execute_reply":"2022-04-30T13:43:07.159927Z"},"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-04-30T13:43:11.158606Z","iopub.execute_input":"2022-04-30T13:43:11.159303Z","iopub.status.idle":"2022-04-30T13:43:11.249361Z","shell.execute_reply.started":"2022-04-30T13:43:11.159264Z","shell.execute_reply":"2022-04-30T13:43:11.248642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:43:12.280973Z","iopub.execute_input":"2022-04-30T13:43:12.281345Z","iopub.status.idle":"2022-04-30T13:43:12.301673Z","shell.execute_reply.started":"2022-04-30T13:43:12.281307Z","shell.execute_reply":"2022-04-30T13:43:12.300966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.is_exist.unique()","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:43:14.859942Z","iopub.execute_input":"2022-04-30T13:43:14.860313Z","iopub.status.idle":"2022-04-30T13:43:14.875783Z","shell.execute_reply.started":"2022-04-30T13:43:14.860276Z","shell.execute_reply":"2022-04-30T13:43:14.875056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 256\nBATCH_SIZE = 32","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:49:54.882716Z","iopub.execute_input":"2022-04-30T13:49:54.882981Z","iopub.status.idle":"2022-04-30T13:49:54.886895Z","shell.execute_reply.started":"2022-04-30T13:49:54.882950Z","shell.execute_reply":"2022-04-30T13:49:54.886030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ntrain_datagen = ImageDataGenerator(\n    rotation_range=30,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    zoom_range=0.15,\n    horizontal_flip=True,\n    fill_mode='nearest',\n    validation_split=0.2\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:49:55.263313Z","iopub.execute_input":"2022-04-30T13:49:55.264054Z","iopub.status.idle":"2022-04-30T13:49:55.269841Z","shell.execute_reply.started":"2022-04-30T13:49:55.263985Z","shell.execute_reply":"2022-04-30T13:49:55.268848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=path/'train',\n    x_col='image',\n    y_col='cultivar',\n    class_mode='sparse',\n    target_size=(IMG_SIZE, IMG_SIZE),\n    batch_size=BATCH_SIZE,\n    subset='training',\n    shuffle=True\n)\n\nvalid_generator = train_datagen.flow_from_dataframe(\n    dataframe=train_df,\n    directory=path/'train',\n    x_col='image',\n    y_col='cultivar',\n    class_mode='sparse',\n    target_size=(IMG_SIZE, IMG_SIZE),\n    batch_size=BATCH_SIZE,\n    subset='validation',\n    shuffle=True\n)\n\ntest_datagen = ImageDataGenerator()","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:49:55.981480Z","iopub.execute_input":"2022-04-30T13:49:55.981734Z","iopub.status.idle":"2022-04-30T13:50:04.383198Z","shell.execute_reply.started":"2022-04-30T13:49:55.981704Z","shell.execute_reply":"2022-04-30T13:50:04.382079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = keras.applications.EfficientNetB4(weights='imagenet', include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3))","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:44:37.443244Z","iopub.execute_input":"2022-04-30T13:44:37.443653Z","iopub.status.idle":"2022-04-30T13:44:43.729732Z","shell.execute_reply.started":"2022-04-30T13:44:37.443615Z","shell.execute_reply":"2022-04-30T13:44:43.728920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Dense, GlobalAveragePooling2D, Input\n\ninputs = Input(shape=(IMG_SIZE, IMG_SIZE, 3))\noutput = base_model(inputs)\noutput = GlobalAveragePooling2D()(output)\noutput = Dense(100, activation='softmax')(output)\n\nmodel = keras.Model(inputs, output)\n\nmodel.summary()\n\nmodel.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:50:04.384871Z","iopub.execute_input":"2022-04-30T13:50:04.385132Z","iopub.status.idle":"2022-04-30T13:50:05.487485Z","shell.execute_reply.started":"2022-04-30T13:50:04.385096Z","shell.execute_reply":"2022-04-30T13:50:05.486810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel.fit(train_generator, validation_data=valid_generator, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:50:07.280443Z","iopub.execute_input":"2022-04-30T13:50:07.280699Z","iopub.status.idle":"2022-04-30T16:08:56.508697Z","shell.execute_reply.started":"2022-04-30T13:50:07.280670Z","shell.execute_reply":"2022-04-30T16:08:56.507980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\ntest_filenames = os.listdir(path/'test')\ntest_df = pd.DataFrame({'filename' : test_filenames})\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-04-30T16:24:13.522638Z","iopub.execute_input":"2022-04-30T16:24:13.522912Z","iopub.status.idle":"2022-04-30T16:24:22.318639Z","shell.execute_reply.started":"2022-04-30T16:24:13.522881Z","shell.execute_reply":"2022-04-30T16:24:22.317905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = list(train_df.cultivar.unique())\nlen(classes)","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:20:38.375805Z","iopub.execute_input":"2022-04-30T16:20:38.376116Z","iopub.status.idle":"2022-04-30T16:20:38.383502Z","shell.execute_reply.started":"2022-04-30T16:20:38.376084Z","shell.execute_reply":"2022-04-30T16:20:38.382727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\npreds = 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-04-30T16:25:15.774403Z","iopub.execute_input":"2022-04-30T16:25:15.774780Z","iopub.status.idle":"2022-04-30T16:36:41.175835Z","shell.execute_reply.started":"2022-04-30T16:25:15.774731Z","shell.execute_reply":"2022-04-30T16:36:41.174249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['cultivar'] = predictions","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:37:15.939902Z","iopub.execute_input":"2022-04-30T16:37:15.940414Z","iopub.status.idle":"2022-04-30T16:37:16.003887Z","shell.execute_reply.started":"2022-04-30T16:37:15.940372Z","shell.execute_reply":"2022-04-30T16:37:16.003207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:46:17.178438Z","iopub.execute_input":"2022-04-30T16:46:17.178709Z","iopub.status.idle":"2022-04-30T16:46:17.184548Z","shell.execute_reply.started":"2022-04-30T16:46:17.178677Z","shell.execute_reply":"2022-04-30T16:46:17.183679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.filename = test_df.filename.map(lambda x: x.split('.jpeg')[0] + '.png')","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:57:03.916201Z","iopub.execute_input":"2022-04-30T16:57:03.916774Z","iopub.status.idle":"2022-04-30T16:57:03.935115Z","shell.execute_reply.started":"2022-04-30T16:57:03.916735Z","shell.execute_reply":"2022-04-30T16:57:03.934352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:57:42.573510Z","iopub.execute_input":"2022-04-30T16:57:42.574107Z","iopub.status.idle":"2022-04-30T16:57:42.584575Z","shell.execute_reply.started":"2022-04-30T16:57:42.574062Z","shell.execute_reply":"2022-04-30T16:57:42.583686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-04-30T16:58:44.293890Z","iopub.execute_input":"2022-04-30T16:58:44.294764Z","iopub.status.idle":"2022-04-30T16:58:44.348232Z","shell.execute_reply.started":"2022-04-30T16:58:44.294720Z","shell.execute_reply":"2022-04-30T16:58:44.347518Z"},"trusted":true},"execution_count":null,"outputs":[]}]}