Keras showing images from a data generator

I use the image generator for keras as follows:

val_generator = datagen.flow_from_directory(
        path+'/valid',
        target_size=(224, 224),
        batch_size=batch_size,)

x,y = val_generator.next()
for i in range(0,1):
    image = x[i]
    plt.imshow(image.transpose(2,1,0))
    plt.show()

This shows the wrong colors: enter image description here

I have two questions.

  • How to fix the problem

  • How to get file names in files (so that I can read it myself from something like matplotlib)

Edit: this is what my datagen looks like

datagen = ImageDataGenerator(
    rotation_range=3,
#     featurewise_std_normalization=True,
    fill_mode='nearest',
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True
)

Edit 2:

After Marcin's answer:

image = 255 - image

I get normal colors, but there are still some weird colors:

enter image description here

+6
source share
6 answers
  • There are at least three ways to have these twisted colors. So:

    • , , question.
    • -, ( 255 - x), , .
    • score/255.

    , .

  • , ( , Keras flow_from_directory), os.listdir os.path.join :

    list_of_labels = os.listdir(path_to_dir_with_label_dirs)
    for label in list_of_labels:
        current_label_dir_path = os.path.join(path_to_dir_with_label_dirs, label
        list_of_images = os.listdir(current_label_dir_path)
        for image in list_of_images:
            current_image_path = os.path.join(current_label_dir_path, image)
            image = open(current_image_path) # use the function which you want.
    
+3

- 'float32', 'uint8':

plt.imshow(image.astype('uint8'))
+6

, OP, , 0-255 0-1.

ImageDataGenerator Keras ' ' rescale ', (1/255).

image_gen = ImageDataGenerator(rescale=(1/255))
+4

. , Linux-.

Keras:

: https://github.com/fchollet/keras/blob/master/keras/preprocessing/image.py .

820, next() DirectoryIterator: .

838, save_to_dir , . . .

:

filenames=[] #<-------------------------------------------- new code
for i, j in enumerate(index_array):
    fname = self.filenames[j]
    img = load_img(os.path.join(self.directory, fname),
                   grayscale=grayscale,
                   target_size=self.target_size)
    x = img_to_array(img, dim_ordering=self.dim_ordering)
    x = self.image_data_generator.random_transform(x)
    x = self.image_data_generator.standardize(x)

    filenames.append(fname) # <-----------------------------store the used image name
    batch_x[i] = x
# optionally save augmented images to disk for debugging purposes
if self.save_to_dir:
    for i in range(current_batch_size):
        img = array_to_img(batch_x[i], self.dim_ordering, scale=True)
        #fname = '{prefix}_{index}_{hash}.{format}'.format(prefix=self.save_prefix,
        #                                                  index=current_index + i,
        #                                                  hash=np.random.randint(1e4),
        #                                                  format=self.save_format)
        fname=filenames[i] # <------------------------------ use the stored code instead
        img.save(os.path.join(self.save_to_dir, fname))

.

. , Keras?

:

  • clone Keras: git clone https://github.com/fchollet/keras
  • , . .
  • Python, , pip.

.

# this is the path to the cloned repository
# if you cloned it next to your script
# then just use keras/
# if it one folder above
# then use ../keras/
sys.path.insert(0, os.getcwd() + "/path/to/keras/")

import keras

DirectoryIterator .

, , . python linux. .

+1

, test_batches=Imagedatagenerator().flow . , , shuffle=false . , , 1.jpg, 2.jpg .. , . : 1.jpg, 10.jpg, 2.jpg, 20.jpg .. . , 0 , 01.jpg, 02.jpg ..

+1
from skimage import io

def imshow(image_RGB):
  io.imshow(image_RGB)
  io.show()

x,y = train_generator.next()

for i in range(0,11):
    image = x[i]
    imshow(image)

.

0

Source: https://habr.com/ru/post/1667003/


All Articles