## How to change input shape in Sequential model in Keras

keras set input shape

keras input shape

resnet50 input shape

keras change layer input shape

keras dynamic input shape

keras change input tensor shape

keras change output shape

I have a sequential model that I built in Keras. I try to figure out how to change the shape of the input. In the following example

model = Sequential() model.add(Dense(32, input_shape=(500,))) model.add(Dense(10, activation='softmax')) model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

let's say that I want to build a new model with different input shape, conceptual this should looks like this:

model1 = model model1.layers[0] = Dense(32, input_shape=(250,))

is there a way to modify the model input shape?

Think about what changing the input shape in that situation would mean.

Your first model

model.add(Dense(32, input_shape=(500,)))

Has a dense layer that really is a 500x32 matrix.

If you changed your input to 250 elements, your layers's matrix and input dimension would mismatch.

If, however, what you were trying to achieve was to reuse your last layer's trained parameters from your first 500 element input model, you could get those weights by get_weights. Then you could rebuild a new model and set values at the new model with set_weights.

model1 = Sequential() model1.add(Dense(32, input_shape=(250,))) model1.add(Dense(10, activation='softmax')) model1.layers[1].set_weights(model1.layers[1].get_weights())

Keep in mind that model1 first layer (aka model1.layers[0]) would still be untrained

**Keras -- Transfer learning -- changing Input tensor shape,** You can do this by creating a new VGG16 model instance with the new input shape new_shape and copying over all the layer weights. The code is roughly In general, it's a recommended best practice to always specify the input shape of a Sequential model in advance if you know what it is. A common debugging workflow: add() + summary() When building a new Sequential architecture, it's useful to incrementally stack layers with add() and frequently print model summaries.

Somewhat related, so hopefully someone will find this useful: If you have an existing model where the input is a placeholder that looks like (None, None, None, 3) for example, you can load the model, replace the first layer with a concretely shaped input. Transformation of this kind is very useful when for example you want to use your model in iOS CoreML (In my case the input of the model was a MLMultiArray instead of CVPixelBuffer, and the model compilation failed)

from keras.models import load_model from keras import backend as K from keras.engine import InputLayer import coremltools model = load_model('your_model.h5') # Create a new input layer to replace the (None,None,None,3) input layer : input_layer = InputLayer(input_shape=(272, 480, 3), name="input_1") # Save and convert : model.layers[0] = input_layer model.save("reshaped_model.h5") coreml_model = coremltools.converters.keras.convert('reshaped_model.h5') coreml_model.save('MyPredictor.mlmodel')

**How to change input shape in Sequential model in Keras,** The following post has helped me: Keras VGG16 fine tuning The changes I need to make are the following: Add in the input shape to the model definition base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(299,299,3)), and Add a Flatten() layer to flatten the tensor output: x = base_model. Sequential groups a linear stack of layers into a tf.keras.Model.. Sequential provides training and inference features on this model.. Examples >>> # Optionally, the first layer can receive an ` input_shape ` argument: >>> model = tf. keras.

Here is another solution without defining each layer of the model from scratch. The key for me was to use "_layers" instead of "layers". The latter only seems to return a copy.

import keras import numpy as np def get_model(): old_input_shape = (20, 20, 3) model = keras.models.Sequential() model.add(keras.layers.Conv2D(9, (3, 3), padding="same", input_shape=old_input_shape)) model.add(keras.layers.MaxPooling2D((2, 2))) model.add(keras.layers.Flatten()) model.add(keras.layers.Dense(1, activation="sigmoid")) model.compile(loss='binary_crossentropy', optimizer=keras.optimizers.Adam(lr=0.0001), metrics=['acc'], ) model.summary() return model def change_model(model, new_input_shape=(None, 40, 40, 3)): # replace input shape of first layer model._layers[1].batch_input_shape = new_input_shape # feel free to modify additional parameters of other layers, for example... model._layers[2].pool_size = (8, 8) model._layers[2].strides = (8, 8) # rebuild model architecture by exporting and importing via json new_model = keras.models.model_from_json(model.to_json()) new_model.summary() # copy weights from old model to new one for layer in new_model.layers: try: layer.set_weights(model.get_layer(name=layer.name).get_weights()) except: print("Could not transfer weights for layer {}".format(layer.name)) # test new model on a random input image X = np.random.rand(10, 40, 40, 3) y_pred = new_model.predict(X) print(y_pred) return new_model if __name__ == '__main__': model = get_model() new_model = change_model(model)

**Change input shape dimensions for fine-tuning with Keras ,** Resizing to the original input dimensions of the CNN hurts accuracy and you postulate increasing resolution will help improve your model. In� To do this, I need to change my input layer to Input(shape=(None,None,3)). The obvious way to solve the problem would have been to train my model directly with an input shape of (None,None,3) but I use a custom loss function where I need to specify the size of my training images.

**The Sequential model,** Specifying the input shape in advance. Generally, all layers in Keras need to know the shape of their inputs� Model groups layers into an object with training and inference features. Arguments. inputs: The input(s) of the model: a keras.Input object or list of keras.Input objects. outputs: The output(s) of the model. See Functional API example below. name: String, the name of the model. There are two ways to instantiate a Model:

**The Sequential class,** This is identical to the following: >>> model = tf.keras.Sequential() >>> model.add (tf.keras.Input(shape=(16,))) >>> model.add(tf.keras.layers.Dense(8)). >� Change input shape dimensions for fine-tuning with Keras. 2020-06-04 Update: This blog post is now TensorFlow 2+ compatible! In the first part of this tutorial, we’ll discuss the concept of an input shape tensor and the role it plays with input image dimensions to a CNN.

**tf.keras.layers.Reshape,** Input shape: Arbitrary, although all dimensions in the input shape must be known/ fixed. Use the keyword as first layer in a Sequential model model = tf.keras. The main point is that the shape of the input to the Dense layers is dependent on width and height of the input to the entire model. The shape input to the dense layer cannot change as this would mean adding or removing nodes from the neural network.

##### Comments

- Is there a way to keep the entire model and just changed the input layer? actually I have a complex model that I want to train both with a full set of inputs and with a partial sets of input and to compare the results. I look for a way to modify the input dimension in modlel1 while keep the rest of the model identical (except for for the input dimension and for the firist layer of course)
- I think not. As I said, the dimensions would mismatch. If you want to find out how well your model does with partial inputs, I would suggest training a different model solely with your partial inputs. What you could do though if you really want to use the same model for both situations with different size inputs would be to use a RNN instead of a MLP.