{"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":"2022-11-15T08:12:30.531057Z","iopub.execute_input":"2022-11-15T08:12:30.531716Z","iopub.status.idle":"2022-11-15T08:12:30.538602Z","shell.execute_reply.started":"2022-11-15T08:12:30.531685Z","shell.execute_reply":"2022-11-15T08:12:30.536912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.python.client import device_lib\nprint(device_lib.list_local_devices())","metadata":{"execution":{"iopub.status.busy":"2022-11-15T08:14:49.203880Z","iopub.execute_input":"2022-11-15T08:14:49.204253Z","iopub.status.idle":"2022-11-15T08:14:49.218694Z","shell.execute_reply.started":"2022-11-15T08:14:49.204224Z","shell.execute_reply":"2022-11-15T08:14:49.217682Z"},"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-classification-3/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":"2022-11-15T08:12:30.540761Z","iopub.execute_input":"2022-11-15T08:12:30.541210Z","iopub.status.idle":"2022-11-15T08:12:30.810470Z","shell.execute_reply.started":"2022-11-15T08:12:30.541173Z","shell.execute_reply":"2022-11-15T08:12:30.809425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data['image'] = train_data['id'].apply(lambda x : f'../input/cloud-type-classification-3/images/train/{x}')\nprint(train_data.head())","metadata":{"execution":{"iopub.status.busy":"2022-11-15T08:12:30.811836Z","iopub.execute_input":"2022-11-15T08:12:30.812289Z","iopub.status.idle":"2022-11-15T08:12:30.824325Z","shell.execute_reply.started":"2022-11-15T08:12:30.812253Z","shell.execute_reply":"2022-11-15T08:12:30.822901Z"},"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":"2022-11-15T08:12:30.827316Z","iopub.execute_input":"2022-11-15T08:12:30.828097Z","iopub.status.idle":"2022-11-15T08:13:08.543671Z","shell.execute_reply.started":"2022-11-15T08:12:30.828059Z","shell.execute_reply":"2022-11-15T08:13:08.542295Z"},"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":"2022-11-15T08:13:08.545154Z","iopub.execute_input":"2022-11-15T08:13:08.545592Z","iopub.status.idle":"2022-11-15T08:13:08.558129Z","shell.execute_reply.started":"2022-11-15T08:13:08.545547Z","shell.execute_reply":"2022-11-15T08:13:08.557071Z"},"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":"2022-11-15T08:13:08.559613Z","iopub.execute_input":"2022-11-15T08:13:08.560254Z","iopub.status.idle":"2022-11-15T08:13:12.906981Z","shell.execute_reply.started":"2022-11-15T08:13:08.560215Z","shell.execute_reply":"2022-11-15T08:13:12.904391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.build()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-15T08:13:12.908517Z","iopub.execute_input":"2022-11-15T08:13:12.908965Z","iopub.status.idle":"2022-11-15T08:13:12.918311Z","shell.execute_reply.started":"2022-11-15T08:13:12.908907Z","shell.execute_reply":"2022-11-15T08:13:12.917300Z"},"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":"2022-11-15T08:13:12.919922Z","iopub.execute_input":"2022-11-15T08:13:12.920626Z","iopub.status.idle":"2022-11-15T08:13:13.281648Z","shell.execute_reply.started":"2022-11-15T08:13:12.920591Z","shell.execute_reply":"2022-11-15T08:13:13.280680Z"},"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":"2022-11-15T08:13:13.286340Z","iopub.execute_input":"2022-11-15T08:13:13.286668Z","iopub.status.idle":"2022-11-15T08:13:13.304718Z","shell.execute_reply.started":"2022-11-15T08:13:13.286642Z","shell.execute_reply":"2022-11-15T08:13:13.303868Z"},"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":"2022-11-15T08:15:12.921836Z","iopub.execute_input":"2022-11-15T08:15:12.922561Z","iopub.status.idle":"2022-11-15T08:21:50.135480Z","shell.execute_reply.started":"2022-11-15T08:15:12.922523Z","shell.execute_reply":"2022-11-15T08:21:50.134342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#predict test data\ntest_data = pd.read_csv('../input/cloud-type-classification-3/submit.csv')\nnum_rows = test_data.shape[0]\nprint(num_rows)","metadata":{"execution":{"iopub.status.busy":"2022-11-15T08:21:50.138563Z","iopub.execute_input":"2022-11-15T08:21:50.138998Z","iopub.status.idle":"2022-11-15T08:21:50.153220Z","shell.execute_reply.started":"2022-11-15T08:21:50.138953Z","shell.execute_reply":"2022-11-15T08:21:50.151905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfile_path = '../input/cloud-type-classification-3/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":"2022-11-15T08:21:50.154470Z","iopub.execute_input":"2022-11-15T08:21:50.154895Z","iopub.status.idle":"2022-11-15T08:23:46.615522Z","shell.execute_reply.started":"2022-11-15T08:21:50.154861Z","shell.execute_reply":"2022-11-15T08:23:46.614534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_data.head()) ","metadata":{"execution":{"iopub.status.busy":"2022-11-15T08:23:46.617745Z","iopub.execute_input":"2022-11-15T08:23:46.618109Z","iopub.status.idle":"2022-11-15T08:23:46.626545Z","shell.execute_reply.started":"2022-11-15T08:23:46.618074Z","shell.execute_reply":"2022-11-15T08:23:46.625447Z"},"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":"2022-11-15T08:23:46.628089Z","iopub.execute_input":"2022-11-15T08:23:46.628760Z","iopub.status.idle":"2022-11-15T08:23:46.645543Z","shell.execute_reply.started":"2022-11-15T08:23:46.628722Z","shell.execute_reply":"2022-11-15T08:23:46.644519Z"},"trusted":true},"execution_count":null,"outputs":[]}]}