Example #1
0
def dimension_reduction():
    # save_model_file()
    constants = GlobalConstants()
    model = Model()
    features = model.load_model('cm_np')
    redn = DimensionReduction(dimension_reduction_model=constants.PCA,
                              extractor_model=constants.CM,
                              matrix=features,
                              conversion=True,
                              k_value=500)
    redn.execute()
    pass
def main():
    """Main function for the Task 8"""
    k_value = get_input_k()
    while k_value > 8:
        print("Please enter a value of k within 8")
        k_value = get_input_k()
    folder = get_input_folder()

    print(global_constants.LINE_SEPARATOR)
    print("User Inputs summary")
    print(global_constants.LINE_SEPARATOR)
    print("k-value: {}\nFolder: {}".format(k_value, folder))
    print(global_constants.LINE_SEPARATOR)

    dim_red = DimensionReduction(None,
                                 "NMF",
                                 k_value,
                                 image_metadata=True,
                                 folder_metadata=folder)
    w, h, model = dim_red.execute()

    # printing the term weight
    print_tw(w, h, image_metadata=True)

    # save to csv
    filename = "task8" + "_" + str(k_value)
    CSVReader().save_to_csv(w, h, filename, image_metadata=True)
    print("Please check the CSV file: output/{}.csv".format(filename))
Example #3
0
def get_x_train(fea_ext_mod, dim_red_mod, k_value, train_set):
    model_interact = Model()
    dim_reduction = DimensionReduction(fea_ext_mod,
                                       dim_red_mod,
                                       k_value,
                                       folder_metadata=train_set,
                                       metadata_collection="labelled")
    obj_lat, feat_lat, model = dim_reduction.execute()
    filename = "{0}_{1}_{2}_{3}".format(fea_ext_mod, dim_red_mod, str(k_value),
                                        os.path.basename(train_set))
    model_interact.save_model(model=model, filename=filename)
    return obj_lat
Example #4
0
def main():
    feature_extraction_model = task6.feature_extraction_model
    dimension_reduction_model = task6.dimension_reduction_model
    k_value_for_ss_similarity = 10

    given_k_value = get_input_k()
    print(global_constants.LINE_SEPARATOR)
    print("User Inputs summary")
    print(global_constants.LINE_SEPARATOR)
    print("k-value: {}".format(given_k_value))
    print(global_constants.LINE_SEPARATOR)

    dim_reduction = DimensionReduction(feature_extraction_model, dimension_reduction_model, k_value_for_ss_similarity)
    # original feature vectors
    obj_feat_matrix = dim_reduction.get_object_feature_matrix()
    # get the img IDs from the database for images in the fit model
    img_set = pd.DataFrame({"imageId": obj_feat_matrix['imageId']})
    # get the metadata for each image with given subject id
    subject_data = dim_reduction.get_metadata("imageName", list(set(img_set["imageId"].tolist())))
    # unique subject IDs in dataset
    dataset_subject_ids = set((subject_data)["id"])
    subject_subject_matrix = []
    m_value = len(img_set)
    starttime = time.time()
    model = task6.load_model(dim_reduction, feature_extraction_model, dimension_reduction_model,
                             k_value_for_ss_similarity)
    folder = path.basename(path.dirname(obj_feat_matrix['path'][0]))

    for i, subjectid in enumerate(dataset_subject_ids):
        given_subject_images = dim_reduction.get_metadata("id", list([subjectid]))["imageName"].tolist()
        image_list_for_subject = list(set(given_subject_images).intersection(set(img_set["imageId"].tolist())))
        similar_subjects = task6.find_similar_subjects(subjectid, image_list_for_subject, model,
                                                       img_set, dim_reduction, m_value, folder)
        subject_subject_matrix.append(np.asarray(list(similar_subjects.values())))

    print("\nTime taken to create subject subject matrix: {}\n".format(time.time() - starttime))
    # perform nmf on subject_subject_matrix
    # given_k_value = 1
    matrix = pd.DataFrame(data={'imageId': list(dataset_subject_ids), 'featureVector': subject_subject_matrix})
    dim_red = DimensionReduction(None, "NMF", given_k_value, subject_subject=True, matrix=matrix)
    w, h, model = dim_red.execute()

    # display latent semantics
    # printing the term weight
    print_tw(w, h, subject_subject=True)
    # save to csv
    filename = "task7" + '_' + str(given_k_value)
    CSVReader().save_to_csv(w, None, filename, subject_subject=True)
    print("Please check the CSV file: output/{}.csv".format(filename))
Example #5
0
def run_task3(feature_extraction_model, dimension_reduction_model, label,
              k_value):
    """Main function for the Task3"""
    # Performs the dimensionality reduction
    dim_reduction = DimensionReduction(feature_extraction_model,
                                       dimension_reduction_model, k_value,
                                       label)
    obj_feature = dim_reduction.get_object_feature_matrix()
    obj_lat, feat_lat, model = dim_reduction.execute()

    # Saves the returned model
    filename = "{0}_{1}_{2}_{3}".format(feature_extraction_model,
                                        dimension_reduction_model,
                                        label.replace(" ", ''), str(k_value))
    model_interact.save_model(model=model, filename=filename)
def compute_latent_semantic_for_label(fea_ext_mod, dim_red_mod, label, k_value,
                                      folder):

    # p2task5.run_task3(fea_ext_mod, dim_red_mod, label, k_value)

    dim_reduction = DimensionReduction(fea_ext_mod,
                                       dim_red_mod,
                                       k_value,
                                       label,
                                       folder_metadata=folder,
                                       metadata_collection="labelled")
    obj_lat, feat_lat, model = dim_reduction.execute()
    # Saves the returned model
    filename = "{0}_{1}_{2}_{3}_{4}".format(fea_ext_mod, dim_red_mod, label,
                                            str(k_value),
                                            os.path.basename(folder))
    model_interact.save_model(model=model, filename=filename)
    return obj_lat, feat_lat, model
def main():
    """Main function for the Task3"""
    feature_extraction_model = get_input_feature_extractor_model()
    dimension_reduction_model = get_input_dimensionality_reduction_model()
    k_value = get_input_k()
    label = get_input_image_label()

    print(global_constants.LINE_SEPARATOR)
    print("User Inputs summary")
    print(global_constants.LINE_SEPARATOR)
    print(
        "Feature Extraction Model: {}\nDimensionality Reduction Model: {}\nk-value: {}"
        .format(feature_extraction_model, dimension_reduction_model, k_value))
    print(global_constants.LINE_SEPARATOR)

    print(global_constants.LINE_SEPARATOR)
    print("Saving the metadata to MongoDB")
    print(global_constants.LINE_SEPARATOR)
    csv_reader.save_hand_csv_mongo("HandInfo.csv")
    print(global_constants.LINE_SEPARATOR)

    # Performs the dimensionality reduction
    dim_reduction = DimensionReduction(feature_extraction_model,
                                       dimension_reduction_model, k_value,
                                       label)
    obj_lat, feat_lat, model = dim_reduction.execute()

    # Saves the returned model
    filename = "{0}_{1}_{2}_{3}".format(feature_extraction_model,
                                        dimension_reduction_model,
                                        label.replace(" ", ''), str(k_value))
    model_interact.save_model(model=model, filename=filename)

    # Printing the term weight pairs
    data_tw = print_tw(obj_lat, feat_lat)

    # save term weight pairs to csv
    filename = "task3_{}_{}_{}_{}".format(feature_extraction_model,
                                          dimension_reduction_model, label,
                                          k_value)
    csv_reader.save_to_csv(obj_lat, feat_lat, filename)
    print("Please check the CSV file: output/{}.csv".format(filename))