def get_pretrained_weights(flags): save_name = os.path.join(flags.weight_dir, 'weight.pkl') if not os.path.exists(save_name): X = tf.placeholder(tf.float32, shape=[None, flags.input_size[0], flags.input_size[1], 3], name='X') y = tf.placeholder(tf.int32, shape=[None, flags.input_size[0], flags.input_size[1], 1], name='y') mode = tf.placeholder(tf.bool, name='mode') model = uabMakeNetwork_UNet.UnetModelCrop({'X': X, 'Y': y}, trainable=mode, model_name=flags.model_name, input_size=flags.input_size, batch_size=flags.batch_size, learn_rate=flags.learning_rate, decay_step=flags.decay_step, decay_rate=flags.decay_rate, epochs=flags.epochs, start_filter_num=flags.sfn) model.create_graph('X', class_num=flags.num_classes) train_vars = [v for v in tf.global_variables() if 'global_step' not in v.name] weight_dict = dict() with tf.Session() as sess: model.load(flags.model_dir, sess, epoch=95) for v in train_vars: theta = sess.run(v) weight_dict[v.name] = theta ersa_utils.save_file(save_name, weight_dict) else: weight_dict = ersa_utils.load_file(save_name) tf.reset_default_graph() return weight_dict
def get_pretrained_weights(weight_dir, model_dir): save_name = os.path.join(weight_dir, 'weight.pkl') if not os.path.exists(save_name): X = tf.placeholder(tf.float32, shape=[None, input_size[0], input_size[1], 3], name='X') y = tf.placeholder(tf.int32, shape=[None, input_size[0], input_size[1], 1], name='y') mode = tf.placeholder(tf.bool, name='mode') model = uabMakeNetwork_UNet.UnetModelCrop({'X': X, 'Y': y}, trainable=mode, input_size=input_size, start_filter_num=32) model.create_graph('X', class_num=2) train_vars = [v for v in tf.trainable_variables()] weight_dict = dict() with tf.Session() as sess: model.load(model_dir, sess, epoch=95) for v in train_vars: theta = sess.run(v) weight_dict[v.name] = theta ersa_utils.save_file(save_name, weight_dict) else: weight_dict = ersa_utils.load_file(save_name) tf.reset_default_graph() return weight_dict
def evaluate_on_a_patch( chip_size, X_batch, pretrained_model_dir=r'/hdd/Models/UnetCrop_inria_aug_grid_PS(572, 572)_BS5_EP100_LR0.0001_DS60_DR0.1_SFN32' ): # experiment settings batch_size = 9 # mini-batch size learn_rate = 1e-4 # learning rate decay_step = 60 # learn rate dacay after 60 epochs decay_rate = 0.1 # learn rate decay to 0.1*before epochs = 100 # total number of epochs to rum start_filter_num = 32 # the number of filters at the first layer model_name = 'inria_aug_grid' # a suffix for model name # make network # define place holder tf.reset_default_graph() X = tf.placeholder(tf.float32, shape=[None, chip_size[0], chip_size[1], 3], name='X') y = tf.placeholder(tf.int32, shape=[None, chip_size[0], chip_size[1], 1], name='y') mode = tf.placeholder(tf.bool, name='mode') model = uabMakeNetwork_UNet.UnetModelCrop( { 'X': X, 'Y': y }, trainable=mode, model_name=model_name, input_size=chip_size, batch_size=batch_size, learn_rate=learn_rate, decay_step=decay_step, decay_rate=decay_rate, epochs=epochs, start_filter_num=start_filter_num) model.create_graph('X', class_num=2) with tf.Session() as sess: init = tf.global_variables_initializer() sess.run(init) model.load(pretrained_model_dir, sess) pred = sess.run(model.output, feed_dict={ model.inputs['X']: X_batch, model.trainable: False }) pred = pred[0, :, :, :] pred = np.argmax(pred, axis=2) return pred
# make the model # define place holder X = tf.placeholder(tf.float32, shape=[None, input_size[0], input_size[1], 3], name='X') Z = tf.placeholder(tf.float32, shape=[None, input_size[0], input_size[1], 3], name='Z') y = tf.placeholder(tf.int32, shape=[None, input_size[0], input_size[1], 1], name='y') mode = tf.placeholder(tf.bool, name='mode') model = uabMakeNetwork_UNet.UnetModelCrop({ 'X': X, 'Y': y }, trainable=mode, input_size=input_size, batch_size=batch_size) # create graph model.create_graph('X', class_num=2) # evaluate on tiles model.evaluate(file_list, file_list_truth, parent_dir, parent_dir_truth, input_size, None, batch_size, img_mean,
def main(flags): # make network # define place holder X = tf.placeholder(tf.float32, shape=[None, flags.input_size[0], flags.input_size[1], 3], name='X') y = tf.placeholder(tf.int32, shape=[None, flags.input_size[0], flags.input_size[1], 1], name='y') mode = tf.placeholder(tf.bool, name='mode') model = uabMakeNetwork_UNet.UnetModelCrop({'X':X, 'Y':y}, trainable=mode, model_name=flags.model_name, input_size=flags.input_size, batch_size=flags.batch_size, learn_rate=flags.learning_rate, decay_step=flags.decay_step, decay_rate=flags.decay_rate, epochs=flags.epochs, start_filter_num=flags.sfn) model.create_graph('X', class_num=flags.num_classes) # create collection # the original file is in /ei-edl01/data/uab_datasets/inria blCol = uab_collectionFunctions.uabCollection('inria') opDetObj = bPreproc.uabOperTileDivide(255) # inria GT has value 0 and 255, we map it back to 0 and 1 # [3] is the channel id of GT rescObj = uabPreprocClasses.uabPreprocMultChanOp([], 'GT_Divide.tif', 'Map GT to (0, 1)', [3], opDetObj) rescObj.run(blCol) img_mean = blCol.getChannelMeans([0, 1, 2]) # get mean of rgb info # extract patches extrObj = uab_DataHandlerFunctions.uabPatchExtr([0, 1, 2, 4], # extract all 4 channels cSize=flags.input_size, # patch size as 572*572 numPixOverlap=int(model.get_overlap()/2), # overlap as 92 extSave=['jpg', 'jpg', 'jpg', 'png'], # save rgb files as jpg and gt as png isTrain=True, gtInd=3, pad=model.get_overlap()) # pad around the tiles patchDir = extrObj.run(blCol) # make data reader # use uabCrossValMaker to get fileLists for training and validation idx, file_list = uabCrossValMaker.uabUtilGetFolds(patchDir, 'fileList.txt', 'force_tile') # use first 5 tiles for validation file_list_train = uabCrossValMaker.make_file_list_by_key(idx, file_list, [i for i in range(6, 37)]) file_list_valid = uabCrossValMaker.make_file_list_by_key(idx, file_list, [i for i in range(0, 6)]) with tf.name_scope('image_loader'): # GT has no mean to subtract, append a 0 for block mean dataReader_train = uabDataReader.ImageLabelReader([3], [0, 1, 2], patchDir, file_list_train, flags.input_size, flags.tile_size, flags.batch_size, dataAug='flip,rotate', block_mean=np.append([0], img_mean)) # no augmentation needed for validation dataReader_valid = uabDataReader.ImageLabelReader([3], [0, 1, 2], patchDir, file_list_valid, flags.input_size, flags.tile_size, flags.batch_size, dataAug=' ', block_mean=np.append([0], img_mean)) # train start_time = time.time() model.train_config('X', 'Y', flags.n_train, flags.n_valid, flags.input_size, uabRepoPaths.modelPath, loss_type='xent') model.run(train_reader=dataReader_train, valid_reader=dataReader_valid, pretrained_model_dir=None, # train from scratch, no need to load pre-trained model isTrain=True, img_mean=img_mean, verb_step=100, # print a message every 100 step(sample) save_epoch=5, # save the model every 5 epochs gpu=GPU, tile_size=flags.tile_size, patch_size=flags.input_size, continue_dir=model.ckdir) duration = time.time() - start_time print('duration {:.2f} hours'.format(duration/60/60))
# make network # define place holder X = tf.placeholder(tf.float32, shape=[None, chip_size[0], chip_size[1], 3], name='X') y = tf.placeholder(tf.int32, shape=[None, chip_size[0], chip_size[1], 1], name='y') mode = tf.placeholder(tf.bool, name='mode') model = uabMakeNetwork_UNet.UnetModelCrop({ 'X': X, 'Y': y }, trainable=mode, input_size=chip_size, batch_size=batch_size, learn_rate=learn_rate, decay_step=decay_step, decay_rate=decay_rate, epochs=epochs, start_filter_num=start_filter_num) model.create_graph('X', class_num=2) # If you only want to load a specific number of layers, you have to do load_weight() here # don't give pretrained_model_dir and layers2keep when calling model.run(), that will cause problem model.load_weights(pre_trained_model_dir, layers2keep) # create collection # the original file is in /ei-edl01/data/uab_datasets/inria blCol = uab_collectionFunctions.uabCollection('inria') opDetObj = bPreproc.uabOperTileDivide( 255) # inria GT has value 0 and 255, we map it back to 0 and 1
def main(flags): city_list = ['austin', 'chicago', 'kitsap', 'tyrol-w', 'vienna'] flags.llh_file_dir = flags.llh_file_dir.format(flags.finetune_city) weight = np.load(flags.llh_file_dir) # make network # define place holder X = tf.placeholder(tf.float32, shape=[None, flags.input_size[0], flags.input_size[1], 3], name='X') y = tf.placeholder(tf.int32, shape=[None, flags.input_size[0], flags.input_size[1], 1], name='y') mode = tf.placeholder(tf.bool, name='mode') model = uabMakeNetwork_UNet.UnetModelCrop({'X': X, 'Y': y}, trainable=mode, model_name=flags.model_name, input_size=flags.input_size, batch_size=flags.batch_size, learn_rate=flags.learning_rate, decay_step=flags.decay_step, decay_rate=flags.decay_rate, epochs=flags.epochs, start_filter_num=flags.sfn) model.create_graph('X', class_num=flags.num_classes) # create collection # the original file is in /ei-edl01/data/uab_datasets/inria blCol = uab_collectionFunctions.uabCollection('inria') opDetObj = bPreproc.uabOperTileDivide(255) # inria GT has value 0 and 255, we map it back to 0 and 1 # [3] is the channel id of GT rescObj = uabPreprocClasses.uabPreprocMultChanOp([], 'GT_Divide.tif', 'Map GT to (0, 1)', [3], opDetObj) rescObj.run(blCol) img_mean = blCol.getChannelMeans([0, 1, 2]) # get mean of rgb info # extract patches extrObj = uab_DataHandlerFunctions.uabPatchExtr([0, 1, 2, 4], # extract all 4 channels cSize=flags.input_size, # patch size as 572*572 numPixOverlap=int(model.get_overlap()/2), # overlap as 92 extSave=['jpg', 'jpg', 'jpg', 'png'], # save rgb files as jpg and gt as png isTrain=True, gtInd=3, pad=model.get_overlap()) # pad around the tiles patchDir = extrObj.run(blCol) # make data reader # use uabCrossValMaker to get fileLists for training and validation idx_city, file_list = uabCrossValMaker.uabUtilGetFolds(patchDir, 'fileList.txt', 'city') idx_tile, _ = uabCrossValMaker.uabUtilGetFolds(patchDir, 'fileList.txt', 'force_tile') idx = [j * 10 + i for i, j in zip(idx_city, idx_tile)] # use first city for validation filter_train = [] filter_valid = [] for i in range(5): for j in range(1, 37): if i != flags.finetune_city and j > 5: filter_train.append(j * 10 + i) elif i == flags.finetune_city and j <= 5: filter_valid.append(j * 10 + i) # use first city for validation file_list_train = uabCrossValMaker.make_file_list_by_key(idx, file_list, filter_train) file_list_valid = uabCrossValMaker.make_file_list_by_key(idx, file_list, filter_valid) dataReader_train = uabDataReader.ImageLabelReaderPatchSampleControl( [3], [0, 1, 2], patchDir, file_list_train, flags.input_size, flags.batch_size, weight, dataAug='flip,rotate', block_mean=np.append([0], img_mean)) # no augmentation needed for validation dataReader_valid = uabDataReader.ImageLabelReader([3], [0, 1, 2], patchDir, file_list_valid, flags.input_size, flags.batch_size, dataAug=' ', block_mean=np.append([0], img_mean), batch_code=0) # train start_time = time.time() model.train_config('X', 'Y', flags.n_train, flags.n_valid, flags.input_size, uabRepoPaths.modelPath, loss_type='xent', par_dir='Inria_Domain_Selection') model.run(train_reader=dataReader_train, valid_reader=dataReader_valid, pretrained_model_dir=flags.pred_model_dir.format(flags.finetune_city), isTrain=True, img_mean=img_mean, verb_step=100, # print a message every 100 step(sample) save_epoch=5, # save the model every 5 epochs gpu=GPU, tile_size=flags.tile_size, patch_size=flags.input_size ) duration = time.time() - start_time print('duration {:.2f} hours'.format(duration/60/60))
def main(flags): city_dict = { 'austin': 0, 'chicago': 1, 'kitsap': 2, 'tyrol-w': 3, 'vienna': 4 } city_alpha = [0.2, 0.5, 0.1, 0.1, 0.1] # make network # define place holder X = tf.placeholder( tf.float32, shape=[None, flags.input_size[0], flags.input_size[1], 3], name='X') y = tf.placeholder( tf.int32, shape=[None, flags.input_size[0], flags.input_size[1], 1], name='y') mode = tf.placeholder(tf.bool, name='mode') model = uabMakeNetwork_UNet.UnetModelCrop({ 'X': X, 'Y': y }, trainable=mode, model_name=flags.model_name, input_size=flags.input_size, batch_size=flags.batch_size, learn_rate=flags.learning_rate, decay_step=flags.decay_step, decay_rate=flags.decay_rate, epochs=flags.epochs, start_filter_num=flags.sfn) model.create_graph('X', class_num=flags.num_classes) # create collection # the original file is in /ei-edl01/data/uab_datasets/inria blCol = uab_collectionFunctions.uabCollection('inria') opDetObj = bPreproc.uabOperTileDivide( 255) # inria GT has value 0 and 255, we map it back to 0 and 1 # [3] is the channel id of GT rescObj = uabPreprocClasses.uabPreprocMultChanOp([], 'GT_Divide.tif', 'Map GT to (0, 1)', [3], opDetObj) rescObj.run(blCol) img_mean = blCol.getChannelMeans([0, 1, 2]) # get mean of rgb info # extract patches extrObj = uab_DataHandlerFunctions.uabPatchExtr( [0, 1, 2, 4], cSize=flags.input_size, numPixOverlap=int(model.get_overlap()), extSave=['jpg', 'jpg', 'jpg', 'png'], isTrain=True, gtInd=3, pad=model.get_overlap() / 2) patchDir = extrObj.run(blCol) # make data reader # use uabCrossValMaker to get fileLists for training and validation idx, file_list = uabCrossValMaker.uabUtilGetFolds(patchDir, 'fileList.txt', 'force_tile') # use first 5 tiles for validation file_list_train = uabCrossValMaker.make_file_list_by_key( idx, file_list, [i for i in range(6, 37)]) file_list_valid = uabCrossValMaker.make_file_list_by_key( idx, file_list, [i for i in range(0, 6)]) dataReader_train = uabDataReader.ImageLabelReaderCitySampleControl( [3], [0, 1, 2], patchDir, file_list_train, flags.input_size, flags.batch_size, city_dict, city_alpha, dataAug='flip,rotate', block_mean=np.append([0], img_mean)) # no augmentation needed for validation dataReader_valid = uabDataReader.ImageLabelReaderCitySampleControl( [3], [0, 1, 2], patchDir, file_list_valid, flags.input_size, flags.batch_size, city_dict, city_alpha, dataAug=' ', block_mean=np.append([0], img_mean)) # train start_time = time.time() model.train_config('X', 'Y', flags.n_train, flags.n_valid, flags.input_size, uabRepoPaths.modelPath, loss_type='xent', par_dir='Inria_Domain') model.run( train_reader=dataReader_train, valid_reader=dataReader_valid, pretrained_model_dir=PRED_DIR, isTrain=True, img_mean=img_mean, verb_step=100, # print a message every 100 step(sample) save_epoch=5, # save the model every 5 epochs gpu=GPU, tile_size=flags.tile_size, patch_size=flags.input_size, ) duration = time.time() - start_time print('duration {:.2f} hours'.format(duration / 60 / 60))
orig_img_name = os.path.join(orig_img_dir, task_id, '{}.tif'.format(img_id)) orig_img = imageio.imread(orig_img_name) orig_img = scipy.misc.imresize(orig_img, [2541, 2541]) orig_img = cv2.LUT(orig_img, table) imageio.imsave(os.path.join(adjust_save_dir, '{}.tif'.format(img_id)), orig_img) img_mean = get_sum_of_channel(orig_img) / (2541 ** 2) print(img_mean) tf.reset_default_graph() # make the model # define place holder X = tf.placeholder(tf.float32, shape=[None, input_size[0], input_size[1], 3], name='X') y = tf.placeholder(tf.int32, shape=[None, input_size[0], input_size[1], 1], name='y') mode = tf.placeholder(tf.bool, name='mode') model = uabMakeNetwork_UNet.UnetModelCrop({'X': X, 'Y': y}, trainable=mode, input_size=input_size, batch_size=5, start_filter_num=32) # create graph model.create_graph('X', class_num=2) reader = uabDataReader.ImageLabelReader(gtInds=[0], dataInds=[0], nChannels=3, parentDir=adjust_save_dir, chipFiles=[['{}.tif'.format(img_id)]], chip_size=input_size, tile_size=tile_size, batchSize=5, block_mean=img_mean, overlap=model.get_overlap(), padding=np.array((model.get_overlap() / 2, model.get_overlap() / 2)), isTrain=False)
X = tf.placeholder(tf.float32, shape=[None, chip_size[0], chip_size[1], 3], name='X') y = tf.placeholder(tf.int32, shape=[None, chip_size[0], chip_size[1], 1], name='y') mode = tf.placeholder( tf.bool, name='mode') # This controls if you'll update weights or not # Set this True when training model = uabMakeNetwork_UNet.UnetModelCrop( { 'X': X, 'Y': y }, trainable=mode, # control if you're training or not input_size=chip_size, # input size to NN, same as extracted # patch size batch_size=batch_size, # mini-batch size learn_rate=learn_rate, # learning rate decay_step=decay_step, # learn rate decay after 60 epochs decay_rate=decay_rate, # learn rate decay to 0.1*before epochs=epochs, # total number of epochs to run start_filter_num=start_filter_num) # number of filters at the first layer model.create_graph('X', class_num=2) # TensorFlow will now draw the graph # create collection # the original file is in /ei-edl01/data/uab_datasets/inria blCol = uab_collectionFunctions.uabCollection('inria') opDetObj = bPreproc.uabOperTileDivide( 255) # inria GT has value 0 and 255, we map it back to 0 and 1 # [3] is the channel id of GT rescObj = uabPreprocClasses.uabPreprocMultChanOp([], 'GT_Divide.tif',
def main(flags): np.random.seed(int(flags.run_id)) tf.set_random_seed(int(flags.run_id)) if flags.start_layer >= 10: pass else: flags.model_name += '_up{}'.format(flags.start_layer) # make network # define place holder X = tf.placeholder( tf.float32, shape=[None, flags.input_size[0], flags.input_size[1], 3], name='X') y = tf.placeholder( tf.int32, shape=[None, flags.input_size[0], flags.input_size[1], 1], name='y') mode = tf.placeholder(tf.bool, name='mode') model = uabMakeNetwork_UNet.UnetModelCrop({ 'X': X, 'Y': y }, trainable=mode, model_name=flags.model_name, input_size=flags.input_size, batch_size=flags.batch_size, learn_rate=flags.learning_rate, decay_step=flags.decay_step, decay_rate=flags.decay_rate, epochs=flags.epochs, start_filter_num=flags.sfn) model.create_graph('X', class_num=flags.num_classes) # create collection # the original file is in /ei-edl01/data/uab_datasets/inria blCol = uab_collectionFunctions.uabCollection(flags.ds_name) blCol.readMetadata() img_mean = blCol.getChannelMeans([1, 2, 3]) # get mean of rgb info # extract patches extrObj = uab_DataHandlerFunctions.uabPatchExtr( [0, 1, 2, 3], cSize=flags.input_size, numPixOverlap=int(model.get_overlap()), extSave=['png', 'jpg', 'jpg', 'jpg'], isTrain=True, gtInd=0, pad=int(model.get_overlap() // 2)) patchDir = extrObj.run(blCol) # make data reader # use first 5 tiles for validation idx, file_list = uabCrossValMaker.uabUtilGetFolds(patchDir, 'fileList.txt', 'tile') file_list_train = uabCrossValMaker.make_file_list_by_key( idx, file_list, [0, 1, 2, 3]) file_list_valid = uabCrossValMaker.make_file_list_by_key( idx, file_list, [4, 5]) with tf.name_scope('image_loader'): # GT has no mean to subtract, append a 0 for block mean dataReader_train = uabDataReader.ImageLabelReader( [0], [1, 2, 3], patchDir, file_list_train, flags.input_size, flags.tile_size, flags.batch_size, dataAug='flip,rotate', block_mean=np.append([0], img_mean)) # no augmentation needed for validation dataReader_valid = uabDataReader.ImageLabelReader([0], [1, 2, 3], patchDir, file_list_valid, flags.input_size, flags.tile_size, flags.batch_size, dataAug=' ', block_mean=np.append( [0], img_mean)) # train start_time = time.time() if flags.start_layer >= 10: model.train_config('X', 'Y', flags.n_train, flags.n_valid, flags.input_size, uabRepoPaths.modelPath, loss_type='xent', par_dir='aemo/{}'.format(flags.ds_name)) else: model.train_config('X', 'Y', flags.n_train, flags.n_valid, flags.input_size, uabRepoPaths.modelPath, loss_type='xent', par_dir='aemo/{}'.format(flags.ds_name), train_var_filter=[ 'layerup{}'.format(i) for i in range(flags.start_layer, 10) ]) model.run( train_reader=dataReader_train, valid_reader=dataReader_valid, pretrained_model_dir=flags. model_dir, # train from scratch, no need to load pre-trained model isTrain=True, img_mean=img_mean, verb_step=100, # print a message every 100 step(sample) save_epoch=5, # save the model every 5 epochs gpu=GPU, tile_size=flags.tile_size, patch_size=flags.input_size) duration = time.time() - start_time print('duration {:.2f} hours'.format(duration / 60 / 60))
idx_truth, file_list_truth, [i for i in range(0, 6)], filter_list=[ 'bellingham', 'bloomington', 'sfo', 'tyrol-e', 'innsbruck' ]) img_mean = blCol.getChannelMeans([0, 1, 2]) # make the model # define place holder X = tf.placeholder(tf.float32, shape=[None, 1052, 1052, 3], name='X') y = tf.placeholder(tf.int32, shape=[None, 1052, 1052, 1], name='y') mode = tf.placeholder(tf.bool, name='mode') model = uabMakeNetwork_UNet.UnetModelCrop({ 'X': X, 'Y': y }, trainable=mode, input_size=[1052, 1052], batch_size=1) # create graph model.create_graph('X', class_num=2) # evaluate on tiles iou_return = model.evaluate(file_list_valid, file_list_valid_truth, parent_dir, parent_dir_truth, [1052, 1052], tile_size, batch_size, img_mean, model_dir,