{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport cv2\nfrom IPython.display import Image\nimport json\n\nimport glob\n\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    print(dirname)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!pip install -U efficientnet","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import efficientnet.keras as efn ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def show(img):\n    return (Image(cv2.imencode(\".png\",img)[1].tobytes()))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv',dtype=str)\ntrain_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(train_df['label'].value_counts())\nprint(sum(list(train_df['label'].value_counts())))\nprint(len(list(train_df['image_id'].unique())))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_images = glob.glob('/kaggle/input/cassava-leaf-disease-classification/test_images/*')\nprint(len(test_images))\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Read Json File"},{"metadata":{"trusted":true},"cell_type":"code","source":"with open('../input/cassava-leaf-disease-classification/label_num_to_disease_map.json') as f:\n    label_map = json.load(f)\nfor key,value in label_map.items():\n    print(key,value)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# from keras.models import Sequential\nfrom keras_preprocessing.image import ImageDataGenerator\nfrom keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization\nfrom keras.layers import Conv2D, MaxPooling2D, Input, GlobalAveragePooling2D\nfrom keras import regularizers, optimizers,Model, layers\nfrom keras.callbacks import ReduceLROnPlateau,ModelCheckpoint\n# from keras.applications.inception_v3 import InceptionV3\nimport tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_images_folder = '/kaggle/input/cassava-leaf-disease-classification/train_images/'\ntrain_images_shape = (256,256)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Image Generator"},{"metadata":{"trusted":true},"cell_type":"code","source":"datagen=ImageDataGenerator(rescale=1./255.,\n                           featurewise_center=True,\n                           featurewise_std_normalization=True,                       \n                           horizontal_flip=True,\n                           vertical_flip=True,\n                           brightness_range=[0.5,1.0],\n                           width_shift_range=0.2,\n                            height_shift_range=0.2,\n                           fill_mode='nearest',\n                           rotation_range=180,\n                           zoom_range=[0.5,1.0],\n                           validation_split=0.25)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# datagen = image.ImageDataGenerator(featurewise_center=True,\n#                                    featurewise_std_normalization=True)\ndatagen.mean = np.array([0.485, 0.456, 0.406], dtype=np.float32).reshape((1,1,3)) # ordering: [R, G, B]\ndatagen.std = np.array([0.229, 0.224, 0.225], dtype=np.float32).reshape((1,1,3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator=datagen.flow_from_dataframe(\ndataframe=train_df,\ndirectory=train_images_folder,\nx_col=\"image_id\",\ny_col=\"label\",\nsubset=\"training\",\nbatch_size=32,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size=train_images_shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"valid_generator=datagen.flow_from_dataframe(\ndataframe=train_df,\ndirectory=train_images_folder,\nx_col=\"image_id\",\ny_col=\"label\",\nsubset=\"validation\",\nbatch_size=32,\nseed=42,\nshuffle=True,\nclass_mode=\"categorical\",\ntarget_size=train_images_shape)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"efficientNet = efn.EfficientNetB1(weights='imagenet')\n\nlast_output = efficientNet.layers[-1].output\nx = Dense(units = 128, activation = tf.nn.relu)(last_output)\nx = Dense(5, activation = tf.nn.softmax)(x)\n\nmodel = Model( efficientNet.input, x)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Compile Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"checkpoint_filepath = 'model_efficientNetB1_256_25_32BS.h5'\ncheckpoint = ModelCheckpoint(filepath=checkpoint_filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')\n\nlearning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',\n                                            patience=1,\n                                            verbose=1,\n                                            factor=0.25,\n                                            min_lr=0.000003)\n\nmodel.compile(loss = 'categorical_crossentropy', optimizer= optimizers.Adam(), metrics=['acc'])\n# model.summary()","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","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(STEP_SIZE_TRAIN,train_generator.batch_size)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class_weight = {0:1,1:5,2:5,3:5,4:5}","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Fit Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit_generator(generator=train_generator,\n                                validation_data = valid_generator,\n                               epochs = 25,\n                   callbacks=[learning_rate_reduction,checkpoint],class_weight=class_weight)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Save Model"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import load_model\n\nmodel.save('latest_efficientNetB1_256_25_32BS.h5') ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","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}