{"cells":[{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import numpy as np\nimport tensorflow as tf\nfrom keras.models import Sequential\nfrom keras.layers.core import Dense, Dropout, Activation, Flatten\nfrom keras.layers.convolutional import Convolution2D, MaxPooling2D\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom sklearn.model_selection import train_test_split\nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = data.sample(frac=1, random_state=100)\ndata = data.reset_index(drop=True)\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"extra_path = \"../input/cassava-leaf-disease-classification/train_images/\"\ndata[\"image_id\"] = extra_path + data[\"image_id\"]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"type(data[\"label\"][0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# converting numpy.int --> str for categorization\nconvert_dict = { 'label': 'string'} \ndata = data.astype(convert_dict)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"type(data[\"label\"][0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#datagen=ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255)\nvalid_datagen = ImageDataGenerator(rescale=1./255)\ntest_datagen = ImageDataGenerator(rescale=1./255)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = data[:int(0.8*len(data))]\nvalid_df = data[int(0.8*len(data)):]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator=train_datagen.flow_from_dataframe(dataframe=train_df, \n                                                  x_col=\"image_id\", y_col=\"label\",\n                                                  class_mode=\"categorical\", \n                                                  target_size=(224,224), batch_size=32,\n                                                  shuffle=True, seed=42)\nvalid_generator=valid_datagen.flow_from_dataframe(dataframe=valid_df, \n                                                  x_col=\"image_id\", y_col=\"label\",\n                                                  class_mode=\"categorical\", \n                                                  target_size=(224,224), batch_size=32, \n                                                  seed=47)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"STEP_SIZE_TRAIN = train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID = valid_generator.n//valid_generator.batch_size\nEPOCHS = 20","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow_addons as tfa","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def fully_connected_model():\n    base_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False)\n    base_model.trainable = True #it is false by default\n\n    # Add custom layers\n    x = base_model.output\n    x = tf.keras.layers.GlobalAveragePooling2D()(x)\n    x = tf.keras.layers.Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results.\n    x = tf.keras.layers.Dropout(0.5)(x)\n    x = tf.keras.layers.Dense(1024,activation='relu')(x) #dense layer \n    x = tf.keras.layers.Dropout(0.5)(x)\n    x = tf.keras.layers.Dense(512,activation='relu')(x) #dense layer \n    x = tf.keras.layers.Dropout(0.5)(x)\n    x = tf.keras.layers.Dense(512,activation='relu')(x) #dense layer \n    x = tf.keras.layers.Dropout(0.5)(x)\n    x = tf.keras.layers.Dense(128,activation='relu')(x) #dense layer \n    x = tf.keras.layers.Dropout(0.5)(x)\n    preds = tf.keras.layers.Dense(5,activation='softmax')(x) \n\n    model = tf.keras.Model(inputs=base_model.input,outputs=preds) \n    \n    lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate=1e-3,\n                                                                 decay_steps=10000, \n                                                                 decay_rate=0.9)\n    #optimizer = tf.keras.optimizers.Adam()\n    optimizer = tfa.optimizers.RectifiedAdam(learning_rate=lr_schedule)\n    model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])\n    \n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"resnet_model = fully_connected_model()\nresnet_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#resnet_model.load_weights('../input/latest-weights/resnet_2_2.02-0.76.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"my_callbacks = [\n    tf.keras.callbacks.ModelCheckpoint(filepath='resnet_3.{epoch:02d}-{val_accuracy:.2f}.h5',\n                                       monitor='val_accuracy',\n                                       mode='max', verbose=1)\n]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history2 = resnet_model.fit_generator(generator=train_generator,\n                                      steps_per_epoch=STEP_SIZE_TRAIN, epochs=EPOCHS,\n                                      validation_data=valid_generator,\n                                      validation_steps=STEP_SIZE_VALID,\n                                      verbose=1, shuffle=1, \n                                      callbacks=my_callbacks)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#  \"Accuracy\"\nplt.plot(history2.history['accuracy'])\nplt.plot(history2.history['val_accuracy'])\nplt.title('Accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()\n# \"Loss\"\nplt.plot(history2.history['loss'])\nplt.plot(history2.history['val_loss'])\nplt.title('Loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'validation'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Saving the model\n\n1. Save model architecture in .json\n2. Save model weights in .h5"},{"metadata":{"trusted":true},"cell_type":"code","source":"# serialize model to JSON\nmodel_json = resnet_model.to_json()\nwith open(\"Resnet_3.json\", \"w\") as json_file:\n    json_file.write(model_json)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# serialize weights to HDF5\nresnet_model.save_weights(\"Resnet_3.h5\")\nprint(\"Saved model to disk\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}