Question:
Sorry for spamming down the forum, but I have problems understanding the Eager Few Shot OD Training TF2 tutorial 3.
For this part:
detection_model = model_builder.build(
      model_config=model_config, is_training=True)

# Set up object-based checkpoint restore --- RetinaNet has two prediction
# `heads` --- one for classification, the other for box regression.  We will
# restore the box regression head but initialize the classification head
# from scratch (we show the omission below by commenting out the line that
# we would add if we wanted to restore both heads)

fake_box_predictor = tf.compat.v2.train.Checkpoint(
    _base_tower_layers_for_heads=detection_model._box_predictor._base_tower_layers_for_heads,
    # _prediction_heads=detection_model._box_predictor._prediction_heads,
    #    (i.e., the classification head that we *will not* restore)
    _box_prediction_head=detection_model._box_predictor._box_prediction_head,
    )
fake_model = tf.compat.v2.train.Checkpoint(
          _feature_extractor=detection_model._feature_extractor,
          _box_predictor=fake_box_predictor)
ckpt = tf.compat.v2.train.Checkpoint(model=fake_model)
ckpt.restore(checkpoint_path).expect_partial()

# Run model through a dummy image so that variables are created
image, shapes = detection_model.preprocess(tf.zeros([1, 640, 640, 3]))
prediction_dict = detection_model.predict(image, shapes)
_ = detection_model.postprocess(prediction_dict, shapes)
print('Weights restored!')

I don’t see how we actually restore the weights? As far as I can understand we create a checkpoint called fake_model that loads features from the model itself (bare ssd_resnet50 architecture with no weights, expect for random initial values).
We run restore on the provided checkpoint, but this is not linked to the model (detection_model) that is going to be trained in any way? Hence, we call restore on a checkpoint that is not linked to the model we are going to train?
So the model (detection_model) does not contain any of the weights from the checkpoint file.
In my mind this should be:
fake_box_predictor = tf.compat.v2.train.Checkpoint(
    _base_tower_layers_for_heads=detection_model._box_predictor._base_tower_layers_for_heads,
    # _prediction_heads=detection_model._box_predictor._prediction_heads,
    #    (i.e., the classification head that we *will not* restore)
    _box_prediction_head=detection_model._box_predictor._box_prediction_head,
    )
fake_model = tf.compat.v2.train.Checkpoint(
          _feature_extractor=detection_model._feature_extractor,
          _box_predictor=fake_box_predictor,
          model=detection_model)

fake_model.restore(checkpoint_path).expect_partial()

Thanks for any help and clarification!

Context:
It took me sometime to understand this too!
Think like this:

detection_model is loaded from a configuration with random weights
this structure is used as the base for fake_box_predictor and fake_model.
the weights are loaded on fake_model. detection_model is part of the fake_model so it’s weights will also be populated on the load.
finally, run a fake image over detection_model so that everything is structured properly

does it makes sense?


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

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

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 evaluate this instruction tuned model, which we call FLAN, on unseen task types. FLAN substantially improves the performance of its unmodified counterpart and surpasses zero-shot 175B GPT-3 on 19 of 25 tasks that we evaluate. FLAN even outperforms few-shot GPT-3 by a large margin on ANLI, RTE, BoolQ, AI2-ARC, OpenbookQA, and StoryCloze. Ablation studies reveal that number of tasks and model scale are key components to the success of instruction tuning.

724×522 198 KB

Language models (LMs) at scale, such as GPT-3 (Brown et al., 2020), have been shown to perform few-shot learning remarkably well. They are less successful at zero-shot learning, however. For example, GPT-3’s zero-shot performance is much worse than few-shot performance on tasks such as reading comprehension, question answering, and natural language inference. One potential reason is that, without few-shot exemplars, it is harder for models to perform well on prompts that are not similar to the format of the pretraining data.
…
Our empirical results underscore the ability of language models to perform tasks described using natural language instructions. More broadly, as shown in Figure 2, instruction tuning combines appealing characteristics of the pretrain–finetune and prompting paradigms by using supervision via finetuning to improve the ability of language models to respond to inference-time text interactions.

Model architecture and pretraining. In our experiments, we use a dense left-to-right, decoder-only transformer language model of 137B parameters. This model is pretrained on a collection of web documents (including those with computer code), dialog data, and Wikipedia tokenized into 2.81T BPE tokens with a vocabulary of 32K tokens using the SentencePiece library (Kudo & Richardson, 2018). Approximately 10% of the pretraining data was non-English. This dataset is not as clean as the GPT-3 training set and also has a mixture of dialog and code, and so we expect the zero and few-shot performance of this pretrained LM on NLP tasks to be slightly lower. We henceforth refer to this pretrained model as Base LM. This same model was also previously used for program synthesis (Austin et al., 2021).
Instruction tuning procedure. FLAN is the instruction-tuned version of Base LM. Our instruction tuning pipeline mixes all datasets and randomly samples examples from each dataset. Some datasets have more than ten million training examples (e.g., translation), and so we limit the number of training examples per dataset to 30,000. Other datasets have few training examples (e.g., CommitmentBank only has 250), and so to prevent these datasets from being marginalized, we follow the examples-proportional mixing scheme (Raffel et al., 2020) with a mixing rate maximum of 3,000.3 We finetune all models for 30,000 gradient updates at a batch size of 8,192 using the Adafactor Optimizer (Shazeer & Stern, 2018) with a learning rate of 3e-5. The input and target sequence lengths used in our finetuning procedure are 1024 and 256 respectively. We use packing (Raffel et al., 2020) to combine multiple training examples into a single sequence, separating inputs from targets using a special end-of-sequence token.

      arXiv.org

Finetuned Language Models Are Zero-Shot Learners 31

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

Context:
Source code for loading the instruction tuning
dataset used for FLAN is made publicly available at
https://github.com/google-research/flan 21

But it isn’t.


Unable to read TFRecord using tf.data.TFRecordDataset

Question:
I am trying to read a TFRecord file like this:
dataset = tf.data.TFRecordDataset("./tfrecords/train.record").map(_extract_fn).batch(3)

However, when I run
features, labels = iter(dataset).next()
I get this error:
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [4], [batch]: [2] [Op:IteratorGetNext]

This is the function that parses the TFRecord file:
    features = {
        'image/height': tf.io.FixedLenFeature([],tf.int64),
        'image/width': tf.io.FixedLenFeature([], tf.int64),
        'image/filename': tf.io.VarLenFeature(tf.string),
        'image/id': tf.io.FixedLenFeature([], tf.string),
        'image/encoded': tf.io.FixedLenFeature([],tf.string),
        'image/format': tf.io.FixedLenFeature([], tf.string),
        'image/object/class/text': tf.io.VarLenFeature(tf.string),
        'image/object/class/label': tf.io.VarLenFeature(tf.int64),
        'image/object/bbox/xmin': tf.io.VarLenFeature(tf.float32),
        'image/object/bbox/xmax': tf.io.VarLenFeature(tf.float32),
        'image/object/bbox/ymin': tf.io.VarLenFeature(tf.float32),
        'image/object/bbox/ymax': tf.io.VarLenFeature(tf.float32),
        }
sample = tf.io.parse_single_example(tfrecord, features)
data = {}

data["image/encoded"] = tf.image.decode_jpeg(sample["image/encoded"], channels=3)
label = sample['image/object/class/label'].values

return data,label

If I write return data instead and only set features = iter(dataset).next() it works fine.
What is the issue here?
Thanks for any help!

Context:
The label has a variable length:

 TensorOverflow:

'image/object/class/label': tf.io.VarLenFeature(tf.int64),

So when you try to .batch the dataset it can’t pack the different sized tensors together.
You either want to use .padded_patch or .apply(tf.data.experimental.dense_to_ragged_batch(...))


Question:
Im trying to import to tensorflow an onnx saved model from a pytorch implementation of efficientdet-d0 and I get the following error when I try to run a prediction or export the model :
ValueError: DepthwiseConv2D requires the stride attribute to contain 4 values, but got: 3 for ‘{{node depthwise}} = DepthwiseConv2dNative[T=DT_FLOAT, data_format=“NHWC”, dilations=[1, 1, 1, 1], explicit_paddings=[], padding=“VALID”, strides=[1, 1, 1]](transpose_4, Reshape_2)’ with input shapes: [?,?,?,?], [3,3,32,1].
code :
onnx_model = onnx.load(PATH + ‘model.onnx’)
onnx.checker.check_model(onnx_model)
tf_rep = prepare(onnx_model)
tf_rep.export_graph(“d0_s256_b32_ep400_TF.pb”)
or
onnx_model = onnx.load(PATH + ‘model.onnx’)
onnx.checker.check_model(onnx_model)
tf_rep = prepare(onnx_model)
tf_rep.run(Variable(torch.randn(32,3,256,256, dtype=torch.float32)))

Context:
I suggest you to post this on the LinuxFoundation ONNX slack:
https://onnx.ai/slack.html 6


Error while converting keras model to tensorflow saved model

Question:
Able to get keras model h5 format for masked RCNN.
I have tried 2 approaches:
1.While trying to convert keras model to tensorflow saved_model using GitHub - bendangnuksung/mrcnn_serving_ready: 🛠 Converting Mask R-CNN Keras model to Tensorflow 6 getting error as   File “main.py”, line 113, in
model.load_weights(H5_WEIGHT_PATH, by_name=True)
File “/home/ubuntu/Downloads/Blaize/mrcnn_serving_ready-master/model.py”, line 2131, in load_weights
saving.load_weights_from_hdf5_group_by_name(f, layers)
File “/home/ubuntu/Downloads/venv/lib/python3.6/site-packages/keras/engine/saving.py”, line 1328, in load_weights_from_hdf5_group_by_name
str(weight_values[i].shape) + ‘.’)
ValueError: Layer #389 (named “mrcnn_bbox_fc”), weight <tf.Variable ‘mrcnn_bbox_fc/kernel:0’ shape=(1024, 24) dtype=float32, numpy=
array([[-0.05486   , -0.03290214,  0.05897582, …, -0.05898178,
-0.06868616,  0.05374715],
[-0.06710163, -0.03682471, -0.03057443, …, -0.05611433,
-0.04561458,  0.05178914],
[-0.0041154 , -0.07344876, -0.06137543, …,  0.0011842 ,
0.04365869, -0.05199062],
…,
[ 0.06231805, -0.02443966, -0.00532094, …, -0.01833269,
-0.02245103, -0.01552512],
[-0.04047406, -0.06753345,  0.02390008, …,  0.01883602,
-0.04362615, -0.05265519],
[ 0.00530255,  0.04341973,  0.03085093, …, -0.07011634,
0.01440722,  0.02777647]], dtype=float32)> has shape (1024, 24), but the saved weight has shape (1024, 324).
2.in the second method using https://github.com/amir-abdi/keras_to_tensorflow,got 1 error as
ValueError('Unknown ’ + printable_module_name + ': ’ + class_name)
ValueError: Unknown layer: BatchNorm

Context:
I’m not sure I understand your question correctly. Does the error occur when you are saving the keras model into .h5 file or loading it from it?
If you are loading a model from a file and see messages about unknown layers, it mean that the original model contained some custom layers. You need to import them or initialize from scratch in your new code before loading the model.


Poisson matrix factorization in TFP with variational inference and surrogate posterior

Question:
Dear Tensorflow community,
I am new to Tensorflow and have thus encountered problem when I try to implement Poisson Matrix factorization (on the Wisconsin Breast Cancer dataset). The problem is the following:
I wish to build a gamma/poisson version of the PPCA model described here:

      TensorFlow

Probabilistic PCA  |  TensorFlow Probability 1

I have defined my model with gamma and Poisson distributions for the posterior, i.e. the target log joint probability, as well as initialising the two latent variables in my model (u,v). Moreover,  That is:
N, M = x_train.shape
L = 5
min_scale = 1e-5
mask = 1-holdout_mask

# Number of data points = N, data dimension = M, latent dimension = L
def pmf_model(M, L, N, gamma_prior = 0.1, mask = mask):
    v = yield tfd.Gamma(concentration = gamma_prior * tf.ones([L, M]),
                        rate = gamma_prior * tf.ones([L, M]),
                        name = "v")  # parameter
    u = yield tfd.Gamma(concentration = gamma_prior * tf.ones([N, L]),
                        rate = gamma_prior * tf.ones([N, L]),
                        name = "u")  # local latent variable
    x = yield tfd.Poisson(rate = tf.multiply(tf.matmul(u, v), mask), name="x")  # (modeled) data

pmf_model(M = M, L = L, N = N, mask = mask)

concrete_pmf_model = functools.partial(pmf_model,
                                       M = M,
                                       L = L,
                                       N = N,
                                       mask = mask)

model = tfd.JointDistributionCoroutineAutoBatched(concrete_pmf_model)

# Initialize v and u as a tensorflow variable
v = tf.Variable(tf.random.gamma([L, M], alpha = 0.1))
u = tf.Variable(tf.random.gamma([N, L], alpha = 0.1))

# target log joint porbability
target_log_prob_fn = lambda v, u: model.log_prob((v, u, x_train))

# Initialize v and u as a tensorflow variable
v = tf.Variable(tf.random.gamma([L, M], alpha = 0.1))
u = tf.Variable(tf.random.gamma([N, L], alpha = 0.1))

Then I need to state trainable variables/ parameters, which I do in the following (possibly wrong) way:

qV_variable0 = tf.Variable(tf.random.uniform([L, M]))
qU_variable0 = tf.Variable(tf.random.uniform([N, L]))

qV_variable1 = tf.maximum(tfp.util.TransformedVariable(tf.random.uniform([L, M]),
                                                        bijector=tfb.Softplus()), min_scale)
qU_variable1 = tf.maximum(tfp.util.TransformedVariable(tf.random.uniform([N, L]),
                                                        bijector=tfb.Softplus()), min_scale)

Ultimately, I make my model for the surrogate posterior and estimate the losses and trainable parameters:
def factored_pmf_variational_model():

    qv = yield tfd.TransformedDistribution(distribution = tfd.Normal(loc = qV_variable0,
                                                                     scale = qV_variable1),
                                           bijector = tfb.Exp(),
                                           name = "qv")

    qu = yield tfd.TransformedDistribution(distribution = tfd.Normal(loc = qU_variable0,
                                                                     scale = qU_variable1),
                                           bijector = tfb.Exp(),
                                           name = "qu")

surrogate_posterior = tfd.JointDistributionCoroutineAutoBatched(
    factored_pmf_variational_model)

losses = tfp.vi.fit_surrogate_posterior(
    target_log_prob_fn,
    surrogate_posterior=surrogate_posterior,
    optimizer=tf.optimizers.Adam(learning_rate=0.05),
    num_steps=500)

My code does NOT give an error however, my (after running the entire script stated here) trainable parameters are NaN for the qV_variable0 and qU_variable0. Is there a kind person, who can tell me why it goes wrong and it would be lovely to see a demonstration of how to use bijectors in the correct manner in tensorflow probability/ distributions with models estimated using Variational inference. Please also let me know if it is my target model or my surrogate posterior understanding that is wrong.
Thank you so much in advance!

Context: maybe @Christopher_Suter might be able to help


Question: I want to use a pretrained TensorFlow model inside a custom loss function to train another model. Is it possible to do that? it seems that I cannot run a sess in a graph.

Context:
Hi Leonard, can you clarify a little bit.
Do you want to use the pretrained TF model to be the ground truth and those values are compared to what your model is generating?
If so, could you instead cache the pre trained model’s results and use that instead?


Question:
Hi MoveNet developers,
I’ve been following MoveNet for a while I love how you guys are creating really useful and performant model to be used in mobile applications. I have a use case where I would like to fine-tune/modify the model. (e.g. modify number of key point, provide more targeted training from custom dataset)
Is there any plan to open source/enable fine tuning on MoveNet models? I’m relatively new to ML field (coming from SW) and looking at TFHub suggests that it’s not fine-tunable. (I did try to import/inspect the model on TF and yeah it looked pretty blackbox)  I searched around open source implementation and there’s one written in PyTorch,

      github.com

GitHub - lee-man/movenet: Un-official implementation of MoveNet from Google 20

  Un-official implementation of MoveNet from Google. Contribute to lee-man/movenet development by creating an account on GitHub.

I’ve no idea how accurate this implementation is, but I guess I have few options.

Use PyTorch implementation as baseline
Port this implementation/build in TF
Ask original devs for generosity

Let me know what you guys have planned, and any suggestions!
Thanks,
Daniel

Context:
Hello Daniel,
Thanks a lot for your interest in using MoveNet! You are right the MoveNet releases are currently not fine-tunable. But the model implementation is actually based on the Tensorflow Object Detection API 7 so you should be able to access the model code and modify it for your own purpose. As mentioned in the documentation, the MoveNet model is based on CenterNet with some modifications in the postprocessing logics and ops re-write to boost the performance. The closest model architecture can be found here 24. You can also find some example training configs in the /models/research/object_detection/configs/tf2/ folder.
I’d recommend you to start with the above codebase and modify from it. There are a few internal modifications which have not been released yet. We are still working on it and will let you know once it is there. Thanks.


Fine-tuning Tensorflow 2 Model Zoo models?

Question:
Hi,
Can someone from the Tensorflow Team please provide information on how to fine-tune a model from the Tensorflow 2 Model Zoo when training it for object detection?
I have read that there is a freeze_variables parameter in the pipeline.config file, but is this still usable?
Let’s say you want to freeze all the layers, expect the top 10, how can this be done when doing object detection?
Thanks!

Context: You could check Adding community contributed guides by sayakpaul · Pull Request #9271 · tensorflow/models · GitHub 20


Can we talk about TF-Hub downloading to TensorflowJS

Question:
Can someone comment on V5 downloading of TF-HUB models to TensorflowJS, I am not sure but things seem to have changed in the last year. I am most interested in fine-tuning which I thought needed to be uploaded using
const model = await tf.loadLayersModel

and frozen models converted from Tensorflow saved models could only be uploaded using
const model = await tf.loadGraphModel(modelUrl, {fromTFHub: true});

but V5 on TF-HUB seems to have fine-tunable Tensorflow Saved models, but the JS v1 and V3 versions only show the GraphModel load method which I thought was for frozen models.
tf-hub link here 1
I guess my question is, how do I find fine-tunable Layers models from TF-HUB?
Searching more I did find a JS(V5)  here 1
but it is using the Graph Model Load which as far as I know is frozen
const model = await tf.loadGraphModel(
    'https://tfhub.dev/google/tfjs-model/imagenet/mobilenet_v3_large_100_224/feature_vector/5/default/1',
    { fromTFHub: true });

Any opinions? I want to search, find and load layers models from TF-HUB?

Context: @lgusm May know more about TFHub discoverability for models


I want to develop an astronomical app using tensorflow to detect exoplanets

Question:
Greetings everyone,
I am a computer science student. For my graduation thesis, I want to work on a project that can detect exoplanets using machine learning. If you want to join me, want to support me, please do not hesitate to contact me.

Context: Hi. It will be a pleasure to help you in your project


Question: I will be using the EfficeintNet-D7 model through TensorFlow to count the number of objects in an image taken from the sky. I want to extract objects that only appear at 3ft or higher in an image (the object matches the ground so the model keeps incorrectly selecting it). How would I write this into the model?

Context:
Do you mean something like this?

      arXiv.org

IM2HEIGHT: Height Estimation from Single Monocular Imagery via Fully Residual... 2

  In this paper we tackle a very novel problem, namely height estimation from a
single monocular remote sensing image, which is inherently ambiguous, and a
technically ill-posed problem, with a large source of uncertainty coming from
the overall scale....


Deep Learning for signal processing problem

Question:
Hi there,
i am working on a loudspeaker that can generate audible soundwaves in a special medium that is a fluid-gel. Since the equations for the signal processing part are not completely understood yet, i want to use a deep learning script to aid. It is not complicated the only problem is that i am very new to Tensorflow and have not really an idea how to implement it, although i have been working a week on it.
So my problem is this: the loudspeaker has to generate ultrasonic modulated signals, the medium is nonlinear and therefore demodulates it, it becomes audible again in the medium. I can make many samples of some random signals and recorded samples, or hook the PC onto the loudpeaker and microfone, so that tensorflow can “learn” itself.
I just have no clue how to do is, are there any sample projects you know? I am very thankful for any help

Context:
I don’t know if you could find this useful for your project:

      github.com

magenta/ddsp 12

  DDSP: Differentiable Digital Signal Processing. Contribute to magenta/ddsp development by creating an account on GitHub.


Using deep learning to color blood smear images

Question:
Hi, i wanted to build a deep learning model to color white blood cells and platelets, without actually staining them. For instance, i input a blood smear image without any colors added, and the model should output the WBCs and platelets colored as if it was stained by hand. What would be the best way to build such a model?
Thanks!

Context:
You can start with:
https://lup.lub.lu.se/student-papers/search/publication/8998594 4
But generally you can adapt and experiment many segmentation models.
E.g. for U-NET style:
http://www.worldascience.com/journals/index.php/wassn/article/view/24/14 2


ZeroDivisionError with Dormand-Price ODE Solver Gradients

Question:
Hello,
I’m trying to implement a mechanistic model using TensorFlow which will be used as part of a GAN, based on the approach shown in this paper: [2009.08267] Integration of AI and mechanistic modeling in generative adversarial networks for stochastic inverse problems 1
The mechanistic model uses a TF Dormand-Prince solver to solve a set of differential equations which yield pressure waveforms for different regions of the cardiovascular system. I want to get gradients of the waveforms with respect to parameters of the mechanistic model for training the generator of the GAN.
A couple of my differential equations incorporate a variable which is time-varying (piecewise but continuous, no “sharp corners”) and which is computed from a subset of the parameters to the mechanistic model. If I set this variable to a constant, I can get gradients of the waveforms wrt model parameters. However, if I keep this variable as time-varying, then I get a ZeroDivisionError when I try to compute the gradients.
Any idea why this error might appear? I have included a stack trace below.
Thanks a lot for your help!
---------------------------------------------------------------------------

ZeroDivisionError                         Traceback (most recent call last)
 in
----> 1 dy6_dX = tape.gradient(y6, X)
~/.conda/envs/mpk/lib/python3.7/site-packages/tensorflow/python/eager/backprop.py in gradient(self, target, sources, output_gradients, unconnected_gradients)
1078         output_gradients=output_gradients,
1079         sources_raw=flat_sources_raw,
 → 1080         unconnected_gradients=unconnected_gradients)
1081
1082     if not self._persistent:
~/.conda/envs/mpk/lib/python3.7/site-packages/tensorflow/python/eager/imperative_grad.py in imperative_grad(tape, target, sources, output_gradients, sources_raw, unconnected_gradients)
75       output_gradients,
76       sources_raw,
—> 77       compat.as_str(unconnected_gradients.value))
~/.conda/envs/mpk/lib/python3.7/site-packages/tensorflow/python/ops/custom_gradient.py in actual_grad_fn(*result_grads)
472                          “@custom_gradient grad_fn.”)
473     else:
 → 474       input_grads = grad_fn(*result_grads)
475       variable_grads = []
476     flat_grads = nest.flatten(input_grads)
~/.conda/envs/mpk/lib/python3.7/site-packages/tensorflow_probability/python/math/ode/base.py in grad_fn(*dresults, **kwargs)
454               initial_time=result_time_array.read(initial_n),
455               initial_state=make_augmented_state(initial_n,
 → 456                                                  terminal_augmented_state),
457           )
458
~/.conda/envs/mpk/lib/python3.7/site-packages/tensorflow_probability/python/math/ode/dormand_prince.py in _initialize_solver_internal_state(self, ode_fn, initial_time, initial_state)
307     p = self._prepare_common_params(initial_state, initial_time)
308
 → 309     initial_derivative = ode_fn(p.initial_time, p.initial_state)
310     initial_derivative = tf.nest.map_structure(tf.convert_to_tensor,
311                                                initial_derivative)
~/.conda/envs/mpk/lib/python3.7/site-packages/tensorflow_probability/python/math/ode/base.py in augmented_ode_fn(backward_time, augmented_state)
388              adjoint_constants_ode) = tape.gradient(
389                  adjoint_dot_derivatives, (state, tuple(variables), constants),
 → 390                  unconnected_gradients=tf.UnconnectedGradients.ZERO)
391             return (negative_derivatives, adjoint_ode, adjoint_variables_ode,
392                     adjoint_constants_ode)
~/.conda/envs/mpk/lib/python3.7/site-packages/tensorflow/python/eager/backprop.py in gradient(self, target, sources, output_gradients, unconnected_gradients)
1078         output_gradients=output_gradients,
1079         sources_raw=flat_sources_raw,
 → 1080         unconnected_gradients=unconnected_gradients)
1081
1082     if not self._persistent:
~/.conda/envs/mpk/lib/python3.7/site-packages/tensorflow/python/eager/imperative_grad.py in imperative_grad(tape, target, sources, output_gradients, sources_raw, unconnected_gradients)
75       output_gradients,
76       sources_raw,
—> 77       compat.as_str(unconnected_gradients.value))
~/.conda/envs/mpk/lib/python3.7/site-packages/tensorflow/python/eager/backprop.py in _gradient_function(op_name, attr_tuple, num_inputs, inputs, outputs, out_grads, skip_input_indices, forward_pass_name_scope)
157       gradient_name_scope += forward_pass_name_scope + “/”
158     with ops.name_scope(gradient_name_scope):
 → 159       return grad_fn(mock_op, *out_grads)
160   else:
161     return grad_fn(mock_op, *out_grads)
~/.conda/envs/mpk/lib/python3.7/site-packages/tensorflow/python/ops/array_grad.py in _ConcatGradV2(op, grad)
228 def _ConcatGradV2(op, grad):
229   return _ConcatGradHelper(
 → 230       op, grad, start_value_index=0, end_value_index=-1, dim_index=-1)
231
232
~/.conda/envs/mpk/lib/python3.7/site-packages/tensorflow/python/ops/array_grad.py in _ConcatGradHelper(op, grad, start_value_index, end_value_index, dim_index)
117       # in concat implementation to be within the allowed [-rank, rank) range.
118       non_neg_concat_dim = (
 → 119           concat_dim._numpy().item(0) % input_values[0]._rank())  # pylint: disable=protected-access
120       # All inputs are guaranteed to be EagerTensors in eager mode
121       sizes = pywrap_tfe.TFE_Py_TensorShapeSlice(input_values,
ZeroDivisionError: integer division or modulo by zero

Context:
I was able to resolve this issue! I realized that the parameters which were giving me errors were passed into the function using them as 0-dimensional tensors rather than 1-d tensors; changing to pass them in as 1-d tensors alone solved the issue.
Not sure why such a minor difference (0-d vs 1-d) would result in a ZeroDivisionError - if anyone has suggestions on why, I’d really appreciate it! Thank you so much!


Question:
The new TF Decision Forests library is really cool in Colab, and I’m trying to get it working in a Kaggle notebook. Please find below a link to a Kaggle notebook where I’ve successfully used the new Decision Forest library. However, there is an issue with the model_plotter (on bottom of the Kaggle Kernel).
Any suggestion on what can be done to plot the model as it worked without a problem in Google Colab? Thank you!

      kaggle.com

TF-DF Car Evaluation 2

  Explore and run machine learning code with Kaggle Notebooks | Using data from [Private Datasource]

Context:
Hi NNCV,
Thanks  (for the positive feedback and alert).
TF-DF uses D3 for plotting. And it seems (looking at the console of the web browser development tool) that Jupiter does not support the way TF-DF import D3.
A solution, is to re-import D3 in a Jupyer notebook cell:
%%javascript
require.config({
    paths: {
        d3: "https://d3js.org/d3.v7.min"
     }
});

require(["d3"], function(d3) {
    window.d3 = d3;
});

then, the following like should work fine
tfdf.model_plotter.plot_model_in_colab(model, tree_idx=0, max_depth=3)

Note that you might have to clear the cell output and reload the webpage.
Alternatively, or if you don’t have a Jupiter or a Colab nodebook, the model plot can be exported to an html and visualized separably. For example:
with open("/tmp/model.html", "w") as f:
    f.write(tfdf.model_plotter.plot_model(model, tree_idx=0, max_depth=3))

# Then, open "/tmp/model.html" in any web browser.


Question:
Hello,
I recently try to start a project according to the excellent link below, I call it example project.

      Medium – 16 May 19

Human Activity Recognition using LSTMs on Android — TensorFlow for Hackers... 3

  Ever wondered how your smartphone, smartwatch or wristband knows when you’re walking, running or sitting?

    Reading time: 8 min read

In my new project, I plan to collect human activity movement data by some “expert” people to build a “expert” model and save it.
So, for example, when I do some activities, the expert model can not only predict what activity I am doing, and tells me the “Similarity”.
It means I can realize how much difference between me and the experts in the same activity movement.
Can anyone tells me if my idea workable? or give me any hint?
Are there any methods I can use in TensorFlow?
Any suggestions will be appreciated.
Thanks sincerely…

Context:
Hi Anthony,
This is a very interesting idea. Some parts that might be hard:

Have the comparison (pro vs user) be on the same perspective and position (rotation wise)

You might need a model (or algorithm) to pair the movement execution and then compare the differences.
There’s also some discussion on the topic here: https://discuss.tensorflow.org/t/what-model-s-for-a-sequence-of-human-poses


Question:
Hello!
I’m using the efficientnet model and the 1280 vector output for image similarity. It’s working great and from testing is the best model I’ve found with the amount of data that is used.
Sometimes it just does weird things though.
Here are the image in question: Imgur: The magic of the Internet 26
The first image is the input, the 2nd which should be found and the third one which is actually found as closest.
I’m using a compare script and these are the results of said images:
input against image that should be found (img1 vs img2)

features shape: (1280,)
euclidean: 0.839167594909668
hamming: 1.0
chebyshev: 0.14557853
correlation: 0.36508870124816895
cosine: 0.35210108757019043
minkowski: 0.839167594909668

input against image that is incorrectly returned as closest (img1 vs img3

features shape: (1280,)
euclidean: 0.7945413589477539
hamming: 1.0
chebyshev: 0.11684865
correlation: 0.32784974575042725
cosine: 0.3156479597091675
minkowski: 0.7945413589477539

I’m not understanding how img3 can be closer to the input than the other. It’s working most of the time and this is a weird outlier.
Any ideas how this can be solved?
Thanks!

Context: Was It trained only on your own dataset?


Question:
I am trying to build my own word2vec model using the code provided here
Link: - Word2Vec  |  TensorFlow Core 3
So i have even tried to increase the data as well for training the word embedding and i am able to achieve a good model accuracy but when i plot the word vectors on the Embedding Projector the distance between words or the word similarity is really bad, if i even use the cosine distance formula between very similar words the result is bad.
Whereas if the same data is used to train own embeddings using the Gensim library ( not pre-trained) the results of distance and similarity are way better, even on the Embedding Projector as well.
Please can someone help me regarding this, i want to use the Word2Vec code only which is provided by TensorFlow but i am not able to get good results for word distance and word similarity.

Context:
Could there be a problem in how you are serializing the embedding vectors and the associated words?
Also, can you confirm there is no difference in the hyperparameters that you are using in TensorFlow and Genism?


Freeze layers in Tensorflow Object Detection API

Question:
Is it possible to freeze specific layers when using Tensorflow Object Detection API?
For example, I am using EfficientDet downloaded from the Tensorflow 2 Model Zoo. When I train the model, I am going to make it predict whether an object is car,plane or motorcycle. The model is already trained on these types of objects (COCO 2017 dataset), but I want to train it more on my specific use case . But, since it is already trained on these types of object I do not want it to “forget” what it has already learned. Hence, I need to freeze some of the layers. So, is this possible? And, if it is possible, how do I know which layers I actually need to freeze? I have found that I might can use the freeze_variables parameter in the pipeline.config file?
Thanks for any help!

Context:
This might be what you’re after
Transfer Learning example 10
Specifically these lines:
base_model.trainable = True
# Let's take a look to see how many layers are in the base model
print("Number of layers in the base model: ", len(base_model.layers))

# Fine-tune from this layer onwards
fine_tune_at = 100

# Freeze all the layers before the `fine_tune_at` layer
for layer in base_model.layers[:fine_tune_at]:
    layer.trainable =  False


Recommender Dataset Structures (Tutorial)

Question:
Just getting into Tensorflow and i want to run the tensorflow recommender tutorials with my own data.
However, the datastructures there (dict-like, tensors can be addressed by “keys”) do not fit with the datasets structures in the tf.data starters.
Can pls someone point me somewhere there i can get a better overview over the dataset types (sub-classes) and best to work with recommenders?

Context:
I am not sure exactly what you want.
You can get Tensor from csv file via pandas like this.
df = pd.read_csv(‘movie.csv’)
tf.data.Dataset.from_tensor_slices(dict(df))


Question:
Dear all!
Thank you for reading this far
I was wondering if it is possible to make some kind of machine learning model that made an inference on each of the horizontal lines of an image. That is if I have a 1920x1080 image where each of the 1920 lines represents a spectra and outputs 1080 predictions. I belive that it could be beneficial for the training if I could input this prior knowledge into the model.
I want to be able to do this to port it by high-level synthesis to an FPGA for effective edge computing and hoped to be able to generate one (1) TensorFlow model for effective edge computing. The idea was to use PYNQ.
If anyone anywhere has anything to add to this idea, e.g. it is a bad idea, I would love to hear from you!
Best,
Sivert Bakken

Context:
I think that the first point to check Is the status of TF support on this board:

      PYNQ – 29 Sep 20

Apply Tensorflow model on pynq? 3

  @nfmzl @rock  Despite that this is a Pynq-Z2, is another option to leverage DPU-PYNQ in conjunction with TVM, assuming that nfmzl can port DPU-PYNQ to the Z2?       You should also be able to consider use of the TVM runtime / compiler with no DPU.  I...

I also suggest to check this:

      github.com

tanmay-ty/SpectralNET 4

  Contribute to tanmay-ty/SpectralNET development by creating an account on GitHub.


Question: I am trying to train a model with tf2 to find ambiguous objects such as small damages or small deformations in vehicles. I have tried several models but I have to lower the threshold to a lot so that it detects something … do you advise me some special model or some configuration for the pipeline.config? Thank you very much

Context:
From the my understanding, if the classes are very similar, you might need a very good dataset with lot’s of examples.
How many images/boxes are you using?


Implementing a CNN LSTM architecture for audio segmentation

Question:
Hi everyone,
I’m trying to implement a part of this paper: https://people.kth.se/~ghe/pubs/pdf/szekely2019casting.pdf 18
This part specifically:

Mel-spectrograms were extracted using the Librosa Python package with a window width of 20 ms and 2.5 ms hop length. The resulting spectrograms for two seconds of audio have 128×800 pixels. Zero crossing rates were calculated on the same windows. The neural network was implemented in Keras following the architecture in Figure 1. The first convolutional layer used 16 2D filters (size 3×3, stride 1×1) and ReLU nonlinearities, followed by batch normalisation and 5×4 max-pooling in both time and frequency. The second 2D convolutional layer used 8 filters in the frequency domain (4×1) and ReLU, followed by batch norm and 6×5 max pooling. Due to downsampling by the pooling layers, this produced 40 1×1 cells with 8 channels at a rate of 20 times per second. These were fed into a bidirectional LSTM layer of 8 hidden units in each direction, followed by a softmax output layer. The network was randomly initialised and trained for 40 epochs to minimise cross-entropy using Adadelta (with default parameters) batches of 16 two-second spectrogram excerpts. The softmax outputs can be interpreted as estimated per-frame class probabilities and used to automatically annotate the held-out episodes. Prior to further processing by either method, the temporal coherence of the automatic annotations was improved by merging mixed speech after a single-speaker segment into that speaker’s speech.

This is what I have :
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

model = keras.Sequential(
    [
     keras.Input(shape=(128, 800, 2)),
      layers.Conv2D(16, (3, 3), activation='relu'),
      layers.BatchNormalization()
      layers.MaxPooling2D(pool_size=(5, 4)),
      layers.Conv2D(8, (4, 1), activation='relu'),
      layers.BatchNormalization()
      layers.MaxPooling2D(pool_size=(6, 5)),
      layers.Bidirectional(layers.LSTM(8))
      layers.Dense(7),
    ]
)

model.compile(
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
    optimizer = keras.optimizers.Adadelta(),
    metrics = ["accuracy"],
)

model.fit(x_train, y_train, epochs=40, batch_size=16)

Can someone please help?

Context:
What do you need in particular?
Do you need to prepare audio data?

      TensorFlow

Audio Data Preparation and Augmentation  |  TensorFlow I/O 6


Parameter not updated with gradients at the end

Question:
Hello, I just tried implementing my own version of Scene GCNN from this paper (Holistic 3D Scene Understanding from a Single Image
with Implicit Representation by Zhang et al., arxiv code: 2103.06422) using TF2.0 framework (previous experience only in Pytorch)`:
What I run, what I get as a warning is :
WARNING:tensorflow:Gradients do not exist for variables [‘scene_gcnn/scene_gcn_conv_2/weight_rs/kernel:0’, ‘scene_gcnn/scene_gcn_conv_2/weight_rs/bias:0’, ‘scene_gcnn/scene_gcn_conv_2/weight_rd/kernel:0’, ‘scene_gcnn/scene_gcn_conv_2/weight_rd/bias:0’] when minimizing the loss.

Here’s my code down below:
import tensorflow as tf
from tensorflow.keras import activations, regularizers, constraints, initializers
import numpy as np

dot = tf.matmul
spdot = tf.sparse.sparse_dense_matmul

class Scene_GCNConv(tf.keras.layers.Layer):

    def __init__(self,
                 activation=lambda x: x,
                 use_bias=True,
                 kernel_initializer='glorot_uniform',
                 kernel_regularizer=None,
                 kernel_constraint=None,
                 bias_initializer='ones',
                 bias_regularizer=None,
                 bias_constraint=None,
                 activity_regularizer=None,
                 weight_shape=None,
                 **kwargs):

        self.activation = activations.get(activation)
        self.use_bias = use_bias
        self.kernel_initializer = initializers.get(kernel_initializer)
        self.bias_initializer = initializers.get(bias_initializer)
        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.bias_regularizer = regularizers.get(bias_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)
        self.kernel_constraint = constraints.get(kernel_constraint)
        self.bias_constraint = constraints.get(bias_constraint)

        self.weight_shape = weight_shape

        self.weight_sd = tf.keras.layers.Dense(self.weight_shape[1], activation=None, use_bias=self.use_bias, name="weight_sd")
        self.weight_sr = tf.keras.layers.Dense(self.weight_shape[1], activation=None, use_bias=self.use_bias, name="weight_sr")
        self.weight_dr = tf.keras.layers.Dense(self.weight_shape[1], activation=None, use_bias=self.use_bias, name="weight_dr")
        self.weight_rs = tf.keras.layers.Dense(self.weight_shape[1], activation=None, use_bias=self.use_bias, name="weight_rs")
        self.weight_rd = tf.keras.layers.Dense(self.weight_shape[1], activation=None, use_bias=self.use_bias, name="weight_rd")

        super(Scene_GCNConv, self).__init__()

    def call(self, z_o_prev, z_r_prev):

        # TODO : switched without z_o and z_r, and included all ones adjacent matrix in initialization
        # adjacent_matrix = 1 - tf.eye(z_o_prev.shape[1]) #which is N + 1

        dim = z_o_prev.shape[1]
        adjacent_matrix = 1 - tf.eye(dim) #which is N + 1

        z_o = self.update_object_nodes(z_o_prev, z_r_prev, adjacent_matrix)
        z_r = self.update_relationship_nodes(z_o_prev, z_r_prev, adjacent_matrix)

        output = [z_o, z_r]

        return output

    def update_object_nodes(self, object_nodes, relationship_nodes, adjacent_matrix):
        z_o = object_nodes
        z_r = relationship_nodes

        dim = adjacent_matrix.shape
        adjacent_matrix_r_compatible = tf.concat([adjacent_matrix, tf.ones([(dim[0]-1)*dim[0], dim[1]])], axis=0)

        first_term = self.weight_sd(z_o)
        second_term = dot(adjacent_matrix_r_compatible,self.weight_sr(z_r), transpose_a = True)
        third_term = dot(adjacent_matrix_r_compatible, self.weight_dr(z_r), transpose_a = True)

        z_o = self.activation(first_term + second_term + third_term)

        return z_o

    def update_relationship_nodes(self, object_nodes, relationship_nodes, adjacent_matrix):
        z_o = object_nodes
        z_r = relationship_nodes

        dim = adjacent_matrix.shape
        adjacent_matrix_o_compatible = tf.concat([adjacent_matrix, tf.ones([dim[0], (dim[1]-1)*dim[1]])], axis=1)

        first_term = dot(adjacent_matrix_o_compatible, self.weight_rs(z_o), transpose_a = True)
        second_term = dot(adjacent_matrix_o_compatible, self.weight_rd(z_o),transpose_a = True)

        z_r = self.activation(first_term + second_term)

        return z_r

## separate embedding transformation that should be inside a overall Scene Graph Conv Net

class Scene_GCNN(tf.keras.layers.Layer):

    def __init__(self,
                 activation=lambda x: x,
                 use_bias=True,
                 kernel_initializer='glorot_uniform',
                 kernel_regularizer=None,
                 kernel_constraint=None,
                 bias_initializer='ones',
                 bias_regularizer=None,
                 bias_constraint=None,
                 activity_regularizer=None,
                 weight_shape_array=None,
                 **kwargs):

        super(Scene_GCNN, self).__init__()

        self.activation = activations.get(activation)
        self.use_bias = use_bias
        self.kernel_initializer = initializers.get(kernel_initializer)
        self.bias_initializer = initializers.get(bias_initializer)
        self.kernel_regularizer = regularizers.get(kernel_regularizer)
        self.bias_regularizer = regularizers.get(bias_regularizer)
        self.activity_regularizer = regularizers.get(activity_regularizer)
        self.kernel_constraint = constraints.get(kernel_constraint)
        self.bias_constraint = constraints.get(bias_constraint)
        ## Initialize number of layers

        self.weight_shape_array = weight_shape_array

        self.num_iterations = len(weight_shape_array)

        self.sgcnn_layers = []

        for i, weight_shape in enumerate(self.weight_shape_array):
            self.sgcnn_layers.append(Scene_GCNConv(
                activation=self.activation,
                use_bias=self.use_bias,
                kernel_initializer=self.kernel_initializer,
                kernel_regularizer=self.kernel_regularizer,
                kernel_constraint=self.kernel_constraint,
                bias_initializer=self.bias_initializer,
                bias_regularizer=self.bias_regularizer,
                bias_constraint=self.bias_constraint,
                activity_regularizer=self.activity_regularizer,
                weight_shape=weight_shape))

        d = self.weight_shape_array[0][0]

        embed_relationship = tf.keras.models.Sequential()
        embed_relationship.add(tf.keras.Input(shape=(6,)))
        embed_relationship.add(tf.keras.layers.Dense(d, activation='relu'))
        embed_relationship.add(tf.keras.layers.Dense(d, activation=None))

        self.embed_relationship = embed_relationship

        embed_background = tf.keras.models.Sequential()
        embed_background.add(tf.keras.Input(shape=(3,)))
        embed_background.add(tf.keras.layers.Dense(d, activation='relu'))
        embed_background.add(tf.keras.layers.Dense(d, activation=None))

        self.embed_background = embed_background

        embed_slots = tf.keras.models.Sequential()
        embed_slots.add(tf.keras.Input(shape=(21,)))
        embed_slots.add(tf.keras.layers.Dense(d, activation='relu'))
        embed_slots.add(tf.keras.layers.Dense(d, activation=None))

        self.embed_slots = embed_slots

        final_embed_background = tf.keras.models.Sequential()
        final_embed_background.add(tf.keras.Input(shape=(21,)))
        final_embed_background.add(tf.keras.layers.Dense(3, activation=tf.keras.layers.LeakyReLU(alpha=0.01)))

        self.final_embed_background = final_embed_background

    # def call(self, slots, background_latent,):
    def call(self, inputs):

        slots = inputs[0]
        background_latent = inputs[1]
        #slots [B, num_obj, 21]
        #background_latent [B, 1, 3]
        background_latent = background_latent[:,None,:]

        object_nodes = self.get_object_nodes(slots, background_latent)
        relationship_nodes = self.get_relationship_nodes(slots)

        for i in range(self.num_iterations):
            object_nodes, relationship_nodes = self.sgcnn_layers[i](object_nodes, relationship_nodes)

        #object_nodes [B, num_object + 1, 21]
        slots = object_nodes[:,0:-1,:]
        background_latent = self.final_embed_background(object_nodes[:,-1,:])
        # output = [object_nodes, relationship_nodes]
        output = [slots, background_latent]

        return output

    def get_object_nodes(self, slots=None, background_latent = None):
        #Embedding of slot
        slots_embedded = self.embed_slots(slots)

        #Embedding of background
        background_latent_embedded = self.embed_background(background_latent)

        object_nodes = tf.concat([slots_embedded, background_latent_embedded], axis = 1)
        return object_nodes

    def get_relationship_nodes(self, slots):
        #Relationship nodes, between background and slots

        #  For nodes connecting two different objects, the geometry feature [20, 49] of 2D object bounding
        # boxes and the box corner coordinates of both connected objects normalized by the image height and width are used as
        # features.
        # In our example, we use x,y,z as values from each slot to get (N+1)^2 x 2d matrix where d=(x,y,z)

        #  The coordinates are flattened and concatenated in
        # the order of source-destination, which differentiate the relationships of different directions.

        # For nodes connecting
        # objects and layouts, since the relationship is presumably
        # different from object-object relationship, we initialize the
        # representations with constant values, leaving the job of inferring reasonable relationship representation to SGCN

        slots_extended = tf.concat([slots[:,:,18:21], tf.ones([slots.shape[0], 1, 3])],axis=1)
        A = tf.repeat(slots_extended, axis = 1,repeats=slots_extended.shape[1])
        #Add [B,1,latent_size] to both A and B to include layout
        B = tf.tile(slots_extended, multiples=[1,slots_extended.shape[1],1])

        relationship_nodes = tf.concat([A,B], axis=2)

        relationship_latent_embedded = self.embed_relationship(relationship_nodes)

        relationship_nodes = relationship_latent_embedded

        return relationship_nodes

if(__name__ == "__main__"):
    weight_shape_array=[(64,128),(128,64),(64,21)]

    scene_gcnn = Scene_GCNN(
            activation='sigmoid',
            use_bias=True,
            kernel_initializer='glorot_uniform',
            kernel_regularizer=None,
            kernel_constraint=None,
            bias_initializer='glorot_normal',
            bias_regularizer=None,
            bias_constraint=None,
            activity_regularizer=None,
            weight_shape_array=weight_shape_array)

    slots = tf.random.uniform([8,3,21])
    background_latent = tf.random.uniform([8,3])
    print(background_latent)
    print(scene_gcnn([slots, background_latent]))

    #Made up learning rate
    optimizer = tf.keras.optimizers.Adam(learning_rate=1e-7)

    is_training = True
    with tf.GradientTape() as tape:

        output = scene_gcnn([slots, background_latent])

        #Made up losses, don't think about the metric
        losses_background = tf.reduce_sum(tf.random.uniform([8,3]) - output[1])
        losses_foreground = tf.reduce_sum(tf.random.uniform([8,3,21]) - output[0])
        losses = losses_background + losses_foreground

    if is_training:
        variables = scene_gcnn.trainable_variables
        gradients = tape.gradient(losses, variables)
        optimizer.apply_gradients(zip(gradients, variables))

How do I avoid and solve this warning?
I know it’s a long code sample, but I included dummy test example which you can run if you call python command on it (TF2.0 required). I tried debugging this thing and seeing where are my holes and it has been an ongoing struggle to understand it, but no results so far.

Context:
You can use:
        optimizer.apply_gradients([
          (grad, var)
          for (grad, var) in zip(gradients, variables)
          if grad is not None
        ])


Semantic segmentation: How to maximize precision for the minor class in an imbalanced training dataset

Question:
I am using a deep neural network for a semantic segmentation task. The training data comprise 17,000 images that have been interpreted manually to generate two classes: MMD, and non-MMDs.
While I have good confidence in the accuracy of interpreted MMDs (high accuracy for true positives), there is still a chance that we misclassified some MMDs as non-MMD in the training data (even though its not very frequent). Further, the ratio of MMD to non-MMD samples is not balanced; and only ~20% of the training images are covered by MMDs. As the result, the training step can get biased toward the non-MMD class and show high accuracy for identifying the non-MMDs, which cover 80% of the image. I am currently using the following parameters:
• softmax as the activation function for the last layer
• Adam as the optimizer in Keras
• soft dice as the loss function to account for the imbalanced training data
• precision (tf.keras.metrics.Precision) as the metric to find the optimum model during training.
Obviously, my objective is to focus the training on maximizing the precision for the MMD class, and punish the training for generating false positives specifically for the MMD class. However, the above configuration does not seem to produce the desired results.
My question is, what would you do differently to achieve this objective? all suggestions are welcome!

Context:
I suggest to take a look at:

      github.com

JunMa11/SegLoss 11

  A collection of loss functions for medical image segmentation


How to improve classification prediction models accuracy?

Question:
Hi,
I have applied several classification methods, unfortunately, the developed models never exceed 62% of accuracy.
here I attached a comparison table of the developed models.
I’m wondering how I can improve the models’ accuracy!?

Accurcy Table699×451 17.1 KB

Context:
The first question i suggest to you is:

what are your performances on the training set?


Question: Hi, new to tfjs, just tested out tfjs-node mobilenet with pre-trained model.  I would like to learn how to train custom models using exiting video footage.  I came across roboflow.com 5 which looks like to give me what I want, however it only offer formats in TFRecords and Tensorflow Object Detection.  Any suggestions what tools I should use?

Context:
What specific frameworks are you looking for? Object Detection API is a well-developed and appreciated framework among the vision community. It comes with performance benefits, a rich repository of SOTA model implementations many of which are TFLite-compatible.
But here’s a standalone tutorial from keras.io 2 that shows you how to train a RetinaNet from scratch:

      keras.io

Keras documentation: Object Detection with RetinaNet 8


Question:
I have a button with an on_click() to run a function which executes a query.  It seems the on_click() is triggered when the parameters change, even if the button is not clicked. Example below - when queryText is changed via a text_area control, the on_click() function is executed.
 queryText = st.sidebar.text_area("SQL to execute:", value="", height=3, max_chars=None )
    if queryText:
        # issue - on_click executes when queryText is changed  , even if not clicked
        btnResult= st.sidebar.button('Run', key='RunBtn', help=None, on_click=runQuery(queryText,False), args=None, kwargs=None)
        if btnResult:
            st.sidebar.text('Button pushed')

Context:
Hi @MarkSim.,
Based on your simplified example I don’t think you should use state for this. I actually think you just need to use a form, that collects the SQL query and then has a form_submit_button that runs the typed query:
with st.sidebar.form("Input"):
    queryText = st.text_area("SQL to execute:", height=3, max_chars=None)
    btnResult = st.form_submit_button('Run')

if btnResult:
    st.sidebar.text('Button pushed')

    # run query
    st.write(queryText)

In my token example, I simply write the query to the page but you would be able to actually send the queryText to get the data.
Let me know if this solves your issue!
Happy Streamlit-ing
Marisa


Question: Is it possible to select rows of a table and then get the selected data? Can be st.table() or st.write(dataframe)

Context:
Hi @gaboc4. Unfortunately that functionality is not yet supported but we are designing it! In the meantime, a workaround would be to use an st.multiselect, e.g.:
selected_indices = st.multiselect('Select rows:', data.index)

Which will work up to about 1000 rows or so and which will look like this:

67903453-88fb1080-fb63-11e9-932e-c5d69ef97289.png1296×1684 466 KB

You can see an example in this gist 411.
This feature request is being tracked here 455. Please feel free to follow the issue to keep up with the latest developments. Also please feel free to comment on it so that we can better understand your use-case.
Thanks for using Streamlit!!


Text input strange behavior when key present

Question:
I wanted text input in a loop. st rightly complained about the fields having the same key. When I insert key using random, it stops printing values outside.
The following works fine:
import random
for i in range(1): # single field
    foo = random.choice(string.ascii_uppercase)+str(random.randint(0,1000))
    myc2 = st.text_input('')
    st.write(myc2,myc2[::-1])

The following complains about same key:
import random
for i in range(3): # loop and no key
    foo = random.choice(string.ascii_uppercase)+str(random.randint(0,1000))
    myc2 = st.text_input('')
    st.write(myc2,myc2[::-1])

The following stops printing the string and its reverse:
import random
for i in range(3): # loop and key
    foo = random.choice(string.ascii_uppercase)+str(random.randint(0,1000))
    myc2 = st.text_input('',key=foo)
    st.write(myc2,myc2[::-1])

The issue seems to be with the key since it also fails to print for loop of 1 with key
import random
for i in range(1): # no loop; key yes
    foo = random.choice(string.ascii_uppercase)+str(random.randint(0,1000))
    myc2 = st.text_input('',key=foo)
    st.write(myc2,myc2[::-1])

Baffled ;-(
Do others see the same thing?

Context:
Hi @AshishMahabal, welcome to the Streamlit community!
What you are seeing is one of the underlying mechanisms of how Streamlit works. In your first code snippet, you are creating a single st.text_input widget, so Streamlit can determine with certainty which one you are referring to.
In the second snippet, now you define a st.text_input 3 times; this does not overwrite the text_input, but rather orphans the references to the other ones (but the front end is still keeping track). So you get a key error.
If you want to keep writing over the same widget, put an st.empty() outside of your loop:

      docs.streamlit.io

st.empty - Streamlit Docs

  st.empty inserts a single-element container.

If your code has to be this way for some reason, you can add a key argument to st.text_input…set that value to your loop value i to make distinct text_input widgets.
Best,
Randy


How can i plot a GIF or video on streamlit?

Question:
I have a code that generates an animation of an orbit. How can I configure to plot the same video in a streamlit webapp? I tried something dot “st.pyplot(ani)” at the end, but the result generated an empty white square. Below is the part referring to the plot of my code.
fig = plt.figure()

plt.xlabel("x (km)")
plt.ylabel("y (km)")
plt.gca().set_aspect('equal')
ax = plt.axes()
ax.set_facecolor("black")
circle = Circle((0, 0), rs_sun, color='dimgrey',linewidth=0)
plt.gca().add_patch(circle)
plt.axis([- (rs_sun / 2.0) / u1 , (rs_sun / 2.0) / u1 , - (rs_sun / 2.0) / u1 , (rs_sun / 2.0) / u1 ])

# GIF

graph, = plt.plot([], [], color="gold", markersize=3, label='Tempo: 0 s')
L = plt.legend(loc=1)

plt.close()

def animate(i):
       lab = 'Tempo: ' + str(round(dt*i * (rs_sun / 2.0) * 3e-5 , -int(math.floor(math.log10(abs(dt*(rs_sun / 2.0)*3e-5)))))) + ' s'
        graph.set_data(x[:i], y[:i])
        L.get_texts()[0].set_text(lab)  # Atualiza a legenda a cada frame
        return graph,

skipframes = int(len(x)/200)
        if skipframes == 0:
        skipframes = 1

ani = animation.FuncAnimation(fig, animate, frames=range(0,len(x),skipframes), interval=30, blit = True, repeat = False)
HTML(ani.to_jshtml())

st.pyplot(ani)

Context:
isa.rsn:

HTML(ani.to_jshtml())

st.pyplot(ani)

Hi @isa.rsn, welcome to the Streamlit community!
What does HTML() represent? Is that the Jupyter HTML function? Streamlit has it’s own html function that might work:

      docs.streamlit.io

Components API - Streamlit Docs 2

Roughly
import streamlit.components.v1 as components

# ..all the other code

components.html(ani.to_jshtml())

Best,
Randy


How to solve a error generated in a gif in streamlit?

Question:
I added import streamlit.components.v1 as components at the beginning of my code and replaced st.pyplot(ani) with components.html(ani.to_jshtml()) . However, apparently the animation image was cut in half (pictured below). I’ve looked at the documentation and still haven’t been able to fix this error. Do you know what it could be?
Apart from that, adding a new question, my code is indeed a bit heavy and takes a few seconds to run. Is there any way to decrease plot time? Maybe if I previously make a text file with all the calculated data, or some configuration?
fig = plt.figure()

        plt.xlabel("x (km)")
        plt.ylabel("y (km)")
        plt.gca().set_aspect('equal')
        ax = plt.axes()
        ax.set_facecolor("black")
        circle = Circle((0, 0), rs_sun, color='dimgrey',linewidth=0)
        plt.gca().add_patch(circle)
        plt.axis([- (rs_sun / 2.0) / u1 , (rs_sun / 2.0) / u1 , - (rs_sun / 2.0) / u1 , (rs_sun / 2.0) / u1 ])

        # Montagem do gif

        graph, = plt.plot([], [], color="gold", markersize=3, label='Tempo: 0 s')
        L = plt.legend(loc=1)

        plt.close()  # Não mostra a imagem de fundo

        def animate(i):
            lab = 'Tempo: ' + str(round(dt*i * (rs_sun / 2.0) * 3e-5 , -int(math.floor(math.log10(abs(dt*(rs_sun / 2.0)*3e-5)))))) + ' s'
            graph.set_data(x[:i], y[:i])
            L.get_texts()[0].set_text(lab)  # Atualiza a legenda a cada frame
            return graph,

        skipframes = int(len(x)/200)
        if skipframes == 0:
            skipframes = 1

        ani = animation.FuncAnimation(fig, animate, frames=range(0,len(x),skipframes), interval=30, blit = True, repeat = False)
        HTML(ani.to_jshtml())

        components.html(ani.to_jshtml())

Streamlit
how it look in Google colab

streamlit1905×891 38 KB

Context: Thank you!!


Caption to matplotlib figure just like an image has

Question:
Hi Streamlit team!
Is it possible to add a caption to a st.pyplot just like a caption is nicely added to st.image? I do not see that option yet on the documentation page of st.pyplot - Streamlit Docs 1
Thanks in advance!

Context:
Hi @stefanbloemheuvel -
This isn’t a functionality we had considered…st.pyplot is a pretty simple wrapper, as we assume that whatever people are doing in matplotlib, the user will be create that sort of caption via code as well.
You could do something like saving a PNG from matplotlib and passing it to st.image to achieve what you are looking for.
Best,
Randy


Question:
Hi everyone, I currently have a view with a dataframe and 5 filters. The problem I have is that I can’t find a way to build a dynamic logic (users have the possibility to only use only 1 filter), and the only way I found is with the ‘if elif’ statement for all the combinations possible (not very convenient).
If there a better way to solve this?
Thanks!
Pierre

Context:
Hi pvtaisne,
See below for an example with multiple filters for one dataframe. Let me know if it helps you
import streamlit as st
import pandas as pd
import numpy as np

np.random.seed(0)  # Seed so random arrays do not change on each rerun
n_rows = 1000
random_data = pd.DataFrame(
    {"A": np.random.random(size=n_rows), "B": np.random.random(size=n_rows)}
)

sliders = {
    "A": st.sidebar.slider(
        "Filter A", min_value=0.0, max_value=1.0, value=(0.0, 1.0), step=0.01
    ),
    "B": st.sidebar.slider(
        "Filter B", min_value=0.0, max_value=1.0, value=(0.0, 1.0), step=0.01
    ),
}

filter = np.full(n_rows, True)  # Initialize filter as only True

for feature_name, slider in sliders.items():
    # Here we update the filter to take into account the value of each slider
    filter = (
        filter
        & (random_data[feature_name] >= slider[0])
        & (random_data[feature_name] <= slider[1])
    )

st.write(random_data[filter])


Question: How to create new pages by only using st.button?

Context:
st.button will triger page rerun, I do not recommond you to use it.
if you want to create new page, you can choose st.radio or streamlit-option-menu
see here of streamlit-option-menu:

    Streamlit-option-menu is a simple Streamlit component that allows users to select a single item from a list of options in a menu Show the Community!

    Happy new year everyone!
I discovered Streamlit a couple of weeks ago and fell in love with it immediately. To help myself learn how to make custom components, I created streamlit-option-menu, whose functions, though very simple, are not found in existing components. Hopefully it can be of use to someone else as well!
streamlit-option-menu is a simple Streamlit component that allows users to select a single item from a list of options in a menu.
 [menu]
It is similar in function to st.select…


Refreshing the screen without stacking images(tables) in a while loop application

Question:
Hi everyone,
I’ve stated using Streamlit recently and I’m still trying to find my way around to produce the result I need from my application.
My program has the following structure:

in the main function I am retrieving data via api, then do a lot of processing with pandas and I get four final tables which I am displaying on the Streamlit app.
this process is in an infinite loop, so everytime I retrive new data from api, I need to re-do the calculations and call the Streamlit display function to print me the updated tables.
My main problem is that the tables are stacking-up, instead of clearing the window and print tables on a fresh empty screen for each cycle of the loop.

This is what I have inside the StreamlitDisplay function:
st.empty()
col1, col2 = st.columns((3, 1))
fig1 = go.Figure(data=go.Table(header=dict(values=['Symbol', 'Price'], fill_color='paleturquoise'), cells=dict(values=[table1['Symbol'], table1['Price']))
with col1:
    st.header('My Table1')
    st.write(fig1)

I simplified the version of my function with just one table (and only two columns), so you have an idea about the structure of it. After I do this table printing job, the function returns to the main where the loop restarts with new api calls.
My expectation was that calling st.empty() when I call this display function will clear the screen allowing printing as if it was the first iteration of the loop. In reality I get images adding up to the bottom without end.
Using plotly.graph_objects I managed to do really nice customization to the layout of my tables, with colors and so on, so this is basically the last thing remaining to have the job done.
Is there any way to fix this or how can I approach this type of application involving an infinite loop where thing are re-calculated on each iteration and new results have to be updated on the screen?
Thanks you!

Context:
Hi @mg1795, welcome to the Streamlit community!
You need to define st.empty() outside of your function call, so that you can continue to write over that object.

    St.empty for images Using Streamlit

    Hi @Bonian, welcome to the Streamlit community!
Yes, what you are asking is how st.empty is intended to work. However, you should have the st.empty container defined outside of the loop if you want to keep reusing the same position. Our documentation has a couple of examples:

Best,
Randy

Best,
Randy


Question:
I have 8 multi-select boxes with me and I want them to be put in  2 columns (4 per column) , I’m using a dataset that i have connected. I’ve tired using the st. columns statement and I keep getting an error  ‘list’ object has no attribute ‘columns’.
How can I Proceed with this

Context:
Hi @Harsha_vardhan_Mudia, welcome to the Streamlit community!
Can you post the code you ran that doesn’t work?
Best,
Randy


Question:
I am trying to build a lineplot using plotly. However, whenever it is accessed from smaller screens, such as a mobile, it does not apply the responsive widths and forces you to horizontally scroll the page, in order to visualize the whole plot.

Everything else is responsive (apart from the plotly graphs).
Additionally, I tried to change the width of the plotly structure, and it seems to work fine! Although, I don’t have any way to retrieve the dynamic screen width (so I could calculate the length I would need, and thus solve the responsive issue myself).

This is the best I could reach so far.
Something as simple as width = st.screen.width would solve my point, as well as many others that require a minimum javascript aperture to work.
What should I do to guarantee a mobile-friendly app?
Thanks!

Context:
Hello @caio.hc.oliveira, welcome to the forums !
Would the default behavior of st.plotly_chart(fig, use_container_width=True) answer this ?

image422×502 13 KB

Regards


Question: I have a large valued option like 1012562,1012563,1012564,1012565,1012566, 1016243,1016244,1016245,1016246,1016247. And I used aggrid but the option is showed like 1012562,1012563,1012564,1012… but I need the entire option to be shown in the screen. I will be grateful if someone give me a solution.

Context: Please suggest me a solution.


Question:
So, I’m use @PablocFonseca  's EXCELLENT ag-grid component, but I’m up against a wall.  I have an editable grid setup.  But after the user has made some changes, I’d like to have a button that resets the contents.  I’m just not what to set.  Thanks.
gb = ag.GridOptionsBuilder.from_dataframe(orig_inputs)
gb.configure_default_column(groupable=True, value=True, enableRowGroup=True, aggFunc='sum', editable=True, type=["numericColumn", "numberColumnFilter"])
gb.configure_column('FlowDate', enableRowGroup=True, aggFunc='sum', editable=False, type=["dateColumnFilter", "customDateTimeFormat"], custom_format_string='yyyy-MM-dd', pivot=True)
go = gb.build()

inputs = ag.AgGrid(orig_inputs, gridOptions=go, update_mode='MANUAL')['data']

Context:
Answer my own question:
AgGrid has a parameter called “reload_data” - if called with True, will reload the data from the input.  (OW ignores it after initial input.)
this is my code:
    reload_data = False
    if st.button('Reset Inputs'):
        inputs = orig_inputs.copy()
        reload_data = True

    inputs = ag.AgGrid(inputs, gridOptions=go, reload_data=reload_data, update_mode='MANUAL')['data']
    reload_data = False


React custom component: ./frontend/build, and hasn’t received its “streamlit:componentReady” message

Question:
Hello,
I tried to embed React DataGrid table to streamlit (thanks for the examples here 1 and this tutorial 1), the development environment works fine, however the production shows the following error.

Screenshot 2022-01-26 at 11.39.43729×290 32.6 KB

I googled similar issues. My Streamlit version is the latest, 1.4.0 (don’t think it caused by timeout), and didn’t use st.columns in the code. It shows the same error in Streamlit Cloud. Couldn’t find any other possible answers for this problem…
Much appreciate your help!

Context:
Hi,
I’m having the same issue with 1.3.1 version.
When I run my React component locally it works fine, but when I’m setting the _RELEASE to point to “frontend/build” path - I get the “streamlit:componentReady”** message.
(In my case, it shows ** and not the path to the build folder).
Any help would be appreciated!

Screen Shot 2022-01-26 at 2.45.05 PM942×227 28 KB


What is the best way to publish my webapp made with streamlit?

Question: I want to release a code that generates orbit animations for educational purposes. Does streamlite allow me to publicize the site to be used by students and teachers? Also, is its interface independent? So that if more than one person is accessing the webapp, the parameter choices for example will not be affected or influenced by another user who is also accessing the website.

Context:
Hi @isa.rsn -
We at Streamlit believe that Streamlit Cloud 2 is the best way to deploy a Streamlit app.

 isa.rsn:

Does streamlite allow me to publicize the site to be used by students and teachers?

By default, an app deployed from the Community tier of Streamlit Cloud are publicly available, so you can share the URL with whomever. Recently, we’ve added the ability to launch one app from a private GitHub repo; in that case, you would need to either make the app public or invite individual people.

 isa.rsn:

Also, is its interface independent? So that if more than one person is accessing the webapp, the parameter choices for example will not be affected or influenced by another user who is also accessing the website.

Yes, Streamlit is designed to be multi-user, with the caveat that anything you are caching is currently global. This is usually what people want, such that if you load a large dataset, the data becomes available for everybody. If that’s not what you want, then if you don’t cache things the app should be independent for all users.
How many simultaneous users is a function of what the app does, i.e. how much RAM and computation the app needs to supply to each user. The larger the app, the fewer the users (or, upgrade to a paid tier where more resources are provided).
Best,
Randy


Question:
I am getting the following error when trying to create an Altair pie chart. I have Altair bar charts, line charts and stacked bar charts all working properly. However, as soon as I try to create a pie chart I run into a fatal error. To eliminate any issues with my typing and incorrect encodings etc. I copied the example from the Altair documentation (Pie Chart — Altair 4.2.0 documentation). This also fails. I am not sure what I am doing incorrectly.

Screenshot 2022-01-24 1835211055×276 10.5 KB

Context:
Make sure you have Altair 4.2 installed, which is when the mark_arc method was added:
https://altair-viz.github.io/releases/changes.html#version-4-2-0-released-dec-29-2021
Upgrading my local instance from Altair 4.1 to 4.2 with your code snippet displays a pie chart for me
Best,
Randy


Autoplay and controls properties for audio and video

Question:
Problem:
I have an app with a few audios (OGG bytes-like stored in a db). Works just fine if user press the play button using st.audio(mybytes). But I need this to autoplay, without showing those controls.
I tried to hack the  tag using js code, but couldn’t have it done even if using unsafe_allow_html. Probably injection of js is not yet allowed.
string="""
<button onclick='myFunction()'>Try it</button>
<script>function myFunction() {alert('Hello, world!');}</ script >
"""
st.markdown(string, unsafe_allow_html=True)

I then tried to create my own HTML using markdown, but in this case, I can’t use my bytes-like object without prior writing it to disk.
Expected (sth like):
st.audio(mybytes, format='audio/ogg', autoplay=True, controls=False)

Have anyone solved this kind of problem?

Context:
Solution:
mymidia_bytes is a blob object stored in my sqlite database [sqlite.Bytes(my_audio_bytes)], loaded to a pandas dataframe (row.Audio).
To autoplay it in the streamlit app:
import base64

mymidia_placeholder = st.empty()

mymidia_str = "data:audio/ogg;base64,%s"%(base64.b64encode(mymidia_bytes).decode())
mymidia_html = """
                <audio autoplay class="stAudio">
                <source src="%s" type="audio/ogg">
                Your browser does not support the audio element.
                </audio>
            """%mymidia_str

mymidia_placeholder.empty()
time.sleep(1)
mymidia_placeholder.markdown(mymidia_html, unsafe_allow_html=True)


NLTK error on deployment | Module cannot be found

Question:
So I’m trying to create a dictionary website using streamlit and wordnet from nltk inside an environment called “nlp” which already includes full nltk packages. Everything works properly except on deployment. I got a weird error message when I try to search a word on the deployed website, saying something like “this ‘wordnet’ package cannot be found” although I already added the nltk package inside the requirements.txt
Here’s the full error message
Resource e[93mwordnete[0m not found. Please use the NLTK Downloader to obtain the resource:
e[31m>>> import nltk

nltk.download(‘wordnet’) e[0m For more information see: NLTK :: Installing NLTK Data

Attempted to load e[93mcorpora/wordnete[0m
Searched in: - ‘/home/appuser/nltk_data’ - ‘/home/appuser/venv/nltk_data’ - ‘/home/appuser/venv/share/nltk_data’ - ‘/home/appuser/venv/lib/nltk_data’ - ‘/usr/share/nltk_data’ - ‘/usr/local/share/nltk_data’ - ‘/usr/lib/nltk_data’ - ‘/usr/local/lib/nltk_data’
Note: NLTK is installed inside the nlp conda environment
I’m completely stuck at this. Help me :’)

Context:
Hi @Dylan_Mac,
The error message also states that in addition to installing NLTK in the nlp conda environment (as you have done), you need to additionally download the ‘wordnet’ package in your app.
Include the following in your app:
import nltk
nltk.download('wordnet')

Source: NLTK :: Installing NLTK Data
Best,
Snehan


Disabled parameter resets the widget value

Question:
Hi Streamlit community!
I just noticed that changing the disabled parameter of widgets resets their value. Not sure if this behavior is intended or not, but is there a way to disable a widget while not resetting its value?
Here a code example of the behavior:
import streamlit as st

def toggle_widget():
    if st.session_state.widget_disabled:
        st.session_state.widget_disabled = False
    else:
        st.session_state.widget_disabled = True

if 'widget_disabled' not in st.session_state:
    st.session_state.widget_disabled = False

widget = st.number_input('Select a number between 1 and 6',min_value=1,max_value=6,value=3,disabled=st.session_state.widget_disabled)

st.button('Toggle widget',on_click=toggle_widget)

st.write(widget)

Thanks in advance!

Context:
Hey marduk,
import streamlit as st

def toggle_widget():
    if st.session_state.widget_disabled:
        st.session_state.widget_disabled = False
    else:
        st.session_state.widget_disabled = True

if 'widget_disabled' not in st.session_state:
    st.session_state.widget_disabled = False

if 'def_val' not in st.session_state:
    defaultValue=3
else:
   defaultValue=st.session_state.def_val

widget = st.number_input('Select a number between 1 and 6',min_value=1,max_value=6,value=defaultValue,key='def_val',disabled=st.session_state.widget_disabled)

st.button('Toggle widget',on_click=toggle_widget)

st.write(widget)


Are you using HTML in Markdown? Tell us why!

Question:
As described in this Github issue 83, it is very easy to write unsafe apps when developers have the ability to code in HTML. The idea is that users should always be able to trust Streamlit apps. So we have turned HTML off by default in Streamlit and will be removing the option to turn it on at some point in the future. (Update: We heard you, and have no plans to deprecate allow_unsafe_html! )
However, there are some legitimate use cases for writing HTML today to get around limitations in Streamlit’s current feature set. Some examples are:

Layout: laying items side by side, changing margin/padding/alignment properties, at.

Text-based diagrams: see this example 557 from a user working on NLP.

(Rest assured, we are currently working on solutions to the use-cases above. You will hear about them very soon!)
If you’re using HTML in Streamlit, we would like to hear from you! Let us know:

Why are you using it?
Any thoughts on what you’d like to see in Streamlit if you did not have the ability to use HTML?

Thanks!

Context:
I am working in NLP and want to highlight passages of text in different ways.
E.g. highlight negated words in red.
Afaik is this not possible in markdown and when introducing custom HTML elements as text they get moved into their own span objects even with allow_unsave_html turned on.
It is possible to do this, we just can’t use quotes or style tags in the element. The solution was to also print the css in  tags and avoid styling individual elements.


How to make one column in dataframe as checkbox

Question:
Hello,
How to display dataframe like below and also trap the event when checkbox is checked/unchecked?
Accept         col1  col2
--------       ----  ----
checkbox       row1  row1
checkbox       row2  row2

Did find this:
https://www.jscodetips.com/examples/how-to-add-a-checkbox-in-pandas-dataframe 4
But it’s just showing the html text in the cell.
Also perhaps, there’s other way.
TIA.

Context:
Why don’t you try aggrid with a checkbox column?
Cheers


Create link to open file from local system

Question:
Hi All,
In jupyter notebook
%%html
href="…/files/Downloads/jaipur.pdf#page=10" target="_blank">Link to a pdf
creates a link pointing towards pdf file and when clicked opens pdf in new tab.
How I can create a html link to open file using streamlit ?

Context:
Hi @kamal_kumawat
Have you checked this hack from @ash2shukla?

    Creating a PDF file generator Using Streamlit

    Hi @Pavan_Cheyutha,
You sure can !
Though streamlit doesnt support PDF generation out of the box for obvious reasons but you can look into PyFPDF and coupled with streamlit it can do the job. Adding a snippet that I just tried for reference.
import streamlit as st
from fpdf import FPDF
import base64

report_text = st.text_input("Report Text")

export_as_pdf = st.button("Export Report")

def create_download_link(val, filename):
    b64 = base64.b64encode(val)  # val looks like b'...'
    retu…

Thanks
Charly


Question:
Hello, I have a question regarding rerunning my streamlit app.
I have been using streamlit v0.72.0 with no problems, but when I upgrade to v1.3.0 I get the following error:

TypeError: request_rerun() missing 1 required positional argument: ‘client_state’

I am not really sure what it means, but this occurs when I call the “sync” function from the class below:

class SessionState:

def __init__(self, session, hash_funcs):
    """Initialize SessionState instance."""
    self.__dict__["_state"] = {
        "data": {},
        "hash": None,
        "hasher": streamlit.legacy_caching.hashing._CodeHasher(hash_funcs),
        "is_rerun": False,
        "session": session,
    }

def __call__(self, **kwargs):
    """Initialize state data once."""
    for item, value in kwargs.items():
        if item not in self._state["data"]:
            self._state["data"][item] = value

def __getitem__(self, item):
    """Return a saved state value, None if item is undefined."""
    return self._state["data"].get(item, None)

def __getattr__(self, item):
    """Return a saved state value, None if item is undefined."""
    return self._state["data"].get(item, None)

def __setitem__(self, item, value):
    """Set state value."""
    self._state["data"][item] = value

def __setattr__(self, item, value):
    """Set state value."""
    self._state["data"][item] = value

def clear(self):
    """Clear session state and request a rerun."""
    self._state["data"].clear()
    self._state["session"].experimental_rerun()

def sync(self):
    """Rerun the app with all state values up to date from the beginning to fix rollbacks."""

    # Ensure to rerun only once to avoid infinite loops
    # caused by a constantly changing state value at each run.
    #
    # Example: state.value += 1
    if self._state["is_rerun"]:
        self._state["is_rerun"] = False

    elif self._state["hash"] is not None:
        if self._state["hash"] != self._state["hasher"].to_bytes(self._state["data"], None):
            self._state["is_rerun"] = True
            self._state["session"].experimental_rerun()

    self._state["hash"] = self._state["hasher"].to_bytes(self._state["data"], None)

As I said this works perfectly fine in v.0.72.0 and only occurs when I upgrade.
Any help would be greatly appreciated.

Context: Any ideas on this one please?


Question:
Hi Folks,
I would like to include the open source molecule editor JSME 13 into a Streamlit app.
I’ve found out that there is already a React component: https://github.com/DouglasConnect/jsme-react 19
However, I’m not getting it to display in my app.
I used the component template and added something like:
import { Jsme } from 'jsme-react'
class MyComponent extends StreamlitComponentBase {
  public render = (): ReactNode => {
	return (

Hello World

	<Jsme height="500px" width="800px" />

	  )
  }
}
export default withStreamlitConnection(MyComponent);

The “Hello World” shows up but not the rest.
I am very new to React, JS, etc.
Anyone any idea how I could get it working?
Many thanks in advance.
Cheers,
LeChuck

Context: Nobody?


Streamlit app working with dev server and not in production

Question:
I have created an app that uses Vue for the frontend. I followed the documentation and the available reactless-template in the GitHub repo 1.
My code is working locally when running npm run start which internally invokes the script vue-cli-service serve. However, when I build the code for deploying with npm run build which uses vue-cli-service build (I rename the build folder from dist to build) it doesn’t work any more and it gives me the following message:
Your app is having trouble loading the streamlit_app.terminal component.

(The app is attempting to load the component from *, and has not received its "streamlit:componentReady" message.)

If this is a development build, have you started the dev server?
If this is a release build, have you compiled the frontend?
For more troubleshooting help, please see the Streamlit Component docs or visit our forums.

Is it possible that it is not working because of the npm run start?
P.S. my code is available on GH 3 as well, it might give a better idea!.
Any ideas or comments are welcome

Context:
Hey there,
Did you manage to solve this?
I have the same issue.
Thanks!


Question:
There are examples everywhere using streamlit.ReportThread, however streamlit.ReportThread throws a module not found error on multiple machines. streamlit.report_thread does exist but none of the functions work if i use streamlit.report_thread. How do i fix this error? do i need a specific version of streamlit so that streamlit.ReportThread does exist and the examples work?
Thanks for the help!!
Here is an example that previously worked but no longer does because ReportThread is not found

import streamlit.ReportThread as ReportThread

To add a little more to this. streamlit version 0.63.0 works with
from streamlit import ReportThread
If ReportThread was changed to streamlit.report_thread after version 0.63.0 then why are all examples still showing
from streamlit import ReportThread
Also, if i try to update from ReportThread to report_thread in later versions streamlit then it breaks code that used ReportThread.

Context:
I was running into the same problem and I found this gist to be helpful: A possible design for doing per-session persistent state in Streamlit · GitHub 83
Particularly this section for this specific problem:
try:
    import streamlit.ReportThread as ReportThread
    from streamlit.server.Server import Server
except Exception:
    # Streamlit >= 0.65.0
    import streamlit.report_thread as ReportThread
    from streamlit.server.server import Server

But I also found this section useful and related:

s = session_info.session
if (
    # Streamlit < 0.54.0
    (hasattr(s, '_main_dg') and s._main_dg == ctx.main_dg)
    or
    # Streamlit >= 0.54.0
    (not hasattr(s, '_main_dg') and s.enqueue == ctx.enqueue)
    or
    # Streamlit >= 0.65.2
    (not hasattr(s, '_main_dg') and s._uploaded_file_mgr == ctx.uploaded_file_mgr)
):
    this_session = s


Question:
Hey Streamilt community,
I am getting rather unexecpted behaviour from the st.columns method, I would expect the layout of the page to be identical each run, however, if I refresh the page a few times, the layout is likely to be different on some of the runs, see the attatched image as an example when things go wrong. Is there a solution to this problem?
st.set_page_config(layout=“wide”)
col1, col2, col3, col4, col5 = st.columns([1, .1, 1, .1, 1])
col1.write(‘Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.’)

image1894×330 15.4 KB

Context: I am using Chrome btw.


Alignment is different in local version and cloud version

Question:
Hello Streamlit!
First of all, amazing work on this package, it is great!
However, one small issue that i am facing is alignment issues when i deploy my app online with streamlit share cloud.
The following screenshots use the same code, but as is visible, the cloud version (bottom screenshot) stops nicely aligning my table in the middle.

localversion1215×403 30 KB

onlineversion1169×438 29.6 KB

The code used is the folowing:
col1, col2, col3 = st.columns([1,2.5,1])

with col2:
    st.table(pd.DataFrame({
            'Time': ['21-12-21 10:00:00', '21-12-21 10:00:01','21-12-21 10:00:02','21-12-21 10:00:03'],
            'Sensor1': [10, 10, 11, 10],
            'Sensor2': [14,15,14,14]
        }).style.applymap(color_column, subset=['Time']))

How could i fix this?

Context:
You can set the Python version in the Advanced Settings menu:

      docs.streamlit.io

Deploy an app - Streamlit Docs

Best,
Randy


Get_query_params() not working with ‘#’ instead of ‘?’

Question:
I am trying to integrate the Streamlit frontend with a Supabase backend. The great thing is that I can build upon the user sign_up, password_recovery, etc. logic already build into Supabase. This requires me to read the URL of the Streamlit report and check all parameters. Supabase uses a # sign behind its confirmation URL.
<SITE_URL>#access_token=x&refresh_token=y&expires_in=z&token_type=bearer&type=recovery
See also:

      supabase.com

Reset Password (Email) | Supabase 1

  Sends a reset request to an email address.

Unfortunately the get_query_params() function does not parse this url into a correct query_string. The query_string only works with a ? as separator. Is there some workaround for this issue? Or could get_query_params() be updated to work with a # as separator?

Context: get_query_params() only uses data available to the server. I’m trying to use Supabase too, I’ll try releasing some useful bits soon.


Question: How to notify user that the code is running in streamlit? I edit the theme so the “running” icon is not clearly shown in the top right corner.

Context:
You can use a while-loop to show an animation while something is loading. So something like:
while (condition):
       st.image('https://media.giphy.com/media/gu9XBXiz60HlO5p9Nz/giphy.gif')

So while a function is doing its thing you can display something for the user. If statements also work … If button == True: st.image(‘https://media.giphy.com/media/gu9XBXiz60HlO5p9Nz/giphy.gif’)
If you mean audio feedback then there is also a python library for that e.g.  winsound. There you can also apply the same logic as before.
Edit: And don’t forget “show the community” is more to showcase your finished work rather than discussing specific problems. “Using streamlit” is for discussing problems.


50MB dataset limitation when using Plotly.py

Question:
Hi there,
I’m using Plotly.py to show figures in my Streamlit app with big datasets. And now I get a notification of dataset oversizing, bigger than 50.0MB, is there anyway to solve this?

Context:
Thanks for the reply! I found this github link before I posted this issue, but it didn’t solve my problem.
But later my friends and I found this error message in ~/site-packages/streamlit/server/server_util.py, and changed the parameter MESSAGE_LIMIT_SIZE to 200*1e6, making the write limit now 200 MB


Where to set page width when set into non widescreeen mode?

Question:
Hi,
please is there any parameter i could tweak, where I could set the app width?
Widescreen is too wide and compact mode is too small.
Take a look at how it looks on ultrawide.

screen3440×1306 137 KB

Context:
Hi @Dominik_Novotny
You can insert custom css using the style tag. The below snippet is from the Layout Experiments app in the gallery at awesome-streamlit.org 604
st.markdown(
        f"""
<style>
    .reportview-container .main .block-container{{
        max-width: {max_width}px;
        padding-top: {padding_top}rem;
        padding-right: {padding_right}rem;
        padding-left: {padding_left}rem;
        padding-bottom: {padding_bottom}rem;
    }}
    .reportview-container .main {{
        color: {COLOR};
        background-color: {BACKGROUND_COLOR};
    }}
</style>
""",
        unsafe_allow_html=True,
    )

Visit the gallery if you wan’t to see it in action and use a slider to experiment with the max-width setting.

image1962×1043 193 KB


Question: Is there a way to redirect from page app_1 to page app_2, and hide page app_1

Context:
Here’s a simple implementation of a multipage app that you can modify for your use. Hope this helps:
import streamlit as st
def App1page():
st.write(“Showing app 1”)
if st.button(“Return to Main Page”):
st.session_state.runpage = main_page
st.experimental_rerun()
def App2page():
st.write(“Showing app 2”)
if st.button(“Return to Main Page”):
st.session_state.runpage = main_page
st.experimental_rerun()
def main_page():
st.write(“This is my main menu page”)
btn1 = st.button(“Show App1”)
btn2 = st.button(“Show App2”)
if btn1:
    st.session_state.runpage = App1page
    st.session_state.runpage()
    st.experimental_rerun()

if btn2:
    st.session_state.runpage = App2page
    st.session_state.runpage()
    st.experimental_rerun()

if ‘runpage’ not in st.session_state:
st.session_state.runpage = main_page
st.session_state.runpage()


Problem using multipage with If and Multiselect Widget

Question:
Hi guys. I’m facing a problem while trying to create a multipage app through “if conditions”. First of all, i created a main.py file which contais only a fews lines of code plus a menu. In this menu there are a few options the user can select. After him selecting one of them, he is redirectioned to the page. In this new page i added a multiselect widget so he can have another three options.
This new page (he is) are located in another .py file named variables_control.
Resuming:
The main-page are in the figure below (which are in main.py). After i a select the option in the selectbox i go to another page which are in variables_control.py

11135×895 83.8 KB

Here in variables control page, there is a multiselect widget which i added, so the user can decide what he wants to do:

1923×477 17.4 KB

But after a selected an option in this widget he isn’t showing a message i created in case worked…
I’ll be waiting for a hand!!!

Context:
See if this link helps you.

    Page redirect Using Streamlit

    Here’s a simple implementation of a multipage app that you can modify for your use. Hope this helps:
import streamlit as st
def App1page():
st.write(“Showing app 1”)
if st.button(“Return to Main Page”):
st.session_state.runpage = main_page
st.experimental_rerun()
def App2page():
st.write(“Showing app 2”)
if st.button(“Return to Main Page”):
st.session_state.runpage = main_page
st.experimental_rerun()
def main_page():
st.write(“This is my main menu page”)
btn1 = st.button(“Show App1…

Cheers


Question:
When accessing my Streamlit app, via a Satellite Internet connection, I get “Please wait” then, after a while, a popup saying “Connection Error” - “Connection times out”. I do NOT have any issues connecting to and using this application via other forms internet access. And my co-workers can access it without issue.ScreenClip1715×637 37.4 KB

Note that this Steamlit app is running in a container in a GCP VM inside our VPN.
I tried the solutions provided at Symptom #2: The app says “Please wait…” forever 19 to no avail.
I also output all the streamlit configs but am unable to find any type of ‘timeout’ params etc.
I am guessing this has something to do with the high latency of Satellite internet.
Streamlit version=0.62.1

Any help with this will be greatly appreciated. Thanks.

Context:
Hi @dplutchok -
This sort of sounds like what this issue is describing:

      github.com/streamlit/streamlit

      Improve experience when end user has an intermittent connection.  147

        opened

        Jun 24, 2020

          karriebear

  Problem
Communications currently happen via a session opened through a websocket. There are cases when an user has an intermittent connection that...

    enhancement
    needs triage
    spec needed

Not sure if there are any other specifics that @karriebear might need, but she might be able to work with you to solve your solution and inform the larger solution.
Best,
Randy