{"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"},{"sourceId":235,"sourceType":"modelInstanceVersion","modelInstanceId":163}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"#extracting the zip dataset","metadata":{"id":"vL5OoeI57Gix"}},{"cell_type":"code","source":"from zipfile import ZipFile\ndataset = \"/kaggle/input/dogs-vs-cats/train.zip\"\n\nwith ZipFile(dataset, 'r') as zip:\n  zip.extractall()\n  print(\"Extracted train file!!!!!\")","metadata":{"id":"qR3KVyF17uuz","outputId":"266bd1f7-16ed-4c96-dca6-a15c6bb5a3e1","execution":{"iopub.status.busy":"2024-06-24T18:32:03.128591Z","iopub.execute_input":"2024-06-24T18:32:03.129602Z","iopub.status.idle":"2024-06-24T18:32:15.207158Z","shell.execute_reply.started":"2024-06-24T18:32:03.12956Z","shell.execute_reply":"2024-06-24T18:32:15.206165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n#counting the number of files in train folder\npath,dirs,files=next(os.walk(\"/kaggle/working/train\"))\nfile_count=len(files)\nprint('Number of images: ', file_count)","metadata":{"id":"az9uq4rO8Ajq","outputId":"346a108f-912f-480c-887b-3df4d0360e77","execution":{"iopub.status.busy":"2024-06-24T18:32:15.20905Z","iopub.execute_input":"2024-06-24T18:32:15.209392Z","iopub.status.idle":"2024-06-24T18:32:15.245788Z","shell.execute_reply.started":"2024-06-24T18:32:15.209349Z","shell.execute_reply":"2024-06-24T18:32:15.244953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"printing the name of images","metadata":{"id":"b0P9mw_29fTp"}},{"cell_type":"code","source":"file_names = os.listdir(\"/kaggle/working/train\")\nprint(file_names)","metadata":{"id":"halUC3Jl9QHC","outputId":"48d18fcc-7235-4551-be5f-da95a76a1592","execution":{"iopub.status.busy":"2024-06-24T18:32:15.246722Z","iopub.execute_input":"2024-06-24T18:32:15.246962Z","iopub.status.idle":"2024-06-24T18:32:15.276321Z","shell.execute_reply.started":"2024-06-24T18:32:15.246941Z","shell.execute_reply":"2024-06-24T18:32:15.275492Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"importing dependencies","metadata":{"id":"eanmmfXS-Gk5"}},{"cell_type":"code","source":"import numpy as np\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\nimport cv2\n#from google.colab.patches import cv2_imshow","metadata":{"id":"FhKF6p2vfpaw","execution":{"iopub.status.busy":"2024-06-24T18:32:15.278234Z","iopub.execute_input":"2024-06-24T18:32:15.278561Z","iopub.status.idle":"2024-06-24T18:32:16.169423Z","shell.execute_reply.started":"2024-06-24T18:32:15.278537Z","shell.execute_reply":"2024-06-24T18:32:16.168576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"displaying the images of dogs and cats","metadata":{"id":"wJQLdNbo_Okp"}},{"cell_type":"code","source":"#show dog images\nimg= mpimg.imread(\"/kaggle/working/train/dog.2403.jpg\")\nimgplot = plt.imshow(img)\nplt.show()","metadata":{"id":"TOjKa94q_D2s","outputId":"13b2d0ba-0001-475d-efd6-6e00569ff924","execution":{"iopub.status.busy":"2024-06-24T18:32:16.170667Z","iopub.execute_input":"2024-06-24T18:32:16.170973Z","iopub.status.idle":"2024-06-24T18:32:16.489462Z","shell.execute_reply.started":"2024-06-24T18:32:16.170948Z","shell.execute_reply":"2024-06-24T18:32:16.488438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#show cat images\nimg= mpimg.imread(\"/kaggle/working/train/cat.9013.jpg\")\nimgplot = plt.imshow(img)\nplt.show()","metadata":{"id":"KAtuQmXi_qGi","outputId":"37b1b57d-f042-4de8-debe-18390f918d9a","execution":{"iopub.status.busy":"2024-06-24T18:32:16.490495Z","iopub.execute_input":"2024-06-24T18:32:16.490779Z","iopub.status.idle":"2024-06-24T18:32:16.746526Z","shell.execute_reply.started":"2024-06-24T18:32:16.490755Z","shell.execute_reply":"2024-06-24T18:32:16.745632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_names = os.listdir(\"/kaggle/working/train\")\ndog_count = 0\ncat_count = 0\nfor img_file in file_names:\n  name = img_file[0:3]\n  if name == \"dog\":\n    dog_count +=1\n  elif name == \"cat\":\n    cat_count +=1\n\nprint(\"Number of dogs: \", dog_count)\nprint(\"Number of cats: \", cat_count)","metadata":{"id":"QfRob7oNFb88","outputId":"03e47062-cc26-42d2-8d7d-d6259dd2c91b","execution":{"iopub.status.busy":"2024-06-24T18:32:16.747807Z","iopub.execute_input":"2024-06-24T18:32:16.748126Z","iopub.status.idle":"2024-06-24T18:32:16.781887Z","shell.execute_reply.started":"2024-06-24T18:32:16.748099Z","shell.execute_reply":"2024-06-24T18:32:16.780997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#resizing images","metadata":{"id":"qGHqADoAIHp1"}},{"cell_type":"code","source":"#creating directory for resized images\nos.mkdir(\"/kaggle/working/resized_images\")","metadata":{"id":"kMG-egeFFb2N","outputId":"1fb4f2e7-40d5-4df6-de69-b9e2ab0d9af6","execution":{"iopub.status.busy":"2024-06-24T18:32:16.782949Z","iopub.execute_input":"2024-06-24T18:32:16.783233Z","iopub.status.idle":"2024-06-24T18:32:16.859303Z","shell.execute_reply.started":"2024-06-24T18:32:16.783209Z","shell.execute_reply":"2024-06-24T18:32:16.856942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"original_folder = \"/kaggle/working/train/\"\nresized_folder = \"/kaggle/working/resized_images/\"\n\nfor i in range(10000):\n    filename = os.listdir(original_folder)[i]\n    img_path = original_folder+filename\n    img = Image.open(img_path)\n    img = img.resize((224,224))\n    img = img.convert('RGB')\n    img.save(resized_folder+filename)\n","metadata":{"id":"JHdIaulUFbvE","execution":{"iopub.status.busy":"2024-06-24T18:33:34.647711Z","iopub.execute_input":"2024-06-24T18:33:34.648414Z","iopub.status.idle":"2024-06-24T18:36:25.710873Z","shell.execute_reply.started":"2024-06-24T18:33:34.648382Z","shell.execute_reply":"2024-06-24T18:36:25.709873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#show dog images\nimg= mpimg.imread(\"/kaggle/working/resized_images/dog.2403.jpg\")\nimgplot = plt.imshow(img)\nplt.show()","metadata":{"id":"BYnjEI-lA8Q7","outputId":"f3f0f317-14e8-452e-c249-7ea2308d704d","execution":{"iopub.status.busy":"2024-06-24T18:32:16.861689Z","iopub.status.idle":"2024-06-24T18:32:16.861997Z","shell.execute_reply.started":"2024-06-24T18:32:16.861844Z","shell.execute_reply":"2024-06-24T18:32:16.861856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#show dog images\nimg= mpimg.imread(\"/kaggle/working/resized_images/cat.9013.jpg\")\nimgplot = plt.imshow(img)\nplt.show()","metadata":{"id":"CXWUL-jLfVfZ","outputId":"d6484f58-8478-46b8-ab7a-5e25eeed39d2","execution":{"iopub.status.busy":"2024-06-24T18:32:16.863042Z","iopub.status.idle":"2024-06-24T18:32:16.863347Z","shell.execute_reply.started":"2024-06-24T18:32:16.863194Z","shell.execute_reply":"2024-06-24T18:32:16.863206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Creating label for resized images of dogs and cats\ncat == 0  \ndog == 1","metadata":{"id":"rgwuH-ViQaAP"}},{"cell_type":"code","source":"#creating for loop to assign labels\n\nfilenames = os.listdir(\"/kaggle/working/resized_images/\")\n\nlabels = []\nfor i in range (10000):\n  file_name = filenames[i]\n  label = file_name[0:3]\n  if label == \"dog\":\n    labels.append(1)\n  elif label == \"cat\":\n    labels.append(0)","metadata":{"id":"44fFoGzzQOiF","execution":{"iopub.status.busy":"2024-06-24T18:36:33.770035Z","iopub.execute_input":"2024-06-24T18:36:33.770965Z","iopub.status.idle":"2024-06-24T18:36:33.787434Z","shell.execute_reply.started":"2024-06-24T18:36:33.77093Z","shell.execute_reply":"2024-06-24T18:36:33.786713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(filenames[0:5])\nprint(labels[0:5])\nprint(len(filenames))\nprint(len(labels))","metadata":{"id":"YA0WupDPQOdV","outputId":"5b4fdc3f-11db-478a-a055-e171f1a9a7aa","execution":{"iopub.status.busy":"2024-06-24T18:36:36.444904Z","iopub.execute_input":"2024-06-24T18:36:36.445799Z","iopub.status.idle":"2024-06-24T18:36:36.450992Z","shell.execute_reply.started":"2024-06-24T18:36:36.445768Z","shell.execute_reply":"2024-06-24T18:36:36.449865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#counting the images of cats and dogs out of 250000 images\nvalues, counts = np.unique(labels, return_counts=True)\nprint(values)\nprint(counts)","metadata":{"id":"yd_1Erd7QOad","outputId":"75b5893d-ee17-45a7-f2f4-58242946dc07","execution":{"iopub.status.busy":"2024-06-24T18:36:38.669623Z","iopub.execute_input":"2024-06-24T18:36:38.670531Z","iopub.status.idle":"2024-06-24T18:36:38.679074Z","shell.execute_reply.started":"2024-06-24T18:36:38.670495Z","shell.execute_reply":"2024-06-24T18:36:38.678019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Converting all the images to numpy arrays","metadata":{"id":"vh-L-IjOrUzy"}},{"cell_type":"code","source":"import cv2\nimport glob","metadata":{"id":"7ZwgOWTyrDna","execution":{"iopub.status.busy":"2024-06-24T18:36:41.461169Z","iopub.execute_input":"2024-06-24T18:36:41.462023Z","iopub.status.idle":"2024-06-24T18:36:41.466222Z","shell.execute_reply.started":"2024-06-24T18:36:41.461989Z","shell.execute_reply":"2024-06-24T18:36:41.465156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_directory = \"/kaggle/working/resized_images/\"\nimage_extension = [ 'jpg', 'png', 'jpeg']\n\nfiles = []\n\n[files.extend(glob.glob(image_directory + '*.' + e)) for e in image_extension]\n\ndog_cat_images = np.asarray([cv2.imread(file) for file in files])","metadata":{"id":"Rgv7UV3rr6GZ","execution":{"iopub.status.busy":"2024-06-24T18:36:43.367593Z","iopub.execute_input":"2024-06-24T18:36:43.367935Z","iopub.status.idle":"2024-06-24T18:36:48.235529Z","shell.execute_reply.started":"2024-06-24T18:36:43.36791Z","shell.execute_reply":"2024-06-24T18:36:48.234608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dog_cat_images)","metadata":{"id":"57w71q46r6DR","outputId":"cce9bc1c-1095-4556-d568-56585690bc6e","execution":{"iopub.status.busy":"2024-06-24T18:36:52.805597Z","iopub.execute_input":"2024-06-24T18:36:52.806264Z","iopub.status.idle":"2024-06-24T18:36:52.814409Z","shell.execute_reply.started":"2024-06-24T18:36:52.806231Z","shell.execute_reply":"2024-06-24T18:36:52.813494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dog_cat_images.shape)","metadata":{"id":"WteKbDyEr57j","outputId":"01a318d4-87ff-4fc0-dfcb-45783767dd50","execution":{"iopub.status.busy":"2024-06-24T18:36:54.021296Z","iopub.execute_input":"2024-06-24T18:36:54.021935Z","iopub.status.idle":"2024-06-24T18:36:54.026685Z","shell.execute_reply.started":"2024-06-24T18:36:54.021902Z","shell.execute_reply":"2024-06-24T18:36:54.025758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X= dog_cat_images\nY=np.asarray(labels)","metadata":{"id":"jdtlxVyK0DOy","execution":{"iopub.status.busy":"2024-06-24T18:36:55.117683Z","iopub.execute_input":"2024-06-24T18:36:55.118038Z","iopub.status.idle":"2024-06-24T18:36:55.123471Z","shell.execute_reply.started":"2024-06-24T18:36:55.11801Z","shell.execute_reply":"2024-06-24T18:36:55.122534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Train test Split","metadata":{"id":"0Cxd7nHg0ipq"}},{"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":{"id":"CAuXf45E0h3J","execution":{"iopub.status.busy":"2024-06-24T18:36:57.661683Z","iopub.execute_input":"2024-06-24T18:36:57.662623Z","iopub.status.idle":"2024-06-24T18:36:58.070794Z","shell.execute_reply.started":"2024-06-24T18:36:57.662588Z","shell.execute_reply":"2024-06-24T18:36:58.069706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X.shape)\nprint(X_train.shape)\nprint(X_test.shape)\nprint(Y_train.shape)\nprint(Y_test.shape )","metadata":{"id":"rAZwiuHd0Z0C","outputId":"18c246cf-40b1-437c-886c-274c2231365b","execution":{"iopub.status.busy":"2024-06-24T18:36:58.973989Z","iopub.execute_input":"2024-06-24T18:36:58.975204Z","iopub.status.idle":"2024-06-24T18:36:58.980897Z","shell.execute_reply.started":"2024-06-24T18:36:58.975161Z","shell.execute_reply":"2024-06-24T18:36:58.979864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#scaling the data\nX_train_scaled = X_train/255\nX_test_scaled = X_test/255","metadata":{"id":"KRtF4t1XITCV","execution":{"iopub.status.busy":"2024-06-24T18:37:00.750027Z","iopub.execute_input":"2024-06-24T18:37:00.750702Z","iopub.status.idle":"2024-06-24T18:37:04.357959Z","shell.execute_reply.started":"2024-06-24T18:37:00.750669Z","shell.execute_reply":"2024-06-24T18:37:04.356843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train_scaled)","metadata":{"id":"FrkjO3cEIx_m","execution":{"iopub.status.busy":"2024-06-24T18:37:18.832043Z","iopub.execute_input":"2024-06-24T18:37:18.832931Z","iopub.status.idle":"2024-06-24T18:37:18.845661Z","shell.execute_reply.started":"2024-06-24T18:37:18.832896Z","shell.execute_reply":"2024-06-24T18:37:18.844723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Building the Neural Network**","metadata":{"id":"u201Ov67JTmr"}},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_hub as hub\n!pip install --upgrade tensorflow-hub","metadata":{"id":"VWx5oTKqIx9W","execution":{"iopub.status.busy":"2024-06-24T18:48:31.138419Z","iopub.execute_input":"2024-06-24T18:48:31.138804Z","iopub.status.idle":"2024-06-24T18:48:47.275506Z","shell.execute_reply.started":"2024-06-24T18:48:31.138776Z","shell.execute_reply":"2024-06-24T18:48:47.274517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobilenet_model = 'https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4'\n\npretrained_model = hub.KerasLayer(mobilenet_model, input_shape=(224,224,3), trainable=False)\n\nnum_of_classes = 2\n\nmodel = tf.keras.Sequential([\n    \n    pretrained_model,\n    tf.keras.layers.Dense(num_of_classes)\n\n])\n\nmodel.summary()","metadata":{"id":"_1KARVM2PI-n","execution":{"iopub.status.busy":"2024-06-24T18:48:57.210412Z","iopub.execute_input":"2024-06-24T18:48:57.211161Z","iopub.status.idle":"2024-06-24T18:48:58.570474Z","shell.execute_reply.started":"2024-06-24T18:48:57.211122Z","shell.execute_reply":"2024-06-24T18:48:58.569047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer='adam',\n    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n    metrics=['acc']\n)","metadata":{"id":"L_PJyiIDPI6h","execution":{"iopub.status.busy":"2024-06-24T18:50:03.199792Z","iopub.execute_input":"2024-06-24T18:50:03.200483Z","iopub.status.idle":"2024-06-24T18:50:03.24015Z","shell.execute_reply.started":"2024-06-24T18:50:03.200434Z","shell.execute_reply":"2024-06-24T18:50:03.238523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train the model\nmodel.fit(X_train_scaled, Y_train, epochs=5)","metadata":{"id":"GzJTAGF3Rz3D","execution":{"iopub.status.busy":"2024-06-24T18:32:16.893268Z","iopub.status.idle":"2024-06-24T18:32:16.893635Z","shell.execute_reply.started":"2024-06-24T18:32:16.893437Z","shell.execute_reply":"2024-06-24T18:32:16.89347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --upgrade tensorflow tensorflow-hub\n\nimport tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow.keras import layers\n\n# Print TensorFlow and TensorFlow Hub versions for debugging\nprint(\"TensorFlow version:\", tf.__version__)\nprint(\"TensorFlow Hub version:\", hub.__version__)\n\n# URL of the pre-trained MobileNet V2 model from TensorFlow Hub\nmobilenet_model = \"https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4\"\n\n# Load the pre-trained model using KerasLayer from TensorFlow Hub\npretrained_model = hub.KerasLayer(mobilenet_model, input_shape=(224, 224, 3), trainable=False)\n\nnum_of_classes = 2\n\n# Create the sequential model\nmodel = tf.keras.Sequential([\n    pretrained_model,\n    layers.Dense(num_of_classes, activation='softmax')  # Add activation function\n])\n\n# Compile the model (optional but recommended)\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-24T19:19:39.454646Z","iopub.execute_input":"2024-06-24T19:19:39.455369Z","iopub.status.idle":"2024-06-24T19:20:39.413409Z","shell.execute_reply.started":"2024-06-24T19:19:39.455338Z","shell.execute_reply":"2024-06-24T19:20:39.412106Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip uninstall -y tensorflow tensorflow-hub\n!pip install tensorflow tensorflow-hub","metadata":{"execution":{"iopub.status.busy":"2024-06-24T19:27:24.448753Z","iopub.execute_input":"2024-06-24T19:27:24.449425Z","iopub.status.idle":"2024-06-24T19:27:59.671767Z","shell.execute_reply.started":"2024-06-24T19:27:24.449391Z","shell.execute_reply":"2024-06-24T19:27:59.670603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow.keras import layers\n\n# Print TensorFlow and TensorFlow Hub versions for debugging\nprint(\"TensorFlow version:\", tf.__version__)\nprint(\"TensorFlow Hub version:\", hub.__version__)\n\n# URL of the pre-trained MobileNet V2 model from TensorFlow Hub\nmobilenet_model_url = \"https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4\"\n\n# Load the pre-trained model using KerasLayer from TensorFlow Hub\npretrained_model = hub.KerasLayer(mobilenet_model_url, input_shape=(224, 224, 3), trainable=False)\n\n# Check the type of pretrained_model to ensure it is correctly loaded\nprint(\"Type of pretrained_model:\", type(pretrained_model))\n\n# Define the number of classes\nnum_of_classes = 2\n\n# Create the functional model\ninputs = tf.keras.Input(shape=(224, 224, 3))\nx = pretrained_model(inputs)\noutputs = layers.Dense(num_of_classes, activation='softmax')(x)\nmodel = tf.keras.Model(inputs, outputs)\n\n# Compile the model (optional but recommended)\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-24T19:31:57.681419Z","iopub.execute_input":"2024-06-24T19:31:57.682184Z","iopub.status.idle":"2024-06-24T19:31:59.009112Z","shell.execute_reply.started":"2024-06-24T19:31:57.682152Z","shell.execute_reply":"2024-06-24T19:31:59.007943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow.keras import layers\n\n# Print TensorFlow and TensorFlow Hub versions for debugging\nprint(\"TensorFlow version:\", tf.__version__)\nprint(\"TensorFlow Hub version:\", hub.__version__)\n\n# URL of the pre-trained MobileNet V2 model from TensorFlow Hub\nmobilenet_model_url = \"https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4\"\n\n# Load the pre-trained model using KerasLayer from TensorFlow Hub\npretrained_model = hub.KerasLayer(mobilenet_model_url, input_shape=(224, 224, 3), trainable=False)\n\n# Check the type of pretrained_model to ensure it is correctly loaded\nprint(\"Type of pretrained_model:\", type(pretrained_model))\n\n# Define the number of classes\nnum_of_classes = 2\n\n# Custom layer to cast inputs to float32\nclass CastLayer(layers.Layer):\n    def call(self, inputs):\n        return tf.cast(inputs, dtype=tf.float32)\n\n# Create the functional model\ninputs = tf.keras.Input(shape=(224, 224, 3))\nx = CastLayer()(inputs)\nx = pretrained_model(x)\noutputs = layers.Dense(num_of_classes, activation='softmax')(x)\nmodel = tf.keras.Model(inputs, outputs)\n\n# Compile the model (optional but recommended)\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-24T19:35:02.558425Z","iopub.execute_input":"2024-06-24T19:35:02.559054Z","iopub.status.idle":"2024-06-24T19:35:04.423776Z","shell.execute_reply.started":"2024-06-24T19:35:02.559021Z","shell.execute_reply":"2024-06-24T19:35:04.422519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}