Isomap Cant Reshape将72号大小的阵列重塑为形状(8,8)

时间:2018-01-25 09:26:36

标签: image machine-learning reshape

我正在编写使用Isomap进行图像识别的教程,代码如下:

主要错误是def Plot2D中的重塑函数,ValueError:无法将72号大小的数组重塑为形状(8,8)。

2d情节的功能:

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))

图像上传和处理功能:

  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)

绘图功能:

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

错误消息:

<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 个答案:

答案 0 :(得分:0)

我知道对您来说可能为时已晚,但是希望使用此课程的任何人都不会将他们的时间浪费在如此简单的事情上(就像我今天早上所做的那样)。我相信,自从首次发布以来,该课程已不再更新,并且一些熊猫和numpy函数已更改。

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

需要更改为:

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

与转换MATLAB文件的代码相同:

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

更改为:

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