Question:
I’ve scoured various tutorials on applying attention to a LSTM, but they either implement custom Attention layers, or use Keras/TF’s classes using examples that don’t relate to my study. I’m making a Bidirectional LSTM with Attention model for the sake of time series forecasting. After various attempts, I’ve landed on the following setup for which there are still problems needing ironing out:
input = Input(batch_input_shape=self.batch_input_shape)

layer = None

output = input

output = LayerNormalization(name="Normalize", epsilon=1e-7)(output)

cell = LSTMCell(

    name="LSTM",

    units=self.lstm_units,

    activation=self.activation,

    recurrent_activation=self.recurrent_activation,

    recurrent_regularizer=self.lstm_recurrent_regularizer,

    kernel_regularizer=self.lstm_kernel_regularizer,

    bias_regularizer=self.lstm_bias_regularizer,

    activity_regularizer=self.lstm_activity_regularizer,

    dropout=self.dropout,

    recurrent_dropout=self.recurrent_dropout,

)

if self.attention_type == "bahdanau":

    mechanism = BahdanauAttention(units=self.attention_units, memory=output)

else:

    mechanism = LuongAttention(units=self.attention_units, memory=output)

cell = AttentionWrapper(cell, mechanism, name="Attention", output_attention=False)

layer = RNN(cell, stateful=self.stateful, return_sequences=True)

output = Bidirectional(layer, name="Bidirectional")(output)

output = Dense(1, name="Reduce")(output)

Why do the attention mechanisms require the memory argument? According to the docs, it’s optional. The way I’ve set this up, it receives the normalized inputs whereas it should be receiving the LSTM hidden layer inputs, no?
The current error I’m receiving is:

TypeError: To be compatible with tf.eager.defun, Python functions must return zero or more Tensors; in compilation of <function while_loop..wrapped_body at 0x7f6ceb5386a8>, found return value of type <class ‘keras.engine.keras_tensor.KerasTensor’>, which is not a Tensor.

Is this even the optimal flow? Input > Normalize > LSTM > Attention > Bidirection > Dense
I’m using BahdanauAttention/LuongAttention with AttentionWrapper solely because of a Tensorflow example; but if there’s a way of using Keras’ Attention class in simpler fashion I’d be happy to learn how that works.

Context:
SnakeIsTheName:

Why do the attention mechanisms require the memory argument? According to the docs, it’s optional.

There’s an option to use this layer by passing the memory to call instead of init:

      github.com

    tensorflow/addons/blob/v0.15.0/tensorflow_addons/seq2seq/attention_wrapper.py#L212-L231

          if setup_memory:
              if isinstance(inputs, list):
                  if len(inputs) not in (1, 2):
                      raise ValueError(
                          "Expect inputs to have 1 or 2 tensors, got %d" % len(inputs)
                      )
                  memory = inputs[0]
                  memory_sequence_length = inputs[1] if len(inputs) == 2 else None
                  memory_mask = mask
              else:
                  memory, memory_sequence_length = inputs, None
                  memory_mask = mask
              self.setup_memory(memory, memory_sequence_length, memory_mask)
              # We force the self.built to false here since only memory is,
              # initialized but the real query/state has not been call() yet. The
              # layer should be build and call again.
              self.built = False
              # Return the processed memory in order to create the Keras
              # connectivity data for it.
              return self.values

The way I’ve set this up, it receives the normalized inputs whereas it should be receiving the LSTM hidden layer inputs, no?

IIUC The AttentionWrapper is implementing something similar to this decoder but wrapped up in the keras-RNN api.
“LSTM hidden layer inputs” is ambiguous to me. So I’m not 100% sure what you’re trying to do.
Remember that the AttentionWrapper architecture was designed for sequence to sequence type problems, and here it looks like you’re just using a single input sequence.
Looking at the code for AttentionWrapper, I see it’s doing what I expect: At each time step the output from the LSTM is returned (return_attention=False), and then used as the query for the attention. The output of the attention is stacked with the input of the next timestep.

      github.com

    tensorflow/addons/blob/v0.15.0/tensorflow_addons/seq2seq/attention_wrapper.py#L1986

          Returns:
            A tuple `(attention_or_cell_output, next_state)`, where:

  - `attention_or_cell_output` depending on `output_attention`.
            - `next_state` is an instance of `tfa.seq2seq.AttentionWrapperState`
               containing the state calculated at this time step.

Raises:
            TypeError: If `state` is not an instance of `tfa.seq2seq.AttentionWrapperState`.
          """
          if not isinstance(state, AttentionWrapperState):
              try:
                  state = AttentionWrapperState(*state)
              except TypeError:
                  raise TypeError(
                      "Expected state to be instance of AttentionWrapperState or "
                      "values that can construct AttentionWrapperState. "
                      "Received type %s instead." % type(state)
                  )

# Step 1: Calculate the true inputs to the cell based on the

So the LSTM is seeing the input sequence both from the current time’s direct input and its attention over the whole input sequence.

TypeError: To be compatible with tf.eager.defun… <function while_loop… found return value of type <class ‘keras.engine.keras_tensor.KerasTensor’>, which is not a Tensor.

I think what this is trying to tell you is that you can’t use one of those RNN components with the keras-functional interface. Which line throws that error? Is it the RNN line?
Try building that as a Layer subclass instead of a keras-functional model.

Is this even the optimal flow? Input > Normalize > LSTM > Attention > Bidirection > Dense
I’m using BahdanauAttention/LuongAttention with AttentionWrapper solely because of a Tensorflow example; but if there’s a way of using Keras’ Attention class in simpler fashion I’d be happy to learn how that works.

Transformers work by applying attention layers without the RNNs. You can definitely stack Transformer and LSTM layers. Maybe that’s more inline with what you’re trying to do.
This tutorial implements the transformer layer from scratch:

      TensorFlow

Transformer model for language understanding  |  Text  | ...

But I should update it to use the new MultiHeadAttentionLayers from keras:

      TensorFlow

tf.keras.layers.MultiHeadAttention  |  TensorFlow Core v2.7.0

  MultiHeadAttention layer.


Question:
In tf.keras.layers.RNN 1, the documentation states:

Note on using statefulness in RNNs: You can set RNN layers to be ‘stateful’, which means that the states computed for the samples in one batch will be reused as initial states for the samples in the next batch. This assumes a one-to-one mapping between samples in different successive batches.

Does this mean that if stateful = True, then states are not reset between epochs as well?

If stateful = False, is the layer’s state being reset each time the layer is called?

Context:
I think you’d have to test and find out, I can imagine arguments for either way.
Yes, the state is zero-initialized, unless you capture the state, and pass it back in on the next call. See these two docs for examples of manual state management:

      TensorFlow

Recurrent Neural Networks (RNN) with Keras  |  TensorFlow Core

      TensorFlow

Neural machine translation with attention  |  Text  | ...


Tensorflow Lite compatible gradient explainable techiniques

Question: Greetings, I’ve been looking around and I wasn’t able to find, is there any compatible gradient explanation techniques for TensorFlow Lite? I noticed that TensorFlow has a very wide options of gradient explanation, but so far, I was not able to find any for TFLite

Context:
Not what tflite supports on-device  training, most things that work in tensorflow could be exported to run in tflite, following this document:

      TensorFlow

On-Device Training with TensorFlow Lite 2


Unknown layer ERROR in importing Keras model to TensorFlow JS

Question:
Hello,
I’m pretty new to TF and just started by working around this tutorial: “pose classification in keras” 1.
I am using my own dataset for training/testing and I adjusted the model config :
model.compile(

    optimizer='adam',

    loss='categorical_crossentropy',

    metrics=['accuracy']

)

# Add a checkpoint callback to store the checkpoint that has the highest

# validation accuracy.

checkpoint_path = "weights.best.hdf5"

checkpoint = keras.callbacks.ModelCheckpoint(checkpoint_path,

                             monitor='val_accuracy',

                             verbose=1,

                             save_best_only=True,

                             mode='max')

earlystopping = keras.callbacks.EarlyStopping(monitor='val_accuracy',

                                              patience=20)

# Start training

history = model.fit(X_train, y_train,

                    epochs=200,

                    batch_size=16,

                    validation_data=(X_val, y_val),

                    callbacks=[checkpoint, earlystopping])

tfjs.converters.save_keras_model(model,'./path')

when importing the tfjs converted model with this command:
model = await tf.loadLayersModel('./path');
I get the following error :
Error: Unknown layer: SlicingOpLambda. This may be due to one of the following reasons:
1. The layer is defined in Python, in which case it needs to be ported to TensorFlow.js or your JavaScript code.
2. The custom layer is defined in JavaScript, but is not registered properly with tf.serialization.registerClass().

I’m not post processing or rescaling the layers. If you know what the issue might be and have any suggestions, I’ll much appreciate it.
Thanks,
Parvin

Context:
It seems you are using some op contained in a layer that is not supported in TensorFlow.js. I do not use Python so maybe someone who is a TF Python expert can tell you what layer is using that SlicingOpLambda and you can remove it and just use regular layers that are more common that are known to convert. After all we have many pose estimation models in TFJS that do work (MoveNet, PoseNet etc) so it seems this just needs to be changed.
Looking at your layers in the example code though you appear to have:
# Define the model
inputs = tf.keras.Input(shape=(51))
embedding = landmarks_to_embedding(inputs)

layer = keras.layers.Dense(128, activation=tf.nn.relu6)(embedding)
layer = keras.layers.Dropout(0.5)(layer)
layer = keras.layers.Dense(64, activation=tf.nn.relu6)(layer)
layer = keras.layers.Dropout(0.5)(layer)
outputs = keras.layers.Dense(len(class_names), activation="softmax")(layer)

model = keras.Model(inputs, outputs)
model.summary()

Tensorflow.js definitely supports layers for dense and dropout 1 so maybe the problem is coming from the embedding line? But that is just a guess as I am not familiar with Python code.
The other option is to write your pre/post processing in JS and then just use the TFJS api instead directly to make your model rather than trying to convert Python ones.
Also check this related post that shows the op support TFJS has:

    TensorFlow.js Op support matrix - if you are having Python model conversion issues, check this first! General Discussion

    So I have seen a few forum posts around conversion of TF Python models to TensorFlow.js format and sometimes running into an issue where the Python model uses some unsupported op.
Well, I am pleased to share a nice easy to view spreadsheet that shows the current state of op support in TensorFlow.js across the many backends we support such as CPU, WASM, WebGL, Node, WebGPU (each is like its own version of TensorFlow) for which each op needs to be written in that backend for it to be supported on…


Tensorflowjs conversion error from tflite-model-maker generated SAVED_model

Question:
Hi @khanhlvg
Thanks for your tutorials and colab notebook.
Since I was experimenting with using your android_figurine tflite model in a browser, I extracted it as SAVED_MODEL as shown below.

Because ExportFormat.TFJS is not allowed for object detection model yet…
I’ve tried to use this flow: Model maker → SAVED_MODEL → TFJS

Step1: Model maker → SAVED_MODEL (Success)
// model.export(export_dir='.', tflite_filename='android.tflite') // original code

model.export(export_dir="/js_export/", export_format=[ExportFormat.SAVED_MODEL])
!zip -r /js_export/ModelFiles.zip /js_export/

Step 2 SAVED_MODEL → TFJS (Failed)

스크린샷 2022-01-03 오후 5.33.031920×1093 212 KB

Error log
Traceback (most recent call last):
  File "/Users/judepark/Sites/TensorFlow/.direnv/python-3.6.8/lib/python3.6/site-packages/tensorflowjs/converters/wizard.py", line 619, in run
    converter.convert(arguments)
  File "/Users/judepark/Sites/TensorFlow/.direnv/python-3.6.8/lib/python3.6/site-packages/tensorflowjs/converters/converter.py", line 804, in convert
    weight_shard_size_bytes, metadata_map)
  File "/Users/judepark/Sites/TensorFlow/.direnv/python-3.6.8/lib/python3.6/site-packages/tensorflowjs/converters/converter.py", line 533, in _dispatch_converter
    metadata=metadata_map)
  File "/Users/judepark/Sites/TensorFlow/.direnv/python-3.6.8/lib/python3.6/site-packages/tensorflowjs/converters/tf_saved_model_conversion_v2.py", line 771, in convert_tf_saved_model
    metadata=metadata)
  File "/Users/judepark/Sites/TensorFlow/.direnv/python-3.6.8/lib/python3.6/site-packages/tensorflowjs/converters/tf_saved_model_conversion_v2.py", line 724, in _convert_tf_saved_model
    metadata=metadata)
  File "/Users/judepark/Sites/TensorFlow/.direnv/python-3.6.8/lib/python3.6/site-packages/tensorflowjs/converters/tf_saved_model_conversion_v2.py", line 157, in optimize_graph
    ', '.join(unsupported))
ValueError: Unsupported Ops in the model before optimization
TensorListConcatV2

Context: It seems some of the ops your TFLite model is using are not supported in TensorFlow.js. The only way to get around this error is to change the original model not to use those ops or to contribute the missing ops to the TensorFlow.js project.


StatisticsGen From examples saved in GCS?

Question:
Hello, I’ve run my TFX pipeline on VertexAI and the output from the ExampleGen component has been saved to a location in GCS - is there a way I could load those examples in to an InteractiveContext within a notebook so I can view the Facets output?
For example:
statistics_gen = StatisticsGen(examples="? examples processed and saved in GCS")
context.run(statistics_gen)
context.show(statistics_gen.outputs['statistics'])

Or, is there a way of loading the statistics that have already been emitted by the pipeline?
It looks like the stats were saved to locations:
StatisticsGen/statistics/40/Split-train
StatisticsGen/statistics/40/Split-eval

Could I use the metadata DB for this purpose?
So many questions  Thanks!

Context: The ExampleGen component outputs dataset artifacts, which are TFrecord files.  You can import a previously generated artifact by using the ImporterNode 1.  Here’s an example 2 of importing a previously generated schema, but a dataset would be similar.


Tflite-model-maker generated SAVED_MODEL is not working

Question:
This is my colab test notebook that uses @khanhlvg 's dataset and training jobs.
The only difference is that I extracted in SAVED_MODEL format.
Unfortunately, this saved model doesn’t work…
and it also unable to convert to TFJS as you can see #6824 2
Where did I go wrong? please let me know.

      colab.research.google.com

Google Colaboratory 1

Context:
Is your ultimate goal to use the object detection model in TF.js? If that’s the case, please export the model as TFLite and use the TFLite Task Library for TF.js to run the model.

      js.tensorflow.org

TensorFlow.js

  A WebGL accelerated, browser based JavaScript library for training and deploying ML models


How to implement and use a Linear Chain CRF in TensorFlow?

Question:
Hi, I’m currently working on my first machine learning project - using neural networks to try and syllabify words using the Moby Hyphenator II 2 dataset.
I am treating this as a multi-label classification problem in which words and their syllables are encoded in the following format:
t e n - s o r - f l o w
0 0 1   0 0 1   0 0 0 0

I have been padding all inputs to a length of 15 characters, so tensorflow would be encoded as 001001000000000.
I need to implement a linear chain conditional random field as my classifier because the online guides that I have based my project around suggest that its inclusion can greatly boost accuracy - this guide achieves 96.89% validation accuracy after hyperparameter tuning without one, but this model achieves near 100% accuracy when including a Linear Chain CRF output layer.
I have seen a guide 1 that implements a linear chain CRF in PyTorch, but I am unsure as to how to recreate this in TensorFlow. This guide also includes special characters which are checked for in order to avoid padding being included in the computations, but this isn’t a problem that I am currently concerned with - my main problem is being able to implement a linear chain CRF in Tensorflow as the final output layer.
I looked at the official TensorFlow CRF layer implementation 2 as well as the TFA module but I have no idea as to how to use them with the form of data that I have nor do I understand which specific functions to use. The second example model I referenced uses this CRF implementation 1 but I again do not know how to use it - I tried to use it in my model as per the comment in the code:
    # As the last layer of sequential layer with
    # model.output_shape == (None, timesteps, nb_classes)
    crf = ChainCRF()
    model.add(crf)
    # now: model.output_shape == (None, timesteps, nb_classes)

However, using this leads to an output shape of (None, 15, 64) - this is different from my currently working dense output layer applied after global max pooling which has an output shape of (None, 15) and I am unsure of how to remedy this, as I believe that I need the output shape to be (None, 15) for the model to work.

Context:
Have you checked:

      TensorFlow

tfa.layers.CRF  |  TensorFlow Addons 7

  Linear chain conditional random field (CRF).

We have now a new codeowner for CRF


TFX: CsvExampleGen does not work with simply example? (help)

Question:
Hi there!
I am trying out TFX pipelines, and the first step is to ingest data. In this simple example I am ingesting data from a local (google drive) csv file with CsvExampleGen, following the book "Building Machine Learning Pipelines, publish by O’reilly. However, it has proven difficult to simply import the csv-file. I don’t get an error, but the artifact is empty.
Here is the code snipped:
from tfx.components import CsvExampleGen

os.chdir("/content/gdrive/MyDrive/TFXnotebooks")
base_dir = os.getcwd()
data_dir = os.path.join(base_dir, "data/")

context = InteractiveContext()
example_gen = CsvExampleGen(input_base= data_dir)
context.run(example_gen)

I’ve checked with os.listdir(data_dir) that the file is indeed there.
Note: I know in many sources it says to import external_input from tfx.utils.dsl_utils, but this method is not supported any longer (the module doesn’t exist anymore).
Hope someone can help,
Thanks

Context: Maybe @Robert_Crowe might be able to help here


Question:
I want to apply cartoon effect to picture in android (java/kotlin or c/c++) with tensorflow lite. (tflite model / dr, fp16, int8 quantization)
I found this tutorial.
This code is also based on it.
My demo app works on CPU with 4 threads but when I want to test it with GPU delegate gives an error. 1
I used several devices and the result was the same in all of them.
// Check if device supports GPU delegate

val compatList = CompatibilityList()

val isSupported = compatList.isDelegateSupportedOnThisDevice

// If device does not support, then it will be done on 4 CPU threads

val options = Model.Options.Builder().apply {

if(isSupported) setDevice(Model.Device.GPU)

else setNumThreads(4)

}.build()

val model = WhiteboxCartoonGanFp16.newInstance(this, options)

val process = model.process(picture)

val cartoon = process.cartoonizedImageAsTensorImage

val bitmap = image.bitmap

model.close()

Context:
Hi @soiava ,
Have you tried the specific code here without ml model binding feature? Please try it and see if that initialize the delegate correctly.  It also has an additional step of finding the best options for specific device. (Use both dependencies in build.gradle file and change their versions to 2.7.0)
Get back here with the results.
Best


Question:
I need to make a color sensor. The input is 6 wavelengths of color data (6 values for each measurement) and I want to compute the corresponding CIELAB value (consists of 3 output variables) from these 6 values. For training I can have it scan color swatches with known CIELAB value. We could make a csv table to feed the training. In days past I would have used a neural net but I was wondering if this could be down in tensorflow lite? Would also be a learning experience for bigger things. No experience with it yet but very proficient in arduino and python.
Thanks for any advice.

Context:
Hi Vijay & welcome to the TF Forum!
TensorFlow is a pretty extensive computation platform that’s designed for deep learning tasks, e.g. neural networks, so you can certainly implement your neural net in TF. TensorFlow Lite is a mobile/edge-optimised runtime for TensorFlow models, so you can run your TensorFlow neural network or other model on a mobile or IoT device.
You can train a model using TF Lite, but it’s typically faster to train your network on a workstation or cloud service with more compute power (using “normal” TensorFlow), and then use TF Lite to run your model on your Arduino. The guide explains how to convert a model to use with TF Lite.
If you are looking for a pre-existing model to start using without training, check out TF Hub for some solutions in common problem domains.


Checking if a URL exists or not during dataset construction

Question:
Say I have the following URLs:
[
	'http://media.rightmove.co.uk/148k/147518/58718876/147518_SWO160154_EPCGRAPH_01_0000_max_135x100.png',
	'https://thumbs.ebaystatic.com/images/g/DYEAAOSwMHdXR0Vh/s-l225.jpg',
	'https://farm1.staticflickr.com/784/40182677504_27d67600f3_o.jpg',
	'https://t2.ftcdn.net/jpg/00/58/35/35/240_F_58353522_3plS29kylx1KZQ0lU6pYHuCAhUINvCSp.jpg',
	'https://findingblanche.files.wordpress.com/2013/07/photo4-1.jpg?w=764&',
]

I am constructing a tf.data.Dataset object with tf.data.Dataset.from_tensor_slices() with this list. Now, is it possible to filter out the URL whose status code is 404 (requests.get(url).response_code) during within the data pipeline itself?

Context:
May I additionally ask if there is also a possibility to filter out those above URLs,
where the robots.txt, explicitly disallow’s my user-agent?


Possible to interleave reading files when using `tf.data`?

Question:
I have the following data reading utility:
def read_files(image_path, text_path):
    image = tf.io.read_file(image_path)
    image = tf.image.decode_jpeg(image, channels=3)
    image = tf.image.resize(image, IMG_SZ, antialias=True)

    text = tf.io.read_file(text_path)
    text = tf.compat.as_str_any(text)
    return image, text

This is how I am constructing the dataset:
dataset = tf.data.Dataset.zip((image_ds, text_ds))
dataset = dataset.map(read_files, num_parallel_calls=AUTO).cache()

Is there a way to interleave the read_files() function?

Context: Do you mean: tf.data.Dataset.zip((image_ds.map(read_img), text_ds.map(read_text)))?


Question:
Hi, I have tried to replace my old TF2 training notebook with Tflite-model-maker. And I would like to question some of the limitations I found in this process.

Does the model maker’s object_detector API not yet support the moblenet_v2 model?
According to model_spec 1, It only supports efficientdet-lite models right now. Is there any way to use resNet or mobileNetV2 manually?

How can I use DataLoader by uploading my Record files directly to Google Colab?
I tried several times with reference to this guide 1, but I actually failed to load the data. Are there any examples for me?

The object_detector cannot be exported to TFJS format?
 Unlike text_classifier, the object_detector doesn’t support ExportFormat.TFJS. Is it right? Or Are there any options to export it?

Context:
colab.research.google.com

Google Colaboratory 1

this is the Colab notebook that I’ve tried to train a model with #tflite-model-maker


Question:
I am getting the following error when training a custom model:
W tensorflow/core/kernels/data/model_dataset_op.cc:205] Optimization loop failed: Cancelled: Operation was cancelled

Has anyone seen this error before? My code is a bit of a mess right now so hard to share any specifics. So I am not looking for a concrete solution, just some pointers on what could cause this so I can debug further. Right now I don’t even know what could possibly cause this error/warning.

Context: Generally I suggest you to use tags on new thread cause specialized technical team members could be subscribed only on tag subset (e.g. in this case tfdata and help_request tags).


Question: I might have missed it but is there going to be GNN released for JavaScript any time soon?

Context: This is a good question. AFAIK I am unware of any timeline for this in the immediate future but maybe @pyu or @lina128 knows if that is in the pipeline or not for 2022.


Question:
I want to use this:

      github.com

    examples/tensorflow_examples/lite/model_maker at master · tensorflow/examples 7

  master/tensorflow_examples/lite/model_maker

  TensorFlow examples. Contribute to tensorflow/examples development by creating an account on GitHub.

with this:

      Apple Developer

Tensorflow Plugin - Metal - Apple Developer 5

  Find presentations, documentation, sample code, and resources for building macOS, iOS, and tvOS apps with the Metal framework.

Is this possible? I want the brevity/ease of use of tflite-model-maker with the acceleration capabilities of my M1 MacBook Air running macOS 12.0.
I successfully completed the step to install tensorflow-macos, which to my understanding is just a special version of tensorflow 2.5 to take advantage of the M1 hardware acceleration.
Now, when I try to follow the tflite-model-maker example this happens:

Screen Shot 2021-08-25 at 9.47.25 PM2878×810 353 KB

I expected an error because in the requirements.txt, tensorflow==2.6.0 is specified (not tensorflow-macos==2.5.0).
Could I just edit that requirements file to specify tensorflow-macos=2.5.0 instead of tensorflow==2.6.0? Or is there an older version of tflite-model-maker that will work with tensorflow-macos==2.5.0?
I assume this is what is also causing the tflite-support dependency problem.

Context: @Yuqi_Li or @Tian_LIN ight be able to help here


How can I overlay numbers of RGB of an image on the image itself?

Question:
Any available sources or codes to help me do this automatically?
The result will be like a bitmap with the image itself.

Context:
Hi @curcur
welcome to the community. You could use annotated heatmaps from matplotlib 1 for example.
In the example you can also find a nice helper function to adust the text color contrast (white font on white background)

image1490×1116 318 KB

(Creating annotated heatmaps — Matplotlib 3.5.1 documentation 1)
Hope that helps in a nutshell,
Dennis


Tensorflow Developer Certificate Preparation

Question:
Hi everyone!
I recently joined the forum and I’m preparing for the Tensorflow Developer Certificate exam. I looked around the internet and found some blogs and videos regarding this exam. I am currently going through the Tensorflow Developer Professional Certificate course on Coursera. I’m planning to take the test next month.
I am creating this thread so that people can share the resources and their experience at a single place. I will be updating this post with my experience of the exam soon!

Context:
I am tagging people I know who have passed this exam and written about it.
@Sayak_Paul
@rishit_dagli
Request you to share your experience and guidance for this exam. And tag more people who have passed this exam!


Tflite-model-maker training error (custom object detection)

Question:
Hi, I’ve tried to train custom object detection model via tflite-model-makers referred to @khanhlvg 's Train a custom object detection model using your data 2
Unfortunately, I met the following error, and I don’t know how to solve it. Is there anyone who can give me the Christmas present?

      colab.research.google.com

Google Colaboratory 6

InvalidArgumentError: 2 root error(s) found.
  (0) INVALID_ARGUMENT:  assertion failed: [ERROR: please increase config.max_instances_per_image] [Condition x < y did not hold element-wise:] [x (parser/strided_slice_17:0) = ] [129] [y (parser/assert_less/y:0) = ] [100]
	 [[{{node parser/assert_less/Assert/Assert}}]]
	 [[MultiDeviceIteratorGetNextFromShard]]
	 [[RemoteCall]]
	 [[IteratorGetNext]]
	 [[batch_cls_loss/write_summary/summary_cond/then/_4138/batch_cls_loss/write_summary/ReadVariableOp/_94]]
  (1) INVALID_ARGUMENT:  assertion failed: [ERROR: please increase config.max_instances_per_image] [Condition x < y did not hold element-wise:] [x (parser/strided_slice_17:0) = ] [129] [y (parser/assert_less/y:0) = ] [100]
	 [[{{node parser/assert_less/Assert/Assert}}]]
	 [[MultiDeviceIteratorGetNextFromShard]]
	 [[RemoteCall]]
	 [[IteratorGetNext]]

Context:
I googled and found this issue but I have no I idea how i use it on TF lite model maker env.

      github.com/facebookresearch/detectron2

      Maximum number of instances per image 6

        opened

        Mar 13, 2020

          closed

        Mar 13, 2020

          EBJames

    Hi,

I'm using maskrcnn in Detectron2  to count the number of bacteria in an i…mage using a custom dataset, and the tutorial jupyter notebook and I've noticed that for every image I use I get a maximum of 100 instances. Is this a strange coincidence, or does detectron2/mask max out at 100 instances per image?

Cheers,

Ed


Beginner try to test installed Tensorflow

Question:
(tensorflow) C:\Users\kamil\Documents\TensorFlow\models\research>python object_detection/builders/model_builder_tf2_test.py
2021-11-10 09:17:25.209316: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudart64_110.dll
Traceback (most recent call last):
  File "C:\Users\kamil\Documents\TensorFlow\models\research\object_detection\builders\model_builder_tf2_test.py", line 25, in <module>
    from object_detection.builders import model_builder
  File "C:\Tensorflow\models\research\object_detection\builders\model_builder.py", line 35, in <module>
    from object_detection.meta_architectures import ssd_meta_arch
  File "C:\Tensorflow\models\research\object_detection\meta_architectures\ssd_meta_arch.py", line 32, in <module>
    from object_detection.utils import visualization_utils
  File "C:\Tensorflow\models\research\object_detection\utils\visualization_utils.py", line 29, in <module>
    import matplotlib; matplotlib.use('Agg')  # pylint: disable=multiple-statements
ModuleNotFoundError: No module named 'matplotlib'

(tensorflow) C:\Users\kamil\Documents\TensorFlow\models\research

Please help me to fix it!

Context:
This is my bag:
(tensorflow) C:\Users\kamil\Documents\TensorFlow\models\research>python object_detection/builders/model_builder_tf2_test.py
2021-11-10 09:17:25.209316: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudart64_110.dll
Traceback (most recent call last):
  File "C:\Users\kamil\Documents\TensorFlow\models\research\object_detection\builders\model_builder_tf2_test.py", line 25, in <module>
    from object_detection.builders import model_builder
  File "C:\Tensorflow\models\research\object_detection\builders\model_builder.py", line 35, in <module>
    from object_detection.meta_architectures import ssd_meta_arch
  File "C:\Tensorflow\models\research\object_detection\meta_architectures\ssd_meta_arch.py", line 32, in <module>
    from object_detection.utils import visualization_utils
  File "C:\Tensorflow\models\research\object_detection\utils\visualization_utils.py", line 29, in <module>
    import matplotlib; matplotlib.use('Agg')  # pylint: disable=multiple-statements
ModuleNotFoundError: No module named 'matplotlib'

(tensorflow) C:\Users\kamil\Documents\TensorFlow\models\research>

The instruction which I used step by step:
https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html#cuda-install 5
Yjank you in advance!


Add “FlexVarHandleOp” operator to tesnorflow-lite interpretrer (C++/Arm)

Question:
Hello TF experts!
I’m trying to run a TF-Lite model (Armv7, Linux, C++) converted from a ONNX/TF model.
I already did it for a simple DNN, but since I’m trying a LSTM I get errors at runtime.
I have been able to convert the model to TF-Lite by following these instructions: Select TensorFlow operators | TensorFlow Lite 3
I have also updated the TF-Lite library to compile with content of “tensorflow/lite/delegates/flex” folder, but I still get this error at runtime:
“ERROR: Regular TensorFlow ops are not supported by this interpreter. Make sure you apply/link the Flex delegate before inference.
ERROR: Node number 1 (FlexVarHandleOp) failed to prepare.”
I’m not building TF-Lite with Bazel (specific build env), maybe there are some additionnal things to do?
Many thanks for any advice onn my issue,
Kind regards,
Nicolas

Context:
Thanks for your advice but unfortunately what I’m trying to do seems to be unsupported.
Building tflite lib with TF operators is not supported when using CMake build system.
It’s explained here: Build TensorFlow Lite for ARM boards 5
I didn’t found it at first because of my research keywords :-\ … but the only answer to my problem is to use Bazel, no other options.


Model prediction (real-time) in Docker became slower after some time

Question:
Hello,
I have deployed my model in the server with the docker container(with GPU support) which is a real-time prediction application. But after some time like 1 or 2 hours, the real-time prediction is becoming slow.  Any suggestion?

Context: Are you using Tensorflow serving?


Question:
@tensorflow_team
I’ve been reading up on TFX and wanted to make a pipeline for object detection, using an “off-the-shelf” model from Model Hub or the object detection API. I’ve also had a look at the TF Lite model builder.
But (as far as I can see) all of these options has a snag that makes it unusable in the context of a TFX pipeline.
The closest I’ve gotten is with a Efficientdet feature-vector from Model Hub:

      gitlab.com

    smedegaard/objdet_ml_pipeline/-/blob/trunk/pipeline/trainer.py#L80 7

My question:
Is there an easy way to retrain a Tensorflow object detection model in a TFX pipeline at this moment in time?
regards,
Anders

Context:
Here is the sample example on image classification for TFX.
Train and serve a TensorFlow model with TensorFlow Serving 9


Raspberry pi zero suitable for tensorflow lite? illegal instruction error

Question:
I finally managed to install tensorflow lite incl. opencv on my raspberry pi zero w (v.1.1) with bullseye OS and python3. When I try to run the example recommended on this site for Raspberry PI (called classify.py) but also when I try to run the example label_image.py I get an error message “illegal instruction”, with no further details.
Through the use of print statements in the python files, I manage to narrow it down to the statement
“from tflite_runtime.interpreter import Interpreter”. That is when the error occurs (in the file image_classifier.py)
When I type this import statement manually at a python prompt, I also get the same error message.
I have a suspicion that the PI Zero is just not suitable to run tf lite due to limited processor capabilities? Is that correct? Do I need a more powerful Raspberry PI?
Or is there another explanation (or even better: a solution!) ? How can I debug further?
In the end, my goal is to do some object recognition to control the direction of a small robot.
Thanks

Context:
some additional info:
" import tflite_runtime" does not trigger an error
" import tflite_runtime.interpreter" does trigger the same error as reported in the original post above
so, with some further research. it looks to be an error related to namespaces (but I am not an expert in how to use the import statements)
Could it be because I installed opencv separately before running the tensorflow lite installation instructions (which also seems to install opencv in the setup.sh shell script)?
I’ll start from scratch (again, already spent 3 days …)  but I hope someone can shed some light on this issue.


Question:
Hi,
I was wondering if it possible/how easy it would be to implement a TFX pipeline (on a real dataset, with 100+ GB dataset, not a tutorial with a small dataset) in AWS?
For the orchestration, I might use Kubeflow. But I suppose, the major issue would be setting up a proper scalable runner for the Apache Beam. I am thinking of using Apache Flink for that.
Anyone with experience doing it? How would you go about putting a TF in production in AWS in general when you need to train the model on a regular basis on new data, do you write the pipeline from scratch or use some tool?
Thank You,
Gorjan Todorovski

Context: Generally I suggest you to select tags and category from the menu on a new thread cause specialized technical team members could be subscribed only to a tag subset (e.g. in this case I suggest tfx tag)


Generalizing function from matrices to tensors

Question:
Hi everyone. This is my first post on this forum and I am relatively new to Tensorflow. As per the title, my overall goal is to generalize a function that works with regular numpy arrays to a function that works with tensors. The long term goal is to use this function as part of a custom loss function but that is a different topic.
From my own debugging sessions, I think my issue is that I do not know how to write the form exp^{ix} . Using numpy, I could do something like: np.exp(1j*x), where x is an array of reals. When I tried to do something similar for tensors, I get an error: TypeError: Cannot convert 1j to EagerTensor of dtype double.
Any comments and help is greatly appreciated! Thanks for your time.
PS: I tried to attach a Jupyter Notebook containing the code I am working with but it seems I am not allowed to do so. What’s the best way to share my code and any relevant files needed to run it?

Context:
Do you mean something like:
import tensorflow as tf
x = tf.constant([2.0, 8.0],dtype=tf.complex64)

tf.math.exp(1j*x)


How to use Tensorflow.js for Discord.js chat bot

Question:
Hi so i have a working Discord.js bot where i setup some arrays with responses that gets randomly selected and send to the channel if the server when a user mentions his name.
Since this approach is not accurate i wanted to implement Tensorflow.js so my bot’s replies fit what he was asked to or said to in the first place.
TL;DR
I’d need to know how to install tensorflow and or how to use it in my discord bot. I want to train it with custom responses so i can give my bot a custom personality.

Context:
Hello there! Thank you for your question  Sorry for the delayed reply due to the weekend and holiday season.  This sounds like a really great project!  You may want to follow my TensorFlow.js Spam detection tutorial. Even though it is for spam, it may help you understand how to use for other things too such as knowing what sentences refer to the bot vs not etc.
Start with this one:

      Google Developers

TensorFlow.js: Build a comment spam detection system  |  Google... 5

  In this codelab, you’ll learn how to build a simple web page that has commenting ability akin to a blog post article and integrate it with a pre trained machine learning model to detect comment spam posts, enabling you to filter these out before they...

Then try this one to learn how to retrain a Python model and convert it to JS:

      Google Codelabs

TensorFlow.js: Retrain a comment spam detection model to handle edge cases... 1

  In this codelab you’ll learn how to use Model Maker to retrain a comment spam model to account for edge cases the pre-trained model could not handle, and then re-deploy the new model to the web application.

Using these tutorials you can train a model to learn if a sentence is bot related or not for example and then reply accordingly.
We also have a pre-trained model for toxicity detection so if someone is being rude on the Discord server you could send them a warning or such. I would always get a human to verify vs plain blocking right away as no ML system is perfect 100% of the time but at least you can auto flag the message for moderation or such.

      github.com

    tfjs-models/toxicity at master · tensorflow/tfjs-models 1

  master/toxicity

  Pretrained models for TensorFlow.js. Contribute to tensorflow/tfjs-models development by creating an account on GitHub.

For more advanced stuff you may want to check out our Universal Sentence Encoder here:

      github.com

    tfjs-models/universal-sentence-encoder at master · tensorflow/tfjs-models

  master/universal-sentence-encoder

  Pretrained models for TensorFlow.js. Contribute to tensorflow/tfjs-models development by creating an account on GitHub.

There is a nice introduction by Yannick who used to be on the TensorFlow.js team here that shows how to use it in a real world use case for understanding language:
Machine Learning Magic for Your JavaScript Application (Google I/O'19)

And finally feel free to come hang out on discord server too given you are probably a discord user  The TensorFlow.js working group use this to keep in touch between our monthly meetings but some content folk share is useful to all (events, new demos, etc) to just keep in touch


Question:
I am trying to freeze a model I trained based on a model from the Tensorflow Model Zoo but cannot figure out how to.

Things I have tried:

Used exporter_main_v2.py (models/exporter_main_v2.py at master · tensorflow/models · GitHub), as outlined in: TFODCourse/2. Training and Detection.ipynb at main · nicknochnack/TFODCourse · GitHub.
This caused the warning:
WARNING:tensorflow:Skipping full serialization of Keras layer <keras.layers.core.lambda_layer.Lambda object at 0x7fa3b11775d0>, because it is not built.
To be printed numerous times and the final result wasn’t readable when I tried to pass it into:
cv.dnn.readNetFromTensorflow

Tried to use freeze_graph.py, however one of the required parameters is the output node and I am unable to figure out how to get the output nodes from the model. Do note the model also does not have a .meta file. Here is a an image of the model’s directory structure for reference:

Model Used:
ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8
Training Script Used:
model_main_tf2.py

Image of directory:

Capture555×512 19.7 KB

freeze: contains the result of running exporter_main_v2.py
pre-trained: contains the model downloaded from tf2 model zoo
trained: contains the result of running the mode_main_tf2.py training script

If there is anymore information you need please let me know, I am new to this.

Context: Your model contains Lambda layer. The documentation 2 says that this layer is meant for quick experimentation and is likely to cause serialization problems. Try to subclass tf.keras.layers.Layer and implement the same logic there instead of Lambda.


Model zoo TF1 frozen graph conversion fails or creates a .tflite file that cannot be used inside android

Question:
Hi community,
I have an issue with tf1 object detection models at model zoo 1. Especially I want to convert faster_rcnn_inception_v2_coco. Inside the folder if you download it there is a saved_model and a frozen_inference_graph.pb

With the saved model I use a concrete function to convert it:

model = tf.saved_model.load('/content/faster_rcnn_inception_v2_coco_2018_01_28/saved_model')
concrete_func = model.signatures[
tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY]
concrete_func.inputs[0].set_shape([1, 300, 300, 3])

converter = tf.lite.TFLiteConverter.from_concrete_functions([concrete_func])
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]

tflite_model = converter.convert()

with open('detect2.tflite', 'wb') as f:
  f.write(tflite_model)

the .tflite file is generated without errors but it seems that it throws an error inside android:
java.lang.RuntimeException: java.lang.IllegalArgumentException: Internal error: Cannot create interpreter: Did not get operators or tensors in subgraph 1.

With the frozen graph the conversion procedure fails:

converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph(
    graph_def_file = '/content/faster_rcnn_inception_v2_coco_2018_01_28/frozen_inference_graph.pb',
    input_arrays = ['image_tensor'],
    output_arrays = ['detection_boxes', 'detection_classes', 'detection_scores', 'num_detections']
)

converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]

tflite_model = converter.convert()

with open('detect.tflite', 'wb') as f:
  f.write(tflite_model)

ConverterError: Merge of two inputs that differ on more than one predicate {s(SecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/PadOrClipBoxList/Greater:0,else), s(SecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/PadOrClipBoxList/cond/cond/pred_id/_390__cf__396:0,then), s(SecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/PadOrClipBoxList/cond/Greater/_389__cf__395:0,then)} and {s(SecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/PadOrClipBoxList/Greater:0,else), s(SecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/PadOrClipBoxList/cond/cond/pred_id/_390__cf__396:0,else), s(SecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/PadOrClipBoxList/cond/Greater/_389__cf__395:0,else)} for node {{node SecondStagePostprocessor/BatchMultiClassNonMaxSuppression/map/while/PadOrClipBoxList/cond/cond/Merge}} Failed to functionalize Control Flow V1 ops. Consider using Control Flow V2 ops instead.
I use colab and TensorFlow version is 2.7.0
Does anyone know how to overcome the above error or a different procedure for successful conversion of the tf1 files of model zoo?
Thanks in advance.

Context:
UPDATE
I have managed to overcome 2nd error: Failed to functionalize Control Flow V1 ops. Consider using Control Flow V2 ops instead …by using enable_control_flow_V2 during loading the checkpoint files in TensorFlow version 1.15 and converting to frozen_graph.pb as below:
import os, argparse

# The original freeze_graph function
# from tensorflow.python.tools.freeze_graph import freeze_graph

# dir = os.path.dirname(os.path.realpath(__file__))

def freeze_graph(model_dir, output_node_names):
    """Extract the sub graph defined by the output nodes and convert
    all its variables into constant
    Args:
        model_dir: the root folder containing the checkpoint state file
        output_node_names: a string, containing all the output node's names,
                            comma separated
    """
    if not tf.gfile.Exists(model_dir):
        raise AssertionError(
            "Export directory doesn't exists. Please specify an export "
            "directory: %s" % model_dir)

    if not output_node_names:
        print("You need to supply the name of a node to --output_node_names.")
        return -1

    # We retrieve our checkpoint fullpath
    checkpoint = tf.train.get_checkpoint_state(model_dir)
    input_checkpoint = checkpoint.model_checkpoint_path

    # We precise the file fullname of our freezed graph
    absolute_model_dir = "/".join(input_checkpoint.split('/')[:-1])
    output_graph = absolute_model_dir + "/frozen_model_V2.pb"

    # We clear devices to allow TensorFlow to control on which device it will load operations
    clear_devices = True

    # We start a session using a temporary fresh Graph
    with tf.Session(graph=tf.Graph()) as sess:
        # We import the meta graph in the current default Graph
        saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)

        # We restore the weights
        saver.restore(sess, input_checkpoint)

        tf.enable_control_flow_v2()

        # We use a built-in TF helper to export variables to constants
        output_graph_def = tf.graph_util.convert_variables_to_constants(
            sess, # The session is used to retrieve the weights
            tf.get_default_graph().as_graph_def(), # The graph_def is used to retrieve the nodes
            output_node_names.split(",") # The output node names are used to select the usefull nodes
        )

        # Finally we serialize and dump the output graph to the filesystem
        with tf.gfile.GFile(output_graph, "wb") as f:
            f.write(output_graph_def.SerializeToString())
        print("%d ops in the final graph." % len(output_graph_def.node))

    return output_graph_def

freeze_graph('/content/faster_rcnn_inception_v2_coco_2018_01_28', 'detection_boxes')

Then switching to TF version 2.7.0 and converting to .tflite:
converter = tf.compat.v1.lite.TFLiteConverter.from_frozen_graph(
    graph_def_file='/content/faster_rcnn_inception_v2_coco_2018_01_28/frozen_model_V2.pb',
    input_shapes = {'image_tensor':[1,300,300,3]},
    input_arrays = ['image_tensor'],
    output_arrays = ['detection_boxes']
)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()

with open('detectV2.tflite', 'wb') as f:
  f.write(tflite_model)

BUT the first error still persists…Inside android application you get:
java.lang.RuntimeException: java.lang.IllegalArgumentException: Internal error: Cannot create interpreter: Did not get operators or tensors in subgraph 1.
Any thoughts?


Unable to get good accuracy in sequence classification

Question:
Hi,
I am new to machine learning and I am working on a problem of sequence classification.
The data in the dataset consists of sequences of shape(20,9)
9 features of length 20.
I have tried below model but I have failed to get good accuracy.
f = 32
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(units = 4*f, activation='tanh', return_sequences = True, input_shape = (window_length, 9)))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.LSTM(units = 4*f, activation='tanh', return_sequences = True))
model.add(tf.keras.layers.Dropout(0.2))
model.add(tf.keras.layers.LSTM(units = 4*f, activation='tanh'))
model.add(tf.keras.layers.Dense(units = 256, activation = 'tanh'))
model.add(tf.keras.layers.Dense(units = 64, activation = 'tanh'))
model.add(tf.keras.layers.Dense(units = 1, activation = 'sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy', TP, TN, FP, FN])
model.build()
model.summary()
history = model.fit(X_train, y_train, epochs=256, batch_size=256, callbacks=[WeightsSaver(model,1)], validation_data=(X_test, y_test))

I have also tried more deep network, CNN_LSTMs and CNN_LSTM with dense layers at the end.
I am not getting good confustion matrix. It is mostly giving me false negatives even on train set.
I have also tried to balance the data using SMOTE and SVM_SMOTE still it is giving me low accuracy.
The problems I am facing:
Without resampling: majority class prevails and very less minority class.
With resampling: I am getting many false positives on train and test data.
I have pasted the data on below kaggle page.

      kaggle.com

binary seq-classification of input_shape(20,9) 1

  sequence classification over 9 features of window length 20

Can someone expert in this field help to share experiences and possible solution or point out mistakes that I could be making.

Context:
Other activation functions (relu, swish) have proven effective.
Is return_sequences=True necessary?


Question:
below is the code that gives me error
auc = tf.compat.v1.metrics.auc(batch_y,tf.nn.sigmoid(pred))
train_auc = sess.run(auc, feed_dict= {x:batch_x, y:batch_y})
Error=
FailedPreconditionError: 2 root error(s) found.
(0) FAILED_PRECONDITION: Attempting to use uninitialized value auc_8/true_negatives
[[node auc_8/true_negatives/read
(defined at :31)
tried several options:
tf.compat.v1.disable_eager_execution()
init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer())
running short of ideas, any help?

Context: What is your TF version?


Question:
I encountered an error while executing the code on the site below.

keras-io/image_captioning.py at master · keras-team/keras-io · GitHub 2

And i  am using tensorflow version 2.6 (tensorflow-metal PluggableDevice) in m1 mac.
I can’t find a history of the same issue in m1 mac, so I leave an inquiry. Please help me.
I executed the code below.
# Fit the model
caption_model.fit(
    train_dataset,
    epochs=EPOCHS,
    validation_data=valid_dataset,
    callbacks=[early_stopping],
)

The error below occurs.

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/var/folders/bs/5zylmdxn5pg233sn6f3tkj0w0000gn/T/ipykernel_4580/3281984782.py in <module>
     36
     37 # Fit the model
---> 38 caption_model.fit(
     39     train_dataset,
     40     epochs=EPOCHS,

~/miniforge3/envs/image_captioning_tf26/lib/python3.9/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1182                 _r=1):
   1183               callbacks.on_train_batch_begin(step)
-> 1184               tmp_logs = self.train_function(iterator)
   1185               if data_handler.should_sync:
   1186                 context.async_wait()

~/miniforge3/envs/image_captioning_tf26/lib/python3.9/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
    883
    884       with OptionalXlaContext(self._jit_compile):
--> 885         result = self._call(*args, **kwds)
    886
    887       new_tracing_count = self.experimental_get_tracing_count()

~/miniforge3/envs/image_captioning_tf26/lib/python3.9/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    948         # Lifting succeeded, so variables are initialized and we can run the
    949         # stateless function.
--> 950         return self._stateless_fn(*args, **kwds)
    951     else:
    952       _, _, _, filtered_flat_args = \

~/miniforge3/envs/image_captioning_tf26/lib/python3.9/site-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs)
   3037       (graph_function,
   3038        filtered_flat_args) = self._maybe_define_function(args, kwargs)
-> 3039     return graph_function._call_flat(
   3040         filtered_flat_args, captured_inputs=graph_function.captured_inputs)  # pylint: disable=protected-access
   3041

~/miniforge3/envs/image_captioning_tf26/lib/python3.9/site-packages/tensorflow/python/eager/function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
   1961         and executing_eagerly):
   1962       # No tape is watching; skip to running the function.
-> 1963       return self._build_call_outputs(self._inference_function.call(
   1964           ctx, args, cancellation_manager=cancellation_manager))
   1965     forward_backward = self._select_forward_and_backward_functions(

~/miniforge3/envs/image_captioning_tf26/lib/python3.9/site-packages/tensorflow/python/eager/function.py in call(self, ctx, args, cancellation_manager)
    589       with _InterpolateFunctionError(self):
    590         if cancellation_manager is None:
--> 591           outputs = execute.execute(
    592               str(self.signature.name),
    593               num_outputs=self._num_outputs,

~/miniforge3/envs/image_captioning_tf26/lib/python3.9/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     57   try:
     58     ctx.ensure_initialized()
---> 59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
     60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:

InvalidArgumentError: Cannot assign a device for operation sequential/random_flip/stateful_uniform_full_int/RngReadAndSkip: Could not satisfy explicit device specification '' because the node {{colocation_node sequential/random_flip/stateful_uniform_full_int/RngReadAndSkip}} was colocated with a group of nodes that required incompatible device '/job:localhost/replica:0/task:0/device:GPU:0'. All available devices [/job:localhost/replica:0/task:0/device:CPU:0, /job:localhost/replica:0/task:0/device:GPU:0].
Colocation Debug Info:
Colocation group had the following types and supported devices:
Root Member(assigned_device_name_index_=2 requested_device_name_='/job:localhost/replica:0/task:0/device:GPU:0' assigned_device_name_='/job:localhost/replica:0/task:0/device:GPU:0' resource_device_name_='/job:localhost/replica:0/task:0/device:GPU:0' supported_device_types_=[CPU] possible_devices_=[]
RngReadAndSkip: CPU
_Arg: GPU CPU

Colocation members, user-requested devices, and framework assigned devices, if any:
  sequential_random_flip_stateful_uniform_full_int_rngreadandskip_resource (_Arg)  framework assigned device=/job:localhost/replica:0/task:0/device:GPU:0
  sequential/random_flip/stateful_uniform_full_int/RngReadAndSkip (RngReadAndSkip)

	 [[{{node sequential/random_flip/stateful_uniform_full_int/RngReadAndSkip}}]] [Op:__inference_train_function_98057]

Context:
I suggest you to post this at:
https://developer.apple.com/forums/tags/tensorflow-metal 57


Metric not available cause save_model KeyError

Question:
TensorFlow Version: 1.15.4
Keras in TensorFlow:2.2.4-tf
I’m training a keras model using fit and 4 callbacks, Here is code:
mode = 'max'
verbose = 1
ckpt_name = os.path.join(
    self.context.ckpt_path, "{epoch:03d}-{%s:.4f}.h5" % monitor_name)
callbacks_list = [
    LogCallback(log_batch_num=self.context.metric_size),
    ReduceLROnPlateau(monitor=monitor_name,
                        factor=self.context.lr_decay_factor,
                        patience=self.context.lr_decay_step,
                        cooldown=1,
                        verbose=verbose,
                        min_lr=0.0001),
    TensorBoard(log_dir=self.context.tensorboard_path, write_graph=False),
    ModelCheckpoint(ckpt_name,
                    monitor=monitor_name,
                    verbose=verbose,
                    save_best_only=True,
                    save_weights_only=True,
                    mode=mode),
    EarlyStopping(monitor=monitor_name,
                    min_delta=self.context.min_delta,
                    patience=self.context.patience,
                    verbose=verbose,
                    mode=mode,
                    baseline=self.context.base_metric,
                    restore_best_weights=True)
]

It will cause the Error

image956×326 22.5 KB

I think maybe OutOfData->Train stop->Save model->Metric not available->metric not in Logs->_get_file_path Error
I want to know how to fix it, make the training stop and save model ok even if Out of Data?

Context:
Can you try to use an updated TF/Keras version?
TF 1.x Is de-facto EOL.


Question:
I am trying to build a simple model and then compiling it. This is what I get as output:
2021-06-15 15:43:48.088419: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-06-15 15:43:53.962346: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library nvcuda.dll
2021-06-15 15:43:53.995966: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1779] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: NVIDIA GeForce GTX 1660 Ti computeCapability: 7.5
coreClock: 1.59GHz coreCount: 24 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 268.26GiB/s
2021-06-15 15:43:54.002691: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
2021-06-15 15:43:54.011579: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublas64_11.dll
2021-06-15 15:43:54.013741: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cublasLt64_11.dll
2021-06-15 15:43:54.018713: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cufft64_10.dll
2021-06-15 15:43:54.022424: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library curand64_10.dll
2021-06-15 15:43:54.028365: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusolver64_10.dll
2021-06-15 15:43:54.033437: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cusparse64_11.dll
2021-06-15 15:43:54.035969: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudnn64_8.dll
2021-06-15 15:43:54.038805: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1917] Adding visible gpu devices: 0
Num GPUs Available:  1
2021-06-15 15:50:17.508570: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-06-15 15:50:17.515800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1779] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: NVIDIA GeForce GTX 1660 Ti computeCapability: 7.5
coreClock: 1.59GHz coreCount: 24 deviceMemorySize: 6.00GiB deviceMemoryBandwidth: 268.26GiB/s
2021-06-15 15:50:17.520069: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1917] Adding visible gpu devices: 0
2021-06-15 15:50:17.886477: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-06-15 15:50:17.889673: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1310]      0
2021-06-15 15:50:17.891514: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1323] 0:   N
2021-06-15 15:50:17.893731: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1464] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4007 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce GTX 1660 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5)

I understand this means that it found my GPU and that is OK, but why doesn’t the compiler work?
This is the simple code I am running:
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28,28)),
    keras.layers.Dense(128, activation="relu"),
    keras.layers.Dense(10, activation="softmax")
    ])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)
test_loss, test_accuracy = model.evaluate(test_images, test_labels)
print("Tested accuracy: ", test_accuracy)

Context:
juzun:

why doesn’t the compiler work?

What is the error and your TF version?


Question:
Hi!
I’m currently trying to get the weights of a pre trained modle on a Arduino (Portenta). I want to use the weights for a optimisation for a controll task.
The problem i got is, that i don’t finde a way to read the weights of the modle on the Arduino out of the .cc or the .h file. Did somebody else try this yet?
I read that training on microcontrollers is new, so i hope the weights are some where readable in the file.

Context:
Hi!
I have the same problem.
Have you found a solution?


Question: I’m new to programming and installed tensorflow with my Python terminal, but I still need to make tensorflow accessible for Jupyter Notebook. How should I do that? When I try to enter any command into the Python terminal, I always get “SyntaxError: invalid syntax”

Context:
Hi @new_programmer and welcome! What is your operating system?
You might encounter an issue of having various Python installations at some point:

      xkcd.com

    Python Environment

The Python environmental protection agency wants to seal it in a cement chamber, with pictorial messages to future civilizations warning them about the danger of using sudo to install random Python packages.

To make sure you don’t have that issue in future, you may want to use virtualenv/venv - a virtual environment with an isolated version of Python (e.g. 3.x) with any compatible version of Jupyter (Jupyter Lab) and/or any other library, such as TensorFlow 2.7.
You may find the following posts helpful:

Installing TensorFlow: Your Complete Tutorial | Udacity 2 (Windows, macOS, Linux)
Using Virtual Environments in Jupyter Notebook and Python - Parametric Thoughts 2

Let us know how it goes.

Here are the official docs for virtualenv: Installing packages using pip and virtual environments — Python Packaging User Guide 2
The official TensorFlow installation page:

      TensorFlow

Install TensorFlow 2

  Learn how to install TensorFlow on your system. Download a pip package, run in a Docker container, or build from source. Enable the GPU on supported cards.


I tried to convert the model of an open source mask recognition project on GitHub into tflite, but the accuracy of the tflite model after the conversion is seriously lost. Where did I do wrong?

Question:
I am learning tensorflow, and now I am trying to convert the keras model into the tflite model. There is no error in the conversion process, but when I use the tflite model successfully converted, the value provided by it is completely wrong. Whether I wear a mask or not, it may be recognized as not wearing or wearing. This is the code of my identification part, The code of the conversion part and the code of the training part
file/d/1qPSYkdWqduojMg5NPxTOTdLAzQiZRgEp/view?usp=sharing

Context: In addition, it seems that I can’t directly publish the links of Google online disk, so I only publish the key parts. I don’t know whether this operation is in line with the rules of tensorflow community


How to implement a soft update for targetnetworks in tf?

Question:
I am following a tutorial from a youtuber where the soft update of the target networks are perfromed by this function:
    def update_network_parameters(self, tau=None):
        if tau is None:
            tau = self.tau

        weights = []
        targets = self.target_actor.weights
        for i, weight in enumerate(self.actor.weights):
            weights.append(weight * tau + targets[i]*(1-tau))
        self.target_actor.set_weights(weights)

        weights = []
        targets = self.target_critic.weights
        for i, weight in enumerate(self.critic.weights):
            weights.append(weight * tau + targets[i]*(1-tau))
        self.target_critic.set_weights(weights)

But idk if that’s the proper way. What if there are NoisyDense layers or Normalization layers? Are they updated also using this approach? Is there somewhere a proper implementation? Maybe its already somewhere in the tf lib and idk it…

Context:
Hi @Unnamed, welcome to the Forum.
yes, please feel free to have a look at the Soft Actor Critic 1 agent tf_agents.agents.SacAgent
There’s a target_update_tau factor (+ many more) for soft updates of the target network.
Also used in this colab tutorial:

      TensorFlow

SAC minitaur with the Actor-Learner API  |  TensorFlow Agents

Lucky Rewards,
Dennis


On-Device Training with TensorFlow Lite for Microcontrollers

Question:
There is a colab example (code snippet) about On-Device Training with TensorFlow Lite 2 so I wonder if there is such example for TensorFlow Lite Micro as well.
Is it possible to use this feature On-Device Training within TensorFlow Lite for Microcontrollers?

Context:
Jack_Daniels:

Is it possible to use this feature On-Device Training within TensorFlow Lite for Microcontrollers?

Thanks for the question @Jack_Daniels. Checking with the team.


How generare a tflite from a keras model

Question:
Hi,
i created my model with tensorflow and i tryed it with this
txt = ["I love a cat]
seq = tokenizer.texts_to_sequences(txt)
padded = pad_sequences(seq, maxlen=max_length)
pred = model.predict(padded)
print(labels[np.argmax(pred)-1])
and the result is goog, so now i want to try this model on my android app.
how can I pass the string to the model and do the same evaluation written above?
when i export on tflite file, the model with this commands
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
i can’t determine string type input.
do you have any ideas?
thanks a lot
Simone

Context:
Using from_keras_model() will give you a model that expects the same input that you pass to predict(), so you’ll need to do the same pre-processing.
If you’re performing text classification in your Android app, you might find the TF Lite Task Library helps. When you convert the model, you’ll need to add metadata (example here 1), and then you can use NlClassifier 1 (reference) in your app:
List<Category> results = classifier.classify("I love a cat");


How can I do with Swift to use Tensorflowlite?

Question:
// Returns a model from the provided buffer, or null on failure.
//
// NOTE: The caller retains ownership of the model_data and should ensure that
// the lifetime of the model_data must be at least as long as the lifetime
// of the TfLiteModel.
TFL_CAPI_EXPORT extern TfLiteModel* TfLiteModelCreate(const void* model_data,
size_t model_size);
I want to use this method  in swift
//================//in swift i have writer method in Model
init?(filePath: String) {
guard !filePath.isEmpty, let cModel = TfLiteModelCreateFromFile(filePath) else { return nil }
self.cModel = cModel
}
init?(modelData: UnsafeRawPointer!,modelSize:Int) {
guard let cModel = TfLiteModelCreate(modelData, modelSize) else { return nil }
self.cModel = cModel
}
//=================//
//==  in Interpreter Class  ==//
public init(modelData: UnsafeRawPointer!,modelSize:Int, options: Options? = nil, delegates: [Delegate]? = nil) throws {
  guard let model = Model(modelData: modelData, modelSize: modelSize) else { throw InterpreterError.failedToLoadModel }
  guard let cInterpreterOptions = TfLiteInterpreterOptionsCreate() else {
    throw InterpreterError.failedToCreateInterpreter
  }
  defer { TfLiteInterpreterOptionsDelete(cInterpreterOptions) }

  self.options = options
  self.delegates = delegates
  options.map {
    if let threadCount = $0.threadCount, threadCount > 0 {
      TfLiteInterpreterOptionsSetNumThreads(cInterpreterOptions, Int32(threadCount))
    }
    TfLiteInterpreterOptionsSetErrorReporter(
      cInterpreterOptions,
      { (_, format, args) -> Void in
        // Workaround for optionality differences for x86_64 (non-optional) and arm64 (optional).
        let optionalArgs: CVaListPointer? = args
        guard let cFormat = format,
          let arguments = optionalArgs,
          let message = String(cFormat: cFormat, arguments: arguments)
        else {
          return
        }
        print(String(describing: InterpreterError.tensorFlowLiteError(message)))
      },
      nil
    )
  }
  delegates?.forEach { TfLiteInterpreterOptionsAddDelegate(cInterpreterOptions, $0.cDelegate) }

  // Configure the XNNPack delegate after the other delegates explicitly added by the user.
  options.map {
    if $0.isXNNPackEnabled {
      configureXNNPack(options: $0, cInterpreterOptions: cInterpreterOptions)
    }
  }

  guard let cInterpreter = TfLiteInterpreterCreate(model.cModel, cInterpreterOptions) else {
    throw InterpreterError.failedToCreateInterpreter
  }
  self.cInterpreter = cInterpreter
}

//===========//
    let data =  try! Data.init(contentsOf: URL.init(fileURLWithPath: modelPath))
    let nsData = data as NSData

    let rawPtr = nsData.bytes

    interpreter = try Interpreter(modelData: rawPtr,modelSize: nsData.length, options: options)

//   ======  =//
run  and  it crash  ,how can i do

Context: Could you please refer to TensorFlow Lite guide 4 and let us know if it helps?Thank you


Unable to create environments file. Path not writable. Mac M1 Chip python modules

Question:
Hi I’m trying to install TensorFlow for MAC M1 Chip OS 12.0. Post installing the application and trying to install numpy and Jupiter notebook using the below command after installing Miniforge
conda install notebook - y

conda install -c conda-forge numpy -y

I get the error
Unable to create environments file. Path not writable.
  environment location: /Users/ren/.conda/environments.txt

done
Executing transaction: | WARNING conda.core.envs_manager:register_env(50): Unable to register environment. Path not writable or missing.
  environment location: /Users/ren/miniforge3
  registry file: /Users/ren/.conda/environments.txt

I went to the folder and changed the permissions of the file manually it did not work
I ran the command
775 /.conda/

from the terminal it did not work. I removed all the folders for anaconda and Miniforge multiple times I get the same error.
Any suggestions how to get this to work?
Regards,
Ren

Context: Thank you I closed the issue. I first went to apple dev forums. They directed me here. Closed it by watching you tube another person had the same issue like me


Kudos to PluggableDevices team; question about AMD GPU

Question:
Hi all, I just wanted to show my appreciation for the PluggableDevices implementation. I think it was a good compromise between expanding availability of GPU acceleration and not touching the core CUDA kernels, which would probably need a complete re-development.
In particular, I have been using the MacOS/Metal implementation and liking it very much. One question I have to take this one step further is, what are some guidelines on memory usage the experts could share? For example, in my setting I have an 8GB AMD Radeon pro 5500. When I’m setting buffer size for training my TF models, is there a rule or thumb or any other rough guideline on how I could get the most bang for the buck (in other words, more GPU acceleration for the CPU workload that sending / fetching data to and from the GPU entails).
Many thanks,
Doug

Context:
Hi Doug,
Thank you very much for the kind words! We are excited to hear that you are liking it!
@kulin_seth and his team develop the Metal plug-in 41. (They have worked really hard for the release – Kudos!) He can help answer your question.
Best,
Penporn
P.S. We have changed the tag of this thread from tfdata to pluggable_device just so it’s easier to look up all PluggableDevice-related posts.


Question:
I have a series json data like this:
[
	{
		"data": [
			{"p": 12.1, "q": 12.35, "g": 3.2},
			{"p": 12.2, "q": 12.36, "g": 3.3},
			{"p": 12.3, "q": 12.37, "g": 3.4}
		],
		"target": 0.001
	},
	{
		"data": [
			{"p": 13.1, "q": 13.35, "g": 4.2},
			{"p": 13.2, "q": 13.36, "g": 4.3},
			{"p": 13.3, "q": 13.37, "g": 4.4}
		],
		"target": 0.002
	},
	{
		"data": [
			{"p": 14.1, "q": 14.35, "g": 5.2},
			{"p": 14.2, "q": 14.36, "g": 5.3},
			{"p": 14.3, "q": 14.37, "g": 5.4}
		],
		"target": 0.003
	}
]

I am trying to read the data by Pandas and convert it to tensor like this
import pandas as pd
import tensorflow as tf

js = pd.read_json('training.txt')
ds = tf.data.Dataset.from_tensor_slices((js.datas.values, js.target.values))

But it returns an error:
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list).
So how to convert my json data to tensor?
Thank you!

Context:
Why convert it with pandas first? why not json.load?
The data field is currently a list of dictionaries. TensorFlow tries to convert lists to tensors, but it can’t put a dict n a tensor. Flip this to be a dict of str:list and this will work with json.load, IDK about pandas.


AttributeError: ‘NoneType’ object has no attribute ‘take’

Question:
I want to train an object detector based on the Train a salad detector with TensorFlow Lite Model Maker notebook 5, but I’m using a dataset in Pascal VOC format 7.
The dataset can be loaded with
dataloader = object_detector.DataLoader.from_pascal_voc('chess-detection/images', 'chess-detection/annotations', ['white-rook', 'white-knight', 'white-bishop', 'white-king', 'white-queen', 'white-pawn', 'black-rook', 'black-knight', 'black-bishop', 'black-king', 'black-queen', 'black-pawn'])

but when trying to split it into a training and validation set using the split method 5 (train_data, validation_data = dataloader.split(0.8)) im getting the following error:
AttributeError Traceback (most recent call last)

[<ipython-input-21-337838b38d0a>](https://localhost:8080/#) in <module>() ----> 1 train_data, validation_data = dataloader.split(0.8)

1 frames

[/usr/local/lib/python3.7/dist-packages/tensorflow_examples/lite/model_maker/core/data_util/dataloader.py](https://localhost:8080/#) in _split(self, fraction, *args) 145 146 train_size = int(self._size * fraction) --> 147 trainset = self.__class__(ds.take(train_size), train_size, *args) 148 149 test_size = self._size - train_size

AttributeError: 'NoneType' object has no attribute 'take'

Any help is greatly appreciated.

Context:
Hi @Gi_T
Have you checked version of TensorFlow? Is it 2.5.0? Can you try with this version?


Unable to apply image preprocessing to tf.data.Dataset

Question:
Hi,
I’m trying to apply preprocessing to tf.data.Dataset (option 2 in the Keras Preprocessing Guide: Bekerja dengan lapisan preprocessing  |  TensorFlow Core 2). I get an exception thrown when trying to read from a mapped Dataset which includes the RandomFlip preprocessing layer.
Any ideas what am I doing wrong here? code + output below.
Thanks!
import functools
import numpy as np
import tensorflow as tf

def data_gen():
    for i in range(10):
        x = np.random.random(size=(80, 80, 3)) * 255
        y = np.random.random(size=(40, 40, 1)) * 255
        yield x, y

def preprocess(image, label, cropped_image_size, cropped_label_size, skip_augmentations=False):

    x = image
    y = label

    x_size = cropped_image_size
    y_size = cropped_label_size

    if not skip_augmentations:
        x = tf.keras.layers.RandomFlip(mode="horizontal")(x)
        y = tf.keras.layers.RandomFlip(mode="horizontal")(y)

        x = tf.keras.layers.RandomRotation(factor=1.0, fill_mode='constant')(x)
        y = tf.keras.layers.RandomRotation(factor=1.0, fill_mode='constant')(y)

    x = tf.keras.layers.CenterCrop(x_size, x_size)(x)
    y = tf.keras.layers.CenterCrop(y_size, y_size)(y)

    return x, y

print(tf.__version__)
dataset = tf.data.Dataset.from_generator(data_gen, output_signature=(
    tf.TensorSpec(shape=(80, 80, 3), dtype='float32'),
    tf.TensorSpec(shape=(40, 40, 1), dtype='float32')
))

crop_only_fn = functools.partial(preprocess, cropped_image_size=50, cropped_label_size=25, skip_augmentations=True)
train_preprocess_fn = functools.partial(preprocess, cropped_image_size=50, cropped_label_size=25, skip_augmentations=False)

crop_dataset = dataset.map(crop_only_fn)
train_dataset = dataset.map(train_preprocess_fn)

it_crop = iter(crop_dataset)
ex_crop = next(it_crop)         # this works

it_train = iter(train_dataset)
ex_train = next(it_train)       # this fails

Output:
2021-12-13 16:10:15.106947: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-12-13 16:10:15.876142: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 22302 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:17:00.0, compute capability: 8.6
2021-12-13 16:10:15.877134: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:1 with 22302 MB memory:  -> device: 1, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:65:00.0, compute capability: 8.6
2021-12-13 16:10:16.720083: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at stateful_random_ops.cc:278 : NOT_FOUND: Resource localhost/_AnonymousVar3/N10tensorflow3VarE does not exist.
2021-12-13 16:10:16.720119: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at stateful_random_ops.cc:278 : NOT_FOUND: Resource localhost/_AnonymousVar1/N10tensorflow3VarE does not exist.
2021-12-13 16:10:16.720143: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at stateful_random_ops.cc:278 : NOT_FOUND: Resource localhost/_AnonymousVar0/N10tensorflow3VarE does not exist.
2021-12-13 16:10:16.720199: W tensorflow/core/framework/op_kernel.cc:1745] OP_REQUIRES failed at stateful_random_ops.cc:278 : NOT_FOUND: Resource localhost/_AnonymousVar2/N10tensorflow3VarE does not exist.
2.7.0
Traceback (most recent call last):
  File "./issue_dataaug.py", line 51, in <module>
    ex_train = next(it_train)       # this fails
  File "/.../mickey/venvs/cvi/lib/python3.8/site-packages/tensorflow/python/data/ops/iterator_ops.py", line 800, in __next__
    return self._next_internal()
  File "/.../mickey/venvs/cvi/lib/python3.8/site-packages/tensorflow/python/data/ops/iterator_ops.py", line 783, in _next_internal
    ret = gen_dataset_ops.iterator_get_next(
  File "/.../mickey/venvs/cvi/lib/python3.8/site-packages/tensorflow/python/ops/gen_dataset_ops.py", line 2845, in iterator_get_next
    _ops.raise_from_not_ok_status(e, name)
  File "/.../mickey/venvs/cvi/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 7107, in raise_from_not_ok_status
    raise core._status_to_exception(e) from None  # pylint: disable=protected-access
tensorflow.python.framework.errors_impl.NotFoundError: 2 root error(s) found.
  (0) NOT_FOUND: {{function_node __inference_Dataset_map_classfunctools.partial_972}} Resource localhost/_AnonymousVar3/N10tensorflow3VarE does not exist.
	 [[{{node random_rotation_1/stateful_uniform/RngReadAndSkip}}]]
	 [[random_rotation_1/stateful_uniform/RngReadAndSkip/_14]]
  (1) NOT_FOUND: {{function_node __inference_Dataset_map_classfunctools.partial_972}} Resource localhost/_AnonymousVar3/N10tensorflow3VarE does not exist.
	 [[{{node random_rotation_1/stateful_uniform/RngReadAndSkip}}]]
0 successful operations.
0 derived errors ignored. [Op:IteratorGetNext]
2021-12-13 16:10:16.949380: W tensorflow/core/kernels/data/generator_dataset_op.cc:107] Error occurred when finalizing GeneratorDataset iterator: FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated.
	 [[{{node PyFunc}}]]

Context: How did you install tensorflow? It looks like an operation your code requires isn’t available. I think this is usually due to a bad install or some version mismatches


Standard way of sharing architectures, e.g., to PyPi.org

Question:
There are a huge number of new statistical, machine-learning and artificial intelligence solutions being released every month.
Most are open-source and written in a popular Python framework like TensorFlow, JAX, or PyTorch.
In order to ‘guarantee’ you are using the best [for given metric(s)] solution for your dataset, some way of automatically adding these new statistical, machine-learning and artificial intelligence solutions to your automated pipeline needs to be created.
(additionally: useful for testing your new optimiser, loss function, &etc. across a zoo of datasets)
Ditto for transfer learning models. A related problem is automatically putting ensemble networks together. Something like:
import some_broke_arch  # pip install some_broke_arch
import other_neat_arch  # pip install other_neat_arch
import horrible_v_arch  # builtin to keras

model   = some_broke_arch.get_arch(   **standard_arch_params  )
metrics = other_neat_arch.get_metrics(**standard_metric_params)
loss    = horrible_v_arch.get_loss(   **standard_loss_params  )

model.compile(loss=loss, optimizer=keras.optimizers.RMSprop, metrics=metrics)
print(model.summary())
# &etc.

In summary, I am petitioning for standard ways of:

exposing algorithms for consumption;
combining algorithms;
comparing algorithms.

To that end, I would recommend encouraging the PyPi folk to add a few new classifiers, and a bunch of us trawl through GitHub every month sending PRs to random repositories—associated with academic papers—linking up with CI/CD so that they are now installable with pip install and searchable by classifier on PyPi.
Related:

my open-source multi-ML meta-framework;

uses builtin ast and inspect 1 modules to traverse the module, class, and function hierarchy for 10 popular open-source ML/AI frameworks;
will enable experimentation with entire ‘search-space’ of all these ML frameworks (every transfer learning model, optimiser, loss function, &etc.)
with a standard way of sharing architectures will be able to expand the ‘search-space’ with community contributed solutions.

this issue from Jul 20, 2019: Standard way of sharing architectures? · Issue #30896 · tensorflow/tensorflow · GitHub 2

Context:
From a more general point of view it reminds me of:
https://colinraffel.com/blog/a-call-to-build-models-like-we-build-open-source-software.html 1
The full twitter thread:

      twitter.com

Colin Raffel 1
@colinraffel

  Announcing a new research focus in my lab: Developing tools to enable collaboratively-built and continually-improved models.

Blog post: https://t.co/rdZhAFvb3J
Paper on model "patches": https://t.co/qtwlyS1Gys
Paper on "merging" models: https://t.co/Qw6nF5Nl42
Thread ⬇ (1/11)

  8:29 AM - 8 Dec 2021

      1.5K

      311


Layers not pruned after using prune_low_magnitude

Question:
I’m using a Resnet50 model

image670×742 22.3 KB

Even after the pruning step, the weights doesn’t become 0’s.
Can anyone help me?

Context: Could you please refer to this thread 2 , pruning guide and let us know if it helps? Thank you!


Question:
Tensorflow is a great library for tensor operations, covering most of the functions necessary for effective tensor manimulation. The code is extremely well optimized and it automatically scale to take advantage of the hardware available.
For these resons it’s an appealing choice for writing parallel programs, but (as a software engineer) I would need some more informations on the computational cost of each operation for implementing it effectively in the programs.
Let’s make an example. I’m currently enrolled in a master degree in artificial intelligence, a few weeks ago we were given a reinforcement learning problem were we had to write an agent able to play a certain board game. In order to get all the data for training our agent we had to play a lot of games, but the simulation engine provided was too slow. For this reason we decided to rewrite completey the simulations parallelizing it in tensorflow, and we were able to run hundreds of tousands of simulations in paralell in approximately the same time. Writing all the optimized code was not easy but it could be done.
Doing this project we realized that some of the operations were way slower than others, for example al the segment operations (segment_sum, segment_max, … ) were way slower than creating a ragged tensor and applying a reduce sum on the ragged dimension, while still doing approximately the same thing.
Or in another case, trying to implement a sparsely connected neural network, we were not able to understand if it was faster computing the product using some sparse tensor, or using gather to get the slices we needed and applying the multiplication manually on neurons we needed or other.
A complete documentation of the library, expecially one that offers such low level operations, in my opinion should not omit the computational cost of the basic operations.
I’m aware of the fact that running the fact that running the code on many different architectures will result in completely different behaviours (running on a cpu is different than running on a gpu or tpu), but still I think a complete desctiption of the behaviour should be available for the advanced users. The two main point of interest of course are the time complexity and the space complexity (becaause if the gpu crashes for out of memory the fast code is of no use) of each basic tensorflow operation, highliting the differences among the architectures.
Maybe such resource is already availabe somewere, but in all my researches I wasn’t able to find it. If it’s now I think it would be a very usefull addition, even if only for a few specialized developer and researcher

Context:
I don’t think that we have this documented but you can always check FLOPS and memory details at runtime with:

    How to find out keras model memory size? General Discussion

    You can retrieve memory info with flops from the profiler as in:

You can also try:


Question:
Hi all
So I had lots of trouble getting tensorflow to work in jupyter notebook, but after much trial and error and installing it in several different places not knowing what I was doing, I did what this guy said and it worked:
‘Solved no module named 'tensorflow'. Easiest Way to install TensorFlow For Anaconda on Windows 10. - YouTube 1’
He basically says to install tensorflow in anaconda, and then change from the base (root) environment to the tensorflow environment that we create and launch jupyter notebook from there, as shown in these images:
What I click:

image1045×386 33.6 KB

Proof that tensorflow works in jupyter notebook now:

Now I want tensorflow to work in VS Code, but following the same route as before doesn’t work. I go into the tensorflow/tf environment I created in anaconda, launch VS Code from there, open a jupyter notebook file in VS Code and it doesn’t work:
What I click:

image633×573 26.6 KB

Proof it doesn’t work:

image853×516 32.7 KB

So now that you know the story, my question is (and I’m crossing my fingers tightly hoping that someone knows how all this installing business works and why):

How do I get tensorflow to work in VS Code?

Thanks for your time
Jaime

Context:
Not sure if you have checked this, try selecting the interpreter(python env with TensorFlow) in the notebook editor, as mentioned here
Working with Jupyter Notebooks in Visual Studio Code 1


Question:
Hi everyone,
when training my model using model.fit() and using tf.data for my training and validation data the GPU usage dips to 0% after each epoch even though I am using the prefetch method for tf.data.Dataset.
Have you experienced something similar?
Sadly I cannot provide any code.
Thank you in advance.

Context:
My first two guesses would be:

The dataset needs to refill the shuffle buffer after each epoch, like model.fit(ds.shuffle(buffer_size).repeat()) instead of model.fit(ds.repeat().shuffle(), steps_per_epoch=N)

Maybe something with the evaluation logic?


Question:
I was wondering if there’s an openly available sample or demo of:

A model that trains and inferences in the browser
And updates a global model using federated learning

I was thinking of something like hand gestures model which retrains locally in the browser based on new user actions and model weights are updated on a central model.
Am I thinking it properly? Is there some demo/sample I can use as starting point? Is this even feasible?

Context:
I don’t think there are so much updates since 2019:

      github.com/tensorflow/federated

      Federated in Tensorflow.js 5

        opened

        Mar 8, 2019

          closed

        May 15, 2019

          bhack

  Will this api be exposed in Tensorflow.js?
So that you can join in the federation from a browser tab.

      github.com/tensorflow/federated

      TFF with web browser clients 3

        opened

        Dec 14, 2020

          thankrandomness

  I am hoping to use TFF to do federated learning with web-based clients from the browser environments using javascript. There was...

    contributions welcome
    enhancement


Help installation tensorflow ubuntu not return

Question:
I have problems importing tensorflow.
I followed all the documentation and I don’t have a GPU.
When I try to import tensorflow it doesn’t return the terminal to me, does anyone have any ideas?

Captura de tela de 2021-12-09 12-09-58725×142 26.3 KB

Context: Can you try to just run  import tensorflow?


A new dataset for multi-label text classification

Question:
Soumik 1 and I are pleased to share a new NLP dataset for multi-label text classification. The dataset consists of paper titles, abstracts, and term categories scraped from arXiv. Find the dataset on Kaggle: arXiv Paper Abstracts | Kaggle 2.
We are also releasing our data collection pipeline which is based on Apache Beam that can be run on Cloud Dataflow (GCP) at scale and can be used to accumulate an even bigger dataset at ease: multi-label-text-classification/beam_arxiv_scrape.ipynb at master · soumik12345/multi-label-text-classification · GitHub 3.
To help the community get started quickly we have authored this blog post on keras.io that shows how to build a simple baseline model for a smaller version of the dataset: Large-scale multi-label text classification 3. Thanks to @fchollet @mattdangerw for all the help.
It would be great to see this dataset also being included in tfds.

Context: Thank you so much for sharing this one with us…


TensorFlow Probability log_prob clarification

Question:
Hi Folks,
I’m a newbie when it comes to TensorFlow Probability and could use some expert advice / guidance.  I’d like to understand the math behind log_prob for a normal distribution with batch_shape=[3] and event_shape = [2].  When 3x2 matrix input is passed to log_prob, how is log_prob calculating final output as shape (3,). I understand the batch result but what is log_prob doing with 2 elements per each batch item?  I was interested in learning the math behind this function.
-Dipesh

Context: For a MultivariateNormal distribution of dimension (event_shape) N, the samples are vectors in N-dimensional Euclidean space. log_prob called on one such vector x will yield a single scalar – the log of the probability density of the MVN at that x. If your mean vector is m and covariance matrix is C, this log_prob is -1/2 (x - m)^T C^{-1} (x - m) - 1/N log 2π|C|  (pretty sure i got that right). More info here: Multivariate normal distribution - Wikipedia 1. Hopefully this answers some of your question! I know you said you were ok w/ batch_shape, but it may still be worth reading through this tutorial 2.


Using TF timeseries_dataset_from_array with more samples

Question:
I have to handle a huge amount of samples, where each sample contains unique time series. The goal is to feed this data into the Tensorflow LSTM model and predict some features. I have created the tf timeseries_dataset_from_array generator function to feed the data to the TF model, but I haven’t figured out how to create a generator function when I have multiple samples. If I use the usual pipeline, tf timeseries_dataset_from_array overlap the time series of two individual samples.
Does anyone have an idea how to effectively pass a time series of multiple samples to the TF model?
E.g. the Human Activity Recognition Dataset is one such dataset where each person has a separate long, time series, and each user’s time series can be further parsed with the SLIDING/ROLLING WINDOS-like timeseries_dataset_from_array function.
Here is a simpler example:
I want to use timeseries_dataset_from_array to generate samples for the TF model. Example: sample 1 where column 0 has 0, sample 2 starts where column 0 has 100. Here is a simpler example:

image786×362 42.3 KB

I want to get 3D data (samples, timesteps, features) without overlap.For example (6,2,7) Like this:

image865×428 48.6 KB

Here is the sample code:
from tensorflow.keras.preprocessing import timeseries_dataset_from_array
import numpy as np

x = np.array([[0,1,2,3,4,5,6],
              [0,11,12,13,14,15,16],
              [0,21,22,23,24,25,26],
              [0,31,32,33,34,35,36],
              [0,41,42,43,44,45,46]
              ])

xx = np.concatenate((x, x+100), axis=0)#.reshape(2,5,6)

sequence_length=2
stride=1
rate=1
input_dataset = timeseries_dataset_from_array(xx,
                                              None,
                                              sequence_length,
                                              sequence_stride=stride,
                                              sampling_rate=rate)

x_test = np.concatenate([x for x in input_dataset], axis=0)

Context:
I have figured out the solution…
### solution ######

# reshape the dataset
xxx = xx.reshape(2,5,7)

# create two tensorflow datasets
input_dataset0 = timeseries_dataset_from_array(xxx[0,:,:],
                                                      None,
                                                      sequence_length,
                                                      sequence_stride=stride,
                                                      sampling_rate=rate)
input_dataset1 = timeseries_dataset_from_array(xxx[1,:,:],
                                                      None,
                                                      sequence_length,
                                                      sequence_stride=stride,
                                                      sampling_rate=rate)
# concatenate the two tensorflow datasets
input_dataset = input_dataset0.concatenate(input_dataset1)

x_test = np.concatenate([x for x in input_dataset], axis=0)


Is the TensoFlow Decision Forests prediction probability calibrated?

Question:
I would like to understand whether the TensoFlow Decision Forests prediction probability is calibrated?
If not what are the ways of calibrating it.
Thank you in advance.

Context:
Hi,
TF-DF models are not calibrated by default, and automated calibration is (currently) not available in the open source version. The amount of calibration/non-calibration of a model is dependent on the learning algorithm (e.g. RF or GBT) and the hyper-parameters. For example, the classification Random Forest with default hyper-parameter is certainly not calibrated.
A solution is to train a calibration model on top of the model. For example, (1) you can learn an isotonic regression 2 with the Pava algorithm, or (2) train a piece-wise linear function with Gradient descent using TF-Latice 2.
Cheers,