<jupyter_start><jupyter_text>Recognition gap: Search for MIRCs This notebook contains the code for the main experiment of the thirdcase study "recognition gap" in "The Notorious Difficult of ComparingHuman and Machine Perception" (Funke, Borowski et al. 2020): Weimplement a search algorithm for a deep convolutional neural network toidentify MIRCs (minimal recognizable configuration). The procedure isvery similar to the human experiment performed by Ullman et al. (2016).  Libraries, packages, ...<jupyter_code># basic imports
import os

# standard libraries
import numpy as np
import math
from PIL import Image
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import csv
import time

# torch imports
import torch

# custom imports
import configuration_for_experiment as config
import utils.pytorchnet_bagnets as pytorchnet_bagnets
import utils.data_in as data_in
import utils.data_out as data_out
import utils.search as search<jupyter_output><empty_output><jupyter_text>Set device<jupyter_code># set device on GPU if available, else CPU
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")<jupyter_output><empty_output><jupyter_text>Load data<jupyter_code># get data_loader
data_loader = data_in.get_data_loader(config.Ullman_or_ImageNet)<jupyter_output><empty_output><jupyter_text>Load model<jupyter_code>model = pytorchnet_bagnets.bagnet33(pretrained=True).to(DEVICE)
model.avg_pool = False
model.eval()
torch.set_grad_enabled(False)<jupyter_output><empty_output><jupyter_text>Directories for ouput data<jupyter_code>exp_dir = data_out.make_exp_dir(
    config.Ullman_or_ImageNet,
    config.list_as_one_class,
    config.start_idx,
    config.stop_idx,
    config.descendent_specifier)<jupyter_output><empty_output><jupyter_text>Do it! Search MIRCs - and while you're at it, also sub-MIRCs<jupyter_code>write_or_append = "w"
start = time.time()
# loop through all images in data_loader
# note that this data_loader was slightly modified and that it returns a
# list of target(s) and the path to the image file
for number_IN, (image_from_loader, target, path) in enumerate(data_loader):
    # only perform the search if the images are from Ullman et al.
    # or if the images are in the specified range (start_idx and stop_idx)
    if (
        # and ("suit" in path[0])) # TODO
        ((config.Ullman_or_ImageNet == "Ullman"))
        or ((config.Ullman_or_ImageNet == "ImageNet") and (number_IN >= config.start_idx) and (number_IN < config.stop_idx))
    ):
        print("\nnumber_IN", number_IN)

        # all classes as one class
        if config.list_as_one_class:
            target_list = target
            search.perform_MIRC_search(
                image_from_loader,
                target_list,
                path,
                model,
                DEVICE,
                config.Ullman_or_ImageNet,
                config.descendent_specifier,
                exp_dir,
                write_or_append)
            write_or_append = "a"
        # individual elements as separate classes
        else:
            for target_i in target:
                target_list = [target_i]
                search.perform_MIRC_search(
                    image_from_loader,
                    target_list,
                    path,
                    model,
                    DEVICE,
                    config.Ullman_or_ImageNet,
                    config.descendent_specifier,
                    exp_dir,
                    write_or_append)
                write_or_append = "a"

print("done")
stop = time.time()
print(f"time {stop-start}")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0719562243502052, 'fraction_numerical': 0.003009575923392613, 'mean_word_length': 3.0532150776053215, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '447037', 'license': 'MIT', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1096, 'n_words': 298, 'path': 'recognition_gap/recognition_gap_experiment_JOV.ipynb', 'repo_name': 'bethgelab/notorious_difficulty_of_comparing_human_and_machine_perception', 'size': '3655'}