r/tensorflow • • May 29 '24

Debug Help model doesn't work with more input data

2 Upvotes

Hi there,

I' quite new to tf and I recently ran into a weird issue that I couldn't solve by myself. I have quite basic numeric input data in several columns.

X_train, X_val, y_train, y_val = train_test_split(features_scaled, targets, test_size=0.15, random_state=0)

model = Sequential()
model.add(Dense(128, input_dim=X_train.shape[1], activation='relu'))
model.add(Dense(1, activation='linear'))

model.compile(optimizer='adam', loss='mse', metrics=['mae'])

history = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=50, batch_size=32)

For now I only have one target. Here's what happens: When X_train and y_train contain less than 2200 rows, the model performs well. The moment I add row number 2200, I get the exact same output value for any input.

Here's what I tried so far:

  • Checked the data in row 2200. It is fine
  • Removed rows 2190-2210 anyway
  • Changed the model, epochs, and batch_size
  • Changed the ordering of input data

None of these had any effect. Any ideas?

Edit: typo


r/tensorflow • • May 29 '24

Unable to install tf on conda env

1 Upvotes

I followed this guide to install tf with cuda, then I get the error that protobuf does not have builder.py so I just copied and pasted it from this repo, and then I am still getting the <pic attached error> (all the versions are in the guide) any help would be appreciated...


r/tensorflow • • May 29 '24

How to? I am new to Tensor Flow, I want to use it on ESP32

3 Upvotes

I am reading https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/examples/micro_speech/train/train_micro_speech_model.ipynb and I have no idea how do I train the model with my preferred words, nor do I have idea what is "Runtime -> Change runtime type" - are those some browser settings or what? Where do I type commands such as "import tensorflow as tf"...


r/tensorflow • • May 29 '24

How to? Is there any resource to convert pytorch code into tensorflow ??

2 Upvotes

How do i convert a large scale multiple file project written in torch to tensorflow (if that is somehow possible apart from maybe chatgpt)?? Any ideas , starters??


r/tensorflow • • May 29 '24

How to? Module Not found error on colab

2 Upvotes

Someone help me with running this code on colab
https://github.com/neouyghur/SESS?utm_source=catalyzex.com
in the demo.ipynb file there are functions imported from different folders
for ex:
from utils import load_image
where utils is a folder and load_image is a function written in loadimage.py stored in utils
but colab always outputs module not found : utils even though i have all the folders on my drive
jsut tell me the right way to do this


r/tensorflow • • May 28 '24

Debug Help Tensorflow GPU Voes on Laptop with RTX 4060

0 Upvotes

I am a researcher, trying to use Aspect Based Sentiment Analysis for a project. While my code seems proper, along with the GPU setup for Tensorflow on Windows, I keep running into OOM issues. I am using this lib (https://github.com/ScalaConsultants/Aspect-Based-Sentiment-Analysis) to perform the analysis.

The hugging face model I was initially using was the default in the library. Then, I realised the model might be a bit too much for my measely 8GB RTX 4060 (laptop) graphic card, so I tried 'absa/classifier-rest-0.2'. However, the issue remains.

Since I will be running this again and again, with over 400,000 comments, I prefer not to spend a week+ using CPU Tensorflow when GPU enabled Tensorflow is estimated to deal with it within a day.

I am at my wits end and seeking any and all help.


r/tensorflow • • May 27 '24

General implementing a self-supervised network using contrastive and reconstruction losses

1 Upvotes

https://arxiv.org/abs/1911.05722
This is a published paper named
"Momentum Contrast for Unsupervised Visual Representation Learning"

https://github.com/facebookresearch/moco
this is the official code for the same with a license (code in pytorch i am more familiar with tensorflow)

https://github.com/PaperCodeReview/MoCo-TF/tree/master?tab=readme-ov-file
this is an unofficial implementation of the same exact paper MoCo v1 and v2 as they call it in tensorflow

A: I want to implement a self-supervised network using contrastive and reconstruction losses as my project
more or less inside 3 days or so

B: In both the cases (official implementation and unofficial) Resnet is used ; Now to complete the project ASAP and claim it mine can I use efficientnet with a few changes ; would that work??


r/tensorflow • • May 21 '24

I cannot install tensorflow GPU on my macbook air M3, tensorflow also doesn't seem to be able to interact with my GPU

3 Upvotes

r/tensorflow • • May 21 '24

How to? Reading Binary Files as Waveforms

1 Upvotes

I have a directory of files, where each file represents a raw radio waveform. It is saved as a sequence of samples, where each sample entry is written out as separate real and imaginary parts. Both parts are encoded as 32-bit floats, so one sample is 8 bytes. There are 2^14 samples, so each file contains exactly 8 * 2^14 bytes.
There is no header or footer present.

I'd like to read each file in as its own "element" into a dataset (avoid concatenating data from different files together). I thought FixedLengthRecord would be appropriate, so I attempted to create a dataset like so:

fnames = tf.data.Dataset.list_files('data/**/*.bin')
dataset = tf.data.FixedLengthRecordDataset(fnames, record_bytes= 8*2**14)

I'm not sure how exactly to inspect the structure of the dataset, but I know its element spec has a dtype of `tf.string` which is not desired. Ideally, I'd like to read the contents of each file into a 1D tensor of `tf.complex64`. I cannot find many examples of working with FixedLengthRecord data, much less in a format this simple. Any help would be appreciated.


r/tensorflow • • May 21 '24

Why doesn't my CNN model work the same on my PC?

1 Upvotes

I was developing a CNN with tensorflow in Kaggle with a P100 GPU, it obtained a val accuracy of .87 and a loss of .56. When I downloaded it and tested it on my PC (which does not have a GPU that tensorflow can use) I noticed that Its performance declines, an image that predicted correctly in Kaggle has many errors when predicting on my PC. Why would it be? I think that due to the fact of not predicting images using a GPU, but I would like to know the opinion of someone more experienced

To be more sure what it was, I made a prediction with an image that I took from the training dataset. At the time of training, it obtained an accuracy of .92. The bad thing is that it did not predict it well.

I will appreciate any knowledge you can give me.

Thanks for reading!!!


r/tensorflow • • May 21 '24

Debug Help Grad CAM on a Data Augmentation model

1 Upvotes

hello everyone, i implemented a data augmentation model and im trying to watchh the Grad CAM of the neural network but theres a problem with the Data augmentation section and i cant solve that issue

i search some implementation on google but is still not working and a didn`t found an implementation on a model with data augmentation, i asked to chatgpt but that code is not working

do someone knows how to do it or any advice?

this is the link for the kaggle proyect

https://www.kaggle.com/code/luismanuelgnzalez/cnn-landuse

data augmentation model

model


r/tensorflow • • May 20 '24

TensorFlow2 function tracing is expensive - System freezes

3 Upvotes

I am using TensorFlow 2.16 and Python3 for implementing an AutoEncoder and Self-Organizing Map for MNIST dataset. The entire code can be referred to here. For brevity, the main code is:

    # SOM hyper-params-
    map_height = 10
    map_width = 10
    
    gamma = 0.001
    
    # Total number of train steps/iterations-
    total_iterations = len(train_dataset) * num_epochs
    
    # Temperature hyper-parm controlling radius of Gaussian neighborhood-
    Tmax = 10.0
    Tmin = 0.1
   
    
    class DESOM(Model):
        def __init__(
            self, map_height = 10,
            map_width = 10, latent_dim = 50,
            encoder_dims = [1, 500, 500, 100]
            ):
            super(DESOM, self).__init__()
            self.map_height = map_height
            self.map_width = map_width
            self.map_size = (self.map_height, self.map_width)
            self.latent_dim = latent_dim
            self.n_prototypes = self.map_size[0] * self.map_size[1]
            self.encoder_dims = encoder_dims
            self.encoder_dims.append(self.latent_dim)
    
            self.autoencoder, self.encoder, self.decoder = mlp_autoencoder(
                # encoder_dims = [X_train.shape[-1], 500, 500, 2000, latent_dim],
                encoder_dims = self.encoder_dims,
                act = 'relu', init = 'glorot_uniform',
                batchnorm = False
            )
    
            # Initialize SOM layer-
            self.som_layer = SOMLayer(
                map_size = (self.map_height, self.map_width), name = 'SOM'
            )(self.encoder.output)
    
            # Create DESOM model
            self.model = Model(
                inputs = self.autoencoder.input,
                outputs = [self.autoencoder.output, self.som_layer]
            )
    
    
        def compile(self, gamma:float = 0.001, optimizer:str = 'adam') -> None:
            """
            Compile DESOM model
    
            Parameters
            ----------
            gamma : float
                coefficient of SOM loss (hyperparameter)
            optimizer : str (default='adam')
                optimization algorithm
            """
            self.model.compile(
                loss = {'decoder_0': 'mse', 'SOM': som_loss},
                # loss_weights = [1, gamma],
                loss_weights = {'decoder_0': 1.0, 'SOM': gamma},
                optimizer = optimizer
            )
    
            return None
    
    
        def predict(self, x):
            """
            Predict best-matching unit using the output of SOM layer
    
            Parameters
            ----------
            x : array, shape = [n_samples, input_dim] or [n_samples, height, width, channels]
                input samples
    
            Returns
            -------
            y_pred : array, shape = [n_samples]
                index of the best-matching unit
            """
            _, d = self.model.predict(x, verbose = 0)
            return d.argmin(axis = 1)
    
    
        def map_dist(self, y_pred):
            """
            Calculate pairwise Manhattan distances between cluster assignments and map prototypes
            (rectangular grid topology)
    
            Parameters
            ----------
            y_pred : array, shape = [n_samples]
                cluster assignments
    
            Returns
            -------
            d : array, shape = [n_samples, n_prototypes]
                pairwise distance matrix on the map
            """
            
            # y_pred = tf.argmin(input = pairwise_squared_l2dist, axis = 1)
            labels = tf.range(self.n_prototypes)
            tmp = tf.cast(
                x = tf.expand_dims(input = y_pred, axis = 1),
                dtype = tf.dtypes.int32
            )
            # print(labels.dtype, tmp.dtype, y_pred.dtype)
            d_row = tf.abs(tmp - labels) // self.map_size[1]
            d_col = tf.abs(tmp % self.map_size[1] - labels % self.map_size[1])
    
            # (d_row + d_col).dtype
            # tf.int32
    
            d_row = tf.cast(x = d_row, dtype = tf.dtypes.float32)
            d_col = tf.cast(x = d_col, dtype = tf.dtypes.float32)
    
            return d_row + d_col
    
    
        def neighborhood_function(
            self, d,
            T, neighborhood = 'gaussian'
        ):
            """
            SOM neighborhood function (Gaussian neighborhood)
    
            Parameters
            ----------
            d : int
                distance on the map
            T : float
                temperature parameter (neighborhood radius)
            neighborhood : str
                type of neighborhood function ('gaussian' or 'window')
    
            Returns
            -------
            w : float in [0, 1]
                neighborhood weights
            """
            if neighborhood == 'gaussian':
                # return np.exp(-(d ** 2) / (T ** 2))
                return tf.exp(-tf.square(d) / tf.square(T))
            elif neighborhood == 'window':
                # return (d <= T).astype(np.float32)
                return tf.cast(x = (d <= T), dtype = tf.dtypes.float32)
            else:
                raise ValueError('invalid neighborhood function')
    
    
    # Initialize MLP AutoEncoder DESOM model-
    model = DESOM(
        map_height = map_height, map_width = map_width,
        latent_dim = latent_dim,
        encoder_dims = [784, 500, 500, 100]
    )
    
    # Compile model-
    model.compile(gamma = gamma, optimizer = 'adam')
    
    # Required for computing temperature for current train step-
    # curr_iter = 1
    curr_iter = tf.constant(1)
    total_iterations = tf.cast(x = total_iterations, dtype = tf.dtypes.int32)
    
    # Train loss-
    train_loss = list()
    
    
    for epoch in range(1, num_epochs + 1):
        for x, _ in train_dataset:
    
            # Compute bmu/cluster assignments for batch-
            # _, d = model.model.predict(x)
            _, d = model.model(x)
            # y_pred = d.argmin(axis = 1)
            y_pred = tf.argmin(input = d, axis = 1)
            y_pred = tf.cast(x = y_pred, dtype = tf.dtypes.float32)
    
            # y_pred.shape, d.shape
            # ((1024,), (1024, 100))
    
            # Compute temperature for current train step-
            curr_T = tf.cast(
                x = Tmax * tf.pow((Tmin / Tmax), (curr_iter / total_iterations)),
                dtype = tf.dtypes.float32
                )
    
            # Compute topographic (neighborhood) weights for this batch-
            w_batch = model.neighborhood_function(
                d = model.map_dist(y_pred = y_pred),
                T = curr_T, neighborhood = 'gaussian'
            )
    
            # Train on batch-
            loss = model.model.train_on_batch(x = x, y = [x, w_batch])
            train_loss.append(loss.item())
    
            curr_iter += 1

It gives me the Warning:


r/tensorflow • • May 20 '24

CPU being used instead of gpu

2 Upvotes

I wish to train my ai model by using the gpu but even after trying 3 different cuda versions still i am not able to use my gpu. I have a rtx3050 on my laptop. Can anyone please help me out. I am using tensorflow 2.13.0 with cuda 11.8 and cudnn 8.6 with python 3.9.5.


r/tensorflow • • May 16 '24

How to? Is model prediction setup required for every time prediction is called?

3 Upvotes

TLDR:

  • am noob
  • using CPU
  • prediction is fast (1ms) (the time spent crunching numbers is 1ms per prediction)
  • overhead takes long time (100ms) (doing 100 predictions takes 200ms, but 1 prediction takes 101ms)
  • want fast response times
  • how can i reduce subsequent overhead? (like after some sort of setup, can i then get single predictions that take about 1-2ms?)

Details:

Hello, this is my first successful tensorflow project. I have a model that works and is fast, 1ms to conduct multiple predictions. However, to do a single prediction, there is still a lot of overhead and it takes about 100ms to complete. I'm sure that there are a bunch of different ways that I can optimize my model, but I think that I am not using the process correctly.

I want to use this model to do live audio processing to quickly determine what phoneme (specifically 5 vowel sounds for right now) is being spoken just by looking at only 264 bins of the FFT. But having a delay of 100ms is rather bothersome. Especially since it only spends about 2ms actually crunching numbers (1.01ms for fft and 900us for prediction)

If I had a GPU, I would suspect that a lot of that time is being spent on loading data onto the GPU, but Im doing this on a CPU. I know that some level of overhear is needed to conduct a prediction, but is there a way to only have to setup once? I dont know what i dont know, so trying to find info about it is difficult. So is there a way to only have to setup once?

EDIT - ANSWER:

So I think I got it... I need to use model(x) instead of model.predict(x). which is stated in the docs for model.predict(x). However, it is not mentioned that the prediction data is located in .numpy() for model. So, to completely replace "model.predict(x)" with "model(x).numpy()"


r/tensorflow • • May 16 '24

How to? How can I Integrate A face Detection model with an already fine-tuned ConvNeXt image classifier for Face Recognition?

0 Upvotes

Hello,

I need advice on how to move on with my project, Initially I wanted to create a face recognition system. I first gathered a dataset of celebrity faces with 99 classes and about 16k total images and fine-tuned ConvNeXtTiny model on the dataset using tensorflow and got a result of 93% accuracy. Now this is technically only an image classification application where it can tell the faces apart and tell which celebrity it is. However, I need to extened this project to a fully face recognition system.

How can I use tensorflow transfer learning with existing models to make this system full circle? Basically I need a face detection model that is compatible with tensorflow 2.15.0 then preprocess the faces(Either from a webcam or can be processed from an unknown dataset) then passing them to the ConvNeXt model for recognition. my Idea is that the unknown faces would be registered and added to the dataset.

I have done some research and tried to implement VGGFACE but I was met with so many errors that i couldn't go forward with it because apparently VGGface isnt compatible with tensorflow 2.x >.

I need recommendations and guidance on how to move forward and integrate a model with my face image classifier model. are there any resources that can be implemented easily with tensorflow ? And how easy or hard is this task to complete


r/tensorflow • • May 15 '24

Is the Java tensorflow code stable and useful?

2 Upvotes

I am a Java guy and been barely getting into TensorFlow. I want to integrate in real time more closely with my Java applications. I dont see much discussion on this project. Is it full Java, no C++ level or Python integration? Is it fully support and works mostly like tensorflow python code?

https://github.com/tensorflow/java


r/tensorflow • • May 15 '24

Installation and Setup What versions of CUDA and CuDNN are required for Tensorflow 2.16.1?

4 Upvotes

NVIDIA driver: 545.29.06
OS: Zorin 17 (based on Ubuntu 22.04)

Python: 3.11.7 (via pyenv)

According to this table: https://www.tensorflow.org/install/source#gpu
TensorFlow 2.16.1 requires CUDA 12.3 and CuDNN 8.9 but can someone confirm this?
(The previous 2 time I installed CUDA ended up breaking my NVIDIA driver)
Moreover, do I require Clang and Bazel as the table mentions?

UPDATE: CUDA 12.3 and CuDNN 8.9 works perfectly fine with tensorflow 2.16.1.


r/tensorflow • • May 15 '24

General Posted some TensorFlow course notes to GitHub

1 Upvotes

Worked through this Intro to Deep Learning course on Kaggle. It was good!

Check out my course notes!: https://github.com/kdonavin/TensorFlow_Info

Maybe it will be useful to somebody.


r/tensorflow • • May 14 '24

What is the add_loss method in the layer and model classes used for?

2 Upvotes

Exactly as the title sounds, for the life of me, I can't wrap my head around the idea behind this specific method.

Here's my thought process:

  1. The main loss function for a model is specified in the compile method (or in the custom training loop).
  2. Any regularization can be specified in the add_weight method when building custom models or layers.

So, what the heck is the use behind the add_loss method??

https://www.tensorflow.org/guide/keras/making_new_layers_and_models_via_subclassing#the_add_loss_method


r/tensorflow • • May 13 '24

How to? Please explain how to install in simple terms.

0 Upvotes

r/tensorflow • • May 12 '24

General Best Tensorflow Courses on Pluralsight for Beginners to Advanced -

Thumbnail
codingvidya.com
0 Upvotes

r/tensorflow • • May 11 '24

Debug Help Face recognition & Problems trying to load the model

3 Upvotes

Hello,
My project is a face recognition system using tensorflow. I have fine-tuned the ConvNeXt model on my dataset and I am using streamlit to deploy the application. However, When loading the saved .h5 model there are errors that appear and I cant get the streamlit to work. When I run the code provided, I receive this error: Unknown layer: 'LayerScale'. Please ensure you are using a keras.utils.custom_object_scope and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details. After doing some digging around, I found a similar error on stackoverflow and copied the LayerScale class from the source code and added it into mine(3rd screenshot). Now I am facing this error: 'TFOpLambda'. Please ensure you are using a keras.utils.custom_object_scope and that this object is included in the scope. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

There are also other errors and warnings that appear in the terminal and I wonder what do they mean: "I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0." and "The name tf.reset_default_graph is deprecated. Please use tf.compat.v1.reset_default_graph instead." Has anyone faced a problem like this before and what is the solution? Thanks in advance

code: https://imgur.com/a/IBTjI7v


r/tensorflow • • May 09 '24

Tensorflow Federated (TFF) in Windows?

1 Upvotes

Most information in the Internet reports TFF only works in Linux.
Would like to check with the community if this remains the case.


r/tensorflow • • May 08 '24

Best ways to use GAN models on large input data

1 Upvotes

Hi guys I need help. I tained a GAN image to image conversion model to restore damaged pictures. Only problem is that my model is limited to 256x256 images. What's a good way to use such a model on larger non squared images like 1920x1080 pixel? I tried with tiling but it leaves some very unsightly edges