In [1]:
import tensorflow as tf
import numpy as np
from scipy.misc import imread, imresize
import matplotlib.pyplot as plt
import time
import os
%matplotlib inline
In [2]:
current_dir = os.getcwd()
data_dir = os.listdir("./train/")
In [3]:
def image_road(data_dir):
img_matrix = []
label = []
index = 0
for data_label in data_dir:
category_list = os.listdir(os.getcwd()+"/train/"+data_label)
for data in category_list:
img = imread(current_dir+"/train/"+data_label+"/"+data)
resize_img = imresize(img, [256, 256, 3])
img_matrix.append(resize_img)
label.append(index)
index += 1
return img_matrix, label
In [4]:
img_matrix, label = image_road(data_dir)
In [5]:
y_data = np.eye(len(data_dir))[label]
In [6]:
plt.imshow(img_matrix[0])
plt.title("Original Image")
print("Image size :", np.shape(img_matrix))
In [7]:
img = np.array(img_matrix)/255
print("change the range of image : \n",img[0,0:3,0:3,0])
print(np.shape(img))
In [8]:
# RGB channel
for i in range(len(img)):
row_img = img[i]
red = row_img[:,:,0]
green = row_img[:,:,1]
blue = row_img[:,:,2]
red_reshape = np.reshape(red, -1)
green_reshape = np.reshape(green, -1)
blue_reshape = np.reshape(blue, -1)
for index, value in enumerate(zip(red_reshape,green_reshape,blue_reshape)):
if value[1] < 0.20:
img[i,index//256, index%256, :] = 0
elif value[0] > 0.30:
img[i,index//256, index%256, :] = 0
elif value[2] > 0.20:
img[i,index//256, index%256, :] = 0
In [9]:
plt.imshow(img[2])
plt.title("Convert remove obstacle feature")
Out[9]:
In [10]:
randidx = np.random.randint(len(img),size=len(img))
shuffle_x = img[randidx,:]
shuffle_y = y_data[randidx,:]
In [11]:
split_value = int(len(img)*0.8)
train_x = shuffle_x[:split_value]
train_y = shuffle_y[:split_value]
test_x = shuffle_x[split_value:]
test_y = shuffle_y[split_value:]
In [12]:
img_width = np.shape(img)[1]
img_height = np.shape(img)[1]
channel = 3
batch_size = 64
learning_rate = 0.01
epoch = 7
In [13]:
X = tf.placeholder(tf.float32, [None, img_width, img_width, channel])
y = tf.placeholder(tf.float32, [None, 2])
X_img = tf.reshape(X,[-1, img_width*img_height*channel])
In [14]:
w_1 = tf.get_variable("weight1",shape=[img_width*img_height*channel, 256], initializer=tf.random_normal_initializer())
b_1 = tf.get_variable("bias1", shape=[256,] ,initializer=tf.zeros_initializer())
layer_1 = tf.nn.bias_add(tf.matmul(X_img,w_1),b_1)
layer_1 = tf.nn.relu(layer_1)
w_2 = tf.get_variable("weight2",shape=[256, 512], initializer=tf.random_normal_initializer())
b_2 = tf.get_variable("bias2", shape=[512,] ,initializer=tf.zeros_initializer())
layer_2 = tf.nn.bias_add(tf.matmul(layer_1,w_2),b_2)
layer_2 = tf.nn.relu(layer_2)
w_3 = tf.get_variable("weight3",shape=[512, 2], initializer=tf.random_normal_initializer())
b_3 = tf.get_variable("bias3", shape=[2,] ,initializer=tf.zeros_initializer())
layer_3 = tf.nn.bias_add(tf.matmul(layer_2,w_3),b_3)
In [15]:
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=layer_3, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)
correct_prediction = tf.equal(tf.argmax(layer_3, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
In [17]:
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(epoch):
avg_cost = 0
total_batch = int(len(train_x)/batch_size)
for step in range(total_batch):
randidx = np.random.randint(len(train_x),size=batch_size)
batch_xs = train_x[randidx,:]
batch_ys = train_y[randidx,:]
feed_dict = {X: batch_xs, y: batch_ys}
c, _ = sess.run([loss, optimizer], feed_dict=feed_dict)
avg_cost += c / total_batch
if epoch%1 == 0:
print("Cost :",avg_cost)
print("Test Accuracy :", sess.run(accuracy, feed_dict={X:test_x, y:test_y}))
print('Finished')