I'm trying to create an A3C to play a game using frames as an input.
I think my A3C could benefit from having a form of memory layer like a lstm layer.
From what I understand of how the lstm works, you have to give it data by batch and the memory is only going to work on what's given in the batch.
Unfortunately, it's not possible for me to give the whole replay in the batch as the batch size would be way too big. So I wanted to know if it was possible to create a memory layer that would similar to how a lstm layer works. What I had in mind would generate some values based on the output of a layer from the neural network and decide if it's worth saving the values or keep the previous ones and then this memory layer would be fed to the next layer of the neural network.
S = Input(shape = (self.IMAGE_ROWS, self.IMAGE_COLS, self.IMAGE_CHANNELS, ), name = 'Input')
h0 = Convolution2D(1, kernel_size = (8,8), strides = (4,4), activation = 'relu', kernel_initializer = 'random_uniform', bias_initializer = 'random_uniform')(S)
h1 = Convolution2D(1, kernel_size = (4,4), strides = (2,2), activation = 'relu', kernel_initializer = 'random_uniform', bias_initializer = 'random_uniform')(h0)
h2 = Flatten()(h1)
h3 = Dense(256, activation = 'relu', kernel_initializer = 'random_uniform', bias_initializer = 'random_uniform') (h2)
h3 = Dropout(0.5)(h3)
# I was thinking of adding the memory layer here
# It would take the values of h3 and the output would feed h4_k with the values of h3
h4_k = Dense(256, activation = 'relu', kernel_initializer = 'random_uniform', bias_initializer = 'random_uniform') (h3)
h4_k = Dropout(0.5)(h4_k)
h5_k = Dense(256, activation = 'relu', kernel_initializer = 'random_uniform', bias_initializer = 'random_uniform') (h4_k)
h5_k = Dropout(0.5)(h5_k)
probs_k = Dense(self.n_actions_k, activation = 'softmax')(h5_k)
values_k = Dense(1, activation = 'linear')(h5_k)
Does this kind of layer already exist? If not how can I create a custom layer in tensorflow with the capacity to choose if it should update it's values or not?
Related
I am trying to have a normal classification model with a tabular dataset. I came across the Attention layer and I would like to use it to improve my model's accuracy.
input_features_size = X_train.shape[1]
layers = [
tf.keras.Input(shape = input_features_size),
tf.keras.layers.Dense(64, activation = 'relu', name = 'first_layer'),
tf.keras.layers.Dense(128, activation = 'relu', name = 'second_layer'),
tf.keras.layers.BatchNormalization(axis = 1),
tf.keras.layers.Dense(1, activation = 'sigmoid', name = 'output_layer')
]
metrics = [
tf.keras.metrics.BinaryAccuracy(name = 'accuracy'),
tf.keras.metrics.Precision(name = 'precision'),
tf.keras.metrics.Recall(name = 'recall')
]
NUM_EPOCHS = 20
deep_learning_model = Sequential(layers = layers, name = 'DL_Classifier')
deep_learning_model.compile(
loss = binary_crossentropy,
optimizer = Adam(learning_rate = 1e-4),
metrics = metrics
)
I tried adding Addention layer (tf.keras.layers.Attention()) in the layers list but I am doing some mistake here. I am getting this error : Attention layer must be called on a list of inputs, namely [query, value]
How to add an Attention layer?
I have to realize a project about images reconstruction. I have an 1024x1024 image which represents a place before a natural disaster, and I got the same image but post disaster. The goal is to reconstruct the post disaster image using an auto encoder.
First I've split my 1024x1024 into several tile with a size of 16x16. Then I created my layers for the auto encoder.
I guess I need to fit my model for every tiles to calculate the weights, but can I? Like create a for loop and fit my model for every tiles.
And when I realize the prediction, it's wrong and the same result for each tile.
I have no clue how to do that and I'm new in image processing and deep learning so I need help.
I share some piece of code here. Thanks!
My auto encoder layers below:
input_layer = layers.Input(shape= (16,16,1))
encoded = layers.Conv2D(64, (4,4), activation = 'relu' , padding = 'same')(input_layer)
encoded = layers.MaxPooling2D((2,2), padding='same')(encoded)
encoded_1 = layers.Conv2D(32, (4,4), activation = 'relu' , padding = 'same')(encoded)
encoded_1 = layers.MaxPooling2D((2,2), padding='same')(encoded_1)
encoded_2 = layers.Conv2D(16, (4,4), activation = 'relu' , padding = 'same')(encoded_1)
encoded_2 = layers.MaxPooling2D((2,2), padding='same')(encoded_2)
encoded_3 = layers.Conv2D(8, (4,4), activation = 'relu' , padding = 'same')(encoded_2)
encoded_3 = layers.MaxPooling2D((2,2), padding='same')(encoded_3)
encoding_layer = layers.Conv2D(4,(4,4), activation = 'relu', padding='same')(encoded_3)
decoded = layers.Conv2D(8, (4,4), activation='relu', padding='same')(encoding_layer)
decoded = layers.UpSampling2D((2,2))(decoded)
decoded_1 = layers.Conv2D(16,(4,4), activation = 'relu', padding='same')(decoded)
decoded_1 = layers.UpSampling2D((2,2))(decoded_1)
decoded_2 = layers.Conv2D(32, (4,4), activation='relu', padding='same')(decoded_1)
decoded_2 = layers.UpSampling2D((2,2))(decoded_2)
decoded_3 = layers.Conv2D(64,(4,4), activation = 'relu', padding='same')(decoded_2)
decoded_3 = layers.UpSampling2D((2,2))(decoded_3)
decoding_layer = layers.Conv2D(1,(4,4), activation='sigmoid', padding='same')(decoded_3)
autoencoder = models.Model(input_layer, decoding_layer)
my prediction block below:
number_epochs = 25
autoencoder.compile('adam', loss='binary_crossentropy')
for i in range(0,64):
for j in range(0,64):
history = autoencoder.fit(x = bands_split_train[i,j], y = bands_split_test[i,j], batch_size = 256, epochs=number_epochs,shuffle=True)
the bands_split_train variable is an array with a shape of (64,64,16,16,1)
And Below it's how I predict 1 tile :
predicted_tiles = autoencoder.predict(bands_split_train[0,0])
With that I have the same result for every single tile in the image
Could you help me with the code such that along with the dense layers also the last convolutional layer of Efficientnet is trained as well ?
features_url ="https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b3/feature_vector/2"
img_shape = (299,299,3)
features_layer = hub.KerasLayer(features_url,
input_shape=img_shape)
# below commented code keeps all the cnn layers frozen, thus it does not work for me at the moment
#features_layer.trainable = False
model = tf.keras.Sequential([
features_layer,
tf.keras.layers.Dense(256, activation = 'relu'),
tf.keras.layers.Dense(64, activation = 'relu'),
tf.keras.layers.Dense(4, activation = 'softmax')
])
In addition how can I save in a variable the name of the last convolutional layer ?
I have a task for my project paper and I do not get how to train the model. This model is supposed to take an image and segment it into different classes. The hard part is that the different segmentation is the same but I would like to differentiate between them. When I try to make a model with convolutional layers and LSTM, The model only predicts the class of the background.
Here is my model:
def LSTMconv10x9(input_size = (200, 9, 10, 1)):
input = Input(input_size)
conv1 = TimeDistributed(Conv2D(32, 3, padding = "same", activation='relu'))(input)
conv2 = TimeDistributed(Conv2D(64, 3, padding = "same", activation='relu'))(conv1)
lstm = ConvLSTM2D(32, 3, return_sequences=True, padding="same", activation="softmax")(conv2)
conv4 = TimeDistributed(Conv2D(64,3, padding = 'same', activation='relu'))(lstm)
conv5 = TimeDistributed(Conv2D(32,3, padding = 'same', activation='relu'))(conv4)
output = ConvLSTM2D(11,1, return_sequences = True, padding = "same", activation = None)(conv5)
model = Model(inputs = input, outputs = output)
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), optimizer = tf.keras.optimizers.Adam(),
metrics=["accuracy"], sample_weight_mode='temporal')
And the way I train the model:
weights = np.where(train_y == 0, 0.1, 0.9)
model1 = LSTMconv10x9simple()
model1.fit(train_x,train_y,epochs=20, batch_size=32,validation_data=(test_x, test_y),sample_weight=weights)
The training set size is (2000,200,9,10,1) and the validation set is (1000,200,9,10,1), where I have 2000 videos of 200 frames in the trainingset, the videos are of 10 structures that look the same but I would to numerate them in a way as different structures. This is a segmentation problem.
The data is very unbalanced as there are objects in each video that I want to separate, but the background is about 90% of the videos. I have tried initializing weights with the "sample_weight_mode='temporal'" method in TensorFlow, but it did not seem to work. The most important thing in the model is to find the structures.
Does anyone have any solutions to my problems?
I am using Python 3.X along with TensorFlow 2.0 to create a toy neural network model which is as follows:
model = Sequential()
model.add(
Dense(
units = 2, activation = 'relu',
kernel_initializer = tf.keras.initializers.GlorotNormal(),
input_shape = (2,)
)
)
model.add(
Dense(
units = 2, activation = 'relu',
kernel_initializer = tf.keras.initializers.GlorotNormal()
)
)
model.add(
Dense(
units = 1, activation = 'sigmoid'
)
)
I now want to modify the weights/biases of the model in a layer-wise manner. The code I have come up with to change the connections of the randomly initialized weights/biases of the model is that connections having magnitude less than 0.5 should become zero, while the others should remain the same:
for layer in model.trainable_weights:
layer = tf.where(tf.less(layer, 0.5), 0, layer)
However, this code does not change the connections as I want. What should I do?
Thanks!
Your code simply creates new tensors that have the desired values and puts them in the Python variable layer, but doesn't change the Tensorflow variables as you want to. You need to use the assign method of the Variable class:
for layer in model.trainable_weights:
layer.assign(tf.where(tf.less(layer, 0.5), 0, layer))