Why Huggingface create_optimizer method not working? - tensorflow

How to use Huggingface create_optimizer method ?
My code is as follows:
import tensorflow as tf
from transformers import RobertaConfig, TFRobertaForMaskedLM, create_optimizer
config = RobertaConfig()
optimizer,lr = create_optimizer(1e-4,1000000,10000,0.1,1e-6,0.01)
training_loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model = TFRobertaForMaskedLM(config)
model.compile(optimizer=optimizer, loss=training_loss)
input = tf.random.uniform(shape=[1,25], maxval=100, dtype=tf.int32)
hist = model.fit(input, input, epochs=1, steps_per_epoch=1,verbose=0)
I am getting an error:
TypeError: apply_gradients() got an unexpected keyword argument
'experimental_aggregate_gradients'
I tried with tensorflow 2.3.0 and 2.2.0, transformers 3.0.2.

I added this line:
optimizer._HAS_AGGREGATE_GRAD = False

Related

How to fix type error with Keras Lambda layer

I need to embed sentence universal encoder into my keras model using Google colab.
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow import keras
def UniversalEmbedding(x):
results = embed(tf.squeeze(tf.cast(x, tf.string)))["outputs"]
return keras.backend.concatenate([results])
module_url = "https://tfhub.dev/google/universal-sentence-encoder/4"
embed = hub.load(module_url)
input_size = 1
embed_size = 512
input_1 = keras.layers.Input(shape=(input_size,), dtype=tf.string)
embed_layer = keras.layers.Lambda(UniversalEmbedding, output_shape=(embed_size,))
x1 = embed_layer(input_1)
It throws a type error as follows.
TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got 'outputs'
TF version: 2.3.0
Any hint to fix it is appreciated.

When trying to convert Keras .h5 file to .tflite file: Converter gives AttributeError: 'str' object has no attribute 'call'

The line "tflite_model = converter.convert()" gives the AttributeError: 'str' object has no attribute 'call'.
See screenshot of code ->1
CODE:
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model('///Users/theunskuhn/Desktop/Savedfile/basic_malaria_pos_neg_v3.h5')
converter.experimental_new_converter = True
tflite_model = converter.convert()
open("basic_malaria_pos_neg_v3.tflite", "wb").write(tflite_model)
ERROR:
AttributeError: 'str' object has no attribute 'call'
The Error points to the line 4: "tflite_model = converter.convert()"
Screenshot of new code from answer below
If you're using the TFLiteConverter API in TensorFlow 2.0 or above, the TFLiteConverter.from_keras_model takes in a Keras Model object and not the path of the model which is str.
First, load the model using tf.keras.models.load_model() and then pass this model to the TFLiteConverter API.
import tensorflow as tf
model = tf.keras.models.load_model( '///Users/theunskuhn/Desktop/Savedfile/basic_malaria_pos_neg_v3.h5' )
converter = tf.lite.TFLiteConverter.from_keras_model( model )
tflite_model = converter.convert()
open("basic_malaria_pos_neg_v3.tflite", "wb").write(tflite_model)
The method TFLiteConverter.from_keras_model_file() was replaced by TFLiteConverter.from_keras_model() in TF 2.0. See the docs.

How to convert Tensorflow .pb models to Tensforflow Lite

I need to convert a tensorflow pb model into tensorflow lite, by using Google CoLab.
The conversion procedures are next:
1) To upload the model:
from google.colab import files
pbfile = files.upload()
2) To convert it:
import tensorflow as tf
pb_file = 'data_513.pb'
tflite_file = 'data_513.tlite'
converter = tf.lite.TFLiteConverter.from_frozen_graph(pb_file, ['ImageTensor'], ['SemanticPredictions'],
input_shapes={"ImageTensor":[1,513,513,3]})
tflite_model = converter.convert()
open(tflite_file,'wb').write(tflite_model)
The conversion fails with the next error
Check failed: array.data_type == array.final_data_type Array "ImageTensor" has mis-matching actual and final data types (data_type=uint8, final_data_type=float).
I think I may need to specify some extra commands to overcome this error, but I can't find any information about it.
Finally found the solution. Here the snipped for others to use:
import tensorflow as tf
pb_file = 'model.pb'
tflite_file = 'model.tflite'
converter = tf.lite.TFLiteConverter.from_frozen_graph(pb_file, ['ImageTensor'], ['SemanticPredictions'],
input_shapes={"ImageTensor":[1,513,513,3]})
converter.inference_input_type=tf.uint8
converter.quantized_input_stats = {'ImageTensor': (128, 127)} # (mean, stddev)
tflite_model = converter.convert()
open(tflite_file,'wb').write(tflite_model)
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
files.download(tflite_file)

Workaround for lack of broadcast in TFLite

I would like to run a TFLite model that requires me to produce a 3d output (the sample code is a minimum example generating the error). Is there a tensorflow equivalent to gather_nd that does not reduce the dimension by one?
I've tried looking through the documentation for related functions that I can think of and haven't found a good option.
import tensorflow.compat.v1 as tf
import numpy as np
tf.disable_v2_behavior()
initial_input = tf.placeholder(dtype=tf.float32, shape=(None,5,1024))
cap_i = tf.gather_nd(initial_input, [[0,1]]) #[0,2],[0,3],[0,4],[0,5]
cap_i_broadcast = tf.broadcast_to(cap_i, [1,5,1024])
cap_iT = tf.transpose(cap_i_broadcast, perm=[0,2,1])
sess = tf.Session()
sess.run(tf.global_variables_initializer())
tf.io.write_graph(sess.graph_def, '', 'train.pbtxt')
converter = tf.lite.TFLiteConverter.from_session(sess, [initial_input], [cap_iT])
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
open('converted_model.tflite', "wb").write(tflite_model)
sess.close()
Some of the operators in the model are not supported by the standard TensorFlow Lite runtime and are not recognized by TensorFlow. If you have a custom implementation for them you can disable this error with --allow_custom_ops, or by setting allow_custom_ops=True when calling tf.lite.TFLiteConverter(). Here is a list of builtin operators you are using: GATHER_ND, TRANSPOSE. Here is a list of operators for which you will need custom implementations: BroadcastTo.
The following code has a solution using strided slice with dimensionality reduction and then reshape to get back the correct dimension.
import tensorflow.compat.v1 as tf
import numpy as np
tf.disable_v2_behavior()
initial_input = tf.placeholder(dtype=tf.float32, shape=(None,5,1024))
cap_i = tf.strided_slice(initial_input, [0,0,0], [0,5,1024], [1,1,1],
shrink_axis_mask=1)
cap_i_reshaped =tf.reshape(cap_i,[1,5,1024])
cap_iT = tf.transpose(cap_i_reshaped, perm=[0,2,1])
sess = tf.Session()
sess.run(tf.global_variables_initializer())
tf.io.write_graph(sess.graph_def, '', 'train.pbtxt')
converter = tf.lite.TFLiteConverter.from_session(sess, [initial_input],
[cap_iT])
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
open('converted_model.tflite', "wb").write(tflite_model)
sess.close()
Previously thought slice was supported in TFLite but only strided_slice is.

Error converting keras model to tfjs: duplicate weight name Variable

Follwing the tutorial at https://www.tensorflow.org/tutorials/images/hub_with_keras resulted in a file model.h5. Converting to tensorflow-js with the command
tensorflowjs_converter --input_format keras ./model.h5 /tmp/jsmodel/
failed with
Exception: Error dumping weights, duplicate weight name Variable
Why is this and how can it be fixed?
MCVE
from __future__ import absolute_import, division, print_function
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras import layers
import numpy as np
data_root = tf.keras.utils.get_file(
'flower_photos','https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
untar=True)
image_generator = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/255)
IMAGE_SHAPE = (224, 224)
image_data = image_generator.flow_from_directory(str(data_root), target_size=IMAGE_SHAPE)
feature_extractor_url = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/2" ##param {type:"string"}
feature_extractor_layer = hub.KerasLayer(feature_extractor_url,
input_shape=(224,224,3))
for image_batch, label_batch in image_data:
print("Image batch shape: ", image_batch.shape)
print("Labe batch shape: ", label_batch.shape)
break
feature_extractor_layer.trainable = False
model = tf.keras.Sequential([
feature_extractor_layer,
layers.Dense(image_data.num_classes, activation='softmax')
])
model.compile(
optimizer=tf.keras.optimizers.Adam(),
loss='categorical_crossentropy',
metrics=['acc'])
steps_per_epoch = np.ceil(image_data.samples/image_data.batch_size)
history = model.fit(image_data, epochs=2,
steps_per_epoch=steps_per_epoch) # removed callback
model.save("/tmp/so_model.h5")
This fails with a
RuntimeError: Unable to create link (name already exists)
but the model is created. Calling the above tensorflowjs_converter --input_format keras /tmp/model.h5 /tmp/jsmodel fails with the above
Exception: Error dumping weights, duplicate weight name Variable
UPDATE: see also Retrain image detection with MobileNet