{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"#!pip3 install --upgrade pip","metadata":{"execution":{"iopub.status.busy":"2023-03-26T11:01:11.113046Z","iopub.execute_input":"2023-03-26T11:01:11.113874Z","iopub.status.idle":"2023-03-26T11:01:11.142958Z","shell.execute_reply.started":"2023-03-26T11:01:11.113765Z","shell.execute_reply":"2023-03-26T11:01:11.142149Z"},"trusted":true},"execution_count":1,"outputs":[]},{"cell_type":"code","source":"!pip install opencv-python-headless\n!pip install scikit-learn","metadata":{"execution":{"iopub.status.busy":"2023-04-02T14:03:15.749671Z","iopub.execute_input":"2023-04-02T14:03:15.749968Z","iopub.status.idle":"2023-04-02T14:03:40.119305Z","shell.execute_reply.started":"2023-04-02T14:03:15.749941Z","shell.execute_reply":"2023-04-02T14:03:40.118048Z"},"trusted":true},"execution_count":1,"outputs":[{"name":"stdout","text":"Requirement already satisfied: opencv-python-headless in /opt/conda/lib/python3.7/site-packages (4.5.4.60)\nRequirement already satisfied: numpy>=1.14.5 in /opt/conda/lib/python3.7/site-packages (from opencv-python-headless) (1.21.6)\n\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n\u001b[0mRequirement already satisfied: scikit-learn in /opt/conda/lib/python3.7/site-packages (1.0.2)\nRequirement already satisfied: threadpoolctl>=2.0.0 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (3.1.0)\nRequirement already satisfied: numpy>=1.14.6 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (1.21.6)\nRequirement already satisfied: joblib>=0.11 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (1.2.0)\nRequirement already satisfied: scipy>=1.1.0 in /opt/conda/lib/python3.7/site-packages (from scikit-learn) (1.7.3)\n\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n\u001b[0m","output_type":"stream"}]},{"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport tensorflow as tf\nimport cv2\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\nfrom sklearn.utils import class_weight\nfrom sklearn.model_selection import KFold\nimport datetime\nimport scipy\nimport gc\n","metadata":{"execution":{"iopub.status.busy":"2023-04-02T14:08:34.535129Z","iopub.execute_input":"2023-04-02T14:08:34.536508Z","iopub.status.idle":"2023-04-02T14:08:34.542238Z","shell.execute_reply.started":"2023-04-02T14:08:34.536466Z","shell.execute_reply":"2023-04-02T14:08:34.541106Z"},"trusted":true},"execution_count":3,"outputs":[]},{"cell_type":"code","source":"#import timeit\n\n#device_name = tf.test.gpu_device_name()\n#if \"GPU\" not in device_name:\n#    print(\"GPU device not found\")\n#print('Found GPU at: {}'.format(device_name))","metadata":{"execution":{"iopub.status.busy":"2023-03-26T11:02:02.217489Z","iopub.execute_input":"2023-03-26T11:02:02.218022Z","iopub.status.idle":"2023-03-26T11:02:02.224978Z","shell.execute_reply.started":"2023-03-26T11:02:02.217993Z","shell.execute_reply":"2023-03-26T11:02:02.22384Z"},"trusted":true},"execution_count":4,"outputs":[]},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect(tpu=\"local\") # \"local\" for 1VM TPU\n    strategy = tf.distribute.TPUStrategy(tpu)\nexcept tf.errors.NotFoundError:\n    strategy = tf.distribute.MirroredStrategy()\n    \nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2023-04-02T14:09:21.559024Z","iopub.execute_input":"2023-04-02T14:09:21.559755Z","iopub.status.idle":"2023-04-02T14:09:26.924257Z","shell.execute_reply.started":"2023-04-02T14:09:21.559716Z","shell.execute_reply":"2023-04-02T14:09:26.922609Z"},"trusted":true},"execution_count":4,"outputs":[{"name":"stdout","text":"REPLICAS:  2\n","output_type":"stream"}]},{"cell_type":"code","source":"retina_df = pd.read_csv('/kaggle/input/diabetic-retinopathy-resized/trainLabels.csv')","metadata":{"execution":{"iopub.status.busy":"2023-04-02T14:09:31.211647Z","iopub.execute_input":"2023-04-02T14:09:31.212674Z","iopub.status.idle":"2023-04-02T14:09:31.26366Z","shell.execute_reply.started":"2023-04-02T14:09:31.212631Z","shell.execute_reply":"2023-04-02T14:09:31.262706Z"},"trusted":true},"execution_count":5,"outputs":[]},{"cell_type":"code","source":"#df_true=retina_df[retina_df['exists'] == True]","metadata":{"execution":{"iopub.status.busy":"2023-03-26T11:02:06.170123Z","iopub.execute_input":"2023-03-26T11:02:06.17048Z","iopub.status.idle":"2023-03-26T11:02:06.177144Z","shell.execute_reply.started":"2023-03-26T11:02:06.170446Z","shell.execute_reply":"2023-03-26T11:02:06.176194Z"},"trusted":true},"execution_count":7,"outputs":[]},{"cell_type":"code","source":"size=min(retina_df[\"level\"].value_counts())\n","metadata":{"execution":{"iopub.status.busy":"2023-04-02T14:09:32.801593Z","iopub.execute_input":"2023-04-02T14:09:32.802282Z","iopub.status.idle":"2023-04-02T14:09:32.831955Z","shell.execute_reply.started":"2023-04-02T14:09:32.802243Z","shell.execute_reply":"2023-04-02T14:09:32.830818Z"},"trusted":true},"execution_count":6,"outputs":[]},{"cell_type":"code","source":"df_train=pd.DataFrame()\n#df_test=pd.DataFrame()\n\nfor i in range(5):\n    temp = retina_df[retina_df['level']==i].head(1500)\n    df_train = df_train.append(temp)\n    #temp2=df_true[df_true['level']==i].tail(size//10)\n    #df_test = df_test.append(temp2)","metadata":{"execution":{"iopub.status.busy":"2023-04-02T14:09:33.986976Z","iopub.execute_input":"2023-04-02T14:09:33.987416Z","iopub.status.idle":"2023-04-02T14:09:34.008089Z","shell.execute_reply.started":"2023-04-02T14:09:33.987377Z","shell.execute_reply":"2023-04-02T14:09:34.007099Z"},"trusted":true},"execution_count":7,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2023-04-02T14:09:36.934546Z","iopub.execute_input":"2023-04-02T14:09:36.934933Z","iopub.status.idle":"2023-04-02T14:09:36.960723Z","shell.execute_reply.started":"2023-04-02T14:09:36.934899Z","shell.execute_reply":"2023-04-02T14:09:36.959804Z"},"trusted":true},"execution_count":8,"outputs":[{"execution_count":8,"output_type":"execute_result","data":{"text/plain":"             image  level\n0          10_left      0\n1         10_right      0\n2          13_left      0\n3         13_right      0\n8          17_left      0\n...            ...    ...\n34914   44100_left      4\n35046   44247_left      4\n35047  44247_right      4\n35048   44249_left      4\n35049  44249_right      4\n\n[6081 rows x 2 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>image</th>\n      <th>level</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>10_left</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>10_right</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>13_left</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>13_right</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>17_left</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>34914</th>\n      <td>44100_left</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>35046</th>\n      <td>44247_left</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>35047</th>\n      <td>44247_right</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>35048</th>\n      <td>44249_left</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>35049</th>\n      <td>44249_right</td>\n      <td>4</td>\n    </tr>\n  </tbody>\n</table>\n<p>6081 rows × 2 columns</p>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"base_image_dir = os.path.join('/kaggle/input/diabetic-retinopathy-resized/resized_train_cropped/resized_train_cropped/')\ndf_train['path'] = df_train['image'].map(lambda x: os.path.join(base_image_dir,'{}.jpeg'.format(x)))\ndf_train['exists'] = df_train['path'].map(os.path.exists) ","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2023-04-02T14:09:40.105882Z","iopub.execute_input":"2023-04-02T14:09:40.106287Z","iopub.status.idle":"2023-04-02T14:09:57.402282Z","shell.execute_reply.started":"2023-04-02T14:09:40.106256Z","shell.execute_reply":"2023-04-02T14:09:57.401264Z"},"trusted":true},"execution_count":9,"outputs":[]},{"cell_type":"code","source":"df=df_train[df_train['exists'] == True]","metadata":{"execution":{"iopub.status.busy":"2023-04-02T14:17:50.057294Z","iopub.execute_input":"2023-04-02T14:17:50.058259Z","iopub.status.idle":"2023-04-02T14:17:50.068787Z","shell.execute_reply.started":"2023-04-02T14:17:50.058206Z","shell.execute_reply":"2023-04-02T14:17:50.067717Z"},"trusted":true},"execution_count":10,"outputs":[]},{"cell_type":"code","source":"data_dir='/kaggle/input/diabetic-retinopathy-resized/resized_train_cropped/resized_train_cropped/'","metadata":{"execution":{"iopub.status.busy":"2023-04-02T14:17:52.327979Z","iopub.execute_input":"2023-04-02T14:17:52.328787Z","iopub.status.idle":"2023-04-02T14:17:52.333887Z","shell.execute_reply.started":"2023-04-02T14:17:52.328728Z","shell.execute_reply":"2023-04-02T14:17:52.332655Z"},"trusted":true},"execution_count":11,"outputs":[]},{"cell_type":"code","source":"len(df)","metadata":{"execution":{"iopub.status.busy":"2023-04-02T10:19:52.654044Z","iopub.execute_input":"2023-04-02T10:19:52.654446Z","iopub.status.idle":"2023-04-02T10:19:52.665777Z","shell.execute_reply.started":"2023-04-02T10:19:52.654409Z","shell.execute_reply":"2023-04-02T10:19:52.664646Z"},"trusted":true},"execution_count":17,"outputs":[{"execution_count":17,"output_type":"execute_result","data":{"text/plain":"6073"},"metadata":{}}]},{"cell_type":"code","source":"import cv2\nfrom PIL import Image\nimport tensorflow as tf\nimport numpy as np\n\ntf.experimental.numpy.experimental_enable_numpy_behavior(\n    prefer_float32=False\n)\n\n\n\ndef sobel_func(path_tensor):\n    kernel = np.ones((3,3),np.uint8)\n    path = path_tensor.numpy().decode('utf-8')\n    img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)\n    #img = cv2.GaussianBlur(img, (3, 3), 0)\n    sobelx = cv2.Sobel(img, cv2.CV_16S, 0 , 1, ksize=3)\n    sobely = cv2.Sobel(img, cv2.CV_16S, 1, 0, ksize=3)\n\n    sobelx=sobelx.astype(np.uint8)\n\n    #sobely=sobely.astype(np.uint8)\n    #sobelxor=cv2.bitwise_or(sobelx, sobely)\n    sobelxor=cv2.GaussianBlur(sobelx,(3,3),0)\n    sobelxor=cv2.bitwise_not(sobelx)\n    \n    result = cv2.addWeighted(img, 0.25, sobelxor,1, 0.75, dtype=cv2.CV_8U)\n    #result=tf.convert_to_tensor(result)\n    result = np.expand_dims(result, axis=-1)\n    return result","metadata":{"execution":{"iopub.status.busy":"2023-04-02T14:18:02.456871Z","iopub.execute_input":"2023-04-02T14:18:02.457647Z","iopub.status.idle":"2023-04-02T14:18:02.466065Z","shell.execute_reply.started":"2023-04-02T14:18:02.457612Z","shell.execute_reply":"2023-04-02T14:18:02.465024Z"},"trusted":true},"execution_count":12,"outputs":[]},{"cell_type":"code","source":" def load_and_preprocess_image(path):\n    #image = tf.io.read_file(path)\n    #image = tf.image.decode_jpeg(image, channels=1)\n    input_tensor = tf.constant(path)\n    image=tf.py_function(sobel_func, [input_tensor], Tout=tf.uint8)\n    image = tf.image.resize(image, [224,224])\n    image = image/255.0\n    image = tf.image.grayscale_to_rgb(image)\n    image = tf.keras.applications.vgg16.preprocess_input(image)\n    return image","metadata":{"execution":{"iopub.status.busy":"2023-04-02T10:19:57.439064Z","iopub.execute_input":"2023-04-02T10:19:57.440234Z","iopub.status.idle":"2023-04-02T10:19:57.447171Z","shell.execute_reply.started":"2023-04-02T10:19:57.440176Z","shell.execute_reply":"2023-04-02T10:19:57.445866Z"},"trusted":true},"execution_count":19,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tensorflow.keras.utils import to_categorical\nall_img_paths=df['path']\nall_image_labels=df['level']\ntrain_ds = all_img_paths.map(load_and_preprocess_image,  num_parallel_calls=AUTOTUNE)\ntrain_labels = to_categorical(all_image_labels, num_classes=5)\n#image_label_ds=tf.data.Dataset.zip((image_ds,label_ds))","metadata":{"execution":{"iopub.status.busy":"2023-04-02T10:19:59.927363Z","iopub.execute_input":"2023-04-02T10:19:59.928475Z","iopub.status.idle":"2023-04-02T10:21:58.37874Z","shell.execute_reply.started":"2023-04-02T10:19:59.928422Z","shell.execute_reply":"2023-04-02T10:21:58.377565Z"},"trusted":true},"execution_count":20,"outputs":[]},{"cell_type":"code","source":"df=df.sample(frac=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T03:59:26.894416Z","iopub.execute_input":"2023-03-27T03:59:26.894862Z","iopub.status.idle":"2023-03-27T03:59:26.902269Z","shell.execute_reply.started":"2023-03-27T03:59:26.894829Z","shell.execute_reply":"2023-03-27T03:59:26.901222Z"},"trusted":true},"execution_count":16,"outputs":[]},{"cell_type":"code","source":"df['level'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-03-27T03:59:29.178403Z","iopub.execute_input":"2023-03-27T03:59:29.178889Z","iopub.status.idle":"2023-03-27T03:59:29.187898Z","shell.execute_reply.started":"2023-03-27T03:59:29.178852Z","shell.execute_reply":"2023-03-27T03:59:29.186992Z"},"trusted":true},"execution_count":17,"outputs":[{"execution_count":17,"output_type":"execute_result","data":{"text/plain":"0    1498\n2    1498\n1    1497\n3     872\n4     708\nName: level, dtype: int64"},"metadata":{}}]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2023-04-02T10:24:34.142766Z","iopub.execute_input":"2023-04-02T10:24:34.143871Z","iopub.status.idle":"2023-04-02T10:24:34.160682Z","shell.execute_reply.started":"2023-04-02T10:24:34.143826Z","shell.execute_reply":"2023-04-02T10:24:34.159339Z"},"trusted":true},"execution_count":21,"outputs":[{"execution_count":21,"output_type":"execute_result","data":{"text/plain":"             image  level                                               path  \\\n0          10_left      0  /kaggle/input/diabetic-retinopathy-resized/res...   \n1         10_right      0  /kaggle/input/diabetic-retinopathy-resized/res...   \n2          13_left      0  /kaggle/input/diabetic-retinopathy-resized/res...   \n3         13_right      0  /kaggle/input/diabetic-retinopathy-resized/res...   \n8          17_left      0  /kaggle/input/diabetic-retinopathy-resized/res...   \n...            ...    ...                                                ...   \n34914   44100_left      4  /kaggle/input/diabetic-retinopathy-resized/res...   \n35046   44247_left      4  /kaggle/input/diabetic-retinopathy-resized/res...   \n35047  44247_right      4  /kaggle/input/diabetic-retinopathy-resized/res...   \n35048   44249_left      4  /kaggle/input/diabetic-retinopathy-resized/res...   \n35049  44249_right      4  /kaggle/input/diabetic-retinopathy-resized/res...   \n\n       exists  \n0        True  \n1        True  \n2        True  \n3        True  \n8        True  \n...       ...  \n34914    True  \n35046    True  \n35047    True  \n35048    True  \n35049    True  \n\n[6073 rows x 4 columns]","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>image</th>\n      <th>level</th>\n      <th>path</th>\n      <th>exists</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>10_left</td>\n      <td>0</td>\n      <td>/kaggle/input/diabetic-retinopathy-resized/res...</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>10_right</td>\n      <td>0</td>\n      <td>/kaggle/input/diabetic-retinopathy-resized/res...</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>13_left</td>\n      <td>0</td>\n      <td>/kaggle/input/diabetic-retinopathy-resized/res...</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>13_right</td>\n      <td>0</td>\n      <td>/kaggle/input/diabetic-retinopathy-resized/res...</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>17_left</td>\n      <td>0</td>\n      <td>/kaggle/input/diabetic-retinopathy-resized/res...</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>34914</th>\n      <td>44100_left</td>\n      <td>4</td>\n      <td>/kaggle/input/diabetic-retinopathy-resized/res...</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>35046</th>\n      <td>44247_left</td>\n      <td>4</td>\n      <td>/kaggle/input/diabetic-retinopathy-resized/res...</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>35047</th>\n      <td>44247_right</td>\n      <td>4</td>\n      <td>/kaggle/input/diabetic-retinopathy-resized/res...</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>35048</th>\n      <td>44249_left</td>\n      <td>4</td>\n      <td>/kaggle/input/diabetic-retinopathy-resized/res...</td>\n      <td>True</td>\n    </tr>\n    <tr>\n      <th>35049</th>\n      <td>44249_right</td>\n      <td>4</td>\n      <td>/kaggle/input/diabetic-retinopathy-resized/res...</td>\n      <td>True</td>\n    </tr>\n  </tbody>\n</table>\n<p>6073 rows × 4 columns</p>\n</div>"},"metadata":{}}]},{"cell_type":"code","source":"from tensorflow.keras.utils import img_to_array\nfrom keras.applications.vgg16 import preprocess_input\n\ntrain_ds = preprocess_input(np.asarray([img_to_array(img) for img in train_ds]))","metadata":{"execution":{"iopub.status.busy":"2023-04-02T10:25:31.301668Z","iopub.execute_input":"2023-04-02T10:25:31.302432Z","iopub.status.idle":"2023-04-02T10:25:37.307185Z","shell.execute_reply.started":"2023-04-02T10:25:31.302389Z","shell.execute_reply":"2023-04-02T10:25:37.306145Z"},"trusted":true},"execution_count":28,"outputs":[]},{"cell_type":"code","source":"\nfrom tensorflow.keras.applications.vgg16 import VGG16\nfrom tensorflow.keras.applications.vgg16 import preprocess_input\n\n## Loading VGG16 model\nbase_model = VGG16(weights=\"imagenet\", include_top=False, input_shape=train_ds[0].shape)\nbase_model.trainable = False ## Not trainable weights","metadata":{"execution":{"iopub.status.busy":"2023-04-02T10:24:45.407155Z","iopub.execute_input":"2023-04-02T10:24:45.40755Z","iopub.status.idle":"2023-04-02T10:24:46.220997Z","shell.execute_reply.started":"2023-04-02T10:24:45.407515Z","shell.execute_reply":"2023-04-02T10:24:46.21985Z"},"trusted":true},"execution_count":22,"outputs":[{"name":"stdout","text":"Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5\n58889256/58889256 [==============================] - 0s 0us/step\n","output_type":"stream"}]},{"cell_type":"code","source":"from tensorflow.keras import layers, models\n\nflatten_layer = layers.Flatten()\ndense_layer_1 = layers.Dense(50, activation='relu')\ndense_layer_2 = layers.Dense(20, activation='relu')\nprediction_layer = layers.Dense(5, activation='softmax')\n\n\nmodel = models.Sequential([\n    base_model,\n\n    prediction_layer\n])","metadata":{"execution":{"iopub.status.busy":"2023-04-02T10:25:49.209417Z","iopub.execute_input":"2023-04-02T10:25:49.210162Z","iopub.status.idle":"2023-04-02T10:25:49.300425Z","shell.execute_reply.started":"2023-04-02T10:25:49.210123Z","shell.execute_reply":"2023-04-02T10:25:49.29944Z"},"trusted":true},"execution_count":29,"outputs":[]},{"cell_type":"code","source":"import tensorflow_hub as hub\n    \nkeras_layer = hub.KerasLayer('https://kaggle.com/models/google/mobilenet-v3/frameworks/TensorFlow2/variations/small-100-224-classification/versions/1')\n","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping\n\nmodel.compile(\n    optimizer='adam',\n    loss='categorical_crossentropy',\n    metrics=['accuracy'],\n)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-04-02T10:25:56.398826Z","iopub.execute_input":"2023-04-02T10:25:56.399635Z","iopub.status.idle":"2023-04-02T10:25:56.414508Z","shell.execute_reply.started":"2023-04-02T10:25:56.399595Z","shell.execute_reply":"2023-04-02T10:25:56.413247Z"},"trusted":true},"execution_count":31,"outputs":[]},{"cell_type":"code","source":"len(train_ds)","metadata":{"execution":{"iopub.status.busy":"2023-03-26T11:05:20.30828Z","iopub.execute_input":"2023-03-26T11:05:20.308593Z","iopub.status.idle":"2023-03-26T11:05:20.314613Z","shell.execute_reply.started":"2023-03-26T11:05:20.308561Z","shell.execute_reply":"2023-03-26T11:05:20.313659Z"},"trusted":true},"execution_count":25,"outputs":[{"execution_count":25,"output_type":"execute_result","data":{"text/plain":"7571"},"metadata":{}}]},{"cell_type":"code","source":"train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"es = EarlyStopping(monitor='val_loss', mode='max', patience=5,  restore_best_weights=True)\n#train_fin=train_ds.data.Dataset.from_tensor_slices(all_digits)\nmodel.fit(train_ds, train_labels, epochs=25, validation_split=0.30, batch_size=32,  shuffle=True)\n","metadata":{"execution":{"iopub.status.busy":"2023-04-02T10:25:58.346908Z","iopub.execute_input":"2023-04-02T10:25:58.347669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_folds = 10\nkf = KFold(n_splits=num_folds, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T03:59:57.780696Z","iopub.execute_input":"2023-03-27T03:59:57.781176Z","iopub.status.idle":"2023-03-27T03:59:57.786144Z","shell.execute_reply.started":"2023-03-27T03:59:57.781136Z","shell.execute_reply":"2023-03-27T03:59:57.785227Z"},"trusted":true},"execution_count":22,"outputs":[]},{"cell_type":"code","source":"\nfrom sklearn.metrics import accuracy_score\n\nmetrics = ['accuracy']\neval_metrics = []\nfor i in range(len(train_labels)):\n    test_image = train_ds[i]\n    test_label = train_labels[i]\n    \n    train_images = tf.concat([train_ds[:i], train_ds[i+1:]], axis=0)\n    train_labels = tf.concat([train_labels[:i], train_labels[i+1:]], axis=0)\n    \n    # Train the VGG-16 model on the training data\n    model.fit(train_images, train_labels)\n    \n    # Evaluate the VGG-16 model on the test data\n    y_pred = model.predict(test_image)\n    y_pred_label = tf.argmax(y_pred, axis=1)\n    eval_metric = accuracy_score([test_label], y_pred_label)\n    eval_metrics.append(eval_metric)\n\n# Compute the final evaluation metric\nfinal_eval_metric = sum(eval_metrics) / len(eval_metrics)\nprint('Final evaluation metric:', final_eval_metric)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i, (train_index, val_index) in enumerate(kf.split(train_ds)):\n    # Print the current fold number\n    print(f'Fold {i+1}/{num_folds}')\n\n    # Split the data into training and validation sets for this fold\n    X_train, X_val = train_ds[train_index], train_ds[val_index]\n    y_train, y_val = train_labels[train_index], train_labels[val_index]\n\n    model.fit(X_train, y_train, epochs=10, batch_size=32, verbose=1)\n\n    # Evaluate the model on the validation data for this fold\n    scores = model.evaluate(X_val, y_val, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2023-03-27T04:00:00.763553Z","iopub.execute_input":"2023-03-27T04:00:00.76464Z","iopub.status.idle":"2023-03-27T04:54:49.013028Z","shell.execute_reply.started":"2023-03-27T04:00:00.764593Z","shell.execute_reply":"2023-03-27T04:54:49.011402Z"},"trusted":true},"execution_count":23,"outputs":[{"name":"stdout","text":"Fold 1/10\nEpoch 1/10\n171/171 [==============================] - 99s 570ms/step - loss: 1.6050 - accuracy: 0.2384\nEpoch 2/10\n171/171 [==============================] - 98s 573ms/step - loss: 1.5780 - accuracy: 0.2602\nEpoch 3/10\n171/171 [==============================] - 99s 577ms/step - loss: 1.5628 - accuracy: 0.2490\nEpoch 4/10\n171/171 [==============================] - 99s 580ms/step - loss: 1.5589 - accuracy: 0.2633\nEpoch 5/10\n171/171 [==============================] - 99s 582ms/step - loss: 1.5594 - accuracy: 0.2565\nEpoch 6/10\n171/171 [==============================] - 99s 582ms/step - loss: 1.5514 - accuracy: 0.2609\nEpoch 7/10\n171/171 [==============================] - 99s 578ms/step - loss: 1.5457 - accuracy: 0.2633\nEpoch 8/10\n171/171 [==============================] - 99s 582ms/step - loss: 1.5495 - accuracy: 0.2589\nEpoch 9/10\n171/171 [==============================] - 99s 581ms/step - loss: 1.5552 - accuracy: 0.2564\nEpoch 10/10\n171/171 [==============================] - 99s 579ms/step - loss: 1.5462 - accuracy: 0.2620\n19/19 [==============================] - 11s 569ms/step - loss: 1.5531 - accuracy: 0.2385\nFold 2/10\nEpoch 1/10\n171/171 [==============================] - 98s 573ms/step - loss: 1.5429 - accuracy: 0.2586\nEpoch 2/10\n171/171 [==============================] - 98s 575ms/step - loss: 1.5480 - accuracy: 0.2606\nEpoch 3/10\n171/171 [==============================] - 99s 580ms/step - loss: 1.5410 - accuracy: 0.2662\nEpoch 4/10\n171/171 [==============================] - 99s 578ms/step - loss: 1.5409 - accuracy: 0.2635\nEpoch 5/10\n171/171 [==============================] - 99s 578ms/step - loss: 1.5441 - accuracy: 0.2560\nEpoch 6/10\n171/171 [==============================] - 99s 580ms/step - loss: 1.5454 - accuracy: 0.2565\nEpoch 7/10\n171/171 [==============================] - 100s 583ms/step - loss: 1.5387 - accuracy: 0.2725\nEpoch 8/10\n171/171 [==============================] - 99s 577ms/step - loss: 1.5442 - accuracy: 0.2454\nEpoch 9/10\n171/171 [==============================] - 99s 580ms/step - loss: 1.5487 - accuracy: 0.2532\nEpoch 10/10\n171/171 [==============================] - 100s 584ms/step - loss: 1.5571 - accuracy: 0.2454\n19/19 [==============================] - 11s 587ms/step - loss: 1.5394 - accuracy: 0.2747\nFold 3/10\nEpoch 1/10\n171/171 [==============================] - 99s 577ms/step - loss: 1.5435 - accuracy: 0.2569\nEpoch 2/10\n171/171 [==============================] - 98s 576ms/step - loss: 1.5453 - accuracy: 0.2618\nEpoch 3/10\n171/171 [==============================] - 99s 577ms/step - loss: 1.5391 - accuracy: 0.2650\nEpoch 4/10\n171/171 [==============================] - 100s 583ms/step - loss: 1.5382 - accuracy: 0.2794\nEpoch 5/10\n171/171 [==============================] - 100s 584ms/step - loss: 1.5329 - accuracy: 0.2690\nEpoch 6/10\n171/171 [==============================] - 101s 589ms/step - loss: 1.5359 - accuracy: 0.2668\nEpoch 7/10\n171/171 [==============================] - 100s 587ms/step - loss: 1.5414 - accuracy: 0.2668\nEpoch 8/10\n171/171 [==============================] - 100s 587ms/step - loss: 1.5429 - accuracy: 0.2633\nEpoch 9/10\n171/171 [==============================] - 100s 586ms/step - loss: 1.5365 - accuracy: 0.2748\nEpoch 10/10\n171/171 [==============================] - 100s 585ms/step - loss: 1.5314 - accuracy: 0.2736\n19/19 [==============================] - 11s 579ms/step - loss: 1.5208 - accuracy: 0.2549\nFold 4/10\nEpoch 1/10\n171/171 [==============================] - 99s 579ms/step - loss: 1.5388 - accuracy: 0.2587\nEpoch 2/10\n171/171 [==============================] - 99s 577ms/step - loss: 1.5371 - accuracy: 0.2613\nEpoch 3/10\n108/171 [=================>............] - ETA: 36s - loss: 1.5276 - accuracy: 0.2677","output_type":"stream"},{"traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)","Cell \u001b[0;32mIn[23], line 9\u001b[0m\n\u001b[1;32m      6\u001b[0m X_train, X_val \u001b[38;5;241m=\u001b[39m train_ds[train_index], train_ds[val_index]\n\u001b[1;32m      7\u001b[0m y_train, y_val \u001b[38;5;241m=\u001b[39m train_labels[train_index], train_labels[val_index]\n\u001b[0;32m----> 9\u001b[0m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfit\u001b[49m\u001b[43m(\u001b[49m\u001b[43mX_train\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my_train\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mepochs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mbatch_size\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m32\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mverbose\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m     11\u001b[0m \u001b[38;5;66;03m# Evaluate the model on the validation data for this fold\u001b[39;00m\n\u001b[1;32m     12\u001b[0m scores \u001b[38;5;241m=\u001b[39m model\u001b[38;5;241m.\u001b[39mevaluate(X_val, y_val, verbose\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m)\n","File \u001b[0;32m/usr/local/lib/python3.8/site-packages/keras/utils/traceback_utils.py:65\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m     63\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m     64\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 65\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     66\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m     67\u001b[0m     filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n","File \u001b[0;32m/usr/local/lib/python3.8/site-packages/keras/engine/training.py:1650\u001b[0m, in \u001b[0;36mModel.fit\u001b[0;34m(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)\u001b[0m\n\u001b[1;32m   1642\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m tf\u001b[38;5;241m.\u001b[39mprofiler\u001b[38;5;241m.\u001b[39mexperimental\u001b[38;5;241m.\u001b[39mTrace(\n\u001b[1;32m   1643\u001b[0m     \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtrain\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m   1644\u001b[0m     epoch_num\u001b[38;5;241m=\u001b[39mepoch,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   1647\u001b[0m     _r\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m1\u001b[39m,\n\u001b[1;32m   1648\u001b[0m ):\n\u001b[1;32m   1649\u001b[0m     callbacks\u001b[38;5;241m.\u001b[39mon_train_batch_begin(step)\n\u001b[0;32m-> 1650\u001b[0m     tmp_logs \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtrain_function\u001b[49m\u001b[43m(\u001b[49m\u001b[43miterator\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1651\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m data_handler\u001b[38;5;241m.\u001b[39mshould_sync:\n\u001b[1;32m   1652\u001b[0m         context\u001b[38;5;241m.\u001b[39masync_wait()\n","File \u001b[0;32m/usr/local/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py:150\u001b[0m, in \u001b[0;36mfilter_traceback.<locals>.error_handler\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m    148\u001b[0m filtered_tb \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m    149\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 150\u001b[0m   \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mfn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    151\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    152\u001b[0m   filtered_tb \u001b[38;5;241m=\u001b[39m _process_traceback_frames(e\u001b[38;5;241m.\u001b[39m__traceback__)\n","File \u001b[0;32m/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py:880\u001b[0m, in \u001b[0;36mFunction.__call__\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m    877\u001b[0m compiler \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mxla\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_jit_compile \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mnonXla\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    879\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m OptionalXlaContext(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_jit_compile):\n\u001b[0;32m--> 880\u001b[0m   result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    882\u001b[0m new_tracing_count \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mexperimental_get_tracing_count()\n\u001b[1;32m    883\u001b[0m without_tracing \u001b[38;5;241m=\u001b[39m (tracing_count \u001b[38;5;241m==\u001b[39m new_tracing_count)\n","File \u001b[0;32m/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py:912\u001b[0m, in \u001b[0;36mFunction._call\u001b[0;34m(self, *args, **kwds)\u001b[0m\n\u001b[1;32m    909\u001b[0m   \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lock\u001b[38;5;241m.\u001b[39mrelease()\n\u001b[1;32m    910\u001b[0m   \u001b[38;5;66;03m# In this case we have created variables on the first call, so we run the\u001b[39;00m\n\u001b[1;32m    911\u001b[0m   \u001b[38;5;66;03m# defunned version which is guaranteed to never create variables.\u001b[39;00m\n\u001b[0;32m--> 912\u001b[0m   \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_no_variable_creation_fn\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwds\u001b[49m\u001b[43m)\u001b[49m  \u001b[38;5;66;03m# pylint: disable=not-callable\u001b[39;00m\n\u001b[1;32m    913\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_variable_creation_fn \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m    914\u001b[0m   \u001b[38;5;66;03m# Release the lock early so that multiple threads can perform the call\u001b[39;00m\n\u001b[1;32m    915\u001b[0m   \u001b[38;5;66;03m# in parallel.\u001b[39;00m\n\u001b[1;32m    916\u001b[0m   \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lock\u001b[38;5;241m.\u001b[39mrelease()\n","File \u001b[0;32m/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/polymorphic_function/tracing_compiler.py:134\u001b[0m, in \u001b[0;36mTracingCompiler.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m    131\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_lock:\n\u001b[1;32m    132\u001b[0m   (concrete_function,\n\u001b[1;32m    133\u001b[0m    filtered_flat_args) \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_maybe_define_function(args, kwargs)\n\u001b[0;32m--> 134\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mconcrete_function\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_call_flat\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    135\u001b[0m \u001b[43m    \u001b[49m\u001b[43mfiltered_flat_args\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcaptured_inputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mconcrete_function\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcaptured_inputs\u001b[49m\u001b[43m)\u001b[49m\n","File \u001b[0;32m/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/polymorphic_function/monomorphic_function.py:1745\u001b[0m, in \u001b[0;36mConcreteFunction._call_flat\u001b[0;34m(self, args, captured_inputs, cancellation_manager)\u001b[0m\n\u001b[1;32m   1741\u001b[0m possible_gradient_type \u001b[38;5;241m=\u001b[39m gradients_util\u001b[38;5;241m.\u001b[39mPossibleTapeGradientTypes(args)\n\u001b[1;32m   1742\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (possible_gradient_type \u001b[38;5;241m==\u001b[39m gradients_util\u001b[38;5;241m.\u001b[39mPOSSIBLE_GRADIENT_TYPES_NONE\n\u001b[1;32m   1743\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m executing_eagerly):\n\u001b[1;32m   1744\u001b[0m   \u001b[38;5;66;03m# No tape is watching; skip to running the function.\u001b[39;00m\n\u001b[0;32m-> 1745\u001b[0m   \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_build_call_outputs(\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_inference_function\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcall\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   1746\u001b[0m \u001b[43m      \u001b[49m\u001b[43mctx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcancellation_manager\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcancellation_manager\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m   1747\u001b[0m forward_backward \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_select_forward_and_backward_functions(\n\u001b[1;32m   1748\u001b[0m     args,\n\u001b[1;32m   1749\u001b[0m     possible_gradient_type,\n\u001b[1;32m   1750\u001b[0m     executing_eagerly)\n\u001b[1;32m   1751\u001b[0m forward_function, args_with_tangents \u001b[38;5;241m=\u001b[39m forward_backward\u001b[38;5;241m.\u001b[39mforward()\n","File \u001b[0;32m/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/polymorphic_function/monomorphic_function.py:378\u001b[0m, in \u001b[0;36m_EagerDefinedFunction.call\u001b[0;34m(self, ctx, args, cancellation_manager)\u001b[0m\n\u001b[1;32m    376\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m _InterpolateFunctionError(\u001b[38;5;28mself\u001b[39m):\n\u001b[1;32m    377\u001b[0m   \u001b[38;5;28;01mif\u001b[39;00m cancellation_manager \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m--> 378\u001b[0m     outputs \u001b[38;5;241m=\u001b[39m \u001b[43mexecute\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m    379\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msignature\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    380\u001b[0m \u001b[43m        \u001b[49m\u001b[43mnum_outputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_num_outputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    381\u001b[0m \u001b[43m        \u001b[49m\u001b[43minputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    382\u001b[0m \u001b[43m        \u001b[49m\u001b[43mattrs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mattrs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m    383\u001b[0m \u001b[43m        \u001b[49m\u001b[43mctx\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mctx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    384\u001b[0m   \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m    385\u001b[0m     outputs \u001b[38;5;241m=\u001b[39m execute\u001b[38;5;241m.\u001b[39mexecute_with_cancellation(\n\u001b[1;32m    386\u001b[0m         \u001b[38;5;28mstr\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msignature\u001b[38;5;241m.\u001b[39mname),\n\u001b[1;32m    387\u001b[0m         num_outputs\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_num_outputs,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    390\u001b[0m         ctx\u001b[38;5;241m=\u001b[39mctx,\n\u001b[1;32m    391\u001b[0m         cancellation_manager\u001b[38;5;241m=\u001b[39mcancellation_manager)\n","File \u001b[0;32m/usr/local/lib/python3.8/site-packages/tensorflow/python/eager/execute.py:52\u001b[0m, in \u001b[0;36mquick_execute\u001b[0;34m(op_name, num_outputs, inputs, attrs, ctx, name)\u001b[0m\n\u001b[1;32m     50\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m     51\u001b[0m   ctx\u001b[38;5;241m.\u001b[39mensure_initialized()\n\u001b[0;32m---> 52\u001b[0m   tensors \u001b[38;5;241m=\u001b[39m \u001b[43mpywrap_tfe\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mTFE_Py_Execute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mctx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_handle\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdevice_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     53\u001b[0m \u001b[43m                                      \u001b[49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mattrs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnum_outputs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     54\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m core\u001b[38;5;241m.\u001b[39m_NotOkStatusException \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m     55\u001b[0m   \u001b[38;5;28;01mif\u001b[39;00m name \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n","\u001b[0;31mKeyboardInterrupt\u001b[0m: "],"ename":"KeyboardInterrupt","evalue":"","output_type":"error"}]},{"cell_type":"code","source":"#scores = model.evaluate(X_val, y_val, verbose=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"len(train_labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"AUTOTUNE = tf.data.experimental.AUTOTUNE","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count=size*5\nBATCH_SIZE=16\nds=image_label_ds.shuffle(count)\nds=ds.repeat()\nds=ds.batch(BATCH_SIZE)\nds=ds.prefetch(buffer_size=AUTOTUNE)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mobile_net = tf.keras.applications.MobileNetV2(input_shape=(256,256, 3),include_top=False)\nmobile_net.trainable=False","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def change_range(image,label):\n  return 2*image-1, label\n\nkeras_ds = ds.map(change_range)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_batch, label_batch = next(iter(keras_ds))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"feature_map_batch = mobile_net(image_batch)\nprint(feature_map_batch.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n  mobile_net,\n  tf.keras.layers.GlobalAveragePooling2D(),\n  tf.keras.layers.Dense(count, activation = 'softmax')])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logit_batch = model(image_batch).numpy()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.optimizers.Adam(),\n              loss=tf.keras.losses.sparse_categorical_crossentropy,\n              metrics=[\"accuracy\"])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"steps_per_epoch=(count//BATCH_SIZE)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(ds, epochs=10, steps_per_epoch=steps_per_epoch)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import keras,os\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, MaxPool2D , Flatten\nfrom keras.preprocessing.image import ImageDataGenerator\nimport numpy as np","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(input_shape=(224,224,3),filters=64,kernel_size=(3,3),padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=64,kernel_size=(3,3),padding=\"same\", activation=\"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\nmodel.add(Conv2D(filters=128, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=128, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\nmodel.add(Conv2D(filters=256, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=256, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=256, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Flatten())\nmodel.add(Dense(units=4096,activation=\"relu\"))\nmodel.add(Dense(units=4096,activation=\"relu\"))\nmodel.add(Dense(units=2, activation=\"softmax\"))\nfrom keras.optimizers import Adam\nopt = Adam(learning_rate=0.001)\nmodel.compile(optimizer=opt, loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint, EarlyStopping\ncheckpoint = ModelCheckpoint(\"vgg16_1.h5\", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)\nearly = EarlyStopping(monitor='val_acc', min_delta=0, patience=20, verbose=1, mode='auto')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit_generator(steps_per_epoch=100,generator=image_label_ds, validation_steps=10,epochs=15,callbacks=[checkpoint,early])","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}