gyuhyong 9 months ago
commit
2215e65407
45 changed files with 182929 additions and 0 deletions
  1. BIN
      1_Plant Seedlings Classification_0.97/.DS_Store
  2. 275
    0
      1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/15.10_Kaggle_Plants_Train_VGG-checkpoint.ipynb
  3. 268
    0
      1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/15.3_Kaggle_Plants_Seperate-checkpoint.ipynb
  4. 171
    0
      1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/15.5_Kaggle_Plants_Predict-checkpoint.ipynb
  5. 145
    0
      1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/15.7_Kaggle_Plants_Normalize-checkpoint.ipynb
  6. 300
    0
      1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/15.8_Kaggle_Plants_Train_Normalized-checkpoint.ipynb
  7. 202
    0
      1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/1_3rd_DataAugmentation_and_Resize-checkpoint.ipynb
  8. 111
    0
      1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/2_3rd_categorical_normalize-checkpoint.ipynb
  9. 117
    0
      1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/3_3rd_Train-checkpoint.ipynb
  10. 69
    0
      1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/4_3rd_Testset-checkpoint.ipynb
  11. 148
    0
      1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/5_3rd_predict-checkpoint.ipynb
  12. 150
    0
      1_Plant Seedlings Classification_0.97/1_3rd_DataAugmentation_and_Resize.ipynb
  13. 77
    0
      1_Plant Seedlings Classification_0.97/1_3rd_DataAugmentation_and_Resize.py
  14. 130
    0
      1_Plant Seedlings Classification_0.97/2_3rd_categorical_normalize.ipynb
  15. 69
    0
      1_Plant Seedlings Classification_0.97/2_3rd_categorical_normalize.py
  16. 117
    0
      1_Plant Seedlings Classification_0.97/3_3rd_Train.ipynb
  17. 80
    0
      1_Plant Seedlings Classification_0.97/3_3rd_Train.py
  18. 108
    0
      1_Plant Seedlings Classification_0.97/4_3rd_Testset.ipynb
  19. 61
    0
      1_Plant Seedlings Classification_0.97/4_3rd_Testset.py
  20. 148
    0
      1_Plant Seedlings Classification_0.97/5_3rd_predict.ipynb
  21. 1
    0
      1_Plant Seedlings Classification_0.97/model.json
  22. 795
    0
      1_Plant Seedlings Classification_0.97/result_1.csv
  23. 795
    0
      1_Plant Seedlings Classification_0.97/result_2.csv
  24. 535
    0
      2_MNIST_0.99/.ipynb_checkpoints/1_MNIST_Train-checkpoint.ipynb
  25. 535
    0
      2_MNIST_0.99/1_MNIST_Train.ipynb
  26. 131
    0
      2_MNIST_0.99/2_MNIST_Predict.ipynb
  27. 1
    0
      2_MNIST_0.99/model.json
  28. 28001
    0
      2_MNIST_0.99/result_1.csv
  29. 28001
    0
      2_MNIST_0.99/result_2.csv
  30. 28001
    0
      2_MNIST_0.99/sample.csv
  31. 28001
    0
      2_MNIST_0.99/test.csv
  32. 42001
    0
      2_MNIST_0.99/train.csv
  33. BIN
      3_Dog_Breed_Identification/.DS_Store
  34. 264
    0
      3_Dog_Breed_Identification/1_data.ipynb
  35. 153
    0
      3_Dog_Breed_Identification/1_data.py
  36. 122
    0
      3_Dog_Breed_Identification/2_normalize.ipynb
  37. 86
    0
      3_Dog_Breed_Identification/2_normalize.py
  38. 105
    0
      3_Dog_Breed_Identification/3_testset.ipynb
  39. 1815
    0
      3_Dog_Breed_Identification/4_train.ipynb
  40. 75
    0
      3_Dog_Breed_Identification/4_train.py
  41. 182
    0
      3_Dog_Breed_Identification/Untitled.ipynb
  42. 10223
    0
      3_Dog_Breed_Identification/labels.csv
  43. 1
    0
      3_Dog_Breed_Identification/model.json
  44. 1
    0
      3_Dog_Breed_Identification/result.csv
  45. 10358
    0
      3_Dog_Breed_Identification/sample_submission.csv

BIN
1_Plant Seedlings Classification_0.97/.DS_Store View File


+ 275
- 0
1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/15.10_Kaggle_Plants_Train_VGG-checkpoint.ipynb View File

@@ -0,0 +1,275 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "raw",
5
+   "metadata": {},
6
+   "source": [
7
+    "In this file I will build the Neural Network and train it"
8
+   ]
9
+  },
10
+  {
11
+   "cell_type": "code",
12
+   "execution_count": 1,
13
+   "metadata": {},
14
+   "outputs": [
15
+    {
16
+     "name": "stderr",
17
+     "output_type": "stream",
18
+     "text": [
19
+      "/usr/local/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
20
+      "  from ._conv import register_converters as _register_converters\n",
21
+      "Using TensorFlow backend.\n"
22
+     ]
23
+    }
24
+   ],
25
+   "source": [
26
+    "import keras\n",
27
+    "from keras.datasets import cifar10\n",
28
+    "from keras.models import Model, Sequential\n",
29
+    "from keras.layers import Dense, Dropout, Flatten, Input, AveragePooling2D, merge, Activation\n",
30
+    "from keras.layers import Conv2D, MaxPooling2D, BatchNormalization, Lambda, MaxPool2D\n",
31
+    "from keras.layers import Concatenate\n",
32
+    "from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n",
33
+    "from keras.optimizers import Adam\n",
34
+    "import numpy as np\n",
35
+    "import os"
36
+   ]
37
+  },
38
+  {
39
+   "cell_type": "code",
40
+   "execution_count": 2,
41
+   "metadata": {},
42
+   "outputs": [],
43
+   "source": [
44
+    "# Hyperparameters\n",
45
+    "batch_size = 20\n",
46
+    "num_classes = 12\n",
47
+    "epochs = 50"
48
+   ]
49
+  },
50
+  {
51
+   "cell_type": "code",
52
+   "execution_count": 3,
53
+   "metadata": {},
54
+   "outputs": [],
55
+   "source": [
56
+    "#Load Data\n",
57
+    "class_name = np.load(\"class_name.npy\")\n",
58
+    "x_list = []\n",
59
+    "\n",
60
+    "file_list = os.listdir(\".\")\n",
61
+    "\n",
62
+    "for file in file_list:\n",
63
+    "    if file.startswith(\"x_train\") and file.endswith(\".npy\"):\n",
64
+    "        x_list.append(file)"
65
+   ]
66
+  },
67
+  {
68
+   "cell_type": "code",
69
+   "execution_count": 4,
70
+   "metadata": {},
71
+   "outputs": [],
72
+   "source": [
73
+    "model = Sequential()\n",
74
+    "\n",
75
+    "model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same', \n",
76
+    "                 activation ='relu', input_shape = (300,300,3)))\n",
77
+    "model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same', \n",
78
+    "                 activation ='relu'))\n",
79
+    "model.add(MaxPool2D(pool_size=(2,2)))\n",
80
+    "model.add(Dropout(0.25))\n",
81
+    "\n",
82
+    "model.add(Conv2D(filters = 256, kernel_size = (3,3),padding = 'Same', \n",
83
+    "                 activation ='relu'))\n",
84
+    "model.add(Conv2D(filters = 256, kernel_size = (3,3),padding = 'Same', \n",
85
+    "                 activation ='relu'))\n",
86
+    "model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n",
87
+    "model.add(Dropout(0.25))\n",
88
+    "\n",
89
+    "model.add(Conv2D(filters = 512, kernel_size = (3,3),padding = 'Same', \n",
90
+    "                 activation ='relu'))\n",
91
+    "model.add(Conv2D(filters = 512, kernel_size = (3,3),padding = 'Same', \n",
92
+    "                 activation ='relu'))\n",
93
+    "model.add(Conv2D(filters = 10, kernel_size = (3,3),padding = 'Same', \n",
94
+    "                 activation ='relu'))\n",
95
+    "model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n",
96
+    "model.add(Dropout(0.25))\n",
97
+    "\n",
98
+    "model.add(Flatten())\n",
99
+    "model.add(Dense(240, activation = \"relu\"))\n",
100
+    "model.add(Dropout(0.3))\n",
101
+    "model.add(Dense(num_classes, activation = \"softmax\"))"
102
+   ]
103
+  },
104
+  {
105
+   "cell_type": "code",
106
+   "execution_count": 5,
107
+   "metadata": {},
108
+   "outputs": [
109
+    {
110
+     "name": "stdout",
111
+     "output_type": "stream",
112
+     "text": [
113
+      "_________________________________________________________________\n",
114
+      "Layer (type)                 Output Shape              Param #   \n",
115
+      "=================================================================\n",
116
+      "conv2d_1 (Conv2D)            (None, 300, 300, 128)     3584      \n",
117
+      "_________________________________________________________________\n",
118
+      "conv2d_2 (Conv2D)            (None, 300, 300, 128)     147584    \n",
119
+      "_________________________________________________________________\n",
120
+      "max_pooling2d_1 (MaxPooling2 (None, 150, 150, 128)     0         \n",
121
+      "_________________________________________________________________\n",
122
+      "dropout_1 (Dropout)          (None, 150, 150, 128)     0         \n",
123
+      "_________________________________________________________________\n",
124
+      "conv2d_3 (Conv2D)            (None, 150, 150, 256)     295168    \n",
125
+      "_________________________________________________________________\n",
126
+      "conv2d_4 (Conv2D)            (None, 150, 150, 256)     590080    \n",
127
+      "_________________________________________________________________\n",
128
+      "max_pooling2d_2 (MaxPooling2 (None, 75, 75, 256)       0         \n",
129
+      "_________________________________________________________________\n",
130
+      "dropout_2 (Dropout)          (None, 75, 75, 256)       0         \n",
131
+      "_________________________________________________________________\n",
132
+      "conv2d_5 (Conv2D)            (None, 75, 75, 512)       1180160   \n",
133
+      "_________________________________________________________________\n",
134
+      "conv2d_6 (Conv2D)            (None, 75, 75, 512)       2359808   \n",
135
+      "_________________________________________________________________\n",
136
+      "conv2d_7 (Conv2D)            (None, 75, 75, 512)       2359808   \n",
137
+      "_________________________________________________________________\n",
138
+      "max_pooling2d_3 (MaxPooling2 (None, 37, 37, 512)       0         \n",
139
+      "_________________________________________________________________\n",
140
+      "dropout_3 (Dropout)          (None, 37, 37, 512)       0         \n",
141
+      "_________________________________________________________________\n",
142
+      "conv2d_8 (Conv2D)            (None, 37, 37, 1)         4609      \n",
143
+      "_________________________________________________________________\n",
144
+      "max_pooling2d_4 (MaxPooling2 (None, 18, 18, 1)         0         \n",
145
+      "_________________________________________________________________\n",
146
+      "flatten_1 (Flatten)          (None, 324)               0         \n",
147
+      "_________________________________________________________________\n",
148
+      "dense_1 (Dense)              (None, 12)                3900      \n",
149
+      "=================================================================\n",
150
+      "Total params: 6,944,701\n",
151
+      "Trainable params: 6,944,701\n",
152
+      "Non-trainable params: 0\n",
153
+      "_________________________________________________________________\n"
154
+     ]
155
+    }
156
+   ],
157
+   "source": [
158
+    "model.summary()"
159
+   ]
160
+  },
161
+  {
162
+   "cell_type": "code",
163
+   "execution_count": 6,
164
+   "metadata": {},
165
+   "outputs": [],
166
+   "source": [
167
+    "# determine Loss function and Optimizer\n",
168
+    "model.compile(loss='categorical_crossentropy',\n",
169
+    "              optimizer=Adam(),\n",
170
+    "              metrics=['accuracy'])\n",
171
+    "\n",
172
+    "# Convert model into JSON Format\n",
173
+    "model_json = model.to_json()\n",
174
+    "with open(\"vgg_model.json\", \"w\") as json_file:\n",
175
+    "    json_file.write(model_json)"
176
+   ]
177
+  },
178
+  {
179
+   "cell_type": "code",
180
+   "execution_count": 7,
181
+   "metadata": {},
182
+   "outputs": [],
183
+   "source": [
184
+    "learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n",
185
+    "                                            patience=3, \n",
186
+    "                                            verbose=1, \n",
187
+    "                                            factor=0.5, \n",
188
+    "                                            min_lr=0.00001)\n",
189
+    "\n",
190
+    "filepath=\"VGG_weights-improvement-{val_acc:.4f}.hdf5\"\n",
191
+    "checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')\n",
192
+    "\n",
193
+    "callbacks_list = [checkpoint, learning_rate_reduction]"
194
+   ]
195
+  },
196
+  {
197
+   "cell_type": "code",
198
+   "execution_count": null,
199
+   "metadata": {},
200
+   "outputs": [
201
+    {
202
+     "name": "stdout",
203
+     "output_type": "stream",
204
+     "text": [
205
+      "Train on 1080 samples, validate on 120 samples\n",
206
+      "Epoch 1/5\n",
207
+      "1060/1080 [============================>.] - ETA: 0s - loss: 2.5020 - acc: 0.0764\n",
208
+      "Epoch 00001: val_acc improved from -inf to 0.03333, saving model to VGG_weights-improvement-0.0333.hdf5\n",
209
+      "1080/1080 [==============================] - 50s 46ms/step - loss: 2.5017 - acc: 0.0759 - val_loss: 2.4859 - val_acc: 0.0333\n",
210
+      "Epoch 2/5\n",
211
+      "1060/1080 [============================>.] - ETA: 0s - loss: 2.4851 - acc: 0.0792\n",
212
+      "Epoch 00002: val_acc did not improve\n",
213
+      "1080/1080 [==============================] - 43s 40ms/step - loss: 2.4851 - acc: 0.0806 - val_loss: 2.4864 - val_acc: 0.0333\n",
214
+      "Epoch 3/5\n",
215
+      " 840/1080 [======================>.......] - ETA: 9s - loss: 2.4848 - acc: 0.0976 "
216
+     ]
217
+    }
218
+   ],
219
+   "source": [
220
+    "for i in range(int(epochs/10)):\n",
221
+    "    for file in x_list:\n",
222
+    "        x_train = np.load(file)\n",
223
+    "        y_name = \"y\" + file[1:]\n",
224
+    "    #     print(y_name)\n",
225
+    "        y_train = np.load(y_name)\n",
226
+    "\n",
227
+    "        model.fit(x_train, y_train,\n",
228
+    "                        batch_size=batch_size,\n",
229
+    "                        epochs=int(epochs/10),\n",
230
+    "                        verbose=1,\n",
231
+    "                         validation_split=0.1,\n",
232
+    "                        callbacks=callbacks_list)"
233
+   ]
234
+  },
235
+  {
236
+   "cell_type": "code",
237
+   "execution_count": null,
238
+   "metadata": {},
239
+   "outputs": [],
240
+   "source": [
241
+    "# Save the trained weights in to .h5 format\n",
242
+    "model.save_weights(\"finished_{}.hdf5\".format(epochs))\n",
243
+    "print(\"Saved model to disk\")"
244
+   ]
245
+  },
246
+  {
247
+   "cell_type": "code",
248
+   "execution_count": null,
249
+   "metadata": {},
250
+   "outputs": [],
251
+   "source": []
252
+  }
253
+ ],
254
+ "metadata": {
255
+  "kernelspec": {
256
+   "display_name": "Python 3",
257
+   "language": "python",
258
+   "name": "python3"
259
+  },
260
+  "language_info": {
261
+   "codemirror_mode": {
262
+    "name": "ipython",
263
+    "version": 3
264
+   },
265
+   "file_extension": ".py",
266
+   "mimetype": "text/x-python",
267
+   "name": "python",
268
+   "nbconvert_exporter": "python",
269
+   "pygments_lexer": "ipython3",
270
+   "version": "3.6.4"
271
+  }
272
+ },
273
+ "nbformat": 4,
274
+ "nbformat_minor": 2
275
+}

+ 268
- 0
1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/15.3_Kaggle_Plants_Seperate-checkpoint.ipynb View File

@@ -0,0 +1,268 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "markdown",
5
+   "metadata": {},
6
+   "source": [
7
+    "I am going to seperate the dataset into training set and the testing set\n",
8
+    "\n",
9
+    "As mentioned in previous file\n",
10
+    "\n",
11
+    "I will use 90% for training set and 10% for testing set"
12
+   ]
13
+  },
14
+  {
15
+   "cell_type": "code",
16
+   "execution_count": 1,
17
+   "metadata": {},
18
+   "outputs": [],
19
+   "source": [
20
+    "import os\n",
21
+    "import cv2\n",
22
+    "import numpy as np\n",
23
+    "from random import shuffle"
24
+   ]
25
+  },
26
+  {
27
+   "cell_type": "code",
28
+   "execution_count": 2,
29
+   "metadata": {},
30
+   "outputs": [
31
+    {
32
+     "name": "stdout",
33
+     "output_type": "stream",
34
+     "text": [
35
+      "['Cleavers', 'Sugar beet', 'Common Chickweed', 'Loose Silky-bent', 'Scentless Mayweed', 'Shepherds Purse', 'Fat Hen', 'Common wheat', 'Black-grass', 'Small-flowered Cranesbill', 'Charlock', 'Maize']\n",
36
+      "Cleavers\n",
37
+      "Sugar beet\n",
38
+      "Common Chickweed\n",
39
+      "Loose Silky-bent\n",
40
+      "Scentless Mayweed\n",
41
+      "Shepherds Purse\n",
42
+      "Fat Hen\n",
43
+      "Common wheat\n",
44
+      "Black-grass\n",
45
+      "Small-flowered Cranesbill\n",
46
+      "Charlock\n",
47
+      "Maize\n",
48
+      "12\n"
49
+     ]
50
+    }
51
+   ],
52
+   "source": [
53
+    "data_dir = \"plants/resized/\"\n",
54
+    "class_list = os.listdir(data_dir)\n",
55
+    "class_name = []\n",
56
+    "for name in class_list:\n",
57
+    "    if not name.startswith('.'):\n",
58
+    "        class_name.append(name)\n",
59
+    "        \n",
60
+    "print(class_name)\n",
61
+    "\n",
62
+    "for name in class_name:\n",
63
+    "    print(name)\n",
64
+    "    \n",
65
+    "num_classes = len(class_name)\n",
66
+    "print(num_classes)\n",
67
+    "np.save(\"class_name\", class_name)"
68
+   ]
69
+  },
70
+  {
71
+   "cell_type": "code",
72
+   "execution_count": 3,
73
+   "metadata": {},
74
+   "outputs": [
75
+    {
76
+     "name": "stdout",
77
+     "output_type": "stream",
78
+     "text": [
79
+      "900 100\n",
80
+      "1800 200\n",
81
+      "2700 300\n",
82
+      "3600 400\n",
83
+      "4500 500\n",
84
+      "5400 600\n",
85
+      "6300 700\n",
86
+      "7200 800\n",
87
+      "8100 900\n",
88
+      "9000 1000\n",
89
+      "9900 1100\n",
90
+      "10800 1200\n"
91
+     ]
92
+    }
93
+   ],
94
+   "source": [
95
+    "x_train = []\n",
96
+    "y_train = []\n",
97
+    "x_test = []\n",
98
+    "y_test = []\n",
99
+    "temp_train = []\n",
100
+    "temp_test = []\n",
101
+    "\n",
102
+    "for name in class_name:\n",
103
+    "    class_dir = data_dir + name + \"/\"\n",
104
+    "    file_list = os.listdir(class_dir)\n",
105
+    "    i = 0\n",
106
+    "    \n",
107
+    "    for file in file_list:\n",
108
+    "        file_dir = class_dir + file\n",
109
+    "        image = cv2.imread(file_dir) \n",
110
+    "#         image = image.astype('float16')/255\n",
111
+    "        \n",
112
+    "        if i < 900:\n",
113
+    "            temp_train.append([image, name])\n",
114
+    "        else:\n",
115
+    "            temp_test.append([image,name])\n",
116
+    "        \n",
117
+    "        i += 1\n",
118
+    "        \n",
119
+    "    print(len(temp_train), len(temp_test))\n",
120
+    "    \n",
121
+    "shuffle(temp_train)\n",
122
+    "shuffle(temp_test)"
123
+   ]
124
+  },
125
+  {
126
+   "cell_type": "code",
127
+   "execution_count": 4,
128
+   "metadata": {},
129
+   "outputs": [],
130
+   "source": [
131
+    "y_train = [i[1] for i in temp_train]\n",
132
+    "y_test = [i[1] for i in temp_test]"
133
+   ]
134
+  },
135
+  {
136
+   "cell_type": "code",
137
+   "execution_count": 5,
138
+   "metadata": {},
139
+   "outputs": [
140
+    {
141
+     "name": "stdout",
142
+     "output_type": "stream",
143
+     "text": [
144
+      "Black-grass\n",
145
+      "Loose Silky-bent\n",
146
+      "Black-grass\n",
147
+      "Charlock\n",
148
+      "Black-grass\n",
149
+      "Shepherds Purse\n",
150
+      "Charlock\n",
151
+      "Common Chickweed\n",
152
+      "Loose Silky-bent\n",
153
+      "[0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]\n",
154
+      "[0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
155
+      "[0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]\n",
156
+      "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]\n",
157
+      "[0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]\n",
158
+      "[0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]\n",
159
+      "[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.]\n",
160
+      "[0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]\n",
161
+      "[0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]\n"
162
+     ]
163
+    }
164
+   ],
165
+   "source": [
166
+    "for i in range(9):\n",
167
+    "    print(y_train[i])\n",
168
+    "\n",
169
+    "# convert to one hot encoing \n",
170
+    "# print(class_name, len(class_name))\n",
171
+    "train_hot = np.zeros([len(y_train), num_classes])\n",
172
+    "test_hot = np.zeros([len(y_test), num_classes])\n",
173
+    "\n",
174
+    "for i in range(len(y_train)):\n",
175
+    "    j = 0\n",
176
+    "    for name in class_name:\n",
177
+    "        if y_train[i] == name:\n",
178
+    "            break\n",
179
+    "        else:\n",
180
+    "            j += 1\n",
181
+    "    \n",
182
+    "    train_hot[i][j] = 1   \n",
183
+    "    \n",
184
+    "for i in range(len(y_test)):\n",
185
+    "    j = 0\n",
186
+    "    \n",
187
+    "    for name in class_name:\n",
188
+    "        if y_test[i] == name:\n",
189
+    "            break\n",
190
+    "        else:\n",
191
+    "            j += 1\n",
192
+    "    \n",
193
+    "    test_hot[i][j] = 1   \n",
194
+    "\n",
195
+    "y_train = train_hot\n",
196
+    "y_test = test_hot\n",
197
+    "    \n",
198
+    "for i in range(9):\n",
199
+    "    print(y_train[i])"
200
+   ]
201
+  },
202
+  {
203
+   "cell_type": "code",
204
+   "execution_count": 6,
205
+   "metadata": {},
206
+   "outputs": [],
207
+   "source": [
208
+    "np.save(\"y_train.npy\",y_train)\n",
209
+    "np.save(\"y_test.npy\",y_test)\n",
210
+    "y_train = []\n",
211
+    "y_test = []"
212
+   ]
213
+  },
214
+  {
215
+   "cell_type": "code",
216
+   "execution_count": 7,
217
+   "metadata": {},
218
+   "outputs": [],
219
+   "source": [
220
+    "temp_test = np.array([i[0] for i in temp_test]).reshape(-1,300,300,3)\n",
221
+    "np.save(\"x_test.npy\",temp_test)\n",
222
+    "temp_test = []"
223
+   ]
224
+  },
225
+  {
226
+   "cell_type": "code",
227
+   "execution_count": 8,
228
+   "metadata": {},
229
+   "outputs": [],
230
+   "source": [
231
+    "temp_train = np.array([i[0] for i in temp_train]).reshape(-1,300,300,3)\n",
232
+    "np.save(\"x_train.npy\",temp_train)"
233
+   ]
234
+  },
235
+  {
236
+   "cell_type": "markdown",
237
+   "metadata": {},
238
+   "source": [
239
+    "Now I have seperated the Dataset into Training and Test set and into input and output\n",
240
+    "\n",
241
+    "time to save it into the disk so I can reload the file into the memory without doing all the processing\n",
242
+    "\n",
243
+    "Beofre Save it into the file, I want to convert to one-hot encoding and normalize input"
244
+   ]
245
+  }
246
+ ],
247
+ "metadata": {
248
+  "kernelspec": {
249
+   "display_name": "Python 3",
250
+   "language": "python",
251
+   "name": "python3"
252
+  },
253
+  "language_info": {
254
+   "codemirror_mode": {
255
+    "name": "ipython",
256
+    "version": 3
257
+   },
258
+   "file_extension": ".py",
259
+   "mimetype": "text/x-python",
260
+   "name": "python",
261
+   "nbconvert_exporter": "python",
262
+   "pygments_lexer": "ipython3",
263
+   "version": "3.6.4"
264
+  }
265
+ },
266
+ "nbformat": 4,
267
+ "nbformat_minor": 2
268
+}

+ 171
- 0
1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/15.5_Kaggle_Plants_Predict-checkpoint.ipynb View File

@@ -0,0 +1,171 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": 11,
6
+   "metadata": {},
7
+   "outputs": [],
8
+   "source": [
9
+    "import keras\n",
10
+    "from keras.datasets import mnist\n",
11
+    "from keras.models import model_from_json\n",
12
+    "from keras.optimizers import Adam\n",
13
+    "import numpy as np\n",
14
+    "import cv2\n",
15
+    "import os"
16
+   ]
17
+  },
18
+  {
19
+   "cell_type": "code",
20
+   "execution_count": 12,
21
+   "metadata": {},
22
+   "outputs": [],
23
+   "source": [
24
+    "# this part will prevent tensorflow to allocate all the avaliable GPU Memory\n",
25
+    "# backend\n",
26
+    "import tensorflow as tf\n",
27
+    "from keras import backend as k\n",
28
+    "\n",
29
+    "# Don't pre-allocate memory; allocate as-needed\n",
30
+    "config = tf.ConfigProto()\n",
31
+    "config.gpu_options.allow_growth = True\n",
32
+    "\n",
33
+    "# Create a session with the above options specified.\n",
34
+    "k.tensorflow_backend.set_session(tf.Session(config=config))"
35
+   ]
36
+  },
37
+  {
38
+   "cell_type": "code",
39
+   "execution_count": 15,
40
+   "metadata": {},
41
+   "outputs": [
42
+    {
43
+     "ename": "NameError",
44
+     "evalue": "name 'tf' is not defined",
45
+     "output_type": "error",
46
+     "traceback": [
47
+      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
48
+      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
49
+      "\u001b[0;32m<ipython-input-15-81e2f0f8ce6c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mjson_file\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'model.json'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'r'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmodel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel_from_json\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjson_file\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0mjson_file\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0;31m#model.load_weights(\"weights-improvement-03-0.79.hdf5\")\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
50
+      "\u001b[0;32m/usr/local/lib/python3.6/site-packages/keras/models.py\u001b[0m in \u001b[0;36mmodel_from_json\u001b[0;34m(json_string, custom_objects)\u001b[0m\n\u001b[1;32m    347\u001b[0m     \"\"\"\n\u001b[1;32m    348\u001b[0m     \u001b[0mconfig\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mjson\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mloads\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mjson_string\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 349\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0mlayer_module\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdeserialize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mconfig\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcustom_objects\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcustom_objects\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    350\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    351\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
51
+      "\u001b[0;32m/usr/local/lib/python3.6/site-packages/keras/layers/__init__.py\u001b[0m in \u001b[0;36mdeserialize\u001b[0;34m(config, custom_objects)\u001b[0m\n\u001b[1;32m     53\u001b[0m                                     \u001b[0mmodule_objects\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mglobs\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     54\u001b[0m                                     \u001b[0mcustom_objects\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mcustom_objects\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 55\u001b[0;31m                                     printable_module_name='layer')\n\u001b[0m",
52
+      "\u001b[0;32m/usr/local/lib/python3.6/site-packages/keras/utils/generic_utils.py\u001b[0m in \u001b[0;36mdeserialize_keras_object\u001b[0;34m(identifier, module_objects, custom_objects, printable_module_name)\u001b[0m\n\u001b[1;32m    141\u001b[0m                 return cls.from_config(config['config'],\n\u001b[1;32m    142\u001b[0m                                        custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +\n\u001b[0;32m--> 143\u001b[0;31m                                                            list(custom_objects.items())))\n\u001b[0m\u001b[1;32m    144\u001b[0m             \u001b[0;32mwith\u001b[0m \u001b[0mCustomObjectScope\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcustom_objects\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    145\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mcls\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfrom_config\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mconfig\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'config'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
53
+      "\u001b[0;32m/usr/local/lib/python3.6/site-packages/keras/engine/topology.py\u001b[0m in \u001b[0;36mfrom_config\u001b[0;34m(cls, config, custom_objects)\u001b[0m\n\u001b[1;32m   2515\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0mlayer\u001b[0m \u001b[0;32min\u001b[0m \u001b[0munprocessed_nodes\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2516\u001b[0m                     \u001b[0;32mfor\u001b[0m \u001b[0mnode_data\u001b[0m \u001b[0;32min\u001b[0m \u001b[0munprocessed_nodes\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlayer\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2517\u001b[0;31m                         \u001b[0mprocess_node\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlayer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mnode_data\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2518\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2519\u001b[0m         \u001b[0mname\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mconfig\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'name'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
54
+      "\u001b[0;32m/usr/local/lib/python3.6/site-packages/keras/engine/topology.py\u001b[0m in \u001b[0;36mprocess_node\u001b[0;34m(layer, node_data)\u001b[0m\n\u001b[1;32m   2472\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0minput_tensors\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2473\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_tensors\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2474\u001b[0;31m                     \u001b[0mlayer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_tensors\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2475\u001b[0m                 \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2476\u001b[0m                     \u001b[0mlayer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput_tensors\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
55
+      "\u001b[0;32m/usr/local/lib/python3.6/site-packages/keras/engine/topology.py\u001b[0m in \u001b[0;36m__call__\u001b[0;34m(self, inputs, **kwargs)\u001b[0m\n\u001b[1;32m    615\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    616\u001b[0m             \u001b[0;31m# Actually call the layer, collecting output(s), mask(s), and shape(s).\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 617\u001b[0;31m             \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    618\u001b[0m             \u001b[0moutput_mask\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcompute_mask\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mprevious_mask\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    619\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
56
+      "\u001b[0;32m/usr/local/lib/python3.6/site-packages/keras/layers/core.py\u001b[0m in \u001b[0;36mcall\u001b[0;34m(self, inputs, mask)\u001b[0m\n\u001b[1;32m    661\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mhas_arg\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfunction\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'mask'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    662\u001b[0m             \u001b[0marguments\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'mask'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmask\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 663\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfunction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0marguments\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    664\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    665\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mcompute_mask\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmask\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
57
+      "\u001b[0;32m/usr/local/lib/python3.6/site-packages/keras/layers/core.py\u001b[0m in \u001b[0;36maugment_2d\u001b[0;34m(inputs, rotation, horizontal_flip, vertical_flip)\u001b[0m\n\u001b[1;32m     78\u001b[0m       \u001b[0minput\u001b[0m \u001b[0mdata\u001b[0m \u001b[0mafter\u001b[0m \u001b[0maugmentation\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwhose\u001b[0m \u001b[0mshape\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0mthe\u001b[0m \u001b[0msame\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mits\u001b[0m \u001b[0moriginal\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     79\u001b[0m     \"\"\"\n\u001b[0;32m---> 80\u001b[0;31m     \u001b[0;32mif\u001b[0m \u001b[0minputs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     81\u001b[0m         \u001b[0minputs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mconvert_image_dtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minputs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     82\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
58
+      "\u001b[0;31mNameError\u001b[0m: name 'tf' is not defined"
59
+     ]
60
+    }
61
+   ],
62
+   "source": [
63
+    "json_file = open('second.json', 'r')\n",
64
+    "model = model_from_json(json_file.read())\n",
65
+    "json_file.close()\n",
66
+    "\n",
67
+    "model.load_weights(\"weights-improvement-04-0.92.hdf5\")"
68
+   ]
69
+  },
70
+  {
71
+   "cell_type": "code",
72
+   "execution_count": 4,
73
+   "metadata": {},
74
+   "outputs": [
75
+    {
76
+     "ename": "NameError",
77
+     "evalue": "name 'model' is not defined",
78
+     "output_type": "error",
79
+     "traceback": [
80
+      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
81
+      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
82
+      "\u001b[0;32m<ipython-input-4-5f15418b3570>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msummary\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
83
+      "\u001b[0;31mNameError\u001b[0m: name 'model' is not defined"
84
+     ]
85
+    }
86
+   ],
87
+   "source": [
88
+    "model.summary()"
89
+   ]
90
+  },
91
+  {
92
+   "cell_type": "code",
93
+   "execution_count": 4,
94
+   "metadata": {},
95
+   "outputs": [],
96
+   "source": [
97
+    "# determine Loss function and Optimizer\n",
98
+    "model.compile(loss='categorical_crossentropy',\n",
99
+    "              optimizer=Adam(),\n",
100
+    "              metrics=['accuracy'])"
101
+   ]
102
+  },
103
+  {
104
+   "cell_type": "code",
105
+   "execution_count": 5,
106
+   "metadata": {},
107
+   "outputs": [],
108
+   "source": [
109
+    "class_name = np.load(\"class_name.npy\")\n",
110
+    "file_name = os.listdir(\"plants/test/\")\n",
111
+    "\n",
112
+    "output = []\n",
113
+    "\n",
114
+    "for file in file_name:\n",
115
+    "    file_dir = \"plants/test/\" + file\n",
116
+    "    image = cv2.imread(file_dir) \n",
117
+    "    image = cv2.resize(image, (300,300))\n",
118
+    "    image = np.array(image).reshape([1,300,300,3])\n",
119
+    "    predict = model.predict(image,1)\n",
120
+    "    predict = np.argmax(predict)\n",
121
+    "    str = file + \",\" + class_name[predict]\n",
122
+    "    output.append(str)\n",
123
+    "    \n",
124
+    "# print(output)"
125
+   ]
126
+  },
127
+  {
128
+   "cell_type": "code",
129
+   "execution_count": 8,
130
+   "metadata": {},
131
+   "outputs": [],
132
+   "source": [
133
+    "file_name = \"result_2.csv\"\n",
134
+    "file = open(file_name, 'w')\n",
135
+    "print(\"file,species\", file=file)\n",
136
+    "for result in output:\n",
137
+    "    print(result, file=file) \n",
138
+    "    \n",
139
+    "file.close()"
140
+   ]
141
+  },
142
+  {
143
+   "cell_type": "code",
144
+   "execution_count": null,
145
+   "metadata": {},
146
+   "outputs": [],
147
+   "source": []
148
+  }
149
+ ],
150
+ "metadata": {
151
+  "kernelspec": {
152
+   "display_name": "Python 3",
153
+   "language": "python",
154
+   "name": "python3"
155
+  },
156
+  "language_info": {
157
+   "codemirror_mode": {
158
+    "name": "ipython",
159
+    "version": 3
160
+   },
161
+   "file_extension": ".py",
162
+   "mimetype": "text/x-python",
163
+   "name": "python",
164
+   "nbconvert_exporter": "python",
165
+   "pygments_lexer": "ipython3",
166
+   "version": "3.6.4"
167
+  }
168
+ },
169
+ "nbformat": 4,
170
+ "nbformat_minor": 2
171
+}

+ 145
- 0
1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/15.7_Kaggle_Plants_Normalize-checkpoint.ipynb View File

@@ -0,0 +1,145 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "markdown",
5
+   "metadata": {},
6
+   "source": [
7
+    "I am going to seperate the dataset into training set and the testing set\n",
8
+    "\n",
9
+    "As mentioned in previous file\n",
10
+    "\n",
11
+    "I will use 90% for training set and 10% for testing set"
12
+   ]
13
+  },
14
+  {
15
+   "cell_type": "code",
16
+   "execution_count": 1,
17
+   "metadata": {},
18
+   "outputs": [],
19
+   "source": [
20
+    "import os\n",
21
+    "import cv2\n",
22
+    "import numpy as np\n",
23
+    "from random import shuffle"
24
+   ]
25
+  },
26
+  {
27
+   "cell_type": "code",
28
+   "execution_count": 3,
29
+   "metadata": {},
30
+   "outputs": [],
31
+   "source": [
32
+    "data_dir = \"plants/resized/\"\n",
33
+    "class_list = os.listdir(data_dir)\n",
34
+    "class_name = []\n",
35
+    "for name in class_list:\n",
36
+    "    if not name.startswith('.'):\n",
37
+    "        class_name.append(name)\n",
38
+    "        \n",
39
+    "# print(class_name)\n",
40
+    "\n",
41
+    "# for name in class_name:\n",
42
+    "#     print(name)\n",
43
+    "    \n",
44
+    "num_classes = len(class_name)\n",
45
+    "# print(num_classes)\n",
46
+    "np.save(\"class_name\", class_name)"
47
+   ]
48
+  },
49
+  {
50
+   "cell_type": "code",
51
+   "execution_count": 4,
52
+   "metadata": {},
53
+   "outputs": [
54
+    {
55
+     "ename": "SyntaxError",
56
+     "evalue": "invalid syntax (<ipython-input-4-0cf7e27c6285>, line 4)",
57
+     "output_type": "error",
58
+     "traceback": [
59
+      "\u001b[0;36m  File \u001b[0;32m\"<ipython-input-4-0cf7e27c6285>\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m    for i range(10)\u001b[0m\n\u001b[0m              ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
60
+     ]
61
+    }
62
+   ],
63
+   "source": [
64
+    "x_train = []\n",
65
+    "y_train = []\n",
66
+    "temp_train = []\n",
67
+    "for i in range(10):\n",
68
+    "    for name in class_name:\n",
69
+    "        class_dir = data_dir + name + \"/\"\n",
70
+    "        file_list = os.listdir(class_dir)\n",
71
+    "        file_list = file_list[i*100:(i+1)*100]\n",
72
+    "\n",
73
+    "        for file in file_list:\n",
74
+    "            file_dir = class_dir + file\n",
75
+    "            image = cv2.imread(file_dir) \n",
76
+    "            image = image.astype('float32')/255\n",
77
+    "            temp_train.append([image, name])\n",
78
+    "        \n",
79
+    "        print(len(temp_train))\n",
80
+    "        \n",
81
+    "    shuffle(temp_train)\n",
82
+    "    \n",
83
+    "    y_train = [j[1] for j in temp_train]\n",
84
+    "    \n",
85
+    "    # convert to one hot encoing \n",
86
+    "    train_hot = np.zeros([len(y_train), num_classes])\n",
87
+    "\n",
88
+    "    for k in range(len(y_train)):\n",
89
+    "        j = 0\n",
90
+    "        for name in class_name:\n",
91
+    "            if y_train[k] == name:\n",
92
+    "                break\n",
93
+    "            else:\n",
94
+    "                j += 1\n",
95
+    "\n",
96
+    "        train_hot[k][j] = 1   \n",
97
+    "\n",
98
+    "    y_train = train_hot\n",
99
+    "\n",
100
+    "    \n",
101
+    "    x_train = np.array([j[0] for j in temp_train]).reshape(-1,300,300,3)\n",
102
+    "\n",
103
+    "    np.save(\"x_train_{}.npy\".format(i) ,x_train)\n",
104
+    "    np.save(\"y_train_{}.npy\".format(i) ,y_train)\n",
105
+    "    \n",
106
+    "    train_hot = []\n",
107
+    "    x_train = []\n",
108
+    "    y_train = []\n",
109
+    "    temp_train = []"
110
+   ]
111
+  },
112
+  {
113
+   "cell_type": "markdown",
114
+   "metadata": {},
115
+   "source": [
116
+    "Now I have seperated the Dataset into Training and Test set and into input and output\n",
117
+    "\n",
118
+    "time to save it into the disk so I can reload the file into the memory without doing all the processing\n",
119
+    "\n",
120
+    "Beofre Save it into the file, I want to convert to one-hot encoding and normalize input"
121
+   ]
122
+  }
123
+ ],
124
+ "metadata": {
125
+  "kernelspec": {
126
+   "display_name": "Python 3",
127
+   "language": "python",
128
+   "name": "python3"
129
+  },
130
+  "language_info": {
131
+   "codemirror_mode": {
132
+    "name": "ipython",
133
+    "version": 3
134
+   },
135
+   "file_extension": ".py",
136
+   "mimetype": "text/x-python",
137
+   "name": "python",
138
+   "nbconvert_exporter": "python",
139
+   "pygments_lexer": "ipython3",
140
+   "version": "3.6.4"
141
+  }
142
+ },
143
+ "nbformat": 4,
144
+ "nbformat_minor": 2
145
+}

+ 300
- 0
1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/15.8_Kaggle_Plants_Train_Normalized-checkpoint.ipynb View File

@@ -0,0 +1,300 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "raw",
5
+   "metadata": {},
6
+   "source": [
7
+    "In this file I will build the Neural Network and train it"
8
+   ]
9
+  },
10
+  {
11
+   "cell_type": "code",
12
+   "execution_count": null,
13
+   "metadata": {},
14
+   "outputs": [],
15
+   "source": [
16
+    "import keras\n",
17
+    "from keras.datasets import cifar10\n",
18
+    "from keras.models import Model, Sequential\n",
19
+    "from keras.layers import Dense, Dropout, Flatten, Input, AveragePooling2D, merge, Activation\n",
20
+    "from keras.layers import Conv2D, MaxPooling2D, BatchNormalization, Lambda\n",
21
+    "from keras.layers import Concatenate\n",
22
+    "from keras.callbacks import ModelCheckpoint\n",
23
+    "from keras.optimizers import Adam\n",
24
+    "import numpy as np\n",
25
+    "import os"
26
+   ]
27
+  },
28
+  {
29
+   "cell_type": "code",
30
+   "execution_count": null,
31
+   "metadata": {},
32
+   "outputs": [],
33
+   "source": [
34
+    "# this part will prevent tensorflow to allocate all the avaliable GPU Memory\n",
35
+    "# backend\n",
36
+    "import tensorflow as tf\n",
37
+    "from keras import backend as k\n",
38
+    "\n",
39
+    "# Don't pre-allocate memory; allocate as-needed\n",
40
+    "config = tf.ConfigProto()\n",
41
+    "config.gpu_options.allow_growth = True\n",
42
+    "\n",
43
+    "# Create a session with the above options specified.\n",
44
+    "k.tensorflow_backend.set_session(tf.Session(config=config))"
45
+   ]
46
+  },
47
+  {
48
+   "cell_type": "code",
49
+   "execution_count": null,
50
+   "metadata": {},
51
+   "outputs": [],
52
+   "source": [
53
+    "# Hyperparameters\n",
54
+    "batch_size = 8\n",
55
+    "num_classes = 12\n",
56
+    "epochs = 50\n",
57
+    "l = 10\n",
58
+    "num_filter = 10"
59
+   ]
60
+  },
61
+  {
62
+   "cell_type": "code",
63
+   "execution_count": null,
64
+   "metadata": {},
65
+   "outputs": [],
66
+   "source": [
67
+    "#Load Data\n",
68
+    "class_name = np.load(\"class_name.npy\")\n",
69
+    "x_list = []\n",
70
+    "\n",
71
+    "file_list = os.listdir(\".\")\n",
72
+    "\n",
73
+    "for file in file_list:\n",
74
+    "    if file.startswith(\"x_train\") and file.endswith(\".npy\"):\n",
75
+    "        x_list.append(file)\n",
76
+    "\n",
77
+    "x_train = np.load(x_list[0])\n",
78
+    "# print(x_list)"
79
+   ]
80
+  },
81
+  {
82
+   "cell_type": "code",
83
+   "execution_count": null,
84
+   "metadata": {},
85
+   "outputs": [],
86
+   "source": [
87
+    "def augment_2d(inputs, rotation=0, horizontal_flip=False, vertical_flip=False):\n",
88
+    "    \"\"\"Apply additive augmentation on 2D data.\n",
89
+    "    # Arguments\n",
90
+    "      rotation: A float, the degree range for rotation (0 <= rotation < 180),\n",
91
+    "          e.g. 3 for random image rotation between (-3.0, 3.0).\n",
92
+    "      horizontal_flip: A boolean, whether to allow random horizontal flip,\n",
93
+    "          e.g. true for 50% possibility to flip image horizontally.\n",
94
+    "      vertical_flip: A boolean, whether to allow random vertical flip,\n",
95
+    "          e.g. true for 50% possibility to flip image vertically.\n",
96
+    "    # Returns\n",
97
+    "      input data after augmentation, whose shape is the same as its original.\n",
98
+    "    \"\"\"\n",
99
+    "    if inputs.dtype != tf.float32:\n",
100
+    "        inputs = tf.image.convert_image_dtype(inputs, dtype=tf.float32)\n",
101
+    "\n",
102
+    "    with tf.name_scope('augmentation'):\n",
103
+    "        shp = tf.shape(inputs)\n",
104
+    "        batch_size, height, width = shp[0], shp[1], shp[2]\n",
105
+    "        width = tf.cast(width, tf.float32)\n",
106
+    "        height = tf.cast(height, tf.float32)\n",
107
+    "\n",
108
+    "        transforms = []\n",
109
+    "        identity = tf.constant([1, 0, 0, 0, 1, 0, 0, 0], dtype=tf.float32)\n",
110
+    "\n",
111
+    "        if rotation > 0:\n",
112
+    "            angle_rad = rotation * 3.141592653589793 / 180.0\n",
113
+    "            angles = tf.random_uniform([batch_size], -angle_rad, angle_rad)\n",
114
+    "            f = tf.contrib.image.angles_to_projective_transforms(angles,\n",
115
+    "                                                                 height, width)\n",
116
+    "            transforms.append(f)\n",
117
+    "\n",
118
+    "        if horizontal_flip:\n",
119
+    "            coin = tf.less(tf.random_uniform([batch_size], 0, 1.0), 0.5)\n",
120
+    "            shape = [-1., 0., width, 0., 1., 0., 0., 0.]\n",
121
+    "            flip_transform = tf.convert_to_tensor(shape, dtype=tf.float32)\n",
122
+    "            flip = tf.tile(tf.expand_dims(flip_transform, 0), [batch_size, 1])\n",
123
+    "            noflip = tf.tile(tf.expand_dims(identity, 0), [batch_size, 1])\n",
124
+    "            transforms.append(tf.where(coin, flip, noflip))\n",
125
+    "\n",
126
+    "        if vertical_flip:\n",
127
+    "            coin = tf.less(tf.random_uniform([batch_size], 0, 1.0), 0.5)\n",
128
+    "            shape = [1., 0., 0., 0., -1., height, 0., 0.]\n",
129
+    "            flip_transform = tf.convert_to_tensor(shape, dtype=tf.float32)\n",
130
+    "            flip = tf.tile(tf.expand_dims(flip_transform, 0), [batch_size, 1])\n",
131
+    "            noflip = tf.tile(tf.expand_dims(identity, 0), [batch_size, 1])\n",
132
+    "            transforms.append(tf.where(coin, flip, noflip))\n",
133
+    "\n",
134
+    "    if transforms:\n",
135
+    "        f = tf.contrib.image.compose_transforms(*transforms)\n",
136
+    "        inputs = tf.contrib.image.transform(inputs, f, interpolation='BILINEAR')\n",
137
+    "    return inputs"
138
+   ]
139
+  },
140
+  {
141
+   "cell_type": "code",
142
+   "execution_count": null,
143
+   "metadata": {},
144
+   "outputs": [],
145
+   "source": [
146
+    "# Dense Block\n",
147
+    "def add_denseblock(input):\n",
148
+    "    temp = input\n",
149
+    "    for _ in range(l):\n",
150
+    "        BatchNorm = BatchNormalization()(temp)\n",
151
+    "        relu = Activation('relu')(BatchNorm)\n",
152
+    "        Conv2D_3_3 = Conv2D(num_filter, (3,3), use_bias=False ,padding='same')(relu)\n",
153
+    "        concat = Concatenate(axis=-1)([temp,Conv2D_3_3])\n",
154
+    "        temp = concat\n",
155
+    "        \n",
156
+    "    return temp"
157
+   ]
158
+  },
159
+  {
160
+   "cell_type": "code",
161
+   "execution_count": null,
162
+   "metadata": {},
163
+   "outputs": [],
164
+   "source": [
165
+    "def add_transition(input):\n",
166
+    "    BatchNorm = BatchNormalization()(input)\n",
167
+    "    relu = Activation('relu')(BatchNorm)\n",
168
+    "    Conv2D_BottleNeck = Conv2D(num_filter, (1,1), use_bias=False ,padding='same')(relu)\n",
169
+    "    avg = AveragePooling2D(pool_size=(2,2))(Conv2D_BottleNeck)\n",
170
+    "    \n",
171
+    "    return avg"
172
+   ]
173
+  },
174
+  {
175
+   "cell_type": "code",
176
+   "execution_count": null,
177
+   "metadata": {},
178
+   "outputs": [],
179
+   "source": [
180
+    "def output_layer(input):\n",
181
+    "    BatchNorm = BatchNormalization()(input)\n",
182
+    "    relu = Activation('relu')(BatchNorm)\n",
183
+    "    AvgPooling = AveragePooling2D(pool_size=(2,2))(relu)\n",
184
+    "    flat = Flatten()(AvgPooling)\n",
185
+    "    output = Dense(num_classes, activation='softmax')(flat)\n",
186
+    "    \n",
187
+    "    return output"
188
+   ]
189
+  },
190
+  {
191
+   "cell_type": "code",
192
+   "execution_count": null,
193
+   "metadata": {},
194
+   "outputs": [],
195
+   "source": [
196
+    "input = Input(shape=(300, 300, 3,))\n",
197
+    "augment = Lambda(augment_2d,\n",
198
+    "                 input_shape=x_train.shape[1:],\n",
199
+    "                 arguments={'rotation': 8.0, 'horizontal_flip': True, 'vertical_flip':True})(input)\n",
200
+    "\n",
201
+    "First_Conv2D = Conv2D(num_filter, (7,7), use_bias=False ,padding='same')(augment)\n",
202
+    "\n",
203
+    "First_Block = add_denseblock(First_Conv2D)\n",
204
+    "First_Transition = add_transition(First_Block)\n",
205
+    "\n",
206
+    "Second_Block = add_denseblock(First_Transition)\n",
207
+    "Second_Transition = add_transition(Second_Block)\n",
208
+    "\n",
209
+    "Third_Block = add_denseblock(Second_Transition)\n",
210
+    "Third_Transition = add_transition(Third_Block)\n",
211
+    "\n",
212
+    "Last_Block = add_denseblock(Third_Transition)\n",
213
+    "output = output_layer(Last_Block)"
214
+   ]
215
+  },
216
+  {
217
+   "cell_type": "code",
218
+   "execution_count": null,
219
+   "metadata": {},
220
+   "outputs": [],
221
+   "source": [
222
+    "model = Model(inputs=[input], outputs=[output])\n",
223
+    "model.summary()"
224
+   ]
225
+  },
226
+  {
227
+   "cell_type": "code",
228
+   "execution_count": null,
229
+   "metadata": {},
230
+   "outputs": [],
231
+   "source": [
232
+    "# determine Loss function and Optimizer\n",
233
+    "model.compile(loss='categorical_crossentropy',\n",
234
+    "              optimizer=Adam(),\n",
235
+    "              metrics=['accuracy'])\n",
236
+    "\n",
237
+    "# Convert model into JSON Format\n",
238
+    "model_json = model.to_json()\n",
239
+    "with open(\"model.json\", \"w\") as json_file:\n",
240
+    "    json_file.write(model_json)\n",
241
+    "\n",
242
+    "filepath=\"weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5\"\n",
243
+    "checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')\n",
244
+    "callbacks_list = [checkpoint]"
245
+   ]
246
+  },
247
+  {
248
+   "cell_type": "code",
249
+   "execution_count": null,
250
+   "metadata": {},
251
+   "outputs": [],
252
+   "source": [
253
+    "for file in x_list:\n",
254
+    "    x_train = np.load(file)\n",
255
+    "    y_name = \"y\" + file[1:]\n",
256
+    "#     print(y_name)\n",
257
+    "    y_train = np.load(y_name)\n",
258
+    "    \n",
259
+    "    model.fit(x_train, y_train,\n",
260
+    "                    batch_size=batch_size,\n",
261
+    "                    epochs=epochs,\n",
262
+    "                    verbose=1,\n",
263
+    "                     validation_split=0.1,\n",
264
+    "                    callbacks=callbacks_list)"
265
+   ]
266
+  },
267
+  {
268
+   "cell_type": "code",
269
+   "execution_count": null,
270
+   "metadata": {},
271
+   "outputs": [],
272
+   "source": [
273
+    "# Save the trained weights in to .h5 format\n",
274
+    "model.save_weights(\"finished.h5\")\n",
275
+    "print(\"Saved model to disk\")"
276
+   ]
277
+  }
278
+ ],
279
+ "metadata": {
280
+  "kernelspec": {
281
+   "display_name": "Python 3",
282
+   "language": "python",
283
+   "name": "python3"
284
+  },
285
+  "language_info": {
286
+   "codemirror_mode": {
287
+    "name": "ipython",
288
+    "version": 3
289
+   },
290
+   "file_extension": ".py",
291
+   "mimetype": "text/x-python",
292
+   "name": "python",
293
+   "nbconvert_exporter": "python",
294
+   "pygments_lexer": "ipython3",
295
+   "version": "3.6.4"
296
+  }
297
+ },
298
+ "nbformat": 4,
299
+ "nbformat_minor": 2
300
+}

+ 202
- 0
1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/1_3rd_DataAugmentation_and_Resize-checkpoint.ipynb View File

@@ -0,0 +1,202 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": 1,
6
+   "metadata": {},
7
+   "outputs": [
8
+    {
9
+     "name": "stderr",
10
+     "output_type": "stream",
11
+     "text": [
12
+      "/usr/local/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
13
+      "  from ._conv import register_converters as _register_converters\n",
14
+      "Using TensorFlow backend.\n"
15
+     ]
16
+    }
17
+   ],
18
+   "source": [
19
+    "import os\n",
20
+    "import numpy as np\n",
21
+    "import cv2\n",
22
+    "from keras.preprocessing.image import ImageDataGenerator\n",
23
+    "from math import ceil\n",
24
+    "from multiprocessing import Pool"
25
+   ]
26
+  },
27
+  {
28
+   "cell_type": "code",
29
+   "execution_count": 2,
30
+   "metadata": {},
31
+   "outputs": [],
32
+   "source": [
33
+    "raw_dir = \"plants/raw/\"\n",
34
+    "file_list = os.listdir(raw_dir)\n",
35
+    "class_name = []\n",
36
+    "for name in file_list:\n",
37
+    "    if not name.startswith('.'):\n",
38
+    "        class_name.append(name)"
39
+   ]
40
+  },
41
+  {
42
+   "cell_type": "code",
43
+   "execution_count": 3,
44
+   "metadata": {},
45
+   "outputs": [],
46
+   "source": [
47
+    "augmented_dir = \"plants/augmented_multi/\"\n",
48
+    "\n",
49
+    "if not os.path.exists(augmented_dir):\n",
50
+    "        os.mkdir(augmented_dir)\n",
51
+    "        \n",
52
+    "for name in class_name:\n",
53
+    "    class_dir = augmented_dir + name + \"/\"\n",
54
+    "    if not os.path.exists(class_dir):\n",
55
+    "        os.mkdir(class_dir)"
56
+   ]
57
+  },
58
+  {
59
+   "cell_type": "code",
60
+   "execution_count": null,
61
+   "metadata": {},
62
+   "outputs": [],
63
+   "source": [
64
+    "target = 3000\n",
65
+    "\n",
66
+    "datagen = ImageDataGenerator(\n",
67
+    "        rotation_range=40,\n",
68
+    "        width_shift_range=0.2,\n",
69
+    "        height_shift_range=0.2,\n",
70
+    "        shear_range=0.2,\n",
71
+    "        zoom_range=0.2,\n",
72
+    "        vertical_flip=True,\n",
73
+    "        horizontal_flip=True,\n",
74
+    "        fill_mode='reflect')\n",
75
+    "\n",
76
+    "def augmentation(name):\n",
77
+    "    print(\"Start Augmenting \" + name)\n",
78
+    "    class_dir = raw_dir + name +\"/\"\n",
79
+    "    augmented_class_dir = augmented_dir + name + \"/\"\n",
80
+    "    \n",
81
+    "    file_list = os.listdir(class_dir)\n",
82
+    "    augmented_list = os.listdir(augmented_class_dir)\n",
83
+    "    \n",
84
+    "    for file_name in file_list:\n",
85
+    "        if not file_name.endswith(\".png\"):\n",
86
+    "            file_list.remove(file_name)\n",
87
+    "#             print(\"Removed\")\n",
88
+    "    \n",
89
+    "    num_augment = ceil(target / len(file_list)) + 1\n",
90
+    "    \n",
91
+    "    for file_name in file_list:\n",
92
+    "        if not file_name.endswith(\".png\"):\n",
93
+    "            continue\n",
94
+    "\n",
95
+    "        file_dir = class_dir + file_name\n",
96
+    "        image = cv2.imread(file_dir)\n",
97
+    "        print(\"1\")\n",
98
+    "        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
99
+    "#         image = image[...,::-1] # BGR2RGB\n",
100
+    "        print(image.shape)\n",
101
+    "        image = cv2.resize(image,(100,100))\n",
102
+    "        print(\"2\")\n",
103
+    "        height, width, channel = image.shape\n",
104
+    "        \n",
105
+    "        if channel != 3:\n",
106
+    "            continue\n",
107
+    "            \n",
108
+    "        x = np.array(image).reshape([1,height,width,channel])\n",
109
+    "    \n",
110
+    "        i = 0\n",
111
+    "        for batch in datagen.flow(x, batch_size=1,\n",
112
+    "                                  save_to_dir=augmented_class_dir, save_prefix='aug_', save_format='png'):\n",
113
+    "            i += 1\n",
114
+    "            if i >= num_augment:\n",
115
+    "                break  # otherwise the generator would loop indefinitely\n",
116
+    "\n",
117
+    "    print(\"Finished Augmenting \" + name)"
118
+   ]
119
+  },
120
+  {
121
+   "cell_type": "code",
122
+   "execution_count": null,
123
+   "metadata": {},
124
+   "outputs": [
125
+    {
126
+     "name": "stdout",
127
+     "output_type": "stream",
128
+     "text": [
129
+      "Start Augmenting Sugar beet\n",
130
+      "Start Augmenting Loose Silky-bent\n",
131
+      "Start Augmenting Cleavers\n",
132
+      "Start Augmenting Scentless Mayweed\n",
133
+      "Start Augmenting Shepherds Purse\n",
134
+      "Start Augmenting Common Chickweed\n",
135
+      "Start Augmenting Common wheat\n",
136
+      "Start Augmenting Black-grass\n",
137
+      "Start Augmenting Fat Hen\n",
138
+      "Start Augmenting Small-flowered Cranesbill\n",
139
+      "Start Augmenting Charlock\n",
140
+      "Start Augmenting Maize\n",
141
+      "1\n",
142
+      "1\n",
143
+      "1\n",
144
+      "1\n",
145
+      "(109, 109, 3)\n",
146
+      "1\n",
147
+      "1\n",
148
+      "(186, 186, 3)\n",
149
+      "(134, 134, 3)\n",
150
+      "(104, 104, 3)\n",
151
+      "1\n",
152
+      "(221, 221, 3)\n",
153
+      "(234, 234, 3)\n",
154
+      "1\n",
155
+      "1\n",
156
+      "(352, 352, 3)\n",
157
+      "1\n",
158
+      "1\n",
159
+      "(365, 365, 3)\n",
160
+      "1\n",
161
+      "(531, 531, 3)\n",
162
+      "(618, 618, 3)\n",
163
+      "(577, 577, 3)\n",
164
+      "(575, 575, 3)\n"
165
+     ]
166
+    }
167
+   ],
168
+   "source": [
169
+    "p = Pool()\n",
170
+    "p.map(augmentation, [name for name in class_name])"
171
+   ]
172
+  },
173
+  {
174
+   "cell_type": "code",
175
+   "execution_count": null,
176
+   "metadata": {},
177
+   "outputs": [],
178
+   "source": []
179
+  }
180
+ ],
181
+ "metadata": {
182
+  "kernelspec": {
183
+   "display_name": "Python 3",
184
+   "language": "python",
185
+   "name": "python3"
186
+  },
187
+  "language_info": {
188
+   "codemirror_mode": {
189
+    "name": "ipython",
190
+    "version": 3
191
+   },
192
+   "file_extension": ".py",
193
+   "mimetype": "text/x-python",
194
+   "name": "python",
195
+   "nbconvert_exporter": "python",
196
+   "pygments_lexer": "ipython3",
197
+   "version": "3.6.4"
198
+  }
199
+ },
200
+ "nbformat": 4,
201
+ "nbformat_minor": 2
202
+}

+ 111
- 0
1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/2_3rd_categorical_normalize-checkpoint.ipynb View File

@@ -0,0 +1,111 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": 5,
6
+   "metadata": {},
7
+   "outputs": [],
8
+   "source": [
9
+    "import cv2\n",
10
+    "import os\n",
11
+    "import numpy as np\n",
12
+    "from multiprocessing import Pool\n",
13
+    "from random import shuffle"
14
+   ]
15
+  },
16
+  {
17
+   "cell_type": "code",
18
+   "execution_count": 6,
19
+   "metadata": {},
20
+   "outputs": [],
21
+   "source": [
22
+    "class_name = np.load(\"class_name.npy\")\n",
23
+    "\n",
24
+    "data_dir = \"plants/augmented_resized/\"\n",
25
+    "for name in class_name:\n",
26
+    "    data_list = os.listdir(data_dir + name)\n",
27
+    "    shuffle(data_list)\n",
28
+    "    np.save(\"{}_list.npy\".format(name), data_list)"
29
+   ]
30
+  },
31
+  {
32
+   "cell_type": "code",
33
+   "execution_count": null,
34
+   "metadata": {},
35
+   "outputs": [],
36
+   "source": [
37
+    "target = 3000\n",
38
+    "each = 100\n",
39
+    "\n",
40
+    "x_train = []\n",
41
+    "y_train = []\n",
42
+    "temp_train = []\n",
43
+    "\n",
44
+    "for i in range(int(target/each)):\n",
45
+    "    for name in class_name:\n",
46
+    "        file_list = np.load(name + \"_list.npy\")\n",
47
+    "        file_list = file_list[i*100:(i+1)*100]\n",
48
+    "\n",
49
+    "        for file in file_list:\n",
50
+    "            file_dir = class_dir + file\n",
51
+    "            image = cv2.imread(file_dir) \n",
52
+    "            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
53
+    "            image = image.astype('float32')/255\n",
54
+    "            temp_train.append([image, name])\n",
55
+    "        \n",
56
+    "        print(len(temp_train))\n",
57
+    "        \n",
58
+    "    shuffle(temp_train)\n",
59
+    "    \n",
60
+    "    y_train = [j[1] for j in temp_train]\n",
61
+    "    \n",
62
+    "    # convert to one hot encoing \n",
63
+    "    train_hot = np.zeros([len(y_train), num_classes])\n",
64
+    "\n",
65
+    "    for k in range(len(y_train)):\n",
66
+    "        j = 0\n",
67
+    "        for name in class_name:\n",
68
+    "            if y_train[k] == name:\n",
69
+    "                break\n",
70
+    "            else:\n",
71
+    "                j += 1\n",
72
+    "\n",
73
+    "        train_hot[k][j] = 1   \n",
74
+    "\n",
75
+    "    y_train = train_hot\n",
76
+    "\n",
77
+    "    \n",
78
+    "    x_train = np.array([j[0] for j in temp_train]).reshape(-1,300,300,3)\n",
79
+    "\n",
80
+    "    np.save(\"x_train_{}.npy\".format(i) ,x_train)\n",
81
+    "    np.save(\"y_train_{}.npy\".format(i) ,y_train)\n",
82
+    "    \n",
83
+    "    train_hot = []\n",
84
+    "    x_train = []\n",
85
+    "    y_train = []\n",
86
+    "    temp_train = []"
87
+   ]
88
+  }
89
+ ],
90
+ "metadata": {
91
+  "kernelspec": {
92
+   "display_name": "Python 3",
93
+   "language": "python",
94
+   "name": "python3"
95
+  },
96
+  "language_info": {
97
+   "codemirror_mode": {
98
+    "name": "ipython",
99
+    "version": 3
100
+   },
101
+   "file_extension": ".py",
102
+   "mimetype": "text/x-python",
103
+   "name": "python",
104
+   "nbconvert_exporter": "python",
105
+   "pygments_lexer": "ipython3",
106
+   "version": "3.6.4"
107
+  }
108
+ },
109
+ "nbformat": 4,
110
+ "nbformat_minor": 2
111
+}

+ 117
- 0
1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/3_3rd_Train-checkpoint.ipynb View File

@@ -0,0 +1,117 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": null,
6
+   "metadata": {},
7
+   "outputs": [],
8
+   "source": [
9
+    "import numpy as np # linear algebra\n",
10
+    "import os, cv2\n",
11
+    "from tqdm import tqdm\n",
12
+    "\n",
13
+    "from keras import optimizers\n",
14
+    "from keras.models import Sequential, Model, load_model\n",
15
+    "from keras.layers import Dense, Dropout, Flatten, Input\n",
16
+    "from keras.layers import Conv2D, MaxPooling2D, BatchNormalization\n",
17
+    "from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n",
18
+    "from keras.applications import *"
19
+   ]
20
+  },
21
+  {
22
+   "cell_type": "code",
23
+   "execution_count": null,
24
+   "metadata": {},
25
+   "outputs": [],
26
+   "source": [
27
+    "dim = 150\n",
28
+    "epochs = 100\n",
29
+    "batch_size = 32 \n",
30
+    "\n",
31
+    "base_model = InceptionResNetV2(input_shape=(dim, dim, 3), include_top=False, weights=None, pooling='avg')\n",
32
+    "x = base_model.output\n",
33
+    "x = Dense(256, activation='relu')(x)\n",
34
+    "x = Dropout(0.3)(x)\n",
35
+    "predictions = Dense(12, activation='softmax')(x)\n",
36
+    "model = Model(inputs=base_model.input, outputs=predictions)\n",
37
+    "model.compile(loss='categorical_crossentropy', \n",
38
+    "              optimizer=optimizers.Adam(), metrics=['accuracy'])\n",
39
+    "model.summary()"
40
+   ]
41
+  },
42
+  {
43
+   "cell_type": "code",
44
+   "execution_count": null,
45
+   "metadata": {},
46
+   "outputs": [],
47
+   "source": [
48
+    "filepath = \"weights-improvement-{val_acc:.4f}.hdf5\"\n",
49
+    "checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')\n",
50
+    "\n",
51
+    "learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n",
52
+    "                                            patience=3, \n",
53
+    "                                            verbose=1, \n",
54
+    "                                            factor=0.5, \n",
55
+    "                                            min_lr=0.00001)\n",
56
+    "\n",
57
+    "callbacks_list = [checkpoint, learning_rate_reduction]"
58
+   ]
59
+  },
60
+  {
61
+   "cell_type": "code",
62
+   "execution_count": null,
63
+   "metadata": {},
64
+   "outputs": [],
65
+   "source": [
66
+    "x_list = os.listdir(\"numpy/\")\n",
67
+    "for file in x_list:\n",
68
+    "    if not file.startswith(\"x_train\"):\n",
69
+    "        x_list.remove(file)\n",
70
+    "        \n",
71
+    "for i in range(int(epochs/10)):\n",
72
+    "    for file in x_list:\n",
73
+    "        x_train = np.load(\"numpy/\" + file)\n",
74
+    "        y_name = \"y\" + file[1:]\n",
75
+    "    #     print(y_name)\n",
76
+    "        y_train = np.load(\"numpy/\" + y_name)\n",
77
+    "\n",
78
+    "        model.fit(x_train, y_train,\n",
79
+    "                        batch_size=batch_size,\n",
80
+    "                        epochs=int(epochs/10),\n",
81
+    "                        verbose=1,\n",
82
+    "                          validation_data = (x_test, y_test),\n",
83
+    "                        callbacks=callbacks_list)\n",
84
+    "    # Save the trained weights in to .h5 format\n",
85
+    "    model.save_weights(\"finished_{}.hdf5\".format((i+1)*int(epochs/10)))"
86
+   ]
87
+  },
88
+  {
89
+   "cell_type": "code",
90
+   "execution_count": null,
91
+   "metadata": {},
92
+   "outputs": [],
93
+   "source": []
94
+  }
95
+ ],
96
+ "metadata": {
97
+  "kernelspec": {
98
+   "display_name": "Python 3",
99
+   "language": "python",
100
+   "name": "python3"
101
+  },
102
+  "language_info": {
103
+   "codemirror_mode": {
104
+    "name": "ipython",
105
+    "version": 3
106
+   },
107
+   "file_extension": ".py",
108
+   "mimetype": "text/x-python",
109
+   "name": "python",
110
+   "nbconvert_exporter": "python",
111
+   "pygments_lexer": "ipython3",
112
+   "version": "3.6.4"
113
+  }
114
+ },
115
+ "nbformat": 4,
116
+ "nbformat_minor": 2
117
+}

+ 69
- 0
1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/4_3rd_Testset-checkpoint.ipynb View File

@@ -0,0 +1,69 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": 3,
6
+   "metadata": {},
7
+   "outputs": [],
8
+   "source": [
9
+    "import os\n",
10
+    "import numpy as np\n",
11
+    "import cv2\n",
12
+    "from random import shuffle"
13
+   ]
14
+  },
15
+  {
16
+   "cell_type": "code",
17
+   "execution_count": 2,
18
+   "metadata": {},
19
+   "outputs": [],
20
+   "source": [
21
+    "class_name = np.load(\"numpy/class_name.npy\")"
22
+   ]
23
+  },
24
+  {
25
+   "cell_type": "code",
26
+   "execution_count": null,
27
+   "metadata": {},
28
+   "outputs": [],
29
+   "source": [
30
+    "raw_dir = \"plants/raw/\"\n",
31
+    "temp = []\n",
32
+    "\n",
33
+    "for name in class_name:\n",
34
+    "    class_dir = raw + name + \"/\"\n",
35
+    "    file_list = os.listdir(class_dir)\n",
36
+    "    shuffle(file_list)\n",
37
+    "    file_list = file_list[:50]\n",
38
+    "    \n",
39
+    "    for file in file_list:\n",
40
+    "        file_dir = class_dir + file\n",
41
+    "        image = cv2.imread(file_dir) \n",
42
+    "        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
43
+    "        image = image.astype('float32')/255\n",
44
+    "        temp_train.append([image, name])"
45
+   ]
46
+  }
47
+ ],
48
+ "metadata": {
49
+  "kernelspec": {
50
+   "display_name": "Python 3",
51
+   "language": "python",
52
+   "name": "python3"
53
+  },
54
+  "language_info": {
55
+   "codemirror_mode": {
56
+    "name": "ipython",
57
+    "version": 3
58
+   },
59
+   "file_extension": ".py",
60
+   "mimetype": "text/x-python",
61
+   "name": "python",
62
+   "nbconvert_exporter": "python",
63
+   "pygments_lexer": "ipython3",
64
+   "version": "3.6.4"
65
+  }
66
+ },
67
+ "nbformat": 4,
68
+ "nbformat_minor": 2
69
+}

+ 148
- 0
1_Plant Seedlings Classification_0.97/.ipynb_checkpoints/5_3rd_predict-checkpoint.ipynb View File

@@ -0,0 +1,148 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": 1,
6
+   "metadata": {},
7
+   "outputs": [
8
+    {
9
+     "name": "stderr",
10
+     "output_type": "stream",
11
+     "text": [
12
+      "/usr/local/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
13
+      "  from ._conv import register_converters as _register_converters\n",
14
+      "Using TensorFlow backend.\n"
15
+     ]
16
+    }
17
+   ],
18
+   "source": [
19
+    "import keras\n",
20
+    "from keras.models import model_from_json\n",
21
+    "from keras.optimizers import Adam\n",
22
+    "import numpy as np\n",
23
+    "import os\n",
24
+    "import cv2"
25
+   ]
26
+  },
27
+  {
28
+   "cell_type": "code",
29
+   "execution_count": 2,
30
+   "metadata": {},
31
+   "outputs": [],
32
+   "source": [
33
+    "# this part will prevent tensorflow to allocate all the avaliable GPU Memory\n",
34
+    "# backend\n",
35
+    "import tensorflow as tf\n",
36
+    "from keras import backend as k\n",
37
+    "\n",
38
+    "# Don't pre-allocate memory; allocate as-needed\n",
39
+    "config = tf.ConfigProto()\n",
40
+    "config.gpu_options.allow_growth = True\n",
41
+    "\n",
42
+    "# Create a session with the above options specified.\n",
43
+    "k.tensorflow_backend.set_session(tf.Session(config=config))"
44
+   ]
45
+  },
46
+  {
47
+   "cell_type": "code",
48
+   "execution_count": 3,
49
+   "metadata": {},
50
+   "outputs": [],
51
+   "source": [
52
+    "json_file = open('model.json', 'r')\n",
53
+    "model = model_from_json(json_file.read())\n",
54
+    "json_file.close()"
55
+   ]
56
+  },
57
+  {
58
+   "cell_type": "code",
59
+   "execution_count": 9,
60
+   "metadata": {},
61
+   "outputs": [],
62
+   "source": [
63
+    "model.load_weights(\"weights-improvement-0.9983.hdf5\")"
64
+   ]
65
+  },
66
+  {
67
+   "cell_type": "code",
68
+   "execution_count": 4,
69
+   "metadata": {},
70
+   "outputs": [],
71
+   "source": [
72
+    "# determine Loss function and Optimizer\n",
73
+    "model.compile(loss='categorical_crossentropy',\n",
74
+    "              optimizer=Adam(),\n",
75
+    "              metrics=['accuracy'])"
76
+   ]
77
+  },
78
+  {
79
+   "cell_type": "code",
80
+   "execution_count": 10,
81
+   "metadata": {},
82
+   "outputs": [],
83
+   "source": [
84
+    "class_name = np.load(\"numpy/class_name.npy\")\n",
85
+    "file_name = os.listdir(\"plants/test/\")\n",
86
+    "\n",
87
+    "output = []\n",
88
+    "\n",
89
+    "for file in file_name:\n",
90
+    "    file_dir = \"plants/test/\" + file\n",
91
+    "    image = cv2.imread(file_dir) \n",
92
+    "    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
93
+    "    image = cv2.resize(image, (150,150))\n",
94
+    "    image = np.array(image).reshape([1,150,150,3])\n",
95
+    "    image = image.astype(\"float32\")/255\n",
96
+    "    predict = model.predict(image,1)\n",
97
+    "    predict = np.argmax(predict)\n",
98
+    "    str = file + \",\" + class_name[predict]\n",
99
+    "    output.append(str)\n",
100
+    "    \n",
101
+    "# print(output)"
102
+   ]
103
+  },
104
+  {
105
+   "cell_type": "code",
106
+   "execution_count": 11,
107
+   "metadata": {},
108
+   "outputs": [],
109
+   "source": [
110
+    "file_name = \"result_2.csv\"\n",
111
+    "file = open(file_name, 'w')\n",
112
+    "print(\"file,species\", file=file)\n",
113
+    "for result in output:\n",
114
+    "    print(result, file=file) \n",
115
+    "    \n",
116
+    "file.close()"
117
+   ]
118
+  },
119
+  {
120
+   "cell_type": "code",
121
+   "execution_count": null,
122
+   "metadata": {},
123
+   "outputs": [],
124
+   "source": []
125
+  }
126
+ ],
127
+ "metadata": {
128
+  "kernelspec": {
129
+   "display_name": "Python 3",
130
+   "language": "python",
131
+   "name": "python3"
132
+  },
133
+  "language_info": {
134
+   "codemirror_mode": {
135
+    "name": "ipython",
136
+    "version": 3
137
+   },
138
+   "file_extension": ".py",
139
+   "mimetype": "text/x-python",
140
+   "name": "python",
141
+   "nbconvert_exporter": "python",
142
+   "pygments_lexer": "ipython3",
143
+   "version": "3.6.4"
144
+  }
145
+ },
146
+ "nbformat": 4,
147
+ "nbformat_minor": 2
148
+}

+ 150
- 0
1_Plant Seedlings Classification_0.97/1_3rd_DataAugmentation_and_Resize.ipynb View File

@@ -0,0 +1,150 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": null,
6
+   "metadata": {},
7
+   "outputs": [],
8
+   "source": [
9
+    "import os\n",
10
+    "import numpy as np\n",
11
+    "import cv2\n",
12
+    "from keras.preprocessing.image import ImageDataGenerator\n",
13
+    "from math import ceil\n",
14
+    "from multiprocessing import Pool"
15
+   ]
16
+  },
17
+  {
18
+   "cell_type": "code",
19
+   "execution_count": null,
20
+   "metadata": {},
21
+   "outputs": [],
22
+   "source": [
23
+    "raw_dir = \"plants/raw/\"\n",
24
+    "file_list = os.listdir(raw_dir)\n",
25
+    "class_name = []\n",
26
+    "for name in file_list:\n",
27
+    "    if not name.startswith('.'):\n",
28
+    "        class_name.append(name)"
29
+   ]
30
+  },
31
+  {
32
+   "cell_type": "code",
33
+   "execution_count": null,
34
+   "metadata": {},
35
+   "outputs": [],
36
+   "source": [
37
+    "augmented_dir = \"plants/augmented_multi/\"\n",
38
+    "\n",
39
+    "if not os.path.exists(augmented_dir):\n",
40
+    "        os.mkdir(augmented_dir)\n",
41
+    "        \n",
42
+    "for name in class_name:\n",
43
+    "    class_dir = augmented_dir + name + \"/\"\n",
44
+    "    if not os.path.exists(class_dir):\n",
45
+    "        os.mkdir(class_dir)"
46
+   ]
47
+  },
48
+  {
49
+   "cell_type": "code",
50
+   "execution_count": null,
51
+   "metadata": {},
52
+   "outputs": [],
53
+   "source": [
54
+    "target = 3000\n",
55
+    "\n",
56
+    "datagen = ImageDataGenerator(\n",
57
+    "        rotation_range=40,\n",
58
+    "        width_shift_range=0.2,\n",
59
+    "        height_shift_range=0.2,\n",
60
+    "        shear_range=0.2,\n",
61
+    "        zoom_range=0.2,\n",
62
+    "        vertical_flip=True,\n",
63
+    "        horizontal_flip=True,\n",
64
+    "        fill_mode='reflect')\n",
65
+    "\n",
66
+    "def augmentation(name):\n",
67
+    "    print(\"Start Augmenting \" + name)\n",
68
+    "    class_dir = raw_dir + name +\"/\"\n",
69
+    "    augmented_class_dir = augmented_dir + name + \"/\"\n",
70
+    "    \n",
71
+    "    file_list = os.listdir(class_dir)\n",
72
+    "    augmented_list = os.listdir(augmented_class_dir)\n",
73
+    "    \n",
74
+    "    for file_name in file_list:\n",
75
+    "        if not file_name.endswith(\".png\"):\n",
76
+    "            file_list.remove(file_name)\n",
77
+    "            print(\"Removed_1\")\n",
78
+    "    \n",
79
+    "    num_augment = ceil(target / len(file_list)) + 1\n",
80
+    "    \n",
81
+    "    for file_name in file_list:\n",
82
+    "        if not file_name.endswith(\".png\"):\n",
83
+    "            continue\n",
84
+    "\n",
85
+    "        file_dir = class_dir + file_name\n",
86
+    "        image = cv2.imread(file_dir)\n",
87
+    "        print(\"1\")\n",
88
+    "        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
89
+    "#         image = image[...,::-1] # BGR2RGB\n",
90
+    "        print(image.shape)\n",
91
+    "        image = cv2.resize(image,(100,100))\n",
92
+    "        print(\"2\")\n",
93
+    "        height, width, channel = image.shape\n",
94
+    "        \n",
95
+    "        if channel != 3:\n",
96
+    "            print(\"Removed_2\")\n",
97
+    "            continue\n",
98
+    "            \n",
99
+    "        x = np.array(image).reshape([1,height,width,channel])\n",
100
+    "    \n",
101
+    "        i = 0\n",
102
+    "        for batch in datagen.flow(x, batch_size=1,\n",
103
+    "                                  save_to_dir=augmented_class_dir, save_prefix='aug_', save_format='png'):\n",
104
+    "            i += 1\n",
105
+    "            if i >= num_augment:\n",
106
+    "                break  # otherwise the generator would loop indefinitely\n",
107
+    "\n",
108
+    "    print(\"Finished Augmenting \" + name)"
109
+   ]
110
+  },
111
+  {
112
+   "cell_type": "code",
113
+   "execution_count": null,
114
+   "metadata": {},
115
+   "outputs": [],
116
+   "source": [
117
+    "p = Pool()\n",
118
+    "p.map(augmentation, [name for name in class_name])"
119
+   ]
120
+  },
121
+  {
122
+   "cell_type": "code",
123
+   "execution_count": null,
124
+   "metadata": {},
125
+   "outputs": [],
126
+   "source": []
127
+  }
128
+ ],
129
+ "metadata": {
130
+  "kernelspec": {
131
+   "display_name": "Python 3",
132
+   "language": "python",
133
+   "name": "python3"
134
+  },
135
+  "language_info": {
136
+   "codemirror_mode": {
137
+    "name": "ipython",
138
+    "version": 3
139
+   },
140
+   "file_extension": ".py",
141
+   "mimetype": "text/x-python",
142
+   "name": "python",
143
+   "nbconvert_exporter": "python",
144
+   "pygments_lexer": "ipython3",
145
+   "version": "3.6.4"
146
+  }
147
+ },
148
+ "nbformat": 4,
149
+ "nbformat_minor": 2
150
+}

+ 77
- 0
1_Plant Seedlings Classification_0.97/1_3rd_DataAugmentation_and_Resize.py View File

@@ -0,0 +1,77 @@
1
+import os
2
+import numpy as np
3
+import cv2
4
+from keras.preprocessing.image import ImageDataGenerator
5
+from math import ceil
6
+from multiprocessing import Pool
7
+
8
+raw_dir = "plants/raw/"
9
+file_list = os.listdir(raw_dir)
10
+class_name = []
11
+for name in file_list:
12
+    if not name.startswith('.'):
13
+        class_name.append(name)
14
+
15
+np.save("numpy/class_name.npy", class_name)
16
+
17
+augmented_dir = "plants/augmented_resized/"
18
+
19
+if not os.path.exists(augmented_dir):
20
+        os.mkdir(augmented_dir)
21
+        
22
+for name in class_name:
23
+    class_dir = augmented_dir + name + "/"
24
+    if not os.path.exists(class_dir):
25
+        os.mkdir(class_dir)
26
+
27
+target = 3000
28
+
29
+datagen = ImageDataGenerator(
30
+        rotation_range=40,
31
+        width_shift_range=0.2,
32
+        height_shift_range=0.2,
33
+        shear_range=0.2,
34
+        zoom_range=0.2,
35
+        vertical_flip=True,
36
+        horizontal_flip=True,
37
+        fill_mode='reflect')
38
+
39
+def augmentation(name):
40
+    print("Start Augmenting " + name)
41
+    class_dir = raw_dir + name +"/"
42
+    augmented_class_dir = augmented_dir + name + "/"
43
+    
44
+    file_list = os.listdir(class_dir)
45
+    augmented_list = os.listdir(augmented_class_dir)
46
+    
47
+    num_augment = ceil(target / len(file_list)) + 1
48
+    
49
+    for file_name in file_list:
50
+        if not file_name.endswith(".png"):
51
+            continue
52
+
53
+        file_dir = class_dir + file_name
54
+        image = cv2.imread(file_dir)
55
+        image = cv2.resize(image,(150,150))
56
+        height, width, channel = image.shape
57
+        
58
+        if channel != 3:
59
+            continue
60
+        
61
+        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
62
+        x = np.array(image).reshape([1,height,width,channel])
63
+    
64
+        for i in range(num_augment):
65
+            for gen_image in datagen.flow(x, batch_size=1):
66
+                gen_image = gen_image.reshape([height, width, channel])
67
+                RGB_image = cv2.cvtColor(gen_image, cv2.COLOR_RGB2BGR)
68
+                cv2.imwrite('{}aug_{}_{}.png'.format(augmented_class_dir,file_name,i), RGB_image)
69
+                break
70
+
71
+    print("Finished Augmenting " + name)
72
+
73
+
74
+# In[ ]:
75
+
76
+p = Pool()
77
+p.map(augmentation, [name for name in class_name])

+ 130
- 0
1_Plant Seedlings Classification_0.97/2_3rd_categorical_normalize.ipynb View File

@@ -0,0 +1,130 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": 5,
6
+   "metadata": {},
7
+   "outputs": [],
8
+   "source": [
9
+    "import cv2\n",
10
+    "import os\n",
11
+    "import numpy as np\n",
12
+    "from multiprocessing import Pool\n",
13
+    "from random import shuffle"
14
+   ]
15
+  },
16
+  {
17
+   "cell_type": "code",
18
+   "execution_count": 6,
19
+   "metadata": {},
20
+   "outputs": [],
21
+   "source": [
22
+    "class_name = np.load(\"class_name.npy\")\n",
23
+    "\n",
24
+    "data_dir = \"plants/augmented_resized/\"\n",
25
+    "for name in class_name:\n",
26
+    "    data_list = os.listdir(data_dir + name)\n",
27
+    "    shuffle(data_list)\n",
28
+    "    np.save(\"{}_list.npy\".format(name), data_list)"
29
+   ]
30
+  },
31
+  {
32
+   "cell_type": "code",
33
+   "execution_count": 8,
34
+   "metadata": {},
35
+   "outputs": [
36
+    {
37
+     "ename": "error",
38
+     "evalue": "/Users/gyuhyongjeon/opencv/modules/imgproc/src/color.cpp:9716: error: (-215) scn == 3 || scn == 4 in function cvtColor\n",
39
+     "output_type": "error",
40
+     "traceback": [
41
+      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
42
+      "\u001b[0;31merror\u001b[0m                                     Traceback (most recent call last)",
43
+      "\u001b[0;32m<ipython-input-8-5c2394932987>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     14\u001b[0m             \u001b[0mfile_dir\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata_dir\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mfile\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m             \u001b[0mimage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfile_dir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m             \u001b[0mimage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcvtColor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCOLOR_BGR2RGB\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     17\u001b[0m             \u001b[0mimage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mimage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'float32'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m255\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m             \u001b[0mtemp_train\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
44
+      "\u001b[0;31merror\u001b[0m: /Users/gyuhyongjeon/opencv/modules/imgproc/src/color.cpp:9716: error: (-215) scn == 3 || scn == 4 in function cvtColor\n"
45
+     ]
46
+    }
47
+   ],
48
+   "source": [
49
+    "target = 3000\n",
50
+    "each = 100\n",
51
+    "\n",
52
+    "x_train = []\n",
53
+    "y_train = []\n",
54
+    "temp_train = []\n",
55
+    "\n",
56
+    "for i in range(int(target/each)):\n",
57
+    "    for name in class_name:\n",
58
+    "        file_list = np.load(name + \"_list.npy\")\n",
59
+    "        file_list = file_list[i*100:(i+1)*100]\n",
60
+    "\n",
61
+    "        for file in file_list:\n",
62
+    "            file_dir = data_dir + file\n",
63
+    "            image = cv2.imread(file_dir) \n",
64
+    "            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
65
+    "            image = image.astype('float32')/255\n",
66
+    "            temp_train.append([image, name])\n",
67
+    "        \n",
68
+    "#         print(len(temp_train))\n",
69
+    "        \n",
70
+    "    shuffle(temp_train)\n",
71
+    "    \n",
72
+    "    y_train = [j[1] for j in temp_train]\n",
73
+    "    \n",
74
+    "    # convert to one hot encoing \n",
75
+    "    train_hot = np.zeros([len(y_train), num_classes]).astype(\"unit8\")\n",
76
+    "\n",
77
+    "    for k in range(len(y_train)):\n",
78
+    "        j = 0\n",
79
+    "        for name in class_name:\n",
80
+    "            if y_train[k] == name:\n",
81
+    "                break\n",
82
+    "            else:\n",
83
+    "                j += 1\n",
84
+    "\n",
85
+    "        train_hot[k][j] = 1   \n",
86
+    "\n",
87
+    "    y_train = train_hot\n",
88
+    "\n",
89
+    "    \n",
90
+    "    x_train = np.array([j[0] for j in temp_train]).reshape(-1,100,100,3)\n",
91
+    "\n",
92
+    "    np.save(\"x_train_{}.npy\".format(i) ,x_train)\n",
93
+    "    np.save(\"y_train_{}.npy\".format(i) ,y_train)\n",
94
+    "    \n",
95
+    "    train_hot = []\n",
96
+    "    x_train = []\n",
97
+    "    y_train = []\n",
98
+    "    temp_train = []"
99
+   ]
100
+  },
101
+  {
102
+   "cell_type": "code",
103
+   "execution_count": null,
104
+   "metadata": {},
105
+   "outputs": [],
106
+   "source": []
107
+  }
108
+ ],
109
+ "metadata": {
110
+  "kernelspec": {
111
+   "display_name": "Python 3",
112
+   "language": "python",
113
+   "name": "python3"
114
+  },
115
+  "language_info": {
116
+   "codemirror_mode": {
117
+    "name": "ipython",
118
+    "version": 3
119
+   },
120
+   "file_extension": ".py",
121
+   "mimetype": "text/x-python",
122
+   "name": "python",
123
+   "nbconvert_exporter": "python",
124
+   "pygments_lexer": "ipython3",
125
+   "version": "3.6.4"
126
+  }
127
+ },
128
+ "nbformat": 4,
129
+ "nbformat_minor": 2
130
+}

+ 69
- 0
1_Plant Seedlings Classification_0.97/2_3rd_categorical_normalize.py View File

@@ -0,0 +1,69 @@
1
+import cv2
2
+import os
3
+import numpy as np
4
+from multiprocessing import Pool
5
+from random import shuffle
6
+
7
+class_name = np.load("numpy/class_name.npy")
8
+num_classes = len(class_name)
9
+
10
+data_dir = "plants/augmented_resized/"
11
+for name in class_name:
12
+    data_list = os.listdir(data_dir + name)
13
+    shuffle(data_list)
14
+    np.save("numpy/{}_list.npy".format(name), data_list)
15
+
16
+target = 3000
17
+each = 100
18
+
19
+x_train = []
20
+y_train = []
21
+temp_train = []
22
+
23
+for i in range(int(target/each)):
24
+    for name in class_name:
25
+        class_dir = data_dir + name + "/"
26
+        #print(class_dir)
27
+        file_list = np.load("numpy/" + name + "_list.npy")
28
+        file_list = file_list[i*100:(i+1)*100]
29
+
30
+        for file in file_list:
31
+            file_dir = class_dir + file
32
+            #print(file_dir)
33
+            image = cv2.imread(file_dir) 
34
+            #print(image.shape)
35
+            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
36
+            image = image.astype('float32')/255
37
+            temp_train.append([image, name])
38
+        
39
+#         print(len(temp_train))
40
+        
41
+    shuffle(temp_train)
42
+    
43
+    y_train = [j[1] for j in temp_train]
44
+    
45
+    # convert to one hot encoing 
46
+    train_hot = np.zeros([len(y_train), num_classes]).astype("uint8")
47
+
48
+    for k in range(len(y_train)):
49
+        j = 0
50
+        for name in class_name:
51
+            if y_train[k] == name:
52
+                break
53
+            else:
54
+                j += 1
55
+
56
+        train_hot[k][j] = 1   
57
+
58
+    y_train = train_hot
59
+    
60
+    x_train = np.array([j[0] for j in temp_train]).reshape(-1,150,150,3)
61
+
62
+    np.save("numpy/x_train_{}.npy".format(i) ,x_train)
63
+    np.save("numpy/y_train_{}.npy".format(i) ,y_train)
64
+    
65
+    train_hot = []
66
+    x_train = []
67
+    y_train = []
68
+    temp_train = []
69
+

+ 117
- 0
1_Plant Seedlings Classification_0.97/3_3rd_Train.ipynb View File

@@ -0,0 +1,117 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": null,
6
+   "metadata": {},
7
+   "outputs": [],
8
+   "source": [
9
+    "import numpy as np # linear algebra\n",
10
+    "import os, cv2\n",
11
+    "from tqdm import tqdm\n",
12
+    "\n",
13
+    "from keras import optimizers\n",
14
+    "from keras.models import Sequential, Model, load_model\n",
15
+    "from keras.layers import Dense, Dropout, Flatten, Input\n",
16
+    "from keras.layers import Conv2D, MaxPooling2D, BatchNormalization\n",
17
+    "from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n",
18
+    "from keras.applications import *"
19
+   ]
20
+  },
21
+  {
22
+   "cell_type": "code",
23
+   "execution_count": null,
24
+   "metadata": {},
25
+   "outputs": [],
26
+   "source": [
27
+    "dim = 150\n",
28
+    "epochs = 100\n",
29
+    "batch_size = 32 \n",
30
+    "\n",
31
+    "base_model = InceptionResNetV2(input_shape=(dim, dim, 3), include_top=False, weights=None, pooling='avg')\n",
32
+    "x = base_model.output\n",
33
+    "x = Dense(256, activation='relu')(x)\n",
34
+    "x = Dropout(0.3)(x)\n",
35
+    "predictions = Dense(12, activation='softmax')(x)\n",
36
+    "model = Model(inputs=base_model.input, outputs=predictions)\n",
37
+    "model.compile(loss='categorical_crossentropy', \n",
38
+    "              optimizer=optimizers.Adam(), metrics=['accuracy'])\n",
39
+    "model.summary()"
40
+   ]
41
+  },
42
+  {
43
+   "cell_type": "code",
44
+   "execution_count": null,
45
+   "metadata": {},
46
+   "outputs": [],
47
+   "source": [
48
+    "filepath = \"weights-improvement-{val_acc:.4f}.hdf5\"\n",
49
+    "checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')\n",
50
+    "\n",
51
+    "learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', \n",
52
+    "                                            patience=3, \n",
53
+    "                                            verbose=1, \n",
54
+    "                                            factor=0.5, \n",
55
+    "                                            min_lr=0.00001)\n",
56
+    "\n",
57
+    "callbacks_list = [checkpoint, learning_rate_reduction]"
58
+   ]
59
+  },
60
+  {
61
+   "cell_type": "code",
62
+   "execution_count": null,
63
+   "metadata": {},
64
+   "outputs": [],
65
+   "source": [
66
+    "x_list = os.listdir(\"numpy/\")\n",
67
+    "for file in x_list:\n",
68
+    "    if not file.startswith(\"x_train\"):\n",
69
+    "        x_list.remove(file)\n",
70
+    "        \n",
71
+    "for i in range(int(epochs/10)):\n",
72
+    "    for file in x_list:\n",
73
+    "        x_train = np.load(\"numpy/\" + file)\n",
74
+    "        y_name = \"y\" + file[1:]\n",
75
+    "    #     print(y_name)\n",
76
+    "        y_train = np.load(\"numpy/\" + y_name)\n",
77
+    "\n",
78
+    "        model.fit(x_train, y_train,\n",
79
+    "                        batch_size=batch_size,\n",
80
+    "                        epochs=int(epochs/10),\n",
81
+    "                        verbose=1,\n",
82
+    "                         validation_split=0.1,\n",
83
+    "                        callbacks=callbacks_list)\n",
84
+    "    # Save the trained weights in to .h5 format\n",
85
+    "    model.save_weights(\"finished_{}.hdf5\".format((i+1)*int(epochs/10)))"
86
+   ]
87
+  },
88
+  {
89
+   "cell_type": "code",
90
+   "execution_count": null,
91
+   "metadata": {},
92
+   "outputs": [],
93
+   "source": []
94
+  }
95
+ ],
96
+ "metadata": {
97
+  "kernelspec": {
98
+   "display_name": "Python 3",
99
+   "language": "python",
100
+   "name": "python3"
101
+  },
102
+  "language_info": {
103
+   "codemirror_mode": {
104
+    "name": "ipython",
105
+    "version": 3
106
+   },
107
+   "file_extension": ".py",
108
+   "mimetype": "text/x-python",
109
+   "name": "python",
110
+   "nbconvert_exporter": "python",
111
+   "pygments_lexer": "ipython3",
112
+   "version": "3.6.4"
113
+  }
114
+ },
115
+ "nbformat": 4,
116
+ "nbformat_minor": 2
117
+}

+ 80
- 0
1_Plant Seedlings Classification_0.97/3_3rd_Train.py View File

@@ -0,0 +1,80 @@
1
+
2
+import numpy as np
3
+import os, cv2
4
+from tqdm import tqdm
5
+
6
+from keras import optimizers
7
+from keras.models import Sequential, Model, load_model
8
+from keras.layers import Dense, Dropout, Flatten, Input
9
+from keras.layers import Conv2D, MaxPooling2D, BatchNormalization
10
+from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
11
+from keras.applications import *
12
+
13
+
14
+# In[2]:
15
+
16
+
17
+dim = 150
18
+epochs = 100
19
+batch_size = 32 
20
+
21
+base_model = InceptionResNetV2(input_shape=(dim, dim, 3), include_top=False, weights=None, pooling='max')
22
+x = base_model.output
23
+x = Dense(256, activation='relu')(x)
24
+x = Dropout(0.3)(x)
25
+predictions = Dense(12, activation='softmax')(x)
26
+model = Model(inputs=base_model.input, outputs=predictions)
27
+model.compile(loss='categorical_crossentropy', 
28
+              optimizer=optimizers.Adam(), metrics=['accuracy'])
29
+model.summary()
30
+
31
+# Convert model into JSON Format
32
+model_json = model.to_json()
33
+with open("model.json", "w") as json_file:
34
+    json_file.write(model_json)
35
+
36
+# In[8]:
37
+
38
+
39
+filepath = "weights-improvement-{val_acc:.4f}.hdf5"
40
+checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
41
+
42
+learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', 
43
+                                            patience=3, 
44
+                                            verbose=1, 
45
+                                            factor=0.5, 
46
+                                            min_lr=0.00001)
47
+
48
+callbacks_list = [checkpoint, learning_rate_reduction]
49
+
50
+
51
+# In[ ]:
52
+
53
+
54
+x_list = os.listdir("numpy/")
55
+temp = []
56
+for file in x_list:
57
+    if file.startswith("x_train"):
58
+        temp.append(file)
59
+x_list = temp
60
+del temp
61
+
62
+x_test = np.load("numpy/x_test.npy")
63
+y_test = np.load("numpy/y_test.npy")
64
+
65
+for i in range(int(epochs/10)):
66
+    print("Epochs : ", i+1)
67
+    for file in x_list:
68
+        x_train = np.load("numpy/" + file)
69
+        y_name = "y" + file[1:]
70
+    #     print(y_name)
71
+        y_train = np.load("numpy/" + y_name)
72
+
73
+        model.fit(x_train, y_train,
74
+                        batch_size=batch_size,
75
+                        epochs=int(epochs/10),
76
+                        verbose=1,
77
+                        validation_data=(x_test,y_test),
78
+                        callbacks=callbacks_list)
79
+
80
+    model.save_weights("finished_{}.hdf5".format((i+1)*int(epochs/10)))

+ 108
- 0
1_Plant Seedlings Classification_0.97/4_3rd_Testset.ipynb View File

@@ -0,0 +1,108 @@
1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": 5,
6
+   "metadata": {},
7
+   "outputs": [],
8
+   "source": [
9
+    "import os\n",
10
+    "import numpy as np\n",
11
+    "import cv2\n",
12
+    "from random import shuffle"
13
+   ]
14
+  },
15
+  {
16
+   "cell_type": "code",
17
+   "execution_count": 6,
18
+   "metadata": {},
19
+   "outputs": [],
20
+   "source": [
21
+    "class_name = np.load(\"numpy/class_name.npy\")\n",
22
+    "num_classes = len(class_name)"
23
+   ]
24
+  },
25
+  {
26
+   "cell_type": "code",
27
+   "execution_count": 8,
28
+   "metadata": {},
29
+   "outputs": [
30
+    {
31
+     "ename": "error",
32
+     "evalue": "/Users/gyuhyongjeon/opencv/modules/imgproc/src/color.cpp:9716: error: (-215) scn == 3 || scn == 4 in function cvtColor\n",
33
+     "output_type": "error",
34
+     "traceback": [
35
+      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
36
+      "\u001b[0;31merror\u001b[0m                                     Traceback (most recent call last)",
37
+      "\u001b[0;32m<ipython-input-8-1dfbd775bd5c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m     11\u001b[0m         \u001b[0mfile_dir\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mclass_dir\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mfile\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     12\u001b[0m         \u001b[0mimage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfile_dir\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m         \u001b[0mimage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcvtColor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mCOLOR_BGR2RGB\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     14\u001b[0m         \u001b[0mimage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mimage\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mastype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'float32'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0;36m255\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m         \u001b[0mtemp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
38
+      "\u001b[0;31merror\u001b[0m: /Users/gyuhyongjeon/opencv/modules/imgproc/src/color.cpp:9716: error: (-215) scn == 3 || scn == 4 in function cvtColor\n"
39
+     ]
40
+    }
41
+   ],
42
+   "source": [
43
+    "raw_dir = \"plants/raw/\"\n",
44
+    "temp = []\n",
45
+    "\n",
46
+    "for name in class_name:\n",
47
+    "    class_dir = raw_dir + name + \"/\"\n",
48
+    "    file_list = os.listdir(class_dir)\n",
49
+    "    shuffle(file_list)\n",
50
+    "    file_list = file_list[:50]\n",
51
+    "    \n",
52
+    "    for file in file_list:\n",
53
+    "        file_dir = class_dir + file\n",
54
+    "        image = cv2.imread(file_dir) \n",
55
+    "        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
56
+    "        image = image.astype('float32')/255\n",
57
+    "        temp.append([image, name])\n",
58
+    "        \n",
59
+    "y_test = [j[1] for j in temp_train]\n",
60
+    "train_hot = np.zeros([len(y_test), num_classes]).astype(\"unit8\")\n",
61
+    "\n",
62
+    "for k in range(len(y_test)):\n",
63
+    "    j = 0\n",
64
+    "    for name in class_name:\n",
65
+    "        if y_test[k] == name:\n",
66
+    "            break\n",
67
+    "        else:\n",
68
+    "            j += 1\n",
69
+    "\n",
70
+    "    train_hot[k][j] = 1   \n",
71
+    "\n",
72
+    "y_test = train_hot\n",
73
+    "x_test = np.array([j[0] for j in temp]).reshape(-1,158,150,3)\n",
74
+    "\n",
75
+    "np.save(\"x_test_{}.npy\".format(i) ,x_test)\n",
76
+    "np.save(\"y_test_{}.npy\".format(i) ,y_test)"
77
+   ]
78
+  },
79
+  {
80
+   "cell_type": "code",
81
+   "execution_count": null,
82
+   "metadata": {},
83
+   "outputs": [],
84
+   "source": []
85
+  }
86
+ ],