{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-01-16T08:51:08.651223Z","iopub.execute_input":"2023-01-16T08:51:08.651687Z","iopub.status.idle":"2023-01-16T08:51:08.664794Z","shell.execute_reply.started":"2023-01-16T08:51:08.651657Z","shell.execute_reply":"2023-01-16T08:51:08.662548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install gdown","metadata":{"execution":{"iopub.status.busy":"2023-01-16T08:51:08.668667Z","iopub.execute_input":"2023-01-16T08:51:08.669663Z","iopub.status.idle":"2023-01-16T08:51:18.757751Z","shell.execute_reply.started":"2023-01-16T08:51:08.669611Z","shell.execute_reply":"2023-01-16T08:51:18.755630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!gdown 1nBK_UkO2bN3xHPhbm_nOEcA1Et0g-Kp5","metadata":{"execution":{"iopub.status.busy":"2023-01-16T08:51:18.760058Z","iopub.execute_input":"2023-01-16T08:51:18.760441Z","iopub.status.idle":"2023-01-16T08:51:23.297335Z","shell.execute_reply.started":"2023-01-16T08:51:18.760411Z","shell.execute_reply":"2023-01-16T08:51:23.295077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import zipfile\ntrain_zip = zipfile.ZipFile('/kaggle/working/train1.zip')\ntrain_zip.extractall('/kaggle/working/Dataset2')","metadata":{"execution":{"iopub.status.busy":"2023-01-16T08:51:23.299299Z","iopub.execute_input":"2023-01-16T08:51:23.299767Z","iopub.status.idle":"2023-01-16T08:51:32.477193Z","shell.execute_reply.started":"2023-01-16T08:51:23.299725Z","shell.execute_reply":"2023-01-16T08:51:32.474884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport tensorflow as tf\nimport tensorflow.keras.layers as tfl\n\nfrom tensorflow.keras.preprocessing import image_dataset_from_directory\nfrom tensorflow.keras.layers.experimental.preprocessing import RandomFlip, RandomRotation\nBATCH_SIZE = 500\nIMG_SIZE = (96, 96)\ndirectory = \"/kaggle/working/Dataset2/train1\"\ntrain_dataset = image_dataset_from_directory(directory,\n                                             shuffle=True,\n                                             batch_size=BATCH_SIZE,\n                                             image_size=IMG_SIZE,\n                                             validation_split=0.2,\n                                             subset='training',\n                                             seed=42)\nvalidation_dataset = image_dataset_from_directory(directory,\n                                             shuffle=True,\n                                             batch_size=BATCH_SIZE,\n                                             image_size=IMG_SIZE,\n                                             validation_split=0.2,\n                                             subset='validation',\n                                             seed=42)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T08:51:32.480740Z","iopub.execute_input":"2023-01-16T08:51:32.481364Z","iopub.status.idle":"2023-01-16T08:51:33.769312Z","shell.execute_reply.started":"2023-01-16T08:51:32.481313Z","shell.execute_reply":"2023-01-16T08:51:33.767095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_dataset.class_names\n\nplt.figure(figsize=(10, 10))\nfor images, labels in train_dataset.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(class_names[labels[i]])\n        plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2023-01-16T08:51:33.770964Z","iopub.execute_input":"2023-01-16T08:51:33.771396Z","iopub.status.idle":"2023-01-16T08:51:36.771347Z","shell.execute_reply.started":"2023-01-16T08:51:33.771356Z","shell.execute_reply":"2023-01-16T08:51:36.770179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.experimental.AUTOTUNE\ntrain_dataset = train_dataset.prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T08:51:36.772945Z","iopub.execute_input":"2023-01-16T08:51:36.774205Z","iopub.status.idle":"2023-01-16T08:51:36.782284Z","shell.execute_reply.started":"2023-01-16T08:51:36.774155Z","shell.execute_reply":"2023-01-16T08:51:36.780690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input","metadata":{"execution":{"iopub.status.busy":"2023-01-16T08:51:36.784288Z","iopub.execute_input":"2023-01-16T08:51:36.784684Z","iopub.status.idle":"2023-01-16T08:51:36.799555Z","shell.execute_reply.started":"2023-01-16T08:51:36.784648Z","shell.execute_reply":"2023-01-16T08:51:36.797735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input_shape=IMG_SIZE+(3,)\nbase_model = tf.keras.applications.MobileNetV2(input_shape=input_shape,\n                                                   include_top=False,weights='imagenet')\nbase_model.trainable = False\ninputs = tf.keras.Input(shape=input_shape)\nx = preprocess_input(inputs)\nx = base_model(x, training=False)\nx = tfl.GlobalAveragePooling2D()(x) \nx = tfl.Dropout(0.2)(x)\nprediction_layer = tfl.Dense(1)\noutputs = prediction_layer(x) \n#outputs=tf.keras.layers.Dense(2,activation='softmax')(x)\nmodel = tf.keras.Model(inputs, outputs)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T08:51:36.802304Z","iopub.execute_input":"2023-01-16T08:51:36.804729Z","iopub.status.idle":"2023-01-16T08:51:38.649463Z","shell.execute_reply.started":"2023-01-16T08:51:36.804674Z","shell.execute_reply":"2023-01-16T08:51:38.646804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_learning_rate = 0.01\nmodel.compile(optimizer=tf.keras.optimizers.Adam(lr=base_learning_rate),\n              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),\n              metrics=['accuracy'])\n#model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-01-16T08:51:38.652599Z","iopub.execute_input":"2023-01-16T08:51:38.653101Z","iopub.status.idle":"2023-01-16T08:51:38.672218Z","shell.execute_reply.started":"2023-01-16T08:51:38.653056Z","shell.execute_reply":"2023-01-16T08:51:38.670089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"initial_epochs = 5\nhistory = model.fit(train_dataset, validation_data=validation_dataset, epochs=initial_epochs)","metadata":{"execution":{"iopub.status.busy":"2023-01-16T08:51:38.674276Z","iopub.execute_input":"2023-01-16T08:51:38.674741Z","iopub.status.idle":"2023-01-16T08:56:34.031185Z","shell.execute_reply.started":"2023-01-16T08:51:38.674704Z","shell.execute_reply":"2023-01-16T08:56:34.029511Z"},"trusted":true},"execution_count":null,"outputs":[]}]}