Average of the two inputs in multi-input deep learning model
up vote
0
down vote
favorite
I want to create a multi-input deep learning model. The model takes two inputs (images) from different datasets and calculates the average of them. See the code:
input1 = keras.layers.Input(shape=(16,))
x1 = keras.layers.Dense(8, activation='relu')(input1)
input2 = keras.layers.Input(shape=(32,))
x2 = keras.layers.Dense(8, activation='relu')(input2)
a = keras.layers.average([x1, x2])
out = keras.layers.Dense(4)(a)
model = keras.models.Model(inputs=[input1, input2], outputs=out)
I tried the following code to create the generator, but I got an error:
input_imgen = ImageDataGenerator(
rotation_range=10,
shear_range=0.2,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1
)
test_imgen = ImageDataGenerator()
def generate_generator_multiple(generator,dir1, dir2, batch_size, img_height,img_width):
genX1 = generator.flow_from_directory(dir1,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
genX2 = generator.flow_from_directory(dir2,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
while True:
X2i = genX2.next()
X1i = genX1.next()
yield X1i[0], X2i[0]
inputgenerator=generate_generator_multiple(generator=input_imgen,
dir1=train_data1,
dir2=train_data2,
batch_size=32,
img_height=224,
img_width=224)
validgenerator=generate_generator_multiple(generator=test_imgen,
dir1=valid_data1,
dir2=valid_data2,
batch_size=32,
img_height=224,
img_width=224)
testgenerator=generate_generator_multiple(generator=test_imgen,
dir1=test_data1,
dir2=test_data2,
batch_size=32,
img_height=224,
img_width=224)
# compile the model
multi_model.compile(
loss='categorical_crossentropy',
optimizer=Adam(lr=0.0001),
metrics=['accuracy']
)
# train the model and save the history
history = multi_model.fit_generator(
inputgenerator,
steps_per_epoch=len(train_data) // batch_size,
epochs=10,
verbose=1,
validation_data=validgenerator,
validation_steps=len(valid_data) // batch_size,
use_multiprocessing=True,
shuffle=False)
I got this error:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[[108.930984, 108.930984, 108.930984],
[113.63957 , 113.63957 , 113.63957 ],
[113.07516 , 113.07516 , 113.07516 ],
...,
[ 99.46968 , 99.46968 , 99.46968 ...
How can I solve this problem and create the generator?
tensorflow machine-learning keras neural-network deep-learning
add a comment |
up vote
0
down vote
favorite
I want to create a multi-input deep learning model. The model takes two inputs (images) from different datasets and calculates the average of them. See the code:
input1 = keras.layers.Input(shape=(16,))
x1 = keras.layers.Dense(8, activation='relu')(input1)
input2 = keras.layers.Input(shape=(32,))
x2 = keras.layers.Dense(8, activation='relu')(input2)
a = keras.layers.average([x1, x2])
out = keras.layers.Dense(4)(a)
model = keras.models.Model(inputs=[input1, input2], outputs=out)
I tried the following code to create the generator, but I got an error:
input_imgen = ImageDataGenerator(
rotation_range=10,
shear_range=0.2,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1
)
test_imgen = ImageDataGenerator()
def generate_generator_multiple(generator,dir1, dir2, batch_size, img_height,img_width):
genX1 = generator.flow_from_directory(dir1,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
genX2 = generator.flow_from_directory(dir2,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
while True:
X2i = genX2.next()
X1i = genX1.next()
yield X1i[0], X2i[0]
inputgenerator=generate_generator_multiple(generator=input_imgen,
dir1=train_data1,
dir2=train_data2,
batch_size=32,
img_height=224,
img_width=224)
validgenerator=generate_generator_multiple(generator=test_imgen,
dir1=valid_data1,
dir2=valid_data2,
batch_size=32,
img_height=224,
img_width=224)
testgenerator=generate_generator_multiple(generator=test_imgen,
dir1=test_data1,
dir2=test_data2,
batch_size=32,
img_height=224,
img_width=224)
# compile the model
multi_model.compile(
loss='categorical_crossentropy',
optimizer=Adam(lr=0.0001),
metrics=['accuracy']
)
# train the model and save the history
history = multi_model.fit_generator(
inputgenerator,
steps_per_epoch=len(train_data) // batch_size,
epochs=10,
verbose=1,
validation_data=validgenerator,
validation_steps=len(valid_data) // batch_size,
use_multiprocessing=True,
shuffle=False)
I got this error:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[[108.930984, 108.930984, 108.930984],
[113.63957 , 113.63957 , 113.63957 ],
[113.07516 , 113.07516 , 113.07516 ],
...,
[ 99.46968 , 99.46968 , 99.46968 ...
How can I solve this problem and create the generator?
tensorflow machine-learning keras neural-network deep-learning
What exactly are you asking? What generator? The question is asking about averaging two inputs, correct? Looks like you're doing it already
– Ian Quah
Nov 22 at 21:39
The approach is correct. If you are getting errors , mention them here
– Dulmina
Nov 23 at 2:44
@lanQuah @lan Quah thanks for replying. I meanImageDataGenerator
andfit_generator
.
– Noran
Nov 23 at 7:44
@Dulmina Thanks for replying, I added some details to the post.
– Noran
Nov 23 at 7:57
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to create a multi-input deep learning model. The model takes two inputs (images) from different datasets and calculates the average of them. See the code:
input1 = keras.layers.Input(shape=(16,))
x1 = keras.layers.Dense(8, activation='relu')(input1)
input2 = keras.layers.Input(shape=(32,))
x2 = keras.layers.Dense(8, activation='relu')(input2)
a = keras.layers.average([x1, x2])
out = keras.layers.Dense(4)(a)
model = keras.models.Model(inputs=[input1, input2], outputs=out)
I tried the following code to create the generator, but I got an error:
input_imgen = ImageDataGenerator(
rotation_range=10,
shear_range=0.2,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1
)
test_imgen = ImageDataGenerator()
def generate_generator_multiple(generator,dir1, dir2, batch_size, img_height,img_width):
genX1 = generator.flow_from_directory(dir1,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
genX2 = generator.flow_from_directory(dir2,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
while True:
X2i = genX2.next()
X1i = genX1.next()
yield X1i[0], X2i[0]
inputgenerator=generate_generator_multiple(generator=input_imgen,
dir1=train_data1,
dir2=train_data2,
batch_size=32,
img_height=224,
img_width=224)
validgenerator=generate_generator_multiple(generator=test_imgen,
dir1=valid_data1,
dir2=valid_data2,
batch_size=32,
img_height=224,
img_width=224)
testgenerator=generate_generator_multiple(generator=test_imgen,
dir1=test_data1,
dir2=test_data2,
batch_size=32,
img_height=224,
img_width=224)
# compile the model
multi_model.compile(
loss='categorical_crossentropy',
optimizer=Adam(lr=0.0001),
metrics=['accuracy']
)
# train the model and save the history
history = multi_model.fit_generator(
inputgenerator,
steps_per_epoch=len(train_data) // batch_size,
epochs=10,
verbose=1,
validation_data=validgenerator,
validation_steps=len(valid_data) // batch_size,
use_multiprocessing=True,
shuffle=False)
I got this error:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[[108.930984, 108.930984, 108.930984],
[113.63957 , 113.63957 , 113.63957 ],
[113.07516 , 113.07516 , 113.07516 ],
...,
[ 99.46968 , 99.46968 , 99.46968 ...
How can I solve this problem and create the generator?
tensorflow machine-learning keras neural-network deep-learning
I want to create a multi-input deep learning model. The model takes two inputs (images) from different datasets and calculates the average of them. See the code:
input1 = keras.layers.Input(shape=(16,))
x1 = keras.layers.Dense(8, activation='relu')(input1)
input2 = keras.layers.Input(shape=(32,))
x2 = keras.layers.Dense(8, activation='relu')(input2)
a = keras.layers.average([x1, x2])
out = keras.layers.Dense(4)(a)
model = keras.models.Model(inputs=[input1, input2], outputs=out)
I tried the following code to create the generator, but I got an error:
input_imgen = ImageDataGenerator(
rotation_range=10,
shear_range=0.2,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1
)
test_imgen = ImageDataGenerator()
def generate_generator_multiple(generator,dir1, dir2, batch_size, img_height,img_width):
genX1 = generator.flow_from_directory(dir1,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
genX2 = generator.flow_from_directory(dir2,
target_size = (img_height,img_width),
class_mode = 'categorical',
batch_size = batch_size,
shuffle=False,
seed=7)
while True:
X2i = genX2.next()
X1i = genX1.next()
yield X1i[0], X2i[0]
inputgenerator=generate_generator_multiple(generator=input_imgen,
dir1=train_data1,
dir2=train_data2,
batch_size=32,
img_height=224,
img_width=224)
validgenerator=generate_generator_multiple(generator=test_imgen,
dir1=valid_data1,
dir2=valid_data2,
batch_size=32,
img_height=224,
img_width=224)
testgenerator=generate_generator_multiple(generator=test_imgen,
dir1=test_data1,
dir2=test_data2,
batch_size=32,
img_height=224,
img_width=224)
# compile the model
multi_model.compile(
loss='categorical_crossentropy',
optimizer=Adam(lr=0.0001),
metrics=['accuracy']
)
# train the model and save the history
history = multi_model.fit_generator(
inputgenerator,
steps_per_epoch=len(train_data) // batch_size,
epochs=10,
verbose=1,
validation_data=validgenerator,
validation_steps=len(valid_data) // batch_size,
use_multiprocessing=True,
shuffle=False)
I got this error:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 2 array(s), but instead got the following list of 1 arrays: [array([[[[108.930984, 108.930984, 108.930984],
[113.63957 , 113.63957 , 113.63957 ],
[113.07516 , 113.07516 , 113.07516 ],
...,
[ 99.46968 , 99.46968 , 99.46968 ...
How can I solve this problem and create the generator?
tensorflow machine-learning keras neural-network deep-learning
tensorflow machine-learning keras neural-network deep-learning
edited Nov 23 at 13:46
today
8,62621435
8,62621435
asked Nov 22 at 17:16
Noran
1649
1649
What exactly are you asking? What generator? The question is asking about averaging two inputs, correct? Looks like you're doing it already
– Ian Quah
Nov 22 at 21:39
The approach is correct. If you are getting errors , mention them here
– Dulmina
Nov 23 at 2:44
@lanQuah @lan Quah thanks for replying. I meanImageDataGenerator
andfit_generator
.
– Noran
Nov 23 at 7:44
@Dulmina Thanks for replying, I added some details to the post.
– Noran
Nov 23 at 7:57
add a comment |
What exactly are you asking? What generator? The question is asking about averaging two inputs, correct? Looks like you're doing it already
– Ian Quah
Nov 22 at 21:39
The approach is correct. If you are getting errors , mention them here
– Dulmina
Nov 23 at 2:44
@lanQuah @lan Quah thanks for replying. I meanImageDataGenerator
andfit_generator
.
– Noran
Nov 23 at 7:44
@Dulmina Thanks for replying, I added some details to the post.
– Noran
Nov 23 at 7:57
What exactly are you asking? What generator? The question is asking about averaging two inputs, correct? Looks like you're doing it already
– Ian Quah
Nov 22 at 21:39
What exactly are you asking? What generator? The question is asking about averaging two inputs, correct? Looks like you're doing it already
– Ian Quah
Nov 22 at 21:39
The approach is correct. If you are getting errors , mention them here
– Dulmina
Nov 23 at 2:44
The approach is correct. If you are getting errors , mention them here
– Dulmina
Nov 23 at 2:44
@lanQuah @lan Quah thanks for replying. I mean
ImageDataGenerator
and fit_generator
.– Noran
Nov 23 at 7:44
@lanQuah @lan Quah thanks for replying. I mean
ImageDataGenerator
and fit_generator
.– Noran
Nov 23 at 7:44
@Dulmina Thanks for replying, I added some details to the post.
– Noran
Nov 23 at 7:57
@Dulmina Thanks for replying, I added some details to the post.
– Noran
Nov 23 at 7:57
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
The error is raised because your model has two inputs but in this line:
yield X1i[0], X2i[0]
The generator would return a tuple of two arrays. In fit_generator
the first one would be interpreted as the model input and the second one would be interpreted as the model output. Hence you would get that error saying you have only passed one input to the model. To resolve this put the inputs in a list and also return the labels, whatever they should be:
yield [X1i[0], X2i[0]], the_labels_array
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435711%2faverage-of-the-two-inputs-in-multi-input-deep-learning-model%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The error is raised because your model has two inputs but in this line:
yield X1i[0], X2i[0]
The generator would return a tuple of two arrays. In fit_generator
the first one would be interpreted as the model input and the second one would be interpreted as the model output. Hence you would get that error saying you have only passed one input to the model. To resolve this put the inputs in a list and also return the labels, whatever they should be:
yield [X1i[0], X2i[0]], the_labels_array
add a comment |
up vote
1
down vote
accepted
The error is raised because your model has two inputs but in this line:
yield X1i[0], X2i[0]
The generator would return a tuple of two arrays. In fit_generator
the first one would be interpreted as the model input and the second one would be interpreted as the model output. Hence you would get that error saying you have only passed one input to the model. To resolve this put the inputs in a list and also return the labels, whatever they should be:
yield [X1i[0], X2i[0]], the_labels_array
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The error is raised because your model has two inputs but in this line:
yield X1i[0], X2i[0]
The generator would return a tuple of two arrays. In fit_generator
the first one would be interpreted as the model input and the second one would be interpreted as the model output. Hence you would get that error saying you have only passed one input to the model. To resolve this put the inputs in a list and also return the labels, whatever they should be:
yield [X1i[0], X2i[0]], the_labels_array
The error is raised because your model has two inputs but in this line:
yield X1i[0], X2i[0]
The generator would return a tuple of two arrays. In fit_generator
the first one would be interpreted as the model input and the second one would be interpreted as the model output. Hence you would get that error saying you have only passed one input to the model. To resolve this put the inputs in a list and also return the labels, whatever they should be:
yield [X1i[0], X2i[0]], the_labels_array
answered Nov 23 at 13:45
today
8,62621435
8,62621435
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435711%2faverage-of-the-two-inputs-in-multi-input-deep-learning-model%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What exactly are you asking? What generator? The question is asking about averaging two inputs, correct? Looks like you're doing it already
– Ian Quah
Nov 22 at 21:39
The approach is correct. If you are getting errors , mention them here
– Dulmina
Nov 23 at 2:44
@lanQuah @lan Quah thanks for replying. I mean
ImageDataGenerator
andfit_generator
.– Noran
Nov 23 at 7:44
@Dulmina Thanks for replying, I added some details to the post.
– Noran
Nov 23 at 7:57