def _get_mscn_variance(block, sub_block_size=(50, 50)):

    blocks = segmentation.divide_in_blocks(block, sub_block_size)

    data = []

    for block in blocks:
        mscn_coefficients = transform.get_mscn_coefficients(block)
        flat_coeff = mscn_coefficients.flatten()
        data.append(np.var(flat_coeff))

    return np.sort(data)
def main():

    parser = argparse.ArgumentParser(
        description=
        "Classify zones using complexity criteria (unsupervised learning)")

    parser.add_argument('--data',
                        type=str,
                        help='required data file',
                        required=True)
    parser.add_argument('--clusters',
                        type=int,
                        help='number of expected clusters',
                        default=2)
    parser.add_argument('--output',
                        type=str,
                        help='output folder name',
                        required=True)

    args = parser.parse_args()

    p_data = args.data
    p_clusters = args.clusters
    p_output = args.output

    x_values = []
    images_path = []
    images = []
    zones = []
    scenes = []

    with open(p_data, 'r') as f:
        for line in f.readlines():
            data = line.split(';')
            del data[-1]

            scene = data[0]
            if scene not in scenes:
                scenes.append(scene)

            images_path.append(data[1])
            zones.append(int(data[2]))

            img_arr = segmentation.divide_in_blocks(Image.open(data[1]),
                                                    (200, 200))[int(data[2])]
            images.append(np.array(img_arr))

            x = []
            for v in data[3:]:
                x.append(float(v))

            x_values.append(x)

    print(scenes)
    # plt.show()
    # TODO : save kmean model
    kmeans = KMeans(init='k-means++', n_clusters=p_clusters, n_init=10)
    labels = kmeans.fit(x_values).labels_

    unique, counts = np.unique(labels, return_counts=True)
    print(dict(zip(unique, counts)))

    pca = PCA(n_components=2)
    x_data = pca.fit_transform(x_values)

    # Need to create as global variable so our callback(on_plot_hover) can access
    fig, ax = plt.subplots()
    fig.set_figheight(20)
    fig.set_figwidth(40)

    ax.tick_params(axis='both', which='major', labelsize=20)

    sc = plt.scatter(x_data[:, 0], x_data[:, 1], c=labels, linewidths=10)

    # annot = ax.annotate("", xy=(0,0), xytext=(20,20), textcoords="offset points",
    #                     bbox=dict(boxstyle="round", fc="w"),
    #                     arrowprops=dict(arrowstyle="->"))
    imagebox = OffsetImage(images[0], zoom=1.2)
    imagebox.image.axes = ax

    annot = AnnotationBbox(imagebox,
                           xy=(0, 0),
                           xybox=(-150., 150.),
                           xycoords='data',
                           boxcoords="offset points",
                           pad=0.8,
                           arrowprops=dict(arrowstyle="->"))

    annot.set_visible(False)
    ax.add_artist(annot)

    def update_annot(ind):

        imagebox = OffsetImage([images[n] for n in ind["ind"]][0], zoom=1.2)
        imagebox.image.axes = ax

        pos = sc.get_offsets()[ind["ind"][0]]

        setattr(annot, 'offsetbox', imagebox)
        annot.xy = pos
        # text = "{}, {}".format(" ".join(list(map(str,ind["ind"]))),
        #                     " ".join([images_path[n] for n in ind["ind"]]))
        # annot.text(text)
        # #annot.get_bbox_patch().set_facecolor(cmap(norm(c[ind["ind"][0]])))
        # annot.get_bbox_patch().set_alpha(0.4)

    def hover(event):
        vis = annot.get_visible()
        if event.inaxes == ax:
            cont, ind = sc.contains(event)
            if cont:
                update_annot(ind)
                annot.set_visible(True)
                fig.canvas.draw_idle()
            else:
                if vis:
                    annot.set_visible(False)
                    fig.canvas.draw_idle()

    fig.canvas.mpl_connect("motion_notify_event", hover)
    #plt.show()

    if not os.path.exists(model_output_folder):
        os.makedirs(model_output_folder)

    model_path = os.path.join(model_output_folder, p_output + '.joblib')
    print('Model saved into {0}'.format(model_path))
    joblib.dump(kmeans, model_path)
Ejemplo n.º 3
0
def display_data_scenes(data_type, p_scene, p_kind):
    """
    @brief Method which displays data from scene
    @param data_type,  feature choice
    @param scene, scene choice
    @param mode, normalization choice
    @return nothing
    """

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    # go ahead each scenes
    for folder_scene in scenes:

        if p_scene == folder_scene:
            print(folder_scene)
            scene_path = os.path.join(path, folder_scene)

            # construct each zones folder name
            zones_folder = []

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str

                current_zone = "zone" + index_str
                zones_folder.append(current_zone)

            zones_images_data = []
            threshold_info = []

            # get all images of folder
            scene_images = sorted([
                os.path.join(scene_path, img) for img in os.listdir(scene_path)
                if cfg.scene_image_extension in img
            ])

            start_image_path = scene_images[0]
            end_image_path = scene_images[-1]

            start_quality_image = dt.get_scene_image_quality(scene_images[0])
            end_quality_image = dt.get_scene_image_quality(scene_images[-1])

            for id_zone, zone_folder in enumerate(zones_folder):

                zone_path = os.path.join(scene_path, zone_folder)

                # get threshold information
                path_seuil = os.path.join(zone_path, seuil_expe_filename)

                # open treshold path and get this information
                with open(path_seuil, "r") as seuil_file:
                    threshold_learned = int(seuil_file.readline().strip())

                threshold_image_found = False

                for img_path in scene_images:
                    current_quality_image = dt.get_scene_image_quality(
                        img_path)

                    if threshold_learned < int(current_quality_image
                                               ) and not threshold_image_found:

                        threshold_image_found = True
                        threshold_image_path = img_path

                        threshold_image = dt.get_scene_image_postfix(img_path)
                        threshold_info.append(threshold_image)

                # all indexes of picture to plot
                images_path = [
                    start_image_path, threshold_image_path, end_image_path
                ]
                images_data = []

                for img_path in images_path:

                    current_img = Image.open(img_path)
                    img_blocks = segmentation.divide_in_blocks(
                        current_img, (200, 200))

                    # getting expected block id
                    block = img_blocks[id_zone]

                    data = get_image_features(data_type, block)

                    ##################
                    # Data mode part #
                    ##################

                    # modify data depending mode

                    if p_kind == 'svdn':
                        data = utils.normalize_arr(data)

                    if p_kind == 'svdne':
                        path_min_max = os.path.join(
                            path, data_type + min_max_filename)

                        with open(path_min_max, 'r') as f:
                            min_val = float(f.readline())
                            max_val = float(f.readline())

                        data = utils.normalize_arr_with_range(
                            data, min_val, max_val)

                    # append of data
                    images_data.append(data)

                zones_images_data.append(images_data)

            fig = plt.figure(figsize=(8, 8))
            fig.suptitle(data_type + " values for " + p_scene +
                         " scene (normalization : " + p_kind + ")",
                         fontsize=20)

            for id, data in enumerate(zones_images_data):
                fig.add_subplot(4, 4, (id + 1))
                plt.plot(data[0], label='Noisy_' + start_quality_image)
                plt.plot(data[1], label='Threshold_' + threshold_info[id])
                plt.plot(data[2], label='Reference_' + end_quality_image)
                plt.ylabel(data_type + ' SVD, ZONE_' + str(id + 1),
                           fontsize=18)
                plt.xlabel('Vector features', fontsize=18)
                plt.legend(bbox_to_anchor=(0.5, 1),
                           loc=2,
                           borderaxespad=0.2,
                           fontsize=18)
                plt.ylim(0, 0.1)
            plt.show()
def main():

    parser = argparse.ArgumentParser(description="Read and compute entropy data file")

    parser.add_argument('--model', type=str, help='model file')
    parser.add_argument('--method', type=str, help='method name to used', choices=cfg.features_choices_labels, default=cfg.features_choices_labels[0])
    parser.add_argument('--interval', type=str, help='Interval value to keep from svd', default='"0, 200"')
    parser.add_argument('--kind', type=str, help='Kind of normalization level wished', choices=cfg.normalization_choices)
    parser.add_argument('--imnorm', type=int, help="specify if image is normalized before computing something", default=0, choices=[0, 1])
    parser.add_argument('--scene', type=str, help='Scene index to use', choices=cfg.scenes_indices)
    parser.add_argument('--save', type=str, help='filename where to save input data')
    parser.add_argument('--label', type=str, help='label to use when saving thresholds')

    args = parser.parse_args()

    p_model    = args.model
    p_method   = args.method
    p_interval = list(map(int, args.interval.split(',')))
    #p_n_stop   = args.n_stop
    p_imnorm   = args.imnorm
    p_scene    = args.scene
    p_mode     = args.kind
    p_save     = args.save
    p_label    = args.label

    p_n_stop = 1
    begin, end = p_interval

    # 1. get scene name
    scenes_list = cfg.scenes_names
    scenes_indices = cfg.scenes_indices

    scene_index = scenes_indices.index(p_scene.strip())
    scene = scenes_list[scene_index]

    scene_path = os.path.join(cfg.dataset_path, scene)

    # 2. load model and compile it

    # TODO : check kind of model
    model = joblib.load(p_model)
    # model.compile(loss='binary_crossentropy',
    #               optimizer='rmsprop',
    #               metrics=['accuracy'])


    estimated_thresholds = []
    n_estimated_thresholds = []
    human_thresholds = []

    # 3. retrieve human_thresholds
    # construct zones folder
    zones_list = []

    for index in zones_indices:

        index_str = str(index)

        while len(index_str) < 2:
            index_str = "0" + index_str
        
        zones_list.append(cfg.zone_folder + index_str)

    for zone in zones_list:
            zone_path = os.path.join(scene_path, zone)

            with open(os.path.join(zone_path, cfg.seuil_expe_filename), 'r') as f:
                human_thresholds.append(int(f.readline()))

    # 4. get estimated thresholds using model and specific method
    images_path = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
    number_of_images = len(images_path)
    image_indices = [ dt.get_scene_image_quality(img_path) for img_path in images_path ]

    image_counter = 0

    print(human_thresholds)

    # append empty list
    for zone in zones_list:
        estimated_thresholds.append(None)
        n_estimated_thresholds.append(0)

    for img_i, img_path in enumerate(images_path):

        blocks = segmentation.divide_in_blocks(Image.open(img_path), (200, 200))

        for index, block in enumerate(blocks):
            
            if estimated_thresholds[index] is None:
                # normalize if necessary
                if p_imnorm:
                    block = np.array(block) / 255.
                
                # check if prediction is possible
                data = np.array(get_image_features(p_method, np.array(block)))

                if p_mode == 'svdn':
                    data = utils.normalize_arr_with_range(data)

                data = data[begin:end]

                #data = np.expand_dims(data, axis=0)
                #print(data.shape)
                
                prob = model.predict(np.array(data).reshape(1, -1))[0]
                #print(index, ':', image_indices[img_i], '=>', prob)

                if prob < 0.5:
                    n_estimated_thresholds[index] += 1

                    # if same number of detection is attempted
                    if n_estimated_thresholds[index] >= p_n_stop:
                        estimated_thresholds[index] = image_indices[img_i]
                else:
                    n_estimated_thresholds[index] = 0

        # write progress bar
        write_progress((image_counter + 1) / number_of_images)
        
        image_counter = image_counter + 1
    
    # default label
    for i, _ in enumerate(zones_list):
        if estimated_thresholds[i] == None:
            estimated_thresholds[i] = image_indices[-1]

    # 6. save estimated thresholds into specific file
    print(estimated_thresholds)
    print(p_save)
    if p_save is not None:
        with open(p_save, 'a') as f:
            f.write(p_label + ';')

            for t in estimated_thresholds:
                f.write(str(t) + ';')
            f.write('\n')
def main():

    parser = argparse.ArgumentParser(
        description="Read and compute entropy data file")

    parser.add_argument('--model', type=str, help='model .h5 file')
    parser.add_argument('--folder',
                        type=str,
                        help='folder where scene dataset is available',
                        required=True)
    parser.add_argument(
        '--features',
        type=str,
        help="list of features choice in order to compute data",
        default='svd_reconstruction, ipca_reconstruction',
        required=True)
    parser.add_argument(
        '--params',
        type=str,
        help=
        "list of specific param for each feature choice (See README.md for further information in 3D mode)",
        default='100, 200 :: 50, 25',
        required=True)
    parser.add_argument('--size',
                        type=str,
                        help="specific size of image",
                        default='100, 100',
                        required=True)
    parser.add_argument('--n_stop',
                        type=int,
                        help='number of detection to make sure to stop',
                        default=1)
    parser.add_argument('--save',
                        type=str,
                        help='filename where to save input data')
    parser.add_argument('--label',
                        type=str,
                        help='label to use when saving thresholds')

    args = parser.parse_args()

    p_model = args.model
    p_folder = args.folder
    p_features = list(map(str.strip, args.features.split(',')))
    p_params = list(map(str.strip, args.params.split('::')))
    p_size = args.size
    p_n_stop = args.n_stop
    p_save = args.save
    p_label = args.label

    # 1. Load expected transformations

    # list of transformations
    transformations = []

    for id, feature in enumerate(p_features):

        if feature not in cfg.features_choices_labels or feature == 'static':
            raise ValueError(
                "Unknown feature, please select a correct feature (`static` excluded) : ",
                cfg.features_choices_labels)

        transformations.append(Transformation(feature, p_params[id], p_size))

    # 2. load model and compile it

    # TODO : check kind of model
    model = load_model(p_model)
    # model.compile(loss='binary_crossentropy',
    #               optimizer='rmsprop',
    #               metrics=['accuracy'])

    estimated_thresholds = []
    n_estimated_thresholds = []

    scene_path = p_folder

    if not os.path.exists(scene_path):
        print('Unvalid scene path:', scene_path)
        exit(0)

    # 3. retrieve human_thresholds
    # construct zones folder
    zones_indices = np.arange(16)
    zones_list = []

    for index in zones_indices:

        index_str = str(index)

        while len(index_str) < 2:
            index_str = "0" + index_str

        zones_list.append(cfg.zone_folder + index_str)

    # 4. get estimated thresholds using model and specific method
    images_path = sorted([
        os.path.join(scene_path, img) for img in os.listdir(scene_path)
        if cfg.scene_image_extension in img
    ])
    number_of_images = len(images_path)
    image_indices = [
        dt.get_scene_image_quality(img_path) for img_path in images_path
    ]

    image_counter = 0

    # append empty list
    for _ in zones_list:
        estimated_thresholds.append(None)
        n_estimated_thresholds.append(0)

    for img_i, img_path in enumerate(images_path):

        blocks = segmentation.divide_in_blocks(Image.open(img_path),
                                               (200, 200))

        for index, block in enumerate(blocks):

            if estimated_thresholds[index] is None:

                transformed_list = []
                # compute data here
                for transformation in transformations:
                    transformed = transformation.getTransformedImage(block)
                    transformed_list.append(transformed)

                data = np.array(transformed_list)

                # compute input size
                n_chanels, _, _ = data.shape

                if K.image_data_format() == 'chanels_first':
                    if n_chanels > 1:
                        data = np.expand_dims(data, axis=0)

                else:
                    if n_chanels > 1:
                        data = data.transpose()
                        data = np.expand_dims(data, axis=0)
                    else:
                        data = data.transpose()

                data = np.expand_dims(data, axis=0)

                probs = model.predict(np.array(data))[0]
                prediction = list(probs).index(max(probs))
                #print(index, ':', image_indices[img_i], '=>', prediction)

                if prediction == 0:
                    n_estimated_thresholds[index] += 1

                    # if same number of detection is attempted
                    if n_estimated_thresholds[index] >= p_n_stop:
                        estimated_thresholds[index] = image_indices[img_i]
                else:
                    n_estimated_thresholds[index] = 0

        # write progress bar
        write_progress((image_counter + 1) / number_of_images)

        image_counter = image_counter + 1

    # default label
    for i, _ in enumerate(zones_list):
        if estimated_thresholds[i] == None:
            estimated_thresholds[i] = image_indices[-1]

    # 6. save estimated thresholds into specific file
    print('\nEstimated thresholds', estimated_thresholds)
    if p_save is not None:
        with open(p_save, 'a') as f:
            f.write(p_label + ';')

            for t in estimated_thresholds:
                f.write(str(t) + ';')
            f.write('\n')
def generate_data_svd(data_type, color, mode):
    """
    @brief Method which generates all .csv files from scenes
    @param data_type,  feature choice
    @param mode, normalization choice
    @return nothing
    """

    scenes = os.listdir(path)

    # filter scene
    scenes = [s for s in scenes if calibration_folder not in s]

    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    # keep in memory min and max data found from data_type
    min_val_found = sys.maxsize
    max_val_found = 0

    data_min_max_filename = os.path.join(path, data_type + min_max_filename)

    # go ahead each scenes
    for id_scene, folder_scene in enumerate(scenes):

        print(folder_scene)
        scene_path = os.path.join(path, folder_scene)

        for noise in noise_choices:

            noise_path = os.path.join(scene_path, noise)

            # getting output filename
            if color:
                output_svd_filename = data_type + "_color_" + mode + generic_output_file_svd
            else:
                output_svd_filename = data_type + "_" + mode + generic_output_file_svd

            # construct each zones folder name
            zones_folder = []
            svd_output_files = []

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str

                current_zone = "zone"+index_str
                zones_folder.append(current_zone)

                zone_path = os.path.join(noise_path, current_zone)

                if not os.path.exists(zone_path):
                    os.makedirs(zone_path)

                svd_file_path = os.path.join(zone_path, output_svd_filename)

                # add writer into list
                svd_output_files.append(open(svd_file_path, 'w'))

            counter_index = 1

            while(counter_index < end_counter_index):

                if counter_index % picture_step == 0:
                    counter_index_str = str(counter_index)

                    if color:
                        img_path = os.path.join(noise_path, folder_scene + "_" + noise + "_color_" + counter_index_str + ".png")
                    else:
                        img_path = os.path.join(noise_path, folder_scene + "_" + noise + "_" + counter_index_str + ".png")

                    current_img = Image.open(img_path)
                    img_blocks = divide_in_blocks(current_img, (200, 200))

                    for id_block, block in enumerate(img_blocks):

                        ###########################
                        # feature computation part #
                        ###########################

                        data = get_image_features(data_type, block)

                        ##################
                        # Data mode part #
                        ##################

                        # modify data depending mode
                        if mode == 'svdne':

                            # getting max and min information from min_max_filename
                            with open(data_min_max_filename, 'r') as f:
                                min_val = float(f.readline())
                                max_val = float(f.readline())

                            data = utils.normalize_arr_with_range(data, min_val, max_val)

                        if mode == 'svdn':
                            data = utils.normalize_arr(data)

                        # save min and max found from dataset in order to normalize data using whole data known
                        if mode == 'svd':

                            current_min = data.min()
                            current_max = data.max()

                            if current_min < min_val_found:
                                min_val_found = current_min

                            if current_max > max_val_found:
                                max_val_found = current_max

                        # now write data into current writer
                        current_file = svd_output_files[id_block]

                        # add of index
                        current_file.write(counter_index_str + ';')

                        for val in data:
                            current_file.write(str(val) + ";")

                        current_file.write('\n')

                if color:
                    print(data_type + "_" + noise + "_color_" + mode + "_" + folder_scene + " - " + "{0:.2f}".format((counter_index) / (end_counter_index)* 100.) + "%")
                else:
                    print(data_type + "_" + noise + "_"+ mode + "_" + folder_scene + " - " + "{0:.2f}".format((counter_index) / (end_counter_index)* 100.) + "%")

                sys.stdout.write("\033[F")

                counter_index += 1

            for f in svd_output_files:
                f.close()

            if color:
                print(data_type + "_" + noise + "_color_" + mode + "_" + folder_scene + " - " + "Done...")
            else:
                print(data_type + "_" + noise + "_"+ mode + "_" + folder_scene + " - " + "Done...")


    # save current information about min file found
    if mode == 'svd':
        with open(data_min_max_filename, 'w') as f:
            f.write(str(min_val_found) + '\n')
            f.write(str(max_val_found) + '\n')

    print("%s : end of data generation\n" % mode)
def display_data_scenes(p_scene, p_bits, p_shifted):
    """
    @brief Method which generates all .csv files from scenes photos
    @param p_scene, scene we want to show values
    @param nb_bits, number of bits expected
    @param p_shifted, number of bits expected to be shifted
    @return nothing
    """

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    # go ahead each scenes
    for folder_scene in scenes:

        if p_scene == folder_scene:
            print(folder_scene)
            scene_path = os.path.join(path, folder_scene)

            # construct each zones folder name
            zones_folder = []

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str

                current_zone = "zone" + index_str
                zones_folder.append(current_zone)

            zones_images_data = []
            threshold_info = []

            # get all images of folder
            scene_images = sorted([
                os.path.join(scene_path, img) for img in os.listdir(scene_path)
                if cfg.scene_image_extension in img
            ])

            start_image_path = scene_images[0]
            end_image_path = scene_images[-1]

            start_quality_image = dt.get_scene_image_quality(scene_images[0])
            end_quality_image = dt.get_scene_image_quality(scene_images[-1])

            for id_zone, zone_folder in enumerate(zones_folder):

                zone_path = os.path.join(scene_path, zone_folder)

                # get threshold information
                path_seuil = os.path.join(zone_path, seuil_expe_filename)

                # open treshold path and get this information
                with open(path_seuil, "r") as seuil_file:
                    threshold_learned = int(seuil_file.readline().strip())

                threshold_image_found = False

                # for each images
                for img_path in scene_images:
                    current_quality_image = dt.get_scene_image_quality(
                        img_path)

                    if threshold_learned < int(current_quality_image
                                               ) and not threshold_image_found:

                        threshold_image_found = True
                        threshold_image_path = img_path

                        threshold_image = dt.get_scene_image_postfix(img_path)
                        threshold_info.append(threshold_image)

                # all indexes of picture to plot
                images_path = [
                    start_image_path, threshold_image_path, end_image_path
                ]
                images_data = []

                for img_path in images_path:

                    current_img = Image.open(img_path)
                    img_blocks = segmentation.divide_in_blocks(
                        current_img, (200, 200))

                    # getting expected block id
                    block = img_blocks[id_zone]

                    # get data from mode
                    # Here you can add the way you compute data
                    low_bits_block = transform.rgb_to_LAB_L_bits(
                        block, (p_shifted + 1, p_shifted + p_bits + 1))
                    data = compression.get_SVD_s(low_bits_block)

                    ##################
                    # Data mode part #
                    ##################

                    # modify data depending mode
                    data = utils.normalize_arr(data)
                    images_data.append(data)

                zones_images_data.append(images_data)

            fig = plt.figure(figsize=(8, 8))
            fig.suptitle('Lab SVD ' + str(p_bits) + ' bits shifted by ' +
                         str(p_shifted) + " for " + p_scene + " scene",
                         fontsize=20)

            for id, data in enumerate(zones_images_data):
                fig.add_subplot(4, 4, (id + 1))
                plt.plot(data[0], label='Noisy_' + start_quality_image)
                plt.plot(data[1], label='Threshold_' + threshold_info[id])
                plt.plot(data[2], label='Reference_' + end_quality_image)
                plt.ylabel('Lab SVD ' + str(p_bits) + ' bits shifted by ' +
                           str(p_shifted) + ', ZONE_' + str(id + 1),
                           fontsize=14)
                plt.xlabel('Vector features', fontsize=16)
                plt.legend(bbox_to_anchor=(0.5, 1),
                           loc=2,
                           borderaxespad=0.2,
                           fontsize=14)
                plt.ylim(0, 0.1)
            plt.show()
def main():

    parser = argparse.ArgumentParser(
        description="Read and compute entropy data file")

    parser.add_argument(
        '--data',
        type=str,
        help='entropy file data with estimated threshold to read and compute')
    parser.add_argument('--scene',
                        type=str,
                        help='Scene index to use',
                        choices=cfg.scenes_indices)
    parser.add_argument('--metric',
                        type=str,
                        help='metric to use to compare',
                        choices=['ssim', 'mse', 'rmse', 'mae', 'psnr'])
    parser.add_argument('--norm',
                        type=int,
                        help='normalize or not values',
                        choices=[0, 1],
                        default=0)
    parser.add_argument('--info', type=str, help='title information to add')

    args = parser.parse_args()

    p_data = args.data
    p_scene = args.scene
    p_metric = args.metric
    p_norm = args.norm
    p_info = args.info

    # check fr_iqa metric
    try:
        fr_iqa = getattr(fr, p_metric)
    except AttributeError:
        raise NotImplementedError("FR IQA `{}` not implement `{}`".format(
            fr.__name__, p_metric))

    scenes_list = cfg.scenes_names
    scenes_indices = cfg.scenes_indices

    scene_index = scenes_indices.index(p_scene.strip())
    scene = scenes_list[scene_index]

    displayed_data = {}
    steps_zones = []
    scene_found = False
    reference_zones = None

    # read line by line file to estimate threshold entropy stopping criteria
    with open(p_data, 'r') as f:
        lines = f.readlines()

        for line in lines:

            data = line.split(';')
            scene_name = data[0]
            zone_index = int(data[1])
            steps = data[4].split(',')

            if scene_name == scene:

                if not scene_found:
                    scene_folder = os.path.join(cfg.dataset_path, scene_name)
                    images = sorted([
                        img for img in os.listdir(scene_folder)
                        if cfg.scene_image_extension in img
                    ])
                    images_scene = [
                        Image.open(os.path.join(scene_folder, img))
                        for img in images
                    ]
                    scene_found = True

                    # get zones of each step images
                    for i, _ in enumerate(steps):
                        steps_zones.append(
                            segmentation.divide_in_blocks(
                                images_scene[i], block_size))

                    # get zone of reference
                    reference_name = cfg.references_scenes[scene_index]
                    reference_path = os.path.join(cfg.references_folder,
                                                  reference_name)
                    reference_zones = segmentation.divide_in_blocks(
                        Image.open(reference_path), block_size)

                # TODO : load images at each step and compare error
                # store all needed data
                displayed_data[zone_index] = {}
                displayed_data[zone_index]['zone'] = data[2]
                displayed_data[zone_index]['human_threshold'] = data[3]
                displayed_data[zone_index]['steps'] = list(map(int, steps))
                displayed_data[zone_index]['data'] = list(
                    map(float, data[5].split(',')))

                errors = []

                previous_block = None
                # get errors from images zones (using step and reference)
                for i, _ in enumerate(steps):

                    if i > 0:
                        noisy_block = steps_zones[i][zone_index]

                        step_error = fr_iqa(noisy_block, previous_block)
                        errors.append(step_error)
                    previous_block = steps_zones[i][zone_index]

                displayed_data[zone_index]['errors'] = errors

    display_estimated_thresholds(scene, displayed_data, p_info, p_metric,
                                 p_norm)
Ejemplo n.º 9
0
def generate_data_model(_scenes_list,
                        _filename,
                        _transformations,
                        _scenes,
                        _nb_zones=4,
                        _random=0,
                        _only_noisy=0):

    output_train_filename = _filename + ".train"
    output_test_filename = _filename + ".test"

    if not '/' in output_train_filename:
        raise Exception(
            "Please select filename with directory path to save data. Example : data/dataset"
        )

    # create path if not exists
    if not os.path.exists(output_data_folder):
        os.makedirs(output_data_folder)

    train_file_data = []
    test_file_data = []

    scenes = os.listdir(dataset_path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    # go ahead each scenes
    for id_scene, folder_scene in enumerate(_scenes_list):

        scene_path = os.path.join(dataset_path, folder_scene)

        config_file_path = os.path.join(scene_path, config_filename)

        # only get last image path
        with open(config_file_path, "r") as config_file:
            last_image_name = config_file.readline().strip()

        ref_image_path = os.path.join(scene_path, last_image_name)

        zones_indices = zones

        # shuffle list of zones (=> randomly choose zones)
        # only in random mode
        if _random:
            random.shuffle(zones_indices)

        # store zones learned
        learned_zones_indices = zones_indices[:_nb_zones]

        # write into file
        folder_learned_path = os.path.join(learned_folder,
                                           _filename.split('/')[1])

        if not os.path.exists(folder_learned_path):
            os.makedirs(folder_learned_path)

        file_learned_path = os.path.join(folder_learned_path,
                                         folder_scene + '.csv')

        with open(file_learned_path, 'w') as f:
            for i in learned_zones_indices:
                f.write(str(i) + ';')

        ref_image_blocks = divide_in_blocks(Image.open(ref_image_path),
                                            cfg.keras_img_size)

        for id_zone, index_folder in enumerate(zones_indices):

            index_str = str(index_folder)
            if len(index_str) < 2:
                index_str = "0" + index_str

            current_zone_folder = "zone" + index_str
            zone_path = os.path.join(scene_path, current_zone_folder)

            # path of zone of reference image
            # ref_image_block_path = os.path.join(zone_path, last_image_name)

            # compute augmented images for ref image
            current_ref_zone_image = ref_image_blocks[id_zone]

            ref_image_name_prefix = last_image_name.replace('.png', '')
            dt.augmented_data_image(current_ref_zone_image, zone_path,
                                    ref_image_name_prefix)

            # get list of all augmented ref images
            ref_augmented_images = [
                os.path.join(zone_path, f) for f in os.listdir(zone_path)
                if ref_image_name_prefix in f
            ]

            # custom path for interval of reconstruction and features
            features_path = []

            for transformation in _transformations:

                # check if it's a static content and create augmented images if necessary
                if transformation.getName() == 'static':

                    # {sceneName}/zoneXX/static
                    static_features_path = os.path.join(
                        zone_path, transformation.getName())

                    # img.png
                    image_name = transformation.getParam().split('/')[-1]

                    # {sceneName}/zoneXX/static/img
                    image_prefix_name = image_name.replace('.png', '')
                    image_folder_path = os.path.join(static_features_path,
                                                     image_prefix_name)

                    if not os.path.exists(image_folder_path):
                        os.makedirs(image_folder_path)

                    features_path.append(image_folder_path)

                    # get image path to manage
                    # {sceneName}/static/img.png
                    transform_image_path = os.path.join(
                        scene_path, transformation.getName(), image_name)
                    static_transform_image = Image.open(transform_image_path)

                    static_transform_image_block = divide_in_blocks(
                        static_transform_image, cfg.keras_img_size)[id_zone]

                    # generate augmented data
                    dt.augmented_data_image(static_transform_image_block,
                                            image_folder_path,
                                            image_prefix_name)

                else:
                    features_interval_path = os.path.join(
                        zone_path, transformation.getTransformationPath())
                    features_path.append(features_interval_path)

            # as labels are same for each features
            for label in os.listdir(features_path[0]):

                if (label == cfg.not_noisy_folder
                        and _only_noisy == 0) or label == cfg.noisy_folder:

                    label_features_path = []

                    for path in features_path:
                        label_path = os.path.join(path, label)
                        label_features_path.append(label_path)

                    # getting images list for each features
                    features_images_list = []

                    for index_features, label_path in enumerate(
                            label_features_path):

                        if _transformations[index_features].getName(
                        ) == 'static':
                            # by default append nothing..
                            features_images_list.append([])
                        else:
                            images = sorted(os.listdir(label_path))
                            features_images_list.append(images)

                    # construct each line using all images path of each
                    for index_image in range(0, len(features_images_list[0])):

                        images_path = []

                        # get information about rotation and flip from first transformation (need to be a not static transformation)
                        current_post_fix = features_images_list[0][
                            index_image].split(
                                cfg.post_image_name_separator)[-1]

                        # getting images with same index and hence name for each features (transformation)
                        for index_features in range(0, len(features_path)):

                            # custom behavior for static transformation (need to check specific image)
                            if _transformations[index_features].getName(
                            ) == 'static':
                                # add static path with selecting correct data augmented image
                                image_name = _transformations[
                                    index_features].getParam().split(
                                        '/')[-1].replace('.png', '')
                                img_path = os.path.join(
                                    features_path[index_features], image_name +
                                    cfg.post_image_name_separator +
                                    current_post_fix)
                                images_path.append(img_path)
                            else:
                                img_path = features_images_list[
                                    index_features][index_image]
                                images_path.append(
                                    os.path.join(
                                        label_features_path[index_features],
                                        img_path))

                        # get information about rotation and flip
                        current_post_fix = images_path[0].split(
                            cfg.post_image_name_separator)[-1]

                        # get ref block which matchs we same information about rotation and flip
                        augmented_ref_image_block_path = next(
                            img for img in ref_augmented_images
                            if img.split(cfg.post_image_name_separator)[-1] ==
                            current_post_fix)

                        line = augmented_ref_image_block_path + ';'

                        # compute line information with all images paths
                        for id_path, img_path in enumerate(images_path):
                            if id_path < len(images_path) - 1:
                                line = line + img_path + '::'
                            else:
                                line = line + img_path

                        line = line + '\n'

                        if id_zone < _nb_zones and folder_scene in _scenes:
                            train_file_data.append(line)
                        else:
                            test_file_data.append(line)

    train_file = open(output_train_filename, 'w')
    test_file = open(output_test_filename, 'w')

    random.shuffle(train_file_data)
    random.shuffle(test_file_data)

    for line in train_file_data:
        train_file.write(line)

    for line in test_file_data:
        test_file.write(line)

    train_file.close()
    test_file.close()
def main():

    parser = argparse.ArgumentParser(description="Script which predicts threshold using specific keras model")

    parser.add_argument('--features', type=str, 
                                     help="list of features choice in order to compute data",
                                     default='svd_reconstruction, ipca_reconstruction',
                                     required=True)
    parser.add_argument('--params', type=str, 
                                    help="list of specific param for each metric choice (See README.md for further information in 3D mode)", 
                                    default='100, 200 :: 50, 25',
                                    required=True)
    parser.add_argument('--model', type=str, help='.json file of keras model', required=True)
    parser.add_argument('--size', type=str, help="Expected output size before processing transformation", default="100,100")
    parser.add_argument('--renderer', type=str, 
                                      help='Renderer choice in order to limit scenes used', 
                                      choices=cfg.renderer_choices, 
                                      default='all', 
                                      required=True)

    args = parser.parse_args()

    p_features   = list(map(str.strip, args.features.split(',')))
    p_params     = list(map(str.strip, args.params.split('::')))
    p_model_file = args.model
    p_size       = args.size
    p_renderer   = args.renderer

    scenes_list = dt.get_renderer_scenes_names(p_renderer)

    scenes = os.listdir(scenes_path)

    print(scenes)

    # go ahead each scenes
    for id_scene, folder_scene in enumerate(scenes):

        # only take in consideration renderer scenes
        if folder_scene in scenes_list:

            print(folder_scene)

            scene_path = os.path.join(scenes_path, folder_scene)

            # get all images of folder
            scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
            number_scene_image = len(scene_images)

            start_quality_image = dt.get_scene_image_quality(scene_images[0])
            end_quality_image   = dt.get_scene_image_quality(scene_images[-1])
            # using first two images find the step of quality used
            quality_step_image  = dt.get_scene_image_quality(scene_images[1]) - start_quality_image

            threshold_expes = []
            threshold_expes_found = []
            block_predictions_str = []

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str
                zone_folder = "zone"+index_str

                threshold_path_file = os.path.join(os.path.join(scene_path, zone_folder), threshold_expe_filename)

                with open(threshold_path_file) as f:
                    threshold = int(f.readline())
                    threshold_expes.append(threshold)

                    # Initialize default data to get detected model threshold found
                    threshold_expes_found.append(int(end_quality_image)) # by default use max

                block_predictions_str.append(index_str + ";" + p_model_file + ";" + str(threshold) + ";" + str(start_quality_image) + ";" + str(quality_step_image))

            # for each images
            for img_path in scene_images:

                current_img = Image.open(img_path)
                img_blocks = divide_in_blocks(current_img, cfg.sub_image_size)

                current_quality_image = dt.get_scene_image_quality(img_path)

                for id_block, block in enumerate(img_blocks):

                    # check only if necessary for this scene (not already detected)
                    #if not threshold_expes_detected[id_block]:

                        tmp_file_path = tmp_filename.replace('__model__',  p_model_file.split('/')[-1].replace('.json', '_'))
                        block.save(tmp_file_path)

                        python_cmd = "python predict_noisy_image.py --image " + tmp_file_path + \
                                        " --features " + p_features + \
                                        " --params " + p_params + \
                                        " --model " + p_model_file + \
                                        " --size " + p_size 

                        ## call command ##
                        p = subprocess.Popen(python_cmd, stdout=subprocess.PIPE, shell=True)

                        (output, err) = p.communicate()

                        ## Wait for result ##
                        p_status = p.wait()

                        prediction = int(output)

                        # save here in specific file of block all the predictions done
                        block_predictions_str[id_block] = block_predictions_str[id_block] + ";" + str(prediction)

                        print(str(id_block) + " : " + str(current_quality_image) + "/" + str(threshold_expes[id_block]) + " => " + str(prediction))

                print("------------------------")
                print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)))
                print("------------------------")

            # end of scene => display of results

            # construct path using model name for saving threshold map folder
            model_threshold_path = os.path.join(threshold_map_folder, p_model_file.split('/')[-1].replace('.joblib', ''))

            # create threshold model path if necessary
            if not os.path.exists(model_threshold_path):
                os.makedirs(model_threshold_path)

            map_filename = os.path.join(model_threshold_path, simulation_curves_zones + folder_scene)
            f_map = open(map_filename, 'w')

            for line in block_predictions_str:
                f_map.write(line + '\n')
            f_map.close()

            print("Scene " + str(id_scene + 1) + "/" + str(len(maxwell_scenes)) + " Done..")
            print("------------------------")

            print("Model predictions are saved into %s" % map_filename)
def generate_data_model(_filename, _transformations, _dataset_folder, _selected_zones):

    output_train_filename = os.path.join(output_data_folder, _filename, _filename + ".train")
    output_test_filename = os.path.join(output_data_folder, _filename, _filename + ".test")

    # create path if not exists
    if not os.path.exists(os.path.join(output_data_folder, _filename)):
        os.makedirs(os.path.join(output_data_folder, _filename))

    train_file_data = []
    test_file_data  = []

    # specific number of zones (zones indices)
    zones = np.arange(16)

    # go ahead each scenes
    for folder_scene in _selected_zones:

        scene_path = os.path.join(_dataset_folder, folder_scene)

        train_zones = _selected_zones[folder_scene]

        for id_zone, index_folder in enumerate(zones):

            index_str = str(index_folder)
            if len(index_str) < 2:
                index_str = "0" + index_str
            
            current_zone_folder = "zone" + index_str
            zone_path = os.path.join(scene_path, current_zone_folder)

            # custom path for interval of reconstruction and metric

            features_path = []

            for transformation in _transformations:
                
                # check if it's a static content and create augmented images if necessary
                if transformation.getName() == 'static':
                    
                    # {sceneName}/zoneXX/static
                    static_metric_path = os.path.join(zone_path, transformation.getName())

                    # img.png
                    image_name = transformation.getParam().split('/')[-1]

                    # {sceneName}/zoneXX/static/img
                    image_prefix_name = image_name.replace('.png', '')
                    image_folder_path = os.path.join(static_metric_path, image_prefix_name)
                    
                    if not os.path.exists(image_folder_path):
                        os.makedirs(image_folder_path)

                    features_path.append(image_folder_path)

                    # get image path to manage
                    # {sceneName}/static/img.png
                    transform_image_path = os.path.join(scene_path, transformation.getName(), image_name) 
                    static_transform_image = Image.open(transform_image_path)

                    static_transform_image_block = divide_in_blocks(static_transform_image, cfg.sub_image_size)[id_zone]

                    dt.augmented_data_image(static_transform_image_block, image_folder_path, image_prefix_name)

                else:
                    metric_interval_path = os.path.join(zone_path, transformation.getTransformationPath())
                    features_path.append(metric_interval_path)

            # as labels are same for each metric
            for label in os.listdir(features_path[0]):

                label_features_path = []

                for path in features_path:
                    label_path = os.path.join(path, label)
                    label_features_path.append(label_path)

                # getting images list for each metric
                features_images_list = []
                    
                for index_metric, label_path in enumerate(label_features_path):

                    if _transformations[index_metric].getName() == 'static':
                        # by default append nothing..
                        features_images_list.append([])
                    else:
                        images = sorted(os.listdir(label_path))
                        features_images_list.append(images)

                # construct each line using all images path of each
                for index_image in range(0, len(features_images_list[0])):
                    
                    images_path = []

                    # get information about rotation and flip from first transformation (need to be a not static transformation)
                    current_post_fix =  features_images_list[0][index_image].split(cfg.post_image_name_separator)[-1]

                    # getting images with same index and hence name for each metric (transformation)
                    for index_metric in range(0, len(features_path)):

                        # custom behavior for static transformation (need to check specific image)
                        if _transformations[index_metric].getName() == 'static':
                            # add static path with selecting correct data augmented image
                            image_name = _transformations[index_metric].getParam().split('/')[-1].replace('.png', '')
                            img_path = os.path.join(features_path[index_metric], image_name + cfg.post_image_name_separator + current_post_fix)
                            images_path.append(img_path)
                        else:
                            img_path = features_images_list[index_metric][index_image]
                            images_path.append(os.path.join(label_features_path[index_metric], img_path))

                    if label == cfg.noisy_folder:
                        line = '1;'
                    else:
                        line = '0;'

                    # compute line information with all images paths
                    for id_path, img_path in enumerate(images_path):
                        if id_path < len(images_path) - 1:
                            line = line + img_path + '::'
                        else:
                            line = line + img_path
                    
                    line = line + '\n'

                    if id_zone in train_zones:
                        train_file_data.append(line)
                    else:
                        test_file_data.append(line)

    train_file = open(output_train_filename, 'w')
    test_file = open(output_test_filename, 'w')

    random.shuffle(train_file_data)
    random.shuffle(test_file_data)

    for line in train_file_data:
        train_file.write(line)

    for line in test_file_data:
        test_file.write(line)

    train_file.close()
    test_file.close()
Ejemplo n.º 12
0
def generate_data(transformation, _dataset_path, _output, _human_thresholds,
                  _replace):
    """
    @brief Method which generates all .csv files from scenes
    @return nothing
    """

    # path is the default dataset path
    scenes = os.listdir(_dataset_path)
    n_scenes = len(scenes)

    # go ahead each scenes
    for id_scene, folder_scene in enumerate(scenes):

        print('Scene {0} of {1} ({2})'.format((id_scene + 1), n_scenes,
                                              folder_scene))
        scene_path = os.path.join(_dataset_path, folder_scene)
        output_scene_path = os.path.join(cfg.output_data_generated, _output,
                                         folder_scene)

        # construct each zones folder name
        zones_folder = []
        features_folder = []

        if folder_scene in _human_thresholds:

            zones_threshold = _human_thresholds[folder_scene]
            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str

                current_zone = "zone" + index_str
                zones_folder.append(current_zone)
                zone_path = os.path.join(output_scene_path, current_zone)

                # custom path for feature
                feature_path = os.path.join(zone_path,
                                            transformation.getName())

                if not os.path.exists(feature_path):
                    os.makedirs(feature_path)

                # custom path for interval of reconstruction and feature
                feature_interval_path = os.path.join(
                    zone_path, transformation.getTransformationPath())
                features_folder.append(feature_interval_path)

                if not os.path.exists(feature_interval_path):
                    os.makedirs(feature_interval_path)

                # create for each zone the labels folder
                labels = [cfg.not_noisy_folder, cfg.noisy_folder]

                for label in labels:
                    label_folder = os.path.join(feature_interval_path, label)

                    if not os.path.exists(label_folder):
                        os.makedirs(label_folder)

            # get all images of folder
            scene_images = sorted([
                os.path.join(scene_path, img) for img in os.listdir(scene_path)
                if cfg.scene_image_extension in img
            ])
            number_scene_image = len(scene_images)

            # for each images
            for id_img, img_path in enumerate(scene_images):

                current_img = Image.open(img_path)
                img_blocks = divide_in_blocks(current_img, cfg.sub_image_size)

                current_quality_index = int(get_scene_image_quality(img_path))

                for id_block, block in enumerate(img_blocks):

                    ##########################
                    # Image computation part #
                    ##########################

                    label_path = features_folder[id_block]

                    # get label folder for block
                    if current_quality_index > zones_threshold[id_block]:
                        label_path = os.path.join(label_path,
                                                  cfg.not_noisy_folder)
                    else:
                        label_path = os.path.join(label_path, cfg.noisy_folder)

                    # check if necessary to compute or not images
                    # Disable use of data augmentation for the moment
                    # Data augmentation!
                    # rotations = [0, 90, 180, 270]

                    #img_flip_labels = ['original', 'horizontal', 'vertical', 'both']
                    # img_flip_labels = ['original', 'horizontal']

                    # output_images_path = []
                    # check_path_exists = []
                    # # rotate and flip image to increase dataset size
                    # for id, flip_label in enumerate(img_flip_labels):
                    #     for rotation in rotations:
                    #         output_reconstructed_filename = img_path.split('/')[-1].replace('.png', '') + '_' + zones_folder[id_block] + cfg.post_image_name_separator
                    #         output_reconstructed_filename = output_reconstructed_filename + flip_label + '_' + str(rotation) + '.png'
                    #         output_reconstructed_path = os.path.join(label_path, output_reconstructed_filename)

                    #         if os.path.exists(output_reconstructed_path):
                    #             check_path_exists.append(True)
                    #         else:
                    #             check_path_exists.append(False)

                    #         output_images_path.append(output_reconstructed_path)

                    # compute only if not exists or necessary to replace
                    # if _replace or not np.array(check_path_exists).all():
                    # compute image
                    # pass block to grey level
                    # output_block = transformation.getTransformedImage(block)
                    # output_block = np.array(output_block, 'uint8')

                    # # current output image
                    # output_block_img = Image.fromarray(output_block)

                    #horizontal_img = output_block_img.transpose(Image.FLIP_LEFT_RIGHT)
                    #vertical_img = output_block_img.transpose(Image.FLIP_TOP_BOTTOM)
                    #both_img = output_block_img.transpose(Image.TRANSPOSE)

                    #flip_images = [output_block_img, horizontal_img, vertical_img, both_img]
                    #flip_images = [output_block_img, horizontal_img]

                    # Only current image img currenlty
                    # flip_images = [output_block_img]

                    # # rotate and flip image to increase dataset size
                    # counter_index = 0 # get current path index
                    # for id, flip in enumerate(flip_images):
                    #     for rotation in rotations:

                    #         if _replace or not check_path_exists[counter_index]:
                    #             rotated_output_img = flip.rotate(rotation)
                    #             rotated_output_img.save(output_images_path[counter_index])

                    #         counter_index +=1

                    if _replace:

                        _, filename = os.path.split(img_path)

                        # build of output image filename
                        filename = filename.replace('.png', '')
                        filename_parts = filename.split('_')

                        # get samples : `00XXX`
                        n_samples = filename_parts[-1]
                        del filename_parts[-1]

                        # `p3d_XXXXXX`
                        output_reconstructed = '_'.join(filename_parts)

                        output_reconstructed_filename = output_reconstructed + '_' + zones_folder[
                            id_block] + '_' + n_samples + '.png'
                        output_reconstructed_path = os.path.join(
                            label_path, output_reconstructed_filename)

                        output_block = transformation.getTransformedImage(
                            block)
                        output_block = np.array(output_block, 'uint8')

                        # current output image
                        output_block_img = Image.fromarray(output_block)
                        output_block_img.save(output_reconstructed_path)

                write_progress((id_img + 1) / number_scene_image)

            print('\n')

    print("{0}_{1} : end of data generation\n".format(
        transformation.getName(), transformation.getParam()))
Ejemplo n.º 13
0
def generate_data(transformation):
    """
    @brief Method which generates all .csv files from scenes
    @return nothing
    """

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    # go ahead each scenes
    for id_scene, folder_scene in enumerate(scenes):

        print(folder_scene)
        scene_path = os.path.join(path, folder_scene)

        config_file_path = os.path.join(scene_path, config_filename)

        with open(config_file_path, "r") as config_file:
            last_image_name = config_file.readline().strip()
            prefix_image_name = config_file.readline().strip()
            start_index_image = config_file.readline().strip()
            end_index_image = config_file.readline().strip()
            step_counter = int(config_file.readline().strip())

        # construct each zones folder name
        zones_folder = []
        features_folder = []
        zones_threshold = []

        # get zones list info
        for index in zones:
            index_str = str(index)
            if len(index_str) < 2:
                index_str = "0" + index_str

            current_zone = "zone"+index_str
            zones_folder.append(current_zone)
            zone_path = os.path.join(scene_path, current_zone)

            with open(os.path.join(zone_path, cfg.seuil_expe_filename)) as f:
                zones_threshold.append(int(f.readline()))

            # custom path for feature
            feature_path = os.path.join(zone_path, transformation.getName())

            if not os.path.exists(feature_path):
                os.makedirs(feature_path)

            # custom path for interval of reconstruction and feature
            feature_interval_path = os.path.join(zone_path, transformation.getTransformationPath())
            features_folder.append(feature_interval_path)

            if not os.path.exists(feature_interval_path):
                os.makedirs(feature_interval_path)

            # create for each zone the labels folder
            labels = [cfg.not_noisy_folder, cfg.noisy_folder]

            for label in labels:
                label_folder = os.path.join(feature_interval_path, label)

                if not os.path.exists(label_folder):
                    os.makedirs(label_folder)


        # get all images of folder
        scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
        number_scene_image = len(scene_images)

        # for each images
        for id_img, img_path in enumerate(scene_images):

            current_img = Image.open(img_path)
            img_blocks = divide_in_blocks(current_img, cfg.keras_img_size)

            current_quality_index = int(get_scene_image_quality(img_path))

            for id_block, block in enumerate(img_blocks):

                ##########################
                # Image computation part #
                ##########################
                
                # pass block to grey level


                output_block = transformation.getTransformedImage(block)
                output_block = np.array(output_block, 'uint8')
                
                # current output image
                output_block_img = Image.fromarray(output_block)

                label_path = features_folder[id_block]

                # get label folder for block
                if current_quality_index > zones_threshold[id_block]:
                    label_path = os.path.join(label_path, cfg.not_noisy_folder)
                else:
                    label_path = os.path.join(label_path, cfg.noisy_folder)

                # Data augmentation!
                rotations = [0, 90, 180, 270]
                img_flip_labels = ['original', 'horizontal', 'vertical', 'both']

                horizontal_img = output_block_img.transpose(Image.FLIP_LEFT_RIGHT)
                vertical_img = output_block_img.transpose(Image.FLIP_TOP_BOTTOM)
                both_img = output_block_img.transpose(Image.TRANSPOSE)

                flip_images = [output_block_img, horizontal_img, vertical_img, both_img]

                # rotate and flip image to increase dataset size
                for id, flip in enumerate(flip_images):
                    for rotation in rotations:
                        rotated_output_img = flip.rotate(rotation)

                        output_reconstructed_filename = img_path.split('/')[-1].replace('.png', '') + '_' + zones_folder[id_block] + cfg.post_image_name_separator
                        output_reconstructed_filename = output_reconstructed_filename + img_flip_labels[id] + '_' + str(rotation) + '.png'
                        output_reconstructed_path = os.path.join(label_path, output_reconstructed_filename)

                        rotated_output_img.save(output_reconstructed_path)

            print(transformation.getName() + "_" + folder_scene + " - " + "{0:.2f}".format(((id_img + 1) / number_scene_image)* 100.) + "%")
            sys.stdout.write("\033[F")

        print('\n')

    print("%s_%s : end of data generation\n" % (transformation.getName(), transformation.getParam()))
Ejemplo n.º 14
0
def main():

    p_custom = False

    parser = argparse.ArgumentParser(
        description="Script which predicts threshold using specific model")

    parser.add_argument('--interval',
                        type=str,
                        help='Interval value to keep from svd',
                        default='"0, 200"')
    parser.add_argument('--model',
                        type=str,
                        help='.joblib or .json file (sklearn or keras model)')
    parser.add_argument('--mode',
                        type=str,
                        help='Kind of normalization level wished',
                        choices=normalization_choices)
    parser.add_argument('--feature',
                        type=str,
                        help='feature data choice',
                        choices=features_choices)
    #parser.add_argument('--limit_detection', type=int, help='Specify number of same prediction to stop threshold prediction', default=2)
    parser.add_argument(
        '--custom',
        type=str,
        help='Name of custom min max file if use of renormalization of data',
        default=False)

    args = parser.parse_args()

    # keep p_interval as it is
    p_interval = args.interval
    p_model_file = args.model
    p_mode = args.mode
    p_feature = args.feature
    #p_limit      = args.limit
    p_custom = args.custom

    scenes = os.listdir(scenes_path)
    scenes = [s for s in scenes if s in maxwell_scenes]

    print(scenes)

    # go ahead each scenes
    for id_scene, folder_scene in enumerate(scenes):

        # only take in consideration maxwell scenes
        if folder_scene in maxwell_scenes:

            print(folder_scene)

            scene_path = os.path.join(scenes_path, folder_scene)

            threshold_expes = []
            threshold_expes_found = []
            block_predictions_str = []

            # get all images of folder
            scene_images = sorted([
                os.path.join(scene_path, img) for img in os.listdir(scene_path)
                if cfg.scene_image_extension in img
            ])

            start_quality_image = dt.get_scene_image_quality(scene_images[0])
            end_quality_image = dt.get_scene_image_quality(scene_images[-1])
            # using first two images find the step of quality used
            quality_step_image = dt.get_scene_image_quality(
                scene_images[1]) - start_quality_image

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str
                zone_folder = "zone" + index_str

                threshold_path_file = os.path.join(
                    os.path.join(scene_path, zone_folder),
                    threshold_expe_filename)

                with open(threshold_path_file) as f:
                    threshold = int(f.readline())
                    threshold_expes.append(threshold)

                    # Initialize default data to get detected model threshold found
                    threshold_expes_found.append(
                        end_quality_image)  # by default use max

                block_predictions_str.append(index_str + ";" + p_model_file +
                                             ";" + str(threshold) + ";" +
                                             str(start_quality_image) + ";" +
                                             str(quality_step_image))

            # for each images
            for img_path in scene_images:

                current_img = Image.open(img_path)
                current_quality_image = dt.get_scene_image_quality(img_path)

                img_blocks = segmentation.divide_in_blocks(
                    current_img, (200, 200))

                for id_block, block in enumerate(img_blocks):

                    # check only if necessary for this scene (not already detected)
                    #if not threshold_expes_detected[id_block]:

                    tmp_file_path = tmp_filename.replace(
                        '__model__',
                        p_model_file.split('/')[-1].replace('.joblib', '_'))
                    block.save(tmp_file_path)

                    python_cmd_line = "python prediction/predict_noisy_image_svd.py --image {0} --interval '{1}' --model {2} --mode {3} --feature {4}"
                    python_cmd = python_cmd_line.format(
                        tmp_file_path, p_interval, p_model_file, p_mode,
                        p_feature)

                    # specify use of custom file for min max normalization
                    if p_custom:
                        python_cmd = python_cmd + ' --custom ' + p_custom

                    ## call command ##
                    p = subprocess.Popen(python_cmd,
                                         stdout=subprocess.PIPE,
                                         shell=True)

                    (output, err) = p.communicate()

                    ## Wait for result ##
                    p_status = p.wait()

                    prediction = int(output)

                    # save here in specific file of block all the predictions done
                    block_predictions_str[id_block] = block_predictions_str[
                        id_block] + ";" + str(prediction)

                    print(
                        str(id_block) + " : " + str(current_quality_image) +
                        "/" + str(threshold_expes[id_block]) + " => " +
                        str(prediction))

                print("------------------------")
                print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)))
                print("------------------------")

            # end of scene => display of results

            # construct path using model name for saving threshold map folder
            model_threshold_path = os.path.join(
                threshold_map_folder,
                p_model_file.split('/')[-1].replace('.joblib', ''))

            # create threshold model path if necessary
            if not os.path.exists(model_threshold_path):
                os.makedirs(model_threshold_path)

            map_filename = os.path.join(model_threshold_path,
                                        simulation_curves_zones + folder_scene)
            f_map = open(map_filename, 'w')

            for line in block_predictions_str:
                f_map.write(line + '\n')
            f_map.close()

            print("Scene " + str(id_scene + 1) + "/" +
                  str(len(maxwell_scenes)) + " Done..")
            print("------------------------")

            print("Model predictions are saved into %s" % map_filename)
Ejemplo n.º 15
0
def estimate(estimator, arr):

    if estimator == 'variance':
        return np.var(arr)

    if estimator == 'l_variance':
        return np.var(transform.get_LAB_L(arr))

    if estimator == 'mean':
        return np.mean(arr)

    if estimator == 'l_mean':
        return np.mean(transform.get_LAB_L(arr))

    if estimator == 'sv_struct_all':
        return transform.get_LAB_L_SVD_s(arr)[0:50]

    if estimator == 'sv_struct':
        return np.mean(transform.get_LAB_L_SVD_s(arr)[0:50])

    if estimator == 'sv_noise_all':
        return transform.get_LAB_L_SVD_s(arr)[50:]

    if estimator == 'sv_noise':
        return np.mean(transform.get_LAB_L_SVD_s(arr)[50:])

    if estimator == 'sobel':

        lab_img = transform.get_LAB_L(arr)

        # 1. extract sobol complexity with kernel 3
        sobelx = cv2.Sobel(lab_img, cv2.CV_64F, 1, 0, ksize=5)
        sobely = cv2.Sobel(lab_img, cv2.CV_64F, 0, 1, ksize=5)

        sobel_mag = np.array(np.hypot(sobelx, sobely), 'uint8')  # magnitude

        return np.std(sobel_mag)

    if estimator == 'l_kolmogorov':

        lab_img = transform.get_LAB_L(arr)

        bytes_data = lab_img.tobytes()
        compress_data = gzip.compress(bytes_data)

        mo_size = sys.getsizeof(compress_data) / 1024.
        go_size = mo_size / 1024.

        return np.float64(go_size)

    if estimator == 'l_sv_entropy_blocks':

        # get L channel
        L_channel = transform.get_LAB_L(arr)

        # split in n block
        blocks = segmentation.divide_in_blocks(L_channel, (20, 20))

        entropy_list = []

        for block in blocks:
            reduced_sigma = compression.get_SVD_s(block)
            reduced_entropy = get_entropy(reduced_sigma)
            entropy_list.append(reduced_entropy)

        return entropy_list

    if estimator == '26_attributes':

        img_width, img_height = 200, 200

        lab_img = transform.get_LAB_L(arr)
        arr = np.array(lab_img)

        # compute all filters statistics
        def get_stats(arr, I_filter):

            e1 = np.abs(arr - I_filter)
            L = np.array(e1)
            mu0 = np.mean(L)
            A = L - mu0
            H = A * A
            E = np.sum(H) / (img_width * img_height)
            P = np.sqrt(E)

            return mu0, P
            # return np.mean(I_filter), np.std(I_filter)

        stats = []

        kernel = np.ones((3, 3), np.float32) / 9
        stats.append(get_stats(arr, cv2.filter2D(arr, -1, kernel)))

        kernel = np.ones((5, 5), np.float32) / 25
        stats.append(get_stats(arr, cv2.filter2D(arr, -1, kernel)))

        stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 0.5)))

        stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 1)))

        stats.append(get_stats(arr, cv2.GaussianBlur(arr, (3, 3), 1.5)))

        stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 0.5)))

        stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 1)))

        stats.append(get_stats(arr, cv2.GaussianBlur(arr, (5, 5), 1.5)))

        stats.append(get_stats(arr, medfilt2d(arr, [3, 3])))

        stats.append(get_stats(arr, medfilt2d(arr, [5, 5])))

        stats.append(get_stats(arr, wiener(arr, [3, 3])))

        stats.append(get_stats(arr, wiener(arr, [5, 5])))

        wave = w2d(arr, 'db1', 2)
        stats.append(get_stats(arr, np.array(wave, 'float64')))

        data = []

        for stat in stats:
            data.append(stat[0])

        for stat in stats:
            data.append(stat[1])

        data = np.array(data)

        return data

    if estimator == 'statistics_extended':

        # data = estimate('26_attributes', arr)
        data = np.empty(0)
        # add kolmogorov complexity
        bytes_data = np.array(arr).tobytes()
        compress_data = gzip.compress(bytes_data)

        mo_size = sys.getsizeof(compress_data) / 1024.
        go_size = mo_size / 1024.
        data = np.append(data, go_size)

        lab_img = transform.get_LAB_L(arr)
        arr = np.array(lab_img)

        # add of svd entropy
        svd_entropy = utils.get_entropy(compression.get_SVD_s(arr))
        data = np.append(data, svd_entropy)

        # add sobel complexity (kernel size of 3)
        sobelx = cv2.Sobel(arr, cv2.CV_64F, 1, 0, ksize=3)
        sobely = cv2.Sobel(arr, cv2.CV_64F, 0, 1, ksize=3)

        sobel_mag = np.array(np.hypot(sobelx, sobely), 'uint8')  # magnitude

        data = np.append(data, np.std(sobel_mag))

        # add sobel complexity (kernel size of 5)
        sobelx = cv2.Sobel(arr, cv2.CV_64F, 1, 0, ksize=5)
        sobely = cv2.Sobel(arr, cv2.CV_64F, 0, 1, ksize=5)

        sobel_mag = np.array(np.hypot(sobelx, sobely), 'uint8')  # magnitude

        data = np.append(data, np.std(sobel_mag))

        return data
def get_image_features(data_type, block):
    """
    Method which returns the data type expected
    """

    if data_type == 'lab':

        block_file_path = '/tmp/lab_img.png'
        block.save(block_file_path)
        data = transform.get_LAB_L_SVD_s(Image.open(block_file_path))

    if data_type == 'mscn':

        img_mscn_revisited = transform.rgb_to_mscn(block)

        # save tmp as img
        img_output = Image.fromarray(img_mscn_revisited.astype('uint8'), 'L')
        mscn_revisited_file_path = '/tmp/mscn_revisited_img.png'
        img_output.save(mscn_revisited_file_path)
        img_block = Image.open(mscn_revisited_file_path)

        # extract from temp image
        data = compression.get_SVD_s(img_block)
    """if data_type == 'mscn':

        img_gray = np.array(color.rgb2gray(np.asarray(block))*255, 'uint8')
        img_mscn = transform.calculate_mscn_coefficients(img_gray, 7)
        img_mscn_norm = transform.normalize_2D_arr(img_mscn)

        img_mscn_gray = np.array(img_mscn_norm*255, 'uint8')

        data = compression.get_SVD_s(img_mscn_gray)
    """

    if data_type == 'low_bits_6':

        low_bits_6 = transform.rgb_to_LAB_L_low_bits(block, 6)
        data = compression.get_SVD_s(low_bits_6)

    if data_type == 'low_bits_5':

        low_bits_5 = transform.rgb_to_LAB_L_low_bits(block, 5)
        data = compression.get_SVD_s(low_bits_5)

    if data_type == 'low_bits_4':

        low_bits_4 = transform.rgb_to_LAB_L_low_bits(block, 4)
        data = compression.get_SVD_s(low_bits_4)

    if data_type == 'low_bits_3':

        low_bits_3 = transform.rgb_to_LAB_L_low_bits(block, 3)
        data = compression.get_SVD_s(low_bits_3)

    if data_type == 'low_bits_2':

        low_bits_2 = transform.rgb_to_LAB_L_low_bits(block, 2)
        data = compression.get_SVD_s(low_bits_2)

    if data_type == 'low_bits_4_shifted_2':

        data = compression.get_SVD_s(transform.rgb_to_LAB_L_bits(
            block, (3, 6)))

    if data_type == 'sub_blocks_stats':

        block = np.asarray(block)
        width, height, _ = block.shape
        sub_width, sub_height = int(width / 4), int(height / 4)

        sub_blocks = segmentation.divide_in_blocks(block,
                                                   (sub_width, sub_height))

        data = []

        for sub_b in sub_blocks:

            # by default use the whole lab L canal
            l_svd_data = np.array(transform.get_LAB_L_SVD_s(sub_b))

            # get information we want from svd
            data.append(np.mean(l_svd_data))
            data.append(np.median(l_svd_data))
            data.append(np.percentile(l_svd_data, 25))
            data.append(np.percentile(l_svd_data, 75))
            data.append(np.var(l_svd_data))

            area_under_curve = utils.integral_area_trapz(l_svd_data, dx=100)
            data.append(area_under_curve)

        # convert into numpy array after computing all stats
        data = np.asarray(data)

    if data_type == 'sub_blocks_stats_reduced':

        block = np.asarray(block)
        width, height, _ = block.shape
        sub_width, sub_height = int(width / 4), int(height / 4)

        sub_blocks = segmentation.divide_in_blocks(block,
                                                   (sub_width, sub_height))

        data = []

        for sub_b in sub_blocks:

            # by default use the whole lab L canal
            l_svd_data = np.array(transform.get_LAB_L_SVD_s(sub_b))

            # get information we want from svd
            data.append(np.mean(l_svd_data))
            data.append(np.median(l_svd_data))
            data.append(np.percentile(l_svd_data, 25))
            data.append(np.percentile(l_svd_data, 75))
            data.append(np.var(l_svd_data))

        # convert into numpy array after computing all stats
        data = np.asarray(data)

    if data_type == 'sub_blocks_area':

        block = np.asarray(block)
        width, height, _ = block.shape
        sub_width, sub_height = int(width / 8), int(height / 8)

        sub_blocks = segmentation.divide_in_blocks(block,
                                                   (sub_width, sub_height))

        data = []

        for sub_b in sub_blocks:

            # by default use the whole lab L canal
            l_svd_data = np.array(transform.get_LAB_L_SVD_s(sub_b))

            area_under_curve = utils.integral_area_trapz(l_svd_data, dx=50)
            data.append(area_under_curve)

        # convert into numpy array after computing all stats
        data = np.asarray(data)

    if data_type == 'sub_blocks_area_normed':

        block = np.asarray(block)
        width, height, _ = block.shape
        sub_width, sub_height = int(width / 8), int(height / 8)

        sub_blocks = segmentation.divide_in_blocks(block,
                                                   (sub_width, sub_height))

        data = []

        for sub_b in sub_blocks:

            # by default use the whole lab L canal
            l_svd_data = np.array(transform.get_LAB_L_SVD_s(sub_b))
            l_svd_data = utils.normalize_arr(l_svd_data)

            area_under_curve = utils.integral_area_trapz(l_svd_data, dx=50)
            data.append(area_under_curve)

        # convert into numpy array after computing all stats
        data = np.asarray(data)

    if data_type == 'mscn_var_4':

        data = _get_mscn_variance(block, (100, 100))

    if data_type == 'mscn_var_16':

        data = _get_mscn_variance(block, (50, 50))

    if data_type == 'mscn_var_64':

        data = _get_mscn_variance(block, (25, 25))

    if data_type == 'mscn_var_16_max':

        data = _get_mscn_variance(block, (50, 50))
        data = np.asarray(data)
        size = int(len(data) / 4)
        indices = data.argsort()[-size:][::-1]
        data = data[indices]

    if data_type == 'mscn_var_64_max':

        data = _get_mscn_variance(block, (25, 25))
        data = np.asarray(data)
        size = int(len(data) / 4)
        indices = data.argsort()[-size:][::-1]
        data = data[indices]

    if data_type == 'ica_diff':
        current_image = transform.get_LAB_L(block)

        ica = FastICA(n_components=50)
        ica.fit(current_image)

        image_ica = ica.fit_transform(current_image)
        image_restored = ica.inverse_transform(image_ica)

        final_image = utils.normalize_2D_arr(image_restored)
        final_image = np.array(final_image * 255, 'uint8')

        sv_values = utils.normalize_arr(compression.get_SVD_s(current_image))
        ica_sv_values = utils.normalize_arr(compression.get_SVD_s(final_image))

        data = abs(np.array(sv_values) - np.array(ica_sv_values))

    if data_type == 'svd_trunc_diff':

        current_image = transform.get_LAB_L(block)

        svd = TruncatedSVD(n_components=30, n_iter=100, random_state=42)
        transformed_image = svd.fit_transform(current_image)
        restored_image = svd.inverse_transform(transformed_image)

        reduced_image = (current_image - restored_image)

        U, s, V = compression.get_SVD(reduced_image)
        data = s

    if data_type == 'ipca_diff':

        current_image = transform.get_LAB_L(block)

        transformer = IncrementalPCA(n_components=20, batch_size=25)
        transformed_image = transformer.fit_transform(current_image)
        restored_image = transformer.inverse_transform(transformed_image)

        reduced_image = (current_image - restored_image)

        U, s, V = compression.get_SVD(reduced_image)
        data = s

    if data_type == 'svd_reconstruct':

        reconstructed_interval = (90, 200)
        begin, end = reconstructed_interval

        lab_img = transform.get_LAB_L(block)
        lab_img = np.array(lab_img, 'uint8')

        U, s, V = lin_svd(lab_img, full_matrices=True)

        smat = np.zeros((end - begin, end - begin), dtype=complex)
        smat[:, :] = np.diag(s[begin:end])
        output_img = np.dot(U[:, begin:end], np.dot(smat, V[begin:end, :]))

        output_img = np.array(output_img, 'uint8')

        data = compression.get_SVD_s(output_img)

    if 'sv_std_filters' in data_type:

        # convert into lab by default to apply filters
        lab_img = transform.get_LAB_L(block)
        arr = np.array(lab_img)
        images = []

        # Apply list of filter on arr
        images.append(medfilt2d(arr, [3, 3]))
        images.append(medfilt2d(arr, [5, 5]))
        images.append(wiener(arr, [3, 3]))
        images.append(wiener(arr, [5, 5]))

        # By default computation of current block image
        s_arr = compression.get_SVD_s(arr)
        sv_vector = [s_arr]

        # for each new image apply SVD and get SV
        for img in images:
            s = compression.get_SVD_s(img)
            sv_vector.append(s)

        sv_array = np.array(sv_vector)

        _, length = sv_array.shape

        sv_std = []

        # normalize each SV vectors and compute standard deviation for each sub vectors
        for i in range(length):
            sv_array[:, i] = utils.normalize_arr(sv_array[:, i])
            sv_std.append(np.std(sv_array[:, i]))

        indices = []

        if 'lowest' in data_type:
            indices = utils.get_indices_of_lowest_values(sv_std, 200)

        if 'highest' in data_type:
            indices = utils.get_indices_of_highest_values(sv_std, 200)

        # data are arranged following std trend computed
        data = s_arr[indices]

    # with the use of wavelet
    if 'wave_sv_std_filters' in data_type:

        # convert into lab by default to apply filters
        lab_img = transform.get_LAB_L(block)
        arr = np.array(lab_img)
        images = []

        # Apply list of filter on arr
        images.append(medfilt2d(arr, [3, 3]))

        # By default computation of current block image
        s_arr = compression.get_SVD_s(arr)
        sv_vector = [s_arr]

        # for each new image apply SVD and get SV
        for img in images:
            s = compression.get_SVD_s(img)
            sv_vector.append(s)

        sv_array = np.array(sv_vector)

        _, length = sv_array.shape

        sv_std = []

        # normalize each SV vectors and compute standard deviation for each sub vectors
        for i in range(length):
            sv_array[:, i] = utils.normalize_arr(sv_array[:, i])
            sv_std.append(np.std(sv_array[:, i]))

        indices = []

        if 'lowest' in data_type:
            indices = utils.get_indices_of_lowest_values(sv_std, 200)

        if 'highest' in data_type:
            indices = utils.get_indices_of_highest_values(sv_std, 200)

        # data are arranged following std trend computed
        data = s_arr[indices]

    # with the use of wavelet
    if 'sv_std_filters_full' in data_type:

        # convert into lab by default to apply filters
        lab_img = transform.get_LAB_L(block)
        arr = np.array(lab_img)
        images = []

        # Apply list of filter on arr
        kernel = np.ones((3, 3), np.float32) / 9
        images.append(cv2.filter2D(arr, -1, kernel))

        kernel = np.ones((5, 5), np.float32) / 25
        images.append(cv2.filter2D(arr, -1, kernel))

        images.append(cv2.GaussianBlur(arr, (3, 3), 0.5))

        images.append(cv2.GaussianBlur(arr, (3, 3), 1))

        images.append(cv2.GaussianBlur(arr, (3, 3), 1.5))

        images.append(cv2.GaussianBlur(arr, (5, 5), 0.5))

        images.append(cv2.GaussianBlur(arr, (5, 5), 1))

        images.append(cv2.GaussianBlur(arr, (5, 5), 1.5))

        images.append(medfilt2d(arr, [3, 3]))

        images.append(medfilt2d(arr, [5, 5]))

        images.append(wiener(arr, [3, 3]))

        images.append(wiener(arr, [5, 5]))

        wave = w2d(arr, 'db1', 2)
        images.append(np.array(wave, 'float64'))

        # By default computation of current block image
        s_arr = compression.get_SVD_s(arr)
        sv_vector = [s_arr]

        # for each new image apply SVD and get SV
        for img in images:
            s = compression.get_SVD_s(img)
            sv_vector.append(s)

        sv_array = np.array(sv_vector)

        _, length = sv_array.shape

        sv_std = []

        # normalize each SV vectors and compute standard deviation for each sub vectors
        for i in range(length):
            sv_array[:, i] = utils.normalize_arr(sv_array[:, i])
            sv_std.append(np.std(sv_array[:, i]))

        indices = []

        if 'lowest' in data_type:
            indices = utils.get_indices_of_lowest_values(sv_std, 200)

        if 'highest' in data_type:
            indices = utils.get_indices_of_highest_values(sv_std, 200)

        # data are arranged following std trend computed
        data = s_arr[indices]

    if 'sv_entropy_std_filters' in data_type:

        lab_img = transform.get_LAB_L(block)
        arr = np.array(lab_img)

        images = []

        kernel = np.ones((3, 3), np.float32) / 9
        images.append(cv2.filter2D(arr, -1, kernel))

        kernel = np.ones((5, 5), np.float32) / 25
        images.append(cv2.filter2D(arr, -1, kernel))

        images.append(cv2.GaussianBlur(arr, (3, 3), 0.5))

        images.append(cv2.GaussianBlur(arr, (3, 3), 1))

        images.append(cv2.GaussianBlur(arr, (3, 3), 1.5))

        images.append(cv2.GaussianBlur(arr, (5, 5), 0.5))

        images.append(cv2.GaussianBlur(arr, (5, 5), 1))

        images.append(cv2.GaussianBlur(arr, (5, 5), 1.5))

        images.append(medfilt2d(arr, [3, 3]))

        images.append(medfilt2d(arr, [5, 5]))

        images.append(wiener(arr, [3, 3]))

        images.append(wiener(arr, [5, 5]))

        wave = w2d(arr, 'db1', 2)
        images.append(np.array(wave, 'float64'))

        sv_vector = []
        sv_entropy_list = []

        # for each new image apply SVD and get SV
        for img in images:
            s = compression.get_SVD_s(img)
            sv_vector.append(s)

            sv_entropy = [
                utils.get_entropy_contribution_of_i(s, id_sv)
                for id_sv, sv in enumerate(s)
            ]
            sv_entropy_list.append(sv_entropy)

        sv_std = []

        sv_array = np.array(sv_vector)
        _, length = sv_array.shape

        # normalize each SV vectors and compute standard deviation for each sub vectors
        for i in range(length):
            sv_array[:, i] = utils.normalize_arr(sv_array[:, i])
            sv_std.append(np.std(sv_array[:, i]))

        indices = []

        if 'lowest' in data_type:
            indices = utils.get_indices_of_lowest_values(sv_std, 200)

        if 'highest' in data_type:
            indices = utils.get_indices_of_highest_values(sv_std, 200)

        # data are arranged following std trend computed
        s_arr = compression.get_SVD_s(arr)
        data = s_arr[indices]

    if 'convolutional_kernels' in data_type:

        sub_zones = segmentation.divide_in_blocks(block, (20, 20))

        data = []

        diff_std_list_3 = []
        diff_std_list_5 = []
        diff_mean_list_3 = []
        diff_mean_list_5 = []

        plane_std_list_3 = []
        plane_std_list_5 = []
        plane_mean_list_3 = []
        plane_mean_list_5 = []

        plane_max_std_list_3 = []
        plane_max_std_list_5 = []
        plane_max_mean_list_3 = []
        plane_max_mean_list_5 = []

        for sub_zone in sub_zones:
            l_img = transform.get_LAB_L(sub_zone)
            normed_l_img = utils.normalize_2D_arr(l_img)

            # bilateral with window of size (3, 3)
            normed_diff = convolution.convolution2D(normed_l_img,
                                                    kernels.min_bilateral_diff,
                                                    (3, 3))
            std_diff = np.std(normed_diff)
            mean_diff = np.mean(normed_diff)

            diff_std_list_3.append(std_diff)
            diff_mean_list_3.append(mean_diff)

            # bilateral with window of size (5, 5)
            normed_diff = convolution.convolution2D(normed_l_img,
                                                    kernels.min_bilateral_diff,
                                                    (5, 5))
            std_diff = np.std(normed_diff)
            mean_diff = np.mean(normed_diff)

            diff_std_list_5.append(std_diff)
            diff_mean_list_5.append(mean_diff)

            # plane mean with window of size (3, 3)
            normed_plane_mean = convolution.convolution2D(
                normed_l_img, kernels.plane_mean, (3, 3))
            std_plane_mean = np.std(normed_plane_mean)
            mean_plane_mean = np.mean(normed_plane_mean)

            plane_std_list_3.append(std_plane_mean)
            plane_mean_list_3.append(mean_plane_mean)

            # plane mean with window of size (5, 5)
            normed_plane_mean = convolution.convolution2D(
                normed_l_img, kernels.plane_mean, (5, 5))
            std_plane_mean = np.std(normed_plane_mean)
            mean_plane_mean = np.mean(normed_plane_mean)

            plane_std_list_5.append(std_plane_mean)
            plane_mean_list_5.append(mean_plane_mean)

            # plane max error with window of size (3, 3)
            normed_plane_max = convolution.convolution2D(
                normed_l_img, kernels.plane_max_error, (3, 3))
            std_plane_max = np.std(normed_plane_max)
            mean_plane_max = np.mean(normed_plane_max)

            plane_max_std_list_3.append(std_plane_max)
            plane_max_mean_list_3.append(mean_plane_max)

            # plane max error with window of size (5, 5)
            normed_plane_max = convolution.convolution2D(
                normed_l_img, kernels.plane_max_error, (5, 5))
            std_plane_max = np.std(normed_plane_max)
            mean_plane_max = np.mean(normed_plane_max)

            plane_max_std_list_5.append(std_plane_max)
            plane_max_mean_list_5.append(mean_plane_max)

        diff_std_list_3 = np.array(diff_std_list_3)
        diff_std_list_5 = np.array(diff_std_list_5)

        diff_mean_list_3 = np.array(diff_mean_list_3)
        diff_mean_list_5 = np.array(diff_mean_list_5)

        plane_std_list_3 = np.array(plane_std_list_3)
        plane_std_list_5 = np.array(plane_std_list_5)

        plane_mean_list_3 = np.array(plane_mean_list_3)
        plane_mean_list_5 = np.array(plane_mean_list_5)

        plane_max_std_list_3 = np.array(plane_max_std_list_3)
        plane_max_std_list_5 = np.array(plane_max_std_list_5)

        plane_max_mean_list_3 = np.array(plane_max_mean_list_3)
        plane_max_mean_list_5 = np.array(plane_max_mean_list_5)

        if 'std_max_blocks' in data_type:

            data.append(np.std(diff_std_list_3[0:int(len(sub_zones) / 5)]))
            data.append(np.std(diff_mean_list_3[0:int(len(sub_zones) / 5)]))
            data.append(np.std(diff_std_list_5[0:int(len(sub_zones) / 5)]))
            data.append(np.std(diff_mean_list_5[0:int(len(sub_zones) / 5)]))

            data.append(np.std(plane_std_list_3[0:int(len(sub_zones) / 5)]))
            data.append(np.std(plane_mean_list_3[0:int(len(sub_zones) / 5)]))
            data.append(np.std(plane_std_list_5[0:int(len(sub_zones) / 5)]))
            data.append(np.std(plane_mean_list_5[0:int(len(sub_zones) / 5)]))

            data.append(np.std(plane_max_std_list_3[0:int(len(sub_zones) /
                                                          5)]))
            data.append(
                np.std(plane_max_mean_list_3[0:int(len(sub_zones) / 5)]))
            data.append(np.std(plane_max_std_list_5[0:int(len(sub_zones) /
                                                          5)]))
            data.append(
                np.std(plane_max_mean_list_5[0:int(len(sub_zones) / 5)]))

        if 'mean_max_blocks' in data_type:

            data.append(np.mean(diff_std_list_3[0:int(len(sub_zones) / 5)]))
            data.append(np.mean(diff_mean_list_3[0:int(len(sub_zones) / 5)]))
            data.append(np.mean(diff_std_list_5[0:int(len(sub_zones) / 5)]))
            data.append(np.mean(diff_mean_list_5[0:int(len(sub_zones) / 5)]))

            data.append(np.mean(plane_std_list_3[0:int(len(sub_zones) / 5)]))
            data.append(np.mean(plane_mean_list_3[0:int(len(sub_zones) / 5)]))
            data.append(np.mean(plane_std_list_5[0:int(len(sub_zones) / 5)]))
            data.append(np.mean(plane_mean_list_5[0:int(len(sub_zones) / 5)]))

            data.append(
                np.mean(plane_max_std_list_3[0:int(len(sub_zones) / 5)]))
            data.append(
                np.mean(plane_max_mean_list_3[0:int(len(sub_zones) / 5)]))
            data.append(
                np.mean(plane_max_std_list_5[0:int(len(sub_zones) / 5)]))
            data.append(
                np.mean(plane_max_mean_list_5[0:int(len(sub_zones) / 5)]))

        if 'std_normed' in data_type:

            data.append(np.std(diff_std_list_3))
            data.append(np.std(diff_mean_list_3))
            data.append(np.std(diff_std_list_5))
            data.append(np.std(diff_mean_list_5))

            data.append(np.std(plane_std_list_3))
            data.append(np.std(plane_mean_list_3))
            data.append(np.std(plane_std_list_5))
            data.append(np.std(plane_mean_list_5))

            data.append(np.std(plane_max_std_list_3))
            data.append(np.std(plane_max_mean_list_3))
            data.append(np.std(plane_max_std_list_5))
            data.append(np.std(plane_max_mean_list_5))

        if 'mean_normed' in data_type:

            data.append(np.mean(diff_std_list_3))
            data.append(np.mean(diff_mean_list_3))
            data.append(np.mean(diff_std_list_5))
            data.append(np.mean(diff_mean_list_5))

            data.append(np.mean(plane_std_list_3))
            data.append(np.mean(plane_mean_list_3))
            data.append(np.mean(plane_std_list_5))
            data.append(np.mean(plane_mean_list_5))

            data.append(np.mean(plane_max_std_list_3))
            data.append(np.mean(plane_max_mean_list_3))
            data.append(np.mean(plane_max_std_list_5))
            data.append(np.mean(plane_max_mean_list_5))

        data = np.array(data)

    if data_type == 'convolutional_kernel_stats_svd':

        l_img = transform.get_LAB_L(block)
        normed_l_img = utils.normalize_2D_arr(l_img)

        # bilateral with window of size (5, 5)
        normed_diff = convolution.convolution2D(normed_l_img,
                                                kernels.min_bilateral_diff,
                                                (5, 5))

        # getting sigma vector from SVD compression
        s = compression.get_SVD_s(normed_diff)

        data = s

    if data_type == 'svd_entropy':
        l_img = transform.get_LAB_L(block)

        blocks = segmentation.divide_in_blocks(l_img, (20, 20))

        values = []
        for b in blocks:
            sv = compression.get_SVD_s(b)
            values.append(utils.get_entropy(sv))
        data = np.array(values)

    if data_type == 'svd_entropy_20':
        l_img = transform.get_LAB_L(block)

        blocks = segmentation.divide_in_blocks(l_img, (20, 20))

        values = []
        for b in blocks:
            sv = compression.get_SVD_s(b)
            values.append(utils.get_entropy(sv))
        data = np.array(values)

    if data_type == 'svd_entropy_noise_20':
        l_img = transform.get_LAB_L(block)

        blocks = segmentation.divide_in_blocks(l_img, (20, 20))

        values = []
        for b in blocks:
            sv = compression.get_SVD_s(b)
            sv_size = len(sv)
            values.append(utils.get_entropy(sv[int(sv_size / 4):]))
        data = np.array(values)

    return data
Ejemplo n.º 17
0
def main():

    parser = argparse.ArgumentParser(
        description=
        "Check complexity of each zone of scene using estimator during rendering"
    )

    parser.add_argument(
        '--folder',
        type=str,
        help='folder where scenes with png scene file are stored')
    parser.add_argument('--estimators',
                        type=str,
                        help='list of estimators',
                        default='l_mean,l_variance')
    parser.add_argument('--output',
                        type=str,
                        help='output data filename',
                        required=True)

    args = parser.parse_args()

    p_folder = args.folder
    p_estimators = [i.strip() for i in args.estimators.split(',')]
    p_output = args.output
    print(p_estimators)

    folders = [f for f in os.listdir(p_folder) if 'min_max' not in f]

    n_zones = 16

    if not os.path.exists(data_output):
        os.makedirs(data_output)

    datafile = open(os.path.join(data_output, p_output), 'w')

    for i, scene in enumerate(sorted(folders)):

        zones = []
        zones_indices = np.arange(n_zones)

        for _ in zones_indices:
            zones.append([])

        scene_folder = os.path.join(p_folder, scene)

        # get all images and extract estimator for each blocks
        images = sorted([i for i in os.listdir(scene_folder) if '.png' in i])

        # get reference image
        img_path = os.path.join(scene_folder, images[0])

        img_arr = np.array(Image.open(img_path))

        blocks = segmentation.divide_in_blocks(img_arr, (200, 200))

        for index, b in enumerate(blocks):

            # extract data and write into file
            x = []

            for estimator in p_estimators:
                estimated = estimate(estimator, b)

                if not isinstance(estimated, np.float64):
                    for v in estimated:
                        x.append(v)
                else:
                    x.append(estimated)

            line = scene + ';' + img_path + ';' + str(index) + ';'
            for v in x:
                line += str(v) + ';'
            line += '\n'

            datafile.write(line)

            write_progress((i * n_zones + index + 1) /
                           (float(len(folders)) * float(n_zones)))

    datafile.close()
Ejemplo n.º 18
0
def display_svd_values(p_scene, p_interval, p_indices, p_zone, p_feature,
                       p_mode, p_step, p_norm, p_ylim):
    """
    @brief Method which gives information about svd curves from zone of picture
    @param p_scene, scene expected to show svd values
    @param p_interval, interval [begin, end] of svd data to display
    @param p_interval, interval [begin, end] of samples or minutes from render generation engine
    @param p_zone, zone's identifier of picture
    @param p_feature, feature computed to show
    @param p_mode, normalization's mode
    @param p_step, step of images indices
    @param p_norm, normalization or not of selected svd data
    @param p_ylim, ylim choice to better display of data
    @return nothing
    """

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    begin_data, end_data = p_interval
    begin_index, end_index = p_indices

    data_min_max_filename = os.path.join(path, p_feature + min_max_filename)

    # go ahead each scenes
    for folder_scene in scenes:

        if p_scene == folder_scene:
            scene_path = os.path.join(path, folder_scene)
            # construct each zones folder name
            zones_folder = []

            # get zones list info
            for index in zones:
                index_str = str(index)
                if len(index_str) < 2:
                    index_str = "0" + index_str

                current_zone = "zone" + index_str
                zones_folder.append(current_zone)

            zones_images_data = []
            images_path = []

            zone_folder = zones_folder[p_zone]

            zone_path = os.path.join(scene_path, zone_folder)

            # get threshold information
            path_seuil = os.path.join(zone_path, seuil_expe_filename)

            # open treshold path and get this information
            with open(path_seuil, "r") as seuil_file:
                seuil_learned = int(seuil_file.readline().strip())

            threshold_image_found = False

            # get all images of folder
            scene_images = sorted([
                os.path.join(scene_path, img) for img in os.listdir(scene_path)
                if cfg.scene_image_extension in img
            ])

            # for each images
            for img_path in scene_images:

                current_quality_image = dt.get_scene_image_quality(img_path)

                if current_quality_image % p_step == 0:
                    if current_quality_image >= begin_index and current_quality_image <= end_index:
                        images_path.append(img_path)

                    if seuil_learned < current_quality_image and not threshold_image_found:

                        threshold_image_found = True
                        threshold_image_zone = dt.get_scene_image_postfix(
                            img_path)

                        if img_path not in images_path:
                            images_path.append(img_path)

            for img_path in images_path:

                current_img = Image.open(img_path)
                img_blocks = segmentation.divide_in_blocks(
                    current_img, (200, 200))

                # getting expected block id
                block = img_blocks[p_zone]

                # get data from mode
                # Here you can add the way you compute data
                data = get_image_features(p_feature, block)

                # TODO : improve part of this code to get correct min / max values
                if p_norm:
                    data = data[begin_data:end_data]

                ##################
                # Data mode part #
                ##################

                if p_mode == 'svdne':

                    # getting max and min information from min_max_filename
                    if not p_norm:
                        with open(data_min_max_filename, 'r') as f:
                            min_val = float(f.readline())
                            max_val = float(f.readline())
                    else:
                        min_val = min_value_interval
                        max_val = max_value_interval

                    data = utils.normalize_arr_with_range(
                        data, min_val, max_val)

                if p_mode == 'svdn':
                    data = utils.normalize_arr(data)

                if not p_norm:
                    zones_images_data.append(data[begin_data:end_data])
                else:
                    zones_images_data.append(data)

            fig, ax = plt.subplots(figsize=(30, 22))
            ax.set_facecolor('#FFFFFF')

            # plt.title(p_scene + ' scene (zone  ' + str(p_zone) + ') interval information SVD['+ str(begin_data) +', '+ str(end_data) +'], from scenes indices [' + str(begin_index) + ', '+ str(end_index) + '], ' + p_feature + ' feature, ' + p_mode + ', with step of ' + str(p_step) + ', svd norm ' + str(p_norm), fontsize=24)
            ax.set_ylabel('Component values', fontsize=28)
            ax.set_xlabel('Vector features', fontsize=28)

            ax.tick_params(labelsize=22)

            for id, data in enumerate(zones_images_data):

                p_label = p_scene + "_" + dt.get_scene_image_postfix(
                    images_path[id])

                if int(dt.get_scene_image_postfix(
                        images_path[id])) == int(threshold_image_zone):
                    ax.plot(data,
                            label=p_label + ' (zone ' + str(p_zone) +
                            ' threshold)',
                            lw=4,
                            color='red')
                else:
                    ax.plot(data, label=p_label)

            plt.legend(bbox_to_anchor=(0.60, 0.98),
                       loc=2,
                       borderaxespad=0.2,
                       fontsize=24)

            start_ylim, end_ylim = p_ylim
            plt.ylim(start_ylim, end_ylim)

            plot_name = p_scene + '_zone_' + str(
                p_zone) + '_' + p_feature + '_' + str(
                    p_step) + '_' + p_mode + '_' + str(p_norm) + '.png'
            plt.savefig(plot_name, facecolor=ax.get_facecolor())
def main():

    parser = argparse.ArgumentParser(
        description="Compute sobel complexity on scene images")

    parser.add_argument(
        '--output',
        type=str,
        help='save complexity for each zone of each scene into file')
    parser.add_argument('--ksize',
                        type=int,
                        help='sobel kernel size',
                        default=3)
    parser.add_argument('--interval',
                        type=str,
                        help='svd interval to use',
                        default="0,200")
    parser.add_argument(
        '--imnorm',
        type=int,
        help="specify if image is normalized before computing something",
        default=0,
        choices=[0, 1])

    args = parser.parse_args()

    p_output = args.output
    p_ksize = args.ksize
    p_interval = tuple(map(int, args.interval.split(',')))
    p_imnorm = args.imnorm

    # create output path if not exists
    p_output_path = os.path.join(cfg.output_data_folder, cfg.data_generated,
                                 p_output)
    if not os.path.exists(cfg.output_data_folder):
        os.makedirs(os.path.join(cfg.output_data_folder, cfg.data_generated))

    zones_list = []

    # construct zones folder
    for index in zones_indices:

        index_str = str(index)

        while len(index_str) < 2:
            index_str = "0" + index_str

        zones_list.append(cfg.zone_folder + index_str)

    thresholds = {}
    images_path = {}
    number_of_images = 0

    # create dictionnary of threshold and get all images path
    for scene in scenes_list:

        scene_path = os.path.join(dataset_folder, scene)

        threshold_list = []

        for zone in zones_list:
            zone_path = os.path.join(scene_path, zone)

            with open(os.path.join(zone_path, cfg.seuil_expe_filename),
                      'r') as f:
                threshold_list.append(int(f.readline()))

        thresholds[scene] = threshold_list
        images_path[scene] = sorted([
            os.path.join(scene_path, img) for img in os.listdir(scene_path)
            if cfg.scene_image_extension in img
        ])
        number_of_images = number_of_images + len(images_path[scene])

    with open(p_output_path, 'w') as f:
        print("Erase", p_output_path, "previous file if exists")

    image_counter = 0
    # compute complexity for each zones of each scene images
    for scene in scenes_list:

        image_indices = [
            dt.get_scene_image_quality(img_path)
            for img_path in images_path[scene]
        ]

        blocks_complexity = []

        # append empty list
        for zone in zones_list:
            blocks_complexity.append([])

        for img_path in images_path[scene]:

            blocks = segmentation.divide_in_blocks(Image.open(img_path),
                                                   (200, 200))
            complexity_list = get_zone_sobel_svd_entropy(
                blocks, p_interval, p_ksize, p_imnorm)

            for index, complexity in enumerate(complexity_list):
                blocks_complexity[index].append(complexity)

            # write progress bar
            write_progress((image_counter + 1) / number_of_images)

            image_counter = image_counter + 1

        # write data into files
        with open(p_output_path, 'a') as f:
            for index, zone in enumerate(zones_list):
                f.write(scene + ';')
                f.write(str(index) + ';')
                f.write(zone + ';')

                f.write(str(thresholds[scene][index]) + ';')

                for index_img, img_quality in enumerate(image_indices):
                    f.write(str(img_quality))

                    if index_img + 1 < len(image_indices):
                        f.write(',')

                f.write(';')

                for index_v, v in enumerate(blocks_complexity[index]):
                    f.write(str(v))

                    if index_v + 1 < len(blocks_complexity[index]):
                        f.write(',')

                f.write(';\n')
def main():

    parser = argparse.ArgumentParser(
        description=
        "Check complexity of each zone of scene using estimator during rendering"
    )

    parser.add_argument(
        '--folder',
        type=str,
        help='folder where scenes with png scene file are stored')
    parser.add_argument('--estimator',
                        type=str,
                        help='metric to use to compare',
                        choices=estimators_list)
    parser.add_argument('--ylim',
                        type=str,
                        help='0,100 : ylim expected',
                        default='')
    parser.add_argument('--output',
                        type=str,
                        help='output folder name',
                        required=True)

    args = parser.parse_args()

    p_folder = args.folder
    p_estimator = args.estimator
    p_y_lim = args.ylim
    p_output = args.output

    folders = [f for f in os.listdir(p_folder) if 'min_max' not in f]

    for scene in sorted(folders):

        zones = []
        zones_indices = np.arange(16)

        for _ in zones_indices:
            zones.append([])

        print('Start extracting', p_estimator, 'for', scene)
        scene_folder = os.path.join(p_folder, scene)

        # get all images and extract estimator for each blocks
        images = sorted([i for i in os.listdir(scene_folder) if '.png' in i])
        n_images = float(len(images))

        for i, img_name in enumerate(images):
            img_path = os.path.join(scene_folder, img_name)

            img_arr = np.array(Image.open(img_path))

            blocks = segmentation.divide_in_blocks(img_arr, (200, 200))

            for index, b in enumerate(blocks):
                zones[index].append(estimate(p_estimator, b))

            write_progress((i + 1) / n_images)

        # display each block stats
        plt.figure(figsize=(25, 20))
        plt.rc('xtick', labelsize=22)  # fontsize of the tick labels
        plt.rc('ytick', labelsize=22)  # fontsize of the tick labels

        for i, zone in enumerate(zones):
            plt.plot(zone, linewidth=3, label='zone ' + str(i))

        plt.title('Estimator evolution on zones for ' + scene, fontsize=30)
        plt.legend(fontsize=20)
        plt.xlabel('Number of images', fontsize=28)
        plt.ylabel(p_estimator + ' evolution during rendering', fontsize=28)

        if len(p_y_lim) > 0:
            p_x, p_y = list(map(int, p_y_lim.split(',')))
            plt.ylim((p_x, p_y))

        output_folder = os.path.join(figures_output, p_output)
        if not os.path.exists(output_folder):
            os.makedirs(output_folder)

        plt.savefig(
            os.path.join(output_folder, p_estimator + '_' + scene +
                         '.png'))  #, transparent=True)
        print()
Ejemplo n.º 21
0
def main():

    p_custom = False

    parser = argparse.ArgumentParser(
        description="Script which predicts threshold using specific model")

    parser.add_argument('--interval',
                        type=str,
                        help='Interval value to keep from svd',
                        default='"0, 200"')
    parser.add_argument('--model',
                        type=str,
                        help='.joblib or .json file (sklearn or keras model)')
    parser.add_argument('--mode',
                        type=str,
                        help='Kind of normalization level wished',
                        choices=normalization_choices)
    parser.add_argument('--feature',
                        type=str,
                        help='Feature data choice',
                        choices=features_choices)
    parser.add_argument(
        '--limit_detection',
        type=int,
        help='Specify number of same prediction to stop threshold prediction',
        default=2)
    parser.add_argument(
        '--custom',
        type=str,
        help='Name of custom min max file if use of renormalization of data',
        default=False)

    args = parser.parse_args()

    p_interval = list(map(int, args.interval.split(',')))
    p_model_file = args.model
    p_mode = args.mode
    p_feature = args.feature
    p_limit = args.limit
    p_custom = args.custom

    scenes = os.listdir(scenes_path)
    scenes = [s for s in scenes if not min_max_filename in s]

    # go ahead each scenes
    for id_scene, folder_scene in enumerate(scenes):

        print(folder_scene)

        scene_path = os.path.join(scenes_path, folder_scene)

        threshold_expes = []
        threshold_expes_detected = []
        threshold_expes_counter = []
        threshold_expes_found = []

        # get all images of folder
        scene_images = sorted([
            os.path.join(scene_path, img) for img in os.listdir(scene_path)
            if cfg.scene_image_extension in img
        ])

        start_quality_image = dt.get_scene_image_quality(scene_images[0])
        end_quality_image = dt.get_scene_image_quality(scene_images[-1])

        # get zones list info
        for index in zones:
            index_str = str(index)
            if len(index_str) < 2:
                index_str = "0" + index_str
            zone_folder = "zone" + index_str

            threshold_path_file = os.path.join(
                os.path.join(scene_path, zone_folder), threshold_expe_filename)

            with open(threshold_path_file) as f:
                threshold = int(f.readline())
                threshold_expes.append(threshold)

                # Initialize default data to get detected model threshold found
                threshold_expes_detected.append(False)
                threshold_expes_counter.append(0)
                threshold_expes_found.append(
                    end_quality_image)  # by default use max

        check_all_done = False

        # for each images
        for img_path in scene_images:

            current_img = Image.open(img_path)
            current_quality_image = dt.get_scene_image_quality(img_path)
            current_image_potfix = dt.get_scene_image_postfix(img_path)

            img_blocks = segmentation.divide_in_blocks(current_img, (200, 200))
            current_img = Image.open(img_path)
            img_blocks = segmentation.divide_in_blocks(current_img, (200, 200))

            check_all_done = all(d == True for d in threshold_expes_detected)

            if check_all_done:
                break

            for id_block, block in enumerate(img_blocks):

                # check only if necessary for this scene (not already detected)
                if not threshold_expes_detected[id_block]:

                    tmp_file_path = tmp_filename.replace(
                        '__model__',
                        p_model_file.split('/')[-1].replace('.joblib', '_'))
                    block.save(tmp_file_path)

                    python_cmd = "python prediction/predict_noisy_image_svd.py --image " + tmp_file_path + \
                                    " --interval '" + p_interval + \
                                    "' --model " + p_model_file  + \
                                    " --mode " + p_mode + \
                                    " --feature " + p_feature

                    # specify use of custom file for min max normalization
                    if p_custom:
                        python_cmd = python_cmd + ' --custom ' + p_custom

                    ## call command ##
                    p = subprocess.Popen(python_cmd,
                                         stdout=subprocess.PIPE,
                                         shell=True)

                    (output, err) = p.communicate()

                    ## Wait for result ##
                    p_status = p.wait()

                    prediction = int(output)

                    if prediction == 0:
                        threshold_expes_counter[
                            id_block] = threshold_expes_counter[id_block] + 1
                    else:
                        threshold_expes_counter[id_block] = 0

                    if threshold_expes_counter[id_block] == p_limit:
                        threshold_expes_detected[id_block] = True
                        threshold_expes_found[id_block] = current_quality_image

                    print(
                        str(id_block) + " : " + current_image_potfix + "/" +
                        str(threshold_expes[id_block]) + " => " +
                        str(prediction))

            print("------------------------")
            print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)))
            print("------------------------")

        # end of scene => display of results

        # construct path using model name for saving threshold map folder
        model_treshold_path = os.path.join(
            threshold_map_folder,
            p_model_file.split('/')[-1].replace('.joblib', ''))

        # create threshold model path if necessary
        if not os.path.exists(model_treshold_path):
            os.makedirs(model_treshold_path)

        abs_dist = []

        map_filename = os.path.join(model_treshold_path,
                                    threshold_map_file_prefix + folder_scene)
        f_map = open(map_filename, 'w')

        line_information = ""

        # default header
        f_map.write('|  |    |    |  |\n')
        f_map.write('---|----|----|---\n')
        for id, threshold in enumerate(threshold_expes_found):

            line_information += str(threshold) + " / " + str(
                threshold_expes[id]) + " | "
            abs_dist.append(abs(threshold - threshold_expes[id]))

            if (id + 1) % 4 == 0:
                f_map.write(line_information + '\n')
                line_information = ""

        f_map.write(line_information + '\n')

        min_abs_dist = min(abs_dist)
        max_abs_dist = max(abs_dist)
        avg_abs_dist = sum(abs_dist) / len(abs_dist)

        f_map.write('\nScene information : ')
        f_map.write('\n- BEGIN : ' + str(start_quality_image))
        f_map.write('\n- END : ' + str(end_quality_image))

        f_map.write('\n\nDistances information : ')
        f_map.write('\n- MIN : ' + str(min_abs_dist))
        f_map.write('\n- MAX : ' + str(max_abs_dist))
        f_map.write('\n- AVG : ' + str(avg_abs_dist))

        f_map.write('\n\nOther information : ')
        f_map.write('\n- Detection limit : ' + str(p_limit))

        # by default print last line
        f_map.close()

        print("Scene " + str(id_scene + 1) + "/" + str(len(scenes)) +
              " Done..")
        print("------------------------")
Ejemplo n.º 22
0
def generate_data_feature(path, output, human_thresholds, data_type, mode):
    """
    @brief Method which generates all .csv files from scenes
    @param data_type,  feature choice
    @param mode, normalization choice
    @return nothing
    """

    scenes = os.listdir(path)
    # remove min max file from scenes folder
    scenes = [s for s in scenes if min_max_filename not in s]

    # keep in memory min and max data found from data_type
    min_val_found = sys.maxsize
    max_val_found = 0

    output_path = os.path.join(cfg.output_data_generated, output)

    if not os.path.exists(output_path):
        os.makedirs(output_path)

    data_min_max_filename = os.path.join(output_path, data_type + min_max_filename)

    # go ahead each scenes
    for folder_scene in human_thresholds:

        print(folder_scene)
        scene_path = os.path.join(path, folder_scene)
        output_scene_path = os.path.join(output_path, folder_scene)

        if not os.path.exists(output_scene_path):
            os.makedirs(output_scene_path)

        # getting output filename
        output_svd_filename = data_type + "_" + mode + generic_output_file_svd

        # construct each zones folder name
        zones_folder = []
        svd_output_files = []

        # get zones list info
        for index in zones:
            index_str = str(index)
            if len(index_str) < 2:
                index_str = "0" + index_str

            current_zone = "zone"+index_str
            zones_folder.append(current_zone)

            zone_path = os.path.join(scene_path, current_zone)
            output_zone_path = os.path.join(output_scene_path, current_zone)

            if not os.path.exists(output_zone_path):
                os.makedirs(output_zone_path)

            svd_file_path = os.path.join(output_zone_path, output_svd_filename)

            # add writer into list
            svd_output_files.append(open(svd_file_path, 'w'))

        # get all images of folder
        scene_images = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if cfg.scene_image_extension in img])
        number_scene_image = len(scene_images)
            
        for id_img, img_path in enumerate(scene_images):
            
            current_image_postfix = dt.get_scene_image_postfix(img_path)

            current_img = Image.open(img_path)
            img_blocks = segmentation.divide_in_blocks(current_img, (200, 200))

            for id_block, block in enumerate(img_blocks):

                ###########################
                # feature computation part #
                ###########################

                data = get_image_features(data_type, block)

                ##################
                # Data mode part #
                ##################

                # modify data depending mode
                if mode == 'svdne':

                    # getting max and min information from min_max_filename
                    with open(data_min_max_filename, 'r') as f:
                        min_val = float(f.readline())
                        max_val = float(f.readline())

                    data = utils.normalize_arr_with_range(data, min_val, max_val)

                if mode == 'svdn':
                    data = utils.normalize_arr(data)

                # save min and max found from dataset in order to normalize data using whole data known
                if mode == 'svd':

                    current_min = data.min()
                    current_max = data.max()

                    if current_min < min_val_found:
                        min_val_found = current_min

                    if current_max > max_val_found:
                        max_val_found = current_max

                # now write data into current writer
                current_file = svd_output_files[id_block]

                # add of index
                current_file.write(current_image_postfix + ';')

                for val in data:
                    current_file.write(str(val) + ";")

                current_file.write('\n')

            print(data_type + "_" + mode + "_" + folder_scene + " - " + "{0:.2f}".format((id_img + 1) / number_scene_image * 100.) + "%")
            sys.stdout.write("\033[F")

        for f in svd_output_files:
            f.close()

        print('\n')

    # save current information about min file found
    if mode == 'svd':
        with open(data_min_max_filename, 'w') as f:
            f.write(str(min_val_found) + '\n')
            f.write(str(max_val_found) + '\n')

    print("%s_%s : end of data generation\n" % (data_type, mode))