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)


[[[178 185 191]
  [182 189 195]
  [149 156 162]
  ..., 
  [216 220 231]
  [220 224 233]
  [220 224 233]]

 [[177 184 190]
  [182 189 195]
  [153 160 166]
  ..., 
  [215 219 230]
  [220 224 233]
  [220 224 233]]

 [[177 184 190]
  [182 189 195]
  [161 168 174]
  ..., 
  [214 218 229]
  [219 223 232]
  [219 223 232]]

 ..., 
 [[ 11  14  23]
  [ 11  14  23]
  [ 11  14  23]
  ..., 
  [  4   7  16]
  [  5   8  17]
  [  5   8  17]]

 [[  9  12  21]
  [  9  12  21]
  [  9  12  21]
  ..., 
  [  8  11  18]
  [  5   8  17]
  [  5   8  17]]

 [[  9  12  21]
  [  9  12  21]
  [  9  12  21]
  ..., 
  [  8  11  18]
  [  5   8  17]
  [  5   8  17]]]

In [108]:
plt.imshow(img)


Out[108]:
<matplotlib.image.AxesImage at 0x11467ca58>

In [109]:
img.shape


Out[109]:
(218, 178, 3)

In [110]:
plt.imshow(img[:,:,0], cmap='gray')


Out[110]:
<matplotlib.image.AxesImage at 0x114712e80>

In [111]:
imgs = utils.get_celeb_imgs()

In [112]:
imgs[0]


Out[112]:
array([[[253, 231, 194],
        [253, 231, 194],
        [253, 231, 194],
        ..., 
        [247, 226, 225],
        [254, 238, 222],
        [254, 238, 222]],

       [[253, 231, 194],
        [253, 231, 194],
        [253, 231, 194],
        ..., 
        [249, 228, 225],
        [254, 238, 222],
        [254, 238, 222]],

       [[253, 231, 194],
        [253, 231, 194],
        [253, 231, 194],
        ..., 
        [250, 231, 227],
        [255, 239, 223],
        [255, 239, 223]],

       ..., 
       [[140,  74,  26],
        [116,  48,   1],
        [146,  78,  33],
        ..., 
        [122,  55,  28],
        [122,  56,  30],
        [122,  56,  30]],

       [[130,  62,  15],
        [138,  70,  23],
        [166,  98,  53],
        ..., 
        [118,  49,  20],
        [118,  51,  24],
        [118,  51,  24]],

       [[168, 100,  53],
        [204, 136,  89],
        [245, 177, 132],
        ..., 
        [118,  49,  20],
        [120,  50,  24],
        [120,  50,  24]]], dtype=uint8)

In [113]:
imgs[0].shape


Out[113]:
(218, 178, 3)

In [114]:
type(imgs)


Out[114]:
list

In [115]:
data = np.array(imgs)

In [116]:
data.shape


Out[116]:
(100, 218, 178, 3)

In [117]:
mean_img = np.mean(data, axis=0)
plt.imshow(mean_img.astype(np.uint8))


Out[117]:
<matplotlib.image.AxesImage at 0x115362240>

In [122]:
mean_img = np.mean(data, axis=(0,1))
plt.plot(mean_img.astype(np.uint8))


Out[122]:
[<matplotlib.lines.Line2D at 0x115649710>,
 <matplotlib.lines.Line2D at 0x1156498d0>,
 <matplotlib.lines.Line2D at 0x115649ac8>]

In [123]:
mean_img = np.mean(data, axis=(0,2))
plt.plot(mean_img.astype(np.uint8))


Out[123]:
[<matplotlib.lines.Line2D at 0x1156ec0b8>,
 <matplotlib.lines.Line2D at 0x1156ec278>,
 <matplotlib.lines.Line2D at 0x1156ec470>]

In [124]:
std_img = np.std(data, axis =0)
plt.imshow(std_img.astype(np.uint8))


Out[124]:
<matplotlib.image.AxesImage at 0x1157e7f98>

In [125]:
plt.imshow(np.mean(std_img, axis=2).astype(np.uint8))


Out[125]:
<matplotlib.image.AxesImage at 0x115886080>

In [19]:
flattened = data.ravel()
print(data[:1])
print(flattened[:10])


[[[[253 231 194]
   [253 231 194]
   [253 231 194]
   ..., 
   [247 226 225]
   [254 238 222]
   [254 238 222]]

  [[253 231 194]
   [253 231 194]
   [253 231 194]
   ..., 
   [249 228 225]
   [254 238 222]
   [254 238 222]]

  [[253 231 194]
   [253 231 194]
   [253 231 194]
   ..., 
   [250 231 227]
   [255 239 223]
   [255 239 223]]

  ..., 
  [[140  74  26]
   [116  48   1]
   [146  78  33]
   ..., 
   [122  55  28]
   [122  56  30]
   [122  56  30]]

  [[130  62  15]
   [138  70  23]
   [166  98  53]
   ..., 
   [118  49  20]
   [118  51  24]
   [118  51  24]]

  [[168 100  53]
   [204 136  89]
   [245 177 132]
   ..., 
   [118  49  20]
   [120  50  24]
   [120  50  24]]]]
[253 231 194 253 231 194 253 231 194 253]

In [20]:
flattened.ravel().shape


Out[20]:
(11641200,)

In [21]:
flattened.shape


Out[21]:
(11641200,)

In [22]:
plt.hist(flattened, 255)


Out[22]:
(array([ 216804.,  117821.,   99125.,   71049.,   66478.,   62066.,
          62528.,   58085.,   56686.,   56114.,   56848.,   58551.,
          63168.,   61086.,   59193.,   59305.,   62526.,   63596.,
          62285.,   65061.,   63389.,   61989.,   61411.,   60742.,
          60464.,   60307.,   59074.,   59312.,   60353.,   64807.,
          67305.,   61667.,   59906.,   60546.,   62017.,   62268.,
          63030.,   62799.,   62396.,   62609.,   59541.,   58918.,
          57822.,   61451.,   63481.,   57782.,   57212.,   56516.,
          56039.,   58385.,   56252.,   55089.,   54677.,   52066.,
          51012.,   48826.,   47602.,   46064.,   45351.,   43735.,
          42849.,   42903.,   42571.,   41789.,   42412.,   42705.,
          42982.,   43107.,   43372.,   43416.,   43323.,   42808.,
          42253.,   42302.,   42008.,   41846.,   42800.,   42476.,
          42435.,   42405.,   42584.,   42612.,   42671.,   42344.,
          42610.,   42023.,   42214.,   41700.,   41302.,   41160.,
          41885.,   42650.,   41703.,   42498.,   41983.,   42702.,
          42735.,   43540.,   42428.,   42376.,   42394.,   41547.,
          42188.,   41566.,   40389.,   41123.,   40529.,   41291.,
          42112.,   41069.,   41266.,   41475.,   41254.,   41075.,
          41171.,   41432.,   41208.,   41324.,   41922.,   41582.,
          42013.,   42234.,   42460.,   42677.,   42786.,   41815.,
          41975.,   42119.,   41887.,   41585.,   41084.,   40260.,
          40819.,   40261.,   40525.,   40688.,   40924.,   41340.,
          42065.,   41707.,   42271.,   41754.,   42675.,   42349.,
          43355.,   44380.,   44117.,   43332.,   42148.,   41166.,
          41503.,   41165.,   40511.,   40550.,   40868.,   40656.,
          40710.,   40901.,   41201.,   41412.,   41278.,   41196.,
          42040.,   40484.,   41872.,   40003.,   39540.,   39412.,
          39419.,   39000.,   39120.,   41259.,   40142.,   39750.,
          39523.,   39662.,   40226.,   40471.,   40539.,   41334.,
          40587.,   39869.,   41072.,   42369.,   38908.,   36865.,
          36743.,   37041.,   37979.,   37332.,   36199.,   37241.,
          38136.,   38718.,   40154.,   40233.,   40927.,   42931.,
          42440.,   44739.,   43546.,   47023.,   45519.,   43844.,
          39307.,   38330.,   38893.,   39873.,   38623.,   39838.,
          40250.,   40881.,   39237.,   39683.,   38854.,   38480.,
          40170.,   40491.,   41457.,   39982.,   38166.,   37335.,
          36543.,   37362.,   36063.,   37213.,   38517.,   39497.,
          39859.,   37243.,   38053.,   36426.,   32394.,   37110.,
          32811.,   31258.,   27632.,   35700.,   30117.,   29352.,
          28333.,   29947.,   27756.,   21400.,   20086.,   20511.,
          23938.,   22960.,   24523.,   23963.,   28714.,   30620.,
          30354.,   41291.,  149885.]),
 array([   0.,    1.,    2.,    3.,    4.,    5.,    6.,    7.,    8.,
           9.,   10.,   11.,   12.,   13.,   14.,   15.,   16.,   17.,
          18.,   19.,   20.,   21.,   22.,   23.,   24.,   25.,   26.,
          27.,   28.,   29.,   30.,   31.,   32.,   33.,   34.,   35.,
          36.,   37.,   38.,   39.,   40.,   41.,   42.,   43.,   44.,
          45.,   46.,   47.,   48.,   49.,   50.,   51.,   52.,   53.,
          54.,   55.,   56.,   57.,   58.,   59.,   60.,   61.,   62.,
          63.,   64.,   65.,   66.,   67.,   68.,   69.,   70.,   71.,
          72.,   73.,   74.,   75.,   76.,   77.,   78.,   79.,   80.,
          81.,   82.,   83.,   84.,   85.,   86.,   87.,   88.,   89.,
          90.,   91.,   92.,   93.,   94.,   95.,   96.,   97.,   98.,
          99.,  100.,  101.,  102.,  103.,  104.,  105.,  106.,  107.,
         108.,  109.,  110.,  111.,  112.,  113.,  114.,  115.,  116.,
         117.,  118.,  119.,  120.,  121.,  122.,  123.,  124.,  125.,
         126.,  127.,  128.,  129.,  130.,  131.,  132.,  133.,  134.,
         135.,  136.,  137.,  138.,  139.,  140.,  141.,  142.,  143.,
         144.,  145.,  146.,  147.,  148.,  149.,  150.,  151.,  152.,
         153.,  154.,  155.,  156.,  157.,  158.,  159.,  160.,  161.,
         162.,  163.,  164.,  165.,  166.,  167.,  168.,  169.,  170.,
         171.,  172.,  173.,  174.,  175.,  176.,  177.,  178.,  179.,
         180.,  181.,  182.,  183.,  184.,  185.,  186.,  187.,  188.,
         189.,  190.,  191.,  192.,  193.,  194.,  195.,  196.,  197.,
         198.,  199.,  200.,  201.,  202.,  203.,  204.,  205.,  206.,
         207.,  208.,  209.,  210.,  211.,  212.,  213.,  214.,  215.,
         216.,  217.,  218.,  219.,  220.,  221.,  222.,  223.,  224.,
         225.,  226.,  227.,  228.,  229.,  230.,  231.,  232.,  233.,
         234.,  235.,  236.,  237.,  238.,  239.,  240.,  241.,  242.,
         243.,  244.,  245.,  246.,  247.,  248.,  249.,  250.,  251.,
         252.,  253.,  254.,  255.]),
 <a list of 255 Patch objects>)

In [23]:
plt.hist(mean_img.ravel(),255)


Out[23]:
(array([    2.,     0.,     0.,     0.,     2.,    18.,    30.,    23.,
           42.,    42.,    43.,    41.,    51.,    48.,    30.,    52.,
           58.,    59.,    67.,    67.,    71.,    83.,    94.,   110.,
          110.,   107.,   134.,   103.,   109.,   160.,   198.,   206.,
          211.,   265.,   306.,   289.,   344.,   335.,   361.,   382.,
          402.,   390.,   400.,   497.,   544.,   594.,   663.,   679.,
          749.,   803.,   847.,   882.,   886.,   938.,   817.,   857.,
          801.,   825.,   831.,   896.,   875.,   861.,   843.,   870.,
          842.,   897.,   863.,   861.,   906.,   939.,   905.,   935.,
          946.,   972.,  1004.,  1064.,  1079.,  1124.,  1162.,  1103.,
         1129.,  1108.,  1085.,  1079.,  1051.,  1155.,  1068.,  1093.,
         1170.,  1171.,  1146.,  1213.,  1146.,  1174.,  1211.,  1229.,
         1173.,  1196.,  1216.,  1150.,  1255.,  1373.,  1344.,  1445.,
         1554.,  1554.,  1553.,  1478.,  1506.,  1469.,  1546.,  1626.,
         1659.,  1568.,  1548.,  1559.,  1576.,  1376.,  1283.,  1189.,
         1151.,  1093.,  1046.,  1068.,  1021.,  1059.,   985.,   927.,
          801.,   696.,   566.,   508.,   448.,   399.,   352.,   371.,
          343.,   345.,   321.,   285.,   293.,   283.,   261.,   244.,
          218.,   221.,   263.,   210.,   208.,   202.,   191.,   173.,
          209.,   182.,   208.,   199.,   196.,   185.,   197.,   172.,
          188.,   147.,   157.,   174.,   147.,   168.,   138.,   140.,
          130.,   102.,   104.,    99.,   117.,   103.,   113.,   112.,
           86.,    93.,    84.,    77.,    95.,    88.,    89.,    72.,
           64.,    71.,    63.,    58.,    56.,    52.,    60.,    57.,
           55.,    59.,    57.,    54.,    66.,    60.,    65.,    72.,
           67.,    75.,    83.,    91.,    84.,    83.,    89.,    78.,
           83.,    85.,   102.,    82.,    77.,    89.,   111.,   101.,
           89.,   100.,   111.,   109.,    97.,    83.,    80.,    72.,
           85.,   108.,   108.,    91.,    80.,    83.,    82.,    86.,
           96.,    98.,    77.,    73.,    74.,    64.,    63.,    56.,
           49.,    62.,    50.,    52.,    64.,    64.,    64.,    77.,
           50.,    33.,    26.,    30.,    10.,     8.,     7.]),
 array([  43.25      ,   43.95035294,   44.65070588,   45.35105882,
          46.05141176,   46.75176471,   47.45211765,   48.15247059,
          48.85282353,   49.55317647,   50.25352941,   50.95388235,
          51.65423529,   52.35458824,   53.05494118,   53.75529412,
          54.45564706,   55.156     ,   55.85635294,   56.55670588,
          57.25705882,   57.95741176,   58.65776471,   59.35811765,
          60.05847059,   60.75882353,   61.45917647,   62.15952941,
          62.85988235,   63.56023529,   64.26058824,   64.96094118,
          65.66129412,   66.36164706,   67.062     ,   67.76235294,
          68.46270588,   69.16305882,   69.86341176,   70.56376471,
          71.26411765,   71.96447059,   72.66482353,   73.36517647,
          74.06552941,   74.76588235,   75.46623529,   76.16658824,
          76.86694118,   77.56729412,   78.26764706,   78.968     ,
          79.66835294,   80.36870588,   81.06905882,   81.76941176,
          82.46976471,   83.17011765,   83.87047059,   84.57082353,
          85.27117647,   85.97152941,   86.67188235,   87.37223529,
          88.07258824,   88.77294118,   89.47329412,   90.17364706,
          90.874     ,   91.57435294,   92.27470588,   92.97505882,
          93.67541176,   94.37576471,   95.07611765,   95.77647059,
          96.47682353,   97.17717647,   97.87752941,   98.57788235,
          99.27823529,   99.97858824,  100.67894118,  101.37929412,
         102.07964706,  102.78      ,  103.48035294,  104.18070588,
         104.88105882,  105.58141176,  106.28176471,  106.98211765,
         107.68247059,  108.38282353,  109.08317647,  109.78352941,
         110.48388235,  111.18423529,  111.88458824,  112.58494118,
         113.28529412,  113.98564706,  114.686     ,  115.38635294,
         116.08670588,  116.78705882,  117.48741176,  118.18776471,
         118.88811765,  119.58847059,  120.28882353,  120.98917647,
         121.68952941,  122.38988235,  123.09023529,  123.79058824,
         124.49094118,  125.19129412,  125.89164706,  126.592     ,
         127.29235294,  127.99270588,  128.69305882,  129.39341176,
         130.09376471,  130.79411765,  131.49447059,  132.19482353,
         132.89517647,  133.59552941,  134.29588235,  134.99623529,
         135.69658824,  136.39694118,  137.09729412,  137.79764706,
         138.498     ,  139.19835294,  139.89870588,  140.59905882,
         141.29941176,  141.99976471,  142.70011765,  143.40047059,
         144.10082353,  144.80117647,  145.50152941,  146.20188235,
         146.90223529,  147.60258824,  148.30294118,  149.00329412,
         149.70364706,  150.404     ,  151.10435294,  151.80470588,
         152.50505882,  153.20541176,  153.90576471,  154.60611765,
         155.30647059,  156.00682353,  156.70717647,  157.40752941,
         158.10788235,  158.80823529,  159.50858824,  160.20894118,
         160.90929412,  161.60964706,  162.31      ,  163.01035294,
         163.71070588,  164.41105882,  165.11141176,  165.81176471,
         166.51211765,  167.21247059,  167.91282353,  168.61317647,
         169.31352941,  170.01388235,  170.71423529,  171.41458824,
         172.11494118,  172.81529412,  173.51564706,  174.216     ,
         174.91635294,  175.61670588,  176.31705882,  177.01741176,
         177.71776471,  178.41811765,  179.11847059,  179.81882353,
         180.51917647,  181.21952941,  181.91988235,  182.62023529,
         183.32058824,  184.02094118,  184.72129412,  185.42164706,
         186.122     ,  186.82235294,  187.52270588,  188.22305882,
         188.92341176,  189.62376471,  190.32411765,  191.02447059,
         191.72482353,  192.42517647,  193.12552941,  193.82588235,
         194.52623529,  195.22658824,  195.92694118,  196.62729412,
         197.32764706,  198.028     ,  198.72835294,  199.42870588,
         200.12905882,  200.82941176,  201.52976471,  202.23011765,
         202.93047059,  203.63082353,  204.33117647,  205.03152941,
         205.73188235,  206.43223529,  207.13258824,  207.83294118,
         208.53329412,  209.23364706,  209.934     ,  210.63435294,
         211.33470588,  212.03505882,  212.73541176,  213.43576471,
         214.13611765,  214.83647059,  215.53682353,  216.23717647,
         216.93752941,  217.63788235,  218.33823529,  219.03858824,
         219.73894118,  220.43929412,  221.13964706,  221.84      ]),
 <a list of 255 Patch objects>)

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]:
<matplotlib.text.Text at 0x110dd4208>

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]:
<matplotlib.text.Text at 0x111197ef0>

In [28]:
plt.imshow((data[6] - mean_img)/std_img)


Out[28]:
<matplotlib.image.AxesImage at 0x1114a40f0>

Tensorflow


In [29]:
import tensorflow as tf

In [30]:
x = np.linspace(-3., 3, 100)
print(x)
print(x.shape)
print(x.dtype)


[-3.         -2.93939394 -2.87878788 -2.81818182 -2.75757576 -2.6969697
 -2.63636364 -2.57575758 -2.51515152 -2.45454545 -2.39393939 -2.33333333
 -2.27272727 -2.21212121 -2.15151515 -2.09090909 -2.03030303 -1.96969697
 -1.90909091 -1.84848485 -1.78787879 -1.72727273 -1.66666667 -1.60606061
 -1.54545455 -1.48484848 -1.42424242 -1.36363636 -1.3030303  -1.24242424
 -1.18181818 -1.12121212 -1.06060606 -1.         -0.93939394 -0.87878788
 -0.81818182 -0.75757576 -0.6969697  -0.63636364 -0.57575758 -0.51515152
 -0.45454545 -0.39393939 -0.33333333 -0.27272727 -0.21212121 -0.15151515
 -0.09090909 -0.03030303  0.03030303  0.09090909  0.15151515  0.21212121
  0.27272727  0.33333333  0.39393939  0.45454545  0.51515152  0.57575758
  0.63636364  0.6969697   0.75757576  0.81818182  0.87878788  0.93939394
  1.          1.06060606  1.12121212  1.18181818  1.24242424  1.3030303
  1.36363636  1.42424242  1.48484848  1.54545455  1.60606061  1.66666667
  1.72727273  1.78787879  1.84848485  1.90909091  1.96969697  2.03030303
  2.09090909  2.15151515  2.21212121  2.27272727  2.33333333  2.39393939
  2.45454545  2.51515152  2.57575758  2.63636364  2.6969697   2.75757576
  2.81818182  2.87878788  2.93939394  3.        ]
(100,)
float64

In [31]:
x = tf.linspace(-3., 3 , 100)
print(x)


Tensor("LinSpace:0", shape=(100,), dtype=float32)

In [32]:
g = tf.get_default_graph()

In [33]:
[ op.name for op in g.get_operations()]


Out[33]:
['LinSpace/start', 'LinSpace/stop', 'LinSpace/num', 'LinSpace']

In [34]:
g.get_tensor_by_name('LinSpace'+':0')


Out[34]:
<tf.Tensor 'LinSpace:0' shape=(100,) dtype=float32>

In [35]:
sess= tf.Session()

computed_x = sess.run(x)
print(computed_x)

computed_x = x.eval(session=sess)
print(computed_x)
sess.close()


[-3.         -2.939394   -2.87878799 -2.81818175 -2.75757575 -2.69696975
 -2.63636351 -2.5757575  -2.5151515  -2.4545455  -2.3939395  -2.33333325
 -2.27272725 -2.21212125 -2.15151501 -2.090909   -2.030303   -1.969697
 -1.90909088 -1.84848475 -1.78787875 -1.72727275 -1.66666663 -1.6060605
 -1.5454545  -1.4848485  -1.42424238 -1.36363626 -1.30303025 -1.24242425
 -1.18181813 -1.12121201 -1.060606   -1.         -0.939394   -0.87878776
 -0.81818175 -0.75757575 -0.69696951 -0.63636351 -0.5757575  -0.5151515
 -0.4545455  -0.39393926 -0.33333325 -0.27272725 -0.21212101 -0.15151501
 -0.090909   -0.030303    0.030303    0.09090924  0.15151525  0.21212125
  0.27272749  0.33333349  0.3939395   0.4545455   0.5151515   0.57575774
  0.63636374  0.69696975  0.75757599  0.81818199  0.87878799  0.939394    1.
  1.060606    1.12121201  1.18181849  1.24242449  1.30303049  1.36363649
  1.4242425   1.4848485   1.5454545   1.60606098  1.66666698  1.72727299
  1.78787899  1.84848499  1.909091    1.969697    2.030303    2.090909
  2.15151548  2.21212149  2.27272749  2.33333349  2.3939395   2.4545455
  2.5151515   2.57575798  2.63636398  2.69696999  2.75757599  2.81818199
  2.87878799  2.939394    3.        ]
[-3.         -2.939394   -2.87878799 -2.81818175 -2.75757575 -2.69696975
 -2.63636351 -2.5757575  -2.5151515  -2.4545455  -2.3939395  -2.33333325
 -2.27272725 -2.21212125 -2.15151501 -2.090909   -2.030303   -1.969697
 -1.90909088 -1.84848475 -1.78787875 -1.72727275 -1.66666663 -1.6060605
 -1.5454545  -1.4848485  -1.42424238 -1.36363626 -1.30303025 -1.24242425
 -1.18181813 -1.12121201 -1.060606   -1.         -0.939394   -0.87878776
 -0.81818175 -0.75757575 -0.69696951 -0.63636351 -0.5757575  -0.5151515
 -0.4545455  -0.39393926 -0.33333325 -0.27272725 -0.21212101 -0.15151501
 -0.090909   -0.030303    0.030303    0.09090924  0.15151525  0.21212125
  0.27272749  0.33333349  0.3939395   0.4545455   0.5151515   0.57575774
  0.63636374  0.69696975  0.75757599  0.81818199  0.87878799  0.939394    1.
  1.060606    1.12121201  1.18181849  1.24242449  1.30303049  1.36363649
  1.4242425   1.4848485   1.5454545   1.60606098  1.66666698  1.72727299
  1.78787899  1.84848499  1.909091    1.969697    2.030303    2.090909
  2.15151548  2.21212149  2.27272749  2.33333349  2.3939395   2.4545455
  2.5151515   2.57575798  2.63636398  2.69696999  2.75757599  2.81818199
  2.87878799  2.939394    3.        ]

In [36]:
sess = tf.Session(graph=g)
sess.close()

In [37]:
g2 = tf.Graph()

In [38]:
sess = tf.InteractiveSession()
x.eval()


Out[38]:
array([-3.        , -2.939394  , -2.87878799, -2.81818175, -2.75757575,
       -2.69696975, -2.63636351, -2.5757575 , -2.5151515 , -2.4545455 ,
       -2.3939395 , -2.33333325, -2.27272725, -2.21212125, -2.15151501,
       -2.090909  , -2.030303  , -1.969697  , -1.90909088, -1.84848475,
       -1.78787875, -1.72727275, -1.66666663, -1.6060605 , -1.5454545 ,
       -1.4848485 , -1.42424238, -1.36363626, -1.30303025, -1.24242425,
       -1.18181813, -1.12121201, -1.060606  , -1.        , -0.939394  ,
       -0.87878776, -0.81818175, -0.75757575, -0.69696951, -0.63636351,
       -0.5757575 , -0.5151515 , -0.4545455 , -0.39393926, -0.33333325,
       -0.27272725, -0.21212101, -0.15151501, -0.090909  , -0.030303  ,
        0.030303  ,  0.09090924,  0.15151525,  0.21212125,  0.27272749,
        0.33333349,  0.3939395 ,  0.4545455 ,  0.5151515 ,  0.57575774,
        0.63636374,  0.69696975,  0.75757599,  0.81818199,  0.87878799,
        0.939394  ,  1.        ,  1.060606  ,  1.12121201,  1.18181849,
        1.24242449,  1.30303049,  1.36363649,  1.4242425 ,  1.4848485 ,
        1.5454545 ,  1.60606098,  1.66666698,  1.72727299,  1.78787899,
        1.84848499,  1.909091  ,  1.969697  ,  2.030303  ,  2.090909  ,
        2.15151548,  2.21212149,  2.27272749,  2.33333349,  2.3939395 ,
        2.4545455 ,  2.5151515 ,  2.57575798,  2.63636398,  2.69696999,
        2.75757599,  2.81818199,  2.87878799,  2.939394  ,  3.        ], dtype=float32)

In [39]:
print(x.get_shape())
print(type(x.get_shape()))
print(x.get_shape().as_list())
print(type(x.get_shape().as_list()))


(100,)
<class 'tensorflow.python.framework.tensor_shape.TensorShape'>
[100]
<class '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]:
[<matplotlib.lines.Line2D at 0x1117bda20>]

In [42]:
y_eval


Out[42]:
array([ 0.00443191,  0.00530586,  0.00632887,  0.00752144,  0.00890595,
        0.01050665,  0.01234962,  0.01446263,  0.01687508,  0.01961775,
        0.02272256,  0.02622228,  0.03015006,  0.03453908,  0.03942196,
        0.04483017,  0.05079339,  0.05733884,  0.06449047,  0.07226815,
        0.08068691,  0.08975609,  0.09947861,  0.10985006,  0.12085805,
        0.13248162,  0.14469069,  0.15744552,  0.17069657,  0.18438436,
        0.19843954,  0.21278308,  0.22732687,  0.24197429,  0.25662115,
        0.27115688,  0.28546539,  0.29942709,  0.31292021,  0.32582232,
        0.33801258,  0.34937328,  0.35979187,  0.36916268,  0.37738881,
        0.38438377,  0.39007297,  0.39439505,  0.39730301,  0.39876503,
        0.39876503,  0.39730301,  0.39439505,  0.39007294,  0.38438374,
        0.37738878,  0.36916265,  0.35979187,  0.34937328,  0.33801255,
        0.32582229,  0.31292015,  0.29942703,  0.28546533,  0.27115682,
        0.25662115,  0.24197429,  0.22732687,  0.21278308,  0.19843945,
        0.18438432,  0.17069651,  0.15744548,  0.14469066,  0.13248162,
        0.12085805,  0.10984997,  0.09947855,  0.08975606,  0.08068687,
        0.07226812,  0.06449046,  0.05733884,  0.05079339,  0.04483017,
        0.03942192,  0.03453906,  0.03015004,  0.02622227,  0.02272256,
        0.01961775,  0.01687508,  0.01446261,  0.0123496 ,  0.01050664,
        0.00890594,  0.00752143,  0.00632887,  0.00530586,  0.00443191], dtype=float32)

In [43]:
ksize = z.get_shape().as_list()[0]
print(ksize)


100

In [44]:
z


Out[44]:
<tf.Tensor 'mul_2:0' shape=(100,) dtype=float32>

In [45]:
z.get_shape().as_list()


Out[45]:
[100]

In [46]:
z_2d = tf.matmul(tf.reshape(z,[ksize,1]), tf.reshape(z,[1,ksize]))
print(z_2d.eval())


[[  1.96418587e-05   2.35151329e-05   2.80489930e-05 ...,   2.80489930e-05
    2.35151329e-05   1.96418587e-05]
 [  2.35151329e-05   2.81521970e-05   3.35801124e-05 ...,   3.35801124e-05
    2.81521970e-05   2.35151329e-05]
 [  2.80489930e-05   3.35801124e-05   4.00545614e-05 ...,   4.00545614e-05
    3.35801124e-05   2.80489930e-05]
 ..., 
 [  2.80489930e-05   3.35801124e-05   4.00545614e-05 ...,   4.00545614e-05
    3.35801124e-05   2.80489930e-05]
 [  2.35151329e-05   2.81521970e-05   3.35801124e-05 ...,   3.35801124e-05
    2.81521970e-05   2.35151329e-05]
 [  1.96418587e-05   2.35151329e-05   2.80489930e-05 ...,   2.80489930e-05
    2.35151329e-05   1.96418587e-05]]

In [47]:
plt.imshow(z_2d.eval())


Out[47]:
<matplotlib.image.AxesImage at 0x1118789e8>

convolve gaussian


In [48]:
from skimage import data
img = data.camera().astype(np.float32)
plt.imshow(img,cmap='gray')
print(img.shape)


(512, 512)

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())


(1, 512, 512, 1)

In [52]:
z_4d = tf.reshape(z_2d, [ksize, ksize, 1, 1])
print(z_4d.get_shape().as_list())


[100, 100, 1, 1]

In [53]:
z_4d_eval = z_4d.eval()
plt.imshow(z_4d_eval[:,:,0,0],cmap='gray')


Out[53]:
<matplotlib.image.AxesImage at 0x119d92fd0>

In [54]:
convolved = tf.nn.conv2d(img_4d, z_4d, strides=[1,1,1,1],padding='SAME' )
res = convolved.eval()
print(res.shape)


(1, 512, 512, 1)

In [55]:
plt.imshow(np.squeeze(res),cmap='gray')


Out[55]:
<matplotlib.image.AxesImage at 0x10e5fa128>

In [56]:
plt.imshow(res[0,:,:,0], cmap='gray')


Out[56]:
<matplotlib.image.AxesImage at 0x1137fce10>

In [57]:
res[0,:,:,0]


Out[57]:
array([[ 11683.55761719,  12224.83398438,  12762.63574219, ...,
         11864.92089844,  11339.77441406,  10813.08984375],
       [ 12220.69726562,  12786.89355469,  13349.45800781, ...,
         12410.26269531,  11860.96484375,  11310.0625    ],
       [ 12753.83789062,  13344.77441406,  13931.921875  , ...,
         12951.61621094,  12378.34179688,  11803.39550781],
       ..., 
       [  9620.06152344,  10067.05371094,  10510.93554688, ...,
          9359.68945312,   8949.125     ,   8537.16894531],
       [  9199.49511719,   9627.00390625,  10051.53515625, ...,
          8949.17578125,   8556.5703125 ,   8162.65332031],
       [  8778.00976562,   9185.97949219,   9591.10351562, ...,
          8537.52929688,   8162.94384766,   7787.12402344]], dtype=float32)

In [58]:
res[0,:,:,0].shape


Out[58]:
(512, 512)

In [59]:
res


Out[59]:
array([[[[ 11683.55761719],
         [ 12224.83398438],
         [ 12762.63574219],
         ..., 
         [ 11864.92089844],
         [ 11339.77441406],
         [ 10813.08984375]],

        [[ 12220.69726562],
         [ 12786.89355469],
         [ 13349.45800781],
         ..., 
         [ 12410.26269531],
         [ 11860.96484375],
         [ 11310.0625    ]],

        [[ 12753.83789062],
         [ 13344.77441406],
         [ 13931.921875  ],
         ..., 
         [ 12951.61621094],
         [ 12378.34179688],
         [ 11803.39550781]],

        ..., 
        [[  9620.06152344],
         [ 10067.05371094],
         [ 10510.93554688],
         ..., 
         [  9359.68945312],
         [  8949.125     ],
         [  8537.16894531]],

        [[  9199.49511719],
         [  9627.00390625],
         [ 10051.53515625],
         ..., 
         [  8949.17578125],
         [  8556.5703125 ],
         [  8162.65332031]],

        [[  8778.00976562],
         [  9185.97949219],
         [  9591.10351562],
         ..., 
         [  8537.52929688],
         [  8162.94384766],
         [  7787.12402344]]]], dtype=float32)

In [60]:
xs = tf.linspace(-3. ,3., ksize)

In [61]:
ys = tf.sin(xs)
plt.figure()
plt.plot(xs.eval(), ys.eval())


Out[61]:
[<matplotlib.lines.Line2D at 0x1139e4630>]

In [62]:
ys = tf.reshape(ys, [ksize, 1])

In [80]:
ys.eval().shape


Out[80]:
(100, 1)

In [65]:
ones = tf.ones((1,ksize))
wave =tf.matmul(ys, ones)
plt.imshow(wave.eval(), cmap='gray')


Out[65]:
<matplotlib.image.AxesImage at 0x113abd198>

In [71]:
plt.imshow(z_2d.eval(), cmap='gray')


Out[71]:
<matplotlib.image.AxesImage at 0x113cc33c8>

In [72]:
gabor = tf.mul(wave, z_2d)
plt.imshow(gabor.eval(), cmap='gray')


Out[72]:
<matplotlib.image.AxesImage at 0x113da5e48>

In [70]:



Out[70]:
<matplotlib.image.AxesImage at 0x113c34f60>

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)


(?, ?, 1)

In [76]:
img_4d = tf.expand_dims(img_3d, 0)
print(img_4d.get_shape().as_list())


[1, None, None, 1]

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]:
<matplotlib.image.AxesImage at 0x11421d8d0>

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]:
<matplotlib.image.AxesImage at 0x11454f4e0>

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]:
array([[ 10.,  12.],
       [ 14.,  16.]], dtype=float32)

In [ ]:


In [ ]: