Why doesn’t model.build() set input shape?

Question:
I’ve got a subclassed model which I’m trying to run model.save() on, but I get the following error:

Model <main.ReId object at 0x7fe554658590> cannot be saved because the input shapes have not been set. Usually, input shapes are automatically determined from calling .fit() or .predict(). To manually set the shapes, call model.build(input_shape).

This is despite explicitly calling model.build(input_shape=(256,256,3)) to set the input shape.
I’ve realised that this only happens when I use my custom BatchDataset. When I run model.fit() on a dataset generated by an ImageDataGenerator the model saves normally.
The full code is available at the link below:
https://vehiclereidjupyternotebook.s3.eu-west-2.amazonaws.com/broken_saving_tf.html 11

Context:
Do you have already tried with:

      github.com/tensorflow/tensorflow

      `tf.keras.Model.save` does not support subclassed model when saving model as SavedModel format 153

        opened

        Jul 26, 2019

          closed

        Aug 10, 2019

          zakizhou

        TF 2.0
        comp:keras
        type:support

    Please make sure that this is a bug. As per our [GitHub Policy](https://gith…ub.com/tensorflow/tensorflow/blob/master/ISSUES.md), we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template

**System information**
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow): NA
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Mac
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: None
- TensorFlow installed from (source or binary): binary
- TensorFlow version (use command below): 2.0.0-dev20190724
- Python version: 3.6
- Bazel version (if compiling from source): None
- GCC/Compiler version (if compiling from source): None
- CUDA/cuDNN version: None
- GPU model and memory: None

You can collect some of this information using our environment capture
[script](https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh)
You can also obtain the TensorFlow version with: 1. TF 1.0: `python -c "import
tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"` 2. TF 2.0: `python -c
"import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"`

**Describe the current behavior**
`tf.keras.Model.save`  **DOES NOT** support subclassed model when saving model as SavedModel format
**Describe the expected behavior**
`tf.keras.Model.save` **SHOULD** support subclassed model when saving model as SavedModel format
**Code to reproduce the issue**
```
import tensorflow as tf

class Model(tf.keras.Model):
    def __init__(self):
        super(Model, self).__init__()
        self.d = tf.keras.layers.Dense(2)

    @tf.function
    def call(self, x, training=True, mask=None):
        return self.d(x)

model = Model()
model(tf.random.normal((2, 3)))
# next line raises errors
model.save("save/model", save_format="tf")
```
Provide a reproducible test case that is the bare minimum necessary to generate the problem.

**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.


Question:
history = model.fit_generator(train_generator, epochs=epochs, steps_per_epoch=train_steps, verbose=1, callbacks=[checkpoint], validation_data=val_generator, validation_steps=val_steps)
def create_sequences(tokenizer, max_length, desc_list, photo, vocab_size):
X1, X2, y = list(), list(), list()
for desc in desc_list:
seq = tokenizer.texts_to_sequences([desc])[0]
for i in range(1, len(seq)):
in_seq, out_seq = seq[:i], seq[i]
in_seq = pad_sequences([in_seq], maxlen=max_length)[0]
out_seq = to_categorical([out_seq], num_classes=vocab_size)[0]
X1.append(photo)
X2.append(in_seq)
y.append(out_seq)
return array(X1), array(X2), array(y)
def data_generator(descriptions, photos, tokenizer, max_length, imgsIds, vocab_size):
while 1:
for ind in range(len(imgsIds)):
photo = photos[ind]
key = imgsIds[ind]
desc_list = descriptions[str(key)]
in_img, in_seq, out_word = create_sequences(
tokenizer, max_length, desc_list, photo, vocab_size)
yield [in_img, in_seq], out_word

i got
Failed to convert a NumPy array to a Tensor (Unsupported object type dict).

if there is anything i should add it please comment … Thanks
Traceback (most recent call last):
  File "fit.py", line 271, in <module>
    main(sys.argv)
  File "fit.py", line 268, in main
    fit_model(train, train_descriptions, train_rnn_input, val, val_descriptions, val_rnn_input)
  File "fit.py", line 255, in fit_model
    history = model.fit_generator(train_generator, epochs=epochs, steps_per_epoch=train_steps, verbose=1, callbacks=[checkpoint], validation_data=val_generator, validation_steps=val_steps)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 324, in new_func
    return func(*args, **kwargs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1479, in fit_generator
    initial_epoch=initial_epoch)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 872, in fit
    return_dict=True)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 66, in _method_wrapper
    return method(self, *args, **kwargs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1057, in evaluate
    model=self)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1112, in __init__
    model=model)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 775, in __init__
    peek = _process_tensorlike(peek)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1013, in _process_tensorlike
    inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/util/nest.py", line 617, in map_structure
    structure[0], [func(*x) for x in entries],
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/util/nest.py", line 617, in <listcomp>
    structure[0], [func(*x) for x in entries],
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/keras/engine/data_adapter.py", line 1008, in _convert_numpy_and_scipy
    return ops.convert_to_tensor(x, dtype=dtype)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1341, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 262, in constant
    allow_broadcast=True)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 270, in _constant_impl
    t = convert_to_eager_tensor(value, ctx, dtype)
  File "/path/.local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 96, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type dict).
2021-06-27 04:46:22.936001: W tensorflow/core/kernels/data/generator_dataset_op.cc:103] Error occurred when finalizing GeneratorDataset iterator: Failed precondition: Python interpreter state is not initialized. The process may be terminated.
	 [[{{node PyFunc}}]]

Context: is there any help please ?


Sklearn random forests results seem to be different to tfdf’s

Question:
I used both sklearn’s random forests and tfdf on the same dataset. The results was very different between the two. Below was my configurations for the sklearn one.
RandomForestClassifier(n_estimators=1000, max_depth=16, oob_score=True, min_samples_leaf=1, random_state=42, n_jobs=-1)
I tried to use the same configurations with tfdf’s, but no luck. Please correct me on the configurations if I am wrong.

Context:
Hi,
While both SkLearn and TF-DF implement the classical Random Forest algorithm, there is some few differences in between the implementations. For this reason, it is expected for the results (both the model structure and model quality) not to be exactly the same (but still very close).
Following are some parameter values that should make sklearn’s RandomForestClassifier as close as possible to TF-DF’s Random Forest.
PS: Random Forest and Gradient Boosted Trees are different algorithms.
n_estimators = 300
max_depth = 16
criterion = "entropy"
min_samples_split = 5

In addition, if the problem is regressive, make sure to have:
max_features = 1./3

If your dataset contains categorical or categorical-set features, there are not equivalent parameters for sklearn as it does not support those type of features.
If the differences are larges, it would be very interesting for us to look at it.


Question: Hi, I made a “simple” python script with embedding layer, skip connection… for tabular data, with usual API function for a multi classification target and I have tried to connect tfdf.keras.GradientBoostedTreesModel to one of the last layer (before the softmax) to see if it can improves the result. It works, and when you compare the Neural Network alone and the NN + Gradient BoostedTrees, you get always a better logloss result. But when you don’t import tensorflow_decision_forests as tfdf the NN gets better results than in the first case, even comparing to NN+Gradient. I made several tests and the difference is quite significant and reproducible. Weird ? it seems that tf 2.5 get worse resuts  comparing to 2.4.1 (script without tfdf) ?

Context: @Mathieu might be able to help here


Question:
Hello,
I am trying to get Tensor Flow to apply a vairable noise floor to some X & Y data.  Essentially this:

image260×696 5.28 KB

The problem I am having is that the Boolean Masking function does not return a tensor of the same shape that i fed it.  Moreover, I cannot assign a NULL, ZERO or some default value to the cells that are below the cut off.   Here is the code I have:
def NoiseFloor(x):
NFValue = tf.Variable(1.,dtype=tf.float64,constraint=lambda t: tf.clip_by_value(t, 10, 20))
y = tf.fill(tf.shape(x), NFValue)
y = tf.cast(y, np.float32)
return(tf.math.greater(x, y))
InputLayer = layers.Dense(5, activation=NoiseFloor, name=“Input_layer”)(TensorRFPower)
The issue is that this returns a T,F array that is of a different overall shape.

Context:
So I have been trying to solve this issue with no luck so far.  I switched to trying to gain access to the elements in the tensor, so I could use an IF statement to make the element assignments.  I tried a couple different functions:

tf.tensor_scatter_nd_add(
tf.assign_sub(
tf.assign(

Accessing the elements in the tensor itself (in v2) seems to be limited.  Like I said I can:

Get a Bool Array of values above and below the cutoff
Apply a Bool Mask to that array and then get the values.  But this array is not the same shape.

I also looked at recursive code to step through the elements in this array
def my_elementwise_func(tensorelement):
return tensorelement + 1
def NoiseFloor(x):
print(x)
if tf.shape(x).ndims > 0:
return tf.map_fn(NoiseFloor, x)
else:
return my_elementwise_func(x)
result = NoiseFloor(x)
print(result)

I just do not think their isn’t an existing function that works with Tensorflow that does not already do this.  I am going to look at SciPy if nobody gets back to me.


Unable to pip install TF decision forests

Question:
Hi all,
as many of you I was very excited to learn about TensorFlow Decision Forests, and especially that its API lies within Keras. However, I am unable to install it. My pip install returns the following message:
“ERROR: Could not find a version that satisfies the requirement tensorflow_decision_forests (from versions: none)
ERROR: No matching distribution found for tensorflow_decision_forests”
Would you have any pointers here that could help me? Thanks very much in advance.

Context:
Hi Doug,
Sorry about the disagreement.
Can you share details about your operating system and python version.
Note that TF-DF is currently only available for Linux x64 with Pyhton>=3.6.


Question: What is the different between tf.data.experimental.TFRecordWriter and tf.io.TFRecordWriter?

Context:
E.g. this official tutorial mix both the API. /cc @markdaoust

      TensorFlow

TFRecord and tf.train.Example  |  TensorFlow Core 2


Question:
I new in TF, I tried the classic flowers recognition sample:

      colab.research.google.com

Google Colaboratory 2

I replicate this in PyCharm and it works but I dont’ understand how to test this script passing some real flowers  picture to test this in real world.
Thank in advance

Context: You can follow this tutorial 10 to get an idea. Go through the particular section I linked and if there’s any doubt, let me know.


Gaussian Process example in tensorflow website is giving error?

Question:
The Gaussian process regression example  (Gaussian Process Regression in TensorFlow Probability) is  giving following error
ValueError: No gradients provided for any variable: ['amplitude:0', 'length_scale:0', 'observation_noise_variance_var:0'].
This error goes away when I comment out the tf.function decorator.
I am using tensorflow 2.5.0.
What is the cause of the error?

Context:
Thanks for pointing that out @tonygrey
I filled a bug on that and I’ll keep you posted!


How to plot model graph using keras plot_model function or model summary or anything similar?

Question:
I could not get the plot of model graph of tensorflow object detection models. It doesn’t even showup on tensorboard.
Is there any script to handle this? Where should I look for?

Context:
Are  you training/finetuning the model with  model.fit?
Did you set theTensorboard callback 3?


Question: What is the proper way to reinitialize tf.Dataset with initializable iterator? I tried many ways and always results in memory leak. Should we use gc.collect or tf.reset_graph_default? How do I use tf.reset_graph_default if it always results in error “AssertionError: Do not use tf.reset_default_graph() to clear nested graphs. If you need a cleared graph, exit the nesting and create a new graph”?   I just want to change train dataset and continue training with new data

Context: What is your specific use case? Why do you need to reset the iterator?


Does ImageDataGenerator apply data augmentation to validation data if validation_split is specified?

Question:
Hi,  I was going through the tutorials at tf.keras.preprocessing.image.ImageDataGenerator 2
and Data augmentation  |  TensorFlow Core 1 when I came across this doubt.
If I have a training directory with some images and I used  ImageDataGenerator to augment the data with a validation_split = 0.2,  as shown below.
train_datagen = keras.preprocessing.image.ImageDataGenerator(
rescale=1./255, width_shift_range=0.2,
shear_range=0.2, height_shift_range = 0.2,
zoom_range=0.2, validation_split = 0.2,
horizontal_flip=True)
test_datagen = keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
train_ds = train_datagen.flow_from_directory(
train_dir, seed = 42,
target_size= img_size, subset = ‘training’,
batch_size=32)
valid_ds = train_datagen.flow_from_directory(
train_dir, seed = 42,
target_size= img_size, subset = ‘validation’,
batch_size=32)
test_ds = test_datagen.flow_from_directory(
test_dir, seed = 42,
target_size= img_size,
batch_size=32)
my question is this:
Does the image augmentation applies to the validation_ds by default ?. If so it wouldn’t it create more bias towards the original training data? (as mentioned in Data augmentation  |  TensorFlow Core 1 We should not augment the validation data.)
What if the validation_split argument was provided in the model.fit() method instead? does it mean that the validation split would have applied in the augmented training data?

Context:
It was just closed 4 days ago
Check Split train data into training and validation when using ImageDataGenerator and model.fit_generator · Issue #5862 · keras-team/keras · GitHub 82


The accuracy do not increase when I train model

Question:
indent preformatted text by 4 spaces
import numpy as np
import tensorflow as tf
from PIL import Image
from tensorflow.keras import Model,Sequential
from tensorflow.keras.layers import   Conv2D,MaxPooling2D,Flatten,Dense,Dropout,
from tensorflow.keras.applications.vgg16 import VGG16
from sklearn.model_selection import train_test_split
from keras_preprocessing.image import ImageDataGenerator
from PIL import Image
import os.path
import glob
import random

image_path=glob.glob('gar/*/*.jpg')

label_type=[image_p.split('\\')[1] for image_p in image_path]
labels=np.unique(label_type)

index_to_label=dict((l,n)for l,n in enumerate(labels))
label_to_index=dict((n,l)for l,n in index_to_label.items())

all_labels=[label_to_index.get(name)for name in label_type]

random_index=np.random.permutation(len(image_path))

image_path=np.array(image_path)[random_index]
all_labels=np.array(all_labels)[random_index]

sep=int(len(image_path)*0.7)
train_image_path=image_path[:sep]
train_y=all_labels[:sep]
test_image_path=image_path[sep:]
test_y=all_labels[sep:]

train=tf.data.Dataset.from_tensor_slices((train_image_path,train_y))
test=tf.data.Dataset.from_tensor_slices((test_image_path,test_y))

def load_pic(path,label):
    image=tf.io.read_file(path)
    image=tf.image.decode_jpeg(image,channels=3)
    image=tf.image.resize(image,[256,256])
    image=tf.cast(image,tf.float32)
    image=image/255
    return image,label
train=train.map(load_pic)
train=train.shuffle(1000).batch(64)
test=test.map(load_pic)
test=test.shuffle(1000).batch(64)
def get_model():
    gmodel=Sequential([

        Conv2D(64, (3, 3), activation='relu', input_shape=(256,256,3)),
        Conv2D(64,(3,3),activation='relu'),
        MaxPooling2D((1,1),strides=2,padding='same'),
        Conv2D(128, (3, 3), activation='relu'),
        Conv2D(128, (3, 3), activation='relu'),
        MaxPooling2D((1,1), strides=2, padding='same'),
        Conv2D(256, (3, 3), activation='relu'),
        Conv2D(256, (3, 3), activation='relu'),
        MaxPooling2D((1,1), strides=2, padding='same'),
        Conv2D(512, (3, 3), activation='relu'),
        Conv2D(512, (3, 3), activation='relu'),
        MaxPooling2D((1,1), strides=2, padding='same'),
        Conv2D(512, (3, 3), activation='relu'),
        Conv2D(512, (3, 3), activation='relu'),
        MaxPooling2D((1,1), strides=2, padding='same'),
        Flatten(),
        Dense(256),
        Dense(3, activation='softmax')

    ])

    return gmodel
model=get_model()

model.compile(
    optimizer=tf.keras.optimizers.Adam(0.0001),
    loss="categorical_crossentropy",
    metrics=['accuracy']
)
history=model.fit(train,epochs=5,validation_data=test)

I dont know why when I try to train this model to classify picture into four catagories, the accuracy only about 25%, and I have tried change model, loss function and optimizer but all of these do not not. Can some one tell me how to solve this problems. Thank very much

Context: What is your accuracy over train?


ValueError: Dimension 0 in both shapes must be equal, but are 0 and 512

Question:
Hi, I try to run a Probabilistic face embeddings model. It works well when I run my function with tensorflow 2.1 or tensorflow 1.x but when I tried to run it with tensorflow 2.2 and more I have this error : ValueError: Node ‘gradients/UncertaintyModule/fc_log_sigma_sq/BatchNorm/cond/FusedBatchNorm_1_grad/FusedBatchNormGrad’ has an _output_shapes attribute inconsistent with the GraphDef for output [#3]: Dimension 0 in both shapes must be equal, but are 0 and 512. Shapes are [0] and [512]
It happens when the pretrained model I have is loading when it does saver = tf.compat.v1.train.import_meta_graph(meta_file, clear_devices=True, import_scope=scope) to import the meta file of the PFE_sphere64_msarcface_am model

Capture du 2021-06-28 10-43-59906×361 38.2 KB

To reproduce the error : download the folder Probabilistic-Face-Embeddings_new :  Probabilistic-Face-Embeddings_new - Google Drive 2 and run eval_lfw with parameters --model_dir pretrained/PFE_sphere64_msarcface_am --dataset_path data/Dataset --protocol_path ./proto/pairs_dataset.txt

Capture du 2021-06-28 10-49-401193×470 66.4 KB

Thank you for your help!

Context:
Have you checked:

      github.com/davidsandberg/facenet

      Workaround for tensorflow 2.0 27

        opened

        Nov 25, 2019

          rizkyputranto

    Is there any workaround for tensorflow 2.0?

TensorFlow version compatibility  |  TensorFlow Core


Question: .whl is not supported wheel on this platform

Context: Can you share more info about your environment?


Is it possible to implement tensorflow federated framework in a Raspberry Pi

Question:
Hi, I would like to know if it is possible to use the code available for tensorflow federated research on the stackoverflow dataset for next word prediction on a Raspberry Pi.
Also, there are various online resources which suggest that tensorflow federated implementations are just simulations and cannot be embedded to a real world scenario using IoT devices like Raspberry Pi. Is this information correct?
I am trying to implement an LSTM for next word prediction using stackoverflow dataset but on a Pi to check if its possible to get the results on a resource constrained device like a Pi.
Please Advice.

Context:
Probably the status is still this:

      github.com/tensorflow/federated

      Installation Federated Learning on raspberry pi  10

        opened

        Mar 18, 2019

          closed

        May 15, 2019

          codarm

    Would be needing help with installation of tensorflow_federated on raspberry pi ….

      github.com/tensorflow/federated

      Is it possible to have Federated Learning on Cloud-Edge? 15

        opened

        Oct 15, 2020

          Martiniann

    Hi everyone,

Currently I am working on a school project about federated lear…ning and came across your framework during exploratory analysis. My project should utilize federated learning in this manner - I have an aggregation server (let's say in a cloud). I want this server to provide model to my 2 Raspberry PIs. These two RPIs would then train the model on a local data for x epochs and provide the trained models/gradients back to the global server. On this server, the results would be federated averaged and new model would  be sent to the PIs. Is such a workflow possible with your framework? If so, could you provide me a hint?

Thank you,
Best regards


Dealing with potentially sparse trainable weights

Question:
I’m wondering what’s the Tensorflow way of storing and performing Tensor manipulation (i.e. Tensor multiplication, apply_gradient) of trainable weights in a custom layer that could potentially be sparse based on user specification. I’ve looked into

      TensorFlow

tf.boolean_mask  |  TensorFlow Core v2.5.0

  Apply boolean mask to tensor.

and

      TensorFlow

tf.RaggedTensor  |  TensorFlow Core v2.5.0

  Represents a ragged tensor.

but I’m not sure if it’s possible to apply these as ways to store and update trainable weights.

Context:
Have you already take a look at:

      github.com/tensorflow/tensorflow

      Back-propagating gradients through a sparse tensor? 2

        opened

        Jan 22, 2017

          closed

        Jun 16, 2017

          zergylord

        stat:awaiting tensorflower

    I have a normal feed-forward network that produces a vector v. The elements of v… are then used as the non-zero entries of a sparse matrix M (assume the coordinates are predefined). The sparse matrix is then multiplied by a dense vector and a loss is defined on the resulting scalar. I want to back-propagate the loss w.r.t. the weights of the network, which entails going through the sparse matrix.

This seems like a perfectly reasonable use-case for a sparse matrix, but it appears that such functionality is not supported. Indeed, even calling tf.gradients(M,[v]) produces an error:

> AttributeError: 'SparseTensor' object has no attribute 'value_index'

Am I doing something wrong or am I correct in presuming that this functionality doesn't (yet?) exist? If the latter, then is there a work-around for this particular use-case short of rewriting all of the sparse tensor operations with gradients defined?


Tensorflow operations to perform the following numpy equivalent

Question:
I am modifying identity_3d which is initialized as an n by n by n numpy array per the following operations:

     identity_3d = np.zeros((n, n, n))

        idx = np.arange(n)
        identity_3d[:, idx, idx] = 1
        I,J = np.nonzero(wR==0)
        identity_3d[I,:,J]=0
        identity_3d[I,J,:]=0

If identity_3d was an  Tensor instead, is there a way to perform the equivalent operation?

Context: Do you have a complete numpy running example?


How to prepare data for multi-label classification with Bert?

Question:
If I want to do a multi-label text classification task, not multi-class classification, and my data is in this format:
1 this is a test. 0,0,1,0
2 this is another test 0,1,1,1
3 one more test 1,0,0,1
How should I prepare my data so that Keras preprocessing API can easily create TF.DataSet from it? For single label classification, I can use this format (one file directory per class) as below from the Keras/TF tutorial. But if my task is multi-label classification, how should I go about this and make  tf.keras.preprocessing.text_dataset_from_directory still works with my data?
raw_train_ds = tf.keras.preprocessing.text_dataset_from_directory(
    'aclImdb/train',
    batch_size=batch_size,
    validation_split=0.2,
    subset='training',
    seed=seed)

class_names = raw_train_ds.class_names
train_ds = raw_train_ds.cache().prefetch(buffer_size=AUTOTUNE)

Context:
You cannot use this directly for this kind of multi labels.
See this example, also if It is for images It Is quite the same:

      stackoverflow.com

  How to manually specify class labels in keras flow_from_directory? 49

  python, image-processing, deep-learning, keras

  asked by

    Malte

  on 07:02AM - 29 Mar 17 UTC


How to implement LayerDrop in TensorFlow Transformers

Question:
I need to implement layer drop in TensorFlow Transformer. Can some one guide me how to do that??
Reference paper: https://arxiv.org/pdf/1909.11556.pdf 4
Thanks!!

Context:
You can take a look at the HugginFace impl in different TF models.
E.g. TFBert

      github.com

    huggingface/transformers/blob/master/src/transformers/models/bart/modeling_tf_bart.py#L755:L762 1

          # The tf.debugging asserts are not compliant with XLA then they
          # have to be disabled in other modes than eager.
          if inputs["head_mask"] is not None and tf.executing_eagerly():
              tf.debugging.assert_equal(
                  shape_list(inputs["head_mask"])[0],
                  len(self.layers),
                  message=f"The head_mask should be specified for {len(self.layers)} layers, but it is for {shape_list(inputs['head_mask'])[0]}.",
              )

# encoder layers
          for idx, encoder_layer in enumerate(self.layers):

    if inputs["output_hidden_states"]:
                  encoder_states = encoder_states + (hidden_states,)
              # add LayerDrop (see https://arxiv.org/abs/1909.11556 for description)
              dropout_probability = random.uniform(0, 1)
              if inputs["training"] and (dropout_probability < self.layerdrop):  # skip the layer
                  continue

    hidden_states, attn = encoder_layer(
                  hidden_states,


Question:
Hi!
I am a complete beginner in Tensorflow so, please excuse my noob question.
I am trying to extract snippets of text from a larger text file. Eg. extract Stanford University from Jim is a smart guy. He studied at Stanford University in his 20s. The solution must also work with languages other than English (at least Finnish).
I searched online, but wasn’t able to find any examples that fit my requirements. Could somebody give me an example or help me get started with this? I already have a dataset with which I managed to train a text classification model which worked well. Now I just need to implement it in a way that allows me to extract snippets similar to those in the dataset.
Thanks in advance!

Context:
As I understand, you are trying to implement Named Entity Recognition (NER). Also known as Entity Extraction.
I don’t think there’s a tutorial on tensorflow.org 7 but I found some available from the community.
any insights @markdaoust ?


Tensorflow Object Detection - Improve loading time?

Question:
I have downloaded SSD ResNet50 V1 and converted it to a tflite model since I am going to use it on a Jetson Nano. The loading time is about 226 seconds.
If I do the same operation with MobileNet v2 it takes about 195 seconds to load the model
Of course, for an application to use over 3 minutes to get ready is a little bit high in my opinion. If for example the application crashes and has to be restarted it can already be to late doing predictions as the object might be gone.
Is there anyway I can make the loading time improve?

Context:
Hi @TensorOverflow ,
I got a response from a TensorFlow Advocate:
" Jetson Nano uses TF 4 for inference, not TFLite nor TFLite Micro.
TF has a benchmark tool: GitHub - tensorflow/benchmarks: A benchmark framework for Tensorflow 8
As I’m not familiar with Jetson Nano, I’m not sure if this tool can run on the device but it’s worth a try.
If one wants to do object detection on an IoT device, I’d advise them to use Coral 3. AFAIK, Coral doesn’t have this slow initialization issue."


Question:
I am running a script using GPU and the output has “^H” and “^M” characters interspersed between the Epoch and ETA, loss and error values.
Is this an issue, why is it happening and can I supress it or stop it
Sorry in advance, I am new to the forum and program

Context:
Hi @sportsfanspaceman
If you could put the code snippet and part of the output with the issue it would make it easier to understand the issue and help you.


Question:
I’m in the need of creating a virtual chat bot. The bot should be multilingual and be able to interact with its user with text as well as voice.
Need to incorporate in an android device.
Can this be implemented completely using TensorFlow?
Any idea/suggestions would be very helpful!
Thanks.

Context:
Yes but you need to build up many things around TF to create a chatbot service.
I suggest you take an overview to:
Deconstructing Chatbots - An Overview

So you can figure out how a real E2E chatbot system is working
There are also other videos at:

      Google Developers

Build chatbots with Dialogflow  |  Google Developers 2

  Learn to build chatbots with Dialogflow, and create a great conversational experience for users with BigQuery, Cloud Functions, and Stackdriver.

If you want to experiment a little bit with a specialized library check:

      github.com

deepmipt/DeepPavlov 2

  An open source library for deep learning end-to-end dialog systems and chatbots.


How well is MKL supported by TensorFlow? (now disabled in Java)

Question:
Hi everyone,
At SIG JVM, we have just decided to stop supporting and building native TensorFlow MKL-enabled artifacts for the following reasons:

At pretty much each new release of TensorFlow, the MKL build is broken on various platforms and it requires some gymnastics on our side to get it work again (if we are even able to).

We did not investigated much on the reasons why but performances with MKL were many times worst than without it.

So that being said, if anyone here has some insights to share about the actual status of MKL in TensorFlow and/or any ideas on how we can continue to support it without trouble, that would be greatly appreciated.
Thanks!
Karl

Context:
Hi @karllessard
Back in TensorFlow 1.12.x I did a benchmark via custom build of TensorFlow with mkl and opt flags. To build the Java part I just followed these instructions: tensorflow/README.md at master · tensorflow/tensorflow · GitHub 8
In my benchmark (training NER) the Intel Cascade Lake w/MKL was close and sometimes better than GPU (using the system’s memory it could have a larger batch size).
That’s being said, I’ve never tried testing the inference. But the training was much faster than a native CPU build on newer CPU architectures.


Neural Network Training(back-propagation) in C++

Question:
I’m working on a neural network implementation in C++.
In Python, we built the model sequentially via keras.
I want to learn C++, is there any good example or explanation?
The TensorFlow C++ manual is too hard to explain, so it’s quite difficult for me as a beginner to understand.
From what I understand, in the first python, the form of the neural network is saved as a .pb file.
Second, learn C++. An example from this second part is desperately needed.
Looking forward to advice from anyone who knows or has experience. Thank you in advance.

Context:
It is really not supported/documented for end users.
You could check if this tutorial is still valid:

      Medium – 10 Jun 19

Creating a TensorFlow CNN in C++ (Part 2) 34

  In this post I’ll show how to create, train and test a Convolutional Neural Network using the TensorFlow C++ API

    Reading time: 13 min read

More in general, what is your use case? Why do you need to train the model in c++?


Minimal example for tf.raw_ops.DenseToCSRSparseMatrix

Question:
Would you please give me a simple example to work with tf.raw_ops.DenseToCSRSparseMatrix.  Here is my latest try but It didn’t work:
import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()

ss = tf.constant([[1, 2, 3, 4, 5], [0, 0, 0, 2, 1]], dtype=tf.float32)
indecies = tf.where(tf.not_equal(ss, 0))

b = tf.compat.v1.raw_ops.DenseToCSRSparseMatrix(dense_input=ss, indices=indecies)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(b))

Context:
I think this is deprecated.
Have you already checked:

      TensorFlow

tf.sparse.from_dense  |  TensorFlow Core v2.5.0 1

  Converts a dense tensor into a sparse tensor.

And more in general the sparse tensor guide

      TensorFlow

Working with sparse tensors  |  TensorFlow Core 1


Request for Participation in Competetion

Question:
There is a machine learning challenge on" Machine Learning Based Feature Extraction of Electrical Substations from Satellite Data Using Open-Source Tools". Details are available at
http://ietcint.com/user/electrical_substation_detection 8

Context: Deadline is approaching. Please download the datasets and submit your outputs. Need more info? Please message me


Can graph regularization of tensorflow be also applicable to regression problems?

Question: The official webpage of the Neural Structured Learning 1 has provided three tutorials all of which only focus on classification problems. So, can NSL and its graph regularization be also applicable to regression problems? If so, is there any example representing how that works?

Context:
Yes check:

      github.com/tensorflow/neural-structured-learning

      NSL for Regression 1

        opened

        Apr 8, 2020

          closed

        Apr 11, 2020

          santhoshkolloju

        question

    I was able to integrate adversarial loss to my TensorFlow model, My input data h…as combination of categorical and continuous attributes.
Is there any way to specify not to perturb categorical data while creating adversarial examples?

```
code:
x = tf.placeholder(tf.float32,shape=(None,20))
y = tf.placeholder(tf.float32,shape=(None,1))
#build a sample model
def model(x,y,is_training):
    with tf.variable_scope("regression_model",reuse=tf.AUTO_REUSE) as scope:
        layer0 = tf.layers.Dense(units=64,activation=tf.nn.relu)(x)
        layer1 = tf.layers.Dense(units=128,activation=tf.nn.relu)(layer0)
        output = tf.layers.Dense(units=1)(layer1)
        error = tf.subtract(y,output)
        print(error)
        loss = tf.reduce_mean(error,axis=0)
    return loss
#normal loss mean absolute error
regular_loss = model(x,y,True)
adv_config = nsl.configs.AdvRegConfig()
adv_input,adv_weights = nsl.lib.gen_adv_neighbor(x,regular_loss,config=adv_config.adv_neighbor_config)
adv_loss = model(adv_input,y,True)
overall_loss = regular_loss + 0.2*adv_loss
train_step = optim.minimize(overall_loss)
tf.random.set_random_seed(100)
sess = tf.Session()
init_op = tf.global_variables_initializer()
sess.run(init_op)
X = np.random.random((32,20))#batch of 32
Y = np.random.random((32,1))
sess.run([train_step,adv_loss,regular_loss,overall_loss],feed_dict={x:X,y:Y})
```

      Medium – 15 Apr 20

Neural Structured Learning 1

  Neural structured learning is a framework used for training neural networks with structured signals. This can be applied to NLP, vision or…

    Reading time: 2 min read


Question:
Hello. Trying to learn tensorflow so apologies if I’m missing something obvious. I can’t seem to make predictions on a loaded model.  My original model seems to work just find and I can get a prediction without issues. However, saving and loading that same model doesn’t seem to do what I would think.
Predicting with the original model gives me appropriate values while the loaded model gives me an error:

model.save(“Model”)
s = testFrame.head(1).drop([‘classification’,‘cloudProb’], axis=1).loc[0].to_dict()
loadedModel = tf.saved_model.load(‘Model’)
model(s)
loadedModel(s)

The above code works when passing ‘s’ into model but fails for the loadedModel.

ValueError: Could not find matching function to call loaded from the SavedModel. Got:
Positional arguments (2 total):
* {‘band2’: 346.0, ‘band3’: 625.0, ‘band4’: 443.0, ‘band8’: 2688.0}
* False
Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1:
Positional arguments (2 total):
* {‘band4’: TensorSpec(shape=(None, 1), dtype=tf.float32, name=‘band4’), ‘band8’: TensorSpec(shape=(None, 1), dtype=tf.float32, name=‘band8’), ‘band3’: TensorSpec(shape=(None, 1), dtype=tf.float32, name=‘band3’), ‘band2’: TensorSpec(shape=(None, 1), dtype=tf.float32, name=‘band2’)}
* False
Keyword arguments: {}

… it goes on to give me all the options. What am I missing here? Thanks for the help.

Context: @Mathieu might be able to help


Question:
Hello,
I’m working on training a neural network with data from elasticsearch cluster. I’ve followed this tutorial : Streaming structured data from Elasticsearch using Tensorflow-IO 2
but i’m getting an error.
python3.9 main.py
Type  Age                Breed1  Gender Color1    Color2 MaturitySize FurLength Vaccinated Sterilized   Health  Fee                                        Description  PhotoAmt  AdoptionSpeed
0  Cat    3                 Tabby    Male  Black     White        Small     Short         No         No  Healthy  100  Nibble is a 3+ month old ball of cuteness. He …         1              2
1  Cat    1  Domestic Medium Hair    Male  Black     Brown       Medium    Medium   Not Sure   Not Sure  Healthy    0  I just found it alone yesterday near my apartm…         2              0
2  Dog    1           Mixed Breed    Male  Brown     White       Medium    Medium        Yes         No  Healthy    0  Their pregnant mother was dumped by her irresp…         7              3
3  Dog    4           Mixed Breed  Female  Black     Brown       Medium     Short        Yes         No  Healthy  150  Good guard dog, very alert, active, obedience …         8              2
4  Dog    1           Mixed Breed    Male  Black  No Color       Medium     Short         No         No  Healthy    0  This handsome yet cute boy is up for adoption…         3              2
11537 14
Number of training samples:  8075
Number of testing sample:  3462
/home/cdt/.local/lib/python3.9/site-packages/elasticsearch/connection/base.py:208: ElasticsearchWarning: Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See Set up minimal security for Elasticsearch | Elasticsearch Guide [7.13] | Elastic 2 to enable security.
warnings.warn(message, category=ElasticsearchWarning)
deleting the ‘train’ index.
Response from server: {‘acknowledged’: True}
creating the ‘train’ index.
Response from server: {‘acknowledged’: True, ‘shards_acknowledged’: True, ‘index’: ‘train’}
bulk index the data
/home/cdt/.local/lib/python3.9/site-packages/elasticsearch/connection/base.py:208: ElasticsearchWarning: [types removal] Specifying types in bulk requests is deprecated.
warnings.warn(message, category=ElasticsearchWarning)
Errors: False, Num of records indexed: 8075
deleting the ‘test’ index.
Response from server: {‘acknowledged’: True}
creating the ‘test’ index.
Response from server: {‘acknowledged’: True, ‘shards_acknowledged’: True, ‘index’: ‘test’}
bulk index the data
Errors: False, Num of records indexed: 3462
Connection successful: http://172.16.238.221:9200/_cluster/health
Connection successful: http://172.16.238.221:9200/_cluster/health
{‘Type’: b’Cat’, ‘Age’: 2, ‘Breed1’: b’Domestic Short Hair’, ‘Gender’: b’Male’, ‘Color1’: b’Black’, ‘Color2’: b’White’, ‘MaturitySize’: b’Small’, ‘FurLength’: b’Short’, ‘Vaccinated’: b’No’, ‘Sterilized’: b’No’, ‘Health’: b’Healthy’, ‘Fee’: 0, ‘PhotoAmt’: 1}
Traceback (most recent call last):
File “/home/cdt/stream/elk/main.py”, line 149, in
File “/home/cdt/stream/elk/main.py”, line 108, in get_normalization_layer
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/keras/engine/base_preprocessing_layer.py”, line 242, in adapt
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/eager/def_function.py”, line 889, in call
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/eager/def_function.py”, line 917, in _call
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/eager/function.py”, line 3023, in call
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/eager/function.py”, line 1960, in _call_flat
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/eager/function.py”, line 591, in call
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/eager/execute.py”, line 59, in quick_execute
tensorflow.python.framework.errors_impl.FailedPreconditionError:  Corrupted response from the server null
[[{{node IO>ElasticsearchReadableNext}}]]
[[IteratorGetNext]] [Op:__inference_adapt_step_829]
Function call stack:
adapt_step
Can you help me

Context:
import os
os.environ[‘TF_CPP_MIN_LOG_LEVEL’] = ‘3’
import tensorflow_io as tfio
import tensorflow as tf
from tensorflow.keras import layers
dataset = tfio.experimental.elasticsearch.ElasticsearchIODataset(
**                nodes=[“XXX.XXX.XXX.XXX:9200”],**
**                index=“test”**
**            )**
dataset = dataset.enumerate(start=0)
for element in dataset.as_numpy_iterator():
**    print(element)**
there are 3462 records, and this code works until 3379
(3379, {‘Type’: b’Cat’, ‘Age’: 4, ‘Breed1’: b’Domestic Medium Hair’, ‘Gender’: b’Female’, ‘Color1’: b’Black’, ‘Color2’: b’No Color’, ‘MaturitySize’: b’Medium’, ‘FurLength’: b’Medium’, ‘Vaccinated’: b’No’, ‘Sterilized’: b’No’, ‘Health’: b’Healthy’, ‘Fee’: 0, ‘PhotoAmt’: 2, ‘target’: 1})
Traceback (most recent call last):
File “/home/cdt/temp/elk.py”, line 13, in
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/data/ops/dataset_ops.py”, line 4194, in next
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/data/ops/iterator_ops.py”, line 761, in next
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/data/ops/iterator_ops.py”, line 744, in _next_internal
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/ops/gen_dataset_ops.py”, line 2728, in iterator_get_next
File “/home/cdt/.local/lib/python3.9/site-packages/tensorflow/python/framework/ops.py”, line 6897, in raise_from_not_ok_status
File “”, line 3, in raise_from
tensorflow.python.framework.errors_impl.FailedPreconditionError: Corrupted response from the server null
[[{{node IO>ElasticsearchReadableNext}}]] [Op:IteratorGetNext]


Link logs file from google drive folder to google colab and run Tensorboard for visualization the model

Question:
Hi, I am running a dozen of model with different architecture in google colab and save the logs file to my google drive folder. Now I would like to use TensorBoard to visualize the accuracy and loss of the models, but I can’t call it from my google drive folder
According to sample provided by Tensorflow (Google Colaboratory 2). The code is shown below:
%tensorboard --logdir logs
But I saved the logs in my google drive in case of losing them when colab collapsed or interrupted.
I tried the following code but it is still not working
%tensorboard --logdir content/drive/MyDrive/CNN/logs
%tensorboard --logdir ‘content/drive/MyDrive/CNN/logs’
Hope someone can help me on this issue

Context:
Just to make sure, can you access the logs from a cell, like !lson the dir?
Even with the log’s on drive, if you were disconnected from colab at some point and comeback, you might need to re-mount the drive folder for each to be accessible.


OOM each time reinitialize input pipeline

Question:
Hi everyone, I’m a newbie in tensorflow. Currently I’m using tensorflow 2.3.0, my project need to reinitialize the input pipeline regularly, but each time I reinitialize my input sometimes the memory increase rapidly and OOM. Anyone meet this issue before?
Below is what my input pipeline looks like:
train_holder,label_holder = tf.placeholder(tf.float32, [None, 32, 32, 3],name = “train_holder”), tf.placeholder(tf.int32, [None],name = “label_holder”)
input_tuple = (self.train_holder, self.label_holder)
ds = tf.data.Dataset.from_tensor_slices(data)
map_fn = lambda x, y: (cifar_process(x, is_train), y)
train_dataflow = ds.map(map_fn, num_parallel_calls=tf.data.experimental.AUTOTUNE)
train_ds = train_dataflow.repeat().batch(
self.batch_size, drop_remainder=True
).map(
autoaug_batch_process_map_fn,
num_parallel_calls=tf.data.experimental.AUTOTUNE).prefetch(
buffer_size=tf.data.experimental.AUTOTUNE)
train_input_iterator = (
self.strategy.experimental_distribute_dataset(
train_ds).make_initializable_iterator())
Each time I reinitialize this pipeline with new input by feedict:
self.sess.run([train_input_iterator.initializer],feed_dict = …)
Sometimes I get OOM.

Context:
I may be wrong but there is no  tf.placeholder in tf 2.3.0. Only tf.compat.v1.placeholder.
Are you sure its 2.3.0?


How to modify the average pooling at the end of the model for specific number of classes?

Question:
I am using the resnet model from keras.
I can use
tf.keras.applications.resnet50.ResNet50(include_top=True, weights=None, input_tensor=None,
    input_shape=(224,224,3), classes=2 )

and my ourput will be two classes
predictions (Dense)             (None, 2)            4098        avg_pool[0][0]

is there anyway to not have the Dense layer and just have two class prediction via average pooling?
I thought this code should do the trick:

tf.keras.applications.resnet50.ResNet50(
    include_top=False, weights=None, input_tensor=None,
    input_shape=(224,224,3), pooling='avg' , classes=2 )

but my output is still 2048:
avg_pool (GlobalAveragePooling2 (None, 2048)         0           conv5_block3_out[0][0]

Context:
Hi,
The output you get is the feature vector from resnet50.
You need now to add your classification head with the two classes you want.
To have a complete code, you can see it on this link: Transfer learning and fine-tuning  |  TensorFlow Core 8
the main difference is that on the tutorial, the base model is MobileNetV2 instead of ResNet50, but it’s the same idea


Question:
Somebody please explain me exactly what is going on in this code
model = Sequential()
model.add(Dense(10, input_dim=x.shape[1], activation='relu'))
model.add(Dense(50, input_dim=x.shape[1], activation='relu'))
model.add(Dense(10, input_dim=x.shape[1], activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
model.add(Dense(y.shape[1],activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam')

model.fit(x_train,y_train,validation_data=(x_test,y_test),
          verbose=2,epochs=10)

what is input_dim exactly, what are the numbers 10,50,10,1 and why is y.shape[1] in last layer
I know activation functions but what is kernel_initializer =‘normal’ .
thanks in Advance

Context:
Hi Tornike,
the numbers your are asking are the number of neurons on each of those layers.
for kernel_initializer (docs 3), this defines how the starting random weights of that layer will be initialized. the ‘normal’ might (I don’t know for sure) generate the numbers from a random values from a Normal distribution.
y.shape[1]  is the second dimension size of y, for example:
a = [[1, 2, 3],[1, 2, 3]]
b = tf.constant(a)
b.shape
>>> TensorShape([2, 3])
b.shape[1]
>>> 3

you can find more information here: tf.shape  |  TensorFlow Core v2.5.0 1


When does tensorflow.js fetch model again instead of using cached files?

Question:
Hi,
I checked some tfjs examples, and noticed that the page loads the model.json and weight files for the first time. Later if I refresh the page, the browser uses cached model.json, weight files and so on. So I would love to know if the model is updated, will the browser get the latest model automatically? If so, how does it work or how to make it work?

Context: @Jason might be able to help here


Question:
Hi all,
I apologize if I’ve formatted this wrong or posted it in the wrong area, I’m new to the forum.
I’ve been browsing the tutorials of both tensorflow and tensorflow.js today, and saw that with tensorflow, policies can be written (and in this way, known and extracted). This is documented here → Policies  |  TensorFlow Agents 5. However, I didn’t see this described in tutorials on tensorflow.js. As I want to use tensorflow.js for my dissertation (as it has to be web-based), I was hoping someone could advise me as to whether policies could still be written.
Thanks so much for any advice you can give!

Context:
See Is there a javascript version of this? · Issue #420 · tensorflow/agents · GitHub 4
/cc @Jason if there is any fresh update on that thread


Question: When I am referring to official tensorflow tutorials on https://www.tensorflow.org/text/tutorials 11 I see tf. experimental being used at many places. From the name it is clear that these are experimental APIs and something that should not be used in production code as things might change. However I don’t understand why are they so much widely used in official tutorials? Can we have these tutorials built using more stable APIs? Something people can refer to and use them in their production code?

Context:
Hi @dhavalsays ,
I guess you’re mentioning
tensorflow.keras.layers.experimental

correct?
thanks for the feedback.
Do you have any specific task in mind?


Question: hello to all and am should u are good? Pls i want to start a research work on transfer learning on deep learning based on plant disease detection,  pls, what are the areas i need to research for the work and how can i start using tensor flow thanks

Context:
Hello,
Start with Tensorflow basics, learn how to write neural networks in Tensorflow.
Then learn about computer vision and transfer learning.


Question:
how to change the keypoints in a model.
I wanted to use something like centernet_resnet50_v2_512x512_kpts_coco17_tpu-8
Which does OBJ detection and keypoints.
I have learned how to change the classes, I know how to add the keypoints on the label maps and the pictures themselves.
But how to I add it to the premade TS model above.
I don’t want to reinvent the wheel here just be able to alter for my own needs.
That model does the face and body, I want to do birds.
is there anyone who knows how to change the key points?

Context:
There was already a thread at:

      github.com/tensorflow/models

      Train a CenterNet model using custom keypoints 29

        opened

        Mar 30, 2021

          bileki

        models:research
        type:docs

    # Prerequisites

Please answer the following question for yourself before subm…itting an issue.

- [*] I checked to make sure that this issue has not been filed already.

## 1. The entire URL of the documentation with the issue

https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/configuring_jobs.md

## 2. Describe the issue

I am trying to train a CenterNet model using custom keypoints.
However, I can't find any documentation or example on how to configure the CenterNet pipeline file.

In https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md, the examples are just about a human pose and there is not even an explanation on how to set the **keypoint_estimation_task**, or what the **value** parameter of **keypoint_label_to_std** means or how to calculate it. The same for **keypoint_label_to_sigmas**.

Example of **centernet_mobilenetv2fpn_512x512_coco17_kpts.tar.gz**:

>     keypoint_label_map_path: "PATH_TO_BE_CONFIGURED/label_map.txt"
>     keypoint_estimation_task {
>       task_name: "human_pose"
>       task_loss_weight: 1.0
>       loss {
>         localization_loss {
>           l1_localization_loss {
>           }
>         }
>         classification_loss {
>           penalty_reduced_logistic_focal_loss {
>             alpha: 2.0
>             beta: 4.0
>           }
>         }
>       }
>       keypoint_class_name: "/m/01g317"
>       keypoint_label_to_std {
>         key: "left_ankle"
>         value: 0.89
>       }
>       keypoint_label_to_std {
>         key: "left_ear"
>         value: 0.35
>       }
>       keypoint_label_to_std {
>         key: "left_elbow"
>         value: 0.72
>       }
>       ....
>       keypoint_regression_loss_weight: 0.1
>       keypoint_heatmap_loss_weight: 1.0
>       keypoint_offset_loss_weight: 1.0
>       offset_peak_radius: 3
>       per_keypoint_offset: true
>     }


Question:
Hello, I’m receiving an error while importing TensorFlow. I have AMD Radeon graphics and python 3.8 installed on my pc. Can anyone help me with these? Will these errors affect my work in any way further?
Error:-
W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library ‘cudart64_110.dll’; dlerror: cudart64_110.dll not found
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.

Context:
For AMD GPU check:

    Use AMD GPU for model training General Discussion

    Hello folks,
I have got an AMD GPU on my system and I want to use it for model training. After several efforts I have not been able to do so.
Since CUDA is only supported on NVIDIA GPU, it doesn’t work for me.
Any suggestions?
Thank you.


How to use generators with tfdf.keras.pandas_dataframe_to_tf_dataset

Question:
Hi all,
Novice question here. I have a very large dataset that I want to feed into a tfdf model. How can I couple tfdf.keras.pd_dataframe_to_tf_dataset() to a generator feeding the data to it? Of course, if there are better methods for feeding a generator into  tfdf dataset than using this method I’d be interested to know.
Many thanks in advance,
Doug

Context:
Hi Doug,
tfdf.keras.pd_dataframe_to_tf_dataset is just a convenience method if you already have a dataframe. If you are starting off with a generator, you can use it to create a tf.data.Dataset directly from it tf.data.Dataset  |  TensorFlow Core v2.5.0 3, which might be a better fit – TF-DF datasets are not special, and any dataset object created for another keras model should work.
There are the following caveats:

Your dataset still needs to fit in memory at training time. If this is not the case, you can do something like dataset = dataset.take(10000000) to subsample 10 million rows (the exact number will depend on the number of features + memory capacity)
There are a few data sanitization steps happening in pandas_dataframe_to_tf_dataset like making sure feature names don’t have spaces or other forbidden characters, you might want to consult the source code 3 and copy that logic as is appropriate.

Hope that helps!
Arvind


Convert a tensor string label to one hot encoding

Question:
def to_one_hot(image,label):
return image,tf.one_hot(classes_to_indices[label],depth=14)
train_ds = train_ds.map(to_one_hot)
calsses_to_indices is a simple python dictionary containing   { label_name: indices }
this code is showing an error:-
Tensor is unhashable. Instead, use tensor.ref() as the key.
is there any way to do one_hot encoding while using tf.data API ?

Context:
Can you check:

    One Hot Encoding General Discussion

    Hey Community i hope you’re doing fine, i have data frame with more than 7000 rows and a 4 columns, and the label column with categorical labels and i want to convert them to numerical labels, and i don’t know how to do it!
is there any tensorflow function that can do this?
any help please.


Is it possible to implement CondInst in TensorFlow?

Question:
Hi,
I would like to implement Instance segmentation in TensorFlow 2+ based on CondInst + CenterNet. CondInst is currently implemented only in PyTorch. I saw the implementation in pytorch and I’m not sure if this is possible in  tensorflow  …
The paper is interesting https://arxiv.org/pdf/2003.05664.pdf 3
Has anyone worked on it? Any tips?
Thank you!

Context:
You need to write your CondConv custom layer like this (not personally tested):

      github.com

    prstrive/CondConv-tensorflow/blob/master/network/condconv.py 4

    import tensorflow as tf
from config import WEIGHT_DECAY
from tensorflow.keras import layers

def conv2d(kernel_size, stride, filters, kernel_regularizer=tf.keras.regularizers.l2(WEIGHT_DECAY), padding="same", use_bias=False,
           kernel_initializer="he_normal", **kwargs):
    return layers.Conv2D(kernel_size=kernel_size, strides=stride, filters=filters, kernel_regularizer=kernel_regularizer, padding=padding,
                         use_bias=use_bias, kernel_initializer=kernel_initializer, **kwargs)

class Routing(layers.Layer):
    def __init__(self, out_channels, dropout_rate, temperature=30, **kwargs):
        super(Routing, self).__init__(**kwargs)
        self.avgpool = layers.GlobalAveragePooling2D()
        self.dropout = layers.Dropout(rate=dropout_rate)
        self.fc = layers.Dense(units=out_channels)
        self.softmax = layers.Softmax()
        self.temperature = temperature

  This file has been truncated. show original

Centernet models are available in the Model Gardens research section:

      github.com

tensorflow/models

  master/research/object_detection

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


Question:
Hi, Is it possible to use a custom loss function for the DecisionForest models (any of them), like you would with any other Keras model? If so, is there an example?
Thanks!

Context:
Hi,
Unfortunately, it is not currently possible to define a custom loss in the python code like you would do with classical Keras models.
Instead, custom losses are created by extending the C++ AbstractLoss class 3 and implementing the corresponding methods (e.g. loss value, gradient). This 8 is the list of the currently available losses.
Specific losses can be requested by posting an issue 3 with the “Enhancement” tag.
Cheers,
M.


Issues in deciphering TensorFlowLite Interpreter output

Question:
I have been doing an image classification problem where in the objective is to train a predefined neural network model with set of tfrecords and do inference. This all is happening with reasonable accuracy In Colab.
Subsequent to this I converted the saved_model.pb into model.tflite file. I have checked it against the netron app it is seemingly taking correct inputs (which is an image tensor).
After this I called interpreter.invoke().
Following this when I try to decipher the output tensor I should be able to at least render the output the image, but i am having difficulty in doing this.
This is the link of colab notebook Google Colaboratory 2  where I have maintained the code.
I have other colab notebooks where similar code was done with training for upto 7500 iterations, but i am stuck in all the case at the interpreter level, since i have to port this app on to Android platform

Context:
Hi @Vishal_Virmani,
Welcome to the community.
I see at the end of your colab notebook that you try to print the output of the model which is an array of [1,10,4]. So why are you doing that as this is a classification problem as you mentioned? Or this is an object detection problem?
From the previous cells I see that with the model you do:
input_tensor = tf.convert_to_tensor(
      np.expand_dims(image_np, 0), dtype=tf.float32)
  detections, predictions_dict, shapes = detect_fn(input_tensor)

and then you do:
label_id_offset = 1
  image_np_with_detections = image_np.copy()

  viz_utils.visualize_boxes_and_labels_on_image_array(
        image_np_with_detections,
        detections['detection_boxes'][0].numpy(),
        (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
        detections['detection_scores'][0].numpy(),
        category_index,
        use_normalized_coordinates=True,
        max_boxes_to_draw=200,
        min_score_thresh=.5,
        agnostic_mode=False,
  )

  plt.figure(figsize=(12,16))
  plt.imshow(image_np_with_detections)
  plt.show()

so you use the model output, you post process it and then visualize the boxes on the image.
You can think the output of the Interpreter as the output of the saved-model. So you have to do the same procedure there also eg. preprocess the image, make the predictions with the Interpreter, post process and create the boxes on the image.
If indeed this is an Object detection project check a good example here:

      github.com

tensorflow/examples 1

  master/lite/examples/object_detection/android

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


Convert TFLite Model Maker Object detection model to OpenVino

Question:
I was wondering how I can convert a Tensorflow Lite object detection model created with the TFLite Model Maker. I don’t think this is possible after exporting the model to Tensorflow Lite but it should work if the model is exported as a saved model.
Any help is greatly appreciated.

Context:
Hi @Gi_T
After training the model with create() method you can do:
serving_model = model.create_serving_model()

print(f'Model\'s input shape and type: {serving_model.inputs}')
print(f'Model\'s output shape and type: {serving_model.outputs}')

and then save it to saved model format as always:
saved_model_path = './object_model_maker'
serving_model.save(saved_model_path, include_optimizer=False)

Check a workflow here with an audio classification example (it is still an ongoing project).


Compile( optimizer=‘rmsprop’, loss=None, metrics=None, loss_weights=None, weighted_metrics=None, run_eagerly=None, steps_per_execution=None, **kwargs )

Question: Continuing the discussion from Greetings!:

Context: Hi @1118 , I tried to follow the link you posted but the page is not available anymore. I’d suggest you post the full question so that people might be able to help you


Question:
I am curious about how to set the Device in TF. I want to implement a custom distributed data parallel algorithm, and I want to say , for example, split input tensor x into three parts and transfer it to three devices.
so basically, I want to
x0, x1, x2 = tf.split(x, num_or_size_splits=3, axis=1)
x0 = x0.to('device:0')
x1 = x1.to('device:1')
x2 = x2.to('device:2')

But this seems quite impossible in TF.
I found one is about colocation_graph, should I use that?

Context:
you can do that using the with(device)

      TensorFlow

Use a GPU  |  TensorFlow Core 3

does it help?


Question:
hi guys i have this error for days and i couldn’t resolve it

er1456×742 152 KB

Context:
tf.contrib Is not supported anymore.
Please use:

      TensorFlow

tf.keras.layers.Flatten  |  TensorFlow Core v2.5.0

  Flattens the input. Does not affect the batch size.


How to use batch normalization in Mobile Net for a quantized TensorFlow lite model?

Question:
Hi everyone! I woul like to run a Mobile Net on an embedded system (Coral USB Accelerator) to analyze audio recordings that I convert into spectrograms. I have implemented the model according to the Mobile Net paper 16 in Tensorflow.
def SeparableConv(x, num_filters, strides, alpha=1.0):
     x = tf.keras.layers.DepthwiseConv2D(kernel_size=3, padding='same')(x)
     x = tf.keras.layers.BatchNormalization()(x)
     x = tf.keras.layers.ReLU()(x)
     x = tf.keras.layers.Conv2D(np.floor( num_filters * alpha), kernel_size=(1, 1), strides=strides, use_bias=False, padding='same')(x)
     x = tf.keras.layers.BatchNormalization()(x)
     x = tf.keras.layers.ReLU()(x)
     return x

def Conv(x, num_filters, kernel_size, strides=1, alpha=1.0):
    x = tf.keras.layers.Conv2D((np.floor( num_filters * alpha)), kernel_size=kernel_size, strides=strides, use_bias=False , padding='same')(x)
    x = tf.keras.layers.BatchNormalization()(x)
    x = tf.keras.layers.Activation('relu')(x)
    x = tf.keras.layers.ReLU()(x)
    return x

inputs = tf.keras.layers.Input(shape=(64, 64, 1))

x = Conv(inputs, num_filters=16, kernel_size=3 , strides=2)
x = SeparableConv(x, num_filters=32, strides=1)
x = SeparableConv(x, num_filters=64, strides=2)
x = SeparableConv(x, num_filters=64, strides=1)
x = SeparableConv(x, num_filters=128, strides=2)
x = SeparableConv(x, num_filters=128, strides=1)
x = SeparableConv(x, num_filters=256, strides=1)
x = SeparableConv(x, num_filters=256, strides=2)
x = SeparableConv(x, num_filters=256, strides=1)
x = SeparableConv(x, num_filters=256, strides=1)
x = SeparableConv(x, num_filters=256, strides=1)
x = SeparableConv(x, num_filters=256, strides=1)
x = SeparableConv(x, num_filters=512, strides=2)
x = SeparableConv(x, num_filters=512, strides=1)

x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dense(512)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dropout(0.001)(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(2)(x)

outputs = tf.keras.layers.ReLU()(x)
model = tf.keras.models.Model(inputs, outputs)

To run it on the Coral USB Accelerator I need to quantize it and convert it to a TensorFlow Lite model (and then compile it again with the Edge TPU compiler). But the prediction is massively worse after quantizing from 32bit float to 8bit int. The problem seems to be the batch normalization keras.layers.BatchNormalization() 2. This is not an instruction that is allowed in TensorFlow lite. But since Mobile Net was designed specifically for embedded systems and Batch Normalization is a fundamental part of it I can’t imagine that this is not possible on the Edge TPU. So I wanted to ask if anyone knows a workaround to get Mobile Net, specifically Batch Normalization working on the Coral USB Accelerator?
Many thanks for your help and advice in advance!

Context:
Hi @Olivier_Staehli
Why do you say that the problem is with Batch normalization? Have you tried without it? Have you tried the model without integer quantization to check it?


Question:
Hey everyone!
i’ve majorly worked with PyTorch and lately I’ve found myself working with libraries based on Tensorflow. I wish to ask how can i convert saved TF model to pytorch  models?

Context: Take a look at ONNX.


Question:
Hello folks,
I have got an AMD GPU on my system and I want to use it for model training. After several efforts I have not been able to do so.
Since CUDA is only supported on NVIDIA GPU, it doesn’t work for me.
Any suggestions?
Thank you.

Context:
Rohan_Raj:

I have got an AMD GPU on my system and I want to use it for model training. After several efforts I have not been able to do so.
Since CUDA is only supported on NVIDIA GPU, it doesn’t work for me.
Any suggestions?
Thank you.

CUDA is Nvidia proprietary technology, for AMD GPUs you have OpenCL


Input parameters for tfa.image.sparse_image_warp

Question:
I have the log mel spectrograms of a few audio clips and I am trying to augment the spectrograms using tfa.image.sparse_image_warp so that time warping can be achieved as done in Google’s SpecAugment
But I am confused on how to do achieve time warping as the documentation does not specify how to initialize arguments to sparse_image_warp.
The method declaration is like this:

tfa.image.sparse_image_warp(
image: tfa.types.TensorLike,
source_control_point_locations: tfa.types.TensorLike,
dest_control_point_locations: tfa.types.TensorLike,
interpolation_order: int = 2,
regularization_weight: tfa.types.FloatTensorLike = 0.0,
num_boundary_points: int = 0,
name: str = ‘sparse_image_warp’) → tf.Tensor

Can someone point out how to initialize source_control_point_locations, dest_control_point_locations and num_boundary_points?

Context:
I suppose you need to use the Deepspeech fork of this
See this thread:

      github.com/tensorflow/io

      Audio Processing API and tfio.audio 3

        opened

        Mar 6, 2020

          yongtang

    This is about API design of the audio processing in tfio. Comments and discussio…ns are welcomed.

When we started to get into reading audio for tfio, we start with a feature request from the community of 24bit WAV file   which was not possible in tf core repo. Since then, we also supported `Dataset` types of access that essentially is a sequential read, and random access (AudioIOTensor) which allows python `__getitem__()` and `__len__()` styles access in tensorflow graph.

Later, we added additional audio format support such as `Flac`, `Ogg`, `MP3` and `MP4`, in addition to the already supported `WAV` file. Note `WAV`, `Flac`, `Ogg`, `MP3` does not requires external .so dependency. For `MP4`, we use AVFoundation on macOS, but needs FFmpeg's .so files on Linux. We also added the `resample` as a basic ops (not a python class) recently upon request from community.

Recently, new requests to add supports for decoding memory input audio into tensors comes in play (See PR #815). So the APIs for audio processing in tfio will expand even further.

Initially we have not truly come up with a good audio api design as those features are added over gradually, span across more than a year. Now as the features are gradually in shape, I think it is point to start lay out tfio.audio to revisit APIs so that:
1. Capture the usage from community for different use cases.
2. Allow expansion in the future (our focus was on decoding but encoding will be added soon).

The audio processing in tf repo, is pretty much limited and is really not a good template for us to follow. I also look into pytorch's audio package as well. While pytorch's APIs are typically very clean, the audio related apis are not as clean as some other parts. pytorch also relies on sox which could be an issue (sometimes could be a benefit depending on the scenario.).

We can summarize the use case that has been covered:
1. AudioIODataset which is a subclass of tf.data.Dataset and could be passed to tf.keras.
    Dataset is a sequential access (not random accessed) that allows iteration from the beginning to the end.
   The shape and dtype are probed automatically in eager mode. dtype has to be provided by user in graph mode.
   It is normally file based (or callback-based) and will not load the whole clip into memory (lazy-loaded).
2. AudioIOTensor which exposes `__getitem__`, `__len__`, `shape`, `dtype`, and `rate` API in python and could be used in graph mode (certain restriction apply).
    IOTensor is a random access that allows reading data at any index (`__getitem__`). This is very useful in situations where user want to access just a small clip of the audio. The `shape` API allows user to get the number of channels and samples. `shape = [samples, channels]`.
   The shape and dtype are probed automatically in eager mode. dtype has to be provided by user in graph mode.
    It is file based (or callback-based) and will not load the whole clip into memory (lazy-loaded).
3. `decode`/`decode_mp3`/`decode_mp4`/etc (see discussion in #815) where the API could be used to decode a string (memory) into tensor.
   This is useful in situations where we already load the small clip of audio files into memory and we just want to do a decoding.
   On a separate discussion, we may want to expose `metainfo()` API in audio to return back user the shape, dtype, and rate of the audio clip in kernel ops.
4. `encode`/`encode_mp3`/`encode_mp4`/etc which is the counterpart of 3.
   We haven't add any ops yet though I the need is obvious.
5. `resample` is an audio processing API we recently added. We may want to expand more in the future.

Given the above I think the layout could be:
```
tfio.audio
  - AudioIODataset
  - AudioIOTensor
  - decode
  - decode_mp3
  - decode_mp4
  - decode_wav
  - encode
  - encode_mp3
  - encode_mp4
  - encode_wav
  - resample
```

The list could be easy though details could be challenging. For example in tf:
```
tf.audio.decode_wav(
    contents, desired_channels=-1, desired_samples=-1, name=None
)
```

Do we really want to provide the option of `desired_channels` and `desired_samples`? My understanding is that we already provided AudioIODataset and AudioIOTensor which already allows lazy-loaded partial access (either sequentially or completely random). I don't see we want to manipulate further. Any additional manipulation could be done after a tensor has been returned.

For example, to reduce the stereo to mono is just to drop one dim in tensor (after the tensor is returned from decode_wav).

Another thing is the dtype. In order for a custom op to work in graph mode, Shape is not needed because shape could be pass as unknown dimension. But, at the minimum a dtype has to be provided (unless it is known before hand). For example, even in `decode_wav` case, the above will not work for 24bit wav file. We do have to provide an API with minimal input:
```
tfio.audio.decode_wav(input, dtype, name=None)
```

There might be some other details to sort out. /cc @jjedele @terrytangyuan @BryanCutler, also  cc @faroit @lieff in case you are interested.


Question:
Hello good people.
So, I would want to do predictions using image data of goats. My independent variable was continuous data that I divided into 3 classes to make it categorical: group 1= <30%, group 2= 30% to 70%, and group 3= >70%. However, my worry is that prediction accuracy may be compromised by images from goats with output values close to the border, eg 31%. To address this issue, I would want to use multiple label classification but only for images from goats with percentages within +/- 5% from the border ie 25% to 35% and 65%-75%. I would not want to maintain 3 groups.
My question is that, is this possible. if so, how do I group my training and testing data set, which Multi-label classification method would best fit my problem and how do I go about it

Context: What are the outputs of your model going to look like? From your description, it sounds like you’re going to have one output, and use it’s value between 0 and 1 to determine which group it falls into. Are your classes along a continuous spectrum, or are they distinct and mutually exclusive?


Performing Predictions on TF2.x SavedModel

Question:
Hello,
Is there any method that allows us to feed images to a pre-trained Image Classification CNN (saved_model.pb) and have it perform predictions on those images? Similar to the ‘.predict()’ method that exists for Keras models.
Thanks,
Ahmad

Context:
The predict method still works on TF2.x : Basic classification: Classify images of clothing  |  TensorFlow Core 4
There are some examples here: Image Classification with TensorFlow Hub 5


Question:
Hey Community i hope you’re doing fine, i have data frame with more than 7000 rows and a 4 columns, and the label column with categorical labels and i want to convert them to numerical labels, and i don’t know how to do it!
is there any tensorflow function that can do this?
any help please.

Context:
You’d have a couple of options here. Since you’re working in a dataframe, there’s a function in Pandas called get_dummies 5 that will one-hot encode for you.
There’s also a function in Keras called to_categorical 5, though that seems like it’s limited to taking integer values as your classes. Those integers could be the indexes for a list of class name strings if you want to convert back to meaningful names.
Here are some links to the documentation pages for those functions.
https://pandas.pydata.org/docs/reference/api/pandas.get_dummies.html 5
https://www.tensorflow.org/api_docs/python/tf/keras/utils/to_categorical 5


Is it possible to use a custom input shape with efficient det?

Question:
Hi,
I want to use TFLite Model Maker to train custom object detection models. When I try to change the hparam image_size.
spec = EfficientDetModelSpec(
    model_name="efficientdet-lite0",
    uri="https://tfhub.dev/tensorflow/efficientdet/lite0/feature-vector/1",
    hparams={
        "image_size": "420x420"
    }
)
train_data, validation_data, test_data = object_detector.DataLoader.from_csv("...")
model = object_detector.create( train_data,
                                validation_data=validation_data,
                                model_spec=spec,
                                epochs=1,
                                batch_size=4,
                                train_whole_model=True)

The following error occurs:

  Error
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-4ed9b6a821a2> in <module>
----> 1 model = object_detector.create( train_data,
    2                                 validation_data=validation_data,
    3                                 model_spec=spec,
    4                                 epochs=1,
    5                                 batch_size=4,

c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow_examples\lite\model_maker\core\task\object_detector.py in create(cls, train_data, model_spec, validation_data, epochs, batch_size, train_whole_model, do_train)
  285     if do_train:
  286       tf.compat.v1.logging.info('Retraining the models...')
--> 287       object_detector.train(train_data, validation_data, epochs, batch_size)
  288     else:
  289       object_detector.create_model()

c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow_examples\lite\model_maker\core\task\object_detector.py in train(self, train_data, validation_data, epochs, batch_size)
  154       validation_ds, validation_steps, val_json_file = self._get_dataset_and_steps(
  155           validation_data, batch_size, is_training=False)
--> 156       return self.model_spec.train(self.model, train_ds, steps_per_epoch,
  157                                    validation_ds, validation_steps, epochs,
  158                                    batch_size, val_json_file)

c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow_examples\lite\model_maker\core\task\model_spec\object_detector_spec.py in train(self, model, train_dataset, steps_per_epoch, val_dataset, validation_steps, epochs, batch_size, val_json_file)
  262             val_json_file=val_json_file,
  263             batch_size=batch_size))
--> 264     train.setup_model(model, config)
  265     train.init_experimental(config)
  266     model.fit(

c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow_examples\lite\model_maker\third_party\efficientdet\keras\train.py in setup_model(model, config)
  111 def setup_model(model, config):
  112   """Build and compile model."""
--> 113   model.build((None, *config.image_size, 3))
  114   model.compile(
  115       steps_per_execution=config.steps_per_execution,

c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\engine\training.py in build(self, input_shape)
  417                            'method accepts an `inputs` argument.')
  418         try:
--> 419           self.call(x, **kwargs)
  420         except (errors.InvalidArgumentError, TypeError):
  421           raise ValueError('You cannot build your model by calling `build` '

c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow_examples\lite\model_maker\third_party\efficientdet\keras\train_lib.py in call(self, inputs, training)
  883
  884   def call(self, inputs, training):
--> 885     cls_outputs, box_outputs = self.base_model(inputs, training=training)
  886     for i in range(self.config.max_level - self.config.min_level + 1):
  887       cls_outputs[i] = self.classes(cls_outputs[i])

c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in __call__(self, *args, **kwargs)
 1010         with autocast_variable.enable_auto_cast_variables(
 1011             self._compute_dtype_object):
-> 1012           outputs = call_fn(inputs, *args, **kwargs)
 1013
 1014         if self._activity_regularizer:

c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\autograph\impl\api.py in wrapper(*args, **kwargs)
  668       except Exception as e:  # pylint:disable=broad-except
  669         if hasattr(e, 'ag_error_metadata'):
--> 670           raise e.ag_error_metadata.to_exception(e)
  671         else:
  672           raise

ValueError: in user code:

  c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow_hub\keras_layer.py:243 call  *
      result = smart_cond.smart_cond(training,
  c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\saved_model\load.py:668 _call_attribute  **
      return instance.__call__(*args, **kwargs)
  c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\eager\def_function.py:828 __call__
      result = self._call(*args, **kwds)
  c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\eager\def_function.py:871 _call
      self._initialize(args, kwds, add_initializers_to=initializers)
  c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\eager\def_function.py:725 _initialize
      self._stateful_fn._get_concrete_function_internal_garbage_collected(  # pylint: disable=protected-access
  c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\eager\function.py:2969 _get_concrete_function_internal_garbage_collected
      graph_function, _ = self._maybe_define_function(args, kwargs)
  c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\eager\function.py:3361 _maybe_define_function
      graph_function = self._create_graph_function(args, kwargs)
  c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\eager\function.py:3196 _create_graph_function
      func_graph_module.func_graph_from_py_func(
  c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\framework\func_graph.py:990 func_graph_from_py_func
      func_outputs = python_func(*func_args, **func_kwargs)
  c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\eager\def_function.py:634 wrapped_fn
      out = weak_wrapped_fn().__wrapped__(*args, **kwds)
  c:\users\felix\appdata\local\programs\python\python38\lib\site-packages\tensorflow\python\saved_model\function_deserialization.py:267 restored_function_body
      raise ValueError(

  ValueError: Could not find matching function to call loaded from the SavedModel. Got:
    Positional arguments (2 total):
      * Tensor("inputs:0", shape=(None, 420, 420, 3), dtype=float32)
      * False
    Keyword arguments: {}

  Expected these arguments to match one of the following 4 option(s):

  Option 1:
    Positional arguments (2 total):
      * TensorSpec(shape=(None, 320, 320, 3), dtype=tf.float32, name='inputs')
      * True
    Keyword arguments: {}

  Option 2:
    Positional arguments (2 total):
      * TensorSpec(shape=(None, 320, 320, 3), dtype=tf.float32, name='input_1')
      * False
    Keyword arguments: {}

  Option 3:
    Positional arguments (2 total):
      * TensorSpec(shape=(None, 320, 320, 3), dtype=tf.float32, name='input_1')
      * True
    Keyword arguments: {}

  Option 4:
    Positional arguments (2 total):
      * TensorSpec(shape=(None, 320, 320, 3), dtype=tf.float32, name='inputs')
      * False
    Keyword arguments: {}

Does this mean, that custom input shape is not supported?

Context:
felithium:

spec = EfficientDetModelSpec(
    model_name="efficientdet-lite0",
    uri="https://tfhub.dev/tensorflow/efficientdet/lite0/feature-vector/1",
    hparams={
        "image_size": "420x420"
    }
)

Hi @felithium
Please where did you find this code snippet? Place here the link to check it.
I read the documentation here 7 and I cannot find EfficientDetModelSpec…and the dictionary for the hparams


Question:
Do you think that Tensorflow could plan to introduce in 2021 Rust in the codebase like in other Google projects (Chromium, Android, Linux Kernel etc.) or do you think that we will have some specific issues for our library?
E.g. For Rust/C++ interop in Chromium see:

      chromium.org

Rust and C++ interoperability - The Chromium Projects 6

  Home of the Chromium Open Source Project

/cc @mihaimaruseac

Context: See also GitHub - google/autocxx: Tool for safe ergonomic Rust/C++ interop driven from existing C++ headers 3


How to build Universal Sentence Embedding for custom corpus?

Question:
Wish to use Universal Sentence Embeddings from Tf hub, retrain with own corpus for a classification task.
So layers are:

UnivSentEmb pretrained from tf hub with trainable ON
Dense 300, relu
Dense num_classes, softmax

Once the model is trained on own corpus, during the prediction time it behaves as a classifier.
predicted_class = model.predict(test_sentence)
Here, wish to query middle layer values, which is a 300 long embedding vector, for the test_sentence.
Possible? How? Is this approach correct? Can we say that this 300 long vector of that test_sentence is its USE embedding?

Context:
Hi @Yogesh_Kulkarni
one way of doing what you want is to have your custom model have 2 outputs: the dense layer with num_classes and the dense layer with the 300 embeddings
for that you might need to use the Functional API: The Functional API  |  TensorFlow Core 3


Error when trying to import tensorflow_probability twice

Question:
Hi!
Every time I run a code more than once with tensorflow_probability imported I get the following error:
import tensorflow_probability
Traceback (most recent call last):
File “”, line 1, in
import tensorflow_probability
File “/home/slevy/Desktop/probability/tensorflow_probability/init.py”, line 23, in
from tensorflow_probability.python import *  # pylint: disable=wildcard-import
File “/home/slevy/Desktop/probability/tensorflow_probability/python/init.py”, line 142, in
dir(globals()[pkg_name])  # Forces loading the package from its lazy loader.
File “/home/slevy/Desktop/probability/tensorflow_probability/python/internal/lazy_loader.py”, line 61, in dir
module = self._load()
File “/home/slevy/Desktop/probability/tensorflow_probability/python/internal/lazy_loader.py”, line 44, in _load
module = importlib.import_module(self.name)
File “/home/slevy/anaconda3/lib/python3.7/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “/home/slevy/Desktop/probability/tensorflow_probability/python/experimental/init.py”, line 35, in
from tensorflow_probability.python.experimental import bijectors
File “/home/slevy/Desktop/probability/tensorflow_probability/python/experimental/bijectors/init.py”, line 19, in
from tensorflow_probability.python.experimental.bijectors.distribution_bijectors import make_distribution_bijector
File “/home/slevy/Desktop/probability/tensorflow_probability/python/experimental/bijectors/distribution_bijectors.py”, line 25, in
from tensorflow_probability.python.distributions import deterministic
File “/home/slevy/Desktop/probability/tensorflow_probability/python/distributions/init.py”, line 23, in
from tensorflow_probability.python.distributions.autoregressive import Autoregressive
File “/home/slevy/Desktop/probability/tensorflow_probability/python/distributions/autoregressive.py”, line 30, in
from tensorflow_probability.python.util.seed_stream import SeedStream
File “/home/slevy/Desktop/probability/tensorflow_probability/python/util/init.py”, line 23, in
from tensorflow_probability.python.util.deferred_tensor import DeferredTensor
File “/home/slevy/Desktop/probability/tensorflow_probability/python/util/deferred_tensor.py”, line 738, in
class _DeferredTensorSpec(_DeferredTensorSpecBase, type_spec.BatchableTypeSpec):
File “/home/slevy/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/type_spec.py”, line 713, in decorator_fn
_NAME_TO_TYPE_SPEC[name].name))
ValueError: Name tfp.util.DeferredTensorSpec has already been registered for class tensorflow_probability.python.util.deferred_tensor._DeferredTensorSpec.
I couldn’t find any discussions on this error anywhere,  what can I do to fix it? It’s really annoying to restart the kernel each time to run my code.
Thanks in advance!

Context: Hi, can you provide a bit more context? If you can provide a link to a colab that reproduces this problem we’ll have an easier time helping out.


RuntimeException: Unable to start activity ComponentInfo(tensorflow.lite.examples)

Question:
#help_request #tflite
I am working on this project: TensorFlow Lite image classification Android example application
I run this project on my own phone (xiaomi redmi note 8) on Android Studio (not emulator)
When I run this project on Android Studio, TFL Classify applications gives the following error: application keep stopping.
Then Android Studio gives this error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.tensorflow.lite.examples.classification, PID: 7599
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.tensorflow.lite.examples.classification/org.tensorflow.lite.examples.classification.ClassifierActivity}: java.lang.IllegalArgumentException: No enum constant org.tensorflow.lite.examples.classification.tflite.Classifier.Model.QUANTİZED_EFFİCİENTNET
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
Caused by: java.lang.IllegalArgumentException: No enum constant org.tensorflow.lite.examples.classification.tflite.Classifier.Model.QUANTİZED_EFFİCİENTNET
at java.lang.Enum.valueOf(Enum.java:258)
at org.tensorflow.lite.examples.classification.tflite.Classifier$Model.valueOf(Classifier.java:47)
at org.tensorflow.lite.examples.classification.CameraActivity.onCreate(CameraActivity.java:195)
at android.app.Activity.performCreate(Activity.java:7232)
at android.app.Activity.performCreate(Activity.java:7221)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2964)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3119)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1839)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6864)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

I updated SDK version 28 to 29.0.2 but it didn’t work

Context: @Ugur_Ercelik Please give us a link of the project that it has a problem to take a look.


Export/Build tf2 ssd-resnet model from ckpt.data to tflite

Question:
Hello,
i recently trained a ssd_resnet model for some custom classification.
In general everything worked out as i was able to run inference on the data which was produced by the training process.
What i want to do is to convert the model to a tflite model and compile it for a coral TPU.
What does not work atm: I cannot find any information on how to convert my model into a tflite model.
The training process was done using the script model_main_tf2.py from the object detection API.
This produces files as follows:

checkpoint
ckpt-XXX.data-00000-of-00001
ckpt-XXX.index
(which is as far as i can tell everything)

I am aware that there are a lot of sites talking about converting modesl to tflite but none that work from the kind of checkpoints i have. (it always refers to model.ckpt files with some kind of meta file given which i guess is a tf1 format?)
What i tried:

using the export scripts provided by the opject detection API (export_tflide_ssd_graph.py, export_tflite_graph_tf2.py) - both did not work as it says: “your model is not built”.

loading and building the model and then manually exporting it:
# load model
configs = config_util.get_configs_from_pipeline_file(PATH_TO_CFG)
model_config = configs['model']
detection_model = model_builder.build(model_config=model_config, is_training=False)

# Restore checkpoint
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt.restore(os.path.join(PATH_TO_CKPT, 'ckpt-63')).expect_partial()
detection_model.build((614,514))
converter = tf.lite.TFLiteConverter.from_keras_model(detection_model)
tflite_model = converter.convert()```

which results in the same problem telling me that the input is not set and that i could solve this by “building” the model. (which i thought i did…?)
So if anyone can shed some light on how this could work or where i can find any documentation on how to convert these checkpoints to a .tflite model i’d be really grateful. Let me know if i forgot to provide any information.
Cheers.

Context:
so i was finally able to figure this out. Posting my own answer in a hope that it might help someone else:
the model gained from the learning process apparently is not a standard tensorflow or keras model and first must be exported using the script called “exporter_main_v2.py”.
This is relatively selfexplainatory as it takes the arguments --trained_checkpoint_dir, --pipeline_config_path and --output_directory
when this step is done a folder is created at the output location which should be called “saved_model”
The following python script can be used to transform this into a TF-Lite model:
MODEL_PATH  = "your/abs/path/to/folder/saved_model"
MODEL_SAVE_PATH = "your/target/save/location"
converter = tf.lite.TFLiteConverter.from_saved_model(MODEL_PATH ,signature_keys=['serving_default'])
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.experimental_new_converter = True
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]

tflite_model = converter.convert()
with tf.io.gfile.GFile(MODEL_SAVE_PATH, 'wb') as f:
  f.write(tflite_model)

Hope this helps someone.


Tutorials/materials for fast image retrieval tasks

Question:
Hi all.
Looking for resources/materials that build on TensorFlow/Keras (preferably) for doing scalable image similarity searches.

Context:
With Keras you can take a look at:

Investigating the Vision Transformer Model for Image Retrieval Tasks 16
*DELF tutorial 10

More in general I suggest you an overview:

      arXiv.org

Deep Image Retrieval: A Survey 7

  In recent years a vast amount of visual content has been generated and shared
from various fields, such as social media platforms, medical images, and
robotics. This abundance of content creation and sharing has introduced new
challenges. In...


Model Zoo models to TensorRT not possible?

Question:
I am trying to use a model from Tensorflow Model Zoo on a Jetson Nano computer. First I convert the model (.pb -file) to ONNX-format.
But when I try to convert it to TensorRT format after the ONNX-step) I only get an error saying:
[8] Assertion failed: convertDtype(onnxDtype.elem_type(), &trtDtype)
I then run a script in order to turn the data type to float32 since uint8 is not supported by TensorRT. I then try to convert the model to TensorRT format once again, but now I get the error:
    [8] Assertion failed: cond.is_weights() && cond.weights().count() == 1 && "If condition must be a initializer!"
And at this point I am stuck. It seems to me that Jetson/Nvidia does not actually support object detection using models from the Tensorflow Model Zoo?
At least I get the impression that most of the layers that the models in the Model Zoo are built on is not supported by TensorRT or ONNX, which therefor creates all sort of problems?
Note that I am using the tf.experimental.tensorrt.Converter but the actual tensorrt library provided by Nvidia in order to convert the model.
Thanks for any help!

Context: I meant that I am NOT using the tf.experimental.tensorrt.Converter in the post above.


Question:
I was looking at the sample in: Action Recognition with an Inflated 3D CNN  |  TensorFlow Hub 5
and works just fine but how to run this against a life feed? is it possible? and how to train my own model if required?

Context:
For streaming I suggest you to take a look at the streaming models in:
https://tfhub.dev/google/collections/movinet/ 8
You can finetune these on your data or train from scratch.