Commit 7d6b5d4f authored by Pietro Morerio's avatar Pietro Morerio
Browse files

full accuracy

parent 1cf3dca3
%% Cell type:code id: tags:
``` python
%matplotlib inline
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import tensorflow.contrib.slim as slim
import pandas as pd
from dataset import get_mnist
from model import *
from scipy.spatial.distance import cdist
from matplotlib import gridspec
```
%% Output
/usr/local/lib/python2.7/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
%% Cell type:code id: tags:
``` python
mnist = get_mnist()
train_images = np.array([im.reshape((28,28,1)) for im in mnist.train.images])
test_images = np.array([im.reshape((28,28,1)) for im in mnist.test.images])
len_test = len(mnist.test.images)
len_train = len(mnist.train.images)
```
%% Output
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
%% Cell type:code id: tags:
``` python
#helper function to plot image
def show_image(idxs, data):
if type(idxs) != np.ndarray:
idxs = np.array([idxs])
fig = plt.figure()
gs = gridspec.GridSpec(1,len(idxs))
for i in range(len(idxs)):
ax = fig.add_subplot(gs[0,i])
ax.imshow(data[idxs[i],:,:,0],cmap='gray')
ax.axis('off')
plt.show()
```
%% Cell type:markdown id: tags:
## Create the siamese net feature extraction model
%% Cell type:code id: tags:
``` python
img_placeholder = tf.placeholder(tf.float32, [None, 28, 28, 1], name='img')
net = siamese(img_placeholder, reuse=False)
```
%% Cell type:code id: tags:
``` python
offset = len(train_images)
offset = 10000
```
%% Cell type:markdown id: tags:
## Restore from checkpoint and calc the features from all of train data
%% Cell type:code id: tags:
``` python
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
ckpt = tf.train.get_checkpoint_state("model")
saver.restore(sess, "model/model.ckpt")
train_feat = sess.run(net, feed_dict={img_placeholder:train_images[:offset]})
```
%% Output
INFO:tensorflow:Restoring parameters from model/model.ckpt
%% Cell type:markdown id: tags:
## Restore from checkpoint and calc the features from all of corrupted train data
%% Cell type:code id: tags:
``` python
from syn_dataset import add_defect
train_images_noisy = np.empty((offset,28,28,1))
for i, img in enumerate(train_images[:offset]):
train_images_noisy[i]=add_defect(img)
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
ckpt = tf.train.get_checkpoint_state("model")
saver.restore(sess, "model/model.ckpt")
train_feat_noisy = sess.run(net, feed_dict={img_placeholder:train_images_noisy})
```
%% Output
INFO:tensorflow:Restoring parameters from model/model.ckpt
%% Cell type:markdown id: tags:
## Add noisy images and labels to training and test sets
%% Cell type:code id: tags:
``` python
#add noisy images & features to training set
train_feat = np.vstack([train_feat,train_feat_noisy])
train_images = np.vstack([train_images[:offset],train_images_noisy])
assert len(train_images) == len(train_feat)
#add noisy images to test set
test_images_noisy = np.empty((len(test_images),28,28,1))
for i, img in enumerate(test_images):
test_images_noisy[i]=add_defect(img)
test_images = np.vstack([test_images,test_images_noisy])
```
%% Cell type:markdown id: tags:
## Searching for similar test images from trainset based on siamese feature
%% Cell type:code id: tags:
``` python
#generate new random test image
idx = np.random.randint(0, len_test+offset)
# 2050 -> 8
# 20 -> 9
# 8644 -> 6
# idx=20
im = test_images[idx]
#show the test image
show_image(idx, test_images)
print "This is image from id:", idx
```
%% Output
This is image from id: 12286
This is image from id: 10267
%% Cell type:code id: tags:
``` python
#run the test image through the network to get the test features
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
ckpt = tf.train.get_checkpoint_state("model")
saver.restore(sess, "model/model.ckpt")
search_feat = sess.run(net, feed_dict={img_placeholder:[im]})
#calculate the cosine similarity and sort
dist = cdist(train_feat, search_feat, 'cosine')
rank = np.argsort(dist.ravel())
#show the top n similar image from train data
n = 10
show_image(rank[:n], train_images)
print "retrieved ids:", rank[:n]
```
%% Output
INFO:tensorflow:Restoring parameters from model/model.ckpt
retrieved ids: [15933 17369 16575 11803 12360]
retrieved ids: [15058 16803 11192 10850 16441 16817 10262 12622 17817 19964]
%% Cell type:code id: tags:
``` python
```
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment