{"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-07-14T11:35:40.31504Z","iopub.execute_input":"2023-07-14T11:35:40.315585Z","iopub.status.idle":"2023-07-14T11:35:49.679105Z","shell.execute_reply.started":"2023-07-14T11:35:40.31555Z","shell.execute_reply":"2023-07-14T11:35:49.677938Z"},"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-07-14T11:35:49.680832Z","iopub.execute_input":"2023-07-14T11:35:49.681488Z","iopub.status.idle":"2023-07-14T11:35:49.693356Z","shell.execute_reply.started":"2023-07-14T11:35:49.681455Z","shell.execute_reply":"2023-07-14T11:35:49.692296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.getcwd() # find the current working directory","metadata":{"execution":{"iopub.status.busy":"2023-07-14T11:35:49.695002Z","iopub.execute_input":"2023-07-14T11:35:49.695395Z","iopub.status.idle":"2023-07-14T11:35:49.725805Z","shell.execute_reply.started":"2023-07-14T11:35:49.695345Z","shell.execute_reply":"2023-07-14T11:35:49.724672Z"},"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-07-14T11:35:49.729265Z","iopub.execute_input":"2023-07-14T11:35:49.730666Z","iopub.status.idle":"2023-07-14T11:36:05.203338Z","shell.execute_reply.started":"2023-07-14T11:35:49.730633Z","shell.execute_reply":"2023-07-14T11:36:05.202307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('cats')\nos.mkdir('dogs')","metadata":{"execution":{"iopub.status.busy":"2023-07-14T11:36:05.204657Z","iopub.execute_input":"2023-07-14T11:36:05.205041Z","iopub.status.idle":"2023-07-14T11:36:05.21059Z","shell.execute_reply.started":"2023-07-14T11:36:05.205006Z","shell.execute_reply":"2023-07-14T11:36:05.209379Z"},"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-07-14T11:36:05.212124Z","iopub.execute_input":"2023-07-14T11:36:05.212774Z","iopub.status.idle":"2023-07-14T11:36:06.412876Z","shell.execute_reply.started":"2023-07-14T11:36:05.212721Z","shell.execute_reply":"2023-07-14T11:36:06.411943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.rmtree('train')","metadata":{"execution":{"iopub.status.busy":"2023-07-14T11:36:06.414396Z","iopub.execute_input":"2023-07-14T11:36:06.41505Z","iopub.status.idle":"2023-07-14T11:36:06.419713Z","shell.execute_reply.started":"2023-07-14T11:36:06.415018Z","shell.execute_reply":"2023-07-14T11:36:06.418607Z"},"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-07-14T11:36:06.421182Z","iopub.execute_input":"2023-07-14T11:36:06.421839Z","iopub.status.idle":"2023-07-14T11:36:06.436072Z","shell.execute_reply.started":"2023-07-14T11:36:06.421809Z","shell.execute_reply":"2023-07-14T11:36:06.435115Z"},"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-07-14T11:36:06.437306Z","iopub.execute_input":"2023-07-14T11:36:06.437715Z","iopub.status.idle":"2023-07-14T11:36:06.44574Z","shell.execute_reply.started":"2023-07-14T11:36:06.437683Z","shell.execute_reply":"2023-07-14T11:36:06.444763Z"},"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-07-14T11:36:06.451556Z","iopub.execute_input":"2023-07-14T11:36:06.451818Z","iopub.status.idle":"2023-07-14T11:36:11.688336Z","shell.execute_reply.started":"2023-07-14T11:36:06.451797Z","shell.execute_reply":"2023-07-14T11:36:11.687438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# class_names = dataset.class_names","metadata":{"execution":{"iopub.status.busy":"2023-07-14T11:36:11.689923Z","iopub.execute_input":"2023-07-14T11:36:11.690271Z","iopub.status.idle":"2023-07-14T11:36:11.696263Z","shell.execute_reply.started":"2023-07-14T11:36:11.69024Z","shell.execute_reply":"2023-07-14T11:36:11.695323Z"},"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-07-14T11:36:11.697817Z","iopub.execute_input":"2023-07-14T11:36:11.698167Z","iopub.status.idle":"2023-07-14T11:36:15.235109Z","shell.execute_reply.started":"2023-07-14T11:36:11.698137Z","shell.execute_reply":"2023-07-14T11:36:15.234292Z"},"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-07-14T11:36:15.236198Z","iopub.execute_input":"2023-07-14T11:36:15.237257Z","iopub.status.idle":"2023-07-14T11:36:15.251916Z","shell.execute_reply.started":"2023-07-14T11:36:15.237224Z","shell.execute_reply":"2023-07-14T11:36:15.251161Z"},"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-07-14T11:36:15.253513Z","iopub.execute_input":"2023-07-14T11:36:15.254236Z","iopub.status.idle":"2023-07-14T11:36:15.274599Z","shell.execute_reply.started":"2023-07-14T11:36:15.254195Z","shell.execute_reply":"2023-07-14T11:36:15.273562Z"},"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-07-14T11:36:15.276179Z","iopub.execute_input":"2023-07-14T11:36:15.276525Z","iopub.status.idle":"2023-07-14T11:36:15.295102Z","shell.execute_reply.started":"2023-07-14T11:36:15.276494Z","shell.execute_reply":"2023-07-14T11:36:15.294149Z"},"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-07-14T11:36:15.296731Z","iopub.execute_input":"2023-07-14T11:36:15.297074Z","iopub.status.idle":"2023-07-14T11:36:15.309291Z","shell.execute_reply.started":"2023-07-14T11:36:15.297042Z","shell.execute_reply":"2023-07-14T11:36:15.308381Z"},"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-07-14T11:36:15.310716Z","iopub.execute_input":"2023-07-14T11:36:15.311061Z","iopub.status.idle":"2023-07-14T11:36:15.719876Z","shell.execute_reply.started":"2023-07-14T11:36:15.311032Z","shell.execute_reply":"2023-07-14T11:36:15.718866Z"},"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-07-14T11:36:15.721101Z","iopub.execute_input":"2023-07-14T11:36:15.72148Z","iopub.status.idle":"2023-07-14T11:36:15.742972Z","shell.execute_reply.started":"2023-07-14T11:36:15.721447Z","shell.execute_reply":"2023-07-14T11:36:15.742073Z"},"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-07-14T11:36:15.744251Z","iopub.execute_input":"2023-07-14T11:36:15.744692Z","iopub.status.idle":"2023-07-14T11:40:59.173072Z","shell.execute_reply.started":"2023-07-14T11:36:15.744657Z","shell.execute_reply":"2023-07-14T11:40:59.172066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scores = model.evaluate(test_ds)","metadata":{"execution":{"iopub.status.busy":"2023-07-14T11:40:59.17466Z","iopub.execute_input":"2023-07-14T11:40:59.175103Z","iopub.status.idle":"2023-07-14T11:41:19.661559Z","shell.execute_reply.started":"2023-07-14T11:40:59.175069Z","shell.execute_reply":"2023-07-14T11:41:19.660551Z"},"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-07-14T11:41:19.663058Z","iopub.execute_input":"2023-07-14T11:41:19.66352Z","iopub.status.idle":"2023-07-14T11:41:19.98825Z","shell.execute_reply.started":"2023-07-14T11:41:19.663486Z","shell.execute_reply":"2023-07-14T11:41:19.987342Z"},"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-07-14T11:41:19.989566Z","iopub.execute_input":"2023-07-14T11:41:19.989991Z","iopub.status.idle":"2023-07-14T11:41:19.997493Z","shell.execute_reply.started":"2023-07-14T11:41:19.989946Z","shell.execute_reply":"2023-07-14T11:41:19.995605Z"},"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-07-14T11:41:19.9987Z","iopub.execute_input":"2023-07-14T11:41:19.999613Z","iopub.status.idle":"2023-07-14T11:41:22.507833Z","shell.execute_reply.started":"2023-07-14T11:41:19.999579Z","shell.execute_reply":"2023-07-14T11:41:22.506968Z"},"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-07-14T11:41:22.509263Z","iopub.execute_input":"2023-07-14T11:41:22.509828Z","iopub.status.idle":"2023-07-14T11:41:25.493979Z","shell.execute_reply.started":"2023-07-14T11:41:22.509795Z","shell.execute_reply":"2023-07-14T11:41:25.492912Z"},"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-07-14T11:41:25.495804Z","iopub.execute_input":"2023-07-14T11:41:25.496717Z","iopub.status.idle":"2023-07-14T11:41:26.684944Z","shell.execute_reply.started":"2023-07-14T11:41:25.496678Z","shell.execute_reply":"2023-07-14T11:41:26.683929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows = list()","metadata":{"execution":{"iopub.status.busy":"2023-07-14T11:41:26.686643Z","iopub.execute_input":"2023-07-14T11:41:26.687001Z","iopub.status.idle":"2023-07-14T11:41:26.691722Z","shell.execute_reply.started":"2023-07-14T11:41:26.686956Z","shell.execute_reply":"2023-07-14T11:41:26.690735Z"},"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-07-14T11:41:26.693299Z","iopub.execute_input":"2023-07-14T11:41:26.693641Z","iopub.status.idle":"2023-07-14T11:42:21.782584Z","shell.execute_reply.started":"2023-07-14T11:41:26.693611Z","shell.execute_reply":"2023-07-14T11:42:21.781629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rows.sort()","metadata":{"execution":{"iopub.status.busy":"2023-07-14T11:42:21.787816Z","iopub.execute_input":"2023-07-14T11:42:21.788097Z","iopub.status.idle":"2023-07-14T11:42:21.792521Z","shell.execute_reply.started":"2023-07-14T11:42:21.788072Z","shell.execute_reply":"2023-07-14T11:42:21.79148Z"},"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-07-14T11:42:21.793853Z","iopub.execute_input":"2023-07-14T11:42:21.794403Z","iopub.status.idle":"2023-07-14T11:42:21.808132Z","shell.execute_reply.started":"2023-07-14T11:42:21.794373Z","shell.execute_reply":"2023-07-14T11:42:21.807178Z"},"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-07-14T11:42:21.809517Z","iopub.execute_input":"2023-07-14T11:42:21.809854Z","iopub.status.idle":"2023-07-14T11:42:25.716954Z","shell.execute_reply.started":"2023-07-14T11:42:21.809821Z","shell.execute_reply":"2023-07-14T11:42:25.714507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}