No gradients provided for any variable though get_grads is defined

Question:
I try to create implement a papaer I read, on which the loss has some kind of probablistic dependence on a variable in the network. Therefore I need an access to hidden variables. Therefore I use low level API for loss. Here is my model:
class probablistic_model(tf.keras.Model):
    def call(self,inputs):
        return self.auto_encoder(inputs),self.z

    # get gradients
    def get_grad(self, X, Y):
        return self.auto_encoder.get_grad(X,Y)
        with tf.GradientTape() as tape:

            L = self.get_loss(X, Y)
            g = tape.gradient(L,tf.ones(self.input.shape))
            #might be incorrect
        return g
    def get_loss(self,X, Y):
        with tf.GradientTape() as tape:
            z = self.z
            X=X[0]
            diff = (X - Y)
            diff = tf.norm(diff, ord=2, axis=1)
            diff *= 2

            diff *= z
            score = diff - λ * tf.norm(diff, ord=1)
        return score

    def __init__(self,dimension,sigma):
        super().__init__()
        self.z=probablistic_feature(dimension,sigma)
        self.auto_encoder=keras_auto_encoder(dimension[0], 30)
        self.λ=2e-1

but when I try to run the model
import csv
import sys
dataset=[]
tf.config.run_functions_eagerly(True)

dataset=np.loadtxt(sys.argv[1],delimiter=",")[1:-1,:]
model=probablistic_model(dataset.shape[::-1],1)
model.compile()
model.fit(x=dataset,y=dataset)

I get: ValueError: No gradients provided for any variable: [‘dense/kernel:0’, ‘dense/bias:0’, ‘dense_1/kernel:0’, ‘dense_1/bias:0’].
Though get_grads is defined (though not correctly). Why tensorflow ignores get_grads?

Context:
Because get_grads is not a standard API. Why did you think that would work?
If you want to implement the training step yourself see this doc:

      TensorFlow

Customize what happens in Model.fit  |  TensorFlow Core 16


Question:
In recent days, I toured TensorFlow Ecosystem and made an article out of it. There were so many great updates from I/O that make me feel I should update it, but either way, this is a great and full ecosystem.
Will love to know if you have an idea of what’s missing there
https://jeande.tech/tf-ecosystem

Context: Hi Jean, your blog seems to have some issue with the certificate now.


Question: I built a custom op. One thing I am not sure is how to load it from a c++ program. In python you can use tf.load_op_library, but it does not seem to have a correspondent function in c++.

Context:
You can try TF_LoadLibrary 1 from C API, Env::LoadDynamicLibrary 4 from C++ API.
See an example in loading filesystem plugins 4


Question:
Hi: I’m not new to javascript but virtually clueless about NLP.
I’m trying come up with something, web based that can determine the
topic
sub-topic if it exists and the
sentiment
of a word or phrase from a sample of text, usually a few words but occasionally as much as a few paragraphs.
The sample text is answers to survey questions. I can combine all the answers into 1 large sample if that helps.
If I have a sample of text I need to find all the topics in the sample based on words, phrases or sentences. From there I need to know the words, quantity per topic and the sentiment per topic.
Can I do this?
I’d really prefer it in a server based web technology, PHP, but I can’t us Python because my site is on a shared server running PHP/MySql with hostgator and they will not allow me to install anything that is python.
So, it looks like I’m stuck with javascript.
Not that I don’t like javascript but this looks job intensive and I’d like to be able to do it without making users wait or worry about users closing the browser before the job is completed.
I’ve looked through some of the docs and NLP can do all kinds of things.
It looks like I need a pre-trained model as well as tfjs.
Also, I’m looking at the version: v3.11.0
Is that what I need?
I could head in a lot of different directions.
Where do I start?

Context:
There is example on Sentiment analysis using Tensorflow JS.
[TensorFlow.js Example: Sentiment Analysis](https://TensorFlow.js Example: Sentiment Analysis)
Alternate option.
Convert any text classification model into Tensorflow.js compatible using Tensorflowjs converter.

tensorflowjs_converter --input_format keras \
                                            path/to/my_model.h5 \
                                            path/to/tfjs_target_dir

Follow the steps mentioned Importing a Keras model into TensorFlow.js


Distribution parameter as a function of a trainable variable

Question:
I am currently trying to fit a hidden markov model with a time-varying transition distribution using the Adam optimizer. In order to specify the transition distribution as a function of some covariates that depend on time, I am trying to describe its logits in terms of a trainable variable. However, I’m not sure how to train the variable that the distribution’s parameters depend on instead of training the distribution’s parameters directly as I’ve done in the past. Here is the code I have so far:
# Set the number of states and covariates for the model
num_states = 2
num_covariates = 1

# Randomly initialize the initial state distribution
initial_logits = tf.Variable(rng.random([num_states]), name='initial_logits', dtype=tf.float32)
initial_distribution = tfd.Categorical(logits=initial_logits)

# Randomly initialize the regression coefficients.
# This is the variable I want to train that my transition distribution's logits depend on
regression_coeffs = tf.Variable(
    rng.random([num_covariates + 1, num_states, num_states]) * (1 - np.diag([1] * num_states)),
    name='regression_coeffs',
    dtype=tf.float32)

# Randomly generate some covariates
covariates = rng.random([num_observations - 1, num_covariates])

# Compute the transition logits for each time step
# I think this is the part that I need to rewrite in order to allow the regression_coeffs variable to be trained
transition_logits = []
for i in range(num_observations - 1):
    transition_logits.append(
        regression_coeffs[0] +
        tf.reduce_sum(
            regression_coeffs[1:] * covariates[i].reshape(num_covariates, 1, 1),
            axis=0))

# Package up all of the logits for each time step into a tensor and create the transition distribution from them
transition_logits = tf.Variable(np.array(transition_logits), name='transition_logits', dtype=tf.float32, trainable=False)
transition_distribution = tfd.Categorical(logits=transition_logits)

# I have some other stuff to define my observation distribution...

# Defining my hidden markov model
hmm = tfd.HiddenMarkovModel(
    initial_distribution = initial_distribution,
    transition_distribution = transition_distribution,
    observation_distribution = joint_dists,
    num_steps = num_observations,
    time_varying_transition_distribution = True
)

# Define a loss function
def compute_loss():
    return -tf.reduce_logsumexp(hmm.log_prob(observations))

# Define an optimizer to perform back propagation
optimizer = tf.keras.optimizers.Adam(learning_rate=0.1)
criterion = tfp.optimizer.convergence_criteria.LossNotDecreasing(rtol=0.01)

def trace_fn(traceable_quantities):
    return traceable_quantities.loss

# And finally, training the HMM
loss_history = tfp.math.minimize(
    loss_fn=compute_loss,
    num_steps=1000,
    optimizer=optimizer,
    convergence_criterion=criterion,
    trace_fn=trace_fn)

Please let me know if I should clarify anything or include more of my code. Any help would be greatly appreciated!

Context:
Ok, I figured out a solution. I changed my loss function to recalculate my transition logits and recreate the hmm on each training step. My new loss function looks like this:
def compute_loss():
    hmm = tfd.HiddenMarkovModel(
        initial_distribution = initial_distribution,
        transition_distribution = tfd.Categorical(logits=get_transition_logits()),
        observation_distribution = joint_dists,
        num_steps = num_observations,
        time_varying_transition_distribution = True
    )
    return -tf.reduce_logsumexp(hmm.log_prob(observations))


Question:
I trained a yolov3 model for a specific dataset and converted it to tflite, the tflite model weighs 200 MB, how do other object detection apps create light tflite models? Is it about the network size? here is the model:
keras-yolo3/model.py at master · roboflow-ai/keras-yolo3 · GitHub 3,

Context:
TF Lite includes a Model Optimization Toolkit for helping you trade off size, latency, accuracy, etc. There’s also this model optimization guide 1.
Typically you do things like quantization (e.g. converting 32-bit floats into something with less precision), model pruning (e.g. trimming the parts unused for inference) and clustering (e.g. reducing the number of unique weights by sharing when approximately equal). The toolkit and guide can help you with all of these.


How to perform pruning on trained object detection model?

Question: Hi I have trained object detection model using tensorflow 1.14 object detection API, my model is performing well. However, I want to reduce/optimize parameters of model to make it lighter. How can I use pruning on trained model?

Context: Fine-tune pre-trained model with pruning 2


To print dataset after each layer? possible

Question:
I have following model defined (below) and using fit to train. But during training, I would like to print out the dataset or at least its shape/dimensions for troubleshooting purpose. Is it possble?
model=keras.models.Sequential([\
#    keras.layers.Conv2D(64, 7, activation="relu", padding="same", input_shape=[28, 28, 1]),\
    keras.layers.Conv2D(64, 7, activation="relu", padding="same", input_shape=[28, 28, 1]),\
    keras.layers.MaxPooling2D(2), \
    keras.layers.Conv2D(128, 3, activation="relu", padding="same"), \
    keras.layers.Conv2D(128, 3, activation="relu", padding="same"), \
    keras.layers.MaxPooling2D(2), \
    keras.layers.Conv2D(256, 3, activation="relu", padding="same"), \
    keras.layers.Conv2D(256, 3, activation="relu", padding="same"), \
    keras.layers.MaxPooling2D(2), \
    keras.layers.Flatten(), \
    keras.layers.Dense(128, activation="relu"), \
    keras.layers.Dropout(0.5), \
    keras.layers.Dense(64, activation="relu"), \
    keras.layers.Dropout(0.5), \
    keras.layers.Dense(10, activation="softmax") \
])
model.compile(loss="sparse_categorical_crossentropy", optimizer="sgd", metrics=["accuracy"])
history=model.fit(X_train, y_train, epochs=CONFIG_EPOCHS, batch_size=CONFIG_BATCH_SIZE, validation_data=(X_valid, y_valid))

and I am doigng exactly same thing in pytorch and this is how pytorch conveniently allows printing of x between layers:
class MLP(Module):

prepare the dataset
# define model elements

def init(self):
super(MLP, self).init()
self.conv1 = Conv2d(1, 64, 7, padding=“same”)
self.act1 = ReLU()
self.maxpool1 = MaxPool2d(2)
…
…
def forward(self, X):
if DEBUG:
print("forward entered: X: ", X.size())
printdbg("X: " + str(X.size()))

X = self.conv1(X)
X = self.act1(X)
printdbg("X, conv1/act1: " + str(X.size()))
X = self.maxpool1(X)
printdbg("X, maxpool1: " + str(X.size()))

X = self.conv2a(X)
X = self.act2a(X)
printdbg("X, conv2a/act2a: " + str(X.size()))
X = self.conv2b(X)
X = self.act2b(X)

def printdbg(msg):
if DEBUG_PRT:
print(msg)

Context:
You can visualize the whole graph with the shapes of each layer:
tf.keras.utils.plot_model(model, show_shapes=True, show_dtype=True)


Question: Greetings everyone, I hope everyone is well in these difficult times.  Now I have a question, I searched a lot to solve it but I am confused .  I have a real-time object detection project, which libraries and platforms would you recommend I use for this?  thank you so much

Context:
Hi Tarik,
There are a couple of options but to help you better, what platform are you deploying this?
Are the model going to run on-device? on the browser? on a server over API?


Question:
I find Tensorflow decision forest has nearly 2x slower training time than alternative libraries when I call .fit from within a Colab notebook. It also seems to hang for a long time after the Keras progress bar reaches 100%.
In this 11 example I get the following results on 250k rows, and 200 features:
tfdf 5min 49s
xgboost (tree_method = ‘approx’) 3min 11s
lightgbm 46.1 s
xgboost (tree_method = ‘histogram’) 30.4 s
(1) Does anyone here have an understanding of why tfdf is significantly slower than xgboost on the same dataset, with similar parameters, and any tips on steps that can be taken to debug or speed up? I’m posting the Colab example because it’s easiest to share but I get similar magnitudes of differences internally, on larger datasets.
(2) Is there any plan to offer histogram-based-binning options for TFDF? It does seem to dramatically reduce training time which would be very helpful to potential users with large datasets.

Context: @Mathieu might know this one


Question:
hi all!
i’m about to train my first object recognition for a jetson nano project. i looked at a lot of howtos/helps/etc. but currently i’m stuck while creating the TFRecords.
Whatever i try, i get
ModuleNotFoundError: No module named ‘dataset_util’
at “import dataset_util”
what is wrong? what do i need to install to get this module?
thanks in advance

Context:
After many many more tries i found the problem.
I had to expand my PYTHONPATH to my “object_detection/utils/” folder.
(I’ve read that, but initial tests didn’t work for me)


Tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)

Question:
tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
Cuda - 11.4
TF - 2.6.2
Docker Container Runtime
Logs and python File below
Python File

import argparse
import io
import os
import subprocess
import ray
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
#import tensorflow as tf
from PIL import Image
from psutil import cpu_count
tf.disable_v2_behavior()
from utils import *
from object_detection.utils import dataset_util, label_map_util
label_map = label_map_util.load_labelmap(’./label_map.pbtxt’)
label_map_dict = label_map_util.get_label_map_dict(label_map)
t2idict = {y:x for x,y in label_map_dict.items()}
def class_text_to_int(text):
return t2idict[text]
def create_tf_example(filename, encoded_jpeg, annotations):
“”"
This function create a tf.train.Example from the Waymo frame.
args:
    - filename [str]: name of the image
    - encoded_jpeg [bytes]: jpeg encoded image
    - annotations [protobuf object]: bboxes and classes

returns:
    - tf_example [tf.Train.Example]: tf example in the objection detection api format.
"""

# TODO: Implement function to convert the data
encoded_jpg_io = io.BytesIO(encoded_jpeg)
image = Image.open(encoded_jpg_io)
width, height = image.size

image_format = b'jpeg'

xmins = []
xmaxs = []
ymins = []
ymaxs = []
classes_text = []
classes = []

for index, row in enumerate(annotations):

    xmin = row.box.center_x - row.box.length/2.0
    xmax = row.box.center_x + row.box.length/2.0
    ymin = row.box.center_y - row.box.width/2.0
    ymax = row.box.center_y + row.box.width/2.0

    xmins.append(xmin / width)
    xmaxs.append(xmax / width)
    ymins.append(ymin / height)
    ymaxs.append(ymax / height)
    classes_text.append(class_text_to_int(row.type).encode('utf8'))
    classes.append(row.type)

filename = filename.encode('utf8')
tf_example = tf.train.Example(features=tf.train.Features(feature={
    'image/height': int64_feature(height),
    'image/width': int64_feature(width),
    'image/filename': bytes_feature(filename),
    'image/source_id': bytes_feature(filename),
    'image/encoded': bytes_feature(encoded_jpeg),
    'image/format': bytes_feature(image_format),
    'image/object/bbox/xmin': float_list_feature(xmins),
    'image/object/bbox/xmax': float_list_feature(xmaxs),
    'image/object/bbox/ymin': float_list_feature(ymins),
    'image/object/bbox/ymax': float_list_feature(ymaxs),
    'image/object/class/text': bytes_list_feature(classes_text),
    'image/object/class/label': int64_list_feature(classes),
}))
return tf_example

def download_tfr(filepath, temp_dir):
“”"
download a single tf record
args:
    - filepath [str]: path to the tf record file
    - temp_dir [str]: path to the directory where the raw data will be saved

returns:
    - local_path [str]: path where the file is saved
"""
# create data dir
dest = os.path.join(temp_dir, 'raw')
os.makedirs(dest, exist_ok=True)
filename = os.path.basename(filepath)
local_path = os.path.join(dest, filename)
if os.path.exists(local_path):
    return local_path
print("start downloading {}".format(local_path))
# download the tf record file
#cmd = ['gsutil', 'cp', filepath, f'{dest}']
#logger.info(f'Downloading {filepath}')
#res = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
#if res.returncode != 0:
#    logger.error(f'Could not download file {filepath}')

#print("complete downloading {}".format(local_path))
return local_path

def process_tfr(filepath, data_dir):
“”"
process a Waymo tf record into a tf api tf record
args:
    - filepath [str]: path to the Waymo tf record file
    - data_dir [str]: path to the destination directory
"""
# create processed data dir
dest = os.path.join(data_dir, 'processed')
os.makedirs(dest, exist_ok=True)
file_name = os.path.basename(filepath)

if os.path.exists(f'{dest}/{file_name}'):
    return

logger.info(f'Processing {filepath}')
writer = tf.python_io.TFRecordWriter(f'{dest}/{file_name}')
dataset = tf.data.TFRecordDataset(filepath, compression_type='')
for idx, data in enumerate(dataset):
    frame = open_dataset.Frame()
    frame.ParseFromString(bytearray(data.numpy()))
    encoded_jpeg, annotations = parse_frame(frame)
    filename = file_name.replace('.tfrecord', f'_{idx}.tfrecord')
    tf_example = create_tf_example(filename, encoded_jpeg, annotations)
    writer.write(tf_example.SerializeToString())
writer.close()
return

@ray.remote
def download_and_process(filename, temp_dir, data_dir):
# need to re-import the logger because of multiprocesing
dest = os.path.join(data_dir, ‘processed’)
os.makedirs(dest, exist_ok=True)
file_name = os.path.basename(filename)
if os.path.exists(f'{dest}/{file_name}'):
    print("processed file  {} exists, skip".format(file_name))
    return
logger = get_module_logger(__name__)
local_path = download_tfr(filename, temp_dir)
#local_path = "/app/project/training_0000"
process_tfr(local_path, data_dir)
# remove the original tf record to save space
#if os.path.exists(local_path):
#    logger.info(f'Deleting {local_path}')
#    os.remove(local_path)

if name == “main”:
parser = argparse.ArgumentParser(description=‘Download and process tf files’)
parser.add_argument(’–data_dir’, required=False, default="./data",
help=‘processed data directory’)
parser.add_argument(’–temp_dir’, required=False, default="/app/project/training_0000",
help=‘raw data directory’)
args = parser.parse_args()
logger = get_module_logger(name)
# open the filenames file
with open(‘filenames1.txt’, ‘r’) as f:
filenames = f.read().splitlines()
logger.info(f’Download {len(filenames)} files. Be patient, this will take a long time.’)
data_dir = args.data_dir
temp_dir = args.temp_dir

download_and_process(filenames[0], temp_dir, data_dir)
# init ray
ray.init(num_cpus=cpu_count())

workers = [download_and_process.remote(fn, temp_dir, data_dir) for fn in filenames[:100]]
_ = ray.get(workers)
print("Done with downloading")

Logs

2021-11-05 05:52:19,569 WARNING services.py:1559 – WARNING: The object store is using /tmp instead of /dev/shm because /dev/shm has only 67108864 bytes available. This may slow down performance! You may be able to free up space by deleting files in /dev/shm or terminating any running plasma_store_server processes. If you are inside a Docker container, you may need to pass an argument with the flag ‘–shm-size’ to ‘docker run’.
2021-11-05 05:52:19,584 WARNING services.py:1559 – WARNING: The object store is using /tmp instead of /dev/shm because /dev/shm has only 67108864 bytes available. This may slow down performance! You may be able to free up space by deleting files in /dev/shm or terminating any running plasma_store_server processes. If you are inside a Docker container, you may need to pass an argument with the flag ‘–shm-size’ to ‘docker run’.
(pid=8218) 2021-11-05 05:52:25,785 INFO     Processing /app/project/training_0000/raw/segment-10444454289801298640_4360_000_4380_000_with_camera_labels.tfrecord
(pid=8218) 2021-11-05 05:52:25.788420: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8214) 2021-11-05 05:52:25,822 INFO     Processing /app/project/training_0000/raw/segment-10212406498497081993_5300_000_5320_000_with_camera_labels.tfrecord
(pid=8214) 2021-11-05 05:52:25.823758: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8281) 2021-11-05 05:52:25,833 INFO     Processing /app/project/training_0000/raw/segment-10023947602400723454_1120_000_1140_000_with_camera_labels.tfrecord
(pid=8281) 2021-11-05 05:52:25.835462: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8278) 2021-11-05 05:52:25,833 INFO     Processing /app/project/training_0000/raw/segment-10107710434105775874_760_000_780_000_with_camera_labels.tfrecord
(pid=8278) 2021-11-05 05:52:25.836066: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8208) 2021-11-05 05:52:25,914 INFO     Processing /app/project/training_0000/raw/segment-10226164909075980558_180_000_200_000_with_camera_labels.tfrecord
(pid=8208) 2021-11-05 05:52:25.916432: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8240) 2021-11-05 05:52:25,943 INFO     Processing /app/project/training_0000/raw/segment-10082223140073588526_6140_000_6160_000_with_camera_labels.tfrecord
(pid=8240) 2021-11-05 05:52:25.945284: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8259) 2021-11-05 05:52:25,965 INFO     Processing /app/project/training_0000/raw/segment-1005081002024129653_5313_150_5333_150_with_camera_labels.tfrecord
(pid=8259) 2021-11-05 05:52:25.968303: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8253) 2021-11-05 05:52:25,973 INFO     Processing /app/project/training_0000/raw/segment-10061305430875486848_1080_000_1100_000_with_camera_labels.tfrecord
(pid=8253) 2021-11-05 05:52:25.976335: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8260) 2021-11-05 05:52:25,980 INFO     Processing /app/project/training_0000/raw/segment-10017090168044687777_6380_000_6400_000_with_camera_labels.tfrecord
(pid=8260) 2021-11-05 05:52:25.983356: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8251) 2021-11-05 05:52:25,996 INFO     Processing /app/project/training_0000/raw/segment-10075870402459732738_1060_000_1080_000_with_camera_labels.tfrecord
(pid=8251) 2021-11-05 05:52:25.998741: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8248) 2021-11-05 05:52:26,006 INFO     Processing /app/project/training_0000/raw/segment-10072140764565668044_4060_000_4080_000_with_camera_labels.tfrecord
(pid=8248) 2021-11-05 05:52:26.010197: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8244) 2021-11-05 05:52:26,013 INFO     Processing /app/project/training_0000/raw/segment-10094743350625019937_3420_000_3440_000_with_camera_labels.tfrecord
(pid=8244) 2021-11-05 05:52:26.015093: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8205) 2021-11-05 05:52:26,026 INFO     Processing /app/project/training_0000/raw/segment-10231929575853664160_1160_000_1180_000_with_camera_labels.tfrecord
(pid=8205) 2021-11-05 05:52:26.029786: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8201) 2021-11-05 05:52:26,032 INFO     Processing /app/project/training_0000/raw/segment-10327752107000040525_1120_000_1140_000_with_camera_labels.tfrecord
(pid=8201) 2021-11-05 05:52:26.034096: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8195) 2021-11-05 05:52:26,101 INFO     Processing /app/project/training_0000/raw/segment-10455472356147194054_1560_000_1580_000_with_camera_labels.tfrecord
(pid=8195) 2021-11-05 05:52:26.104592: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8230) 2021-11-05 05:52:26,447 INFO     Processing /app/project/training_0000/raw/segment-10275144660749673822_5755_561_5775_561_with_camera_labels.tfrecord
(pid=8230) 2021-11-05 05:52:26.450848: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8241) 2021-11-05 05:52:26,477 INFO     Processing /app/project/training_0000/raw/segment-10206293520369375008_2796_800_2816_800_with_camera_labels.tfrecord
(pid=8241) 2021-11-05 05:52:26.483426: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8216) 2021-11-05 05:52:26,786 INFO     Processing /app/project/training_0000/raw/segment-10391312872392849784_4099_400_4119_400_with_camera_labels.tfrecord
(pid=8216) 2021-11-05 05:52:26.791352: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8198) 2021-11-05 05:52:26,908 INFO     Processing /app/project/training_0000/raw/segment-10235335145367115211_5420_000_5440_000_with_camera_labels.tfrecord
(pid=8198) 2021-11-05 05:52:26.912763: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8274) 2021-11-05 05:52:26,922 INFO     Processing /app/project/training_0000/raw/segment-1022527355599519580_4866_960_4886_960_with_camera_labels.tfrecord
(pid=8274) 2021-11-05 05:52:26.926734: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8189) 2021-11-05 05:52:27,069 INFO     Processing /app/project/training_0000/raw/segment-10241508783381919015_2889_360_2909_360_with_camera_labels.tfrecord
(pid=8189) 2021-11-05 05:52:27.072531: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8282) 2021-11-05 05:52:27,160 INFO     Processing /app/project/training_0000/raw/segment-10096619443888687526_2820_000_2840_000_with_camera_labels.tfrecord
(pid=8282) 2021-11-05 05:52:27.164348: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8263) 2021-11-05 05:52:27,190 INFO     Processing /app/project/training_0000/raw/segment-10072231702153043603_5725_000_5745_000_with_camera_labels.tfrecord
(pid=8263) 2021-11-05 05:52:27.194641: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
(pid=8238) 2021-11-05 05:52:27,203 INFO     Processing /app/project/training_0000/raw/segment-10153695247769592104_787_000_807_000_with_camera_labels.tfrecord
(pid=8238) 2021-11-05 05:52:27.207482: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: UNKNOWN ERROR (34)
Done with downloading

Context:
I don’t think it is currently supported, please upvote and subcribe to:

      github.com/tensorflow/tensorflow

      Upgrade TF to CUDA 11.4 and cuDNN 8.2

        opened

        Aug 25, 2021

          pkanwar23

        type:build/install

    Upgrade TF to CUDA 11.4 and cuDNN 8.2


(bounty offered) error when taking gradient of numerical integration results

Question:
I am having problems when taking the gradient of numerical integration results. I’d like to hire someone familiar with the TensorFlow Probability codebase to help me troubleshoot the issue.
(Edit: It looks like some user(s) flagged this post as violating the community guidelines 1, but I don’t see anything that prohibits posting want ads? If this is a bug with TensorFlow and not user error, paying someone to fix it and submit a pull request is one way of contributing to the open source project.)
See posted job on Upwork here:
https://www.upwork.com/jobs/~0107ba22d28b7bc8b9 4
See question on Stack Overflow here:

      stackoverflow.com

  (cash bounty offered) avoiding matrix inversion error when taking gradient of TensorFlow numerical integration results 1

  tensorflow, numerical-integration, tensorflow-probability, automatic-differentiation

  asked by

    Jack Elsey

  on 08:06PM - 04 Nov 21 UTC

Context: Hi Jack! I think we generally want to avoid commercial activity on the forum, but this is a bit of a gray area – I’d put “Help Needed” in your topic title here.


Question:
How to fix random seed to calculate gradient (gradient tape)?
[Environment]
OS: Windows 10
Tensorflow 2.6.0
[Issue]
I show GradCAM image like this sample code.

      keras.io

Keras documentation: Grad-CAM class activation visualization 1

It uses GradientTape to get gradient, and the result is different each time in spite of same source image and logit values of inference.
So heatmap image is different in each time with this issue.
I fixed random seed like below,  but I couldn’t fix random seed for gradient tape.

import os
import random
os.environ[‘TF_DETERMINISTIC_OPS’] = ‘1’
SEED = 42
random.seed(SEED)
np.random.seed(SEED)
tf.random.set_seed(SEED)
os.environ[“PYTHONHASHSEED”] = str(SEED)

Calc gradient and Get heatmap like below:

def make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None):
with tf.GradientTape() as tape:
last_conv_layer_output, preds = grad_model(img_array)#this preds’ value is same in every time with fix random seed
if pred_index is None:
pred_index = tf.argmax(preds[0])
class_channel = preds[:, pred_index]
# This is the gradient of the output neuron (top predicted or chosen)
# with regard to the output feature map of the last conv layer
grads = tape.gradient(class_channel, last_conv_layer_output)

Context:
Bhack,
I solved this problem!
The cause is I set “input_tensor” when I make a model.
I can get a deterministic heatmap not to set input_tensor!


Question:
i have used the tensorflow book example, but concatenated version of NN fron two different input is output NaN. There is second simpler similar code in which single input is separated and concatenated back which works. I was wondering what is wrong with 2 input code below such that it is outputting NaN???
Here is the code that is output NaN from the output layer (As a debugging effort, I put second code much simpler far below that works.
In brief, here the training layers flow goes like from the code below:
inputA->  → (to concat layer)
inputB->hidden1->hidden2-> (to concat layer) →
concat → output
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow import keras
housing = fetch_california_housing()
X_train_full, X_test, y_train_full, y_test = train_test_split(housing.data, housing.target)
X_train, X_valid, y_train, y_valid = train_test_split(X_train_full, y_train_full)
print("X_train/y_train/full/test shapes: ", X_train_full.shape, X_test.shape, y_train_full.shape, y_test.shape)
print("X_/y_/train/valid: ", X_train.shape, X_valid.shape, y_train.shape, y_valid.shape)
X_train_A, X_train_B = X_train[:, :5], X_train[:, 2:]
X_valid_A, X_valid_B = X_valid[:, :5], X_valid[:, 2:]
X_test_A, X_test_B = X_test[:, :5], X_test[:, 2:]
X_new_A, X_new_B = X_test_A[:3], X_test[:3, 2:]
print("X_train_A/B/valid_A/B: ", X_train_A.shape, X_train_B.shape, X_valid_A.shape, X_valid_B.shape)
print("X_test_A/B/new_A/B: ", X_test_A.shape, X_test_B.shape, X_new_A.shape, X_new_B.shape)
scaler=StandardScaler()
X_train = scaler.fit_transform(X_train)
X_valid = scaler.transform(X_valid)
X_test = scaler.transform(X_test)
input_A = keras.layers.Input(shape=[5] , name=“wide_input”)
input_B = keras.layers.Input(shape=[6] , name=“deep_input”)
hidden1 = keras.layers.Dense(30, activation=“relu”)(input_B)
hidden2 = keras.layers.Dense(30, activation=“relu”)(hidden1)
concat = keras.layers.Concatenate()([input_A, hidden2])
output = keras.layers.Dense(1, name=“output”)(concat)
model = keras.Model(inputs=[input_A, input_B], outputs=[output])
model.compile(loss=“mse”, optimizer=keras.optimizers.SGD(lr=1e-3))
history = model.fit(
(X_train_A, X_train_B), y_train,
epochs=20,
validation_data=((X_valid_A, X_valid_B),y_valid)
)
print("training result (shape): ", history)
mse_test = model.evaluate((X_test_A, X_test_B), y_test)
y_pred = model.predict((
X_new_A,
X_new_B))
model.save(“p310.h5”)
output:
11610/11610 [==============================] - 1s 58us/sample - loss: nan - val_loss: nan
Epoch 2/20
11610/11610 [==============================] - 0s 35us/sample - loss: nan - val_loss: nan
Epoch 3/20
11610/11610 [==============================] - 0s 37us/sample - loss: nan - val_loss: nan
Epoch 4/20
Working code:
NN layers:
input_ → hidden1->hidden2-> (to concat layer)
input_ → (to concat layer)
concat ->output
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from tensorflow import keras
housing = fetch_california_housing()
X_train_full, X_test, y_train_full, y_test = train_test_split(housing.data, housing.target)
X_train, X_valid, y_train, y_valid = train_test_split(X_train_full, y_train_full)
print(“training data shapes:”)
print("X_train_full/test/t_train_full/test: ", X_train_full.shape, X_test.shape, y_train_full.shape, y_test.shape)
print("X_train/X_valid/y_train/y_valid: ", X_train.shape, X_valid.shape, y_train.shape, y_valid.shape)
scaler=StandardScaler()
X_train = scaler.fit_transform(X_train)
X_valid = scaler.transform(X_valid)
X_test = scaler.transform(X_test)
input_ = keras.layers.Input(shape=X_train.shape[1:])
hidden1 = keras.layers.Dense(30, activation=“relu”)(input_)
hidden2 = keras.layers.Dense(30, activation=“relu”)(hidden1)
concat = keras.layers.Concatenate()([input_, hidden2])
output = keras.layers.Dense(1)(concat)
model = keras.Model(inputs=[input_], outputs=[output])
model.compile(loss=“mse”, optimizer=keras.optimizers.SGD(lr=1e-3))
history = model.fit(X_train, y_train, epochs=20, validation_data=(X_valid, y_valid))
print("training result (shape): ", history)
mse_test = model.evaluate(X_test, y_test)
X_new = X_test[:3] # prertend these are new instances.
y_preid = model.predict(X_new)
output:
11610/11610 [==============================] - 1s 54us/sample - loss: 1.8919 - val_loss: 0.8798
Epoch 2/20
11610/11610 [==============================] - 0s 32us/sample - loss: 0.8452 - val_loss: 0.7558
Epoch 3/20
11610/11610 [==============================] - 0s 34us/sample - loss: 0.7188 - val_loss: 0.6991
Epoch 4/20
11610/11610 [==============================] - 0s 32us/sample - loss: 0.6705 - val_loss: 0.6597
Epoch 5/20

Context:
Hi! The problem is not in the concatenation layer but in how you normalize the input data and how you pass it to the model. You transform X_train but pass X_train_A and X_train_B into the model, which were never transformed by the scaler and contain negative values.
It’s better to use TensorFlow native normalization utilities rather than scalers from other frameworks. This way you will not forget to transform the inputs and will be able to save normalization parameters in a single file as a part of the model.
The second issue is that in a multi-input model values should be represented by a dataset containing dictionary with keys identical to input layer names.
You data preprocessing should look like:
import tensorflow as tf

def process_data(x1, x2, y):
    return {'wide_input': x1, 'deep_input': x2}, y

train_ds = tf.data.Dataset.from_tensor_slices((X_train_A, X_train_B, y_train)).map(process_data).batch(64)

And the same goes for the validation and the test set.
And the model with normalization included should be initialized like this:
normalizer1 = tf.keras.layers.experimental.preprocessing.Normalization(axis=None)
normalizer1.adapt(X_train_A)
normalizer2 = tf.keras.layers.experimental.preprocessing.Normalization(axis=None)
normalizer2.adapt(X_train_B)

input_A = keras.layers.Input(shape=[5] , name='wide_input')
input_B = keras.layers.Input(shape=[6] , name='deep_input')
norm1 = normalizer1(input_A)
norm2 = normalizer2(input_B)
hidden1 = keras.layers.Dense(30, activation='relu')(norm2)
hidden2 = keras.layers.Dense(30, activation='relu')(hidden1)
concat = keras.layers.Concatenate()([norm1, hidden2])
output = keras.layers.Dense(1, name='output')(concat)
model = keras.Model(inputs=[input_A, input_B], outputs=[output])

When you call adapt() on the normalization layer, it learns the scale of the train subset of the data. The model will automatically normalize any new data when asked to predict or evaluate.
And history object does not have shape. It contains a dictionary. You can plot the training progress with pd.DataFrame(history.history).plot()


Question:
Hi everyone!
I’m relatively new to tensorflow. I’m trying to use the tfdf.keras.RandomForestModel (regression). I just reproduced the example for the regression from here
https://tensorflow.google.cn/decision_forests/tutorials/beginner_colab#training_a_regression_model
I was playing with the num_candidate_attributes and I guess I was expecting that during the evaluation of the test dataset the MSE and RMSE dataset would be sensitive to that parameter, but they stay exactly the same no matter what value I give it. I saw some publications that used random forest in R where the choice of mtry (which I’m assuming is the equivalent to num_candidate_attributes?) had an impact on the error metric chosen to evaluate the test dataset. I’m just wondering whether I need to change more attributes to actually change that value or whether I’d just expect it doesn’t change anything (and if so - why?)
Thanks a lot!

Context:
Long story short, you found a bug :).
Thanks for sharing the issue. The bug has been resolved, and the fix will be published in the next release of TF-DF.
In the meantime, when you specify “num_candidate_attributes”, make sure to also set “num_candidate_attributes_ratio=None”. For example:
model = tfdf.keras.RandomForestModel(
    num_candidate_attributes=5,
    num_candidate_attributes_ratio=None
    )

Yes, ‘num_candidate_attributes’ exactly corresponds to ‘mtry’ in R’s Random Forest libraries.
Details about the bug
In TF-DF, you can specify “num_candidate_attributes” or “num_candidate_attributes_ratio”. The bug is that “num_candidate_attributes_ratio” overrides “num_candidate_attributes” unless “num_candidate_attributes_ratio=None” (it should be “num_candidate_attributes_ratio=None” or “num_candidate_attributes_ratio=-1”). Unfortunately, “num_candidate_attributes_ratio=-1” by default.


Retraining Object detection model tutorials

Question:
Hi all,
I was hoping to train an object detection model.
I came across TFHub and TF model Garden, where I think model garden is more suitable if I want to retrain models.
Inside the GitHub repo, there is no clearly tutorial or example on training your model, and then making predictions with it.
Can someone point me to the right place unless I am missing something?
I thought would the shear number of models available, there would be an end to end example, that shows the model in action and prediction outputs atleast.
Thanks,

Context:
Hi Viraj,
There’s a couple of options to help you.
The easiest one is using Model Maker. This is not for on-device only! You can train regular TF models with it too.
On Model garden, you can follow these tutorials here: models/research/object_detection/colab_tutorials at master · tensorflow/models · GitHub 1
more specifically the Eager few shot training
Some more info here:

models/tf2.md at master · tensorflow/models · GitHub
models/tf2_training_and_evaluation.md at master · tensorflow/models · GitHub


Question:
Hi, I’m trying to use tf.data.Dataset.list_files to load .tiff images and infer their labels from their names.
I use the following code but stumbled upon a strange issue, as described bellow:
import os
import datetime as dt
import numpy as np
import pathlib
from pathlib import Path
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import cv2

DATA_PATH = Path('PATH TO DATA')
BATCH_SIZE = 32
INPUT_IMAGE_SHAPE = (128, 128, 1)
CROP_SHAPE = INPUT_IMAGE_SHAPE
CENTRAL_CROP_PROP = .7
BRIGHTNESS_DELTA = 0.1
CONTRAST = (0.4, 0.6)

class ConvModel(keras.Model):
    def __init__(self, input_shape):
        super().__init__()
        self.input_image_shape = input_shape
        self.model = keras.Sequential([
            layers.Input(shape=input_shape),
            layers.Conv2D(32, 3),
            layers.BatchNormalization(),
            layers.ReLU(),
            layers.MaxPool2D(),
            layers.Conv2D(64, 5),
            layers.BatchNormalization(),
            layers.ReLU(),
            layers.MaxPool2D(),
            layers.Conv2D(128, 3, kernel_regularizer=keras.regularizers.l2(0.01)),
            layers.BatchNormalization(),
            layers.ReLU(),
            layers.Flatten(),
            layers.Dense(64, activation='relu', kernel_regularizer=keras.regularizers.l2(0.01)),
            layers.Dropout(0.5),
            layers.Dense(10)
        ])

    def call(self, inputs):
        return self.model(inputs)

def preprocessing_func(image):
    img = tf.image.central_crop(image, CENTRAL_CROP_PROP)
    if img.shape[2] == 3:
        img = tf.image.rgb_to_grayscale(img)
    return img

def augment(image):
    img = tf.image.random_crop(image, size=CROP_SHAPE)  # Slices a shape size portion out of value at a uniformly chosen offset. Requires value.shape >= size.
    img = tf.image.random_brightness(img, max_delta=BRIGHTNESS_DELTA)  # Equivalent to adjust_brightness() using a delta randomly picked in the interval [-max_delta, max_delta)
    img = tf.image.random_contrast(img, lower=CONTRAST[0], upper=CONTRAST[1])  # Equivalent to adjust_contrast() but uses a contrast_factor randomly picked in the interval [lower, upper).
    img = tf.image.random_flip_left_right(img)
    img = tf.image.random_flip_up_down(img)

    return img

def load_image(image_file):
    # 1) Decode the path
    image_file = image_file.decode('utf-8')

    # 2) Read the image
    img = cv2.imread(image_file)
    if len(img.shape) < 3:
        img = np.expand_dims(img, axis=-1)
    img = preprocessing_func(image=img)
    img = augment(img)
    img = tf.cast(img, tf.float32)
    img.set_shape(INPUT_IMAGE_SHAPE)
    # 3) Get the label
    label = tf.strings.split(image_file, "\\")[-1]
    label = tf.strings.substr(label, pos=0, len=1)
    label = tf.strings.to_number(label, out_type=tf.float32)
    label = tf.cast(label, tf.float32)
    label.set_shape([])
    return img, label

def _fixup_shape(images, labels):
    images.set_shape(INPUT_IMAGE_SHAPE)
    labels.set_shape([])
    return images, labels

if __name__=='__main__':
    train_ds = tf.data.Dataset.list_files(str(DATA_PATH / '*.tiff'))
    train_ds = train_ds.map(lambda x: tf.numpy_function(load_image, [x], (tf.float32, tf.float32)))
    # train_ds = train_ds.map(_fixup_shape)
    train_ds = train_ds.batch(BATCH_SIZE)
    train_ds = train_ds.shuffle(buffer_size=1000)
    train_ds = train_ds.prefetch(tf.data.AUTOTUNE)
    train_ds = train_ds.repeat()

    model = ConvModel(input_shape=INPUT_IMAGE_SHAPE)
    model.compile(
        loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
        optimizer=keras.optimizers.Adam(learning_rate=3e-4),
        metrics=['accuracy']
    )

    train_log_dir = f'./logs/{dt.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}/train_data'
    callbacks = [
        keras.callbacks.TensorBoard(
            log_dir=train_log_dir,
            write_images=True
        )
    ]

    model.fit(
        train_ds,
        batch_size=32,
        steps_per_epoch=10,
        epochs=10,
        callbacks=callbacks
    )

While I try to run it it throws up an error :
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-213-b1f3d317135b> in <module>
----> 1 model.fit(
      2     train_ds,
      3     batch_size=32,
      4     steps_per_epoch=10,
      5     epochs=10,

~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

~\anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py in autograph_handler(*args, **kwargs)
   1127           except Exception as e:  # pylint:disable=broad-except
   1128             if hasattr(e, "ag_error_metadata"):
-> 1129               raise e.ag_error_metadata.to_exception(e)
   1130             else:
   1131               raise

ValueError: in user code:

    File "C:\Users\mchls\anaconda3\lib\site-packages\keras\engine\training.py", line 878, in train_function  *
        return step_function(self, iterator)
    File "C:\Users\mchls\anaconda3\lib\site-packages\keras\engine\training.py", line 867, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\mchls\anaconda3\lib\site-packages\keras\engine\training.py", line 860, in run_step  **
        outputs = model.train_step(data)
    File "C:\Users\mchls\anaconda3\lib\site-packages\keras\engine\training.py", line 817, in train_step
        self.compiled_metrics.update_state(y, y_pred, sample_weight)
    File "C:\Users\mchls\anaconda3\lib\site-packages\keras\engine\compile_utils.py", line 439, in update_state
        self.build(y_pred, y_true)
    File "C:\Users\mchls\anaconda3\lib\site-packages\keras\engine\compile_utils.py", line 359, in build
        self._metrics = tf.__internal__.nest.map_structure_up_to(y_pred, self._get_metric_objects,
    File "C:\Users\mchls\anaconda3\lib\site-packages\keras\engine\compile_utils.py", line 485, in _get_metric_objects
        return [self._get_metric_object(m, y_t, y_p) for m in metrics]
    File "C:\Users\mchls\anaconda3\lib\site-packages\keras\engine\compile_utils.py", line 485, in <listcomp>
        return [self._get_metric_object(m, y_t, y_p) for m in metrics]
    File "C:\Users\mchls\anaconda3\lib\site-packages\keras\engine\compile_utils.py", line 506, in _get_metric_object
        y_t_rank = len(y_t.shape.as_list())

    ValueError: as_list() is not defined on an unknown TensorShape.

though manually running X.shape.as_list() and y.shape.as_list() works, as shown below:
X, y = next(iter(train_ds))
X.shape.as_list(), y.shape.as_list()
[OUT] ([16, 128, 128, 1], [16])

This issue is fixed by manually mapping the following function on the dataset by train_ds = train_ds.map(_fixup_shape).batch(BATCH_SIZE):
def _fixup_shape(images, labels):
    images.set_shape([128, 128, 1])
    labels.set_shape([]) # I have 19 classes
    # weights.set_shape([None])
    return images, labels

if __name__=='__main__':
    train_ds = tf.data.Dataset.list_files(str(DATA_PATH / '*.tiff'))
    train_ds = train_ds.map(lambda x: tf.numpy_function(load_image, [x], (tf.float32, tf.float32)))
    train_ds = train_ds.map(_fixup_shape)
    train_ds = train_ds.batch(BATCH_SIZE)
    train_ds = train_ds.shuffle(buffer_size=1000)
    train_ds = train_ds.prefetch(tf.data.AUTOTUNE)
    train_ds = train_ds.repeat()

as described in this GitHub thread 1 there is a problem while using the tf.data.Dataset.list_files, which can be solved by mapping a fix function on the dataset.
Is it a bug in TF 2.6.1 or is it an expected behavior?
Thanks

Context:
Michael_Sidorov:

I do state the shapes of the images and the labels

Ah, yes that could use a little more explanation.
tf.data and tf.function (also used in keras) use TensorFlow’s 2-stage process. First it builds the graph, and then it executes the graph on your tensors.
Tensor.shape is the shape that was determined at graph build time (tf.shape(tensor) gets you the runtime shape).
So what’s happening here is that tf.numpy_function(load_image, ...)  says “don’t build a graph for this part, just run it in python”. So none of the code in  load_image runs during graph building, and TensorFlow doesn’t know you’re setting the shape in there.
Try putting only the non-tensorflow parts in your tf.numpy_function(load_image, ...). Move everything from img = preprocessing_func(image=img) down, into another function, then you should be able to ds.map() that function without the numpy_function wrapper.
But if you can use the tfio’s decode tiff, maybe you can drop the numpy_function entierly.


Problem computing gradients for Grad-CAM visualization

Question:
Hi,
I want to visualize a custom CNN (pre-trained feature extractor plus classification head finetuned on a new task) using Grad-CAM. I started with the example in Grad-CAM class activation visualization 2
Here is how the custom model looks like:
import tensorflow as tf
IMG_SHAPE = (299, 299, 3)
num_classes = 5
data_augmentation = tf.keras.Sequential([
tf.keras.layers.experimental.preprocessing.RandomFlip(‘horizontal’),
tf.keras.layers.experimental.preprocessing.RandomRotation(0.1) ])
base_model = tf.keras.applications.InceptionV3(input_shape=IMG_SHAPE,include_top=False,weights=‘imagenet’)
preprocess_input = tf.keras.applications.inception_v3.preprocess_input
global_avg_layer = tf.keras.layers.GlobalAveragePooling2D()
dense_layer = tf.keras.layers.Dense(1024, activation = ‘relu’)
predict_layer = tf.keras.layers.Dense(num_classes)

define model
inputs = tf.keras.Input(shape=IMG_SHAPE)
x = data_augmentation(inputs)
x = preprocess_input(x)
x = base_model(x, training=False)
x = global_avg_layer(x)
x = dense_layer(x)
x = tf.keras.layers.Dropout(0.2)(x)
outputs = predict_layer(x)
model = tf.keras.Model(inputs, outputs)
Then I need to calculate the output of the last conv layer and then compute the gradient of the top predicted class w.r.t. the activations of this last conv layer. However, using the following code to access the output of the last conv layer throws a “graph disconnected” error:
last_conv_layer = model.get_layer(‘inception_v3’).get_layer(‘conv2d_93’)
grad_model = tf.keras.models.Model(
[model.inputs], [last_conv_layer.output, model.output]
)
I don’t understand why the grad_model isn’t able to give me the outputs of the last conv layer. Can anyone please help me debug this?

Context:
Below may helps you ( please modify VGG and prediction layer).
You can set [data_augumentation] and [preprocess_input] to “input_tensor” option.
However it is not good to get a deterministic heatmap.

base_model = tf.keras.applications.VGG16(input_shape=(229,229,3),
                                       include_top=False,
                                       weights='imagenet',
                                       classes = len(class_names))

last_layer=base_model.layers[-1]
last_layer_outputshape=last_layer.output_shape
dense_unit_to_set=last_layer_outputshape[-1]

x2=base_model.output
x2 = tf.keras.layers.GlobalAveragePooling2D()(x2)
x2 = tf.keras.layers.Dropout(0.2)(x2)

x2=tf.keras.layers.Dense(dense_unit_to_set,activation='relu')(x2)

prediction_layer = tf.keras.layers.Dense(len(class_names),activation="softmax")#
x2 = prediction_layer(x2)

model = tf.keras.Model(inputs=base_model.input, outputs=x2)

base_learning_rate = 0.0001
batch_size= BATCH_SIZE
momentum_usage=0.99
initial_epochs = 50
sgd=tf.keras.optimizers.SGD(learning_rate=base_learning_rate,momentum=momentum_usage)

model.compile(optimizer=sgd,loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),metrics=['accuracy'])


What does your coding environment look like?

Question:
I’m always curious to learn new things!
I really enjoy using colab, but step-by-step debugging is hard, so I often use PyCharm to do this. Problem is getting a GPU to work well, so once I have a PyCharm environment working on a PC with a GPU, and TF works happily with that GPU, I’m in coding heaven.
Would love to hear about your setup here!

Context:
I got used to do everything on Colab!
I usually have close to 10 tabs open with different projects at the same time, am I the only one?


Can random forest model be converted to tensorflow.js

Question: I’m thinking about training a random_forest model in python using the tensorflow decision forests library. I was wondering if anyone knew if the model could be converted to tensorflow.js.

Context:
hi @Umair_Malik,
Unfortunately not. But thanks for bringing this up. Actually feel free to add a “feature request” issue in our github repository
This (or a variation, some way of having training and inference in the browser)  is a long standing item in our “we would love to do/have it” list!
We cannot commit to it yet, because we have a large list of higher priority items to work on right now. But I’ll post here whenever this changes.


Question:
def bn_act(x, act=True):
  x = keras.layers.BatchNormalization()(x)
  if act == True:
    x = keras.layers.Activation("relu")(x)
    return x

def conv_block(x, filters, kernel_size=(3, 3), padding="same", strides=1):
  conv = bn_act(x)
  conv = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides)(conv)
  return conv

def stem(x, filters, kernel_size=(3, 3), padding="same", strides=1):
  conv = keras.layers.Conv2D(filters, kernel_size, padding=padding, strides=strides)(x)
  conv = conv_block(conv, filters, kernel_size=kernel_size, padding=padding, strides=strides)
    #identity mapping
  shortcut = keras.layers.Conv2D(filters, kernel_size=(1, 1), padding=padding, strides=strides)(x)
  shortcut = bn_act(shortcut, act=False)

  output = keras.layers.Add()([conv, shortcut])
  return output

def residual_block(x, filters, kernel_size=(3, 3), padding="same", strides=1):
  res = conv_block(x, filters, kernel_size=kernel_size, padding=padding, strides=strides)
  res = conv_block(res, filters, kernel_size=kernel_size, padding=padding, strides=1)

  shortcut = keras.layers.Conv2D(filters, kernel_size=(1, 1), padding=padding, strides=strides)(x)
  shortcut = bn_act(shortcut, act=False)

  output = keras.layers.Add()([shortcut, res])
  return output

def upsample_concat_block(x, xskip):
  u = keras.layers.UpSampling2D((2, 2))(x)
  c = keras.layers.Concatenate()([u, xskip])
  return c
def ResUNet():
    f = [16, 32, 64, 128, 256]
    inputs = keras.layers.Input((image_size, image_size, 3))

    ## Encoder
    e0 = inputs
    e1 = stem(e0, f[0])
    e2 = residual_block(e1, f[1], strides=2)
    e3 = residual_block(e2, f[2], strides=2)
    e4 = residual_block(e3, f[3], strides=2)

    ## Bridge
    b0 = residual_block(e4, f[4], strides=2)

    ## Decoder
    u1 = upsample_concat_block(b0, e4)
    d1 = residual_block(u1, f[4])

    u2 = upsample_concat_block(d1, e3)
    d2 = residual_block(u2, f[3])

    u3 = upsample_concat_block(d2, e2)
    d3 = residual_block(u3, f[2])

    u4 = upsample_concat_block(d3, e1)
    d4 = residual_block(u4, f[1])

    outputs = keras.layers.Conv2D(1, (1, 1), padding="same", activation="sigmoid")(d4)
    model = keras.models.Model(inputs, outputs)
    return model

    model.summary()

I am trying to execute this model however it is executing in zero seconds which means its not executing and when u try to print model summary it gives me an error saying model is not defined

Context: Hi can you try to reformat your pose in code section?


Question: I’m trying to create a translation model, but the two languages have completely different alphabets, and are written in opposite directions. Thus, the embedding of one language will not help for the other. Any ideas on how to do this?

Context:
You can find an example of character level translation model for English and French languages here: Character-level recurrent sequence-to-sequence model 2
Probably, if you reverse the order of characters in the sentences of the language, which is written right to left, you would be able to use this code without other significant changes.


Reading input as binary from Tensorflow C++ api

Question:
I have input images written in the binary files.  I have to read binary file input and create a tiling of 512*512 image and do the inference and then padd all the output and return a single file.
I am using Tensorflow C++ API. I have the inference pipeline ready. I am struggling with reading binary files with TensorFlow and creating a tile and feed into the inference pipeline.
I have seen the available API. But nothing helps me to solve my problem. please guide How I can do it?

Context:
HI,
Can’t you read the binary file and do the tiling using regular C++ and just give the resulting image for the inference?


Question:
I am doing some cat poop research and I tried to use YoloV5 to detect different types of poop in the litter box. I collected about 130 poop pictures (Just poop pictures with no background) and labeled them and use roboflow to get annotations, then I follow colab notebook to train data and get the best.pt file for detection. When I run detection using a ramdom litter box picture, the rectangle just bound the whole image or half of the image instead of marking the poops in that image. Then I tried to labled 3 litter box images (Marked poops inside the litter box ) and do it all over again. But when I run detection using a litter box image. Nothing happened. I am so confused. Is it because poop shapes and color are so different to one and the other and they are irregular objects so it caused the detection didn’t work. By the way, I tried OpenCV Haar Cascade method. It at least give me very good result.  Anyone could give me some clues on how to lable the images and train them? And why the old technique is much better than those state of the art method?
The first picture I attached here was done by YoloV5. The second one was done by Haar Cascade method
Thank you very much for your help

pop3814×572 139 KB

haar_cascade1680×1270 346 KB

Context:
Hi Frank,
which colab did you follow to fine tune yolo5?


How do i get output of an internal layer for a custom model?

Question:
I am trying to make a Siamese convolutional neural network based on this tutorial 2. Basic form of the code i’m using to build this model:
def sister_cnn(inputShape, outputshape = 32):

    inputs = Input(inputShape)

    x = Conv2D(...)(inputs)
    x = MaxPooling2D(..)(x)
                .
                .
    outputs = Dense(outputshape, activation="..")(x)

    model = Model(inputs, outputs)
    return model

dataA = Input(shape=DATA_SHAPE)
dataB = Input(shape=DATA_SHAPE)
featureExtractor = sister_cnn(DATA_SHAPE)
featuresA = featureExtractor(dataA)
featuresB = featureExtractor(dataB)

distance = Lambda(custom_function)([featuresA, featuresB])
outputs = Dense(1, activation="sigmoid")(distance)
model = Model(inputs=[dataA, dataB], outputs=outputs)

# 'model' being the final siamese network i built

I want to get the output of an internal layewr of this model, more specifically, i want to get the output of the two 'sister_cnn’s which are essentially a 32 element arrays. None of these methods 4 worked for me. I keep getting some form of “Graph disconnected: cannot obtain value for tensor Tensor Input Keras Python” error when i acces model’s layer with index 2.
From what i could gather, it seems like these is some flaw with respect to connections of different layers in the way i build my model. But i’m not sure what to do to properly connect them, or maybe the issue is something else…
What should i do? Any help will be highly appreciated!

Context: Can you paste the full error? maybe there’s some clue there that can help


Question:
Is there a way to get the values of a Keras Tensor as a numpy array?
A normal K.eval() does not work and results in:
AttributeError: 'KerasTensor' object has no attribute 'numpy'

I use the eager execution.
I need this to access and check the outputs of some layers of my sequential model. Example: (out2 = K.eval(cnn.layers[2].output))
Minimal example:
import numpy
import tensorflow.keras.models as models
import tensorflow.keras.layers as layers
from keras import backend as K

cnn = models.Sequential([
    layers.Conv2D(filters=64, kernel_size=3, activation='relu',
                  kernel_initializer='he_uniform', padding='same',
                  input_shape=(32,32,3))],
    name='cnn')

res = cnn(numpy.random.random((1,32,32,3)))

print(K.eval(cnn.layers[0].output))

Context:
I don’t think that is the correct way to do this. You will need to pass your tensors to specific layers to get their individual outputs. Something like this will work:
import numpy
import tensorflow.keras.models as models
import tensorflow.keras.layers as layers

cnn = models.Sequential([layers.Conv2D(64, 3, 1, 'same', input_shape=(32,32,3), name='cnn1')])

# Get the convolutional layer
layer1 = cnn.layers[0]
# Call the layer by passing your tensor to the layer
layer1(numpy.random.random((1,32,32,3)))


How to do a 3D human body motion capture, which can drives to animated character using TensorFlow in Android?

Question:
Analytics India Magazine – 2 Sep 21

TensorFlow Releases New 3D Pose Detection Model 9

  TensorFlow recently launched its first 3D model in TensorFlow.js pose detection API. The new model opens up doors to new design opportunities for applications such as fitness, medical motion capture, entertainment, etc.

TensorFlow launched its first 3D model in TensorFlow.js pose detection API.
In the above link, there is an example of 3D motion capture, which drives an animated character in the browser using this TensorFlow.js pose detection API.
How can we do the same in Android or can we do it using webView in Android?

Context:
You can find more info with an android example at:

      mediapipe

Pose 4

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

But you still need to write the animated character rendering code on your side, you can find some example about TF-lite blazepose with unity at:

      GitHub

GitHub - asus4/tf-lite-unity-sample: TensorFlow Lite Samples on Unity 7

  TensorFlow Lite Samples on Unity. Contribute to asus4/tf-lite-unity-sample development by creating an account on GitHub.


Question:
I am trying to generate image segmentation for cerebral aneurysms. I have used UNET resnet50-unet and dense-net. UNET. after 5 epochs the valid loss increases or doesn’t change. I have done data augmentation. The images are Maximum intensity projection of brain MRIs. The loss starts at 0.99 and the lowest it goes is 0.95. The Jaccard score is 0.04 (highest) masks generated by the model are vague. Kernel size has been changed from 3 to 5 , learning rate is 0.001, optimiser Adam, loss function dice loss.
The code for model is:
from tensorflow.keras.layers import Conv2D, BatchNormalization, Activation, MaxPool2D, Conv2DTranspose, Concatenate, Input
from tensorflow.keras.models import Model
from tensorflow.keras.applications import ResNet50
def conv_block(input, num_filters):
x = Conv2D(num_filters, 5, padding=“same”)(input)
x = BatchNormalization()(x)
x = Activation(“relu”)(x)
x = Conv2D(num_filters, 5, padding="same")(x)
x = BatchNormalization()(x)
x = Activation("relu")(x)

return x

def decoder_block(input, skip_features, num_filters):
x = Conv2DTranspose(num_filters, (2, 2), strides=2, padding=“same”)(input)
x = Concatenate()([x, skip_features])
x = conv_block(x, num_filters)
return x
def build_resnet50_unet(input_shape):
“”" Input “”"
inputs = Input(input_shape)
""" Pre-trained ResNet50 Model """
resnet50 = ResNet50(include_top=False, weights=None, input_tensor=inputs)

""" Encoder """
s1 = resnet50.layers[0].output          ## (512 x 512)
s2 = resnet50.get_layer("conv1_relu").output        ## (256 x 256)
s3 = resnet50.get_layer("conv2_block3_out").output  ## (128 x 128)
s4 = resnet50.get_layer("conv3_block4_out").output  ## (64 x 64)

""" Bridge """
b1 = resnet50.get_layer("conv4_block6_out").output  ## (32 x 32)

""" Decoder """
d1 = decoder_block(b1, s4, 512)                     ## (64 x 64)
d2 = decoder_block(d1, s3, 256)                     ## (128 x 128)
d3 = decoder_block(d2, s2, 128)                     ## (256 x 256)
d4 = decoder_block(d3, s1, 64)                      ## (512 x 512)

""" Output """
outputs = Conv2D(1, 1, padding="same", activation="sigmoid")(d4)

model = Model(inputs, outputs, name="ResNet152_U-Net")
return model

if name == “main”:
input_shape = (512, 512, 3)
model = build_resnet50_unet(input_shape)
model.summary()
The code for training is:
import os
import numpy as np
import tensorflow as tf
from tensorflow.keras import backend as K
os.environ[“TF_CPP_MIN_LOG_LEVEL”] = “2” #set to 1 for warnings and errors
import numpy as np
import cv2
import keras
import keras.utils
from glob import glob
from sklearn.utils import shuffle
import tensorflow as tf
from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger, ReduceLROnPlateau, EarlyStopping, TensorBoard
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import Recall, Precision
H = 512
W = 512
def create_dir(path):
“”" Create a directory. “”"
if not os.path.exists(path):
os.makedirs(path)
def shuffling(x, y):
x, y = shuffle(x, y, random_state=42)
return x, y
def load_data(path):
x = sorted(glob(os.path.join(path, “image”, “.png")))
y = sorted(glob(os.path.join(path, “mask”, ".png”)))
return x, y
def read_image(path):
path = path.decode()
x = cv2.imread(path, cv2.IMREAD_COLOR)
x = x/255.0
x = x.astype(np.float32)
return x
def read_mask(path):
path = path.decode()
x = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
x = x/255.0

x = x > 0.5
x = x.astype(np.float32)
x = np.expand_dims(x, axis=-1)
return x

def tf_parse(x, y):
def _parse(x, y):
x = read_image(x)
y = read_mask(y)
return x, y
x, y = tf.numpy_function(_parse, [x, y], [tf.float32, tf.float32])
x.set_shape([H, W, 3])
y.set_shape([H, W, 1])
return x, y

def tf_dataset(x, y, batch=8):
dataset = tf.data.Dataset.from_tensor_slices((x, y))
dataset = dataset.map(tf_parse)
dataset = dataset.batch(batch)
dataset = dataset.prefetch(10)
return dataset
if name == “main”:
“”" Seeding “”"
np.random.seed(42)
tf.random.set_seed(42)
""" Directory for storing files """
create_dir("files")

""" Hyperparameters """
batch_size = 10
lr = 0.001
num_epochs = 200
model_path = os.path.join("files", "model.h5")
csv_path = os.path.join("files", "data.csv")

""" Dataset """
train_path = os.path.join("/content/drive/MyDrive/Data_brain/train/")
valid_path = os.path.join("/content/drive/MyDrive/Data_brain/test/")

train_x, train_y = load_data(train_path)
train_x, train_y = shuffling(train_x, train_y)
valid_x, valid_y = load_data(valid_path)

print(f"Train: {len(train_x)} - {len(train_y)}")
print(f"Valid: {len(valid_x)} - {len(valid_y)}")

train_dataset = tf_dataset(train_x, train_y, batch=batch_size)
valid_dataset = tf_dataset(valid_x, valid_y, batch=batch_size)

""" Model """
model = build_resnet50_unet(input_shape)
metrics = [dice_coef, Recall(), Precision()]
model.compile(loss=dice_loss, optimizer=Adam(lr), metrics=metrics)

callbacks = [
    ModelCheckpoint(model_path, verbose=1, save_best_only=True),
    #ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, min_lr=1e-7, verbose=1),
    CSVLogger(csv_path),
    TensorBoard(),
    #EarlyStopping(monitor='val_loss', patience=50, restore_best_weights=False),
]

model.fit(
    train_dataset,
    epochs=num_epochs,
    validation_data=valid_dataset,
    callbacks=callbacks,
    shuffle=False)

code for dice loss:
import numpy as np
import tensorflow as tf
from tensorflow.keras import backend as K
def iou(y_true, y_pred):
def f(y_true, y_pred):
intersection = (y_true * y_pred).sum()
union = y_true.sum() + y_pred.sum() - intersection
x = (intersection + 1e-15) / (union + 1e-15)
x = x.astype(np.float32)
return x
return tf.numpy_function(f, [y_true, y_pred], tf.float32)
smooth = 1e-15
def dice_coef(y_true, y_pred):
y_true = tf.keras.layers.Flatten()(y_true)
y_pred = tf.keras.layers.Flatten()(y_pred)
intersection = tf.reduce_sum(y_true * y_pred)
return (2. * intersection + smooth) / (tf.reduce_sum(y_true) + tf.reduce_sum(y_pred) + smooth)
def dice_loss(y_true, y_pred):
return 1.0 - dice_coef(y_true, y_pred)

Context:
My guess is that custom loss function is the culprit. Try to train the model with a crossentropy loss.
Also you can read this discussion of dice loss: python - Implementing Multiclass Dice Loss Function - Stack Overflow 2


Question:
Hello,
I am trying to run the classify_picamera.py example shown here.

      github.com

examples/lite/examples/image_classification/raspberry_pi at master ·... 5

  master/lite/examples/image_classification/raspberry_pi

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

when I try to run
python3 classify_picamera.py \
  --model /tmp/mobilenet_v1_1.0_224_quant.tflite \
  --labels /tmp/labels_mobilenet_quant_v1_224.txt

I get
Traceback (most recent call last):
      File "classify_picamera.py", line 96, in <module> main()
      File "classify_picamera.py", line 68, in main labels = load_labels(args.labels)
      File "classify_picamera.py", line 33, in load_labels with open(path, 'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/labels_mobilenet_quant_v1_224.txt'

I am feeling like I missed a step in the installation or maybe the instructions are not complete?
Has anyone run across these kinds of error or has gotten these errors?
Any help would be appreciated!
Thank you,
Sean Worcester

Context: @khanhlvg might be able to help here


As computational graph and serialized description of computation

Question:
I am studying the way the TFsaved data.
For saved_model format, one textbook mentions it saves “computational graph” wherein , in Training checkpoints  |  TensorFlow Core 2, it says it saves “serialized description of computation”. Are they referring to same? I am assuming so. Thanks.,

Context:
Take a look at:

      github.com

    tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md#tensorflow-savedmodel 1

    # TensorFlow SavedModel

[TOC]

## Overview

SavedModel is the universal serialization format for
[TensorFlow](https://www.tensorflow.org/) models.

SavedModel provides a language-neutral format to save machine-learning models
that is recoverable and hermetic. It enables higher-level systems and tools to
produce, consume and transform TensorFlow models.

## Guides
* [Using the SavedModel Format](https://www.tensorflow.org/guide/saved_model)
* [Save and load Keras models](https://www.tensorflow.org/guide/keras/save_and_serialize)
* [Save and load with checkpointing in Keras](https://www.tensorflow.org/tutorials/keras/save_and_load)
* [Training checkpoints](https://www.tensorflow.org/guide/checkpoint)
* [Save and load a model using a distribution strategy](https://www.tensorflow.org/tutorials/distribute/save_and_load)

  This file has been truncated. show original


Determining the orientation of eye co ordinates using pose estimation (python)

Question:
Hi , I am currently using pose estimation (movenet) to get the x,y co ordinates of the keypoints for the eyes . I am looking for a way to find out the orientation of the eye how it is moving. For simplicity , i am taking the average of both co-ordinates for left and right eye to get mid_x_coordinate and mid_y_cordinate.  However i am struggling to find out a way to find how the orientation of eyes are .
For simplicity , i was thinking to use cv2 .drawArrow function to see where the eyes are looking . However I am not sure how to solve it . Since we have only one set of cordinates. Any ideas ? A screen shot of the orientation is shown below .

Context:
I suggest you to take a look at the problem formulation in section B Data Post-processing  of:
https://arxiv.org/abs/2104.12668 5


Question:
I’ve a model that takes two inputs and outputs:
import tensorflow as tf
from tensorflow.keras import Input, Model

input1 = tf.keras.layers.Input(shape=(None, ))
input2 = tf.keras.layers.Input(shape=(None,))
inputs= tf.keras.layers.Concatenate(axis=1)([input1, input2])
inputs = tf.keras.layers.Reshape((-1,2))(inputs)
x = tf.keras.layers.Conv1D(filters=16, kernel_size=3, strides=1, padding="causal", activation="relu",input_shape=[None,2,1])(inputs)
x = tf.keras.layers.Bidirectional(tf.keras.layers.GRU(128, activation="tanh", return_sequences=True))(x)
x = tf.keras.layers.Bidirectional(tf.keras.layers.GRU(256, activation="tanh", return_sequences=True))(x)
x = tf.keras.layers.Dense(128, activation="tanh")(x)
o1 = tf.keras.layers.Dense(1, activation="linear",name="ed")(x)
o2 = tf.keras.layers.Dense(1, activation="sigmoid",name="sd")(x)

model = Model(inputs=[input1,input2], outputs=[o1, o2])

model.compile(loss={'ed': 'mean_squared_error',
                    'sd': 'binary_crossentropy'},
              optimizer='adam',
              metrics={'ed': tf.keras.metrics.MeanSquaredError(name="mean_squared_error", dtype=None),
                       'sd': tf.keras.metrics.BinaryCrossentropy(name="binary_crossentropy", dtype=None, from_logits=False, label_smoothing=0)})

Before feeding in the inputs , I’m windowing and shuffling them:
def windowed_dataset(series, window_size):
    series = tf.expand_dims(series, axis=-1)
    ds = tf.data.Dataset.from_tensor_slices(series)
    ds = ds.window(window_size + 1, shift=1, drop_remainder=True)
    ds = ds.flat_map(lambda w: w.batch(window_size + 1))
    return ds

I’m individually passing each variable through the windowing function :
w1 = 512 #window size
at = windowed_dataset(at, w1) #input
timt = windowed_dataset(timt, w1) #input
wt = windowed_dataset(wt, w1) #output
wbt = windowed_dataset(wbt, w1) #output

Finally, I’m calling model.fit:
lr_schedule = tf.keras.callbacks.LearningRateScheduler(
    lambda epoch: 1e-8 * 10**(epoch / 20))
history = model.fit([timt,at],[wt,wbt],epochs=100,callbacks=[lr_schedule])

The problem is it can’t find an adapter for FlatMapDataset:
ValueError: Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'tensorflow.python.data.ops.dataset_ops.FlatMapDataset'>"}), (<class 'list'> containing values of types {"<class 'tensorflow.python.data.ops.dataset_ops.FlatMapDataset'>"})

Pls guide me on how to solve this.
Thanks

Context: It seems this code is incomplete. So I am unable to reproduce the issue. Could your please provide minimal reproducible code (if it can be shared) to find the error?


Building Tensorflow from source with docker: how to change cuda version?

Question:
Dear,
We would like to rebuild tensorflow from source on your compute cluster to take advantage of hardware optimisation. We use the build based on docker since it’s difficult to install build dependencies on this cluster.
The build using cuda default option works but are relatively slow at runtime. We’re wondering if it is due to the fact that the build was done with cuda 11.2 and cuDNN 8 and in the cluster with installed cuda 11.4 ?
The other question is how to change the cuda and cudnn version when building tensorflow from sources ?
Best,
Jaonary

Context:
I think that you can upvote and subscribe to:

      github.com/tensorflow/tensorflow

      Upgrade TF to CUDA 11.4 and cuDNN 8.2  3

        opened

        Aug 25, 2021

          pkanwar23

        type:build/install

    Upgrade TF to CUDA 11.4 and cuDNN 8.2

E.g. the RBE image used in the CI was updated:

      github.com/tensorflow/tensorflow

      Update docker file for CUDA 11.4 RBE to use TensorRT 8. 1

        committed

        Aug 17, 2021

          chsigg

          +6
          -15

PiperOrigin-RevId: 391235853
Change-Id: I2247783906d709805c7e804729c9ced19069cb…75

But not in the tensorflow/docker-deve GPU:

      github.com

    angerson/tensorflow/blob/master/tensorflow/tools/dockerfiles/dockerfiles/devel-gpu.Dockerfile 2

    # Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
#
# THIS IS A GENERATED DOCKERFILE.
#
# This file was assembled from multiple pieces, whose use is documented
# throughout. Please refer to the TensorFlow dockerfiles documentation
# for more information.

  This file has been truncated. show original

/cc @angerson


Question:
Dear All,
I’ve updated Tensorflow and Keras to 2.6.0. (I use Windows 10 and Python 3.9.6)
While trying to follow this tutorial, Next-Frame Video Prediction with Convolutional LSTMs 12
import numpy as np
import matplotlib.pyplot as plt

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

I’ve faced the following issue.
---------------------------------------------------------------------------
AlreadyExistsError                        Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16440/2264619257.py in <module>
      4 import tensorflow as tf
      5 from tensorflow import keras
----> 6 from tensorflow.keras import layers
      7
      8 import io

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\api\_v2\keras\__init__.py in <module>
      8 import sys as _sys
      9
---> 10 from keras import __version__
     11 from keras.api._v2.keras import __internal__
     12 from keras.api._v2.keras import activations

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\__init__.py in <module>
     23
     24 # See b/110718070#comment18 for more details about this import.
---> 25 from keras import models
     26
     27 from keras.engine.input_layer import Input

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\models.py in <module>
     18 import tensorflow.compat.v2 as tf
     19 from keras import backend
---> 20 from keras import metrics as metrics_module
     21 from keras import optimizer_v1
     22 from keras.engine import functional

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\metrics.py in <module>
     24
     25 import numpy as np
---> 26 from keras import activations
     27 from keras import backend
     28 from keras.engine import base_layer

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\activations.py in <module>
     18
     19 from keras import backend
---> 20 from keras.layers import advanced_activations
     21 from keras.utils.generic_utils import deserialize_keras_object
     22 from keras.utils.generic_utils import serialize_keras_object

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\layers\__init__.py in <module>
     21
     22 # Generic layers.
---> 23 from keras.engine.input_layer import Input
     24 from keras.engine.input_layer import InputLayer
     25 from keras.engine.input_spec import InputSpec

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\engine\input_layer.py in <module>
     19 from keras import backend
     20 from keras.distribute import distributed_training_utils
---> 21 from keras.engine import base_layer
     22 from keras.engine import keras_tensor
     23 from keras.engine import node as node_module

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\engine\base_layer.py in <module>
     40 from keras.engine import node as node_module
     41 from keras.mixed_precision import autocast_variable
---> 42 from keras.mixed_precision import loss_scale_optimizer
     43 from keras.mixed_precision import policy
     44 from keras.saving.saved_model import layer_serialization

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\mixed_precision\loss_scale_optimizer.py in <module>
     16
     17 from keras import backend
---> 18 from keras import optimizers
     19 from keras.mixed_precision import loss_scale as keras_loss_scale_module
     20 from keras.optimizer_v2 import optimizer_v2

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\optimizers.py in <module>
     24 from keras.optimizer_v1 import Optimizer
     25 from keras.optimizer_v1 import TFOptimizer
---> 26 from keras.optimizer_v2 import adadelta as adadelta_v2
     27 from keras.optimizer_v2 import adagrad as adagrad_v2
     28 from keras.optimizer_v2 import adam as adam_v2

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\optimizer_v2\adadelta.py in <module>
     20 import numpy as np
     21 from keras import backend_config
---> 22 from keras.optimizer_v2 import optimizer_v2
     23 from tensorflow.python.util.tf_export import keras_export
     24

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\keras\optimizer_v2\optimizer_v2.py in <module>
     34
     35
---> 36 keras_optimizers_gauge = tf.__internal__.monitoring.BoolGauge(
     37     "/tensorflow/api/keras/optimizers", "keras optimizer usage", "method")
     38

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\tensorflow\python\eager\monitoring.py in __init__(self, name, description, *labels)
    358       *labels: The label list of the new metric.
    359     """
--> 360     super(BoolGauge, self).__init__('BoolGauge', _bool_gauge_methods,
    361                                     len(labels), name, description, *labels)
    362

c:\users\admin\appdata\local\programs\python\python39\lib\site-packages\tensorflow\python\eager\monitoring.py in __init__(self, metric_name, metric_methods, label_length, *args)
    133           self._metric_name, len(self._metric_methods)))
    134
--> 135     self._metric = self._metric_methods[self._label_length].create(*args)
    136
    137   def __del__(self):

AlreadyExistsError: Another metric with the same name already exists.

Context:
Do you have an env with Python 3.7? could you try on that?
I’m asking because on Colab (Python 3.7.11), that cell worked fine.
This is just to help find the issue.


Question:
Hello,
Does anyone know whether tf.print (or an equivalent workaround) can be made to work with XLA compilation? I get an error saying that XLA does not recognize the “printV2” operation if I have a tf.print statement inside a function decorated with @tf.function(jit_compile=True).
Thanks!

Context: Do you have already a github ticket?


TeOptimization loop cancelled with Keras tensorflow

Question:
0
I am running a NN on keras with tensorflow 2.6.0. The NN is used for RL. I am getting a message saying Optimization loop failed: Cancelled operation. My intuition is that the program was not able to carry out the optimization loop with gradient descent. The cost function also does not change at all hence no learning is taking place. Is there any solution for this.

Context: Do you have a very minimal Colab or gist to share with dummy inputs?


Question:
Due to limited funds I have been doing some experiments on a Raspbery Pi.
I’m currently training a model and the memory usage (global) is always bellow 1.5Gb.
I had access to a Jetson and when running the same code the memory ( 3Gb availble  before starting to run ) was exausted and had memory allocation errors.
On the Raspberry the Tensorflow version is 1.14.0 and on the Jetson 2.5.0.
The model I’m trying to fit:
model = Sequential()
model.add(Conv2D(filters=64,
kernel_size=(5, 5),
activation=“relu”,
dtype=‘float16’,
input_shape=(trainSize[0],trainSize[1],3 if trainMode == ‘COLOR’ else 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.10))
model.add(Conv2D(filters=64,
kernel_size=(5, 5),
activation=‘relu’))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.10))
model.add(Conv2D(filters=64,
kernel_size=(3, 3),
activation=“relu”))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.10))
model.add(Conv2D(filters=64,
kernel_size=(3, 3),
activation=‘relu’))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256,
dtype=‘float16’,
activation=‘relu’))
model.add(Dropout(0.25))
model.add(Dense(256,
dtype=‘float16’,
activation=‘relu’))
model.add(Dropout(0.25))
model.add(Dense(256,
dtype=‘float16’,
activation=‘relu’))
model.add(Dropout(0.10))
model.add(Dense(int(outputs*1.5),
dtype=‘float16’,
activation=‘relu’))
model.add(Dropout(0.10))
model.add(Dense(outputs,
activation=‘sigmoid’))
model.compile(optimizer=‘adam’,
loss=[keras.losses.MeanAbsoluteError()],
metrics=[keras.metrics.Precision(),keras.metrics.AUC(),‘accuracy’])
model.fit(sequence,
epochs=epochs,
steps_per_epoch = int(trainNumber/batchSize),
validation_steps = int(validationNumber/batchSize),
validation_data=validation,
verbose = False,
callbacks = callback)
What can cause the difference in memory usage between GPU and CPU based training?
What am I missing?

Context:
Have you tried with:

      TensorFlow

tf.config.experimental.set_memory_growth  |  TensorFlow Core v2.6.1 3

  Set if memory growth should be enabled for a PhysicalDevice.


Dataset_util is not founding any type of Module

Question:
Hello Everyone, I’m about to train my first object recognition for a jetson nano project. i looked at a lot of howtos/helps/etc. but currently i’m stuck while creating the TFRecords.
Whatever i try, i get
ModuleNotFoundError: No module named ‘dataset_util’
at “import dataset_util”
what is wrong? what do i need to install to get this module?
Thanks in Advance
Regards: Diksha

Context: If you are working on the stable TF version you need to install pip install tf-models-official for dataset_util


Update random value of tensor using random indices

Question:
So, i have a list that contain some seed generated from somewhere else.
I need to use those seed to create a list of random indeces and then create a new tensor with 0 everywhere except for those indeces.
For now the code is like so:
for index in [1,3,5]:
    seed = 1254 #Only for testing now
    #Tensor with X elements all 0s
    tensor_testing = tf.zeros([size_for_layer[index],], tf.float32)
    #Tensor with Y random generated indices
    indices = tf.random.uniform(shape=[size_for_layer_submodel[index],], minval=0,
                                maxval=size_for_layer[index], dtype=tf.dtypes.int64, seed=seed, name=None)

    #Same dimension as indices with the new value
    values = tf.fill([tf.shape(indices)[0], ], 15.4)
    #start the update
    tensor_testing = tf.tensor_scatter_nd_update(tensor_testing, indices, values)
    result[index] = tf.reshape(tensor_testing, shape_for_layer[index])

But i get this error:
ValueError: Dimensions [6,1) of input[shape=[32]] = [] must match dimensions [0,1) of updates[shape=[6]] = [6]: Shapes must be equal rank, but are 0 and 1 for '{{node TensorScatterUpdate}} = TensorScatterUpdate[T=DT_FLOAT, Tindices=DT_INT64](zeros, random_uniform, Fill)' with input shapes: [32], [6], [6].
I have actually solved the problem like so:
for index in [1,3,5]:
    seed = 1254 #Only for testing now
    #Tensor with X elements all 0s
    tensor_testing = tf.zeros(size_for_layer[index], tf.float32)
    # Tensor with Y random generated indices
    indices = tf.random.uniform(shape=[size_for_layer_submodel[index], ], minval=0,
                                maxval=size_for_layer[index], dtype=tf.dtypes.int64, seed=seed, name=None)

    values = tf.fill([tf.shape(tensor_testing)[0], ], 15.4)
    tensor_testing = tf.tensor_scatter_nd_update(tensor_testing, tf.expand_dims(indices, 1),
                                                 tf.gather(values, indices))

    result[index] = tf.reshape(tensor_testing, shape_for_layer[index])

Is there a way to make tf.random.uniform generate values with no duplicates?

Context:
Are you looking for something like:

      github.com/tensorflow/tensorflow

      [Feature request] np.random.choice analogue 2

        opened

        Mar 17, 2017

          closed

        Jun 17, 2017

          persiyanov

        stat:contributions welcome

    TF has no analogue to `np.random.choice` function that chooses random element fr…om tensor (optionally according to provided probabilities). It's especially useful in RL problems when you use epsilon-greedy exploration strategy.

### What related GitHub issues or StackOverflow threads have you found by searching the web for your problem?
[These](http://stackoverflow.com/questions/41123879/numpy-random-choice-in-tensorflow) [two](http://stackoverflow.com/questions/37757986/weighted-random-tensor-select-in-tensorflow) solve the problem using `tf.multinomial`, but maybe it will be convenient to include `tf.random_choice` in TF API?

It could be implemented through `tf.multinomial` , for example.

See also:

      github.com/tensorflow/tensorflow

      Sampling from a categorical distribution without replacement 1

        opened

        Apr 17, 2017

          danijar

        stat:contributions welcome
        type:feature

    Both `tf.multinomial()` and `tf.contrib.distributions.Categorical.sample()` allo…w to sample from a multinomial distribution. However, they only allow sampling with replacement.

In constrast, Numpy's `numpy.random.choice()` has a `replace` parameter that allows sampling without replacement. Would it be possible to add a similar functionality to TensorFlow?

One use case is sampling examples from the dataset proportional to the model's last loss on them. When an example generates a very large loss, the next batch will mainly consist of that example. Using sampling without replacement, we can avoid this problem.

I see that sampling with replacement can be parallelized and implemented in a vectorized way, but I don't think sampling speed is a bottleneck in most people's programs.


Question:
Hi,
I am switching from Pytorch (for several reasons) and I am exited on many improvements from Tensorflow 1 to Tensorflow 2.
Couple days ago I was able to install tensorflow on my linux machines (several Fedora versions) through pip, but today I can’t, its telling me “ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)”  PIP is working for everything else. Installing from source is not working also, and it is not easy to debug (due to use bazel, not more convenient for me configure, cmake, make).  PIP on Ubuntu works, but I would like to use Fedora 35 with python 3.10.
Any help will be appreciated. Thanks!

Context:
Please check:
https://discuss.tensorflow.org/t/support-python-3-10/


How to find mAP (mean absolute precision) of tflite of object detection?

Question:
Hi everyone, I converted the PyTorch file of yolov5 to tflite (object detection model). and I’m going to get the mAP(mean absolute precision) or other performance metrics of this file. but I can’t find code or a way to do that. do you have this experience or solution?
thank you

Context:
You can check:

      TensorFlow

TensorFlow Lite Delegates 4


Question:
Hi
OK, so i switch the runtime to use The GPU and restart the notebook.
I use the following code to check if GPU is connected.
import tensorflow as tf
tf.test.gpu_device_name()

I am using the below git-hub project to remove the background from images .

      github.com

GitHub - OPHoperHPO/image-background-remove-tool: 🥧 A tool for removing... 5

  🥧 A tool for removing background from photos with neural networks 🥧 - GitHub - OPHoperHPO/image-background-remove-tool: 🥧 A tool for removing background from photos with neural networks 🥧

It takes approx 30 mins to remove background of 86 Images.
I have used Google drive to read my Images (It took the Same 30 mins)
Then i downloaded the same dateset to my Google colab itself from MediaFire (Again took same 30 mins)
Now the thing is without the GPU, it also takes the Same time, that is 30 Mins (SO am i using the GPU or CPU wrong or is it not being utilized properly. ?
Please Help, Thanks

Context: You need to update the requirement.txt to work with the current stable version of TF 2.6


[Help Wanted]: Keras github-issue #15577

Question:
Following is the main query. Any suggestion on that would be appreciated. Thanks.

      github.com/keras-team/keras

      InvalidArgumentError: 9 root error(s) found. 9

        opened

        Nov 3, 2021

          innat

    https://discuss.tensorflow.org/tag/keras

**System information**.
- Have I wr…itten custom code (as opposed to using a stock example script provided in Keras):
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Colab
- TensorFlow installed from (source or binary): Colab
- TensorFlow version (use command below): 2.6
- Python version: Colab
- Bazel version (if compiling from source):
- GPU model and memory: TPU
- Exact command to reproduce:

You can collect some of this information using our environment capture script:

https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh

**Describe the problem**.

Describe the problem clearly here. Be sure to convey here why it's a bug in Keras or why the requested feature is needed.

**Describe the current behavior**.

I was trying to run [`keras/vit`](https://keras.io/examples/vision/image_classification_with_vision_transformer/) code example on TPU but it causes the following error.

> InvalidArgumentError: 9 root error(s) found.
  (0) Invalid argument: {{function_node __inference_train_function_49310}} Incompatible shapes: [256,25,64] vs. [144,64]

>	 [[{{node gradient_tape/model_1/patch_encoder_1/add/BroadcastGradientArgs}}]]
	 [[TPUReplicate/_compile/_12894506912996439067/_4]]
	 [[tpu_compile_succeeded_assert/_14236840403296971761/_5/_197]]
  (1) Invalid argument: {{function_node __inference_train_function_49310}} Incompatible shapes: [256,25,64] vs. [144,64]

>	 [[{{node gradient_tape/model_1/patch_encoder_1/add/BroadcastGradientArgs}}]]
	 [[TPUReplicate/_compile/_12894506912996439067/_4]]
	 [[tpu_compile_succeeded_assert/_14236840403296971761/_5/_245]]
  (2) Invalid argument: {{function_node __inference_train_function_49310}} Incompatible shapes: [256,25,64] vs. [144,64]

 I think it's due to the use of `tf.image.extract_patches` function on TPU. But not sure.

## [Reproducible Coe. ](https://colab.research.google.com/drive/1DAQpG9YD-5vH-A-jfaISwFzMkE6oAlm9?usp=sharing)

**Describe the expected behavior**.

It should simply run.

**[Contributing](https://github.com/keras-team/keras/blob/master/CONTRIBUTING.md)**.

- Do you want to contribute a PR? (yes/no):
- If yes, please read [this page](https://github.com/keras-team/keras/blob/master/CONTRIBUTING.md) for instructions
- Briefly describe your candidate solution(if contributing):

**Standalone code to reproduce the issue**.

Provide a reproducible test case that is the bare minimum necessary to generate
the problem. If possible, please share a link to Colab/Jupyter/any notebook.

**Source code / 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. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem.

Context: I think that we could continue this thread on the github issue.


How to manage gpu memory allocation properly

Question:
Hello all,
I have been running into issues with TensorFlow 2.6.1 in which my GPU memory is exhausted by relatively minor models. I have found a lot of documentation on using GPUs and limiting their memory allocation. But for me, they haven’t worked and they felt more like a patchwork solution anyways.
How can I begin writing more GPU optimal code for TensorFlow? What parameters determine the amount of memory used? Is GPU memory allocated in full at the start or is it allocated per batch as needed? Is memory then reallocated?
Details:
The error occurs at the beginning of training for a model with 4 conv2D layers and then 3 fully connected layers. Around 300 images are used and they are 512px512px3ch in size.
Trained on my home computer which uses 64GB of system RAM and an Nvidia 3070Ti 8 GB
I’m eager to get answers to these questions and to learn more about this area, let me know if you require any more information from me in order to assist.
Cheers!

Context:
Do you have already tried with:

      TensorFlow

tf.config.experimental.set_memory_growth  |  TensorFlow Core v2.6.1 7

  Set if memory growth should be enabled for a PhysicalDevice.

Also please check your model memory requirement for your specific input size:

    How to find out keras model memory size? General Discussion

    Hello!
I am doing a school work and I need to find out keras model memory size so I could compare different models. It is supposed to be composed of weights/parameters and model itself. It was given that model.summary() should contain all the information. From there I see that layer info and output shapes with the number of parameters.
I understand parameters as they are just a numbers and so, number of parameters * 4B most likely will give how much room parameters take. But I know that more i…


How to use Bert algorithm for multiple choice questions and answers

Question: I want to use the Bert algorithm which are pre trained model and also cdqa for multiple choice questions. Like this

Context:
You can take a look at:

    [Research ] Finetuned Language Models Are Zero-Shot Learners (by Google Research) Research & Models

    arXiv: [2109.01652] Finetuned Language Models Are Zero-Shot Learners

This paper explores a simple method for improving the zero-shot learning abilities of language models. We show that instruction tuning—finetuning language models on a collection of tasks described via instructions—substantially boosts zero-shot performance on unseen tasks. We take a 137B parameter pretrained language model and instruction-tune it on over 60 NLP tasks verbalized via natural language instruction templates. We e…

As the mentioned OpenbookQA is a multiple choiche Q/A dataset and it is available at:

      TensorFlow

openbookqa  |  TensorFlow Datasets 1

You can also see how hugginface is finetuning a model with the Keras API:

      huggingface.co

Fine-tuning a pretrained model 1

  In this tutorial, we will show you how to fine-tune a pretrained model from the Transformers library. In TensorFlow, models can be directly trained using Ker...

And an example of Bert on Multiplechoice (RocStories/SWAG dataset)

      huggingface.co

BERT 6

  Overview: The BERT model was proposed in BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding by Jacob Devlin, Ming-Wei Chang, Ke...