def get_all_data(prefix, domain):
    data = None

    for stage in range(constants.training_stages):
        filename = constants.data_filename(prefix, stage)
        if data is None:
            data = np.load(filename)
        else:
            newdata = np.load(filename)
            data = np.concatenate((data, newdata), axis=0)

    return data
def remember(experiment, occlusion = None, bars_type = None, tolerance = 0):
    """ Creates images from features.
    
    Uses the decoder part of the neural networks to (re)create images from features.

    Parameters
    ----------
    experiment : TYPE
        DESCRIPTION.
    occlusion : TYPE, optional
        DESCRIPTION. The default is None.
    tolerance : TYPE, optional
        DESCRIPTION. The default is 0.

    Returns
    -------
    None.

    """

    for i in range(constants.training_stages):
        testing_data_filename = constants.data_name + constants.testing_suffix
        testing_data_filename = constants.data_filename(testing_data_filename, i)
        testing_features_filename = constants.features_name(experiment, occlusion, bars_type) + constants.testing_suffix
        testing_features_filename = constants.data_filename(testing_features_filename, i)
        testing_labels_filename = constants.labels_name + constants.testing_suffix
        testing_labels_filename = constants.data_filename(testing_labels_filename, i)
        memories_filename = constants.memories_name(experiment, occlusion, bars_type, tolerance)
        memories_filename = constants.data_filename(memories_filename, i)
        labels_filename = constants.labels_name + constants.memory_suffix
        labels_filename = constants.data_filename(labels_filename, i)
        model_filename = constants.model_filename(constants.model_name, i)

        testing_data = np.load(testing_data_filename)
        testing_features = np.load(testing_features_filename)
        testing_labels = np.load(testing_labels_filename)
        memories = np.load(memories_filename)
        labels = np.load(labels_filename)
        model = tf.keras.models.load_model(model_filename)

        # Drop the classifier.
        autoencoder = Model(model.input, model.output[1])
        autoencoder.summary()

        # Drop the encoder
        input_mem = Input(shape=(constants.domain, ))
        decoded = get_decoder(input_mem)
        decoder = Model(inputs=input_mem, outputs=decoded)
        decoder.summary()

        for dlayer, alayer in zip(decoder.layers[1:], autoencoder.layers[11:]):
            dlayer.set_weights(alayer.get_weights())

        produced_images = decoder.predict(testing_features)
        n = len(testing_labels)

        Parallel(n_jobs=constants.n_jobs, verbose=5)( \
            delayed(store_images)(original, produced, constants.testing_directory(experiment, occlusion, bars_type), i, j, label) \
                for (j, original, produced, label) in \
                    zip(range(n), testing_data, produced_images, testing_labels))

        total = len(memories)
        steps = len(constants.memory_fills)
        step_size = int(total/steps)

        for j in range(steps):
            print('Decoding memory size ' + str(j) + ' and stage ' + str(i))
            start = j*step_size
            end = start + step_size
            mem_data = memories[start:end]
            mem_labels = labels[start:end]
            produced_images = decoder.predict(mem_data)

            Parallel(n_jobs=constants.n_jobs, verbose=5)( \
                delayed(store_memories)(label, produced, features, constants.memories_directory(experiment, occlusion, bars_type, tolerance), i, j) \
                    for (produced, features, label) in zip(produced_images, mem_data, mem_labels))
def obtain_features(model_prefix, features_prefix, labels_prefix, data_prefix,
            training_percentage, am_filling_percentage, experiment,
            occlusion = None, bars_type = None):
    """ Generate features for images.
    
    Uses the previously trained neural networks for generating the features corresponding
    to the images. It may introduce occlusions.
    """
    stages = constants.training_stages

    (data, labels) = get_data(experiment, occlusion, bars_type)

    total = len(data)
    step = int(total/constants.training_stages)

    training_size = int(total*training_percentage)
    filling_size = int(total*am_filling_percentage)
    testing_size = total - training_size - filling_size

    histories = []
    for n in range(stages):
        i = int(n*step)
        j = (i+training_size) % total

        training_data = get_data_in_range(data, i, j)
        training_labels = get_data_in_range(labels, i, j)

        k = (j+filling_size) % total
        filling_data = get_data_in_range(data, j, k)
        filling_labels = get_data_in_range(labels, j, k)

        l = (k+testing_size) % total
        testing_data = get_data_in_range(data, k, l)
        testing_labels = get_data_in_range(labels, k, l)

        # Recreate the exact same model, including its weights and the optimizer
        model = tf.keras.models.load_model(constants.model_filename(model_prefix, n))

        # Drop the autoencoder and the last layers of the full connected neural network part.
        classifier = Model(model.input, model.output[0])
        no_hot = to_categorical(testing_labels)
        classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics='accuracy')
        history = classifier.evaluate(testing_data, no_hot, batch_size=batch_size, verbose=1, return_dict=True)
        print(history)
        histories.append(history)
        model = Model(classifier.input, classifier.layers[-4].output)
        model.summary()

        training_features = model.predict(training_data)
        if len(filling_data) > 0:
            filling_features = model.predict(filling_data)
        else:
            r, c = training_features.shape
            filling_features = np.zeros((0, c))
        testing_features = model.predict(testing_data)

        dict = {
            constants.training_suffix: (training_data, training_features, training_labels),
            constants.filling_suffix : (filling_data, filling_features, filling_labels),
            constants.testing_suffix : (testing_data, testing_features, testing_labels)
            }

        for suffix in dict:
            data_fn = constants.data_filename(data_prefix+suffix, n)
            features_fn = constants.data_filename(features_prefix+suffix, n)
            labels_fn = constants.data_filename(labels_prefix+suffix, n)

            d, f, l = dict[suffix]
            np.save(data_fn, d)
            np.save(features_fn, f)
            np.save(labels_fn, l)
    
    return histories
def test_recalling(domain,
                   mem_size,
                   experiment,
                   occlusion=None,
                   bars_type=None,
                   tolerance=0):
    n_memories = constants.n_labels

    all_recalls = {}
    all_entropies = {}
    all_mprecision = {}
    all_mrecall = {}
    total_precisions = np.zeros(
        (constants.training_stages, len(constants.memory_fills)))
    total_recalls = np.zeros(
        (constants.training_stages, len(constants.memory_fills)))
    total_mismatches = np.zeros(
        (constants.training_stages, len(constants.memory_fills)))

    xlabels = constants.memory_fills
    list_results = Parallel(n_jobs=constants.n_jobs, verbose=50)(
        delayed(
            test_recalling_fold)(n_memories, mem_size, domain, fold,
                                 experiment, occlusion, bars_type, tolerance)
        for fold in range(constants.training_stages))

    for fold, stage_recalls, stage_entropies, stage_mprecision, stage_mrecall,\
            total_precision, total_recall, mismatches in list_results:
        all_recalls[fold] = stage_recalls
        for msize in stage_entropies:
            all_entropies[msize] = all_entropies[msize] + [stage_entropies[msize]] \
                if msize in all_entropies.keys() else [stage_entropies[msize]]
            all_mprecision[msize] = all_mprecision[msize] + [stage_mprecision[msize]] \
                if msize in all_mprecision.keys() else [stage_mprecision[msize]]
            all_mrecall[msize] = all_mrecall[msize] + [stage_mrecall[msize]] \
                if msize in all_mrecall.keys() else [stage_mrecall[msize]]
            total_precisions[fold] = total_precision
            total_recalls[fold] = total_recall
            total_mismatches[fold] = mismatches

    for fold in all_recalls:
        list_tups = all_recalls[fold]
        tags = []
        memories = []
        for (idx, label, features) in list_tups:
            tags.append((idx, label))
            memories.append(np.array(features))

        tags = np.array(tags)
        memories = np.array(memories)
        memories_filename = constants.memories_name(experiment, occlusion,
                                                    bars_type, tolerance)
        memories_filename = constants.data_filename(memories_filename, fold)
        np.save(memories_filename, memories)
        tags_filename = constants.labels_name + constants.memory_suffix
        tags_filename = constants.data_filename(tags_filename, fold)
        np.save(tags_filename, tags)

    main_avrge_entropies = get_means(all_entropies)
    main_stdev_entropies = get_stdev(all_entropies)
    main_avrge_mprecision = get_means(all_mprecision)
    main_stdev_mprecision = get_stdev(all_mprecision)
    main_avrge_mrecall = get_means(all_mrecall)
    main_stdev_mrecall = get_stdev(all_mrecall)

    np.savetxt(constants.csv_filename('main_average_precision', experiment,
                                      occlusion, bars_type, tolerance),
               main_avrge_mprecision,
               delimiter=',')
    np.savetxt(constants.csv_filename('main_average_recall', experiment,
                                      occlusion, bars_type, tolerance),
               main_avrge_mrecall,
               delimiter=',')
    np.savetxt(constants.csv_filename('main_average_entropy', experiment,
                                      occlusion, bars_type, tolerance),
               main_avrge_entropies,
               delimiter=',')

    np.savetxt(constants.csv_filename('main_stdev_precision', experiment,
                                      occlusion, bars_type, tolerance),
               main_stdev_mprecision,
               delimiter=',')
    np.savetxt(constants.csv_filename('main_stdev_recall', experiment,
                                      occlusion, bars_type, tolerance),
               main_stdev_mrecall,
               delimiter=',')
    np.savetxt(constants.csv_filename('main_stdev_entropy', experiment,
                                      occlusion, bars_type, tolerance),
               main_stdev_entropies,
               delimiter=',')
    np.savetxt(constants.csv_filename('main_total_recalls', experiment,
                                      occlusion, bars_type, tolerance),
               total_recalls,
               delimiter=',')
    np.savetxt(constants.csv_filename('main_total_mismatches', experiment,
                                      occlusion, bars_type, tolerance),
               total_mismatches,
               delimiter=',')

    plot_pre_graph(main_avrge_mprecision * 100,
                   main_avrge_mrecall * 100,
                   main_avrge_entropies,
                   main_stdev_mprecision * 100,
                   main_stdev_mrecall * 100,
                   main_stdev_entropies,
                   'recall-',
                   xlabels=xlabels,
                   xtitle=_('Percentage of memory corpus'),
                   action=experiment,
                   occlusion=occlusion,
                   bars_type=bars_type,
                   tolerance=tolerance)

    plot_pre_graph(np.average(total_precisions, axis=0) * 100,
                   np.average(total_recalls, axis=0) * 100,
                   main_avrge_entropies,
                   np.std(total_precisions, axis=0) * 100,
                   np.std(total_recalls, axis=0) * 100,
                   main_stdev_entropies,
                   'total_recall-',
                   xlabels=xlabels,
                   xtitle=_('Percentage of memory corpus'),
                   action=experiment,
                   occlusion=occlusion,
                   bars_type=bars_type,
                   tolerance=tolerance)

    print('Test completed')
def test_recalling_fold(n_memories,
                        mem_size,
                        domain,
                        fold,
                        experiment,
                        occlusion=None,
                        bars_type=None,
                        tolerance=0):
    # Create the required associative memories.
    ams = dict.fromkeys(range(n_memories))
    for j in ams:
        ams[j] = AssociativeMemory(domain, mem_size, tolerance)

    suffix = constants.filling_suffix
    filling_features_filename = constants.features_name() + suffix
    filling_features_filename = constants.data_filename(
        filling_features_filename, fold)
    filling_labels_filename = constants.labels_name + suffix
    filling_labels_filename = constants.data_filename(filling_labels_filename,
                                                      fold)

    suffix = constants.testing_suffix
    testing_features_filename = constants.features_name(
        experiment, occlusion, bars_type) + suffix
    testing_features_filename = constants.data_filename(
        testing_features_filename, fold)
    testing_labels_filename = constants.labels_name + suffix
    testing_labels_filename = constants.data_filename(testing_labels_filename,
                                                      fold)

    filling_features = np.load(filling_features_filename)
    filling_labels = np.load(filling_labels_filename)
    testing_features = np.load(testing_features_filename)
    testing_labels = np.load(testing_labels_filename)

    filling_max = filling_features.max()
    testing_max = testing_features.max()
    fillin_min = filling_features.min()
    testing_min = testing_features.min()

    maximum = filling_max if filling_max > testing_max else testing_max
    minimum = fillin_min if fillin_min < testing_min else testing_min

    total = len(filling_features)
    percents = np.array(constants.memory_fills)
    steps = np.round(total * percents / 100.0).astype(int)

    stage_recalls = []
    stage_entropies = {}
    stage_mprecision = {}
    stage_mrecall = {}
    total_precisions = []
    total_recalls = []
    mismatches = []

    i = 0
    for j in range(len(steps)):
        k = steps[j]
        features = filling_features[i:k]
        labels = filling_labels[i:k]

        recalls, measures, entropies, total_precision, total_recall, mis_count = get_recalls(
            ams, mem_size, domain, minimum, maximum, features, labels,
            testing_features, testing_labels, fold)

        # A list of tuples (position, label, features)
        stage_recalls += recalls

        # An array with entropies per memory
        stage_entropies[j] = entropies

        # An array with precision per memory
        stage_mprecision[j] = measures[constants.precision_idx, :]

        # An array with recall per memory
        stage_mrecall[j] = measures[constants.recall_idx, :]

        #
        # Recalls and precisions per step
        total_recalls.append(total_recall)
        total_precisions.append(total_precision)

        i = k

        mismatches.append(mis_count)

    return fold, stage_recalls, stage_entropies, stage_mprecision, \
        stage_mrecall, np.array(total_precisions), np.array(
            total_recalls), np.array(mismatches)
def test_memories(domain, experiment):

    average_entropy = []
    stdev_entropy = []

    average_precision = []
    stdev_precision = []
    average_recall = []
    stdev_recall = []

    all_precision = []
    all_recall = []

    no_response = []
    no_correct_response = []
    no_correct_chosen = []
    correct_chosen = []
    total_responses = []

    labels_x_memory = constants.labels_per_memory[experiment]
    n_memories = int(constants.n_labels / labels_x_memory)

    for i in range(constants.training_stages):
        gc.collect()

        suffix = constants.filling_suffix
        training_features_filename = constants.features_name(
            experiment) + suffix
        training_features_filename = constants.data_filename(
            training_features_filename, i)
        training_labels_filename = constants.labels_name + suffix
        training_labels_filename = constants.data_filename(
            training_labels_filename, i)

        suffix = constants.testing_suffix
        testing_features_filename = constants.features_name(
            experiment) + suffix
        testing_features_filename = constants.data_filename(
            testing_features_filename, i)
        testing_labels_filename = constants.labels_name + suffix
        testing_labels_filename = constants.data_filename(
            testing_labels_filename, i)

        training_features = np.load(training_features_filename)
        training_labels = np.load(training_labels_filename)
        testing_features = np.load(testing_features_filename)
        testing_labels = np.load(testing_labels_filename)

        measures_per_size = np.zeros(
            (len(constants.memory_sizes), n_memories, constants.n_measures),
            dtype=np.float64)

        # An entropy value per memory size and memory.
        entropies = np.zeros((len(constants.memory_sizes), n_memories),
                             dtype=np.float64)
        behaviours = np.zeros(
            (len(constants.memory_sizes), constants.n_behaviours))

        print('Train the different co-domain memories -- NxM: ', experiment,
              ' run: ', i)
        # Processes running in parallel.
        list_measures_entropies = Parallel(
            n_jobs=constants.n_jobs,
            verbose=50)(delayed(get_ams_results)(
                midx, msize, domain, labels_x_memory, training_features,
                testing_features, training_labels, testing_labels)
                        for midx, msize in enumerate(constants.memory_sizes))

        for j, measures, entropy, behaviour in list_measures_entropies:
            measures_per_size[j, :, :] = measures.T
            entropies[j, :] = entropy
            behaviours[j, :] = behaviour

        ##########################################################################################

        # Calculate precision and recall

        precision = np.zeros((len(constants.memory_sizes), n_memories + 2),
                             dtype=np.float64)
        recall = np.zeros((len(constants.memory_sizes), n_memories + 2),
                          dtype=np.float64)

        for j, s in enumerate(constants.memory_sizes):
            precision[j, 0:n_memories] = measures_per_size[
                j, :, constants.precision_idx]
            precision[j, constants.mean_idx(n_memories)] = measures_per_size[
                j, :, constants.precision_idx].mean()
            precision[j, constants.std_idx(n_memories)] = measures_per_size[
                j, :, constants.precision_idx].std()
            recall[j, 0:n_memories] = measures_per_size[j, :,
                                                        constants.recall_idx]
            recall[j, constants.mean_idx(n_memories)] = measures_per_size[
                j, :, constants.recall_idx].mean()
            recall[j, constants.std_idx(n_memories)] = measures_per_size[
                j, :, constants.recall_idx].std()

        ###################################################################3##
        # Measures by memory size

        # Average entropy among al digits.
        average_entropy.append(entropies.mean(axis=1))
        stdev_entropy.append(entropies.std(axis=1))

        # Average precision as percentage
        average_precision.append(
            precision[:, constants.mean_idx(n_memories)] * 100)
        stdev_precision.append(precision[:, constants.std_idx(n_memories)] *
                               100)

        # Average recall as percentage
        average_recall.append(recall[:, constants.mean_idx(n_memories)] * 100)
        stdev_recall.append(recall[:, constants.std_idx(n_memories)] * 100)

        all_precision.append(behaviours[:, constants.precision_idx] * 100)
        all_recall.append(behaviours[:, constants.recall_idx] * 100)

        no_response.append(behaviours[:, constants.no_response_idx])
        no_correct_response.append(
            behaviours[:, constants.no_correct_response_idx])
        no_correct_chosen.append(behaviours[:,
                                            constants.no_correct_chosen_idx])
        correct_chosen.append(behaviours[:, constants.correct_response_idx])
        total_responses.append(behaviours[:, constants.mean_responses_idx])

    average_precision = np.array(average_precision)
    stdev_precision = np.array(stdev_precision)
    main_average_precision = []
    main_stdev_precision = []

    average_recall = np.array(average_recall)
    stdev_recall = np.array(stdev_recall)
    main_average_recall = []
    main_stdev_recall = []

    all_precision = np.array(all_precision)
    main_all_average_precision = []
    main_all_stdev_precision = []

    all_recall = np.array(all_recall)
    main_all_average_recall = []
    main_all_stdev_recall = []

    average_entropy = np.array(average_entropy)
    stdev_entropy = np.array(stdev_entropy)
    main_average_entropy = []
    main_stdev_entropy = []

    no_response = np.array(no_response)
    no_correct_response = np.array(no_correct_response)
    no_correct_chosen = np.array(no_correct_chosen)
    correct_chosen = np.array(correct_chosen)
    total_responses = np.array(total_responses)

    main_no_response = []
    main_no_correct_response = []
    main_no_correct_chosen = []
    main_correct_chosen = []
    main_total_responses = []
    main_total_responses_stdev = []

    for i in range(len(constants.memory_sizes)):
        main_average_precision.append(average_precision[:, i].mean())
        main_average_recall.append(average_recall[:, i].mean())
        main_average_entropy.append(average_entropy[:, i].mean())

        main_stdev_precision.append(stdev_precision[:, i].mean())
        main_stdev_recall.append(stdev_recall[:, i].mean())
        main_stdev_entropy.append(stdev_entropy[:, i].mean())

        main_all_average_precision.append(all_precision[:, i].mean())
        main_all_stdev_precision.append(all_precision[:, i].std())
        main_all_average_recall.append(all_recall[:, i].mean())
        main_all_stdev_recall.append(all_recall[:, i].std())

        main_no_response.append(no_response[:, i].mean())
        main_no_correct_response.append(no_correct_response[:, i].mean())
        main_no_correct_chosen.append(no_correct_chosen[:, i].mean())
        main_correct_chosen.append(correct_chosen[:, i].mean())
        main_total_responses.append(total_responses[:, i].mean())
        main_total_responses_stdev.append(total_responses[:, i].std())

    main_behaviours = [
        main_no_response, main_no_correct_response, main_no_correct_chosen,
        main_correct_chosen, main_total_responses
    ]

    np.savetxt(constants.csv_filename(
        'main_average_precision--{0}'.format(experiment)),
               main_average_precision,
               delimiter=',')
    np.savetxt(constants.csv_filename(
        'main_all_average_precision--{0}'.format(experiment)),
               main_all_average_precision,
               delimiter=',')
    np.savetxt(constants.csv_filename(
        'main_average_recall--{0}'.format(experiment)),
               main_average_recall,
               delimiter=',')
    np.savetxt(constants.csv_filename(
        'main_all_average_recall--{0}'.format(experiment)),
               main_all_average_recall,
               delimiter=',')
    np.savetxt(constants.csv_filename(
        'main_average_entropy--{0}'.format(experiment)),
               main_average_entropy,
               delimiter=',')

    np.savetxt(constants.csv_filename(
        'main_stdev_precision--{0}'.format(experiment)),
               main_stdev_precision,
               delimiter=',')
    np.savetxt(constants.csv_filename(
        'main_all_stdev_precision--{0}'.format(experiment)),
               main_all_stdev_precision,
               delimiter=',')
    np.savetxt(constants.csv_filename(
        'main_stdev_recall--{0}'.format(experiment)),
               main_stdev_recall,
               delimiter=',')
    np.savetxt(constants.csv_filename(
        'main_all_stdev_recall--{0}'.format(experiment)),
               main_all_stdev_recall,
               delimiter=',')
    np.savetxt(constants.csv_filename(
        'main_stdev_entropy--{0}'.format(experiment)),
               main_stdev_entropy,
               delimiter=',')

    np.savetxt(constants.csv_filename(
        'main_behaviours--{0}'.format(experiment)),
               main_behaviours,
               delimiter=',')

    plot_pre_graph(main_average_precision,
                   main_average_recall,
                   main_average_entropy,
                   main_stdev_precision,
                   main_stdev_recall,
                   main_stdev_entropy,
                   action=experiment)

    plot_pre_graph(main_all_average_precision,
                   main_all_average_recall,
                   main_average_entropy,
                   main_all_stdev_precision,
                   main_all_stdev_recall,
                   main_stdev_entropy,
                   'overall',
                   action=experiment)

    plot_size_graph(main_total_responses,
                    main_total_responses_stdev,
                    action=experiment)

    plot_behs_graph(main_no_response,
                    main_no_correct_response,
                    main_no_correct_chosen,
                    main_correct_chosen,
                    action=experiment)

    print('Test complete')
Пример #7
0
def test_recalling(domain,
                   mem_size,
                   experiment,
                   occlusion=None,
                   bars_type=None,
                   tolerance=0):
    n_memories = constants.n_labels
    memory_fills = constants.memory_fills
    training_stages = constants.training_stages

    # All recalls, per memory fill and fold.
    all_recalls = {}

    # All entropies, precision, and recall, per fold, fill, and memory.
    all_mfill_entropies = \
        np.zeros((training_stages, len(memory_fills), n_memories))
    all_mfill_precision = \
        np.zeros((training_stages, len(memory_fills), n_memories))
    all_mfill_recall = \
        np.zeros((training_stages, len(memory_fills), n_memories))

    # Store the matrix of stages x memory fills.
    total_precisions = np.zeros((training_stages, len(memory_fills)))
    total_recalls = np.zeros((training_stages, len(memory_fills)))
    total_mismatches = np.zeros((training_stages, len(memory_fills)))

    list_results = Parallel(n_jobs=constants.n_jobs, verbose=50)(
        delayed(test_recalling_fold)(n_memories, mem_size, domain, fold, experiment, occlusion, bars_type, tolerance) \
            for fold in range(constants.training_stages))

    for fold, recalls, fill_mem_entropies, fill_mem_precision, fill_mem_recall,\
        fold_precision, fold_recall, fold_mismatches in list_results:

        all_recalls[fold] = recalls
        total_precisions[fold] = fold_precision
        total_recalls[fold] = fold_recall
        total_mismatches[fold] = fold_mismatches

        all_mfill_entropies[fold] = fill_mem_entropies
        all_mfill_precision[fold] = fill_mem_precision
        all_mfill_recall[fold] = fill_mem_recall

    for fold in all_recalls:
        list_tups = all_recalls[fold]
        tags = []
        memories = []
        for (idx, label, features) in list_tups:
            tags.append((idx, label))
            memories.append(np.array(features))

        tags = np.array(tags)
        memories = np.array(memories)
        memories_filename = constants.memories_name(experiment, occlusion,
                                                    bars_type, tolerance)
        memories_filename = constants.data_filename(memories_filename, fold)
        np.save(memories_filename, memories)
        tags_filename = constants.labels_name + constants.memory_suffix
        tags_filename = constants.data_filename(tags_filename, fold)
        np.save(tags_filename, tags)

    main_avrge_entropies = np.mean(all_mfill_entropies, axis=(0, 2))
    main_stdev_entropies = np.std(all_mfill_entropies, axis=(0, 2))
    main_avrge_mprecision = np.mean(all_mfill_precision, axis=(0, 2))
    main_stdev_mprecision = np.std(all_mfill_precision, axis=(0, 2))
    main_avrge_mrecall = np.mean(all_mfill_recall, axis=(0, 2))
    main_stdev_mrecall = np.std(all_mfill_recall, axis=(0, 2))

    np.savetxt(constants.csv_filename('main_average_precision',experiment, occlusion, bars_type, tolerance), \
        main_avrge_mprecision, delimiter=',')
    np.savetxt(constants.csv_filename('main_average_recall',experiment, occlusion, bars_type, tolerance), \
        main_avrge_mrecall, delimiter=',')
    np.savetxt(constants.csv_filename('main_average_entropy',experiment, occlusion, bars_type, tolerance), \
        main_avrge_entropies, delimiter=',')

    np.savetxt(constants.csv_filename('main_stdev_precision',experiment, occlusion, bars_type, tolerance), \
        main_stdev_mprecision, delimiter=',')
    np.savetxt(constants.csv_filename('main_stdev_recall',experiment, occlusion, bars_type, tolerance), \
        main_stdev_mrecall, delimiter=',')
    np.savetxt(constants.csv_filename('main_stdev_entropy',experiment, occlusion, bars_type, tolerance), \
        main_stdev_entropies, delimiter=',')
    np.savetxt(constants.csv_filename('main_total_recalls',experiment, occlusion, bars_type, tolerance), \
        total_recalls, delimiter=',')
    np.savetxt(constants.csv_filename('main_total_mismatches',experiment, occlusion, bars_type, tolerance), \
        total_mismatches, delimiter=',')

    plot_pre_graph(main_avrge_mprecision*100, main_avrge_mrecall*100, main_avrge_entropies,\
        main_stdev_mprecision*100, main_stdev_mrecall*100, main_stdev_entropies, 'recall-', \
            xlabels = constants.memory_fills, xtitle = _('Percentage of memory corpus'), action = experiment,
            occlusion = occlusion, bars_type = bars_type, tolerance = tolerance)

    plot_pre_graph(np.average(total_precisions, axis=0)*100, np.average(total_recalls, axis=0)*100, \
        main_avrge_entropies, np.std(total_precisions, axis=0)*100, np.std(total_recalls, axis=0)*100, \
            main_stdev_entropies, 'total_recall-', \
            xlabels = constants.memory_fills, xtitle = _('Percentage of memory corpus'), action=experiment,
            occlusion = occlusion, bars_type = bars_type, tolerance = tolerance)

    print('Test completed')
Пример #8
0
def obtain_features(model_prefix,
                    features_prefix,
                    labels_prefix,
                    data_prefix,
                    training_percentage,
                    am_filling_percentage,
                    experiment,
                    occlusion=None,
                    bars_type=None):
    """ Generate features for images.
    
    Uses the previously trained neural networks for generating the features corresponding
    to the images. It may introduce occlusions.
    """
    (data, labels) = get_data(experiment, occlusion, bars_type)

    total = len(data)
    step = int(total / constants.training_stages)

    # Amount of data used for training the networks
    trdata = int(total * training_percentage)

    # Amount of data used for testing memories
    tedata = step

    n = 0
    histories = []
    for i in range(0, total, step):
        j = (i + tedata) % total

        if j > i:
            testing_data = data[i:j]
            testing_labels = labels[i:j]
            other_data = np.concatenate((data[0:i], data[j:total]), axis=0)
            other_labels = np.concatenate((labels[0:i], labels[j:total]),
                                          axis=0)
            training_data = other_data[:trdata]
            training_labels = other_labels[:trdata]
            filling_data = other_data[trdata:]
            filling_labels = other_labels[trdata:]
        else:
            testing_data = np.concatenate((data[0:j], data[i:total]), axis=0)
            testing_labels = np.concatenate((labels[0:j], labels[i:total]),
                                            axis=0)
            training_data = data[j:j + trdata]
            training_labels = labels[j:j + trdata]
            filling_data = data[j + trdata:i]
            filling_labels = labels[j + trdata:i]

        # Recreate the exact same model, including its weights and the optimizer
        model = tf.keras.models.load_model(
            constants.model_filename(model_prefix, n))

        # Drop the autoencoder and the last layers of the full connected neural network part.
        classifier = Model(model.input, model.output[0])
        no_hot = to_categorical(testing_labels)
        classifier.compile(optimizer='adam',
                           loss='categorical_crossentropy',
                           metrics='accuracy')
        history = classifier.evaluate(testing_data,
                                      no_hot,
                                      batch_size=batch_size,
                                      verbose=1,
                                      return_dict=True)
        print(history)
        histories.append(history)
        model = Model(classifier.input, classifier.layers[-4].output)
        model.summary()

        training_features = model.predict(training_data)
        if len(filling_data) > 0:
            filling_features = model.predict(filling_data)
        else:
            r, c = training_features.shape
            filling_features = np.zeros((0, c))
        testing_features = model.predict(testing_data)

        dict = {
            constants.training_suffix:
            (training_data, training_features, training_labels),
            constants.filling_suffix:
            (filling_data, filling_features, filling_labels),
            constants.testing_suffix:
            (testing_data, testing_features, testing_labels)
        }

        for suffix in dict:
            data_fn = constants.data_filename(data_prefix + suffix, n)
            features_fn = constants.data_filename(features_prefix + suffix, n)
            labels_fn = constants.data_filename(labels_prefix + suffix, n)

            d, f, l = dict[suffix]
            np.save(data_fn, d)
            np.save(features_fn, f)
            np.save(labels_fn, l)

        n += 1

    return histories