0
votes

I am working on tutorial which use Isomap for image reconginzation, the code is as following:

the major error is that reshape function in def Plot2D , ValueError: cannot reshape array of size 72 into shape (8,8).

function for 2d plot :

def Plot2D(T, title, x, y, num_to_plot=40):
# This method picks a bunch of random samples (images in your case)
# to plot onto the chart:
fig = plt.figure()

ax = fig.add_subplot(111)
ax.set_title(title)
ax.set_xlabel('Component: {0}'.format(x))
ax.set_ylabel('Component: {0}'.format(y))

x_size = (max(T[:,x]) - min(T[:,x])) * 0.08
y_size = (max(T[:,y]) - min(T[:,y])) * 0.08

for i in range(num_to_plot):
    img_num = int(random.random() * num_images)
    x0, y0 = T[img_num,x]-x_size/2., T[img_num,y]-y_size/2.
    x1, y1 = T[img_num,x]+x_size/2., T[img_num,y]+y_size/2.
    img = df.iloc[img_num,:].reshape(num_pixels, num_pixels)
    ax.imshow(img, aspect='auto', cmap=plt.cm.gray, interpolation='nearest', zorder=100000, extent=(x0, x1, y0, y1))

function for image uploading and processing:

  df = []
  for image_path in glob.glob("path/*.png"):
      image= misc.imread(image_path)
      df.append((image[::2, ::2] / 255.0).reshape(-1))
      df = pd.DataFrame(df).T
      iso = Isomap(n_neighbors=3,n_components=3).fit(df)
      T = iso.transform(df)

function for Plotting :

     num_images, num_pixels = df.shape
     num_pixels = int(math.sqrt(num_pixels))
     Plot2D(T, "test", 0, 1, num_to_plot=40)

error message:

<ipython-input-30-e9aeee7b57c9> in Plot2D(T, title, x, y, num_to_plot)
 16         x0, y0 = T[img_num,x]-x_size/2., T[img_num,y]-y_size/2.
 17         x1, y1 = T[img_num,x]+x_size/2., T[img_num,y]+y_size/2.
 -> 18         img = df.iloc[img_num,:].reshape(num_pixels, num_pixels)
 19         ax.imshow(img, aspect='auto', cmap=plt.cm.gray, 
 interpolation='nearest', zorder=100000, extent=(x0, x1, y0, y1))

 ValueError: cannot reshape array of size 72 into shape (8,8)
1

1 Answers

0
votes

I know it might be too late for you, but hopefully whoever is using this course won't waste their time on something so simple (like I did this morning). The course doesn't get updated anymore and some pandas and numpy function changed since its first release I believe.

img = df.iloc[img_num,:].reshape(num_pixels, num_pixels)

Needs to be changed into:

img = df.iloc[img_num,:].values.reshape(num_pixels, num_pixels)

Same with the code where they transform the MATLAB file:

for i in range(num_images):
    df.loc[i,:] = df.loc[i,:].reshape(num_pixels, num_pixels).T.reshape(-1)

change to:

for i in range(num_images):
    df.loc[i,:] = df.loc[i,:].values.reshape(num_pixels, num_pixels).T.reshape(-1)