{"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 os\n\nprint(os.getcwd())\n# for i in os.listdir('checkpoint'):\n#     os.remove(f\"checkpoint/{i}\")\n# os.rmdir('checkpoint')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-26T14:55:44.756515Z","iopub.execute_input":"2023-06-26T14:55:44.757230Z","iopub.status.idle":"2023-06-26T14:55:44.770105Z","shell.execute_reply.started":"2023-06-26T14:55:44.757195Z","shell.execute_reply":"2023-06-26T14:55:44.768972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd\nfrom tensorflow import keras\nfrom keras.models import Sequential, model_from_json\nfrom keras.layers import Activation, Dense, Flatten, Rescaling, Conv2D, MaxPooling2D, Dropout, RandomRotation, RandomFlip\nfrom keras.optimizers import Adam\nfrom keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\nfrom keras.losses import BinaryCrossentropy\nfrom keras.utils import image_dataset_from_directory\nfrom keras.preprocessing.image import ImageDataGenerator\nimport os","metadata":{"execution":{"iopub.status.busy":"2023-06-26T14:55:44.771554Z","iopub.execute_input":"2023-06-26T14:55:44.772213Z","iopub.status.idle":"2023-06-26T14:55:48.205178Z","shell.execute_reply.started":"2023-06-26T14:55:44.772179Z","shell.execute_reply":"2023-06-26T14:55:48.203951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"gpus = tf.config.experimental.list_physical_devices('GPU')\nprint(\"Num GPUs available : \", len(gpus))\nfor gpu in gpus:\n      tf.config.experimental.set_memory_growth(gpu, True)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T14:55:48.207172Z","iopub.execute_input":"2023-06-26T14:55:48.208114Z","iopub.status.idle":"2023-06-26T14:55:48.329271Z","shell.execute_reply.started":"2023-06-26T14:55:48.208066Z","shell.execute_reply":"2023-06-26T14:55:48.328217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"strategy = tf.distribute.MirroredStrategy()","metadata":{"execution":{"iopub.status.busy":"2023-06-26T14:55:48.333517Z","iopub.execute_input":"2023-06-26T14:55:48.334263Z","iopub.status.idle":"2023-06-26T14:55:50.553712Z","shell.execute_reply.started":"2023-06-26T14:55:48.334224Z","shell.execute_reply":"2023-06-26T14:55:50.552461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_path = '../input/cat-dog-full/train'","metadata":{"execution":{"iopub.status.busy":"2023-06-26T14:55:50.555555Z","iopub.execute_input":"2023-06-26T14:55:50.556311Z","iopub.status.idle":"2023-06-26T14:55:50.572229Z","shell.execute_reply.started":"2023-06-26T14:55:50.556272Z","shell.execute_reply":"2023-06-26T14:55:50.566005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = (200, 200)\nbatch_size = 32","metadata":{"execution":{"iopub.status.busy":"2023-06-26T14:55:50.574796Z","iopub.execute_input":"2023-06-26T14:55:50.580101Z","iopub.status.idle":"2023-06-26T14:55:50.589147Z","shell.execute_reply.started":"2023-06-26T14:55:50.580066Z","shell.execute_reply":"2023-06-26T14:55:50.588079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds, val_ds = image_dataset_from_directory(\n    train_path,\n    seed = 42,\n    image_size=image_size,\n    batch_size=batch_size*strategy.num_replicas_in_sync,\n    label_mode='binary',\n    validation_split=0.2,\n    subset='both'\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T14:55:50.593034Z","iopub.execute_input":"2023-06-26T14:55:50.593972Z","iopub.status.idle":"2023-06-26T14:55:57.944620Z","shell.execute_reply.started":"2023-06-26T14:55:50.593937Z","shell.execute_reply":"2023-06-26T14:55:57.943332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor images, labels in train_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    plt.title(train_ds.class_names[int(labels[i])])\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-06-26T14:55:57.945823Z","iopub.execute_input":"2023-06-26T14:55:57.946181Z","iopub.status.idle":"2023-06-26T14:56:00.372695Z","shell.execute_reply.started":"2023-06-26T14:55:57.946147Z","shell.execute_reply":"2023-06-26T14:56:00.371826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    model = Sequential([\n        RandomFlip(mode=\"horizontal_and_vertical\", seed=42),\n        RandomRotation(factor=0.2, seed=42),\n        Rescaling(1.0/255),\n        Conv2D(filters=24, kernel_size=(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'),\n        MaxPooling2D(pool_size=(2, 2), strides=2),\n#         Dropout(0.2, seed=42),\n        Conv2D(filters=48, kernel_size=(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'),\n        MaxPooling2D(pool_size=(2, 2), strides=2),\n#         Dropout(0.2, seed=42),\n        Conv2D(filters=96, kernel_size=(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'),\n        MaxPooling2D(pool_size=(2, 2), strides=2),\n        Dropout(0.2, seed=42),\n        Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'),\n        MaxPooling2D(pool_size=(2, 2), strides=2),\n        Dropout(0.2, seed=42),\n        Conv2D(filters=128, kernel_size=(3, 3), activation='relu', padding='same', kernel_initializer='he_uniform'),\n        MaxPooling2D(pool_size=(2, 2), strides=2),\n        Dropout(0.5, seed=42),\n        Flatten(),\n        Dense(100, activation='relu', kernel_initializer='he_uniform'),\n        Dense(1, activation='sigmoid')\n    ])\n\n    model.compile(\n        optimizer = keras.optimizers.Adam(learning_rate=0.001),\n        loss = 'binary_crossentropy',\n        metrics=['accuracy']\n    )","metadata":{"execution":{"iopub.status.busy":"2023-06-26T14:56:00.373729Z","iopub.execute_input":"2023-06-26T14:56:00.375121Z","iopub.status.idle":"2023-06-26T14:56:00.462046Z","shell.execute_reply.started":"2023-06-26T14:56:00.375085Z","shell.execute_reply":"2023-06-26T14:56:00.461064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_callbacks = [\n    EarlyStopping(patience=5),\n    ModelCheckpoint(filepath='checkpoint1/model.h5',monitor='val_loss', mode=min, save_best_only=True),\n    ReduceLROnPlateau(monitor='val_loss', factor=0.5,\n                              patience=4, min_lr=0.0001)\n]\n\n# keras.backend.set_value(model.optimizer.lr, 0.001)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T16:19:38.954218Z","iopub.execute_input":"2023-06-26T16:19:38.954630Z","iopub.status.idle":"2023-06-26T16:19:38.960326Z","shell.execute_reply.started":"2023-06-26T16:19:38.954599Z","shell.execute_reply":"2023-06-26T16:19:38.959385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(\n    train_ds,\n    validation_data=val_ds,\n    epochs=50,\n    verbose=2,\n    callbacks=my_callbacks\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T16:19:40.907742Z","iopub.execute_input":"2023-06-26T16:19:40.908144Z","iopub.status.idle":"2023-06-26T16:29:25.438141Z","shell.execute_reply.started":"2023-06-26T16:19:40.908113Z","shell.execute_reply":"2023-06-26T16:29:25.436400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def summarize_diagnostics(history):\n # plot loss\n plt.subplot(211)\n plt.title('Cross Entropy Loss')\n plt.plot(history.history['loss'], color='blue', label='train')\n plt.plot(history.history['val_loss'], color='orange', label='test')\n # plot accuracy\n plt.subplot(212)\n plt.title('Classification Accuracy')\n plt.plot(history.history['accuracy'], color='blue', label='train')\n plt.plot(history.history['val_accuracy'], color='orange', label='test')\n plt.show()\n\nsummarize_diagnostics(history)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T16:29:35.892747Z","iopub.execute_input":"2023-06-26T16:29:35.893785Z","iopub.status.idle":"2023-06-26T16:29:36.293216Z","shell.execute_reply.started":"2023-06-26T16:29:35.893738Z","shell.execute_reply":"2023-06-26T16:29:36.292241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('model.tf')","metadata":{"execution":{"iopub.status.busy":"2023-06-26T15:27:30.753341Z","iopub.execute_input":"2023-06-26T15:27:30.754230Z","iopub.status.idle":"2023-06-26T15:27:33.442742Z","shell.execute_reply.started":"2023-06-26T15:27:30.754192Z","shell.execute_reply":"2023-06-26T15:27:33.441737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(train_ds)\nmodel.evaluate(val_ds)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T16:37:45.757847Z","iopub.execute_input":"2023-06-26T16:37:45.758558Z","iopub.status.idle":"2023-06-26T16:38:25.846423Z","shell.execute_reply.started":"2023-06-26T16:37:45.758521Z","shell.execute_reply":"2023-06-26T16:38:25.845389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_filenames = os.listdir(\"../input/cat-dog-test/test\")\ntest_df = pd.DataFrame({\n    'id': test_filenames\n})","metadata":{"execution":{"iopub.status.busy":"2023-06-26T16:30:21.120413Z","iopub.execute_input":"2023-06-26T16:30:21.120799Z","iopub.status.idle":"2023-06-26T16:30:21.137387Z","shell.execute_reply.started":"2023-06-26T16:30:21.120769Z","shell.execute_reply":"2023-06-26T16:30:21.136448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = ImageDataGenerator()\ntest_generator = test_gen.flow_from_dataframe(\n    test_df, \n    \"../input/cat-dog-test/test\", \n    x_col='id',\n    y_col=None,\n    class_mode=None,\n    target_size=image_size,\n    batch_size=batch_size,\n    shuffle=False\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T16:30:22.795507Z","iopub.execute_input":"2023-06-26T16:30:22.795887Z","iopub.status.idle":"2023-06-26T16:30:47.303144Z","shell.execute_reply.started":"2023-06-26T16:30:22.795842Z","shell.execute_reply":"2023-06-26T16:30:47.302167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T16:55:13.629868Z","iopub.execute_input":"2023-06-26T16:55:13.630293Z","iopub.status.idle":"2023-06-26T16:55:41.497965Z","shell.execute_reply.started":"2023-06-26T16:55:13.630263Z","shell.execute_reply":"2023-06-26T16:55:41.496920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['labels'] = np.round(predict).astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T16:31:22.604476Z","iopub.execute_input":"2023-06-26T16:31:22.604860Z","iopub.status.idle":"2023-06-26T16:31:22.611706Z","shell.execute_reply.started":"2023-06-26T16:31:22.604826Z","shell.execute_reply":"2023-06-26T16:31:22.610428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.set_index('id')\ntest_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-06-26T16:32:20.716340Z","iopub.execute_input":"2023-06-26T16:32:20.716718Z","iopub.status.idle":"2023-06-26T16:32:20.744185Z","shell.execute_reply.started":"2023-06-26T16:32:20.716688Z","shell.execute_reply":"2023-06-26T16:32:20.743231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}