def noise(image, mrStruct, new_flow): k = np.squeeze(image[0]) print(k.shape) #input_4 = tf.convert_to_tensor(k, dtype=tf.float32) #input_4 = input_[32:160,...] #input_4 = tf.image.resize_image_with_crop_or_pad(input_4,128,96) #input_4 = (input_4 - tf.reduce_min(input_4))/(tf.reduce_max(input_4) - tf.reduce_min(input_4)) #input_4 = tf.expand_dims(input_4, dim=0) #input_4 = tf.expand_dims(input_4, dim=4) #with graph.as_default(): input_4 = tf.convert_to_tensor(k, dtype=tf.float32) input_4 = tf.expand_dims(input_4, dim=0) #input_4 = tf.expand_dims(input_4, dim=4) flag = tf.placeholder(tf.bool) input_2p = tf.placeholder(tf.float32, shape=[1, 160,96,None, 2]) #input_2p = tf.placeholder(tf.float32, shape=[1, 160,130,None, 2]) logits_2 = Unet(x = input_2p, training=flag).model #logits_2 = Unet2(x = input_2p, training=flag).model llogits_2 = tf.nn.softmax(logits_2) #sesss = tf.Session(graph=graph) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.4 with tf.Session(config=config) as sesss: sesss.run(tf.global_variables_initializer()) saver = tf.train.Saver(tf.global_variables()) #checkpoint1 = tf.train.get_checkpoint_state("E:/HB/scripts_and_stuff/aliasing/new_noise") #sesss.run(tf.local_variables_initializer()) s = sesss.run([input_4]) print(s[0].shape) saver.restore(sesss, tf.train.get_checkpoint_state("./new_noise").model_checkpoint_path) h , seg3 = sesss.run([logits_2, llogits_2], feed_dict={input_2p:s[0], flag: True}) sesss.close() tf.keras.backend.clear_session() mask2 = np.squeeze(seg3[...,1]) mask2 = mask2>0.5 mask2 = np.logical_not(mask2) mask2 = mask2.astype(np.float32) mask2 = np.expand_dims(mask2,axis=3) mask2 = np.expand_dims(mask2,axis=4) final_flow = np.multiply(new_flow, mask2) #mrStruct1 = flows['mrStruct'] mrStruct['dataAy'][0,0] = final_flow #mag = mags['mrStruct']['dataAy'][0,0] #mag = mag[h1:HH-h1,w1:WW-w1,...] #mrStruct = mags['mrStruct'] #mrStruct['dataAy'][0,0] = mag print(final_flow.shape) io.savemat('MK/vel_struct.mat',{'mrStruct':mrStruct}) #io.savemat('MK/mag_struct.mat',{'mrStruct':mrStruct}) io.savemat('MK/noise_mask.mat',{'noise':mask2}) #del mrStruct1 return image, mrStruct
def noise(image, new_flow): k = np.squeeze(image[0]) print(k.shape) input_4 = tf.convert_to_tensor(k, dtype=tf.float32) input_4 = tf.expand_dims(input_4, dim=0) #input_4 = tf.expand_dims(input_4, dim=4) flag = tf.placeholder(tf.bool) input_2p = tf.placeholder(tf.float32, shape=[1, 160, 96, None, 2]) #input_2p = tf.placeholder(tf.float32, shape=[1, 160,130,None, 2]) logits_2 = Unet(x=input_2p, training=flag).model #logits_2 = Unet2(x = input_2p, training=flag).model llogits_2 = tf.nn.softmax(logits_2) #sesss = tf.Session(graph=graph) config = tf.ConfigProto() config.gpu_options.allow_growth = True #config.gpu_options.per_process_gpu_memory_fraction = 0.4 with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(tf.global_variables()) #checkpoint1 = tf.train.get_checkpoint_state("E:/HB/scripts_and_stuff/aliasing/new_noise") #sesss.run(tf.local_variables_initializer()) s = sess.run([input_4]) print(s[0].shape) saver.restore( sess, tf.train.get_checkpoint_state( "/opt/codes/python-ismrmrd-server/new_noise"). model_checkpoint_path) h, seg3 = sess.run([logits_2, llogits_2], feed_dict={ input_2p: s[0], flag: True }) sess.close() tf.keras.backend.clear_session() mask2 = np.squeeze(seg3[..., 1]) mask2 = mask2 > 0.5 #mask2 = np.logical_not(mask2) mask2 = mask2.astype(np.float32) if new_flow.shape[1] < 96: W = new_flow.shape[1] #h1 = np.abs((HH - 160))/2 w1 = np.abs((W - 96)) / 2 w1 = w1.astype(np.int16) mask2 = mask2[:, w1:96 - w1, :] mask2 = np.expand_dims(mask2, axis=3) mask2 = np.expand_dims(mask2, axis=4) final_flow = np.multiply(new_flow, mask2) print(final_flow.shape) return image, final_flow
def segment(image): k = np.squeeze(image[0]) #mrStruct = io.loadmat('MK/mag_struct.mat') print(k.shape) k = k[..., 0] input_5 = tf.convert_to_tensor(k, dtype=tf.float32) input_5 = tf.expand_dims(input_5, dim=0) input_5 = tf.expand_dims(input_5, dim=4) flag = tf.placeholder(tf.bool) input_3p = tf.placeholder(tf.float32, shape=[1, 160, 96, None, 1]) #input_3p = tf.placeholder(tf.float32, shape=[1, 160,130,None, 1]) logits_3 = Unet(x=input_3p, training=flag).model #logits_2 = Unet2(x = input_2p, training=flag).model llogits_3 = tf.nn.softmax(logits_3) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.4 with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) saver = tf.train.Saver(tf.global_variables()) #checkpoint1 = tf.train.get_checkpoint_state("E:/HB/scripts_and_stuff/aliasing/new_noise") #sesss.run(tf.local_variables_initializer()) s = sess.run([input_5]) print(s[0].shape) saver.restore( sess, tf.train.get_checkpoint_state( "/opt/codes/python-ismrmrd-server/pre-trained"). model_checkpoint_path) h, seg4 = sess.run([logits_3, llogits_3], feed_dict={ input_3p: s[0], flag: True }) sess.close() mask3 = np.squeeze(seg4[..., 1]) mask3 = mask3 > 0.5 #mrStruct = mags['mrStruct'] #mrStruct['mrStruct']['dataAy'][0,0] = mask3 #mrStruct['mrStruct']['dim4'] = 'unused' #mrStruct['mrStruct']['dim5'] = 'unused' return mask3
def eddy(mag, flow): imaspeed = np.power(np.mean(np.square(flow), axis=3), 0.5) minV = np.amin(0.7 * np.reshape(mag, (-1, 1))) maxV = np.amax(0.7 * np.reshape(mag, (-1, 1))) tmp = mag > maxV not_tmp = np.logical_not(tmp) mag = np.multiply(mag, not_tmp) + np.multiply(tmp, maxV) tmp = mag < minV not_tmp = np.logical_not(tmp) mag = np.multiply(mag, not_tmp) + np.multiply(tmp, minV) mag = (mag - minV) / (maxV - minV) pcmra = np.multiply(imaspeed, mag) pcmra = np.squeeze(np.mean(np.square(pcmra), axis=3)) std_flow = np.std(imaspeed, axis=3) #del flow #del mag print(pcmra.shape) print(std_flow.shape) pcmra = np.expand_dims(pcmra, axis=3) std_flow = np.expand_dims(std_flow, axis=3) X = np.concatenate((pcmra, std_flow), axis=3) X = X.astype(np.float32) del pcmra del std_flow for j in range(2): X[..., j] = (X[..., j] - np.amin(X[..., j])) / (np.amax(X[..., j]) - np.amin(X[..., j])) #X = X[10:138,...] print(X.shape) input_ = tf.convert_to_tensor(X, dtype=tf.float32) input_ = tf.transpose(input_, perm=[2, 0, 1, 3]) input_ = tf.image.resize_image_with_crop_or_pad(input_, 160, 96) #input_ = tf.image.resize_image_with_crop_or_pad(input_, 160, 130) input_ = tf.transpose(input_, perm=[1, 2, 0, 3]) input_ = tf.squeeze(input_) #input_ = (input_ - tf.reduce_min(input_))/(tf.reduce_max(input_) - tf.reduce_min(input_)) input_ = tf.expand_dims(input_, dim=0) #input_ = tf.expand_dims(input_, dim=4) input_p = tf.placeholder(tf.float32, shape=[None, 160, 96, None, 2]) #input_p = tf.placeholder(tf.float32, shape=[None, 160,130,None, 2]) flag = tf.placeholder(tf.bool) logits_1 = Unet(x=input_p, training=flag).model saver1 = tf.train.Saver() llogits_1 = tf.nn.softmax(logits_1) checkpoint1 = tf.train.get_checkpoint_state( "/opt/codes/python-ismrmrd-server/new_eddy") config = tf.ConfigProto() config.gpu_options.allow_growth = True #config.gpu_options.per_process_gpu_memory_fraction = 0.4 with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) image = sess.run([input_]) saver1.restore(sess, checkpoint1.model_checkpoint_path) h, seg1 = sess.run([logits_1, llogits_1], feed_dict={ input_p: image[0], flag: True }) #h , seg2 = sess.run([logits_1, llogits_1], feed_dict={input_1p: image[1], flag: True}) #h , seg3 = sess.run([logits_1, llogits_1], feed_dict={input_1p: image[2], flag: True}) sess.close() tf.reset_default_graph() tf.keras.backend.clear_session() mask1 = seg1[..., 1] mask1 = mask1 > 0.5 print(len(image)) #U = image[0] #plt.show() mask1 = np.squeeze(mask1) print(mask1.shape) HH = flow.shape[0] WW = flow.shape[1] h1 = np.abs((HH - 160)) / 2 w1 = np.abs((WW - 96)) / 2 print(h1) print(w1) h1 = np.int(h1) w1 = np.int(w1) if flow.shape[1] >= 96: flow = flow[h1:HH - h1, w1:WW - w1, ...] elif flow.shape[1] < 96: #flows = np.zeros([flow[0].shape, 96, flow]) flow = flow[h1:HH - h1, ...] mask1 = mask1[:, w1:96 - w1, :] print(mask1.shape) #print(w1) #print(WW) [H, W, D] = mask1.shape xx = np.arange(W) xxx = np.tile(xx, [H, 1]) print(xxx.shape) yy = np.arange(H) yy = yy[::-1] yy = np.expand_dims(yy, axis=1) #yy = np.fliplr([yy])[0] #print(yy) yyy = np.tile(yy, (1, W)) print(np.amin(flow)) print(flow.shape) #yyy = np.fliplr(yyy) yyy = yyy + 1 xxx = xxx + 1 print(yyy) print(xxx) #new_flow = np.zeros(flow.shape) print(flow.shape) for i in range(flow.shape[2]): imaFlow = np.squeeze(flow[..., i, :, :]) #print(imaFlow.shape) statMask = mask1[..., i] statMask = statMask.astype(np.int16) imaFlow1 = np.zeros(imaFlow.shape) #statMask = statMask.astype(np.float32) for k in range(imaFlow.shape[2]): tt = flow.shape[4] phi, alpha, beta = lsqf2(imaFlow[..., k, tt - 1], statMask, xxx, yyy) phi = phi.astype(np.float32) alpha = alpha.astype(np.float32) beta = beta.astype(np.float32) fitPlane = phi + alpha * xxx + beta * yyy zeroMask = imaFlow[:, :, k, tt - 1] != 0 zeroPlane = np.multiply(fitPlane, zeroMask) #print(zeroPlane.shape) zeroPlane = np.expand_dims(zeroPlane, axis=2) factor3D = np.tile(zeroPlane, (1, 1, tt)) imaFlow1[:, :, k, :] = np.squeeze(imaFlow[:, :, k, :]) - factor3D #print(phi) #print(alpha) #print(beta) #print(Md) flow[:, :, i, :, :] = imaFlow1 del imaFlow del imaFlow1 print("Finished flow loop in Eddy Current") del mag #io.savemat('new_vel.mat',{'data':flow}) #io.savemat('eddy_mask.mat',{'data':mask1}) return image, flow
def Unet_test(): image_batch_placeholder = tf.placeholder(tf.float32, shape=[None, 160,96,None, 1]) #label_batch_placeholder = tf.placeholder(tf.float32, shape=[None, 160, 96,None]) #labels_pixels = tf.reshape(label_batch_placeholder, [-1, 1]) # if_training_placeholder = tf.placeholder(tf.bool, shape=[]) training_flag = tf.placeholder(tf.bool) image_batch, venc, phase = feed_data() #label_batch_dense = tf.arg_max(label_batch, dimension = 1) # if_training = tf.Variable(False, name='if_training', trainable=False) logits = Unet(x = image_batch_placeholder, training=training_flag).model #logits = logits>1 #logs = cost_dice(logits,label_batch_placeholder) llogits = tf.nn.softmax(logits) #logits = tf.argmax(logits,axis=3) #logits = tf.reshape(logits,(-1, 2)) #logits_batch = tf.to_int64(tf.arg_max(logits, dimension = 3)) #logi = tf.nn.softmax(logits) #N,S,W,C = logits.get_shape() #GH = tf.reshape(logits,[-1, 1]) #GT = tf.nn.softmax(GT) #GH = tf.nn.softmax(GT, axis=1) #GH = tf.reshape(GT,[H.value,W.value]) #GH = tf.argmax(GH, axis = 1) #GH = tf.reshape(GH,[S.value,W.value]) #probs = tf.slice(logits,[0, 0, 0, 1], [-1, -1, -1, -1]) #probs = tf.squeeze(probs, axis = -1) #y = tf.reshape(label_batch_placeholder,[-1,2]) #y = tf.argmax(y, axis = 1) #y = tf.reshape(y,[S.value,W.value]) #H = tf.cast(GH,tf.float32) #y = tf.cast(y,tf.float32) #inter = tf.reduce_sum(H * y) #u = tf.constant(1e-5,dtype=tf.int64) #union = 0.00001+ tf.reduce_sum(H) + tf.reduce_sum(y) #logits = tl.activation.pixel_wise_softmax(logits) #log = tf.reshape(logits,[-1,2]) #log = tf.argmax(log,axis = 1) #log = tf.reshape(log,[S.value,W.value]) #correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels_pixels, 1)) #accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) checkpoint = tf.train.get_checkpoint_state('./alias')#"/media/haben/D4CC01B2CC018FC2/alis_og_crop") #good_alis #new_alis_z #new_alis_y saver = tf.train.Saver() #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) # logits_batch = tf.to_int64(tf.arg_max(logits, dimension = 1)) d = 0 config = tf.ConfigProto(log_device_placement=False) all_trainable_vars = tf.reduce_sum([tf.reduce_prod(v.shape) for v in tf.trainable_variables()]) config.gpu_options.allow_growth=True config.gpu_options.per_process_gpu_memory_fraction = 0.4 with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) #tf.logging.info("Restoring full model from checkpoint file %s",checkpoint.model_checkpoint_path) saver.restore(sess, checkpoint.model_checkpoint_path) #accuracy_accu = 0 coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord, sess = sess) ave = [] ma = [] tru = [] use = [] gtruth = [] low = [] low_true = [] low_loss = [] new_data = [] phases = sess.run(phase) print(phases) for i in (range(int(phases))): image_out, Venc = sess.run([image_batch, venc]) _, llogit = sess.run([logits, llogits], feed_dict={image_batch_placeholder: image_out, #label_batch_placeholder: truth, training_flag: True}) infer_out = llogit[...,1] print(i) #lo = np.squeeze(soft[...,0]) #print(logis.shape) data = np.squeeze(infer_out) #print(Venc) #print(ys.shape) #gt = np.squeeze(Y) mask = data.transpose() #use.append(mask>0.2) #gt = gt.transpose() h = data>0.2 #print(np.max(alias)) #alias = np.squeeze(alias) #data = data>0.2 #data = data[...,1] #im = np.squeeze(image_out) #im = im[...,1] #plt.imshow(im) #plt.show() #plt.imshow(data) #plt.show() #new_alis = alias """ for i in range(alias.shape[2]): for x in range(alias.shape[0]): for y in range(alias.shape[1]): if h[x,y,i] == 1: value = alias[x,y,i] new = value - (np.sign(value) * Venc*2/100) new_alis[x,y,i] = new else: continue new_data.append(new_alis) """ #mask2 = alias>=0 #mask1 = mask2 * h #new_alis[mask1] = new_alis[mask1] - (Venc*2/100) #mask3 = np.invert(mask2) #mask4 = mask3 * h #new_alis[mask4] = new_alis[mask4] + (Venc*2/100) new_data.append(h) #print(np.unique(new_alis)) #ys = (ys) #print(W.shape) #plt.pause(0.1) #plt.imshow(alias[...,2]) #plt.show() #plt.imshow(mask1[...,2]) #plt.show() #plt.imshow(mask4[...,2]) #plt.show() #plt.pause(0.1) io.savemat('./new_vel.mat',{'data':new_data}) #tf.train.write_graph(sess.graph_def, 'graph/', 'my_graph.pb', as_text=False) coord.request_stop() coord.join(threads) sess.close() tf.keras.backend.clear_session() tf.reset_default_graph() return new_data
input_1 = tf.image.resize_image_with_crop_or_pad(input_1, 128, 96) h1 = (h - 128) input_2 = input_[h1:h, ...] input_2 = tf.image.resize_image_with_crop_or_pad(input_2, 128, 96) input_1 = (input_1 - tf.reduce_min(input_1)) / (tf.reduce_max(input_1) - tf.reduce_min(input_1)) input_1 = tf.expand_dims(input_1, dim=0) input_1 = tf.expand_dims(input_1, dim=4) input_1p = tf.placeholder(tf.float32, shape=[1, 128, 96, None, 1]) input_2 = (input_2 - tf.reduce_min(input_2)) / (tf.reduce_max(input_2) - tf.reduce_min(input_2)) input_2 = tf.expand_dims(input_2, dim=0) input_2 = tf.expand_dims(input_2, dim=4) flag = tf.placeholder(tf.bool) logits_1 = Unet(x=input_1p, training=flag).model saver1 = tf.train.Saver() #logits_1 = Unet(x = input_1p, training=flag).model llogits_1 = tf.nn.softmax(logits_1) checkpoint1 = tf.train.get_checkpoint_state("./pre-trained") checkpoint1 = tf.train.get_checkpoint_state("./pre-trained") config = tf.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.4 with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer())
def eddy(path): flows = io.loadmat(os.path.join(os.path.sep, path, 'vel_struct.mat')) mags = io.loadmat(os.path.join(os.path.sep, path, 'mag_struct.mat')) flow = flows['mrStruct'] mag = mags['mrStruct'] flow = flow['dataAy'] mag = mag['dataAy'] flow = flow[0, 0] mag = mag[0, 0] imaspeed = np.power(np.mean(np.square(flow), axis=3), 0.5) minV = np.amin(0.7 * np.reshape(mag, (-1, 1))) maxV = np.amax(0.7 * np.reshape(mag, (-1, 1))) tmp = mag > maxV not_tmp = np.logical_not(tmp) mag = np.multiply(mag, not_tmp) + np.multiply(tmp, maxV) tmp = mag < minV not_tmp = np.logical_not(tmp) mag = np.multiply(mag, not_tmp) + np.multiply(tmp, minV) mag = (mag - minV) / (maxV - minV) pcmra = np.multiply(imaspeed, mag) pcmra = np.squeeze(np.mean(np.square(pcmra), axis=3)) std_flow = np.std(imaspeed, axis=3) del flow del mag #print(list(f.keys())) #print(list(g.keys())) #tv = f['test'] #la = g['test'] #print(list(tv.keys())) #flow = f[tv['data'][0,0]].value #labe = g[la['truth'][0,0]].value #mask = g[la['mask'][0,0]].value #print(flow.dtype) #print(labe.dtype) #print(mask.dtype) #print(la) #te = k['test'] #print(list(la.keys())) #flow = f[tv['data'][0,0]].value #labe = g[la['data'][0,0]].value #test = k[te['data'][0,0]].value #print( flow.shape) #print(tv.shape) #for i in range(1): #flow = f[tv['data'][i,0]].value #labe = g[la['truth'][i,0]].value #mask = g[la['mask'][i,0]].value print(pcmra.shape) print(std_flow.shape) pcmra = np.expand_dims(pcmra, axis=3) std_flow = np.expand_dims(std_flow, axis=3) X = np.concatenate((pcmra, std_flow), axis=3) X = X.astype(np.float32) del pcmra del std_flow #Y = labe.transpose() #Z = mask.transpose() #dim1 = X.shape[0] #dim2 = X.shape[1] #h1 = round((dim1-160)/2) #w2 = round((dim2-80)/2) #X = X.astype(np.float32) #Y = Y.astype(np.float32) #X = X[:140,:,:] #Y = Y[:140,:,:] #Z = Z[:140,:,:] for j in range(2): X[..., j] = (X[..., j] - np.amin(X[..., j])) / (np.amax(X[..., j]) - np.amin(X[..., j])) #X = X[10:138,...] print(X.shape) #images.append(X) #labels.append(Y) #masks.append(Z) #print(len(images)) #print(len(labels)) #print(images[0].shape) #print(labels[0].shape) #print(masks[0].shape) #print(images[0].dtype) #print(labels[0].dtype) #print(np.unique(labels[0])) #print(np.unique(masks[0])) """ def _bytes_feature(value): return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) def _int64_feature(value): return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) #plt.imshow(train_image[0]) #plt.show() s = list(range(len(images))) print(s) #random.shuffle(s) test_filename = 'test_eddy.tfrecords' writer = tf.python_io.TFRecordWriter(test_filename) for i in s: image_raw = images[i].tostring() label_raw = labels[i].tostring() #mask_raw = masks[i].tostring() height = images[i].shape[0] width = images[i].shape[1] depth = images[i].shape[2] features = {'test/image': _bytes_feature(image_raw), 'test/label': _bytes_feature(label_raw), #'test/mask': _bytes_feature(mask_raw), 'test/height': _int64_feature(height), 'test/depth':_int64_feature(depth), 'test/width': _int64_feature(width)} examples = tf.train.Example(features = tf.train.Features(feature = features)) writer.write(examples.SerializeToString()) writer.close() """ input_ = tf.convert_to_tensor(X, dtype=tf.float32) input_ = tf.transpose(input_, perm=[2, 0, 1, 3]) input_ = tf.image.resize_image_with_crop_or_pad(input_, 160, 96) #input_ = tf.image.resize_image_with_crop_or_pad(input_, 160, 130) input_ = tf.transpose(input_, perm=[1, 2, 0, 3]) input_ = tf.squeeze(input_) #input_ = (input_ - tf.reduce_min(input_))/(tf.reduce_max(input_) - tf.reduce_min(input_)) input_ = tf.expand_dims(input_, dim=0) #input_ = tf.expand_dims(input_, dim=4) input_p = tf.placeholder(tf.float32, shape=[None, 160, 96, None, 2]) #input_p = tf.placeholder(tf.float32, shape=[None, 160,130,None, 2]) flag = tf.placeholder(tf.bool) logits_1 = Unet(x=input_p, training=flag).model saver1 = tf.train.Saver() llogits_1 = tf.nn.softmax(logits_1) checkpoint1 = tf.train.get_checkpoint_state("./new_eddy") config = tf.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.4 with tf.Session(config=config) as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) image = sess.run([input_]) saver1.restore(sess, checkpoint1.model_checkpoint_path) h, seg1 = sess.run([logits_1, llogits_1], feed_dict={ input_p: image[0], flag: True }) #h , seg2 = sess.run([logits_1, llogits_1], feed_dict={input_1p: image[1], flag: True}) #h , seg3 = sess.run([logits_1, llogits_1], feed_dict={input_1p: image[2], flag: True}) sess.close() tf.keras.backend.clear_session() mask1 = seg1[..., 1] mask1 = mask1 > 0.5 print(len(image)) #U = image[0] #plt.imshow(np.squeeze(U[...,16,0]), cmap=plt.get_cmap('gray')) #plt.imshow(np.squeeze(mask1[...,16]), alpha=0.3) #plt.show() mask1 = np.squeeze(mask1) print(mask1.shape) [H, W, D] = mask1.shape xx = np.arange(W) xxx = np.tile(xx, [H, 1]) print(xxx.shape) yy = np.arange(H) yy = yy[::-1] yy = np.expand_dims(yy, axis=1) #yy = np.fliplr([yy])[0] #print(yy) yyy = np.tile(yy, (1, W)) flow = flows['mrStruct']['dataAy'][0, 0] HH = flow.shape[0] WW = flow.shape[1] h1 = (HH - 160) / 2 w1 = (WW - 96) / 2 print(h1) print(w1) h1 = np.int(h1) w1 = np.int(w1) flow = flow[h1:HH - h1, w1:WW - w1, ...] #plt.imshow(np.squeeze(flow[...,16,0,5]), cmap=plt.get_cmap('gray')) #plt.imshow(np.squeeze(mask1[...,16]), alpha=0.3) #plt.show() print(np.amin(flow)) print(flow.shape) #yyy = np.fliplr(yyy) yyy = yyy + 1 xxx = xxx + 1 print(yyy) print(xxx) #new_flow = np.zeros(flow.shape) print(flow.shape) for i in range(flow.shape[2]): imaFlow = np.squeeze(flow[..., i, :, :]) #print(imaFlow.shape) statMask = mask1[..., i] statMask = statMask.astype(int) imaFlow1 = np.zeros(imaFlow.shape) #statMask = statMask.astype(np.float32) for k in range(imaFlow.shape[2]): tt = flow.shape[4] phi, alpha, beta = lsqf2(imaFlow[..., k, tt - 1], statMask, xxx, yyy) phi = phi.astype(np.float32) alpha = alpha.astype(np.float32) beta = beta.astype(np.float32) fitPlane = phi + alpha * xxx + beta * yyy zeroMask = imaFlow[:, :, k, tt - 1] != 0 zeroPlane = np.multiply(fitPlane, zeroMask) #print(zeroPlane.shape) zeroPlane = np.expand_dims(zeroPlane, axis=2) factor3D = np.tile(zeroPlane, (1, 1, tt)) imaFlow1[:, :, k, :] = np.squeeze(imaFlow[:, :, k, :]) - factor3D #print(phi) #print(alpha) #print(beta) #print(Md) flow[:, :, i, :, :] = imaFlow1 del imaFlow del imaFlow1 print("Finished flow loop in Eddy Current") #time.sleep(10) #print(flow.shape) mrStruct = flows['mrStruct'] mag = mags['mrStruct']['dataAy'][0, 0] mag = mag[h1:HH - h1, w1:WW - w1, ...] mrStruct1 = mags['mrStruct'] mrStruct1['dataAy'][0, 0] = mag io.savemat('MK/mag_struct.mat', {'mrStruct': mrStruct1}) del mag del flows io.savemat('MK/new_vel.mat', {'data': flow}) io.savemat('MK/eddy_mask.mat', {'data': mask1}) return image, mrStruct, flow
examples = tf.train.Example(features = tf.train.Features(feature = features)) writer.write(examples.SerializeToString()) writer.close() """ input_ = tf.convert_to_tensor(X, dtype=tf.float16) input_ = tf.transpose(input_, perm=[2,0,1,3]) input_ = tf.image.resize_image_with_crop_or_pad(input_, 160, 96) input_ = tf.transpose(input_, perm=[1,2,0,3]) #input_ = (input_ - tf.reduce_min(input_))/(tf.reduce_max(input_) - tf.reduce_min(input_)) #input_ = tf.expand_dims(input_, dim=0) #input_ = tf.expand_dims(input_, dim=4) input_p = tf.placeholder(tf.float32, shape=[1, 160,96,None, 2]) flag = tf.placeholder(tf.bool) logits_1 = Unet(x = input_p, training=flag).model saver1 = tf.train.Saver() llogits_1 = tf.nn.softmax(logits_1) checkpoint1 = tf.train.get_checkpoint_state("./new_eddy") config = tf.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.per_process_gpu_memory_fraction = 0.4 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) image = sess.run([input_]) saver1.restore(sess, checkpoint1.model_checkpoint_path) h , seg1 = sess.run([logits_1, llogits_1], feed_dict={input_p: image, flag: True}) #h , seg2 = sess.run([logits_1, llogits_1], feed_dict={input_1p: image[1], flag: True}) #h , seg3 = sess.run([logits_1, llogits_1], feed_dict={input_1p: image[2], flag: True})