Loading pack_tile.py +134 −3 Original line number Original line Diff line number Diff line Loading @@ -6,7 +6,8 @@ __email__ = "oleg@elphel.com" import numpy as np import numpy as np # pack from 9x9x4 to 25x1 # pack from 9x9x4 to 100x1 # type 1, all the same def pack_tile_type1(tile): def pack_tile_type1(tile): out = np.empty(100) out = np.empty(100) Loading Loading @@ -125,13 +126,143 @@ def pack_tile_type1(tile): return out return out # pack from 9x9x4 to 104x1 # type 1, all the same def pack_tile_type2(tile): out = np.empty(104) # pack diagm-pair (not tested) l = np.ravel(tile[:,:,0]) out[ 0] = 1.0*l[ 0]+1.0*l[ 1]+1.0*l[ 2]+1.0*l[ 9]+1.0*l[10]+1.0*l[18] out[ 1] = 1.0*l[ 3]+1.0*l[11]+1.0*l[19]+1.0*l[27] out[ 2] = 1.0*l[ 4]+1.0*l[12]+1.0*l[20]+1.0*l[28]+1.0*l[36] out[ 3] = 1.0*l[ 5]+1.0*l[ 6]+1.0*l[14] out[ 4] = 1.0*l[ 7]+1.0*l[15]+1.0*l[23] out[ 5] = 1.0*l[ 8]+1.0*l[16]+1.0*l[24] out[ 6] = 1.0*l[13]+1.0*l[21]+1.0*l[29]+1.0*l[37] out[ 7] = 1.0*l[17]+1.0*l[25]+1.0*l[33] out[ 8] = 1.0*l[22]+1.0*l[30]+1.0*l[38] out[ 9] = 1.0*l[26]+1.0*l[34]+1.0*l[35] out[10] = 1.0*l[31] out[11] = 1.0*l[32] out[12] = 1.0*l[39] out[13] = 1.0*l[40] out[14] = 1.0*l[41] out[15] = 1.0*l[42]+1.0*l[50]+1.0*l[58] out[16] = 1.0*l[43]+1.0*l[51]+1.0*l[59]+1.0*l[67] out[17] = 1.0*l[44]+1.0*l[52]+1.0*l[60]+1.0*l[68]+1.0*l[76] out[18] = 1.0*l[45]+1.0*l[46]+1.0*l[54] out[19] = 1.0*l[47]+1.0*l[55]+1.0*l[63] out[20] = 1.0*l[48] out[21] = 1.0*l[49] out[22] = 1.0*l[53]+1.0*l[61]+1.0*l[69]+1.0*l[77] out[23] = 1.0*l[56]+1.0*l[64]+1.0*l[72] out[24] = 1.0*l[57]+1.0*l[65]+1.0*l[73] out[25] = 1.0*l[62]+1.0*l[70]+1.0*l[71]+1.0*l[78]+1.0*l[79]+1.0*l[80] out[26] = 1.0*l[66]+1.0*l[74]+1.0*l[75] # pack diago-pair (not tested) l = np.ravel(tile[:,:,1]) out[27] = 1.0*l[ 0]+1.0*l[10]+1.0*l[20] out[28] = 1.0*l[ 1]+1.0*l[11]+1.0*l[21] out[29] = 1.0*l[ 2]+1.0*l[ 3]+1.0*l[12] out[30] = 1.0*l[ 4]+1.0*l[14]+1.0*l[24]+1.0*l[34]+1.0*l[44] out[31] = 1.0*l[ 5]+1.0*l[15]+1.0*l[25]+1.0*l[35] out[32] = 1.0*l[ 6]+1.0*l[ 7]+1.0*l[ 8]+1.0*l[16]+1.0*l[17]+1.0*l[26] out[33] = 1.0*l[ 9]+1.0*l[19]+1.0*l[29] out[34] = 1.0*l[13]+1.0*l[23]+1.0*l[43] out[35] = 1.0*l[18]+1.0*l[27]+1.0*l[28] out[36] = 1.0*l[22]+1.0*l[32]+1.0*l[42] out[37] = 1.0*l[30] out[38] = 1.0*l[31] out[39] = 1.0*l[36]+1.0*l[46]+1.0*l[56]+1.0*l[66]+1.0*l[76] out[40] = 1.0*l[37]+1.0*l[47]+1.0*l[57]+1.0*l[67] out[41] = 1.0*l[38]+1.0*l[48]+1.0*l[58] out[42] = 1.0*l[39] out[43] = 1.0*l[40] out[44] = 1.0*l[41] out[45] = 1.0*l[45]+1.0*l[55]+1.0*l[65]+1.0*l[75] out[46] = 1.0*l[49] out[47] = 1.0*l[50] out[48] = 1.0*l[51]+1.0*l[61]+1.0*l[71] out[49] = 1.0*l[52]+1.0*l[53]+1.0*l[62] out[50] = 1.0*l[54]+1.0*l[63]+1.0*l[64]+1.0*l[72]+1.0*l[73]+1.0*l[74] out[51] = 1.0*l[59]+1.0*l[69]+1.0*l[79] out[52] = 1.0*l[60]+1.0*l[70]+1.0*l[80] out[53] = 1.0*l[68]+1.0*l[77]+1.0*l[78] # pack hor-pairs l = np.ravel(tile[:,:,2]) out[54] = 1.0*l[ 0]+1.0*l[ 1]+1.0*l[ 9]+1.0*l[10]+1.0*l[18]+1.0*l[27]+1.0*l[36]+1.0*l[45]+1.0*l[54]+1.0*l[63]+1.0*l[64]+1.0*l[72]+1.0*l[73] out[55] = 1.0*l[ 2]+1.0*l[11]+1.0*l[20] out[56] = 1.0*l[ 3]+1.0*l[12]+1.0*l[21] out[57] = 1.0*l[ 4]+1.0*l[13]+1.0*l[22] out[58] = 1.0*l[ 5]+1.0*l[14]+1.0*l[23] out[59] = 1.0*l[ 6]+1.0*l[15]+1.0*l[24] out[60] = 1.0*l[ 7]+1.0*l[ 8]+1.0*l[16]+1.0*l[17]+1.0*l[26]+1.0*l[35]+1.0*l[44]+1.0*l[53]+1.0*l[62]+1.0*l[70]+1.0*l[71]+1.0*l[79]+1.0*l[80] out[61] = 1.0*l[19]+1.0*l[28]+1.0*l[37]+1.0*l[46]+1.0*l[55] out[62] = 1.0*l[25]+1.0*l[34]+1.0*l[43]+1.0*l[52]+1.0*l[61] out[63] = 1.0*l[29]+1.0*l[38]+1.0*l[47] out[64] = 1.0*l[30] out[65] = 1.0*l[31] out[66] = 1.0*l[32] out[67] = 1.0*l[33]+1.0*l[42]+1.0*l[51] out[68] = 1.0*l[39] out[69] = 1.0*l[40] out[70] = 1.0*l[41] out[71] = 1.0*l[48] out[72] = 1.0*l[49] out[73] = 1.0*l[50] out[74] = 1.0*l[56]+1.0*l[65]+1.0*l[74] out[75] = 1.0*l[57]+1.0*l[66]+1.0*l[75] out[76] = 1.0*l[58]+1.0*l[67]+1.0*l[76] out[77] = 1.0*l[59]+1.0*l[68]+1.0*l[77] out[78] = 1.0*l[60]+1.0*l[69]+1.0*l[78] # pack vert-pairs l = np.ravel(tile[:,:,3]) out[79] = 1.0*l[ 0]+1.0*l[ 1]+1.0*l[ 2]+1.0*l[ 3]+1.0*l[ 4]+1.0*l[ 5]+1.0*l[ 6]+1.0*l[ 7]+1.0*l[ 8]+1.0*l[ 9]+1.0*l[10]+1.0*l[11]+1.0*l[16]+1.0*l[17] out[80] = 1.0*l[11]+1.0*l[12]+1.0*l[13]+1.0*l[14]+1.0*l[15] out[81] = 1.0*l[18]+1.0*l[19]+1.0*l[20] out[82] = 1.0*l[21]+1.0*l[22]+1.0*l[23] out[83] = 1.0*l[24]+1.0*l[25]+1.0*l[26] out[84] = 1.0*l[27]+1.0*l[28]+1.0*l[29] out[85] = 1.0*l[30] out[86] = 1.0*l[31] out[87] = 1.0*l[32] out[88] = 1.0*l[33]+1.0*l[34]+1.0*l[35] out[89] = 1.0*l[36]+1.0*l[37]+1.0*l[38] out[90] = 1.0*l[39] out[91] = 1.0*l[40] out[92] = 1.0*l[41] out[93] = 1.0*l[42]+1.0*l[43]+1.0*l[44] out[94] = 1.0*l[45]+1.0*l[46]+1.0*l[47] out[95] = 1.0*l[48] out[96] = 1.0*l[49] out[97] = 1.0*l[50] out[98] = 1.0*l[51]+1.0*l[52]+1.0*l[53] out[99] = 1.0*l[54]+1.0*l[55]+1.0*l[56] out[100] = 1.0*l[57]+1.0*l[58]+1.0*l[59] out[101] = 1.0*l[60]+1.0*l[61]+1.0*l[62] out[102] = 1.0*l[63]+1.0*l[64]+1.0*l[70]+1.0*l[71]+1.0*l[72]+1.0*l[73]+1.0*l[74]+1.0*l[75]+1.0*l[76]+1.0*l[77]+1.0*l[78]+1.0*l[79]+1.0*l[80] out[103] = 1.0*l[65]+1.0*l[66]+1.0*l[67]+1.0*l[68]+1.0*l[69] return out # pack single # pack single def pack_tile(tile): def pack_tile(tile): return pack_tile_type1(tile) return pack_tile_type1(tile) # pack all tiles # pack all tiles def pack(tiles): def pack(tiles,ptype=1): output = np.array([[pack_tile(tiles[i,j]) for j in range(tiles.shape[1])] for i in range(tiles.shape[0])]) if ptype==1: pack_func = pack_tile_type1 elif ptype==2: pack_func = pack_tile_type2 output = np.array([[pack_func(tiles[i,j]) for j in range(tiles.shape[1])] for i in range(tiles.shape[0])]) return output return output Loading test_nn_feed.py +120 −104 Original line number Original line Diff line number Diff line Loading @@ -46,6 +46,7 @@ def print_time(): VALUES_LAYER_NAME = 'other' VALUES_LAYER_NAME = 'other' LAYERS_OF_INTEREST = ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs'] LAYERS_OF_INTEREST = ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs'] RADIUS = 1 RADIUS = 1 TILE_PACKING_TYPE = 1 DEBUG_PLT_LOSS = True DEBUG_PLT_LOSS = True # If false - will not pack or rescal # If false - will not pack or rescal Loading Loading @@ -91,22 +92,26 @@ def lrelu(x): def network(input): def network(input): fc1 = slim.fully_connected(input,512,activation_fn=lrelu,scope='g_fc1') fc1 = slim.fully_connected(input,1024,activation_fn=lrelu,scope='g_fc1') fc2 = slim.fully_connected(fc1, 2,activation_fn=lrelu,scope='g_fc2') #fc2 = slim.fully_connected(fc1, 2,activation_fn=lrelu,scope='g_fc2') return fc2 #return fc2 #fc2 = slim.fully_connected(fc1, 1024,activation_fn=lrelu,scope='g_fc2') fc2 = slim.fully_connected(fc1, 1024,activation_fn=lrelu,scope='g_fc2') #fc3 = slim.fully_connected(fc2, 512,activation_fn=lrelu,scope='g_fc3') fc3 = slim.fully_connected(fc2, 512,activation_fn=lrelu,scope='g_fc3') #fc4 = slim.fully_connected(fc3, 8,activation_fn=lrelu,scope='g_fc4') fc4 = slim.fully_connected(fc3, 8,activation_fn=lrelu,scope='g_fc4') #fc5 = slim.fully_connected(fc4, 4,activation_fn=lrelu,scope='g_fc5') fc5 = slim.fully_connected(fc4, 4,activation_fn=lrelu,scope='g_fc5') #fc6 = slim.fully_connected(fc5, 2,activation_fn=lrelu,scope='g_fc6') fc6 = slim.fully_connected(fc5, 2,activation_fn=lrelu,scope='g_fc6') #return fc6 return fc6 sess = tf.Session() sess = tf.Session() if TILE_PACKING_TYPE==1: in_tile = tf.placeholder(tf.float32,[None,101]) in_tile = tf.placeholder(tf.float32,[None,101]) elif TILE_PACKING_TYPE==2: in_tile = tf.placeholder(tf.float32,[None,105]) gt = tf.placeholder(tf.float32,[None,2]) gt = tf.placeholder(tf.float32,[None,2]) Loading Loading @@ -135,23 +140,23 @@ cf_w_norm = tf.nn.softmax(cf_w) #G_loss = tf.reduce_mean(tf.abs(tf.nn.softmax(out[:,1])*out[:,0]-cf_w_norm*gt[:,0])) #G_loss = tf.reduce_mean(tf.abs(tf.nn.softmax(out[:,1])*out[:,0]-cf_w_norm*gt[:,0])) #G_loss = tf.reduce_mean(tf.squared_difference(out[:,0], gt[:,0])) #G_loss = tf.reduce_mean(tf.squared_difference(out[:,0], gt[:,0])) #G_loss = tf.reduce_mean(tf.abs(out[:,0]-gt[:,0])) G_loss = tf.reduce_mean(tf.abs(out[:,0]-gt[:,0])) G_loss = tf.losses.mean_squared_error(gt[:,0],out[:,0],cf_w) #G_loss = tf.losses.mean_squared_error(gt[:,0],out[:,0],cf_w) tf.summary.scalar('loss', G_loss) #tf.summary.scalar('loss', G_loss) tf.summary.scalar('prediction', out[0,0]) #tf.summary.scalar('prediction', out[0,0]) tf.summary.scalar('ground truth', gt[0,0]) #tf.summary.scalar('ground truth', gt[0,0]) t_vars=tf.trainable_variables() t_vars=tf.trainable_variables() lr=tf.placeholder(tf.float32) lr=tf.placeholder(tf.float32) G_opt=tf.train.AdamOptimizer(learning_rate=lr).minimize(G_loss,var_list=[var for var in t_vars if var.name.startswith('g_')]) G_opt=tf.train.AdamOptimizer(learning_rate=lr).minimize(G_loss) saver=tf.train.Saver() saver=tf.train.Saver() # ?!!!!! # ?!!!!! merged = tf.summary.merge_all() #merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter(result_dir + '/train', sess.graph) #train_writer = tf.summary.FileWriter(result_dir + '/train', sess.graph) test_writer = tf.summary.FileWriter(result_dir + '/test') #test_writer = tf.summary.FileWriter(result_dir + '/test') sess.run(tf.global_variables_initializer()) sess.run(tf.global_variables_initializer()) ckpt=tf.train.get_checkpoint_state(checkpoint_dir) ckpt=tf.train.get_checkpoint_state(checkpoint_dir) Loading Loading @@ -185,75 +190,90 @@ if DEBUG_PLT_LOSS: pass pass training_tiles = np.array([]) training_values = np.array([]) # get epoch train data for i in range(len(tlist)): # RUN print(bcolors.OKGREEN+"Opening "+tlist[i]+bcolors.ENDC) # epoch is one image tmp_tiff = ijt.imagej_tiff(tlist[i]) tmp_tiles = tmp_tiff.getstack(labels,shape_as_tiles=True) tmp_vals = tmp_tiff.getvalues(label=VALUES_LAYER_NAME) # might not need it because going to loop through anyway if TILE_PACKING_TYPE==1: packed_tiles = pile.pack(tmp_tiles) elif TILE_PACKING_TYPE==2: packed_tiles = pile.pack(tmp_tiles,TILE_PACKING_TYPE) for epoch in range(lastepoch,lastepoch+len(tlist)): packed_tiles = np.dstack((packed_tiles,tmp_vals[:,:,0])) print(bcolors.HEADER+"Epoch #"+str(epoch)+bcolors.ENDC) if os.path.isdir("result/%04d"%epoch): packed_tiles = np.reshape(packed_tiles,(-1,packed_tiles.shape[-1])) continue values = np.reshape(tmp_vals[:,:,1:3],(-1,2)) tlist_index = epoch - lastepoch packed_tiles_filtered = np.array([]) print(bcolors.OKGREEN+"Processing "+tlist[tlist_index]+bcolors.ENDC) print("Unfiltered: "+str(packed_tiles.shape)) tmp_tiff = ijt.imagej_tiff(tlist[tlist_index]) for j in range(packed_tiles.shape[0]): tmp_tiles = tmp_tiff.getstack(labels,shape_as_tiles=True) tmp_vals = tmp_tiff.getvalues(label=VALUES_LAYER_NAME) # might not need it because going to loop through anyway skip_tile = False packed_tiles = pile.pack(tmp_tiles) if np.isnan(np.sum(packed_tiles[j])): packed_tiles = np.dstack((packed_tiles,tmp_vals[:,:,0])) skip_tile = True if np.isnan(np.sum(values[j])): skip_tile = True #if epoch > 2000: if not skip_tile: # LR = 1e-5 if len(packed_tiles_filtered)==0: packed_tiles_filtered = np.array([packed_tiles[j]]) values_filtered = np.array([values[j]]) else: packed_tiles_filtered = np.append(packed_tiles_filtered,[packed_tiles[j]],axis=0) values_filtered = np.append(values_filtered,[values[j]],axis=0) # so, here get the image, remove nans and run for 100x times print("NaN-filtered: "+str(packed_tiles_filtered.shape)) packed_tiles[np.isnan(packed_tiles)] = 0.0 tmp_vals[np.isnan(tmp_vals)] = 0.0 #packed_tiles = packed_tiles[::,::] if i==0: values = tmp_vals training_tiles = packed_tiles_filtered training_values = values_filtered else: training_tiles = np.concatenate((training_tiles,packed_tiles_filtered),axis=0) training_values = np.concatenate((training_values,values_filtered),axis=0) input_patch = np.reshape(packed_tiles,(-1,101)) print("Training set shape: "+str(training_tiles.shape)) gt_patch = np.reshape(values[:,:,1:3],(-1,2)) g_loss = np.zeros(input_patch.shape[0]) # RUN # epoch is all available images # batch is a number of non-zero tiles g_loss = np.zeros(training_tiles.shape[0]) for i in range(100): #for epoch in range(lastepoch,lastepoch+len(tlist)): for epoch in range(lastepoch,500): print(bcolors.OKBLUE+"Iteration "+str(i)+bcolors.ENDC) print(bcolors.HEADER+"Epoch #"+str(epoch)+bcolors.ENDC) st=time.time() if os.path.isdir("result/%04d"%epoch): continue skip_iteration = False #if epoch > 2000: # LR = 1e-5 # if nan skip run! # so, here get the image, remove nans and run for 100x times if np.isnan(np.sum(gt_patch)): #packed_tiles[np.isnan(packed_tiles)] = 0.0 print("GT has NaNs") #tmp_vals[np.isnan(tmp_vals)] = 0.0 #skip_iteration = True if np.isnan(np.sum(input_patch)): input_patch = training_tiles print("Patch has NaNs") gt_patch = training_values #skip_iteration = True if skip_iteration: st=time.time() #print(bcolors.WARNING+"Found NaN, skipping iteration for tile "+str(i)+","+str(j)+bcolors.ENDC) pass else: run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) #run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() #run_metadata = tf.RunMetadata() #_,G_current,output = sess.run([G_opt,G_loss,out],feed_dict={in_tile:input_patch,gt:gt_patch,lr:LR},options=run_options,run_metadata=run_metadata) _,G_current,output,summary = sess.run([G_opt,G_loss,out,merged],feed_dict={in_tile:input_patch,gt:gt_patch,lr:LR},options=run_options,run_metadata=run_metadata) _,G_current,output = sess.run([G_opt,G_loss,out],feed_dict={in_tile:input_patch,gt:gt_patch,lr:LR}) #_,G_current,output = sess.run([G_opt,G_loss,out],feed_dict={in_tile:input_patch,gt:gt_patch,lr:LR}) g_loss[i]=G_current g_loss[i]=G_current mean_loss = np.mean(g_loss[np.where(g_loss)]) mean_loss = np.mean(g_loss[np.where(g_loss)]) Loading Loading @@ -300,17 +320,13 @@ for epoch in range(lastepoch,lastepoch+len(tlist)): else: else: print("%d %d Loss=%.3f CurrentLoss=%.3f Time=%.3f"%(epoch,i,mean_loss,G_current,time.time()-st)) print("%d %d Loss=%.3f CurrentLoss=%.3f Time=%.3f"%(epoch,i,mean_loss,G_current,time.time()-st)) #train_writer.add_run_metadata(run_metadata, 'step%d' % cnt) #test_writer.add_summary(summary,cnt) #train_writer.add_summary(summary, cnt) if epoch%save_freq==0: if epoch%save_freq==0: if not os.path.isdir(result_dir + '%04d'%epoch): if not os.path.isdir(result_dir + '%04d'%epoch): os.makedirs(result_dir + '%04d'%epoch) os.makedirs(result_dir + '%04d'%epoch) saver.save(sess, checkpoint_dir + 'model.ckpt') saver.save(sess, checkpoint_dir + 'model.ckpt') train_writer.close() test_writer.close() print_time() print_time() print(bcolors.OKGREEN+"time: "+str(time.time())+bcolors.ENDC) print(bcolors.OKGREEN+"time: "+str(time.time())+bcolors.ENDC) Loading test_nn_infer.py +22 −3 Original line number Original line Diff line number Diff line Loading @@ -44,6 +44,7 @@ def print_time(): VALUES_LAYER_NAME = 'other' VALUES_LAYER_NAME = 'other' LAYERS_OF_INTEREST = ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs'] LAYERS_OF_INTEREST = ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs'] RADIUS = 1 RADIUS = 1 TILE_PACKING_TYPE = 1 try: try: src = sys.argv[1] src = sys.argv[1] Loading Loading @@ -83,7 +84,11 @@ def network(input): sess = tf.Session() sess = tf.Session() if TILE_PACKING_TYPE==1: in_tile = tf.placeholder(tf.float32,[None,101]) in_tile = tf.placeholder(tf.float32,[None,101]) elif TILE_PACKING_TYPE==2: in_tile = tf.placeholder(tf.float32,[None,105]) gt = tf.placeholder(tf.float32,[None,2]) gt = tf.placeholder(tf.float32,[None,2]) out = network(in_tile) out = network(in_tile) Loading Loading @@ -143,7 +148,11 @@ for item in tlist: # tiles and values # tiles and values # might not need it because going to loop through anyway # might not need it because going to loop through anyway if TILE_PACKING_TYPE==1: packed_tiles = pile.pack(tiles) packed_tiles = pile.pack(tiles) elif TILE_PACKING_TYPE==2: packed_tiles = pile.pack(tiles,TILE_PACKING_TYPE) packed_tiles = np.dstack((packed_tiles,values[:,:,0])) packed_tiles = np.dstack((packed_tiles,values[:,:,0])) print(packed_tiles.shape) print(packed_tiles.shape) Loading @@ -168,11 +177,12 @@ for item in tlist: packed_tiles_flat = packed_tiles[i] packed_tiles_flat = packed_tiles[i] values_flat = values[i] values_flat = values[i] # whole row at once output = sess.run(out,feed_dict={in_tile:packed_tiles_flat}) output = sess.run(out,feed_dict={in_tile:packed_tiles_flat}) output_image[i] = output output_image[i] = output # so, let's print # so, let's print for j in range(output.shape[0]): for j in range(packed_tiles.shape[0]): p = output[j,0] p = output[j,0] pc = output[j,1] pc = output[j,1] fv = values_flat[j,0] fv = values_flat[j,0] Loading Loading @@ -204,6 +214,15 @@ for item in tlist: tif = np.dstack((im1,im2,im3)) tif = np.dstack((im1,im2,im3)) im3 = np.ravel(im3) print(im3.shape) im4 = im3[~np.isnan(im3)] rms = np.sqrt(np.mean(np.square(im4))) print("RMS = "+str(rms)) imagej_tiffwriter.save('prediction_results.tiff',tif) imagej_tiffwriter.save('prediction_results.tiff',tif) #sys.exit(0) #sys.exit(0) Loading Loading
pack_tile.py +134 −3 Original line number Original line Diff line number Diff line Loading @@ -6,7 +6,8 @@ __email__ = "oleg@elphel.com" import numpy as np import numpy as np # pack from 9x9x4 to 25x1 # pack from 9x9x4 to 100x1 # type 1, all the same def pack_tile_type1(tile): def pack_tile_type1(tile): out = np.empty(100) out = np.empty(100) Loading Loading @@ -125,13 +126,143 @@ def pack_tile_type1(tile): return out return out # pack from 9x9x4 to 104x1 # type 1, all the same def pack_tile_type2(tile): out = np.empty(104) # pack diagm-pair (not tested) l = np.ravel(tile[:,:,0]) out[ 0] = 1.0*l[ 0]+1.0*l[ 1]+1.0*l[ 2]+1.0*l[ 9]+1.0*l[10]+1.0*l[18] out[ 1] = 1.0*l[ 3]+1.0*l[11]+1.0*l[19]+1.0*l[27] out[ 2] = 1.0*l[ 4]+1.0*l[12]+1.0*l[20]+1.0*l[28]+1.0*l[36] out[ 3] = 1.0*l[ 5]+1.0*l[ 6]+1.0*l[14] out[ 4] = 1.0*l[ 7]+1.0*l[15]+1.0*l[23] out[ 5] = 1.0*l[ 8]+1.0*l[16]+1.0*l[24] out[ 6] = 1.0*l[13]+1.0*l[21]+1.0*l[29]+1.0*l[37] out[ 7] = 1.0*l[17]+1.0*l[25]+1.0*l[33] out[ 8] = 1.0*l[22]+1.0*l[30]+1.0*l[38] out[ 9] = 1.0*l[26]+1.0*l[34]+1.0*l[35] out[10] = 1.0*l[31] out[11] = 1.0*l[32] out[12] = 1.0*l[39] out[13] = 1.0*l[40] out[14] = 1.0*l[41] out[15] = 1.0*l[42]+1.0*l[50]+1.0*l[58] out[16] = 1.0*l[43]+1.0*l[51]+1.0*l[59]+1.0*l[67] out[17] = 1.0*l[44]+1.0*l[52]+1.0*l[60]+1.0*l[68]+1.0*l[76] out[18] = 1.0*l[45]+1.0*l[46]+1.0*l[54] out[19] = 1.0*l[47]+1.0*l[55]+1.0*l[63] out[20] = 1.0*l[48] out[21] = 1.0*l[49] out[22] = 1.0*l[53]+1.0*l[61]+1.0*l[69]+1.0*l[77] out[23] = 1.0*l[56]+1.0*l[64]+1.0*l[72] out[24] = 1.0*l[57]+1.0*l[65]+1.0*l[73] out[25] = 1.0*l[62]+1.0*l[70]+1.0*l[71]+1.0*l[78]+1.0*l[79]+1.0*l[80] out[26] = 1.0*l[66]+1.0*l[74]+1.0*l[75] # pack diago-pair (not tested) l = np.ravel(tile[:,:,1]) out[27] = 1.0*l[ 0]+1.0*l[10]+1.0*l[20] out[28] = 1.0*l[ 1]+1.0*l[11]+1.0*l[21] out[29] = 1.0*l[ 2]+1.0*l[ 3]+1.0*l[12] out[30] = 1.0*l[ 4]+1.0*l[14]+1.0*l[24]+1.0*l[34]+1.0*l[44] out[31] = 1.0*l[ 5]+1.0*l[15]+1.0*l[25]+1.0*l[35] out[32] = 1.0*l[ 6]+1.0*l[ 7]+1.0*l[ 8]+1.0*l[16]+1.0*l[17]+1.0*l[26] out[33] = 1.0*l[ 9]+1.0*l[19]+1.0*l[29] out[34] = 1.0*l[13]+1.0*l[23]+1.0*l[43] out[35] = 1.0*l[18]+1.0*l[27]+1.0*l[28] out[36] = 1.0*l[22]+1.0*l[32]+1.0*l[42] out[37] = 1.0*l[30] out[38] = 1.0*l[31] out[39] = 1.0*l[36]+1.0*l[46]+1.0*l[56]+1.0*l[66]+1.0*l[76] out[40] = 1.0*l[37]+1.0*l[47]+1.0*l[57]+1.0*l[67] out[41] = 1.0*l[38]+1.0*l[48]+1.0*l[58] out[42] = 1.0*l[39] out[43] = 1.0*l[40] out[44] = 1.0*l[41] out[45] = 1.0*l[45]+1.0*l[55]+1.0*l[65]+1.0*l[75] out[46] = 1.0*l[49] out[47] = 1.0*l[50] out[48] = 1.0*l[51]+1.0*l[61]+1.0*l[71] out[49] = 1.0*l[52]+1.0*l[53]+1.0*l[62] out[50] = 1.0*l[54]+1.0*l[63]+1.0*l[64]+1.0*l[72]+1.0*l[73]+1.0*l[74] out[51] = 1.0*l[59]+1.0*l[69]+1.0*l[79] out[52] = 1.0*l[60]+1.0*l[70]+1.0*l[80] out[53] = 1.0*l[68]+1.0*l[77]+1.0*l[78] # pack hor-pairs l = np.ravel(tile[:,:,2]) out[54] = 1.0*l[ 0]+1.0*l[ 1]+1.0*l[ 9]+1.0*l[10]+1.0*l[18]+1.0*l[27]+1.0*l[36]+1.0*l[45]+1.0*l[54]+1.0*l[63]+1.0*l[64]+1.0*l[72]+1.0*l[73] out[55] = 1.0*l[ 2]+1.0*l[11]+1.0*l[20] out[56] = 1.0*l[ 3]+1.0*l[12]+1.0*l[21] out[57] = 1.0*l[ 4]+1.0*l[13]+1.0*l[22] out[58] = 1.0*l[ 5]+1.0*l[14]+1.0*l[23] out[59] = 1.0*l[ 6]+1.0*l[15]+1.0*l[24] out[60] = 1.0*l[ 7]+1.0*l[ 8]+1.0*l[16]+1.0*l[17]+1.0*l[26]+1.0*l[35]+1.0*l[44]+1.0*l[53]+1.0*l[62]+1.0*l[70]+1.0*l[71]+1.0*l[79]+1.0*l[80] out[61] = 1.0*l[19]+1.0*l[28]+1.0*l[37]+1.0*l[46]+1.0*l[55] out[62] = 1.0*l[25]+1.0*l[34]+1.0*l[43]+1.0*l[52]+1.0*l[61] out[63] = 1.0*l[29]+1.0*l[38]+1.0*l[47] out[64] = 1.0*l[30] out[65] = 1.0*l[31] out[66] = 1.0*l[32] out[67] = 1.0*l[33]+1.0*l[42]+1.0*l[51] out[68] = 1.0*l[39] out[69] = 1.0*l[40] out[70] = 1.0*l[41] out[71] = 1.0*l[48] out[72] = 1.0*l[49] out[73] = 1.0*l[50] out[74] = 1.0*l[56]+1.0*l[65]+1.0*l[74] out[75] = 1.0*l[57]+1.0*l[66]+1.0*l[75] out[76] = 1.0*l[58]+1.0*l[67]+1.0*l[76] out[77] = 1.0*l[59]+1.0*l[68]+1.0*l[77] out[78] = 1.0*l[60]+1.0*l[69]+1.0*l[78] # pack vert-pairs l = np.ravel(tile[:,:,3]) out[79] = 1.0*l[ 0]+1.0*l[ 1]+1.0*l[ 2]+1.0*l[ 3]+1.0*l[ 4]+1.0*l[ 5]+1.0*l[ 6]+1.0*l[ 7]+1.0*l[ 8]+1.0*l[ 9]+1.0*l[10]+1.0*l[11]+1.0*l[16]+1.0*l[17] out[80] = 1.0*l[11]+1.0*l[12]+1.0*l[13]+1.0*l[14]+1.0*l[15] out[81] = 1.0*l[18]+1.0*l[19]+1.0*l[20] out[82] = 1.0*l[21]+1.0*l[22]+1.0*l[23] out[83] = 1.0*l[24]+1.0*l[25]+1.0*l[26] out[84] = 1.0*l[27]+1.0*l[28]+1.0*l[29] out[85] = 1.0*l[30] out[86] = 1.0*l[31] out[87] = 1.0*l[32] out[88] = 1.0*l[33]+1.0*l[34]+1.0*l[35] out[89] = 1.0*l[36]+1.0*l[37]+1.0*l[38] out[90] = 1.0*l[39] out[91] = 1.0*l[40] out[92] = 1.0*l[41] out[93] = 1.0*l[42]+1.0*l[43]+1.0*l[44] out[94] = 1.0*l[45]+1.0*l[46]+1.0*l[47] out[95] = 1.0*l[48] out[96] = 1.0*l[49] out[97] = 1.0*l[50] out[98] = 1.0*l[51]+1.0*l[52]+1.0*l[53] out[99] = 1.0*l[54]+1.0*l[55]+1.0*l[56] out[100] = 1.0*l[57]+1.0*l[58]+1.0*l[59] out[101] = 1.0*l[60]+1.0*l[61]+1.0*l[62] out[102] = 1.0*l[63]+1.0*l[64]+1.0*l[70]+1.0*l[71]+1.0*l[72]+1.0*l[73]+1.0*l[74]+1.0*l[75]+1.0*l[76]+1.0*l[77]+1.0*l[78]+1.0*l[79]+1.0*l[80] out[103] = 1.0*l[65]+1.0*l[66]+1.0*l[67]+1.0*l[68]+1.0*l[69] return out # pack single # pack single def pack_tile(tile): def pack_tile(tile): return pack_tile_type1(tile) return pack_tile_type1(tile) # pack all tiles # pack all tiles def pack(tiles): def pack(tiles,ptype=1): output = np.array([[pack_tile(tiles[i,j]) for j in range(tiles.shape[1])] for i in range(tiles.shape[0])]) if ptype==1: pack_func = pack_tile_type1 elif ptype==2: pack_func = pack_tile_type2 output = np.array([[pack_func(tiles[i,j]) for j in range(tiles.shape[1])] for i in range(tiles.shape[0])]) return output return output Loading
test_nn_feed.py +120 −104 Original line number Original line Diff line number Diff line Loading @@ -46,6 +46,7 @@ def print_time(): VALUES_LAYER_NAME = 'other' VALUES_LAYER_NAME = 'other' LAYERS_OF_INTEREST = ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs'] LAYERS_OF_INTEREST = ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs'] RADIUS = 1 RADIUS = 1 TILE_PACKING_TYPE = 1 DEBUG_PLT_LOSS = True DEBUG_PLT_LOSS = True # If false - will not pack or rescal # If false - will not pack or rescal Loading Loading @@ -91,22 +92,26 @@ def lrelu(x): def network(input): def network(input): fc1 = slim.fully_connected(input,512,activation_fn=lrelu,scope='g_fc1') fc1 = slim.fully_connected(input,1024,activation_fn=lrelu,scope='g_fc1') fc2 = slim.fully_connected(fc1, 2,activation_fn=lrelu,scope='g_fc2') #fc2 = slim.fully_connected(fc1, 2,activation_fn=lrelu,scope='g_fc2') return fc2 #return fc2 #fc2 = slim.fully_connected(fc1, 1024,activation_fn=lrelu,scope='g_fc2') fc2 = slim.fully_connected(fc1, 1024,activation_fn=lrelu,scope='g_fc2') #fc3 = slim.fully_connected(fc2, 512,activation_fn=lrelu,scope='g_fc3') fc3 = slim.fully_connected(fc2, 512,activation_fn=lrelu,scope='g_fc3') #fc4 = slim.fully_connected(fc3, 8,activation_fn=lrelu,scope='g_fc4') fc4 = slim.fully_connected(fc3, 8,activation_fn=lrelu,scope='g_fc4') #fc5 = slim.fully_connected(fc4, 4,activation_fn=lrelu,scope='g_fc5') fc5 = slim.fully_connected(fc4, 4,activation_fn=lrelu,scope='g_fc5') #fc6 = slim.fully_connected(fc5, 2,activation_fn=lrelu,scope='g_fc6') fc6 = slim.fully_connected(fc5, 2,activation_fn=lrelu,scope='g_fc6') #return fc6 return fc6 sess = tf.Session() sess = tf.Session() if TILE_PACKING_TYPE==1: in_tile = tf.placeholder(tf.float32,[None,101]) in_tile = tf.placeholder(tf.float32,[None,101]) elif TILE_PACKING_TYPE==2: in_tile = tf.placeholder(tf.float32,[None,105]) gt = tf.placeholder(tf.float32,[None,2]) gt = tf.placeholder(tf.float32,[None,2]) Loading Loading @@ -135,23 +140,23 @@ cf_w_norm = tf.nn.softmax(cf_w) #G_loss = tf.reduce_mean(tf.abs(tf.nn.softmax(out[:,1])*out[:,0]-cf_w_norm*gt[:,0])) #G_loss = tf.reduce_mean(tf.abs(tf.nn.softmax(out[:,1])*out[:,0]-cf_w_norm*gt[:,0])) #G_loss = tf.reduce_mean(tf.squared_difference(out[:,0], gt[:,0])) #G_loss = tf.reduce_mean(tf.squared_difference(out[:,0], gt[:,0])) #G_loss = tf.reduce_mean(tf.abs(out[:,0]-gt[:,0])) G_loss = tf.reduce_mean(tf.abs(out[:,0]-gt[:,0])) G_loss = tf.losses.mean_squared_error(gt[:,0],out[:,0],cf_w) #G_loss = tf.losses.mean_squared_error(gt[:,0],out[:,0],cf_w) tf.summary.scalar('loss', G_loss) #tf.summary.scalar('loss', G_loss) tf.summary.scalar('prediction', out[0,0]) #tf.summary.scalar('prediction', out[0,0]) tf.summary.scalar('ground truth', gt[0,0]) #tf.summary.scalar('ground truth', gt[0,0]) t_vars=tf.trainable_variables() t_vars=tf.trainable_variables() lr=tf.placeholder(tf.float32) lr=tf.placeholder(tf.float32) G_opt=tf.train.AdamOptimizer(learning_rate=lr).minimize(G_loss,var_list=[var for var in t_vars if var.name.startswith('g_')]) G_opt=tf.train.AdamOptimizer(learning_rate=lr).minimize(G_loss) saver=tf.train.Saver() saver=tf.train.Saver() # ?!!!!! # ?!!!!! merged = tf.summary.merge_all() #merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter(result_dir + '/train', sess.graph) #train_writer = tf.summary.FileWriter(result_dir + '/train', sess.graph) test_writer = tf.summary.FileWriter(result_dir + '/test') #test_writer = tf.summary.FileWriter(result_dir + '/test') sess.run(tf.global_variables_initializer()) sess.run(tf.global_variables_initializer()) ckpt=tf.train.get_checkpoint_state(checkpoint_dir) ckpt=tf.train.get_checkpoint_state(checkpoint_dir) Loading Loading @@ -185,75 +190,90 @@ if DEBUG_PLT_LOSS: pass pass training_tiles = np.array([]) training_values = np.array([]) # get epoch train data for i in range(len(tlist)): # RUN print(bcolors.OKGREEN+"Opening "+tlist[i]+bcolors.ENDC) # epoch is one image tmp_tiff = ijt.imagej_tiff(tlist[i]) tmp_tiles = tmp_tiff.getstack(labels,shape_as_tiles=True) tmp_vals = tmp_tiff.getvalues(label=VALUES_LAYER_NAME) # might not need it because going to loop through anyway if TILE_PACKING_TYPE==1: packed_tiles = pile.pack(tmp_tiles) elif TILE_PACKING_TYPE==2: packed_tiles = pile.pack(tmp_tiles,TILE_PACKING_TYPE) for epoch in range(lastepoch,lastepoch+len(tlist)): packed_tiles = np.dstack((packed_tiles,tmp_vals[:,:,0])) print(bcolors.HEADER+"Epoch #"+str(epoch)+bcolors.ENDC) if os.path.isdir("result/%04d"%epoch): packed_tiles = np.reshape(packed_tiles,(-1,packed_tiles.shape[-1])) continue values = np.reshape(tmp_vals[:,:,1:3],(-1,2)) tlist_index = epoch - lastepoch packed_tiles_filtered = np.array([]) print(bcolors.OKGREEN+"Processing "+tlist[tlist_index]+bcolors.ENDC) print("Unfiltered: "+str(packed_tiles.shape)) tmp_tiff = ijt.imagej_tiff(tlist[tlist_index]) for j in range(packed_tiles.shape[0]): tmp_tiles = tmp_tiff.getstack(labels,shape_as_tiles=True) tmp_vals = tmp_tiff.getvalues(label=VALUES_LAYER_NAME) # might not need it because going to loop through anyway skip_tile = False packed_tiles = pile.pack(tmp_tiles) if np.isnan(np.sum(packed_tiles[j])): packed_tiles = np.dstack((packed_tiles,tmp_vals[:,:,0])) skip_tile = True if np.isnan(np.sum(values[j])): skip_tile = True #if epoch > 2000: if not skip_tile: # LR = 1e-5 if len(packed_tiles_filtered)==0: packed_tiles_filtered = np.array([packed_tiles[j]]) values_filtered = np.array([values[j]]) else: packed_tiles_filtered = np.append(packed_tiles_filtered,[packed_tiles[j]],axis=0) values_filtered = np.append(values_filtered,[values[j]],axis=0) # so, here get the image, remove nans and run for 100x times print("NaN-filtered: "+str(packed_tiles_filtered.shape)) packed_tiles[np.isnan(packed_tiles)] = 0.0 tmp_vals[np.isnan(tmp_vals)] = 0.0 #packed_tiles = packed_tiles[::,::] if i==0: values = tmp_vals training_tiles = packed_tiles_filtered training_values = values_filtered else: training_tiles = np.concatenate((training_tiles,packed_tiles_filtered),axis=0) training_values = np.concatenate((training_values,values_filtered),axis=0) input_patch = np.reshape(packed_tiles,(-1,101)) print("Training set shape: "+str(training_tiles.shape)) gt_patch = np.reshape(values[:,:,1:3],(-1,2)) g_loss = np.zeros(input_patch.shape[0]) # RUN # epoch is all available images # batch is a number of non-zero tiles g_loss = np.zeros(training_tiles.shape[0]) for i in range(100): #for epoch in range(lastepoch,lastepoch+len(tlist)): for epoch in range(lastepoch,500): print(bcolors.OKBLUE+"Iteration "+str(i)+bcolors.ENDC) print(bcolors.HEADER+"Epoch #"+str(epoch)+bcolors.ENDC) st=time.time() if os.path.isdir("result/%04d"%epoch): continue skip_iteration = False #if epoch > 2000: # LR = 1e-5 # if nan skip run! # so, here get the image, remove nans and run for 100x times if np.isnan(np.sum(gt_patch)): #packed_tiles[np.isnan(packed_tiles)] = 0.0 print("GT has NaNs") #tmp_vals[np.isnan(tmp_vals)] = 0.0 #skip_iteration = True if np.isnan(np.sum(input_patch)): input_patch = training_tiles print("Patch has NaNs") gt_patch = training_values #skip_iteration = True if skip_iteration: st=time.time() #print(bcolors.WARNING+"Found NaN, skipping iteration for tile "+str(i)+","+str(j)+bcolors.ENDC) pass else: run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) #run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() #run_metadata = tf.RunMetadata() #_,G_current,output = sess.run([G_opt,G_loss,out],feed_dict={in_tile:input_patch,gt:gt_patch,lr:LR},options=run_options,run_metadata=run_metadata) _,G_current,output,summary = sess.run([G_opt,G_loss,out,merged],feed_dict={in_tile:input_patch,gt:gt_patch,lr:LR},options=run_options,run_metadata=run_metadata) _,G_current,output = sess.run([G_opt,G_loss,out],feed_dict={in_tile:input_patch,gt:gt_patch,lr:LR}) #_,G_current,output = sess.run([G_opt,G_loss,out],feed_dict={in_tile:input_patch,gt:gt_patch,lr:LR}) g_loss[i]=G_current g_loss[i]=G_current mean_loss = np.mean(g_loss[np.where(g_loss)]) mean_loss = np.mean(g_loss[np.where(g_loss)]) Loading Loading @@ -300,17 +320,13 @@ for epoch in range(lastepoch,lastepoch+len(tlist)): else: else: print("%d %d Loss=%.3f CurrentLoss=%.3f Time=%.3f"%(epoch,i,mean_loss,G_current,time.time()-st)) print("%d %d Loss=%.3f CurrentLoss=%.3f Time=%.3f"%(epoch,i,mean_loss,G_current,time.time()-st)) #train_writer.add_run_metadata(run_metadata, 'step%d' % cnt) #test_writer.add_summary(summary,cnt) #train_writer.add_summary(summary, cnt) if epoch%save_freq==0: if epoch%save_freq==0: if not os.path.isdir(result_dir + '%04d'%epoch): if not os.path.isdir(result_dir + '%04d'%epoch): os.makedirs(result_dir + '%04d'%epoch) os.makedirs(result_dir + '%04d'%epoch) saver.save(sess, checkpoint_dir + 'model.ckpt') saver.save(sess, checkpoint_dir + 'model.ckpt') train_writer.close() test_writer.close() print_time() print_time() print(bcolors.OKGREEN+"time: "+str(time.time())+bcolors.ENDC) print(bcolors.OKGREEN+"time: "+str(time.time())+bcolors.ENDC) Loading
test_nn_infer.py +22 −3 Original line number Original line Diff line number Diff line Loading @@ -44,6 +44,7 @@ def print_time(): VALUES_LAYER_NAME = 'other' VALUES_LAYER_NAME = 'other' LAYERS_OF_INTEREST = ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs'] LAYERS_OF_INTEREST = ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs'] RADIUS = 1 RADIUS = 1 TILE_PACKING_TYPE = 1 try: try: src = sys.argv[1] src = sys.argv[1] Loading Loading @@ -83,7 +84,11 @@ def network(input): sess = tf.Session() sess = tf.Session() if TILE_PACKING_TYPE==1: in_tile = tf.placeholder(tf.float32,[None,101]) in_tile = tf.placeholder(tf.float32,[None,101]) elif TILE_PACKING_TYPE==2: in_tile = tf.placeholder(tf.float32,[None,105]) gt = tf.placeholder(tf.float32,[None,2]) gt = tf.placeholder(tf.float32,[None,2]) out = network(in_tile) out = network(in_tile) Loading Loading @@ -143,7 +148,11 @@ for item in tlist: # tiles and values # tiles and values # might not need it because going to loop through anyway # might not need it because going to loop through anyway if TILE_PACKING_TYPE==1: packed_tiles = pile.pack(tiles) packed_tiles = pile.pack(tiles) elif TILE_PACKING_TYPE==2: packed_tiles = pile.pack(tiles,TILE_PACKING_TYPE) packed_tiles = np.dstack((packed_tiles,values[:,:,0])) packed_tiles = np.dstack((packed_tiles,values[:,:,0])) print(packed_tiles.shape) print(packed_tiles.shape) Loading @@ -168,11 +177,12 @@ for item in tlist: packed_tiles_flat = packed_tiles[i] packed_tiles_flat = packed_tiles[i] values_flat = values[i] values_flat = values[i] # whole row at once output = sess.run(out,feed_dict={in_tile:packed_tiles_flat}) output = sess.run(out,feed_dict={in_tile:packed_tiles_flat}) output_image[i] = output output_image[i] = output # so, let's print # so, let's print for j in range(output.shape[0]): for j in range(packed_tiles.shape[0]): p = output[j,0] p = output[j,0] pc = output[j,1] pc = output[j,1] fv = values_flat[j,0] fv = values_flat[j,0] Loading Loading @@ -204,6 +214,15 @@ for item in tlist: tif = np.dstack((im1,im2,im3)) tif = np.dstack((im1,im2,im3)) im3 = np.ravel(im3) print(im3.shape) im4 = im3[~np.isnan(im3)] rms = np.sqrt(np.mean(np.square(im4))) print("RMS = "+str(rms)) imagej_tiffwriter.save('prediction_results.tiff',tif) imagej_tiffwriter.save('prediction_results.tiff',tif) #sys.exit(0) #sys.exit(0) Loading