tf.keras.models.clone_model unknown activation: ReLU - python

The environment is Python 3.7.6, below is my import:
import os, sys
import tensorflow as tf # v2.2.0
tf.compat.v1.enable_eager_execution()
import numpy as np
import matplotlib.pyplot as plt
from sys import platform
import time
import random
import pickle
from tensorflow.keras.layers import ReLU
I tried to clone a tf.keras.Model without success, because ReLU is an unknown activation. Nevertheless, the initiation was successful, so the system should know what ReLU is. I wonder how to fix this.
def init_model(D=8, W=256):
# This is a simple MLP neural network
# D: The number of layers
# H: The neurons in each layer
relu = ReLU()
dense = lambda W=W, act=relu: tf.keras.layers.Dense(W, activation=act, dtype='float32')
inputs = tf.keras.Input(shape=(3 + 3 * 2 * L_embed))
outputs = inputs
for i in range(D):
outputs = dense()(outputs)
if i % 4 == 0 and i > 0:
outputs = tf.concat([outputs, inputs], -1)
outputs = dense(4, act=None)(outputs)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
return model
Then I called:
model_inner_copy = tf.keras.models.clone_model(model)
The error message is:
File "D:/Code Repository/Meta-NeRF/Code/NeRF/Tiny_MAML_NeRF.py", line 211, in train_maml_nerf
model_inner_copy = tf.keras.models.clone_model(model)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 427, in clone_model
model, input_tensors=input_tensors, layer_fn=clone_function)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 196, in _clone_functional_model
model, new_input_layers, layer_fn)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 246, in _clone_layers_and_model_config
config = network.get_network_config(model, serialize_layer_fn=_copy_layer)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\network.py", line 2119, in get_network_config
layer_config = serialize_layer_fn(layer)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 243, in _copy_layer
created_layers[layer.name] = layer_fn(layer)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 61, in _clone_layer
return layer.__class__.from_config(layer.get_config())
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\base_layer.py", line 655, in from_config
return cls(**config)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\layers\core.py", line 1135, in __init__
self.activation = activations.get(activation)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\activations.py", line 465, in get
identifier, printable_module_name='activation')
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 362, in deserialize_keras_object
config, module_objects, custom_objects, printable_module_name)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 321, in class_and_config_for_serialized_keras_object
raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown activation: ReLU

So the problem is that tf.keras.layers.ReLU is a layer that implements the ReLU activation, but it is not an activation function by itself. It is meant to be used as a layer inside your model, not as a parameter of your Dense layer.
To have a function that works as an activation to give as a parameter to Dense, you should use tf.keras.activations.relu.

Related

bad zip file error while loading the trained deep learning model

I trained my model in colab and save it with torch.save('model.pth')
and then when i wanted to load it in my pycharm i get this error:
File "C:\Users\Amin\AppData\Local\Programs\Python\Python310\lib\zipfile.py", line 1334, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file`
can anyone help me to fix this error please
i could not find any solution for it on internet
i used tensorflow for training my model and used these imports :
from tensorflow.keras.preprocessing.text import text_to_word_sequence
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.layers import Embedding
from tensorflow.keras.layers import Conv1D, GlobalMaxPooling1D
my program can load the tokenizer that i have built but it wont load the model
this is my model :
max_features = 1000
maxlen = 650
embedding_dims = 50
filters = 250
kernel_size = 3
hidden_dims = 250
model5 = Sequential()
model5.add(Embedding(max_features, embedding_dims ))
model5.add(Dropout(0.2))
model5.add(Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1))
model5.add(GlobalMaxPooling1D())
model5.add(Dense(hidden_dims)) model5.add(Dropout(0.2)) model5.add(Activation('relu'))
model5.add(Dense(5)) model5.add(Activation('softmax'))
model5.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model5.fit(X_train, y_train,
batch_size=32,
epochs=14,
validation_data=(X_test, y_test))
torch.save(model5,'model.pth')
i loaded my model in colab and it was fine but it didn't work in pycharm
relative_model_path = "model.pth"
full_model_path = os.path.join(absolute_path, relative_model_path)
model = torch.load(full_model_path)
Traceback (most recent call last):
File "C:\\Users\\Amin\\PycharmProjects\\src\\model\\categorizer.py", line 25, in \<module\>
model = torch.load(full_model_path)
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\torch\\serialization.py", line 789, in load
return \_load(opened_zipfile, map_location, pickle_module, \*\*pickle_load_args)
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\torch\\serialization.py", line 1131, in \_load
result = unpickler.load()
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\pickle_utils.py", line 48, in deserialize_model_from_bytecode
raise e
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\pickle_utils.py", line 46, in deserialize_model_from_bytecode
model = saving_lib.load_model(filepath)
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\experimental\\saving_lib.py", line 196, in load_model
raise e
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\experimental\\saving_lib.py", line 173, in load_model
with zipfile.ZipFile(filepath, "r") as zipfile_to_load:
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\zipfile.py", line 1267, in __init__
self.\_RealGetContents()
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\zipfile.py", line 1334, in \_RealGetContents
raise BadZipFile("File is not a zip file")`your text`
zipfile.BadZipFile: File is not a zip file
I just needed to save the model with
keras.save('model')
not torch because the model was built in tensorflow keras

ValueError: Passing a dictionary input to a Sequential Model which doesn't have FeatureLayer as the first layer is an error

I've tried running the following code, but got this error:
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 819, in fit
use_multiprocessing=use_multiprocessing)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py",
line 235, in fit
use_multiprocessing=use_multiprocessing)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py",
line 593, in _process_training_inputs
use_multiprocessing=use_multiprocessing)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py",
line 706, in _process_inputs
use_multiprocessing=use_multiprocessing)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py",
line 702, in init
x = standardize_function(x)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py",
line 660, in standardize_function
standardize(dataset, extract_tensors_from_dataset=False)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 2346, in _standardize_user_data
all_inputs, y_input, dict_inputs = self._build_model_with_inputs(x, y)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 2572, in _build_model_with_inputs
self._set_inputs(cast_inputs)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 2647, in _set_inputs
inputs = self._set_input_attrs(inputs)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\training\tracking\base.py",
line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 2681, in _set_input_attrs
raise ValueError('Passing a dictionary input to a Sequential Model '
ValueError: Passing a dictionary input to a Sequential Model which
doesn't have FeatureLayer as the first layer is an error.
Code:
# -*- coding: utf-8 -*-
import os
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
import tensorflow_datasets as tfds
try:
model = keras.models.load_model("passrockmodel.h5")
except:
print('\nDownloading Train Dataset...\n')
train_dataset = tfds.load(name="rock_you", split="train[:75%]")
assert isinstance(train_dataset, tf.data.Dataset)
print('\nDownloading Test Dataset...\n')
test_dataset = tfds.load("rock_you", split='train[-25%:]')
assert isinstance(test_dataset, tf.data.Dataset)
model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid'),
])
model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(train_dataset, epochs=20)
model.save("passrockmodel.h5")
test_loss, test_accuracy = model.evaluate(test_dataset)
print('\nPredicting...\n')
predictions = model.predict(test_dataset)
print(predictions[0])
I've had your problem yesterday. Here's what solved it for me:
your first layer should be of type tf.keras.layers.DenseFeatures
This first layer must be instantiated with an array of tf.feature_column objects. It happens that all my columns were numeric so my array was:
featureColumns = [tf.feature_column.numeric_column(columnNames[i], normalizer_fn= lambda x: (x - mean[i])/std[i]) for i in range(len(columnNames[:-1]))]
Note: the normalizer_fn arg is very useful as you can see, as well. It can eliminate the need of any additional normalization preprocessing layer should you need it.
And so my layer became:
layers.DenseFeatures(feature_columns=featureColumns, trainable=True)
I believe this should solve the error mentioned above in your question. Which is quoted as
ValueError: Passing a dictionary input to a Sequential Model which
doesn't have FeatureLayer as the first layer is an error.

More input and one output issue in Keras

self.embed = Sequential([Embedding(9488, output_dim=512,input_length=14),
Activation('relu'),
Dropout(0.5)], name='embed.0')
self.fc_embed = Sequential([Dense(512, input_shape=(10,2048)),
Activation('relu'),
Dropout(0.5)], name='fc_embed.0')
inputs_bedding = Input(shape=(10,))
xt = self.embed(inputs_bedding)
input_feats = Input(shape=(10,2048))
fc_feats = self.fc_embed(input_feats)
fc_feats_new = K.reshape(fc_feats, [fc_feats.shape[1], fc_feats.shape[2]])
xt_new = K.reshape(xt, [xt.shape[1], xt.shape[2]])
prev_h = state[0][-1] (shape is (10,512))
att_lstm_input = Concatenate([prev_h, fc_feats_new, xt_new], axis=1)
lstm, h_att, c_att = LSTM(units=512, name='core.att_lstm', return_state=True)(att_lstm_input)
model = Model([input_feats, inputs_att, inputs_bedding], lstm)
model.summary()
This is the error I get:
File "copy_eval.py", line 165, in <module>
model1 = TopDownModel.forward(fc_feats, att_feats, seq, att_masks)
File "/home/ubuntu/misc/customize_keras.py", line 127, in forward
lstm, h_att, c_att = LSTM(units=512, name='core.att_lstm', return_state=True)(att_lstm_input)
File "/usr/local/lib/python2.7/dist-packages/keras/layers/recurrent.py", line 500, in call
return super(RNN, self).call(inputs, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 575, in call
self.assert_input_compatibility(inputs)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 448, in assert_input_compatibility
str(inputs) + '. All inputs to the layer '
ValueError: Layer core.att_lstm was called with an input that isn't a symbolic tensor. Received type: . Full input: []. All inputs to the layer should be tensors.
For more input, how to merge them into one output?
Concatenate should be used as a layer, like this:
att_lstm_input = Concatenate(axis=1)([prev_h, fc_feats_new, xt_new])

I can not run my code Tensorflow

I working on Ubuntu 14.04 ,i wrote a code for Recognition of letters whith Tensorflow V 0.11 ,
i'm creat a code source for uses the model LeNet5
my code source :
`
import PIL
import numpy
import tensorflow as tf
# from tensorflow.examples.tutorials.mnist import input_data
import Input as input_data
from tensorflow.python.framework.importer import import_graph_def
from Resize import Resize_img
# these functions to optimize the accurancy of the mnist training
#from imp_image import imp_img
import scipy.misc
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
# ============================================================ End Functions part
# mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
class MNIST:
def __init__(self):
# Open the compuation session
self.sess = tf.InteractiveSession()
# Load the network
self.Deep_Network()
def Deep_Network(self):
# nodes for the input images and target output classes.
# supervised classifier
self.x = tf.placeholder(tf.float32, shape=[None, 784])
self.y_ = tf.placeholder(tf.float32, shape=[None, 10])
# First convolutionanal Layer =====================================
# It will consist of convolution, followed by max pooling
# The convolutional will compute 32 features for each 5x5 patch.
self.W_conv1 = weight_variable([5, 5, 1, 32])
self.b_conv1 = bias_variable([32])
# To apply the layer, we first reshape x to a 4d tensor,
# with the second and third dimensions corresponding to image width and height,
# and the final dimension corresponding to the number of color channels.
self.x_image = tf.reshape(self.x, [-1, 28, 28, 1])
# We then convolve x_image with the weight tensor, add the bias, apply the ReLU function, and finally max pool.
self.h_conv1 = tf.nn.relu(conv2d(self.x_image, self.W_conv1) + self.b_conv1)
self.h_pool1 = max_pool_2x2(self.h_conv1)
# Second Convolutional Layer =====================================
# In order to build a deep network, we stack several layers of this type.
# The second layer will have 64 features for each 5x5 patch.
self.W_conv2 = weight_variable([5, 5, 32, 64])
self.b_conv2 = bias_variable([64])
self.h_conv2 = tf.nn.relu(conv2d(self.h_pool1, self.W_conv2) + self.b_conv2)
self.h_pool2 = max_pool_2x2(self.h_conv2)
# Densely Connected Layer
# Now that the image size has been reduced to 7x7, we add a fully-connected layer with 1024 neurons
# to allow processing on the entire image. We reshape the tensor from the pooling layer into
# a batch of vectors, multiply by a weight matrix, add a bias, and apply a ReLU.
self.W_fc1 = weight_variable([7 * 7 * 64, 1024])
self.b_fc1 = bias_variable([1024])
self.h_pool2_flat = tf.reshape(self.h_pool2, [-1, 7 * 7 * 64])
self.h_fc1 = tf.nn.relu(
tf.matmul(self.h_pool2_flat, self.W_fc1) + self.b_fc1) # ReLu Computes rectified linear: max(features, 0).
# Dropout
self.keep_prob = tf.placeholder(tf.float32)
self.h_fc1_drop = tf.nn.dropout(self.h_fc1, self.keep_prob)
# Readout Layer ========================================
# Finally, we add a softmax layer, just like for the one layer softmax regression above.
self.W_fc2 = weight_variable([1024, 10])
self.b_fc2 = bias_variable([10])
self.y_conv = tf.nn.softmax(tf.matmul(self.h_fc1_drop, self.W_fc2) + self.b_fc2)
self.cross_entropy = -tf.reduce_sum(self.y_ * tf.log(self.y_conv))
self.correct_prediction = tf.equal(tf.argmax(self.y_conv, 1), tf.argmax(self.y_, 1))
self.accuracy = tf.reduce_mean(tf.cast(self.correct_prediction, tf.float32))
def Prediction(self, imageName):
# Load the trained model
' Restore the model '
'here i should create the model saver'
Saved_model_dir = '/home/brm17/Desktop/PFE/'
saver = tf.train.Saver()
ckpt = tf.train.get_checkpoint_state(Saved_model_dir)
'verifie if the saved model exists or not!'
if ckpt and ckpt.model_checkpoint_path:
saver.restore(self.sess, ckpt.model_checkpoint_path)
else:
print '# No saved model found!'
exit() # exit the prgm
# image_test = 'number-3.jpg'
ResizedImage = Resize_img(imageName)
ImageInput = ResizedImage.mnist_image_input.reshape(1, -1)
print 'Predection > ', tf.argmax(self.y_conv, 1).eval(feed_dict={self.x: ImageInput, self.keep_prob: 1.0})
# print("test accuracy %g"%accuracy.eval(feed_dict={x: myTestImg, y_: myLabel, keep_prob: 1.0}))
def main():
image = '/home/brm17/Desktop/PFE/n2.jpeg'
model = MNIST()
model.Prediction(image)
if __name__ == "__main__":
main()
`
if i run this code , he print the error :
brm17#Brahim:~/Desktop/PFE$ python LeNet5.py
Traceback (most recent call last):
File "LeNet5.py", line 137, in <module>
model.Prediction(image)
File "LeNet5.py", line 120, in Prediction
saver.restore(self.sess, ckpt.model_checkpoint_path)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1129, in restore
{self.saver_def.filename_tensor_name: save_path})
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 710, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 908, in _run
feed_dict_string, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 958, in _do_run
target_list, options, run_metadata)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 978, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.NotFoundError: Tensor name "Variable_1" not found in checkpoint files /home/brm17/Desktop/PFE/MNISTmodel-20000
[[Node: save/restore_slice_1 = RestoreSlice[dt=DT_FLOAT, preferred_shard=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice_1/tensor_name, save/restore_slice_1/shape_and_slice)]]
Caused by op u'save/restore_slice_1', defined at:
File "LeNet5.py", line 137, in <module>
model.Prediction(image)
File "LeNet5.py", line 115, in Prediction
saver = tf.train.Saver()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 861, in __init__
restore_sequentially=restore_sequentially)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 519, in build
filename_tensor, vars_to_save, restore_sequentially, reshape)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 272, in _AddRestoreOps
values = self.restore_op(filename_tensor, vs, preferred_shard)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 187, in restore_op
preferred_shard=preferred_shard)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/io_ops.py", line 203, in _restore_slice
preferred_shard, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_io_ops.py", line 359, in _restore_slice
preferred_shard=preferred_shard, name=name)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2317, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1239, in __init__
self._traceback = _extract_stack()
what is the problem and how resolved this ?
Covißio,
I think the problem is as follows:
You created a network, and saved this network...
You changed the network, and did not remove the saved network
Now you try to reload your network from an old version, but a new variable you created does not exist.
Can you try to either:
Remove the saved state of your network and retrain it
Remove the saving and loading of your network and see if this works?
You can remove the state of your network by removing the checkpoint file in your folder /home/brm17/Desktop/PFE/
Edit: read your code thoroughly and the problem is that if there is no checkpoint you dont start retraining your network... Maybe you start by writing this before saving, loading, and changing your network.
Good luck and let me know if this works!

Load a pretrained caffe model to lasagne?

I am trying to reproduce the Long-term Recurrent Convolutional Networks paper.
I have a pretrained caffe model that I'd like to use in theano.
I have the .caffemodel for this file, and the prototxt.
I have used the lasagne example to load the caffe weights to the caffe model.
This is the code I used, but the data is not loaded to the lasagne model.
I check it by using the lasagne.layers.get_all_param_values(net) command, which throws this error.
Traceback (most recent call last):
File "/home/anilil/projects/pycharm-community-5.0.4/helpers/pydev/pydevd.py", line 2411, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/home/anilil/projects/pycharm-community-5.0.4/helpers/pydev/pydevd.py", line 1802, in run
launch(file, globals, locals) # execute the script
File "/media/anilil/Data/charm/mv_clean/Vgg_las.py", line 218, in <module>
x=lasagne.layers.get_all_param_values(net)
File "/usr/local/lib/python2.7/dist-packages/lasagne/layers/helper.py", line 439, in get_all_param_values
params = get_all_params(layer, **tags)
File "/usr/local/lib/python2.7/dist-packages/lasagne/layers/helper.py", line 353, in get_all_params
return utils.unique(params)
File "/usr/local/lib/python2.7/dist-packages/lasagne/utils.py", line 157, in unique
for el in l:
File "/usr/local/lib/python2.7/dist-packages/lasagne/layers/helper.py", line 352, in <genexpr>
params = chain.from_iterable(l.get_params(**tags) for l in layers)
AttributeError: 'str' object has no attribute 'get_params'
TRIAL/Test Code:-
# -*- coding: utf-8 -*-
import os
import sys
import lasagne
from lasagne.layers import InputLayer
from lasagne.layers import DenseLayer
from lasagne.layers import NonlinearityLayer
from lasagne.nonlinearities import rectify
from lasagne.layers import DropoutLayer
from lasagne.layers import Pool2DLayer as PoolLayer
from lasagne.layers.dnn import Conv2DDNNLayer as ConvLayer
from lasagne.nonlinearities import softmax
import theano as T
from lasagne.layers import LocalResponseNormalization2DLayer as LRN
sys.path.append('/home/anilil/projects/lstm/lisa-caffe-public/python/')
import caffe
from lasagne.utils import floatX
import numpy as np
def build_model():
net = {}
# Input layer
net['input'] = InputLayer((None, 3, 227, 227))
# First Conv Layer
net['conv1'] = ConvLayer(net['input'], num_filters=96,filter_size=7, pad=0, flip_filters=False,stride=2,nonlinearity=rectify)
net['pool1'] = PoolLayer(net['conv1'], pool_size=3,stride=2,mode='max')
net['norm1'] = LRN(net['pool1'],alpha=0.0001,beta=0.75,n=5)
# 2nd Conv Layer
net['conv2'] = ConvLayer(net['norm1'], num_filters=384,filter_size=5, pad=0, flip_filters=False,stride=2,nonlinearity=rectify)
net['pool2'] = PoolLayer(net['conv2'], pool_size=3,stride=2,mode='max')
net['norm2'] = LRN(net['pool2'],alpha=0.0001,beta=0.75,n=5)
# 3rd Conv Layer
net['conv3'] = ConvLayer(net['norm2'], num_filters=512,filter_size=3, pad=1, flip_filters=False,nonlinearity=rectify)
net['conv4'] = ConvLayer(net['conv3'], num_filters=512,filter_size=3, pad=1, flip_filters=False,nonlinearity=rectify)
net['conv5'] = ConvLayer(net['conv4'], num_filters=384,filter_size=3, pad=1, flip_filters=False,nonlinearity=rectify)
net['pool5'] = PoolLayer(net['conv5'], pool_size=3,stride=2,mode='max')
net['fc6'] = DenseLayer(net['pool5'], num_units=4096,nonlinearity=rectify)
net['fc6_dropout'] = DropoutLayer(net['fc6'], p=0.5)
net['fc7'] = DenseLayer(net['fc6_dropout'], num_units=4096)
net['fc7_dropout'] = DropoutLayer(net['fc7'], p=0.5)
net['fc8-ucf'] = DenseLayer(net['fc7_dropout'], num_units=101, nonlinearity=None)
net['prob'] = NonlinearityLayer(net['fc8-ucf'], softmax)
return net
if __name__=="__main__":
net = build_model()
#net= load_caffe_weights(net,'/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/deploy_singleFrame.prototxt','/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/singleframe_flow/snaps/snapshots_singleFrame_flow_v2_iter_50000.caffemodel')
caffe.set_device(0)
caffe.set_mode_gpu()
net_caffe = caffe.Net('/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/deploy_singleFrame.prototxt', '/home/anilil/projects/lstm/lisa-caffe-public/examples/LRCN_activity_recognition/singleframe_flow/snaps/snapshots_singleFrame_flow_v2_iter_50000.caffemodel', caffe.TEST)
layers_caffe = dict(zip(list(net_caffe._layer_names), net_caffe.layers))
for name, layer in net.items():
try:
layer.W.set_value(layers_caffe[name].blobs[0].data,borrow=True)
layer.b.set_value(layers_caffe[name].blobs[1].data,borrow=True)
except AttributeError:
continue
print ("Loaded the files without issues !!!!!!!!!!")
x=lasagne.layers.get_all_param_values(net)
print ("Saved Weights to the file without issues !!!!!!!!!!")
try:
x=lasagne.layers.get_all_param_values(net['prob'])
or make your net in this way:
def build_model():
net = {}
# Input layer
net = InputLayer((None, 3, 227, 227))
# First Conv Layer
net = ConvLayer(net, num_filters=96,filter_size=7, pad=0, flip_filters=False,stride=2,nonlinearity=rectify)
net = PoolLayer(net, pool_size=3,stride=2,mode='max')
....
net= NonlinearityLayer(net, softmax)
return net

Categories

Resources