Converting channel_last data to channel_first

Question:
I was writing a tutorial for one of my paper implementations quite similar to the Image classification tutorial and the model I use expects data in the channels_first format.  Of course, while using tf.data.Dataset we can simply use tf.transpose.
I was wondering if there was any built-in way to load data with tf.keras.utils.image_dataset_from_directory in the channels_first format or a layer to do so?
Right now I do so in my model adding a lambda layer to do so:
model = tf.keras.Sequential([tf.keras.layers.Lambda(lambda x: tf.einsum("...ijk->...kij", x)),
                                                   ...
                                                   ...])

Here is my complete Colab notebook if required (created in a similar fashion to the classification guide on TensorFlow website):

      github.com

    Rishit-dagli/Transformer-in-Transformer/blob/main/training.ipynb

    {
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "view-in-github"
      },
      "source": [
        ""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FE7KNzPPVrVV"
      },
      "source": [
        "# Image classification with Transformer iN Transformer\n",
        "\n",

  This file has been truncated. show original

Context:
Another option is to use the tf.keras.layers.Permute
Link: tf.keras.layers.Permute  |  TensorFlow Core v2.7.0


Can someone help me to answer this question please .

Question: You work as a quality engineer at a company that manufactures high quality hand-held tools, for example battery driven screwdrivers and drilling machines. The quality testing procedure is to be automated. One aspect of tool quality is the sound of the tool. It is known at the company that several defects can be identified by anomalies in the sound of the tool. Discuss how you could use machine learning techniques (unsupervised and/or supervised learning) to identify product defects based on sound. Maximum length of answer is 1000 words.

Context:
Hi @_Amo,
welcome to the forum. Please feel free to have a look at this article, it sounds pretty similar. There’re some inspirations for supervised and unsupervised learning approaches.

      Google AI Blog

Acoustic Detection of Humpback Whales Using a Convolutional Neural Network 7

  Posted by Matt Harvey, Software Engineer, Google AI Perception   Over the last several years, Google AI Perception  teams have developed tec...


How do you export a frozen inference graph in Tensorflow 2.x Object Detection API?

Question:
I’ve been following along some tutorials in training a custom object detection model using Tensorflow 2.x Object Detection API.
Everything seems to work up until when I try exporting the trained inference graph. Basically, in TensorFlow 1.x, there is a script master/research/object_detection/export_inference_graph.py which is used to export the trained model checkpoints to a single frozen inference graph.
In TensorFlow 2.x, this script no longer works and instead, we use master/researchobject_detection/exporter_main_v2.py which outputs a SavedModel directory and some other stuff, but not the frozen inference graph. This is because in TF 2.x, frozen models have been deprecated.
I want to be able to retrieve the frozen inference graph from TensorFlow 1, in TensorFlow 2. I tried looking at this post Lei Mao's Log Book – Save, Load and Inference From TensorFlow 2.x Frozen Graph 14 but I was encountering a “_UserObject has no attribute ‘inputs’” error.
Does anyone know how I can work around this error, or if there are any other solutions to export an object detection SavedModel into a single frozen inference graph?

Context:
Have you tried if this hack Is still working for your model?

      Medium – 9 Aug 20

How to export a TensorFlow 2.x Keras model to a frozen and optimized graph 14

  Recently, I struggled trying to export a model built with Keras and TensorFlow 2.0 in the proper format to make inference with OpenCV’s…

    Reading time: 5 min read

      Medium – 28 Mar 21

Part-III : Convert Keras Model to TensorFlow frozen graph model 10

  3 Part blog series : Documenting the learnings of my experimentation on Object Detection using Keras, Tensorflow

    Reading time: 2 min read


Performing data wrangling on tf.data.Dataset

Question:
Are the tf.data.experimental.CsvDataset available in (https://methods)) enough to perform data wrangling and transformation usually done using libraries like scipy, numpy and pandas? . for example, i have been trying to perform an equivalent of the below line of pandas code using dataset methods but couldn’t find the suitable one for it. Looking forward to your reply.
sum_of_features = {}
for col in features:
    sum_of_features[col] = sum_ofs[col].groupby(["year", "month", "day", "second"]).sum(col)

Context:
Do you mean this page?

      TensorFlow

tf.data.experimental.CsvDataset  |  TensorFlow Core v2.7.0

  A Dataset comprising lines from one or more CSV files.

It refers to a “richer” API here:

      TensorFlow

tf.data.experimental.make_csv_dataset  |  TensorFlow Core v2.7.0

  Reads CSV files into a dataset.

And has a tutorial here:

      TensorFlow

Load CSV data  |  TensorFlow Core


Perform TimeDistributed step by step to save VRAM

Question:
We are applying TensorFlow/Keras to analyse data obtained in an astroparticle physics experiment that consists of a grid of detectors. The input of our network has a shape of (n, n, 360, embed_dim), where n*n is the size of our grid.
In the first step, we are using two TimeDistributed layers to individually analyse the signals on the detector level, followed by a convolution over all detectors to combine the results.
Increasing n, we found that when we use transformers, the necessary VRAM exceeds the 24 GB that our GPU cluster can provide, as TensorFlow seems to perform all of the n*n individual detector-analyses at once, resulting in huge tensors for large values of n.
As the operations are independent of each other, is it possible to force TensorFlow to calculate the operations step by step instead?

Context: What TF version are you using?


How do i setup tensorboard profiler for training , as currently is not working and not showing profiling tab?

Question:
Hi all, I have wanted to add profiling to my train code, but it’s not showing up in tensorboard
    %load_ext tensorboard
    experiment_log_dir = '/dbfs/prajwal.rao/logs/'
    tboard_callback = tf.keras.callbacks.TensorBoard(log_dir = "/dbfs/prajwal.rao/logs/mle_lstm_combined_profile",
                                                     histogram_freq = 1,
                                                     profile_batch = '500,540')

    model.fit(train_dataset_val,
                         steps_per_epoch=steps_per_epoch,
                         epochs=NUM_EPOCHS,
                         validation_data=val_dataset_val,
                         validation_steps=validation_steps,
                         verbose=2,callbacks = [tboard_callback])

I have installed tensorboard-plugin-profile on my system but can’t see the profile section:
but in the logs the data is there eg:
/dbfs/prajwal.rao/logs/mle_lstm_combined_profile/train/plugins/profile/2022_01_19_01_55_20/1221-040809-hr2wu5va-10-173-52-147.input_pipeline.pb0

Context:
Have you tried hitting refresh on TensorBoard?
It happened to me where “Profile” was not showing but after clicking TB refresh it showed.


Error: AttributeError: module ‘tensorflow’ has no attribute ‘lite’

Question:
Hi!
I’m recycling old motherboards that need to use tensorflow version 1.5.
When I use tflite, it gives an error.
Thanks for any help.
import tensorflow as tf

interpreter = tf.lite.Interpreter(model_path='model.tflite')  # ERROR

Error:
interpreter = tf.lite.Interpreter(model_path=‘model.tflite’)
AttributeError: module ‘tensorflow’ has no attribute ‘lite’

Context:
Maybe the lite object wasn’t available on that version of TF.
Can you try using at least 1.15?


Question:
i got
  File "..../site-packages/tensorflow_hub/module.py", line 403, in _try_get_state_scope
    "name_scope was already taken." % abs_state_scope)
RuntimeError: variable_scope bert_layer_module/ was unused but the corresponding name_scope was already taken.

when tried to use BertLayer class included with

self.bert = hub.Module(
“TensorFlow Hub”,
trainable=self.trainable,
name="{}_module".format(self.name)
)

Context: is there any help please


Model evaluation fails with: too many values to unpack (expected 2)

Question:
Can get the model evaluation to succeed thinking there is something wrong with the trained model.
Followed the turorial on:

      TensorFlow

Object Detection with TensorFlow Lite Model Maker 2

Ran:
train_data, validation_data, test_data = object_detector.DataLoader.from_csv('my-dataset.csv')

model_arch = 'efficientdet_lite0'
spec = model_spec.get(model_arch)

EPOCHS = 20
model = object_detector.create(train_data, model_spec=spec, epochs=EPOCHS, batch_size=64, train_whole_model=True, validation_data=validation_data)

.
.
.
Epoch 20/20
513/513 [========================] - 108s 211ms/step - det_loss: 0.0851 - cls_loss: 0.0691 - box_loss: 3.1992e-04 - reg_l2_loss: 0.0627 - loss: 0.1477 - learning_rate: 2.2774e-05 - gradient_norm: 1.1593 - val_det_loss: 0.0648 - val_cls_loss: 0.0523 - val_box_loss: 2.5131e-04 - val_reg_l2_loss: 0.0627 - val_loss: 0.1275

And then:
loss, accuracy = model.evaluate(test_data)

*ValueError: too many values to unpack (expected 2)*

Dataset splits (image size 500x300):

Training Set - 4.8k images  92%
Validation Set 271 images   5%
Testing Set 135  images       3%

Context: Can you print model.evaluate(test_data) directly?


Having issue with tf.keras.preprocessing.image_dataset_from_directory

Question:
When I try to read the images from directory order is not preserved. Please find the below screenshots for reference.
Below is the code
train_set = tf.keras.preprocessing.image_dataset_from_directory(
“train”, shuffle=False,
color_mode=‘grayscale’,
#class_names=class_names,
labels=LABELS_n,
#label_mode=‘int’,
seed=None,
image_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE)
plt.figure(figsize=(25, 25))
for image_batch, labels_batch in train_set.take(1):
for i in range(32):
ax = plt.subplot(8, 4, i + 1)
plt.imshow(image_batch[i].numpy())
plt.title(class_names[labels_batch[i]])
plt.axis(“off”)
because of this wrong ordering, my train set is wrongly tagged with classes.
Any help is highly
from_tensorflow.PNG1328×366 67.5 KB

image1174×76 8.68 KB

appreciated.

Context:
You could use class_names param to control order of classes.

      TensorFlow

tf.keras.utils.image_dataset_from_directory  |  TensorFlow Core v2.7.0 2

  Generates a tf.data.Dataset from image files in a directory.


The speed of training is reduced using a custom method in tensorflow.keras.layers

Question:
I’m using tensorflow.data and custom layers to solve the bottleneck of data augmentation, but I found that using tensorflow.data alone is faster than mixing, I don’t know what’s going on in the custom layers, can someone please tell me?
Thanks in advance!
This is my data augmentation code, mainly to do standardization and resize.
def random_normalization(data, mean, std):
    mean = tf.multiply(mean, tf.random.uniform(shape=(), minval=0.5,maxval=0.9, dtype=tf.float64))
    std = tf.multiply(std, tf.random.uniform(shape=(), minval=0.5,maxval=0.9, dtype=tf.float64))
    return tf.divide((tf.subtract(data, mean)), std)

def random_resize(data):
    def resizing(index, data, choice, enable, new_data, number, overlap):
        FrontEnd = tf.cond(tf.math.greater_equal(tf.subtract(index, overlap), tf.constant(0)),
                           lambda: tf.subtract(index, overlap),
                           lambda: index)

        BackEnd = tf.cond(tf.math.less(tf.add(tf.add(index, 10),overlap),tf.constant(2000)),
                          lambda: tf.add(tf.add(index, 10),overlap),
                          lambda: index)

        z1 = tf.gather(data, indices=[0], axis=1)
        z1 = tf.gather(z1, indices=tf.range(FrontEnd, BackEnd), axis=0)

        z2 = tf.gather(data, indices=[1], axis=1)
        z2 = tf.gather(z2, indices=tf.range(FrontEnd, BackEnd), axis=0)

        z3 = tf.gather(data, indices=[2], axis=1)
        z3 = tf.gather(z3, indices=tf.range(FrontEnd, BackEnd), axis=0)

        z4 = tf.gather(data, indices=[3], axis=1)
        z4 = tf.gather(z4, indices=tf.range(FrontEnd, BackEnd), axis=0)

        z5 = tf.gather(data, indices=[4], axis=1)
        z5 = tf.gather(z5, indices=tf.range(FrontEnd, BackEnd), axis=0)

        z6 = tf.gather(data, indices=[5], axis=1)
        z6 = tf.gather(z6, indices=tf.range(FrontEnd, BackEnd), axis=0)

        new_data = tf.tensor_scatter_nd_update(new_data, [[number, 0], [number, 1], [number, 2],
                                                          [number, 3], [number, 4], [number, 5]],
                                               [tf.math.reduce_mean(z1), tf.math.reduce_mean(z2),
                                                tf.math.reduce_mean(z3), tf.math.reduce_mean(z4),
                                                tf.math.reduce_mean(z5), tf.math.reduce_mean(z6)])

        return tf.add(index, 10), data, choice, enable, new_data, tf.add(number, 1), overlap

    choice = tf.random.uniform(shape=(), minval=0,maxval=4,dtype=tf.int32)
    enable = tf.random.uniform(shape=(), minval=0,maxval=1,dtype=tf.float64)
    overlap = tf.random.uniform(shape=(), minval=5,maxval=21,dtype=tf.int32)

    new_data = tf.zeros((200,6), dtype=tf.float64)
    index = tf.constant(0)
    number = tf.constant(0)
    condition = lambda index, data, choice, enable, new_data, number, overlap: tf.less(index, 2000)
    r = tf.while_loop(condition, resizing, loop_vars=(index, data, choice, enable, new_data, number, overlap))
    return r[4]

def normal_resize(data):
    data = tf.reshape(data, (2000,6,1))
    data = tf.image.resize(data, size=[200,6])
    return tf.cast(tf.reshape(data, (200,6)),dtype=tf.float64)

def augmentation(data, labels):
    mean = tf.math.reduce_mean(data,axis=0)
    std = tf.math.reduce_std(data,axis=0)
    data = tf.cond(tf.random.uniform(shape=(), minval=0, maxval=1,dtype=tf.float64) < tf.constant(0.8,dtype=tf.float64),
                   lambda: random_normalization(data, mean, std),
                   lambda: tf.divide((tf.subtract(data, mean)), std))

    # 2000 resize to 200
    data = tf.cond(tf.random.uniform(shape=(), minval=0, maxval=1,dtype=tf.float64) < tf.constant(0.8,dtype=tf.float64),
                   lambda: random_resize(data),
                   lambda: normal_resize(data))

    return data, labels

Main code, including tf.data and model
if __name__ == '__main__':
    trainDS = tf.data.Dataset.from_tensor_slices((np.random.rand(3000,2000,6),
                                                  np.concatenate((np.zeros((1500)),np.ones((1500))))))
    trainDS = (
        trainDS
        .cache()
        .shuffle(1000, reshuffle_each_iteration=False)
        .map(augmentation, num_parallel_calls=tf.data.AUTOTUNE)
        .batch(128, drop_remainder=True)
        .prefetch(tf.data.AUTOTUNE))

    input = Input((200,6))
    x = LSTM(64, return_sequences=True)(input)
    output = Dense(1,activation='sigmoid')(x)
    model = Model(input, output)
    model.compile(optimizer='adam', loss='BinaryCrossentropy')
    model.fit(trainDS, epochs=3)

Then this is the code of my custom layer, although it is a bit cumbersome, it still achieves the result I want.
import tensorflow as tf
from tensorflow.keras.layers import LSTM, Dense, Input
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Layer
import numpy as np

class CustomLayer(Layer):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    def execute(self, data, batch_size, new_data, _type):
        def _fun(index, data, _type, new_data):
            resized = tf.cond(_type,
                              lambda:augmentation(tf.reshape(tf.gather(data,[index]), (2000,6))),
                              lambda:normal_resize(tf.reshape(tf.gather(data,[index]), (2000,6))))
            values = tf.reshape(resized, (1,-1))[0]
            _Indices = self.createIndices(index)
            new_data = tf.tensor_scatter_nd_update(new_data, _Indices, values)
            return tf.add(index,1), data, _type, new_data

        index = tf.constant(0)
        condition = lambda index, data, _type, new_data: tf.less(index, batch_size)
        r = tf.while_loop(condition, _fun, loop_vars=(index, data, _type, new_data))
        return r[-1]

    def createIndices(self, BatchSizeIndex):
        def loop1(_i, BatchSizeIndex, col_num, _Indices):
            def loop2(_i, _j, BatchSizeIndex, col_num, _Indices):
                _Indices = tf.tensor_scatter_nd_update(_Indices, [[col_num, 0], [col_num, 1], [col_num, 2]],
                                                        [BatchSizeIndex, _i, _j])
                return _i, tf.add(_j,1), BatchSizeIndex, tf.add(col_num,1), _Indices

            _j = tf.constant(0)
            condition_loop2 = lambda _i, _j, BatchSizeIndex, col_num, _Indices: tf.less(_j, 6)
            r_loop2 = tf.while_loop(condition_loop2, loop2, loop_vars=(_i, _j, BatchSizeIndex, col_num, _Indices))
            return tf.add(_i,1), BatchSizeIndex, r_loop2[3], r_loop2[4]

        _Indices = tf.zeros((1200,3), dtype=tf.int32)
        col_num = tf.constant(0)
        _i = tf.constant(0)
        condition_loop1 = lambda _i, BatchSizeIndex, col_num, _Indices: tf.less(_i, 200)
        r_loop1 = tf.while_loop(condition_loop1, loop1, loop_vars=(_i, BatchSizeIndex, col_num, _Indices))
        return r_loop1[-1]

    def call(self, images, training):
        batch_size = tf.shape(images)[0]
        new_data = tf.zeros((batch_size, 200, 6), dtype=tf.float64)
        images = tf.cast(images, dtype=tf.float64)
        if training:
            data = self.execute(images, batch_size, new_data, tf.constant(True))
        else:
            data = self.execute(images, batch_size, new_data, tf.constant(False))

        return data

The final code can be modified to execute like this.
def augmentation(data):
    .....
    return data

if __name__ == '__main__':
    trainDS = tf.data.Dataset.from_tensor_slices((np.random.rand(3000,2000,6),
                                                  np.concatenate((np.zeros((1500)),np.ones((1500))))))
    trainDS = (
        trainDS
        .cache()
        .shuffle(1000, reshuffle_each_iteration=False)
        .batch(128, drop_remainder=True)
        .prefetch(tf.data.AUTOTUNE))

    input = Input((2000,6))
    x = CustomLayer()(input)
    x = LSTM(64, return_sequences=True)(x)
    output = Dense(1,activation='sigmoid')(x)
    model = Model(input, output)
    model.compile(optimizer='adam', loss='BinaryCrossentropy')
    model.fit(trainDS, epochs=3)

Results: Alone tf.data spend about 18s, tf.data+CustomLayer spend about 38s.
The thing I want to clarify is that the use of map in tf.data to run augmentation is on the CPU, but if I write augmentation in the Layer, it should theoretically run on the GPU. Why is there such a big gap between the two?
Environment: python3.6, tensorflow2.4.0

Context:
@tf.function
def _resize(data):
    new_data = tf.TensorArray(dtype=tf.float64, size=200, dynamic_size=False)
    number = tf.constant(0)
    for index in tf.range(0,10,2000):
        select_range = tf.gather(data, indices=tf.range(index, tf.add(index, 10)), axis=0)
        new_data = new_data.write(number, tf.cast(tf.reduce_mean(select_range, axis=0),dtype=tf.float64))
        number = tf.add(number,1)
    return new_data.stack()

I changed tf.while_loop to for loop, and the training time changed from 2min to 2s.


How to use a single dataset to train multiple input model in tensorflow keras

Question:
Hi all, I am trying to train a multi-input model, but have a single large dataset. that has 3 column, featuers_mlp, featuers_lstm, labels
(using pentastorm as the dataset is very large)
code :
model
LEARNING_RATE=0.001
BATCH_SIZE = 256
TRAIN_MAX_STEPS=None
STEPS = None
NUM_EPOCHS = 2
LEAKY_RELU_ALPHA = 0.1

input_layer_mlp = Input(shape=(len(mlp_feature),), name='input_mlp')
input_layer_lstm = Input(shape=(5,6,), name='input_lstm')

l1 = LSTM(64, return_sequences=True, kernel_initializer='glorot_uniform')(input_layer_lstm)
l2 = LSTM(32, return_sequences=False)(l1)
m1 = Dense(32, activation=LeakyReLU(alpha=LEAKY_RELU_ALPHA), kernel_initializer='glorot_uniform')(input_layer_mlp)
m2 = Concatenate()([l2, m1])
m3 = Dense(16, activation=LeakyReLU(alpha=LEAKY_RELU_ALPHA))(m2)
out = Dense(1, activation=LeakyReLU(alpha=LEAKY_RELU_ALPHA))(m3)
optimizer = tf.keras.optimizers.Adam(learning_rate = LEARNING_RATE)

model = tf.keras.Model(inputs=[input_layer_mlp, input_layer_lstm],
                  outputs=[out])

losses = {'out': Huber(delta=1.0)}
metrics = {'out': MeanAbsoluteError()}

model.compile(optimizer=optimizer, loss=losses, metrics = metrics)

Training code
model = build_model_mlp(in_shape=11)
with penta_train_mlp.make_tf_dataset(transform_spec=transform_spec_fn,
                                   batch_size=BATCH_SIZE) as train_dataset, \
   penta_test_mlp.make_tf_dataset(transform_spec=transform_spec_fn,
                                 batch_size=BATCH_SIZE) as val_dataset:
    # tf.keras only accept tuples, not namedtuples
#     print(train_dataset)
    train_dataset_val = train_dataset.map(lambda x: (x.features, x.labels))
    steps_per_epoch = len(penta_train_mlp) // BATCH_SIZE

    val_dataset_val = val_dataset.map(lambda x: (x.features, x.labels))
    validation_steps = max(1, len(penta_test_mlp) // BATCH_SIZE)

    print(f"steps_per_epoch: {steps_per_epoch}, validation_steps: {validation_steps}")

    hist = model.fit(train_dataset_val,
                     steps_per_epoch=steps_per_epoch,
                     epochs=NUM_EPOCHS,
                     validation_data=val_dataset_val,
                     validation_steps=validation_steps,
                     verbose=2)

# print("Validation Accuracy: {}".format(accuracy))

error:
 Failed to find data adapter that can handle input: (<class 'tuple'> containing values of types {"<class 'tensorflow.python.data.ops.dataset_ops.DatasetV1Adapter'>"}), <class 'tensorflow.python.data.ops.dataset_ops.DatasetV1Adapter'>

How do I create a multi-input dataset?
I tried using tensor slices, but as the data is already in dataset format it does not work
dataset_train = tf.data.Dataset.from_tensor_slices(({'input_mlp': train_mlp_x, 'input_lstm': train_lstm_x}, train_mlp_y)).batch(1)#tf.data.Dataset.from_tensor_slices(((val_mlp_x, data_b), labels)).batch(1).repeat()

error:
 Unbatching a dataset is only supported for rank >= 1

Context:
solved with
df = pd.DataFrame({
‘input_x’: data_a,
‘input_y’: data_b,
‘label’ : labels
})


Question:
Can someone guide, how to include the Custom DataGenerator functionality from tf.keras with methods
keras.utils.Sequence ,
 on_epoch_end, __len__, __getitem__ ,
`
in tensorflow alone?

Context: I don’t understand your issue, do you have a minimized standalone example or Colab?


Question:
Hi, community!
For research purposes, I started looking into tf.autodiff.ForwardProp. However, I have also noticed that supporting batches of tangents inside the function are not yet fully implemented 10. I saw that there has been some awesome work on this topic from a GSOC student last year, and I would like to bring it to an end or bring it forward at least!
However, it is worth noting that I am also a student, and I would need some support on the matter.
Thanks!

Context:
Welcome to the TF forum,
Can you try to comment on the ticket as I don’t think Allen is on the forum.
/cc @markdaoust


How to modify network output before calculating loss function

Question:
I’m training a 3D image classification network, and would like to modify the output of the network before the loss function is calculated. However, it’s not clear to me how best to do this in Tensorflow, so I’m looking for advice.
The output of the network (a U-net) is a one-hot-encoded 3D volume. I would like to dilate the predicted segmentation by an amount that depends on the location of each pixel within the volume. The image transforms I need to use are available in numpy (and/or scipy), but it’s not clear to me how best to incorporate that functionality between the network output and the loss function. I’ve written a custom model class with a train_step method that calls the loss function, but there doesn’t seem to be an clear way to move between tensors and numpy arrays. Or is there a different aproach I could be taking…?

Context:
It is explained here in the doc:

Since the function takes numpy arrays, you cannot take gradients through a numpy_function. If you require something that is differentiable, please consider using tf.py_function.

If you just need a dilation you could try to add a dilation layer:

      stackoverflow.com

  Tensorflow dilation behave differently than morphological dilation 2

  python, tensorflow, image-processing, mathematical-morphology

  asked by

    Jav

  on 09:19AM - 14 Feb 19 UTC

We have also a subset of numpy API (with some limits) available at:

      TensorFlow

NumPy API on TensorFlow  |  TensorFlow Core 1


Question: I didn’t find any official documentation for installing TensorFlow 2.5 on Mac M1 pro. Any resource or steps would be helpful.

Context:
Have you tried with:

      Apple Developer

Tensorflow Plugin - Metal - Apple Developer 4

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


Tensor Flow Lite Model Maker - add more epochs after one training run

Question:
Hello,
I ran a image_classifier.create(train_data, validation_data=validation_data, epochs=70)
I wanted to add more epochs and carry-forward the learning gained by 70.
How do I do that?

Context:
Did you try
image_classifier.create(train_data, validation_data=validation_data, model_dir = “ckp_path”, epochs=20)


Question:
I’m working on some work for my first chatbot
from tensorflow.models.rnn.translate import seq2seq_model
I get an error when I try to run a “.py” that contains
Traceback (most recent call last):
   File "translate.py", line 29, in <module>
     from tensorflow.models.rnn.translate import seq2seq_model
ModuleNotFoundError: No module named'tensorflow.models'

Is there a way to solve this?

Context: What tutorial/doc are you using?


Question: How can I detect an object in the image coming from rtsp using flutter tensorflow? I tried to connect the rtsp connection I established instead of the camera, but I kept failing.

Context:
I think that before this you need to solve retriving single image from an rtsp stream.
Check or post in this thread:

      github.com/flutter/flutter

      RTSP (real time streaming) protocol in Flutter? 2

        opened

        Dec 5, 2018

          closed

        Dec 22, 2020

          weekaung

          severe: new feature

          plugin

          p: first party

          a: video

          customer: crowd

          p: video_player

          P6

    Hi Flutter Team,

Our company is developing an IoT Security solution which req…uires us to access live cameras using the rtsp protocol. On looking into the video_player plugin that you have developed, we realized that it does not support this protocol.

Will you be enabling this protocol in the near future? Or do you have a solution for our problem?
We would very much appreciate your help.

Thank you,
David Lee :>


How to stop and resume object detector training(object detection model maker)

Question:
I am working on object detection with autonomous datasets . I want to train my model with 10000 train images,2000 test,2000 validation images.So, i will use object detection tensorflow lite model maker.
Project link : tensorflow.org/lite/tutorials/model_maker_object_detection 3
But the large dataset and batch size of 32, the training takes 50 epochs and takes 2 days(Step 3).I can’t keep my computer on for two days.I am running the project in jupyter notebook
How can i stop model training and again resume it ? (e.g. stop the 10th epoch and continue one day later)

Context:
Hi Ugur,
I don’t think you can do that. Model Maker, as of today, doesn’t have a stop and resume option.
you have a couple of options:

make sure you’re using a GPU for training. This makes a huge difference in execution time
run the same notebook on the cloud (eg: GCP) with a higher spec machine. This way you can keep the machine turned on during the process. The drawback is that you have to pay

Object Detection is a complex task and it’s expected that it would take a long time to finish, even with top HW spec.
@Yuqi_Li any other suggestion?


TFLITE model maker DataLoader.from_csv returns a None type

Question: i am trying to load my datasets using a csv file using the DataLoader.from_csv('path to csv',  images_dir = 'path to image directory) however when i go on to start training training the model i get an error AttributeError: 'NoneType' object has no attribute 'label_map'. it seems like whatever from returned from Dataloader.from_csv` was a None type

Context:
Hi @Ebyau_Bryan ! Can you show the code you are using to load the data. The function returns None only if it doesn’t find correct train, training,test data in the CSV.

      TensorFlow

tflite_model_maker.object_detector.DataLoader  |  TensorFlow Lite 7

  DataLoader for object detector.


Custom audio classification with Teachable machine

Question:
Hi all,
I am starting a new project and want to build a custom audio classifier using the great website Teachable machine and his speech-commands model.
I have collected audio samples from a personal user interface, and now i have many wav files available.
My goal is to upload them inside teachable machine website, following the audio zip format, which is just a zip file containing :

all the samples sound files for a class concatenated inside a single webm file
a json file explaining for each sound sample his audio characteristics.

Using a simple Node.js script file, i achieve to concatenate my files, and creating the json file.
The last missing information foreach file is an attribute called “frequencyFrames”. For a sample i have downloaded from TM (and created online using microphone), it is an array of array.
Is someone knows how can i get this information for each wav file ?
Thanks
Regards

Context:
Hi Vincent,
can you share the link where you read those instructions?
I think that frequencyFrames might be the framerate of the audio files. This is defined when you record them (eg: 16kHz, 24kHz, …)


Question:
Hi everyone, I’m new to the forum, I don’t know if I posted in the right place.
I am a Ph.D. student in web science, and my thesis is on the recognition of educational emotions.
My current project is a serious game, which recognizes students’ emotions and, depending on the result, makes the game easier or more difficult.
I would like to know if anyone on the forum has worked with recognition of educational emotions.
Thank you very much.

Context:
Check also his assembled tutorial

      CodeProject – 2 Apr 21

Real-Time Facial Emotion Detection with Webcam in the Browser UsingTensorFlow.js

  In this article which we;ll use the live webcam video of our face and see if the model can react to our facial expressions in real time.

    Reading time: 3 min read


Couldn’t submit Tensorflow Certificate Exam

Question:
Hi,
I recently took the TensoFlow Developer Certificate exam. I wasn’t able to end the exam because of a Java exception when submitting it. The description of the exception is below:
java.io.IOException: Error writing request body to server
at java.base/sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(HttpURLConnection.java:3657)
at java.base/sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:3640)
at com.google.api.client.util.ByteStreams.copy(ByteStreams.java:52)
at com.google.api.client.util.IOUtils.copy(IOUtils.java:91)
at com.google.api.client.http.AbstractInputStreamContent.writeTo(AbstractInputStreamContent.java:67)
at com.google.api.client.http.javanet.NetHttpRequest$DefaultOutputWriter.write(NetHttpRequest.java:76)
at com.google.api.client.http.javanet.NetHttpRequest.writeContentToOutputStream(NetHttpRequest.java:174)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:117)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:84)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1012)
at com.google.developers.certification.android.engine.CertTransport.execute(CertTransport.kt:69)
at com.google.developers.certification.android.project.ExamControl.submitExam(ExamControl.kt:71)
at com.google.developers.certification.android.project.SubmitExam.run(SubmitExam.kt:80)
at com.intellij.openapi.progress.impl.CoreProgressManager.startTask(CoreProgressManager.java:436)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.startTask(ProgressManagerImpl.java:120)
at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcessWithProgressSynchronously$8(CoreProgressManager.java:542)
at com.intellij.openapi.progress.impl.ProgressRunner.lambda$new$0(ProgressRunner.java:83)
at com.intellij.openapi.progress.impl.ProgressRunner.lambda$submit$3(ProgressRunner.java:244)
at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$runProcess$2(CoreProgressManager.java:188)
at com.intellij.openapi.progress.impl.CoreProgressManager.lambda$executeProcessUnderProgress$12(CoreProgressManager.java:624)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:698)
at com.intellij.openapi.progress.impl.CoreProgressManager.computeUnderProgress(CoreProgressManager.java:646)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:623)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:66)
at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:175)
at com.intellij.openapi.progress.impl.ProgressRunner.lambda$submit$4(ProgressRunner.java:244)
at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1700)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:668)
at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1$1.run(Executors.java:665)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.util.concurrent.Executors$PrivilegedThreadFactory$1.run(Executors.java:665)
at java.base/java.lang.Thread.run(Thread.java:829)
Can someone tell me what is wrong or indicate me a solution so it won’t happen in the future?

Context:
I had similar problems during my attempt. Could not submit.
I just installed the plugins, restarted the computer and installed it again. The exam picked up from there


Question:
'm a beginner
How can I solve it?
Cent7
tensorflow 2.7.0
Python 3.8.8
First, try seq2seq with the prepared model and dataset.
(Try Walkthrough as it is)
[root@localhost ~]# PROBLEM=translate_ende_wmt32k
[root@localhost ~]# MODEL=transformer
[root@localhost ~]# HPARAMS=transformer_base_single_gpu
[root@localhost ~]# t2t-datagen \
>  --data_dir=$DATA_DIR \
>  --tmp_dir=$TMP_DIR \
>  --problem=$PROBLEM

type or paste code here
2022-01-14 16:25:53.603694: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2022-01-14 16:25:53.603743: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "/root/anaconda3/bin/t2t-datagen", line 4, in <module>
    __import__('pkg_resources').run_script('tensor2tensor-optuna==1.10.0', 't2t-datagen')
  File "/root/anaconda3/lib/python3.8/site-packages/pkg_resources/__init__.py", line 651, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/root/anaconda3/lib/python3.8/site-packages/pkg_resources/__init__.py", line 1448, in run_script
    exec(code, namespace, namespace)
  File "/root/anaconda3/lib/python3.8/site-packages/tensor2tensor_optuna-1.10.0-py3.8.egg/EGG-INFO/scripts/t2t-datagen", line 18, in <module>
    from tensor2tensor.bin import t2t_datagen
  File "/root/anaconda3/lib/python3.8/site-packages/tensor2tensor_optuna-1.10.0-py3.8.egg/tensor2tensor/bin/t2t_datagen.py", line 39, in <module>
    from tensor2tensor import problems as problems_lib  # pylint: disable=unused-import
  File "/root/anaconda3/lib/python3.8/site-packages/tensor2tensor_optuna-1.10.0-py3.8.egg/tensor2tensor/problems.py", line 33, in <module>
    all_problems.import_modules(all_problems.ALL_MODULES)
  File "/root/anaconda3/lib/python3.8/site-packages/tensor2tensor_optuna-1.10.0-py3.8.egg/tensor2tensor/data_generators/all_problems.py", line 120, in import_modules
    importlib.import_module(module)
  File "/root/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/root/anaconda3/lib/python3.8/site-packages/tensor2tensor_optuna-1.10.0-py3.8.egg/tensor2tensor/data_generators/algorithmic.py", line 26, in <module>
    from tensor2tensor.data_generators import problem
  File "/root/anaconda3/lib/python3.8/site-packages/tensor2tensor_optuna-1.10.0-py3.8.egg/tensor2tensor/data_generators/problem.py", line 31, in <module>
    from tensor2tensor.layers import modalities
  File "/root/anaconda3/lib/python3.8/site-packages/tensor2tensor_optuna-1.10.0-py3.8.egg/tensor2tensor/layers/modalities.py", line 22, in <module>
    from tensor2tensor.layers import common_attention
  File "/root/anaconda3/lib/python3.8/site-packages/tensor2tensor_optuna-1.10.0-py3.8.egg/tensor2tensor/layers/common_attention.py", line 4344, in <module>
    @expert_utils.add_var_scope()
  File "/root/anaconda3/lib/python3.8/site-packages/tensor2tensor_optuna-1.10.0-py3.8.egg/tensor2tensor/utils/expert_utils.py", line 68, in add_var_scope
    return add_scope(scope, scope_fn=tf.variable_scope)
AttributeError: module 'tensorflow' has no attribute 'variable_scope'

Context:
Resolve
Add CXXABI 1.3.9


EfficientNetB0 kernel crashes as training begins

Question:
I’m trying to use EfficientNetB0 on Keras. The Keras model instantiates correctly, finds the training images and sets up the first epoch, but then the Python kernel dies and re-initializes.
It isn’t just EfficientNetB0 – other architectures that have worked fine now produce the same behavior. What’s different is that I’ve had to install keras-nightly in order to get the keras-efficientnets package. I reinstalled Keras 2.7.0 and Tensorflow 2.7.0 without uninstalling the other Keras versions, so what I have now is:
pip list | findstr "keras"
keras                         2.7.0
keras-efficientnets           0.1.7
keras-nightly                 2.9.0.dev2022011508

pip list | findstr "tensorflow"
tensorflow                    2.7.0
tensorflow-estimator          2.7.0
tensorflow-gpu                2.7.0
tensorflow-io-gcs-filesystem  0.23.1

My training code, which has worked fine for various Keras model architectures, is below.

> def train(self, trainpath, validatepath, l_rate=.001, epochs=50, save_every_epoch=True, patience=50, batch_size=16):
>         os.makedirs(trainpath, exist_ok=True)
>         model = self.model
>         nb_train_samples = sum(len(files) for _, _, files in os.walk(trainpath))
>         nb_validation_samples = sum(len(files) for _, _, files in os.walk(validatepath))
>         if save_every_epoch:
>             mc = ModelCheckpoint(filepath=self.codepath+"saved-ResNet50-model-"+str(self.img_size)+"-{epoch:02d}-{accuracy:.2f}-{loss:.2f}.h5", \
>                     monitor='accuracy', verbose=1, save_best_only=False, mode='min')
>             callbacks_list = [mc]
>         else:
>             es = EarlyStopping(monitor='accuracy', mode='min', verbose=1, patience=patience)
>             mc = ModelCheckpoint(filepath=self.codepath+"ResNet50-model"+self.name+".h5", \
>                     monitor='accuracy', verbose=1, save_best_only=True)
>             callbacks_list = [es, mc]
>         model.compile(loss='binary_crossentropy',
>                       optimizer=Adam(lr=l_rate),
>                       metrics=['accuracy'])
>         train_datagen = ImageDataGenerator(rescale=1. / 255,
>                             horizontal_flip=True, vertical_flip=True,
>                             brightness_range=[0.8,1.2])
>         test_datagen = ImageDataGenerator(rescale=1. / 255)
>         train_generator = train_datagen.flow_from_directory(
>                 trainpath,
>                 target_size=(self.img_size, self.img_size),
>                 class_mode='binary')
>         validation_generator = test_datagen.flow_from_directory(
>                 validatepath,
>                 target_size=(self.img_size, self.img_size),
>                 batch_size=batch_size,
>                 class_mode='binary')
>         print("Training "+self.name+" with tiles in "+trainpath)
>         model.fit_generator(
>                 train_generator,
>                 epochs=epochs,
>                 callbacks=callbacks_list,
>                 validation_data=validation_generator)
>         print(self.name+" trained successfully.\n")
>         return

Context:
Why you need to use keras-efficientnets?
Can you use EfficientNet B0 to B7 ?


Please implement the resnet with functional API not sequential

Question: I’m confused in implementation of resnet…

Context: What impl are you using?


Question: Hello everyone!!! I’m a complete beginner but I’m quite interested in contributing to TF. I came to know that I should read “Deep Learning with Python” by fchollet. I have already started reading that book. So after reading this book, what can I do so that I can reach to code contributor stage?? I’m very comfortable with python. So can I contribute with python? Is there anything I need to learn for contributing to code? Can someone please guide me? Any help is appreciated. Thank you.

Context:
Hi welcome to the TF Forum.
Please take a look at Contribution to the open source projects for Beginners - #3 by lgusm 6


Question:
I am trying to train a custom object detection model using TF Lite Model Maker by referring the Train a salad detector with TensorFlow Lite Model Maker notebook 4
The dataset can be loaded with
dataloader = object_detector.DataLoader.from_pascal_voc(‘cartoon-detection/images’, ‘cartoon-detection/annotations’, [‘doraemon’, ‘mcqueen’, ‘mickey’, ‘mrbean’, ‘scooby’])
However, as I run the cell, I see an error that says, ValueError: Image format not JPEG despite the images being of the said format. Here’s a snapshot of the same.

image1900×676 274 KB

I have tried using both the extensions - .jpg and .jpeg but nothing seems to work. The error persists.

Context:
Hi @Nitin_Tiwari
From the image you have posted I see also .xml files inside the folder. Maybe the DataLoader tries to use .jpg images and finds .xml files. Check again the folder or the link you have downloaded the dataset.


Question:
Hey everyone!! I’m helping someone working on his master thesis and the teacher guiding him suggested using the Word2Vec algorithm to create an heatmap related to stock price variations.
My question is if it is possible to adapt the original Word2Vec algorithm to make it work with data different than words/strings. That is, just change the data that is passed to the model…
Any help on this toppic would be much appreciated.
Thank you.

Context:
I think you could consider something similar to this line of work to create a stock embeddings:

      ACL Anthology

Stock Embeddings Acquired from News Articles and Price History, and an... 5

  Xin Du, Kumiko Tanaka-Ishii. Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics. 2020.

Probably you want to ignore the news in your problem set but I think they are still quite useful.


Run “Object Detection with TensorFlow Lite Model Maker” in Raspberry PI

Question:
Greetings to the whole community.
I have successfully run the object detector (examples/lite/examples/object_detection/raspberry_pi at master · tensorflow/examples · GitHub 3) on a Raspberry PI 3B with OS Buster.
Now I try to run the model trained in the Object Detection lesson with TensorFlow Lite Model Maker (Object Detection with TensorFlow Lite Model Maker 1) but it throws a segmentation error. Reviewing the lesson it is specified that “The model can be integrated into an Android or iOS application”, but it does not indicate what to do for its execution in a Raspberry PI.
Can anyone point me to the additional steps to run the model on a Raspberry PI?
Thanks

Context:
Hi.
Indeed, I am using tflite-maker 2.5.0 on Raspberry OS Buster, but when I try to upgrade, it is not possible (apparently there is no installer for this version).
If I use Raspberry OS Bullseye it is possible to install tflite-maker 2.7.0, but in this distribution the OS does not recognize the camera. I have already done the relevant steps for its configuration, but it is still not recognized.
Based on your comments, I opted to try converting the model using tflite-maker version 0.3.1, instead of version 0.3.4 which is installed by default. To do so, I modify the original line of the Object Detection with TensorFlow Lite Model Maker example (Object Detection with TensorFlow Lite Model Maker 1)
!pip install -q --use-deprecated=legacy-resolver tflite-model-maker
Adding the version I want to install (0.3.1)
!pip install -q --use-deprecated=legacy-resolver tflite-model-maker==0.3.1
Once trained and exported the model with this version, the model runs correctly (on the Raspberry OS Buster)


Question:
i got
tensorflow.python.framework.errors_impl.InvalidArgumentError:  logits and labels must be
broadcastable: logits_size=[68,30522] labels_size=[68,1619381]
	 [[node categorical_crossentropy/softmax_cross_entropy_with_logits (defined at fit-bert.py:379) ]]
[Op:__inference_train_function_7360]

Context: You may need to check the model filter coefficients you created. I guess your predict output numbers and total dataset size don’t match.


[Opinion needed] What community programs would you like to see in 2022?

Question:
Hi everyone!
I would love to hear from you: the TensorFlow Community!!!
In order to consistently provide value to our members, I would like to hear what programs or initiatives you would like to see in the TensorFlow Community this year?
More events? tutorials? resources for advanced learners or beginners? Please share your needs and ideas in this thread!
Thank you in advance and I wish you a great year ahead!
PS: I hope to see you soon at one of our events

Context:
Hi!
I think there could be special events based on themes, for example, a special event about games, where people who are developing something focused on games can talk about their projects, and there could also be training on the subject.


After converting the model to .tflite and running it on Android, the accuracy drops

Question:
Hello everyone!
This is my first neural network, so there are often problems. And now there is a problem that I can’t solve.
My network produces a binary classification (patient is healthy, patient is sick). The input layer is fed 12 numeric values. I created and trained a neural network in Collab, it trained well and shows acceptable results on the validation sample (val_accuracy: 0.95
val_loss: 0.13), but after converting the model to .tflite and running it on a smartphone, it can’t predict anything.
I changed the number of layers, converted the model with tf.lite.TFLiteConverter.from_saved_model and tf.lite.TFLiteConverter.from_keras_model, viewed .tflite in Netron, tried to change the data input in Android, but nothing helped.
I think the problem is the wrong data transfer to the input layer of the tflite model in Android, but this is just a guess. And if so, please tell me how to fix the error?
This is my Colab code
raw_dataset = pd.read_csv('data.csv')
dataset = raw_dataset.copy()

train_dataset = dataset.sample(frac=0.8, random_state=0)
test_dataset = dataset.drop(train_dataset.index)

X = train_dataset.values
Y = test_dataset.values

Y = np.array(Y).astype("float32")

test_x, test_y = X[:,0:12], X[:,12]
train_x, train_y = Y[:,0:12], Y[:,12]

model = models.Sequential()
model.add(layers.Dense(64, activation = "tanh", input_dim=12))
model.add(layers.Dense(32, activation = "tanh"))
model.add(layers.Dense(16, activation = "tanh"))
model.add(layers.Dense(1, activation = "sigmoid"))
model.summary()

model.compile( optimizer = "adam", loss = "binary_crossentropy", metrics = ["accuracy"])

results = model.fit( train_x, train_y, epochs = 100, batch_size = 10, validation_data = (test_x, test_y))

# Save model
mobilenet_save_path = "my_SavedModel"
tf.saved_model.save(model, mobilenet_save_path)

# Convert the SavedModel
converter = tf.lite.TFLiteConverter.from_saved_model("my_SavedModel") # path to the SavedModel directory
tflite_model = converter.convert()

# Save the tflite_model.
with open('modelSavedModel.tflite', 'wb') as f:
  f.write(tflite_model)

This is my Java code in Android
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(48);
                byteBuffer.putFloat(valueInt1);
                byteBuffer.putFloat(valueInt2);
                byteBuffer.putFloat(valueInt3);
                byteBuffer.putFloat(valueInt4);
                byteBuffer.putFloat(valueInt5);
                byteBuffer.putFloat(valueInt6);
                byteBuffer.putFloat(valueInt7);
                byteBuffer.putFloat(valueInt8);
                byteBuffer.putFloat(valueInt9);
                byteBuffer.putFloat(valueInt10);
                byteBuffer.putFloat(valueInt11);
                byteBuffer.putFloat(valueInt12);

                try {
                    ModelSavedModel model = ModelSavedModel.newInstance(context);

                    // Creates inputs for reference.
                    TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 12}, DataType.FLOAT32);
                    inputFeature0.loadBuffer(byteBuffer);

                    // Runs model inference and gets result.
                    ModelSavedModel.Outputs outputs = model.process(inputFeature0);
                    TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();

                    // Releases model resources if no longer used.
                    model.close();
                    float preResult = outputFeature0.getFloatArray()[0]*100;
                    int result = (int) preResult;
                    System.out.println(preResult);
                } catch (IOException e) {
                    // TODO Handle the exception
                }

Context:
Van_Gil:

My network produces a binary classification (patient is healthy, patient is sick). The input layer is fed 12 numeric values. I created and trained a neural network in Collab, it trained well and shows acceptable results on the validation sample (val_accuracy: 0.95
val_loss: 0.13), but after converting the model to .tflite and running it on a smartphone, it can’t predict anything.

Hello @Van_Gil Prior to deploying the model, have you considered the following by any chance to improve generalization?

Post-training quantization (Post-training quantization  |  TensorFlow Lite 1):

For example, float16 quantization can lead to a “minimal loss in accuracy”.
Another example: integer-only “16x8” quantization (experimental), which “can improve accuracy significantly, but only slightly increase model size”.

Some basic things to try to reduce overfitting and improve generalization (Overfit and underfit  |  TensorFlow Core):

More training data; “quality” data/data quality, etc.
Weight regularization, dropout, and various other techniques, which are active areas in ML research.
In “imbalanced datasets” …" overfitting is a significant concern from the lack of training data" (Classification on imbalanced data  |  TensorFlow Core).

Or, perhaps, the issue here is with the Java code.
A few more sources:

      Google Developers

On-Device Machine Learning  |  Google Developers 3

  Run machine learning models in your Android, iOS, and Web app. Low latency. Keep data on-device. Works offline.

      TensorFlow

TensorFlow Lite | ML for Mobile and Edge Devices 2

  A deep learning framework for on-device inference. Train and deploy machine learning models on mobile and IoT devices, Android, iOS, Edge TPU, Raspberry Pi.


Help getting started with a personal project

Question:
Hi, excited to be here
I’m very new to tensorflow, and was hoping that someone could point me in the right direction.
I want to create a little system that can have inputs in the form of:

Text (inputs can be PDF, Office document, or Picture with text)
Audio (spoken, possibly multiple participants in the same conversation, but also individuals)
Video (same as audio, but with images too)

I want to then be able to find similarities/themes/contexts between them.
Could anyone point me to some existing libraries/models that I can integrate to achieve this? And any useful tutorials?
TIA
A

Context:
Multimodal deep learning isn’t one of the easiest domain to handle as the first project.
For an introduction you could start to look at some material in:

      GitHub

GitHub - pliang279/awesome-multimodal-ml: Reading list for research topics in... 1

  Reading list for research topics in multimodal machine learning - GitHub - pliang279/awesome-multimodal-ml: Reading list for research topics in multimodal machine learning


Question:
Hi,
how can I integrate model from this tutorial Reconhecimento de áudio simples: como reconhecer palavras-chave  |  TensorFlow Core 2 into my Android app? What inputs should I provide and how to interpret the outputs?

Context:
Check out this reference app:

      github.com

    examples/lite/examples/speech_commands/android at master · tensorflow/examples

  master/lite/examples/speech_commands/android

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

And this community app:

      GitHub

GitHub - windmaple/tflite-asr: An example app that demos how to use TFLite to...

  An example app that demos how to use TFLite to do automatic speech recognition on-device - GitHub - windmaple/tflite-asr: An example app that demos how to use TFLite to do automatic speech recognit...


Question: All the tutorials I found on the Internet are to convert H5 model into tflite model, but I didn’t find that the directly trained model is tflite. What should I do if the trained model is tflite? Because I tried to convert H5 to tflite, but it seems that I lost a little more precision

Context: You lose precision as normal TF model by default uses higher degree of precision for calculations (unit32) however post conversion to TFLite, it becomes slightly lower due to conversion of values to (unit8). As far conversion from H5Model to TFLite, that’s the way it is. I shared my personal experience, so accuracy will drop with TFLite but sole idea behind TFLite is to give inference using edge devices like Rasp Pi , Coral TPU etc, so its a tradeoff.


Checkpoint not saved when using ModelCheckpoint with save_freq = 5000

Question:
hello,
I was not sure if I could report it as a bug…  I’m practicing using an argument, save_freq = as an interger from an online course (they use tensorflow 2.0.0 while I have the latest tensorflow, 2.5.0).
here’s the relevant documentation but without an example using interger.

      TensorFlow

tf.keras.callbacks.ModelCheckpoint  |  TensorFlow Core v2.7.0 1

  Callback to save the Keras model or model weights at some frequency.

here is my code:
import tensorflow as tf
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D

using a CIFAR dataset sample
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0

Use smaller subset – speeds things up
x_train = x_train[:10000]
y_train = y_train[:10000]
x_test = x_test[:1000]
y_test = y_test[:1000]

define a function that creates a new instance of a simple CNN.
def get_new_model():
model = Sequential([
Conv2D(filters=16, input_shape=(32, 32, 3), kernel_size=(3, 3),
activation=‘relu’, name=‘conv_1’),
Conv2D(filters=8, kernel_size=(3, 3), activation=‘relu’, name=‘conv_2’),
MaxPooling2D(pool_size=(4, 4), name=‘pool_1’),
Flatten(name=‘flatten’),
Dense(units=32, activation=‘relu’, name=‘dense_1’),
Dense(units=10, activation=‘softmax’, name=‘dense_2’)
])
model.compile(optimizer=‘adam’,
loss=‘sparse_categorical_crossentropy’,
metrics=[‘accuracy’])
return model

Create Tensorflow checkpoint object with epoch and batch details
checkpoint_5000_path = ‘/model_checkpoints_5000/checkpoint_{epoch:02d}-{batch:04d}’
checkpoint_5000 = ModelCheckpoint(filepath = checkpoint_5000_path,
save_weights_only = True,
save_freq = 5000,
verbose = 1)

Create and fit model with checkpoint
model = get_new_model()
model.fit(x = x_train,
y = y_train,
epochs = 3,
validation_data = (x_test, y_test),
batch_size = 10,
callbacks = [checkpoint_5000])
It is meant to make and save the filenames including the epoch and batch number.
However,  the files are not created. After I create manually this directory, model_checkpoints_5000, no files are added in.
(we can check  the contents by running ’ ! dir -a model_checkpoints_5000’ (windows)
or  ‘ls -lh model_checkpoints_500’ (linux)).
I have also tried to change to ‘’/model_checkpoints_5000/checkpoint_{epoch:02d}’, it still does not save the files with every epoch’s number.

Context:
Maria_B:

checkpoint_5000_path = ‘/model_checkpoints_5000/checkpoint_{epoch:02d}-{batch:04d}’
checkpoint_5000 = ModelCheckpoint(filepath = checkpoint_5000_path,
save_weights_only = True,
save_freq = 5000,
verbose = 1)

Hi @Maria_B
Try changing the save_freq parameter in ModelCheckpoint to something smaller, like 1000, since you’re only training over 3 epochs here.
Full code:
import tensorflow as tf
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
x_train = x_train / 255.0
x_test = x_test / 255.0

x_train = x_train[:10000]
y_train = y_train[:10000]
x_test = x_test[:1000]
y_test = y_test[:1000]

def get_new_model():
  model = Sequential([
                      Conv2D(filters=16,
                             input_shape=(32, 32, 3),
                             kernel_size=(3, 3),
                             activation='relu',
                             name='conv_1'),
                      Conv2D(filters=8,
                             kernel_size=(3, 3),
                             activation='relu',
                             name='conv_2'),
                      MaxPooling2D(pool_size=(4, 4),
                                   name='pool_1'),
                      Flatten(name='flatten'),
                      Dense(units=32, activation='relu', name='dense_1'),
                      Dense(units=10, activation='softmax', name='dense_2')
                      ])
  model.compile(optimizer='adam',
                loss='sparse_categorical_crossentropy',
                metrics=['accuracy'])
  return model

checkpoint_5000_path = '/model_checkpoints_5000/checkpoint_{epoch:02d}-{batch:04d}'
checkpoint_5000 = ModelCheckpoint(filepath=checkpoint_5000_path,
                                  save_weights_only=True,
                                  save_freq=1000,
                                  verbose=1)

model = get_new_model()

model.fit(x=x_train,
          y=y_train,
          epochs=3,
          validation_data=(x_test, y_test),
          batch_size=10,
          callbacks=[checkpoint_5000])

Your output should hopefully include the following during training:
Epoch 00001: saving model to /model_checkpoints_5000/checkpoint_01-1000
1000/1000 [==============================] ......
Epoch 2/3
 999/1000 [============================>.] ......
Epoch 00002: saving model to /model_checkpoints_5000/checkpoint_02-1000
1000/1000 [==============================] ......
Epoch 3/3
 997/1000 [============================>.] ......
Epoch 00003: saving model to /model_checkpoints_5000/checkpoint_03-1000

Then, check the /model_checkpoints_5000/ folder.
Let us know if this helps.


Question:
Hi there,
At the beginning of this year, I’ve spent a lot of time learning about handwriting and speech recognition, but there wasn’t any out-of-the-box solution in TFJS since most of the solutions rely on the CTC loss calculation algorithm. I’ve found this in the issue list: https://github.com/tensorflow/tfjs/issues/1759 3 and also some hints that “it would be good to have it in TFJS”.
Since I had time, I prepared a naive implementation of the original paper, which currently fits into the TFJS ecosystem - I mean, it’s callable, it runs one sample ok, it handles batches as well, fits into the layered models’s call hierarchy, and calculates the loss and the gradient so that model.fit() would work.
I’m struggling with the tests. The obvious ones (prediction and the label is the same so it returns the expected zero gradient) are there, but there aren’t any I could find in the Python implementation. So if somebody could help me out with that, I’d be really grateful.
Also, I’m planning to donate the code to the TFJS project, but wouldn’t want to commit faulty code.
Thanks,
hp

Context:
Thanks for sharing your enthusiasm and contributing to the TensorFlow.js ecosystem! This sounds like a really interesting project. Just a heads up a lot of our engineers are out of office for the holidays this week so it may be best to pick this up in the new year.
On a somewhat related note however (I know this is not handwriting recognition) I have seen a promising library for OCR come out in TFJS recently from a group of folk over in France. They may have some pointers for guidance for you as similar domain in the meantime.

      GitHub

GitHub - mindee/doctr-tfjs-demo: Javascript demo of docTR, powered by... 1

  Javascript demo of docTR, powered by TensorFlowJS. Contribute to mindee/doctr-tfjs-demo development by creating an account on GitHub.

PS you said you had a demo somewhere - is it somewhere I can try for handwriting / speech recognition?


How to load tflite model in tfjs for pose classification?

Question: Hi, I am working on a project for yoga pose detection aiming at a much accurate model. I have trained the model using the MoveNet pose detection model using Keras CNN layers and finally exported tflite model after training. But I am finding difficulty in how to load and use this model in my web app. I will be using tfjs for pose detection using webcam and want my trained model to detect the particular yoga pose. Can anyone guide me!

Context: @Jason  would probably be of great help. I am new here and to ML in general but I hope you get the help you need.


Question:
I am new to tensorflow quantum. I’d just downloaded tensorflow 2.4.1 along with qiskit and pennylane. When I tried to install Tensorflow Quantum based on the instructions given on this link: ติดตั้ง TensorFlow Quantum 3, I received this installation error:
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorflow 2.4.1 requires protobuf>=3.9.2, but you have protobuf 3.8.0 which is incompatible.

For the record, all the installation is done within a Python 3.7.9 environment to avoid conflicts with other packages. This issue occurred on my Windows 10 machine.

Context: Hi @Hatedfate thank you for raising this question. Our protobuf dependency has been made artifically tight in certain areas In the latest release version of tensorflow quantum. (TF having certain requirements that conflicted with cirq requirements). While pip does give a dependancy warning, things will still function correctly (just tested by running this tutorial on colab w/ py37 without issues) and this should be resolved in the next release version of TFQ.


How to load SlicingOpLambda and TFOpLambda Layer in tfjs?

Question: I’m trying to train a custom model for pose detection using MoveNet. For that, I need to normalise the landmarks or key points of the training data. The Keras and tf functions which are being used in python for normalizing are working fine but when I am loading that model in tfjs, the model is giving errors stating SlicingOpLambda and TFOpLambda Layer is undefined. If someone can help with this, please do.

Context: Check https://github.com/tensorflow/tfjs/issues/4853#issuecomment-867946449 23


How to find a good first issue in Tensorflow Models

Question:
Hi,
I am new to open-source contributions. I have used Tensorflow for some of my personal projects and want to contribute and give something back to the community. I am currently in search of some good first issues for me to tackle so I can get started specifically in the models repository. I search some the tags and found good first issue but it has no issues listed in it. Please help me find some good issues I will be greatly thankful.

Context:
Hello Nikhil and welcome to the tensorflow community! There are plenty of requests for contribution, mostly in the space of papers implementation. If that feels too much to start with, there are also some trivial tasks listed in the contribution guide of the models repo:

      GitHub

How to contribute · tensorflow/models Wiki 5

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


TensorFlow Decision Forests with TFX (model serving and evaluation)

Question:
Hi all, I’m trying to incorporate a GBT model using TFDF into an existing TFX project. Thus far, we are able to successfully train a GBT using TFDF inside a trainer component, but we are having issues creating an Evaluator stage for this model.
I believe our issue lies with the serving functions we’re creating for this model. We are following the same technique used previously for normal TF/Keras NNs. Here is the error that occurs during the evaluation stage:
RuntimeError: tensorflow.python.framework.errors_impl.InvalidArgumentError: slice index 0 of dimension 1 out of bounds.
            [[{{node StatefulPartitionedCall/gradient_boosted_trees_model/StatefulPartitionedCall/strided_slice_3}}]] [Op:__inference_signature_wrapper_1194552]

Here is the code for creating the serving functions & signatures:
def _get_serve_tf_examples_fn(model, tf_transform_output, transform=False):
    """Returns a function that parses a serialized tf.Example and applies TFT."""

    model.tft_layer = tf_transform_output.transform_features_layer()

    @tf.function
    def serve_tf_examples_fn(serialized_tf_examples):
        """Returns the output to be used in the serving signature."""
        raw_feature_spec = tf_transform_output.raw_feature_spec().copy()
        feature_spec = { k: v for k, v in raw_feature_spec.items() if k in FEATURES }
        label_spec = raw_feature_spec.get(_LABEL_KEY) or {}

        if transform:
            parsed_features_with_label = tf.io.parse_example(
                serialized_tf_examples, { **feature_spec, **label_spec }
            )

            return model.tft_layer(parsed_features_with_label)

        parsed_features = tf.io.parse_example(serialized_tf_examples, feature_spec)
        transformed_features = model.tft_layer(parsed_features)

        return model(transformed_features)

    return serve_tf_examples_fn

...

# Code for creating signatures & saving model
 _serving_default = _get_serve_tf_examples_fn(model, tf_transform_output) \
          .get_concrete_function(
              tf.TensorSpec(shape=[None], dtype=tf.string, name='examples')
          )

_transform = _get_serve_tf_examples_fn(model, tf_transform_output, transform=True) \
          .get_concrete_function(
              tf.TensorSpec(shape=[None], dtype=tf.string, name='examples')
          )

signatures = { 'serving_default': _serving_default, 'transform': _transform }

model.save(fn_args.serving_model_dir, save_format='tf', signatures=signatures)

Has anyone successfully used TFDF with TFX?

Context:
Couldn’t find a way to edit my original topic, but I have also inspected the saved model.
I ran:
$ saved_model_cli show --dir <path_to_model> --all
There was this error reported at the end of this output:
FileNotFoundError: Op type not registered 'SimpleMLInferenceOpWithHandle' in binary running on craftsman. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.
 If trying to load on a different device from the computational device, consider using setting the `experimental_io_device` option on tf.saved_model.LoadOptions to the io_device such as '/job:localhost'.

Here is the remainder of the output that came prior to the error:

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['__saved_model_init_op']:
  The given SavedModel SignatureDef contains the following input(s):
  The given SavedModel SignatureDef contains the following output(s):
    outputs['__saved_model_init_op'] tensor_info:
        dtype: DT_INVALID
        shape: unknown_rank
        name: NoOp
  Method name is:

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['examples'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: serving_default_examples:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['output_0'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: StatefulPartitionedCall_10:0
  Method name is: tensorflow/serving/predict

signature_def['transform']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['examples'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: transform_examples:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:3
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:7
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:11
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:15
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: StatefulPartitionedCall_11:16
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: StatefulPartitionedCall_11:17
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:21
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:25
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:29
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:33
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:37
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: StatefulPartitionedCall_11:38
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: StatefulPartitionedCall_11:39
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:43
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:47
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:51
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: StatefulPartitionedCall_11:52
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: StatefulPartitionedCall_11:53
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:57
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:61
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:65
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:69
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:73
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: StatefulPartitionedCall_11:77
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: StatefulPartitionedCall_11:81
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: StatefulPartitionedCall_11:82
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: StatefulPartitionedCall_11:83
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:87
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:91
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:95
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:99
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: StatefulPartitionedCall_11:100
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: StatefulPartitionedCall_11:101
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:105
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:109
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:113
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:117
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:121
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1)
        name: StatefulPartitionedCall_11:125
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: StatefulPartitionedCall_11:126
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: StatefulPartitionedCall_11:127
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:131
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:135
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:139
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: StatefulPartitionedCall_11:140
    outputs['<redacted>'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: StatefulPartitionedCall_11:141
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:145
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:149
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:153
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1)
        name: StatefulPartitionedCall_11:157
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1, -1)
        name:
    outputs['<redacted>'] tensor_info:
        dtype: DT_INT64
        shape: (-1)
        name: StatefulPartitionedCall_11:161
  Method name is: tensorflow/serving/predict


Accelerating TensorFlow using Apple M1 Max?

Question: Hello Everyone! I’m planning to buy the M1 Max 32 core gpu MacBook Pro for some Machine Learning (using TensorFlow) like computer vision and some NLP tasks. Is it worth it? Does the TensorFlow use the M1 gpu or the neural engine to accelerate training? I can’t decide what to do? To be transparent I have all Apple devices like the M1 iPad Pro, iPhone 13 Pro, Apple Watch, etc., So I try so hard not to buy other brands with Nvidia gpu for now, because I like the tight integration of Apple eco-system and the M1 Max performance and efficiency. Also, currently I use Google Colab on my 2017 MacBook Air. Desktop is not an option because I travel a lot. Kindly please help me to decide. Thank You All!

Context: In theory, porting should do this carefree (it’s python), but if Apple was that good at using Keras / TensorFlow, if it was a go-to platform, it would know. I have great doubts, the base M1 equivalent to an RTX 2070, with the performance of a GPU, from AMD, I find that it is less desirable at once. If it is an urgent purchase: either, if you can wait, consider an Nvidia desktop and GPU


Error in Hessian calculation using forward over backward propagation

Question:
System information

Have I written custom code (as opposed to using a stock example script provided in TensorFlow):  Yes
TensorFlow installed from (source or binary):  both colab CPU and GPU mode
TensorFlow version (use command below):2.7.0
Python version: Python 3.7.12
GPU model and memory: Tesla P100

Describe the current behavior
I am trying to compute second derivative using the method provide by the tutorial using forward over backward propagation to calculate the hessian-vector-product. For memory reasons, we chose not to use the double backward method. In the code, we have nested loop and tf.dynamic_partition. Both the gradient and hessian works in the eager mode but when I try to decorate the function by @tf.function the error appears, and I found it’s because the combination of using tf.dynamic_partition and for loop gives the error.
Additionally, a different error is given when I try to decorate the hvp function. Without decorator, there’s a TypeError , with a decorator, it gives a SystemError

Briefly describe your candidate solution(if contributing): I have tried to find a workaround to use tf.gather or use a loop to replace the tf.dynamic_partition, different problems also prompt.

Standalone code to reproduce the issue
I have made a reduced dummy code here and also in colab
import tensorflow as tf
import numpy as np
@tf.function # without the decorator, the function works fine in eager mode
def foo(mu):

  partitions = tf.constant([1, 0, 0])
  points = tf.dynamic_partition(mu, partitions, 2)[0]
  block = points
  # a dummy example of a loop
  for j in tf.range(1): # without this loop, the function works fine
    block =  points

  return block
# dummy input data
mu = tf.constant([[3.,2.,1.],[3.,2.,1.],[3.,2.,1.]])
foo(mu)

# gradient calculation
@tf.function
def grad(mu):

    with tf.GradientTape(watch_accessed_variables=False) as t:
        t.watch(mu)
        property = foo(mu)
    loss = t.gradient(property,mu)

    return(loss)
gradient = grad(mu)

# hessian vector product
@tf.function # with/without the decorator, different error prompt
def hvp(mu,tangents):
    with tf.autodiff.ForwardAccumulator(mu, tangents) as acc:
        with tf.GradientTape(watch_accessed_variables=False) as t:
            t.watch(mu)
            property = foo(mu)
            print('tracing')
            tf.print('executing')
        loss = t.gradient(property,mu)
    hess = acc.jvp(loss)
    return(hess)

tangents = np.zeros(mu.shape)
tangents[0]=1
tangents = tf.convert_to_tensor(tangents,dtype=tf.float32)
hess = hvp(mu,tangents)

Other info / logs Include any logs or source code that would be helpful to
diagnose the problem. If including tracebacks, please include the full
traceback. Large logs and files should be attached.
---------------------------------------------------------------------------
StagingError                              Traceback (most recent call last)
<ipython-input-6-f99c3f212345> in <module>()
     14 tangents[0]=1
     15 tangents = tf.convert_to_tensor(tangents,dtype=tf.float32)
---> 16 hess = hvp(mu,tangents)
     17
     18

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1127           except Exception as e:  # pylint:disable=broad-except
   1128             if hasattr(e, "ag_error_metadata"):
-> 1129               raise e.ag_error_metadata.to_exception(e)
   1130             else:
   1131               raise

StagingError: in user code:

    File "<ipython-input-4-f99c3f212345>", line 9, in hvp  *
        loss = t.gradient(property,mu)

    SystemError: PyEval_EvalFrameEx returned a result with an error set

Context: Any help with this would be appreciated!


Question:
So basically I want to achieve the same goal as in this code but in TensorFlow
def get_function(network, loader):
    ''' Collect function (features) from the self.network.module.forward_features() routine '''
    features = []
    for batch_idx, (inputs, targets) in enumerate(loader):
        inputs, targets = inputs.to('cpu'), targets.to('cpu')

        features.append([f.cpu().data.numpy().astype(np.float16) for f in network.forward_features(inputs)])

    return [np.concatenate(list(zip(*features))[i]) for i in range(len(features[0]))]

Are there any clean ways to do this with TensorFlow iterator?

Context:
Reference for future readers.

      stackoverflow.com

  Collecting features from network.foward() in TensorFlow 3

  python, tensorflow, machine-learning, pytorch

  answered by

    M.Innat

  on 04:05PM - 09 Jan 22 UTC


Question:
Hi All,
Just starting out and reading up on using a Raspberry Pi and Libcamera-detect.
I might be asking the wrong question, but is there a list of the objects that can be detected as with reading the following;
“This application runs a preview window and monitors the contents using a Google MobileNet v1 SSD (Single Shot Detector) neural network that has been trained to identify about 80 classes of objects using the Coco dataset. It should recognise people, cars, cats and many other objects.”
https://github.com/raspberrypi/documentation/blob/develop/documentation/asciidoc/accessories/camera/libcamera_detect.adoc 5
The above states that some 80 ojects have been trained to be recognised.
What are they, is there a list somewhere?

Context:
Refer this :-

      github.com

    models/research/object_detection/data at master · tensorflow/models 4

  master/research/object_detection/data

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

This has all the detection model maps  that are pre-trained on the different datasets. Look up the one you want. There you will find all the desired labels.


Question:
I have problems with the tensorflow lite example for image classification for raspberry pi.
It doesn´t want to load the cv2 module.
Does anyone here have experience with Keras, tensorflow on raspberry pi with his own created dataset and can help me with my problem on deploying it on raspberry pi?
Thank you for your help.

Context:
I want to deploy a model on raspberry pi with my own created dataset.
I already failed to do the example with tensorflow lite somehow.
Is there somebody who did a image classification on raspi with tensorflow keras and can tell me what to do?


Question: Hello everyone! A while ago I was working on a speech recognizer, but it’s been a while so I’m gonna start over ;D. I was working on the tensorflow simple audio I was wondering how you could rain it once and save the model instead of every time you reload it it has to train again. Any help? I’m working on ubuntu.

Context:
You can read this

      TensorFlow

Save and load models  |  TensorFlow Core 3


Getting a shape error in the Dense Layer

Question:
Hello Everyone!
I have recently started learning TensorFlow and so am currently a beginner. Currently, I am trying to perform a classification task using a simple neural network. However, I keep on getting a shape mismatch error when trying to fit the model.
My Input input is a 1D array of shape (45325,) and my Output labels are also of the same shape. And I want to classify the data points into one of the 4 classes, hence the last layer is a Dense(4).
I tried searching the problem and found that I basically need to reshape my input tensor to (batch_size, tensor shape). Even after reshaping my tensor to (1, 45325), I am still getting an error ValueError: Shapes (None, 45325) and (1, 4) are incompatible. I have even tried using the tf.keras.Input() as the starting layer but nothing seems to be working
Can someone kindly guide me on how to reshape my tensors so they are in the right shape
shape_erro710×386 12.1 KB

Thanks!

Context:
If the input data contains 45325 samples, each sample having just one feature, you should not reshape it. Just define model input shape as (1,) and pass the input as a numpy array to model.fit()
Inside fit() you can define any batch size that you like or leave the default settings.
The shape mismatch error refers to the fact that your model has 4 output values from the last dense layer but labels represent a 1D array. To avoid this error you can either apply one-hot encoding to the labels array so that it’s shape would be (45325, 4) or pass the labels as they are but in model.compile() specify SparceCategoricalCrossentropy loss.
In shape mismatch error description the first value in each tuple refers to the batch size and the remaining values describe the actual tensor shape - what the model was expecting and what it got.


Issue with weights or weights_path/file in ResNet50

Question:
Hello, this is my very first Tenserflow project, Please help me to solve the issue below the description.
Traceback (most recent call last):
  File "C:\Tensorflow\workspace\training_demo\training2\1.py", line 40, in <module>
    model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))
  File "C:\Users\kamil\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\keras\applications\resnet.py", line 457, in ResNet50
    return ResNet(stack_fn, False, True, 'resnet50', include_top, weights,
  File "C:\Users\kamil\.conda\envs\tensorflow\lib\site-packages\tensorflow\python\keras\applications\resnet.py", line 124, in ResNet
    raise ValueError('The `weights` argument should be either '
ValueError: The `weights` argument should be either `None` (random initialization), `imagenet` (pre-training on ImageNet), or the path to the weights file to be loaded.

(tensorflow) C:\Tensorflow\workspace\training_demo\training2>

Code:
image_size = 224

data_generator_with_aug = ImageDataGenerator(preprocessing_function=preprocess_input,

                                             horizontal_flip=True,

                                             width_shift_range=0.2,

                                             height_shift_range=0.2)

train_path='C:/Tensorflow/workspace/training_demo/training2/training'

train_generator = data_generator_with_aug.flow_from_directory(train_path,

                                                     target_size=(image_size, image_size),

                                                     batch_size=24,

                                                     class_mode='categorical')

data_generator_with_no_aug = ImageDataGenerator(preprocessing_function=preprocess_input)

validation_path='C:/Tensorflow/workspace/training_demo/training2/validation'

validation_generator = data_generator_with_no_aug.flow_from_directory(validation_path,

                                                          target_size=(image_size, image_size),

                                                          batch_size=24,

                                                          class_mode='categorical')

resnet_weights_path = 'C:/Tensorflow/workspace/training_demo/training2resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'#'C:/Tensorflow/workspace/training_demo/training2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'

#resnet_weights_path = 'C:/Users/kamil/.keras/models/resnet50_weights_th_dim_ordering_th_kernels_notop.h5'

model = Sequential()

model.add(ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path))

num_classes = 10

model.add(Dense(num_classes, activation='softmax'))

# Say not to train first layer (ResNet) model. It is already trained

model.layers[0].trainable = False

model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy'])

model.fit_generator(train_generator,

steps_per_epoch=3,

epochs=20,

validation_data=validation_generator,

validation_steps=1)

I use my images and my json file with 3 classes.

Context:
Katerina_Misik:

/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5

It seems as though your ResNet50 instantiation inside model.add is not finding your downloaded weights. Additionally, the weight file you’re loading seems to be from this (link) repository. Could you try (any) of the following?

Try re-downloading the weights (sometimes it can become corrupted)
Sometimes a separate Python process may also be running (for example if you opened it in a Jupyter notebook), you may want to ensure only a single notebook is using it at any point in time
Ensure the file path is correct, can you verify that using file explorer that the file exists where it’s supposed to be