Ejemplo n.º 1
0
    def __init__(self, imdb):
        self.model_path = "trained_models/music/RefineNet-Res101/semseg"
        self.model_name = "RefineNet-Res101"
        self.saved_net = 'backbone'

        # has to be adjusted according to the training scheme used
        self.energy_loss = "softmax"
        self.class_loss = "softmax"
        self.bbox_loss = "reg"

        self.tf_session = None
        self.root_dir = cfg.ROOT_DIR
        self.sess = tf.Session()
        print('Loading model')
        self.input = tf.placeholder(tf.float32, shape=[None, None, None, 1])
        self.network_heads, self.init_fn = build_dwd_net(
            self.input,
            model=self.model_name,
            num_classes=imdb.num_classes,
            pretrained_dir="",
            substract_mean=False)
        self.saver = tf.train.Saver(max_to_keep=1000)
        self.sess.run(tf.global_variables_initializer())
        print("Loading weights")
        self.saver.restore(
            self.sess,
            self.root_dir + "/" + self.model_path + "/" + self.saved_net)
        self.tf_session = self.sess
Ejemplo n.º 2
0
    def __init__(self, parsed, path, imdb):
        self.model_path = path
        self.config = parsed
        self.imdb = imdb

        self.saved_net = "backbone"

        self.tf_session = None
        self.sess = tf.Session()
        print('Loading model')

        if "DeepScores" not in self.model_path:
            self.input = tf.placeholder(tf.float32,
                                        shape=[None, None, None, 3])
        else:
            self.input = tf.placeholder(tf.float32,
                                        shape=[None, None, None, 1])

        print("Initializing Model:" + self.config.model)
        used_heads = set()
        self.used_heads_loss = []
        for _, assign in enumerate(self.config.training_assignements):
            used_heads.add(assign["stamp_func"])
            self.used_heads_loss.append(
                [assign["stamp_func"], assign["stamp_args"]["loss"]])
        self.used_heads = list(used_heads)
        self.used_heads.sort()
        # for now:
        #self.used_heads = ['stamp_class', 'stamp_bbox', 'stamp_energy']
        #self.used_heads = ['stamp_class', 'stamp_energy', 'stamp_bbox']
        #self.used_heads = ['stamp_bbox', 'stamp_class', 'stamp_energy']
        #self.used_heads = ['stamp_bbox', 'stamp_energy', 'stamp_class']
        #self.used_heads = ['stamp_energy', 'stamp_class', 'stamp_bbox']
        #self.used_heads = ['stamp_energy', 'stamp_bbox', 'stamp_class']

        self.network_heads, init_fn = build_dwd_net(
            self.input,
            model=parsed.model,
            num_classes=len(self.imdb._classes),
            pretrained_dir="",
            max_energy=parsed.max_energy,
            substract_mean=False,
            individual_upsamp=self.config.individual_upsamp,
            paired_mode=self.config.paired_data,
            used_heads=self.used_heads,
            sparse_heads=parsed.sparse_heads)

        self.saver = tf.train.Saver(max_to_keep=1000)
        # self.sess.run(tf.global_variables_initializer())
        # self.sess.run(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='deep_watershed/energy_logits_pair0_3'))
        print("Loading weights")
        self.saver.restore(self.sess, self.model_path + "/" + self.saved_net)
        self.tf_session = self.sess
        self.counter = 0
Ejemplo n.º 3
0
    def __init__(self):
        np.random.seed(self.seed)
        tf.set_random_seed(self.seed)

        print('Loading mappings ...')
        mapping = pa.read_csv(self.root_dir + "/" + self.mapping_name)
        mapping = mapping[mapping["Init_name"] != "-1"]
        mapping = mapping.drop(mapping.columns[[0, 2]], axis=1)

        # strip values
        mapping['Init_name'] = mapping['Init_name'].str.strip()

        # reset index
        self.mapping = mapping.reset_index(drop=True)

        # has to be adjusted according to the training scheme used
        self.energy_loss = "softmax"
        self.class_loss = "softmax"
        self.bbox_loss = "reg"

        sess = tf.Session()
        print('Loading model')
        self.input = tf.placeholder(tf.float32,
                                    shape=[None, None, None, 1],
                                    name='image_input')
        dws_heads, init_fn = build_dwd_net(self.input,
                                           model=self.model_name,
                                           num_classes=len(self.mapping),
                                           pretrained_dir="",
                                           substract_mean=False)
        [self.dws_energy, self.class_logits, self.bbox_size] = [
            dws_heads["stamp_energy"][self.energy_loss][-1],
            dws_heads["stamp_class"][self.class_loss][-1],
            dws_heads["stamp_bbox"][self.bbox_loss][-1]
        ]

        # Name the tensors

        saver = tf.train.Saver(max_to_keep=1000)
        sess.run(tf.global_variables_initializer())
        print("Loading weights")
        saver.restore(
            sess,
            self.root_dir + "/" + self.model_path + "/" + self.model_name)
        self.tf_session = sess
Ejemplo n.º 4
0
    def __init__(self, imdb, path, pa, individual_upsamp=False):
        self.model_path = path
        self.model_name = pa.net_type
        self.saved_net = pa.saved_net
        # has to be adjusted according to the training scheme used
        self.energy_loss = pa.energy_loss
        self.class_loss = pa.class_loss
        self.bbox_loss = pa.bbox_loss

        self.tf_session = None
        self.root_dir = cfg.ROOT_DIR
        self.sess = tf.Session()
        print('Loading model')

        if "realistic" in self.model_path:
            self.input = tf.placeholder(tf.float32,
                                        shape=[None, None, None, 3])
        else:
            self.input = tf.placeholder(tf.float32,
                                        shape=[None, None, None, 1])

        self.network_heads, self.init_fn = build_dwd_net(
            self.input,
            model=self.model_name,
            num_classes=imdb.num_classes,
            pretrained_dir="",
            substract_mean=False,
            individual_upsamp=individual_upsamp)

        self.saver = tf.train.Saver(max_to_keep=1000)
        self.sess.run(tf.global_variables_initializer())
        print("Loading weights")
        self.saver.restore(
            self.sess,
            self.root_dir + "/" + self.model_path + "/" + self.saved_net)
        self.tf_session = self.sess
        self.counter = 0
Ejemplo n.º 5
0
def main(parsed):
    args = parsed[0]
    print(args)
    iteration = 1

    np.random.seed(cfg.RNG_SEED)

    # load database
    imdb, roidb, imdb_val, roidb_val, data_layer, data_layer_val = load_database(
        args)

    global nr_classes
    nr_classes = len(imdb._classes)
    args.nr_classes.append(nr_classes)

    # replaces keywords with function handles in training assignements
    save_objectness_function_handles(args, imdb)

    # Debug stuffs
    #
    # try_all_assign(data_layer,args,100)
    # dws_list = perform_dws(data["dws_energy"], data["class_map"], data["bbox_fcn"])
    #

    # def show_image(data, gt_boxes=None):
    #     from PIL import Image, ImageDraw
    #     if gt_boxes is None:
    #         im = Image.fromarray(data[0].astype("uint8"))
    #         im.show()
    #     else:
    #         im = Image.fromarray(data[0].astype("uint8"))
    #         draw = ImageDraw.Draw(im)
    #         # overlay GT boxes
    #         for row in gt_boxes[0]:
    #             draw.rectangle(((row[0], row[1]), (row[2], row[3])), fill="red")
    #         im.show()
    #     return
    #
    # fetched_dat = data_layer.forward(args, [args.training_assignements[0]], None)

    # show_image(fetched_dat["data"],fetched_dat["gt_boxes"])
    # show_image(fetched_dat["data"], None)

    # tensorflow session
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    # input and output tensors
    if "DeepScores" in args.dataset:
        input = tf.placeholder(tf.float32, shape=[None, None, None, 1])
        resnet_dir = cfg.PRETRAINED_DIR + "/DeepScores/"
        refinenet_dir = cfg.PRETRAINED_DIR + "/DeepScores_semseg/"

    elif "MUSICMA" in args.dataset:
        input = tf.placeholder(tf.float32, shape=[None, None, None, 1])
        resnet_dir = cfg.PRETRAINED_DIR + "/DeepScores/"
        refinenet_dir = cfg.PRETRAINED_DIR + "/DeepScores_semseg/"

    else:
        input = tf.placeholder(tf.float32, shape=[None, None, None, 3])
        resnet_dir = cfg.PRETRAINED_DIR + "/ImageNet/"
        refinenet_dir = cfg.PRETRAINED_DIR + "/VOC2012/"

    if not (len(args.training_help) == 1 and args.training_help[0] is None):
        # initialize helper_input
        helper_input = tf.placeholder(
            tf.float32, shape=[None, None, None, input.shape[-1] + 1])
        feed_head = slim.conv2d(helper_input,
                                input.shape[-1], [3, 3],
                                scope='gt_feed_head')
        input = feed_head

    print("Initializing Model:" + args.model)
    # model has all possible output heads (even if unused) to ensure saving and loading goes smoothly
    network_heads, init_fn = build_dwd_net(input,
                                           model=args.model,
                                           num_classes=nr_classes,
                                           pretrained_dir=resnet_dir,
                                           substract_mean=False)

    # initialize tasks
    preped_assign = []
    for assign in args.training_assignements:
        [
            loss, optim, gt_placeholders, scalar_summary_op, images_summary_op,
            images_placeholders, mask_placholders
        ] = initialize_assignement(assign, imdb, network_heads, sess,
                                   data_layer, input, args)
        preped_assign.append([
            loss, optim, gt_placeholders, scalar_summary_op, images_summary_op,
            images_placeholders, mask_placholders
        ])

    # init tensorflow session
    saver = tf.train.Saver(max_to_keep=1000)
    sess.run(tf.global_variables_initializer())

    # load model weights
    checkpoint_dir = get_checkpoint_dir(args)
    checkpoint_name = "backbone"
    if args.continue_training == "True":
        print("Loading checkpoint")
        saver.restore(sess, checkpoint_dir + "/" + checkpoint_name)
    elif args.pretrain_lvl == "deepscores_to_musicma":
        pretrained_vars = []
        for var in slim.get_model_variables():
            if not ("class_pred" in var.name):
                pretrained_vars.append(var)
        print("Loading network pretrained on Deepscores for Muscima")
        loading_checkpoint_name = cfg.PRETRAINED_DIR + "/DeepScores_to_Muscima/" + "backbone"
        init_fn = slim.assign_from_checkpoint_fn(loading_checkpoint_name,
                                                 pretrained_vars)
        init_fn(sess)

    else:
        if args.pretrain_lvl == "semseg":
            #load all variables except the ones in scope "deep_watershed"
            pretrained_vars = []
            for var in slim.get_model_variables():
                if not ("deep_watershed" in var.name
                        or "gt_feed_head" in var.name):
                    pretrained_vars.append(var)

            print("Loading network pretrained on semantic segmentation")
            loading_checkpoint_name = refinenet_dir + args.model + ".ckpt"
            init_fn = slim.assign_from_checkpoint_fn(loading_checkpoint_name,
                                                     pretrained_vars)
            init_fn(sess)
        elif args.pretrain_lvl == "class":
            print("Loading pretrained weights for level: " + args.pretrain_lvl)
            init_fn(sess)
        else:
            print("Not loading a pretrained network")

    # set up tensorboard
    writer = tf.summary.FileWriter(checkpoint_dir, sess.graph)

    # execute tasks
    for do_a in args.do_assign:
        assign_nr = do_a["assign"]
        do_itr = do_a["Itrs"]
        training_help = args.training_help[do_a["help"]]
        iteration = execute_assign(args, input, saver, sess, checkpoint_dir,
                                   checkpoint_name, data_layer, writer,
                                   network_heads, do_itr,
                                   args.training_assignements[assign_nr],
                                   preped_assign[assign_nr], iteration,
                                   training_help)

    # execute combined tasks
    for do_comb_a in args.combined_assignements:
        #iteration = execute_combined_assign(do_comb_a)
        do_comb_itr = do_comb_a["Itrs"]
        rm_length = do_comb_a["Running_Mean_Length"]
        loss_factors = do_comb_a["loss_factors"]
        orig_assign = [
            args.training_assignements[i] for i in do_comb_a["assigns"]
        ]
        preped_assigns = [preped_assign[i] for i in do_comb_a["assigns"]]
        training_help = None  # unused atm
        execute_combined_assign(args, data_layer, training_help, orig_assign,
                                preped_assigns, loss_factors, do_comb_itr,
                                iteration, input, rm_length, network_heads,
                                sess, checkpoint_dir, checkpoint_name, saver,
                                writer)

    print("done :)")
Ejemplo n.º 6
0
def main(parsed):
    args = parsed[0]
    print(args)
    iteration = 1
    np.random.seed(cfg.RNG_SEED)

    # load database
    imdb, roidb, imdb_val, roidb_val, data_layer, data_layer_val = load_database(
        args)

    global nr_classes
    nr_classes = len(imdb._classes)
    args.nr_classes.append(nr_classes)

    # replaces keywords with function handles in training assignements
    save_objectness_function_handles(args, imdb)

    # tensorflow session
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    sess = tf.Session(config=config)

    # input and output tensors
    if "DeepScores_300dpi" in args.dataset:
        input = tf.placeholder(tf.float32, shape=[None, None, None, 1])
        resnet_dir = cfg.PRETRAINED_DIR + "/DeepScores/"
        refinenet_dir = cfg.PRETRAINED_DIR + "/DeepScores_semseg/"

    elif "DeepScores" in args.dataset:
        input = tf.placeholder(tf.float32, shape=[None, None, None, 1])
        resnet_dir = cfg.PRETRAINED_DIR + "/DeepScores/"
        refinenet_dir = cfg.PRETRAINED_DIR + "/DeepScores_semseg/"

    elif "MUSICMA" in args.dataset:
        input = tf.placeholder(tf.float32, shape=[None, None, None, 1])
        resnet_dir = cfg.PRETRAINED_DIR + "/DeepScores/"
        refinenet_dir = cfg.PRETRAINED_DIR + "/DeepScores_semseg/"

    else:
        input = tf.placeholder(tf.float32, shape=[None, None, None, 3])
        resnet_dir = cfg.PRETRAINED_DIR + "/ImageNet/"
        refinenet_dir = cfg.PRETRAINED_DIR + "/VOC2012/"

    if not (len(args.training_help) == 1 and args.training_help[0] is None):
        # initialize helper_input
        helper_input = tf.placeholder(
            tf.float32, shape=[None, None, None, input.shape[-1] + 1])
        feed_head = slim.conv2d(helper_input,
                                input.shape[-1], [3, 3],
                                scope='gt_feed_head')
        input = feed_head

    print("Initializing Model:" + args.model)
    # model has all possible output heads (even if unused) to ensure saving and loading goes smoothly
    network_heads, init_fn = build_dwd_net(
        input,
        model=args.model,
        num_classes=nr_classes,
        pretrained_dir=resnet_dir,
        substract_mean=False,
        individual_upsamp=args.individual_upsamp)

    # use just one image summary OP for all tasks
    final_pred_placeholder = tf.placeholder(tf.uint8, shape=[1, None, None, 3])
    images_sums = []
    images_placeholders = []

    images_placeholders.append(final_pred_placeholder)
    images_sums.append(
        tf.summary.image('DWD_debug_img', final_pred_placeholder))
    images_summary_op = tf.summary.merge(images_sums)

    # initialize tasks
    preped_assign = []
    for assign in args.training_assignements:
        [loss, optim, gt_placeholders, scalar_summary_op,
         mask_placholders] = initialize_assignement(assign, imdb,
                                                    network_heads, sess,
                                                    data_layer, input, args)
        preped_assign.append([
            loss, optim, gt_placeholders, scalar_summary_op, images_summary_op,
            images_placeholders, mask_placholders
        ])

    # init tensorflow session
    saver = tf.train.Saver(max_to_keep=1000)
    sess.run(tf.global_variables_initializer())

    # load model weights
    checkpoint_dir = get_checkpoint_dir(args)
    checkpoint_name = "backbone"
    if args.continue_training == "True":
        print("Loading checkpoint")
        saver.restore(sess, checkpoint_dir + "/" + checkpoint_name)
    elif args.pretrain_lvl == "deepscores_to_musicma":
        pretrained_vars = []
        for var in slim.get_model_variables():
            if not ("class_pred" in var.name):
                pretrained_vars.append(var)
        print("Loading network pretrained on Deepscores for Muscima")
        loading_checkpoint_name = cfg.PRETRAINED_DIR + "/DeepScores_to_Muscima/" + "backbone"
        init_fn = slim.assign_from_checkpoint_fn(loading_checkpoint_name,
                                                 pretrained_vars)
        init_fn(sess)
    elif args.pretrain_lvl == "DeepScores_to_300dpi":
        pretrained_vars = []
        for var in slim.get_model_variables():
            if not ("class_pred" in var.name):
                pretrained_vars.append(var)
        print("Loading network pretrained on Deepscores for Muscima")
        loading_checkpoint_name = cfg.PRETRAINED_DIR + "/DeepScores_to_300dpi/" + "backbone"
        init_fn = slim.assign_from_checkpoint_fn(loading_checkpoint_name,
                                                 pretrained_vars)
        init_fn(sess)
    else:
        if args.pretrain_lvl == "semseg":
            # load all variables except the ones in scope "deep_watershed"
            pretrained_vars = []
            for var in slim.get_model_variables():
                if not ("deep_watershed" in var.name
                        or "gt_feed_head" in var.name):
                    pretrained_vars.append(var)

            print("Loading network pretrained on semantic segmentation")
            loading_checkpoint_name = refinenet_dir + args.model + ".ckpt"
            init_fn = slim.assign_from_checkpoint_fn(loading_checkpoint_name,
                                                     pretrained_vars)
            init_fn(sess)
        elif args.pretrain_lvl == "class":
            print("Loading pretrained weights for level: " + args.pretrain_lvl)
            init_fn(sess)
        else:
            print("Not loading a pretrained network")

    # set up tensorboard
    writer = tf.summary.FileWriter(checkpoint_dir, sess.graph)

    # execute tasks
    for do_a in args.do_assign:
        assign_nr = do_a["assign"]
        do_itr = do_a["Itrs"]
        training_help = args.training_help[do_a["help"]]
        iteration = execute_assign(args, input, saver, sess, checkpoint_dir,
                                   checkpoint_name, data_layer, writer,
                                   network_heads, do_itr,
                                   args.training_assignements[assign_nr],
                                   preped_assign[assign_nr], iteration,
                                   training_help)

    # execute combined tasks
    for do_comb_a in args.combined_assignements:
        do_comb_itr = do_comb_a["Itrs"]
        rm_length = do_comb_a["Running_Mean_Length"]
        loss_factors = do_comb_a["loss_factors"]
        orig_assign = [
            args.training_assignements[i] for i in do_comb_a["assigns"]
        ]
        preped_assigns = [preped_assign[i] for i in do_comb_a["assigns"]]
        training_help = None  # unused atm
        execute_combined_assign(args, data_layer, training_help, orig_assign,
                                preped_assigns, loss_factors, do_comb_itr,
                                iteration, input, rm_length, network_heads,
                                sess, checkpoint_dir, checkpoint_name, saver,
                                writer)

    print("done :)")