{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3649,"databundleVersionId":46718,"sourceType":"competition"}],"dockerImageVersionId":30698,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-10T09:55:49.684854Z","iopub.execute_input":"2024-05-10T09:55:49.685661Z","iopub.status.idle":"2024-05-10T09:55:50.033884Z","shell.execute_reply.started":"2024-05-10T09:55:49.685631Z","shell.execute_reply":"2024-05-10T09:55:50.032995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# CIFAR-10 Object recognition Using Transfer Learning","metadata":{}},{"cell_type":"markdown","source":"## Importing packages and Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd \nfrom PIL import Image\nimport matplotlib.pyplot as plt \nfrom sklearn.model_selection import train_test_split\nimport matplotlib.image as mpimg","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:55:50.035494Z","iopub.execute_input":"2024-05-10T09:55:50.035862Z","iopub.status.idle":"2024-05-10T09:55:50.642234Z","shell.execute_reply.started":"2024-05-10T09:55:50.035839Z","shell.execute_reply":"2024-05-10T09:55:50.641450Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Reading Dataset","metadata":{}},{"cell_type":"code","source":"label_df = pd.read_csv(\"/kaggle/input/cifar-10/trainLabels.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:55:50.643220Z","iopub.execute_input":"2024-05-10T09:55:50.643490Z","iopub.status.idle":"2024-05-10T09:55:50.682829Z","shell.execute_reply.started":"2024-05-10T09:55:50.643467Z","shell.execute_reply":"2024-05-10T09:55:50.681926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:55:50.684908Z","iopub.execute_input":"2024-05-10T09:55:50.685273Z","iopub.status.idle":"2024-05-10T09:55:50.702421Z","shell.execute_reply.started":"2024-05-10T09:55:50.685248Z","shell.execute_reply":"2024-05-10T09:55:50.701650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:55:50.703491Z","iopub.execute_input":"2024-05-10T09:55:50.703756Z","iopub.status.idle":"2024-05-10T09:55:50.709784Z","shell.execute_reply.started":"2024-05-10T09:55:50.703734Z","shell.execute_reply":"2024-05-10T09:55:50.708963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df['label'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:55:50.711131Z","iopub.execute_input":"2024-05-10T09:55:50.711410Z","iopub.status.idle":"2024-05-10T09:55:50.734045Z","shell.execute_reply.started":"2024-05-10T09:55:50.711387Z","shell.execute_reply":"2024-05-10T09:55:50.732975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df[label_df['id'] == 100]","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:55:50.735455Z","iopub.execute_input":"2024-05-10T09:55:50.735849Z","iopub.status.idle":"2024-05-10T09:55:50.749743Z","shell.execute_reply.started":"2024-05-10T09:55:50.735812Z","shell.execute_reply":"2024-05-10T09:55:50.748833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df[label_df['id'] == 2674]","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:55:50.751071Z","iopub.execute_input":"2024-05-10T09:55:50.751529Z","iopub.status.idle":"2024-05-10T09:55:50.763822Z","shell.execute_reply.started":"2024-05-10T09:55:50.751494Z","shell.execute_reply":"2024-05-10T09:55:50.762889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Label Encoding","metadata":{}},{"cell_type":"code","source":"label_dictionary = {'airplane':0 , 'automobile':1 , 'bird':2 , 'cat':3 , 'deer':4 , 'dog':5 , 'frog':6 , 'horse':7 , \n                   'ship':8 , 'truck':9 }\nlabels = [label_dictionary[i] for i in label_df['label']]","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:55:50.765015Z","iopub.execute_input":"2024-05-10T09:55:50.765412Z","iopub.status.idle":"2024-05-10T09:55:50.784880Z","shell.execute_reply.started":"2024-05-10T09:55:50.765384Z","shell.execute_reply":"2024-05-10T09:55:50.783808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels[0:5])","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:55:50.788976Z","iopub.execute_input":"2024-05-10T09:55:50.789408Z","iopub.status.idle":"2024-05-10T09:55:50.796520Z","shell.execute_reply.started":"2024-05-10T09:55:50.789375Z","shell.execute_reply":"2024-05-10T09:55:50.795609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Verifying Data","metadata":{}},{"cell_type":"code","source":"!pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:55:50.797731Z","iopub.execute_input":"2024-05-10T09:55:50.798013Z","iopub.status.idle":"2024-05-10T09:56:15.546277Z","shell.execute_reply.started":"2024-05-10T09:55:50.797989Z","shell.execute_reply":"2024-05-10T09:56:15.545280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extracting files from archive\nimport py7zr\n\n# path of .7z file \nfile_Path  = '/kaggle/input/cifar-10/train.7z'\n# destination directory\noutput_dir = '/kaggle/working/'\n\nwith py7zr.SevenZipFile(file_Path , mode='r') as z:\n    z.extractall(output_dir)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:15.547644Z","iopub.execute_input":"2024-05-10T09:56:15.547954Z","iopub.status.idle":"2024-05-10T09:56:41.006302Z","shell.execute_reply.started":"2024-05-10T09:56:15.547927Z","shell.execute_reply":"2024-05-10T09:56:41.005453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# displaying sample image\nimport cv2\n\ndef cv2_imshow(image):\n    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) #This line converts the image from the BGR color space to the RGB color space.\n    plt.axis('off')  # This line turns off the axis labels and ticks in the plot\n    plt.show()\n\nimg_path = '/kaggle/working/train/4565.png'\nimg = cv2.imread(img_path)\n\ncv2_imshow(img)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:41.007613Z","iopub.execute_input":"2024-05-10T09:56:41.007970Z","iopub.status.idle":"2024-05-10T09:56:41.250252Z","shell.execute_reply.started":"2024-05-10T09:56:41.007938Z","shell.execute_reply":"2024-05-10T09:56:41.249310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df[label_df['id'] == 4565]","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:41.251993Z","iopub.execute_input":"2024-05-10T09:56:41.252691Z","iopub.status.idle":"2024-05-10T09:56:41.267071Z","shell.execute_reply.started":"2024-05-10T09:56:41.252659Z","shell.execute_reply":"2024-05-10T09:56:41.266141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# displaying sample image\nimport cv2\n\ndef cv2_imshow(image):\n    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) #This line converts the image from the BGR color space to the RGB color space.\n    plt.axis('off')  # This line turns off the axis labels and ticks in the plot\n    plt.show()\n\nimg_path = '/kaggle/working/train/7354.png'\nimg = cv2.imread(img_path)\n\ncv2_imshow(img)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:41.268773Z","iopub.execute_input":"2024-05-10T09:56:41.269465Z","iopub.status.idle":"2024-05-10T09:56:41.361959Z","shell.execute_reply.started":"2024-05-10T09:56:41.269431Z","shell.execute_reply":"2024-05-10T09:56:41.361039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df[label_df['id'] == 7354]","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:41.363739Z","iopub.execute_input":"2024-05-10T09:56:41.364442Z","iopub.status.idle":"2024-05-10T09:56:41.375748Z","shell.execute_reply.started":"2024-05-10T09:56:41.364407Z","shell.execute_reply":"2024-05-10T09:56:41.374726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Creating a list if id\nid_list = list(label_df['id'])","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:41.377394Z","iopub.execute_input":"2024-05-10T09:56:41.377943Z","iopub.status.idle":"2024-05-10T09:56:41.388981Z","shell.execute_reply.started":"2024-05-10T09:56:41.377909Z","shell.execute_reply":"2024-05-10T09:56:41.388129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Image Preprocessing","metadata":{}},{"cell_type":"code","source":"# making a numpy array of images of training data\nimage_data_folder = '/kaggle/working/train/'\nimage_data = []\n\nfor id in id_list:\n    image = Image.open(image_data_folder + str(id) + '.png')\n    image = np.array(image)\n    image_data.append(image)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:41.390391Z","iopub.execute_input":"2024-05-10T09:56:41.390990Z","iopub.status.idle":"2024-05-10T09:56:49.782767Z","shell.execute_reply.started":"2024-05-10T09:56:41.390959Z","shell.execute_reply":"2024-05-10T09:56:49.781972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Type of image_data: ' ,type(image_data))\nprint('--------------------------------------')\nprint('No. of images: ' , len(image_data))\nprint('--------------------------------------')\nprint('Type of Image: ' , type(image))\nprint('--------------------------------------')\nprint('Shape of the Image: ', image.shape)\nprint('--------------------------------------')","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:49.783811Z","iopub.execute_input":"2024-05-10T09:56:49.784101Z","iopub.status.idle":"2024-05-10T09:56:49.789816Z","shell.execute_reply.started":"2024-05-10T09:56:49.784068Z","shell.execute_reply":"2024-05-10T09:56:49.788977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# assigning and converting feature and targets into numpy array\nx = np.array(image_data)\ny  =np.array(labels)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:49.791069Z","iopub.execute_input":"2024-05-10T09:56:49.791715Z","iopub.status.idle":"2024-05-10T09:56:49.884437Z","shell.execute_reply.started":"2024-05-10T09:56:49.791685Z","shell.execute_reply":"2024-05-10T09:56:49.883612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape, ' -- x shape')\nprint(y.shape, '           -- y shape ')","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:49.885655Z","iopub.execute_input":"2024-05-10T09:56:49.885955Z","iopub.status.idle":"2024-05-10T09:56:49.891026Z","shell.execute_reply.started":"2024-05-10T09:56:49.885931Z","shell.execute_reply":"2024-05-10T09:56:49.890133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Train - Test Split**","metadata":{}},{"cell_type":"code","source":"x_train , x_test , y_train , y_test = train_test_split(x , y , test_size=0.2 , random_state=2)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:49.892066Z","iopub.execute_input":"2024-05-10T09:56:49.892385Z","iopub.status.idle":"2024-05-10T09:56:49.956975Z","shell.execute_reply.started":"2024-05-10T09:56:49.892343Z","shell.execute_reply":"2024-05-10T09:56:49.956137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x.shape , x_train.shape , x_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:49.958390Z","iopub.execute_input":"2024-05-10T09:56:49.961212Z","iopub.status.idle":"2024-05-10T09:56:49.966611Z","shell.execute_reply.started":"2024-05-10T09:56:49.961182Z","shell.execute_reply":"2024-05-10T09:56:49.965555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Scaling Data","metadata":{}},{"cell_type":"code","source":"# Scale the pixel values of the training data\nx_train = x_train / 255             # 255 is the maximum pixel value \n                                    #  Dividing by 255 scales the pixel values so that they range from 0 to 1.\n# Scale the pixel values of the test data\nx_test = x_test / 255\n","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:49.967833Z","iopub.execute_input":"2024-05-10T09:56:49.968116Z","iopub.status.idle":"2024-05-10T09:56:50.349896Z","shell.execute_reply.started":"2024-05-10T09:56:49.968093Z","shell.execute_reply":"2024-05-10T09:56:50.349095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Building a simple neural network model","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:56:50.350948Z","iopub.execute_input":"2024-05-10T09:56:50.351262Z","iopub.status.idle":"2024-05-10T09:57:01.426457Z","shell.execute_reply.started":"2024-05-10T09:56:50.351236Z","shell.execute_reply":"2024-05-10T09:57:01.425583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_of_classes = 10\n\nmodel = keras.Sequential([\n    keras.layers.Flatten(input_shape=(32,32,3)),\n    keras.layers.Dense(58,activation='relu'),\n    keras.layers.Dense(no_of_classes , activation='softmax')\n])\n\nmodel.compile(optimizer= 'adam',\n             loss= 'sparse_categorical_crossentropy',\n             metrics= ['acc'])\n\nmodel.fit(x_train , y_train , epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:57:01.428047Z","iopub.execute_input":"2024-05-10T09:57:01.428734Z","iopub.status.idle":"2024-05-10T09:57:25.280796Z","shell.execute_reply.started":"2024-05-10T09:57:01.428709Z","shell.execute_reply":"2024-05-10T09:57:25.279868Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" \n**Our current neural network is struggling to achieve satisfactory accuracy. To enhance our model's performance, we'll leverage a technique called Transfer Learning. This involves harnessing the knowledge gained by pre-trained models such as ResNet, EfficientNet, and MobileNet, which have been trained on extensive datasets. By incorporating these well-learned features into our own model, we can expedite the learning process and potentially achieve better accuracy.**","metadata":{}},{"cell_type":"markdown","source":"### Importing pre-trained model and packages for model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import Sequential , layers , models\nfrom tensorflow.keras import optimizers\nfrom tensorflow.keras.layers import Dense , Dropout , Flatten\nfrom tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras.models import load_model\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.applications.resnet50 import ResNet50\nfrom tensorflow.keras.applications.efficientnet import EfficientNetB0 \nfrom tensorflow.keras.applications import mobilenet","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:57:25.281964Z","iopub.execute_input":"2024-05-10T09:57:25.282344Z","iopub.status.idle":"2024-05-10T09:57:25.293108Z","shell.execute_reply.started":"2024-05-10T09:57:25.282318Z","shell.execute_reply":"2024-05-10T09:57:25.292362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## RestNet50","metadata":{}},{"cell_type":"code","source":"convolutional_base = ResNet50(weights='imagenet', include_top=False, input_shape=(256,256,3))\nconvolutional_base.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:57:25.299042Z","iopub.execute_input":"2024-05-10T09:57:25.299329Z","iopub.status.idle":"2024-05-10T09:57:29.862935Z","shell.execute_reply.started":"2024-05-10T09:57:25.299307Z","shell.execute_reply":"2024-05-10T09:57:29.861971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_of_classes = 10\n\nmodel = models.Sequential()\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(convolutional_base)\nmodel.add(layers.Flatten())\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(128,activation='relu'))\nmodel.add(layers.Dropout(0,5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dropout(0,5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(no_of_classes, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:57:29.864164Z","iopub.execute_input":"2024-05-10T09:57:29.864426Z","iopub.status.idle":"2024-05-10T09:57:29.884390Z","shell.execute_reply.started":"2024-05-10T09:57:29.864404Z","shell.execute_reply":"2024-05-10T09:57:29.883458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=optimizers.RMSprop(learning_rate=2e-5), \n              loss='sparse_categorical_crossentropy', \n              metrics=['acc'])\n","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:57:29.885585Z","iopub.execute_input":"2024-05-10T09:57:29.885874Z","iopub.status.idle":"2024-05-10T09:57:29.902508Z","shell.execute_reply.started":"2024-05-10T09:57:29.885852Z","shell.execute_reply":"2024-05-10T09:57:29.901684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train, y_train, validation_split=0.1, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T09:57:29.903610Z","iopub.execute_input":"2024-05-10T09:57:29.904372Z","iopub.status.idle":"2024-05-10T10:31:05.973496Z","shell.execute_reply.started":"2024-05-10T09:57:29.904340Z","shell.execute_reply":"2024-05-10T10:31:05.972575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss , accuracy = model.evaluate(x_test  ,y_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:31:05.975026Z","iopub.execute_input":"2024-05-10T10:31:05.975386Z","iopub.status.idle":"2024-05-10T10:31:31.087627Z","shell.execute_reply.started":"2024-05-10T10:31:05.975360Z","shell.execute_reply":"2024-05-10T10:31:31.086570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Accuracy: ',accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:31:31.090350Z","iopub.execute_input":"2024-05-10T10:31:31.090957Z","iopub.status.idle":"2024-05-10T10:31:31.095517Z","shell.execute_reply.started":"2024-05-10T10:31:31.090927Z","shell.execute_reply":"2024-05-10T10:31:31.094607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/resnet_trained_model.keras')","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:31:31.096944Z","iopub.execute_input":"2024-05-10T10:31:31.097405Z","iopub.status.idle":"2024-05-10T10:31:32.689487Z","shell.execute_reply.started":"2024-05-10T10:31:31.097316Z","shell.execute_reply":"2024-05-10T10:31:32.688474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## EfficientNetB0","metadata":{}},{"cell_type":"code","source":"convolutional_base_model_1 = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(256, 256, 3))\nconvolutional_base_model_1.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:31:32.690676Z","iopub.execute_input":"2024-05-10T10:31:32.692349Z","iopub.status.idle":"2024-05-10T10:31:35.505370Z","shell.execute_reply.started":"2024-05-10T10:31:32.692323Z","shell.execute_reply":"2024-05-10T10:31:35.504540Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_of_classes = 10\n\nmodel = models.Sequential()\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(convolutional_base_model_1)\nmodel.add(layers.Flatten())\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(128,activation='relu'))\nmodel.add(layers.Dropout(0,5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dropout(0,5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(no_of_classes, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:31:35.506730Z","iopub.execute_input":"2024-05-10T10:31:35.507128Z","iopub.status.idle":"2024-05-10T10:31:35.528566Z","shell.execute_reply.started":"2024-05-10T10:31:35.507093Z","shell.execute_reply":"2024-05-10T10:31:35.527583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=optimizers.RMSprop(learning_rate=2e-5), \n              loss='sparse_categorical_crossentropy', \n              metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:31:35.529691Z","iopub.execute_input":"2024-05-10T10:31:35.530021Z","iopub.status.idle":"2024-05-10T10:31:35.544027Z","shell.execute_reply.started":"2024-05-10T10:31:35.529988Z","shell.execute_reply":"2024-05-10T10:31:35.543161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train , y_train , validation_split=0.1 , epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:31:35.545146Z","iopub.execute_input":"2024-05-10T10:31:35.545507Z","iopub.status.idle":"2024-05-10T10:59:13.310503Z","shell.execute_reply.started":"2024-05-10T10:31:35.545484Z","shell.execute_reply":"2024-05-10T10:59:13.309550Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss , accuracy = model.evaluate(x_test , y_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:59:13.311596Z","iopub.execute_input":"2024-05-10T10:59:13.311879Z","iopub.status.idle":"2024-05-10T10:59:35.229241Z","shell.execute_reply.started":"2024-05-10T10:59:13.311854Z","shell.execute_reply":"2024-05-10T10:59:35.228441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Accuracy: ',accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:59:35.230705Z","iopub.execute_input":"2024-05-10T10:59:35.230991Z","iopub.status.idle":"2024-05-10T10:59:35.235604Z","shell.execute_reply.started":"2024-05-10T10:59:35.230966Z","shell.execute_reply":"2024-05-10T10:59:35.234736Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/efficientnet_trained_model.keras')","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:59:35.236724Z","iopub.execute_input":"2024-05-10T10:59:35.236978Z","iopub.status.idle":"2024-05-10T10:59:36.268221Z","shell.execute_reply.started":"2024-05-10T10:59:35.236957Z","shell.execute_reply":"2024-05-10T10:59:36.267145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## MobileNet","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import MobileNet\nconvolutional_base_model_2 = MobileNet(weights='imagenet', include_top=False, input_shape=(256, 256, 3))\nconvolutional_base_model_2.summary()","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:59:36.269538Z","iopub.execute_input":"2024-05-10T10:59:36.269837Z","iopub.status.idle":"2024-05-10T10:59:38.013293Z","shell.execute_reply.started":"2024-05-10T10:59:36.269812Z","shell.execute_reply":"2024-05-10T10:59:38.012372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_of_classes = 10\n\nmodel = models.Sequential()\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(layers.UpSampling2D((2,2)))\nmodel.add(convolutional_base_model_2)\nmodel.add(layers.Flatten())\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(128,activation='relu'))\nmodel.add(layers.Dropout(0,5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(64, activation='relu'))\nmodel.add(layers.Dropout(0,5))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Dense(no_of_classes, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:59:38.014653Z","iopub.execute_input":"2024-05-10T10:59:38.015007Z","iopub.status.idle":"2024-05-10T10:59:38.036109Z","shell.execute_reply.started":"2024-05-10T10:59:38.014972Z","shell.execute_reply":"2024-05-10T10:59:38.035053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=optimizers.RMSprop(learning_rate=2e-5), \n              loss='sparse_categorical_crossentropy', \n              \n              metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:59:38.037313Z","iopub.execute_input":"2024-05-10T10:59:38.037630Z","iopub.status.idle":"2024-05-10T10:59:38.053157Z","shell.execute_reply.started":"2024-05-10T10:59:38.037606Z","shell.execute_reply":"2024-05-10T10:59:38.052266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_train , y_train , validation_split=0.1, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T10:59:38.054269Z","iopub.execute_input":"2024-05-10T10:59:38.054524Z","iopub.status.idle":"2024-05-10T11:15:18.817654Z","shell.execute_reply.started":"2024-05-10T10:59:38.054502Z","shell.execute_reply":"2024-05-10T11:15:18.816808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss , accuracy = model.evaluate(x_test , y_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T11:15:40.324840Z","iopub.execute_input":"2024-05-10T11:15:40.325371Z","iopub.status.idle":"2024-05-10T11:15:46.609645Z","shell.execute_reply.started":"2024-05-10T11:15:40.325337Z","shell.execute_reply":"2024-05-10T11:15:46.608672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('Accuracy: ',accuracy)","metadata":{"execution":{"iopub.status.busy":"2024-05-10T11:50:35.074236Z","iopub.execute_input":"2024-05-10T11:50:35.074622Z","iopub.status.idle":"2024-05-10T11:50:35.079956Z","shell.execute_reply.started":"2024-05-10T11:50:35.074595Z","shell.execute_reply":"2024-05-10T11:50:35.079054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/mobilenet_trained_model.keras')","metadata":{"execution":{"iopub.status.busy":"2024-05-10T11:29:40.552870Z","iopub.execute_input":"2024-05-10T11:29:40.553241Z","iopub.status.idle":"2024-05-10T11:29:41.134052Z","shell.execute_reply.started":"2024-05-10T11:29:40.553212Z","shell.execute_reply":"2024-05-10T11:29:41.133241Z"},"trusted":true},"execution_count":null,"outputs":[]}]}