Reputation: 8413
I have gone through all SO question on this topic. Facing a strange problem here. I have the image path stored in file_names
.
from skimage import io
import numpy as np
X = np.array([np.array(io.imread(i)) for i in file_names])
print(X.shape)
# (50,)
print(X[0].shape)
# (375, 500, 3)
I need X
to be (50, 375, 500, 3)
. I tried reshape
, adding np.newaxis
etc but all fail. My next step is to use this for CNN
. Basically, I want to create a mnist_cnn kind dataset with my images.
Next lines :
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',
input_shape = (375, 500, 3)))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['accuracy'])
model.fit(X, y, # y is (50,36) using one hot encoding
batch_size=10,
epochs=10,
verbose=2)
Cause this:
ValueError: Error when checking input: expected conv2d_3_input to have 4 dimensions, but got array with shape (50, 1)
Upvotes: 1
Views: 210
Reputation: 33542
The numpy-part looks easy:
from skimage import io
import numpy as np
# assumption: images are homogeneous in terms of dimensions and channels!
files = ['C:/TEMP/pic0.jpg', 'C:/TEMP/pic0.jpg', 'C:/TEMP/pic0.jpg', 'C:/TEMP/pic0.jpg']
image_array = np.stack([io.imread(i) for i in files]) # default: axis=0
image_array.shape
# (4, 720, 540, 3)
Upvotes: 2