Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
import matplotlib.pyplot as plt
%matplotlib inline
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest')
img = load_img('../input/face-expression/IMG_20200628_111907_2.jpg') # this is a PIL image
plt.imshow(img)
x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150)
x = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, 3, 150, 150)
# the .flow() command below generates batches of randomly transformed images
# and saves the results to the `preview/` directory
i = 0
for batch in datagen.flow(x, batch_size=1):
x = x.reshape(3,15925248)
plt.imshow(x)
i += 1
print(i)
if i > 2:
break # otherwise the generator would loop indefinitely
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-21-862c7d55a882> in <module>
1 i = 0
----> 2 for batch in datagen.flow(x, batch_size=1):
3 x = x.reshape(3,15925248)
4 plt.imshow(x)
5 i += 1
/opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/image_data_generator.py in flow(self, x, y, batch_size, shuffle, sample_weight, seed, save_to_dir, save_prefix, save_format, subset)
432 save_format=save_format,
433 subset=subset,
--> 434 dtype=self.dtype
435 )
436
/opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/numpy_array_iterator.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
124 raise ValueError('Input data in `NumpyArrayIterator` '
125 'should have rank 4. You passed an array '
--> 126 'with shape', self.x.shape)
127 channels_axis = 3 if data_format == 'channels_last' else 1
128 if self.x.shape[channels_axis] not in {1, 3, 4}:
ValueError: ('Input data in `NumpyArrayIterator` should have rank 4. You passed an array with shape', (1, 47775744))
!pip install jovian --upgrade --quiet
import jovian