How to handle input/output with a tensorflow lite linear regression model

Question:
Hi TensorFlow team,
I have created a linear regression model that takes a pandas dataframe with five columns and generates an output of array of float arrays of 3 elements. To illustrate this, I have created a Colab notebook linear_regression.ipynb under Colab Notebooks - Google Drive 2
I have converted the model to Tensorflow Lite (see linear_regression.tflite in the same folder that has the notebook). I am now trying to figure out how to use this model in Java to run on Android. Below is my initial code:
LinearRegression model = LinearRegression.newInstance(context);
double features[] = {0.195312, 0.05913, 0.089844, 0.550781, 0.320312};
ByteBuffer bb = ByteBuffer.allocate(features.length * 4);
for (double f : features)
    bb.putFloat((float) f);
// The default code suggested in Android Studio mentions it should be new int[]{1, 1}, so I am not sure
TensorBuffer tb = TensorBuffer.createFixedSize(new int[]{1, 5}, DataType.FLOAT32);
tb.loadBuffer(bb);
LinearRegression.Outputs outputs = model.process(tb);

Sure enough, I got the following exception:
java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (serving_default_normalization_input:0) with 4 bytes from a Java Buffer with 20 bytes.

What is the correct way of passing the input to the model and getting output from the model?

Context:
Hi George_Zheng,
Have you tried without multiplying with * 4? Does it work?
Also I do not think it is necessary to pass ByteBuffer inside the model. You can use directly the float array. You will have lower speed …about 1ms


Question: Is it possible to have a tfjs model on a webpage that is accessible, on in react? For instance if there was a a object recognizer or something on a webpage that was built by me using a tfjs model, can i just put that link in the react native app, that a user can press and be able to use it as if it were a webpage on my laptop, leveraging the same model? just with video stream coming from iphone and android rather than webcam?

Context:
TensorFlow.js runs anywhere JavaScript can run so yes that includes mobile devices via the mobile browser too.
On Android getUserMedia is very well supported - you can access front or rear facing cameras with out issue. On iOS you will  need to use mobile safari possibly as iOS ecosystem is very closed, but it does work! You can check websites like can i use for support: getUserMedia/Stream API | Can I use... Support tables for HTML5, CSS3, etc 1
If you are using in native application then I believe you would need to use react native implementation of TFJS to do so:

      blog.tensorflow.org

TensorFlow.js for React Native is here!

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

Like this guy did:

      Medium – 20 Jul 21

How to use tensorflow.js in react.js — Sentimental Analysis

  In March 2018, Google announced Tensorflow.js version 1.0, which enable developer to use machine learning (ML) models directly in browser…

    Reading time: 5 min read


Question:
Hi, I just started to learn TensorFlow.
I got the message as below after inputting
npm install
How can I fix this?

npm ERR! code 1

npm ERR! path /Users/svj5706/Documents/Machine Learning/MLKits-master/knn-tf/node_modules/@tensorflow/tfjs-node

npm ERR! command failed

npm ERR! command sh -c node scripts/install.js

npm ERR! * Downloading libtensorflow

npm ERR!

npm ERR! * Building TensorFlow Node.js bindings

npm ERR! /Users/svj5706/Documents/Machine Learning/MLKits-master/knn-tf/node_modules/@tensorflow/tfjs-node/scripts/install.js:154

npm ERR! throw new Error('node-gyp rebuild failed with: ' + err);

npm ERR! ^

npm ERR!

npm ERR! Error: node-gyp rebuild failed with: Error: Command failed: node-gyp rebuild

npm ERR! gyp info it worked if it ends with ok

npm ERR! gyp info using node-gyp@7.1.2

npm ERR! gyp info using node@16.13.0 | darwin | x64

npm ERR! gyp info find Python using Python version 2.7.16 found at "/usr/bin/python"

npm ERR! (node:88710) [DEP0150] DeprecationWarning: Setting process.config is deprecated. In the future the property will be read-only.

npm ERR! (Use `node --trace-deprecation ...` to show where the warning was created)

npm ERR! gyp info spawn /usr/bin/python

npm ERR! gyp info spawn args [

npm ERR! gyp info spawn args '/Users/svj5706/node_modules/node-gyp/gyp/gyp_main.py',

npm ERR! gyp info spawn args 'binding.gyp',

npm ERR! gyp info spawn args '-f',

npm ERR! gyp info spawn args 'make',

npm ERR! gyp info spawn args '-I',

npm ERR! gyp info spawn args '/Users/svj5706/Documents/Machine Learning/MLKits-master/knn-tf/node_modules/@tensorflow/tfjs-node/build/config.gypi',

npm ERR! gyp info spawn args '-I',

npm ERR! gyp info spawn args '/Users/svj5706/node_modules/node-gyp/addon.gypi',

npm ERR! gyp info spawn args '-I',

npm ERR! gyp info spawn args '/Users/svj5706/Library/Caches/node-gyp/16.13.0/include/node/common.gypi',

npm ERR! gyp info spawn args '-Dlibrary=shared_library',

npm ERR! gyp info spawn args '-Dvisibility=default',

npm ERR! gyp info spawn args '-Dnode_root_dir=/Users/svj5706/Library/Caches/node-gyp/16.13.0',

npm ERR! gyp info spawn args '-Dnode_gyp_dir=/Users/svj5706/node_modules/node-gyp',

npm ERR! gyp info spawn args '-Dnode_lib_file=/Users/svj5706/Library/Caches/node-gyp/16.13.0/<(target_arch)/node.lib',

npm ERR! gyp info spawn args '-Dmodule_root_dir=/Users/svj5706/Documents/Machine Learning/MLKits-master/knn-tf/node_modules/@tensorflow/tfjs-node',

npm ERR! gyp info spawn args '-Dnode_engine=v8',

npm ERR! gyp info spawn args '--depth=.',

npm ERR! gyp info spawn args '--no-parallel',

npm ERR! gyp info spawn args '--generator-output',

npm ERR! gyp info spawn args 'build',

npm ERR! gyp info spawn args '-Goutput_dir=.'

npm ERR! gyp info spawn args ]

npm ERR! Traceback (most recent call last):

npm ERR! File "/Users/svj5706/node_modules/node-gyp/gyp/gyp_main.py", line 51, in <module>

npm ERR! sys.exit(gyp.script_main())

npm ERR! File "/Users/svj5706/node_modules/node-gyp/gyp/pylib/gyp/__init__.py", line 670, in script_main

npm ERR! return main(sys.argv[1:])

npm ERR! File "/Users/svj5706/node_modules/node-gyp/gyp/pylib/gyp/__init__.py", line 662, in main

npm ERR! return gyp_main(args)

npm ERR! File "/Users/svj5706/node_modules/node-gyp/gyp/pylib/gyp/__init__.py", line 647, in gyp_main

npm ERR! generator.GenerateOutput(flat_list, targets, data, params)

npm ERR! File "/Users/svj5706/node_modules/node-gyp/gyp/pylib/gyp/generator/make.py", line 2457, in GenerateOutput

npm ERR! part_of_all=qualified_target in needed_targets,

npm ERR! File "/Users/svj5706/node_modules/node-gyp/gyp/pylib/gyp/generator/make.py", line 804, in Write

npm ERR! part_of_all,

npm ERR! File "/Users/svj5706/node_modules/node-gyp/gyp/pylib/gyp/generator/make.py", line 1005, in WriteActions

npm ERR! "Spaces in action input filenames not supported (%s)" % input

npm ERR! AssertionError: Spaces in action input filenames not supported (/Users/svj5706/Documents/Machine Learning/MLKits-master/knn-tf/node_modules/@tensorflow/tfjs-node/scripts/deps-stage.js)

npm ERR! gyp ERR! configure error

npm ERR! gyp ERR! stack Error: `gyp` failed with exit code: 1

npm ERR! gyp ERR! stack at ChildProcess.onCpExit (/Users/svj5706/node_modules/node-gyp/lib/configure.js:351:16)

npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:390:28)

npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)

npm ERR! gyp ERR! System Darwin 18.7.0

npm ERR! gyp ERR! command "/usr/local/bin/node" "/Users/svj5706/node_modules/.bin/node-gyp" "rebuild"

npm ERR! gyp ERR! cwd /Users/svj5706/Documents/Machine Learning/MLKits-master/knn-tf/node_modules/@tensorflow/tfjs-node

npm ERR! gyp ERR! node -v v16.13.0

npm ERR! gyp ERR! node-gyp -v v7.1.2

npm ERR! gyp ERR! not ok

npm ERR!

npm ERR! at /Users/svj5706/Documents/Machine Learning/MLKits-master/knn-tf/node_modules/@tensorflow/tfjs-node/scripts/install.js:154:13

npm ERR! at ChildProcess.exithandler (node:child_process:404:5)

npm ERR! at ChildProcess.emit (node:events:390:28)

npm ERR! at maybeClose (node:internal/child_process:1064:16)

npm ERR! at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)

npm ERR! A complete log of this run can be found in:

npm ERR! /Users/svj5706/.npm/_logs/2021-12-02T07_44_05_986Z-debug.log

Context: Can you provide more details about the platform etc you are running on? What are you running npm install on exactly? From the first error it seems no spaces are allowed in path names and your path has a space in it.


Not able to Install an old version of tensorflow

Question:
How can i install an old version of Tensorflow in conda vertual environnement?
I want Tensorflow-gpu 1.4, when i tape this commande :

conda install tensorflow-gpu=1.4

I see the message in attached picture.
Capture769×533 15.9 KB

Context:
You can try with pip install
pip install tensorflow-gpu==1.4.0
Tensorflow 1.4 is very old version, it is highly recommended to use latest Tensorflow version for better result.


Question: I would like to fully understand Tensorflow mechanisms. To complete the task, I would like to build a debug library of Tensorflow 2.4, write a program using Tensorflow’s C++ API, link my object files to the Tensorflow debug library and debug the program using gdb/cuda-gdb. Can I get specific steps of building the debug library that can be used to fulfill my purpose?

Context:
What is the best approach to debug in TensorFlow when working with the C++ code base? General Discussion

    In the past, I’ve had issues debugging in TensorFlow where the problem was somewhere in the C++ code base and I was using gdb, these included debug builds being too large (using -O0) and running out of space, recompile time etc. Does anyone have recommendations to handle debugging in TensorFlow?


Error when using TFLite interpreter in Flask

Question:
I have quantized model with float32. After making tflite model it’s predicting perfectly with single image but when using in while loop it’s showing an error. I tried to follow the instruction of TensorFlow here but didn’t understand their way.
CODE:
def generate_frames(frame):
    while True:

        image = cv2.resize(frame,(256,256))

        #converting into float32
        image = tf.image.convert_image_dtype((image/255.0), dtype=tf.float32).numpy()

        image = run_inference(np.expand_dims(image[:,:,:3], axis=0))

        final_result = (image*255).astype(np.uint8)

        ret,buffer=cv2.imencode('.jpg',final_result)

        frame=buffer.tobytes()

        return frame

#load model
def load_trained_model():
    global interpreter, input_details, output_details
    interpreter = tf.lite.Interpreter(model_path="quant_model.tflite")
    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

def run_inference(image):
    # perform inference and parse the outputs
    interpreter.set_tensor(input_details[0]['index'], image)
    interpreter.invoke()
    outputs = interpreter.get_tensor(output_details[0]['index'])[0]

    return outputs

if __name__ == '__main__':
    load_trained_model()
    app.run(debug=True)

ERROR:

RuntimeError: There is at least 1 reference to internal data in the
interpreter in the form of a NumPy array or slice. Be sure to only
hold the function returned from tensor() if you are using raw data
access.

Context:
Hi @MSI
Maybe tf.expand_dims instead of np.expand_dims?
Also inside generate_frames maybe you want to return image instead of frame?
Best


Question:
When running model.evaluate_tflite('model.tflite', test_data) after training an object detection model with the Tensorflow Lite Model Maker 6 I’m getting the following error: InvalidArgumentError: required broadcastable shapes [Op:Mul].
This can also be observed in the Google Codelab example 46.
Any help is greatly appreciated.

Context: Adding @Yuqi_Li for the visibility.


How to share layers when we use model subclassing?

Question:
When we use sequential, we can just call the model multiple times as shown in the example below.
embedding_model = Sequential([
    Dense(128, activation='elu', input_shape=(Xa_train.shape[1],)),
    Dense(64, activation="elu"),
    Dense(emb_size, activation='linear')
])

input_anchor = Input(shape=(Xa_train.shape[1],))
input_positive = Input(shape=(Xa_train.shape[1],))
input_negative = Input(shape=(Xa_train.shape[1],))

embedding_anchor = embedding_model(input_anchor)
embedding_positive = embedding_model(input_positive)
embedding_negative = embedding_model(input_negative)

output = concatenate([embedding_anchor, embedding_positive, embedding_negative], axis=1)

tnn = Model([input_anchor, input_positive, input_negative], output)

Here, I tried to build the model via model-subclassing and shared the weight.
class TNN_model(tf.keras.Model):
    def __init__(self, input_dim, latent_dim=2, name="autoencoder", **kwargs
    ):
        super(TNN_model, self).__init__(name=name, **kwargs)
        self.model_layer1 = Dense(128, activation="elu", input_shape=(input_dim,))
        self.model_layer2 = Dense(64, activation="elu")
        self.model_output = Dense(latent_dim, activation="linear")

    def call(self, data):
        x = self.model_layer1(data)
        x = self.model_layer2(x)
        x_out = self.model_output(x)

        return x_out

class ModelTrain(tf.keras.Model):
    def __init__(self, model):
        super(ModelTrain, self).__init__()
        self.model = model

    def compile(self, optimizer, loss):
        super(ModelTrain, self).compile()
        self.optimizer = optimizer
        self.loss = loss

    def train_step(self, data):
        x, y = data
        Xa_train_x, Xp_train_x, Xn_train_x = x[0], x[1], x[2]

        with tf.GradientTape() as tape:
            model_out_anchor = self.model(Xa_train_x)
            model_out_positive = self.model(Xp_train_x)
            model_out_negative = self.model(Xn_train_x)

            output = [model_out_anchor, model_out_positive, model_out_negative]

            loss, loss_positive, loss_negative = self.loss(output)

        grads = tape.gradient(loss, self.model.trainable_variables)

        self.optimizer.apply_gradients(zip(grads, self.model.trainable_variables))

        return {'total_loss': loss, 'loss_positive': loss_positive, 'loss_negative': loss_negative}

def triplet_loss(output_encoder):

    anchor, positive, negative = output_encoder[0], output_encoder[1], output_encoder[2]

    positive_dist = tf.reduce_mean(tf.square(anchor - positive), axis=1)
    negative_dist = tf.reduce_mean(tf.square(anchor - negative), axis=1)

    loss = tf.maximum(0., positive_dist - negative_dist)

    return loss, positive_dist, negative_dist

au_TNN = TNN_model(Xa_train_final.shape[1], emb_size)
training = ModelTrain(au_TNN)
training.compile(optimizer=Adam(), loss=triplet_loss)

output_dummy = np.zeros((Xa_train_final.shape[0], emb_size))

history = training.fit([Xa_train_final, Xp_train_final, Xn_train_final], [output_dummy, output_dummy, output_dummy],
                          epochs=number_epoch, batch_size=batch_size_value,
                          shuffle=True, verbose=2)

I used the same loss function (very basic triplet loss) and dataset for both examples (sequential and sub-classing); however, while sequential training loss went down, sub-classing training loss was very unsteady. It seems that instead of sharing the layers (model), it created three models. I could not find any example or documentation for this.
Is sharing model in this way correct for Keras model sub-classing?
with tf.GradientTape() as tape:
            model_out_anchor = self.model(Xa_train_x)
            model_out_positive = self.model(Xp_train_x)
            model_out_negative = self.model(Xn_train_x)

I use TensorFlow 2.3.0

Context:
Solved.
It seems like the problem was with the fit function. In the second example, everything works as expected if I use a custom training loop.


Question:
Hi everybody,
When I am running the code pasted below, the model is just training for “multiplier” =1 or =4.
Running the same code in google colab → just training for multiplier=1
Is there any mistake in how I am using DenseNet here?
Thanks in advance, appreciate your help!
import numpy as np
import tensorflow as tf
from tensorflow.keras.applications.densenet import DenseNet201
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import BinaryCrossentropy

random_array = np.random.rand(128,128,3)
image = tf.convert_to_tensor(
    random_array
)
label = tf.constant(0)

model = DenseNet201(
    include_top=False, weights='imagenet', input_tensor=None,
    input_shape=(128, 128, 3), pooling=None, classes=2
)
model.compile(
optimizer=Adam(),
loss=BinaryCrossentropy(),
metrics=['accuracy'],
)

for multiplier in range(1,20):

    print(f"Using multiplier {multiplier}")
    x_train = np.array([image]*multiplier)
    y_train = np.array([label]*multiplier)

    try:
        model.fit(x=x_train,y=y_train, epochs=2)
    except:
        print("Not training...")
        pass

Context:
Some advice:

Test small pieces  before trying to put them together.
Read your error messages.

The problem is that you set “include_top=False”:
model = DenseNet201(
  include_top=False, weights='imagenet', input_tensor=None,
  input_shape=(128, 128, 3), pooling=None, classes=2
)

So the output is a batch of feature maps:
result = model(x_train)
result.shape

TensorShape([2, 4, 4, 1920])

And you’ve set the label to [0,0] and the loss function to BinaryCrossentropy.
model.compile(
  optimizer=Adam(),
  loss=BinaryCrossentropy(),
  metrics=['accuracy'],
  run_eagerly=True,
)

It’s failing because bce=BinaryCrossentropy(); bce([0,0], result) doesn’t work.
That’s what the error message was trying to tell you.


Question:
Hi,
I trained a model with a tensorflow.keras.Model object. I want to average the model parameters over the last ~10 epochs and then use the average model to predict on my test set.
Is there a simple way to do this?

Context:
TensorFlow

Model Averaging  |  TensorFlow Addons 3


Can ssd mobilenetV2 transfer learning be done with tensorflow?

Question:
hello everyone!
been surfing on Doc + lot of tutorials but i dont quite get what im doing wrong.
What im atempting to do is relativly simple: trying to do transfer learning on the Ssd mobilnetv2 network. I’ve followed this tutorial here 3 , but when i arrrive to this step , the model i want to use is not the mobilenet v2 but Ssd Mobilenet v2. It doesnt seems to exist a tf.keras.application.ssdmobilnetv2 , so i’ve decided to use the model that is aviable on tensorFlow hub   ( this one ) . so i’ve researched on how to get a Tensorflow python Model object to be able to follow the tutorial steps, and i’ve encountered this other tutorial 1 . the thing is, apparently ssd MobilnetV2 has multiple outputs, so i cant call tf.keras.Sequential    with the kerasLayer obtained. Then  the documentation says that when having multiple outputs, you need to use the Functional API. the thing is, how can i call the Functional API with my ssd_mobilnetv2 downloaded on THUB? there seems to be no examples on the web. here is the Python sample of code im trying to make work
    "https://tfhub.dev/tensorflow/ssd_mobilenet_v2/2",
    input_shape=(300, 300, 3),
    dtype=tf.uint8,
    trainable=False)

num_classes = len(class_names)
print(num_classes)

#### using the Functional api ( doesnt work)
model= tf.keras.Model(
              feature_extractor_layer,
              tf.keras.layers.Dense(num_classes))
#### using keras.Sequential, doesnt work too
model = tf.keras.Sequential([
               feature_extractor_layer,
              tf.keras.layers.Dense(num_classes)
              ])

model.summary()````

i would be glad if someone can help me out!

best regards

Context:
i cant edit first post coz of multiple links, but the code is missing first line
feature_extractor_layer = hub.KerasLayer(
   "https://tfhub.dev/tensorflow/ssd_mobilenet_v2/2",
    input_shape=(300, 300, 3),
    dtype=tf.uint8,
    trainable=False)

num_classes = len(class_names)
print(num_classes)

#### using the Functional api ( doesnt work)
model= tf.keras.Model(
              feature_extractor_layer,
              tf.keras.layers.Dense(num_classes))
#### using keras.Sequential, doesnt work too
model = tf.keras.Sequential([
               feature_extractor_layer,
              tf.keras.layers.Dense(num_classes)
              ])

model.summary()


Question:
The original image is in shape (720, 1280) and contains a bounding box. I need to make it square without disturbing the spatial information.  Please look at the below picture.
Demonstration image. 11
I’m not sure how to add such up-down padding to this image to make it a square shape. The width is 1280 and the height is 720. So, to make it square I need to add a border of shape (1280-720) = 560 - divided by 2 - 280 px up and down. How to do this in tensorflow efficiently and dynamically?

Context: use tf.pad with mode ‘CONSTANT’ should work.


Suggestion for OSS PR workflow: Rebasing instead of Merging?

Question:
Hello TensorFlow team,
I just got to know this forum after the TF contributor summit which was held today. I would like to appreciate the organizers and all the contributors for their such a great effort to keep the TF community growing.
I have a request or suggestion for the entire TensorFlow team’s workflow, or more broadly, for the majority of all the Google OSS projects. I wasn’t sure whom or how should I reach out to about this, but it looks like this is the perfect place to make the suggestion regarding general development workflow.
TL;DR: Can we have pull requests from OSS contributors checked in by “rebasing” or kind of “squashing” rather than “merging” (i.e. creating a merge commit), so that the git graph doesn’t look intimidating?
For example, if you clone the tensorflow repo and look at the git commit tree, you can see a large number of Kraken-like branches and parallel lines:

tensorflow_git_repo1818×1620 279 KB

This happened due to merge commits for PRs: the main branch changes so fast, so the merge-base commit can be hundreds of commits ago when the PR gets checked in, unless rebased. In my opinion, this makes reading and following the commit history very difficult.
I can see why this might happen: creating a merge commit is the default behavior in merging PRs in Github, and in Google’s internal VCS (the primary repository to host the code) everything gets “synchronized” and linearized but the workflow hasn’t cared as much how the synchronized git repository will look like.
However, it would be really great if the git history could be maintained as linear and clean as possible, which I think will be helpful for OSS contributors. Most of PRs would consist of one or few commits, so I think there is no much reason of retaining their base commit unless really needed (for example, some branch had been long-lived when consisting of many big commits). Google’s internal VCS (Piper) maintains also all the changesets in a linear fashion, so it would be a reasonable choice for the OSS repositories as well. That being said, I also understand that “merging” (rather than rebasing) often might be a better choice when merging a PR, but in most of the case rebasing or squashing would work better as a default choice.
Any opinions, second-thoughts, discussion about the OSS workflow will be greatly appreciated.
Thank you!
Best,
Jongwook

Context:
Hi.
In general, I agree that rebasing is better. However, this does not really work here. Internally we use a different version control software that can hold code from all of Google. It is similar to perforce.
This entails that every internal change must be atomic, just on single version number change. That is, each PR must correspond to just one single item in history. So, merge commits are the only solution here.


Supplying custom benchmark tensor to loss/metric functions

Question:
Certain loss/metric functions like UMBRAE and MASE make use of a benchmark - typically the “naïve forecast” which is 1 period lag of the target.
However in my dataset, I’m using hourly data to train/predict monthly returns. So in essence my naïve forecast isn’t 1 row behind, it’s N rows behind where N can change over time, especially when dealing with monthly timeframes (some months are shorter/longer than others).
I already have a feature called bars_in_X where X is one of D, W, M, Y respectively for each timeframe (though for the sake of argument, I’m only using M). It’s an integer that references the 1-period-ago row wrt the timeframe. So for bars_in_D, that would typically be 24 (as there are 24 Hours in 1 Day). i.e., the naïve forecast for the hourly value NOW happened 24 bars ago.
As a halfway measure, I find the mean of each of those features in the dataset and before creating the model I make custom loss functions that are supplied this value (see how here 2). This produces a usable, but technically incorrect result because it’s a static backreference as opposed to the dynamic bars_in_X value. It would also be an insufficient method for when I eventually want to find the naïve forecast for ALL timeframes (not just one).
Does anyone have a suggested method of handling this kind of situation?

Context:
My first guess is that your “loss function” should be an an instance of a class that has a build-in circular-memory buffer implemented in a tf.Variable.
Make the buffer large enough that you always have the record you need to go back to look at.
If you’re using keras, you’ll need to train_step so you can thread the bars_in_x feature through to the loss function.

      TensorFlow

Customize what happens in Model.fit  |  TensorFlow Core


Datasets, iterators and compiled tf.functions

Question:
I am hitting some error involving XLA and data.
This is a reproducible short test:
    data = tf.data.Dataset.from_tensor_slices(list(range(10)))
    data = data.repeat()
    iterator = iter(data)

    @tf.function(jit_compile=False)
    def run(it):
        t = 0
        for _ in range(10):
            x = next(it)
            t += x
        return t

    @tf.function(jit_compile=True)
    def run_compiled(it):
        t = 0
        for _ in range(10):
            x = next(it)
            t += x
        return t

    print(run(iterator))
    print(run_compiled(iterator))

Basically the non-compiled version (run) works fine, while the compiled one (run_compiled) is returning the following error:
tensorflow.python.framework.errors_impl.InternalError: Failed copying input tensor from /job:localhost/replica:0/task:0/device:CPU:0 to /job:localhost/replica:0/task:0/device:GPU:0 in order to run __inference_run_compiled_61: No unary variant device copy function found for direction: 1 and Variant type_index: tensorflow::ResourceDeleter [Op:__inference_run_compiled_61]

I am in the following tf version: 2.7.0-dev20210806
Any idea on what is going on?
Thanks!

Context:
The error’s a little clearer if you run it without the GPU “Detected unsupported operations when trying to compile graph”:
InvalidArgumentError: Detected unsupported operations when trying to compile
 graph __inference_run_compiled_86[_XlaMustCompile=true,config_proto=3175580994766145631,executor_type=11160318154034397263]
on XLA_CPU_JIT: IteratorGetNext (No registered 'IteratorGetNext' OpKernel for XLA_CPU_JIT devices
compatible with node {{node IteratorGetNext}}){{node IteratorGetNext}}


Question: Hi evereyone, i face issued after keras model converted into tesorflow js, when i tried fetch model.json , got error ,

Context:
Did you follow the instructions mentioned
Importing a Keras model into TensorFlow.js 2.
Provide the error that you are getting.


Need help compiling TF Serving with custom TF kernels

Question:
Hey, I am looking for some help to debug a compilation issue for Serving. I have a TensorFlow fork with some custom kernel that compiles and runs without an issue. Unfortunately when I try to compile Serving with this same TensorFlow I am unable to get past this point. After a quick google search and multiple iterations I think those warnings are not the error that stopped the build. Does anyone any suggestions for how to get bazel to provide more information about the error that is preventing the build?
root@32863057b38d:/tensorflow/tm/serving# bazel --host_jvm_args="-Xms1024m" --host_jvm_args="-Xmx2048m" build --color=y
es --curses=yes --host_javabase="@local_jdk//:jdk" --verbose_failures -c opt tensorflow_serving/model_servers:tensorflo
w_model_server
DEBUG: Rule 'io_bazel_rules_docker' indicated that a canonical reproducible form can be obtained by modifying arguments
 shallow_since = "1556410077 -0400"
DEBUG: Repository io_bazel_rules_docker instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule git_repository defined at:
  /root/.cache/bazel/_bazel_root/4ec26d1a12bea518b8df81fbc61cec69/external/bazel_tools/tools/build_defs/repo/git.bzl:19
5:33: in <toplevel>
WARNING: /root/.cache/bazel/_bazel_root/4ec26d1a12bea518b8df81fbc61cec69/external/org_tensorflow/tensorflow/compiler/xl
a/client/lib/BUILD:57:11: in hdrs attribute of cc_library rule @org_tensorflow//tensorflow/compiler/xla/client/lib:comp
arators: file 'libliteral_util.a' from target '@org_tensorflow//tensorflow/compiler/xla:literal_util' is not allowed in
 hdrs. Since this rule was created by the macro 'cc_library', the error might have been caused by the macro implementat
ion
WARNING: /root/.cache/bazel/_bazel_root/4ec26d1a12bea518b8df81fbc61cec69/external/org_tensorflow/tensorflow/compiler/xl
a/client/lib/BUILD:57:11: in hdrs attribute of cc_library rule @org_tensorflow//tensorflow/compiler/xla/client/lib:comp
arators: file 'libliteral_util.pic.a' from target '@org_tensorflow//tensorflow/compiler/xla:literal_util' is not allowe
d in hdrs. Since this rule was created by the macro 'cc_library', the error might have been caused by the macro impleme
ntation
WARNING: /root/.cache/bazel/_bazel_root/4ec26d1a12bea518b8df81fbc61cec69/external/org_tensorflow/tensorflow/compiler/xl
a/client/lib/BUILD:57:11: in hdrs attribute of cc_library rule @org_tensorflow//tensorflow/compiler/xla/client/lib:comp
arators: file 'libliteral_util.so' from target '@org_tensorflow//tensorflow/compiler/xla:literal_util' is not allowed i
n hdrs. Since this rule was created by the macro 'cc_library', the error might have been caused by the macro implementa
tion
WARNING: errors encountered while analyzing target '//tensorflow_serving/model_servers:tensorflow_model_server': it wil
l not be built
INFO: Analyzed target //tensorflow_serving/model_servers:tensorflow_model_server (9 packages loaded, 5410 targets confi
gured).
INFO: Found 0 targets...
ERROR: command succeeded, but not all targets were analyzed
INFO: Elapsed time: 27.122s, Critical Path: 0.02s
INFO: 0 processes.
FAILED: Build did NOT complete successfully

Context:
Are you following this guide:

      TensorFlow

Serving TensorFlow models with custom ops  |  TFX 7


Choosing appropriate multiclass loss and metric

Question: I have a multi-class classification problem and the model I train gives an overall reasonable accuracy but fails in one or a few smaller classes (very low performance only on that class). I think part of the reason is that I use regular CrossEntropy as my loss function so I am optimizing for overall accuracy and not enforcing minimum per-class performance in any way. Is there any loss function in Keras that can do that for me? In other words, how do I force my model to not just focus on overall accuracy but ensure that I will have a reasonable performance in all classes?

Context:
Use sigmoid activation on your output layer and binary_crossentropy as loss function.
softmax gives probability distribution around n-classes and it works well when the classes are mutually exclusive and not their probabilities.
Where as sigmiod provides independent probabilities.


Tensorflow lite inferencing is too slow on risc-v platform

Question:
Hi, do any one use tensorflow lite on risc-v platform?
I use both python tflite_runtime and c++ tensorflow lite environment on risc-v platform to do image detecting, however it shows that the performance is really bad.
as the table shows below, I use default param to do benchmark(tensorflow-r2.5\tensorflow\lite\tools\benchmark)

image1194×313 59.6 KB

in order to find whether software matters more to the result, I also compare the coremark scores on U540 and Raspberry 3B， it turns that coremark score gap is not that big as inference benchmark time(both cpu freq 1GHz).

image1122×265 47.7 KB

> Can  the above test   prove "tensorflow lite is bad fitted on risc-v platform " ?
**> **
> Or other low-level software support like math lib have not as good performance as ARM platform done?

Context:
What risc-v board do you have?
ARM has neon delegate:

      TensorFlow

TensorFlow Lite Delegates

In the new MLIR compiler stack they are working on:

      LLVM Discussion Forums – 23 Aug 21

[RFC] Add RISC-V Vector Extension (RVV) Dialect 2

  Hi,  I am writing to propose a RISC-V Vector extension (RVV) dialect. The RISC-V vector extension v1.0 candidate has been released. Currently, LLVM supports the stable release v0.10. RVV is rapidly emerging, I think applications and optimizations...

    Reading time: 13 mins 🕑
      Likes: 18 ❤


Where to find a reliable tutorial on “Strategies for training a model for image recognition”

Question:
Dear coder,
I am interested in a tutorial on how to setup layers for image recognition.
Background to this:
I have to pass +63 percent accuracy on recognize if a given image (150px | 150px) shows a cat or a dog. Running thru the provided tutorial-plan gave me information on how to setup the model, train it and how to use predict/evaluate but I have really no idea how to setup the layers to get a good result.
Can someone tell me where I can find tutorials/information about what I have to do (Strategies on building a stack of layer)?
Thank you!

Context:
Ahoi Thorsten,
sounds like you’re searching for a CNN (Convolutional Neural Network) Model & Architecture.
https://developers.google.com/machine-learning/glossary/#convolutional_layer 3
https://www.tensorflow.org/tutorials/images/cnn 2
For more insights and lectures, feel free to have a deep dive here:
https://cs231n.github.io/convolutional-networks/ 1
https://www.youtube.com/watch?v=iaSUYvmCekI 1
Interesting Codelab Tutorials:
Horse or Human (Python):
https://developers.google.com/codelabs/tensorflow-5-compleximages 3
Optimising CNN’s (Python):
https://developers.google.com/codelabs/tensorflow-4-cnns 1
MNIST with TF.js (JavaScript)
https://codelabs.developers.google.com/codelabs/tfjs-training-classfication/index.html#4 1
Some, already setup, browser based playground(s), e.g for visualisation purpose:
https://cs.stanford.edu/people/karpathy/convnetjs/demo/cifar10.html 1
https://cs.stanford.edu/people/karpathy/convnetjs/demo/mnist.html 1
(hmm, somehow i couldn’t find the tf.js playgrounds adhoc anymore …)
Maxpooled,
Dennis


Tensorflow: Error in gradient calculation for while loop in C++

Question:
I am trying to compute gradients with Tensorflow in C++. I have created a simple example that computes gradients of a while loop using the API function AddSymblicGradients . However, I am receiving the following error at runtime (the code compiles wtihout errors) that I do not understand:
2021-11-23 00:23:52.136520: E tensorflow/core/grappler/optimizers/meta_optimizer.cc:808] layout failed: Invalid argument: Invalid graph: Frame ids for node Test_While_Loop_grad/while/NextIteration_1 does not match frame ids for it's fanout Test_While_Loop_grad/while/Merge_1
2021-11-23 00:23:52.143769: F tf_while_loop_grad.cc:65] Non-OK-status: session.Run(grad_outputs, &outputs) status: Invalidargument: {{node Test_While_Loop_grad/while/Merge_1}} has inputs from different frames. The input {{node Test_While_Loop_grad/while/Enter_1}} is in frame 'Test_While_Loop_backprop'. The input {{node Test_While_Loop_grad/while/NextIteration_1}} is in frame 'Test_While_Loop'.

The following is the simplified code that reproduces the error:
#include <vector>
#include <iostream>

#include <tensorflow/cc/client/client_session.h>
#include <tensorflow/cc/framework/scope.h>
#include <tensorflow/cc/ops/standard_ops.h>
#include <tensorflow/core/framework/tensor.h>
#include <tensorflow/cc/ops/while_loop.h>
#include <tensorflow/cc/framework/gradients.h>

using namespace tensorflow;

Status LessThanOneYearCond(const Scope& s, const std::vector<Output>& inputs,
                           Output* output) {
    *output = ops::Less(s, inputs[0], 10.0);
    return s.status();
}

Status AddOneBody(const Scope& s, const std::vector<Output>& inputs,
                  std::vector<Output>* outputs) {

    auto dt = ops::Const(s, 1.0);
    outputs->push_back(ops::AddN(s, {inputs[0], dt}));
    auto next = ops::Add(s, inputs[1], dt);
    outputs->push_back(next);
    return s.status();
}

int main() {
    Scope root = Scope::NewRootScope();

    ClientSession session(root);

    std::vector<Output> init_values = {ops::Const(root, 0.0), ops::Const(root, 1.0)};
    std::vector<Output> loop_output;
    TF_CHECK_OK(ops::BuildWhileLoop(root, init_values, LessThanOneYearCond, AddOneBody,
                                    "Test_While_Loop", &loop_output));

    std::vector<Tensor> outputs;
    // Now lets compute the gradient
    std::vector<Output> grad_outputs;

    TF_CHECK_OK(AddSymbolicGradients(root, loop_output, init_values, &grad_outputs));
    TF_CHECK_OK(session.Run(grad_outputs, &outputs));

    std::cout << "Gradients: " << outputs[0].DebugString() << std::endl;

    return 0;
}

Context: Hello team - I would really appreciate any insights on the above question. If there are any further details that I can provide, please let me know.


Question:
Hello!
I am trying to install TensorFlow on my TurtleBot’s Raspberry Pi 4.  I am currently running Ubuntu 20.04 with ARM64 architecture.  Are there specific directions that someone could provide me?
Thank you for your time!

Context:
Please check this
Build TensorFlow Lite for ARM boards 12


Transfer learning and Quantization aware training. Subclassed model

Question:
Hello
I am getting the following error when trying to do quantization aware training with tensorflow 2.7:
ValueError: `to_quantize` can only either be a tf.keras Sequential or Functional model.

The error occurs when calling this method:
quantize_model = tfmot.quantization.keras.quantize_model(model)

The model is defined below. I suppose the reason is that subclassed models are not supported? I have already trained(normal training, not QAT) multiple models with the definition below. Post-training quantization works, but i would like to try quantization aware training to see if it improves performance. Is there a way to be able to do quantization aware training with the model below, or alternatively define it in another way and redo normal training.
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense, Flatten, Activation, Dropout
from tensorflow.keras.models import Model

class Mobilenet_v2_transfer(Model):

    def __init__(self):
        super(Mobilenet_v2_transfer, self).__init__()
        self.base = tf.keras.applications.mobilenet_v2.MobileNetV2(
            input_shape=(224, 224, 3), alpha=1.0, include_top=False, weights='imagenet',
            pooling='avg')
        self.base.trainable = True
        for layer in self.base.layers[:130]:
            layer.trainable =  False
        self.flatten = Flatten()
        self.dense = Dense(1, kernel_regularizer=tf.keras.regularizers.L2(0.01)
        self.sigmoid = Activation('sigmoid')

    def call(self, x):
        x = self.base(x)
        x = self.flatten(x)
        x = self.dense(x)
        x = self.sigmoid(x)
        return x

Context:
Have you checked:

      github.com/tensorflow/model-optimization

      QAT (quantization aware training) Support quantizing models recursively 3

        opened

        May 4, 2020

          CRosero

          feature request

          technique:qat

    **Describe the bug**
I'm doing transfer learning and would like to (at the end)… quantize my model. The problem is that when I try to use the _quantize_model()_ function (which is used successfully in numerous tutorials and videos), I get an error. How am I supposed to do quantization for transfer learning (using an already previously built model as a feature extractor)?

**System information**

TensorFlow installed from (source or binary): pip

TensorFlow version: tf-nightly 2.2.0

TensorFlow Model Optimization version: 0.3.0

Python version: 3.7.7

**Describe the expected behavior**
I expect the model to be successfully quantized and for no error messages to appear.

**Describe the current behavior**
I get the error: "ValueError: Quantizing a tf.keras Model inside another tf.keras Model is not supported."

**Code to reproduce the issue**
Can be found [here](https://colab.research.google.com/drive/17MES7jHh_BkngRgmGHhPhhujTsYwPMbe)


Question:
ValueError: Shapes (None, 1) and (None, 10) are incompatible
I use anaconda / spyder to test the tensorflow learning categorise the image and It occurs this error when I run the cnn

image1998×875 86.8 KB

Context: Check the shape of y_train and y_test. It seams that one of them is not one-hot encoded.


TCAV use in other applications beside image classification and text

Question: Hi, I’m trying to use TCAV for Object detection and semantic segmentation models, but I am having a hard time figuring out how to do it. For me is especially hard on the Object detection models that TensorFlow Object Detection API provides. The SSDMetaArch architecture is a weird combination of Keras and Graphs. Hence I’m totally lost. If anyone has a clue what the right approach to this problem is, will be awesome . I really appreciate any help you can provide.

Context:
Hi, to update my recent findings and issues.
Findings:

tracing back to the objectdetection class, I found out how to get the prediction layer of the SSD.

Also, I successfully recreate the loss function for this layer that works in the same fashion as KerasWrapper
Issues:

I copy the KerasWrapper class to build a new KerasWrapperSSD (the SSD layer are Keras layers).

This class is also on model.py of the TCAV repository

I load the model as I did to found the model’s layers, no error in that, except that the model seems that is no loading the backbone or any other layer. Hence I cannot extract the bottlenecks.

  def import_keras_model(self, saved_path):
    """Loads keras model, fetching bottlenecks, inputs and outputs."""
    self.ends = {}
    PATH_TO_CFG=saved_path + "/pipeline.config"
    PATH_TO_CKPT = saved_path + "/checkpoint"
    configs = config_util.get_configs_from_pipeline_file(PATH_TO_CFG)
    model_config = configs['model']
    detection_model = model_builder.build(model_config=model_config, is_training=False)
    ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
    ckpt.restore(os.path.join(PATH_TO_CKPT, 'ckpt-0')).expect_partial()
    self.model = detection_model
    print(self.model)
    print(self.model.feature_extractor.classification_backbone)
    self.get_bottleneck_tensors()
    self.get_inputs_and_outputs_and_ends()
    self.get_image_shape()

If I the load model in this fashion on other script works, but as a class of TCAV, the model is just a simple shell
I don’t know what is missing or what I’m doing wrong. I really, really appreciate any help.
Bests


Thoughts/Advice on a simple model for Chess

Question:
I’m trying to build a very basic model for Chess. What I’m doing is simply use the mnist tutorial and adapt it for this. I have a large collection of games with the end result for each game (win, loss, or draw). What I have done is take the final board position of each game and treat it as a 8x8 data element (empty square is 0 and each piece gets a different number). There are only 3 labels - 0, 1, and 2 (for loss, win, and draw). I have used about 150000 positions for this test.
When I train this data with the following model:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=[64, 1]),
tf.keras.layers.Dense(32, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(3, activation=tf.nn.softmax)
])
The accuracy is quite low. It gets up to 0.58 using 5 epochs. Using the same model with mnist data gets close to 100% accuracy).  Are there any issues with this approach? Is there something I’m missing? Any thoughts or advice is greatly appreciated.

Context:
Your task is not the same as classifying MNIST images. Images of the same class look similar, while there are numerous quite different board positions, which can be labeled as win or loss.
If your input data is 2-dimensional, then probably it would be better to make each sample in the shape of n_squares x n_figure_types. Imagine that you are one-hot encoding chess pieces for each of the 64 squares of the board.
Besides, you should somehow distinguish black and white figures. Either the input data should be 3D (square, figure type, color) or the number of categories in OHE should be twice the number of types of chess pieces.
Obviously, you should try to use some layers that can make sence of the relative positions of the pieces on the board instead of flattening the input data.


Question:
I upgraded to tensorflow 2.5.0 in new anaconda environment; Cuda 11.2 GPU Geforce 1060 (Windows laptop); numpy default 1.20.3
I am having an error in running RNN/GRU/LSTM codes that have been working fine in previous versions. No issues on CNN (1D/2D) and DNN (Dense layers).
The error msg: “NotImplementedError: Cannot convert a symbolic Tensor (lstm_2/strided_slice:0) to a numpy array. This error may indicate that you’re trying to pass a Tensor to a NumPy call, which is not supported”
Looking for help: Any suggestion from anyone?

Context:
Problem is with numpy version.
Tensorflow 2.5.0 version requires Numpy 1.19.5
!pip install tensorflow
Default it will install numpy 1.19.5


Question:
I was going through the mailing list 5 on the website and came across this broken link..
I guess this 4 is the file in the github repo.

Context: @Vansh_Sharma, Old mailing list was outdated and its updated now with correct link for documentation. Please check now for updated mailing lists 2. Thanks!


Found conflicts when installing Spyder in an environment with Tensorflow 2.5

Question:
Dear all,
I’m experiencing dependence conflicts when trying to install spyder in an environment that includes package tensorflow-gpu release 2.5 from conda pkgs/main channel.
conda environment is created as it shows conda info command:
 active environment : tf25
 active env location : C:\Users\MyUser\.conda\envs\tf25
        shell level : 2
        user config file : C:\Users\MyUser\.condarc
        populated config files : C:\Users\MyUser\.condarc
        conda version : 4.10.1
        conda-build version : 3.21.4
        python version : 3.8.8.final.0
        virtual packages : __cuda=11.2=0
                      __win=0=0
                      __archspec=1=x86_64

The enviroment includes only tensorflow-gpu 2.5 release and its dependencies
 C:\UsersMyUser>conda list
 # packages in environment at C:\Users\MyUser\.conda\envs\tf25:
 #
 # Name                    Version                   Build  Channel
 _tflow_select             2.1.0                       gpu
 abseil-cpp                20210324.2           hd77b12b_0
 absl-py                   0.13.0           py39haa95532_0
 aiohttp                   3.7.4            py39h2bbff1b_1
 astor                     0.8.1            py39haa95532_0
 astunparse                1.6.3                      py_0
 async-timeout             3.0.1            py39haa95532_0
 attrs                     21.2.0             pyhd3eb1b0_0
 blas                      1.0                         mkl
 blinker                   1.4              py39haa95532_0
 brotlipy                  0.7.0           py39h2bbff1b_1003
 ca-certificates           2021.5.25            haa95532_1
 cached-property           1.5.2                      py_0
 cachetools                4.2.2              pyhd3eb1b0_0
 certifi                   2021.5.30        py39haa95532_0
 cffi                      1.14.5           py39hcd4344a_0
 chardet                   3.0.4           py39haa95532_1003
 click                     8.0.1              pyhd3eb1b0_0
 coverage                  5.5              py39h2bbff1b_2
 cryptography              3.4.7            py39h71e12ea_0
 cudatoolkit               11.3.1               h59b6b97_2
 cudnn                     8.2.1                cuda11.3_0
 cython                    0.29.23          py39hd77b12b_0
 flatbuffers               2.0.0                h6c2663c_0
 gast                      0.4.0                      py_0
 giflib                    5.2.1                h62dcd97_0
 google-auth               1.32.0             pyhd3eb1b0_0
 google-auth-oauthlib      0.4.1                      py_2
 google-pasta              0.2.0                      py_0
 grpcio                    1.36.1           py39hc60d5dd_1
 h5py                      3.2.1            py39h3de5c98_0
 hdf5                      1.10.6               h7ebc959_0
 icc_rt                    2019.0.0             h0cc432a_1
 icu                       68.1                 h6c2663c_0
 idna                      2.10               pyhd3eb1b0_0
 importlib-metadata        3.10.0           py39haa95532_0
 intel-openmp              2021.2.0           haa95532_616
 jpeg                      9b                   hb83a4c4_2
 keras-preprocessing       1.1.2              pyhd3eb1b0_0
 krb5                      1.18.2               hc04afaa_0
 libcurl                   7.71.1               h2a8f88b_1
 libpng                    1.6.37               h2a8f88b_0
 libprotobuf               3.14.0               h23ce68f_0
 libssh2                   1.9.0                h7a1dbc1_1
 markdown                  3.3.4            py39haa95532_0
 mkl                       2021.2.0           haa95532_296
 mkl-service               2.3.0            py39h2bbff1b_1
 mkl_fft                   1.3.0            py39h277e83a_2
 mkl_random                1.2.1            py39hf11a4ad_2
 multidict                 5.1.0            py39h2bbff1b_2
 numpy                     1.20.2           py39ha4e8547_0
 numpy-base                1.20.2           py39hc2deb75_0
 oauthlib                  3.1.1              pyhd3eb1b0_0
 openssl                   1.1.1k               h2bbff1b_0
 opt_einsum                3.3.0              pyhd3eb1b0_1
 pip                       21.1.3           py39haa95532_0
 protobuf                  3.14.0           py39hd77b12b_1
 pyasn1                    0.4.8                      py_0
 pyasn1-modules            0.2.8                      py_0
 pycparser                 2.20                       py_2
 pyjwt                     2.1.0            py39haa95532_0
 pyopenssl                 20.0.1             pyhd3eb1b0_1
 pyreadline                2.1              py39haa95532_1
 pysocks                   1.7.1            py39haa95532_0
 python                    3.9.0                h6244533_2
 python-flatbuffers        1.12               pyhd3eb1b0_0
 requests                  2.25.1             pyhd3eb1b0_0
 requests-oauthlib         1.3.0                      py_0
 rsa                       4.7.2              pyhd3eb1b0_1
 scipy                     1.6.2            py39h66253e8_1
 setuptools                52.0.0           py39haa95532_0
 six                       1.16.0             pyhd3eb1b0_0
 snappy                    1.1.8                h33f27b4_0
 sqlite                    3.36.0               h2bbff1b_0
 tensorboard               2.5.0                      py_0
 tensorboard-plugin-wit    1.6.0                      py_0
 tensorflow                2.5.0           gpu_py39h7dc34a2_0
 tensorflow-base           2.5.0           gpu_py39hb3da07e_0
 tensorflow-estimator      2.5.0              pyh7b7c402_0
 tensorflow-gpu            2.5.0                h17022bd_0
 termcolor                 1.1.0            py39haa95532_1
 typing-extensions         3.10.0.0             hd3eb1b0_0
 typing_extensions         3.10.0.0           pyh06a4308_0
 tzdata                    2021a                h52ac0ba_0
 urllib3                   1.26.6             pyhd3eb1b0_1
 vc                        14.2                 h21ff451_1
 vs2015_runtime            14.27.29016          h5e58377_2
 werkzeug                  1.0.1              pyhd3eb1b0_0
 wheel                     0.35.1             pyhd3eb1b0_0
 win_inet_pton             1.1.0            py39haa95532_0
 wincertstore              0.2              py39h2bbff1b_0
 wrapt                     1.12.1           py39h196d8e1_1
 yarl                      1.6.3            py39h2bbff1b_0
 zipp                      3.4.1              pyhd3eb1b0_0
 zlib                      1.2.11               h62dcd97_4

When trying to install spyder, I receive a huge amount of dependency conflicts, as it starts like this
UnsatisfiableError: The following specifications were found
to be incompatible with the existing python installation in your environment:
Specifications:
  - attrs -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|>=3.5,<3.6.0a0']
  - brotlipy -> python[version='>=2.7,<2.8.0a0|>=3.5,<3.6.0a0']
  - cached-property -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.8,<3.9.0a0|>=3.7,
    <3.8.0a0|>=3.5,<3.6.0a0']
   - gast -> python[version='>=2.7,<2.8.0a0|>=3.7,<3.8.0a0|>=3.6,<3.7.0a0|>=3.5,<3.6.0a0']
   - grpcio -> python[version='>=2.7,<2.8.0a0']
   - h5py -> python[version='<3']
    - mkl_random -> python[version='>=2.7,<2.8.0a0']
    - pyasn1 -> python[version='>=2.7,<2.8.0a0|>=3.5,<3.6.0a0|>=3.7,<3.8.0a0|>=3.6,<3.7.0a0']
    - pyasn1-modules -> python[version='>=2.7,<2.8.0a0|>=3.8,<3.9.0a0|>=3.6,<3.7.0a0|>=3.7,
      <3.8.0a0|>=3.5,<3.6.0a0']
    - pycparser -> python[version='>=2.7,<2.8.0a0|>=3.5,<3.6.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0']
    - pyopenssl -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.8,<3.9.0a0|>=3.7,<3.8.0a0|>=3.5,
      <3.6.0a0']
    - requests -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|>=3.8,<3.9.0a0|>=3.5,
      <3.6.0a0']
    - requests-oauthlib -> python[version='>=2.7,<2.8.0a0|>=3.7,<3.8.0a0|>=3.5,<3.6.0a0|>=3.6,
      <3.7.0a0']
    - rsa -> python[version='>=2.7,<2.8.0a0|>=3.5,<3.6.0a0|>=3.7,<3.8.0a0|>=3.6,<3.7.0a0']
    - werkzeug -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.7,<3.8.0a0|>=3.5,<3.6.0a0']
    - wheel -> python[version='>=2.7,<2.8.0a0|>=3.6,<3.7.0a0|>=3.8,<3.9.0a0|>=3.7,<3.8.0a0|>=3.5,
     <3.6.0a0']

Error log continues with a huge amount of dependencies conflicts. I’m afraid it doesn’t make sense to include in this posts.
I would appreciate any workaround to enjoy Spyder with tensorflow-gpu release 2.5
Kind regards.

Context:
Hello,
I don’t know if you solve the issue, but I’m posting this for others who may have the same problem!
I created a new environment in Anaconda using the command :
(conda prompt) # conda create --name tf tensorflow-gpu
Then I got the same error rescribed before… when trying to install Spyder IDE !
THE SOLUTION
I managed to solve this issue by doing these steps:

Create new anaconda environnement with python3.7 ONLY (I used Anaconda Navigator for this) => make sure to choose Python 3.7 ONLY !! => choose a name as ‘my_env’
Go to Anaconda Prompt to install separately tensoflow first then spyder.
So we need to navigate to the Python environnement that we created already (I named it my_env)

(base) # conda activate my_env
(my_env) # conda install tensorflow-gpu
(my_env) # conda install spyder

Once finished Spyder should work properly in the environment you installed it in.
Important Note:
Some packages may not be installed due to several reasons (connection problem for example…), So make sure all packages have been installed as 100% ! If not, then re-execute the same command to install all packages correctly !
Regards.
IKRAM


Question:
hello !
i used Classify structured data with feature columns learning a model with Python
To use in Android, it was converted to Pd → tflite and used.
An error occurred when testing the training model
“datatype 11 is not recognized in java”
Java version used is 8
What is data type 11?

Context:
It is however supported by the C++ API, which we can use easily from Java with the JavaCPP Presets for TF Lite:

      github.com

    javacpp-presets/tensorflow-lite at master · bytedeco/javacpp-presets

  master/tensorflow-lite

  The missing Java distribution of native C++ libraries

BTW, I think it’s actually referring to Float64:

      github.com

    bytedeco/javacpp-presets/blob/master/tensorflow-lite/src/gen/java/org/bytedeco/tensorflowlite/global/tensorflowlite.java#L310

            kTfLiteFloat32 = 1,
            kTfLiteInt32 = 2,
            kTfLiteUInt8 = 3,
            kTfLiteInt64 = 4,
            kTfLiteString = 5,
            kTfLiteBool = 6,
            kTfLiteInt16 = 7,
            kTfLiteComplex64 = 8,
            kTfLiteInt8 = 9,
            kTfLiteFloat16 = 10,
            kTfLiteFloat64 = 11,
            kTfLiteComplex128 = 12,
            kTfLiteUInt64 = 13,
            kTfLiteResource = 14,
            kTfLiteVariant = 15,
            kTfLiteUInt32 = 16;
          // Targeting ../TfLiteQuantizationParams.java

// #ifdef __cplusplus  // extern C


CUDA install problem on Ubuntu 20.04 LTS + GPU

Question:
I am having difficulty getting TensorFlow 2.7.0 to work with GPU on Ubuntu 20.04.
The instructions at GPU support | TensorFlow 1 do not describe the procedure for Ubuntu 20.04. Is that described anywhere?
When I run this test script:
import tensorflow as tf

print('TensorFlow version:',tf.__version__)
physical_devices = tf.config.list_physical_devices()
for dev in physical_devices:
    print(dev)

sys_details = tf.sysconfig.get_build_info()
cuda_version = sys_details["cuda_version"]
print("CUDA version:",cuda_version)
cudnn_version = sys_details["cudnn_version"]
print("CUDNN version:",cudnn_version)

I see:
$ python3 TensorFlow/test.py
TensorFlow version: 2.7.0
2021-11-29 17:30:21.825301: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudnn.so.8'; dlerror: libcudnn.so.8: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: :/usr/local/cuda-11.5/lib64:/usr/local/cuda-11.5/lib64
2021-11-29 17:30:21.825324: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1850] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')
CUDA version: 11.2
CUDNN version: 8

I have:
$ echo $LD_LIBRARY_PATH
:/usr/local/cuda-11.5/lib64:/usr/local/cuda-11.5/lib64

How might I fix the errors please?

Context: @David_A, According to Tested build configuration 7 CUDA: 11.2 and cuDNN:8.1 are compatible for TF2.7. Can you let us know the steps that you have followed to install GPU ?


Question:
Hi guys.
I would like to know, if Dropout (dropped out nodes) gets updated once on every input or once every batch.
Asked this question on stackOverFlow aswell, if you are interested in answering there:

      stackoverflow.com

  How often do the dropped out weights get updated 1

  tensorflow, conv-neural-network, artificial-intelligence, dropout

  asked by

    Shaved Man

  on 01:39PM - 26 Nov 21 UTC

Thanks!

Context:
Check the docstring:

      github.com

    keras-team/keras/blob/master/keras/layers/core/dropout.py#L29:L32

          from keras.engine import base_layer
          from keras.utils import control_flow_util
          import tensorflow.compat.v2 as tf
          from tensorflow.python.util.tf_export import keras_export

          @keras_export('keras.layers.Dropout')
          class Dropout(base_layer.BaseRandomLayer):
            """Applies Dropout to the input.

            The Dropout layer randomly sets input units to 0 with a frequency of `rate`
            at each step during training time, which helps prevent overfitting.
            Inputs not set to 0 are scaled up by 1/(1 - rate) such that the sum over
            all inputs is unchanged.

            Note that the Dropout layer only applies when `training` is set to True
            such that no values are dropped during inference. When using `model.fit`,
            `training` will be appropriately set to True automatically, and in other
            contexts, you can set the kwarg explicitly to True when calling the layer.

            (This is in contrast to setting `trainable=False` for a Dropout layer.


Question:
I have a large dataset with 25-45 second audio files with their transcriptions in a low-resource language (has common vocabulary to English in some ways), and I want to fine-tune an existing model against my own data. All tutorials I find use Common Voice, and tailoring them to my use-case isn’t very straightforward. This is the tutorial I tried following; it uses torchaudio but I would prefer tensorflow: Fine-Tune XLSR-Wav2Vec2 for low-resource ASR with 🤗 Transformers
I was wondering if there were any references I could follow. My dataset is simply two columns; a ‘filename’ column which is just the audio file (and I concatenate the path to each filename when I want to load the audio file) and a ‘sentence’ column which is the audio transcription. Audio is in MP3 format.
I’m really stuck on how to proceed from here.

Context: There is a tutorial on Simple audio recognition 2 that can be good reference to start the speech to text using Tensorflow.


[Info Need] Clean implementation of Object Detection models in keras

Question:
I need some information about the clean and best practice implementation of object detection models with tf.keras.  Here is one of the two implementations for example:

Keras RetinaNet 2
YoloV3-Keras 1

Any more recommendations? Please note, it should be a high-quality implementation and trusted.  FYI, I can also use TF-Model but no thanks, maybe later.

Context:
You can find some refactored model in the model garden at:

      github.com

    models/official/vision/beta at master · tensorflow/models 3

  master/official/vision/beta

  Models and examples built with TensorFlow. Contribute to tensorflow/models development by creating an account on GitHub.


Question:
Hi!
I am a new with Python, and I have to produce a small Python script that load a 16bit PNG and run inference with TensorFlow Lite on a Coral board SDK, using an existing model with shape [224, 224, 1]. The input image is 224x224 16bit mono.
image = Image.open(‘input.png’)
CoralAdapter.set_input(interpreter, image)
ValueError: could not broadcast input array from shape (224,224) into shape (224,224,1)
I assume I need to load the PNG file as a (224,224,1) array instead of a (224,224) array.
What is the proper way to load a grayscale PNG this way?
Can TFlite use 16bit mono PNG, or does it only support 24bit RGB?
Thanks!

Context:
TF has native functions to decode images loaded from files like decode_image() or decode_png()
Please, see this documentation page: tf.io.decode_image  |  TensorFlow Core v2.6.0 3
You can specify the number of channels that you need in a decoded tensor, which can be 1, 3, 4 or the same as in the original image.


Can’t get TF Dataset to work with Keras ImageDataGenerator.flow_from_directory()

Question:
So far I was using a Keras ImageDataGenerator with flow_from_directory() to train my Keras model with all images from the image class input folders. Now I want to train on multiple GPUs, so it seems I need to use a TensorFlow Dataset object.
Thus    I came up with this solution:
keras_model = build_model()
train_datagen = ImageDataGenerator()
training_img_generator = train_datagen.flow_from_directory(
    input_path,
    target_size=(image_size, image_size),
    batch_size=batch_size,
    class_mode="categorical",
)
train_dataset = tf.data.Dataset.from_generator(
    lambda: training_img_generator,
    output_types=(tf.float32, tf.float32),
    output_shapes=([None, image_size, image_size, 3], [None, len(image_classes)])
)
# similar for validation_dataset = ...
keras_model.fit(
    train_dataset,
    steps_per_epoch=train_steps_per_epoch,
    epochs=epoch_count,
    validation_data=validation_dataset,
    validation_steps=validation_steps_per_epoch,
)

Now this seem to work, the model is trained as usual. However, during training I get the following warning message, when using a mirrored strategy:

AUTO sharding policy will apply DATA sharding policy as it failed to apply FILE sharding policy because of the following reason: Did not find a shardable source, walked to a node which is not a dataset

So I added the following lines between creating the data sets and calling fit():
options = tf.data.Options()
options.experimental_distribute.auto_shard_policy = tf.data.experimental.AutoShardPolicy.DATA
train_dataset.with_options(options)
validation_dataset.with_options(options)

However, I still get the same warning.
This leads me to these two questions:

What do I need to do in order to get rid of this warning?

Even more important: Why is TF not able to split the dataset with the default AutoShardPolicy.FILE policy, since I am using thousands of images per class in the input folder?

Context:
Use it like thia
train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train))
validation_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test))

data_augmentation = tf.keras.Sequential(
    [
        tf.keras.layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),
        tf.keras.layers.experimental.preprocessing.RandomRotation(factor=0.02),
        tf.keras.layers.experimental.preprocessing.RandomZoom(
            height_factor=0.2, width_factor=0.2
        ),
    ],
    name="data_augmentation",
)

def preprocess_image(image, label):
    image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
    image = tf.image.convert_image_dtype(image, tf.float32) / 255.0
    return image, label

# Training Pipeline
pipeline_train = (
    train_ds
    .shuffle(BATCH_SIZE*100)
    .map(preprocess_image, num_parallel_calls=AUTO)
    .batch(BATCH_SIZE)
    .map(lambda x, y: (data_augmentation(x), y), num_parallel_calls=AUTO)
    .prefetch(AUTO)
)

# Validation Pipeline
pipeline_validation = (
    validation_ds
    .map(preprocess_image, num_parallel_calls=AUTO)
    .batch(BATCH_SIZE)
    .prefetch(AUTO)
)


Measuring objects in real time using TensorFlow

Question: Hello, wondering if any experts in computer vision or people with experience can help with a project I’ve been working on, meant to measure objects using reference objects in real-time. I have a theory that if you can use a reference object with known size, then you can use the pixel size/ratio of the quarter to be able to measure the objects by their pixel ratio as well so that you can be able to get the relative size

Context: Are the reference object and the target object coplanar?


Question: Why the eigenvectors output is different for tensorflow and numpy?

Context:
Just the eigenvectors or also the eigenvalues?
As far as I know, they’re different in the output dtype, but should be equal in the values.
tf.linalg.eig() returns a dtype=tf.complex64, while
np.linalg.eig() depends on your input np.dtype.
You can try to pass your input as dtype=np.complex64 for np.linalg.eig(), then they shouldn’t be different anymore (both as complex numbers).


LSTM false predictions when timestep changes

Question:
Hi everyone,
I am trying to train a  LSTM   network with input (train data shape)  (80,27) (batch size not included) since each sequence in my training data has 80 timesteps and 27 features
For the prediction part I want to predict each timestep separately and not the 80 together.
For example my prediction batch has size (1,1,27) but the result is wrong (MAE Value 4.0000) since the model was trained with 80 timesteps in the input data.
I tried to switch into a stateful Model when predicting and transfering the weights from the trained model to the new one but  still MAE value is 4. So what am I missing?
Network Architecture:
def Model():
    inputs = keras.Input(shape=(None,27))
    x = layers.Bidirectional(layers.LSTM(512,return_sequences=True))(inputs)
    x = layers.Bidirectional(layers.LSTM(512,return_sequences=True))(x)
    x = layers.Bidirectional(layers.LSTM(256,return_sequences=True))(x)
    x = layers.Bidirectional(layers.LSTM(256,return_sequences=True))(x)
    x = layers.Bidirectional(layers.LSTM(128,return_sequences=True))(x)
    x = layers.Bidirectional(layers.LSTM(128,return_sequences=True))(x)
    x = layers.Bidirectional(layers.LSTM(128,return_sequences=True,))(x)

    x = layers.Dense(512,activation="selu")(x)
    x = layers.Dense(256,activation="selu")(x)

    x = layers.Dense(1)(x)

    return keras.Model(inputs=inputs,outputs=x)

Thanks in advance

Context:
You should define the input shape in the first layer as tf.keras.Input(shape=(80, 27))
If you want to get a prediction for 1 timestep out of every 80 x 27 data matrix, you can insert x = tf.keras.layers.GlobalAveragePooling1D()(x) after the last Bidirectional layer. Or GlobalMaxPooling1D.


Question:
Hello,
I try to use that turorial on my webcam:

      TensorFlow

Object Detection  |  TensorFlow Hub 1

I want to replace image input by a video as input:
vid = cv2.VideoCapture("videoplayback.mp4")
while (True):
    ret, frame = vid.read()
    cv2.imshow('frame', frame)

    run_detector_image(detector, frame)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break

How should I modify the load_img to feed the opencv frame ?

Context:
Read the video frame by frame and convert them to JPEG format
import cv2
video = cv2.VideoCapture("videoplayback.mp4")
success,image = video.read()
count = 0
success =True
while success:
  success,image = video.read()
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
  count += 1


TensorFlow Lite Python object detection example w/ Raspberry Pi Crashes during Install

Question:
Hello,
I have a Raspberry Pi 3B+ with a 32GB SDCard running Raspberry Pi OS Buster 2021-05-07.
I tried using the Object Detection example given here

      github.com

    examples/lite/examples/object_detection/raspberry_pi at master ·... 2

  master/lite/examples/object_detection/raspberry_pi

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

When I run the setup.h it works until until it tries to install the dependencies for what I think is OpenCV.  It works for a few minutes then it freezes and crashes.
It looks like the this Object Detection Example was  updated recently.
Will it work with a Raspberry Pi 3B+?
Any tips or tricks on how to get around this issue?
Thank you,

Context:
Hello All,
I solved my problem.
When you install the Object Detection example for Raspberry Pi you need to make sure you meet the Tensorflow 2 requirements.
I updated my pip to 20.3.4 and my pip3 to 21.3.1 and everything installed and ran just fine.
Thanks.


Return Recommendations from models with Context features

Question:
Hi,
I was hoping someone could point in the right direction in being able to return the top n recommendations from a model built in the same way as the retrieval tutorial with context features. (link below)

      TensorFlow

Taking advantage of context features  |  TensorFlow Recommenders 2

Using the same code as in the very first tutorial in the series returns an error.
The code:
index = tfrs.layers.factorized_top_k.BruteForce(model.user_model)

recommends movies out of the entire movies dataset.
index.index_from_dataset(
tf.data.Dataset.zip((movies.batch(100), movies.batch(100).map(model.movie_model)))
)

Get recommendations.
_, titles = index(tf.constant([“42”]))
print(f"Recommendations for user 42: {titles[0, :3]}")

Context:
Hi Nick, can you share the error please?
cc @Wei_Wei might be able to help here


Question:
wrong1429×383 55.3 KB

Hi, while training a CNN, I am seeing an unusual peak in validation loss, though the validation accuracy (epoch 9/250 in the attached image) seems correct ? Can anyone share there views on this. why this is happening …?

Context: @GIRIN_93 , Can you share standalone code to replicate your issue? so that we can try to investigate your issue. Thanks!


How to return list of sparse tensor in Dataset?

Question:
I need to return sparse tensor and tensor in a Dataset, it looks like this:
import tensorflow as tf

def parse(x):
    s1 = tf.sparse.from_dense([0, 0, 2, 1])
    s2 = tf.sparse.from_dense([1, 0])
    return [s1, s2], tf.constant([1, 2, 3])

dataset = tf.data.Dataset.from_tensor_slices(list(range(10)))
dataset = dataset.batch(2)
dataset = dataset.map(parse)

for d in dataset.take(1):
    print(d)

But error occur:
TypeError: Neither a SparseTensor nor SparseTensorValue: [<tensorflow.python.framework.sparse_tensor.SparseTensor object at 0x7f1c5089cc10>, <tensorflow.python.framework.sparse_tensor.SparseTensor object at 0x7f1c9ca4b4c0>].
If return only  [s1, s2],  it works well.
My question is how to return list of sparse Tensor along with tensor

Context:
You should include Tensor into sparse_tensor list.
Working code snippet
import tensorflow as tf

def parse(x):
    s1 = tf.sparse.from_dense([0, 0, 2, 1])
    s2 = tf.sparse.from_dense([0, 1])
    return [s1, s2, tf.constant([1, 2, 3])]

dataset = tf.data.Dataset.from_tensor_slices(list(range(10)))
dataset = dataset.batch(2)
dataset = dataset.map(parse)
for d in dataset.take(1):
    print(d)


Metrics related [predictions must be <= 1] error

Question:
Hello,
I have the below error with TensorFlow 2.7. Same error happens with 2.6 but stack trace is different.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\eager\execute.py", line 58, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError:  assertion failed: [predictions must be <= 1] [Condition x <= y did not hold element-wise:] [x (sequential_4/output/BiasAdd:0) = ] [[1.00585222][1.00123906][0.880351603]...] [y (Cast_3/x:0) = ] [1]
         [[node assert_less_equal/Assert/AssertGuard/Assert
 (defined at C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\utils\metrics_utils.py:612)
]] [Op:__inference_train_function_29922]

Errors may have originated from an input operation.
Input Source operations connected to node assert_less_equal/Assert/AssertGuard/Assert:
In[0] assert_less_equal/Assert/AssertGuard/Assert/assert_less_equal/All:
In[1] assert_less_equal/Assert/AssertGuard/Assert/data_0:
In[2] assert_less_equal/Assert/AssertGuard/Assert/data_1:
In[3] assert_less_equal/Assert/AssertGuard/Assert/data_2:
In[4] assert_less_equal/Assert/AssertGuard/Assert/sequential_4/output/BiasAdd:
In[5] assert_less_equal/Assert/AssertGuard/Assert/data_4:
In[6] assert_less_equal/Assert/AssertGuard/Assert/Cast_3/x:

Operation defined at: (most recent call last)
>>>   File "<stdin>", line 1, in <module>
>>>
>>>   File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\utils\traceback_utils.py", line 64, in error_handler
>>>     return fn(*args, **kwargs)
>>>
>>>   File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 1216, in fit
>>>     tmp_logs = self.train_function(iterator)
>>>
>>>   File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 878, in train_function
>>>     return step_function(self, iterator)
>>>
>>>   File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 867, in step_function
>>>     outputs = model.distribute_strategy.run(run_step, args=(data,))
>>>
>>>   File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 860, in run_step
>>>     outputs = model.train_step(data)
>>>
>>>   File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\engine\training.py", line 817, in train_step
>>>     self.compiled_metrics.update_state(y, y_pred, sample_weight)
>>>
>>>   File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\engine\compile_utils.py", line 460, in update_state
>>>     metric_obj.update_state(y_t, y_p, sample_weight=mask)
>>>
>>>   File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\utils\metrics_utils.py", line 73, in decorated
>>>     update_op = update_state_fn(*args, **kwargs)
>>>
>>>   File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\metrics.py", line 177, in update_state_fn
>>>     return ag_update_state(*args, **kwargs)
>>>
>>>   File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\metrics.py", line 1069, in update_state
>>>     return metrics_utils.update_confusion_matrix_variables(
>>>
>>>   File "C:\Users\hakan\AppData\Roaming\Python\Python39\site-packages\keras\utils\metrics_utils.py", line 612, in update_confusion_matrix_variables
>>>     tf.compat.v1.assert_less_equal(
>>>

Function call stack:
train_function -> assert_less_equal_Assert_AssertGuard_false_28872

Interesting thing is, this only happens when using BinaryCrossentropy(from_logits=True) loss and with metrics other than BinaryAccuracy, for example Precision or AUC metrics.
In other words, with BinaryCrossentropy(from_logits=False) loss it always works with any metrics, with BinaryCrossentropy(from_logits=True) loss it only works with BinaryAccuracy metrics.
Below is the model:
model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(150, activation='relu', name='hidden_1', input_shape=(x_train.shape[1],)),
  tf.keras.layers.Dense(50, activation='relu', name='hidden_2'),
  tf.keras.layers.Dense(1, activation=None, name='output')
])

model.compile(optimizer=tf.keras.optimizers.Adam(),
  loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
  metrics=METRICS)

model.fit(x_train, y_train, epochs=20, validation_data=(x_val, y_val), callbacks=[tensorboard_callback], verbose=1)

Any clues for how to fix this?
Thanks
raft

Context:
Is the the cofusion Matrix update expect predicted values between 0 and 1:

      github.com

    tensorflow/tensorflow/blob/master/tensorflow/python/keras/utils/metrics_utils.py#L620-L629 1

          with ops.control_dependencies([
              check_ops.assert_greater_equal(
                  y_pred,
                  math_ops.cast(0.0, dtype=y_pred.dtype),
                  message='predictions must be >= 0'),
              check_ops.assert_less_equal(
                  y_pred,
                  math_ops.cast(1.0, dtype=y_pred.dtype),
                  message='predictions must be <= 1')
          ]):


Question:
Hi there,
I am new here and would like to do machine learning. To start with, I’ve set myself the following task: I want to classify sensors (of the same type). The sensor delivers measured values that are temperature-dependent. So I’m taking readings while I’m changing the temperature. When I have finished the measurement, I have an array with sensor readings and an array with temperature values. This is a complete data set, e.g. B. contains 100 values each. Now I have recorded readings from several sensors while always changing the temperature in the same way. I have result data for the sensors (good or bad, 1 or 0, true or false). How can you process that with machine learning? I’m a complete newbie to the field and don’t know how to approach the subject.

Context:
If your data has some kind of time dimension I suggest you to start to run and modify some of the Keras Timeseries examples:

      keras.io

Keras documentation: Timeseries 3


Question: Hi, I want to create a sparse linear layer, where I mask certain weights & gradients according to a certain mask. Is there an example where this is done efficiently?

Context:
Do you mean something like:

      stackoverflow.com

  Tensorflow custom layer: Creating a sparse matrix with trainable parameters

  python, tensorflow, machine-learning

  asked by

    Marko Karbevski

  on 03:34PM - 30 Sep 19 UTC


Issue with GradientTape for a custom loss function, TF2.6

Question:
I am trying to use a custom loss function in my Keras sequential model (TensorFlow 2.6.0). This custom loss (ideally) will calculate the data loss plus the residual of a physical equation (say, diffusion equation, Navier Stokes, etc.). This residual error is based on the model output derivative wrt its inputs and I want to use GradientTape.
In this MWE, I removed the data loss term and other equation losses, and just used the derivative of the output wrt its input. The dataset can be found here 4.
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
import tensorflow as tf #tf.__version__ = '2.6.0'
# load the dataset
dataset = loadtxt('pima-indians-diabetes.csv', delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,0:8] #X.shape = (768, 8)
y = dataset[:,8]

def customLoss(y_true,y_pred):
    x_tensor = tf.convert_to_tensor(model.input, dtype=tf.float32)
#     x_tensor = tf.cast(x_tensor, tf.float32)
    with tf.GradientTape() as t:
        t.watch(x_tensor)
        output = model(x_tensor)
    DyDX = t.gradient(output, x_tensor)
    dy_t = DyDX[:, 5:6]
    R_pred=dy_t
    # loss_data = tf.reduce_mean(tf.square(yTrue - yPred), axis=-1)
    loss_PDE = tf.reduce_mean(tf.square(R_pred))
    return loss_PDE

model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(12, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss=customLoss, optimizer='adam', metrics=['accuracy'])

model.fit(X, y, epochs=15)

After execution, I get this ValueError:
ValueError: Passed in object of type <class 'keras.engine.keras_tensor.KerasTensor'>, not tf.Tensor
When I change loss=customLoss to loss='mse', the model starts training, but using that customLoss is the whole point. Any ideas?

Context:
Do you want something like this:

      stackoverflow.com

  Custom loss function with Gradient Tape, TF2.6 16

  python, tensorflow, keras, gradienttape

  asked by

    Milad S

  on 03:36PM - 16 Oct 21 UTC


Question:
First of all:
my env:
MacBookAir M1
tensorflow 0.1a3
I want to study TensorFlow by using this code and datasets, so I copied this code from this link(and I didn’t change any code in it.):
https://www.kaggle.com/roblexnana/cifar10-with-cnn-for-beginer/notebook 1
Problem:
At first, the accuracy is 0.2 which is normal, and the accuracy increase as training which is normal too.
when it comes to around 10/100 epoches, the accuracy began to wane, and about at 20/100 epoches, the accuracy down to ~0.1.
and I put it into my winodws computer, using tensorflow 2.7, the code performs as well as the link above(accuracy: 0.80+).
but I cannot send an issue in Github, so I put this problem here hope somebody matters will see this.

Context: What is tensorflow 0.1a3?


Which tensorflow lite implemention is better ， python or c++ ？

Question: which tensorflow lite implemention is better ， python or c++  ？

Context:
I think that using python or c++ it really depends on your project.
E.g. Are you integrating the network in a c++ project? Do have any constrain to avoid the python interpreter on that specific hw platfrom etc…


Question:
I am working on a project “News Classification”. Where the model has to classify(multi-class classification problem) a given text into business, entertainment, politics, sport and tech.
I am using TensorFlow==2.7.0 on Google Colab. I had trained 7 different models. After, training and made prediction on it. When compared to all models, Conv1d performed the best. The best performing model saved model_2.save('saved_model/my_model').It has done well till here.
But, When I want to load the saved model using the code
loaded_model = tf.keras.models.load_model('saved_model/my_model') then, I am getting the below exception:
TypeError                                 Traceback (most recent call last)
<ipython-input-129-c92edaf0db7f> in <module>()
----> 1 load_model = tf.keras.models.load_model('saved_model/my_model')
      2 # load_model.preditct(val_sentences)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast)
    547       str_values = [compat.as_bytes(x) for x in proto_values]
    548     except TypeError:
--> 549       raise TypeError(f"Failed to convert elements of {values} to Tensor. "
    550                       "Consider casting elements to a supported type. See "
    551                       "https://www.tensorflow.org/api_docs/python/tf/dtypes "

TypeError: Exception encountered when calling layer "conv1d" (type Conv1D).

Failed to convert elements of tf.RaggedTensor(values=tf.RaggedTensor(values=Tensor("Placeholder:0", shape=(None, 128), dtype=float32), row_splits=Tensor("Placeholder_1:0", shape=(None,), dtype=int64)), row_splits=Tensor("conv1d/Conv1D/RaggedExpandDims/RaggedFromUniformRowLength/RowPartitionFromUniformRowLength/mul:0", shape=(None,), dtype=int64)) to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.

Call arguments received:
  • inputs=tf.RaggedTensor(values=Tensor("Placeholder:0", shape=(None, 128), dtype=float32), row_splits=Tensor("Placeholder_1:0", shape=(None,), dtype=int64))

Context:
Have you already checked:

      github.com/tensorflow/tensorflow

      Ragged Tensors in Keras Model Output 48

        opened

        Aug 13, 2020

          ablanch5

          stat:awaiting tensorflower

          type:feature

          comp:keras

    **System information**
- TensorFlow version (you are using): 2.3.0
- Are you w…illing to contribute it (Yes/No): No

**Describe the feature and the current behavior/state.**
I don't think ragged tensors are supported as the outputs to a model. For example the following code does not work

import tensorflow as tf

y_pred = tf.ragged.constant([[0], [0, 0]])
y_true = tf.ragged.constant([[1], [1,1]])

model = tf.keras.Sequential([
    tf.keras.layers.Input(shape=(None,), ragged=True)
])

model.compile(loss='MSE', optimizer='adam')
model.evaluate(x=y_pred, y=y_true)

Returns error:
 TypeError: Failed to convert object of type <class 'tensorflow.python.ops.ragged.ragged_tensor.RaggedTensor'> to Tensor. Contents: tf.RaggedTensor(values=Tensor("sequential_1/Cast_1:0", shape=(None,), dtype=float32), row_splits=Tensor("RaggedFromVariant/RaggedTensorFromVariant:0", shape=(None,), dtype=int64)). Consider casting elements to a supported type.

**Will this change the current api? How?**
yes. implement support for ragged tensors in keras model output

**Who will benefit with this feature?**
People who want to use ragged tensors in output, for example sequence to sequence models with variable lengths.

**Any Other info.**
No.


Question:
Hello TF community!
I’m looking to model a company’s supply chain in order to optimize distribution of products from their warehouses to their retail outlets. The company currently relies on linear programming methods to make these optimizations, and they’re interested in ML approaches which could scale better for their sizable volume.
Considering a distribution network comprises of nodes (type a: warehouse; type b: retail outlet), and flows between these nodes (unidirectional flow of products from warehouse to retailer), this seems like a ripe opportunity to use graph neural networks, such as TF’s newly released TF-GNN package.
Therefore, if the objective is to maximize for something like % of retail outlet space occupied at any given time (aiming for >90% stock of products), how can I model this problem using TF-GNN?
Would you choose a different approach entirely? Curious for more perspective.

Context:
There was a nice survey about GNNs for Combinatorial Optimization at:
https://arxiv.org/abs/2102.09544 8
Take a look also at:

      papers.ssrn.com

Reinforcement Learning for Logistics and Supply Chain Management:... 3

Laurent Charlin: "Exact Combinatorial Optimization with Graph Convolutional Neural Networks"


Question:
Hi,
I am an android and iOS developer and I want to make a chatbot using tensor flow with the help of Flutter & Dart. The chatbot is able to answer of the user input questions. Basically I want a FAQ Chatbot. I want to train my bot with some constant faq question answers by which it can detect and able to answer. I found nothing over internet about my mentioned query.

Context:
You can check:

      TensorFlow

BERT Question and Answer  |  TensorFlow Lite 4

      TensorFlow

BERT Question Answer with TensorFlow Lite Model Maker

      TensorFlow

Integrate BERT question answerer  |  TensorFlow Lite 2

      Dart packages

tflite_flutter | Flutter Package 2

  TensorFlow Lite Flutter plugin provides an easy, flexible, and fast Dart API to integrate TFLite models in flutter apps across mobile and desktop platforms.


Why the accuracy of Channels-First Semantic Segmentation model (U-Net) becomes lower than Channels-Last’s one?

Question:
Hello.
Now I’m trying to create channels-first semantic segmentation model of 2D U-Net with tensorflow v2.5.
Learning phase can run, but the accuracy remains very low (it’s about 0.001-).
I could get high accuracy by channels-last (about 0.9-).
What should I have to confirm ?
— info —
Input shape: (None, 1, 256, 256)
Output shape: (None, 5, 256, 256)
class num: 5
loss : mse
optimizer: adam
Thank you.

Context:
This is an old introduction to the channels-first/channels-last features:

      Machine Learning Mastery – 4 Apr 19

A Gentle Introduction to Channels-First and Channels-Last Image Formats 1

  Color images have height, width, and color channel dimensions. When represented as three-dimensional arrays, the channel dimension for the image […]

    Est. reading time: 10 minutes

You can convert data between channels-first and channels-last with a ‘permute dims’ tensor call. This can be done with a Lambda layer.


Question:
Hi,
I’m currently working on an AI project using an NVIDIA Jetson Nano (4GB) and TensorFlow 2 where we were planning on using a Faster R-CNN Inception ResNet V2 640x640 model. We tried using TF-TRT to reduce the network, but it seems to be too big to fit in, the vRAM memory is not big enough and using Swap doesn’t solve the issue.
We have done several tests and, for the moment, the heaviest network from the TensorFlow Model Zoo we managed to get working is the SSD MobileNet V2 FPNLite 640x640.
I’ve been searching for a list of networks that have been tested on this device for TF2, but I can’t seem to find it. I know of the existence of this list 1, but it is for TF1 and doesn’t involve the TF2 Model Zoo models.
Has anyone tried to get something bigger running on the Jetson? Which network and how? Is there any official documentation on which models are feasible for only 4GB?
Also, I’d like to understand why I can execute these models on my CPU but not on the GPU, as the system runs out of (RAM/vRAM) memory. I’m no expert and it seems a bit weird.
Thank you in advance.

Context: The Nano is not designed to run large neural networks. I repeat: execute. The work must do this upstream and think lite


Neural network with tensorflow after growing data stopped studying

Question:
Hi.
I had POC project with terraform and neural network.
Before describe technical parameters, few words about data.
This is a table of some items.
One item has two properties.
In some array could be present from 5 up to 40 items.
The goal for neural network is choose one item  from others.
The table in csv looks like:
ID, Item_1,Item_1_Property_1,Item_1_Property_2, …  Item_40,Item_40_Property_1,Item_40_Property_2, Number_Of_Item_Which_Need_To_Choose
This Item placed in DB. And I put in csv only index from db table.
I read that it is not good practice to use big numbers, instead of this table should have binary columns (one hot encoding with panda).
But during POC I faced that export from db to csv took too much time to create CSV with binary columns.
Even If I create, It will be impossible to upload this amount of data to RAM, when panda will read it.
I decided to use id from db with such configuration of layers:

output = 40 # Number of output neurons
input = 121 # Number of input neurons
model = Sequential()
model.add(Dense(input, input_dim=input, activation='relu'))
model.add(Dense(input, activation='relu'))

model.add(Dense(input, activation=‘sigmoid’))
model.add(Dense(input, activation=‘sigmoid’))
model.add(Dense(output, activation=‘sigmoid’))
model.summary()
This model studying on google colab GPU about 7 hours.

After studying this model, it becomes to predict data with 20-30% accuracy, and we decided to put to neural network real data.
When I start learn on big real data, nn show:

in 1 day it made Loss 2,6 Accuracy 0,15
in 2 days it made Loss 2,1 Accuracy 0,26
in 3 days it made Loss 1,7 Accuracy 0,33
in 4 days it made Loss 1,6 Accuracy 0,41

During 5-th day it makes no more than 0,4152
I understood that I have never reached at least Accuracy 0,9 as was in test data.
In that case, I want to ask your advice. What should I to do?
My concerns are:

I should not use id from db. Better one hot encoding.
This is big data which required big servers with big amount of RAM.
Use not single server. Instead, use spark + tensorflow.
Any thing else?

Context:
To avoid loading all the data into memory at once you can try to use tf.keras.utils.Sequence and create a data generator. It will load the data into RAM in batches as the training goes.
Here is an example of how it is done with images (Image segmentation with a U-Net-like architecture) but you can rewrite it to query the data base and transform the original sparse data into OHE vectors for 40 items just for the necessary batch.
To increase the accuracy you can experiment with more complicated architectures. Here are examples for structured data: Structured Data


__array__() takes 1 positional argument but 2 were given

Question:
Hi

When I try to train a multi-label image classification model, it gives error:
indent preformatted text by 4 spaces
TypeError                                 Traceback (most recent call last)
 in
----> 1 vgghist = model.fit(train_generator, validation_data = validation_generator, steps_per_epoch = 100, epochs = 10)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
1145           use_multiprocessing=use_multiprocessing,
1146           model=self,
 → 1147           steps_per_execution=self._steps_per_execution)
1148
1149       # Container that configures and calls tf.keras.Callbacks.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in get_data_handler(*args, **kwargs)
1362   if getattr(kwargs[“model”], “_cluster_coordinator”, None):
1363     return _ClusterCoordinatorDataHandler(*args, **kwargs)
 → 1364   return DataHandler(*args, **kwargs)
1365
1366
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in init(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution, distribute)
1164         use_multiprocessing=use_multiprocessing,
1165         distribution_strategy=ds_context.get_strategy(),
 → 1166         model=model)
1167
1168     strategy = ds_context.get_strategy()
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in init(self, x, y, sample_weights, shuffle, workers, use_multiprocessing, max_queue_size, model, **kwargs)
937         max_queue_size=max_queue_size,
938         model=model,
 → 939         **kwargs)
940
941   @staticmethod
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in init(self, x, y, sample_weights, workers, use_multiprocessing, max_queue_size, model, **kwargs)
807     # Since we have to know the dtype of the python generator when we build the
808     # dataset, we have to look at a batch to infer the structure.
 → 809     peek, x = self._peek_and_restore(x)
810     peek = self._standardize_batch(peek)
811     peek = _process_tensorlike(peek)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/data_adapter.py in _peek_and_restore(x)
941   @staticmethod
942   def _peek_and_restore(x):
 → 943     return x[0], x
944
945   def _handle_multiprocessing(self, x, workers, use_multiprocessing,
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/iterator.py in getitem(self, idx)
63         index_array = self.index_array[self.batch_size * idx:
64                                        self.batch_size * (idx + 1)]
—> 65         return self._get_batches_of_transformed_samples(index_array)
66
67     def len(self):
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/iterator.py in _get_batches_of_transformed_samples(self, index_array)
229                            target_size=self.target_size,
230                            interpolation=self.interpolation)
 → 231             x = img_to_array(img, data_format=self.data_format)
232             # Pillow images should be closed after load_img,
233             # but not PIL images.
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/utils.py in img_to_array(img, data_format, dtype)
307     # or (channel, height, width)
308     # but original PIL image has format (width, height, channel)
 → 309     x = np.asarray(img, dtype=dtype)
310     if len(x.shape) == 3:
311         if data_format == ‘channels_first’:
/usr/local/lib/python3.6/dist-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
81
82     “”"
—> 83     return array(a, dtype, copy=False, order=order)
84
85
TypeError: array() takes 1 positional argument but 2 were given

Please help. Thanks

Context: Could you provide code snippet to replicate your issue.