{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"from PIL import Image\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os\nimport cv2\nimport keras\n# For one-hot-encoding\nfrom keras.utils import np_utils\n# For creating sequenttial model\nfrom keras.models import Sequential\nfrom keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Dropout\n# For saving and loading models\nfrom keras.models import load_model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nlabels = pd.read_csv(\"/kaggle/input/understanding_cloud_organization/train.csv\")\nlabels.head()\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n'''\nlabels[\"label\"] = labels[\"Image_Label\"].map(lambda s: s.split(\"_\")[1])\nlabels[\"image\"] = labels[\"Image_Label\"].map(lambda s: s.split(\"_\")[0])\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nlabels.head()\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nlabels['label'].value_counts()\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\n# numpy lists with image names\ngravels = []\nfishes = []\nsugars = []\nflowers = []\nnans = []\nfor _ in labels[\"Image_Label\"]:\n    if _.split(\"_\")[1] == \"Gravel\":\n        gravels.append(_.split(\"_\")[0])\n    elif _.split(\"_\")[1] == \"Fish\":\n        fishes.append(_.split(\"_\")[0])\n    elif _.split(\"_\")[1] == \"Sugar\":\n        sugars.append(_.split(\"_\")[0])\n    elif _.split(\"_\")[1] == \"Flower\":\n        flowers.append(_.split(\"_\")[0])\n    else:\n        nans.append(_.split(\"_\")[0])\n'''\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\ngravels[:5]\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\ntrain_images_location = \"/kaggle/input/understanding_cloud_organization/train_images/\"\ntest_images_location = \"/kaggle/input/understanding_cloud_organization/test_images/\"\ndata = []\nlabels = []\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nN = 0\nfor cloud_type in [gravels, fishes, flowers, sugars]:\n    for filename in cloud_type:\n        try:\n            image = cv2.imread(train_images_location + filename)\n            image_from_numpy_array = Image.fromarray(image, \"RGB\")\n            resized_image = image_from_numpy_array.resize((50,50))\n            data.append(np.array(resized_image))\n            \n            if N == 0:\n                labels.append(0)\n            elif N == 1:\n                labels.append(1)\n            elif N == 2:\n                labels.append(2)\n            elif N == 3:\n                labels.append(3)\n            else:\n                pass\n            \n        except:\n            print(\"error occured for \" + filename +\". It isn't an image\" )\n    N=N+1\n'''\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nclouds = np.array(data)\nlabels = np.array(labels)\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nprint(clouds.shape)\nprint(labels.shape)\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nnp.save(\"all-clouds-as-rgb-image-arrays\", clouds)\nnp.save(\"corresponding-labels-for-all-clouds-unshuffled\", labels)\n'''","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"**Load**"},{"metadata":{"trusted":true},"cell_type":"code","source":"!wget https://www.kaggleusercontent.com/kf/24253071/eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0..UBE9ENvOtVd5H0IKuCGF3g.1WLRXJgnZUzIQv4Xwts-9tSDQtAe_lzMv4eIq7_5G4Lm0EoBxRJa-txxI3nqPzQEM1YJPrDb4XDE_Pd3jB48ACcCeogiytpPHOwIj5y9O02Fnj2ZWDwmmaInJZ7JUeyT-2Tcy-hwGTxSe0JQ8uS4in8VLdPLU37KhG8J9msYcMw.JEO6ltHs0mYYV8K9W_Q4DA/corresponding-labels-for-all-clouds-unshuffled.npy\n!wget https://www.kaggleusercontent.com/kf/24253071/eyJhbGciOiJkaXIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0..sLTBUb9nfTuwKXtGM-ySlg.RmDjJtstKEkKzfjI63eov1VMyZfBuqzRLnqmv7G99wgxaFJJW1MtlSiNYq98Vp9KyK9RpSkXZYw-4xfxVADeUOWZr4dvhmqV0vq6OTZVa2cz0dhXkqwcbDz2KlSbhrAoIGazHxGEkW-oND_ygZKigvPXlaMctn7c1zE4RZUKczg.Pl8brjSq2VyWqt-gHqBp0g/all-clouds-as-rgb-image-arrays.npy","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"clouds = np.load(\"all-clouds-as-rgb-image-arrays.npy\")\nlabels = np.load(\"corresponding-labels-for-all-clouds-unshuffled.npy\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.save(\"all-clouds-as-rgb-image-arrays\", clouds)\nnp.save(\"corresponding-labels-for-all-clouds-unshuffled\", labels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"shuffle = np.arange(clouds.shape[0])\nnp.random.shuffle(shuffle)\nclouds = clouds[shuffle]\nlabels = labels[shuffle]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"num_classes = len(np.unique(labels)) \nlen_data = len(clouds) ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"(x_train,x_test)=clouds[(int)(0.1*len_data):],clouds[:(int)(0.1*len_data)]\n(y_train,y_test)=labels[(int)(0.1*len_data):],labels[:(int)(0.1*len_data)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Normalizing data\nx_train = x_train.astype(\"float32\") / 255.0\nx_test = x_test.astype(\"float32\") / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# one hot encoding for keras\ny_train = keras.utils.to_categorical(y_train, num_classes)\ny_test = keras.utils.to_categorical(y_test, num_classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"x_train.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(filters=16, kernel_size=2, padding=\"same\", activation=\"relu\", input_shape=(50,50,3)))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=32, kernel_size=2, padding=\"same\", activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=64, kernel_size=2, padding=\"same\", activation=\"relu\"))\nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Dropout(0.2))\nmodel.add(Flatten())\nmodel.add(Dense(500, activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(1000, activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(1000, activation=\"relu\"))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(4, activation=\"softmax\"))\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(loss=\"categorical_crossentropy\",\n               optimizer=\"adam\",\n               metrics=[\"accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.fit(x_train, y_train, batch_size=50, epochs=20, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"accuracy =model.evaluate(x_test, y_test, verbose=1)\nprint(accuracy[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# save model weights\nmodel.save(\"keras-malaria-detection-cnn.h5\")","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":1}