{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":63134,"databundleVersionId":6880857,"sourceType":"competition"}],"dockerImageVersionId":30588,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Importing Relevant Modules","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nfrom tqdm import tqdm\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom sklearn.utils import shuffle\nimport seaborn as sns\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential, Model\n# from keras.layers.normalization import BatchNormalization\nfrom keras.layers import Input, Lambda, Dense, Flatten, Activation, Dropout\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.optimizers import RMSprop\nfrom keras.callbacks import EarlyStopping, ReduceLROnPlateau\nfrom keras import applications","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-04T15:16:25.849498Z","iopub.execute_input":"2024-04-04T15:16:25.849836Z","iopub.status.idle":"2024-04-04T15:16:38.675392Z","shell.execute_reply.started":"2024-04-04T15:16:25.849809Z","shell.execute_reply":"2024-04-04T15:16:38.674333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = list(os.walk('/kaggle/input/anyas-gojo-revival-aerospace-enigma/train')) ","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:16:38.677052Z","iopub.execute_input":"2024-04-04T15:16:38.677646Z","iopub.status.idle":"2024-04-04T15:16:39.037742Z","shell.execute_reply.started":"2024-04-04T15:16:38.677618Z","shell.execute_reply":"2024-04-04T15:16:39.036895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_names = train[0][1]\ndict_labels = dict(zip(label_names, list(range(len(label_names)))))\nprint(dict_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:16:39.038907Z","iopub.execute_input":"2024-04-04T15:16:39.039277Z","iopub.status.idle":"2024-04-04T15:16:39.044755Z","shell.execute_reply.started":"2024-04-04T15:16:39.039243Z","shell.execute_reply":"2024-04-04T15:16:39.043782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"def dataset(path): #put path till the highest directory level\n    images = []\n    labels = []\n    for folder in tqdm(os.listdir(path)):\n        value_of_label = dict_labels[folder] #dict_labels is the dictionary whose key:value pairs are classes:numbers representing them\n\n        for file in (os.listdir(os.path.join(path, folder))):\n            path_of_file = os.path.join(os.path.join(path, folder), file)\n\n            image = cv2.imread(path_of_file)\n            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n            image = cv2.resize(image, (150, 150))\n            images.append(image)\n            labels.append(value_of_label)\n\n    images = np.array(images, dtype = 'float32')/255.0\n    labels = np.array(labels)\n\n    return images, labels\n\nimages, labels = dataset('/kaggle/input/anyas-gojo-revival-aerospace-enigma/train')\nimages, labels = shuffle(images, labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:16:39.046720Z","iopub.execute_input":"2024-04-04T15:16:39.046999Z","iopub.status.idle":"2024-04-04T15:16:59.845909Z","shell.execute_reply.started":"2024-04-04T15:16:39.046976Z","shell.execute_reply":"2024-04-04T15:16:59.845087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:16:59.847401Z","iopub.execute_input":"2024-04-04T15:16:59.847705Z","iopub.status.idle":"2024-04-04T15:16:59.854922Z","shell.execute_reply.started":"2024-04-04T15:16:59.847678Z","shell.execute_reply":"2024-04-04T15:16:59.854125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"species = train[0][1]\nno_of_aeroplanes = []\nfor i in range(1, 6):\n    no_of_aeroplanes.append(len(train[i][0]))\nplt.figure(figsize = (8, 8))\nsns.barplot(y = species, x = no_of_aeroplanes);","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:16:59.856231Z","iopub.execute_input":"2024-04-04T15:16:59.856508Z","iopub.status.idle":"2024-04-04T15:17:00.153149Z","shell.execute_reply.started":"2024-04-04T15:16:59.856485Z","shell.execute_reply":"2024-04-04T15:17:00.152293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,10))\nfor i in range(25):\n    plt.subplot(5, 5, i+1)\n    plt.yticks([])\n    plt.xticks([])\n    plt.grid(False)\n    data = images[i]\n    plt.xlabel(label_names[labels[i]])\n    plt.imshow(data);","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:17:00.154523Z","iopub.execute_input":"2024-04-04T15:17:00.154815Z","iopub.status.idle":"2024-04-04T15:17:01.691893Z","shell.execute_reply.started":"2024-04-04T15:17:00.154791Z","shell.execute_reply":"2024-04-04T15:17:01.690855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Preparing Dataset","metadata":{}},{"cell_type":"code","source":"image_size = (224, 224)\nbatch_size = 64\ntrain_datagen = ImageDataGenerator(rescale = 1./255,\n                            shear_range = 0.4,\n                            zoom_range = 0.4,\n                            horizontal_flip = True,\n                            vertical_flip = True,\n                            validation_split = 0.2)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:17:01.693060Z","iopub.execute_input":"2024-04-04T15:17:01.693365Z","iopub.status.idle":"2024-04-04T15:17:01.698400Z","shell.execute_reply.started":"2024-04-04T15:17:01.693339Z","shell.execute_reply":"2024-04-04T15:17:01.697535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_datagen.flow_from_directory('/kaggle/input/anyas-gojo-revival-aerospace-enigma/train',\n                                      target_size = image_size,\n                                      batch_size = batch_size,\n                                      class_mode = 'categorical',\n                                      subset = 'training',\n                                      color_mode=\"rgb\",)\n\nval_ds = train_datagen.flow_from_directory('/kaggle/input/anyas-gojo-revival-aerospace-enigma/train',\n                                      target_size = image_size,\n                                      batch_size = batch_size,\n                                      class_mode = 'categorical',\n                                      subset = 'validation',\n                                      color_mode=\"rgb\")\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:17:01.699506Z","iopub.execute_input":"2024-04-04T15:17:01.699827Z","iopub.status.idle":"2024-04-04T15:17:01.815597Z","shell.execute_reply.started":"2024-04-04T15:17:01.699803Z","shell.execute_reply":"2024-04-04T15:17:01.814806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds.class_indices","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:17:01.818930Z","iopub.execute_input":"2024-04-04T15:17:01.819235Z","iopub.status.idle":"2024-04-04T15:17:01.825442Z","shell.execute_reply.started":"2024-04-04T15:17:01.819210Z","shell.execute_reply":"2024-04-04T15:17:01.824495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(nrows=1, ncols=5, figsize=(15,15))\n\nfor i in range(5):\n    image = next(train_ds)[0][0]\n    image = np.squeeze(image)\n    ax[i].imshow(image)\n    ax[i].axis(False)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:17:01.826579Z","iopub.execute_input":"2024-04-04T15:17:01.826937Z","iopub.status.idle":"2024-04-04T15:17:06.416612Z","shell.execute_reply.started":"2024-04-04T15:17:01.826905Z","shell.execute_reply":"2024-04-04T15:17:06.415473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Xception(Fine Tuning)","metadata":{}},{"cell_type":"code","source":"xcep_base = applications.Xception(weights = 'imagenet', include_top = False, input_shape = (224, 224, 3))\nxcep_base.trainable = False\n\ninputs = Input(shape=(224, 224, 3))\n\nx = xcep_base(inputs, training=False)\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Dense(1024, activation = 'relu')(x)\nx = layers.Dropout(0.5)(x)\noutputs = layers.Dense(5, activation = 'sigmoid')(x)\nxcep_model = Model(inputs, outputs)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:17:06.418041Z","iopub.execute_input":"2024-04-04T15:17:06.418428Z","iopub.status.idle":"2024-04-04T15:17:10.045598Z","shell.execute_reply.started":"2024-04-04T15:17:06.418395Z","shell.execute_reply":"2024-04-04T15:17:10.044781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xcep_model.compile(\n    optimizer=keras.optimizers.Adam(),\n    loss= keras.losses.CategoricalCrossentropy(from_logits = True),\n    metrics= [keras.metrics.CategoricalAccuracy()],\n)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:17:10.046738Z","iopub.execute_input":"2024-04-04T15:17:10.047029Z","iopub.status.idle":"2024-04-04T15:17:10.072668Z","shell.execute_reply.started":"2024-04-04T15:17:10.047004Z","shell.execute_reply":"2024-04-04T15:17:10.071664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_results(history):\n    plt.figure(figsize=(10, 5))\n    \n    plt.subplot(1, 2, 1)\n    plt.plot(history.history['categorical_accuracy'])\n    plt.plot(history.history['val_categorical_accuracy'])\n    plt.title('model accuracy')\n    plt.ylabel('accuracy')\n    plt.xlabel('epochs')\n    plt.legend(['train', 'validation'], loc='upper left')\n    \n    plt.subplot(1, 2, 2)\n    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', 'validation'], loc='upper left')\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:17:10.073829Z","iopub.execute_input":"2024-04-04T15:17:10.074135Z","iopub.status.idle":"2024-04-04T15:17:10.081247Z","shell.execute_reply.started":"2024-04-04T15:17:10.074100Z","shell.execute_reply":"2024-04-04T15:17:10.080343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 25\nhistory=xcep_model.fit(train_ds, epochs=epochs, validation_data=val_ds)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:17:10.082711Z","iopub.execute_input":"2024-04-04T15:17:10.082984Z","iopub.status.idle":"2024-04-04T15:31:13.453288Z","shell.execute_reply.started":"2024-04-04T15:17:10.082960Z","shell.execute_reply":"2024-04-04T15:31:13.452318Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xcep_model.metrics_names\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:31:13.454900Z","iopub.execute_input":"2024-04-04T15:31:13.455601Z","iopub.status.idle":"2024-04-04T15:31:13.462058Z","shell.execute_reply.started":"2024-04-04T15:31:13.455565Z","shell.execute_reply":"2024-04-04T15:31:13.461203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation","metadata":{}},{"cell_type":"code","source":"plot_results(history)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:31:13.463258Z","iopub.execute_input":"2024-04-04T15:31:13.463607Z","iopub.status.idle":"2024-04-04T15:31:13.928017Z","shell.execute_reply.started":"2024-04-04T15:31:13.463574Z","shell.execute_reply":"2024-04-04T15:31:13.927153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predictor(img, model):\n    image = cv2.imread(img)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image = cv2.resize(image, (224, 224))\n    image = np.array(image, dtype = 'float32')/255.0\n    plt.imshow(image)\n    image = image.reshape(1, 224,224,3)\n    label_names = train_ds.class_indices\n    clas = model.predict(image).argmax()\n    name = clas\n    print('The given image is of \\nClass: {0} \\nSpecies: {1}'.format(clas, name))","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:31:13.929250Z","iopub.execute_input":"2024-04-04T15:31:13.929617Z","iopub.status.idle":"2024-04-04T15:31:13.938696Z","shell.execute_reply.started":"2024-04-04T15:31:13.929581Z","shell.execute_reply":"2024-04-04T15:31:13.937960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictor('/kaggle/input/anyas-gojo-revival-aerospace-enigma/test/image223.jpg', xcep_model) #example image","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:31:13.939705Z","iopub.execute_input":"2024-04-04T15:31:13.939982Z","iopub.status.idle":"2024-04-04T15:31:15.420787Z","shell.execute_reply.started":"2024-04-04T15:31:13.939958Z","shell.execute_reply":"2024-04-04T15:31:15.419987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Test Prediction","metadata":{}},{"cell_type":"code","source":"test_path = '/kaggle/input/anyas-gojo-revival-aerospace-enigma/test'\nX_test=[]\nimg_name_list = []\n\nfor img in os.listdir(test_path):\n    img_path = test_path + '/' + img\n    img_arr=cv2.imread(img_path)\n    img_arr=cv2.resize(img_arr,(224,224))\n    X_test.append(img_arr)\n    img_name_list.append(img)\n\nX_test = np.array(X_test).astype('float32')\nX_test = X_test/255.0\n    \ny_pred_test = xcep_model.predict(X_test)\ny_pred_test_final= []\nfor preds in y_pred_test:\n    y_pred_test_final.append(np.argmax(preds))\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:31:15.422041Z","iopub.execute_input":"2024-04-04T15:31:15.422453Z","iopub.status.idle":"2024-04-04T15:31:19.663059Z","shell.execute_reply.started":"2024-04-04T15:31:15.422427Z","shell.execute_reply":"2024-04-04T15:31:19.662139Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '/kaggle/input/anyas-gojo-revival-aerospace-enigma/train'\n\n#encoding the classes\ncode_to_cat = {}\ncat_to_code = {}\n\ncode_to_cat[0]='commercial'\ncode_to_cat[1]='glider'\ncode_to_cat[2]='helicopter'\ncode_to_cat[3]='mig-29'\ncode_to_cat[4]='sukhoi'","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:31:19.664410Z","iopub.execute_input":"2024-04-04T15:31:19.664865Z","iopub.status.idle":"2024-04-04T15:31:19.670883Z","shell.execute_reply.started":"2024-04-04T15:31:19.664830Z","shell.execute_reply":"2024-04-04T15:31:19.669831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_list = []\nfor i in y_pred_test_final:\n    cat_list.append(code_to_cat[i])\n","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:31:19.672160Z","iopub.execute_input":"2024-04-04T15:31:19.672501Z","iopub.status.idle":"2024-04-04T15:31:19.683922Z","shell.execute_reply.started":"2024-04-04T15:31:19.672469Z","shell.execute_reply":"2024-04-04T15:31:19.683219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ndata = {'image_path': img_name_list, 'class': cat_list}\nsub_df = pd.DataFrame(data)\nsub_df.set_index('image_path', inplace=True)\nsub_df","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:31:19.684967Z","iopub.execute_input":"2024-04-04T15:31:19.685570Z","iopub.status.idle":"2024-04-04T15:31:19.704026Z","shell.execute_reply.started":"2024-04-04T15:31:19.685545Z","shell.execute_reply":"2024-04-04T15:31:19.703075Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission File","metadata":{}},{"cell_type":"code","source":"sub_df.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-04-04T15:31:19.705193Z","iopub.execute_input":"2024-04-04T15:31:19.705467Z","iopub.status.idle":"2024-04-04T15:31:19.712061Z","shell.execute_reply.started":"2024-04-04T15:31:19.705444Z","shell.execute_reply":"2024-04-04T15:31:19.711189Z"},"trusted":true},"execution_count":null,"outputs":[]}]}