{"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 keras.models import Sequential\nfrom tensorflow import keras\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, GlobalAveragePooling2D\nfrom tensorflow.keras.optimizers import RMSprop\nfrom tensorflow.keras.applications import InceptionV3\nimport os\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:28:31.684219Z","iopub.execute_input":"2023-05-14T12:28:31.684678Z","iopub.status.idle":"2023-05-14T12:28:43.192840Z","shell.execute_reply.started":"2023-05-14T12:28:31.684561Z","shell.execute_reply":"2023-05-14T12:28:43.190772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip -q /kaggle/input/dogs-vs-cats/train.zip && echo done","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:28:43.194973Z","iopub.execute_input":"2023-05-14T12:28:43.196928Z","iopub.status.idle":"2023-05-14T12:28:53.404240Z","shell.execute_reply.started":"2023-05-14T12:28:43.196864Z","shell.execute_reply":"2023-05-14T12:28:53.402997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creaing a for loop to assign labels\ntrain = os.listdir('/kaggle/working/train')\nlabels = []\n\nfor i in range(1800):\n  file_name = train[i]\n  label = file_name[0:3]\n\n  if label == 'dog':\n    labels.append([0,1])\n\n  else:\n    labels.append([1,0])","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:28:53.405405Z","iopub.execute_input":"2023-05-14T12:28:53.405802Z","iopub.status.idle":"2023-05-14T12:28:53.427312Z","shell.execute_reply.started":"2023-05-14T12:28:53.405771Z","shell.execute_reply":"2023-05-14T12:28:53.426317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(labels[0:10])\n\nprint(train[:10])","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:28:53.429951Z","iopub.execute_input":"2023-05-14T12:28:53.430505Z","iopub.status.idle":"2023-05-14T12:28:53.436599Z","shell.execute_reply.started":"2023-05-14T12:28:53.430474Z","shell.execute_reply":"2023-05-14T12:28:53.435283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Without CutMix Augmentation**","metadata":{}},{"cell_type":"code","source":"AUTO = tf.data.AUTOTUNE\nBATCH_SIZE = 32\nIMG_SIZE = 224","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:28:53.437874Z","iopub.execute_input":"2023-05-14T12:28:53.438220Z","iopub.status.idle":"2023-05-14T12:28:53.718413Z","shell.execute_reply.started":"2023-05-14T12:28:53.438184Z","shell.execute_reply":"2023-05-14T12:28:53.717464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def preprocess_image(image, label):\n    image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))\n    image = tf.image.convert_image_dtype(image, tf.float64) / 255.0\n    return image, label","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:28:53.720366Z","iopub.execute_input":"2023-05-14T12:28:53.720763Z","iopub.status.idle":"2023-05-14T12:28:54.096605Z","shell.execute_reply.started":"2023-05-14T12:28:53.720730Z","shell.execute_reply":"2023-05-14T12:28:54.095806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Initialize an empty list to store the preprocessed image data\npreprocessed_train_images = []\n\n# Iterate over each image filename\nfor filename in train[:1800]:\n    # Load the image using OpenCV\n    image = cv2.imread('/kaggle/working/train/'+filename)\n\n    # Preprocess the image (e.g., resize, normalize, etc.)\n    preprocessed_image,_ = preprocess_image(image,\"0\")\n\n    # Append the preprocessed image to the list\n    preprocessed_train_images.append(preprocessed_image)\n\n# Convert the list of preprocessed images to a numpy array\ntrain_image_data = np.array(preprocessed_train_images)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:28:54.097682Z","iopub.execute_input":"2023-05-14T12:28:54.098611Z","iopub.status.idle":"2023-05-14T12:29:02.833969Z","shell.execute_reply.started":"2023-05-14T12:28:54.098583Z","shell.execute_reply":"2023-05-14T12:29:02.832912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = np.array(labels).astype(np.float32)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:29:02.835685Z","iopub.execute_input":"2023-05-14T12:29:02.836048Z","iopub.status.idle":"2023-05-14T12:29:02.842035Z","shell.execute_reply.started":"2023-05-14T12:29:02.836017Z","shell.execute_reply":"2023-05-14T12:29:02.840784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_test,y_train,y_test = train_test_split(train_image_data,y,test_size=0.20,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:29:02.843180Z","iopub.execute_input":"2023-05-14T12:29:02.843506Z","iopub.status.idle":"2023-05-14T12:29:03.317937Z","shell.execute_reply.started":"2023-05-14T12:29:02.843479Z","shell.execute_reply":"2023-05-14T12:29:03.316402Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)\nprint(x_test.shape)\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:29:03.322168Z","iopub.execute_input":"2023-05-14T12:29:03.322502Z","iopub.status.idle":"2023-05-14T12:29:03.328346Z","shell.execute_reply.started":"2023-05-14T12:29:03.322474Z","shell.execute_reply":"2023-05-14T12:29:03.327327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import VGG16\n\ndef create_vgg16_model(input_shape, num_classes):\n    # Load pre-trained VGG16 model\n    base_model = VGG16(weights='imagenet', include_top=False, input_shape=input_shape)\n    \n    # Freeze the base model's layers\n    base_model.trainable = False\n    \n    # Create the classification head\n    inputs = tf.keras.Input(shape=input_shape)\n    x = base_model(inputs, training=False)\n    x = tf.keras.layers.Flatten()(x)\n    \n    # Apply dropout\n    x = tf.keras.layers.Dropout(0.5)(x)\n    \n    # Add dense layers with regularization\n    x = tf.keras.layers.Dense(4096, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01))(x)\n    x = tf.keras.layers.Dropout(0.5)(x)\n    x = tf.keras.layers.Dense(4096, activation='relu', kernel_regularizer=tf.keras.regularizers.l2(0.01))(x)\n    \n    # Output layer with sigmoid activation for binary classification\n    outputs = tf.keras.layers.Dense(num_classes, activation='sigmoid')(x)\n    \n    # Create the model\n    model = tf.keras.Model(inputs, outputs)\n    return model\n\n# Build the VGG16 model with improvements\nmodel = create_vgg16_model(input_shape=(IMG_SIZE, IMG_SIZE, 3), num_classes=2)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:29:03.329723Z","iopub.execute_input":"2023-05-14T12:29:03.330015Z","iopub.status.idle":"2023-05-14T12:29:06.959771Z","shell.execute_reply.started":"2023-05-14T12:29:03.329990Z","shell.execute_reply":"2023-05-14T12:29:06.958664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:29:06.961273Z","iopub.execute_input":"2023-05-14T12:29:06.961577Z","iopub.status.idle":"2023-05-14T12:29:06.996187Z","shell.execute_reply.started":"2023-05-14T12:29:06.961553Z","shell.execute_reply":"2023-05-14T12:29:06.995129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.Adam(lr=0.001)\nmodel.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:29:06.997729Z","iopub.execute_input":"2023-05-14T12:29:06.998034Z","iopub.status.idle":"2023-05-14T12:29:07.021596Z","shell.execute_reply.started":"2023-05-14T12:29:06.998007Z","shell.execute_reply":"2023-05-14T12:29:07.019957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    x_train,\n    y_train,\n    batch_size=BATCH_SIZE,\n    epochs=5,\n    validation_data=(x_test, y_test)\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T12:29:07.023228Z","iopub.execute_input":"2023-05-14T12:29:07.023575Z","iopub.status.idle":"2023-05-14T13:02:22.009399Z","shell.execute_reply.started":"2023-05-14T12:29:07.023548Z","shell.execute_reply":"2023-05-14T13:02:22.007553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **With CutMix Augmentation**","metadata":{}},{"cell_type":"code","source":"train_ds_one = (\n    tf.data.Dataset.from_tensor_slices((x_train, y_train))\n    .shuffle(1024)\n)\ntrain_ds_two = (\n    tf.data.Dataset.from_tensor_slices((x_train, y_train))\n    .shuffle(1024)\n)\n\ntrain_ds_simple = tf.data.Dataset.from_tensor_slices((x_train, y_train))\n\ntest_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test))\n\ntrain_ds_simple = (\n    train_ds_simple.map(preprocess_image, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n\n# Combine two shuffled datasets from the same training data.\ntrain_ds = tf.data.Dataset.zip((train_ds_one, train_ds_two))\n\ntest_ds = (\n    test_ds.map(preprocess_image, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T13:02:22.013865Z","iopub.execute_input":"2023-05-14T13:02:22.015847Z","iopub.status.idle":"2023-05-14T13:02:27.091336Z","shell.execute_reply.started":"2023-05-14T13:02:22.015799Z","shell.execute_reply":"2023-05-14T13:02:27.089861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def sample_beta_distribution(size, concentration_0=0.2, concentration_1=0.2):\n    gamma_1_sample = tf.random.gamma(shape=[size], alpha=concentration_1)\n    gamma_2_sample = tf.random.gamma(shape=[size], alpha=concentration_0)\n    return gamma_1_sample / (gamma_1_sample + gamma_2_sample)\n\n\n@tf.function\ndef get_box(lambda_value):\n    cut_rat = tf.math.sqrt(1.0 - lambda_value)\n\n    cut_w = IMG_SIZE * cut_rat  # rw\n    cut_w = tf.cast(cut_w, tf.int32)\n\n    cut_h = IMG_SIZE * cut_rat  # rh\n    cut_h = tf.cast(cut_h, tf.int32)\n\n    cut_x = tf.random.uniform((1,), minval=0, maxval=IMG_SIZE, dtype=tf.int32)  # rx\n    cut_y = tf.random.uniform((1,), minval=0, maxval=IMG_SIZE, dtype=tf.int32)  # ry\n\n    boundaryx1 = tf.clip_by_value(cut_x[0] - cut_w // 2, 0, IMG_SIZE)\n    boundaryy1 = tf.clip_by_value(cut_y[0] - cut_h // 2, 0, IMG_SIZE)\n    bbx2 = tf.clip_by_value(cut_x[0] + cut_w // 2, 0, IMG_SIZE)\n    bby2 = tf.clip_by_value(cut_y[0] + cut_h // 2, 0, IMG_SIZE)\n\n    target_h = bby2 - boundaryy1\n    if target_h == 0:\n        target_h += 1\n\n    target_w = bbx2 - boundaryx1\n    if target_w == 0:\n        target_w += 1\n\n    return boundaryx1, boundaryy1, target_h, target_w\n\n\n@tf.function\ndef cutmix(train_ds_one, train_ds_two):\n    (image1, label1), (image2, label2) = train_ds_one, train_ds_two\n\n    alpha = [0.25]\n    beta = [0.25]\n\n    # Get a sample from the Beta distribution\n    lambda_value = sample_beta_distribution(1, alpha, beta)\n\n    # Define Lambda\n    lambda_value = lambda_value[0][0]\n\n    # Get the bounding box offsets, heights and widths\n    boundaryx1, boundaryy1, target_h, target_w = get_box(lambda_value)\n\n    # Get a patch from the second image (`image2`)\n    crop2 = tf.image.crop_to_bounding_box(\n        image2, boundaryy1, boundaryx1, target_h, target_w\n    )\n    # Pad the `image2` patch (`crop2`) with the same offset\n    image2 = tf.image.pad_to_bounding_box(\n        crop2, boundaryy1, boundaryx1, IMG_SIZE, IMG_SIZE\n    )\n    # Get a patch from the first image (`image1`)\n    crop1 = tf.image.crop_to_bounding_box(\n        image1, boundaryy1, boundaryx1, target_h, target_w\n    )\n    # Pad the `image1` patch (`crop1`) with the same offset\n    img1 = tf.image.pad_to_bounding_box(\n        crop1, boundaryy1, boundaryx1, IMG_SIZE, IMG_SIZE\n    )\n\n    # Modify the first image by subtracting the patch from `image1`\n    # (before applying the `image2` patch)\n    image1 = image1 - img1\n    # Add the modified `image1` and `image2`  together to get the CutMix image\n    image = image1 + image2\n\n    # Adjust Lambda in accordance to the pixel ration\n    lambda_value = 1 - (target_w * target_h) / (IMG_SIZE * IMG_SIZE)\n    lambda_value = tf.cast(lambda_value, tf.float32)\n\n    # Combine the labels of both images\n    label = lambda_value * label1 + (1 - lambda_value) * label2\n    return image, label","metadata":{"execution":{"iopub.status.busy":"2023-05-14T13:02:27.092993Z","iopub.execute_input":"2023-05-14T13:02:27.093301Z","iopub.status.idle":"2023-05-14T13:02:27.109687Z","shell.execute_reply.started":"2023-05-14T13:02:27.093277Z","shell.execute_reply":"2023-05-14T13:02:27.108084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nclass_names=['cat','dog']\n# Create the new dataset using our `cutmix` utility\ntrain_ds_cmu = (\n    train_ds.shuffle(1024)\n    .map(cutmix, num_parallel_calls=AUTO)\n    .batch(BATCH_SIZE)\n    .prefetch(AUTO)\n)\n\n# Let's preview 9 samples from the dataset\nimage_batch, label_batch = next(iter(train_ds_cmu))\nplt.figure(figsize=(10, 10))\nfor i in range(9):\n    ax = plt.subplot(3, 3, i + 1)\n    plt.title(class_names[np.argmax(label_batch[i])])\n    plt.imshow(image_batch[i])\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-05-14T13:02:27.111398Z","iopub.execute_input":"2023-05-14T13:02:27.111944Z","iopub.status.idle":"2023-05-14T13:02:30.075522Z","shell.execute_reply.started":"2023-05-14T13:02:27.111900Z","shell.execute_reply":"2023-05-14T13:02:30.074353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = tf.keras.optimizers.Adam(lr=0.001)\nmodel.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['acc'])","metadata":{"execution":{"iopub.status.busy":"2023-05-14T13:04:23.675779Z","iopub.execute_input":"2023-05-14T13:04:23.676769Z","iopub.status.idle":"2023-05-14T13:04:23.689338Z","shell.execute_reply.started":"2023-05-14T13:04:23.676724Z","shell.execute_reply":"2023-05-14T13:04:23.688173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n      train_ds_cmu,\n      steps_per_epoch=BATCH_SIZE,\n      epochs=5,\n      validation_data=test_ds,\n      validation_steps=BATCH_SIZE\n)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T13:04:25.585258Z","iopub.execute_input":"2023-05-14T13:04:25.585686Z","iopub.status.idle":"2023-05-14T13:11:15.195343Z","shell.execute_reply.started":"2023-05-14T13:04:25.585653Z","shell.execute_reply":"2023-05-14T13:11:15.193420Z"},"trusted":true},"execution_count":null,"outputs":[]}]}