{"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-23T14:12:35.134280Z","iopub.execute_input":"2022-07-23T14:12:35.134746Z","iopub.status.idle":"2022-07-23T14:12:41.340108Z","shell.execute_reply.started":"2022-07-23T14:12:35.134658Z","shell.execute_reply":"2022-07-23T14:12:41.339014Z"},"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-23T14:12:41.342065Z","iopub.execute_input":"2022-07-23T14:12:41.342735Z","iopub.status.idle":"2022-07-23T14:12:43.713375Z","shell.execute_reply.started":"2022-07-23T14:12:41.342696Z","shell.execute_reply":"2022-07-23T14:12:43.712390Z"},"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-23T14:12:43.714396Z","iopub.execute_input":"2022-07-23T14:12:43.715058Z","iopub.status.idle":"2022-07-23T14:13:00.299164Z","shell.execute_reply.started":"2022-07-23T14:12:43.715018Z","shell.execute_reply":"2022-07-23T14:13:00.298183Z"},"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-23T14:13:00.303073Z","iopub.execute_input":"2022-07-23T14:13:00.303358Z","iopub.status.idle":"2022-07-23T14:13:00.309475Z","shell.execute_reply.started":"2022-07-23T14:13:00.303332Z","shell.execute_reply":"2022-07-23T14:13:00.308411Z"},"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-23T14:13:00.311430Z","iopub.execute_input":"2022-07-23T14:13:00.311833Z","iopub.status.idle":"2022-07-23T14:13:00.413310Z","shell.execute_reply.started":"2022-07-23T14:13:00.311757Z","shell.execute_reply":"2022-07-23T14:13:00.412348Z"},"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-23T14:13:00.414921Z","iopub.execute_input":"2022-07-23T14:13:00.415290Z","iopub.status.idle":"2022-07-23T14:13:00.479911Z","shell.execute_reply.started":"2022-07-23T14:13:00.415256Z","shell.execute_reply":"2022-07-23T14:13:00.478947Z"},"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-23T14:13:00.481478Z","iopub.execute_input":"2022-07-23T14:13:00.481811Z","iopub.status.idle":"2022-07-23T14:13:00.489569Z","shell.execute_reply.started":"2022-07-23T14:13:00.481777Z","shell.execute_reply":"2022-07-23T14:13:00.488540Z"},"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-23T14:13:00.491333Z","iopub.execute_input":"2022-07-23T14:13:00.491685Z","iopub.status.idle":"2022-07-23T14:13:01.210708Z","shell.execute_reply.started":"2022-07-23T14:13:00.491651Z","shell.execute_reply":"2022-07-23T14:13:01.209648Z"},"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-23T14:13:01.212140Z","iopub.execute_input":"2022-07-23T14:13:01.212470Z","iopub.status.idle":"2022-07-23T14:13:01.228755Z","shell.execute_reply.started":"2022-07-23T14:13:01.212436Z","shell.execute_reply":"2022-07-23T14:13:01.227875Z"},"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-23T14:13:01.232373Z","iopub.execute_input":"2022-07-23T14:13:01.232625Z","iopub.status.idle":"2022-07-23T14:13:01.269864Z","shell.execute_reply.started":"2022-07-23T14:13:01.232603Z","shell.execute_reply":"2022-07-23T14:13:01.268855Z"},"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-23T14:13:01.271439Z","iopub.execute_input":"2022-07-23T14:13:01.271786Z","iopub.status.idle":"2022-07-23T14:13:05.645471Z","shell.execute_reply.started":"2022-07-23T14:13:01.271751Z","shell.execute_reply":"2022-07-23T14:13:05.644483Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:13:05.646919Z","iopub.execute_input":"2022-07-23T14:13:05.647487Z","iopub.status.idle":"2022-07-23T14:43:12.809744Z","shell.execute_reply.started":"2022-07-23T14:13:05.647449Z","shell.execute_reply":"2022-07-23T14:43:12.808722Z"},"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-23T14:43:12.811818Z","iopub.execute_input":"2022-07-23T14:43:12.812192Z","iopub.status.idle":"2022-07-23T14:43:12.866826Z","shell.execute_reply.started":"2022-07-23T14:43:12.812155Z","shell.execute_reply":"2022-07-23T14:43:12.865999Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T14:43:12.868136Z","iopub.execute_input":"2022-07-23T14:43:12.868457Z","iopub.status.idle":"2022-07-23T14:43:12.923191Z","shell.execute_reply.started":"2022-07-23T14:43:12.868425Z","shell.execute_reply":"2022-07-23T14:43:12.922288Z"},"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-23T14:43:12.924610Z","iopub.execute_input":"2022-07-23T14:43:12.924949Z","iopub.status.idle":"2022-07-23T14:43:55.232110Z","shell.execute_reply.started":"2022-07-23T14:43:12.924915Z","shell.execute_reply":"2022-07-23T14:43:55.231220Z"},"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-23T14:43:55.233763Z","iopub.execute_input":"2022-07-23T14:43:55.234479Z","iopub.status.idle":"2022-07-23T14:43:55.294290Z","shell.execute_reply.started":"2022-07-23T14:43:55.234440Z","shell.execute_reply":"2022-07-23T14:43:55.293362Z"},"trusted":true},"execution_count":null,"outputs":[]}]}