{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Import library","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom skimage.io import imread\n\nfrom keras.models import Sequential, Model\nfrom keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Input, Flatten, SeparableConv2D\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\n\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:10:14.358642Z","iopub.execute_input":"2021-11-11T08:10:14.358905Z","iopub.status.idle":"2021-11-11T08:10:20.023446Z","shell.execute_reply.started":"2021-11-11T08:10:14.358832Z","shell.execute_reply":"2021-11-11T08:10:20.022711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"train_data = pd.read_csv('../input/cloud-type-classification2/train.csv')\n\n# Get the counts for each class\ncases_count = train_data['label'].value_counts()\nprint(cases_count)\n\n# Plot the results \nplt.figure(figsize=(10,8))\nsns.barplot(x=cases_count.index, y= cases_count.values)\nplt.title('Number of images', fontsize=14)\nplt.xlabel('class', fontsize=12)\nplt.ylabel('Count', fontsize=12)\nplt.xticks(range(len(cases_count.index)), ['0', '1', '2', '3', '4', '5', '6'])\nplt.show()\ndel cases_count","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:10:20.025006Z","iopub.execute_input":"2021-11-11T08:10:20.026255Z","iopub.status.idle":"2021-11-11T08:10:20.391188Z","shell.execute_reply.started":"2021-11-11T08:10:20.026217Z","shell.execute_reply":"2021-11-11T08:10:20.390399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['image'] = train_data['id'].apply(lambda x : f'../input/cloud-type-classification2/images/train/{x}')\nprint(train_data.head())","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:10:20.394716Z","iopub.execute_input":"2021-11-11T08:10:20.395020Z","iopub.status.idle":"2021-11-11T08:10:20.410807Z","shell.execute_reply.started":"2021-11-11T08:10:20.394991Z","shell.execute_reply":"2021-11-11T08:10:20.410190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get few samples for both the classes\nclass0 = (train_data[train_data['label']==0]['image'].iloc[:5]).tolist()\nclass1 = (train_data[train_data['label']==1]['image'].iloc[:5]).tolist()\nclass2 = (train_data[train_data['label']==2]['image'].iloc[:5]).tolist()\nclass3 = (train_data[train_data['label']==3]['image'].iloc[:5]).tolist()\nclass4 = (train_data[train_data['label']==4]['image'].iloc[:5]).tolist()\nclass5 = (train_data[train_data['label']==5]['image'].iloc[:5]).tolist()\nclass6 = (train_data[train_data['label']==6]['image'].iloc[:5]).tolist()\n\n# Concat the data in a single list and del the above variables\nsamples = class0 + class1+ class2+ class3+ class4+ class5+ class6\ndel class0, class1, class2, class3, class4, class5, class6\n\n# Plot the data \nf, ax = plt.subplots(7,5, figsize=(30,30))\nfor i in range(7*5):\n    img = imread(samples[i])\n    ax[i//5, i%5].imshow(img)\n    if i<5:\n        ax[i//5, i%5].set_title(\"0\")\n    elif i<10:\n        ax[i//5, i%5].set_title(\"1\")\n    elif i<15:\n        ax[i//5, i%5].set_title(\"2\")\n    elif i<20:\n        ax[i//5, i%5].set_title(\"3\")\n    elif i<25:\n        ax[i//5, i%5].set_title(\"4\")\n    elif i<30:\n        ax[i//5, i%5].set_title(\"5\")\n    else:\n        ax[i//5, i%5].set_title(\"6\")\n    ax[i//5, i%5].axis('off')\n    ax[i//5, i%5].set_aspect('auto')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:10:20.414397Z","iopub.execute_input":"2021-11-11T08:10:20.416398Z","iopub.status.idle":"2021-11-11T08:10:52.864719Z","shell.execute_reply.started":"2021-11-11T08:10:20.416358Z","shell.execute_reply":"2021-11-11T08:10:52.863863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_class = 7\n\n# Training data generator\ndef data_gen(data, batch_size):\n    # Get total number of samples in the data\n    n = len(data)\n    steps = n//batch_size\n    \n    # Define two numpy arrays for containing batch data and labels\n    batch_data = np.zeros((batch_size, 224, 224, 3), dtype=np.float32)\n    batch_labels = np.zeros((batch_size,num_class), dtype=np.float32)\n\n    # Get a numpy array of all the indices of the input data\n    indices = np.arange(n)\n    \n    # Initialize a counter\n    i =0\n    while True:\n        np.random.shuffle(indices)\n        # Get the next batch \n        count = 0\n        next_batch = indices[(i*batch_size):(i+1)*batch_size]\n        for j, idx in enumerate(next_batch):\n            img_name = data.iloc[idx]['image']\n            label = data.iloc[idx]['label']\n            \n            # one hot encoding\n            encoded_label = to_categorical(label, num_classes=num_class)\n            # read the image and resize\n            img = cv2.imread(str(img_name))\n            img = cv2.resize(img, (224,224))\n            \n            # normalize the image pixels\n            orig_img = img.astype(np.float32)/255.\n            \n            batch_data[count] = orig_img\n            batch_labels[count] = encoded_label\n            \n        i+=1\n        yield batch_data, batch_labels\n            \n        if i>=steps:\n            i=0","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:21:45.563557Z","iopub.execute_input":"2021-11-11T08:21:45.563850Z","iopub.status.idle":"2021-11-11T08:21:45.574029Z","shell.execute_reply.started":"2021-11-11T08:21:45.563817Z","shell.execute_reply":"2021-11-11T08:21:45.573179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set the CNN model \nmodel = Sequential()\n\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu', input_shape = (224,224,3)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same', \n                 activation ='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))\nmodel.add(Dropout(0.25))\n\n\nmodel.add(Flatten())\nmodel.add(Dense(256, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(num_class, activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:22:25.711692Z","iopub.execute_input":"2021-11-11T08:22:25.712309Z","iopub.status.idle":"2021-11-11T08:22:25.786592Z","shell.execute_reply.started":"2021-11-11T08:22:25.712267Z","shell.execute_reply":"2021-11-11T08:22:25.785861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.build()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:22:32.976013Z","iopub.execute_input":"2021-11-11T08:22:32.976277Z","iopub.status.idle":"2021-11-11T08:22:32.989679Z","shell.execute_reply.started":"2021-11-11T08:22:32.976249Z","shell.execute_reply":"2021-11-11T08:22:32.988964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import Adam\n# Define the optimizer\nadam = Adam(learning_rate=0.01)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:22:37.442098Z","iopub.execute_input":"2021-11-11T08:22:37.442875Z","iopub.status.idle":"2021-11-11T08:22:37.448081Z","shell.execute_reply.started":"2021-11-11T08:22:37.442836Z","shell.execute_reply":"2021-11-11T08:22:37.447277Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=adam)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:22:40.569304Z","iopub.execute_input":"2021-11-11T08:22:40.569846Z","iopub.status.idle":"2021-11-11T08:22:40.579591Z","shell.execute_reply.started":"2021-11-11T08:22:40.569809Z","shell.execute_reply":"2021-11-11T08:22:40.578783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nepochs = 3\nbatch_size = 100\ntrain_data_gen = data_gen(data=train_data, batch_size=batch_size)\nmodel.fit(train_data_gen,\n                epochs = epochs,\n                steps_per_epoch=train_data.shape[0] // batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:34:58.529071Z","iopub.execute_input":"2021-11-11T08:34:58.529350Z","iopub.status.idle":"2021-11-11T08:40:15.369125Z","shell.execute_reply.started":"2021-11-11T08:34:58.529321Z","shell.execute_reply":"2021-11-11T08:40:15.368453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predict test data\ntest_data = pd.read_csv('../input/cloud-type-classification2/submit.csv')\nnum_rows = test_data.shape[0]\nprint(num_rows)","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:43:12.264328Z","iopub.execute_input":"2021-11-11T08:43:12.264600Z","iopub.status.idle":"2021-11-11T08:43:12.276355Z","shell.execute_reply.started":"2021-11-11T08:43:12.264570Z","shell.execute_reply":"2021-11-11T08:43:12.275627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfile_path = '../input/cloud-type-classification2/images/test'      \nfor i in range(num_rows):\n    img = cv2.imread(os.path.join(file_path, test_data.iloc[i,0]))\n    img = cv2.resize(img, (224,224))\n    img = img.astype(np.float32)/255.\n    img = np.array(img)\n    img = np.expand_dims(img, axis=0)\n    predict = model.predict(img)\n    predict = np.argmax(predict)\n    test_data.loc[i, 'predict'] = predict\n    ","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:49:34.806466Z","iopub.execute_input":"2021-11-11T08:49:34.806905Z","iopub.status.idle":"2021-11-11T08:51:37.475299Z","shell.execute_reply.started":"2021-11-11T08:49:34.806850Z","shell.execute_reply":"2021-11-11T08:51:37.474492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_data.head()) ","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:54:13.756730Z","iopub.execute_input":"2021-11-11T08:54:13.757039Z","iopub.status.idle":"2021-11-11T08:54:13.766552Z","shell.execute_reply.started":"2021-11-11T08:54:13.757007Z","shell.execute_reply":"2021-11-11T08:54:13.765775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.to_csv('../working/submit.csv', index=False)\nfrom IPython.display import FileLink\nFileLink(r'submit.csv')","metadata":{"execution":{"iopub.status.busy":"2021-11-11T08:55:46.158817Z","iopub.execute_input":"2021-11-11T08:55:46.159538Z","iopub.status.idle":"2021-11-11T08:55:46.171426Z","shell.execute_reply.started":"2021-11-11T08:55:46.159503Z","shell.execute_reply":"2021-11-11T08:55:46.170685Z"},"trusted":true},"execution_count":null,"outputs":[]}]}