Пример #1
0
def create_sprite(config, df):

    images_array = [
        Tools.read_np_picture(os.path.join(
            config.get('directory')['collections'], "%s.jpg" % picture_id),
                              target_size=(TARGET_SIZE_HEIGHT,
                                           TARGET_SIZE_WIDTH))
        for picture_id in df['pictures_id']
    ]
    sprite = np.zeros((TARGET_SIZE_HEIGHT * SPRITE_NB_LIGNE,
                       TARGET_SIZE_WIDTH * SPRITE_NB_COLONNE, 3))
    index = 0
    for i in range(SPRITE_NB_LIGNE):
        for j in range(SPRITE_NB_COLONNE):
            sprite[(i * TARGET_SIZE_HEIGHT):(i + 1) * TARGET_SIZE_HEIGHT,
                   (j * TARGET_SIZE_WIDTH):(j + 1) *
                   TARGET_SIZE_WIDTH, :] = images_array[index]
            index += 1
            if index >= len(images_array):
                break
        if index >= len(images_array):
            break

    img = Tools.display_one_picture(sprite)
    return img
Пример #2
0
def plot_mosaics(config, clustering_type, clustering, output_image_width,
                 output_image_height, mosaic_nrow, mosaic_ncol_max):
    """
    Mosaic of each cluster
    """
    clusters_id = np.unique(clustering.final_labels)
    clustering_res = clustering.get_results()

    for cluster_id in clusters_id:
        cluster_image_filenames = [
            os.path.join(
                config.get('directory')['collections'], "%s.jpg" % one_res[0])
            for one_res in clustering_res if one_res[1] == cluster_id
        ]

        images_array = [
            Tools.read_np_picture(img_filename,
                                  target_size=(output_image_height,
                                               output_image_width))
            for img_filename in cluster_image_filenames
        ]

        img = Tools.display_mosaic(images_array,
                                   nrow=mosaic_nrow,
                                   ncol_max=mosaic_ncol_max)
        img.save(
            os.path.join(clustering.save_directory,
                         "cluster_%s.png" % str(cluster_id).zfill(2)), "PNG")

    return clusters_id
Пример #3
0
def create_poster_picture(config, poster_config, poster, pictures_df):

    pictures_id_positions = []
    for part in poster_config['DECOUPAGE']:

        picture_id = None
        directory = config.get('directory')['collections']
        if 'directory' in part.keys() and part['directory']:
            directory = os.path.join(
                config.get('directory')['project_dir'], part['directory'])

        if part['cluster']:
            selection = pictures_df.loc[pictures_df.label.isin(
                part['cluster'])].sample(1)
            pictures_df = pictures_df.drop(selection.index)

            picture_id = selection.pictures_id.values[0]
        else:
            picture_id = random.choice(part['picture_ids'])

        pictures_id_positions.append({
            'pictures_id': picture_id,
            'position': part['position']
        })

        if part['size'][0] < O_WIDTH or part['size'][1] < O_HEIGHT:
            part_array = Tools.read_np_picture(os.path.join(
                directory, "%s.jpg" % picture_id),
                                               target_size=(O_HEIGHT, O_WIDTH))
            index_col = np.floor((O_WIDTH - part['size'][0]) / 2).astype(int)
            index_row = np.floor((O_HEIGHT - part['size'][1]) / 2).astype(int)
            part_array = part_array[index_col:(index_col + part['size'][1]),
                                    index_row:(index_row +
                                               part['size'][0]), :].copy()
        else:
            part_array = Tools.read_np_picture(
                os.path.join(directory, "%s.jpg" % picture_id),
                target_size=(part['size'][1], part['size'][0]))

        poster[part['origin'][1]:(part['origin'][1] + part['size'][1]),
               part['origin'][0]:(part['origin'][0] +
                                  part['size'][0]), :] = part_array

    return poster, pictures_id_positions
#%% [markdown]
# ## Visualisation des clusters

#%% 
def select_cluster(clustering, id_cluster):
    return [os.path.join('data/processed/models/autoencoder/train/k/', res[0] + '.jpg') for res in clustering.get_zip_results() if res[2] == id_cluster]

#%%
from IPython.display import Image

#%%
for cl in range(0,19):
    print("Cluster %s" % (cl))
    res_tmp = select_cluster(clustering, cl)
    print(len(res_tmp))
    image_array = [Tools.read_np_picture(f, target_size = (54, 96)) for f in res_tmp[:100]]
    # img = Tools.display_mosaic(image_array, nrow = 10)
    # fig = plt.figure(1, figsize=(12, 7))
    # plt.imshow(img, aspect = 'auto')
    # plt.show()


#%% [markdown]
# ## Zoom sur le cluster 0

#%%
res_tmp = select_cluster(clustering, 1)

#%%
print(len(res_tmp))
image_array = [Tools.read_np_picture(f, target_size = (54, 96)) for f in res_tmp]
Пример #5
0
#%%[markdown]
# # Graphiques

#%%
def select_cluster(clustering, id_cluster):
    return [os.path.join('data/processed/models/autoencoder/train/k/', res[0] + '.jpg') for res in clustering.get_zip_results() if res[2] == id_cluster]


#%%
for cl in np.unique(clustering.kmeans_labels):
    print("Cluster %s" % (cl))
    res_tmp = select_cluster(clustering, cl)
    if len(res_tmp) >= 0:
        print(len(res_tmp))
        image_array = [Tools.read_np_picture(f, target_size = (54, 96)) for f in res_tmp[:100]]
        img = Tools.display_mosaic(image_array, nrow = 10)
        fig = plt.figure(1, figsize=(12, 7))
        plt.imshow(img, aspect = 'auto')
        plt.show()

#%% [markdown]
# ## faut essayer de faire des paquets

#%%
from sklearn.manifold import TSNE

output_tnse = TSNE(n_components=2).fit_transform(clustering.pca_reduction)


#%%
Пример #6
0
def run_plots(config, clustering_type, clustering):
    """
    Plots specifics graphs
    """

    if clustering_type in ['classical']:
        ## Graphs of PCA and final clusters
        fig, ax = plt.subplots(figsize=(24, 14))
        scatter = ax.scatter(clustering.pca_reduction[:, 0],
                             clustering.pca_reduction[:, 1],
                             c=clustering.colors)
        legend1 = ax.legend(*scatter.legend_elements(),
                            loc="lower left",
                            title="Classes")
        ax.add_artist(legend1)
        plt.savefig(os.path.join(clustering.save_directory,
                                 'pca_clusters.png'))

    if clustering_type in ['classical']:
        ## Graphs of TSNE and final clusters
        fig, ax = plt.subplots(figsize=(24, 14))
        classes = clustering.final_labels
        scatter = ax.scatter(clustering.tsne_embedding[:, 0],
                             clustering.tsne_embedding[:, 1],
                             c=clustering.colors)
        legend1 = ax.legend(*scatter.legend_elements(),
                            loc="lower left",
                            title="Classes")
        ax.add_artist(legend1)
        plt.savefig(
            os.path.join(clustering.save_directory, 'tsne_clusters.png'))

    if clustering_type in ['n2d', 'dbscan']:
        ## Graphs of TSNE and final clusters
        fig, ax = plt.subplots(figsize=(24, 14))
        classes = clustering.final_labels
        scatter = ax.scatter(clustering.umap_embedding[:, 0],
                             clustering.umap_embedding[:, 1],
                             c=clustering.colors)
        legend1 = ax.legend(*scatter.legend_elements(),
                            loc="lower left",
                            title="Classes")
        ax.add_artist(legend1)
        plt.savefig(
            os.path.join(clustering.save_directory, 'umap_clusters.png'))

    if clustering_type in ['n2d', 'classical', 'dbscan']:
        filenames = [
            os.path.join(
                config.get('directory')['collections'], "%s.jpg" % one_res[0])
            for one_res in clustering.get_results()
        ]
        images_array = [
            Tools.read_np_picture(img_filename, target_size=(54, 96))
            for img_filename in filenames
        ]
        base64_images = [Tools.base64_image(img) for img in images_array]

        if clustering_type in ['n2d', 'dbscan']:
            x = clustering.umap_embedding[:, 0]
            y = clustering.umap_embedding[:, 1]
            html_file = 'umap_bokeh.html'
            title = 'UMAP projection of iss clusters'
        elif clustering_type == 'classical':
            x = clustering.tsne_embedding[:, 0]
            y = clustering.tsne_embedding[:, 1]
            html_file = 'tsne_bokeh.html'
            title = 't-SNE projection of iss clusters'

        df = pd.DataFrame({'x': x, 'y': y})
        df['image'] = base64_images
        df['label'] = clustering.final_labels.astype(str)
        df['color'] = df['label'].apply(Tools.get_color_from_label)

        datasource = ColumnDataSource(df)

        output_file(os.path.join(clustering.save_directory, html_file))

        plot_figure = figure(
            title=title,
            # plot_width=1200,
            # plot_height=1200,
            tools=('pan, wheel_zoom, reset'))

        plot_figure.add_tools(
            HoverTool(tooltips="""
        <div>
            <div>
                <img src='@image' style='float: left; margin: 5px 5px 5px 5px'/>
            </div>
            <div>
                <span style='font-size: 16px'>Cluster:</span>
                <span style='font-size: 18px'>@label</span>
            </div>
        </div>
        """))

        plot_figure.circle('x',
                           'y',
                           source=datasource,
                           color=dict(field='color'),
                           line_alpha=0.6,
                           fill_alpha=0.6,
                           size=4)

        show(plot_figure)

    if clustering_type in ['classical']:
        ## Dendogram
        fig, ax = plt.subplots(figsize=(24, 14))
        plt.title('Hierarchical Clustering Dendrogram')
        Tools.plot_dendrogram(clustering.cah_fit, labels=clustering.cah_labels)
        plt.savefig(os.path.join(clustering.save_directory, 'dendograms.png'))

    return True