0

I have a project with TensorFlow and I am struggling with the preparation in double input data. I hope I can recieve your assist from this problem. So I will present my code here:

#input data
import os, cv2
train_path = r"..\Dataset\Train"
val_path = r"..\Dataset\Val"
def read_image_data(root):
    all file = os.listdir (root) #take all couple files
    #print(len(all_file))
    #load couple images
    datal = []
    data2 = []
    for i in all file:
        #take couple path
        temp_path = os.listdir(root + '\\' + i)
        datal.append(cv2.resize(cv2.imread(...))) # read and resize image
        data2.append(cv2.resize(cv2.imread(...))) # read and resize image
    return[data1, data2]

train_data = read_image_data(train_path)
val_data = read_image_data (val_path)

Then I create label: train_labels = np.ones (len(train_data[0]), dtype=int) This is my shape of train_data and label_data:

  • Shape of my data: train_data (2,27542, 320, 320, 3)
  • Shape of my labels: train_labels(27542,)

After that, I use code ImageGenerator for double input from: https://github.com/keras-team/keras/issues/3386

from tensorflow.keras.preprocessing.image import ImageDataGenerator

#This code I take from above link:
generator = ImageDataGenerator (rescale = 1/.255)
def generate_data_generator_for_two_images (X1, X2, Y) :
    print (np.shape(X1))
    print (np.shape(X2))
    print (np.shape(Y))
    genX1 = generator.flow(X1, Y, seed=7)
    genX2 = generator.flow(X2, seed=7)
    While True:
        XIi = genX1.next()
        X2i = genX2.next()
        yield [Xli[0], X2i J, X1i[1]
temp = next (generate_data_generator_for_two_images(train_data[e], train_data[1], train_labels))

It throws error: Error

I found a lot of information in many forum, but it seems make me more confuse. Besides, I didn't have many experiences in this multiple input case, I hope you can assist me in this problem.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Tờ Ân
  • 3
  • 3
  • Could you copy the error message and paste it in your question instead of posting an image? the relevant stacktrace would be the second one in your image (the line starting with `genX1 ...`). – Bahman Rouhani Jul 10 '23 at 14:49
  • Hi, I have a new error with command: import tensorflow as tf model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer='adam') model.fit(temp, #validation_data=(val_data, val_labels), epochs=200, batch_size=8) – Tờ Ân Jul 10 '23 at 18:42
  • --------------------------------------------------------------------------- ValueError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_7708\1553221597.py in 1 import tensorflow as tf 2 model.compile(loss=tf.keras.losses.BinaryCrossentropy(), optimizer='adam') ----> 3 model.fit(temp, 4 #validation_data=(val_data, val_labels), 5 epochs=200, batch_size=8) – Tờ Ân Jul 10 '23 at 18:45
  • c:\Users\VinhTH\anaconda3\lib\site-packages\keras\src\utils\traceback_utils.py in error_handler(*args, **kwargs) 68 # To get the full stack trace, call: 69 # `tf.debugging.disable_traceback_filtering()` ---> 70 raise e.with_traceback(filtered_tb) from None 71 finally: 72 del filtered_tb – Tờ Ân Jul 10 '23 at 18:46
  • c:\Users\VinhTH\anaconda3\lib\site-packages\keras\src\engine\training.py in tf__train_function(iterator) 13 try: 14 do_return = True ---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope) 16 except: 17 do_return = False – Tờ Ân Jul 10 '23 at 18:46
  • ValueError: in user code: ... File "c:\Users\VinhTH\anaconda3\lib\site-packages\keras\src\engine\input_spec.py", line 219, in assert_input_compatibility raise ValueError( ValueError: Layer "model_1" expects 2 input(s), but it received 3 input tensors. Inputs received: [, , ] – Tờ Ân Jul 10 '23 at 18:46
  • Hi, it's really hard for me or others to read and understand the error from the comments. If your previous question is resolved please either delete this question or post your answer so others will know what was the problem. You can make a new question about your new error OR alternatively you can update this question if the error is related. Generally it's best if all the information about the issue are contained inside your post and not on other websites/comments. You will also get better/faster answers this way. Cheers. – Bahman Rouhani Jul 11 '23 at 20:27
  • It seems like the problem is with the inputs (or shape of the inputs), I can't see the input you're passing to the `model` but just as a blind guess, are you by any chance also passing the labels to the model as input? – Bahman Rouhani Jul 11 '23 at 20:42

0 Answers0