{"cells":[{"metadata":{},"cell_type":"markdown","source":"The loading of EfficientNet without Internet copied from:\n\nThis notebook uses an old docker format. I also found I have to recreate the model architecture from scratch, rather than just load the model and weights.\n\nBut, it works.\n\nYou need to add a dataset with your model and set model_file1.\n\nSource:\n\nfrom https://github.com/qubvel/efficientnet into a dataset:\n- https://www.kaggle.com/guesejustin/efficientnet100minimal\n\n\nFor further questions shoot me a message or https://www.linkedin.com/in/justin-guese/"},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np, pandas as pd, os\nimport matplotlib.pyplot as plt\nimport sys\n\npackage_path = '/kaggle/input/efficientnet100minimal/'\nsys.path.append(package_path)\nimport efficientnet.keras as efn \n#model = efn.EfficientNetB2(include_top=False, input_shape=(128,128,3), weights=None, pooling='avg')\n# now this would usually download the weights, but because this is offline we will import \n# the weights from another datasource\n# just be sure to add the matching b0 to b7 number, depending on which model you started above\n#model.load_weights('../input/efficientnetb0b7-keras-weights/efficientnet-b2_weights_tf_dim_ordering_tf_kernels_autoaugment_notop.h5')\nprint('done')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import cv2\nimport tensorflow as tf, re, math\nimport random\nimport csv\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.utils import Sequence\nimport tensorflow.keras.backend as K\nimport tensorflow.keras.backend\nimport tensorflow.keras.layers as L\nfrom skimage.transform import resize\nimport PIL\n\nmypath = '../input/cassava-leaf-disease-classification'\n\ntrain_or_test = 'test'\nimages_folder =    mypath+'/' + train_or_test + '_images'\n\nif train_or_test == 'test':\n    sample_submission_file = 'sample_submission.csv'\nelse:\n    sample_submission_file = 'train.csv'\n    \nmodel_file1 = '../input/cassava-model/xxxx.hdf5'\n\nkaggle_temp = './kaggle/temp/'\ntfrec_folder = '.'\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"DEVICE = \"TPU\"\n\nif DEVICE == \"TPU\":\n    print(\"connecting to TPU...\")\n    try:\n        tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n        print('Running on TPU ', tpu.master())\n    except ValueError:\n        print(\"Could not connect to TPU\")\n        tpu = None\n    if tpu:\n        try:\n            print(\"initializing  TPU ...\")\n            tf.config.experimental_connect_to_cluster(tpu)\n            tf.tpu.experimental.initialize_tpu_system(tpu)\n            strategy = tf.distribute.experimental.TPUStrategy(tpu)\n            print(\"TPU initialized\")\n        except _:\n            print(\"failed to initialize TPU\")\n    else:\n        DEVICE = \"GPU\"\nif DEVICE != \"TPU\":\n    print(\"Using default strategy for CPU and single GPU\")\n    strategy = tf.distribute.get_strategy()\nif DEVICE == \"GPU\":\n    print(\"Num GPUs Available: \", len(tf.config.experimental.list_physical_devices('GPU')))\n    \nAUTO     = tf.data.experimental.AUTOTUNE\nREPLICAS = strategy.num_replicas_in_sync\n#REPLICAS = 8\nprint(f'REPLICAS: {REPLICAS}')\n\nimport os, gc, random, cv2, csv\nfrom datetime import datetime\nfrom numpy.random import seed\nfrom skimage import measure #scikit-image conda-forge\nfrom skimage.transform import resize\nfrom sklearn.metrics import roc_auc_score  #scikit-learn conda-forge\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nprint(\"done\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from kaggle_datasets import KaggleDatasets\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import sys\npackage_path = '/kaggle/input/efficientnet100minimal/'\nsys.path.append(package_path)\nimport efficientnet.keras as efn \n\nmy_metadata_count = 1\noutput_targets = 5\n\n\n#replace with your model here:\ndef get_model(cfg):\n    effsize = CFG['net_size']\n    input_size = effsize\n    conv_base = efn.EfficientNetB7( \\\n            input_shape=(effsize, effsize, 3), \\\n            weights='imagenet', include_top=False)\n    conv_base.trainable = True\n\n    model_eff = tf.keras.layers.GlobalAveragePooling2D()(conv_base.output)\n\n    model_meta_in = tf.keras.Input(shape=(my_metadata_count,), name='input_metadata_name')\n    model_meta_out = tf.keras.layers.Dense(1,activation='relu')(model_meta_in)    \n    model_meta = tf.keras.Model(model_meta_in,model_meta_out)\n    \n    layers_concat = tf.keras.layers.concatenate([model_eff,model_meta.output], axis=-1)\n\n    layers_concat = tf.keras.layers.Dense(16,activation='relu')(layers_concat)\n    layers_concat = tf.keras.layers.Dropout(0.5)(layers_concat)\n    layers_concat = tf.keras.layers.BatchNormalization()(layers_concat)\n\n    layers_concat = tf.keras.layers.Dense(output_targets, activation='softmax')(layers_concat)\n    model = tf.keras.Model(inputs=[conv_base.input,model_meta.input], \n            outputs=[layers_concat])\n    return model\n\ndef compile_new_model(cfg):    \n    with strategy.scope():\n        model = get_model(cfg)\n     \n        losses = tf.keras.losses.BinaryCrossentropy(\\\n                        label_smoothing = cfg['label_smooth_fac'])        \n        model.compile(\n            optimizer = cfg['optimizer'],\n            loss      = losses,\n            metrics   = ['categorical_accuracy'])\n    return model\nprint(\"done\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import efficientnet.tfkeras\nfrom tensorflow.keras.models import load_model\nmodel = load_model(model_file1)\nprint(\"model and weights loaded\")","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}