{"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-12-13T18:43:04.621861Z","iopub.execute_input":"2021-12-13T18:43:04.622348Z","iopub.status.idle":"2021-12-13T18:43:10.429877Z","shell.execute_reply.started":"2021-12-13T18:43:04.622193Z","shell.execute_reply":"2021-12-13T18:43:10.428964Z"},"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-12-13T18:43:10.436169Z","iopub.execute_input":"2021-12-13T18:43:10.436725Z","iopub.status.idle":"2021-12-13T18:43:10.736494Z","shell.execute_reply.started":"2021-12-13T18:43:10.436683Z","shell.execute_reply":"2021-12-13T18:43:10.735824Z"},"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}')\n# print(train_data.head())","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:44:15.144073Z","iopub.execute_input":"2021-12-13T18:44:15.146566Z","iopub.status.idle":"2021-12-13T18:44:15.151782Z","shell.execute_reply.started":"2021-12-13T18:44:15.146516Z","shell.execute_reply":"2021-12-13T18:44:15.151061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get few samples for both the classes\n# class0 = (train_data[train_data['label']==0]['image'].iloc[:5]).tolist()\n# class1 = (train_data[train_data['label']==1]['image'].iloc[:5]).tolist()\n# class2 = (train_data[train_data['label']==2]['image'].iloc[:5]).tolist()\n# class3 = (train_data[train_data['label']==3]['image'].iloc[:5]).tolist()\n# class4 = (train_data[train_data['label']==4]['image'].iloc[:5]).tolist()\n# class5 = (train_data[train_data['label']==5]['image'].iloc[:5]).tolist()\n# class6 = (train_data[train_data['label']==6]['image'].iloc[:5]).tolist()\n\n# # Concat the data in a single list and del the above variables\n# samples = class0 + class1+ class2+ class3+ class4+ class5+ class6\n# del class0, class1, class2, class3, class4, class5, class6\n\n# # Plot the data \n# f, ax = plt.subplots(7,5, figsize=(30,30))\n# for 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')\n# plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:44:15.404274Z","iopub.execute_input":"2021-12-13T18:44:15.404793Z","iopub.status.idle":"2021-12-13T18:44:15.409315Z","shell.execute_reply.started":"2021-12-13T18:44:15.404758Z","shell.execute_reply":"2021-12-13T18:44:15.408372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# num_class = 7\n\n# # Training data generator\n# def 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-12-13T18:44:15.811766Z","iopub.execute_input":"2021-12-13T18:44:15.812003Z","iopub.status.idle":"2021-12-13T18:44:15.817237Z","shell.execute_reply.started":"2021-12-13T18:44:15.811975Z","shell.execute_reply":"2021-12-13T18:44:15.816268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()\ntrain_data[\"image\"][0]","metadata":{"execution":{"iopub.status.busy":"2021-12-13T19:05:45.49435Z","iopub.execute_input":"2021-12-13T19:05:45.494636Z","iopub.status.idle":"2021-12-13T19:05:45.501007Z","shell.execute_reply.started":"2021-12-13T19:05:45.494605Z","shell.execute_reply":"2021-12-13T19:05:45.500207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def data_gen(train_data):\n    images = []\n    for i in range(len(train_data)):\n        image = cv2.imread(train_data[\"image\"][i])\n        image = cv2.resize(image,(220,220))\n        image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)\n        image = image/255.0\n        images.append(image)\n    return np.array(images)\ntrain_data_gen = data_gen(train_data=train_data)","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:43:21.689119Z","iopub.execute_input":"2021-12-13T18:43:21.689605Z","iopub.status.idle":"2021-12-13T18:44:09.663058Z","shell.execute_reply.started":"2021-12-13T18:43:21.689561Z","shell.execute_reply":"2021-12-13T18:44:09.661895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ndatagen = ImageDataGenerator(\n    rotation_range = 0.2,\n    horizontal_flip = True,\n    zoom_range = 0.2  \n)\n\ndatagen.fit(train_data_gen)\n# tf.keras.preprocessing.image.ImageDataGenerator(\n#     featurewise_center=False, samplewise_center=False,\n#     featurewise_std_normalization=False, samplewise_std_normalization=False,\n#     zca_whitening=False, zca_epsilon=1e-06, rotation_range=0, width_shift_range=0.0,\n#     height_shift_range=0.0, brightness_range=None, shear_range=0.0, zoom_range=0.0,\n#     channel_shift_range=0.0, fill_mode='nearest', cval=0.0,\n#     horizontal_flip=False, vertical_flip=False, rescale=None,\n#     preprocessing_function=None, data_format=None, validation_split=0.0, dtype=None\n# )","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:44:09.664075Z","iopub.status.idle":"2021-12-13T18:44:09.665372Z","shell.execute_reply.started":"2021-12-13T18:44:09.665108Z","shell.execute_reply":"2021-12-13T18:44:09.665137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical\ny_train = to_categorical(train_data[\"label\"],7)","metadata":{"execution":{"iopub.status.busy":"2021-12-13T18:44:09.666716Z","iopub.status.idle":"2021-12-13T18:44:09.667366Z","shell.execute_reply.started":"2021-12-13T18:44:09.66712Z","shell.execute_reply":"2021-12-13T18:44:09.667145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.xception import Xception\nfrom tensorflow.keras.layers import Flatten,Dense,Dropout\nfrom keras import Model\nmobile = Xception(input_shape=(220,220,3),include_top=False,weights=\"imagenet\")\n\nfor layer in mobile.layers:\n    layer.trainable =False\n    \nx = Flatten()(mobile.output)\nx = Dense(1024,activation=\"relu\")(x)\nx = Dense(1024,activation=\"relu\")(x)\nx = Dropout(0.2)(x)\nx = Dense(512,activation=\"relu\")(x)\nx = Dense(512,activation=\"relu\")(x)\nx = Dense(256,activation=\"relu\")(x)\nx = Dense(7,activation=\"softmax\")(x)\n\nmodel = Model(mobile.input,x)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:13:35.589986Z","iopub.execute_input":"2021-11-30T06:13:35.590373Z","iopub.status.idle":"2021-11-30T06:13:39.512608Z","shell.execute_reply.started":"2021-11-30T06:13:35.590336Z","shell.execute_reply":"2021-11-30T06:13:39.511927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.optimizers import SGD\nepochs = 100\nlrate = 0.01\ndecay = lrate/epochs\nsgd = SGD(learning_rate=lrate, momentum=0.9, decay=decay, nesterov=False)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:13:39.513744Z","iopub.execute_input":"2021-11-30T06:13:39.515872Z","iopub.status.idle":"2021-11-30T06:13:39.523359Z","shell.execute_reply.started":"2021-11-30T06:13:39.51584Z","shell.execute_reply":"2021-11-30T06:13:39.522624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=sgd)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:13:39.526891Z","iopub.execute_input":"2021-11-30T06:13:39.527098Z","iopub.status.idle":"2021-11-30T06:13:39.542343Z","shell.execute_reply.started":"2021-11-30T06:13:39.527076Z","shell.execute_reply":"2021-11-30T06:13:39.54152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nepochs = 20\nbatch_size = 128\nmodel.fit(datagen.flow(train_data_gen,y_train,batch_size=64),batch_size=batch_size,epochs = epochs)","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:13:39.544466Z","iopub.execute_input":"2021-11-30T06:13:39.545137Z","iopub.status.idle":"2021-11-30T06:17:08.720732Z","shell.execute_reply.started":"2021-11-30T06:13:39.545102Z","shell.execute_reply":"2021-11-30T06:17:08.720049Z"},"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-30T06:17:08.722208Z","iopub.execute_input":"2021-11-30T06:17:08.722463Z","iopub.status.idle":"2021-11-30T06:17:08.742097Z","shell.execute_reply.started":"2021-11-30T06:17:08.722427Z","shell.execute_reply":"2021-11-30T06:17:08.741262Z"},"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,(220,220))\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\ntest_data[\"predict\"]=test_data[\"predict\"].astype(\"int64\")","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:17:08.743493Z","iopub.execute_input":"2021-11-30T06:17:08.74385Z","iopub.status.idle":"2021-11-30T06:19:01.467061Z","shell.execute_reply.started":"2021-11-30T06:17:08.74382Z","shell.execute_reply":"2021-11-30T06:19:01.466295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data","metadata":{"execution":{"iopub.status.busy":"2021-11-30T06:19:01.468403Z","iopub.execute_input":"2021-11-30T06:19:01.468684Z","iopub.status.idle":"2021-11-30T06:19:01.483175Z","shell.execute_reply.started":"2021-11-30T06:19:01.468649Z","shell.execute_reply":"2021-11-30T06:19:01.482388Z"},"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-30T06:19:01.486446Z","iopub.execute_input":"2021-11-30T06:19:01.486846Z","iopub.status.idle":"2021-11-30T06:19:01.498093Z","shell.execute_reply.started":"2021-11-30T06:19:01.486819Z","shell.execute_reply":"2021-11-30T06:19:01.497488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}