{"cells":[{"metadata":{"execution":{"iopub.execute_input":"2021-01-29T10:09:45.790397Z","iopub.status.busy":"2021-01-29T10:09:45.789608Z","iopub.status.idle":"2021-01-29T10:09:52.755818Z","shell.execute_reply":"2021-01-29T10:09:52.754237Z"},"papermill":{"duration":6.992188,"end_time":"2021-01-29T10:09:52.755943","exception":false,"start_time":"2021-01-29T10:09:45.763755","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"import zipfile \nfrom zipfile import ZipFile\nfrom PIL import Image\nimport keras\nimport tensorflow as tf\nfrom tensorflow.keras.optimizers import Adam\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import BatchNormalization\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D\nfrom tensorflow.keras.layers import Activation\nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Input\nfrom tensorflow.keras.models import Model\nfrom tensorflow.keras.layers import concatenate\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.optimizers import Adam\nimport pandas as pd \nimport numpy as np \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.metrics import classification_report,confusion_matrix\nimport tensorflow as tf\nimport cv2\nimport os\nimport glob\nimport matplotlib.image as mpimg","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-29T10:09:52.787777Z","iopub.status.busy":"2021-01-29T10:09:52.786135Z","iopub.status.idle":"2021-01-29T10:09:52.788536Z","shell.execute_reply":"2021-01-29T10:09:52.78904Z"},"papermill":{"duration":0.019923,"end_time":"2021-01-29T10:09:52.789166","exception":false,"start_time":"2021-01-29T10:09:52.769243","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nfrom keras.applications import ResNet50\nfrom keras.applications.resnet50 import preprocess_input","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","execution":{"iopub.execute_input":"2021-01-29T10:09:52.822722Z","iopub.status.busy":"2021-01-29T10:09:52.822124Z","iopub.status.idle":"2021-01-29T10:09:52.881107Z","shell.execute_reply":"2021-01-29T10:09:52.881754Z"},"papermill":{"duration":0.079694,"end_time":"2021-01-29T10:09:52.881884","exception":false,"start_time":"2021-01-29T10:09:52.80219","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv')\ntrain['label']=train['label'].astype(str)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-29T10:09:52.914461Z","iopub.status.busy":"2021-01-29T10:09:52.913736Z","iopub.status.idle":"2021-01-29T10:09:52.920977Z","shell.execute_reply":"2021-01-29T10:09:52.920391Z"},"papermill":{"duration":0.026229,"end_time":"2021-01-29T10:09:52.921069","exception":false,"start_time":"2021-01-29T10:09:52.89484","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"df_train,df_val=train_test_split(train,test_size=0.05,random_state=42 )","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-29T10:09:52.99101Z","iopub.status.busy":"2021-01-29T10:09:52.980727Z","iopub.status.idle":"2021-01-29T10:10:06.242561Z","shell.execute_reply":"2021-01-29T10:10:06.241811Z"},"papermill":{"duration":13.30827,"end_time":"2021-01-29T10:10:06.242709","exception":false,"start_time":"2021-01-29T10:09:52.934439","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"datagen=ImageDataGenerator(\n                           featurewise_center=True,\n    samplewise_center=True,\n    \n    rotation_range=2,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n    shear_range=0.2,\n    zoom_range=0.2,\n    channel_shift_range=0.2,\n    fill_mode=\"nearest\",\n    cval=0.0,\n    horizontal_flip=True,\n    vertical_flip=True,\n    rescale=1./255,\n    )\ntrain_generator=datagen.flow_from_dataframe(\n    dataframe=df_train, \n    directory=\"../input/cassava-leaf-disease-classification/train_images/\", \n    x_col=\"image_id\", \n    y_col=\"label\", \n    class_mode=\"categorical\", \n    target_size=(300,300),\n     shuffle=True,\n    batch_size=32,color_mode=\"rgb\"\n)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-29T10:10:06.286494Z","iopub.status.busy":"2021-01-29T10:10:06.285783Z","iopub.status.idle":"2021-01-29T10:10:07.736794Z","shell.execute_reply":"2021-01-29T10:10:07.736244Z"},"papermill":{"duration":1.478569,"end_time":"2021-01-29T10:10:07.736926","exception":false,"start_time":"2021-01-29T10:10:06.258357","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"Valdatagen=ImageDataGenerator(rescale=1./255)\nval_generator=Valdatagen.flow_from_dataframe(\n    dataframe=df_val, \n    directory=\"/kaggle/input/cassava-leaf-disease-classification/train_images/\", \n    x_col=\"image_id\", \n    y_col=\"label\", \n    class_mode=\"categorical\", \n    target_size=(300,300),\n    batch_size=32,\n shuffle=False,color_mode=\"rgb\"\n)","execution_count":null,"outputs":[]},{"metadata":{"execution":{"iopub.execute_input":"2021-01-29T10:10:07.810259Z","iopub.status.busy":"2021-01-29T10:10:07.809648Z","iopub.status.idle":"2021-01-29T10:10:07.82277Z","shell.execute_reply":"2021-01-29T10:10:07.823507Z"},"papermill":{"duration":0.036286,"end_time":"2021-01-29T10:10:07.823681","exception":false,"start_time":"2021-01-29T10:10:07.787395","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Device:', tpu.master())\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\nexcept:\n    strategy = tf.distribute.get_strategy()\nprint('Number of replicas:', strategy.num_replicas_in_sync)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from tensorflow.keras.applications.inception_v3 import InceptionV3\n\nlocal_weights_file = '../input/inceptionv3/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5'\n\npre_trained_model = InceptionV3(input_shape = (300,300, 3), \n                                include_top = False, \n                                weights = None)\n\npre_trained_model.load_weights(local_weights_file)\n\nfor layer in pre_trained_model.layers:\n    layer.trainable = False\n\n#pre_trained_model.summary()\n\nlast_layer = pre_trained_model.get_layer('mixed7')\nprint('last layer output shape: ', last_layer.output_shape)\nlast_output = last_layer.output\nfrom tensorflow.keras.optimizers import RMSprop\n\n# Flatten the output layer to 1 dimension\nx = tf.keras.layers.Flatten()(last_output)\n# Add a fully connected layer with 1,024 hidden units and ReLU activation\nx = tf.keras.layers.Dense(1024, activation='relu')(x)\n# Add a dropout rate of 0.2\nx = tf.keras.layers.Dropout(0.2)(x) \nx = tf.keras.layers.Dense(2048, activation='relu')(x)\nx = tf.keras.layers.Dropout(0.2)(x)\nx = tf.keras.layers.Dense(2048, activation='relu')(x)\nx = tf.keras.layers.Dropout(0.2)(x)\nx = tf.keras.layers.Dense(1024, activation='relu')(x)\nx = tf.keras.layers.Dropout(0.2)(x)\n\n# Add a final sigmoid layer for classification\nx = tf.keras.layers.Dense  (5, activation='softmax')(x)           \n\nmodel = Model( pre_trained_model.input, x) \n\nmodel.compile(optimizer = RMSprop(lr=0.0001), \n              loss = 'categorical_crossentropy', \n              metrics = ['accuracy'])\ncallbacks=[tf.keras.callbacks.ModelCheckpoint(filepath='model', monitor='categorical_crossentropy', save_weights_only=True\n                                              , save_best_only=True,\n                                                 model='auto')]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(\n            train_generator,\n            validation_data = val_generator,\n            steps_per_epoch = 400,\n            epochs = 10,\n            validation_steps = 100,\n            callbacks=callbacks\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(10)\n\nplt.figure(figsize=(15, 15))\nplt.subplot(2, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(2, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"papermill":{"duration":1.584897,"end_time":"2021-01-29T11:50:03.727141","exception":false,"start_time":"2021-01-29T11:50:02.142244","status":"completed"},"tags":[],"trusted":true},"cell_type":"code","source":"preds = []\nss = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')\n\nfor image in ss.image_id:\n    img = tf.keras.preprocessing.image.load_img('../input/cassava-leaf-disease-classification/test_images/' + image)\n    img = tf.keras.preprocessing.image.img_to_array(img)\n    img = tf.keras.preprocessing.image.smart_resize(img, (300, 300))\n    img = tf.reshape(img, (-1, 300, 300, 3))\n    prediction = model.predict(img/255)\n    preds.append(np.argmax(prediction))\n\nsubmission = pd.DataFrame({'image_id': ss.image_id, 'label': preds})\nsubmission.to_csv('submission.csv', index=False) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission","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}