{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install colabcode","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from colabcode import ColabCode\n\nColabCode(port = 10000, password = 'typho')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import os\nfrom tqdm import tqdm\nimport numpy as np\nimport pandas as pd\n\nfrom sklearn.model_selection import train_test_split, StratifiedKFold\n\nimport tensorflow as tf\nimport tensorflow.keras as k\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_style('whitegrid')\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# detect and init the TPU\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver()\ntf.config.experimental_connect_to_cluster(tpu)\ntf.tpu.experimental.initialize_tpu_system(tpu)\n\n# instantiate a distribution strategy\ntpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base_path = '../input/cassava-leaf-disease-classification/'\ntrain_im_path = '../input/cassava-leaf-disease-classification/train_images/'\ntrain_data = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data['image_path'] = train_im_path + train_data['image_id']\ntrain_data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(train_data['image_path'], train_data.label, test_size = 0.1, stratify = train_data.label, random_state = 0)\nprint(x_train.shape, y_train.shape, x_test.shape, y_test.shape)\n\ntrain = pd.concat([x_train, y_train], axis = 1)\ntest = pd.concat([x_test, y_test], axis = 1)\n\ntrain['label'] = train['label'].astype('str')\ntest['label'] = test['label'].astype('str')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_size = 224\n# batch = 512\n# batch = 16 * tpu_strategy.num_replicas_in_sync\nbatch = 1024\n\ntrain_datagen = k.preprocessing.image.ImageDataGenerator(horizontal_flip = True,\n                                                         vertical_flip = True,\n                                                         rotation_range = 40,\n                                                         height_shift_range = 0.2,\n                                                         width_shift_range = 0.2,\n                                                         brightness_range = [0.7, 1.5],\n                                                         zoom_range = [0.3,0.6],\n                                                         shear_range = 40\n                                                         )\ntest_datagen = k.preprocessing.image.ImageDataGenerator(horizontal_flip = True,\n                                                        vertical_flip = True,\n                                                        rotation_range = 40,\n                                                        height_shift_range = 0.2,\n                                                        width_shift_range = 0.2,\n                                                        brightness_range = [0.7, 1.5],\n                                                        zoom_range = [0.3, 0.6],\n                                                        shear_range = 40\n                                                        )\n\ntrain_gen = train_datagen.flow_from_dataframe(train,\n                                              target_size = (image_size,)*2,\n                                              x_col = 'image_path',\n                                              y_col = 'label',\n                                              batch_size = batch,\n                                              class_mode = 'sparse')\ntest_gen = train_datagen.flow_from_dataframe(test,\n                                             target_size = (image_size,)*2,\n                                             x_col = 'image_path',\n                                             y_col = 'label',\n                                             batch_size = batch,\n                                             class_mode = 'sparse')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eff_model = k.applications.EfficientNetB0(include_top = False, input_shape = (image_size,)*2 + (3,))\nfor layer in eff_model.layers:\n    layer.trainable = False\n    \nvgg_model = k.applications.VGG19(include_top = False, input_shape = (image_size,)*2 + (3,))\nfor layer in vgg_model.layers:\n    layer.trainable = False\n\nxception_model = k.applications.Xception(include_top = False, input_shape = (image_size,)*2 + (3,))\nfor layer in xception_model.layers:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_eff_b0_model():\n#     x = k.layers.Conv2D(512, 1, activation = 'relu')(eff_model.output)\n#     x = k.layers.BatchNormalization()(x)\n    \n    x = k.layers.GlobalAveragePooling2D()(eff_model.output)\n    \n#     x = k.layers.Flatten()(x)\n    \n    x = k.layers.Dense(256, activation = 'relu')(x)\n    x = k.layers.BatchNormalization()(x)\n    \n    outs = k.layers.Dense(5, activation = 'softmax')(x)\n    \n    model = k.Model(inputs = eff_model.input, outputs = outs)\n    \n    opt = k.optimizers.Adam(learning_rate = 0.01)\n    model.compile(optimizer = opt, loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])\n    return model\n\ndef create_vgg_model():    \n    x = k.layers.Flatten()(vgg_model.output)\n    x = k.layers.BatchNormalization()(x)\n    \n    outs = k.layers.Dense(5, activation = 'softmax')(x)\n    \n    model = k.Model(inputs = vgg_model.input, outputs = outs)\n    \n    opt = k.optimizers.Adam(learning_rate = 0.01)\n    model.compile(optimizer = opt, loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])\n    return model\n\ndef create_xception_model():    \n    x = k.layers.Flatten()(xception_model.output)\n    x = k.layers.BatchNormalization()(x)\n    \n    outs = k.layers.Dense(5, activation = 'softmax')(x)\n    \n    model = k.Model(inputs = xception_model.input, outputs = outs)\n    \n    opt = k.optimizers.Adam(learning_rate = 0.01)\n    model.compile(optimizer = opt, loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = create_eff_b0_model()\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"calls = [k.callbacks.EarlyStopping(patience = 3, monitor = 'val_loss'),\n        k.callbacks.ReduceLROnPlateau(patience = 2)]\n\n# calls = k.callbacks.LearningRateScheduler(lambda epoch: 1e-8 * 10**(epoch / 20))\n\nhistory = model.fit(x = train_gen, batch_size = batch, epochs = 5, validation_data = (test_gen), callbacks = calls)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['loss'], label = 'loss')\nplt.plot(history.history['val_loss'], label = 'val_loss')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(history.history['accuracy'], label = 'accuracy')\nplt.plot(history.history['val_accuracy'], label = 'val_accuracy')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.save('/kaggle/working/model_EFFNetB0')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_img = k.preprocessing.image.load_img('../input/cassava-leaf-disease-classification/test_images/2216849948.jpg', target_size = (image_size,)*2)\ntest_img = np.array(test_img)\ntest_img = test_img.reshape(1, image_size, image_size, 3)\n\ny_pred = model.predict(test_img)\n\ny_pred = np.argmax(y_pred)\ny_pred = pd.DataFrame({'image_id':'2216849948.jpg', 'label':y_pred}, index = [0])\ny_pred.to_csv('/kaggle/working/output.csv', index = False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"eff_model = k.applications.EfficientNetB0(include_top = False, input_shape = (image_size,)*2 + (3,))\nfor layer in eff_model.layers:\n    layer.trainable = False\n    \nvgg_model = k.applications.VGG19(include_top = False, input_shape = (image_size,)*2 + (3,))\nfor layer in vgg_model.layers:\n    layer.trainable = False\n\nxception_model = k.applications.Xception(include_top = False, input_shape = (image_size,)*2 + (3,))\nfor layer in xception_model.layers:\n    layer.trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_model(model_name):\n#     x = k.layers.Flatten()(eff_model.output)\n#     x = k.layers.BatchNormalization()(x)\n    \n#     x = k.layers.Dense(1024, activation = 'relu')(x)\n#     x = k.layers.BatchNormalization()(x)\n\n    x = k.layers.Conv2D(512, 1, activation = 'relu')(eff_model.output)\n    x = k.layers.BatchNormalization()(x)\n    \n    x = k.layers.Flatten()(x)\n    \n    x = k.layers.Dense(256, activation = 'relu')(x)\n    x = k.layers.BatchNormalization()(x)\n    \n    outs = k.layers.Dense(5, activation = 'softmax')(x)\n    \n    model = k.Model(inputs = eff_model.input, outputs = outs)\n    \n    opt = k.optimizers.Adam(learning_rate = 0.01)\n    model.compile(optimizer = opt, loss = 'sparse_categorical_crossentropy', metrics = ['accuracy'])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image_size = 224\nbatch_size = 128\n\nkfolds = StratifiedKFold(n_splits = 10, shuffle = True, random_state = 0)\n\nfor batch, (train_idx, test_idx) in enumerate(kfolds.split(train_data['image_path'], train_data.label)):\n    temp_train = train_data.iloc[train_idx]\n    temp_test = train_data.iloc[test_idx]\n    temp_train['label'] = temp_train['label'].astype('str')\n    temp_test['label'] = temp_test['label'].astype('str')\n    \n    train_gen = train_datagen.flow_from_dataframe(temp_train,\n                                              target_size = (image_size,)*2,\n                                              x_col = 'image_path',\n                                              y_col = 'label',\n                                              batch_size = batch_size,\n                                              class_mode = 'sparse')\n    test_gen = train_datagen.flow_from_dataframe(temp_test,\n                                             target_size = (image_size,)*2,\n                                             x_col = 'image_path',\n                                             y_col = 'label',\n                                             batch_size = batch_size,\n                                             class_mode = 'sparse')\n    \n    model = create_eff_b7_model()\n    calls = [k.callbacks.EarlyStopping(patience = 3, monitor = 'val_loss'), k.callbacks.ReduceLROnPlateau(patience = 2)]\n    history = model.fit(x = train_gen, batch_size = batch_size, epochs = 5, validation_data = (test_gen), callbacks = calls)\n    \n    model.save(f'/kaggle/working/model_{batch}')","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}