{"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-07-29T07:32:31.960841Z","iopub.execute_input":"2021-07-29T07:32:31.961148Z","iopub.status.idle":"2021-07-29T07:32:31.971589Z","shell.execute_reply.started":"2021-07-29T07:32:31.961072Z","shell.execute_reply":"2021-07-29T07:32:31.970708Z"},"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-07-29T07:32:31.979803Z","iopub.execute_input":"2021-07-29T07:32:31.980036Z","iopub.status.idle":"2021-07-29T07:32:37.832750Z","shell.execute_reply.started":"2021-07-29T07:32:31.980014Z","shell.execute_reply":"2021-07-29T07:32:37.831905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"strategy.scope()","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:32:37.834242Z","iopub.execute_input":"2021-07-29T07:32:37.834668Z","iopub.status.idle":"2021-07-29T07:32:37.846874Z","shell.execute_reply.started":"2021-07-29T07:32:37.834616Z","shell.execute_reply":"2021-07-29T07:32:37.846157Z"},"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-07-29T07:32:37.848489Z","iopub.execute_input":"2021-07-29T07:32:37.848847Z","iopub.status.idle":"2021-07-29T07:32:37.852575Z","shell.execute_reply.started":"2021-07-29T07:32:37.848812Z","shell.execute_reply":"2021-07-29T07:32:37.851438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install efficientnet","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:32:37.854585Z","iopub.execute_input":"2021-07-29T07:32:37.855022Z","iopub.status.idle":"2021-07-29T07:32:37.864038Z","shell.execute_reply.started":"2021-07-29T07:32:37.854979Z","shell.execute_reply":"2021-07-29T07:32:37.863203Z"},"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\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-07-29T07:32:37.865366Z","iopub.execute_input":"2021-07-29T07:32:37.865787Z","iopub.status.idle":"2021-07-29T07:32:38.010379Z","shell.execute_reply.started":"2021-07-29T07:32:37.865754Z","shell.execute_reply":"2021-07-29T07:32:38.009553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random.seed(7)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:32:38.011549Z","iopub.execute_input":"2021-07-29T07:32:38.011909Z","iopub.status.idle":"2021-07-29T07:32:38.018531Z","shell.execute_reply.started":"2021-07-29T07:32:38.011873Z","shell.execute_reply":"2021-07-29T07:32:38.017703Z"},"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-07-29T07:32:38.019683Z","iopub.execute_input":"2021-07-29T07:32:38.020018Z","iopub.status.idle":"2021-07-29T07:32:38.028493Z","shell.execute_reply.started":"2021-07-29T07:32:38.019983Z","shell.execute_reply":"2021-07-29T07:32:38.027602Z"},"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-07-29T07:32:38.032637Z","iopub.execute_input":"2021-07-29T07:32:38.032995Z","iopub.status.idle":"2021-07-29T07:32:38.038599Z","shell.execute_reply.started":"2021-07-29T07:32:38.032963Z","shell.execute_reply":"2021-07-29T07:32:38.037738Z"},"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-07-29T07:32:38.042224Z","iopub.execute_input":"2021-07-29T07:32:38.042461Z","iopub.status.idle":"2021-07-29T07:32:39.438289Z","shell.execute_reply.started":"2021-07-29T07:32:38.042438Z","shell.execute_reply":"2021-07-29T07:32:39.437283Z"},"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-07-29T07:32:39.439782Z","iopub.execute_input":"2021-07-29T07:32:39.440279Z","iopub.status.idle":"2021-07-29T07:32:39.457350Z","shell.execute_reply.started":"2021-07-29T07:32:39.440235Z","shell.execute_reply":"2021-07-29T07:32:39.456446Z"},"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-07-29T07:32:39.460837Z","iopub.execute_input":"2021-07-29T07:32:39.461173Z","iopub.status.idle":"2021-07-29T07:32:39.465173Z","shell.execute_reply.started":"2021-07-29T07:32:39.461139Z","shell.execute_reply":"2021-07-29T07:32:39.464271Z"},"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-07-29T07:32:39.467788Z","iopub.execute_input":"2021-07-29T07:32:39.469349Z","iopub.status.idle":"2021-07-29T07:33:44.516843Z","shell.execute_reply.started":"2021-07-29T07:32:39.469311Z","shell.execute_reply":"2021-07-29T07:33:44.515906Z"},"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-07-29T07:33:44.518110Z","iopub.execute_input":"2021-07-29T07:33:44.518440Z","iopub.status.idle":"2021-07-29T07:33:44.522141Z","shell.execute_reply.started":"2021-07-29T07:33:44.518401Z","shell.execute_reply":"2021-07-29T07:33:44.521379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%ls","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:33:44.523287Z","iopub.execute_input":"2021-07-29T07:33:44.524340Z","iopub.status.idle":"2021-07-29T07:33:45.267330Z","shell.execute_reply.started":"2021-07-29T07:33:44.524297Z","shell.execute_reply":"2021-07-29T07:33:45.266339Z"},"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-07-29T07:33:45.271352Z","iopub.execute_input":"2021-07-29T07:33:45.271710Z","iopub.status.idle":"2021-07-29T07:33:45.286846Z","shell.execute_reply.started":"2021-07-29T07:33:45.271672Z","shell.execute_reply":"2021-07-29T07:33:45.285552Z"},"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-07-29T07:33:45.288685Z","iopub.execute_input":"2021-07-29T07:33:45.289070Z","iopub.status.idle":"2021-07-29T07:33:45.297584Z","shell.execute_reply.started":"2021-07-29T07:33:45.289021Z","shell.execute_reply":"2021-07-29T07:33:45.296603Z"},"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-07-29T07:33:45.298994Z","iopub.execute_input":"2021-07-29T07:33:45.299348Z","iopub.status.idle":"2021-07-29T07:33:45.305389Z","shell.execute_reply.started":"2021-07-29T07:33:45.299311Z","shell.execute_reply":"2021-07-29T07:33:45.304293Z"},"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-07-29T07:33:45.307083Z","iopub.execute_input":"2021-07-29T07:33:45.307465Z","iopub.status.idle":"2021-07-29T07:33:46.590843Z","shell.execute_reply.started":"2021-07-29T07:33:45.307427Z","shell.execute_reply":"2021-07-29T07:33:46.589988Z"},"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-07-29T07:33:46.592218Z","iopub.execute_input":"2021-07-29T07:33:46.592587Z","iopub.status.idle":"2021-07-29T07:33:46.625334Z","shell.execute_reply.started":"2021-07-29T07:33:46.592548Z","shell.execute_reply":"2021-07-29T07:33:46.624387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 16","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:33:46.626809Z","iopub.execute_input":"2021-07-29T07:33:46.627244Z","iopub.status.idle":"2021-07-29T07:33:46.642778Z","shell.execute_reply.started":"2021-07-29T07:33:46.627201Z","shell.execute_reply":"2021-07-29T07:33:46.641919Z"},"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-07-29T07:33:46.644159Z","iopub.execute_input":"2021-07-29T07:33:46.644576Z","iopub.status.idle":"2021-07-29T07:33:46.650861Z","shell.execute_reply.started":"2021-07-29T07:33:46.644498Z","shell.execute_reply":"2021-07-29T07:33:46.649850Z"},"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-07-29T07:33:46.652493Z","iopub.execute_input":"2021-07-29T07:33:46.652894Z","iopub.status.idle":"2021-07-29T07:33:47.595813Z","shell.execute_reply.started":"2021-07-29T07:33:46.652855Z","shell.execute_reply":"2021-07-29T07:33:47.595010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nclass_names","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:33:47.599037Z","iopub.execute_input":"2021-07-29T07:33:47.599289Z","iopub.status.idle":"2021-07-29T07:33:47.604832Z","shell.execute_reply.started":"2021-07-29T07:33:47.599263Z","shell.execute_reply":"2021-07-29T07:33:47.603783Z"},"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-07-29T07:33:47.606622Z","iopub.execute_input":"2021-07-29T07:33:47.607263Z","iopub.status.idle":"2021-07-29T07:33:47.612429Z","shell.execute_reply.started":"2021-07-29T07:33:47.607222Z","shell.execute_reply":"2021-07-29T07:33:47.611450Z"},"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-07-29T07:33:47.613803Z","iopub.execute_input":"2021-07-29T07:33:47.614372Z","iopub.status.idle":"2021-07-29T07:33:47.625970Z","shell.execute_reply.started":"2021-07-29T07:33:47.614334Z","shell.execute_reply":"2021-07-29T07:33:47.625109Z"},"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-07-29T07:33:47.627211Z","iopub.execute_input":"2021-07-29T07:33:47.627826Z","iopub.status.idle":"2021-07-29T07:33:47.631922Z","shell.execute_reply.started":"2021-07-29T07:33:47.627785Z","shell.execute_reply":"2021-07-29T07:33:47.630919Z"},"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-07-29T07:33:47.633753Z","iopub.execute_input":"2021-07-29T07:33:47.634122Z","iopub.status.idle":"2021-07-29T07:33:47.640005Z","shell.execute_reply.started":"2021-07-29T07:33:47.634085Z","shell.execute_reply":"2021-07-29T07:33:47.638887Z"},"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.3),\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(x)\n    base_model = EfficientNetB7(weights='imagenet',include_top=False,input_tensor=x)\n    base_model.trainable = False\n    for layer in base_model.layers[-20:]:\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.2)(x)\n    x = prediction_layer2(x)\n    x = Dropout(0.2)(x)\n    x = prediction_layer3(x)\n    x = Dropout(0.15)(x)   \n    outputs = prediction_layer4(x)\n    model = tf.keras.Model(inputs, outputs)\n    return(model)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:33:47.641707Z","iopub.execute_input":"2021-07-29T07:33:47.642128Z","iopub.status.idle":"2021-07-29T07:33:47.653586Z","shell.execute_reply.started":"2021-07-29T07:33:47.642091Z","shell.execute_reply":"2021-07-29T07:33:47.652596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"with strategy.scope():\n    Model = create_model()\n    optimizer = Adam(learning_rate=0.001)\n    Model.compile(loss='categorical_crossentropy',optimizer=optimizer,metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-07-25T15:50:39.473982Z","iopub.execute_input":"2021-07-25T15:50:39.474339Z","iopub.status.idle":"2021-07-25T15:50:50.150599Z","shell.execute_reply.started":"2021-07-25T15:50:39.474309Z","shell.execute_reply":"2021-07-25T15:50:50.149634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(Model.input_shape,Model.output_shape)\nModel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-21T09:52:28.800102Z","iopub.execute_input":"2021-07-21T09:52:28.80043Z","iopub.status.idle":"2021-07-21T09:52:29.153054Z","shell.execute_reply.started":"2021-07-21T09:52:28.800396Z","shell.execute_reply":"2021-07-21T09:52:29.152242Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"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)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:48:31.009943Z","iopub.execute_input":"2021-07-16T09:48:31.010238Z","iopub.status.idle":"2021-07-16T09:48:31.01763Z","shell.execute_reply.started":"2021-07-16T09:48:31.010208Z","shell.execute_reply":"2021-07-16T09:48:31.01634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# with strategy.scope():\nhistory = Model.fit(train_ds,epochs=25,batch_size=16,validation_data=val_ds) ","metadata":{"execution":{"iopub.status.busy":"2021-07-25T15:50:50.152212Z","iopub.execute_input":"2021-07-25T15:50:50.152575Z","iopub.status.idle":"2021-07-25T18:32:16.983416Z","shell.execute_reply.started":"2021-07-25T15:50:50.152536Z","shell.execute_reply":"2021-07-25T18:32:16.980983Z"},"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-07-11T16:19:13.807224Z","iopub.execute_input":"2021-07-11T16:19:13.807676Z","iopub.status.idle":"2021-07-11T16:19:14.965043Z","shell.execute_reply.started":"2021-07-11T16:19:13.80764Z","shell.execute_reply":"2021-07-11T16:19:14.964063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Loading and saving","metadata":{}},{"cell_type":"code","source":"# os.rmdir(\"../working/weights\")\nModel.save('../working/weights/my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2021-07-29T08:48:49.034691Z","iopub.execute_input":"2021-07-29T08:48:49.035037Z","iopub.status.idle":"2021-07-29T08:48:50.911820Z","shell.execute_reply.started":"2021-07-29T08:48:49.035003Z","shell.execute_reply":"2021-07-29T08:48:50.910673Z"},"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-07-29T07:33:47.654786Z","iopub.execute_input":"2021-07-29T07:33:47.655404Z","iopub.status.idle":"2021-07-29T07:34:00.243413Z","shell.execute_reply.started":"2021-07-29T07:33:47.655273Z","shell.execute_reply":"2021-07-29T07:34:00.242521Z"},"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-07-26T08:32:41.291464Z","iopub.execute_input":"2021-07-26T08:32:41.291807Z","iopub.status.idle":"2021-07-26T09:55:32.830522Z","shell.execute_reply.started":"2021-07-26T08:32:41.291777Z","shell.execute_reply":"2021-07-26T09:55:32.827238Z"},"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-07-24T08:25:40.0639Z","iopub.execute_input":"2021-07-24T08:25:40.064295Z","iopub.status.idle":"2021-07-24T08:25:51.468071Z","shell.execute_reply.started":"2021-07-24T08:25:40.064263Z","shell.execute_reply":"2021-07-24T08:25:51.467189Z"},"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-07-24T08:27:24.75391Z","iopub.execute_input":"2021-07-24T08:27:24.75432Z","iopub.status.idle":"2021-07-24T08:27:37.692447Z","shell.execute_reply.started":"2021-07-24T08:27:24.75428Z","shell.execute_reply":"2021-07-24T08:27:37.69153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Model = create_model()\nModel.load_weights(\"../input/efficientnetweights/weights/\")","metadata":{"execution":{"iopub.status.busy":"2021-07-11T17:20:30.927435Z","iopub.execute_input":"2021-07-11T17:20:30.927954Z","iopub.status.idle":"2021-07-11T17:20:38.55583Z","shell.execute_reply.started":"2021-07-11T17:20:30.92791Z","shell.execute_reply":"2021-07-11T17:20:38.553785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model.evaluate(val_ds)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T07:15:31.091343Z","iopub.execute_input":"2021-07-26T07:15:31.091762Z","iopub.status.idle":"2021-07-26T07:16:32.810059Z","shell.execute_reply.started":"2021-07-26T07:15:31.091723Z","shell.execute_reply":"2021-07-26T07:16:32.807793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train more with this config only\n","metadata":{},"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[-40:]:\n        if not isinstance(layer, tf.keras.layers.BatchNormalization):\n            layer.trainable = True\n\n    optimizer = tf.keras.optimizers.Adam(learning_rate=8e-5)\n    Model.compile(\n        optimizer=optimizer, loss=\"categorical_crossentropy\", metrics=[\"accuracy\"]\n    )\n\n\nunfreeze_model(Model)\n\nepochs = 10  # @param {type: \"slider\", min:8, max:50}\nhist = Model.fit(train_ds, epochs=epochs,batch_size=batch_size, validation_data=val_ds)\nplot_hist(hist)","metadata":{"execution":{"iopub.status.busy":"2021-07-29T07:34:00.244825Z","iopub.execute_input":"2021-07-29T07:34:00.245150Z","iopub.status.idle":"2021-07-29T08:48:38.101449Z","shell.execute_reply.started":"2021-07-29T07:34:00.245114Z","shell.execute_reply":"2021-07-29T08:48:38.098889Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:28:38.330803Z","iopub.execute_input":"2021-07-16T09:28:38.331252Z","iopub.status.idle":"2021-07-16T09:28:38.407992Z","shell.execute_reply.started":"2021-07-16T09:28:38.331219Z","shell.execute_reply":"2021-07-16T09:28:38.405973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Model.layers","metadata":{"execution":{"iopub.status.busy":"2021-07-16T09:29:16.590728Z","iopub.execute_input":"2021-07-16T09:29:16.591125Z","iopub.status.idle":"2021-07-16T09:29:16.598034Z","shell.execute_reply.started":"2021-07-16T09:29:16.591091Z","shell.execute_reply":"2021-07-16T09:29:16.596739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}