-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjunghoLee_classification.py
executable file
·162 lines (127 loc) · 4.84 KB
/
junghoLee_classification.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import tensorflow as tf
import numpy as np
import cv2
from tensorflow import keras
from keras.models import Model
from keras.layers import Dense, Conv2D, BatchNormalization, Activation
from keras.layers import AveragePooling2D, MaxPooling2D, Input, Flatten, Dropout
from keras import optimizers
from keras.utils import to_categorical
import sys
import os
DATA_DIR = os.path.join(os.getcwd(),'data')
TRAIN_DIR = os.path.join(DATA_DIR,'seg_train')
VAL_DIR = os.path.join(DATA_DIR,'seg_pred')
TEST_DIR = os.path.join(DATA_DIR,'seg_test')
category = [ 'buildings', 'forest', 'glacier', 'mountain', 'sea', 'street' ]
CLASS_NUM = 6
EPOCHS = 100
for cat in category:
print("The total pics in {} folder are {}".format(cat, len(os.listdir(os.path.join(TRAIN_DIR, cat)))))
def convframe(path, category):
train_data = []
labels = []
data_list = []
images = []
for cat in category:
cat_path = os.path.join(path,cat)
for data in os.listdir(cat_path):
image = cv2.imread(cat_path + '/' + data)
image = cv2.resize(image, (150,150))
images.append(image)
if cat == 'buildings':
label = 0
elif cat == 'forest':
label = 1
elif cat == 'glacier':
label = 2
elif cat == 'mountain':
label = 3
elif cat == 'sea':
label = 4
elif cat == 'street':
label = 5
labels.append(label)
return images, labels
def net(images, labels, test_images, test_labels):
data = Input(shape=(150,150,3))
#conv1
conv1_1 = Conv2D(64, (3,3), padding='same')(data)
bn1_1 = BatchNormalization()(conv1_1)
act1_1 = Activation('relu')(bn1_1)
conv1_2= Conv2D(64, (3,3), padding='same')(act1_1)
bn1_2 = BatchNormalization()(conv1_2)
act1_2 = Activation('relu')(bn1_2)
pool1 = MaxPooling2D((2,2), strides=2)(act1_2)
# 75x75x64
# conv2
conv2_1 = Conv2D(128, (3,3), padding='same')(pool1)
bn2_1 = BatchNormalization()(conv2_1)
act2_1 = Activation('relu')(bn2_1)
conv2_2 = Conv2D(128, (3,3), padding='same')(act2_1)
bn2_2 = BatchNormalization()(conv2_2)
act2_2 = Activation('relu')(bn2_2)
pool2 = MaxPooling2D((2,2), strides=2, padding='same')(act2_2)
# 38x38x128
# conv3
conv3_1 = Conv2D(256, (3,3), padding='same')(pool2)
bn3_1 = BatchNormalization()(conv3_1)
act3_1 = Activation('relu')(bn3_1)
conv3_2 = Conv2D(256, (3,3), padding='same')(act3_1)
bn3_2 = BatchNormalization()(conv3_2)
act3_2 = Activation('relu')(bn3_2)
conv3_3 = Conv2D(256, (3,3), padding='same')(act3_2)
bn3_3 = BatchNormalization()(conv3_3)
act3_3 = Activation('relu')(bn3_3)
pool3 = MaxPooling2D((2,2), strides=2)(act3_3)
# 19x19x256
# conv4
conv4_1 = Conv2D(512, (3,3), padding='same')(pool3)
bn4_1 = BatchNormalization()(conv4_1)
act4_1 = Activation('relu')(bn4_1)
conv4_2 = Conv2D(512, (3,3), padding='same')(act4_1)
bn4_2= BatchNormalization()(conv4_2)
act4_2 = Activation('relu')(bn4_2)
conv4_3 = Conv2D(512, (3,3), padding='same')(act4_2)
bn4_3 = BatchNormalization()(conv4_3)
act4_3 = Activation('relu')(bn4_3)
pool4 = MaxPooling2D((2,2), strides=2, padding='same')(act4_3)
# 10x10x512
# conv5
conv5_1 = Conv2D(512, (3,3), padding='same')(pool4)
bn5_1 = BatchNormalization()(conv5_1)
act5_1 = Activation('relu')(bn5_1)
conv5_2 = Conv2D(512, (3,3), padding='same')(act5_1)
bn5_2= BatchNormalization()(conv5_2)
act5_2 = Activation('relu')(bn5_2)
conv5_3 = Conv2D(512, (3,3), padding='same')(act5_2)
bn5_3 = BatchNormalization()(conv5_3)
act5_3 = Activation('relu')(bn5_3)
pool5 = MaxPooling2D((2,2), strides=2)(act5_3)
# 5x5x512
flat = Flatten()(pool5)
hidden1 = Dense(4096, activation='relu')(flat)
drop1 = Dropout(0.5)(hidden1)
hidden2 = Dense(4096, activation='relu')(drop1)
drop2 = Dropout(0.5)(hidden2)
output = Dense(CLASS_NUM, activation='softmax')(drop2)
model = Model(inputs=data, outputs=output)
print(model.summary())
opt = optimizers.SGD(0.001, 0.9)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(images, to_categorical(labels, CLASS_NUM), batch_size=32, epochs=EPOCHS, validation_data=(test_images, to_categorical(test_labels, CLASS_NUM)), shuffle=True)
model.save('vgg19.h5')
del model
images, labels = convframe(TRAIN_DIR, category)
test_images, test_labels = convframe(TEST_DIR, category)
labels = np.array(labels)
images = np.array(images)
test_images = np.array(test_images)
test_labels = np.array(test_labels)
'''
for idx in range(len(train_data)):
if images[idx].shape[0] is not 150 or images[idx].shape[1] is not 150:
print(images[idx].shape)
num +=1
'''
net(images, labels, test_images, test_labels)