{"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":"%load_ext tensorboard\n%tensorboard --logdir logs","metadata":{"execution":{"iopub.status.busy":"2023-05-14T02:26:36.468015Z","iopub.execute_input":"2023-05-14T02:26:36.468430Z","iopub.status.idle":"2023-05-14T02:26:40.990082Z","shell.execute_reply.started":"2023-05-14T02:26:36.468399Z","shell.execute_reply":"2023-05-14T02:26:40.989174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tensorflow version that utilizes kaggle's gpus\n!pip install tensorflow-gpu==2.8.0","metadata":{"execution":{"iopub.status.busy":"2023-05-14T20:53:36.197031Z","iopub.execute_input":"2023-05-14T20:53:36.197367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom keras.callbacks import TensorBoard","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import Image\n# img = Image.open(\"./test1/10020.jpg\")\n# width, height = img.size\n# print(width, height)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64 \nepochs = 50\nIMG_HEIGHT = 199\nIMG_WIDTH = 200","metadata":{"execution":{"iopub.status.busy":"2023-05-14T02:24:02.765010Z","iopub.execute_input":"2023-05-14T02:24:02.765776Z","iopub.status.idle":"2023-05-14T02:24:02.772885Z","shell.execute_reply.started":"2023-05-14T02:24:02.765740Z","shell.execute_reply":"2023-05-14T02:24:02.771716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_generator = ImageDataGenerator(\nrescale=1./255,\nrotation_range=45,\nwidth_shift_range=.15,\nheight_shift_range=.15,\nhorizontal_flip=True,\nzoom_range=0.3)\n\nvalidation_image_generator = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2023-05-14T02:24:12.129365Z","iopub.execute_input":"2023-05-14T02:24:12.129716Z","iopub.status.idle":"2023-05-14T02:24:12.141563Z","shell.execute_reply.started":"2023-05-14T02:24:12.129686Z","shell.execute_reply":"2023-05-14T02:24:12.139810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!unzip /kaggle/input/dogs-vs-cats/train.zip\n!unzip /kaggle/input/dogs-vs-cats/test1.zip","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_PATH = \"/kaggle/working/train\"\nVAL_PATH = \"/kaggle/working/val\"\nsrc_dir = \"/kaggle/working/train\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport shutil\nimport random\nTRAIN_PATH = \"/kaggle/working/train\"\nVAL_PATH = \"/kaggle/working/val\"\nsrc_dir = \"/kaggle/working/train\"\nos.makedirs(os.path.join(VAL_PATH, 'cat'), exist_ok=True)\nos.makedirs(os.path.join(VAL_PATH, 'dog'), exist_ok=True)\nos.makedirs(os.path.join(TRAIN_PATH, 'cat'), exist_ok=True)\nos.makedirs(os.path.join(TRAIN_PATH, 'dog'), exist_ok=True)\nfor filename in os.listdir(src_dir):\n    if random.random()<0.2: # 20% split\n        target_dir = VAL_PATH\n    else:\n        target_dir = TRAIN_PATH\n    if filename.startswith('cat.'):\n        shutil.move(os.path.join(src_dir, filename), os.path.join(target_dir, 'cat', filename))\n    elif filename.startswith('dog.'):\n        shutil.move(os.path.join(src_dir, filename), os.path.join(target_dir, 'dog', filename))","metadata":{"execution":{"iopub.status.busy":"2023-05-14T02:25:02.024304Z","iopub.execute_input":"2023-05-14T02:25:02.024737Z","iopub.status.idle":"2023-05-14T02:25:02.901863Z","shell.execute_reply.started":"2023-05-14T02:25:02.024701Z","shell.execute_reply":"2023-05-14T02:25:02.900876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data_gen = train_image_generator.flow_from_directory(batch_size=batch_size,\n                                                          directory=TRAIN_PATH,\n                                                          shuffle=True,\n                                                          target_size=(IMG_HEIGHT, IMG_WIDTH),\n                                                          class_mode='binary')\nval_data_gen = validation_image_generator.flow_from_directory(batch_size=batch_size,\n                                                             directory=VAL_PATH,\n                                                             target_size=(IMG_HEIGHT, IMG_WIDTH),\n                                                             class_mode='binary')","metadata":{"execution":{"iopub.status.busy":"2023-05-14T02:25:07.665560Z","iopub.execute_input":"2023-05-14T02:25:07.665930Z","iopub.status.idle":"2023-05-14T02:25:08.337976Z","shell.execute_reply.started":"2023-05-14T02:25:07.665898Z","shell.execute_reply":"2023-05-14T02:25:08.337064Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Build the model\nmodel = Sequential([\n    Conv2D(16, 3, padding='same', activation='relu', \n           input_shape=(IMG_HEIGHT, IMG_WIDTH ,3)),\n    MaxPooling2D(),\n    Dropout(0.2),\n    Conv2D(32, 3, padding='same', activation='relu'),\n    MaxPooling2D(),\n    Conv2D(64, 3, padding='same', activation='relu'),\n    MaxPooling2D(),\n    Dropout(0.2),\n    Flatten(),\n    Dense(512, activation='relu'),\n    Dense(1)\n])\n# Compile the model\nmodel.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy'])\n# print the model architecture\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-05-14T02:25:16.123649Z","iopub.execute_input":"2023-05-14T02:25:16.124018Z","iopub.status.idle":"2023-05-14T02:25:19.954259Z","shell.execute_reply.started":"2023-05-14T02:25:16.123989Z","shell.execute_reply":"2023-05-14T02:25:19.953514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.test.gpu_device_name()\n!nvcc --version\n!pip show tensorflow","metadata":{"execution":{"iopub.status.busy":"2023-05-14T02:54:29.963754Z","iopub.execute_input":"2023-05-14T02:54:29.964174Z","iopub.status.idle":"2023-05-14T02:54:43.627344Z","shell.execute_reply.started":"2023-05-14T02:54:29.964142Z","shell.execute_reply":"2023-05-14T02:54:43.626092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"! mkdir weights","metadata":{"execution":{"iopub.status.busy":"2023-05-14T03:20:35.975834Z","iopub.execute_input":"2023-05-14T03:20:35.976209Z","iopub.status.idle":"2023-05-14T03:20:37.061990Z","shell.execute_reply.started":"2023-05-14T03:20:35.976177Z","shell.execute_reply":"2023-05-14T03:20:37.060634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tensor_board = TensorBoard(log_dir='/kaggle/working/Graph', histogram_freq=1, write_graph=True, write_images=True)\nhistory = model.fit(\n    train_data_gen,\n#     steps_per_epoch=1000,\n    epochs=epochs,\n    validation_data=val_data_gen,\n#     validation_steps=1000,\n    batch_size = 64,\n    validation_batch_size = 64,\n    callbacks = [tensor_board]\n)\nmodel.save_weights('/kaggle/working/weights/model_weights.h5')","metadata":{"execution":{"iopub.status.busy":"2023-05-14T03:48:54.732470Z","iopub.execute_input":"2023-05-14T03:48:54.732883Z","iopub.status.idle":"2023-05-14T06:37:28.457754Z","shell.execute_reply.started":"2023-05-14T03:48:54.732849Z","shell.execute_reply":"2023-05-14T06:37:28.456764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights('/kaggle/working/weights/model_weights.h5')\n!zip -r file.zip /kaggle/working\n","metadata":{"execution":{"iopub.status.busy":"2023-05-14T11:51:49.207304Z","iopub.execute_input":"2023-05-14T11:51:49.207687Z","iopub.status.idle":"2023-05-14T11:51:49.573226Z","shell.execute_reply.started":"2023-05-14T11:51:49.207654Z","shell.execute_reply":"2023-05-14T11:51:49.571848Z"},"trusted":true},"execution_count":null,"outputs":[]}]}