Commit d786da7b authored by Andrey Filippov's avatar Andrey Filippov
Browse files

commited old modified files

parent 8ba9b224
Loading
Loading
Loading
Loading

explore_data5m.py

0 → 100644
+1253 −0

File added.

Preview size limit exceeded, changes collapsed.

+189 −9
Original line number Original line Diff line number Diff line
@@ -13,11 +13,14 @@ import sys


#import numpy as np
#import numpy as np


import imagej_tiffwriter

import time
import time


import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
from matplotlib.backends.backend_pdf import PdfPages
import qcstereo_functions as qsf
import qcstereo_functions as qsf
import numpy as np


#import xml.etree.ElementTree as ET
#import xml.etree.ElementTree as ET


@@ -132,27 +135,143 @@ fig_params = get_fig_params(dbg_parameters['disparity_ranges'])


pass
pass


#temporary:
TIFF_ONLY = False # True
#max_bad =        2.5 # excludes only direct bad
max_bad =        2.5 #2.5 # 1.5 # excludes only direct bad
max_diff =       1.5 # 2.0 # 5.0 # maximal max-min difference
max_target_err = 1.0 # 0.5 # maximal max-min difference
max_disp =       5.0

min_strength =   0.18 #ignore tiles below
min_neibs =      1
max_log_to_mm =  0.5 # difference between center average and center should be under this fraction of max-min (0 - disables feature) 


#num_bins = 256 # number of histogram bins
num_bins =        15 # 50 # number of histogram bins
use_gt_weights = True # False # True
index_gt =         2
index_gt_weight =  3
index_heur_err =   7
index_nn_err =     6
index_mm =         8 # max-min
index_log =        9
index_bad =       10
index_num_neibs = 11
"""
Debugging high 9-tile variations, removing error for all tiles with lower difference between max and min
"""
#min_diff =       0.25 # remove all flat tiles with spread less than this (do not show on heuristic/network disparity errors subplots 
min_diff =       0 # remove all flat tiles with spread less than this 




max_target_err2 = max_target_err * max_target_err
if not 'show' in FIGS_SAVESHOW:
if not 'show' in FIGS_SAVESHOW:
    plt.ioff()
    plt.ioff()


for mode in ['train','infer']:
#for mode in ['train','infer']:
for mode in ['infer']:
    figs = []
    figs = []
    ffiles = [] # no ext
    ffiles = [] # no ext
    def setlimsxy(lim_xy):
    def setlimsxy(lim_xy):
        if not lim_xy is None:
        if not lim_xy is None:
            plt.xlim(min(lim_xy[:2]),max(lim_xy[:2]))            
            plt.xlim(min(lim_xy[:2]),max(lim_xy[:2]))            
            plt.ylim(max(lim_xy[2:]),min(lim_xy[2:]))
            plt.ylim(max(lim_xy[2:]),min(lim_xy[2:]))
    cumul_weights = None                   
        
        
    for nfile, fpars in enumerate(fig_params):
    for nfile, fpars in enumerate(fig_params):
        if not fpars is None:
        if not fpars is None:
            img_file = files['result'][nfile]
            img_file = files['result'][nfile]
            if mode == 'infer':
            if mode == 'infer':
                img_file = img_file.replace('.npy','-infer.npy')
                img_file = img_file.replace('.npy','-infer.npy')
            """    
            try:    
            try:    
                data,_ = qsf.result_npy_prepare(img_file, ABSOLUTE_DISPARITY, fix_nan=True, insert_deltas=True)
#                data,_ = qsf.result_npy_prepare(img_file, ABSOLUTE_DISPARITY, fix_nan=True, insert_deltas=True)
#                data,_ = qsf.result_npy_prepare(img_file, ABSOLUTE_DISPARITY, fix_nan=True, insert_deltas=3)
                data,labels = qsf.result_npy_prepare(img_file, ABSOLUTE_DISPARITY, fix_nan=True, insert_deltas=3)
            except:
            except:
                print ("Image file does not exist:", img_file)
                print ("Image file does not exist:", img_file)
                continue
                continue
            """
            pass
            data,labels = qsf.result_npy_prepare(img_file, ABSOLUTE_DISPARITY, fix_nan=True, insert_deltas=3)
            if  True: #TIFF_ONLY:
                
                
                tiff_path = img_file.replace('.npy','-test.tiff')
                        
                data = data.transpose(2,0,1)
                print("Saving results to TIFF: "+tiff_path)
                imagej_tiffwriter.save(tiff_path,data,labels=labels)
                """
                Calculate histograms
                """
                err_heur2 = data[index_heur_err]*data[index_heur_err] 
                err_nn2 =   data[index_nn_err]*  data[index_nn_err] 
                diff_log2 = data[index_log]*     data[index_log] 
                weights = (
                    (data[index_gt] < max_disp) & 
                    (err_heur2 < max_target_err2) &
                    (data[index_bad] < max_bad) &
                    (data[index_gt_weight] >= min_strength) &
                    (data[index_num_neibs] >= min_neibs)&
#max_log_to_mm =  0.5 # difference between center average and center should be under this fraction of max-min (0 - disables feature) 
                    (data[index_log] < max_log_to_mm * np.sqrt(data[index_mm]) )                    
                    ).astype(data.dtype) # 0.0/1.1
                #max_disp
                
                #max_target_err
                if  use_gt_weights:
                    weights *= data[index_gt_weight]
                mm =     data[index_mm]
                weh = np.nan_to_num(weights*err_heur2)
                wen = np.nan_to_num(weights*err_nn2)
                wel = np.nan_to_num(weights*diff_log2)
                hist_weights,bin_vals =   np.histogram(a=mm, bins = num_bins, range = (0.0, max_diff), weights = weights,  density = False)
                hist_err_heur2,_ = np.histogram(a=mm, bins = num_bins, range = (0.0, max_diff), weights = weh,      density = False)
                hist_err_nn2,_ =   np.histogram(a=mm, bins = num_bins, range = (0.0, max_diff), weights = wen,      density = False)
                hist_diff_log2,_ = np.histogram(a=mm, bins = num_bins, range = (0.0, max_diff), weights = wel,      density = False)
                if cumul_weights is None:
                    cumul_weights =    hist_weights
                    cumul_err_heur2 =  hist_err_heur2
                    cumul_err_nn2 =    hist_err_nn2
                    cumul_diff_log2 =  hist_diff_log2
                else:
                    cumul_weights +=   hist_weights
                    cumul_err_heur2 += hist_err_heur2
                    cumul_err_nn2 +=   hist_err_nn2
                    cumul_diff_log2 += hist_diff_log2
                
                hist_err_heur2 =   np.nan_to_num(hist_err_heur2/hist_weights)
                hist_err_nn2 =     np.nan_to_num(hist_err_nn2/hist_weights)
                hist_gain2 = np.nan_to_num(hist_err_heur2/hist_err_nn2)
                hist_gain = np.sqrt(hist_gain2)
                hist_diff_log2 =   np.nan_to_num(hist_diff_log2/hist_weights)

                print("hist_err_heur2", end = " ")
                print(np.sqrt(hist_err_heur2))
                print("hist_err_nn2", end = " ")
                print(np.sqrt(hist_err_nn2))
                print("hist_gain", end = " ")
                print(hist_gain)
                print("hist_diff_log2", end = " ")
                print(np.sqrt(hist_diff_log2))
                
                
                if min_diff> 0.0:
                    pass
                    good = (mm > min_diff).astype(mm.dtype)
                    good /= good # good - 1, bad - nan
                    data[index_heur_err] *= good
                    data[index_nn_err] *= good
                data = data.transpose(1,2,0)
                
            if TIFF_ONLY:
                continue
        
        
        
            for subindex, rng in enumerate(fpars['ranges']):
            for subindex, rng in enumerate(fpars['ranges']):
                lim_val = rng['lim_val']
                lim_val = rng['lim_val']
@@ -214,7 +333,68 @@ for mode in ['train','infer']:
                        fb_noext+="-"+str(subindex)
                        fb_noext+="-"+str(subindex)
                ffiles.append(fb_noext)
                ffiles.append(fb_noext)
                pass
                pass
    if True:
        cumul_err_heur2 =   np.nan_to_num(cumul_err_heur2/cumul_weights)
        cumul_err_nn2 =     np.nan_to_num(cumul_err_nn2/cumul_weights)
        cumul_gain2 =       np.nan_to_num(cumul_err_heur2/cumul_err_nn2)
        cumul_gain =        np.sqrt(cumul_gain2)
        cumul_diff_log2 =   np.nan_to_num(cumul_diff_log2/cumul_weights)
        print("cumul_weights", end = " ")
        print(cumul_weights)
        print("cumul_err_heur", end = " ")
        print(np.sqrt(cumul_err_heur2))
        print("cumul_err_nn", end = " ")
        print(np.sqrt(cumul_err_nn2))
        print("cumul_gain", end = " ")
        print(cumul_gain)
        print("cumul_diff_log2", end = " ")
        print(np.sqrt(cumul_diff_log2))
        fig, ax1 = plt.subplots()
        ax1.set_xlabel('3x3 tiles ground truth disparity max-min (pix)')
        ax1.set_ylabel('RMSE\n(pix)', color='black', rotation='horizontal')
        ax1.yaxis.set_label_coords(-0.045,0.92)
        
        ax1.plot(bin_vals[0:-1], np.sqrt(cumul_err_nn2),   'tab:red',label="network disparity RMSE")
        ax1.plot(bin_vals[0:-1], np.sqrt(cumul_err_heur2), 'tab:green',label="heuristic disparity RMSE")
        ax1.plot(bin_vals[0:-1], np.sqrt(cumul_diff_log2), 'tab:cyan',label="ground truth LoG")
        
        ax1.tick_params(axis='y', labelcolor='black')
        
        ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis
        ax2.set_ylabel('weight', color='black', rotation='horizontal')  # we already handled the x-label with ax1  
        ax2.yaxis.set_label_coords(1.06,1.0)
        
        
              
        ax2.plot(bin_vals[0:-1], cumul_weights,color='grey',dashes=[6, 2],label='weights = n_tiles * gt_confidence')
        ax1.legend(loc="upper left", bbox_to_anchor=(0.2,1.0))
        ax2.legend(loc="lower right", bbox_to_anchor=(1.0,0.1))
        
        """
    
        fig = plt.figure(figsize=FIGSIZE)
        fig.canvas.set_window_title('Cumulative')
        fig.suptitle('Difference to GT')
#        ax_conf=plt.subplot(322)
        ax_conf=plt.subplot(211)
        ax_conf.set_title("RMS vs max9-min9")
        plt.plot(bin_vals[0:-1], np.sqrt(cumul_err_heur2),'red',
                 bin_vals[0:-1], np.sqrt(cumul_err_nn2),'green',
                 bin_vals[0:-1], np.sqrt(cumul_diff_log2),'blue')
        figs.append(fig)
        ffiles.append('cumulative')
        ax_conf=plt.subplot(212)
        ax_conf.set_title("weights vs max9-min9")
        plt.plot(bin_vals[0:-1], cumul_weights,'black')
        """
        figs.append(fig)
        ffiles.append('cumulative')
        pass
        #bin_vals[0:-1]
        
#            fig.suptitle("Groud truth confidence")


#    
    #whow to allow adjustment before applying tight_layout?
    #whow to allow adjustment before applying tight_layout?
    pass
    pass
    for fig in figs:
    for fig in figs:
@@ -229,9 +409,9 @@ for mode in ['train','infer']:
        pp=None
        pp=None
        if 'pdf' in FIGS_EXTENSIONS:
        if 'pdf' in FIGS_EXTENSIONS:
            if mode == 'infer':
            if mode == 'infer':
                pdf_path = os.path.join(dirs['figures'],"figures-infer.pdf")
                pdf_path = os.path.join(dirs['figures'],"figures-infer%s.pdf"%str(min_diff))
            else:
            else:
                pdf_path = os.path.join(dirs['figures'],"figures-train.pdf")
                pdf_path = os.path.join(dirs['figures'],"figures-train%s.pdf"%str(min_diff))
            pp= PdfPages(pdf_path)
            pp= PdfPages(pdf_path)
        
        
        for fb_noext, fig in zip(ffiles,figs):
        for fb_noext, fig in zip(ffiles,figs):
@@ -259,8 +439,8 @@ if 'show' in FIGS_SAVESHOW:


#FIGS_ESXTENSIONS
#FIGS_ESXTENSIONS


#qsf.evaluateAllResults(result_files = files['result'],
qsf.evaluateAllResults(result_files = files['result'],
#                       absolute_disparity = ABSOLUTE_DISPARITY,
                       absolute_disparity = ABSOLUTE_DISPARITY,
#                       cluster_radius = CLUSTER_RADIUS)
                       cluster_radius = CLUSTER_RADIUS)
print("All done")
print("All done")
exit (0)
exit (0)
+79 −10
Original line number Original line Diff line number Diff line
@@ -517,6 +517,7 @@ def result_npy_prepare(npy_path, absolute, fix_nan, insert_deltas=True,labels=No
           data will be written as 4-layer tiff, extension '.npy' replaced with '.tiff'
           data will be written as 4-layer tiff, extension '.npy' replaced with '.tiff'
    @param absolute - True - the first layer contains absolute disparity, False - difference from target_disparity
    @param absolute - True - the first layer contains absolute disparity, False - difference from target_disparity
    @param fix_nan - replace nan in target_disparity with 0 to apply offset, target_disparity will still contain nan
    @param fix_nan - replace nan in target_disparity with 0 to apply offset, target_disparity will still contain nan
    @parame insert_deltas: +1 - add delta layers, +2 - add variance (max - min of this and 8 neighbors)
    """
    """
    data = np.load(npy_path) #(324,242,4) [nn_disp, target_disp,gt_disp, gt_conf]
    data = np.load(npy_path) #(324,242,4) [nn_disp, target_disp,gt_disp, gt_conf]
    if labels is None:
    if labels is None:
@@ -526,12 +527,17 @@ def result_npy_prepare(npy_path, absolute, fix_nan, insert_deltas=True,labels=No
#    target_disparity =  1     
#    target_disparity =  1     
    gt_disparity =      2     
    gt_disparity =      2     
    gt_strength =       3
    gt_strength =       3
    heur_err =          7
    min_heur_err =      0.001     
    height = data.shape[0]
    width =  data.shape[1]
    nocenter9 = np.array([[[1,1,1,1,np.nan,1,1,1,1]]], dtype = data.dtype)
    if not absolute:
    if not absolute:
        if fix_nan:
        if fix_nan:
            data[...,nn_out] +=  np.nan_to_num(data[...,1], copy=True)
            data[...,nn_out] +=  np.nan_to_num(data[...,1], copy=True)
        else:
        else:
            data[...,nn_out] +=  data[...,1]
            data[...,nn_out] +=  data[...,1]
    if insert_deltas:
    if (insert_deltas & 1):
        np.nan_to_num(data[...,gt_strength], copy=False)
        np.nan_to_num(data[...,gt_strength], copy=False)
        data = np.concatenate([data[...,0:4],data[...,0:2],data[...,0:2],data[...,4:]], axis = 2) # data[...,4:] may be empty
        data = np.concatenate([data[...,0:4],data[...,0:2],data[...,0:2],data[...,4:]], axis = 2) # data[...,4:] may be empty
        labels = labels[:4]+["nn_out","hier_out","nn_err","hier_err"]+labels[4:]
        labels = labels[:4]+["nn_out","hier_out","nn_err","hier_err"]+labels[4:]
@@ -543,6 +549,69 @@ def result_npy_prepare(npy_path, absolute, fix_nan, insert_deltas=True,labels=No
        # All other layers - mast too
        # All other layers - mast too
        for l in range(8,data.shape[2]):
        for l in range(8,data.shape[2]):
            data[...,l] = np.select([data[...,gt_strength]==0.0, data[...,gt_strength]>0.0], [np.nan,data[...,l]])
            data[...,l] = np.select([data[...,gt_strength]==0.0, data[...,gt_strength]>0.0], [np.nan,data[...,l]])
        """
        Calculate bad tiles where ggt was used as a master, to remove them from the results (later versions add random error)
        """
        bad1 =     abs(data[...,heur_err]) < min_heur_err
        bad1_ext = np.concatenate([bad1    [0:1,:], bad1    [0:1,:], bad1[:,:],     bad1    [-1:height,:], bad1    [-1:height,:]],axis = 0)
        bad1_ext = np.concatenate([bad1_ext[:,0:1], bad1_ext[:,0:1], bad1_ext[:,:], bad1_ext[:,-1:width],  bad1_ext[:,-1:width]], axis = 1)
        bad25 = np.empty(shape=[height, width, 25], dtype=bad1.dtype)
        bm25=np.array([[[1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1, 1,1,1,1,1]]])
        bm09=np.array([[[0,0,0,0,0, 0,1,1,1,0, 0,1,1,1,0, 0,1,1,1,0, 0,0,0,0,0]]])
        bm01=np.array([[[0,0,0,0,0, 0,0,0,0,0, 0,0,1,0,0, 0,0,0,0,0, 0,0,0,0,0]]])
        for row in range(5):
            for col in range(5):
                pass
                bad25  [...,row*5+col]= bad1_ext[row:height+row, col:width+col] 
            
        bad_num1=(np.sum(bad25*bm25,axis=2) > 0).astype(data.dtype)  
        bad_num2=(np.sum(bad25*bm09,axis=2) > 0).astype(data.dtype) 
        bad_num3=(np.sum(bad25*bm01,axis=2) > 0).astype(data.dtype)
        bad_num = bad_num1 + bad_num2 + bad_num3   
    if (insert_deltas & 2):
        wo = 0.7 # ortho
        wc = 0.5 #corner
        w8=np.array([wc,wo,wc,wo,0.0,wo,wc,wo,wc], dtype=data.dtype)
        w8/=np.sum(w8) #normalize
        
        gt_ext =  np.concatenate([data[0:1,:,gt_disparity],data[:,:,gt_disparity],data[-1:height,:,gt_disparity]],axis = 0)
        gt_ext =  np.concatenate([gt_ext[:,0:1],           gt_ext[:,:],           gt_ext[:,-1:width]],axis = 1)
        gs_ext =  np.concatenate([data[0:1,:,gt_strength], data[:,:,gt_strength], data[-1:height,:,gt_strength]],axis = 0)
        gs_ext =  np.concatenate([gs_ext[:,0:1],           gs_ext[:,:],           gs_ext[:,-1:width]],axis = 1)
        
        data9 =   np.empty(shape=[height, width, 9], dtype=data.dtype)
        weight9 = np.empty(shape=[height, width, 9], dtype=data.dtype)
        for row in range(3):
            for col in range(3):
                pass
                data9  [...,row*3+col]= gt_ext[row:height+row, col:width+col] 
                weight9[...,row*3+col]= gs_ext[row:height+row, col:width+col]
                
        data9 *= weight9/weight9 # make data=nan where wigth is 0         
            
#        data = np.concatenate([data[...],np.empty_like(data[...,-1])], axis = 2) # data[...,4:] may be empty
        data =        np.concatenate([data[...],np.empty(shape=[height,width,4],dtype=data.dtype)], axis = 2) # data[...,4:] may be empty
        data[...,-4] = np.nanmax(data9*nocenter9, axis=2)-np.nanmin(data9*nocenter9,axis=2)# will ignore nan
        
        np.nan_to_num(data9,copy=False) # replace all nan in data9 with 0.
        weight9 *= w8
        w_center =   np.sum(weight9, axis=2) 
        dw_center =  np.sum(data9*weight9, axis=2)
        dw_center /= w_center # now dw_center - weighted average in the center  
        
        data[...,-3] = np.abs(data[...,gt_disparity]- dw_center)
        
#        data[...,-2] = data[...,gt_disparity]- dw_center
        #data[...,-3] *= (data[...,-4] < 1.0) # just temporary
        #data[...,-3] *= (data[...,gt_disparity] < 5) #just temporary

        data[...,-2] =bad_num.astype(data.dtype)
        
        data [...,-1]= np.sum(np.nan_to_num(weight9/weight9),axis=2).astype(data.dtype)
#        data[...,-1] = dw_center
        labels +=["max-min","abs-center","badness","neibs"]
        #neib = np.concatenate([gt_ext[:height,:width,:],],axis = )
        pass
    return data, labels        
    return data, labels        


def result_npy_to_tiff(npy_path,
def result_npy_to_tiff(npy_path,
+53 −53

File changed.

Contains only whitespace changes.