In [105]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('ggplot')
In [106]:
from libs import utils
files = utils.get_celeb_files()
In [107]:
img = plt.imread(files[50])
print(img)
In [108]:
plt.imshow(img)
Out[108]:
In [109]:
img.shape
Out[109]:
In [110]:
plt.imshow(img[:,:,0], cmap='gray')
Out[110]:
In [111]:
imgs = utils.get_celeb_imgs()
In [112]:
imgs[0]
Out[112]:
In [113]:
imgs[0].shape
Out[113]:
In [114]:
type(imgs)
Out[114]:
In [115]:
data = np.array(imgs)
In [116]:
data.shape
Out[116]:
In [117]:
mean_img = np.mean(data, axis=0)
plt.imshow(mean_img.astype(np.uint8))
Out[117]:
In [122]:
mean_img = np.mean(data, axis=(0,1))
plt.plot(mean_img.astype(np.uint8))
Out[122]:
In [123]:
mean_img = np.mean(data, axis=(0,2))
plt.plot(mean_img.astype(np.uint8))
Out[123]:
In [124]:
std_img = np.std(data, axis =0)
plt.imshow(std_img.astype(np.uint8))
Out[124]:
In [125]:
plt.imshow(np.mean(std_img, axis=2).astype(np.uint8))
Out[125]:
In [19]:
flattened = data.ravel()
print(data[:1])
print(flattened[:10])
In [20]:
flattened.ravel().shape
Out[20]:
In [21]:
flattened.shape
Out[21]:
In [22]:
plt.hist(flattened, 255)
Out[22]:
In [23]:
plt.hist(mean_img.ravel(),255)
Out[23]:
In [26]:
bins =20
fig, axs = plt.subplots(1,3, figsize=(12,6), sharey=True, sharex=True)
axs[0].hist(data[0].ravel(),bins)
axs[0].set_title('img distribution')
axs[1].hist(mean_img.ravel(),bins)
axs[1].set_title('mean distribution')
axs[2].hist((data[0]-mean_img).ravel(), bins)
axs[2].set_title('(img-mean) distribution')
Out[26]:
In [27]:
fig, axs = plt.subplots(1,3, figsize=(12,6), sharey=True, sharex=True)
axs[0].hist((data[0] - mean_img).ravel(), bins)
axs[0].set_title('(img-mean) distribution')
axs[1].hist((std_img).ravel(),bins)
axs[1].set_title('std deviation distribution')
axs[2].hist(((data[0] - mean_img)/std_img).ravel(), bins)
axs[2].set_title(" ((img-mean)/std_dev) distribution")
Out[27]:
In [28]:
plt.imshow((data[6] - mean_img)/std_img)
Out[28]:
In [29]:
import tensorflow as tf
In [30]:
x = np.linspace(-3., 3, 100)
print(x)
print(x.shape)
print(x.dtype)
In [31]:
x = tf.linspace(-3., 3 , 100)
print(x)
In [32]:
g = tf.get_default_graph()
In [33]:
[ op.name for op in g.get_operations()]
Out[33]:
In [34]:
g.get_tensor_by_name('LinSpace'+':0')
Out[34]:
In [35]:
sess= tf.Session()
computed_x = sess.run(x)
print(computed_x)
computed_x = x.eval(session=sess)
print(computed_x)
sess.close()
In [36]:
sess = tf.Session(graph=g)
sess.close()
In [37]:
g2 = tf.Graph()
In [38]:
sess = tf.InteractiveSession()
x.eval()
Out[38]:
In [39]:
print(x.get_shape())
print(type(x.get_shape()))
print(x.get_shape().as_list())
print(type(x.get_shape().as_list()))
In [40]:
mean = 0.
sigma = 1.
z = (tf.exp(tf.neg(tf.pow(x-mean,2.)/
(2.0*tf.pow(sigma,2.0)))) *
(1.0/(sigma*tf.sqrt(2.*3.1415))))
In [41]:
x_eval = x.eval()
y_eval = z.eval()
plt.plot(x_eval,y_eval)
Out[41]:
In [42]:
y_eval
Out[42]:
In [43]:
ksize = z.get_shape().as_list()[0]
print(ksize)
In [44]:
z
Out[44]:
In [45]:
z.get_shape().as_list()
Out[45]:
In [46]:
z_2d = tf.matmul(tf.reshape(z,[ksize,1]), tf.reshape(z,[1,ksize]))
print(z_2d.eval())
In [47]:
plt.imshow(z_2d.eval())
Out[47]:
In [48]:
from skimage import data
img = data.camera().astype(np.float32)
plt.imshow(img,cmap='gray')
print(img.shape)
In [49]:
img_4d = img.reshape(-1,img.shape[0],img.shape[1],1)
In [50]:
img_4d= tf.reshape(img, [-1, img.shape[0], img.shape[1],1])
In [51]:
print(img_4d.get_shape())
In [52]:
z_4d = tf.reshape(z_2d, [ksize, ksize, 1, 1])
print(z_4d.get_shape().as_list())
In [53]:
z_4d_eval = z_4d.eval()
plt.imshow(z_4d_eval[:,:,0,0],cmap='gray')
Out[53]:
In [54]:
convolved = tf.nn.conv2d(img_4d, z_4d, strides=[1,1,1,1],padding='SAME' )
res = convolved.eval()
print(res.shape)
In [55]:
plt.imshow(np.squeeze(res),cmap='gray')
Out[55]:
In [56]:
plt.imshow(res[0,:,:,0], cmap='gray')
Out[56]:
In [57]:
res[0,:,:,0]
Out[57]:
In [58]:
res[0,:,:,0].shape
Out[58]:
In [59]:
res
Out[59]:
In [60]:
xs = tf.linspace(-3. ,3., ksize)
In [61]:
ys = tf.sin(xs)
plt.figure()
plt.plot(xs.eval(), ys.eval())
Out[61]:
In [62]:
ys = tf.reshape(ys, [ksize, 1])
In [80]:
ys.eval().shape
Out[80]:
In [65]:
ones = tf.ones((1,ksize))
wave =tf.matmul(ys, ones)
plt.imshow(wave.eval(), cmap='gray')
Out[65]:
In [71]:
plt.imshow(z_2d.eval(), cmap='gray')
Out[71]:
In [72]:
gabor = tf.mul(wave, z_2d)
plt.imshow(gabor.eval(), cmap='gray')
Out[72]:
In [70]:
Out[70]:
In [75]:
img = tf.placeholder(tf.float32, shape=[None, None], name='img')
img_3d = tf.expand_dims(img, 2)
dims = img_3d.get_shape()
print(dims)
In [76]:
img_4d = tf.expand_dims(img_3d, 0)
print(img_4d.get_shape().as_list())
In [91]:
mean = tf.placeholder(tf.float32, name='mean')
sigma = tf.placeholder(tf.float32, name = 'sigma')
ksize = tf.placeholder(tf.int32, name = 'ksize')
x = tf.linspace(-3., 3., ksize)
z = (tf.exp(tf.neg(tf.pow(x - mean, 2.) /
(2. * tf.pow(sigma, 2.)))) *
(1. / (sigma * tf.sqrt(2. * 3.1415))))
z_2d = tf.matmul(
tf.reshape(z, tf.pack([ksize, 1])),
tf.reshape(z, tf.pack([1,ksize])))
ys = tf.sin(x)
ys = tf.reshape(ys, tf.pack([ksize, 1]))
ones = tf.ones(tf.pack([1, ksize]))
wave = tf.matmul(ys, ones)
gabor = tf.mul(wave, z_2d)
gabor_4d = tf.reshape(gabor, tf.pack([ksize, ksize, 1 ,1]))
convolved = tf.nn.conv2d(img_4d, gabor_4d, strides=[1,1,1,1],padding='SAME', name='convolved')
convolved_img = convolved[0,:,:,0]
In [93]:
res = convolved_img.eval(feed_dict={img:data.camera(), mean:0.0, sigma:1.0, ksize:100})
plt.imshow(res, cmap='gray')
Out[93]:
In [94]:
res = convolved_img.eval(feed_dict={
img: data.camera(),
mean : 0.,
sigma : 0.5,
ksize :32
})
plt.imshow(res, cmap='gray')
Out[94]:
In [160]:
a = tf.placeholder(tf.float32, name='a')
b = tf.placeholder(tf.float32, name='b')
c = a*b
#c = tf.divide(a,b)
In [165]:
a_data = 2.0
b_data = np.array([[5,6],
[7,8]])
In [166]:
c.eval(feed_dict={a:a_data ,b:b_data})
Out[166]:
In [ ]:
In [ ]: