What does different arguments of model.sumary() in keras python mean? - python

When I write model.summary() I get something like
How can I get "connected to" argument? Also, can someone tell me the meaning of None in output shape argument?

None means that the number of samples that you feed into your network is not yet defined. This in general is a hyperparameter that you choose before training and is independent of your models architecture.
model.summary() is documented here
You might want to look into model.get_weights() or model.get_config() (which are methods though).
Useful attributes of class Model are (per Keras documentation):
model.layers: flattened list of the layers comprising the model graph
model.inputs: list of input tensors
model.outputs: list of output tensors

Related

Using custom keras model with layer sharing together dqn_agent.DqnAgent()

I am trying to use a custom neural network with the DqnAgent() from tf. In my model I need to use layer sharing. Thus, I use the functional API to build the model. The model has a dict as input and one layer with n neurons as output. The last layer is a Concat- and not a Dens-Layer though. The type of the model that i get from the functional API keras.Model(inputs=[...], outpunts[...]
is "keras.engine.functional.Functional".
Now i want to use my Model with the tf Agent like this:
agent = dqn_agent.DqnAgent(
train_env.time_step_spec(),
train_env.action_spec(),
q_network=model,
optimizer=optimizer,
td_errors_loss_fn=common.element_wise_squared_loss,
train_step_counter=train_step_counter)
I get the following Error thoug:
AttributeError: 'Functional' object has no attribute 'create_variables'
In call to configurable 'DqnAgent' (<class 'tf_agents.agents.dqn.dqn_agent.DqnAgent'>)
The q_network expects a network from type "network.Network". I am not sure how to convert or wrap my environment in such a way, that the DqnAgent() will accept it. How could I manage to do this? Any support is much appreciated. If you need more information about anything let me know.
Additional information about my Network:
Input dict with multiple inputs.
multiple shared dens layers. output of the last one is shape (1,).
concatenate all those outputs of shape (1,)
one multipy layer to eliminate infeasible actions by multiplying the outputs with 0 or 1 respectively.

Looking for layer names for keras inceptionresnetv2

Really don't have much idea of what I'm doing, followed this tutorial to process deepdream images https://www.youtube.com/watch?v=Wkh72OKmcKI
Trying to change the base model data set to any from here, https://keras.io/api/applications/#models-for-image-classification-with-weights-trained-on-imagenet particularly InceptionResNetV2 currently. InceptionV3 uses "mixed0" up to "mixed10" whereas, the former data set uses a different naming system apparently.
Would have to change this section
# Maximize the activations of these layers
names = ['mixed3', 'mixed5']
layers = [base_model.get_layer(name).output for name in names]
# Create the feature extraction model
dream_model = tf.keras.Model(inputs=base_model.input, outputs=layers)
I'm getting an error "no such layer: mixed3"
So yea, just trying to figure out how to get the names for the layers in this data set as well as others
You can simply enter the following code to find out the model architecture(including layer names).
#Model denotes the Inception model
model.summary()
Or to visualize complex relationships,
tf.keras.utils.plot_model(model, to_file='model.png')

How to use model architecture of pretrained models but no weights

I want to use ResNet model architecture and want to change last few layers; how can I only use model architecture from model zoo in Tensorflow?
To use a ResNet model, you can choose a select few from tensorflow.keras.applications including ResNet50, ResNet101, and ResNet152. Then, you will need to change a few of the default arguments if you want to do transfer learning. For your question, you will need to set the weights parameter equal to None. Otherwise, 'imagenet' weights are provided. Also, you need to set include_top to be False since the number of classes for your problem will likely be different from ImageNet. Finally, you will need to provide the shape of your data in input_shape. This would look something like this.
base = tf.keras.applications.ResNet50(include_top=False, weights=None, input_shape=shape)
To get a summary of the model, you can do
base.summary()
To add your own head, you can use the Functional API. You will need to add an Input layer and your own Dense layer which will correspond to your task. This could be
input = tf.keras.layers.Input(shape=shape)
base = base(input)
out = tf.keras.layers.Dense(num_classes, activation='softmax')(base)
Finally, to construct a model, you can do
model = tf.keras.models.Model(input, out)
The Model constructor takes 2 arguments. The first being the inputs to your model, and the second being the outputs. Note that calling model.summary() will show the ResNet base as a separate layer. To view all layers of the ResNet base, you can do model.layers[1].summary(), or you can modify the code on how you built your model. The second way would be
out = tf.keras.layers.Dense(num_classes, activation='softmax')(base.output)
model = tf.keras.models.Model(base.input, out)
Now you can view all layers with just model.summary().

TensorFlow - Difference between tf.keras.layers.Layer vs tf.keras.Model

Reading through the documentation of implementing custom layers with tf.keras, they specify two options to inherit from, tf.keras.Layer and tf.keras.Model.
Under the context of creating custom layers, I'm asking myself what is the difference between these two? Technically what is different?
If I were to implement the transformer encoder for example, which one would be more suitable? (assuming the transformer is a only a "layer" in my full model)
In the documentation:
The Model class has the same API as Layer, with the following
differences: - It exposes built-in training, evaluation, and
prediction loops (model.fit(), model.evaluate(), model.predict()). -
It exposes the list of its inner layers, via the model.layers
property. - It exposes saving and serialization APIs.
Effectively, the "Layer" class corresponds to what we refer to in the
literature as a "layer" (as in "convolution layer" or "recurrent
layer") or as a "block" (as in "ResNet block" or "Inception block").
Meanwhile, the "Model" class corresponds to what is referred to in the
literature as a "model" (as in "deep learning model") or as a
"network" (as in "deep neural network").
So if you want to be able to call .fit(), .evaluate(), or .predict() on those blocks or you want to be able to save and load those blocks separately or something you should use the Model class. The Layer class is leaner so you won't bloat the layers with unnecessary functionality...but I would guess that that generally wouldn't be a big problem.
A layer takes in a tensor and give out a tensor which is a result of
some tensor operations
A model is a composition of multiple layers.
If you are building a new model architecture using existing keras/tf layers then build a custom model.
If you are implementing your own custom tensor operations with in a layer, then build a custom layer. If you are using non tensor operation inside your custom layer, then you have to code how the layer will forward propagate and backward propagate.

Change the input size in Keras

I have trained a fully convolutional neural network with Keras. I have used the Functional API and have defined the input layer as Input(shape=(128,128,3)), corresponding to the size of the images in my training set.
However, I want to use the trained model on images of variable sizes (which should be ok because the network is fully convolutional). 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.
I have tried to define a new input layer and assign it to my model like this :
from keras.engine import InputLayer
input_layer = InputLayer(input_shape=(None, None, 3), name="input")
model.layers[0] = input_layer
This actually changes the size of the input layers accordingly but the following layers still expect (128,128,filters) inputs.
Is there a way to change all of the inputs values at once ?
Create a new model, exactly the same, except for new input shape; and tranfer weights:
newModel.set_weights(oldModel.get_weights())
If anything goes wrong, then it might not be fully convolutional (ex: contains a Flatten layer).

Categories