keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 909 forks source link

how to use mulit input with different shape in pretrained model? #82

Closed rocknamx8 closed 5 years ago

rocknamx8 commented 5 years ago
I have some image dataset (512 x 512), and I want to make a classification. I have resize them to 3 different resolution [512x512, 256x256, 128x128]. As is well-known that some convolution network with global average pooling can accept different shape input. So, I want to take 3 different shape inputs and concat their output to a new dense layer.
However, keras pretrained model have to specify their input shape, else it would use default input shape. How should I do?
taehoonlee commented 5 years ago

@rocknamx8, Please refer to the official document.

rocknamx8 commented 5 years ago

@rocknamx8, Please refer to the official document.

Thanks, but if I have some image with random(inconstant) shape (In some situation I can't preprocess them), Is that any other means to solve it?

taehoonlee commented 5 years ago

@rocknamx8

import numpy as np
import keras

from keras.models import Model
from keras.layers import Input, Dense, GlobalAveragePooling2D
from keras.applications.resnet50 import ResNet50

model = ResNet50(include_top=False, weights='imagenet', input_shape=(None, None, 3))

digit_a = Input(shape=(512, 512, 3))
digit_b = Input(shape=(256, 256, 3))

out_a = GlobalAveragePooling2D()(model(digit_a))
out_b = GlobalAveragePooling2D()(model(digit_b))

concatenated = keras.layers.concatenate([out_a, out_b])
out = Dense(1, activation='sigmoid')(concatenated)

classification_model = Model([digit_a, digit_b], out)
rocknamx8 commented 5 years ago

@rocknamx8

import numpy as np
import keras

from keras.models import Model
from keras.layers import Input, Dense, GlobalAveragePooling2D
from keras.applications.resnet50 import ResNet50

model = ResNet50(include_top=False, weights='imagenet', input_shape=(None, None, 3))

digit_a = Input(shape=(512, 512, 3))
digit_b = Input(shape=(256, 256, 3))

out_a = GlobalAveragePooling2D()(model(digit_a))
out_b = GlobalAveragePooling2D()(model(digit_b))

concatenated = keras.layers.concatenate([out_a, out_b])
out = Dense(1, activation='sigmoid')(concatenated)

classification_model = Model([digit_a, digit_b], out)

Thanks for your patience of my question, but my means is "IF I CAN'T KNOW THE INPUT SHAPE OF IMAGE",even in some condition I'm not permitted to reshape them.

taehoonlee commented 5 years ago

@rocknamx8, you can replace shape=(512, 512, 3) with shape=(None, None, 3).

rocknamx8 commented 5 years ago

@rocknamx8, you can replace shape=(512, 512, 3) with shape=(None, None, 3).

Thanks! That's awesome!