0
mypath='/Users/sachal/Desktop/data_raw/normal_1/images'
onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
images = np.asarray(np.empty(len(onlyfiles), dtype=object))

for n in range(0, len(onlyfiles)):
  images[n] = cv2.imread( join(mypath,onlyfiles[n]) )
#--------------------------------------------------------------------------------
resized = np.asarray(np.empty(len(onlyfiles), dtype=object))
img_f = np.asarray(np.empty(len(onlyfiles), dtype=object))


for n in range(0, len(onlyfiles)):
  resized[n] = cv2.resize(images[n],(101,101))
  img_f[n] = cv2.cvtColor(resized[n], cv2.COLOR_BGR2YUV)

train_img =  np.asarray(img_f)
#--------------------------------------------------------------------------------

In the above code first I am loading images using opencv then I am resizing and changing their colour space in the second block.

My batch size is 6408 and dimensions of images are 101*101*3 When i do train_img.shape i get(6408,) and upon train_img[i].shape i get 101*101*3 and I am unable to train my neural network model because of this and the dimensions i want are 6408*101*101*3

I tried reshaping with this train_img.resize(6408,101,101,3) i got this ValueError: cannot resize an array that references or is referenced by another array in this way. Use the resize function

and while fitting my model with i got this error Error when checking input: expected conv2d_3_input to have 4 dimensions, but got array with shape (6408, 1)

I want to know if i can change the dimensions of my input with the current method i am using to load my images.

2
  • Please try to provide a working example without local links to your data. Embedd a small matrix with values to show what you mean. Commented Aug 15, 2018 at 12:29
  • what I want is [1 1 1 1] this type of matrix but what i am getting is first a list of 6408 elements then each element is [1 1 1] Commented Aug 15, 2018 at 12:38

1 Answer 1

1

You shouldn't use the dtype=object here. OpenCV creates ndarray images anyway.

Here is a corrected version of your code:

mypath='/Users/sachal/Desktop/data_raw/normal_1/images'
onlyfiles = [ f for f in os.listdir(mypath) if os.path.isfile(join(mypath,f)) ]
images = []

for file in onlyfiles:
   img = cv2.imread(os.path.join(mypath,file))
   resized_img = cv2.resize(img, (101, 101)) 
   yuv_img = cv2.cvtColor(resized_img, cv2.COLOR_BGR2YUV)
   images.append(yuv_img.reshape(1, 101, 101, 3))

train_img = np.concatenate(images, axis=0)
print(train_img.shape)

In the loop, you load each image, resize it, convert it to YUV then put it in a list. At the end of the loop, your list contains all your training images. You can pass it to np.concatenate to create an ndarray.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.