-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCNN_SoftMax.py
More file actions
212 lines (146 loc) · 6.33 KB
/
Copy pathCNN_SoftMax.py
File metadata and controls
212 lines (146 loc) · 6.33 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import numpy as np
import PIL.Image as img
import tensorflow as tf
import matplotlib.pyplot as plt
import pandas as pd
import os
file = pd.read_csv("./label_soft(temp).csv")
file2 = pd.read_csv("./test_label_soft(temp).csv")
label = file.values
test_label = file2.values
label = np.array(label)
print(label.shape)
print(label, label.shape)
label = label[:, np.newaxis]
test_label = np.array(test_label)
print(test_label.shape)
print(test_label, test_label.shape)
test_label = test_label[:, np.newaxis]
def img_data(path):
image = img.open('./'+path+'/1.jpg')
x = np.array(image)
temp1 = np.array(image)
print("초기 이미지 행렬:", x.shape)
x = x[np.newaxis]
x = np.divide(x, 255)
num = len(os.listdir('./'+path+'/')) + 1
for i in range(2, num):
try:
im = img.open('./'+path+'/' + str(i) + '.jpg')
temp = np.array(im)
temp = temp.astype('float32')
temp = np.divide(temp, 255)
if temp1.shape == temp.shape:
temp = temp[np.newaxis]
x = np.concatenate((x, temp), axis=0)
except:
print("파일 없음" + str(i))
return x
data = img_data("resized(temp)")
test_data = img_data("test_image(temp)")
test_data_ = tf.reshape(test_data, [-1, 240*320*3])
print("행렬 합친 후:", data.shape)
data_ = tf.reshape(data, [-1, 240*320*3])
print(data_)
temp_data = tf.reshape(data_, [-1, 320, 240, 3])
print(temp_data.shape)
batch_size = 1
dataset = tf.data.Dataset.from_tensor_slices(({"image" : data_}, label))
dataset = dataset.batch(batch_size).repeat()
iterator = dataset.make_one_shot_iterator()
next_data = iterator.get_next()
dataset2 = tf.data.Dataset.from_tensor_slices(({"image" : test_data_}, test_label))
dataset2 = dataset2.batch(batch_size).repeat()
iterator2 = dataset2.make_one_shot_iterator()
next_data2 = iterator2.get_next()
def build_CNN_classifier(x):
x_image = tf.reshape(x, [-1, 320, 240, 3])
#이미지 크기: 320 * 240 * 3
W_conv1 = tf.Variable(tf.truncated_normal(shape=[5, 5, 3, 512], stddev=5e-2))
b_conv1 = tf.Variable(tf.constant(0.1, shape=[512]))
h_conv1 = tf.nn.relu(tf.nn.conv2d(x_image, W_conv1, strides=[1, 1, 1, 1], padding='SAME') + b_conv1)
h_pool1 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#이미지 크기: 160 * 120 * 512
W_conv2 = tf.Variable(tf.truncated_normal(shape=[5, 5, 512, 256], stddev=5e-2))
b_conv2 = tf.Variable(tf.constant(0.1, shape=[256]))
h_conv2 = tf.nn.relu(tf.nn.conv2d(h_pool1, W_conv2, strides=[1, 1, 1, 1], padding='SAME') + b_conv2)
h_pool2 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#이미지 크기: 80 * 60 * 256
W_conv3 = tf.Variable(tf.truncated_normal(shape=[5, 5, 256, 128], stddev=5e-2))
b_conv3 = tf.Variable(tf.constant(0.1, shape=[128]))
h_conv3 = tf.nn.relu(tf.nn.conv2d(h_pool2, W_conv3, strides=[1, 1, 1, 1], padding='SAME') + b_conv3)
h_pool3 = tf.nn.max_pool(h_conv3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#이미지 크기: 40 * 30 * 128
W_conv4 = tf.Variable(tf.truncated_normal(shape=[5, 5, 128, 64], stddev=5e-2))
b_conv4 = tf.Variable(tf.constant(0.1, shape=[64]))
h_conv4 = tf.nn.relu(tf.nn.conv2d(h_pool3, W_conv4, strides=[1, 1, 1, 1], padding='SAME') + b_conv4)
h_pool4 = tf.nn.max_pool(h_conv4, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
#이미지 크기: 20 * 15 * 64
W_fc1 = tf.Variable(tf.truncated_normal(shape=[20*15*64, 1024], stddev=5e-2))
b_fc1 = tf.Variable(tf.constant(0.1, shape=[1024]))
h_pool2_flat = tf.reshape(h_pool4, [-1, 20*15*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
W_fc2 = tf.Variable(tf.truncated_normal(shape=[1024, 512], stddev=5e-2))
b_fc2 = tf.Variable(tf.constant(0.1, shape=[512]))
h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_fc2) + b_fc2)
W_fc3 = tf.Variable(tf.truncated_normal(shape=[512, 256], stddev=5e-2))
b_fc3 = tf.Variable(tf.constant(0.1, shape=[256]))
h_fc3 = tf.nn.relu(tf.matmul(h_fc2, W_fc3) + b_fc3)
W_output = tf.Variable(tf.truncated_normal(shape=[256, 2], stddev=5e-2))
b_output = tf.Variable(tf.constant(0.1, shape=[2]))
logits = tf.matmul(h_fc3, W_output) + b_output
return logits
x = tf.placeholder(tf.float32, shape=[None, 320*240*3])
y = tf.placeholder(tf.float32, shape=[None, 2])
y_pred = build_CNN_classifier(x)
y_soft = tf.nn.softmax(y_pred)
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y_pred, labels = y))
train_step = tf.train.AdamOptimizer(1e-6).minimize(loss)
tf.summary.scalar('loss', loss)
saver_dir = "pre_model"
saver = tf.train.Saver()
ckpt_path = os.path.join(saver_dir, "model")
ckpt = tf.train.get_checkpoint_state(saver_dir)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
merged = tf.summary.merge_all()
tensorboard_writer = tf.summary.FileWriter('./tensorboard_log', sess.graph)
num1 = 0
ckpt_question = input("파라미터를 불러오시겠습니까?(Y/N) ")
if ckpt_question in "y" or ckpt_question in "Y":
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
else:
print("저장된 가중치 파라미터가 없습니다.")
for i in range(1):
if ckpt_question in "y" or ckpt_question in "Y":
break
batch_X, batch_Y = sess.run(next_data)
#print(batch_X, batch_Y)
batch_Y = batch_Y[0,:]
#print(batch_Y)
if i % 10 == 0:
loss_ = sess.run(loss, feed_dict={x: batch_X['image'], y: batch_Y})
print("반복(Epoch): %d, loss : %f" % (i, loss_))
_, y_pred1 = sess.run([train_step, y_pred], feed_dict={x: batch_X['image'], y: batch_Y})
summary = sess.run(merged, feed_dict = {x :batch_X['image'], y: batch_Y})
tensorboard_writer.add_summary(summary, i)
if i % 1000 == 0:
saver.save(sess, ckpt_path, global_step=i)
count = 0
num = 0
count = 0
for j in range(10):
batch_X, batch_Y = sess.run(next_data2)
#print(batch_X, batch_Y)
batch_Y = batch_Y[0, :]
#print(batch_Y)
correct_pred = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, "float"))
count += accuracy.eval(feed_dict={x:batch_X['image'], y:batch_Y})
print(count)
num += 1
_, y_soft1, y_ans = sess.run([train_step, y_soft, y], feed_dict={x: batch_X['image'], y: batch_Y})
print(y_soft1, y_ans)
acc = count / num
print("정확도 : %f" %acc)