Beispiel #1
0
def SSIM(original_stacked, estimated_stacked, image_size, permutation,
         X_normalized, B):
    C = np.dot(estimated_stacked, X_normalized.T)
    D = np.dot(C, B)
    (sdr, sir, sar,
     perm) = mmetrics.bss_eval_sources(np.asarray(original_stacked),
                                       estimated_stacked)
    if np.array_equal(perm, [[1], [0]]):
        permutation = True
    source1, source2 = unflatten(original_stacked, image_size)
    estimated_stacked_normalized = normalizing_for_ssim(
        estimated_stacked, D, permutation)
    estimated1, estimated2 = unflatten(estimated_stacked_normalized,
                                       image_size)

    if permutation:
        ssim1 = ssim(source1,
                     estimated2,
                     data_range=estimated2.max() - estimated2.min())
        ssim2 = ssim(source2,
                     estimated1,
                     data_range=estimated1.max() - estimated1.min())
    else:
        ssim1 = ssim(source1,
                     estimated1,
                     data_range=estimated1.max() - estimated1.min())
        ssim2 = ssim(source2,
                     estimated2,
                     data_range=estimated2.max() - estimated2.min())
    return ssim1, ssim2
Beispiel #2
0
def sigmoid_proj(S):
    
    S1 = S[0,:]
    #S2 = S[1,:]
    
    S[0,:] = 1 / (1 + np.exp(-S1))
    return S

# Here begins the algorithm
# whitening processing. It's important
R = np.dot(X, X.T)
W = la.sqrtm(np.linalg.inv(R))
X = np.dot(W, X)

(sdr_ref, sir_ref, sar, perm) = mmetrics.bss_eval_sources(np.asarray(source), np.asarray(X))
# mix = [[0.6992, 0.7275], [0.4784, 0.5548]] #or use the matrix from the paper
print("Reference SDR is: ", sdr_ref)
print("Reference SIR is: ", sir_ref)

lambda_max = 0.02
#lambda_final = lambda_max
lambda_final = 0.00002
max_it = 50
lambda_v = np.logspace(np.log10(lambda_max),np.log10(lambda_final),max_it)

#Se = np.random.randn(2, n*n) 
Se = X  
cost_it = np.zeros((1,max_it)) 
SDR_it = np.zeros((2, max_it)) 
SIR_it = np.zeros((2, max_it)) 
source2 = np.matrix(img2_gray)
source2 = source2.flatten('F')  #column wise

source1 = source1 - np.mean(source1)
source2 = source2 - np.mean(source2)

#source1 = source1/np.linalg.norm(source1)
#source2 = source2/np.linalg.norm(source2)

source = np.stack((source1, source2))
#mixing_matrix = np.random.rand(2,2)
mixing_matrix = np.array([[1, 0.5], [0.5, 1]])

X = np.matmul(source.T, mixing_matrix)
(sdr_ref, sir, sar, perm) = mmetrics.bss_eval_sources(np.asarray(source),
                                                      np.asarray(X.T))

print("Reference SDR is: ")
print(sdr_ref)

# DCT of the observation
X1 = np.reshape(X[:, 0], (n, n))
X2 = np.reshape(X[:, 1], (n, n))

X_dct1 = dct(X1)
X_dct2 = dct(X2)

X_dct = np.stack((X_dct1.flatten(), X_dct2.flatten()))

# try the sparsity separation
lambda_max = 0.02
Beispiel #4
0
    print(i + 1)
    # load and mix images
    source1, source2 = load_images(
        '../images_hard/set' + str(i + 1) + '_pic1.png',
        '../images_hard/set' + str(i + 1) + '_pic2.png',
        256,
        show_images=False)
    sources, mixed_sources = linear_mixture(
        source1, source2, mixing_matrix=mixing_matrix,
        show_images=False)  #mixing_matrix = mixing_matrix,

    # Here begins the algorithm
    # whitening processing. It's important
    X = whiten_projection(mixed_sources)
    (sdr_ref, sir_ref, sar,
     perm) = mmetrics.bss_eval_sources(np.asarray(sources), np.asarray(X))
    print('The mean value of the reference SDR is: ', np.mean(sdr_ref))
    reference_sdr.append(np.mean(sdr_ref))

    Se = np.copy(X)
    Se_old = np.copy(Se)
    for it in np.arange(max_it):
        # 1. denoising
        Se = Dic_proj_double(Se_old, num_coeff, sigma)
        # 2. get demixing matrix
        WW = get_demix(X, Se)
        # 3. whiten the demix matrix
        WW = whiten_projection(WW)
        # 4. get the new source
        Se = np.dot(WW, X)
Beispiel #5
0
X3, W3 = whiten_projection(mixture_channel_3)
B3 = np.dot(W3, mixing_matrix)"""
stacked_channel_h_mixtures = flatten(mixture1_hvs[:,:,0], mixture2_hvs[:,:,0])
stacked_channel_s_mixtures = flatten(mixture1_hvs[:,:,1], mixture2_hvs[:,:,1])
stacked_channel_v_mixtures = flatten(mixture1_hvs[:,:,2], mixture2_hvs[:,:,2])

stacked_channel_h_source = flatten(source1_hsv[:,:,0], source2_hsv[:,:,0])
stacked_channel_s_source = flatten(source1_hsv[:,:,1], source2_hsv[:,:,1])
stacked_channel_v_source = flatten(source1_hsv[:,:,2], source2_hsv[:,:,2])

mixture_channel_list_hsv = [stacked_channel_h_mixtures, stacked_channel_s_mixtures, stacked_channel_v_mixtures]
source_channel_list_hsv = [stacked_channel_h_source, stacked_channel_s_source, stacked_channel_v_source]
for i in np.arange(3):
    X_i, W_i = whiten_projection(mixture_channel_list_hsv[i])
    B_i = np.dot(W_i, mixing_matrix)
    (sdr_ref, sir_ref, sar, perm) = mmetrics.bss_eval_sources(source_channel_list_hsv[i], X_i)
    print('The mean value of the reference SDR is: ', np.mean(sdr_ref), perm)
    if np.array_equal(perm, [[1],[0]]):
        permutation = True
    
    X_normalized = normalizing_for_ssim(X_i, B_i, permutation) #used for evaluation of SSIM
    Se = np.copy(X_i) 
    Se_old = np.copy(Se)


    ssim_estimated_source1 = []
    ssim_estimated_source2 = []
    estimated_matrix = []
    mean_ssim = []

    for it in np.arange(max_it):
ref_patches1 = patchify(np.reshape(source1, image_size).T, patch_size, step)
initial_size1 = ref_patches1.shape
ref_patches1 = ref_patches1.reshape((-1, m, m))

ref_patches2 = patchify(np.reshape(source2, image_size).T, patch_size, step)
initial_size2 = ref_patches2.shape
ref_patches2 = ref_patches2.reshape((-1, m, m))

source = np.stack((source1, source2))

mixing_matrix = [[1, 0.5],[0.5, 1]] 
# X = source * mixing_matrix - The mixed images

X = np.matmul(mixing_matrix, source)
(sdr_ref, sir, sar, perm) = mmetrics.bss_eval_sources(np.asarray(source), np.asarray(X))
print("***")
print(sdr_ref)
print("***")

# reconstructing the mixed images
X1 = X[0,:]
X1 = np.reshape(X1, (n,n))

X2 = X[1,:]
X2 = np.reshape(X2, (n,n))

#extracting patches from the mixed images
mix_patches1 = patchify(X1.T, patch_size, step)
mix_patches1 = mix_patches1.reshape((-1, m, m))
Beispiel #7
0
    ref_p = np.stack((refp1.flatten(), refp2.flatten()))

    ica = FastICA(n_components=2, fun='cube')  #, whiten=False)#, fun='cube')
    source_estimated = ica.fit_transform(mix_p.T)
    mixing_estimated = ica.mixing_

    # Prepare for the permutation here
    #source_estimated = oracle_perm(ref_p, source_estimated.T)
    source_estimated = source_estimated.T

    # remove the original mean

    ref_p[0, :] = ref_p[0, :] - np.mean(ref_p[0, :])
    ref_p[1, :] = ref_p[1, :] - np.mean(ref_p[1, :])

    (sdr_ref, sir, sar, perm) = mmetrics.bss_eval_sources(ref_p, mix_p)
    (sdr, sir, sar, perm) = mmetrics.bss_eval_sources(ref_p, source_estimated)
    print(sdr_ref)
    print(sdr)
    print(perm)
    # permute here
    source_estimated = source_estimated[perm, :]
    mixing_estimated = mixing_estimated[:, perm]
    # change the sign here
    if mixing_estimated[0, 0] < 0:
        source_estimated[0, :] = -source_estimated[0, :]

    if mixing_estimated[1, 1] < 0:
        source_estimated[1, :] = -source_estimated[1, :]

    source_estimated[0, :] = n1 * source_estimated[0, :] + m1
for i in np.arange(3):
    print("Channel ", i + 1)
    source_channel_i, mixture_channel_i = linear_mixture(
        image1[:, :, i], image2[:, :, i], mixing_matrix, False)
    if i == 0:
        mixture_channel_1 = mixture_channel_i
    if i == 1:
        mixture_channel_2 = mixture_channel_i
    if i == 2:
        mixture_channel_3 = mixture_channel_i

    X_i, W_i = whiten_projection(mixture_channel_i)
    B_i = np.dot(W_i, mixing_matrix)
    (sdr_ref, sir_ref, sar,
     perm) = mmetrics.bss_eval_sources(source_channel_i, X_i)
    print('The mean value of the reference SDR is: ', np.mean(sdr_ref), perm)
    if np.array_equal(perm, [[1], [0]]):
        permutation = True

    X_normalized = normalizing_for_ssim(
        X_i, B_i, permutation)  #used for evaluation of SSIM
    Se = np.copy(X_i)
    Se_old = np.copy(Se)

    ssim_estimated_source1 = []
    ssim_estimated_source2 = []
    estimated_matrix = []
    mean_ssim = []

    for it in np.arange(max_it):
    #X[:,0] = X[:,0] - mx1
    #X[:,1] = X[:,1] - mx2

    ica = FastICA(n_components=2, fun='cube')
    source_estimated = ica.fit_transform(X)
    mixing_estimated = ica.mixing_

    ms = np.dot(np.linalg.inv(mixing_estimated), mx)

    #source_estimated[:,0] = source_estimated[:,0]+ms[0]
    #source_estimated[:,1] = source_estimated[:,1]+ms[1]

    #print("mixing matrix estimated = ", mixing_estimated)

    (sdr, sir, sar,
     perm) = mmetrics.bss_eval_sources(np.asarray(source),
                                       np.asarray(source_estimated.T))
    (sdr_ref, sir, sar,
     perm) = mmetrics.bss_eval_sources(np.asarray(source), np.asarray(X.T))

    SDR_improv[0, it] = np.mean(sdr) - np.mean(sdr_ref)
    rdr_it[0, it] = rdr_this

plt.figure
plt.subplot(211)
plt.plot(size_v, SDR_improv[0, :], '-*')
plt.xlabel('Image size')
plt.ylabel('SDR improvement (dB)')
plt.grid()
plt.show

plt.subplot(212)
all_sdr_improved = []

for i in range(15):
    print("Set = ", i + 1)
    source1, source2 = load_images('./images/set' + str(i + 1) + '_pic1.png',
                                   './images/set' + str(i + 1) + '_pic2.png',
                                   n,
                                   show_images=False)
    S, X = linear_mixture(source1,
                          source2,
                          mixing_matrix=mixing_matrix,
                          show_images=False)

    #Reference SDR
    (sdr_ref, sir_ref, sar,
     perm) = mmetrics.bss_eval_sources(np.asarray(S), np.asarray(X))
    print('The mean value of the reference SDR is: ', np.mean(sdr_ref))

    #RDC of sources
    rdc_i = rdc(S[0, :], S[1, :])
    all_rdc.append(rdc_i)
    print("rdc of sources = ", rdc_i)

    #Kurtosis of sources
    kurtosis_i = mean_kurtosis(S)
    all_kurtosis_sources.append(kurtosis_i)
    print("mean kurtosis of sources = ", kurtosis_i)
    print("kurtosis of sources  = ", kurtosis(S[0, :]), kurtosis(S[1, :]))

    #Skewness of sources
    skewness_i = mean_skewness(S)
print("rdc = ", rdc(source1.T, source2.T))
source = np.stack((source1, source2))

print('Covariance matrix is: ')
print(np.matmul(source, source.T))

# randomly generated mixing matrix
np.random.seed(0)
#mixing_matrix = np.random.rand(2,2)
mixing_matrix = np.array([[1, 0.5], [0.5, 1]])

# X = source * mixing_matrix - The mixed images

X = np.matmul(source.T, mixing_matrix)
(sdr_ref, sir, sar, perm) = mmetrics.bss_eval_sources(np.asarray(source),
                                                      np.asarray(X.T))
# mix = [[0.6992, 0.7275], [0.4784, 0.5548]] #or use the matrix from the paper
# X = np.matmul(source.T, mix)

X1 = X[:, 0]
mx1 = np.mean(X1)
X1 = np.reshape(X1, (n, n))

#print(X1.min(), X1.max(), X1.mean())

X2 = X[:, 1]
mx2 = np.mean(X2)
X2 = np.reshape(X2, (n, n))

mx = np.array([mx1, mx2])
#print(X2.min(), X2.max(), X2.mean())