Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chapter-4/2-similarity-search-level-1.ipynb - Plot a scatter plot from the generated t-SNE results #172

Closed
zoldaten opened this issue Apr 3, 2023 · 4 comments

Comments

@zoldaten
Copy link

zoldaten commented Apr 3, 2023

1.cant visualize this:

#color_map = plt.cm.get_cmap('coolwarm')
color_map = matplotlib.colormaps['coolwarm']
scatter_plot = plt.scatter(tsne_results[:, 0],
                           tsne_results[:, 1],
                           c=selected_class_ids,
                           cmap=color_map)
plt.colorbar(scatter_plot)
plt.show()
# To save the plot in a high definition format i.e. PDF, uncomment the following line:
#plt.savefig('results/' + str(ADD_NAME_HERE)+'.pdf', format='pdf', dpi=1000)

gives:

ValueError                                Traceback (most recent call last)
File ~/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py:4439, in Axes._parse_scatter_color_args(c, edgecolors, kwargs, xsize, get_next_color_func)
   4438 try:  # Is 'c' acceptable as PathCollection facecolors?
-> 4439     colors = mcolors.to_rgba_array(c)
   4440 except (TypeError, ValueError) as err:

File ~/.local/lib/python3.8/site-packages/matplotlib/colors.py:487, in to_rgba_array(c, alpha)
    486 else:
--> 487     rgba = np.array([to_rgba(cc) for cc in c])
    489 if alpha is not None:

File ~/.local/lib/python3.8/site-packages/matplotlib/colors.py:487, in <listcomp>(.0)
    486 else:
--> 487     rgba = np.array([to_rgba(cc) for cc in c])
    489 if alpha is not None:

File ~/.local/lib/python3.8/site-packages/matplotlib/colors.py:299, in to_rgba(c, alpha)
    298 if rgba is None:  # Suppress exception chaining of cache lookup failure.
--> 299     rgba = _to_rgba_no_colorcycle(c, alpha)
    300     try:

File ~/.local/lib/python3.8/site-packages/matplotlib/colors.py:381, in _to_rgba_no_colorcycle(c, alpha)
    380 if not np.iterable(c):
--> 381     raise ValueError(f"Invalid RGBA argument: {orig_c!r}")
    382 if len(c) not in [3, 4]:

ValueError: Invalid RGBA argument: 0.0

The above exception was the direct cause of the following exception:

ValueError                                Traceback (most recent call last)
Cell In[62], line 3
      1 #color_map = plt.cm.get_cmap('coolwarm')
      2 color_map = matplotlib.colormaps['coolwarm']
----> 3 scatter_plot = plt.scatter(tsne_results[:, 0],
      4                            tsne_results[:, 1],
      5                            c=selected_class_ids,
      6                            cmap=color_map)
      7 plt.colorbar(scatter_plot)
      8 plt.show()

File ~/.local/lib/python3.8/site-packages/matplotlib/pyplot.py:2862, in scatter(x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite, data, **kwargs)
   2857 @_copy_docstring_and_deprecators(Axes.scatter)
   2858 def scatter(
   2859         x, y, s=None, c=None, marker=None, cmap=None, norm=None,
   2860         vmin=None, vmax=None, alpha=None, linewidths=None, *,
   2861         edgecolors=None, plotnonfinite=False, data=None, **kwargs):
-> 2862     __ret = gca().scatter(
   2863         x, y, s=s, c=c, marker=marker, cmap=cmap, norm=norm,
   2864         vmin=vmin, vmax=vmax, alpha=alpha, linewidths=linewidths,
   2865         edgecolors=edgecolors, plotnonfinite=plotnonfinite,
   2866         **({"data": data} if data is not None else {}), **kwargs)
   2867     sci(__ret)
   2868     return __ret

File ~/.local/lib/python3.8/site-packages/matplotlib/__init__.py:1442, in _preprocess_data.<locals>.inner(ax, data, *args, **kwargs)
   1439 @functools.wraps(func)
   1440 def inner(ax, *args, data=None, **kwargs):
   1441     if data is None:
-> 1442         return func(ax, *map(sanitize_sequence, args), **kwargs)
   1444     bound = new_sig.bind(ax, *args, **kwargs)
   1445     auto_label = (bound.arguments.get(label_namer)
   1446                   or bound.kwargs.get(label_namer))

File ~/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py:4602, in Axes.scatter(self, x, y, s, c, marker, cmap, norm, vmin, vmax, alpha, linewidths, edgecolors, plotnonfinite, **kwargs)
   4599 if edgecolors is None:
   4600     orig_edgecolor = kwargs.get('edgecolor', None)
   4601 c, colors, edgecolors = \
-> 4602     self._parse_scatter_color_args(
   4603         c, edgecolors, kwargs, x.size,
   4604         get_next_color_func=self._get_patches_for_fill.get_next_color)
   4606 if plotnonfinite and colors is None:
   4607     c = np.ma.masked_invalid(c)

File ~/.local/lib/python3.8/site-packages/matplotlib/axes/_axes.py:4445, in Axes._parse_scatter_color_args(c, edgecolors, kwargs, xsize, get_next_color_func)
   4443 else:
   4444     if not valid_shape:
-> 4445         raise invalid_shape_exception(c.size, xsize) from err
   4446     # Both the mapping *and* the RGBA conversion failed: pretty
   4447     # severe failure => one may appreciate a verbose feedback.
   4448     raise ValueError(
   4449         f"'c' argument must be a color, a sequence of colors, "
   4450         f"or a sequence of numbers, not {c!r}") from err

ValueError: 'c' argument has 8677 elements, which is inconsistent with 'x' and 'y' with size 2176.
@zoldaten
Copy link
Author

zoldaten commented Apr 3, 2023

  1. this code doesnt work either:
tsne_to_grid_plotter_manual(tsne_results[:, 0], tsne_results[:, 1],
                            selected_filenames)
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[72], line 1
----> 1 tsne_to_grid_plotter_manual(tsne_results[:, 0], tsne_results[:, 1],
      2                             selected_filenames)

Cell In[71], line 11, in tsne_to_grid_plotter_manual(x, y, selected_filenames)
      9 x_y_dict = {}
     10 for i, image_path in enumerate(selected_filenames):
---> 11     a = np.ceil(x[i] * (S - s))
     12     b = np.ceil(y[i] * (S - s))
     13     a = int(a - np.mod(a, s))

IndexError: index 2176 is out of bounds for axis 0 with size 2176

@zoldaten
Copy link
Author

zoldaten commented Apr 3, 2023

  1. and this:
for i in range(3):
    random_image_index = random.randint(0, num_images)
    distances, indices = neighbors.kneighbors(
        [feature_list[random_image_index]])
    # Don't take the first closest image as it will be the same image
    similar_image_paths = [filenames[random_image_index]] + \
        [filenames[indices[0][i]] for i in range(1, 4)]
    plot_images(similar_image_paths, distances[0])

gives:
IndexError: index 5281 is out of bounds for axis 0 with size 2176

but i fix this point:
random_image_index = random.randint(0, 2176)

@zoldaten
Copy link
Author

zoldaten commented Apr 3, 2023

perhaps, it because i have less features extracted:

num_images = len(filenames)
num_features_per_image = len(feature_list[0])
print("Number of images = ", num_images)
print("Number of features per image = ", num_features_per_image)

Number of images =  8677
Number of features per image =  2048

in Books example:

Number of images =  8677
Number of features per image =  100352

dont know why that happened ?

@zoldaten
Copy link
Author

zoldaten commented Apr 3, 2023

fixed in #169

@zoldaten zoldaten closed this as completed Apr 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant