무얼하지
헷갈림과 등산으로 마무리
아무쪼록 빨리 추스리고 내실을 쌓기를
무얼하지
헷갈림과 등산으로 마무리
아무쪼록 빨리 추스리고 내실을 쌓기를
소스는 아래랑 똑같지만 로그출력 등 미세한 차이가 존재
##
#어제 많이 피곤했나보다;;; 이상한 소스를 올리다니 ㅋㅋ
#지금은 많이 졸린데 오늘도 이상한 걸 올리는건 아닐지 모르겠으나 엔비에스피가 너무 많아서 보기가 힘듦으로 그냥 올리자 ㅎㅎ
# Tensorflow tutorial: https://www.tensorflow.org/versions/r0.12/tutorials/mnist/pros/index.html
#
import tensorflow as tf
import numpy as np
import os
import matplotlib as mpl
import random
from numpy import linalg as LA
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
stddev = 0.1
biasInit = 0.1
isDropout = False
training_epochs = 500
batch_size = 60
display_step = 1
sum_level = "full"
prefix = "subset"
point_N = 100
K = 10
Init_D = 8 #start distance
# 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])
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\0\0\0\0\0\0\0\0
if len(test_selt_a) == test_img.shape[0]:
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\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\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\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0, add the bias, apply the tanh function,and finally maxpool
h_conv1 = tf.nn.tanh(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\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
if isDropout :
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 = tf.identity(b_conv2)
h_conv10 = tf.nn.tanh(conv2d(x0_image, ARW_conv1) + ARb_conv1)
h_conv11 = tf.nn.tanh(conv2d(x1_image, ARW_conv1) + ARb_conv1)
h_conv12 = tf.nn.tanh(conv2d(x2_image, ARW_\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0conv1) + ARb_conv1)
h_pool10 = max_pool_2x2(h_conv10)
h_pool11 = max_pool_2x2(h_conv11)
h_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)
sum_loss_vec = 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)
#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-4).minimize(total_loss)
#sum_optimizer = tf.train.AdamOptimizer(1e-4).minimize(sum_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.reduce_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)
################################
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0# Train and Evaluate the Model #
################################
# Print out : I hope the final test set accruacy after runnign this code will be 99.2%
## select cloud!
from scipy.spatial import distance
selected_train_img_idx = []
for num in range(10):
image_vecs = sess.run(h_fc10, feed_dict={x0:train_img[train_img_num[num], :]})
#print "find " + str(num)
eigen_post = np.random.randint(image_vecs.shape[0])
D = Init_D
while True:
tmp_idx = []
rand_idx = np.random.randint(image_vecs.shape[0], size = image_vecs.shape[0])
for i in range(image_vecs.shape[0]):
dist = distance.euclidean(image_vecs[eigen_post], image_vecs[rand_idx[i]])
if dist <= D:
tmp_idx.append(train_img_num[num][rand_idx[i]])
if len(tmp_idx) == K:
break
if len(tmp_idx) == K:
selected_train_img_idx.append(tmp_idx)
break
D = D + 0.1
print str(len(selected_train_img_idx)) + " clusters"
##
# Training
for epoch in range(training_epochs):
# Print out Test data
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))
test_scat = sess.run(h_fc10, feed_dict={x0:mnist_set.test.images[0:point_N]})
plt.figure(figsize=(10,10))
for i in range(point_N):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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\0sp;plt.annotate('%s' % str(label), xy=(LA.norm(test_scat[i]), 0), xytext=(5, 5), textcoords='offset points', ha='right', va='bottom')
filename = "test_" + prefix + "_mnist_tanh_" + sum_level + "_cnn_" + str(epoch) + "_sum_" + str("%.6f"%test_sum_cost) + "_imgcls_" + str("%.6f"%test_acc) + ".png"
plt.savefig(filename)
plt.close()
# Print Conv1, 2
images1 = sess.run(h_pool10, feed_dict={x0:mnist_set.test.images})
images2 = sess.run(h_pool20, feed_dict={x0:mnist_set.test.images})
fig = plt.figure(figsize=(75,10))
rand_img_idx = np.random.randint(test_img.shape[0], size = 15)
for i in range(15):
ax1 = fig.add_subplot(2, 15, i + 1)
ax1.matshow(images1[rand_img_idx[i], :, :, 0], cmap=plt.cm.gray)
ax2 = fig.add_subplot(2, 15, i + 1 + 15)
ax2.matshow(images2[rand_img_idx[i], :, :, 0], cmap=plt.cm.gray)
plt.savefig("convimg_epoch_" + str(epoch) + ".png")
plt.close()
#Training
num_batch = 100 / batch_size
avg_sum_cost = 0.
avg_img_cost = 0.
for i in range(num_batch):
selt_a = []
selt_b = []
selt_c = []
num_count=[0,0,0,0,0,0,0,0,0,0]
for j in range(batch_size):
while True:
a = np.random.randint(10)
b = np.random.randint(10)
c = a + b
if c < 10:
break
num_count[a] = num_count[a] + 1
num_count[b] = num_count[b] + 1
print str(c) + "=" + str(a) + "+" + str(b)
a1 = selected_train_img_idx[a]
b1 = selected_train_img_idx[b]
c1 = selected_tr\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0ain_img_idx[c]
random.shuffle(a1)
random.shuffle(b1)
random.shuffle(c1)
selt_a.extend(a1)
selt_b.extend(b1)
selt_c.extend(c1)
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, :]
for j in range(10):
print str(j) + ":" + str(num_count[j])
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_img_cost += sess.run(accuracy0, feed_dict={x0: batch_a, y0_: batch_a_label, keep_prob: 1.0}) / num_batch
avg_sum_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 out Training Data
print ("epoch: %03d/%03d, train image acc %.6f, train sum_loss %.6f"% (epoch, training_epochs, avg_img_cost, avg_sum_cost))
plt.figure(figsize=(10,10))
for i in range(len(selected_train_img_idx)):
hpf = sess.run(h_fc10, feed_dict={x0:train_img[selected_train_img_idx[i],:]})
for j in range(len(selected_train_img_idx[i])):
label = np.where(train_label[selected_train_img_idx[i][j]] == 1.)[0][0]
plt.scatter(LA.norm(hpf[j]), 0)
plt.annotate('%s' % str(label), xy=(LA.norm(hpf[j]), 0), xytext=(5, 5), textcoords='offset points', ha='right', va='bottom')
filename = "train_" + prefix + "_mn\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\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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