{"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\n# for 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":"2021-08-07T14:28:42.462625Z","iopub.execute_input":"2021-08-07T14:28:42.46317Z","iopub.status.idle":"2021-08-07T14:28:42.482032Z","shell.execute_reply.started":"2021-08-07T14:28:42.463058Z","shell.execute_reply":"2021-08-07T14:28:42.481147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n\nimport os\ntry:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection\n    print(\"Running on TPU \", tpu.cluster_spec().as_dict()[\"worker\"])\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nexcept ValueError:\n    print(\"Not connected to a TPU runtime. Using CPU/GPU strategy\")\n    strategy = tf.distribute.MirroredStrategy()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:42.488814Z","iopub.execute_input":"2021-08-07T14:28:42.491205Z","iopub.status.idle":"2021-08-07T14:28:48.493703Z","shell.execute_reply.started":"2021-08-07T14:28:42.491166Z","shell.execute_reply":"2021-08-07T14:28:48.49293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"strategy.scope()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:48.495071Z","iopub.execute_input":"2021-08-07T14:28:48.495346Z","iopub.status.idle":"2021-08-07T14:28:48.506288Z","shell.execute_reply.started":"2021-08-07T14:28:48.495321Z","shell.execute_reply":"2021-08-07T14:28:48.50512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !conda install '/kaggle/input/pydicom-conda-helper/libjpeg-turbo-2.1.0-h7f98852_0.tar.bz2' -c conda-forge -y\n# !conda install '/kaggle/input/pydicom-conda-helper/libgcc-ng-9.3.0-h2828fa1_19.tar.bz2' -c conda-forge -y\n# !conda install '/kaggle/input/pydicom-conda-helper/gdcm-2.8.9-py37h500ead1_1.tar.bz2' -c conda-forge -y\n# !conda install '/kaggle/input/pydicom-conda-helper/conda-4.10.1-py37h89c1867_0.tar.bz2' -c conda-forge -y\n# !conda install '/kaggle/input/pydicom-conda-helper/certifi-2020.12.5-py37h89c1867_1.tar.bz2' -c conda-forge -y\n# !conda install '/kaggle/input/pydicom-conda-helper/openssl-1.1.1k-h7f98852_0.tar.bz2' -c conda-forge -y","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:48.508803Z","iopub.execute_input":"2021-08-07T14:28:48.509052Z","iopub.status.idle":"2021-08-07T14:28:48.512615Z","shell.execute_reply.started":"2021-08-07T14:28:48.509029Z","shell.execute_reply":"2021-08-07T14:28:48.51143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install efficientnet","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:48.514673Z","iopub.execute_input":"2021-08-07T14:28:48.5152Z","iopub.status.idle":"2021-08-07T14:28:48.523283Z","shell.execute_reply.started":"2021-08-07T14:28:48.515165Z","shell.execute_reply":"2021-08-07T14:28:48.522599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import efficientnet.keras as efn\nimport os\nimport shutil\nimport random\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Dense , Conv2D , Dropout , MaxPooling2D , Flatten, Activation , BatchNormalization\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.applications import EfficientNetB7\nfrom tensorflow.keras.optimizers import Adam, SGD, Adadelta\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nimport pandas as pd\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:48.524931Z","iopub.execute_input":"2021-08-07T14:28:48.525386Z","iopub.status.idle":"2021-08-07T14:28:48.656296Z","shell.execute_reply.started":"2021-08-07T14:28:48.525349Z","shell.execute_reply":"2021-08-07T14:28:48.655449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.seed(7)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:48.657598Z","iopub.execute_input":"2021-08-07T14:28:48.658024Z","iopub.status.idle":"2021-08-07T14:28:48.662582Z","shell.execute_reply.started":"2021-08-07T14:28:48.657983Z","shell.execute_reply":"2021-08-07T14:28:48.661335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess_input = tf.keras.applications.efficientnet.preprocess_input\n","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:48.664326Z","iopub.execute_input":"2021-08-07T14:28:48.665277Z","iopub.status.idle":"2021-08-07T14:28:48.672046Z","shell.execute_reply.started":"2021-08-07T14:28:48.665234Z","shell.execute_reply":"2021-08-07T14:28:48.671341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_model = EfficientNetB7(weights='imagenet',include_top=False,input_shape=(600,600,3))\n# base_model.trainable = False\n# base_model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:48.675096Z","iopub.execute_input":"2021-08-07T14:28:48.675398Z","iopub.status.idle":"2021-08-07T14:28:48.680088Z","shell.execute_reply.started":"2021-08-07T14:28:48.675374Z","shell.execute_reply":"2021-08-07T14:28:48.67902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%cd ..\n%mkdir tmp\n%cd tmp\n%ls\n","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:48.682073Z","iopub.execute_input":"2021-08-07T14:28:48.682493Z","iopub.status.idle":"2021-08-07T14:28:50.063218Z","shell.execute_reply.started":"2021-08-07T14:28:48.68246Z","shell.execute_reply":"2021-08-07T14:28:50.062288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = os.listdir('../input/efficientnetb7-dataset-augmented/augmented/train')\na","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:50.066765Z","iopub.execute_input":"2021-08-07T14:28:50.067082Z","iopub.status.idle":"2021-08-07T14:28:50.085359Z","shell.execute_reply.started":"2021-08-07T14:28:50.067054Z","shell.execute_reply":"2021-08-07T14:28:50.084519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for i in a:\n#     os.mkdir('NewPreprocessed/{}'.format(i))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:50.08668Z","iopub.execute_input":"2021-08-07T14:28:50.087063Z","iopub.status.idle":"2021-08-07T14:28:50.093222Z","shell.execute_reply.started":"2021-08-07T14:28:50.087026Z","shell.execute_reply":"2021-08-07T14:28:50.091356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shutil.copytree(\"../input/efficientnetb7-dataset-augmented/augmented/train\",\"/kaggle/tmp/NewPreprocessed\")","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:28:50.094786Z","iopub.execute_input":"2021-08-07T14:28:50.095197Z","iopub.status.idle":"2021-08-07T14:29:52.906027Z","shell.execute_reply.started":"2021-08-07T14:28:50.095158Z","shell.execute_reply":"2021-08-07T14:29:52.905265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from PIL import Image\n# import os, sys\n\n\n# def resize(path,savepath):\n#     for item in os.listdir(path):\n#         item_path = path+\"/\"+item\n#         im = Image.open(item_path)\n#         imResize = im.resize((600,600), Image.ANTIALIAS)\n#         imResize.save(savepath +\"/\"+item.split('.')[0]+ '_resized.jpg', 'JPEG')\n\n# for i in a:\n#     path = \"../input/rsna-balenced-dataset/Processed/\"+i\n#     savepath = \"NewPreprocessed/\"+i\n#     resize(path,savepath)\n    ","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:52.907377Z","iopub.execute_input":"2021-08-07T14:29:52.907716Z","iopub.status.idle":"2021-08-07T14:29:52.913986Z","shell.execute_reply.started":"2021-08-07T14:29:52.90768Z","shell.execute_reply":"2021-08-07T14:29:52.912593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%ls","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:52.915165Z","iopub.execute_input":"2021-08-07T14:29:52.915902Z","iopub.status.idle":"2021-08-07T14:29:53.656711Z","shell.execute_reply.started":"2021-08-07T14:29:52.915865Z","shell.execute_reply":"2021-08-07T14:29:53.655442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in a:\n    savepath = \"NewPreprocessed/\"\n    print(len(os.listdir(savepath+i)))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:53.6581Z","iopub.execute_input":"2021-08-07T14:29:53.658445Z","iopub.status.idle":"2021-08-07T14:29:53.684449Z","shell.execute_reply.started":"2021-08-07T14:29:53.658408Z","shell.execute_reply":"2021-08-07T14:29:53.683476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"split_size = .85\ndef split_data(SOURCE, TRAINING, VALIDATION, SPLIT_SIZE):  \n    all_images = os.listdir(SOURCE)\n    print(type(all_images))\n    random.shuffle(all_images)\n    splitting_index = round(SPLIT_SIZE*len(all_images))\n\n    #print(splitting_index)\n    #print(splitting_index+portion)\n\n    train_images = all_images[:splitting_index]\n    valid_images = all_images[splitting_index:]\n    for img in train_images:\n        shutil.copy(os.path.join(SOURCE,img),TRAINING)\n    for img in valid_images:\n        shutil.copy(os.path.join(SOURCE,img),VALIDATION)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:53.68562Z","iopub.execute_input":"2021-08-07T14:29:53.685966Z","iopub.status.idle":"2021-08-07T14:29:53.695818Z","shell.execute_reply.started":"2021-08-07T14:29:53.685933Z","shell.execute_reply":"2021-08-07T14:29:53.692763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.mkdir('train')\nos.mkdir('val')","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:53.698641Z","iopub.execute_input":"2021-08-07T14:29:53.699083Z","iopub.status.idle":"2021-08-07T14:29:53.707481Z","shell.execute_reply.started":"2021-08-07T14:29:53.69905Z","shell.execute_reply":"2021-08-07T14:29:53.705629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in a:\n    source = \"NewPreprocessed/\"+i\n    train = \"train/\"+i\n    val = \"val/\"+i\n    os.mkdir(train)\n    os.mkdir(val)\n    split_data(source, train, val, split_size)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:53.710052Z","iopub.execute_input":"2021-08-07T14:29:53.710635Z","iopub.status.idle":"2021-08-07T14:29:55.099142Z","shell.execute_reply.started":"2021-08-07T14:29:53.710601Z","shell.execute_reply":"2021-08-07T14:29:55.098303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in a:\n    train = \"train/\"+i\n    val = \"val/\"+i\n    print(len(os.listdir(train)),len(os.listdir(val)))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:55.100516Z","iopub.execute_input":"2021-08-07T14:29:55.100883Z","iopub.status.idle":"2021-08-07T14:29:55.132731Z","shell.execute_reply.started":"2021-08-07T14:29:55.100847Z","shell.execute_reply":"2021-08-07T14:29:55.131853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 64\n","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:55.134002Z","iopub.execute_input":"2021-08-07T14:29:55.134381Z","iopub.status.idle":"2021-08-07T14:29:55.147442Z","shell.execute_reply.started":"2021-08-07T14:29:55.134345Z","shell.execute_reply":"2021-08-07T14:29:55.146728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# TRAINING_DIR = 'train'\n# train_datagen = tf.keras.preprocessing.image.ImageDataGenerator()\n# train_generator =  train_datagen.flow_from_directory(TRAINING_DIR,\n#                                                       target_size=(600,600),\n#                                                       batch_size=16,\n#                                                      class_mode='categorical',\n#                                                      shuffle = True)\n# VALIDATION_DIR = 'val'\n# validation_datagen = ImageDataGenerator()\n# validation_generator =  validation_datagen.flow_from_directory(VALIDATION_DIR,\n#                                                       target_size=(600,600),\n#                                                       batch_size=16,\n#                                                       class_mode='categorical',\n#                                                       shuffle = True)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:55.148978Z","iopub.execute_input":"2021-08-07T14:29:55.149352Z","iopub.status.idle":"2021-08-07T14:29:55.155421Z","shell.execute_reply.started":"2021-08-07T14:29:55.149317Z","shell.execute_reply":"2021-08-07T14:29:55.154578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAINING_DIR = 'train'\ntrain_ds = tf.keras.preprocessing.image_dataset_from_directory(\n                                                      TRAINING_DIR,\n                                                        shuffle = True,\n                                                      image_size=(600,600),\n                                                      batch_size=batch_size,\n                                                        label_mode='categorical')\nVALIDATION_DIR = 'val'\nval_ds = tf.keras.preprocessing.image_dataset_from_directory(\n                                                      VALIDATION_DIR,\n                                                    shuffle = True,\n                                                      image_size=(600,600),\n                                                    label_mode='categorical',\n                                                      batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:55.156821Z","iopub.execute_input":"2021-08-07T14:29:55.157185Z","iopub.status.idle":"2021-08-07T14:29:56.099529Z","shell.execute_reply.started":"2021-08-07T14:29:55.157149Z","shell.execute_reply":"2021-08-07T14:29:56.098102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nclass_names","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.103314Z","iopub.execute_input":"2021-08-07T14:29:56.103571Z","iopub.status.idle":"2021-08-07T14:29:56.108736Z","shell.execute_reply.started":"2021-08-07T14:29:56.103544Z","shell.execute_reply":"2021-08-07T14:29:56.107871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.figure(figsize=(10, 10))\n# for images, labels in train_ds.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":"2021-08-07T14:29:56.111199Z","iopub.execute_input":"2021-08-07T14:29:56.112121Z","iopub.status.idle":"2021-08-07T14:29:56.12859Z","shell.execute_reply.started":"2021-08-07T14:29:56.112084Z","shell.execute_reply":"2021-08-07T14:29:56.127839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\ntrain_ds = train_ds.prefetch(buffer_size=AUTOTUNE)\nval_ds = val_ds.prefetch(buffer_size=AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.129813Z","iopub.execute_input":"2021-08-07T14:29:56.130336Z","iopub.status.idle":"2021-08-07T14:29:56.156942Z","shell.execute_reply.started":"2021-08-07T14:29:56.130299Z","shell.execute_reply":"2021-08-07T14:29:56.156167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_model = EfficientNetB7(weights='imagenet',include_top=False,input_shape=(600,600,3))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.159582Z","iopub.execute_input":"2021-08-07T14:29:56.159845Z","iopub.status.idle":"2021-08-07T14:29:56.179439Z","shell.execute_reply.started":"2021-08-07T14:29:56.159821Z","shell.execute_reply":"2021-08-07T14:29:56.178669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_model.trainable = False\n# for i in base_model.layers[-10:]:\n#     i.trainable = True\n#     print(i.trainable)\n# for i in base_model.layers[-20:]:\n#     print(i.trainable)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.180357Z","iopub.execute_input":"2021-08-07T14:29:56.180587Z","iopub.status.idle":"2021-08-07T14:29:56.20132Z","shell.execute_reply.started":"2021-08-07T14:29:56.180564Z","shell.execute_reply":"2021-08-07T14:29:56.200358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n    inputs = tf.keras.Input(shape=(600, 600, 3))\n    preprocess_input = tf.keras.applications.efficientnet.preprocess_input\n    data_augmentation = tf.keras.Sequential([\n                                              #tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'),\n                                              tf.keras.layers.experimental.preprocessing.RandomRotation(0.03),\n                                            ])\n    \n    global_average_layer = tf.keras.layers.GlobalAveragePooling2D()\n#     prediction_layer0 = tf.keras.layers.Dense(2048,activation=\"relu\")\n#     prediction_layer1 = tf.keras.layers.Dense(1024,activation=\"relu\")\n    prediction_layer2 = tf.keras.layers.Dense(512,activation=\"relu\")\n    prediction_layer3 = tf.keras.layers.Dense(128,activation=\"relu\")\n    prediction_layer4 = tf.keras.layers.Dense(4,activation=\"softmax\")\n    \n    #x = data_augmentation(inputs)\n    x = preprocess_input(inputs)\n    base_model = EfficientNetB7(weights='imagenet',include_top=False,input_tensor=x)\n    base_model.trainable = False\n    for layer in base_model.layers[-10:]:\n        if not isinstance(layer, tf.keras.layers.BatchNormalization):\n            layer.trainable = True\n    x = global_average_layer(base_model.output)\n#     x = BatchNormalization()(x)\n#     x = Dropout(0.2)(x)\n#     x = prediction_layer0(x)\n#     x = Dropout(0.2)(x)\n#     x = prediction_layer1(x)\n    x = Dropout(0.1)(x)\n    x = prediction_layer2(x)\n    x = Dropout(0.2)(x)\n    x = prediction_layer3(x)\n    outputs = prediction_layer4(x)\n    model = tf.keras.Model(inputs, outputs)\n    return(model)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:30:33.647243Z","iopub.execute_input":"2021-08-07T14:30:33.647602Z","iopub.status.idle":"2021-08-07T14:30:33.658204Z","shell.execute_reply.started":"2021-08-07T14:30:33.647569Z","shell.execute_reply":"2021-08-07T14:30:33.657124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"initial_learning_rate = 0.01\nlr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate,\n    decay_steps=5000,\n    decay_rate=0.1,\n    staircase=False)\n","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:30:34.080404Z","iopub.execute_input":"2021-08-07T14:30:34.080746Z","iopub.status.idle":"2021-08-07T14:30:34.490967Z","shell.execute_reply.started":"2021-08-07T14:30:34.080715Z","shell.execute_reply":"2021-08-07T14:30:34.489912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    Model = create_model()\n    optimizer = Adam(learning_rate=lr_schedule)\n    Model.compile(loss='categorical_crossentropy',optimizer=optimizer,metrics=['accuracy'])\n#Model = tf.keras.models.load_model(\"../input/efficientnetb7/weights/my_model.h5\")\n\nhistory = Model.fit(train_ds,epochs=50,batch_size=batch_size,validation_data=val_ds) \n\nModel.save('../working/weights/my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:30:34.492597Z","iopub.execute_input":"2021-08-07T14:30:34.493104Z","iopub.status.idle":"2021-08-07T14:59:42.693104Z","shell.execute_reply.started":"2021-08-07T14:30:34.493026Z","shell.execute_reply":"2021-08-07T14:59:42.690458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model.save('../working/weights/my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:59:46.159559Z","iopub.execute_input":"2021-08-07T14:59:46.159912Z","iopub.status.idle":"2021-08-07T14:59:47.825998Z","shell.execute_reply.started":"2021-08-07T14:59:46.159878Z","shell.execute_reply":"2021-08-07T14:59:47.825119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with strategy.scope():\n#     Model = create_model()\n#     optimizer = SGD(learning_rate=lr_schedule)\n#     Model.compile(loss='categorical_crossentropy',optimizer=optimizer,metrics=['accuracy'])\n# Model = tf.keras.models.load_model(\"../input/efficientnetb7/weights/my_model.h5\")\n\n# history = Model.fit(train_ds,epochs=10,batch_size=batch_size,validation_data=val_ds) \n\n# Model.save('../working/weights/my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.634186Z","iopub.status.idle":"2021-08-07T14:29:56.634958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with strategy.scope():\n#     Model = create_model()\n#     optimizer = Adadelta(learning_rate=lr_schedule)\n#     Model.compile(loss='categorical_crossentropy',optimizer=optimizer,metrics=['accuracy'])\n# Model = tf.keras.models.load_model(\"../input/efficientnetb7/weights/my_model.h5\")\n\n# history = Model.fit(train_ds,epochs=6,batch_size=batch_size,validation_data=val_ds) \n\n# Model.save('../working/weights/my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.636132Z","iopub.status.idle":"2021-08-07T14:29:56.63688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Model.input_shape,Model.output_shape)\n#Model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.637989Z","iopub.status.idle":"2021-08-07T14:29:56.638725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# history = Model.fit(train_generator,epochs=40,batch_size=16,validation_data=validation_generator)\n#print(len(base_model.layers))","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.639869Z","iopub.status.idle":"2021-08-07T14:29:56.640594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with strategy.scope():","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.641718Z","iopub.status.idle":"2021-08-07T14:29:56.642456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# os.mkdir(\"../working/weights\")\n# Model.save_weights(\"../working/weights/weights.pt\")","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.643576Z","iopub.status.idle":"2021-08-07T14:29:56.644321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Loading and saving","metadata":{}},{"cell_type":"code","source":"# os.rmdir(\"../working/weights\")","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.645477Z","iopub.status.idle":"2021-08-07T14:29:56.646226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model = tf.keras.models.load_model(\"../input/efficientnetb7/weights/my_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.647385Z","iopub.status.idle":"2021-08-07T14:29:56.648124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#history = Model.fit(train_ds,epochs=25,batch_size=16,validation_data=val_ds) ","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.64923Z","iopub.status.idle":"2021-08-07T14:29:56.649985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model =  tf.keras.models.load_model(\"/kaggle/input/efficientnetweights/weights/my_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.651134Z","iopub.status.idle":"2021-08-07T14:29:56.651887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model = tf.keras.models.load_model(\"/kaggle/input/efnetb7-layers-increased-more-trainable-layers/weights/my_model.h5\")","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.653003Z","iopub.status.idle":"2021-08-07T14:29:56.653748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model = create_model()\n#Model.load_weights(\"../input/efficientnetweights/weights/\")","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.654889Z","iopub.status.idle":"2021-08-07T14:29:56.655623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model.evaluate(val_ds)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.656747Z","iopub.status.idle":"2021-08-07T14:29:56.657486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def unfreeze_model(Model):\n#     # We unfreeze the top 20 layers while leaving BatchNorm layers frozen\n#     for layer in Model.layers[-30:]:\n#         if not isinstance(layer, tf.keras.layers.BatchNormalization):\n#             layer.trainable = True\n\n#     optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)\n#     Model.compile(\n#         optimizer=optimizer, loss=\"categorical_crossentropy\", metrics=[\"accuracy\"]\n#     )\n\n\n# unfreeze_model(Model)\n\n# epochs = 10  # @param {type: \"slider\", min:8, max:50}\n# hist = Model.fit(train_ds, epochs=epochs,batch_size=batch_size, validation_data=val_ds)\n# plot_hist(hist)","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.658612Z","iopub.status.idle":"2021-08-07T14:29:56.659428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.660543Z","iopub.status.idle":"2021-08-07T14:29:56.661289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model.layers","metadata":{"execution":{"iopub.status.busy":"2021-08-07T14:29:56.662443Z","iopub.status.idle":"2021-08-07T14:29:56.663166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}