Question:
Hello. I m learning cnn’s and i have missing point about MobileNet V2 base output shape.
I m using this link as my reference: Transfer learning and fine-tuning  |  TensorFlow Core 1
In the code I print the summary of base_model :
“base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=False,
weights=‘imagenet’)
base_model.summary()”
In last line of layers “output shape” is (None, 5, 5, 1280)
This is the output shape of base_model. So I expected to see (1,5,5,1280) shaped output for one image. However, when ı run:
" feature_batch = base_model(image)
print(feature_batch.shape)"
output is (32,5,5,1280) why there are 32 different layers in first dimension and why that 32 is not included in output shape of last layer?

Context:
murat_ersen_unal:

why that 32 is not included in output shape of last layer?

As far as I understand, while following the tutorial, running base_model.summary() on its own prints the string summary of the pre-trained MobileNetV2 network with no inputs fed into the model yet (e.g. no image batch tensor of shape (32, 160, 160, 3) has been used as an input), hence the last layer’s output shape of (None, 5, 5, 1280) and the first layer’s shape of (None, 160, 160, 3).

 murat_ersen_unal:

This is the output shape of base_model. So I expected to see (1,5,5,1280) shaped output for one image. However, when ı run:
" feature_batch = base_model(image)
print(feature_batch.shape)"
output is (32,5,5,1280) why there are 32 different layers in first dimension

You set the batch size initially here:
BATCH_SIZE = 32
IMG_SIZE = (160, 160)

train_dataset = tf.keras.utils.image_dataset_from_directory(train_dir,
                                                            shuffle=True,
                                                            batch_size=BATCH_SIZE,
                                                            image_size=IMG_SIZE)
...
# Instantiate the base model
base_model = tf.keras.applications.MobileNetV2(...)
...
image_batch, label_batch = next(iter(train_dataset))
# Pass the image batch (of 32) to the pre-trained MobileNetV2 `base_model()`
feature_batch = base_model(image_batch)
# Print the shape
print(feature_batch.shape)

which then outputs:
(32, 5, 5, 1280)

If you simply run the same code but without feature extraction:
...
image_batch, label_batch = next(iter(train_dataset))
image_batch.shape # Check the shape

then the shape of the tensor will be:
TensorShape([32, 160, 160, 3])

(where 32 is the batch size.)


Question:
My apologies as this wont have the specifics it should as I’m pressed for time and it was a few days ago when I went through the process so some details are starting to fade.
I started here: Get started with the USB Accelerator | Coral 3 however after getting to the part of installing ‘python3-pycoral’, I later learned that the hosted version of ‘pycoral’ is missing a function(s?)/methods necessary to run the example semantic segmentation script.  It was at that point that the recommended solution was to build from source to get the missing functions/methods and so I tried to build from: “https://github.com/google-coral/pycoral 2” which then led me down a different rabbit hole, where in the end the build failed with errors saying that such-and-such (more than one item) was listed more than once in the build files and thus couldn’t continue.
My question is: Is there a (current) guide anywhere that allows for the Coral TPU example scripts to be ran from a fully updated raspberry Pi 4?
EDIT: The missing module was ‘pycoral.adapters’ which this stackoverflow then recommended building from source: stackoverflow(dot)com/questions/67807906/installing-coral-edge-tpu-software-gives-me-modulenotfounderror-no-module-name  (url butchered because there’s a limit on links)

Context: Hi @IB_short Have you tried installing pycoral using Python 3.8, as the users suggest in python - Installing Coral Edge TPU software gives me "ModuleNotFoundError: No module named 'pycoral.adapters'" error - Stack Overflow 6 ?


Uploaded model: Error: Mismatch between the last dimension of model’s output shape (20) and number of words (2)

Question:
I have started exploring Tensorflow.js and have built a speech-commands example that is based on TensorFlow.js Speech Commands Model Demo 1
Creating and using a model using transfer learning works great. It is able to detect the new commands I say correctly.
What does not work is downloading a model and uploading it again.
When I download a model with 2 words in it (including background noise), and upload the file again, the countExamples() function of the transferRecognizer shows the correct words, so I have the idea the model has loaded correctly.
However as soon as I start using it, I get the following error
speech-commands.esm.js:17 Uncaught (in promise) Error: Mismatch between the last dimension of model’s output shape (20) and number of words (2).
at t. (speech-commands.esm.js:17)
Indeed, when I make a model with 20 words (including background noise), download it and upload it again, the model runs without generating this error, but this is not really what I want.
I have a hunch that the 20 is based on the default ‘18w’ that tensorflowjs provides.
Am I overlooking something? Should I set the number of words in an uploaded model for example?

Context:
I had that problem until I looked at https://github.com/googlecreativelab/teachablemachine-community/blob/master/snippets/markdown/audio/tensorflowjs/javascript.md 1
which passes undefined in the following:
const recognizer = speechCommands.create(
            "BROWSER_FFT", // fourier transform type, not useful to change
            undefined, // speech commands vocabulary feature, not useful for your models
            checkpointURL,
            metadataURL);

https://github.com/tensorflow/tfjs-models/blob/master/speech-commands/README.md 1 states the default is to add 18 words.


Object Detection Error : Faster R-CNN ResNet50 W/ GPU

Question:
My first posting on this forum. Please forgive me if I’m in the wrong place, and any suggestions for the proper way to ask would be appreciated.
I’m self-teaching Tensorflow Object Detection in hopes of being able to identify and count nesting birds from UAV images using available tutorials.
I am able to train and detect with all the various models in the Tensorflow Zoo using my tutorial base setup except the Faster R-CNN versions.
On any Faster R-CNN model I first get a new message :
“This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.”
Followed by a failure :
“raise ValueError(’{} is not supported. See model_builder.py for features '”
I suspect this comes up from some sort of a version error between the tutorial environment and the Faster R-CNN pre-trained model, but I don’t know where to start to find a path forward.
Here is the pre-trained model I’m using…
‘http://download.tensorflow.org/models/object_detection/tf2/20200711/faster_rcnn_resnet50_v1_640x640_coco17_tpu-8.tar.gz 4’
Any suggestions would be appreciated, including any recommendations of where I might find a consulting service to assist.
Thanks in advance

Context: Windows 10, NVidia 3080TI


Question:
Hi,
I am current testing out tensorflow decision forests. I’m struggling to evaluate how the performance changes vs. number of trees for a non-default metric (in this case PR-AUC).
Below is some code with my attempts.
from sklearn.datasets import load_diabetes
from sklearn.model_selection import train_test_split
import pandas as pd
import tensorflow as tf
import tensorflow_decision_forests as tfdf

train = load_diabetes()
X = pd.DataFrame(train['data'])
X['target'] = (pd.Series(train['target']) > 100).astype(int)
X_train, X_test = train_test_split(X)
train_ds = tfdf.keras.pd_dataframe_to_tf_dataset(X_train, label="target")
test_ds = tfdf.keras.pd_dataframe_to_tf_dataset(X_test, label="target")
pr_auc = tf.keras.metrics.AUC( curve='PR',)
tfdf_clf = tfdf.keras.GradientBoostedTreesModel()
tfdf_clf.compile(metrics=[pr_auc])
tfdf_clf.fit(train_ds, validation_data=test_ds,)

Now I get very useful training logs using
tfdf_clf.make_inspector().training_logs()
#[TrainLog(num_trees=1, evaluation=Evaluation(num_examples=None, accuracy=0.9005518555641174, loss=0.6005926132202148, rmse=None, ndcg=None, aucs=None)),
#TrainLog(num_trees=2, evaluation=Evaluation(num_examples=None, accuracy=0.9005518555641174, loss=0.5672071576118469, rmse=None, ndcg=None, aucs=None)),

But it doesn’t contain any info on PR-AUC vs. iterations
If I evaluate the model, it only persists PR-AUC at the end of training.
tfdf_clf.evaluate(test_ds)

1180/1180 [==============================] - 10s 8ms/step - loss: 0.0000e+00 - auc: 0.6832
I was wondering can anyone here help explain How I can find PR-AUC (on test data) vs. number of trees when using TFDF?

Context:
Hello Olivier,
TF-DF has two independent means of model evaluation: 1) The internal metrics / loss used to train the model configured with the task or loss hyper-parameters, and 2) the Keras metrics supplied with compile. There is currently no connection between the two sets of metrics.
tfdf_clf.make_inspector().training_logs() returns the former while tfdf_clf.evaluate() returns the latter.
As you noted, only the internal metrics will be given for each number of trees in the model. The Keras metrics will only be given for the entire model.
Currently, the only solution to your problem is to manually remove trees from the model using the model builder and to evaluate it (more precisely, to create a new model for each number of trees you want to evaluate).
I understand that this is not a user-friendly solution. If you want, you can create a feature request on the project’s github page 2 to help us prioritize it.
Cheers,
Mathieu


Exporter_main_v2.py ‘skipping full serialization of Keras layer’ error

Question:
Hello,
Whenever I try to convert a trained checkpoint to a SavedModel using ‘exporter_main_v2.py’, I get a warning that says WARNING:tensorflow:Skipping full serialization of Keras layer
This is causing trouble for later operations that I want to perform with the trained model.
Any thoughts on why this might be happening? Is there any certain arguments that I need to pass to this function for it to convert properly?
Thanks.

Context:
It’s impossible to answer this without more details.
Can you figure out which layer it’s complaining about? It might be a custom layer missing the .get_config/.from_config methods.
If your model can’t be loaded as a keras model (tf.keras.models.load_model) it can probably still be loaded as a saved_model with tf.saved_model.load. That doesn’t give you access to the layers, but it will let you use the reloaded model, if that’s what you’re trying to do.


Different Results for model.evaluate() compared to model()

Question:
Hi. I have trained a MobileNets model and in the same code used the model.evaluate() on a set of test data to determine its performance. This test is indicating nearly 97% accuracy. Here is the code that performs this.

import os
import tensorflow.keras as keras
from tensorflow.keras.applications import MobileNet
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import load_model
from tensorflow.keras.callbacks import ModelCheckpoint
image_size_y = 1056 # The height of one input image
image_size_x = 1920 # The width of one input image
Choose a width multiplier which changes the number of filters per layer
depth_mul = 1.0/8.0
Set input shape for color images
shape = (image_size_y, image_size_x, 3)
Import the MobileNet model and set input dimensions and hyperparameters.
model = MobileNet(input_shape=shape, alpha=depth_mul, weights=None, classes=2)
Setting up the data directory paths
BaseDir = os.path.join(‘path’,‘to’,‘directory’,‘containing’,‘data’)
train_dir = os.path.join(BaseDir,‘train’)
val_dir = os.path.join(BaseDir,‘val’)
test_dir = os.path.join(BaseDir,‘test’)
train_positive_dir = os.path.join(train_dir,‘positive’)
train_negative_dir = os.path.join(train_dir,‘negative’)
val_positive_dir = os.path.join(val_dir,‘positive’)
val_negative_dir = os.path.join(val_dir,‘negative’)
test_positive_dir = os.path.join(test_dir,‘positive’)
test_negative_dir = os.path.join(test_dir,‘negative’)
Define desired Batch Size
batchsize = 32
Only use data augmentation that generate images that could reasonably occur in real-world situation (just scale brightness a bit)
train_datagen = ImageDataGenerator(
rescale= 1./255,
brightness_range=[0.9,1.1]
)
valid_datagen = ImageDataGenerator(rescale = 1./255)
test_datagen = ImageDataGenerator(rescale = 1./255)
Create Data Generators for each group of data
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(image_size_x,image_size_y),
batch_size=batchsize,
class_mode=‘categorical’
)
validation_generator = valid_datagen.flow_from_directory(
val_dir,
target_size=(image_size_x,image_size_y),
batch_size=batchsize,
class_mode=‘categorical’
)
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(image_size_x,image_size_y),
batch_size=batchsize,
class_mode=‘categorical’
)
Compile the model for training
model.compile(
loss=‘categorical_crossentropy’,
optimizer=‘rmsprop’,
metrics = [‘accuracy’]
)
Save the model at every epoch, overwriting each time, so the final version after the last epoch will remain and can be tested
finalNetwork = os.path.join(‘path’,‘to’,‘MobileNetsModel.h5’)
mcf = ModelCheckpoint(finalNetwork)
Train the network
history = model.fit(
train_generator,
steps_per_epoch = 40646 // batchsize,
epochs = 20,
validation_data = validation_generator,
validation_steps = 5080 // batchsize,
callbacks = [mcf]
)
Evaluation on test data of the model after the final epoch of training
saved_model = load_model(finalNetwork)
_,test_acc = saved_model.evaluate(test_generator,verbose = 0)
print(“Final Model Accuracy = %.1f%%” % (100.0 *test_acc))
keras.backend.clear_session()

And then I created another piece of code to actually use the trained model, but it doesn’t seem to be working. I’m getting nearly 50% true positives and 50% false positives, so only 50% accuracy. Here is that code. Am I performing the inferences wrong in this code? Am I not saving or loading my model properly? Please help!

import os
from matplotlib import image
import tensorflow as tf
from tensorflow.keras.models import load_model
Load a model that was trained and saved
model = load_model(os.path.join(‘path’,‘to’,‘MobileNetsModel.h5’))
Set the directory containing the test images
datadir = os.path.join(‘directory’,‘containing’,‘jpgs’)
Get the filenames of all the test images
imgNames = os.listdir(datadir)
Make inferences using the provided model
for imgName in imgNames:
# Get the image
img = image.imread(os.path.join(datadir,imgName))

# Make an inference
input = tf.convert_to_tensor(img)
input = tf.image.resize(input,(1056,1920))
input = input[None,:,:,:]
input = input/255.0
output = model(input)
prob_pos = output.numpy()[0,0]*100
prob_neg = output.numpy()[0,1]*100

# Categorize inferences and output to console
if prob_pos >= prob_neg:
    print(imgName,' is positive')
else:
    print(imgName,' is negative')

Context:
Hi,
I tried to read all the code but I got lost (maybe I need to sleep a little bit more  )
can you try your data adapting this colab:  Retraining an Image Classifier  |  TensorFlow Hub 1
?


Question:
in this image classification tutorial with transfer learning and fine-tuning ( Transfer learning and fine-tuning | TensorFlow Core](Transfer learning and fine-tuning  |  TensorFlow Core 1), when the model summary is called it shows all the layers, however, all the mobilenet layers are condensed into one layer which reads:  mobilenetv2_1.00_160 (Functi (None, 5, 5, 1280)
model.summary()

Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_2 (InputLayer)         [(None, 160, 160, 3)]     0
_________________________________________________________________
sequential (Sequential)      (None, 160, 160, 3)       0
_________________________________________________________________
tf.math.truediv (TFOpLambda) (None, 160, 160, 3)       0
_________________________________________________________________
tf.math.subtract (TFOpLambda (None, 160, 160, 3)       0
_________________________________________________________________
mobilenetv2_1.00_160 (Functi (None, 5, 5, 1280)        2257984
_________________________________________________________________
global_average_pooling2d (Gl (None, 1280)              0
_________________________________________________________________
dropout (Dropout)            (None, 1280)              0
_________________________________________________________________
dense (Dense)                (None, 1)                 1281
=================================================================
Total params: 2,259,265
Trainable params: 1,281
Non-trainable params: 2,257,984
_________________________________________________________________

why does it do this and how can i make it call a summary that shows all layers including the ones in the condensed mobilenet summary layer?
basically i need to read all convoluted layers for a project but it current not find any.

Context:
You could use expand_nested but some bug was fixed recently:

      github.com/keras-team/keras

      `expand_nested` bug fix and changing `model.summary` style 2

      keras-team:master ← krishrustagi:changes

        opened

        Sep 12, 2021

          krishrustagi

          +42
          -11

    The bug fixing is based on PR #15251.
There were 2 bugs with the expand_nested…:

### Bug1:
![Screenshot from 2021-09-12 13-40-58](https://user-images.githubusercontent.com/54409969/132979071-039f13b1-f09e-4900-8dee-2626a4e3e074.png)

### After Changes Bug1:
![Screenshot from 2021-09-12 13-42-07](https://user-images.githubusercontent.com/54409969/132979105-06469b57-6da5-4a3b-b501-debfd6d5eef0.png)

### Bug2:
```python3
text_file = open('summary.txt', 'w');
def print_text(text, end = "\n"):
    print(text, end = end, file = text_file)
```
Use of end = `\n` additionally.  Without it, throws an error.

### After changes Bug2:
```python3
text_file = open('summary.txt', 'w');
def print_text(text):
    print(text, file = text_file)
````

**Also, I have tried to change the style of `model.summary` by replacing `_` with space that looks good and less chaotic with and without expand_nested**

### expand_nested=False
![Screenshot from 2021-09-12 13-49-31](https://user-images.githubusercontent.com/54409969/132979356-5b9cbd7d-b0d4-4c4e-9f0f-b74cbb382682.png)

### expand_nested=True
![Screenshot from 2021-09-12 13-48-49](https://user-images.githubusercontent.com/54409969/132979340-f79582fe-e007-45f9-a8d0-7055474deffe.png)

**Note:** I need to update the test cases also. But before that, please look into the changes.

So probably you could try with a TF 2.7rc release.


Convert rgba to rgb for all images in dataset

Question:
Hello! How do you create a loop to strip down the 4th colour channel in rgba images to rgb for the whole dataset?
  _tfio.experimental.color.rgba_to_rgb(
  img, name=None
  )_

I tried using the above cell to pre-process images but it yields a transformation for only one image at a time and the resultant shape is not saved for image processing.

Context: You can define the number of channels inside decoding function like tf.image.decode_image(image, channels=3) and use it when you are creating a dataset. It will convert 4-channel images into 3-channel tensors.


Bad file permissions on ‘packages.cloud.google.com’

Question: Don’t know if this is where I can report this issue but while following the guide here: (Python quickstart  |  TensorFlow Lite 1)  The step where you create a deb repo, the repo has bad permissions (either incorrect file permissions and/or SELinux context is wrong) and I can’t download anything: “Could not connect to packages.cloud.google.com:443 2 (142.250.72.78). - connect (111: Connection refused)”  I also tried ipv6, same error.

Context: I should have also added that specifically “/apt/pool” is where I suspect the permission issue: Index of /apt/pool/ 2  All other folders seem to have the correct permissions.


Question:
Hello
i would like to load my tflite model from a specific folder and not from the asset folder.
The TFLiteObjectDetectionAPIModel.create function doesn’t allow to do this. What is the method to init the tflite model outside of the asset folder ?
thanks

Context:
Hi @gael_cobert
Give me a link of the project/documentation that you see this function not working as you want.
Thanks


ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer` when using custom callback to plot conv layer feature maps

Question:
I’m trying to implement a custom callback to get the feature maps of each Conv2D layer in the network plotted in TensorBoard.
When I run the code in Example 1 I get the following error:
<ipython-input-44-b691dabedd05> in on_epoch_end(self, epoch, logs)
     28
     29         # 3) Build partial model
---> 30         partial_model = keras.Model(
     31             inputs=self.model.model.input,
     32             outputs=output_layers

ValueError: Output tensors of a Functional model must be the output of a TensorFlow `Layer` (thus holding past layer metadata). Found: <keras.engine.base_layer.Layer object at 0x000002773C631CA0>

which seams as if it can’t build the partial network, which is strange, because it succeeds when running is separately from the main thread.
Here is an example that illustrates the issue:
Example 1
import os
import io
import datetime as dt
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt

'''
You can adjust the verbosity of the logs which are being printed by TensorFlow
by changing the value of TF_CPP_MIN_LOG_LEVEL:
    0 = all messages are logged (default behavior)
    1 = INFO messages are not printed
    2 = INFO and WARNING messages are not printed
    3 = INFO, WARNING, and ERROR messages are not printed
'''
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

DEBUG = False

class ConvModel(keras.Model):

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

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

def find_sub_string(string: str, sub_string: str):
    return True if string.find(sub_string) > -1 else False

def get_file_type(file_name: str):
    file_type = None
    if isinstance(file_name, str):
        dot_idx = file_name.find('.')
        if dot_idx > -1:
            file_type = file_name[dot_idx + 1:]
    return file_type

def get_image_from_figure(figure):
    buffer = io.BytesIO()

    plt.savefig(buffer, format='png')

    plt.close(figure)
    buffer.seek(0)

    image = tf.image.decode_png(buffer.getvalue(), channels=4)
    image = tf.expand_dims(image, 0)
    return image

class ConvLayerVis(keras.callbacks.Callback):
    def __init__(self, X, figure_configs: dict, log_dir: str, log_interval: int):
        super().__init__()
        self.X_test = X

        n_dims = len(self.X_test.shape)
        assert 2 < n_dims < 5, f'The shape of the test image should be less than 5 and grater than 2, but current shape is {self.X_test.shape}'

        # In case the image is not represented as a tensor - add a dimension to the left for the batch
        if len(self.X_test.shape) < 4:
            self.X_test = np.reshape(self.X_test, (1,) + self.X_test.shape)

        self.file_writer = tf.summary.create_file_writer(log_dir)
        self.figure_configs = figure_configs
        self.log_interval = log_interval

    def on_training_begin(self, logs=None):
        pass

    def on_epoch_end(self, epoch, logs=None):
        # 1) Get the layers
        if epoch % self.log_interval == 0:
            # 1) Get the layers
            output_layer_tuples = [(idx, layer) for idx, layer in enumerate(self.model.model.layers) if find_sub_string(layer.name, 'conv2d') or find_sub_string(layer.name, 'max_pooling2d')]
            output_layers = [layer_tuple[1].output for layer_tuple in output_layer_tuples]

            # 2) Get the layer names
            conv_layer_name_tuples = [(layer_tuple[0], f'Layer #{layer_tuple[0]} - Conv 2D ') for layer_tuple in output_layer_tuples if find_sub_string(layer_tuple[1].name, 'conv2d')]
            max_pool_layer_name_tuples = [(layer_tuple[0], f'Layer #{layer_tuple[0]} - Max Pooling 2D') for layer_tuple in output_layer_tuples if find_sub_string(layer_tuple[1].name, 'max_pooling2d')]

            layer_name_tuples = (conv_layer_name_tuples + max_pool_layer_name_tuples)
            layer_name_tuples.sort(key=lambda x: x[0])

            layer_names = [layer_name_tuple[1] for layer_name_tuple in layer_name_tuples]

            # 3) Build partial model
            partial_model = keras.Model(
                inputs=model.model.input,
                outputs=output_layers
            )

            # 4) Get the feature maps
            feature_maps = partial_model.predict(self.X_test)

            # 5) Plot
            rows, cols = self.figure_configs.get('rows'), self.figure_configs.get('cols')
            for feature_map, layer_name in zip(feature_maps, layer_names):
                fig, ax = plt.subplots(rows, cols, figsize=self.figure_configs.get('figsize'))
                for row in range(rows):
                    for col in range(cols):
                        ax[row][col].imshow(feature_map[0, :, :, row+col], cmap=self.figure_configs.get('cmap'))
                fig.suptitle(f'{layer_name}')

                with self.file_writer.as_default():
                    tf.summary.image(f'{layer_name} Feature Maps', get_image_from_figure(figure=fig), step=epoch)

if __name__ == '__main__':
    print(tf.config.list_physical_devices('GPU'))

    # Load the data
    (X, y), (X_test, y_test) = cifar10.load_data()
    X, X_test = X.astype(np.float32) / 255.0, X_test.astype(np.float32) / 255.0
    n, w, h, c = X.shape[0], X.shape[1], X.shape[2], X.shape[3]
    n_test, w_test, h_test, c_test = X_test.shape[0], X_test.shape[1], X_test.shape[2], X_test.shape[3]

    print(f'''
Dataset Stats:
    Number of train images: {n}
    Dimensions:
        > Train:
            width = {w}, height = {h}, channels = {c}
        > Test:
            width = {w_test}, height = {h_test}, channels = {c_test}
    ''')

    # Model with keras.Sequential
    model = ConvModel(input_shape=(w, h, c))
    model.compile(loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), optimizer=keras.optimizers.Adam(learning_rate=3e-4), metrics=['accuracy'])

    log_dir = f'./logs/{dt.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}'

    callbacks = [
        keras.callbacks.TensorBoard(
            log_dir=log_dir,
            write_images=True
        ),
        ConvLayerVis(
            X=X[0],
            figure_configs=dict(rows=5, cols=5, figsize=(35, 35), cmap='gray'),
            log_dir=f'{log_dir}/train',
            log_interval=3
        )
    ]
    model.fit(
        X,
        y,
        batch_size=64,
        epochs=15,
        callbacks=callbacks
    )

Thanks in advance for any help regarding this issue.

Context:
Probably you could try something like this inside on_epoch_end():
outputs = []
for layer in output_layers:
    outputs.append(layer.output)

partial_model = keras.Model(
        inputs=self.model.model.input,
        outputs=outputs)


Question:
I am a newbie and was going through examples in book ISBN9781492032649.
in page 297.py it uses MINST 28x28 images to train the neural network using regression model. (example 1 pasted below). This took about 167 seconds for 30 epochs on radeon GPU.
But same MNIST images are performy far more slowly on CNN network but i thought CNN would be much more faster and efficient?
EXAMPLE1:

Using neural net to do a classification task.
import tensorflow as tf
import pandas as pd
import matplotlib as plt
from tensorflow import keras
print(tf.version)
print(keras.version)
CONFIG_ENABLE_PLOT=0
fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()
print("X_train_full.shape: ", X_train_full.shape)
print("X_train_full.dtype: ", X_train_full.dtype)
X_valid, X_train = X_train_full[:5000] / 255.0, X_train_full[5000:]/255.0
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
X_test = X_test / 255.0
class_names = [“T-shirt/top”,“Trouser”, “Pullover”, “Dress”, “Coat” , “Sandal”, “Shirt”, “Sneaker”,“Bad”,“Ankle boot”]
model=keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape = [28, 28]))
model.add(keras.layers.Dense(300, activation=“relu”))
model.add(keras.layers.Dense(100, activation=“relu”))
model.add(keras.layers.Dense(30, activation=“softmax”))
print("model summary: ", model.summary())
model.compile(loss=“sparse_categorical_crossentropy”, optimizer=“sgd”, metrics=[“accuracy”])
history=model.fit(X_train, y_train, epochs=30, validation_data=(X_valid, y_valid))
pd.DataFrame(history.history).plot(figsize=(8, 5))
if CONFIG_ENABLE_PLOT:
plt.pyplot.grid(True)
plt.pyplot.gca().set_ylim(0, 1)
plt.pyplot.show()
model.evaluate(X_test, y_test)
print("model layers: ", model.layers)
weights, biases  = model.layers[1].get_weights()
print("weights, biases (shapes): ", weights, biases, weights.shape, biases.shape)
model.save(“p297.h5”)
X_new = X_test[:3]
y_proba = model.predict(X_new)
print(y_proba.round(2))
y_pred = model.predict_classes(X_new)
print("y_pred: ", y_pred)
EXAMPLE2:

Using CNN to do a classification task.
import tensorflow as tf
import pandas as pd
import matplotlib as plt
import time
from tensorflow import keras
print(tf.version)
print(keras.version)
CONFIG_ENABLE_PLOT=0
fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()
X_train_full = X_train_full.reshape(-1, 28, 28, 1)
print("X_train_full.shape: ", X_train_full.shape)
print("X_train_full.dtype: ", X_train_full.dtype)
X_valid, X_train = X_train_full[:5000] / 255.0, X_train_full[5000:]/255.0
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
print("X_test shape: ", X_test.shape)
X_test = X_test.reshape(-1, 28, 28, 1)
X_test = X_test / 255.0
class_names = [“T-shirt/top”,“Trouser”, “Pullover”, “Dress”, “Coat” , “Sandal”, “Shirt”, “Sneaker”,“Bad”,“Ankle boot”]
‘’’
model=keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape = [28, 28]))
model.add(keras.layers.Dense(300, activation=“relu”))
model.add(keras.layers.Dense(100, activation=“relu”))
model.add(keras.layers.Dense(30, activation=“softmax”))
‘’’
model=keras.models.Sequential([
keras.layers.Conv2D(64, 7, activation=“relu”, padding=“same”, input_shape=[28, 28, 1]),
keras.layers.MaxPooling2D(2),
keras.layers.Conv2D(128, 3, activation=“relu”, padding=“same”),
keras.layers.Conv2D(128, 3, activation=“relu”, padding=“same”),
keras.layers.MaxPooling2D(2),
keras.layers.Conv2D(256, 3, activation=“relu”, padding=“same”),
keras.layers.Conv2D(256, 3, activation=“relu”, padding=“same”),
keras.layers.MaxPooling2D(2),
keras.layers.Flatten(),
keras.layers.Dense(128, activation=“relu”),
keras.layers.Dropout(0.5),
keras.layers.Dense(64, activation=“relu”),
keras.layers.Dropout(0.5),
keras.layers.Dense(10, activation=“softmax”)
])
print("model summary: ", model.summary())
model.compile(loss=“sparse_categorical_crossentropy”, optimizer=“sgd”, metrics=[“accuracy”])
history=model.fit(X_train, y_train, epochs=10, validation_data=(X_valid, y_valid))
pd.DataFrame(history.history).plot(figsize=(8, 5))
if CONFIG_ENABLE_PLOT:
plt.pyplot.grid(True)
plt.pyplot.gca().set_ylim(0, 1)
plt.pyplot.show()
model.evaluate(X_test, y_test)
print("model layers: ", model.layers)
weights, biases  = model.layers[1].get_weights()
print("weights, biases (shapes): ", weights, biases, weights.shape, biases.shape)
model.save(“p297.h5”)
X_new = X_test[:3]
y_proba = model.predict(X_new)
print(y_proba.round(2))
y_pred = model.predict_classes(X_new)
print("y_pred: ", y_pred)

Context:
Its running on CPU and that’s why its slow.  Deep leanring models don’t support AMD Radeon GPU.  They only work on Nvidia GPUs.  This is the same for both Tensorflow and PyTorch.
Because the network is training using CPU, Deep models (with many CNN layers as here) will be slower than shallow networks.
I would suggest you try this in Google’s Colab environment.  Please choose Runtime → Change RUntime as GPU in https://colab.research.google.com/ 3


Multiclass semantic segmentation model does not learn

Question:
Hi guys, I’m trying to learn Tensorflow (2.3.0 with Python 3.7) to train a UNET model. I’m using the PASCAL VOC 2012 dataset (21 classes). However, my model doesn’t learn, the accuracy is always around 0.75 and the loss is also stable.
My labels are converted to a tensor with pixel values between 0 and 21 and converted to one-hot (I’m not sure that’s the way it is, but that’s what I get). Images are normalized from 0 to 1. I use cross entropy as loss function and Adam optimizer.
I’ve tried to fix the problem in a number of ways, but I really don’t know what else to do.
can anybody help me? Thanks.
The code is below.
MODEL:
 from tensorflow.keras.layers import Input, Conv2D, BatchNormalization, Activation, MaxPool2D, UpSampling2D, Concatenate
from tensorflow.keras.models import Model

def conv_block(inputs, filters, pool = True):
    x = Conv2D(filters, 3, padding = "same")(inputs)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    x = Conv2D(filters, 3, padding = "same")(inputs)
    x = BatchNormalization()(x)
    x = Activation("relu")(x)

    if pool == True:
        p = MaxPool2D((2, 2))(x)
        return x, p
    else:
        return x

def build_unet(shape, num_classes):
    inputs = Input(shape)

    #Encoder
    x1, p1 = conv_block(inputs, 16, pool = True)
    x2, p2 = conv_block(p1, 32, pool = True)
    x3, p3 = conv_block(p2, 48, pool = True)
    x4, p4 = conv_block(p3, 64, pool = True)

    #Bridge

    b1 = conv_block(p4, 128, pool = False)

    #Decoder
    u1 = UpSampling2D((2, 2), interpolation = "bilinear")(b1)
    c1 = Concatenate()([u1, x4])
    x5 = conv_block(c1, 64, pool = False)

    u2 = UpSampling2D((2, 2), interpolation = "bilinear")(x5)
    c2 = Concatenate()([u2, x3])
    x6 = conv_block(c2, 48, pool = False)

    u3 = UpSampling2D((2, 2), interpolation = "bilinear")(x6)
    c3 = Concatenate()([u3, x2])
    x7 = conv_block(c3, 32, pool = False)

    u4 = UpSampling2D((2, 2), interpolation = "bilinear")(x7)
    c4 = Concatenate()([u4, x1])
    x8 = conv_block(c4, 16, pool = False)

    #output layer

    output = Conv2D(num_classes, 1, padding = "same", activation = "softmax")(x8) #sigmoid

    return Model(inputs, output)

    if __name__ == "__name__":
        model = build_unet((256, 256, 3), 21)
        model.summary()

TRAIN:
    import os
import numpy as np
import tensorflow as tf
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping

from data import load_data, tf_dataset
from model import build_unet

if __name__ == "__main__":
    """ Seeding """
    np.random.seed(42)
    tf.random.set_seed(42)

    """ Dataset """
    path = "VOC2012/"   #root
    (train_x, train_y), (valid_x, valid_y), (test_x, test_y) = load_data(path)
    print(f"Dataset: Train: {len(train_x)} - Valid: {len(valid_x)} - Test: {len(test_x)}")

    """ Hyperparameters """
    shape = (256, 256, 3)
    num_classes = 21
    lr = 1e-2
    batch_size = 2
    epochs = 100

    """ Model """
    model = build_unet(shape, num_classes)
    model.compile(loss="categorical_crossentropy", optimizer=tf.keras.optimizers.Adam(lr), metrics = ['CategoricalAccuracy']) #CategoricalCrossentropy

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

    train_steps = len(train_x)//batch_size
    valid_steps = len(valid_x)//batch_size

    callbacks = [
        ModelCheckpoint("model.h5", verbose=1, save_best_model=True),
        ReduceLROnPlateau(monitor="val_loss", patience=3, factor=0.5, verbose=1, min_lr=5e-8),
        EarlyStopping(monitor="val_loss", patience=8, verbose=1)
    ]

    model.fit(train_dataset,
        steps_per_epoch=train_steps,
        validation_data=valid_dataset,
        validation_steps=valid_steps,
        epochs=epochs,
        callbacks=callbacks
    )

DATA:
    import os
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow as tf
import cv2
from mapeamento import augment_data
from mapeamento import  rgb_to_mask

H = 256
W = 256
factor = 8
batch = 32

def process_data(data_path, file_path):
    df = pd.read_csv(file_path, sep=" ", header=None)
    names = df[0].values
    #names = names[0:int(len(names)/factor)]

    images = [os.path.join(data_path, f"JPEGImages/{name}.jpg") for name in names]
    masks = [os.path.join(data_path, f"SegmentationClass/{name}.png") for name in names]

    return images, masks

def load_data(path):
    train_valid_path = os.path.join(path, "ImageSets/Segmentation/trainval.txt")
    test_path = os.path.join(path, "ImageSets/Segmentation/val.txt")

    train_x, train_y = process_data(path, train_valid_path)
    test_x, test_y = process_data(path, test_path)

    train_x, valid_x = train_test_split(train_x, test_size=0.15, random_state=42)
    train_y, valid_y = train_test_split(train_y, test_size=0.15, random_state=42)

    return (train_x, train_y), (valid_x, valid_y), (test_x, test_y)

def read_image(x):
    x = cv2.imread(x, cv2.IMREAD_COLOR)
    x = cv2.resize(x, (W, H), interpolation = cv2.INTER_NEAREST)
    x = x/255.0
    x = x.astype(np.float32)
    return x

def read_mask(x):
    x = cv2.imread(x, cv2.IMREAD_UNCHANGED)
    x = cv2.cvtColor(x, cv2.COLOR_BGR2RGB) #masks colormap is RGB
    x = cv2.resize(x, (W, H), interpolation = cv2.INTER_NEAREST)
    x =  rgb_to_mask(x)
    #x = x.astype(np.int32)
    return x

def tf_dataset(x, y, batch=batch):
    dataset = tf.data.Dataset.from_tensor_slices((x, y))
    dataset = dataset.shuffle(buffer_size=5000)
    dataset = dataset.map(preprocess)
    dataset = dataset.batch(batch)
    dataset = dataset.repeat()
    dataset = dataset.prefetch(8)
    return dataset

def preprocess(x, y):
    def f(x, y):
        x = x.decode()
        y = y.decode()

        image = read_image(x)
        mask = read_mask(y) #uint8
        #print(f"\nMAX UINT8: {mask.max()} - MIN UINT8: {mask.min()}")

        image, mask = augment_data(image, mask)
        mask = mask.astype(np.int32)
        #print(f"\nMAX INT32: {mask.max()} - MIN INT32: {mask.min()}")

        return image, mask

    image, mask = tf.numpy_function(f, [x, y], [tf.float32, tf.int32])
    mask = tf.one_hot(mask, 21, dtype=tf.int32)
    image.set_shape([H, W, 3])
    mask.set_shape([H, W, 21])

    return image, mask

if __name__ == "__main__":
    path = "VOC2012/"   #root
    (train_x, train_y), (valid_x, valid_y), (test_x, test_y) = load_data(path)
    print(f"Dataset: Train: {len(train_x)} - Valid: {len(valid_x)} - Test: {len(test_x)}")

    dataset = tf_dataset(train_x, train_y, batch=batch)
    for x, y in dataset:
        print(x.shape, y.shape) ## (b, 256, 256, 3), (b, 256, 256, 21)

TRANSFORMS:
    import numpy as np
from albumentations import HorizontalFlip, GridDistortion, OpticalDistortion, ChannelShuffle, CoarseDropout, CenterCrop, Crop, Rotate
import cv2
#from numba import jit
#import random

#@jit
def rgb_to_mask(img):

    ClassIndex = {0:[0, 0, 0], 1:[128, 0, 0], 2:[0, 128, 0], 3:[128, 128, 0], 4:[0, 0, 128], 5:[128, 0, 128],
                     6:[0, 128, 128], 7:[128, 128, 128], 8:[64, 0, 0], 9:[192, 0, 0], 10:[64, 128, 0],
                     11:[192, 128, 0], 12:[64, 0, 128], 13:[192, 0, 128], 14:[64, 128, 128], 15:[192, 128, 128],
                     16:[0, 64, 0], 17:[128, 64, 0], 18:[0, 192, 0], 19:[128, 192, 0], 20:[0, 64, 128]}

    h, w, c = img.shape
    seg_labels = np.zeros((h, w), dtype = np.uint8)

    mask_0  = (img == ClassIndex[0]).all(axis=2)
    mask_1  = (img == ClassIndex[1]).all(axis=2)
    mask_2  = (img == ClassIndex[2]).all(axis=2)
    mask_3  = (img == ClassIndex[3]).all(axis=2)
    mask_4  = (img == ClassIndex[4]).all(axis=2)
    mask_5  = (img == ClassIndex[5]).all(axis=2)
    mask_6  = (img == ClassIndex[6]).all(axis=2)
    mask_7  = (img == ClassIndex[7]).all(axis=2)
    mask_8  = (img == ClassIndex[8]).all(axis=2)
    mask_9  = (img == ClassIndex[9]).all(axis=2)
    mask_10 = (img == ClassIndex[10]).all(axis=2)
    mask_11 = (img == ClassIndex[11]).all(axis=2)
    mask_12 = (img == ClassIndex[12]).all(axis=2)
    mask_13 = (img == ClassIndex[13]).all(axis=2)
    mask_14 = (img == ClassIndex[14]).all(axis=2)
    mask_15 = (img == ClassIndex[15]).all(axis=2)
    mask_16 = (img == ClassIndex[16]).all(axis=2)
    mask_17 = (img == ClassIndex[17]).all(axis=2)
    mask_18 = (img == ClassIndex[18]).all(axis=2)
    mask_19 = (img == ClassIndex[19]).all(axis=2)
    mask_20 = (img == ClassIndex[20]).all(axis=2)

    seg_labels[mask_0] = 0
    seg_labels[mask_1] = 1
    seg_labels[mask_2] = 2
    seg_labels[mask_3] = 3
    seg_labels[mask_4] = 4
    seg_labels[mask_5] = 5
    seg_labels[mask_6] = 6
    seg_labels[mask_7] = 7
    seg_labels[mask_8] = 8
    seg_labels[mask_9] = 9
    seg_labels[mask_10] = 10
    seg_labels[mask_11] = 11
    seg_labels[mask_12] = 12
    seg_labels[mask_13] = 13
    seg_labels[mask_14] = 14
    seg_labels[mask_15] = 15
    seg_labels[mask_16] = 16
    seg_labels[mask_17] = 17
    seg_labels[mask_18] = 18
    seg_labels[mask_19] = 19
    seg_labels[mask_20] = 20

    return seg_labels

def augment_data(images, masks):
    H = 256
    W = 256

    aug = HorizontalFlip(p=1.0)
    augmented = aug(image=images, mask=masks)
    x1 = augmented["image"]
    y1 = augmented["mask"]

    aug = ChannelShuffle(p=1)
    augmented = aug(image=x1, mask=y1)
    x3 = augmented['image']
    y3 = augmented['mask']

    aug = CoarseDropout(p=1, min_holes=3, max_holes=10, max_height=32, max_width=32)
    augmented = aug(image=x3, mask=y3)
    x4 = augmented['image']
    y4 = augmented['mask']

    aug = Rotate(limit=45, interpolation = cv2.INTER_NEAREST, p=1.0)
    augmented = aug(image=x4, mask=y4)
    x5 = augmented["image"]
    y5 = augmented["mask"]

    try:
        """ Center Cropping """
        aug = CenterCrop(H, W, p=1.0)
        augmented = aug(image=x5, mask=y5)
        i = augmented["image"]
        m = augmented["mask"]
        return i, m

    except Exception as e:
        return i, m

Context: Can anyone help? Thanks.


Performance overhead of tensorflow custom ops

Question: Hi all – I have written some tensorflow custom ops that replace stock tensorflow kernels. When benchmarking the kernels in isolation in a C++ test bench my kernels have comparable performance with tensorflow kernels. However when I use the custom op framework to use my kernels in tensorflow serving, it becomes a lot slower. Does anybody here have any ideas why this could be?

Context: Have you benchmarked your kernel in isolation also on TF serving or in a model?


Question:
Hi,
This is my code and the error I get when I use “from_tensor_slice”. Anyone knows the solution (Keras clear_output doesn’t help)?
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split

tf.compat.v1.disable_eager_execution()

df = pd.read_csv('insurance.csv')
X = pd.get_dummies(df, columns = ['sex', 'smoker', 'region'])
y = X.pop('charges')

ds = tf.data.Dataset.from_tensor_slices((X.values, y.values))

The error:
Traceback (most recent call last):
File “C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\training\tracking\tracking.py”, line 269, in del
File “C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\framework\ops.py”, line 4011, in as_default
AttributeError: ‘NoneType’ object has no attribute ‘get_controller’

Context:
The given code is working as expected. Looks like data has some NAN values. Make sure address all the NAN values before converting it to tf.data.Dataset.

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split
tf.compat.v1.disable_eager_execution()
#df = pd.read_csv(‘insurance.csv’)
#X = pd.get_dummies(df, columns = [‘sex’, ‘smoker’, ‘region’])
#y = X.pop(‘charges’)
data = {‘sex’:[‘female’, ‘male’, ‘female’, ‘male’],
‘Age’:[‘yes’, ‘no’, ‘no’,‘yes’ ],
‘region’:[‘USA’,‘India’,‘India’,‘USA’]}
data1 = {‘charges’:[100,200,300,400]}
Create DataFrame
X = pd.DataFrame(data)
y = pd.DataFrame(data1)
ds = tf.data.Dataset.from_tensor_slices((X.values, y.values))

Output

<TensorSliceDataset shapes: ((3,), (1,)), types: (tf.string, tf.int64)>


Question:
Hello everyone!
I have created and trained a model for classifying spam in Russian SMS, but I do not understand how to integrate it into Android. I want to create an application so that a classification is performed on the click of a button. Can anyone help me please?

Context:
Hi @11140
What documentation have you tried so far?


Question:
I would like to see (preferably in the log/on screen) how much time each OP in my model takes. Alternativaly, or better, that each OP in the model/graph will log the time when it starts its execution.
Is that possible with TF1?

Context:
TF1 is ETA, you could use our profiler with TF2:

      TensorFlow

Optimize TensorFlow performance using the Profiler  |  TensorFlow Core


Using tflite model for inference using python

Question:
Hi all,
I have a custom trained object detection model (ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8) trained using tensorflow2 API. I have converted to tflite (post training quantization), using the steps mentioned here:
https://community.nxp.com/t5/eIQ-Machine-Learning-Software/TF2-Object-Detection-API-Converting-mobilenet-SSD-models-into/td-p/1345027
The convertion steps are as follows:
import tensorflow as tf
TEST_DIR = "/home/nxf47724/data/code/python/model_convert"
IMAGE_SIZE = 300

# A generator that provides a representative dataset
def representative_data_gen():
  dataset_list = tf.data.Dataset.list_files(TEST_DIR + '/quante/*')
  for i in range(100):
    image = next(iter(dataset_list))
    image = tf.io.read_file(image)
    image = tf.io.decode_jpeg(image, channels=3)
    image = tf.image.resize(image, [IMAGE_SIZE, IMAGE_SIZE])
    image = tf.cast(image / 255., tf.float32)
    image = tf.expand_dims(image, 0)
    yield [image]

#converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter = tf.lite.TFLiteConverter.from_saved_model('/home/nxf47724/data/code/python/study/models/ssd_mobilenet_v2_320x320_coco17_tpu-8/exported_tflite/saved_model/',signature_keys=['serving_default'])
#converter.experimental_new_converter = False
# This enables quantization
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# This sets the representative dataset for quantization
converter.representative_dataset = representative_data_gen
# This ensures that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# For full integer quantization, though supported types defaults to int8 only, we explicitly declare it for clarity.
converter.target_spec.supported_types = [tf.int8]
# These set the input and output tensors to uint8 (added in r2.3)
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model = converter.convert()

with open('/home/python/study/models/ssd_mobilenet_v2_320x320_coco17_tpu-8/exported_tflite/ssd_mobilenet_v2_quant.tflite', 'wb') as f:
  f.write(tflite_model)

when I am using it in inference, I am getting the classes and scores as below:
classes [128 128 128 128 255 128 128 128 128 128]
scores [255 255 255 255 255 255 255 255 255 255]
and the bounding box values are also not coming properly
How to use int8 quantized model for inference in python? what are the correct preprocessing steps to be followed? and what is the reason for getting output in
128 and 255 format

Context:
Sorry, I don’t know that tutorial but given you have the data to train the model, I’d try this tutorial: Object Detection with TensorFlow Lite Model Maker 2
or try to adapt it.


Difference in performance with `keras.Sequential` in `keras.Model` subclass

Question:
I’m running into a performance difference while running the same model with and without the keras.Sequential object definition inside a custom class which inherits from keras.Model.
The following code demonstrates the issue:
Code for the model which uses keras.Sequential:
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.datasets import cifar10

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

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

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

Code for the model which does not use keras.Sequential:
class ConvModel_2(keras.Model):
    def __init__(self, input_shape):
        super().__init__()
        self.input_image_shape = input_shape
        self.inp = layers.Input(shape=input_shape)
        self.c2d_32 = layers.Conv2D(32, 3)
        self.bnrm_1 = layers.BatchNormalization()
        self.mp_1 = layers.MaxPool2D()
        self.c2d_64 = layers.Conv2D(64, 5)
        self.bnrm_2 = layers.BatchNormalization()
        self.mp_2 = layers.MaxPool2D()
        self.c2d_128 = layers.Conv2D(128, 3, kernel_regularizer=keras.regularizers.l2(0.01))
        self.bnrm_3 = layers.BatchNormalization()
        self.flt = layers.Flatten()
        self.dns_64 = layers.Dense(64, activation='relu', kernel_regularizer=keras.regularizers.l2(0.01))
        self.do = layers.Dropout(0.5)
        self.dns_10  =layers.Dense(10)

    def call(self, inputs):
        x = self.c2d_32(inputs)
        x = self.bnrm_1(x)
        x = keras.activations.relu(x)
        x = self.mp_1(x)

        x = self.c2d_64(x)
        x = self.bnrm_2(x)
        x = keras.activations.relu(x)
        x = self.mp_2(x)

        x = self.c2d_128(x)
        x = self.bnrm_3(x)
        x = keras.activations.relu(x)

        x = self.flt(x)
        x = self.dns_64(x)

        x = self.do(x)

        return self.dns_10(x)

    def model(self):
        x = keras.Input(shape=self.input_image_shape)
        return keras.Model(inputs=[x], outputs=self.call(x))

Main:
if __name__=='__name__':
    print(tf.config.list_physical_devices('GPU'))

    # Load the data
    (X, y), (X_test, y_test) = cifar10.load_data()
    X, X_test = X.astype(np.float32) / 255.0, X_test.astype(np.float32) / 255.0
    w, h, c = X.shape[1], X.shape[2], X.shape[3]
    print(w, h, c)

    # Model with keras.Sequential
    model_1 = ConvModel_1(input_shape=(w, h, c))
    model_1.compile(loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), optimizer=keras.optimizers.Adam(learning_rate=3e-4), metrics=['accuracy'])
    model_1.fit(X, y, batch_size=64, epochs=30)
    model.fit(X, y, batch_size=64, epochs=15)
    model_1.evaluate(X_test, y_test, batch_size=64)
    print(model_1.mdl.summary())

    # Model without keras.Sequential
    model_2 = ConvModel_2(input_shape=(w, h, c))
    model_2.compile(loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), optimizer=keras.optimizers.Adam(learning_rate=3e-4), metrics=['accuracy'])
    model_2.fit(X, y, batch_size=64, epochs=30)
    model_2.fit(X, y, batch_size=64, epochs=15)
    model_2.evaluate(X_test, y_test, batch_size=64)
    print(model_2.model().summary())

so after I fit both models for 30 epochs on CIFAR10 dsataset, the first model (i.e., the one where I use the keras.Sequential object to build the model) performes significantly worse then the second model (i.e., the one where I do not use the keras.Sequential object to build it) model:
For ConvModel_1:
Train:

Epoch 30/30
782/782 [==============================] - 13s 16ms/step - loss: 1.0373 - accuracy: 0.68

---

Test:

157/157 [==============================] - 1s 7ms/step - loss: 1.2568 - accuracy: 0.61

For ConvModel_2:
Train:

Epoch 30/30
782/782 [==============================] - 13s 17ms/step - loss: 0.6688 - accuracy: 0.83

---

Test:

157/157 [==============================] - 2s 8ms/step - loss: 1.0541 - accuracy: 0.72

This doesn’t make sense to me, as the models are identical (besides the input layers, which has no trainable parameters anyway):
ConvModel_1:
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv2d (Conv2D)              (None, 30, 30, 32)        896
_________________________________________________________________
batch_normalization (BatchNo (None, 30, 30, 32)        128
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32)        0
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 11, 11, 64)        51264
_________________________________________________________________
batch_normalization_1 (Batch (None, 11, 11, 64)        256
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 5, 5, 64)          0
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 3, 3, 128)         73856
_________________________________________________________________
batch_normalization_2 (Batch (None, 3, 3, 128)         512
_________________________________________________________________
flatten (Flatten)            (None, 1152)              0
_________________________________________________________________
dense (Dense)                (None, 64)                73792
_________________________________________________________________
dropout (Dropout)            (None, 64)                0
_________________________________________________________________
dense_1 (Dense)              (None, 10)                650
=================================================================
Total params: 201,354
Trainable params: 200,906
Non-trainable params: 448
_________________________________________________________________

ConvModel_2:
Model: "model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_3 (InputLayer)         [(None, 32, 32, 3)]       0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 30, 30, 32)        896
_________________________________________________________________
batch_normalization_3 (Batch (None, 30, 30, 32)        128
_________________________________________________________________
tf.nn.relu (TFOpLambda)      (None, 30, 30, 32)        0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 15, 15, 32)        0
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 11, 11, 64)        51264
_________________________________________________________________
batch_normalization_4 (Batch (None, 11, 11, 64)        256
_________________________________________________________________
tf.nn.relu_1 (TFOpLambda)    (None, 11, 11, 64)        0
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 5, 5, 64)          0
_________________________________________________________________
conv2d_5 (Conv2D)            (None, 3, 3, 128)         73856
_________________________________________________________________
batch_normalization_5 (Batch (None, 3, 3, 128)         512
_________________________________________________________________
tf.nn.relu_2 (TFOpLambda)    (None, 3, 3, 128)         0
_________________________________________________________________
flatten_1 (Flatten)          (None, 1152)              0
_________________________________________________________________
dense_2 (Dense)              (None, 64)                73792
_________________________________________________________________
dropout_1 (Dropout)          (None, 64)                0
_________________________________________________________________
dense_3 (Dense)              (None, 10)                650
=================================================================
Total params: 201,354
Trainable params: 200,906
Non-trainable params: 448
_________________________________________________________________

Just to note: I’m aware of the fact that each time the weights are randomly initialized, but the behavior described above is rather consistent over 10 runs, which renders this possibility quite not likely.
What am I missing here?
Thanks in advance for any help.

Context:
Michael_Sidorov:

Just to note: I’m aware of the fact that each time the weights are randomly initialized, but the behavior described above is rather consistent over 10 runs, which renders this possibility quite not likely.

You can improve the reproducibility with:

      keras.io

Keras documentation: Keras FAQ

Also I don’t see the relu activations in the first sequential model.


Question: hello, I’m trying to run a pose estimation model using TF, the problem I have is that the image is too large so the frame per second decrease to only 3 (I need at least 5\6) is there a way that I could change the size of the image without it harming the pose estimation? (thought of rescaling the image but don’t know how to do that)

Context:
You can use tf.image.resize_with_pad  |  TensorFlow Core v2.6.0 2
It will resize the image to the desired dimensions without distorting the contents.


Clear the graph and free the GPU memory in Tensorflow 2

Question:
I’m training multiple models sequentially, which will be memory-consuming if I keep all models without any cleanup. However, I am not aware of any way to the graph and free the GPU memory in Tensorflow 2.x. Is there a way to do so?

What I’ve tried but not working
tf.keras.backend.clear_session does not work in my case as I’ve defined some custom layers
tf.compat.v1.reset_default_graph does not work either.

Context:
We had another thread about this at:

    Releasing memory after GPU usage General Discussion

    Can you check clear_session() doesn't clear memory from GPU · Issue #9379 · keras-team/keras · GitHub ?
In this thread you can try to close CUDA in python with Numba


How to build 32bit tensorflow on Windows?

Question: hi,is anyone tell me how to build 32bit tensorflow on Windows?version 1.12.0 is just fine.

Context:
We no longer build 64bit TF. The code assumes 64 bits in several places, so building from source might still lead into errors.
If you want to build from source: Build from source on Windows  |  TensorFlow 2


Tensorflow Certification Team Email Problem

Question:
image811×751 29.8 KB

I have received the passed result from Tensorflow Certification Result and it has been 10 days since then but I still got no mail from Google Tensorflow Team.
Both the emails tensorflow-certificate-team@google.com and tensorflow-certificate-support@google.com are alerting that they do not exist at all. Equivalently, I cannot send emails to them.
How can I communicate with the Google Tensorflow Team?

Context: Please try again emailing tensorflow-certificate-support@google.com, this email is the right one.


Question:
Hi, I trained a CNN (small UNet) in Tensorflow 2.4 and Python 3.8 and with CUDA support. The model is working well and fast in Python, but I need to build an executalbe program from it, so a PC can execute it WITHOUT having TF and Python installed. The target device runs on Windows 10 and has CUDA 10.2 installed.  What is more, the executable program should not exceed a size of ~100MB and needs to support CUDA.
Is this even possible and what is the best way to build such an executable program?

Context:
If you need to use CUDA and c++ have you tried to use the Nvidia runtime TensorRT?
https://docs.nvidia.com/deeplearning/tensorrt/developer-guide/index.html 1
https://docs.nvidia.com/deeplearning/tensorrt/install-guide/index.html
Or you can find an example using directly libtensorflow at:

      Medium – 12 Oct 20

Using the new Tensorflow 2.x C++ API for object detection (inference) 1

  Serving Tensorflow Object Detection models in C++

    Reading time: 3 min read

/cc @markdaoust  do you know if we have any other inference example on GPU and Windows? As we have already another request at:

    Basiс things to deploy the model on windows C++ in Visual Studio General Discussion

    Hi, I have a model (autoencoder) it works well in python. Now I need to embed it into the existing c++ project on Windows,
After reading the site, I haven’t yet the basic plan of how to do It. Do I have to Install TF2 with basel and use TF2 C++API ? I can’t find anything similar to .predict in python. Or do I have to use c_api? Can somebody explain to me the common practice?
Do I have to Use c_api, or C++ api? Or I have to use TFLite for inference?
Thanks!


Basiс things to deploy the model on windows C++ in Visual Studio

Question:
Hi, I have a model (autoencoder) it works well in python. Now I need to embed it into the existing c++ project on Windows,
After reading the site, I haven’t yet the basic plan of how to do It. Do I have to Install TF2 with basel and use TF2 C++API ? I can’t find anything similar to .predict in python. Or do I have to use c_api? Can somebody explain to me the common practice?
Do I have to Use c_api, or C++ api? Or I have to use TFLite for inference?
Thanks!

Context: See also: Creating tensorflow executable 16


Question:
need help
create model
Task beloow

input ‘1zS’ string 3 characters // character is encoded 58
output ‘65’ line 2 characters // character is encoded 58
conditionally there are 10,000 lines
input file
1zS; 65 # 1zS input; 65 output
Ad9; 1S # Ad9 input; 1S output
task we load =>  ‘tdS’ predict ‘Gh’   for example
more information
https://github.com/laricoins/btcpredict

Context: As it is something very specific to that repository I suggest you to open a ticket directly there for support.


Question:
Hi all,
I have recently been testing various workflows for optimising inference in production. The non-deprecated workflows that I have found are TF-TRT and conversion to .onnx.
When attempting to convert a Tensorflow 2.6 SavedModel format model using the guidelines published here: Accelerating Inference In TF-TRT User Guide :: NVIDIA Deep Learning Frameworks Documentation 3
I get the following error:
Could not load dynamic library ‘libnvinfer.so.7’
I have a recent version of Cuda (11.4) with up to date Cudnn and TensorRT (v8). The module libnvinfer is installed but at version 8. Does this error indicate that the conversion command trt.TrtGraphConverterV2() only supports tensorRT version8?
Thanks for any help you can provide.

Context:
If I remember correctly TensorRT was breaking API so I think that you need to use TensorRT 7:

      github.com/tensorflow/tensorflow

      Compilation fails on Ubuntu 20.04 when using TensorRT 8.  9

        opened

        May 13, 2021

          feranick

        stat:awaiting tensorflower
        type:bug
        subtype: ubuntu/linux
        comp:gpu:tensorrt
        TF 2.5

    **System information**
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04…): Ubuntu 20.04
- Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: N/A
- TensorFlow installed from (source or binary): binary
- TensorFlow version: 2.4.1, 2.5, etc
- Python version: 3.8
- Installed using virtualenv? pip? conda?: no, built from source
- Bazel version (if compiling from source): 3.1 (for TF 2.4.1), 3.7.2 (for TF 2.5.0-rcx)
- GCC/Compiler version (if compiling from source): gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
- CUDA/cuDNN version: Cuda 11.1, cudnn8 (8.0.5.39-1+cuda11.1) or Cuda-11-2, libcudnn 8.1.1, 8.2,
- GPU model and memory: GTX-1080ti
- TensorRT (crucial): 8.0.0-1+cuda11.0, or 8.0.0-1+cuda11.3

**Describe the problem**
When compiling with support for TensorRT 8 (via libnvinfer8), compilation fails (log is below).

**Provide the exact sequence of commands / steps that you executed before running into the problem**
When configuring the build, make sure you build with TensorRT support, and make sure TensorRT version 8 is selected. Build TF as usual. Compilation will fail.

If you install  TensorRT version 7 manually (from debs available for Ubuntu 18.04), compilation will complete just fine.

**Any other info / logs**
Relevant error:
`C++ compilation of rule '//tensorflow/compiler/tf2tensorrt:tensorrt_stub' failed (Exit 1): crosstool_wrapper_driver_is_not_gcc failed: error executing command`

`In file included from bazel-out/k8-opt/bin/external/local_config_tensorrt/_virtual_includes/tensorrt_headers/third_party/tensorrt/NvInfer.h:54,
                 from tensorflow/compiler/tf2tensorrt/stub/nvinfer_stub.cc:17:
bazel-out/k8-opt/bin/external/local_config_tensorrt/_virtual_includes/tensorrt_headers/third_party/tensorrt/NvInferRuntime.h:2264:51: note: from previous declaration 'nvinfer1::IPluginRegistry* getPluginRegistry() noexcept'
 2264 | extern "C" TENSORRTAPI nvinfer1::IPluginRegistry* getPluginRegistry() noexcept;`

Full log here:
[gesdm-tf2.5.0rc3-error.txt](https://github.com/tensorflow/tensorflow/files/6469944/gesdm-tf2.5.0rc3-error.txt)

But Nvidia has a DRAFT PR for TensorRT 8.2 that you can follow at:

      github.com/tensorflow/tensorflow

      Add Support for TensorRT 8.2 22

      tensorflow:master ← jhalakp-nvidia:trt-8.2

        opened

        Oct 12, 2021

          jhalakp-nvidia

          +282
          -42

    1. Add stub files for TensorRT 8.2
2. Fix accuracy regression with MobilenetV2 …i.e. set dynamic range for (-6, 6)
3. Limit GPU memory utilization for TFTRT Integration tests
4. Fix incorrect usage of TRT version to string API
5. TensorRT model_tests update
- Add separate perf tolerance for FP32, FP16, and INT8
- Add separate accuracy tolerance for FP32, FP16, and INT8
- Add functionality to dump histograms for better accuracy validation

CC: @DEKHTIARJonathan, @tfeher , and @bixia1


I keep getting a call error when trying to run a model.predict

Question:
This is the error I’ve been getting:
Exception has occurred: NotImplementedError       (note: full exception trace is shown but execution is paused at: tf__predict_function)
When subclassing the Model class, you should implement a call method.
I’m not quite sure what to do. The model I’m trying to run the model.predict is tensorflow.python.saved_model.load.Loader._recreate_base_user_object.._UserObject. There’s one parameter in the .predict() function: a numpy array. I’m not sure if the error is in the loading or something else.

Context: Can you share a minimal Colab/Gist to reproduce your error?


Question:
I’m using python 3.10 and I’m trying to install Tensorflow it always shows an error

Screenshot 2021-10-26 1214051105×153 8.57 KB

What is the solution for this error ?

Context:
If you see the list,  also on the next 2.7.0 release, we will have wheels up to python 3.9

      PyPI

tensorflow 9

  TensorFlow is an open source machine learning framework for everyone.

For the python 3.10 roadmap check:
https://discuss.tensorflow.org/t/support-python-3-10/


Tensorflow and CUDA support for latest NVIDA A5000 (Ampere GPU))

Question:
[ISSUE]:

Tensorflow 2.4 and 1.15 support for NVIDIA RTX  A5000 GPU.

[DESCRIPTION]:
I am planning to buy Nvidia RTX A5000 (Ampere Architecture) GPU for training models. However I am concerned if I will be able to run both  tensorflow 1.15 and 2.4 model in this GPU.
I have read that Ampere architecture only supports nvidia-driver versions above 450.36.06+ and cuda versions CUDA 11.
Reference link : CUDA Compatibility :: GPU Deployment and Management Documentation 19
Since Tensorflow 2.4 can be installed with CUDA versions >= 11, I think this might not be a problem. However since tensorflow 1.15 requires CUDA 10 and Ampere cards does not support CUDA 10, I am worried if I can run my tensorflow 1.15 model.
Can you please help me on this.
I want to confirm if the RTX A5000 GPU will support both Tensorflow 1.15 and 2.4.
Thanks in advance!

Context:
github.com

tensorflow-windows-wheel/1.15.4+nv20.12/py38/CPU+GPU at master ·... 33

  master/1.15.4+nv20.12/py38/CPU+GPU

  Tensorflow prebuilt binary for Windows. Contribute to fo40225/tensorflow-windows-wheel development by creating an account on GitHub.


Question:
HI, everyone
SO i am trying to do instance segmentation with PIxel lib ,but get the following Value Error
My Code:
import pixellib
from pixellib.tune_bg import alter_bg
from pixellib.instance import instance_segmentation
from matplotlib import pyplot as plt
from PIL import Image

segmentation_model = instance_segmentation()

segmentation_model.load_model("./mask_rcnn_balloon.h5")

target_classes = segmentation_model.select_target_classes(people == "True")

My Error:
ValueError                                Traceback (most recent call last)
/tmp/ipykernel_147/3813335621.py in <module>
      1 segmentation_model = instance_segmentation()
      2
----> 3 segmentation_model.load_model("./mask_rcnn_balloon.h5")
      4
      5 target_classes = segmentation_model.select_target_classes(people == "True")

/opt/conda/lib/python3.7/site-packages/pixellib/instance/__init__.py in load_model(self, model_path, confidence)
     63         self.model = MaskRCNN(mode = "inference", model_dir = self.model_dir, config = coco_config)
     64
---> 65         self.model.load_weights(model_path, by_name= True)
     66
     67

/opt/conda/lib/python3.7/site-packages/pixellib/instance/mask_rcnn.py in load_weights(self, filepath, by_name, exclude)
   2108
   2109             if by_name:
-> 2110                 hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)
   2111             else:
   2112                 hdf5_format.load_weights_from_hdf5_group(f, layers)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py in load_weights_from_hdf5_group_by_name(f, layers, skip_mismatch)
    793                                symbolic_weights[i])) +
    794                            ', but the saved weight has shape ' +
--> 795                            str(weight_values[i].shape) + '.')
    796
    797         else:

ValueError:

Layer #389 (named "mrcnn_bbox_fc"), weight <tf.Variable 'mrcnn_bbox_fc/kernel:0' shape=(1024, 324) dtype=float32> has shape (1024, 324), but the saved weight has shape (1024, 8).

There is no option for excludng “mrcnn_bbox_fc”. Please Help
EDIT: OK, so it WORKS with 2.0 but not 2.1 baloon.h5 Model (Why is that ? )

Context:
HI,
Since Pixellib is a community library, it might be easier to get answers directly on their github repo, under issues.
unless the creators are also on this forum.


Question:
Hello World,
I am currently in the middle of an internship project. I am doing a Recommender System and now I am developing a solution using Tensor Flow Recommenders.
As I have a large number of features for my products I decided to use the Deep & Cross Network Model, having adapted the code in the tutorials to my dataset.
I can currently run all the code, and it gives really good RMSE values, but I am struggling with adapting the code from the Retrieval Model tutorial to get recommendations from the DCN Model.
Is this DCN Model enough to provide recommendations? Do I need to fit it into another structure (User Model, Query Model, Product Model, Candidate Model)?
Basically all I want is to get recommendations from this, giving the User ID as an input. If anyone of you wants to help me I will compensate you.
Thank you in advance

Context:
Hi,
I believe this might be what you are after, the example works on generating recommendations from the tutorials with the use_timestamp features:
index = tfrs.layers.factorized_top_k.BruteForce(model.query_model)
index.index_from_dataset(
tf.data.Dataset.zip((movies.batch(100), movies.batch(100).map(model.candidate_model)))
)
query = dict(pd.DataFrame({‘user_id’: [“42”],‘timestamp’:[879024327]}).iloc[0].map(lambda x: tf.expand_dims(x,axis=0)))

Get recommendations.
_, titles = index(query)
print(f"Recommendations for user 42: {titles[0, :5]}")


Question:
I want to create rnn or CNN for image editing

if so possible then help me.

Context:
Welcome to the TF community.
Are you looking for something like this?

      github.com

GitHub - genforce/idinvert: [ECCV 2020] In-Domain GAN Inversion for Real Image... 1

  [ECCV 2020] In-Domain GAN Inversion for Real Image Editing - GitHub - genforce/idinvert: [ECCV 2020] In-Domain GAN Inversion for Real Image Editing

https://genforce.github.io/idinvert/


Pytorch mdoule translation to tensorflow

Question:
I was trying to translate a PyTorch code (a single module) to TensorFlow but faced some obstacles. Below are the both code and trails. How should I do this?
In pytorch, it has
import torch.nn as nn
import torch

class Blcoks(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, local_feat, global_feat):
        global_feat_norm = torch.norm(global_feat, p=2, dim=1)

        projection = torch.bmm(global_feat.unsqueeze(1), torch.flatten(local_feat, start_dim=2))
        projection = torch.bmm(global_feat.unsqueeze(2), projection).view(local_feat.size())
        projection = projection / (global_feat_norm * global_feat_norm).view(-1, 1, 1, 1)

        orthogonal_comp = local_feat - projection
        global_feat = global_feat.unsqueeze(-1).unsqueeze(-1)
        return torch.cat([global_feat.expand(orthogonal_comp.size()),
                          orthogonal_comp], dim=1)

local_  = torch.ones(1, 20, 32, 32)
global_ = torch.ones(1, 20)
x = OrthogonalFusion()
x(local_, global_).shape
torch.Size([1, 40, 32, 32])

Above is the working code. Now, I’ve tried to translate into tf but fails.
Major issue,

torch. flatten (input , start_dim=0 , end_dim=- 1 ) 3 - issue 2

Context: Is it possible to translate? I’m mostly used to pytorch but now I’m thinking to switch to tensorflwo but translation is not going smooth.


Input a value to a Neural Network, Kotlin for android

Question:
I built a tflite model for a very basic android app in kotlin, that accepts a float or double as input and yields double as output. The model is a NN. I don’t know how to give inputs to the interpreter. And I can’t find a good documentation about kotlin interpreter class.
var input = 3 // ??
var output = ?? // Do I need to declare an Array?
interpreter.run(input, output)
In the second case, I would like the input to be entered by the user into an editText field.
inputText = findViewById(R.id.editText)
input = (Int) inputText?.getText() // do I need to cast to an Int or Double?

Context:
Hi @maths_soso
There is a splendid guide here 1
and of course you can see a lot of good examples there 1.
Best


Question:
on nvidia equipped machine i can run the training now with tf2.6 but nvidia-smi does not show anything. If the ML code is pushing kernel onto GPU, I can see it shows up along PID (below)
how do i verify that training is done on GPU or CPU?
As a comparison, I can run simple vector algebra kernel on GPU by explicitly pushing to GPU and I can see nvidia-smi rightly shows the name of the executable (a.out):
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.57.02    Driver Version: 470.57.02    CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  On   | 00000000:01:00.0 Off |                  N/A |
| 40%   31C    P2    52W / 215W |    298MiB /  7981MiB |     49%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A       973      G   /usr/lib/xorg/Xorg                 29MiB |
|    0   N/A  N/A      1207      G   /usr/bin/gnome-shell                7MiB |
|    0   N/A  N/A      3085      C   ./a.out                           257MiB |
+-----------------------------------------------------------------------------+

Context:
Can you verify that you have visibile GPUs:

      TensorFlow

tf.config.get_visible_devices  |  TensorFlow Core v2.6.0 2

  Get the list of visible physical devices.


Minimum image sample size and degree of data augmentation for prediction of 1 continuous variable

Question:
Hello good people.
So I would want to predict milk yield from image data. I have a sample of 1000 images. Would this suffice to train my model? Also, how much can I augment the data so as to create more samples.
If you know of published literature on these issues, please provide a link.

Context:
1,000 images is not a very large data set but you should try and see if the model will be able to show reasonable accuracy. The important thing is to set aside a small portion of this data set for validation and check the accuracy on the images that were not used for training.
You can use data augmentation methods as is demonstrated in this tutorial: Data augmentation  |  TensorFlow Core 2
Random horizontal flips, random changes in brightness and contrast and some other transformations could be safely used to make your data set more diverse.


Question:
[Google Colab]
I’ve added some code to save “abalone” model as .tflite file and then load it. It creates .tflite file, but then I can’t use it:
ValueError: Cannot set tensor: Dimension mismatch. Got 7 but expected 1 for dimension 1 of input 0.

What’s wrong?

Context:
Hi burbilog,
Have you checked input shape with netron.app?


Error when trying to run inference using TFlite

Question:
I’ve custom trained ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8_3 with my data and exported the model and I’m able to do inferencing . im using tensorflow 2.6 and now I want to convert the model to tflite to run it on mobile .I’ve used the code below to convert the model to tflite and the code run and a tflite model is saved successfully .
converter=tf.lite.TFLiteConverter.from_saved_model(PATH_TO_SAVED_MODE1L)
    converter.optimizations=[tf.lite.Optimize.DEFAULT]
    converter.target_spec.supported_ops=[tf.lite.OpsSet.TFLITE_BUILTINS,tf.lite.OpsSet.SELECT_TF_OPS]
    tflite_model=converter.convert()
    with open('C:/Users/CX/Desktop/model.tflite', 'wb') as f:
      f.write(tflite_model)

Now im trying to test with some with images using the code below :
interpreter = tf.lite.Interpreter(model_path="C:/Users/CX/OneDrive/Desktop/model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
path=r"C:\t3"
for file in pathlib.Path(path).iterdir():

    img = cv2.imread(r"{}".format(file.resolve()))

    img1 = img.reshape(1,img.shape[0],img.shape[1],3)

    interpreter.set_tensor(input_details[0]['shape_signature'],img1)

    interpreter.invoke()

Im getting the error :
TypeError: SetTensor(): incompatible function arguments. The following argument types are supported:
    1. (self: tensorflow.lite.python.interpreter_wrapper._pywrap_tensorflow_interpreter_wrapper.InterpreterWrapper, arg0: int, arg1: handle) -> object

Invoked with: <tensorflow.lite.python.interpreter_wrapper._pywrap_tensorflow_interpreter_wrapper.InterpreterWrapper object at 0x000001E4D88E38F0>, array([ 1, -1, -1,  3])

here is the details from the interpreter.get_input_details() fucntion :

[{'name': 'serving_default_input_tensor:0', 'index': 0, 'shape': array([1, 1, 1, 3]), 'shape_signature': array([ 1, -1, -1,  3]), 'dtype': <class 'numpy.uint8'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]

and interpreter.get_output_details()
[{'name': 'StatefulPartitionedCall:6', 'index': 462, 'shape': array([    1, 12804,     4]), 'shape_signature': array([    1, 12804,     4]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'StatefulPartitionedCall:0', 'index': 1594, 'shape': array([1, 1]), 'shape_signature': array([ 1, -1]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'StatefulPartitionedCall:5', 'index': 1559, 'shape': array([1]), 'shape_signature': array([1]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'StatefulPartitionedCall:7', 'index': 482, 'shape': array([    1, 12804,    56]), 'shape_signature': array([    1, 12804,    56]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'StatefulPartitionedCall:1', 'index': 1647, 'shape': array([1, 1, 1]), 'shape_signature': array([ 1, -1, -1]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'StatefulPartitionedCall:2', 'index': 1629, 'shape': array([1, 1]), 'shape_signature': array([ 1, -1]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'StatefulPartitionedCall:4', 'index': 1576, 'shape': array([1, 1]), 'shape_signature': array([ 1, -1]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}, {'name': 'StatefulPartitionedCall:3', 'index': 1612, 'shape': array([1, 1, 1]), 'shape_signature': array([ 1, -1, -1]), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]

Any help really appreciated

Context:
Hi @ayadtariq
Change
interpreter.set_tensor(input_details[0]['shape_signature'],img1)

to
interpreter.set_tensor(input_details[0]['index'],img1)

and see if this specific error disappears.


Xla (jit_compile flag) and gpu memory usage

Question:
We are observing unexplained out of GPU memory events when trying to train a complex large model (involving conditional execution) and enabling XLA (jit_compile=True flag for tf.function).
Unfortunately, we haven’t been able to reproduce the issue in a reduced shareable form just yet, so I am writing here mostly for feedback.
What we see:

In general GPU utilization for an XLA compiled model goes considerably down compared to non-compiled graph mode or eager execution. This is what we measure in most of our models and in all our small test-cases.
However, in some instances, large models exceed memory capacity when compiled while they can still run in eager mode for the same exact batch size.

Both behaviors seem contradictory, so we are wondering if there are some known corner cases involving XLA that may produce this and we can avoid (we really need the extra train efficiency coming out of XLA).
Thanks!

Context:
Hi Ramon,
1- I don’t know the answer, but did you watch this: How to make TensorFlow models run faster on GPUs - YouTube 1
you might get some insight there
I’ll ping some people to get you more insights (no promises)


How to find out keras model memory size?

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

Context:
You can retrieve memory info with flops from the profiler as in:

      github.com/tensorflow/tensorflow

      TF 2.0 Feature: Flops calculation 29

        opened

        Sep 25, 2019

          pzobel

        stat:awaiting tensorflower
        type:feature
        comp:tfdbg
        TF 2.0

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

**System information**
- TensorFlow version (you are using): TF 2.0 RC2
- Are you willing to contribute it (Yes/No):

**Describe the feature and the current behavior/state.**

I am missing the opportunity to compute the number of floating point operations of a tf.keras Model in TF 2.0.
In TF 1.x tf.profiler was available [see here](https://stackoverflow.com/questions/45085938) but I can find anything equivalent for TF 2.0 yet.

**Will this change the current api? How?**

**Who will benefit with this feature?**

Everbody interested in the computational complexity of a TensorFlow model.

**Any Other info.**

You can also try:

      PyPI

model-profiler 39

  Tensorflow/Keras Model Profiler: Tells you model's memory requirement, no. of parameters, flops etc.


Question: In order to contribute to tensorflow, what are the requirements?

Context:
Welcome to TF.
Please check this thread:

    Contribution to the open source projects for Beginners General Discussion

    hi Kanishk
welcome to the TensorFlow Forum
To contribute to the TensorFlow Open Source project you can follow this guide here: Contribute to TensorFlow


Load error when loading h5 saved by simple dense model

Question:
I used following code 1 to train and save model and code #2 to load but loading results in error. Any idea? Thanks.,
code #1:
# Using neural net to do a classification task.

import tensorflow as tf
import pandas as pd
import matplotlib as plt
import sys
import time
import re
import numpy as np
import helper
from tensorflow import keras

DEBUG=0
CONFIG_ENABLE_PLOT=0
CONFIG_EPOCHS=30
CONFIG_BATCH_SIZE=32

for i in sys.argv:
    print("Processing ", i)
    try:
        if re.search("epochs=", i):
            CONFIG_EPOCHS=int(i.split('=')[1])

        if re.search("batch_size=", i):
            CONFIG_BATCH_SIZE=int(i.split('=')[1])

    except Exception as msg:
        print("No argument provided, default values will be used.")

print("epochs: ", CONFIG_EPOCHS)
print("batch_size: ", CONFIG_BATCH_SIZE)

fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()
print("X_train_full/y_train_full/X_test/y_test: ", X_train_full.shape, y_train_full.shape, X_test.shape, y_test.shape)

SEPARATOR=10000
X_valid, X_train = X_train_full[:SEPARATOR] / 255.0, X_train_full[SEPARATOR:]/255.0
y_valid, y_train = y_train_full[:SEPARATOR], y_train_full[SEPARATOR:]
X_test = X_test / 255.0

print("X_valid/X_train/y_valid/y_train: ", X_valid.shape, X_train.shape, y_valid.shape, y_train.shape)

class_names = ["T-shirt/top","Trouser", "Pullover", "Dress", "Coat" , "Sandal", "Shirt", "Sneaker","Bad","Ankle boot"]

model=keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape = [28, 28]))
model.add(keras.layers.Dense(300, activation="relu"))
model.add(keras.layers.Dense(100, activation="relu"))
model.add(keras.layers.Dense(30, activation="softmax"))

print("model summary: ", model.summary())

model.compile(loss="sparse_categorical_crossentropy", optimizer="sgd", metrics=["accuracy"])
history=model.fit(X_train, y_train, epochs=CONFIG_EPOCHS, batch_size=CONFIG_BATCH_SIZE, validation_data=(X_valid, y_valid))

if CONFIG_ENABLE_PLOT:
    pd.DataFrame(history.history).plot(figsize=(8, 5))
    plt.pyplot.grid(True)
    plt.pyplot.gca().set_ylim(0, 1)
    plt.pyplot.show()

model.evaluate(X_test, y_test)

if DEBUG:
    print("model layers: ", model.layers)

weights, biases  = model.layers[1].get_weights()

if DEBUG:
    print("weights, biases (shapes): ", weights, biases, weights.shape, biases.shape)

model.save("p297.h5")
X_new = X_test[:3]
print("X_new shape: ", X_new.shape)
y_proba = model.predict(X_new)
print("y_proba (predict)(value): ", y_proba.round(2), "\ny_proba(shape)", np.array(y_proba).shape)

y_pred = model.predict_classes(X_new)
print("y_pred (predict_classes): ", y_pred)
print("y_test: ", y_test[:3])

code #2
# Using neural net to do a classification task.
# This has similar objective as p297.py, the difference
# is p297.py saves the training model and this one load
# the model and predicts the one from p297.py

import tensorflow as tf
import pandas as pd
import matplotlib as plt

from tensorflow import keras
print(tf.__version__)
print(keras.__version__)

CONFIG_ENABLE_PLOT=0

fashion_mnist = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()
print("X_train_full.shape: ", X_train_full.shape)
print("X_train_full.dtype: ", X_train_full.dtype)

X_valid, X_train = X_train_full[:5000] / 255.0, X_train_full[5000:]/255.0
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
X_test - X_test / 255.0
class_names = ["T-shirt/top","Trouser", "Pullover", "Dress", "Coat" , "Sandal", "Shirt", "Sneaker","Bad","Ankle boot"]

model = keras.models.load_model("p297.h5")
model.evaluate(X_test, y_test)

print("model layers: ", model.layers)
weights, biases  = model.layers[1].get_weights()
print("weights, biases (shapes): ", weights, biases, weights.shape, biases.shape)
model.save("p297-2.h5")
X_new = X_test[:3]
y_proba = model.predict(X_new)
print(y_proba.round(2))

y_pred = model.predict_classes(X_new)

#output:
python3 p297-load.py
...
 a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
1.14.0
2.2.4-tf
X_train_full.shape:  (60000, 28, 28)
X_train_full.dtype:  uint8
Traceback (most recent call last):
  File "p297-load.py", line 38, in <module>
    model = keras.models.load_model("p297.h5")
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/save.py", line 146, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
  File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/saving/hdf5_format.py", line 210, in load_model_from_hdf5
    model_config = json.loads(model_config.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'

Context: What is your h5py version?


Question: I was working on Simple audio recognition: Recognizing keywords  |  TensorFlow Core 1 and I completed it, but I was wondering how I could do this recognition live instead of on an audio file. I’m not very experienced, so help would be appreciated. Also, are there any good Tensorflow tutorials on live speech recognition? Thanks.

Context:
Hi Henry,
In which environment you want to capture the audio? on a phone or browser?
The idea is, that model could be ported to the place where the audio is being captured and then you’d need to keep feeding the audio to the model and getting the results
So, in summary the capture and feeding the data to the model is just regular coding


Question: Does anyone know how far along formal efforts are to utilise tfjs code directly on the TPU?

Context:
This was the most updated status:

      github.com/tensorflow/tfjs

      Coral edge tpu bindings for tf.js  8

        opened

        Mar 20, 2019

          closed

        Jul 25, 2019

          oveddan

        type:feature

    I'm not sure if this is possible, but:

[the current starter code](http://stor…age.googleapis.com/cloud-iot-edge-pretrained-models/edgetpu_api.tar.gz) for the Coral edge board on Raspberry Pi contains python bindings for the c++ edge tpu engine.  Would it be possible to create tf.js node bindings as well?  It would be awesome to be able to use javascript for this, especially because of its non-blocking nature, and types provided by typescript.


Question:
I am trying to construct a while loop from elementary operations Enter, Merge, Switch, NextIteration, Exit. Normally, when I build a Graph, I add the Operations one-by-one following the flow of tensors, i.e. when I create an operation I can name the tensor used as an input (coming as an output of another operation). The problem is with the loop, where by nature there always exists an operation (the first to add regardless where I start to build the loop),  where I cannot specify the input tensor yet. Without that I cannot add the operation.
Somewhere I read about a “workaround” to use a dummy tensor and later change it to the output of the last element of the loop. The problem is that I cannot do that either, as a Graph is append only, no existing operation can be changed.
Can someone direct me to a page, where I can learn how to make a while loop (in TF 1.x, i.e. without tf.while)?

Context:
TF 1.x is EOL.
For TF 2 please take a look at:

      TensorFlow

Better performance with tf.function  |  TensorFlow Core 2


Image preparation for prediction of a continuous variable

Question:
Good day friend.
Please keep in mind that I am a novice in the computer science field. So I have Image data for Deep learning, my goal is to be able to predict Milk yield (a continuous variable) based on image data. My question is how and where do I put the milk yield labels?. Do I name the images according to the milk yield values or what?
Your assistance will be greatly appreciated.

Context:
The simplest option is to create a csv-file containing (or something like that) containing filename,  yield rows. Load it as a pandas-dataframe.
Then when you iterate over the rows, for each row read the image file, decode the image, and then process it to match your model’s expected input.
Something like:
def load_image(filename):
  img = tf.io.read_file(filename)
  img = tf.image.decode_jpeg(img)
  img = ...
  return img

def process_row(filename, label):
  return load_image(filename), label

df = pandas.read_csv(csv_file)
ds = tf.data.dataset.from_tensor_slices(dict(df))
ds = ds.shuffle().map(process_row).batch(...)


How is preprocessing applied? On the whole dataset or on batches?

Question:
Hi,
I wanted to know how preprocessing in general and normalization in particular works:

When the preprocessing is applied on dataset using .map, is for example the mean calculated on the whole dataset? Or is it per batch?
Same question when the Normalization is part of the model as a preprocessing layer.

Thanks!
Regards,
Fadi Badine

Context: Hi Fadi, I’d say it’s all evaluated lazily so it’d be per batch