-2

I am trying to resolve the error mentioned above, but i couldn't. I am trying to run the following python function:

def plot_test_samples():

fileList = os.listdir('../data/test_samples')
fileList = filter(lambda x: '.png' in x, fileList)
fileList.sort()
case_slices = [ [int(s) for s in fname.replace('.', '_').split('_') if s.isdigit()] for fname in fileList]
case_slices = np.array(case_slices)
X_test = np.load('../data/X_test.npy')
n_imgs = np.load('../data/test_n_imgs.npy').cumsum()

img_rows = X_test.shape[1]
img_cols = img_rows
model = get_model(img_rows, img_cols)

n_cols= 4
n_rows = int( np.ceil(len(case_slices)/n_cols*2) )

fig = plt.figure(figsize=[ 4*n_cols, int(4*n_rows)] )
gs = gridspec.GridSpec( n_rows , n_cols )

imgs = [ X_test[n_imgs[row[0]-1]+row[1]] for row in case_slices ]
imgs = np.stack(imgs)
masks = model.predict( imgs, verbose=1)
for mm, row in enumerate(case_slices):
    ax = fig.add_subplot(gs[2*mm])
    img = cv2.imread('../data/test_samples'+'/'+fileList[mm],cv2.IMREAD_COLOR)
    ax.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB) )
    ax = fig.add_subplot(gs[2*mm+1])
    ax.imshow(imgs[mm,:,:,0], cmap='gray' )
    contours = find_contours(masks[mm,:,:,0], 0.01, fully_connected='high')
    for n, contour in enumerate(contours):
        ax.plot(contour[:, 1], contour[:, 0], linewidth=1, color='b')

fig.savefig('../images/test_samples.png', bbox_inches='tight', dpi=300 )



len case_slices=795
    shape(case_slices)=(795, 1)
    len n_imgs=30
    len X_test=795
    shape(X_test)=(795, 256, 256, 1)

Please anyone have idea how i can resolve this issue let me know, Thank you in advance

Dar
  • 1
  • 3

1 Answers1

0

The error is basically telling you that you are trying to access the element number 795 from an array with 795 numbers, which goes from position number 0 to position number 794.

Try to change your code to:

len case_slices=795
shape(case_slices)=(794, 1)
len n_imgs=30
len X_test=795
shape(X_test)=(794, 256, 256, 1)
Shunya
  • 2,344
  • 4
  • 16
  • 28
  • thank you for you reply... but the slices's number is 795 so the shape of the vector must be (795,1) and the same for X_test so how I can do ??? – Dar Jan 15 '21 at 21:20
  • Can you tell us where this `shape()` function is coming from and if its yours post a snipe of code as well please? – Shunya Jan 16 '21 at 06:56
  • I mean np.shape () because when i had the error of the index i tried to extract the size of each list to resolve the error, the error is generated by this line: imgs = [ X_test[n_imgs[row[0]-1]+row[1]] for row in case_slices ] – Dar Jan 16 '21 at 10:27