## 분리수거 쓰레기와 음식물 를 버리러 가자 ㅠ 내일 어떻게 일어나나 ㅠ
##
# Tensorflow tutorial: https://www.tensorflow.org/versions/r0.12/tutorials/mnist/pros/index.html
#
import tensorflow as tf
import numpy as np
from sklearn.manifold import TSNE
from mpl_toolkits.mplot3d import Axes3D
import os
import matplotlib as mpl
if os.environ.get('DISPLAY','') == '':
print('no display found. Using non-interactive Agg backend')
mpl.use('Agg')
import matplotlib.pyplot as plt
#data setting
# Get MNIST data set from tensorflow
from tensorflow.examples.tutorials.mnist import input_data
mnist_set = input_data.read_data_sets('images/MNIST/', one_hot = True)
train_img = mnist_set.train.images
train_label = mnist_set.train.labels
test_img = mnist_set.test.images
test_label = mnist_set.test.labels
# Options for training
dim="2d"
stddev = 0.1
biasInit = 0.1
isDropout = False
training_epochs = 100
batch_size = 2000
point_N = 1000
display_step = 1
sum_level = "full" #full, pool
prefix = "sum_only" #total, sum_only
# Options for loading a pretrained model
load_dir = "models/"
load_keyword = "cnn_tanh_mnist_20000.epoch-"
load_epoch = "20000"
test_rand_a = np.random.randint(test_img.shape[0], size=test_img.shape[0])
test_rand_b = np.random.randint(test_img.shape[0], size=test_img.shape[0])
test_selt_a = []
test_selt_b = []
test_selt_c = []
for j in range(test_img.shape[0]):
a = np.where(test_label[test_rand_a[j]] == 1.)[0][0]
b = np.where(test_label[test_rand_b[j]] == 1.)[0][0]
if a + b >= 10:
j = j - 1
continue
test_rand_c = np.random.randint(test_img.shape[0], size = test_img.shape[0])
# find correct image
for k in range(test_img.shape[0]):
c = np.where(test_label[test_rand_c[k]] == 1.)[0][0]
if c == a + b:
test_selt_a.append(test_rand_a[j])
test_selt_b.append(test_rand_b[j])
test_selt_c.append(test_rand_c[k])
break
if len(test_selt_a) == test_img.shape[0]:
&nb\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0sp;break
test_batch_a = test_img[test_selt_a, :]
test_batch_b = test_img[test_selt_b, :]
test_batch_c = test_img[test_selt_c, :]
test_batch_a_label = test_label[test_selt_a, :]
test_batch_b_label = test_label[test_selt_b, :]
test_batch_c_label = test_label[test_selt_c, :]
train_img_num = []
for i in range(10):
train_img_num.append([])
for i in range(train_img.shape[0]):
train_img_num[np.where(train_label[i] == 1.)[0][0]].append(i)
print "Initialized"
##
# Build Graph
# The concept of graph is a sort of function and Placeholders are arguments for that function
#input & output
x = tf.placeholder(tf.float32, shape=[None, train_img.shape[1]])
x_image = tf.reshape(x, [-1,28,28,1])
#y is one-hot .. but it must be float32
y_ = tf.placeholder(tf.float32, shape=[None, train_label.shape[1]])
#Variables
# Initialization
# return variable in the specified shape with stddev 0.1
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=stddev) # Initialize w/ stddev 0.1 normal distribution
return tf.Variable(initial, name='weight')
# return constant bias in the specified shape
def bias_variable(shape):
initial = tf.constant(biasInit, shape=shape) # Initilaize w/ 0.1
return tf.Variable(initial, name='biases')
# 2d cnn layer with stride 1
# padding SAME is valid ??(valid, full.. and .. what?? I can't remember ;;;)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
# Max pooling
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
###
# 2 Convolution layers
###
###
# First Convolution Layer : Input to CNN(1 to 32)
# In order to build a deep network, we stack several layers of this type.
# The first layer will have 32 features for each 5x5 patch.
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
# Applying Max pooling Convolution
# We convolve x_image w/ the weight tensor, add the bias, apply the tanh function,and finally maxpool
h_conv1 = tf.nn.tan\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\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0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0h(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
##########################
##
# Second Convolutional Layer : CNN to CNN (32 to 64)
# In order to build a deep network, we stack several layers of this type.
# The second layer will have 64 features for each 5x5 patch.
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
# Applying Max pooling Convolution
# We convolve x_image w/ the weight tensor, add the bias, apply the tanh function,and finally maxpool
h_conv2 = tf.nn.tanh(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
##########################
###
# 2 Fully Connected layers
###
##
# Densely Connected Layer : Fully connected layer w/ 1024 neurons (CNN to FNN (7 x 7 x 64 to 1024 ) )
# Now that the image size has been reduced to 7x7
W_fc1 = weight_variable([7 * 7 * 64, 1024])
# we add a fully-connected layer with 1024 neurons to allow processing on the entire image.
b_fc1 = bias_variable([1024])
# We reshape the tensor from the pooling layer into a batch of vectors,
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
# multiply by a weight matrix, add a bias, and a tanh
h_fc1 = tf.nn.tanh(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
##########################
##
# Dropout: To reduce overfitting, we will apply dropout before the readout layer.
# We create a placeholder for the probability that a neuron's output is kept during dropout.
# This allows us to turn dropout on during training, and turn it off during testing.
keep_prob = tf.placeholder(tf.float32)
# TensorFlow's tf.nn.dropout op automatically handles scaling neuron outputs in addition
# to masking them, so dropout just works without any additional scaling.
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# For this small convolutional network, performance is actually nearly identical with and
# without dropout. Dropout is often very effective at reducing overfitting, but it is most
# useful when training very large neural networks.
##
# Readout Layer : Fully Connected to Output Layer
# Finally, we add a layer, just like for the one layer softmax regression.
# The last layer shouldn't be activation function??
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
if isDropout :
&nb\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0sp; y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
else:
y_conv = tf.matmul(h_fc1, W_fc2) + b_fc2
################################
# Finally, we can do something with the layer built up
# To train and evaluate it we will use code that is nearly identical to that for the simple
# one layer SoftMax network above.
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_))
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# The differences are that:
# 1) We will replace the steepest gradient descent optimizer with the more sophisticated ADAM opti.
# 2) We will include the additional parameter keep_prob in feed_dict to control the dropout rate.
# 3) We will add logging to every 100th iteration in the training process.
# ADAM optimizer rather than the steepest gradient descent optimizer
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
################################
# Load Model #
################################
# Load saver
saver = tf.train.Saver()
x0 = tf.placeholder(tf.float32, shape=[None, train_img.shape[1]])
x0_image = tf.reshape(x0, [-1,28,28,1])
x1 = tf.placeholder(tf.float32, shape=[None, train_img.shape[1]])
x1_image = tf.reshape(x1, [-1,28,28,1])
x2 = tf.placeholder(tf.float32, shape=[None, train_img.shape[1]])
x2_image = tf.reshape(x2, [-1,28,28,1])
y0_ = tf.placeholder(tf.float32, shape=[None, train_label.shape[1]])
y1_ = tf.placeholder(tf.float32, shape=[None, train_label.shape[1]])
y2_ = tf.placeholder(tf.float32, shape=[None, train_label.shape[1]])
ARW_fc1 = tf.identity(W_fc1)
ARb_fc1 = tf.identity(b_fc1)
ARW_fc2 = tf.identity(W_fc2)
ARb_fc2 = tf.identity(b_fc2)
#ARW_fc3 = weight_variable([1024, 2])
#ARb_fc2 = bias_variable([2])
ARW_conv1 = tf.identity(W_conv1)
ARW_conv2 = tf.identity(W_conv2)
ARb_conv1 = tf.identity(b_conv1)
ARb_conv2 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0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0h_pool12 = max_pool_2x2(h_conv12)
h_conv20 = tf.nn.tanh(conv2d(h_pool10, ARW_conv2) + ARb_conv2)
h_conv21 = tf.nn.tanh(conv2d(h_pool11, ARW_conv2) + ARb_conv2)
h_conv22 = tf.nn.tanh(conv2d(h_pool12, ARW_conv2) + ARb_conv2)
h_pool20 = max_pool_2x2(h_conv20)
h_pool21 = max_pool_2x2(h_conv21)
h_pool22 = max_pool_2x2(h_conv22)
h_pool20_flat = tf.reshape(h_pool20, [-1, 7 * 7 * 64])
h_pool21_flat = tf.reshape(h_pool21, [-1, 7 * 7 * 64])
h_pool22_flat = tf.reshape(h_pool22, [-1, 7 * 7 * 64])
h_fc10 = tf.nn.tanh(tf.matmul(h_pool20_flat, ARW_fc1) + ARb_fc1)
h_fc11 = tf.nn.tanh(tf.matmul(h_pool21_flat, ARW_fc1) + ARb_fc1)
h_fc12 = tf.nn.tanh(tf.matmul(h_pool22_flat, ARW_fc1) + ARb_fc1)
h_fc10_drop = tf.nn.dropout(h_fc10, keep_prob)
h_fc11_drop = tf.nn.dropout(h_fc11, keep_prob)
h_fc12_drop = tf.nn.dropout(h_fc12, keep_prob)
if isDropout :
y_conv0 = tf.matmul(h_fc10_drop, ARW_fc2) + ARb_fc2
y_conv1 = tf.matmul(h_fc11_drop, ARW_fc2) + ARb_fc2
y_conv2 = tf.matmul(h_fc12_drop, ARW_fc2) + ARb_fc2
else:
y_conv0 = tf.matmul(h_fc10, ARW_fc2) + ARb_fc2
y_conv1 = tf.matmul(h_fc11, ARW_fc2) + ARb_fc2
y_conv2 = tf.matmul(h_fc12, ARW_fc2) + ARb_fc2
ce_vec0 = tf.nn.softmax_cross_entropy_with_logits(y_conv0, y0_)
ce_vec1 = tf.nn.softmax_cross_entropy_with_logits(y_conv1, y1_)
ce_vec2 = tf.nn.softmax_cross_entropy_with_logits(y_conv2, y2_)
cross_entropy0 = tf.reduce_mean(ce_vec0)
cross_entropy1 = tf.reduce_mean(ce_vec1)
cross_entropy2 = tf.reduce_mean(ce_vec2)
if sum_level == "pool":
sum_loss_vec = tf.add(h_pool21_flat, h_pool22_flat) - h_pool20_flat
#sum_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(tf.add(h_pool21_flat, h_pool22_flat), h_pool20_flat))
elif sum_level == "full":
sum_loss_vec = tf.add(h_fc11, h_fc12) - h_fc10
#sum_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(tf.add(h_fc11, h_fc12), h_fc10))
sum_loss_vec_sq = tf.square(sum_loss_vec)
sum_loss = tf.reduce_mean(sum_loss_vec_sq)
if prefix == "sum_only" :
total_optimizer = tf.train.AdamOptimizer(1e-3).minimize(sum_loss)
else :
#total_loss = 4/(1/cross_entropy0 + 1/cross_entropy1 + 1/cross_entropy2 + 1/sum_loss)
total_loss = cross_entropy0 + cross_entropy1 + cross_entropy2 + sum_loss
total_optimizer = tf.train.AdamOptimizer(1e-3).minimize(total_loss)
#for checking classifiation task
correct_prediction0 = tf.equal(tf.argmax(y_conv0,1), tf.argmax(y0_,1))
correct_prediction1 = tf.equal(tf.argmax(y_conv1,1), tf.argmax(y1_,1))
correct_prediction2 = tf.equal(tf.argmax(y_conv2,1), tf.argmax(y2_,1))
accuracy0 = tf.reduce_mean(tf.cast(correct_prediction0, tf.float32))
accuracy1 = tf.red\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0uce_mean(tf.cast(correct_prediction1, tf.float32))
accuracy2 = tf.reduce_mean(tf.cast(correct_prediction2, tf.float32))
# Restore graph session
sess = tf.Session()
init = tf.initialize_all_variables()
sess.run(init)
ckpt = tf.train.get_checkpoint_state(load_dir)
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, load_dir + load_keyword + load_epoch)
################################
# Train and Evaluate the Model #
################################
# Print out : I hope the final test set accruacy after runnign this code will be 99.2%
if dim == "2d":
# model = TSNE(n_components=2, random_state=0)
model = TSNE(perplexity=50, n_components=2, init='pca', n_iter=5000)
elif dim == "3d":
model = TSNE(perplexity=50, n_components=3, init='pca', n_iter=5000)
np.set_printoptions(suppress=True)
##
# Training
for epoch in range(training_epochs):
# Checking the classification task & vector summation
test_acc = sess.run(accuracy0, feed_dict={x0: mnist_set.test.images, y0_: mnist_set.test.labels, keep_prob: 1.0});
test_sum_cost = sess.run(sum_loss, feed_dict={x1: test_batch_a, x2: test_batch_b, x0: test_batch_c, y0_: test_batch_c_label, y1_: test_batch_a_label, y2_: test_batch_b_label})
print ("epoch: %03d/%03d, test image acc %.6f, test sum_loss: %.6f"% (epoch, training_epochs, test_acc, test_sum_cost))
# Checking the linear relation
if sum_level == "pool":
hpf = sess.run(h_pool20_flat, feed_dict={x0:mnist_set.test.images[0:point_N]})
elif sum_level == "full":
hpf = sess.run(h_fc10, feed_dict={x0:mnist_set.test.images[0:point_N]})
#save to image
tsne = model.fit_transform(hpf)
plt.figure(figsize=(36,36))
if dim == "3d":
fig = plt.figure(figsize=(36,36))
ax = fig.add_subplot(111, projection='3d')
for i in range(point_N):
if dim == "2d":
plt.scatter(tsne[i][0], tsne[i][1])
label = np.where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\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0_label[i] == 1.)[0][0]
plt.annotate('%s' % str(label), xy=(tsne[i][0], tsne[i][1]), xytext=(5, 5), textcoords='offset points', ha='right', va='bottom')
elif dim == "3d":
ax.scatter(tsne[i][0], tsne[i][1], tsne[i][2])
avg_cost = 0.
filename = prefix + "_mnist_tanh_" + dim + "_" + sum_level + "_cnn_" + str(epoch) + "_summation_" + str("%.6f"%test_sum_cost) + "_classification_" + str("%.6f"%test_acc) + ".png"
plt.savefig(filename)
plt.close()
#Training
num_batch = int(train_img.shape[0] / batch_size )
for i in range(num_batch):
rand_a = np.random.randint(train_img.shape[0], size=train_img.shape[0])
rand_b = np.random.randint(train_img.shape[0], size=train_img.shape[0])
selt_a = []
selt_b = []
selt_c = []
for j in range(batch_size):
a = np.where(train_label[rand_a[j]] == 1.)[0][0]
b = np.where(train_label[rand_b[j]] == 1.)[0][0]
if a + b >= 10:
j = j - 1
continue
rand_c = np.random.randint(train_img.shape[0], size = train_img.shape[0])
# find correct image
for k in range(train_img.shape[0]):
c = np.where(train_label[rand_c[k]] == 1.)[0][0]
if c == a + b:
selt_a.append(rand_a[j])
selt_b.append(rand_b[j])
selt_c.append(rand_c[k])
break
if len(selt_a) == batch_size:
&\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0nbsp; break
if len(selt_a) != len(selt_b) or len(selt_a) != len(selt_c):
print("Something wrong!!", len(selt_a)," ",len(selt_b)," " ,len(selt_c))
batch_a = train_img[selt_a, :]
batch_b = train_img[selt_b, :]
batch_c = train_img[selt_c, :]
batch_a_label = train_label[selt_a, :]
batch_b_label = train_label[selt_b, :]
batch_c_label = train_label[selt_c, :]
sess.run(total_optimizer, feed_dict={x1: batch_a, x2: batch_b, x0: batch_c, y0_: batch_c_label, y1_: batch_a_label, y2_:batch_b_label})
avg_cost += sess.run(sum_loss, feed_dict={x1: batch_a, x2: batch_b, x0: batch_c, y0_: batch_c_label, y1_: batch_a_label, y2_: batch_b_label})/num_batch
print ("epoch: %03d/%03d, train sum_loss: %.6f"% (epoch, training_epochs, avg_cost))