{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3362,"databundleVersionId":31148,"sourceType":"competition"}],"dockerImageVersionId":20479,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"below is implementation for weights and biases","metadata":{}},{"cell_type":"code","source":"# cnn sequential\n# several helpful packages to load in listed below\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten, Dropout, Activation, Conv2D, MaxPooling2D\n\n# input data files are available in the \"../input/\" directory.\n# eg, running will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\nimport zipfile\n\nwith zipfile.ZipFile(\"../input/train.zip\",\"r\") as z:\n    z.extractall(\".\")\n    \nwith zipfile.ZipFile(\"../input/test1.zip\",\"r\") as z:\n    z.extractall(\".\")\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"main_dir = \"/kaggle/working/\"\ntrain_dir = \"train\"\npath = os.path.join(main_dir,train_dir)\n\nfor p in os.listdir(path):\n    category = p.split(\".\")[0]\n    img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n    new_img_array = cv2.resize(img_array, dsize=(80, 80))\n    plt.imshow(new_img_array,cmap=\"gray\")\n    break\n","metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = []\ny = []\nconvert = lambda category : int(category == 'dog')\ndef create_test_data(path):\n    for p in os.listdir(path):\n        category = p.split(\".\")[0]\n        category = convert(category)\n        img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n        new_img_array = cv2.resize(img_array, dsize=(80, 80))\n        X.append(new_img_array)\n        y.append(category)\n    ","metadata":{"_uuid":"cfa2a50c1ea569d97806510e0e313e1ce9d7452b","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_test_data(path)\nX = np.array(X).reshape(-1, 80,80,1)\ny = np.array(y)","metadata":{"_uuid":"6e30159db4207451532ac085e617bc2669c93714","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalize data\nX = X/255.0","metadata":{"_uuid":"aac4daf7957d1429a9d47e19c75459c217f04d50","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n# Adds a densely-connected layer with 64 units to the model:\nmodel.add(Conv2D(64,(3,3), activation = 'relu', input_shape = X.shape[1:]))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n# Add another:\nmodel.add(Conv2D(64,(3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2,2)))\n\nmodel.add(Flatten())\nmodel.add(Dense(64, activation='relu'))\n# Add a softmax layer with 10 output units:\nmodel.add(Dense(1, activation='sigmoid'))\n\nmodel.compile(optimizer=\"adam\",\n              loss='binary_crossentropy',\n              metrics=['accuracy'])","metadata":{"_uuid":"80c4b5ece66f7a069cfb6194779dc02ef76ce3e8","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> method to augment my dataset","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Create an ImageDataGenerator with desired augmentations\ndatagen = ImageDataGenerator(\n    rotation_range=20,       # Random rotations up to 20 degrees\n    width_shift_range=0.2,   # Horizontal shifts\n    height_shift_range=0.2,  # Vertical shifts\n    shear_range=0.2,         # Shear transformations\n    zoom_range=0.2,          # Random zooms\n    horizontal_flip=True,    # Horizontal flips\n    brightness_range=[0.8, 1.2],  # Brightness adjustments\n    fill_mode='nearest'       # How to fill in newly created pixels\n)\n\n# Fit the generator to your training data (optional)\ndatagen.fit(X)\n\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# combine original and augmented data\naugmented_data = datagen.flow(X, y, batch_size=32)  # augmented images\noriginal_data = datagen.flow(X, y, batch_size=32)   # original images, no augmentation\n\n# Create a combined generator that randomly selects from augmented and original\ndef combined_generator():\n    while True:\n        if np.random.rand() < 0.5:  # 50% chance of original, 50% augmented\n            yield next(original_data)\n        else:\n            yield next(augmented_data)\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> below is the augmented data during training","metadata":{}},{"cell_type":"code","source":"# Train the model using the combined generator\nhistory = model.fit(combined_generator(), \n          steps_per_epoch=len(X) // 32,  # Adjust steps_per_epoch as needed\n          epochs=10, \n          validation_split=0.2) ","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> below is the non-augmented","metadata":{}},{"cell_type":"code","source":"history = model.fit(X, y, epochs=10, batch_size=32, validation_split=0.2)","metadata":{"_uuid":"ec02e9538761ef147ba6e0c2129dba117a37efa1","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(history.history.keys())","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Plotting training & validation accuracy values\nplt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()\n\n# Plotting training & validation loss values\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Validation'], loc='upper left')\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_accuracy = history.history['acc'][-1]\nprint(f\"Final training accuracy: {final_accuracy * 100:.2f}%\")\n\nfinal_val_accuracy = history.history['val_acc'][-1]\nprint(f\"Final validation accuracy: {final_val_accuracy * 100:.2f}%\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we have to preporcess our test data also same as that our training data. (So aka normalize, size and set to grayscale)","metadata":{"_uuid":"351552012321cc626c0177be12aee5c797ea72df"}},{"cell_type":"code","source":"train_dir = \"test1\"\npath = os.path.join(main_dir,train_dir)\n#os.listdir(path)\n\nX_test = []\nid_line = []\ndef create_test1_data(path):\n    for p in os.listdir(path):\n        id_line.append(p.split(\".\")[0])\n        img_array = cv2.imread(os.path.join(path,p),cv2.IMREAD_GRAYSCALE)\n        new_img_array = cv2.resize(img_array, dsize=(80, 80))\n        X_test.append(new_img_array)\ncreate_test1_data(path)\nX_test = np.array(X_test).reshape(-1,80,80,1)\nX_test = X_test/255","metadata":{"_uuid":"6b55df2d3396f6d27d4e5d4ea4584a9e1733bc92","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"so now is time to predict, so below code feeds the model with the values within (X_test), saving it under \"predictions\"","metadata":{"_uuid":"35c262be8b002a0cb484de9d22f14ba5d2557baf"}},{"cell_type":"code","source":"predictions = model.predict(X_test)","metadata":{"_uuid":"7b902cc4d61cfaa770fdf3b674b399e87fd62ac5","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Below is the code I added to take a single image, show what it looks like pre-processed to fit to my training data.\nThis also ","metadata":{}},{"cell_type":"code","source":"threshold = 0.5 \nfor i in range(len(predictions)):\n    img_id = id_line[i]\n    dog_probability = predictions[i][0]\n    \n    if dog_probability > threshold:\n        prediction_label = \"dog\"\n    else:\n        prediction_label = \"cat\"\n\n    print(f\"Image ID: {img_id}, Predicted Label: {prediction_label}\") ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This code below goes through every result of predicitons and classifies them as either being \"dog\" or \"cat\"","metadata":{}},{"cell_type":"code","source":"# Assuming you want to access the first image in X_test\nsingle_image = X_test[0] \n\n# If you want to access a specific image by its index in id_line\nimage_index = id_line.index('5')  # Replace ' ' with the desired image ID\nsingle_image = X_test[image_index]\n\n# Now you can use 'single_image' for prediction or visualization\nprediction = model.predict(single_image.reshape(1, 80, 80, 1))  # Reshape for prediction\nplt.imshow(single_image.reshape(80, 80), cmap='gray')\nplt.show()\n\ndog_probability = prediction[0][0]  # Extract probability, assuming 'prediction' is a 2D array\n\nif dog_probability > threshold:\n    prediction_label = \"dog\"\nelse:\n    prediction_label = \"cat\"\n    \n# Use 'image_index' here\nprint(f\"Image ID: {image_index}, Predicted Label: {prediction_label}, Dog Probability: {dog_probability}\") \n","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}