-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
ImageDataGenerator With 2 channel data? #3416
Comments
Have you investigated if and where it fails with 2 channel data? If I remember correctly, the apply_transform method is applying the transformation to each channel separately. In fact, I have used it with data of different channels (2 also), but only by calling the transformation methods (shifting, rotating, etc.) directly, not via ImageDataGenerator. |
To be honest I haven't as I though 99% of training is done with 1 or 3 channels from images. Reading the docs was also stating this. Ill come back with results on this. |
that sounds great! |
@oeway In the keras command, you have to define the image type, that is, color_mode = 'rgb' or 'grayscale' when using the command flow_from_directory with model.fit_generator. So how do I change color_mode in flow_from_directory to use with 2, 4, 5 and 6 channels? I thank you for your attention, |
@gledsonmelotti You can try with my extension: #3338 . For images with 2, 4, 5 and 6 channels or more, you need to define your own This is currently we used by default: def pil_image_reader(filepath, target_mode=None, target_size=None, dim_ordering=K.image_dim_ordering(), **kwargs):
img = load_img(filepath, target_mode=target_mode, target_size=target_size)
return img_to_array(img, dim_ordering=dim_ordering) You can define your own reader, for example, you can load your image in tif format which can contain multiple frames other than 1 or 3. def customized_image_reader(filepath, target_mode=None, target_size=None, dim_ordering=K.image_dim_ordering(), **kwargs):
imgTiff = PIL.Image(filepath, mode="r")
imgs = []
for i in range(6):
imgTiff.seek(i)
img = np.array(imgTiff, dtype="float32")
imgs.append(img)
imgArr = np.stack(imgs)
return imgArr Then you can use it like this: datagenX.flow_from_directory(image_folder, image_reader=customized_image_reader, ...) Please also notice that there maybe compatible issue with latest Keras, you may need to adjust the code yourself. |
@oeway |
@thepate94227 That's because my extension is not in the official implementation, perhaps you want to look at this: https://github.com/bernardohenz/ExtendableImageDatagen a further extension made by @bernardohenz. |
@oeway I tried to do it your way, but i get an error in this line
Do you know why? It seems, that PIL.Image is not a function... This is my code so far:
|
You should do |
Dann i get an error: IsADirectoryError: [Errno 21] Is a directory: '/Keras3/Label/0/' |
@oeway thanky very much. It is perfect. |
You can find a good idea in #10499 and https://stackoverflow.com/questions/49404993/keras-how-to-use-fit-generator-with-multiple-inputs. They use two ImageDataGenerator. |
Thank you, but my problem is, that i have semantic segmentation, so i have one input folder with many images and i have labels for each class. For example: i have an image with a human, a cat and a background. For that image i have a ground truth with two labels: one for the human, one for the cat. I ignore the background. When i use Keras flow_from_directory, i can only use images with 1, 3 or 4 channels, but for my task i have the two labels stacked together, so it has 2 channels... |
@thepate94227 You can split your 2 channels images into one channel images. Create a folder for the first channel and a folder for the second channel. You can save each channel as a new png file. Then you can use the following commands: train_datagen = ImageDataGenerator(rescale=1./255) def generate_generator_multiple(generator,dir1, dir2, batch_size, img_height,img_width):
inputgenerator=generate_generator_multiple(generator=train_datagen, Note that I did this with the explanations of the following sites: |
Thank you for you answer. I think i understand and will try it! And my Generator for my Input Images can be like in the Keras Tutorial?
And then i combine them like in the Tutorial?
With custom_output_generator like your code. |
Hello @thepate94227 Suppose you have 2 main files:
Channel 2 will have:
dir1 = training subfile or validation subfile of Channel1. (path directory of teh subfile) Using the commands below, Keras alone will recognize the subfiles and keras will create the classes that exist in each subfile without you having to do anything. batch_size=64 img_width, img_height, img_channels = 200, 200, 2 model_input = Input(shape = (img_width, img_height, img_channels)) your model model_output = Dense(num_classes, activation='softmax')(z) sgd = SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True) genX1 = generator.flow_from_directory(dir1, genX2 = generator.flow_from_directory(dir2, inputgenerator=generate_generator_multiple(generator=train_datagen, validationtgenerator=generate_generator_multiple(generator=train_datagen, Results_Train=model.fit_generator(inputgenerator, |
@gledsonmelotti I use a Semantic Segmentation NN called DeepLab: https://github.com/bonlime/keras-deeplab-v3-plus DeepLab want the labels to be stacked. For example if i have 20 classes like class rope, class human, class cat etc. then the ground thruth is a stacked image with shape (300, 200, 20). I have two classes, rope blue and rope red. The background is not important. Therfore my ground thruth are images with (300,200,2). Your solution is good, if i have two input images for the same label. But i only want one input image and one label, but the label is a stacked image of all the labels. If i have only one class, my label would have the shape (300,200). I i have 27 classes, my label would have the shape (300,200,27). You can see my code above: #3416 (comment) I tried it like oeway said, but i got an error... I hope you understand, what i mean. |
Hello @thepate94227 Now I understand the your problem. Unfortunately I do not know how to solve or help you. I am sorry. |
No problem! Thank you for your help and answers :) |
To be compatible with Tensorflow 2.1 and the new fit method (fit_generator is deprected) I propose this implementation : `
` using with
|
Any plans for allowing arbitrary number of channels in the processing utils?
Thanks
The text was updated successfully, but these errors were encountered: