print('MY loss: @'+str(iterx)+': ' + str(my_loss) + ' - Fidelity: '+str(my_fidelity)+', Neg: '+str(my_negloss)+', Pos: '+str(my_posloss)+', TV: '+str(my_tvloss)) else: sess.run([tf_lossop], feed_dict={tf_meas:np_meas}) #plt.imshow(np.abs(my_res[:,50,:])) #%% Display the results myfwd, mymeas, my_res = sess.run([tf_fwd, tf_meas, muscat.TF_obj], feed_dict={tf_meas:np_meas}) if(is_display): plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(mymeas))**.2)[:,muscat.mysize[1]//2,:]), plt.colorbar(), plt.show() if(is_display): plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(mymeas))**.2)[muscat.mysize[0]//2,:,:]), plt.colorbar(), plt.show() if(is_display): plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(mymeas))**.2)[:,:,muscat.mysize[2]//2]), plt.colorbar(), plt.show() if(is_display): plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(myfwd))**.2)[:,muscat.mysize[1]//2,:]), plt.colorbar(), plt.show() if(is_display): plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(myfwd))**.2)[muscat.mysize[0]//2,:,:]), plt.colorbar(), plt.show() if(is_display): plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(myfwd))**.2)[:,:,muscat.mysize[2]//2]), plt.colorbar(), plt.show() tf_helper.saveHDF5(np.abs(np.fft.fftshift(np.fft.fftn(myfwd))), 'FFT3D_FWD') if(is_display): plt.title('XZ'),plt.imshow(np.abs(myfwd)[:,muscat.mysize[1]//2,:]), plt.colorbar(), plt.show() if(is_display): plt.title('XZ'),plt.imshow(np.abs(myfwd)[:,:,muscat.mysize[2]//2]), plt.colorbar(), plt.show() if(is_display): plt.title('XY'),plt.imshow(np.abs(myfwd)[muscat.mysize[0]//2,:,:]), plt.colorbar(), plt.show() if(is_display): plt.title('XZ'),plt.imshow(np.angle(myfwd)[:,muscat.mysize[1]//2,:]), plt.colorbar(), plt.show() if(is_display): plt.title('XZ'),plt.imshow(np.angle(myfwd)[:,:,muscat.mysize[2]//2]), plt.colorbar(), plt.show() if(is_display): plt.title('XY'),plt.imshow(np.angle(myfwd)[muscat.mysize[0]//2,:,:]), plt.colorbar(), plt.show() my_residual = tf_helper.abssqr(myfwd-mymeas) if(is_display): plt.title('Residual: XZ'),plt.imshow(my_residual [:,muscat.mysize[1]//2,:]), plt.colorbar(), plt.show() if(is_display): plt.title('Residual: XZ'),plt.imshow(my_residual [:,:,muscat.mysize[2]//2]), plt.colorbar(), plt.show() if(is_display): plt.title('Residual: XY'),plt.imshow(my_residual [muscat.mysize[0]//2,:,:]), plt.colorbar(), plt.show()
#%% Display results # add noise myres_noise = myres + 0.005*np.random.randn(muscat.Nz,muscat.Nx,muscat.Ny) if(is_display): plt.title('XZ-PLot of the result (magn)'), plt.imshow(np.abs(myres[:,muscat.mysize[1]//2,:])), plt.colorbar(), plt.show() if(is_display): plt.title('YZ-PLot of the result (angle)'),plt.imshow(np.abs(myres[:,:,muscat.mysize[2]//2])), plt.colorbar(), plt.show() # dipslay spectrum if(is_display): plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(myres))**.2)[:,muscat.Nx//2,:]), plt.colorbar(), plt.show() if(is_display): plt.imshow(np.abs(np.fft.fftshift(np.fft.fftn(myres))**.2)[muscat.Nz//2+1,:,:]), plt.colorbar(), plt.show() if(is_display): plt.title('XZ'),plt.imshow(np.angle(myres_noise)[:,muscat.mysize[1]//2,:]), plt.colorbar(), plt.show() if(is_display): plt.title('XZ'),plt.imshow(np.angle(myres_noise)[:,:,muscat.mysize[2]//2]), plt.colorbar(), plt.show() if(is_display): plt.title('XY'),plt.imshow(np.angle(myres_noise)[muscat.mysize[0]//2,:,:]), plt.colorbar(), plt.show() # save result np.save('myres.npy', myres) np.save('myobj.npy', obj) np.save('myres_noisy.npy', myres_noise) ''' TODO: - Decaying Learning Rate - Tensorboard INtegration - Write Samples to disc - Load external data in the placeholder tf_helper.saveHDF5(results, 'Obj_Guess') muscat.sess.run(tf.assign(muscat.TF_obj, results)) '''
if (is_display): plt.imshow( np.abs(np.fft.fftshift( np.fft.fftn(myfwd))**.2)[:, muscat.mysize[1] // 2, :]), plt.colorbar(), plt.show() if (is_display): plt.imshow( np.abs(np.fft.fftshift( np.fft.fftn(myfwd))**.2)[muscat.mysize[0] // 2, :, :]), plt.colorbar(), plt.show() if (is_display): plt.imshow( np.abs(np.fft.fftshift( np.fft.fftn(myfwd))**.2)[:, :, muscat.mysize[2] // 2]), plt.colorbar(), plt.show() tf_helper.saveHDF5(np.abs(np.fft.fftshift(np.fft.fftn(myfwd))), 'FFT3D_FWD') if (is_display): plt.title('XZ'), plt.imshow( np.abs(myfwd)[:, muscat.mysize[1] // 2, :]), plt.colorbar(), plt.show() if (is_display): plt.title('XZ'), plt.imshow(np.abs(myfwd)[:, :, muscat.mysize[2] // 2]), plt.colorbar(), plt.show() if (is_display): plt.title('XY'), plt.imshow( np.abs(myfwd)[muscat.mysize[0] // 2, :, :]), plt.colorbar(), plt.show() if (is_display): plt.title('XZ'), plt.imshow(
np.abs(np_meas)[muscat.mysize[2] // 2, :, :]), plt.colorbar(), plt.show() # this is the initial guess of the reconstruction init_guess = np.angle(np_meas) - np.min(np.angle(np_meas)) init_guess = init_guess / np.max(init_guess) * muscat.dn ''' Evaluate the model ''' sess = tf.Session() sess.run(tf.global_variables_initializer()) sess.run( tf.assign(muscat.TF_obj, init_guess)) # assign abs of measurement as initial guess of #my_res = sess.run(tf_my_res, feed_dict={muscat.TF_obj:obj}) #plt.imshow(np.abs(my_res[:,50,:])) ''' TODO: - Decaying Learning Rate - Tensorboard INtegration - Write Samples to disc - Load external data in the placeholder tf_helper.saveHDF5(results, 'Obj_Guess') muscat.sess.run(tf.assign(muscat.TF_obj, results)) - Why there is this asymmetry? ''' #%% ''' Optimize the model ''' print('Start optimizing') for iterx in range(1, 1000): # try to optimize
''' write the results to disk''' mm.saveResults() if(mm.if_optimize): # feed tensor externally if(1): results = mm.allSumAmp_mes results = np.angle(results) results = results - np.min(results) results = results/np.max(results)*(mm.dn-0.01) # results = np.flip(results, 0) # plt.imshow(results[:,mm.Nx/2,:]), plt.colorbar(), plt.show() tf_helper.saveHDF5(results, 'Obj_Guess') mm.sess.run(tf.assign(mm.TF_obj, results)) else: import sys; print('sample was generated successfully') sys.exit(0) print('Start optimizing') tv_lambda = 1e-1 obj_reg_lambda = 1e6 my_learningrate =1e-4 gr_lambda = 1e-2