{"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":"# 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\nimport zipfile\n\nzip_file_path1 = \"/kaggle/input/dogs-vs-cats/test1.zip\"  # Path to your test1.zip file\nzip_file_path2 = \"/kaggle/input/dogs-vs-cats/train.zip\"  # Path to your train.zip file\nextract_dir1 = \"/kaggle/working/test1\"  # Directory where you want to extract the test1 files\nextract_dir2 = \"/kaggle/working/train\"  # Directory where you want to extract the train files\n\n# Create the directories if they don't exist\nos.makedirs(extract_dir1, exist_ok=True)\nos.makedirs(extract_dir2, exist_ok=True)\n\n# Extract test1.zip\nwith zipfile.ZipFile(zip_file_path1, 'r') as zip_ref:\n    zip_ref.extractall(extract_dir1)\n\n# Extract train.zip\nwith zipfile.ZipFile(zip_file_path2, 'r') as zip_ref:\n    zip_ref.extractall(extract_dir2)\n\nprint(\"Extraction complete.\")\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-05-12T21:19:26.864511Z","iopub.execute_input":"2023-05-12T21:19:26.864890Z","iopub.status.idle":"2023-05-12T21:19:41.774174Z","shell.execute_reply.started":"2023-05-12T21:19:26.864858Z","shell.execute_reply":"2023-05-12T21:19:41.773127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install tensorflow tensorflow_hub","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom tensorflow.keras.models import Sequential\n\n\ntrain_data_dir = '/kaggle/working/train'\nvalidation_data_dir = '/kaggle/working/test1'\nimg_width, img_height = 224, 224  # Input image dimensions expected by the ResNet-v2 model\n\n\ntrain_data_gen = ImageDataGenerator(\n    rescale=1.0/255.0,  # Normalize pixel values to [0, 1]\n    shear_range=0.2,\n    zoom_range=0.2,\n    horizontal_flip=True\n)\n\nvalidation_data_gen = ImageDataGenerator(rescale=1.0/255.0)\n\n\ntrain_generator = train_data_gen.flow_from_directory(\n    train_data_dir,\n    target_size=(img_width, img_height),\n    batch_size=32,\n    class_mode='binary'\n)\n\nvalidation_generator = validation_data_gen.flow_from_directory(\n    validation_data_dir,\n    target_size=(img_width, img_height),\n    batch_size=32,\n    class_mode='binary'\n)\n\n\nmodel = Sequential()\nmodel.add(hub.KerasLayer('https://tfhub.dev/google/imagenet/resnet_v2_101/feature_vector/4', trainable=False))\n\n\nmodel.add(Dense(256, activation='relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(1, activation='sigmoid'))\n\n\nmodel.compile(\n    loss='binary_crossentropy',\n    optimizer='adam',\n    metrics=['accuracy']\n)\n\n\nnum_epochs = 1\nhistory = model.fit(\n    train_generator,\n    steps_per_epoch=len(train_generator),\n    epochs=num_epochs,\n    validation_data=validation_generator,\n    validation_steps=len(validation_generator)\n)\n\n\ntest_loss, test_acc = model.evaluate(validation_generator, steps=len(validation_generator))\nprint(f'Test loss: {test_loss:.4f}')\nprint(f'Test accuracy: {test_acc:.4f}')\n","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:19:48.343535Z","iopub.execute_input":"2023-05-12T21:19:48.343901Z","iopub.status.idle":"2023-05-12T21:29:16.956105Z","shell.execute_reply.started":"2023-05-12T21:19:48.343859Z","shell.execute_reply":"2023-05-12T21:29:16.954331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport requests\nfrom io import BytesIO\n\n# Function to upload and classify image\ndef classify_image(image_url):\n    # Download the image from the provided URL\n    response = requests.get(image_url)\n    img = Image.open(BytesIO(response.content))\n    \n    # Preprocess the image\n    img = img.resize((img_width, img_height))\n    img = np.array(img) / 255.0\n    img = np.expand_dims(img, axis=0)\n    \n    # Perform prediction\n    prediction = model.predict(img)\n    \n    # Classify the image\n    if prediction[0] >= 0.5:\n        return 'Dog'\n    else:\n        return 'Cat'\n\n# Example usage\nimage_url = 'https://images.pexels.com/photos/755834/pexels-photo-755834.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2'  # Replace with your image URL\nresult = classify_image(image_url)\nprint(f'Image classified as: {result}')","metadata":{"execution":{"iopub.status.busy":"2023-05-12T21:40:50.339673Z","iopub.execute_input":"2023-05-12T21:40:50.340059Z","iopub.status.idle":"2023-05-12T21:40:50.607584Z","shell.execute_reply.started":"2023-05-12T21:40:50.340027Z","shell.execute_reply":"2023-05-12T21:40:50.605675Z"},"trusted":true},"execution_count":null,"outputs":[]}]}