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
            })
Beispiel #2
0
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})
Beispiel #4
0
def test():
    # 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.float32).squeeze())

    # intial tmp data
    mat_contents = sio.loadmat(pathdat + 'test_outy.mat')
    test_outy = np.array(mat_contents["test_outy"].astype(
        np.float32).squeeze())

    # input MRF time courses
    mat_contents2 = sio.loadmat(pathdat + 'datax1.mat')
    data_x_acc = np.array(mat_contents2["datax1"]).astype(np.float32)
    # prepare for sequence simulation, y->x_hat
    Nk = far.shape[0]
    Nexample = data_x_acc.shape[0]
    ti = 10  #ms
    M0 = np.array([0.0, 0.0, 1.0]).astype(np.float32)
    #image size
    nx = 217
    ny = 181

    # for loop start here
    for _ in range(10):
        # apply CNN model, x-->parameters
        test_outy = ssmrf.batch_apply_tf_cuda(Nexample, ny, sess.run, x,
                                              data_x_acc, y_conv, test_outy,
                                              keep_prob)
        #cuda bloch simulation for each pixel, parameters-->x
        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)
        print(
            np.linalg.norm(data_x_c -
                           ssmrf.seqdata_realimag_2complex(data_x_acc)))
        print(np.linalg.norm(data_x_c))
        sio.savemat(pathdat + 'cnn_cs_testouty.mat', {'test_outy': test_outy})
        sio.savemat(pathdat + 'cnn_cs_testouty1.mat', {'data_x_c': data_x_c})
        sio.savemat(
            pathdat + 'cnn_cs_testouty2.mat',
            {'data_x_acc': ssmrf.seqdata_realimag_2complex(data_x_acc)})
        data_x_acc = ssmrf.seqdata_complex_2realimag(data_x_c)
Beispiel #5
0
def test():
    # 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 = cuopts.FFT2d_cuda_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
    data_x_c = mask_ksp3d(nx, ny, Nk, FTm, data_x_c)
    #data_x0_c     = data_x_c
    data_x = ssmrf.seqdata_complex_2realimag(data_x_c)
    data_x0 = data_x
    # 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)
    timing.stop().display('CNN 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 = 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})
Beispiel #6
0
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')
Beispiel #7
0
def test():

    sess = tf.InteractiveSession()

    #define x and y_
    x = tf.placeholder(tf.float32, shape=[None, 2 * 960])  #changed 784 to 1000
    y_ = tf.placeholder(tf.float32, shape=[None, 2 * 960])

    def weight_variable(shape):
        initial = tf.truncated_normal(shape, stddev=0.1)
        return tf.Variable(initial)

    def bias_variable(shape):
        initial = tf.constant(0.1, shape=shape)
        return tf.Variable(initial)

    #define convolution and max pooling
    def conv2d(x, W):
        return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

    def max_pool_2x2(x):
        return tf.nn.max_pool(x,
                              ksize=[1, 2, 1, 1],
                              strides=[1, 2, 1, 1],
                              padding='SAME')

    #############dense connection layer 1
    #weighting and bias for a layer with 1024 neurons
    W_fc1 = weight_variable([2 * 960, 128])  #40*48 *64 /32
    b_fc1 = bias_variable([128])

    # densely connected layer with relu output
    h_pool2_flat = tf.reshape(x, [-1, 2 * 960])
    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

    #############dense connection layer 2
    #weighting and bias for a layer with 1024 neurons
    W_fc2 = weight_variable([128, 128])  #40*48 *64 /32
    b_fc2 = bias_variable([128])

    # densely connected layer with relu output
    h_fc2 = tf.sigmoid(tf.matmul(h_fc1, W_fc2) + b_fc2)

    #############dense connection layer 3
    #weighting and bias for a layer with 1024 neurons
    #W_fc3 = weight_variable([1024, 1024])  #40*48 *64 /32
    #b_fc3 = bias_variable([1024])

    # densely connected layer with relu output
    #h_fc3 = tf.nn.relu(tf.matmul(h_fc2, W_fc3) + b_fc3)

    #############last layer with dropout
    #do dropout for the last layer, densely connected
    keep_prob = tf.placeholder(tf.float32)
    h_fcn_drop = tf.nn.dropout(h_fc2, keep_prob)

    W_fcn = weight_variable([128, 1920])
    b_fcn = bias_variable([1920])

    #fully connected layer; changed tf.argmax to tf.sigmoid, could also try tf.tanh and tf.nn.relu (not work)
    y_conv = (tf.matmul(h_fcn_drop, W_fcn) + b_fcn)

    #changed AdamOptimizer(1e-4) to GradientDescentOptimizer(0.05)
    # l2-norm
    loss = tf.reduce_sum(tf.pow(tf.subtract(y_conv, (y_)), 2))

    #sigmoid_cross_entropy_with_logits
    #loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits((y_conv), (y_)))

    #algrithm
    train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)

    #training accuracy
    correct_prediction = tf.pow(tf.subtract(y_conv, (y_)), 2)
    accuracy = tf.reduce_mean(correct_prediction)

    #intial sess
    sess.run(tf.global_variables_initializer())

    #define batch size and mini batch size
    batch_size = 800

    # load far and trr
    # read rf and tr arrays from mat file
    mat_contents = sio.loadmat(pathdat + 'mrf_rf_tr.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(200000):
        batch_ys = np.random.uniform(0, 1, (batch_size, 4)).astype(np.float64)
        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 )
        #inputs = range(batch_size)
        #def processFunc(i):
        #    S = seq_data.sim_seq_tc(i,M0, trr, far, ti )
        #    return S

        #num_cores = multiprocessing.cpu_count()
        #batch_xs_c = Parallel(n_jobs=(num_cores//2), verbose=5)(delayed(processFunc)(i) for i in inputs)

        #add noise
        #rand_c = np.random.uniform(-0.001,0.001,(batch_size,Nk)) + 1j*np.random.uniform(-0.001,0.001,(batch_size,Nk))

        #batch_xs_c = batch_xs_c + rand_c
        #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_xsnoise,
            y_: batch_xs,
            keep_prob: 0.5
        })
        if i % 10 == 0:
            train_error = accuracy.eval(feed_dict={
                x: batch_xsnoise,
                y_: batch_xs,
                keep_prob: 1.0
            })
            print("step %d, training error %g" % (i, train_error))
        if i % 1000 == 0:
            #save the model into file
            saver = tf.train.Saver()
            saver.save(
                sess, pathexe +
                'check_point/enc_dencoder_t1t2b0-mrf-ir-ssfp-noise0p01-20170316'
            )

    #save the model into file
    saver = tf.train.Saver()
    saver.save(
        sess, pathexe +
        'check_point/enc_dencoder_t1t2b0-mrf-ir-ssfp-noise0p01-20170316')
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)
Beispiel #9
0
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')
Beispiel #10
0
def test():
    # 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 = cuopts.FFT2d_cuda_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
    data_x_c = mask_ksp3d(nx, ny, Nk, FTm, data_x_c)
    data_x0_c = data_x_c
    data_x_c_pre = data_x_c
    data_x_acc = ssmrf.seqdata_complex_2realimag(data_x_c)
    # intial tmp data
    test_outy0 = np.zeros((Nexample, 4), dtype=np.float64)
    test_outy = np.zeros((Nexample, 4), dtype=np.float64)
    step = 1.0
    # for loop start here
    for _ in range(40):
        for _ in range(10):
            ssmrf.batch_apply_tf_cuda(Nexample, ny, sess.run, x, data_x_acc,
                                      y_conv, test_outy, keep_prob)
            #cuda bloch simulation for each pixel, parameters-->x
            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)
            print(
                np.linalg.norm(data_x_c -
                               ssmrf.seqdata_realimag_2complex(data_x_acc)))
            print(np.linalg.norm(data_x_c))
            data_x_acc = ssmrf.seqdata_complex_2realimag(data_x_c)
        #timing.stop().display('Bloch sim in loop ')
        #0.5* d||M*FT(x)-b||_2^2/dx = 2 * FT^-1(M*(FT(x) - b))
        # lamda * d(x^2)/dx = 2*lambda*x
        data_x_c = mask_ksp3d(nx, ny, Nk, FTm,
                              data_x_c - data_x0_c)  #data_x_c-data_x0_c#
        # gradient descent/update
        data_x_acc = data_x_acc - step * ssmrf.seqdata_complex_2realimag(
            data_x_c)
        #data_x_c   = data_x_c - mask_ksp3d(nx, ny, Nk, FTm, data_x_c) + data_x0_c
        #data_x_acc = ssmrf.seqdata_complex_2realimag(data_x_c)
        #data_x_c      = (wavel1_data(nx, ny, ssmrf.seqdata_realimag_2complex(data_x_acc)))
        # apply CNN model, x-->parameters
        print('gradient 0.5* d||f(p)-b||_2^2/dp: %g' %
              np.linalg.norm(data_x0_c - data_x_c))
        #could do soft thresholding on test_outy here, i.e. test_outy = threshold(test_outy)
        #test_outy     = wavel1_par(nx, ny, test_outy) #
        #test_outy     = constraints( test_outy )
        #test_outy     = tv_par(nx, ny, test_outy)
        #test_outy  = constraints( test_outy )
        sio.savemat(pathdat + 'cnn_cs_testouty.mat', {'test_outy': test_outy})