Question:
import tensorflow as tf
from tensorflow.keras import layers
tf.random.set_seed(21)
vec = tf.random.normal([1, 2, 4], dtype=tf.float32)
print(vec)
lstm = layers.SimpleRNN(3)
output = lstm(vec)
print(output)
output-
tf.Tensor(
[[[-2.0809765   1.7485927   0.7655805  -1.5298417 ]
[-1.1606345  -1.9904611  -0.8419037   0.02641571]]], shape=(1, 2, 4), dtype=float32)
tf.Tensor([[ 0.89376044  0.8860847  -0.5505087 ]], shape=(1, 3), dtype=float32)
As we know that RNN cell takes vector as an input at each time step. How a matrix is given input to a RNN cell at each time step (In what way it is internally working or converting this matrix for each time step)?
My assumption is that it should throw an error because there should be 3 rows corresponding to each time step .i.e input shape should be (1, 3, 4).

Context:
tf.keras.layers.SimpleRNN takes input of shape 3D tensor, with shape [batch, timesteps, feature].  For more information read tf.keras.layers.SimpleRNN  |  TensorFlow Core v2.7.0.
If you want feed matrix as input, you can simply flatten the input
example,  Assuming that your (3,4) is a matrix,
input = tf.keras.layers.Input((3,4)) #none,3,4
x = tf.keras.layers.Flatten()(input)  #none,12


Question: Hi guys, I am using tensorflow training model recently and I find that I can’t get tensorflow serving image by docker. Because I am using distributed device of my campus and I can’t run “sudo” command, neither any other commands need root qualification. When I run “docker pull tensorflow/serving” command, it gives me an error message said “Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?”. Does anyone know what other way to get the serving but not by docker? Any help would be appreciated. Thank you!

Context:
Have you tried if you could run Docker in  rootless mode:

      Docker Documentation – 19 Nov 21

Run the Docker daemon as a non-root user (Rootless mode)

  Run the Docker daemon as a non-root user (Rootless mode)


Detailed CUDA Implementation of TensorFlow

Question: We are currently investigating how to deploy TensorFlow 2 for custom OP/deep learning on our product. We currently understand that a session is the place to execute a TensorFlow graph, which may include both deep learning OPs or self-defined (custom) OPs. To find out the best software architecture for our product, we would like to know how a tensorflow session is implemented in CUDA. Specifically, we would like to understand stream management, memory copy and compute execution in a session. In more depth, we would like to understand the implementation at OS level, e.g., running different sessions in a single process and in multiple processes. Is there a particular document for our questions from tf2?

Context:
I suppose that something it is going to change with the new Tensorflow runtime:

      github.com/tensorflow/tensorflow

      How to create new cuda stream in custom op 5

        opened

        Feb 11, 2020

          closed

        Apr 3, 2020

          chaaland

          stat:awaiting tensorflower

          type:support

          comp:gpu

    I've written a custom op for the GPU that takes X with shape ```(n,p)``` and Y w…ith shape ```(m,p)``` and returns Z with shape ```(n,m)```.

In the backward pass, dX and dY are independent computations so I'd like to have launch two separate CUDA kernels and have them run in separate streams. I know the cuDNN RNN is capable of doing kernels in parallel but I cannot find the source code for how to do this. It seems the only usable stream is provided by the OpKernelContext.

Matrix multiplication must have a similar parallelism when doing the backward pass but I have not found anywhere in the source making use of multiple streams. This is essentially a reopening of [this issue](https://github.com/tensorflow/tensorflow/issues/6675) as there doesn't appear to a public answer


Get intermediate output of layer (not Model!)

Question:
We want to re-use an existing keras layer, but return an intermediate value of the call function.
To be precise we aim to re-use the ResNet class of SimCLR 3, which equals to:
class Resnet(tf.keras.layers.Layer):

  def call(self, inputs, training):
    for layer in self.initial_conv_relu_max_pool:
      inputs = layer(inputs, training=training)

    for i, layer in enumerate(self.block_groups):
      inputs = layer(inputs, training=training)

    inputs = tf.reduce_mean(inputs, [1, 2])
    inputs = tf.identity(inputs, 'final_avg_pool')
    return inputs

We want to obtain the inputs before the reduce_mean function.
If this was a keras Model we could do something like model.get_layer(index=X).output.
Keras Layers do have submodules, and we could identify the correct submodule (resnet_model.submodules[8].name returns block_group4 as expected). However,  resnet_model.submodules[8].output yields an
AttributeError: Layer block_group4 has no inbound nodes.

Is the only way to subclass and redefine call? Or is there another way to get the output of a submodule / intermediate value of the layer?

Context: Similar issue Tensorflow 2 Hub: How can I obtain the output of an intermediate layer? - Stack Overflow 10


Object tracking, is TensorFlow the right way to go about it?

Question:
Hi there, just need a little advice please. I’m new to ML but have done lots of reading and research about various ways to do object tracking.  I want to develop a small system to track the location of a known object in front of the camera. It’s basically a rectangle that moves up and down, forwards and backwards and can tilt. Initially, the object of the exercise is to work out the rectangles position, so I can work out its height from a setpoint.  Once get that running I want to extract the angle of it’s tilt and finally extract it’s size so I can tell how far away it is (roughly - between 1 and 6 meters).  It’s always the same or similar object that’s being tracked.
Initially I’ll create it for my Android phone to prove the idea, then modify to run on an ESP32 CAM or similar.
Is TensorFlow the right way to achieve this?
Cheers
Brent

Context:
I don’t understand if you want to have single object model or not.
For a general bbox tracking pipeline you can see:

      mediapipe

Box Tracking 3

  Cross-platform, customizable ML solutions for live and streaming media.

Then TF micro could run on that devices.


How to initialize Tensorflow on CPP project?

Question:
Hi there,
Is there any CPP functions that initialize the tensorflow?
I am trying to write a graph rewriting pass outside tensorflow,  so I use TFG’s provided
test like mlir::tfg::ImportGraphDefToMlir(&context, debug_info, graphdef);. It throws the error that complains that no tensorflow Op is registered, only my custom op is registered
It is expected that the tensorflow ops is registered as well.
 $ TF_CPP_MAX_VLOG_LEVEL=4 ./test
 2021-11-23 14:50:24.049527: I tensorflow/core/framework/op.cc:130] All registered Ops:
 2021-11-23 14:50:24.049540: I tensorflow/core/framework/op.cc:132] Op<name=CustomOp1; signature=input:int32 -> output:int32>
 2021-11-23 14:50:24.049548: I tensorflow/core/framework/op.cc:132] Op<name=CustomOp2; signature=input:T -> output:T; attr=T:type>
 2021-11-23 14:50:24.049599: I tensorflow/core/framework/op.cc:80] NOT_FOUND: Op type not registered 'NoOp' in binary running on host-machine. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.
 2021-11-23 14:50:24.049655: I tensorflow/core/framework/op.cc:80] NOT_FOUND: Op type not registered 'NoOp' in binary running on host-machine. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.
 2021-11-23 14:50:24.049678: F tensorflow/core/graph/graph.cc:394] Non-OK-status: status status: NOT_FOUND: Op type not registered 'NoOp' in binary running on host-machine. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.

I also tried using the following test case, its output is empty.
// importer.cc
int main(int argc, char **argv) {
  ::tensorflow::port::InitMain(argv[0], &argc, &argv);
  std::vector<::tensorflow::OpDef> op_defs;

  Status s = ::tensorflow::OpRegistry::Global()->ProcessRegistrations();
  if (!s.ok()) {  VLOG(2) << " Not processed";  }

  ::tensorflow::OpRegistry::Global()->GetRegisteredOps(&op_defs);
  for (const tensorflow::OpDef &op : op_defs) {
    VLOG(2) << " Op name " << op.name();
  }
}

So, in that case, it seems that the tensorflow is not initialized , and the tensorflow ops is not registered. How could I call the initialize function?

Context:
If you are looking for new CPP function to be executed on Tensorflow. Firstly we need to register  CPP op in Tensorflow. Read this 7 guide to get more idea on how to create an op.
CPP functions are available on Tensorflow. Read for more TensorFlow C++ API Reference  |  TensorFlow Core v2.7.0 3


TensorFlow .js and .py Whats the difference?

Question: Hello! I know how to code TensorFlow.py but my uni is teaching TensorFlow.js is there any to much difference between the two. I dont want to learn .js when I already know .py. Is one better over the other?

Context:
So TensorFlow.js has several unique advantages over Python equivalent as it can run on the client side too, not just the server side (via Node) and on the server side it can potentially run faster than Python due to the JIT compiler of JS. Other than that the APIs etc are similar and Python is older so is more mature in terms of development as we are a younger team, but growing fast.
Some links for your consideration:
Check this video for an overview:
Web ML: TensorFlow.js in 30 minutes - everything you need to know in 2021 #MadeWithTFJS

Client side benefits - you can not get these server side - unique vs Python:

Privacy

You can both train and classify data on the client machine without ever sending data to a 3rd party web server. There may be times where this may be a requirement to comply with local laws, such as GDPR for example, or when processing any data that the user may want to keep on their machine and not sent to a 3rd party.

Speed

As you are not having to send data to a remote server, inference (the act of classifying the data) can be faster. Even better, you have direct access to the device’s sensors such as the camera, microphone, GPS, accelerometer and more should the user grant you access.

Reach and scale

With one click anyone in the world can click a link you send them, open the web page in their browser, and utilise what you have made. No need for a complex server side Linux setup with CUDA drivers and much more just to use the machine learning system.

Cost

No servers means the only thing you need to pay for is a CDN to host your HTML, CSS, JS, and model files. The cost of a CDN is much cheaper than keeping a server (potentially with a graphics card attached) running 24/7.
Speed:

      blog.tensorflow.org

How Hugging Face achieved a 2x performance boost for Question Answering with... 12

  The TensorFlow blog contains regular news from the TensorFlow team and the community, with articles on Python, TensorFlow.js, TF Lite, TFX, and more.

Which university  / professor is teaching TFJS out of curiosity?


Change batch size (statically) for inference TF2

Question: I have a trained keras .h5 model and want to change the batch size, to allow processing multiple samples at inference (using a .tflite model). How is that possible in tf2? Unfortunately, I didnt find anything.

Context:
What worked for me:
model = tf.keras.models.load_model("model.h5")

conf = model.get_config()
model_config = model.get_config()
input_layer_name = model_config['layers'][0]['name']
model_config['layers'][0] = {
                      'name': 'new_input',
                      'class_name': 'InputLayer',
                      'config': {
                          'batch_input_shape': (2, 512, 768,3),
                          'dtype': 'float32',
                          'sparse': False,
                          'name': 'modified_input'
                      },
                      'inbound_nodes': []
                  }
model_config['layers'][1]['inbound_nodes'] = [[['new_input', 0, 0, {}]]]
model_config['input_layers'] = [['new_input', 0, 0]]
new_model = model.__class__.from_config(model_config, custom_objects={})  # change custom objects if necessary

new_model.set_weights(model.get_weights())

Found on stackoverflow python - Keras replacing input layer - Stack Overflow 4
Solved. But thanks anyway.


Unknown metric val_accuracy using Keras Tuner Error

Question:
I am trying to use a Custom Keras Tuner with fashion-mnist dataset in Google Colab, that’s my code:
!pip install keras-tuner
import tensorflow as tf
import kerastuner
import numpy as np

print("TensorFlow version: ", tf.__version__)

(x_train, y_train) , (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()

def create_model(hp):

  num_hidden_layers = 1
  num_units = 8
  dropout_rate = 0.1
  learning_rate = 0.01

  if hp:
    num_hidden_layers = hp.Choice('num_hidden_layers', values = [1,2,3])
    num_units = hp.Choice('num_units', values = [8,16,32])
    dropout_rate = hp.Float('dropout_rate', min_value = 0.1, max_value = 0.5)
    learning_rate = hp.Float('learning_rate', min_value = 0.0001, max_value = 0.01)

  model = tf.keras.models.Sequential()

  model.add(tf.keras.layers.Flatten(input_shape = (28,28)))
  model.add(tf.keras.layers.Lambda(lambda x: x/255.))

  for _ in range(0, num_hidden_layers):
    model.add(tf.keras.layers.Dense(num_units, activation='relu'))
    model.add(tf.keras.layers.Dropout(dropout_rate))

  model.add(tf.keras.layers.Dense(10, activation='softmax'))

  model.compile(
      loss='sparse_categorical_crossentropy',
      optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate),
      metrics=['accuracy']
  )

  return model

class CustomTuner(kerastuner.tuners.BayesianOptimization):
  def run_trial(self, trial, *args, **kwargs):
    kwargs['batch_size'] = trial.hyperparameters.Int('batch_size', 32, 128, step=32)
    super(CustomTuner, self).run_trial(trial, *args, **kwargs)

tuner = CustomTuner(
    create_model,
    objective='val_accuracy',
    max_trials = 20,
    directory= 'logs',
    project_name='fashion_minist',
    overwrite=True,
)

search = tuner.search(
    x_train, y_train,
    validation_data=(x_test, y_test),
    epochs = 5,
)

And I’m getting the following error:
ValueError: Unknown metric: val_accuracy

Anything I’m doing wrong? I am getting val_accuracy values in training process, but when the first trial finishes it just stops with this error.
I had made somes tests,  and looks  that the error is in the CustomTuner, if i try a default tuner it’s working normal

Context:
I don’t know if it could be related to:

      github.com/keras-team/keras-tuner

      If override "loss", the original "metrics" will be missing. 11

        opened

        Nov 2, 2021

          haifeng-jin

          bug

    **Describe the bug**
Metrics and loss are both defined in hypermodel when compi…ling the model.
If override "loss" when init tuner, the original "metrics" will be missing.

**Expected behavior**
Keep the original metrics.

**Additional context**
The cause of the problem is:

1. `model.metrics` is `[]` before `fit`, no matter what metrics was set by the user.

2. model cannot be compiled multiple times, so overriding compile args have to compile the model with all the args.

3. `model` is recompiled with the empty metric got from `model.metrics`.

/cc @haifeng


Please help me with TensorFlow object detection A.P.I. installation

Question: Hello, I am a new member. I have just started learning TensorFlow. I know about the basics, and I would like to move forward. I am having trouble downloading and installing the TensorFlow object detection A.P.I. for Python. I am following the steps listed on the official TensorFlow website, but nothing works. Every time I run the installation command, my system starts downloading the files and never finishes. It takes hours to complete the download of some files. I would appreciate any form of help.

Context:
You can install Tensorflow Object Detection API in Two ways, one is using PIP and other is by Docker.
Follow the steps mentioned on Tensorflow github

      github.com

    tensorflow/models/blob/master/research/object_detection/g3doc/tf2.md#object-detection-api-with-tensorflow-2 2

    # Object Detection API with TensorFlow 2

## Requirements

[![Python 3.6](https://img.shields.io/badge/Python-3.6-3776AB)](https://www.python.org/downloads/release/python-360/)
[![TensorFlow 2.2](https://img.shields.io/badge/TensorFlow-2.2-FF6F00?logo=tensorflow)](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0)
[![Protobuf Compiler >= 3.0](https://img.shields.io/badge/ProtoBuf%20Compiler-%3E3.0-brightgreen)](https://grpc.io/docs/protoc-installation/#install-using-a-package-manager)

## Installation

You can install the TensorFlow Object Detection API either with Python Package
Installer (pip) or Docker. For local runs we recommend using Docker and for
Google Cloud runs we recommend using pip.

Clone the TensorFlow Models repository and proceed to one of the installation
options.

```bash
git clone https://github.com/tensorflow/models.git
```

  This file has been truncated. show original


Why not specify the shape out of `TextVectorization` class to Keras model

Question:
In the book “Deep Learning with Python, Second Edition”, the author uses the TextVectorization class to preprocess text into sequences as such:
max_length = 600
max_tokens = 20000
text_vectorization = TextVectorization(
    max_tokens=max_tokens,
    output_mode="int",
    output_sequence_length=max_length,
)

where each is integer encoded like [5, 18, 1, 0, 53,...] and so each batch is of shape [batch_size, 600]. However when building the model he doesn’t specify the input shape: inputs = keras.Input(shape=(None,), dtype="int64"). Any idea why that is?

Context:
After text preprocessing using Textvectorization, we define input layer with shape (1, ).

#Start by creating an explicit input layer. It needs to have a shape of
#(1,) (because we need to guarantee that there is exactly one string
#input per batch), and the dtype needs to be ‘string’.
#model.add(tf.keras.Input(shape=(1,), dtype=tf.string))

For more details refer tf.keras.layers.TextVectorization


A tutorial on reproducibility best practices in TensorFlow?

Question:
The caption really says it all. I think it’s time we had a comprehensive tutorial on this topic. I would like to list two of my favorite resources on this area:

Reproducibility as a Vehicle for Engineering Best Practices 17 by Joel Grus

Determinism in Deep Learning 13 by Duncan Riach

Context: I agree with this. Also, I’ve seen a lot addressing use of GPU’s and use of same hardware, but I’d also like to understand what to expect switching between different OS and using CPU.


ValueError: destinations can not be empty

Question: This error happen when I try to : get output from the layer  before last layer as features, and try to compute loss with an expected features, then propagate and update the input (not the network’s weight, but the input)  using cosine similarity loss. I cannot find any clue about this bug. Is there anyone meet this problem before?

Context:
Sample working code which computes intermediate layer output and cosine similarity loss

import tensorflow as tf
(x_train, y_train), (x_test, y_test)=tf.keras.datasets.mnist.load_data()
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=[28,28,1]),
tf.keras.layers.Dense(64,activation=“relu”),
tf.keras.layers.Dense(10, activation=“softmax”)
])
intermediate_layer = tf.keras.Model(inputs=model.inputs,
outputs=model.outputs + [model.layers[1].output])
cosine_loss = tf.keras.losses.CosineSimilarity()
optimezer = tf.optimizers.Adam()
with tf.GradientTape() as tape:
y_pred, intermediate_output= intermediate_layer(x_train)
print(intermediate_output)
loss = cosine_loss(tf.one_hot(y_train, 10), y_pred)
gradients = tape.gradient(loss, intermediate_layer.variables)

Output

tf.Tensor(
[[1.2817659e+02 0.0000000e+00 2.1529781e+01 … 1.4793573e+02
1.7081753e+01 0.0000000e+00]
[3.1562555e+02 0.0000000e+00 8.2275620e+01 … 0.0000000e+00
0.0000000e+00 0.0000000e+00]
[1.5004303e+01 5.3053143e+01 0.0000000e+00 … 0.0000000e+00
0.0000000e+00 9.4606926e+01]
…
[0.0000000e+00 0.0000000e+00 1.2034170e+02 … 1.3093190e+02
0.0000000e+00 1.5922928e-01]
[2.4972404e+02 0.0000000e+00 2.2767758e+02 … 0.0000000e+00
0.0000000e+00 2.0227030e+01]
[9.6311852e+01 0.0000000e+00 0.0000000e+00 … 5.8486767e+00
0.0000000e+00 9.8323425e+01]], shape=(60000, 64), dtype=float32)


MoveNet on TensorFlow Lite for Microcontrollers

Question:
I am planning to deploy the MoveNet pose estimation model on microcontrollers like the ESP32.
But I am not sure whether the MoveNet model architecture is supported by the TensorFlow Lite for Microcontrollers.
Can I get some advice on it? Thanks.

Context:
It looks like the standard int8 version of MoveNet on TFHub is about 2.7MB in size:
https://tfhub.dev/google/lite-model/movenet/singlepose/lightning/tflite/int8/4 3
This is probably too large for most embedded boards to store. I would also guess that the latency would be very high, since it’s designed for mobile phone CPUs. I would look into whether you can use a Raspberry Pi instead, if possible.


Question:
Hi
I am trying to use tfx.componets.CsvExampleGen with a csv file that contains words written in spanish, and I get an error:
UnicodeDecodeError: ‘utf-8 [while running ‘InputToRecord/ReadFromText’]’ codec can’t decode byte 0xc1 in position 72: invalid start byte
When I try to read this .csv with pandas this is solved by adding “encoding=latin-1”, is there a similar solution for tfx?

Context: There isn’t anything similar for TFX, at least not yet.  We have a bug open to address this.  In the meantime, if you set the default encoding 8 I think that will work.


Please add support for multiple GPUs in browser-side TF

Question:
I’m working with Tensorflow.js in the browser (not via Node.js) on my first AI project and making great progress, in general, but I’ve hit a frustrating roadblock of sorts and not finding any good solutions so far.  I have a need to simply expand my GPU capacity substantially but currently for Tensorflow.js in the browser, there is no “tf.device” option like what is available with Tensorflow Core, to be able to make use of multiple GPUs on the server I run my app on.
While at first it may seem like the need to use multiple GPUs on a browser-based application implementation is very very rare, I don’t think the need for this is as rare as it might seem.  There are a huge number of javascript developers who have no experience whatsoever with server-side javascript (Node) or Python, but have only experience building javascript apps in a browser.  It seems short-sighted to me to not enable a multiple-GPU capability for such developers and apps considering that it does not seem that it would require a huge amount of development work to enable this capability.  The lack of such capability forces developers who are comfortable developing javascript apps in a browser environment to have to try to figure out the server-side stack of Node and all the other server-side components that must be enabled to do server-side development.  I have worked on it and just getting a functional Node environment on my development platform of choice (Windows Server) was quite a challenge that has been very frustrating and time-consuming and prone to subtle incompatibilities between different versions of all the various software components.  And additionally, even if I do implement server-side tensorflow.js in Node, then I also have to figure out how to integrate my front-end javascript app with the backend Node implementation for the tensorflow.js support for the tensor crunching my front-end app needs access to.
In short, it’s a nightmare to come this far and find that my browser-based implementation has no means to access a substantial increase in GPU capacity without completely rearchitecting my application which, except for the need to utilize Tensorflow.js with more capacity than what a single GPU can provide, works absolutely great as a browser-based javascript app and is easy and intuitive to use.
As such, I strongly urge you to PLEASE implement support in tensorflow.js to include support for tf.device capability so that a browser-based TensorFlow app can utilize multiple GPUs for much-expanded tensor crunching capacity and speed without having to completely rearchitect the application.

Context: @Jason might be able to help


How to preserve html structure when processing

Question: Hi I’m trying to figure out how I can process info that I’ve scraped through language models while retaining the html tags. I am able to get some pretty good results with pegasus-paraphraser but i’d like to figure out how to bulk process pages.  Whenever i use  tags in the input they get removed in the output of the models. Does anyone have a solution to my issue?

Context:
Do you want to directly control the text generation? As it is not a so easy task…

      Lil'Log – 2 Jan 21

Controllable Neural Text Generation

  The modern language model with SOTA results on many NLP tasks is trained on large scale free text on the Internet. It is challenging to steer such a model to generate content with desired attributes. Although still not perfect, there are several...


Foreign Exchange (FOREX) Price Prediction

Question:
Hello,
First and foremost, please note that I am an absolute newbie to Tensorflow and machine learning. I am also a novice FOREX trader with amateur experience programming in python, MQL5, Java, Javascript, C++, and Dart.
I am interested in predicting the direction of price for foreign exchange, which is a time series problem in my understanding. I have had several ideas about how I could do this, however, please help point me in the right direction in terms of codelabs and documentation to study.
The latest idea I have had is to try and predict if the next candlestick (please google Japanese price candlesticks if you are unfamiliar with the concept) close price on a particular trading timeframe will be higher or lower than the current close price depending on the details (open, high, low, and close prices) of the previous n number of price candlesticks.
I thought that I could approach this problem using reinforcement learning by exposing an agent to time series data and attempt to predict whether the close price will be higher or lower than the current close price in the next timestamp of the series as I have already mentioned above, and then repeat the same process again for the next candlestick and again until the agent is good enough. This solution could also be great for binary options trading.
This in essence, according to my understanding, will be looking for price action patterns in the selected n historical price candlesticks of the selected trading timeframe to predict the future.
Please help me create an environment to expose an agent to historical price data, and an agent to train on this data. Please note that price data is structured exactly the same way stock price data is structured.

Context:
I think that you can start with an overview on:
A Survey of Forex and Stock Price Prediction Using Deep Learning 2


Is there any sample code of distributed training?

Question: I need a sample code of distributed training as a starting point to learn this interesting technique. The documentation is full of details that I don’t understand, so I hope there is a complete distributed training sample code, perhaps in an easy tutorial, that I can run without understanding the details, that is, only by copy and paste. Where can I find such a complete sample code of distributed training? PS, I mean TensorFlow 2.

Context: @zzzhhh, You can refer detail tutorial on Distributed training with Keras 2. Thanks!


Question:
Hello, community!
Is there a way to customize the model from object detection model_maker? e.g., get the loss and do some early stopping with the validation loss, save and load it.  If it is not possible, is there a way to load the model as a tf/keras model to customize the training? Or maybe print the model summary to try to reproduce on tf/keras…
I followed the tutorial with efficiendet, Object Detection with TensorFlow Lite Model Maker 3, and it worked fine, besides the fact that the training is being done on CPU.
Best Regards,

Context:
@khanhlvg may have some advice.
But AFAIK model maker is really limited in terms of customizability.


One-hot encoding a segmentation label map with pure TensorFlow

Question:
How do I one-hot encode a segmentation map with pure TensorFlow ops?
Currently, we are doing something like this:
n_classes = 12
h, w, _ = image.shape
one_hot_encoded_labels = np.zeros((h, w, n_classes), dtype=np.float32)
for i in range(n_classes):
    one_hot_encoded_labels[labels == i, i] = 1

(Say image has a shape of (443, 300, 3) and labels has a shape of (443, 300).)
And then we are using tf.py_function to make it all work.

Context:
TensorFlow

tf.one_hot  |  TensorFlow Core v2.7.0 4

  Returns a one-hot tensor.

tf.one_hot can totally handle this case.


Tensorflow Serving how to filter output?

Question:
Hello all.
I’m using SSD MobileNet V2 320x320 with Tensorflow Serving:latest-gpu on Nvidia 1060 6GB card
Sending a lot of requests by REST API
output_body = {"inputs": [{"b64": input_image}]}
latency: ~1966.1445617675781 ms
And from output i get a lot of data eash request (17mb each!): detection_multiclass_scores, detection_anchor_indices, raw_detection_scores, detection_scores, raw_detection_boxes, detection_boxes, detection_classes etc. And all of this arrays are LEN 100
How can i cut out my output from Tensorflow Serving, filter it?
I was trying a lot of combinations, like:
output_body = {"inputs": [{"b64": input_image}], "outputs": [{"detection_scores": False}], "output_filter": 'detection_classes'}

But nothing works.
I found these parameters at: serving/predict.proto at 5369880e9143aa00d586ee536c12b04e945a977c · tensorflow/serving · GitHub 2
So can i make 10 or 5 or 20 ouput LEN? Or don’t get some output parameters like raw_detection_scores?
Tx!

Context:
Hello again, i found one solution. In pipeline.config i changed this line:
max_number_of_boxes: 50

And retrain model. But anyway latency is hight:

Local model loaded by tf.saved_model.load ~110 ms (60ms loading img, 1-5ms convert to tensor, 60ms prediction
Local model loaded by Tensorflow Serving in Docker: ~1100 ms total

I will continue research. Cause it will be SUPER solution to have only 1 Server with model and 10 PCs getting results on 110ms latence. 1100 latency is to high


How to implement h5 model into a web application

Question:
I have trained a .h5 model from python , i found the ways to implement it to android , you literally put the model into the resource folder and import some library to call it .
But I can’t find anyway to implement .h5 model into web application . It seems that all tutorials are teaching how to train it in javascript and use it on javascript . But none of them tells people how to implement .h5 into web application .

Context:
Simply upload all converted files (.json and .bin) in the same directory on your webserver.
path/to/model/model.json
path/to/model/group1-shard1of1.bin
....

In Step 2: Load the model and make a test prediction (clientside)
//app.js
...
const modelUrl = 'path/to/model/model.json';
const model = await tf.loadGraphModel(modelUrl);
const zeros = tf.zeros([1, 224, 224, 3]);         //change to your original model input data dim
model.predict(zeros).print();

Examples:
API Doc,
Step2: Loading and running in Webbrowser


Extracting feature maps right after each conv layer

Question:
A question to experts
is it possible to extract feature maps right after each conv layer as numpy arrays and do computations on them then convert the resulted feature maps arrays back to tensors to feed them to the next layer in the model?
If that is possible, please let me know, coz i am hopeless as i tried to do that many time and i failed.
To make it clear,  please have a look at the following example :
inputs = Input(shape=(48,48,3))
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv2D(32, (3, 3), activation='relu', padding='same')(conv1)

####   here i need to get the activation maps of conv1 as numpy arrays   ####

pool1 = MaxPooling2D((2, 2))(conv1)

#shape=(None, 64, 24, 24)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv2D(64, (3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling2D((2, 2))(conv2)

Note: i am using
tf 2.7 ,
Keras 2.6,
python 3.8.1

Context:
Why you need to manipulate it in numpy?
You can always create your custom layer or lambda layer:

      keras.io

Keras documentation: Making new layers and models via subclassing 1

      keras.io

Keras documentation: Lambda layer 1

If you want we have also a numpy API in TF:

      TensorFlow

NumPy API on TensorFlow  |  TensorFlow Core 2


Question: Hi guys, I am recently doing research about how gRPC works on TensorFlow and the possibility to replace it with a specific gRPC I download from the official gRPC github repo. Does anyone know how TensorFlow configs the gRPC components and what kind of stuff I need to change? Thank you!

Context:
I wouldn’t consider myself a TF build expert and there may be a better way to do this. The GRPC dependency is defined here: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/workspace2.bzl#L622-L641 9
You can clone the repository and replace the URLs with whatever specific release you want. Make sure you also replace the sha256 with the matching one as well.
Once you change the version, you can follow the directions to build TF from source. No guarantees that changing the GRPC version will work correctly when building TF master.


How to handle normalizing image with blank pixels in a Unet for image segmentation?

Question:
Hello,
I am doing my first unguided project and just need a little advice. I’ve done a lot of reading and some online courses, but have not been able to find information on how to handle blank space. I do not want to tell exactly what I am doing, but here is some information that I can give:
-I have hundreds of images that are inconsistent shapes (curved edges, different sizes) and cropping part of the image is not an option. I put these images in a rectangular matrix so all my data is the same shape, but because of that I have a bunch of blank pixels.
-There is only 1 feature/class I am trying to model, but there is a large class imbalance in terms of the number of pixels(<1:100), so I am using a binaryfocalloss function. The range of values in the actual images is about 100-400. So I could set the blank pixels to zero then normalize or 100 then normalize. I am not sure it matters.
-For the training data, the features are determined by taking the difference in the values of 2 of the image channels and manually identifying the pattern of that feature.
Bonus question: If my channels have different value ranges (like 100-150 vs 300-400) should I normalize each channel by the range of that channel or the range of all the channels? I think the latter?
Thanks for your help!

Context:
Kevin_Sanchez:

To rephrase, what value should I give the blank pixels? Does it matter?

So the “on” pixels have a range of 100-400, okay, I think I’m with you now.

zero then normalize or 100 then normalize

One advantage to using 0 instead of 100, is that it’s easy to tell that zero is not in that valid range.
If you set the background pixels to 100, when you see a 100, it’s never clear if that’s a valid 100 or a background 100.
Another option here would be to add a valid [0,1] channel to the images so your network can see which pixels are valid, then the fill value would matter less.


How to use ray and tensortrade with LSTM for crypto currency price prediction?

Question: a little bit confusing

Context:
As it is a third party library I suggest to use their community support channels:

      GitHub

GitHub - tensortrade-org/tensortrade: An open source reinforcement learning... 25

  An open source reinforcement learning framework for training, evaluating, and deploying robust trading agents. - GitHub - tensortrade-org/tensortrade: An open source reinforcement learning framewor...


Generating a dataset of .npy file using tf.data API

Question:
Hi, I am trying to create a custom dataset using tf.data API, the dataset consists of a 3-Dimensional NumPy file of shape (128, 128, 128, 3). I am trying the following code to create a dataset …
def parse_image(image_path: str) -> dict:
    image = tf.io.read_file(image_path)
    image = tf.io.decode_raw(image, out_type=tf.float32)

    return {'image': image}

images = glob.glob(IMAGE_PATH)
train_ds = tf.data.Dataset.list_files(images)
train_ds = train_ds.map(parse_image, num_parallel_calls=24)
train_ds = train_ds.batch(4)

But the shape I’m getting in output is (2, 12582944) which can’t be converted to (128, 128, 128, 3). what Am I doing wrong? what can be done to solve this ?
Note: there is no chance of loading the dataset in NumPy first and then converting it to tf.data Dataset as there are around 1000 images that can’t be saved simultaneously to the memory at a single instance.

Context: Maybe using tf.image.decode_png/decode_jpg instead of decode_raw will help.


Question:
I have created this PR  17 to fix all the typos in tensorflow/datasets/docs
Can someone please review ?
Edit : It has been approved

Context: I think it is approved now.


How to load a pushed_model from a different run and feed into Bulkinferrer

Question:
Hi there, I am tying to use make use of the  Bulkinferrer by loading pushed_model from a different run/pipeline. Since they are from different runs, i cannot simply feed bulkinferrer with model = trainer.outputs['model'] .
I can find the pushed_model from the differnt pipeline, however, the pushed_model directly fetched from mlmd is of type ml_metadata.proto.metadata_store_pb2.Artifact , if i feed this into the Bulkinferrer i got Argument model should be a Channel of type <class 'tfx.types.standard_artifacts.Model'>.
My question is how can i make use of the mlmd metadata and load the pushed_model into the Bulkinferrer from a different run/pipeline?
Thanks a lot for your help.

Context:
Are you using the latest blessed model resolver to get the pushed model (saved_model)?
  # Get the latest blessed model for model validation.
  model_resolver = tfx.dsl.Resolver(
      strategy_class=tfx.dsl.experimental.LatestBlessedModelStrategy,
      model=tfx.dsl.Channel(type=tfx.types.standard_artifacts.Model),
      model_blessing=tfx.dsl.Channel(
          type=tfx.types.standard_artifacts.ModelBlessing)).with_id(
              'latest_blessed_model_resolver')

      TensorFlow

tfx.v1.dsl.Resolver  |  TFX  |  TensorFlow 1

  Definition for TFX Resolver.

      TensorFlow

tfx.v1.dsl.experimental.LatestBlessedModelStrategy  |  TFX... 1

  LatestBlessedModelStrategy resolves the latest blessed Model artifact.


Question:
Hello, I’ve been using networkx (https://networkx.org/ 3) to help learn about graph data structures. I read Introduction to graphs and tf.function  |  TensorFlow Core 2 but this doesn’t seem to help with GNNs.
Are there any resources for implementing / learning how to build GNNs using Tensorflow?
I was thinking about using networkx and then maybe using tf.GradientTape to update weights between nodes.

Context:
Have you already take a look at:

      github.com

GitHub - tensorflow/neural-structured-learning: Training neural models with... 10

  Training neural models with structured signals. Contribute to tensorflow/neural-structured-learning development by creating an account on GitHub.

      github.com

GitHub - danielegrattarola/spektral: Graph Neural Networks with Keras and... 9

  Graph Neural Networks with Keras and Tensorflow 2. - GitHub - danielegrattarola/spektral: Graph Neural Networks with Keras and Tensorflow 2.


How to add dense layers with embedding layer

Question:
Is it possible to add an embedding layer [n, N] with a dense layer [1, N]?
Or two embedding layers having the same number of rows but different column sizes, eg. embd1 [10, N] and embd2 [3, N]?

Context:
Yes you can add Dense layer with Embedding Layer
Working sample code

import tensorflow as tf
import numpy as np
model = tf.keras.Sequential()
model.add(tf.keras.layers.Embedding(1000, 64, input_length=10))
model.add(tf.keras.layers.Dense(64, activation=‘relu’))
model.add(tf.keras.layers.Dense(1))
input_array = np.random.randint(1000, size=(32, 10))
model.compile(‘rmsprop’, ‘mse’)
output_array = model.predict(input_array)
print(output_array.shape)

Output

(32, 10, 1)


Install problem on Ubuntu 20.04 LTS + GPU

Question:
I am having trouble installing TensorFlow on Ubuntu 20.04 LTS with GPU:
$ export LD_LIBRARY_PATH=/usr/local/lib/python3.8/dist-packages/numpy.libs/
$ source /opt/tensorflow/bin/activate
$ python3 -c 'import tensorflow as tf; print(tf.__version__)'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.8/site-packages/tensorflow/__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "/usr/lib/python3.8/site-packages/tensorflow/python/__init__.py", line 41, in <module>
    from tensorflow.python.eager import context
  File "/usr/lib/python3.8/site-packages/tensorflow/python/eager/context.py", line 30, in <module>
    import numpy as np
  File "/home/daldrich/.local/lib/python3.8/site-packages/numpy/__init__.py", line 163, in <module>
    from . import ma
ImportError: cannot import name 'ma' from partially initialized module 'numpy' (most likely due to a circular import) (/home/<snip>/.local/lib/python3.8/site-packages/numpy/__init__.py)

Can anyone help please?

Context:
Can you try to use the recommended virtual environment as in:

      TensorFlow

Install TensorFlow with pip 4


Training in browser with TFJS and WebGL in background

Question:
Hello, I am trying to train a neural network in the background (or in a tab that is not active).
I am using TFJS (3.11) with React and Ionic to deploy it also for mobile. The training works without problem when the tab of the browser is in foreground or the app is on the foreground with the screen on for the mobile.
When I switch to another tab on the browser or when I turn off the screen of the mobile the training stops. When I go back to the app/web-app the training resumes from where it froze.
Is there a way to keep the app training even when it is moved to the background?
Right now I am trying with web-workers but I am facing some issues with React and I am not guaranteed that it will work.
Best regards,
Nelu

Context:
This is a browser level detail. You do need to have the tab visible on the screen to get processing resource allocated to the tab.
As this is implemented at Browser level it is not something TensorFlow.js can change. The reason for this is that many people have tens of tabs open, if all tabs could have unlimited processing your computer would fast come to a halt or use battery for laptop/mobile pretty fast. Thus only active tabs are given most resources.
As you mentioned, Web Workers at the domain level may be a good bet here. Not sure what the problem with React is, as I am a vanilla JS guy myself, but WebWorkers are well supported at the browser level:
https://caniuse.com/webworkers
Remember you do not have DOM access in web worker, so you will need to stream data to it if you are taking data from say webcam or something, process, and stream back without using DOM elements.
Thus you would also need offscreen canvas support for WebGL acceleration I believe, which is not well supported on iOS / Safari / Firefox:
https://caniuse.com/offscreencanvas
More details in this blog:

      Medium – 19 Sep 19

Webworker in TensorFlowjs

  We are excited to announce that TensorFlow.js will begin supporting Web Workers in its next release.

    Reading time: 2 min read

Also we discovered recently that WebGL on WebWorker will still block main GUI:

      github.com/tensorflow/tfjs

      TFJS WebGL on WebWorker still blocks GUI 2

        opened

        Aug 11, 2021

          jasonmayes

          type:bug

    After investigating a [report by a user](https://discuss.tensorflow.org/t/how-to…-get-the-duration-of-predict/3311/18) who was attempting to use web workers + tfjs to reduce lagging of GUI when model performs inference I have noticed that when using backend "webgl" web worker makes no effect on executing code in a separate context as this does not apply to GPU.

I have confirmed that by setting backend to "cpu" there is no performance issue, as the TFJS execution correctly is executed on a new thread, such that the browser DOM updates are not interfered with. I have also confirmed that browser relies on GPU to update DOM for anything visual - not CPU.

Thus the request here is how to limit "webgl" execution to leave enough processing for DOM updates for other user tasks to prevent this "jankiness" from occuring.

Demo of issue:

https://codepen.io/jasonmayes/project/editor/DBYaRj

Simply change first line of tfWorker.js to import one of:

`importScripts('dist/cpu.js');`

or for WebGL backend change to:

`importScripts('dist/main.js'); `

Confirmed this issue across devices including Windows 10, Desktop, Chrome and also Xiaomi 8 (Android phone with Snapdragon 845 processor)

Example output from Chrome Dev tools shows GPU block when using WebGL backend and also that all execution comes from WebWorker.js.

![unnamed](https://user-images.githubusercontent.com/4972997/128932303-e422ed1d-ef87-46dc-bc36-ee2aac0fe260.png)

We have found a potential fix for this but not pushed live yet. This is due to how the GPU is shared on the browser and again is not TFJS issue itself, but we found a workaround for now.
I assume this will gain support over time, but still relatively new right now, in which case you may want to force Web Assembly backend instead:

      blog.tensorflow.org

Introducing the WebAssembly backend for TensorFlow.js 2

  We’re happy to announce that TensorFlow.js now provides a WebAssembly (WASM) backend for both the browser and for Node.js! This backend is an alternative to the WebGL backend, bringing fast CPU execution with minimal code changes. This backend helps...


Question:
I have been trying to follow this guide. However, I feel like it’s severely lacking.
[Simple audio recognition: Recognizing keywords  |  TensorFlow Core 1](https://Simple audio recognition: Recognizing keywords)
First and before most, why there is no import for tensorflow-io
Reading audio files and their labels
def decode_audio(audio_binary):
audio, _ = tf.audio.decode_wav(audio_binary)
return tf.squeeze(audio, axis=-1)
Regarding this method does it allows mp3? Is possible to load mp3? What tf.squeeze actually does to the audio_binary I provided?
AUTOTUNE = tf.data.AUTOTUNE
files_ds = tf.data.Dataset.from_tensor_slices(train_files)
waveform_ds = files_ds.map(get_waveform_and_label, num_parallel_calls=AUTOTUNE)

Regarding this piece of code, I would love to know what tf.data.AUTOTUNE does.
def get_spectrogram(waveform):
# Padding for files with less than 16000 samples
zero_padding = tf.zeros([16000] - tf.shape(waveform), dtype=tf.float32)
  # Concatenate audio with padding so that all audio clips will be of the
  # same length
  waveform = tf.cast(waveform, tf.float32)
  equal_length = tf.concat([waveform, zero_padding], 0)
  spectrogram = tf.signal.stft(
      equal_length, frame_length=255, frame_step=128)

  spectrogram = tf.abs(spectrogram)

  return spectrogram

Can I create a spectogram from mp3?
What this does? zero_padding = tf.zeros([16000] - tf.shape(waveform), dtype=tf.float32)
Why we casting it to 32? waveform = tf.cast(waveform, tf.float32)
Do I need a degree in SoundPreformatted text Engineering to use this? Cause it all seems gibberish to me.
Now the worst part, “Run inference on an audio file”.
sample_file = data_dir/‘no/01bb6a2a_nohash_0.wav’sample_ds = preprocess_dataset([str(sample_file)])
for spectrogram, label in sample_ds.batch(1):
prediction = model(spectrogram)
plt.bar(commands, tf.nn.softmax(prediction[0]))  plt.title(f’Predictions for “{commands[label[0]]}”’)
plt.show()
So let me see, I generated a model and now its time to use it! So according to this guide, I need to create a dataset with just one entry do sample_ds.batch(1) because again I just have on entry and then magic I use the model I just create!
Shouldn’t instead be explicit in this tutorial how to correctly save the model(including its classes) and then how could I use this model? For example to beep every time I say a trained word with the mic or count the course of a trained word in a file mp3. As it is I don’t think I could possibly use this to turn my house lights on.
I also checked the @tensorflow-models/speech-commands, but again it’s kind of useless when you can only use a pre-defined model. Instead of explaining how to convert a given .h5 model into a JSON and then how to load it.
Sadly, I’m really disappointed;; I wanted to be able to do much more with this and I think this has a future, but as it is is really hard and unpractical to use;; My understanding of audio itself is really limited. Also, I notice even when I train this model the thresholds are ridiculous, if I say a given word not trained by the model this model will classify it in one of the classes really high, I was expecting more entropy in the classification. Again I’m no expert in this field and I was just doing this for fun.

Context: I’m sorry for the text not been formated the way I wanted it;_; Still, I would like to hear some feedback. Maybe a good tutorial or something along those lines would be great. I have always been fascinated with Sound and making this streamline would greatly increase TensorFlow usage.


Question:
Hello Community,
I am pretty new to tensorflow even when I have done lots of trainings though. To get a better understanding, I am asking for some help with my use case:
I want to classify vectors in class a and class b.

A vector consists of 10 numerical features.
For the supervised learning the vectors are labeled as class a or class b.
Not only the content of a single vector is important to classify it to a class but also the previous 20 vectors should define, if the actual vector belongs to class a or b

My first approach looks like this:
import tensorflow as tf
import pandas as pd
import array as arr
import numpy as np

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(1, 17)), #8 oder 17
  tf.keras.layers.Dense(256, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(256, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(100)
])

x_train = tf.constant(np.genfromtxt('training_data.csv',delimiter=','))
x_label = tf.constant(np.genfromtxt('label_data.csv',delimiter=','))

predictions = model(x_train[:1]).numpy()
tf.nn.softmax(predictions).numpy()
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
loss_fn(x_label[:1], predictions).numpy()
model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['accuracy'])
model.fit(x_train, x_label, epochs=200)
model.evaluate(y_test,  y_label, verbose=2)

I think this is far too simple. I have no clue what shape or architecture the network should have. After 3 epochs there is no change on the loss function anymore. I have no idea if this approach makes any sense at all. And I have no clue how to solve requirement 3) from my use case.
Any help is deeply appreciated.
BR, Ray

Context:
To be able to use previous 20 samples to classify the current sample you can use LSTM layer: tf.keras.layers.LSTM  |  TensorFlow Core v2.7.0 1
LSTM goes first after the input layer - it could be a single layer or a stack of several layers.
You should reshape the data before passing it to the model, so than each sample is 2-dimensionsl and has the shape [20,  10].


Question:
Hi
I am trying to implement a TFX pipeline with custom components. As a start I am creating a custom ExampleGen component which takes images and masks as inputs and creates TFRecords as output. I have run this component in the interactive context to verify the output. But which I hook it up to the kubeflow pipeline, I get a failure with the following error
 → 290       input_artifact_spec.task_output_artifact.producer_task = producer_id
291       input_artifact_spec.task_output_artifact.output_artifact_key = output_key
292       task_spec.inputs.artifacts[name].CopyFrom(input_artifact_spec)
TypeError: None has type NoneType, but expected one of: bytes, unicode
Here the producer_id and the outpu_key is not set. Do I need to set them with some value when I configure the component?
Here is how I am setting up the component
import tfx
from tfx import v1
def _create_pipeline(pipeline_name: str, pipeline_root: str, data_root: str,
module_file: str, serving_model_dir: str,
) → tfx.v1.dsl.Pipeline:
“”“Creates a three component penguin pipeline with TFX.”""

Brings data into the pipeline.
#example_gen = tfx.components.CsvExampleGen(input_base=data_root)
#test_context = InteractiveContext()
data_root = os.path.join(DATA_ROOT, ‘buildings/’, ‘images’)
#pdb.set_trace()
examples = data_root
input_artifact = tfx.types.standard_artifacts.Examples()
input_artifact.uri = data_root
input_artifact.split_names = artifact_utils.encode_split_names([‘train’, ‘val’])
input_channel = tfx.types.channel_utils.as_channel(artifacts=[input_artifact])
ingest_images = CustomIngestionComponent(
input=input_channel, name=‘ImageIngestionComponent’)

Uses user-provided Python function that trains a model.
trainer = tfx.v1.components.Trainer(
module_file=module_file,
examples=ingest_images.outputs[‘examples’],
train_args=tfx.v1.proto.TrainArgs(num_steps=100),
eval_args=tfx.v1.proto.EvalArgs(num_steps=5))

Pushes the model to a filesystem destination.
pusher = tfx.v1.components.Pusher(
model=trainer.outputs[‘model’],
push_destination=tfx.v1.proto.PushDestination(
filesystem=tfx.v1.proto.PushDestination.Filesystem(
base_directory=serving_model_dir)))

Following three components will be included in the pipeline.
components = [
ingest_images,
#trainer,
#pusher,
]
return tfx.v1.dsl.Pipeline(
pipeline_name=pipeline_name,
pipeline_root=pipeline_root,
components=components)
PIPELINE_DEFINITION_FILE = PIPELINE_NAME + ‘_pipeline.json’
runner = tfx.v1.orchestration.experimental.KubeflowV2DagRunner(
config=tfx.v1.orchestration.experimental.KubeflowV2DagRunnerConfig(),
output_filename=PIPELINE_DEFINITION_FILE)

Following function will write the pipeline definition to PIPELINE_DEFINITION_FILE.
outpipe = _create_pipeline(
pipeline_name=PIPELINE_NAME,
pipeline_root=PIPELINE_ROOT,
data_root=DATA_ROOT,
module_file=os.path.join(MODULE_ROOT, _trainer_module_file),
serving_model_dir=SERVING_MODEL_DIR)
_ = runner.run( outpipe)

Context:
Hi Subhasish,
It looks like you’re trying to create a custom ExampleGen which reads data from files, which we refer to as a FileBasedExampleGen.  See Custom File-Based ExampleGen 5
There is also an example which reads Avro files: Avro Example
I hope that helps!


Re: Mobile Development Using expo(react native) and tensorflow?

Question: Hi, so I was wondering if anyone had any insight into whether it’s better to use tensorflow mobilenet, tensorflow lite, tensorflow react-native, or just train a tensorflow js model and deploy into my mobile app that involves real time object detection?

Context:
Its better to use Tensorflow lite to perform object detection on Mobile app.
Example for Objection detection model on Mobile app using Tensorflow lite is well demonstrated here 18
.


Object Detection Android App creates error with model from tflite-model-maker (it had worked for many weeks a until a few weeks ago)

Question:
Hi there,
I am creating a custom Android App for object detection. Therefore, I use the Tensorflow Object Detection Android App from here: examples/lite/examples/object_detection/android at master · tensorflow/examples · GitHub 11
I am training my models with TFlite model maker with the following code:

!pip install -q tflite-model-maker
!pip install -q pycocotools
#----------------Python code--------------------------
import numpy as np
import os
from tflite_model_maker.config import ExportFormat
from tflite_model_maker import model_spec
from tflite_model_maker import object_detector
import tensorflow as tf
assert tf.version.startswith(‘2’)
tf.get_logger().setLevel(‘ERROR’)
from absl import logging
logging.set_verbosity(logging.ERROR)
spec = model_spec.get(‘efficientdet_lite0’)
test_data = object_detector.DataLoader.from_pascal_voc(’./test’, ‘./test’, label_map={1: “Ball”, 2: “Spieler Rot”, 3: “Spieler Gelb”})
train_data = object_detector.DataLoader.from_pascal_voc(’./train’, ‘./train’, label_map={1: “Ball”, 2: “Spieler Rot”, 3: “Spieler Gelb”})
validation_data = object_detector.DataLoader.from_pascal_voc(’./valid’, ‘./valid’, label_map={1: “Ball”, 2: “Spieler Rot”, 3: “Spieler Gelb”})
model = object_detector.create(train_data, model_spec=spec, batch_size=16, train_whole_model=True, validation_data=validation_data, epochs=1)
model.evaluate(test_data)
model.export(export_dir=’.’)

This worked without any errors for many weeks. Now I get the following error in Android Studio:
Output tensor at index 0 is expected to have 3 dimensions, found 2.
My dataset is exactly the same and I train on Google Colab. I am sure that I didn’t change anything on the Android App.
I look forward for your answers
Have an nice day.
Greetings,
Daniel Hauser

Context:
Hi @Daniel_Hauser
In which build variant are you getting the error? lib_task or lib_interpreter?
Upload somewhere the tflite file and give us a link so we can verify the output shape.
Thanks


Is the SAC agent in tensorflow up to date?

Question:
I found this:

…there is a new version of the algorithm that uses only a Q function and disposes of the V function. It also adds automatic discovery of the weight of the entropy term called the ‘temperature’

By looking at the current code, I do not see this temperature parameter, or is it just named different?
Reference:
https://arxiv.org/abs/1812.05905 3

Context:
Hi Janis, those are two different algorithms, CQL-SAC and SAC.
CqlSacAgent 1 implements the CQL algorithm for continuous control domains from “Conservative Q-Learning for Offline Reinforcement Learning” 1 (Kumar, 20).
The SACAgent uses target_entropy described in the paper and the Automating Entropy Adjustment described in the paper.


Question:
I’ve set up PyCharm on my M1 machine as per the exam handbook’s instructions.
However, running the script below:
import tensorflow as tf
print(tf.version)
Gives this error: Process finished with exit code 132 (interrupted by signal 4: SIGILL)
From my google search, the exam’s version of TensorFlow seems incompatible with my machine.
But the handbook was very strict about the tensorflow version - otherwise the grader won’t work.
How do I resolve this?
To offer context, my macOS is version 11.4 with the M1 chip.

I’ve tried both intel and the apple silicon versions of Pycharm Community.
The command “python3 --version” successfully shows python 3.8.6 (3.8.x is required for exam)
I’ve tried installing the apple tensorflow for 11.x os versions.

All return the same error.

Context: I got the same error. Although I have tried different solutions and also installed the apple fork of TensorFlow.


Avoid MaxPool3D conversion to FlexMaxPool3D

Question:
I used Colab to convert a keras model with some MaxPool3D layers to a TFLite model.
But by looking at the converted model with netron.app, I found it was converted to a FlexMaxPool3D.
AFAIK, such a model is not directly usable for inference (in my case, I’ll use it in JavaScript).
Although it is an op listed as implemented ( Supported Select TensorFlow operators  |  TensorFlow Lite 3 ), how can I have it properly converted?

Context: What is your TF version?


Question:
I’m having issues building tensorflow+sonnet applications with pyinstaller. I’ve built keras ones fine, but sonnet ones have an issue as below.
Sonnet doesn’t seem to work with pyinstaller. This can be reproduced with a very simple example as below:
import sonnet
print(“Hello world”)
I used a spec file:

-- mode: python ; coding: utf-8 --

The following fixes a different pyinstaller issue due to recursion
import sys
sys.setrecursionlimit(5000)
block_cipher = None
a = Analysis([‘pi_test.py’],
pathex=[‘c:\Software\ICServer\ICServer-src\Python\GraphNet\minimal_pyinstaller_test’],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name=‘pi_test’,
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name=‘pi_test’)
The error is:
2021-11-08 19:42:43.507236: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
Traceback (most recent call last):
File “site-packages\tensorflow\python\autograph\pyct\parser.py”, line 150, in parse_entity
File “site-packages\tensorflow\python\autograph\pyct\inspect_utils.py”, line 147, in getimmediatesource
File “inspect.py”, line 798, in findsource
OSError: could not get source code
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “site-packages\tensorflow\python\autograph\impl\api.py”, line 749, in to_graph
File “site-packages\tensorflow\python\autograph\impl\api.py”, line 284, in _convert_actual
File “site-packages\tensorflow\python\autograph\pyct\transpiler.py”, line 286, in transform
File “site-packages\tensorflow\python\autograph\pyct\transpiler.py”, line 470, in transform_function
File “site-packages\tensorflow\python\autograph\pyct\transpiler.py”, line 346, in transform_function
File “site-packages\tensorflow\python\autograph\pyct\parser.py”, line 152, in parse_entity
ValueError: Unable to locate the source code of <function BaseBatchNorm.call at 0x000002BF2C3C84C0>. Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:\Software\ICServer\ICServer-src\Python\GraphNet\minimal_pyinstaller_test2\pi_test.py”, line 1, in
import sonnet
File “”, line 991, in _find_and_load
File “”, line 975, in _find_and_load_unlocked
File “”, line 671, in load_unlocked
File “c:\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py”, line 623, in exec_module
exec(bytecode, module.dict)
File "site-packages\sonnet_init.py", line 21, in
File “”, line 991, in _find_and_load
File “”, line 975, in _find_and_load_unlocked
File “”, line 671, in _load_unlocked
File “c:\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py”, line 623, in exec_module
exec(bytecode, module.dict)
File “site-packages\sonnet\distribute.py”, line 21, in
File “”, line 991, in _find_and_load
File “”, line 975, in _find_and_load_unlocked
File “”, line 671, in _load_unlocked
File “c:\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py”, line 623, in exec_module
exec(bytecode, module.dict)
File “site-packages\sonnet\src\distribute\batch_norm.py”, line 22, in
File “”, line 991, in _find_and_load
File “”, line 975, in _find_and_load_unlocked
File “”, line 671, in _load_unlocked
File “c:\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py”, line 623, in exec_module
exec(bytecode, module.dict)
File “site-packages\sonnet\src\batch_norm.py”, line 34, in
File “site-packages\sonnet\src\batch_norm.py”, line 132, in BaseBatchNorm
File “site-packages\sonnet\src\utils.py”, line 180, in smart_autograph
File “site-packages\tensorflow\python\autograph\impl\api.py”, line 752, in to_graph
tensorflow.python.autograph.impl.api.ConversionError: converting <function BaseBatchNorm.call at 0x000002BF2C3C84C0>: ValueError: Unable to locate the source code of <function BaseBatchNorm.call at 0x000001EE45EA81F0>. Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code
I reported this to the sonnet developers and the closed the issue straight away saying it wasn’t their problem and I should report it to tensorflow developers

      github.com/deepmind/sonnet

      Sonnet and pyinstaller

        opened

        Nov 9, 2021

          closed

        Nov 13, 2021

          dmagee

    Sonnet doesn't seem to work with pyinstaller. This can be reproduced with a very… simple example as below:

```
import sonnet
print("Hello world")
```
I used a spec file:

```
# -*- mode: python ; coding: utf-8 -*-

# The following fixes a different pyinstaller issue due to recursion
import sys
sys.setrecursionlimit(5000)

block_cipher = None

a = Analysis(['pi_test.py'],
             pathex=['c:\\Software\\ICServer\\ICServer-src\\Python\\GraphNet\\minimal_pyinstaller_test'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='pi_test',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='pi_test')
```

The error is:

```
2021-11-08 19:42:43.507236: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library cudart64_110.dll
Traceback (most recent call last):
  File "site-packages\tensorflow\python\autograph\pyct\parser.py", line 150, in parse_entity
  File "site-packages\tensorflow\python\autograph\pyct\inspect_utils.py", line 147, in getimmediatesource
  File "inspect.py", line 798, in findsource
OSError: could not get source code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "site-packages\tensorflow\python\autograph\impl\api.py", line 749, in to_graph
  File "site-packages\tensorflow\python\autograph\impl\api.py", line 284, in _convert_actual
  File "site-packages\tensorflow\python\autograph\pyct\transpiler.py", line 286, in transform
  File "site-packages\tensorflow\python\autograph\pyct\transpiler.py", line 470, in transform_function
  File "site-packages\tensorflow\python\autograph\pyct\transpiler.py", line 346, in transform_function
  File "site-packages\tensorflow\python\autograph\pyct\parser.py", line 152, in parse_entity
ValueError: Unable to locate the source code of <function BaseBatchNorm.__call__ at 0x000002BF2C3C84C0>. Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Software\ICServer\ICServer-src\Python\GraphNet\minimal_pyinstaller_test2\pi_test.py", line 1, in <module>
    import sonnet
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "c:\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\sonnet\__init__.py", line 21, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "c:\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\sonnet\distribute.py", line 21, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "c:\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\sonnet\src\distribute\batch_norm.py", line 22, in <module>
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "c:\python38\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\sonnet\src\batch_norm.py", line 34, in <module>
  File "site-packages\sonnet\src\batch_norm.py", line 132, in BaseBatchNorm
  File "site-packages\sonnet\src\utils.py", line 180, in smart_autograph
  File "site-packages\tensorflow\python\autograph\impl\api.py", line 752, in to_graph
tensorflow.python.autograph.impl.api.ConversionError: converting <function BaseBatchNorm.__call__ at 0x000002BF2C3C84C0>: ValueError: Unable to locate the source code of <function BaseBatchNorm.__call__ at 0x000002BF2C3C84C0>. Note that functions defined in certain environments, like the interactive Python shell do not expose their source code. If that is the case, you should to define them in a .py source file. If you are certain the code is graph-compatible, wrap the call using @tf.autograph.do_not_convert. Original error: could not get source code
```
The code works fine within the python interpreter, but within pyinstaller it seems to have trouble.

Sonnet is written in pure python, and the method in question (in src/batch_norm.py) is decorated with @utils.smart_autograph. Can anyone suggest how to get this working?
Thanks
Derek

Context:
Is this a gast version issue?

      github.com/deepmind/sonnet

      Import error  1

        opened

        Sep 10, 2019

          closed

        Sep 10, 2019

          ignacio-rocco

    I created a fresh conda environment with python 3.7.4 and installed tf2 and sonn…et2 with:
```
$ pip install "tensorflow-gpu>=2.0.0rc0" --pre
$ pip install "dm-sonnet>=2.0.0b0" --pre
```
However, when I try to import sonnet I get the following error:
```
Python 3.7.4 (default, Aug 13 2019, 20:35:49)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> import sonnet as snt
Traceback (most recent call last):
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py", line 614, in to_graph
    return conversion.convert(entity, program_ctx)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/conversion.py", line 322, in convert
    free_nonglobal_var_names)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/conversion.py", line 240, in _convert_with_cache
    entity, program_ctx)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/conversion.py", line 469, in convert_entity_to_ast
    nodes, name, entity_info = convert_func_to_ast(o, program_ctx)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/conversion.py", line 669, in convert_func_to_ast
    node = node_to_graph(node, context)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/conversion.py", line 699, in node_to_graph
    node = converter.apply_(node, context, function_scopes)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/core/converter.py", line 409, in apply_
    node = converter_module.transform(node, context)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/converters/function_scopes.py", line 120, in transform
    return FunctionBodyTransformer(ctx).visit(node)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/core/converter.py", line 346, in visit
    return super(Base, self).visit(node)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/pyct/transformer.py", line 480, in visit
    result = super(Base, self).visit(node)
  File "/anaconda3/envs/sonnet/lib/python3.7/ast.py", line 262, in visit
    return visitor(node)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/converters/function_scopes.py", line 87, in visit_FunctionDef
    node = self.generic_visit(node)
  File "/anaconda3/envs/sonnet/lib/python3.7/ast.py", line 317, in generic_visit
    value = self.visit(value)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/core/converter.py", line 346, in visit
    return super(Base, self).visit(node)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/pyct/transformer.py", line 480, in visit
    result = super(Base, self).visit(node)
  File "/anaconda3/envs/sonnet/lib/python3.7/ast.py", line 262, in visit
    return visitor(node)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/converters/function_scopes.py", line 44, in visit_Return
    value=node.value)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/pyct/templates.py", line 261, in replace
    replacements[k] = _convert_to_ast(replacements[k])
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/pyct/templates.py", line 223, in _convert_to_ast
    return gast.Name(id=n, ctx=None, annotation=None)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/gast/gast.py", line 19, in create_node
    format(Name, nbparam, len(Fields))
AssertionError: Bad argument number for Name: 3, expecting 4

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/sonnet/__init__.py", line 25, in <module>
    from sonnet import nets
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/sonnet/nets.py", line 22, in <module>
    from sonnet.src.nets.cifar10_convnet import Cifar10ConvNet
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/sonnet/src/nets/cifar10_convnet.py", line 22, in <module>
    from sonnet.src import batch_norm
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/sonnet/src/batch_norm.py", line 30, in <module>
    class BaseBatchNorm(base.Module):
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/sonnet/src/batch_norm.py", line 126, in BaseBatchNorm
    scale=None, offset=None):
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/sonnet/src/utils.py", line 172, in smart_autograph
    f_autograph = tf.autograph.to_graph(f)
  File "/anaconda3/envs/sonnet/lib/python3.7/site-packages/tensorflow_core/python/autograph/impl/api.py", line 618, in to_graph
    entity, e.__class__.__name__, str(e)))
tensorflow.python.autograph.impl.api.ConversionError: converting <function BaseBatchNorm.__call__ at 0x7fb3de0db5f0>: AssertionError: Bad argument number for Name: 3, expecting 4
```


Question:
Hi All,
We are trying to use tensorflow over spark for numerical computation and further visualize in tensorboard for calculation transperancy.
For TF1.0 - found this cool library - GitHub - databricks/tensorframes: [DEPRECATED] Tensorflow wrapper for DataFrames on Apache Spark 1
which actually allows us to define a spark dataframe column as a tensor and apply operations on top of that.
However, it’s deprecated now. The Github page suggests to use Pandas UDF which does not work with @tf.function decorator and hence we cannot visualize stuff with tensorboard.
In summary, we are interested in the below:

Tensorflow with apache spark.
Manipulate Spark dataframe as Tensors.
Visualize the operations in tensorboard.

Appreciate any leads on this.
Thanks.

Context:
Tensorflow >=2.0 with apache spark references

 Distributed TensorFlow on Apache Spark 3.0 1

TensorFlowOnSpark 1


Mapping strings to ints during preprocessing stage

Question:
Hi everyone!
I’m looking to train several models - image classification - on a large dataset which I want to represent as TFRecord files. Today I know how to do it if I save the image (tf.string) and class number (int64). I’d like to be able to set the class numbers during preprocess from a saved string.
Motivation - the dataset I’m working on contains images which can be classified as a more general category (i.e. dogs, cats) and more specifically (labrador, bulldog, persian cat). The labels are mostly more specific, so an image can go though a cascade of a more general category (model of cat/dog/negative), and then a more nuanced model (labrador/bulldog/neg).
I’m relatively new to the framework, and I was able to express what I want in eager execution mode using .numpy() method (code below).
My question is how can this mapping be done efficiently? what methods should I look for?
Thanks
import tensorflow as tf
import functools

# example: this model classifies dogs and cats
example_label_map = {
    "bulldog": "dog",
    "labrador": "dog",
    "persian cat": "cat",
    "cow": "neg",
}

# map the N classes to an int of 0..N-1
labels_to_class_int = {
    "neg": 0,   # negative class
    "dog": 1,
    "cat": 2,
}

def parse_tfrec_function(example, labels_map):
    """
    labels is a map of labels to class numbers. e.g. "bulldog" -> 1, "labrador" -> 1, "persian cat" -> 2, ...
    """
    image_feature_description = {
        "image": tf.io.FixedLenFeature([], tf.string),
        "label": tf.io.FixedLenFeature([], tf.string)
    }

    features = tf.io.parse_single_example(example, image_feature_description)

    image = tf.io.parse_tensor(features["image"], tf.uint8)
    label = features["label"].numpy().decode('utf-8')
    class_num = labels_map[label]

    return image, class_num

tfrec_file = ['/srv/mickey/build-cls/test/data/dataset.tfrec']
dataset = tf.data.TFRecordDataset(tfrec_file)

labels_map = {}
for k, v in example_label_map.items():
    labels_map[k] = labels_to_class_int[v]

print(labels_map)

parser_fn = functools.partial(parse_tfrec_function, labels_map=labels_map)
parset_dataset = dataset.map(parser_fn) # this breaks due to AttributeError for Tensor object

Context:
You can use function tf.keras.utils.image_dataset_from_directory() to read images from directory and map the labels as integer value.
main_directory/
...cat/
......cat_image_1.jpg
......cat_image_2.jpg
...dog/
......dog_image_1.jpg
......dog_image_2.jpg
...neg/
......neg_image_1.jpg
......neg_image_2.jpg

Calling image_dataset_from_directory(main_directory, labels='inferred') will return a tf.data.Dataset that yields batches of images from the subdirectories cat and dog and neg , together with labels 0 ,1 and 2 (0 corresponding to cat and 1 corresponding to dog, 2 corresponding to neg ).
Specify the label_mode as int to get int value.
label_mode
- 'int': means that the labels are encoded as integers(e.g. for `sparse_categorical_crossentropy` loss).

For more information on the library please refer here


Beginner questions after training my first custom object detection model

Question:
Hi!
I have trained my first custom object detection model with 2 classes and 100 test/500 train images and have some questions.
Nothing has been pretrained. I’m running efficientdet_d0_coco17_tpu-32 with my own labeled images created with LabelImg.

Low initial total loss from first step (starts from 1.5)

Total loss down to 0.2 after just 2000 steps, wth? After that loss is increased if continued

Low score when predicting

Should you resize your train/test images to one size or should they be the original size?

Should initial total loss not go up when you retrain with new train/test images? Started at last checkpoint with old images removed, right now it continues on last value (0.2)

My goal is to detect vehicles/license plates, is 500 images not enough for this?

I’m using my model in react-native (expo eas), the following has been done:

Clone tensorflow models
github/tensorflow/models
Setup my configuration files, (using efficientdet_d0_coco17_tpu-32)
Train model untill loss is no longer decreasing
Export using exporter_main_v2
Converted my saved model to tensorflowjs model
github.com/tensorflow/tfjs-converter/blob/0.8.x/README.json.md

Loaded my model using bundleResourceIO
@tensorflow/tfjs-react-native - npm

Is this “the correct way” if you want to use your own custom object detection model in react-native? Maybe I should be using tflite instead?
Really appreciate the help!

Context:
It seems that you are going to overfit this dataset.
Have you tried to use image augmentation, collect more real images or generating synthetic one? 500 images with a from scratch model it seems to me a little bit too small dataset specially if you want to perform “in the wild”.
For the TF.js converter the Readme was moved here:

      github.com

    tfjs/tfjs-converter at master · tensorflow/tfjs

  master/tfjs-converter

  A WebGL accelerated JavaScript library for training and deploying ML models.


While doing quantization, Is it possible to specify the scale and zero point to tensorflow int8 kernel?

Question:
I want to convert float32 model to 8-bit integer by using post training quantization. And follow the webs page and use tflite convertor to do that.  But some operator may cause error,  because it does not comply with the restriction of int8 spec
In case of dense layer:
if dense layer with no bias or zero bias, it will cause error. The message shows below
tensorflow/lite/kernels/kernel_util.cc:106 std::abs(input_product_scale - bias_scale) <= 1e-6 * std::min(input_product_scale, bias_scale)

I found the bias scale is 0, so, that error occur. I need to specified the bias scale to avoid that problem.
In case of slice or stride_slice:
If I concatenate multiple stride_slice tensor.  The scale of all concatenated elements has different scale.
Thus,  i want to specified the scale and zero point to make sure the model is comply with int8 spec.
Is there any API supported to do that?  thanks!

Context:
Hi,
Currently we do not support the manual scale setting API, we’re relying on automatic representative dataset calibrator. I believe you can try to navigate the flatbuffer and modifying some constants, although this may not guarantee the model to work properly with interpreter afterwards.
This looks like an unintended behavior for TF team as well, if you can provide the model PTQ code it would be easier for the team to investigate. Thanks!


Audio metadata problem with speech recognition

Question:
Hello! I’m still working on Simple audio recognition: Recognizing keywords  |  TensorFlow Core 1 and I was unable to use one of my own audio files in place of the training files. When I try to use it, I get the following error: InvalidArgumentError: Dimension -6522 must be >= 0
[[{{node zeros}}]] [Op:IteratorGetNext]
What am I doing wrong?
https://drive.google.com/drive/folders/1QRgtnXqaT9Eu29BENzDiVceBb62KVx9k?usp=sharing 1

Context: Hi @Henry_Sommer , The drive link shared by you is not an open link and it asks for access.


Benchmarking: Jetson Nano vs. Coral Devboard

Question:
Hello Community,
I’m super new to Tensorflow and single-board computers such as the jetson nano or the coral devboard. I am currently doing my masters and for one of my projects I am tasked with comparing the nano to the coral devboard. Before I start: please - feel free to correct my vocabulary/terminology, should I be talking nonsense or making no sense.
I have set up both boards and am kind of stuck. I have plans but am hitting a wall, hoping to get a few clues by some experienced member(s).
I had to ditch my first approach, which was to write an algorithm (e.g. large matrix multiplication) that can run on the “driver level” for the GPU for the nvidia board and on the “driver level” of the TPU on the google board. The idea was to get some algorithm up and running as natively as possible for both plattforms but I’m also a C#/C++ newb and I figured writing cuda kernels let alone tensor base-functions is probably out of scope for me.
The next idea is finding ‘common’ applications and running them on both devices. There are some imagenet neural net architectures that are both available for the jetson and the coral devboard - but I wonder… are those even comparable? The TPU architecture limits the coral devboard running 8BIT precision models whereas the jetson nano does not have this restriction. Moreover the coral devboard can only run tensorflow lite optimized models which have been trained using quantization whereas the jetson nano is not going to use its full potential with tensorflow (lite or not does not matter) at all if not “written in” tensorflowRT. At least that’s what my research is suggesting. Is this a valid benchmarking approach nevertheless?
The last idea is to just go for the “Hello World” of Deeplearning, throw in the MNIST Dataset and train a specific tensorflow architecture which I then want to convert/optimize to work for tensorflowRT and tensorflowLite. Does this even make sense?
Any hints are appreciated. I haven’t really anybody who is helping me in this regard at the moment. I’m trying my best to come up with ideas but right now I fail to make any proper progress. Maybe there is something I haven’t considered yet at all or someone can say that some of the ideas are not going to work out or something like that. Anything is appreciated!
Big Thanks for your time,
JT

Context:
Hello,
Don’t worry you were asking right question here.
It may not be possible to do apples to apples comparison directly. But there are few avenues where you can compare performance of hardware in Deep learning models. I would like to keep this bench marking simple by just comparing a fixed set of models.

Comparison of int8.tflite models
there are quite few models which are supported on both hardware ( Mobilenet, Unet, Resenet). You can find Coral version of models here ( coral.ai/models ). So you can compare inference time of cpu int8.tflite on Jetson and edgetpu.tflite on Coral.

Comparison of FP16 vs Edgetpu.tflite
You can use same models from above. But compare inference time of FP16 models on Jetson vs Edgetpu.tflite models on Coral.  Since Fp16 models are 2x the size of Int8 models; just to keep the compute work load fair export the coral models with 2x input shape.

I assume your project is limited to inference time comparison only.
Hope this helps,
Good luck


Cannot install tensorflow on python 3.10

Question:
I am trying to install tensorflow on Fedora Linux with python 3.10, but getting this error:
$ python3.10 -m pip install tensorflow
Defaulting to user installation because normal site-packages is not writeable
ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
ERROR: No matching distribution found for tensorflow
$

Could somebody please help me?
Thanks in advance!

Context:
Tensorflow support for Python 3.10 is not yet released. Tensorflow 2.8 might release end of the year with Python 3.10 support.
Kindly check this similar issue Support Python 3.10? - #5 by mihaimaruseac 268


Which tensorflow version for mac M1 for developer certificate exam?

Question: I wanna participate the exam of the tensorflow developer certificate. I have an Mac with M1 so tensorflow is only running on tensorflow-macos in PyCharm. The exam requirements say you need tensorflow 2.5. My question is if I can still use tensorflow-macos?

Context: Have you found the answer for this?


Error in reducing Tensorflow lite binary size

Question:
Hi, I was recently trying to selectively reduce the tf delegates for a model link here using Reduce TensorFlow Lite binary size. I have setup docker using the guide here ( Build TensorFlow Lite for Android) and I have no problem building fat binaries only issue is the tf-delegate reduction. I have tried both tensorflow:devel and tensorflow:devel-latest containers both resulting in the same error behavior resembling (Server terminated abruptly (error code: 14, error message: ‘Socket closed’ · Issue #41480 · tensorflow/tensorflow · GitHub)
The steps after setting up docker are as follows

copy my models to the container to a folder using docker cp

run

bash tensorflow/lite/tools/build_aar.sh --
input_models=/host_dir/smallbert_L6_H128,/host_dir/smallbert_L12_H128_mean.tflite --target_archs=arm64-v8a,armeabi-v7a

Error occurs during the same exact phase and almost the same files if I remember correctly, over couple of tried I did. Also the compilation stops and the timer continues for next 100-200 sec depending each time, while reading disk at about 2GB per sec the entire minute or so the timer continues.

pwsh in Github 04-11-2021 01_52_31 AM1136×683 31.3 KB

one possiblity that found was low memory but I have plenty of ram left for the process … so   .

Just one more thing I would like to mention, I have tried compiling custom binaries by editing BUILD file maybe adding the specific build is possible for tf delegates I need but I have no experience how to go about approching it either

tflite_jni_binary(
    name = "libtensorflowlite_jni_normal_with_gpu.so",
    linkscript = ":tflite_version_script.lds",
    deps = [
        "//tensorflow/lite/c:c_api",
        "//tensorflow/lite/c:c_api_experimental",
        "//tensorflow/lite/delegates/nnapi/java/src/main/native",
        "//tensorflow/lite/delegates/xnnpack:xnnpack_delegate",
        "//tensorflow/lite/java/src/main/native",
        "//tensorflow/lite/delegates/gpu/java/src/main/native",
    ],
)

Any help would be appreciated.

Context:
Just an update, I thought I should mention the cause which might help others who view this tread on a later time. The time bazel server was exiting had nothing to do with the scripts that I was running.
When I created the issue on my laptop which was running windows and docker for this purpose. Bazel server exiting was not caused by some bad command but memory constrains, my observations while doing it on linux was that, the memory usage spikes while compiling the mkl library with a delta above 1.5Gigs. The WSL2 being used by Docker to manage the memory outright refuses to provide because of my limited 8G ram causing the bazel to error out. On linux it basically crashed.
The solution was pretty easy I had to compile on my desktop which had more ram available. Docker Hub : Sid911/tflite-builder 2 This is the image I uploaded for my own devops use, just in case someone doesn’t want to build it they can use it. I will update it per month for my own project but that’s not guaranteed.


Question:
I have following saved_model dir structure. The saved_model_cli is working correctly with the path but docker command not. Note this is following the book example and apparently docker part of example not working.
test.npy test input feeds 3 instances of mnist 28x28 images to serving.
DIR STRUCT:
root@nonroot-Standard-PC-i440FX-PIIX-1996:~/dev-learn/gpu/tflow/tensorflow/tflow-2nded# tree p297
p297
├── 0001
│   ├── assets
│   ├── saved_model.pb
│   └── variables
│       ├── variables.data-00000-of-00001
│       └── variables.index
├── assets
├── keras_metadata.pb
├── saved_model.pb
└── variables
    ├── variables.data-00000-of-00001
    └── variables.index

CLI:
    saved_model_cli run --dir p297/0001 --tag_set serve --signature_def serving_default --inputs
flatten_input=test.npy

output of cli (OK)
2021-11-08 15:57:11.458910: 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:  SSE3 SSE4.1 SSE4.2 AVX AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-11-08 15:57:11.460906: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1510] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 31740 MB memory:  -> device: 0, name: Device 738c, pci bus id: 0000:00:07.0
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/tools/saved_model_cli.py:445: load (from tensorflow.python.saved_model.loader_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.loader.load or tf.compat.v1.saved_model.load. There will be a new function for importing SavedModels in Tensorflow 2.0.
INFO:tensorflow:Restoring parameters from p297/0001/variables/variables
2021-11-08 15:57:11.961912: I tensorflow/core/common_runtime/gpu_fusion_pass.cc:507] ROCm Fusion is enabled.
2021-11-08 15:57:12.671939: I tensorflow/core/common_runtime/gpu_fusion_pass.cc:507] ROCm Fusion is enabled.
2021-11-08 15:57:12.686081: I tensorflow/core/common_runtime/gpu_fusion_pass.cc:507] ROCm Fusion is enabled.
Result for output key dense_2:
[[3.65022061e-05 2.47262960e-05 6.37578269e-05 2.08125566e-05
  7.02261750e-05 1.18829332e-01 4.30839646e-05 2.72816449e-01
  1.48763061e-02 5.93046188e-01 6.37045162e-07 3.02645799e-06
  3.80635288e-06 4.22269186e-05 6.12226586e-06 4.33605646e-06
  7.58367719e-07 3.06199559e-06 5.42078442e-06 2.38417056e-06
  4.93106018e-06 7.25027712e-06 1.24132812e-05 1.24427579e-05
  1.15528803e-06 4.87520847e-05 1.68714314e-06 8.28819338e-07
  2.23448342e-06 9.11506140e-06]
 [1.64286757e-04 3.39760754e-06 9.66621459e-01 1.61247503e-04
  1.02249524e-02 7.44288286e-07 2.27110237e-02 3.17756710e-10
  1.12835020e-04 3.98790796e-08 4.89366492e-10 1.29820976e-09
  9.63464334e-13 5.25168797e-10 9.53418247e-11 4.82374418e-10
  2.60897762e-11 2.75338996e-12 3.59164387e-09 7.25419169e-11
  5.41757861e-10 8.33503266e-10 1.25494719e-11 4.14474233e-09
  3.54530544e-10 7.37128275e-11 4.25408209e-10 7.22836443e-11
  6.95292546e-10 2.50320233e-11]
 [1.12954825e-04 9.99067128e-01 5.11940962e-05 5.15281979e-04
  2.25681084e-04 7.90114484e-07 2.39875553e-05 1.08763277e-06
  6.77304740e-07 8.83030324e-08 3.44932758e-08 3.36465895e-08
  1.49800372e-09 2.67271858e-08 7.71939810e-08 1.00484840e-07
  5.05017761e-09 4.95800823e-09 1.20519260e-07 1.41827059e-07
  1.35087987e-07 3.95625591e-07 4.27236913e-08 4.71499533e-08
  3.37586954e-08 1.78841599e-08 1.00948716e-08 4.40149028e-10
  2.45303022e-09 2.79508638e-09]]

DOCKER:
    MODEL_NAME=p297
    docker pull tensorflow/serving
    docker run -it --rm -p 8500:8500 -p 8501:8501 \
        -v "$MODEL_NAME:/models/$MODEL_NAME" \
        -e MODEL_NAME=$MODEL_NAME \
        tensorflow/serving

output of docker instance (fail):
Using default tag: latest
latest: Pulling from tensorflow/serving
Digest: sha256:6651f4839e1124dbde75ee531825112af0a6b8ef082c88ab14ca53eb69a2e4bb
Status: Image is up to date for tensorflow/serving:latest
docker.io/tensorflow/serving:latest
2021-11-09 00:01:43.627821: I tensorflow_serving/model_servers/server.cc:89] Building single TensorFlow model file config:  model_name: p297 model_base_path: /models/p297
2021-11-09 00:01:43.628201: I tensorflow_serving/model_servers/server_core.cc:465] Adding/updating models.
2021-11-09 00:01:43.628227: I tensorflow_serving/model_servers/server_core.cc:591]  (Re-)adding model: p297
2021-11-09 00:01:43.629366: W tensorflow_serving/sources/storage_path/file_system_storage_path_source.cc:268] No versions of servable p297 found under base path /models/p297. Did you forget to name your leaf directory as a number (eg. '/1/')?
2021-11-09 00:01:44.629551: W tensorflow_serving/sources/storage_path/file_system_storage_path_source.cc:268] No versions of servable p297 found under base path /models/p297. Did you forget to name your leaf directory as a number (eg. '/1/')?
2021-11-09 00:01:45.629769: W tensorflow_serving/sources/storage_path/file_system_storage_path_source.cc:268] No versions of servable p297 found under base path /models/p297. Did you forget to name your leaf directory as a number (eg. '/1/')?
^X2021-11-09 00:01:46.629997: W tensorflow_serving/sources/storage_path/file_system_storage_path_source.cc:268] No versions of servable p297 found under base path /models/p297. Did you forget to name your leaf directory as a number (eg. '/1/')?

I logged onto container using
docker-exec -it <C_ID> /bin/bash
and inspected p297/0001 folder but turns out to be empty.

Context: I think the conflict could be due to the fact that you have both leaf directory 0001 and actual Saved Model in your Model directory. Can you try moving 0001 to a separate folder? Or Saved Model to 0002 for example?