{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\n#tf.enable_eager_execution()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import os\nimport numpy as numpy\nfrom PIL import Image\nl=[]\nfor i in os.listdir('../input/cassava-disease/train/train'):\n    print (len(os.listdir(os.path.join('../input/cassava-disease/train/train',i))))\n    l.append(i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"l","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import os\nimport numpy as numpy\nfrom PIL import Image\nfrom keras.preprocessing import image as IMG\nfrom keras.applications.inception_v3 import preprocess_input\n\nRESHAPE=(64,64)\n\ndef load_image(path):\n    img = Image.open(path)\n    return img\n\n\ndef preprocess_image(cv_img):\n    cv_img = cv_img.resize(RESHAPE)\n    img = np.array(cv_img)\n    return img/255.0\n\ndef crop(img, hoffset,woffset):\n    # Note: image_data_format is 'channel_last'\n    assert img.shape[2] == 3\n    height, width = img.shape[0], img.shape[1]\n    \n    return img[woffset:width-woffset, hoffset:height-hoffset, :]\n\ndef load_data(path,n_images):\n    images=[]\n    labels=[]\n    \n    i=0\n    for cat in os.listdir(path):\n        cat_path=os.path.join(path,cat)\n        num_imgs_ld=0\n        for image in os.listdir(cat_path):\n            image_path=os.path.join(cat_path,image)\n            #img=load_image(image_path)\n            \n            img = IMG.load_img(image_path, target_size=(320, 320))\n            x = IMG.img_to_array(img)\n            #x = np.expand_dims(x, axis=0)\n            #x = preprocess_input(x)\n            x=crop(x,48,48)\n            #images.append(preprocess_image(img))\n            images.append(x/255.0)\n            labels.append(i)\n            num_imgs_ld=num_imgs_ld+1\n         #   if num_imgs_ld > n_images[i] - 1: \n           #     break\n            \n        \n        i=i+1    \n    return np.array(images),np.array(labels)\n    #return (images , labels)\n            \n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data,labels=load_data('../input/cassava-disease/train/train',[770,1440,315,465,1400])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(data[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i in range(5):\n    print(i)\n    idx = np.random.permutation(len(data))\n    data,labels = data[idx], labels[idx]\n\nplt.imshow(data[0])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.utils import to_categorical\nlabels= to_categorical(labels)\nlabels.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_data=data[:5000]\ntrain_labels=labels[:5000]\nval_data=data[5000:]\nval_labels=labels[5000:]\n\nprint(train_data.shape)\nprint(val_data.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"cache_dir = os.path.expanduser(os.path.join('~', '.keras'))\nif not os.path.exists(cache_dir):\n    os.makedirs(cache_dir)\nmodels_dir = os.path.join(cache_dir, 'models')\nif not os.path.exists(models_dir):\n    os.makedirs(models_dir)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp ../input/keras-pretrained-models/vgg* ~/.keras/models/\n!ls ~/.keras/models","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.applications.vgg16 import VGG16\nfrom keras.preprocessing import image\nfrom keras.models import Model\nfrom keras.layers import Dense,Input, GlobalAveragePooling2D,Dropout,Cropping2D\nfrom keras import backend as K\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# create the base pre-trained model\nbase_model = VGG16(weights='imagenet', include_top=False,input_shape=(224,224,3))\nbase_model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(base_model.input)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\"\"\"\n\n\"\"\"\nx = base_model.output\nx=GlobalAveragePooling2D()(x)\n# let's add a fully-connected layer\n\n#x = Dense(1024, activation='relu')(x)\n#x=Dropout(0.5)(x)\nx = Dense(1024, activation='relu')(x)\nx=Dropout(0.2)(x)\n\nx = Dense(512, activation='relu')(x)\nx=Dropout(0.2)(x)\n\n\n# and a logistic layer -- let's say we have 200 classes\npredictions = Dense(5, activation='softmax',name=\"out\")(x)\n\n# this is the model we will train\n\nmodel = Model(inputs=base_model.input, outputs=predictions)\n\n# first: train only the top layers (which were randomly initialized)\n# i.e. freeze all convolutional InceptionV3 layers\nfor layer in base_model.layers:\n    layer.trainable = False\n\n# compile the model (should be done *after* setting layers to non-trainable)\nmodel.compile(optimizer='rmsprop', loss='categorical_crossentropy',metrics=['accuracy'])\n\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":"from keras.preprocessing.image import ImageDataGenerator\ndatagen = ImageDataGenerator(\n        # set input mean to 0 over the dataset\n        featurewise_center=False,\n        # set each sample mean to 0\n        samplewise_center=False,\n        # divide inputs by std of dataset\n        featurewise_std_normalization=False,\n        # divide each input by its std\n        samplewise_std_normalization=False,\n        # apply ZCA whitening\n        zca_whitening=False,\n        # epsilon for ZCA whitening\n        zca_epsilon=1e-06,\n        # randomly rotate images in the range (deg 0 to 180)\n        rotation_range=30,\n        # randomly shift images horizontally\n        width_shift_range=0.1,\n        # randomly shift images vertically\n        height_shift_range=0.1,\n        # set range for random shear\n        shear_range=0.,\n        # set range for random zoom\n        zoom_range=0.1,\n        # set range for random channel shifts\n        channel_shift_range=0.,\n        # set mode for filling points outside the input boundaries\n        fill_mode='nearest',\n        # value used for fill_mode = \"constant\"\n        cval=0.,\n        # randomly flip images\n        horizontal_flip=True,\n        # randomly flip images\n        vertical_flip=False,\n        # set rescaling factor (applied before any other transformation)\n        rescale=None,\n        # set function that will be applied on each input\n        preprocessing_function=None,\n        # image data format, either \"channels_first\" or \"channels_last\"\n        data_format='channels_last',\n        # fraction of images reserved for validation (strictly between 0 and 1)\n        validation_split=0.0)\ndatagen.fit(train_data)\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.callbacks import ModelCheckpoint\n\ncheckpointer = ModelCheckpoint(filepath='weights-tfl-02.h5',monitor='val_acc',\n                              verbose=1, save_best_only=True)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n\n\n\nhistory= model.fit_generator(datagen.flow(train_data, train_labels, batch_size=32),\n         epochs=10, verbose=1,validation_data=(val_data, val_labels),\n      workers=4,steps_per_epoch=150, callbacks=[checkpointer])\n\n#history= model.fit(train_data,train_labels, batch_size=32,\n   #          epochs=1, verbose=1, validation_data=(val_data, val_labels))\n    \n# Plot training & validation accuracy values\nplt.plot(history.history['acc'])\nplt.plot(history.history['val_acc'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()\n\n# Plot training & validation loss values\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()\n\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import load_model\nmodel=load_model('weights-tfl-02.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"evl=model.evaluate(x=val_data, y=val_labels, batch_size=32, verbose=0)\nprint(evl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\"\"\"pred = model.predict(val_data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\"\"\"\nAPC=tf.metrics.mean_per_class_accuracy(\n    val_labels,\n    tf.convert_to_tensor(pred),\n    num_classes=5\n    \n)\nwith tf.Session() as sess:\n    sess.run(APC)\nprint(np.array(APC))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\nfor i, layer in enumerate(base_model.layers):\n    print(i, layer.name)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# we chose to train the top 2 inception blocks, i.e. we will freeze\n# the first 249 layers and unfreeze the rest:\n\nfor layer in model.layers[:11]:\n    layer.trainable = False\nfor layer in model.layers[11:]:\n    layer.trainable = True\n\n# we need to recompile the model for these modifications to take effect\n# we use SGD with a low learning rate\nfrom keras.optimizers import SGD\nmodel.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy',metrics=['accuracy'])\n\n# we train our model again (this time fine-tuning the top 2 inception blocks\n# alongside the top Dense layers\n\n\nhistory2= model.fit_generator(datagen.flow(train_data, train_labels, batch_size=32),\n     epochs=5, verbose=1,  validation_data=(val_data, val_labels),\n                workers=4,steps_per_epoch=150,callbacks=[checkpointer])\n#history2= model.fit(train_data,train_labels, batch_size=32,epochs=20,\n                #    verbose=1,validation_data=(val_data, val_labels))\n# Plot training & validation accuracy values\nplt.plot(history2.history['acc'])\nplt.plot(history2.history['val_acc'])\nplt.title('Model accuracy')\nplt.ylabel('Accuracy')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()\n\n# Plot training & validation loss values\nplt.plot(history2.history['loss'])\nplt.plot(history2.history['val_loss'])\nplt.title('Model loss')\nplt.ylabel('Loss')\nplt.xlabel('Epoch')\nplt.legend(['Train', 'Test'], loc='upper left')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model=load_model('weights-tfl-02.h5')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"evl=model.evaluate(x=val_data, y=val_labels, batch_size=32, verbose=0)\nprint(evl)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testimages=[]\ntestimagesnames=[]\nfor imagename in  os.listdir('../input/cassava-disease/test/test/0'):\n    imagepath= os.path.join('../input/cassava-disease/test/test/0',imagename)\n    img = IMG.load_img(imagepath, target_size=(320, 320))\n    x = IMG.img_to_array(img)\n            #x = np.expand_dims(x, axis=0)\n            #x = preprocess_input(x)\n    x=crop(x,48,48)\n            #images.append(preprocess_image(img))\n    testimages.append(x/255.0)\n    testimagesnames.append(imagename)\n    \n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testimages=np.array(testimages)\ntestimages.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"testimagesnames[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#make predictions\ntest_predictions=model.predict(testimages)\npredictions=np.argmax(test_predictions,axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"l","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"index2labs={0:'cgm', 1:'cbsd', 2:'healthy', 3:'cbb', 4:'cmd'}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import pandas as pd\nsubm = pd.DataFrame({'Category':[index2labs[j] for j in predictions],\n         'Id':[testimagesnames[i] for i in range(len(testimagesnames))]})\nsubm.to_csv('submission_crop_withDA_83.csv', index=False)\nsubm.head()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Save tf.keras model in HDF5 format.\n# keras_file = \"keras_model.h5\"\n# tf.keras.models.save_model(model, keras_file)\n\n# # Convert to TensorFlow Lite model.\n# converter = tf.lite.TFLiteConverter.from_keras_model_file(keras_file)\n# tflite_model = converter.convert()\n# open(\"converted_model.tflite\", \"wb\").write(tflite_model)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}