Commit 39c85178 authored by Oleg Dzhimiev's avatar Oleg Dzhimiev
Browse files

colored displaying of weights

parent bb3cc9de
Loading
Loading
Loading
Loading
+29 −25
Original line number Diff line number Diff line
@@ -250,10 +250,10 @@ files_train_hvar = ["/home/oleg/GIT/python3-imagej-tiff/data_sets/tf_data_rand2/
                    "/home/oleg/GIT/python3-imagej-tiff/data_sets/tf_data_rand2/train007_R1_GT_1.5.tfrecords",
]

files_train_lvar = ["/home/oleg/GIT/python3-imagej-tiff/data_sets/tf_data_rand2/train000_R1_LE_1.5.tfrecords",
                    ]
files_train_hvar = ["/home/oleg/GIT/python3-imagej-tiff/data_sets/tf_data_rand2/train000_R1_GT_1.5.tfrecords",
]
#files_train_lvar = ["/home/oleg/GIT/python3-imagej-tiff/data_sets/tf_data_rand2/train000_R1_LE_1.5.tfrecords",
#                    ]
#files_train_hvar = ["/home/oleg/GIT/python3-imagej-tiff/data_sets/tf_data_rand2/train000_R1_GT_1.5.tfrecords",
#]
#files_train_hvar = []
#file_test_lvar=     "/home/eyesis/x3d_data/data_sets/tf_data_3x3a/train000_R1_LE_1.5.tfrecords" # "/home/eyesis/x3d_data/data_sets/train-000_R1_LE_1.5.tfrecords"
file_test_lvar=     "/home/oleg/GIT/python3-imagej-tiff/data_sets/tf_data_rand2/testTEST_R1_LE_1.5.tfrecords"
@@ -855,18 +855,18 @@ with tf.Session() as sess:

    merged = tf.summary.merge_all()
    
    vis_placeholder = tf.placeholder(tf.float32, [1,32,325,3])
    some_image2 = tf.summary.image('custom_test', vis_placeholder)
    # display weights, part 1 begin
    import numpy_image_test as nit
    
    l1 = NN_LAYOUT1.index(next(filter(lambda x: x!=0, NN_LAYOUT1)))
    l2 = NN_LAYOUT2.index(next(filter(lambda x: x!=0, NN_LAYOUT2)))
    with tf.variable_scope('g_fc_sub'+str(l1),reuse=tf.AUTO_REUSE):
        w = tf.get_variable('weights',shape=[325,32])
        wd = w[...,tf.newaxis]
        wds = tf.stack([wd]*3,axis=0)
        #print(wd.shape)
        #some_image = tf.summary.image("tfsi_test",wds.eval(),max_outputs=1)
        some_image = tf.summary.image("tfsi_test",wds,max_outputs=1)
    
    wimg1_placeholder = tf.placeholder(tf.float32, [1,160,80,3])
    wimg1 = tf.summary.image('weights/sub_'+str(l1), wimg1_placeholder)
    
    wimg2_placeholder = tf.placeholder(tf.float32, [1,120,60,3])
    wimg2 = tf.summary.image('weights/inter_'+str(l2), wimg2_placeholder)
    # display weights, part 1 end
        
    train_writer =    tf.summary.FileWriter(TRAIN_PATH, sess.graph)
    test_writer  =    tf.summary.FileWriter(TEST_PATH, sess.graph)
@@ -970,23 +970,27 @@ with tf.Session() as sess:
#        _,_=sess.run([tf_ph_G_loss,tf_ph_sq_diff],feed_dict={tf_ph_G_loss:test_avg, tf_ph_sq_diff:test2_avg})
            
        #train_writer.add_summary(some_image.eval(), epoch)
        
        # display weights, part 2 begin
        l1 = NN_LAYOUT1.index(next(filter(lambda x: x!=0, NN_LAYOUT1)))
        l2 = NN_LAYOUT2.index(next(filter(lambda x: x!=0, NN_LAYOUT2)))
        with tf.variable_scope('g_fc_sub'+str(l1),reuse=tf.AUTO_REUSE):
            w = tf.get_variable('weights',shape=[325,32])
            wd = w[tf.newaxis,...]
            wds = tf.stack([wd]*3,axis=-1)
        
            timg_min = tf.reduce_min(w).eval()
            timg_max = tf.reduce_max(w).eval()
        with tf.variable_scope('g_fc_sub'+str(l1),reuse=tf.AUTO_REUSE):
            w = tf.get_variable('weights',shape=[325,NN_LAYOUT1[l1]])
            w = tf.transpose(w,(1,0))            
            img1 = nit.tiles(nit.coldmap(w.eval(),zero_span=0.0002),(1,4,9,9),tiles_per_line=2,borders=True)
            img1 = img1[np.newaxis,...]
            
            timg = wds.eval()
        train_writer.add_summary(wimg1.eval(feed_dict={wimg1_placeholder: img1}), epoch)

            timg[:,:,:,0] = timg_min
            timg[:,:,:,1] = timg_min
            timg = np.transpose(timg,(0,2,1,3))
        with tf.variable_scope('g_fc_inter'+str(l2),reuse=tf.AUTO_REUSE):
            w = tf.get_variable('weights',shape=[144,NN_LAYOUT1[l2]])
            w = tf.transpose(w,(1,0))            
            img2 = nit.tiles(nit.coldmap(w.eval(),zero_span=0.0002),(3,3,4,4),tiles_per_line=4,borders=True)
            img2 = img2[np.newaxis,...]
            
        train_writer.add_summary(some_image2.eval(feed_dict={vis_placeholder: timg}), epoch)
        train_writer.add_summary(wimg2.eval(feed_dict={wimg2_placeholder: img2}), epoch)
        # display weights, part 2 end

        train_writer.add_summary(train_summary, epoch)
        test_writer.add_summary(test_summaries[0], epoch)

numpy_image_test.py

0 → 100644
+192 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt
import math

def hamming_window(x,N):
  y = 0.2 - 0.46*math.cos(2*math.pi*x/(N-1))
  return y

# input: np.array(a,b) - 1 channel
# output: np.array(a,b,3) - 3 color channels
def coldmap(img,zero_span=0.2):

  out = np.dstack(3*[img])

  img_min = np.nanmin(img)
  img_max = np.nanmax(img)

  #print("min: "+str(img_min)+", max: "+str(img_max))

  ch_r = out[...,0]
  ch_g = out[...,1]
  ch_b = out[...,2]

  # blue for <0
  ch_r[img<0] = 0
  ch_g[img<0] = 0
  ch_b[img<0] = -ch_b[img<0]

  # red for >0
  ch_r[img>0] = ch_b[img>0]
  ch_g[img>0] = 0
  ch_b[img>0] = 0

  # green for 0
  ch_r[img==0] = 0
  ch_g[img==0] = img_max
  ch_b[img==0] = 0

  # green for zero vicinity
  ch_r[abs(img)<zero_span/2] = 0
  ch_g[abs(img)<zero_span/2] = img_max/2
  ch_b[abs(img)<zero_span/2] = 0

  return out

# has to be pre transposed
# it just suppose to match
def tiles(img,shape,tiles_per_line=1,borders=True):

  # shape is (n0,n1,n2,n3)
  # n0*n1*n2*n3 = img.shape[1]
  img_min = np.nanmin(img)
  img_max = np.nanmax(img)

  outer_color = [img_max,img_max,img_min]
  outer_color = [img_max,img_max,img_max]

  inner_color = [img_max/4,img_max/4,img_min]
  inner_color = [img_min,img_min,img_min]
  inner_color = [img_max,img_max,img_min]

  group_h = shape[0]
  group_w = shape[1]
  group_size = group_h*group_w

  tile_h = shape[2]
  tile_w = shape[3]
  tile_size = tile_h*tile_w

  tpl = tiles_per_line

  # main

  tmp1 = []

  for i in range(img.shape[0]):

    if i%tpl==0:
      tmp2 = []

    tmp3 = []

    for igh in range(group_h):

      tmp4 = []

      for igw in range(group_w):

        si = (group_w*igh + igw + 0)*tile_size
        ei = (group_w*igh + igw + 1)*tile_size

        tile = img[i,si:ei]
        tile = np.reshape(tile,(tile_h,tile_w,tile.shape[1]))

        if borders:

          if igw==group_w-1:

            b_h_inner = [[inner_color]*(tile_w+0)]*(       1)
            b_h_outer = [[outer_color]*(tile_w+0)]*(       1)
            b_v_outer = [[outer_color]*(       1)]*(tile_h+1)

            # outer hor
            if igh==group_h-1:
              tile = np.concatenate([tile,b_h_outer],axis=0)
            # inner hor
            else:
              tile = np.concatenate([tile,b_h_inner],axis=0)
            # outer vert
            tile = np.concatenate([tile,b_v_outer],axis=1)

          else:

            b_v_inner = [[inner_color]*(       1)]*(tile_h+0)
            b_h_inner = [[inner_color]*(tile_w+1)]*(       1)
            b_h_outer = [[outer_color]*(tile_w+1)]*(       1)

            # inner vert
            tile = np.concatenate([tile,b_v_inner],axis=1)

            # outer hor
            if igh==group_h-1:
              tile = np.concatenate([tile,b_h_outer],axis=0)
            # inner hor
            else:
              tile = np.concatenate([tile,b_h_inner],axis=0)

        tmp4.append(tile)

      tmp3.append(np.concatenate(tmp4,axis=1))

    tmp2.append(np.concatenate(tmp3,axis=0))

    if i%tpl==(tpl-1):
      tmp1.append(np.concatenate(tmp2,axis=1))

  out = np.concatenate(tmp1,axis=0)
  #out = img
  return out

if __name__=="__main__":
  #
  hw = hamming_window
  #
  image = np.array([[1*hw(i,512)*hw(j,512) for i in range(512)] for j in range(512)],np.float32)
  zeros = np.zeros((512,512))

  # 32x324

  #image2 = np.zeros((32,324))
  #rgb_img_0 = tiles(image2,(1,4,9,9),tiles_per_line=2,borders=True)

  #image2 = np.zeros((32,144))
  image2 = np.array([[1*hw(i,144)*hw(j,32) for i in range(144)] for j in range(32)],np.float32)
  #image3 = coldmap(image2)
  rgb_img_0 = tiles(coldmap(image2),(3,3,4,4),tiles_per_line=8,borders=True)

  fig = plt.figure()
  fig.suptitle("HaWi")
  plt.imshow(rgb_img_0)

  rgb_img = coldmap(image)

  #print(rgb_img)

  '''
  for i in range(512):
    for j in range(512):
      if image[i,j]<0:
        rgb_img[i,j,0] = 0
        rgb_img[i,j,1] = 0
        #rgb_img[i,j,2] = 255

      if image[i,j]>0:
        #rgb_img[i,j,0] = 255
        rgb_img[i,j,1] = 0
        rgb_img[i,j,2] = 0

      if image[i,j]==0:
        rgb_img[i,j,0] = 0
        rgb_img[i,j,1] = 255
        rgb_img[i,j,2] = 0
  '''
  print(rgb_img.shape)

  fig = plt.figure()
  fig.suptitle("HamWindow")
  plt.imshow(rgb_img)
  #plt.colorbar()
  plt.show()
 No newline at end of file