def run_tcav():
    model = get_model_list("../../models/")[0]
    dataset = get_dataset_list("../../datasets")[0]

    dataset_name = dataset.dataset_name
    id_to_labels = dataset.id_to_label

    model_to_run = 'inception_v3'
    tcav_dir = "../../models/tensorflow_inception_v3"

    # where activations are stored (only if your act_gen_wrapper does so)
    activation_dir = os.path.join(tcav_dir, 'activations/')
    # where CAVs are stored.
    # You can say None if you don't wish to store any.
    cav_dir = os.path.join(tcav_dir, 'cavs/')

    concept_directory = "../../datasets/tcav_concepts"
    target_directory = "../../datasets/targets"
    bottlenecks = ['Mixed_5d']  # @param

    utils.make_dir_if_not_exists(activation_dir)
    utils.make_dir_if_not_exists(cav_dir)

    # this is a regularizer penalty parameter for linear classifier to get CAVs.
    alphas = [0.1]
    # a folder that random images are stored
    random_counterpart = 'random_images'
    targets = ['zebra']
    concepts = ["dotted", "striped", "zigzagged", "irregular pattern", "gradient", "single color"]

    #crawl images for concepts and target class
    for concept in concepts:
        if not os.path.isdir(os.path.join(concept_directory, concept)):
            image_crawler.crawl_images(concept_directory, concept, N=50)
        # if not os.path.isdir(os.path.join(concept_directory, random_counterpart)):
        #    image_crawler.crawl_images(concept_directory, 'image', N=500)
    for target in targets:
        if not os.path.isdir(os.path.join(target_directory, target)):
            image_crawler.crawl_images(target_directory, target, N=50)

    the_model = cm.InceptionV3Wrapper_custom(model.session,
                                             model,
                                             id_to_labels)

    act_generator = act_gen.ImageActivationGenerator(the_model, concept_directory, activation_dir, max_examples=100)

    tf.logging.set_verbosity(0)

    tcav_dict = {}

    for target in targets:
        mytcav = TCAV(model.session,
                      target,
                      concepts,
                      bottlenecks,
                      act_generator,
                      alphas,
                      random_counterpart,
                      cav_dir=cav_dir,
                      num_random_exp=5)

        results = mytcav.run()

        # we have to subtract 1 from the target class, as it corresponds with our ground truth labels,
        # internally the network outputs are shifted by one, as 0 represents the background class instead of -1
        summary = utils.print_results(results, class_id=the_model.label_to_id(target)-1, result_dict=tcav_dict)

    tcav_file_path = os.path.join(model.model_path, dataset_name + model.model_name + '-tcavscores' + '.pkl')
    with open(tcav_file_path, 'wb') as f:
        pickle.dump(tcav_dict, f, pickle.HIGHEST_PROTOCOL)
def run_tcav(model, dataset, previous_tcav_dict=None):

    dataset_name = dataset.dataset_name
    id_to_labels = dataset.id_to_label

    model_to_run = 'inception_v3'
    tcav_dir = "models/tensorflow_inception_v3_tcav_temps"

    # where activations are stored (only if your act_gen_wrapper does so)
    activation_dir = os.path.join(tcav_dir, 'activations/')
    # where CAVs are stored.
    # You can say None if you don't wish to store any.
    cav_dir = os.path.join(tcav_dir, 'cavs/')

    concept_directory = "datasets/tcav_concepts"
    target_directory = "datasets/image_ILSVRC2012_validation"
    bottlenecks = ['Mixed_5d', 'Mixed_7c']  # @param

    utils.make_dir_if_not_exists(activation_dir)
    utils.make_dir_if_not_exists(cav_dir)

    # this is a regularizer penalty parameter for linear classifier to get CAVs.
    alphas = [0.1]
    # a folder that random images are stored
    random_counterpart = 'random_images'
    #targets = random.sample(id_to_labels.keys(), 50)
    targets = [286, 370, 757, 595, 147, 108, 478, 517, 334, 173, 948, 727, 23]
    if -1 in targets:
        targets.remove(-1)
    print(targets)

    concepts = [
        dI for dI in os.listdir(concept_directory)
        if os.path.isdir(os.path.join(concept_directory, dI))
        and "random" not in dI and "." not in dI
    ]

    the_model = TCAVInceptionWrapperSlim(model.session, model, id_to_labels)

    act_generator = act_gen.ImageActivationGenerator(
        the_model,
        concept_directory,
        activation_dir,
        max_examples=100,
        target_dir=target_directory,
        label_to_element_dict=dataset.label_to_elements)

    tf.logging.set_verbosity(0)

    if previous_tcav_dict == None:
        tcav_dict = previous_tcav_dict
    else:
        tcav_dict = {}

    for target in targets:
        mytcav = TCAV(model.session,
                      target,
                      concepts,
                      bottlenecks,
                      act_generator,
                      alphas,
                      random_counterpart,
                      cav_dir=cav_dir,
                      num_random_exp=19,
                      use_numeric_class_label=True)

        results = mytcav.run(run_parallel=True, num_workers=5)

        tcav_dict = utils.print_results(results,
                                        class_id=target,
                                        result_dict=tcav_dict)

        tcav_file_path = os.path.join(
            "models",
            dataset_name + model.model_name + '-tcavscores-2' + '.pkl')
        with open(tcav_file_path, 'wb') as f:
            pickle.dump(tcav_dict, f, pickle.HIGHEST_PROTOCOL)

    return tcav_dict
示例#3
0
def run_tcav(model, dataset, previous_tcav_dict=None):
    '''

    :param model:
    :param dataset:
    :param previous_tcav_dict:
    :return:
    '''
    dataset_name = dataset.dataset_name
    id_to_labels = dataset.id_to_label

    tcav_dir = "models/tensorflow_inception_v3_tcav_temps"

    # where activations are stored (only if your act_gen_wrapper does so)
    activation_dir = os.path.join(tcav_dir, 'activations/')
    # where CAVs are stored.
    # You can say None if you don't wish to store any.
    cav_dir = os.path.join(tcav_dir, 'cavs/')

    concept_directory = "datasets/tcav_concepts"
    target_directory = "datasets/image_ILSVRC2012_validation"
    bottlenecks = ['Mixed_5d', 'Mixed_7c']  # @param

    create_tcav_dirs(
        dataset=dataset,
        model=model,
        concept_dir=concept_directory,
        tcav_dir=tcav_dir,
        nr_of_random_experiments=12,
        concepts=[
            "glossy surface", "matt surface", "uneven texture",
            "wrinkled object", "rectangle pattern", "transparent object",
            "translucent", "rectangular", "round", "triangular",
            "complex shape", "spiky surface", "tree", "flower", "plant",
            "grass,moss", "machine", "machine device", "architecture",
            "clothes", "animal fur", "fabric,clothes", "inside lighting",
            "nature", "beach", "underwater", "sky"
        ],
        nr_imgs_per_concept=50,
        nr_random_images=500)

    # this is a regularizer penalty parameter for linear classifier to get CAVs.
    alphas = [0.1]
    # a folder that random images are stored
    random_counterpart = 'random_images'
    # targets = random.sample(id_to_labels.keys(), 10)
    targets = [65, 970, 230, 809, 516, 57, 334, 415, 674, 332, 109]
    if -1 in targets:
        targets.remove(-1)
    print(targets)

    concepts = [
        dI for dI in os.listdir(concept_directory)
        if os.path.isdir(os.path.join(concept_directory, dI))
        and "random" not in dI and "." not in dI
    ]

    the_model = TCAVInceptionWrapperSlim(model.session, model, id_to_labels)

    act_generator = act_gen.ImageActivationGenerator(
        the_model,
        concept_directory,
        activation_dir,
        max_examples=100,
        target_dir=target_directory,
        label_to_element_dict=dataset.label_to_elements)

    tf.logging.set_verbosity(0)

    if previous_tcav_dict == None:
        tcav_dict = previous_tcav_dict
    else:
        tcav_dict = {}

    for target in targets:
        print("create tcavs for:", target)
        mytcav = TCAV(model.session,
                      target,
                      concepts,
                      bottlenecks,
                      act_generator,
                      alphas,
                      random_counterpart,
                      cav_dir=cav_dir,
                      num_random_exp=19,
                      use_numeric_class_label=True)

        print('Run TCAV creation')

        results = mytcav.run(run_parallel=True, num_workers=5)

        print('Finished tcav creation')

        tcav_dict = utils.print_results(results,
                                        class_id=target,
                                        result_dict=tcav_dict)

        print('Saved dict')

        tcav_file_path = os.path.join(
            model.model_path,
            dataset_name + model.model_name + '-tcavscores' + '.pkl')
        with open(tcav_file_path, 'wb') as f:
            pickle.dump(tcav_dict, f, pickle.HIGHEST_PROTOCOL)

        print('Saved to file')

    return tcav_dict