{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":63134,"databundleVersionId":6880857,"sourceType":"competition"}],"dockerImageVersionId":30559,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# ****Importing Modules and Packages****","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport tensorflow as tf\nimport tensorflow_hub as hub\nimport cv2\nimport imghdr\nimport os\nimport pandas as pd\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras import datasets, layers, models\nfrom keras.models import Model\nfrom keras.layers import Dense,Flatten,Dropout,GlobalAveragePooling2D\nfrom keras.applications.inception_v3 import InceptionV3,preprocess_input\nfrom tensorflow.keras.regularizers import l2\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.metrics import make_scorer, accuracy_score\nfrom sklearn.model_selection import GridSearchCV\nfrom keras.wrappers.scikit_learn import KerasClassifier\nimport keras\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom PIL import ImageFile\nfrom tensorflow.keras.preprocessing.image import load_img,img_to_array\nimport matplotlib.pyplot as plt\nfrom IPython.display import display, Image\nimport random\nimport matplotlib.image as mpimg","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-21T19:05:23.277819Z","iopub.execute_input":"2023-11-21T19:05:23.278790Z","iopub.status.idle":"2023-11-21T19:05:23.287295Z","shell.execute_reply.started":"2023-11-21T19:05:23.278738Z","shell.execute_reply":"2023-11-21T19:05:23.286341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Image augmentation on train dataset and data split****","metadata":{}},{"cell_type":"code","source":"# train_datagen=ImageDataGenerator(featurewise_center=True, validation_split=0.06,rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,horizontal_flip=True,preprocessing_function=preprocess_input,zoom_range=0.2,shear_range=0.2,fill_mode='nearest',brightness_range=[0.5, 1.5])\ntrain_datagen=ImageDataGenerator(validation_split=0.1,preprocessing_function=preprocess_input,)\ntrain_data=train_datagen.flow_from_directory(directory=\"/kaggle/input/anyas-gojo-revival-aerospace-enigma/train\",target_size=(256,256),batch_size=50,subset = 'training')\nvalidation_data=train_datagen.flow_from_directory(directory=\"/kaggle/input/anyas-gojo-revival-aerospace-enigma/train\",target_size=(256,256),batch_size=50,subset = 'validation')","metadata":{"execution":{"iopub.status.busy":"2023-11-21T19:05:23.288804Z","iopub.execute_input":"2023-11-21T19:05:23.289079Z","iopub.status.idle":"2023-11-21T19:05:23.418911Z","shell.execute_reply.started":"2023-11-21T19:05:23.289054Z","shell.execute_reply":"2023-11-21T19:05:23.418064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Data Visualization****","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.bar([\"commercial\",\"glider\",\"helicopter\",\"mig-29\",\"sukhoi\"],[len(os.listdir(\"/kaggle/input/anyas-gojo-revival-aerospace-enigma/train/commercial\")),len(os.listdir(\"/kaggle/input/anyas-gojo-revival-aerospace-enigma/train/glider\")),len(os.listdir(\"/kaggle/input/anyas-gojo-revival-aerospace-enigma/train/helicopter\")),len(os.listdir(\"/kaggle/input/anyas-gojo-revival-aerospace-enigma/train/mig-29\")),len(os.listdir(\"/kaggle/input/anyas-gojo-revival-aerospace-enigma/train/sukhoi\"))] , color='skyblue')\nplt.xlabel('Training Labels')\nplt.ylabel('Frequency of Each Label')\nplt.title('Bar Plot for Frequency of Each Label')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T19:05:23.420002Z","iopub.execute_input":"2023-11-21T19:05:23.420323Z","iopub.status.idle":"2023-11-21T19:05:23.614705Z","shell.execute_reply.started":"2023-11-21T19:05:23.420298Z","shell.execute_reply":"2023-11-21T19:05:23.613805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_random_images():\n    images = []\n    labels = []\n    root=\"/kaggle/input/anyas-gojo-revival-aerospace-enigma\"\n    for i in range(20):\n        random_class = \"/train/\" +random.choice(list(train_data.class_indices.keys()))    \n        random_path = root+random_class\n        random_image = random.sample(os.listdir(random_path),2)\n        images.append(random_path + '/' + random_image[0])\n        labels.append(random_class)\n    plt.figure(figsize=(25, 10))\n    for i in range(1, 21):\n        plt.subplot(7, 15, i)\n        img = mpimg.imread(images[i-1])\n        plt.imshow(img,aspect=\"auto\")\n        plt.title(labels[i-1].split(\"/\")[-1])\n        plt.axis(False)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T20:07:21.286533Z","iopub.execute_input":"2023-11-21T20:07:21.287353Z","iopub.status.idle":"2023-11-21T20:07:21.294848Z","shell.execute_reply.started":"2023-11-21T20:07:21.287315Z","shell.execute_reply":"2023-11-21T20:07:21.293816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"view_random_images()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T20:07:23.935622Z","iopub.execute_input":"2023-11-21T20:07:23.936002Z","iopub.status.idle":"2023-11-21T20:07:26.232502Z","shell.execute_reply.started":"2023-11-21T20:07:23.935972Z","shell.execute_reply":"2023-11-21T20:07:26.231629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Model Training****","metadata":{}},{"cell_type":"code","source":"base_model=InceptionV3(input_shape=(256,256,3),include_top=False, weights='imagenet')\nfor layer in base_model.layers:\n    layer.trainable=False\nx=base_model.output\nx=layers.Flatten()(x)\nx=Dense(units=512,activation='relu')(x)\nx=Dropout(0.1)(x)\nx=Dense(units=5,activation='softmax')(x)\nmodel1=Model(base_model.input,x)\nmodel1.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),loss=keras.losses.binary_crossentropy,metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-11-21T19:52:16.325243Z","iopub.execute_input":"2023-11-21T19:52:16.325866Z","iopub.status.idle":"2023-11-21T19:52:18.793531Z","shell.execute_reply.started":"2023-11-21T19:52:16.325834Z","shell.execute_reply":"2023-11-21T19:52:18.792720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = EarlyStopping(monitor='val_loss', patience=5, verbose=1, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T19:05:28.952993Z","iopub.execute_input":"2023-11-21T19:05:28.953292Z","iopub.status.idle":"2023-11-21T19:05:28.957127Z","shell.execute_reply.started":"2023-11-21T19:05:28.953266Z","shell.execute_reply":"2023-11-21T19:05:28.956140Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageFile.LOAD_TRUNCATED_IMAGES = True\nhistory=model1.fit(train_data,steps_per_epoch=30,epochs=30,callbacks=[early_stopping],validation_data = validation_data,validation_steps = 1)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T19:52:22.544501Z","iopub.execute_input":"2023-11-21T19:52:22.545296Z","iopub.status.idle":"2023-11-21T19:55:10.505266Z","shell.execute_reply.started":"2023-11-21T19:52:22.545264Z","shell.execute_reply":"2023-11-21T19:55:10.503956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Prediction on test dataset****","metadata":{}},{"cell_type":"code","source":"test_path='/kaggle/input/anyas-gojo-revival-aerospace-enigma/test'\nresult={}\nindices_dict={train_data.class_indices[i]:i for i in train_data.class_indices.keys()}\nfor img_path in os.listdir(test_path):\n    path =os.path.join(test_path,img_path)\n    img=load_img(path,target_size=(256,256))\n    i=img_to_array(img)\n    i=preprocess_input(i)\n    pres=np.argmax(model1.predict(np.array([i])))\n    result.update({img_path:indices_dict[pres]})","metadata":{"execution":{"iopub.status.busy":"2023-11-21T19:55:15.474863Z","iopub.execute_input":"2023-11-21T19:55:15.475641Z","iopub.status.idle":"2023-11-21T19:55:37.057945Z","shell.execute_reply.started":"2023-11-21T19:55:15.475608Z","shell.execute_reply":"2023-11-21T19:55:37.057146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Data visualizaton of Predicted array****","metadata":{}},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\nepochs_range = range(len(loss))\n\nplt.figure(figsize=(8, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T19:09:53.220926Z","iopub.status.idle":"2023-11-21T19:09:53.221408Z","shell.execute_reply.started":"2023-11-21T19:09:53.221161Z","shell.execute_reply":"2023-11-21T19:09:53.221191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in list(result.keys())[:5]:\n    display(Image(os.path.join(test_path,i)))\n    print(result[i])","metadata":{"execution":{"iopub.status.busy":"2023-11-21T19:55:43.864579Z","iopub.execute_input":"2023-11-21T19:55:43.864982Z","iopub.status.idle":"2023-11-21T19:55:43.885299Z","shell.execute_reply.started":"2023-11-21T19:55:43.864952Z","shell.execute_reply":"2023-11-21T19:55:43.884369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nplt.bar([\"commercial\",\"glider\",\"helicopter\",\"mig-29\",\"sukhoi\"],[list(result.values()).count(\"commercial\"),list(result.values()).count(\"glider\"),list(result.values()).count(\"helicopter\"),list(result.values()).count(\"mig-29\"),list(result.values()).count(\"sukhoi\")],color='skyblue')\nplt.xlabel('Training Labels')\nplt.ylabel('Frequency of Each Label')\nplt.title('Bar Plot for Frequency of Each Label on Test Data')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-21T19:09:53.224956Z","iopub.status.idle":"2023-11-21T19:09:53.225334Z","shell.execute_reply.started":"2023-11-21T19:09:53.225151Z","shell.execute_reply":"2023-11-21T19:09:53.225168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ****Saving the Results****","metadata":{}},{"cell_type":"code","source":"submission=pd.DataFrame({'image_path':result.keys(),\"Class\":result.values()})\nsubmission.to_csv(\"submission0ijhoiuygtfbf.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-21T19:55:54.204598Z","iopub.execute_input":"2023-11-21T19:55:54.204978Z","iopub.status.idle":"2023-11-21T19:55:54.212719Z","shell.execute_reply.started":"2023-11-21T19:55:54.204947Z","shell.execute_reply":"2023-11-21T19:55:54.211767Z"},"trusted":true},"execution_count":null,"outputs":[]}]}