{"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":"import tensorflow as tf\nimport tensorflow.keras as keras\nfrom tensorflow.keras import layers\nimport tensorflow_hub as hub\nimport pandas as pd\nimport numpy as np\nimport os\nimport matplotlib.pyplot as plt\nimport cv2\nimport csv","metadata":{"papermill":{"duration":5.10742,"end_time":"2021-11-08T12:42:17.388204","exception":false,"start_time":"2021-11-08T12:42:12.280784","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:03.564318Z","iopub.execute_input":"2022-04-24T07:27:03.564781Z","iopub.status.idle":"2022-04-24T07:27:11.151267Z","shell.execute_reply.started":"2022-04-24T07:27:03.564668Z","shell.execute_reply":"2022-04-24T07:27:11.150060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()  # TPU detection. No parameters necessary if TPU_NAME environment variable is set. On Kaggle this is always the case.\n    print('Running on TPU ', tpu.master())\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nelse:\n    strategy = tf.distribute.get_strategy() # default distribution strategy in Tensorflow. Works on CPU and single GPU.\n\nprint(\"REPLICAS: \", strategy.num_replicas_in_sync)","metadata":{"papermill":{"duration":0.030623,"end_time":"2021-11-08T12:42:17.436814","exception":false,"start_time":"2021-11-08T12:42:17.406191","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:11.153172Z","iopub.execute_input":"2022-04-24T07:27:11.153462Z","iopub.status.idle":"2022-04-24T07:27:11.173075Z","shell.execute_reply.started":"2022-04-24T07:27:11.153425Z","shell.execute_reply":"2022-04-24T07:27:11.172255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\ndf","metadata":{"papermill":{"duration":0.060732,"end_time":"2021-11-08T12:42:17.511519","exception":false,"start_time":"2021-11-08T12:42:17.450787","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:11.175785Z","iopub.execute_input":"2022-04-24T07:27:11.176053Z","iopub.status.idle":"2022-04-24T07:27:11.244023Z","shell.execute_reply.started":"2022-04-24T07:27:11.176021Z","shell.execute_reply":"2022-04-24T07:27:11.242954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = df.loc[:20896]\nval_df = df.loc[20897:].reset_index(drop = True)","metadata":{"papermill":{"duration":0.020406,"end_time":"2021-11-08T12:42:17.545255","exception":false,"start_time":"2021-11-08T12:42:17.524849","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:11.246093Z","iopub.execute_input":"2022-04-24T07:27:11.246936Z","iopub.status.idle":"2022-04-24T07:27:11.252733Z","shell.execute_reply.started":"2022-04-24T07:27:11.246890Z","shell.execute_reply":"2022-04-24T07:27:11.251396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['image_id'] = '../input/cassava-leaf-disease-classification/train_images'+os.sep + train_df['image_id']\nval_df['image_id'] = '../input/cassava-leaf-disease-classification/train_images'+os.sep + val_df['image_id']","metadata":{"papermill":{"duration":0.02894,"end_time":"2021-11-08T12:42:17.587692","exception":false,"start_time":"2021-11-08T12:42:17.558752","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_files_ds = tf.data.Dataset.from_tensor_slices((train_df['image_id'],train_df['label']))\nval_files_ds = tf.data.Dataset.from_tensor_slices((val_df['image_id'],val_df['label']))","metadata":{"papermill":{"duration":2.164086,"end_time":"2021-11-08T12:42:19.766578","exception":false,"start_time":"2021-11-08T12:42:17.602492","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:11.280135Z","iopub.execute_input":"2022-04-24T07:27:11.281103Z","iopub.status.idle":"2022-04-24T07:27:11.326367Z","shell.execute_reply.started":"2022-04-24T07:27:11.280935Z","shell.execute_reply":"2022-04-24T07:27:11.325552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in train_files_ds:\n    print(i)\n    break","metadata":{"execution":{"iopub.status.busy":"2022-04-24T07:27:11.328437Z","iopub.execute_input":"2022-04-24T07:27:11.329280Z","iopub.status.idle":"2022-04-24T07:27:11.368500Z","shell.execute_reply.started":"2022-04-24T07:27:11.329225Z","shell.execute_reply":"2022-04-24T07:27:11.367503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def handle_filenames(filename,label):\n    image = tf.io.read_file(filename)\n    image = tf.io.decode_jpeg(image,channels = 3)\n    image = tf.image.resize(image,size = (224,224))\n    return image,label\nauto = tf.data.experimental.AUTOTUNE\n\ntrain_ds = train_files_ds.map(handle_filenames).batch(10).prefetch(auto)\nval_ds = val_files_ds.map(handle_filenames).batch(10).prefetch(auto)","metadata":{"papermill":{"duration":0.11751,"end_time":"2021-11-08T12:42:19.898552","exception":false,"start_time":"2021-11-08T12:42:19.781042","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:11.369827Z","iopub.execute_input":"2022-04-24T07:27:11.370059Z","iopub.status.idle":"2022-04-24T07:27:11.498005Z","shell.execute_reply.started":"2022-04-24T07:27:11.370028Z","shell.execute_reply":"2022-04-24T07:27:11.496920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show(images,labels):\n    plt.figure(figsize=(12, 12))\n    for it, (image, label) in enumerate(zip(images[:10], labels[:10])):\n        plt.subplot(4, 4, it+1)\n        plt.imshow(image/255.0)\n        plt.axis('off')\n        plt.title({int(label)})\nfor i in train_ds.take(2):\n    show(i[0],i[1])","metadata":{"papermill":{"duration":1.924907,"end_time":"2021-11-08T12:42:21.838757","exception":false,"start_time":"2021-11-08T12:42:19.91385","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:11.499411Z","iopub.execute_input":"2022-04-24T07:27:11.499704Z","iopub.status.idle":"2022-04-24T07:27:13.223370Z","shell.execute_reply.started":"2022-04-24T07:27:11.499668Z","shell.execute_reply":"2022-04-24T07:27:13.222123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preprocess = keras.Sequential([\n    layers.RandomFlip(),\n    layers.RandomRotation(0.2),\n    layers.RandomZoom((-0.2, 0)),\n    layers.RandomContrast((0.2,0.2))\n])\n\n#vit_backbone = hub.KerasLayer(\"https://tfhub.dev/sayakpaul/vit_b8_fe/1\", trainable=False) \nbackbone = keras.applications.EfficientNetB3(\n    include_top = False,\n    weights = '../input/efficientnetb3notop/efficientnetb3_notop.h5',\n    input_shape = (224,224,3)\n)\nresnet_preprocess = keras.applications.efficientnet.preprocess_input\nbackbone.trainable = True","metadata":{"papermill":{"duration":3.892192,"end_time":"2021-11-08T12:42:25.775965","exception":false,"start_time":"2021-11-08T12:42:21.883773","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:13.226447Z","iopub.execute_input":"2022-04-24T07:27:13.227389Z","iopub.status.idle":"2022-04-24T07:27:18.177957Z","shell.execute_reply.started":"2022-04-24T07:27:13.227323Z","shell.execute_reply":"2022-04-24T07:27:18.175997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inp = keras.Input(shape = (224,224,3))\nx = preprocess(inp)\nx = resnet_preprocess(x)\nx = backbone(x)\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Dropout(0.3)(x)\nx = layers.Dense(5,activation = 'softmax')(x)\nmodel = keras.Model(inp,x)\nmodel.summary()","metadata":{"papermill":{"duration":1.487914,"end_time":"2021-11-08T12:42:27.30864","exception":false,"start_time":"2021-11-08T12:42:25.820726","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:18.181178Z","iopub.execute_input":"2022-04-24T07:27:18.181545Z","iopub.status.idle":"2022-04-24T07:27:19.750882Z","shell.execute_reply.started":"2022-04-24T07:27:18.181483Z","shell.execute_reply":"2022-04-24T07:27:19.749635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"LR_START = 0.0001\nLR_MAX = 0.001 * strategy.num_replicas_in_sync\nLR_MIN = 0.0001\nLR_RAMPUP_EPOCHS = 5\nLR_SUSTAIN_EPOCHS = 0\nLR_EXP_DECAY = .8\n\ndef lrfn(epoch):\n    if epoch < LR_RAMPUP_EPOCHS:\n        lr = (LR_MAX - LR_START) / LR_RAMPUP_EPOCHS * epoch + LR_START\n    elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:\n        lr = LR_MAX\n    else:\n        lr = (LR_MAX - LR_MIN) * LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS) + LR_MIN\n    return lr\n    \nlr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose = True)","metadata":{"papermill":{"duration":0.053433,"end_time":"2021-11-08T12:42:27.406824","exception":false,"start_time":"2021-11-08T12:42:27.353391","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:19.752676Z","iopub.execute_input":"2022-04-24T07:27:19.753348Z","iopub.status.idle":"2022-04-24T07:27:19.766237Z","shell.execute_reply.started":"2022-04-24T07:27:19.753292Z","shell.execute_reply":"2022-04-24T07:27:19.765063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"decay_steps = int(round(len(train_df)/10))*10\ncosine_decay = keras.experimental.CosineDecay(initial_learning_rate=1e-4, decay_steps=decay_steps, alpha=0.3)","metadata":{"papermill":{"duration":0.052285,"end_time":"2021-11-08T12:42:27.50262","exception":false,"start_time":"2021-11-08T12:42:27.450335","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:19.767765Z","iopub.execute_input":"2022-04-24T07:27:19.768191Z","iopub.status.idle":"2022-04-24T07:27:19.798318Z","shell.execute_reply.started":"2022-04-24T07:27:19.768158Z","shell.execute_reply":"2022-04-24T07:27:19.797417Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss = 'sparse_categorical_crossentropy',\n    optimizer = keras.optimizers.Adam(cosine_decay),\n    metrics = ['accuracy']\n)","metadata":{"papermill":{"duration":0.06607,"end_time":"2021-11-08T12:42:27.616059","exception":false,"start_time":"2021-11-08T12:42:27.549989","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:19.799815Z","iopub.execute_input":"2022-04-24T07:27:19.800338Z","iopub.status.idle":"2022-04-24T07:27:19.830561Z","shell.execute_reply.started":"2022-04-24T07:27:19.800298Z","shell.execute_reply":"2022-04-24T07:27:19.829453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = tf.keras.callbacks.EarlyStopping(\n    monitor='val_loss', \n    patience=5\n)\nmodel.fit(train_ds,\n          validation_data = val_ds,\n          epochs = 30,\n          callbacks = [early_stopping])","metadata":{"papermill":{"duration":3501.858091,"end_time":"2021-11-08T13:40:49.517664","exception":false,"start_time":"2021-11-08T12:42:27.659573","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-04-24T07:27:19.832200Z","iopub.execute_input":"2022-04-24T07:27:19.832766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('model.h5')","metadata":{"papermill":{"duration":43.482023,"end_time":"2021-11-08T13:41:37.636161","exception":false,"start_time":"2021-11-08T13:40:54.154138","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"l = os.listdir(\"../input/cassava-leaf-disease-classification/test_images\")\nparent = \"../input/cassava-leaf-disease-classification/test_images/\"\npredictions = []\npredictions.append([\"image_id\", \"label\"])\nfor i in l :\n    child = parent + i\n    img = cv2.imread(child)\n    img = cv2.resize(img, (224, 224))\n    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    img = tf.keras.applications.efficientnet.preprocess_input(img)\n    img = img.reshape((1, 224, 224, 3))\n    pred = model.predict(img)\n    pred = pred.reshape((5,))\n    print(np.argmax(pred))\n    del img\n    predictions.append([i, str(np.argmax(pred))])\n    del pred\n\n\nwith open('submission.csv', 'w', newline='') as file:\n    writer = csv.writer(file)\n    writer.writerows(predictions)","metadata":{"papermill":{"duration":7.46321,"end_time":"2021-11-08T13:41:49.99595","exception":false,"start_time":"2021-11-08T13:41:42.53274","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('submission.csv')","metadata":{"papermill":{"duration":4.66455,"end_time":"2021-11-08T13:41:59.266779","exception":false,"start_time":"2021-11-08T13:41:54.602229","status":"completed"},"tags":[],"trusted":true},"execution_count":null,"outputs":[]}]}