{"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 numpy as np\nimport os\nimport glob\nimport tensorflow as tf\nfrom tensorflow.keras.applications import EfficientNetB3\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras import layers\nimport zipfile\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-31T11:43:14.786753Z","iopub.execute_input":"2022-07-31T11:43:14.787208Z","iopub.status.idle":"2022-07-31T11:43:19.697697Z","shell.execute_reply.started":"2022-07-31T11:43:14.787114Z","shell.execute_reply":"2022-07-31T11:43:19.696677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.experimental.AUTOTUNE\ntf.get_logger().setLevel(\"ERROR\")\n\ndevice_name = tf.test.gpu_device_name()\nif \"GPU\" not in device_name:\n    print(\"GPU device not found\")\nprint('Found GPU at: {}'.format(device_name))\nprint(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T11:43:19.700639Z","iopub.execute_input":"2022-07-31T11:43:19.701589Z","iopub.status.idle":"2022-07-31T11:43:21.831809Z","shell.execute_reply.started":"2022-07-31T11:43:19.701548Z","shell.execute_reply":"2022-07-31T11:43:21.830533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"zip_df = zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats-redux-kernels-edition/train.zip\", 'r')\nzip_df.extractall(\"/kaggle/working/\")\nzip_df.close()\nzip_df = zipfile.ZipFile(\"/kaggle/input/dogs-vs-cats-redux-kernels-edition/test.zip\", 'r')\nzip_df.extractall(\"/kaggle/working/\")\nzip_df.close()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T11:43:21.833325Z","iopub.execute_input":"2022-07-31T11:43:21.834864Z","iopub.status.idle":"2022-07-31T11:43:40.640829Z","shell.execute_reply.started":"2022-07-31T11:43:21.834825Z","shell.execute_reply":"2022-07-31T11:43:40.639779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_path(path,name):\n    return glob.glob(path+'/*.'+name)\ndog_check = lambda x: 1 if x.split('.')[1].split('/')[-1] == 'dog' else 0","metadata":{"execution":{"iopub.status.busy":"2022-07-31T11:43:40.643641Z","iopub.execute_input":"2022-07-31T11:43:40.643937Z","iopub.status.idle":"2022-07-31T11:43:40.652576Z","shell.execute_reply.started":"2022-07-31T11:43:40.643910Z","shell.execute_reply":"2022-07-31T11:43:40.651644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_list = get_path('./train','jpg')\nresult = list(map(dog_check,data_list))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T11:43:40.656168Z","iopub.execute_input":"2022-07-31T11:43:40.656793Z","iopub.status.idle":"2022-07-31T11:43:40.768797Z","shell.execute_reply.started":"2022-07-31T11:43:40.656765Z","shell.execute_reply":"2022-07-31T11:43:40.767982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = []\nval_data = []\ntrain_label = []\nval_label = []\n\ndogs_list = [i for i in data_list if dog_check(i)]\ncats_list = [i for i in data_list if not dog_check(i)]\n\nfor i in range(int(len(data_list) / 2)):\n    if (i < len(data_list) / 2 * 0.9):\n        train_data.append(dogs_list[i])\n        train_data.append(cats_list[i])\n    else:\n        val_data.append(dogs_list[i])\n        val_data.append(cats_list[i])\n\ntrain_label = list(map(dog_check,train_data))\nval_label = list(map(dog_check,val_data))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T11:43:40.769967Z","iopub.execute_input":"2022-07-31T11:43:40.770402Z","iopub.status.idle":"2022-07-31T11:43:40.845135Z","shell.execute_reply.started":"2022-07-31T11:43:40.770366Z","shell.execute_reply":"2022-07-31T11:43:40.844267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_size = 300\n\ndef preprocess_image(image):\n  image = tf.image.decode_jpeg(image, channels=3)\n  image = tf.image.resize(image, [img_size, img_size])\n\n  return image\n\ndef load_and_preprocess_image(path):\n  image = tf.io.read_file(path)\n  return preprocess_image(image)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T11:43:40.846531Z","iopub.execute_input":"2022-07-31T11:43:40.846870Z","iopub.status.idle":"2022-07-31T11:43:40.854908Z","shell.execute_reply.started":"2022-07-31T11:43:40.846833Z","shell.execute_reply":"2022-07-31T11:43:40.853883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_train = tf.data.Dataset.from_tensor_slices((train_data,train_label))\nds_val = tf.data.Dataset.from_tensor_slices((val_data,val_label))\n\ndef load_and_preprocess_from_path_label(path, label):\n  return load_and_preprocess_image(path), tf.one_hot(label, 2)\n\nds_train = ds_train.map(load_and_preprocess_from_path_label)\nds_val = ds_val.map(load_and_preprocess_from_path_label)\n\nprint('train dataset:',len(ds_train),'validation dataset:',len(ds_val))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T11:43:40.856582Z","iopub.execute_input":"2022-07-31T11:43:40.857171Z","iopub.status.idle":"2022-07-31T11:43:41.542414Z","shell.execute_reply.started":"2022-07-31T11:43:40.857129Z","shell.execute_reply":"2022-07-31T11:43:41.541384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#hyper paramater\nbatch_size = 32\nepochs = 15\nlearning_rate = 1e-5\ntop_dropout_rate = 0.5\n\nds_train = ds_train.shuffle(buffer_size=2000)\nds_batch_train = ds_train.batch(batch_size=batch_size)\nds_batch_train = ds_batch_train.prefetch(tf.data.AUTOTUNE)\nds_batch_val = ds_val.batch(batch_size=batch_size, drop_remainder=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T11:43:41.543712Z","iopub.execute_input":"2022-07-31T11:43:41.544465Z","iopub.status.idle":"2022-07-31T11:43:41.559892Z","shell.execute_reply.started":"2022-07-31T11:43:41.544425Z","shell.execute_reply":"2022-07-31T11:43:41.559041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_augmentation = Sequential(\n    [\n        layers.RandomRotation(factor=(-0.2,0.3)),\n        layers.RandomTranslation(height_factor=0.1, width_factor=0.1),\n        layers.RandomFlip(),\n        layers.RandomContrast(factor=0.1),\n    ]\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T11:43:41.564040Z","iopub.execute_input":"2022-07-31T11:43:41.564873Z","iopub.status.idle":"2022-07-31T11:43:41.599077Z","shell.execute_reply.started":"2022-07-31T11:43:41.564837Z","shell.execute_reply":"2022-07-31T11:43:41.598207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    model = Sequential()\n    \n    model.add(layers.Input(shape=(img_size, img_size, 3)))\n    model.add(img_augmentation)\n    base_model = EfficientNetB3(include_top=False, weights=\"imagenet\")\n    base_model.trainable = False\n    model.add(base_model)\n    \n    model.add(layers.GlobalAveragePooling2D())\n    model.add(layers.BatchNormalization())\n    model.add(layers.Dropout(top_dropout_rate))\n    model.add(layers.Dense(2, activation=\"softmax\"))\n    \n    optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)\n    model.compile(optimizer=optimizer, loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])\n\n    model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-31T11:43:41.600275Z","iopub.execute_input":"2022-07-31T11:43:41.601168Z","iopub.status.idle":"2022-07-31T11:43:46.771696Z","shell.execute_reply.started":"2022-07-31T11:43:41.601130Z","shell.execute_reply":"2022-07-31T11:43:46.770655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit(ds_batch_train, epochs=epochs, validation_data=ds_batch_val,batch_size=batch_size, shuffle=True, verbose=1)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-31T11:43:46.773194Z","iopub.execute_input":"2022-07-31T11:43:46.773908Z","iopub.status.idle":"2022-07-31T12:15:33.155357Z","shell.execute_reply.started":"2022-07-31T11:43:46.773865Z","shell.execute_reply":"2022-07-31T12:15:33.154304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_list = get_path('./test','jpg')\nid_load = lambda x : int(x.split('/')[-1].split('.')[0])\nid_list = list(map(id_load,test_list))","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:15:33.157900Z","iopub.execute_input":"2022-07-31T12:15:33.158635Z","iopub.status.idle":"2022-07-31T12:15:33.220757Z","shell.execute_reply.started":"2022-07-31T12:15:33.158596Z","shell.execute_reply":"2022-07-31T12:15:33.219834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_test = tf.data.Dataset.from_tensor_slices(test_list)\nds_test = ds_test.map(load_and_preprocess_image)\nds_batch_test = ds_test.batch(batch_size=64, drop_remainder=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:24:10.808958Z","iopub.execute_input":"2022-07-31T12:24:10.809358Z","iopub.status.idle":"2022-07-31T12:24:10.875257Z","shell.execute_reply.started":"2022-07-31T12:24:10.809324Z","shell.execute_reply":"2022-07-31T12:24:10.874282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = model.predict(ds_batch_test,batch_size=64,max_queue_size=1,verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:24:13.167746Z","iopub.execute_input":"2022-07-31T12:24:13.168113Z","iopub.status.idle":"2022-07-31T12:24:56.004445Z","shell.execute_reply.started":"2022-07-31T12:24:13.168082Z","shell.execute_reply":"2022-07-31T12:24:56.003274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = {'id':id_list,'label':list(map(lambda x:x[1],result))} \nsubmission_df=pd.DataFrame(submission)\nsubmission_df.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-31T12:24:58.443878Z","iopub.execute_input":"2022-07-31T12:24:58.444564Z","iopub.status.idle":"2022-07-31T12:24:58.506224Z","shell.execute_reply.started":"2022-07-31T12:24:58.444526Z","shell.execute_reply":"2022-07-31T12:24:58.505273Z"},"trusted":true},"execution_count":null,"outputs":[]}]}