def test1(): mnist = input_data.read_data_sets('./data/MNIST_data/', one_hot=True) batch_size = 200 model = tf_wrap.tf_model_top([None, 784], [None, 1],\ tf_prediction_func, tf_optimize_func, tf_error_func, \ arg = batch_size, dtype = np.float32) d_loss, g_loss, dopt, gopt = model.model_wrap.optimize G, D1, D2 = model.model_wrap.prediction for i in range(1200): x, y = mnist.train.next_batch(batch_size) z = np.random.uniform(0, 1, (batch_size, 1)).astype(np.float32) loss_d, _ = model.sess.run([d_loss, dopt], { model.data: x, model.target: z }) z = np.random.uniform(0, 1, (batch_size, 1)).astype(np.float32) loss_g, _ = model.sess.run([g_loss, gopt], { model.data: x, model.target: z }) print('{}: {}\t{}'.format(i, loss_d, loss_g)) if i % 1000 == 0: getG = model.sess.run(G, { model.data: x, model.target: z }).reshape((batch_size, 28, 28)) ut.plotim3(sum(getG, 0)) model.save('../save_data/test_model_save')
def test2(): mat_contents = sio.loadmat(pathdat+'im_pca.mat')#im.mat I = np.array(mat_contents["I"].astype(np.float32)) nx, ny, nz, ndiv = I.shape #print(I.shape) imall = I.reshape([nx*ny*nz, ndiv]) npar = 7 imall = imall/np.ndarray.max(imall.flatten()) #ut.plotim3(imall.reshape(I.shape)[...,0]) #for i in range(imall.shape[0]): # tc = imall[i,:] #- np.mean(imall[i,:]) # normtc = np.linalg.norm(tc) # if normtc > 1e-3: # imall[i,:] = tc/normtc # else: # imall[i,:] = np.zeros([1,ndiv]) #imall =imall/np.ndarray.max(imall.flatten())#0.2 #for kk in range(imall.shape[1]): # imall [:,kk] = (imall[:,kk])/np.std(imall[:,kk])# - np.mean(imall[:,kk]) ut.plotim3(imall.reshape(I.shape)[...,0],[5, -1],pause_close = 1) model = tf_wrap.tf_model_top([None, ndiv], [None, npar], tf_prediction_func, tf_optimize_func, tf_error_func, arg = 1.0) model.restore(pathdat + 'test_model_save') prey = model.prediction(imall, np.zeros([imall.shape[0],npar])) immatch = prey.reshape([nx, ny, nz, npar]) ut.plotim3(immatch[...,0],[10, -1],bar = 1, pause_close = 5) ut.plotim3(immatch[...,1],[10, -1],bar = 1, pause_close = 5) ut.plotim3(immatch[...,2],[10, -1],bar = 1, pause_close = 5) ut.plotim3(immatch[...,3],[10, -1],bar = 1, pause_close = 5) ut.plotim3(immatch[...,4],[10, -1],bar = 1, pause_close = 5) sio.savemat(pathdat + 'MRF_cnn_matchtt.mat', {'immatch':immatch, 'imall':imall})
def test1(): batch_size = 12 model = tf_wrap.tf_model_top((batch_size, 1), (batch_size, 1), tf_prediction_func, tf_optimize_func, tf_error_func, arg=batch_size, dtype=np.float32) d_loss, g_loss, dopt, gopt = model.model_wrap.optimize G, D1, D2 = model.model_wrap.prediction ganinst = DATA(model.data, model.target, batch_size, G, D1, D2) for i in range(1200): x = ganinst.data.sample(batch_size).reshape( (batch_size, 1)).astype(np.float32) z = ganinst.gen.sample(batch_size).reshape( (batch_size, 1)).astype(np.float32) loss_d, _ = model.sess.run([d_loss, dopt], { model.data: x, model.target: z }) z = ganinst.gen.sample(batch_size).reshape( (batch_size, 1)).astype(np.float32) loss_g, _ = model.sess.run([g_loss, gopt], { model.data: x, model.target: z }) print('{}: {}\t{}'.format(i, loss_d, loss_g)) ganinst._plot_distributions(model.sess) model.save('../save_data/test_model_save')
def test2(): mat_contents = sio.loadmat(pathdat + 'im_pca_2fa_2freq.mat') #im.mat I = np.array(mat_contents["I"].astype(np.float32)) nx, ny, nz, ndiv = I.shape #print(I.shape) imall = I.reshape([nx * ny * nz, ndiv]) npar = 8 imall = imall / np.ndarray.max(imall.flatten()) ut.plotim3(imall.reshape(I.shape)[..., 0], [5, -1], pause_close=1) model = tf_wrap.tf_model_top([None, ndiv], [None, npar], tf_prediction_func, tf_optimize_func, tf_error_func, arg=1.0) model.restore(pathdat + 'test_model_save_b1_2fa_2freq') prey = model.prediction(imall, np.zeros([imall.shape[0], npar])) immatch = prey.reshape([nx, ny, nz, npar]) #ut.plotim3(immatch[...,0],[10, -1],bar = 1, pause_close = 5) #ut.plotim3(immatch[...,1],[10, -1],bar = 1, pause_close = 5) #ut.plotim3(immatch[...,2],[10, -1],bar = 1, pause_close = 5) #ut.plotim3(immatch[...,3],[10, -1],bar = 1, pause_close = 5) #ut.plotim3(immatch[...,4],[10, -1],bar = 1, pause_close = 5) sio.savemat(pathdat + 'MRF_cnn_matchtt_b1_2fa_2freq.mat', { 'immatch': immatch, 'imall': imall })
def test2(): mnist = input_data.read_data_sets('./data/MNIST_data/', one_hot=True) data = tf.placeholder(tf.float32, [None, 784]) target = tf.placeholder(tf.float32, [None, 10]) model = tf_wrap.tf_model_top(data, target, tf_prediction_func, tf_optimize_func, tf_error_func) model.restore('../save_data/test_model_save') model.test(mnist.test.images, mnist.test.labels)
def test2(): IMAGE_LIB = '../save_data/heart_data/2d_images/' MASK_LIB = '../save_data/heart_data/2d_masks/' IMG_HEIGHT, IMG_WIDTH = 100, 100 batch_size = 8 model = tf_wrap.tf_model_top([None, IMG_HEIGHT, IMG_WIDTH,1], [None, IMG_HEIGHT, IMG_WIDTH,1], \ tf_prediction_func_unet, tf_optimize_func, \ tf_error_func, arg = 1.0) #x_train, x_test, y_train, y_test = get_data( IMAGE_LIB, MASK_LIB, IMG_HEIGHT, IMG_WIDTH ) if 0: #get_data is 1:# os.path.isfile('/home/pcao/git/save_data/heart_data/get_data.mat') x_train, x_test, y_train, y_test, all_images = get_data( IMAGE_LIB, MASK_LIB, IMG_HEIGHT, IMG_WIDTH) sio.savemat('../save_data/heart_data/get_data.mat', \ {'x_train':x_train, 'x_test':x_test, 'y_train':y_train, 'y_test':y_test, 'all_images':all_images}) else: mat_contents = sio.loadmat('../save_data/heart_data/get_data.mat') x_train = np.array(mat_contents["x_train"]) y_train = np.array(mat_contents["y_train"]) x_test = np.array(mat_contents["x_test"]) y_test = np.array(mat_contents["y_test"]) model.restore('../save_data/test_Unet_on_heart_data') model.test(x_test, y_test) y_predict = model.prediction(x_test, np.zeros(y_test.shape)) fig, ax = plt.subplots(8, 2, figsize=(8, 4)) for i in range(8): ax[i, 0].imshow(x_test[i, :, :, 0], cmap='gray') ax[i, 1].imshow(y_predict[i, :, :, 0], cmap='gray') plt.show() sio.savemat('../save_data/heart_data/predict_data.mat', \ {'x_test':x_test, 'y_predict':y_predict, 'y_test':y_test})
def test2(): mat_contents = sio.loadmat(pathdat + 'im_pca.mat') #im.mat I = np.array(mat_contents["I"].astype(np.float32)) nx, ny, nz, ndiv = I.shape imall = I.reshape([nx * ny * nz, ndiv]) imall = imall / np.ndarray.max(imall.flatten()) #ut.plotim3(imall.reshape(I.shape)[...,0]) for i in range(imall.shape[0]): tc = imall[i, :] #- np.mean(imall[i,:]) normtc = np.linalg.norm(tc) if normtc > 5e-2: imall[i, :] = tc / normtc else: imall[i, :] = np.zeros([1, ndiv]) #imall = imall/np.ndarray.max(imall.flatten()) ut.plotim3(imall.reshape(I.shape)[..., 0]) model = tf_wrap.tf_model_top([None, 13], [None, 3], tf_prediction_func, tf_optimize_func, tf_error_func) model.restore('../save_data/test_model_save') prey = model.prediction(imall, np.zeros([imall.shape[0], 3])) immatch = prey.reshape([nx, ny, nz, 3]) ut.plotim3(immatch[..., 0], bar=1) ut.plotim3(immatch[..., 1], bar=1) ut.plotim3(immatch[..., 2], bar=1) sio.savemat(pathdat + 'MRF_cnn_matchtt.mat', { 'immatch': immatch, 'imall': imall })
def test2(): mnist = input_data.read_data_sets('./data/MNIST_data/', one_hot=True) model = tf_wrap.tf_model_top([None, 28, 28, 1], [None, 1], tf_prediction_func, tf_optimize_func, tf_error_func) batch_z = np.random.uniform(0, 1, (mnist.test.images.shape[0], )).astype( np.float32) model.restore('../save_data/test_model_save') model.test({'image': mnist.test.images, 'z': batch_z}, None)
def test2(): mnist = input_data.read_data_sets('./data/MNIST_data/', one_hot=True) model = tf_wrap.tf_model_top([None, 784], [None, 784], tf_prediction_func, tf_optimize_func, tf_error_func, arg=1.0) model.restore('../save_data/test_model_save') model.test(mnist.test.images, mnist.test.images)
def test1(): mnist = input_data.read_data_sets('./data/MNIST_data/', one_hot=True) model = tf_wrap.tf_model_top([None, 784], [None, 10], tf_prediction_func, tf_optimize_func, tf_error_func, arg = 0.5) for _ in range(100): model.test(mnist.test.images, mnist.test.labels) for _ in range(100): batch_xs, batch_ys = mnist.train.next_batch(1000) model.train(batch_xs, batch_ys) model.save('../save_data/test_model_save')
def test1(): mnist = input_data.read_data_sets('./data/MNIST_data/', one_hot=True) data = tf.placeholder(tf.float32, [None, 784]) target = tf.placeholder(tf.float32, [None, 10]) model = tf_wrap.tf_model_top(data, target, tf_prediction_func, tf_optimize_func, tf_error_func) for _ in range(100): model.test(mnist.test.images, mnist.test.labels) for _ in range(100): batch_xs, batch_ys = mnist.train.next_batch(1000) model.train(batch_xs, batch_ys) model.save('../save_data/test_model_save')
def test1(): Nk = 960 #far.shape[0] model = tf_wrap.tf_model_top([None, 2 * Nk], [None, 4], tf_prediction_func, tf_optimize_func, tf_error_func, 0.5) batch_size = 800 # generate far and trr far_amp = np.random.uniform(0, 15.0 / 180.0 * np.pi, (Nk, )) far_phase = np.random.uniform(-np.pi, np.pi, (Nk, )) far = np.multiply(far_amp, np.exp(far_phase)).astype(np.complex128).squeeze() trr = np.random.uniform(3.0, 16.0, (Nk, )).astype(np.float64).squeeze() # prepare for sequence simulation, y->x_hat ti = 10 #ms M0 = np.array([0.0, 0.0, 1.0]).astype(np.float64) #run tensorflow on cpu, count of gpu = 0 config = tf.ConfigProto() #(device_count = {'GPU': 0}) #allow tensorflow release gpu memory config.gpu_options.allow_growth = True Nite = 2000 #run for 2000 for i in range(Nite): batch_ys = np.random.uniform(0, 1, (batch_size, 4)).astype(np.float64) #batch_ys[:,2] = np.zeros(batch_size) batch_ys[:, 3] = np.ones(batch_size) batch_xs = np.zeros((batch_size, 2 * Nk), dtype=np.float64) batch_xs_c = np.zeros((batch_size, Nk), dtype=np.complex128) # intial seq simulation with t1t2b0 values #seq_data = ssad.irssfp_arrayin_data( batch_size, Nk ).set( batch_ys ) T1r, T2r, dfr, PDr = ssmrf.set_par(batch_ys) batch_xs_c[...,0:Nk] = ssmrf.bloch_sim_batch_cuda( batch_size, 100, Nk, PDr,\ T1r, T2r, dfr, M0, trr, far, ti ) #seperate real/imag parts or abs/angle parts, no noise output batch_xs[:, 0:Nk] = np.real(batch_xs_c) batch_xs[:, Nk:2 * Nk] = np.imag(batch_xs_c) #input with noise batch_xsnoise = batch_xs + np.random.uniform(-0.1, 0.1, (batch_size, 2 * Nk)) #train_step.run(feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 0.5}) model.train(batch_xsnoise, batch_ys) if i % 10 == 0: model.test(batch_xsnoise, batch_ys) if i % 1000 == 0 or i >= (Nite - 1): model.save('../save_data/MRF_encoder_t1t2b0') sio.savemat('../save_data/MRF_far_trr.mat', { 'far': far, 'trr': trr })
def test1(): # need to download data from https://www.kaggle.com/kmader/siim-medical-image-analysis-tutorial IMAGE_LIB = '../save_data/heart_data/2d_images/' MASK_LIB = '../save_data/heart_data/2d_masks/' IMG_HEIGHT, IMG_WIDTH = 100, 100 batch_size = 8 model = tf_wrap.tf_model_top([None, IMG_HEIGHT, IMG_WIDTH,1], [None, IMG_HEIGHT, IMG_WIDTH,1], \ tf_prediction_func_cnn, tf_optimize_func, \ tf_error_func, arg = 0.5) if 0: #get_data is 1:# os.path.isfile('/home/pcao/git/save_data/heart_data/get_data.mat') x_train, x_test, y_train, y_test, all_images = get_data( IMAGE_LIB, MASK_LIB, IMG_HEIGHT, IMG_WIDTH) sio.savemat('../save_data/heart_data/get_data.mat', \ {'x_train':x_train, 'x_test':x_test, 'y_train':y_train, 'y_test':y_test, 'all_images':all_images}) else: mat_contents = sio.loadmat('../save_data/heart_data/get_data.mat') x_train = np.array(mat_contents["x_train"]) y_train = np.array(mat_contents["y_train"]) x_test = np.array(mat_contents["x_test"]) y_test = np.array(mat_contents["y_test"]) print('training data size %g; testing data size %g' % (x_train.shape[0], x_test.shape[0])) for i in range(2000): for _ in range(100): image_batch, mask_batch = next( my_generator(x_train, y_train, batch_size)) #fix, ax = plt.subplots(8,2, figsize=(batch_size,20)) #for i in range(8): # ax[i,0].imshow(image_batch[i,:,:,0]) # ax[i,1].imshow(mask_batch[i,:,:,0]) #plt.show() #image_batch = image_batch + np.random.ranf(1)[0]*np.random.uniform(-0.1,0.1,(image_batch.shape)) model.train(image_batch, mask_batch) model.test(x_test, y_test) if i % 2 == 0: y_predict = model.prediction(x_test, np.zeros(y_test.shape)) #fig, ax = plt.subplots(8,2, figsize = (8,4)) #for i in range(8): # ax[i,0].imshow(x_test[i,:,:,0], cmap='gray') # ax[i,1].imshow(y_predict[i,:,:,0], cmap='gray') #plt.show() ut.compare_plot(x_test[..., 0], y_predict[..., 0], 8, pause_close=1) model.save('../save_data/test_Unet_on_heart_data_fcn_dropout') sio.savemat('../save_data/heart_data/predict_data_fcn_dropout.mat', \ {'x_test':x_test, 'y_predict':y_predict, 'y_test':y_test})
def test1(): model = tf_wrap.tf_model_top([None, 2*960], [None, 4], tf_prediction_func, tf_optimize_func, tf_error_func) batch_size = 800 # load far and trr # read rf and tr arrays from mat file mat_contents = sio.loadmat(pathdat+'mrf_t1t2b0pd_mrf_randphasecyc_traintest.mat'); far = np.array(mat_contents["rf"].astype(np.complex128).squeeze()) trr = np.array(mat_contents["trr"].astype(np.float64).squeeze()) # prepare for sequence simulation, y->x_hat Nk = far.shape[0] ti = 10 #ms M0 = np.array([0.0,0.0,1.0]).astype(np.float64) #run tensorflow on cpu, count of gpu = 0 config = tf.ConfigProto()#(device_count = {'GPU': 0}) #allow tensorflow release gpu memory config.gpu_options.allow_growth=True #run for 2000 for i in range(2000): batch_ys = np.random.uniform(0,1,(batch_size,4)).astype(np.float64) #batch_ys[:,2] = np.zeros(batch_size) #batch_ys[:,3] = np.ones(batch_size) batch_xs = np.zeros((batch_size,2*Nk), dtype = np.float64) # intial seq simulation with t1t2b0 values #seq_data = ssad.irssfp_arrayin_data( batch_size, Nk ).set( batch_ys ) T1r, T2r, dfr, PDr = ssmrf.set_par(batch_ys) batch_xs_c = ssmrf.bloch_sim_batch_cuda( batch_size, 100, Nk, PDr,\ T1r, T2r, dfr, M0, trr, far, ti ) #seperate real/imag parts or abs/angle parts, no noise output batch_xs[:,0:Nk] = np.real(batch_xs_c) batch_xs[:,Nk:2*Nk] = np.imag(batch_xs_c) #input with noise #batch_xsnoise = batch_xs + np.random.uniform(-0.01,0.01,(batch_size,2*Nk)) #train_step.run(feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 0.5}) model.train(batch_xs, batch_ys) if i%10 == 0: prey = model.prediction(batch_xs,np.zeros(batch_ys.shape)) ut.plot(prey[...,0], batch_ys[...,0], line_type = '.', pause_close = 1) ut.plot(prey[...,1], batch_ys[...,1], line_type = '.', pause_close = 1) ut.plot(prey[...,2], batch_ys[...,2], line_type = '.', pause_close = 1) ut.plot(prey[...,3], batch_ys[...,3], line_type = '.', pause_close = 1) model.test(batch_xs, batch_ys) if i%100 == 0: model.save('../save_data/MRF_encoder_t1t2b0')
def test4(): # read rf and tr arrays from mat file mat_contents = sio.loadmat('../save_data/MRF_far_trr.mat'); far = np.array(mat_contents["far"].astype(np.complex128).squeeze()) trr = np.array(mat_contents["trr"].astype(np.float64).squeeze()) Nk = far.shape[0]#960# model = tf_wrap.tf_model_top( [None, 2 * Nk], [None, 4], tf_prediction_func, tf_optimize_func, tf_error_func, 1.0) model.restore('../save_data/MRF_encoder_t1t2b0') mat_contents2 = sio.loadmat(pathdat+'test_outy.mat'); data_y = mat_contents2["test_outy"] batch_size = data_y.shape[0] # load far and trr # prepare for sequence simulation, y->x_hat ti = 10 #ms M0 = np.array([0.0,0.0,1.0]).astype(np.float64) #run tensorflow on cpu, count of gpu = 0 config = tf.ConfigProto()#(device_count = {'GPU': 0}) #allow tensorflow release gpu memory config.gpu_options.allow_growth=True batch_ys = data_y#np.random.uniform(0,1,(batch_size,4)).astype(np.float64) #batch_ys[:,2] = np.zeros(batch_size) #batch_ys[:,3] = np.ones(batch_size) batch_xs = np.zeros((batch_size, 2 * Nk), dtype = np.float64) batch_xs_c = np.zeros((batch_size, Nk), dtype = np.complex128) # intial seq simulation with t1t2b0 values #seq_data = ssad.irssfp_arrayin_data( batch_size, Nk ).set( batch_ys ) T1r, T2r, dfr, PDr = ssmrf.set_par(batch_ys) batch_xs_c[...,0:Nk] = ssmrf.bloch_sim_batch_cuda( batch_size, 100, Nk, PDr,\ T1r, T2r, dfr, M0, trr, far, ti ) #seperate real/imag parts or abs/angle parts, no noise output batch_xs[:,0:Nk] = np.real(batch_xs_c) batch_xs[:,Nk:2*Nk] = np.imag(batch_xs_c) #input with noise #batch_xsnoise = batch_xs #+ np.random.uniform(-0.05,0.05,(batch_size,2*Nk)) prey = model.prediction(batch_xs,np.zeros(batch_ys.shape)) model.test(batch_xs, batch_ys) ut.plot(prey[...,0], batch_ys[...,0], line_type = '.') ut.plot(prey[...,1], batch_ys[...,1], line_type = '.') ut.plot(prey[...,2], batch_ys[...,2], line_type = '.') ut.plot(prey[...,3], batch_ys[...,3], line_type = '.') sio.savemat(pathdat+'out_cnn_testdata3.mat', {'testim_outy': prey})
def test3(): # read rf and tr arrays from mat file mat_contents = sio.loadmat('../save_data/MRF_far_trr.mat'); far = np.array(mat_contents["far"].astype(np.complex128).squeeze()) Nk = far.shape[0]#960# model = tf_wrap.tf_model_top( [None, 2 * Nk], [None, 4], tf_prediction_func, tf_optimize_func, tf_error_func, 1.0) model.restore('../save_data/MRF_encoder_t1t2b0') mat_contents2 = sio.loadmat(pathdat+'datax3.mat'); data_x = mat_contents2["datax3"] batch_size = data_x.shape[0]#800 batch_ys = np.zeros((batch_size,4), dtype = np.float64) prey = model.prediction(data_x,np.zeros(batch_ys.shape)) sio.savemat(pathdat+'out_cnn_testdata3.mat', {'testim_outy': prey})
def test2(): mat_contents = sio.loadmat(pathdat + 'im_pca.mat') #im.mat I = np.array(mat_contents["I"])[:, :, 25:45, :] if len(I.shape) == 3: nx, ny, ndiv = I.shape nz = 1 elif len(I.shape) == 4: nx, ny, nz, ndiv = I.shape imall = I.reshape([nx * ny * nz, ndiv]) npar = 4 abs_flag = 0 #apply pca on absolute time course, else compute absolute value after pca if abs_flag is 0: imall = np.absolute(imall).astype(np.float32) else: imall = imall.astype(np.float32) #imall = imall/np.ndarray.max(imall.flatten()) #ut.plotim3(imall.reshape(I.shape)[...,0]) #for i in range(imall.shape[0]): # tc = imall[i,:] #- np.mean(imall[i,:]) # normtc = np.linalg.norm(tc) # if normtc > 1e-3: # imall[i,:] = tc/normtc # else: # imall[i,:] = np.zeros([1,ndiv]) imall = 1000.0 * imall / np.ndarray.max(imall.flatten()) #0.2 ut.plotim3(imall.reshape(I.shape)[..., 0], [10, 6], pause_close=1) model = tf_wrap.tf_model_top([None, ndiv], [None, npar], tf_prediction_func, tf_optimize_func, tf_error_func) model.restore(pathdat + 'test_model_savecnn') prey = model.prediction(imall, np.zeros([imall.shape[0], npar])) immatch = prey.reshape([nx, ny, nz, npar]) ut.plotim3(immatch[..., 0], [10, 6], bar=1, pause_close=5) ut.plotim3(immatch[..., 1], [10, 6], bar=1, pause_close=5) ut.plotim3(immatch[..., 2], [10, 6], bar=1, pause_close=5) ut.plotim3(immatch[..., 3], [10, 6], bar=1, pause_close=5) sio.savemat(pathdat + 'MRF_cnn_matchttt.mat', { 'immatch': immatch, 'imall': imall })
def test3(): mat_contents = sio.loadmat(pathdat + 'dict_pca.mat') dictall = np.array(mat_contents["avedictall"].astype(np.float32)) label = np.array(mat_contents["dict_label"].astype(np.float32)) #dictall = dictall/np.ndarray.max(dictall.flatten()) for i in range(dictall.shape[0]): tc = dictall[i, :] - np.mean(dictall[i, :]) dictall[i, :] = tc / np.linalg.norm(tc) dictall = 1000 * dictall / np.ndarray.max(dictall.flatten()) model = tf_wrap.tf_model_top([None, 13], [None, 3], tf_prediction_func, tf_optimize_func, tf_error_func) model.restore('../save_data/test_model_save') model.test(dictall, label) prey = model.prediction(dictall, np.zeros(label.shape)) ut.plot(prey[..., 0], label[..., 0], line_type='.') ut.plot(prey[..., 1], label[..., 1], line_type='.') ut.plot(prey[..., 2], label[..., 2], line_type='.')
def test1(): mnist = input_data.read_data_sets('./data/MNIST_data/', one_hot=True) model = tf_wrap.tf_model_top([None, 28, 28, 1], [None, 1], tf_prediction_func, tf_optimize_func, tf_error_func) for _ in range(100): model.test(mnist.test.images, mnist.test.images) for _ in range(100): batch_size = 1000 batch_xs, batch_ys = mnist.train.next_batch(batch_size) batch_z = np.random.uniform(0, 1, (batch_size, )).astype(np.float32) model.set_arg('train_D').train({ 'image': batch_image, 'z': batch_z }, None) model.set_arg('train_G').train({ 'image': batch_image, 'z': batch_z }, None) model.save('../save_data/test_model_save')
def test1(): mat_contents = sio.loadmat(pathdat + 'dict_pca.mat') #dict_pca dictall = np.array(mat_contents["avedictall"].astype(np.float32)) label = np.array(mat_contents["dict_label"].astype(np.float32)) batch_size = 2000 #dictall = dictall/np.ndarray.max(dictall.flatten()) for i in range(dictall.shape[0]): tc = dictall[i, :] #- np.mean(dictall[i,:]) dictall[i, :] = tc / np.linalg.norm(tc) #dictall = dictall/np.ndarray.max(dictall.flatten()) #data = tf.placeholder(tf.float32, [None, 13]) #target = tf.placeholder(tf.float32, [None, 3]) model = tf_wrap.tf_model_top([None, 13], [None, 3], tf_prediction_func, tf_optimize_func, tf_error_func) for i in range(1000000): batch_start = (i * batch_size) % dictall.shape[0] batch_stop = batch_start + batch_size batch_xs = dictall[batch_start:batch_stop, :] batch_ys = label[batch_start:batch_stop, :] #batch_ys[:,2] = np.random.ranf(1)[0] batch_xs = batch_xs * np.random.ranf(1)[0] # batch_ys[0,2] #* #batch_xsnoise = batch_xs + np.random.ranf(1)[0]*np.random.uniform(-0.05,0.05,(batch_xs.shape)) #ut.plot(batch_xs[0,:].squeeze()) model.train(batch_xs, batch_ys) model.test(batch_xs, batch_ys) #if i % 1000 == 0: #prey = model.prediction(batch_xs,np.zeros(batch_ys.shape)) #ut.plot(prey[...,0], batch_ys[...,0], line_type = '.') #ut.plot(prey[...,1], batch_ys[...,1], line_type = '.') #ut.plot(prey[...,2], batch_ys[...,2], line_type = '.') if i % 1000 == 0: model.save('../save_data/test_model_save')
def test1(): mat_contents = sio.loadmat(pathdat+'mrf_t1t2b0pd_mrf_randphasecyc_traintest.mat'); far = np.array(mat_contents["rf"].astype(np.complex128).squeeze()) trr = np.array(mat_contents["trr"].astype(np.float64).squeeze()) Nk = far.shape[0]#960# model = tf_wrap.tf_model_top( [None, 2 * Nk], [None, 4], tf_prediction_func, tf_optimize_func, tf_error_func, 0.5) batch_size = 800 # generate far and trr #far_amp = np.random.uniform(0, 15.0/180.0 * np.pi, (Nk,)) #far_phase = np.random.uniform(-np.pi, np.pi, (Nk,)) #far = np.multiply(far_amp, np.exp(far_phase)).astype(np.complex128).squeeze() #trr = np.random.uniform(3.0, 16.0, (Nk,)).astype(np.float64).squeeze() #far, trr = simut.rftr_const(Nk, 15.0, 4.0) #far,trr = simut.rftr_rand(Nk, fa, 3, 16) # prepare for sequence simulation, y->x_hat ti = 10 #ms M0 = np.array([0.0,0.0,1.0]).astype(np.float64) #run tensorflow on cpu, count of gpu = 0 config = tf.ConfigProto()#(device_count = {'GPU': 0}) #allow tensorflow release gpu memory config.gpu_options.allow_growth=True Nite = 200000 #run for 2000 for i in range(Nite): batch_ys = np.random.uniform(0,1,(batch_size,4)).astype(np.float64) #batch_ys = np.random.randint(1,10,(batch_size,4)).astype(np.float64)/10.0 #batch_ys[:,2] = np.random.uniform(0,0.2,(batch_size)).astype(np.float64)#np.zeros(batch_size) #batch_ys[:,3] = np.ones(batch_size) batch_xs = np.zeros((batch_size,2 * Nk), dtype = np.float64) batch_xs_c = np.zeros((batch_size, Nk), dtype = np.complex128) # intial seq simulation with t1t2b0 values #seq_data = ssad.irssfp_arrayin_data( batch_size, Nk ).set( batch_ys ) T1r, T2r, dfr, PDr = ssmrf.set_par(batch_ys) batch_xs_c[...,0:Nk] = ssmrf.bloch_sim_batch_cuda( batch_size, 100, Nk, PDr,\ T1r, T2r, dfr, M0, trr, far, ti ) #seperate real/imag parts or abs/angle parts, no noise output batch_xs[:,0:Nk] = np.real(batch_xs_c) batch_xs[:,Nk:2*Nk] = np.imag(batch_xs_c) #input with noise batch_xsnoise = batch_xs + np.random.ranf(1)[0]*np.random.uniform(-0.4,0.4,(batch_size,2*Nk)) model.train(batch_xsnoise, batch_ys) if i%10 == 0: model.test(batch_xsnoise, batch_ys) if i%100 == 0 or i >= (Nite - 1): model.save('../save_data/MRF_encoder_t1t2b0') sio.savemat('../save_data/MRF_far_trr.mat', {'far':far, 'trr':trr}) if i % 100 == 0: prey = model.prediction(batch_xs,np.zeros(batch_ys.shape)) ut.plot(prey[...,0], batch_ys[...,0], line_type = '.', pause_close = 1) ut.plot(prey[...,1], batch_ys[...,1], line_type = '.', pause_close = 1) ut.plot(prey[...,2], batch_ys[...,2], line_type = '.', pause_close = 1) ut.plot(prey[...,3], batch_ys[...,3], line_type = '.', pause_close = 1)
def test2(): # read rf and tr arrays from mat file mat_contents = sio.loadmat(pathdat+'mrf_t1t2b0pd_mrf_randphasecyc_traintest.mat'); far = np.array(mat_contents["rf"].astype(np.complex128).squeeze()) trr = np.array(mat_contents["trr"].astype(np.float64).squeeze()) # input MRF time courses mat_contents2 = sio.loadmat(pathdat+'datax1.mat'); data_x = np.array(mat_contents2["datax1"]).astype(np.float64) # prepare for sequence simulation, y->x_hat Nk = far.shape[0] Nexample = data_x.shape[0] ti = 10 #ms M0 = np.array([0.0,0.0,1.0]).astype(np.float64) #image size nx = 217 ny = 181 # mask in ksp mask = ut.mask3d( nx, ny, Nk, [15,15,0], 0.4) #FTm = opts.FFT2d_kmask(mask) FTm = opts.FFT2d_kmask(mask) #intial timing timing = utc.timing() # x real and imag parts should be combined data_x_c = np.zeros((Nexample, Nk),np.complex128) data_x_c = ssmrf.seqdata_realimag_2complex(data_x) #could do mask M here, data_x_c = M*data_x_c # intial tmp data test_outy0 = np.zeros((Nexample,4),dtype=np.float64) test_outy = np.zeros((Nexample,4),dtype=np.float64) tmptest_outy = np.zeros((Nexample,4),dtype=np.float64) acctest_outy = np.zeros((Nexample,4),dtype=np.float64) # f'(y), cnn model timing.start() #ssmrf.batch_apply_tf_cuda( Nexample, ny, sess.run, x, data_x, y_conv, test_outy0, keep_prob ) model = tf_wrap.tf_model_top([None, 2*960], [None, 4], tf_prediction_func, tf_optimize_func, tf_error_func) mat_contents3 = sio.loadmat(pathdat+'test_outy.mat'); test_outy = np.array(mat_contents3["test_outy"]).astype(np.float64) T1r, T2r, dfr, PDr = ssmrf.set_par((test_outy) ) data_x_c = ssmrf.bloch_sim_batch_cuda( Nexample, 7*ny, Nk, PDr, T1r, T2r, dfr, M0, trr, far, ti ) data_x_c = mask_ksp3d(nx, ny, Nk, FTm, data_x_c) data_x = ssmrf.seqdata_complex_2realimag(data_x_c) data_x0 = data_x model.restore('../save_data/MRF_encoder_t1t2b0') test_outy0 = model.prediction(data_x, np.zeros(test_outy0.shape)) timing.stop().display('model first estimation ') test_outy0 = constraints( test_outy0 ) acctest_outy = test_outy0 # for loop start here for _ in range(40): #cuda simulate bloch for each pixel T1r, T2r, dfr, PDr = ssmrf.set_par((test_outy) ) timing.start() data_x_c = ssmrf.bloch_sim_batch_cuda( Nexample, 7*ny, Nk, PDr, T1r, T2r, dfr, M0, trr, far, ti ) timing.stop().display('Bloch sim in loop ') #0.5* d||M*f(p)-b||_2^2/dp = f'*(M*f(p)-b) = f'(M*f(p)) - f'(b) # could do mask, M, here, e.g. data_x_c=M*data_x_c data_x_c = mask_ksp3d(nx, ny, Nk, FTm, data_x_c) #seperate real/imag parts or abs/angle parts data_x = ssmrf.seqdata_complex_2realimag(data_x_c) # apply CNN model, f'(i) #ssmrf.batch_apply_tf_cuda( Nexample, ny, sess.run, x, data_x, y_conv, tmptest_outy, keep_prob ) tmptest_outy = model.prediction(data_x, np.zeros(tmptest_outy.shape)) tmptest_outy = constraints( tmptest_outy ) # gradient = f'(M * f(p)) - f'(b) tmptest_outy = tmptest_outy - test_outy0 #+ 0.2*test_outy # gradient descent for test_outy acctest_outy = acctest_outy - tmptest_outy print('gradient 0.5* d||f(p)-b||_2^2/dp: %g' % np.linalg.norm(tmptest_outy)) #print('test_outy: %g' % np.linalg.norm(test_outy)) acctest_outy = constraints( acctest_outy ) #could do soft thresholding on test_outy here, i.e. test_outy = threshold(test_outy) #test_outy = l0_par(nx, ny, acctest_outy) # test_outy = tv_par(nx, ny, acctest_outy) sio.savemat(pathdat +'cnn_cs_testouty.mat', {'test_outy': test_outy})
def test1(): mat_contents = sio.loadmat(pathdat + 'dict_pca_2fa_2freq.mat') #dict_pca coeff = np.array(mat_contents["coeff"].astype(np.float32)) par = mat_contents["par"] batch_size = 800 Nk = par[0]['irfreq'][0][0][0] #892#far.shape[0]#par.irfreq# Ndiv = coeff.shape[1] #par[0]['ndiv'][0][0][0]#16 Nscan = 3 orig_Ndiv = coeff.shape[0] // Nscan npar = 8 ## model = tf_wrap.tf_model_top([None, Ndiv], [None, npar], tf_prediction_func, tf_optimize_func, tf_error_func, arg=0.5) #model.restore(pathdat + 'test_model_save_2fa_2freq') fa1 = 45.0 #par[0]['fa1'][0][0][0].astype(np.float32)#35#30 #deg fa2 = 30.0 #par[0]['fa2'][0][0][0].astype(np.float32) tr = par[0]['tr'][0][0][0].astype(np.float32) #3.932#4.337 #ms ti = par[0]['ti'][0][0][0].astype(np.float32) #11.0 #ms te = par[0]['te'][0][0][0].astype(np.float32) #1.5 #ms far, trr, ter = simut.rftr_const(Nk, 1.0, tr, te) M0 = simut.def_M0() #run tensorflow on cpu, count of gpu = 0 config = tf.ConfigProto() #(device_count = {'GPU': 0}) #allow tensorflow release gpu memory config.gpu_options.allow_growth = True t1t2_group = np.zeros((4, 2), dtype=np.float64) t1t2_group[0, 0] = 600.0 / 5000.0 #600 #0.12 t1t2_group[0, 1] = 40.0 / 500.0 #40 #0.08 t1t2_group[1, 0] = 1000.0 / 5000.0 #1000 #0.2 t1t2_group[1, 1] = 80.0 / 500.0 #80 #0.16 t1t2_group[2, 0] = 3000.0 / 5000.0 #3000 #0.6 t1t2_group[2, 1] = 200.0 / 500.0 #200 #0.4 t1t2_group[3, 0] = 0.0 / 5000.0 t1t2_group[3, 1] = 0.0 / 500.0 for i in range(1000000): batch_ys = np.random.uniform(0, 1, (batch_size, npar)).astype(np.float64) batch_ys[:, 2] = np.random.uniform(0, 1.0 / tr, (batch_size)).astype( np.float64) #np.zeros(batch_size)# batch_ys_tmp = np.random.uniform(0, 4, (batch_size)) for k in range(batch_size): if batch_ys_tmp[k] <= 4 and batch_ys_tmp[k] > 3: batch_ys[k, 0] = t1t2_group[0, 0] + np.random.uniform( -0.05, 0.025) #0.07 to 0.145 batch_ys[k, 1] = t1t2_group[0, 1] + np.random.uniform( -0.05, 0.025) #0.04 to 0.105 batch_ys[k, 4] = 1.0 batch_ys[k, 5] = 0.0 batch_ys[k, 6] = 0.0 elif batch_ys_tmp[k] <= 3 and batch_ys_tmp[k] > 2: batch_ys[k, 0] = t1t2_group[1, 0] + np.random.uniform( -0.035, 0.035) #-0.035,0.035 #0.145 to 0.255 batch_ys[k, 1] = t1t2_group[1, 1] + np.random.uniform( -0.035, 0.035) #0.105 to 0.215 batch_ys[k, 4] = 0.0 batch_ys[k, 5] = 1.0 batch_ys[k, 6] = 0.0 elif batch_ys_tmp[k] <= 2 and batch_ys_tmp[k] > 1: batch_ys[k, 0] = t1t2_group[2, 0] + np.random.uniform( -0.15, 0.25) #-0.15,0.25 #0.45 to 0.85 batch_ys[k, 1] = t1t2_group[2, 1] + np.random.uniform( -0.15, 0.25) #0.55 to 0.65 batch_ys[k, 4] = 0.0 batch_ys[k, 5] = 0.0 batch_ys[k, 6] = 1.0 else: batch_ys[k, 0] = t1t2_group[3, 0] #+ np.random.uniform(-0.0,0.02) batch_ys[k, 1] = t1t2_group[3, 1] #+ np.random.uniform(-0.0,0.08) batch_ys[k, 4] = 0.0 batch_ys[k, 5] = 0.0 batch_ys[k, 6] = 0.0 T1r, T2r, dfr, PDr = ssmrf.set_par(batch_ys[..., 0:4]) b1r = 2.0 * batch_ys[..., 7] + 0.5 batch_xs_c1 = ssmrf.bloch_sim_batch_cuda3(batch_size, 100, Nk, PDr, T1r, T2r, dfr, b1r, M0, trr, ter, fa1 * far, ti) batch_xs_c2 = ssmrf.bloch_sim_batch_cuda3(batch_size, 100, Nk, PDr, T1r, T2r, dfr, b1r, M0, trr, ter, fa2 * far, ti) #batch_xs_c3 = ssmrf.bloch_sim_batch_cuda2( batch_size, 100, Nk, PDr, T1r, T2r, dfr, M0, trr, ter, np.absolute(fa2*far), ti ) batch_xs_c3 = ssmrf.bloch_sim_batch_cuda3(batch_size, 100, Nk, PDr, T1r, T2r, dfr + 107.0, b1r, M0, trr, ter, fa2 * far, ti) #ut.plot(np.absolute(batch_xs_c[0,:])) batch_xs = np.zeros((batch_size, Nscan * orig_Ndiv), dtype=batch_xs_c1.dtype) if orig_Ndiv is not Nk: batch_xs[:, 0:orig_Ndiv] = ( simut.average_dict(batch_xs_c1, orig_Ndiv) ) #(np.dot(np.absolute(simut.average_dict(batch_xs_c, Ndiv)), coeff)) batch_xs[:, orig_Ndiv:2 * orig_Ndiv] = ( simut.average_dict(batch_xs_c2, orig_Ndiv) ) #(np.dot(np.absolute(simut.average_dict(batch_xs_c, Ndiv)), coeff)) batch_xs[:, 2 * orig_Ndiv:3 * orig_Ndiv] = ( simut.average_dict(batch_xs_c3, orig_Ndiv) ) #(np.dot(np.absolute(simut.average_dict(batch_xs_c, Ndiv)), coeff)) batch_xs = batch_xs + np.random.ranf(1)[0] * np.random.uniform( -0.002, 0.002, (batch_xs.shape)) batch_xs = np.absolute(batch_xs) if 1: batch_xs = np.dot(batch_xs, coeff) else: batch_xs = batch_xs for dd in range(batch_xs.shape[0]): tc1 = batch_xs[dd, :] normtc1 = np.linalg.norm(tc1) if normtc1 > 0.01: batch_xs[dd, :] = tc1 else: batch_ys[dd, :] = np.zeros([1, npar]) batch_xs = batch_xs / np.ndarray.max(batch_xs.flatten()) model.test(batch_xs, batch_ys) model.train(batch_xs, batch_ys) if i % 100 == 0: prey = model.prediction(batch_xs, np.zeros(batch_ys.shape)) for nn in range(npar): ut.plot(prey[..., nn], batch_ys[..., nn], line_type='.', pause_close=1) model.save(pathdat + 'test_model_save_b1_2fa_2freq')
def test1(): mat_contents = sio.loadmat(pathdat + 'dict_pca.mat') #dict_pca #dictall = np.array(mat_contents["avedictall"].astype(np.float32)) #label = np.array(mat_contents["dict_label"].astype(np.float32)) par = mat_contents["par"] read_coeff_flag = 1 # 0 not use coeff; 1 read coeff from mat file; 2 generate coeff by pca abs_flag = 0 #apply pca on absolute time course batch_size = 800 Nk = par[0]['irfreq'][0][0][0] #892#far.shape[0]#par.irfreq# if read_coeff_flag is 1: coeff = np.array(mat_contents["coeff"].astype(np.float32)) Ndiv = coeff.shape[1] #Nk #par[0]['ndiv'][0][0][0]#16 orig_Ndiv = coeff.shape[0] #Nk elif read_coeff_flag is 2: Ndiv = 20 #20 pca orig_Ndiv = Nk #coeff.shape[0]#Nk else: Ndiv = Nk #coeff.shape[1]#Nk #par[0]['ndiv'][0][0][0]#16 orig_Ndiv = Nk #coeff.shape[0]#Nk npar = 4 model = tf_wrap.tf_model_top([None, Ndiv], [None, npar], tf_prediction_func, tf_optimize_func, tf_error_func) fa = par[0]['fa'][0][0][0].astype(np.float32) #35#30 #deg tr = par[0]['tr'][0][0][0].astype(np.float32) #3.932#4.337 #ms ti = par[0]['ti'][0][0][0].astype(np.float32) #11.0 #ms #print(fa) #print(tr) #print(ti) #print(Nk) #print(Ndiv) far, trr = simut.rftr_const(Nk, fa, tr) #far,trr = simut.rftr_rand(Nk, fa, tr, 2*tr) M0 = simut.def_M0() #run tensorflow on cpu, count of gpu = 0 config = tf.ConfigProto() #(device_count = {'GPU': 0}) #allow tensorflow release gpu memory config.gpu_options.allow_growth = True #compute pca if read_coeff_flag is 2: batch_ys = np.random.uniform(0, 1, (batch_size, 4)).astype(np.float64) #batch_ys[:,0] = np.random.uniform(0.1,0.6,(batch_size)).astype(np.float64) #batch_ys[:,1] = np.random.uniform(0.1,0.3,(batch_size)).astype(np.float64) #batch_ys[:,2] = np.random.uniform(0,1.0/tr,(batch_size)).astype(np.float64) #batch_ys[:,3] = np.random.uniform(0.1,1.0,(batch_size)).astype(np.float64)# np.ones(batch_size)## np.random.uniform(0.5,1,(batch_size)).astype(np.float64)# # intial seq simulation with t1t2b0 values npca = Ndiv T1r, T2r, dfr, PDr = ssmrf.set_par(batch_ys) batch_xs_c = ssmrf.bloch_sim_batch_cuda(batch_size, 100, Nk, PDr, T1r, T2r, dfr, M0, trr, far, ti) #ut.plot(np.absolute(batch_xs_c[0,:]),pause_close =1) if orig_Ndiv < Nk: batch_xs = simut.average_dict( batch_xs_c, orig_Ndiv ) #(np.dot(np.absolute(simut.average_dict(batch_xs_c, Ndiv)), coeff)) else: batch_xs = batch_xs_c pca_mtx = np.dot(np.matrix(batch_xs).getH(), batch_xs) U, s, V = scipy.sparse.linalg.svds(pca_mtx, npca) coeff = U[:, npca - 1::-1] sio.savemat(pathdat + 'MRF_pca_coeff.mat', { 'coeff': coeff, 'dict': batch_xs }) for i in range(1000000): batch_ys = np.random.uniform(0, 1, (batch_size, 4)).astype(np.float64) #batch_ys[:,0] = np.random.uniform(0.1,0.6,(batch_size)).astype(np.float64) #batch_ys[:,1] = np.random.uniform(0.1,0.3,(batch_size)).astype(np.float64) batch_ys[:, 2] = np.random.uniform(0, 1.0 / tr, (batch_size)).astype(np.float64) #batch_ys[:,3] = np.ones(batch_size)#np.random.uniform(0.1,1.0,(batch_size)).astype(np.float64)# # np.random.uniform(0.5,1,(batch_size)).astype(np.float64)# #batch_ys[:,2] = np.zeros(batch_size) #batch_ys[:,2] = np.random.uniform(0.19,0.21,(batch_size)).astype(np.float64)#0.2*np.ones(batch_size) # intial seq simulation with t1t2b0 values #seq_data = ssad.irssfp_arrayin_data( batch_size, Nk ).set( batch_ys ) T1r, T2r, dfr, PDr = ssmrf.set_par(batch_ys) batch_xs_c = ssmrf.bloch_sim_batch_cuda(batch_size, 100, Nk, PDr, T1r, T2r, dfr, M0, trr, far, ti) #ut.plot(np.absolute(batch_xs_c[0,:]),pause_close =1) if orig_Ndiv < Nk: batch_xs = simut.average_dict( batch_xs_c, orig_Ndiv ) #(np.dot(np.absolute(simut.average_dict(batch_xs_c, Ndiv)), coeff)) else: batch_xs = batch_xs_c batch_xs = batch_xs + np.random.ranf(1)[0] * np.random.uniform( -0.005, 0.005, (batch_xs.shape)) #batch_xs = batch_xs/np.ndarray.max(batch_xs.flatten()) if read_coeff_flag is 1: if abs_flag: batch_xs = np.dot(np.absolute(batch_xs), coeff) else: batch_xs = np.absolute(np.dot(batch_xs, coeff)) elif read_coeff_flag is 2: batch_xs = np.absolute(np.dot(batch_xs, coeff)) else: batch_xs = np.absolute(batch_xs) for dd in range(batch_xs.shape[0]): tc1 = batch_xs[dd, :] #- np.mean(imall[i,:]) normtc1 = np.linalg.norm(tc1) if normtc1 > 0.04 and batch_ys[dd, 0] * 5000 > 3 * 500 * batch_ys[ dd, 1]: batch_xs[dd, :] = tc1 #/normtc1 else: batch_ys[dd, :] = np.zeros([1, npar]) batch_xs = 1000 * batch_xs #ut.plot(np.absolute(batch_xs[0,:]),pause_close =1) #batch_ys[:,2] = np.zeros(batch_size) #batch_ys[:,3] = np.zeros(batch_size) model.train(batch_xs, batch_ys) model.test(batch_xs, batch_ys) if i % 100 == 0: prey = model.prediction(batch_xs, np.zeros(batch_ys.shape)) ut.plot(prey[..., 0], batch_ys[..., 0], line_type='.', pause_close=1) ut.plot(prey[..., 1], batch_ys[..., 1], line_type='.', pause_close=1) ut.plot(prey[..., 2], batch_ys[..., 2], line_type='.', pause_close=1) ut.plot(prey[..., 3], batch_ys[..., 3], line_type='.', pause_close=1) model.save(pathdat + 'test_model_savecnn')
def test1(): mat_contents = sio.loadmat(pathdat + 'dict_pca.mat') #dict_pca #dictall = np.array(mat_contents["avedictall"].astype(np.float32)) #label = np.array(mat_contents["dict_label"].astype(np.float32)) coeff = np.array(mat_contents["coeff"].astype(np.float32)) #cn_orders = np.array(mat_contents["cn_orders"].astype(np.float32)) par = mat_contents["par"] batch_size = 800 Nk = par[0]['irfreq'][0][0][0] #892#far.shape[0]#par.irfreq# Ndiv = coeff.shape[1] #par[0]['ndiv'][0][0][0]#16 orig_Ndiv = coeff.shape[0] npar = 4 model = tf_wrap.tf_model_top([None, Ndiv], [None, npar], tf_prediction_func, tf_optimize_func, tf_error_func, arg=0.5) fa = par[0]['fa'][0][0][0].astype(np.float32) #35#30 #deg tr = par[0]['tr'][0][0][0].astype(np.float32) #3.932#4.337 #ms ti = par[0]['ti'][0][0][0].astype(np.float32) #11.0 #ms #print(fa) #print(tr) #print(ti) #print(Nk) #print(Ndiv) far, trr = simut.rftr_const(Nk, fa, tr) M0 = simut.def_M0() #run tensorflow on cpu, count of gpu = 0 config = tf.ConfigProto() #(device_count = {'GPU': 0}) #allow tensorflow release gpu memory config.gpu_options.allow_growth = True for i in range(1000000): batch_ys = np.random.uniform(0, 1, (batch_size, 4)).astype(np.float64) #batch_ys[:,0] = batch_ys[:,0] + 1.0*batch_ys[:,1]/10.0 #batch_ys[:,0] = np.random.uniform(0.07,1.0,(batch_size)).astype(np.float64) #batch_ys[:,1] = np.random.uniform(0.0,0.2,(batch_size)).astype(np.float64) batch_ys[:, 2] = np.random.uniform(0, 1.0 / tr, (batch_size)).astype(np.float64) #batch_ys[:,2] = np.zeros(batch_size) #batch_ys[:,3] = np.ones(batch_size)#np.random.uniform(0.4,1,(batch_size)).astype(np.float64)# #batch_ys[:,0] = np.round(batch_ys[:,0]*20)/20 #batch_ys[:,1] = np.round(batch_ys[:,1]*20)/20 #batch_ys[:,2] = np.round(batch_ys[:,2]*20)/20 #batch_ys[:,3] = np.round(batch_ys[:,3]*5)/5 #batch_ys[:,3] = np.round(batch_ys[:,3]*5)/5 # intial seq simulation with t1t2b0 values #seq_data = ssad.irssfp_arrayin_data( batch_size, Nk ).set( batch_ys ) T1r, T2r, dfr, PDr = ssmrf.set_par(batch_ys) batch_xs_c = ssmrf.bloch_sim_batch_cuda(batch_size, 100, Nk, PDr, T1r, T2r, dfr, M0, trr, far, ti) #ut.plot(np.absolute(batch_xs_c[0,:])) if orig_Ndiv is not Nk: batch_xs = np.absolute( simut.average_dict(batch_xs_c, orig_Ndiv) ) #(np.dot(np.absolute(simut.average_dict(batch_xs_c, Ndiv)), coeff)) #batch_xs = np.absolute(simut.average_dict_cnorders(batch_xs_c, cn_orders)) #np.absolute(np.dot(batch_xs_c, cn_orders)) else: batch_xs = np.absolute(batch_xs_c) #ut.plot(np.absolute(batch_xs[0,:])) batch_xt = batch_xs batch_xs = batch_xs + np.random.ranf(1)[0] * np.random.uniform( -0.1, 0.1, (batch_xs.shape)) #batch_xs = batch_xs/np.ndarray.max(batch_xs.flatten()) if 1: batch_xs = np.dot(batch_xs, coeff) batch_xt = np.dot(batch_xt, coeff) else: batch_xs = batch_xs batch_xt = batch_xs #batch_ys[:,3] = np.zeros(batch_size) for dd in range(batch_xs.shape[0]): tc1 = batch_xs[dd, :] #- np.mean(imall[i,:]) tc2 = batch_xt[dd, :] normtc1 = np.linalg.norm(tc1) normtc2 = np.linalg.norm(tc2) if normtc2 > 0.1: #and batch_ys[dd,0]*5000 > 3*500*batch_ys[dd,1] and batch_ys[dd,0]*5000 < 20*500*batch_ys[dd,1] batch_xs[dd, :] = tc1 #/normtc1 batch_xt[dd, :] = tc2 #/normtc2 else: # batch_xs[dd,:] = np.zeros([1,Ndiv]) # batch_xt[dd,:] = np.zeros([1,Ndiv]) batch_ys[dd, :] = np.zeros([1, npar]) batch_xs = batch_xs / np.ndarray.max(batch_xs.flatten()) batch_xt = batch_xt / np.ndarray.max(batch_xt.flatten()) #for kk in range(batch_xs.shape[1]): # batch_xs [:,kk] = (batch_xs[:,kk])/np.std(batch_xs[:,kk] )#- np.mean(batch_xs[:,kk]) # batch_xt [:,kk] = (batch_xt[:,kk])/np.std(batch_xt[:,kk] )#- np.mean(batch_xt[:,kk]) #ut.plot(np.real(batch_xs[0,:]),pause_close = 1) #batch_ys[:,3] = np.ones(batch_size) * np.random.ranf(1)[0] #batch_xs = batch_xs * batch_ys[0,3] #* np.random.ranf(1)[0]# model.test(batch_xs, batch_ys) model.train(batch_xt, batch_ys) model.train(batch_xs, batch_ys) if i % 100 == 0: prey = model.prediction(batch_xs, np.zeros(batch_ys.shape)) ut.plot(prey[..., 0], batch_ys[..., 0], line_type='.', pause_close=1) ut.plot(prey[..., 1], batch_ys[..., 1], line_type='.', pause_close=1) ut.plot(prey[..., 2], batch_ys[..., 2], line_type='.', pause_close=1) ut.plot(prey[..., 3], batch_ys[..., 3], line_type='.', pause_close=1) model.save(pathdat + 'test_model_save')
def test1(): mat_contents = sio.loadmat(pathdat+'dict_pca.mat');#dict_pca #dictall = np.array(mat_contents["avedictall"].astype(np.float32)) #label = np.array(mat_contents["dict_label"].astype(np.float32)) coeff = np.array(mat_contents["coeff"].astype(np.float32)) #cn_orders = np.array(mat_contents["cn_orders"].astype(np.float32)) par = mat_contents["par"] batch_size = 800 Nk = par[0]['irfreq'][0][0][0]#892#far.shape[0]#par.irfreq# Ndiv = coeff.shape[1]#par[0]['ndiv'][0][0][0]#16 orig_Ndiv = coeff.shape[0] npar = 7 model = tf_wrap.tf_model_top([None, Ndiv], [None, npar], tf_prediction_func, tf_optimize_func, tf_error_func, arg = 0.5) fa = par[0]['fa'][0][0][0].astype(np.float32)#35#30 #deg tr = par[0]['tr'][0][0][0].astype(np.float32)#3.932#4.337 #ms ti = par[0]['ti'][0][0][0].astype(np.float32)#11.0 #ms te = 1.5 #ms #print(fa) #print(tr) #print(ti) #print(Nk) #print(Ndiv) far, trr,ter = simut.rftr_const(Nk, fa, tr, te) M0 = simut.def_M0() #run tensorflow on cpu, count of gpu = 0 config = tf.ConfigProto()#(device_count = {'GPU': 0}) #allow tensorflow release gpu memory config.gpu_options.allow_growth=True t1t2_group = np.zeros((4, 2),dtype = np.float64) t1t2_group[0,0] = 600.0/5000.0 t1t2_group[0,1] = 40.0/500.0 t1t2_group[1,0] = 1000.0/5000.0 t1t2_group[1,1] = 80.0/500.0 t1t2_group[2,0] = 3000.0/5000.0 t1t2_group[2,1] = 200.0/500.0 t1t2_group[3,0] = 0.0/5000.0 t1t2_group[3,1] = 0.0/500.0 for i in range(1000000): batch_ys = np.random.uniform(0,1,(batch_size,npar)).astype(np.float64) batch_ys[:,2] = np.zeros(batch_size)#np.random.uniform(0,1.0/tr,(batch_size)).astype(np.float64) batch_ys_tmp = np.random.uniform(0,4,(batch_size)) for k in range(batch_size): if batch_ys_tmp[k] <= 4 and batch_ys_tmp[k] > 3: batch_ys[k,0] = t1t2_group[0,0] + np.random.uniform(-0.05,0.025) batch_ys[k,1] = t1t2_group[0,1] + np.random.uniform(-0.05,0.025) batch_ys[k,4] = 1.0 batch_ys[k,5] = 0.0 batch_ys[k,6] = 0.0 elif batch_ys_tmp[k] <= 3 and batch_ys_tmp[k] > 2: batch_ys[k,0] = t1t2_group[1,0] + np.random.uniform(-0.025,0.025) batch_ys[k,1] = t1t2_group[1,1] + np.random.uniform(-0.025,0.025) batch_ys[k,4] = 0.0 batch_ys[k,5] = 1.0 batch_ys[k,6] = 0.0 elif batch_ys_tmp[k] <= 2 and batch_ys_tmp[k] > 1: batch_ys[k,0] = t1t2_group[2,0] + np.random.uniform(-0.15,0.25) batch_ys[k,1] = t1t2_group[2,1] + np.random.uniform(-0.15,0.25) batch_ys[k,4] = 0.0 batch_ys[k,5] = 0.0 batch_ys[k,6] = 1.0 else: batch_ys[k,0] = t1t2_group[3,0] batch_ys[k,1] = t1t2_group[3,1] batch_ys[k,4] = 0.0 batch_ys[k,5] = 0.0 batch_ys[k,6] = 0.0 T1r, T2r, dfr, PDr = ssmrf.set_par(batch_ys[...,0:4]) batch_xs_c = ssmrf.bloch_sim_batch_cuda2( batch_size, 100, Nk, PDr, T1r, T2r, dfr, M0, trr, ter, far, ti ) #ut.plot(np.absolute(batch_xs_c[0,:])) if orig_Ndiv is not Nk: batch_xs = np.absolute(simut.average_dict(batch_xs_c, orig_Ndiv))#(np.dot(np.absolute(simut.average_dict(batch_xs_c, Ndiv)), coeff)) else: batch_xs = np.absolute(batch_xs_c) #ut.plot(np.absolute(batch_xs[0,:])) #batch_xt = batch_xs batch_xs = batch_xs + np.random.ranf(1)[0]*np.random.uniform(-0.1,0.1,(batch_xs.shape)) #batch_xs = batch_xs/np.ndarray.max(batch_xs.flatten()) if 1: batch_xs = np.dot(batch_xs, coeff) #batch_xt = np.dot(batch_xt, coeff) else: batch_xs = batch_xs #batch_xt = batch_xs for dd in range(batch_xs.shape[0]): tc1 = batch_xs[dd,:] #- np.mean(imall[i,:]) #tc2 = batch_xt[dd,:] normtc1 = np.linalg.norm(tc1) #normtc2 = np.linalg.norm(tc2) if normtc1 > 0.05: #and batch_ys[dd,0]*5000 > 3*500*batch_ys[dd,1] and batch_ys[dd,0]*5000 < 20*500*batch_ys[dd,1] batch_xs[dd,:] = tc1#/normtc1 #batch_xt[dd,:] = tc2#/normtc2 else: # batch_xs[dd,:] = np.zeros([1,Ndiv]) # batch_xt[dd,:] = np.zeros([1,Ndiv]) batch_ys[dd,:] = np.zeros([1,npar]) batch_xs = batch_xs/np.ndarray.max(batch_xs.flatten()) #batch_xt = batch_xt/np.ndarray.max(batch_xt.flatten()) #for kk in range(batch_xs.shape[1]): # batch_xs [:,kk] = (batch_xs[:,kk])/np.std(batch_xs[:,kk] )#- np.mean(batch_xs[:,kk]) # batch_xt [:,kk] = (batch_xt[:,kk])/np.std(batch_xt[:,kk] )#- np.mean(batch_xt[:,kk]) #ut.plot(np.real(batch_xs[0,:]),pause_close = 1) #batch_xs = batch_xs * batch_ys[0,3] #* np.random.ranf(1)[0]# model.test(batch_xs, batch_ys) model.train(batch_xs, batch_ys) if i % 100 == 0: prey = model.prediction(batch_xs,np.zeros(batch_ys.shape)) ut.plot(prey[...,0], batch_ys[...,0], line_type = '.', pause_close = 1) ut.plot(prey[...,1], batch_ys[...,1], line_type = '.', pause_close = 1) ut.plot(prey[...,2], batch_ys[...,2], line_type = '.', pause_close = 1) ut.plot(prey[...,3], batch_ys[...,3], line_type = '.', pause_close = 1) ut.plot(prey[...,4], batch_ys[...,4], line_type = '.', pause_close = 1) model.save(pathdat + 'test_model_save')