{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"\n\nimport numpy as np \nimport pandas as pd \nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras import applications\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras import optimizers\nfrom keras.models import Sequential, Model \nfrom keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D\nfrom keras.optimizers import Adam\nfrom keras import backend as k \nfrom keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"main_dir = '../input/cassava-leaf-disease-classification/'\nos.listdir(main_dir) \ntrain_img_path = '../input/cassava-leaf-disease-classification/train_images'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = pd.read_csv(main_dir+'train.csv')\ndata.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.label.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import json\njs = open(main_dir + 'label_num_to_disease_map.json')\nreal_classes = json.load(js)\nreal_classes = {int(k):v for k,v in real_classes.items()}\n\ndata['class_name'] = data.label.map(real_classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.model_selection import train_test_split","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train,val = train_test_split(data, test_size = 0.1, stratify = data['class_name'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_row= 300\nimg_col=300\n\ntrain_datagen = ImageDataGenerator(rescale = 1/255.0,\n                            rotation_range = 270,\n                            brightness_range=[0.1,0.9],       \n                            width_shift_range = 0.2,\n                            height_shift_range = 0.2,\n                            shear_range = 25,\n                            zoom_range = 0.4,\n                            channel_shift_range=0.2,       \n                            horizontal_flip = True,\n                            vertical_flip = True,\n                            fill_mode = 'nearest')\n\nvalidation_datagen = ImageDataGenerator(rescale=1.0/255)\ntrain_generator = train_datagen.flow_from_dataframe(train,\n                                                directory = train_img_path,\n                                                x_col = 'image_id',\n                                                y_col = 'class_name',\n                                                target_size = (img_row,img_col),\n                                                color_mode = 'rgb',\n                                                class_mode = 'categorical',\n                                                shuffle = True,\n                                                batch_size = 10, \n                                                )\n\n\nvalidation_generator = validation_datagen.flow_from_dataframe(val,\n                                                directory = train_img_path,\n                                                x_col = 'image_id',\n                                                y_col = 'class_name',\n                                                target_size = (img_row,img_col),\n                                                color_mode = 'rgb',\n                                                class_mode = 'categorical',\n                                                shuffle = True,\n                                                batch_size = 10, \n                                                )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img_width, img_height = 300, 300\nmodel = applications.VGG16(weights = \"imagenet\", include_top=False, input_shape = (img_width, img_height, 3))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for layer in model.layers:\n    layer.trainable = True\n    \nmodel.layers[0].trainable = False\nmodel.layers[1].trainable = False","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x = model.output\nx = GlobalAveragePooling2D()(x)\noutput = Dense(5,activation='softmax')(x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_final = Model(inputs = model.input, outputs = output)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_final.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model_final.compile(loss='binary_crossentropy',\n              optimizer = Adam(lr = 1e-5),\n              metrics = ['accuracy'])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"checkpoint = ModelCheckpoint(\"model.h5\", monitor='val_accuracy', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)\nearly = EarlyStopping(monitor='val_accuracy', min_delta=0, patience=10, verbose=1, mode='auto')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"epochs=20\nbatch_size=20\nhistory = model_final.fit_generator(generator = train_generator,\n                                    validation_steps = validation_generator.n // validation_generator.batch_size + 1,\n                                    validation_data = validation_generator,\n                                    steps_per_epoch = train_generator.n // train_generator.batch_size + 1,\n                                    epochs = epochs,\n                                    callbacks = [checkpoint, early])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import keras\nmodel = keras.models.load_model('model.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image\nTEST_DIR = '../input/cassava-leaf-disease-classification/test_images/'\ntest_imgs = os.listdir(TEST_DIR)\npredictions = []\n\nfor image in test_imgs:\n    img = Image.open(TEST_DIR + image)\n    img = img.resize((300,300))\n    img = np.expand_dims(img, axis=0)\n    pred = model.predict(img).argmax(axis = 1)[0]\n    predictions.append(pred)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.DataFrame({'image_id': test_imgs, 'label': predictions})","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('submission.csv', index = False)","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}