{"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":"import tensorflow as tf\nfrom tensorflow.keras import models, layers\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport zipfile\nfrom tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:01.359707Z","iopub.execute_input":"2023-06-23T17:15:01.360584Z","iopub.status.idle":"2023-06-23T17:15:01.366360Z","shell.execute_reply.started":"2023-06-23T17:15:01.360551Z","shell.execute_reply":"2023-06-23T17:15:01.365447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:01.368762Z","iopub.execute_input":"2023-06-23T17:15:01.369823Z","iopub.status.idle":"2023-06-23T17:15:01.377800Z","shell.execute_reply.started":"2023-06-23T17:15:01.369773Z","shell.execute_reply":"2023-06-23T17:15:01.376338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.getcwd() # find the current working directory","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:01.379467Z","iopub.execute_input":"2023-06-23T17:15:01.380137Z","iopub.status.idle":"2023-06-23T17:15:01.388691Z","shell.execute_reply.started":"2023-06-23T17:15:01.380048Z","shell.execute_reply":"2023-06-23T17:15:01.387556Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with zipfile.ZipFile(\"../input/dogs-vs-cats/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"../input/dogs-vs-cats/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nos.listdir(\".\")","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:01.390560Z","iopub.execute_input":"2023-06-23T17:15:01.391007Z","iopub.status.idle":"2023-06-23T17:15:16.697112Z","shell.execute_reply.started":"2023-06-23T17:15:01.390977Z","shell.execute_reply":"2023-06-23T17:15:16.696117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('cats')\nos.mkdir('dogs')","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:16.700406Z","iopub.execute_input":"2023-06-23T17:15:16.700768Z","iopub.status.idle":"2023-06-23T17:15:16.706184Z","shell.execute_reply.started":"2023-06-23T17:15:16.700742Z","shell.execute_reply":"2023-06-23T17:15:16.705204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\npath = \"train\"\n\ndirs = [each for each in os.listdir(path)]\n\nfor _ in tqdm(dirs):\n    folder = _.split(\".\")[0]\n    if folder=='cat':\n        shutil.move(path+\"/\"+_, \"cats\")\n    else:\n        shutil.move(path+\"/\"+_, \"dogs\")","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:16.707845Z","iopub.execute_input":"2023-06-23T17:15:16.708196Z","iopub.status.idle":"2023-06-23T17:15:18.085020Z","shell.execute_reply.started":"2023-06-23T17:15:16.708163Z","shell.execute_reply":"2023-06-23T17:15:18.084058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('train')","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:18.086408Z","iopub.execute_input":"2023-06-23T17:15:18.086837Z","iopub.status.idle":"2023-06-23T17:15:18.092044Z","shell.execute_reply.started":"2023-06-23T17:15:18.086805Z","shell.execute_reply":"2023-06-23T17:15:18.091169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('train')\nshutil.move('cats', 'train')\nshutil.move('dogs','train')","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:18.093532Z","iopub.execute_input":"2023-06-23T17:15:18.094118Z","iopub.status.idle":"2023-06-23T17:15:18.103824Z","shell.execute_reply.started":"2023-06-23T17:15:18.094086Z","shell.execute_reply":"2023-06-23T17:15:18.102761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 256\nbatch_size = 32\nchannels = 3\nepochs = 25","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:18.105500Z","iopub.execute_input":"2023-06-23T17:15:18.105958Z","iopub.status.idle":"2023-06-23T17:15:18.111156Z","shell.execute_reply.started":"2023-06-23T17:15:18.105918Z","shell.execute_reply":"2023-06-23T17:15:18.110217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = tf.keras.preprocessing.image_dataset_from_directory(\n    \"train\",\n    shuffle=True,\n    image_size=(image_size,image_size),\n    batch_size=batch_size\n)\nclass_names = dataset.class_names\ndataset = dataset.take(int(0.4*len(dataset)))\nlen(dataset)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:18.112526Z","iopub.execute_input":"2023-06-23T17:15:18.113443Z","iopub.status.idle":"2023-06-23T17:15:22.590803Z","shell.execute_reply.started":"2023-06-23T17:15:18.113412Z","shell.execute_reply":"2023-06-23T17:15:22.589907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class_names = dataset.class_names","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:22.592282Z","iopub.execute_input":"2023-06-23T17:15:22.592630Z","iopub.status.idle":"2023-06-23T17:15:22.597070Z","shell.execute_reply.started":"2023-06-23T17:15:22.592600Z","shell.execute_reply":"2023-06-23T17:15:22.595826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor image_batch, label_batch in dataset.take(1):\n    for i in range(12):\n        ax = plt.subplot(3,4,i+1)\n        plt.imshow(image_batch[i].numpy().astype(\"uint8\"))\n        plt.title(class_names[label_batch[i]])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:22.598680Z","iopub.execute_input":"2023-06-23T17:15:22.599365Z","iopub.status.idle":"2023-06-23T17:15:25.030687Z","shell.execute_reply.started":"2023-06-23T17:15:22.599335Z","shell.execute_reply":"2023-06-23T17:15:25.027426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = 0.8\ntrain_ds = dataset.take(int(train_size*len(dataset)))\n\ncpy = dataset.skip(len(train_ds))\n\nval_size = 0.5 # 50% for remaining 20% = 10%\nval_ds = cpy.take(int(val_size*len(cpy)))\n\ntest_ds = cpy.skip(len(val_ds))\n\nprint([len(train_ds), len(val_ds), len(test_ds)])","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:25.035472Z","iopub.execute_input":"2023-06-23T17:15:25.036071Z","iopub.status.idle":"2023-06-23T17:15:25.051087Z","shell.execute_reply.started":"2023-06-23T17:15:25.036038Z","shell.execute_reply":"2023-06-23T17:15:25.049824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size = tf.data.AUTOTUNE)\nval_ds = val_ds.cache().shuffle(1000).prefetch(buffer_size = tf.data.AUTOTUNE)\ntest_ds = test_ds.cache().shuffle(1000).prefetch(buffer_size = tf.data.AUTOTUNE)\n# this block of code optimises the program by caching.","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:25.052702Z","iopub.execute_input":"2023-06-23T17:15:25.053579Z","iopub.status.idle":"2023-06-23T17:15:25.073879Z","shell.execute_reply.started":"2023-06-23T17:15:25.053546Z","shell.execute_reply":"2023-06-23T17:15:25.072987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resize_and_rescale = tf.keras.Sequential([\n    layers.experimental.preprocessing.Resizing(image_size, image_size),\n    layers.experimental.preprocessing.Rescaling(1.0/255)\n])","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:25.075247Z","iopub.execute_input":"2023-06-23T17:15:25.075576Z","iopub.status.idle":"2023-06-23T17:15:25.093248Z","shell.execute_reply.started":"2023-06-23T17:15:25.075546Z","shell.execute_reply":"2023-06-23T17:15:25.092142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_augmentation = tf.keras.Sequential([\n    layers.experimental.preprocessing.RandomFlip(\"horizontal_and_vertical\"),\n    layers.experimental.preprocessing.RandomRotation(0.2)\n])","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:25.094803Z","iopub.execute_input":"2023-06-23T17:15:25.095141Z","iopub.status.idle":"2023-06-23T17:15:25.105755Z","shell.execute_reply.started":"2023-06-23T17:15:25.095111Z","shell.execute_reply":"2023-06-23T17:15:25.104768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape = (batch_size, image_size, image_size, channels)\nn_classes = 3\nmodel = models.Sequential([\n    resize_and_rescale,\n    data_augmentation,\n    layers.Conv2D(32, (3,3), activation = 'relu', input_shape = input_shape),\n    layers.MaxPooling2D((2,2)),\n    layers.Conv2D(64, kernel_size = (3,3), activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    layers.Conv2D(64, kernel_size = (3,3), activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    layers.Conv2D(128, kernel_size = (3,3), activation='relu'),\n    layers.MaxPooling2D((2,2)),\n    layers.MaxPooling2D((2,2)),\n    layers.Flatten(),\n    layers.Dense(256, activation='relu'),\n    layers.Dense(n_classes, activation='softmax')\n])\n\nmodel.build(input_shape=input_shape)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:25.107530Z","iopub.execute_input":"2023-06-23T17:15:25.107884Z","iopub.status.idle":"2023-06-23T17:15:25.504169Z","shell.execute_reply.started":"2023-06-23T17:15:25.107853Z","shell.execute_reply":"2023-06-23T17:15:25.503205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer = 'adam',\n    loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),\n    metrics=['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:25.505975Z","iopub.execute_input":"2023-06-23T17:15:25.506684Z","iopub.status.idle":"2023-06-23T17:15:25.528008Z","shell.execute_reply.started":"2023-06-23T17:15:25.506648Z","shell.execute_reply":"2023-06-23T17:15:25.527063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_ds,\n    epochs=epochs,\n    batch_size=batch_size,\n    verbose=1,\n    validation_data=val_ds\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:15:25.529363Z","iopub.execute_input":"2023-06-23T17:15:25.529961Z","iopub.status.idle":"2023-06-23T17:20:09.806954Z","shell.execute_reply.started":"2023-06-23T17:15:25.529929Z","shell.execute_reply":"2023-06-23T17:20:09.805935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = model.evaluate(test_ds)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:20:09.808374Z","iopub.execute_input":"2023-06-23T17:20:09.808755Z","iopub.status.idle":"2023-06-23T17:20:23.799259Z","shell.execute_reply.started":"2023-06-23T17:20:09.808722Z","shell.execute_reply":"2023-06-23T17:20:23.798419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nplt.figure(figsize=(8,8))\nplt.subplot(1,2,1)\nplt.plot(range(epochs), acc, label='Training Accuracy')\nplt.plot(range(epochs), val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:20:23.800526Z","iopub.execute_input":"2023-06-23T17:20:23.800895Z","iopub.status.idle":"2023-06-23T17:20:24.129788Z","shell.execute_reply.started":"2023-06-23T17:20:23.800862Z","shell.execute_reply":"2023-06-23T17:20:24.128882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict(model, img):\n    img_array = tf.keras.preprocessing.image.img_to_array(img)\n    img_array = tf.expand_dims(img_array, 0) # create a batch\n\n    predictions = model.predict(img_array)\n\n    predicted_class = class_names[np.argmax(predictions[0])]\n    confidence = round(100*(np.max(predictions[0])), 2)\n    return predicted_class, confidence","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:20:24.131113Z","iopub.execute_input":"2023-06-23T17:20:24.132040Z","iopub.status.idle":"2023-06-23T17:20:24.138318Z","shell.execute_reply.started":"2023-06-23T17:20:24.132004Z","shell.execute_reply":"2023-06-23T17:20:24.137429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,15))\nfor images, labels in test_ds.take(1):\n    for i in range(9):\n        ax = plt.subplot(3,3,i+1)\n        plt.imshow(images[i].numpy().astype(\"uint8\"))\n\n        predicted_class, confidence = predict(model, images[i].numpy())\n        actual_class = class_names[labels[i]]\n\n        plt.title(f\"Actual: {actual_class}, \\nPredicted: {predicted_class},\\n Confidence: {confidence}%\")\n\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:20:24.139601Z","iopub.execute_input":"2023-06-23T17:20:24.140480Z","iopub.status.idle":"2023-06-23T17:20:27.139415Z","shell.execute_reply.started":"2023-06-23T17:20:24.140443Z","shell.execute_reply":"2023-06-23T17:20:27.138444Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('saved_models')\nlocation = \"saved_models\"\nmodel_version = max([int(i) for i in os.listdir(location) + [0]])+1\nmodel.save(f\"saved_models/{model_version}\")","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:20:27.140854Z","iopub.execute_input":"2023-06-23T17:20:27.141908Z","iopub.status.idle":"2023-06-23T17:20:30.286568Z","shell.execute_reply.started":"2023-06-23T17:20:27.141874Z","shell.execute_reply":"2023-06-23T17:20:30.285664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.models.load_model(\"saved_models/1\")\nclass_names = {0: \"Cat\", 1: \"Dog\"}","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:20:30.288322Z","iopub.execute_input":"2023-06-23T17:20:30.288680Z","iopub.status.idle":"2023-06-23T17:20:31.490351Z","shell.execute_reply.started":"2023-06-23T17:20:30.288648Z","shell.execute_reply":"2023-06-23T17:20:31.489331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = list()","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:20:31.492117Z","iopub.execute_input":"2023-06-23T17:20:31.492582Z","iopub.status.idle":"2023-06-23T17:20:31.498248Z","shell.execute_reply.started":"2023-06-23T17:20:31.492544Z","shell.execute_reply":"2023-06-23T17:20:31.497333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = 'test1'\n# img_names = [im for im in os.listdir(path)]\n# img_names = img_names[0:500]\nfor i in tqdm(range(1000)):\n    im = f'{(i+1)}.jpg'\n    img_path = f'{path}/{im}'\n\n    img = tf.io.read_file(img_path)\n    img = tf.image.decode_image(img)\n    img = tf.expand_dims(img, axis=0)\n    img = tf.image.resize(img, [256, 256])\n    img = tf.cast(img, dtype=tf.uint8)\n\n    # Perform prediction\n    predictions = model.predict(img, verbose=None)\n    animal_class = np.argmax(predictions[0])\n    predicted_class = class_names[animal_class]\n    # print(predictions)\n    # break\n\n    rows.append([int(im.split(\".\")[0]), animal_class])","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:45:11.973132Z","iopub.execute_input":"2023-06-23T17:45:11.973545Z","iopub.status.idle":"2023-06-23T17:46:09.041402Z","shell.execute_reply.started":"2023-06-23T17:45:11.973512Z","shell.execute_reply":"2023-06-23T17:46:09.040330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows.sort()","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:46:41.756834Z","iopub.execute_input":"2023-06-23T17:46:41.757204Z","iopub.status.idle":"2023-06-23T17:46:41.762521Z","shell.execute_reply.started":"2023-06-23T17:46:41.757173Z","shell.execute_reply":"2023-06-23T17:46:41.761428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\ncsv_file = 'submission.csv'\nwith open(csv_file, 'w', newline='') as file:\n    # Create a CSV writer object\n    writer = csv.writer(file)\n\n    # Write the column names to the CSV file\n    writer.writerow(['id', 'label'])\n\n    # Write the data to the CSV file\n    writer.writerows(rows)","metadata":{"execution":{"iopub.status.busy":"2023-06-23T17:46:43.189280Z","iopub.execute_input":"2023-06-23T17:46:43.190261Z","iopub.status.idle":"2023-06-23T17:46:43.199877Z","shell.execute_reply.started":"2023-06-23T17:46:43.190226Z","shell.execute_reply":"2023-06-23T17:46:43.198572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Sample Output of the predicted Model on Test Data","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(20,15))\nfor i in range(24):\n    ax = plt.subplot(4,6,i+1)\n    \n    im = f'{(i+1)}.jpg'\n    img_path = f'{path}/{im}'\n    \n    img = tf.io.read_file(img_path)\n    img = tf.image.decode_image(img)\n    img = tf.expand_dims(img, axis=0)\n    img = tf.image.resize(img, [256, 256])\n    img = tf.cast(img, dtype=tf.uint8)\n\n    predictions = model.predict(img, verbose=None)\n    animal_class = np.argmax(predictions[0])\n    predicted_class = class_names[animal_class]\n    confidence = round(100*(np.max(predictions[0])), 2)\n\n    plt.imshow(img[0])\n    plt.title(f\"Predicted: {predicted_class},\\n Confidence: {confidence}%\")\n    plt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-06-23T18:33:46.105360Z","iopub.execute_input":"2023-06-23T18:33:46.105837Z","iopub.status.idle":"2023-06-23T18:33:50.549548Z","shell.execute_reply.started":"2023-06-23T18:33:46.105795Z","shell.execute_reply":"2023-06-23T18:33:50.548239Z"},"trusted":true},"execution_count":null,"outputs":[]}]}