def main(): import tensorflow as tf from tensorflow.keras import backend as K from futils import segmentor as v_seg config = tf.ConfigProto() config.gpu_options.allow_growth = True # dynamically grow the memory used on the GPU sess = tf.Session(config=config) K.set_session( sess) # set this TensorFlow session as the default session for Keras task = 'lobe' str_name = "1600642845_85_lrlb0.0001lrvs1e-05mtscale0netnol-nnl-novpm0.0nldLUNA16ao0ds0pps100lbnb17vsnb50nlnb400ptsz144ptzsz96" # mypath = Mypath(task=task, current_time=str_name) # set task=vessel to predict the lobe masks of SSc model_name = '/data/jjia/new/models/' + task + '/' + str_name + '_valid.hdf5' # model_name = mypath.model_fpath_best_whole("valid", str_name) tr_sp, tr_z_sp = 1.4, 2.5 tr_sz, tr_z_sz = None, None pt_sz, pt_z_sz = 144, 96 print('patch_sz', pt_sz, 'patch_z_size', pt_z_sz) stride = 0.25 segment = v_seg.v_segmentor(batch_size=1, model=model_name, ptch_sz=pt_sz, ptch_z_sz=pt_z_sz, trgt_sz=tr_sz, trgt_z_sz=tr_z_sz, trgt_space_list=[tr_z_sp, tr_sp, tr_sp], task=task, attention=False) print('stride is', stride) sub_dirs = ["COPE", "CONP", "NCPE"] for sub_dir in sub_dirs: write_preds_to_disk( segment=segment, data_dir="/data/jjia/new/COVID/ori/" + sub_dir + "/001", preds_dir="/data/jjia/new/COVID/results/" + sub_dir + "/001", number=2, stride=stride, workers=1, qsize=1)
labels = [1] fissure, lung = False, True if fissure: gntFissure(mypath.pred_path("valid", sub_dir=sub_dir, cntd_pts=biggest_5_lobe), radiusValue=radiusValue, workers=10) else: segment = v_seg.v_segmentor( batch_size=1, model=model_name, ptch_sz=pt_sz, ptch_z_sz=pt_z_sz, trgt_sz=tr_sz, trgt_z_sz=tr_z_sz, trgt_space_list=[tr_z_sp, tr_sp, tr_sp], task=task, attention=False) print('stride is', stride) workers = 5 if sub_dir == "GLUCOLD" else 5 write_preds_to_disk( segment=segment, data_dir=mypath.ori_ct_path("valid", sub_dir=sub_dir), preds_dir=mypath.pred_path("valid", sub_dir=sub_dir), number=None, stride=stride, workers=workers,
def main(): mypath = Mypath("lobe") label = [0, 4, 5, 6, 7, 8] net = sr_model(lr=0.0001) model_figure_fpath = mypath.model_figure_path() + '/sr.png' plot_model(net, show_shapes=True, to_file=model_figure_fpath) print('successfully plot model structure at: ', model_figure_fpath) # save model architecture and config model_json = net.to_json() with open(mypath.json_fpath(), "w") as json_file: json_file.write(model_json) print('successfully write new json file of task lobe', mypath.json_fpath()) x_dir = os.path.join(mypath.train_dir(), "gdth_ct", mypath.sub_dir()) x_ts_dir = os.path.join(mypath.valid_dir(), "gdth_ct", mypath.sub_dir()) tr_it = Sr_data_itr(x_dir=x_dir, x_ext=".nrrd", nb=18, data_argum=1, pps=1000) # enqueuer_train = GeneratorEnqueuer(tr_it.generator(), use_multiprocessing=True) # train_datas = enqueuer_train.get() # enqueuer_train.start() train_datas = tr_it.generator() train_csvlogger = callbacks.CSVLogger(mypath.train_log_fpath(), separator=',', append=True) saver_train = callbacks.ModelCheckpoint( filepath=mypath.model_fpath_best_train(), save_best_only=True, monitor='loss', save_weights_only=True, save_freq=1) steps = 100 for i in range(steps): # x, y = next(train_datas) # x, y = x[np.newaxis, ...], y[np.newaxis, ...] net.fit(train_datas, steps_per_epoch=18000, use_multiprocessing=True, callbacks=[saver_train, train_csvlogger]) # period_valid = 50000 # if i % (period_valid) == (period_valid-1): # one epoch for lobe segmentation, 20 epochs for vessel segmentation # # save predicted results and compute the dices for phase in ['train', 'valid']: segment = v_seg.v_segmentor(batch_size=1, model=mypath.model_fpath_best_train(), ptch_sz=144, ptch_z_sz=96, trgt_space_list=[None, None, None], task='lobe', sr=True) write_preds_to_disk(segment=segment, data_dir=mypath.ori_ct_path(phase), preds_dir=mypath.pred_path(phase), number=1, stride=0.25) # set stride 0.8 to save time write_dices_to_csv(step_nb=i, labels=label, gdth_path=mypath.gdth_path(phase), pred_path=mypath.pred_path(phase), csv_file=mypath.dices_fpath(phase)) save_model_best(dice_file=mypath.dices_fpath(phase), phase=phase, segment=segment, model_fpath=mypath.model_fpath_best_valid()) print('step number', i, 'lr for super resolutin is', K.eval(net.optimizer.lr), file=sys.stderr)
try: ptch_sz = int(model_number[-10:-7]) except: ptch_sz = int(model_number[-9:-7]) print('ptch_sz, {}, ptch_z_sz, {}'.format(ptch_sz, ptch_z_sz)) if fixed_space: trgt_space = [0.3, 0.5, 0.5] elif fixed_size: trgt_size = [128, 256, 256] else: trgt_size = None #LOAD THE MODEL segment = v_seg.v_segmentor(batch_size=1, model='models/' + task + '/' + model_number + 'MODEL.hdf5', patching=True) print('finished first model loading') model = segment.v plot_model(model, show_shapes=True, to_file='lobemodel.png') # # K.set_learning_phase(0) # task = 'lobe' # model_number = '1583105196.4919333_0.00010a_o_0.5ds2dr1bn1fs16ptsz144ptzsz80' # # #LOAD THE MODEL # segment_ = v_seg.v_segmentor(batch_size=1, model='models/' + task + '/' + model_number+'MODEL.hdf5', patching=False) # print('finished first model loading') # model_ = segment_.v