예제 #1
0
 def import_imdb_weighted_graph(self, weights):
     weights_dict = {'movies_edges': weights[0], 'labels_edges': weights[1]}
     dict_paths = {'cast': 'data_set/IMDb title_principals.csv', 'genre': 'data_set/IMDb movies.csv'}
     imdb = MoviesGraph(dict_paths, self.args.graph_percentage)
     gnx = imdb.create_graph()
     labels = imdb.labels2int(gnx)
     knowledge_gnx, knowledge_data = imdb.create_knowledge_graph(labels, float(self.kg_jacard_similarity_threshold))
     weighted_graph = imdb.weighted_graph(gnx, knowledge_gnx, labels, weights_dict)
     return weighted_graph
예제 #2
0
 def import_imdb_weighted_graph(weights):
     from IMDb_data_preparation_E2V import MoviesGraph
     weights_dict = {'movies_edges': weights[0], 'labels_edges': weights[1]}
     dict_paths = {
         'cast': 'data_set/IMDb title_principals.csv',
         'genre': 'data_set/IMDb movies.csv'
     }
     imdb = MoviesGraph(dict_paths)
     gnx = imdb.create_graph()
     labels = imdb.labels2int(gnx)
     knowledge_gnx, knowledge_data = imdb.create_knowledge_graph(labels)
     weighted_graph = imdb.weighted_graph(gnx, knowledge_gnx, labels,
                                          weights_dict)
     return weighted_graph
예제 #3
0
 def import_imdb_multi_graph(self, weights):
     """
     Make our_imdb multi graph using class
     :param weights:
     :return:
     """
     weights_dict = {'movies_edges': weights[0], 'labels_edges': weights[1]}
     dict_paths = {'cast': 'data_set/IMDb title_principals.csv', 'genre': 'data_set/IMDb movies.csv'}
     imdb = MoviesGraph(dict_paths, self.args.graph_percentage)
     gnx = imdb.create_graph()
     labels = imdb.labels2int(gnx)
     knowledge_gnx, knowledge_data = imdb.create_knowledge_graph(labels, self.kg_jacard_similarity_threshold)
     multi_gnx = imdb.weighted_multi_graph(gnx, knowledge_gnx, labels, weights_dict)
     return multi_gnx
예제 #4
0
 def import_imdb_multi_graph(weights):
     """
     Make our_imdb multi graph using class 
     :param weights: 
     :return: 
     """
     from IMDb_data_preparation_E2V import MoviesGraph
     weights_dict = {'movies_edges': weights[0], 'labels_edges': weights[1]}
     dict_paths = {
         'cast': 'data_set/IMDb title_principals.csv',
         'genre': 'data_set/IMDb movies.csv'
     }
     imdb = MoviesGraph(dict_paths)
     gnx = imdb.create_graph()
     labels = imdb.labels2int(gnx)
     knowledge_gnx, knowledge_data = imdb.create_knowledge_graph(labels)
     multi_gnx = imdb.weighted_multi_graph(gnx, knowledge_gnx, labels,
                                           weights_dict)
     return multi_gnx
예제 #5
0
    def create_graph(self):
        weights_dict = {
            'classes_edges': self.class_edges_weights,
            'labels_edges': self.labels_edges_weights
        }
        if self.args.dataset == "our_imdb":
            weights_dict["movies_edges"] = weights_dict["classes_edges"]
            dict_paths = {
                'cast': 'data_set/IMDb title_principals.csv',
                'genre': 'data_set/IMDb movies.csv'
            }
            imdb = MoviesGraph(dict_paths, self.args.images_nodes_percentage)
            image_graph = imdb.create_graph()
            labels = imdb.labels2int(image_graph)
            labels_graph = imdb.create_labels_graph(labels)
            kg, knowledge_data = imdb.create_knowledge_graph(
                labels, float(0.3))
            final_graph = imdb.weighted_graph(image_graph, kg, labels,
                                              weights_dict)
            dict_true_edges = self.label_edges_classes_ordered(
                list(labels_graph.edges))
            classes = list(dict_true_edges.keys())
            for i, k in enumerate(
                    sorted(dict_true_edges,
                           key=lambda x: len(dict_true_edges[x]),
                           reverse=True)):
                classes[i] = k
            seen_classes = classes[:int(0.8 * len(classes))]
            unseen_classes = classes[int(0.8 * len(classes)):]

        elif self.args.dataset == "cub" or self.args.dataset == "lad":
            dict_paths, radius = define_graph_args(self.args.dataset)
            graph_preparation = ImagesEmbeddings(dict_paths, self.args)
            embeds_matrix, dict_image_embed, dict_image_class = graph_preparation.images_embed_calculator(
            )
            dict_idx_image_class = {
                i: dict_image_class[image]
                for i, image in enumerate(list(dict_image_class.keys()))
            }
            final_graph_creator = FinalGraphCreator(
                dict_paths, embeds_matrix, dict_image_embed,
                dict_idx_image_class, self.args.images_nodes_percentage,
                self.args)
            image_graph = final_graph_creator.create_image_graph(radius)
            kg, dict_class_nodes_translation = final_graph_creator.imagenet_knowledge_graph(
            )
            kg = final_graph_creator.attributed_graph(
                kg, dict_class_nodes_translation, self.att_weight, radius)
            seen_classes, unseen_classes = final_graph_creator.seen_classes, final_graph_creator.unseen_classes
            seen_classes = [
                dict_class_nodes_translation[c] for c in seen_classes
            ]
            unseen_classes = [
                dict_class_nodes_translation[c] for c in unseen_classes
            ]
            labels_graph = final_graph_creator.create_labels_graph(
                dict_class_nodes_translation)
            final_graph = final_graph_creator.weighted_graph(
                image_graph, kg, labels_graph, weights_dict)
        else:
            raise ValueError("Wrong dataset name: replace with awa2/cub/lad")
        dict_class_identification = {
            **{c: 0
               for c in seen_classes},
            **{c: 1
               for c in unseen_classes}
        }
        print("image graph edges & nodes: ", len(image_graph.edges), "&",
              len(image_graph.nodes))
        print("knowledge graph edges & nodes: ", len(kg.edges), "&",
              len(kg.nodes))
        print("labels graph edges & nodes: ", len(labels_graph.edges), "&",
              len(labels_graph.nodes))
        print("final graph edges & nodes: ", len(final_graph.edges), "&",
              len(final_graph.nodes))
        return final_graph, labels_graph, dict_class_identification