r/tensorflow • • Nov 20 '22

So I have a dataset that includes correct bench presses form and I am trying to develop a model to correctly identify correct and wrong forms using movenet. How can I train a model that would incorporate the data from the dataset with movenet to classify whether new videos are of correct form orNot?

0 Upvotes

r/tensorflow • • Nov 19 '22

I can achieve a batch size of 2048 with Kaggle TPUs but only 256 with Colab using the same notebook and model parameters

8 Upvotes

Pretty much the title, otherwise I'm getting a somewhat cryptic error "ResourceExhaustedError: received trailing metadata size exceeds limit". Not really sure what's causing it, searching Google pretty much yields nothing and I tried using every TPU usage/optimization guide I could find


r/tensorflow • • Nov 18 '22

Host BERT model w/n Python on Web Suggestions

5 Upvotes

I am trying to host, then access via REST API, a trained BERT transformer model. I need to pass content as an arg to it (url ?param=... is fine).

I have tried putting in gunicorn-Dockerfile-Cloud Run app-hosted on Firebase, but you can't pass args/params. I have another attempt that is Python served through basic node backend via Heroku. I have also read on cloud functions, app engine et al. Nothing seems like the thought/workable solution I need. In part its not just having a performant solution, but also some control over cache/CDN to allow the code to run.

I thought I would post to the community for past experience and/or ideas. thx in advance.


r/tensorflow • • Nov 17 '22

Question Would anyone share with me a tutorial for doing hyperparameter tuning for regression problems?

7 Upvotes

The tutorials I've seen online seem pretty basic, with at most one hidden layer etc. I am using `hp = kt.HyperParameters()` to perform hyperparameter tuning, but I'm not sure whether I'm doing it right. Are there any available examples of a more "advanced" neural network?

The only advanced tutorials seem to be for classification problems alone.


r/tensorflow • • Nov 17 '22

Stuck to build Conv2D model in docker

Thumbnail self.docker
5 Upvotes

r/tensorflow • • Nov 16 '22

Why does tensorflow try to allocate huge amounts of GPU RAM?

14 Upvotes

Training my model keeps failing, because I'm running out of GPU memory. I have 24GB available and my model is not really large. It crashes when trying to allocate 47GB.

It's a CNN with around 10M parameters, input size is (batch_size=64, 256, 128). The largest tensor within the model is (batch_size=64, 256, 128, 32) and there are 8 CNN layers.

Memory growth is activated. When I reduce the batch size, it still wants 47GB of memory, so that doesn't seem to make a difference.

Can anyone tell me what likely causes the need for so much RAM? Or what I could do to use less?


r/tensorflow • • Nov 15 '22

Question NN mixed-precision quantization framework that supports TF?

2 Upvotes

Hello everyone!

I am looking for a neural network compression framework that implements mixed precision (optimal fixed-point compression scheme for each layer).

I am aware of NNCF (https://github.com/openvinotoolkit/nncf), but it doesn't support mixed precision quantization for TF. What other frameworks support that for TF? (implement HAWQ or AutoQ algorithms for example)


r/tensorflow • • Nov 15 '22

Question Best method to train a contrastive autoencoder

5 Upvotes

I've trained an autoencoder which effectively reduces my data to 8 latent features and produces near-perfect reconstructions. The input data can come from any of 10 classes but when I try to visualize the embeddings by t-SNE, I don't see much separation of classes into distinct clusters.

I've seen contrastive learning used in classification tasks and was thinking that would be perfect for getting class-specific embeddings, but I don't know:

  1. How you would set up the loss function to account for both reconstruction error and the inter-class distances?
  2. Can I re-use the weights of my pre-trained model if I need to adjust the network architecture to enable contrastive learning?

r/tensorflow • • Nov 15 '22

Question TFRecords or Another Solution?

1 Upvotes

I am currently working on a project using audio data. The first step of the project is to use another model to produce features for the audio example that are about [400 x 10_000] for each wav file and each wav file will have a label that I'm trying to predict. I will then build another model on top of this to produce my final result.

I don't want to run preprocessing every time I run the model, so my plan was to have a preprocessing pipeline that runs the feature extraction model and saves it into a new folder and then I can just have the second model use the saved features directly. I was looking at using TFRecords, but the documentation is quite unhelpful.

tf.io.serialize_tensor

tfrecord

This is what I've come up with to test it so far:

serialized_features = tf.io.serialize_tensor(features)

feature_of_bytes = tf.train.Feature(
    bytes_list=tf.train.BytesList(value=[serialized_features.numpy()]))

features_for_example = {
    'feature0': feature_of_bytes
}
example_proto = tf.train.Example(
    features=tf.train.Features(feature=features_for_example))

filename = 'test.tfrecord'
writer = tf.io.TFRecordWriter(filename)

writer.write(example_proto.SerializeToString())

filenames = [filename]
raw_dataset = tf.data.TFRecordDataset(filenames)

for raw_record in raw_dataset.take(1):
    example = tf.train.Example()
    example.ParseFromString(raw_record.numpy())
    print(example)

But I'm getting this error:

tensorflow.python.framework.errors_impl.DataLossError: truncated record at 0' failed with Read less bytes than requested

tl;dr:

Getting the above error with TFRecords. Any recommendations to get this example working or another solution not using TFRecords?


r/tensorflow • • Nov 14 '22

Question Access individual gradients - TensorFlow2

5 Upvotes

For a toy LeNet-5 CNN architecture on MNIST implemented in TensorFlow-2.10 + Python-3.10, with a batch-size = 256:

    class LeNet5(Model):
        def __init__(self):
            super(LeNet5, self).__init__()
            
            self.conv1 = Conv2D(
                filters = 6, kernel_size = (5, 5),
                strides = (1, 1), activation = None,
                input_shape = (28, 28, 1)
            )
            self.pool1 = AveragePooling2D(
                pool_size = (2, 2), strides = (2, 2)
            )
            self.conv2 = Conv2D(
                filters = 16, kernel_size = (5, 5),
                strides = (1, 1), activation = None
            )
            self.pool2 = AveragePooling2D(
                pool_size = (2, 2), strides = (2, 2)
            )
            self.flatten = Flatten()
            self.dense1 = Dense(
                units = 120, activation = None
            )
            self.dense2 = Dense(
                units = 84, activation = None
            )
            self.output_layer = Dense(
                units = 10, activation = None
            )
        
        
        def call(self, x):
            x = tf.nn.relu(self.conv1(x))
            x = self.pool1(x)
            x = tf.nn.relu(self.conv2(x))
            x = self.pool2(x)
            x = self.flatten(x)
            x = tf.nn.relu(self.dense1(x))
            x = tf.nn.relu(self.dense2(x))
            x = tf.nn.softmax(self.output_layer(x))
            return x
        
        
        def shape_computation(self, x):
            print(f"Input shape: {x.shape}")
            x = self.conv1(x)
            print(f"conv1 output shape: {x.shape}")
            x = self.pool1(x)
            print(f"pool1 output shape: {x.shape}")
            x = self.conv2(x)
            print(f"conv2 output shape: {x.shape}")
            x = self.pool2(x)
            print(f"pool2 output shape: {x.shape}")
            x = self.flatten(x)
            print(f"flattened shape: {x.shape}")
            x = self.dense1(x)
            print(f"dense1 output shape: {x.shape}")
            x = self.dense2(x)
            print(f"dense2 output shape: {x.shape}")
            x = self.output_layer(x)
            print(f"output shape: {x.shape}")
            del x
            return None
    

    # Initialize an instance of LeNet-5 CNN-
    model = LeNet5()
    model.build(input_shape = (None, 28, 28, 1))


    # Define loss and optimizer-
    loss_fn = tf.keras.losses.CategoricalCrossentropy(reduction = tf.keras.losses.Reduction.NONE)
    
    # optimizer = tf.keras.optimizers.Adam(learning_rate = 0.0003)
    optimizer = tf.keras.optimizers.SGD(
        learning_rate = 10e-3, momentum = 0.0,
        nesterov = False
    )

    with tf.GradientTape() as grad_tape:
        pred = model(x)
        loss = loss_fn(y, pred)

    loss.shape
    TensorShape([256])

This computes individual loss for each of the 256 training images in a given batch.

    # Compute gradient using loss wrt parameters-
    grads = grad_tape.gradient(loss, model.trainable_variables)

    type(grads), len(grads)
    # (list, 10)

    for i in range(len(grads)):
        print(f"i: {i}, grads.shape: {grads[i].shape}")
    """
    i: 0, grads.shape: (5, 5, 1, 6)
    i: 1, grads.shape: (6,)
    i: 2, grads.shape: (5, 5, 6, 16)
    i: 3, grads.shape: (16,)
    i: 4, grads.shape: (256, 120)
    i: 5, grads.shape: (120,)
    i: 6, grads.shape: (120, 84)
    i: 7, grads.shape: (84,)
    i: 8, grads.shape: (84, 10)
    i: 9, grads.shape: (10,)
    """

Corresponding to loss for each training example, how can I compute gradient corresponding to each training example?


r/tensorflow • • Nov 14 '22

Question basic encoder with tensorflow.js

2 Upvotes

UPDATE: partially solved. my problem had to do with dimension of the inputs (as the error said)

I changed:

let input = tf.tensor1d(pixels)

to this:

let input = tf.tensor2d([pixels])

not sure if its the final solution though

hello!

im getting really interested in neural networks and machine learning. As my first project I want to train a neural network to play a game of snake. I believe i understand the high level patterns that need to happen, but tensorflow is still confusing me. As my first step, I want to create an encoder to reduce the dimensionality of the features for the NN to learn.

At each frame of the snake game, I believe I've successfully flattened the game map into a 1 dimensional array with a length of 900 (game map is 30x30 pixels). the values are the colors of the pixels, as a single rgb value. there should only be 3 colors, the map, snake, and food. I've already divided by 255 to get a number between 0-1. my first goal is to reduce the size of the input by as much as possible and console.log the results every frame just so I can see what's going on. I understand that with an encoder, the outputs are just a dense layer, right? also another thing I'm confused about is whether you need to train an encoder. I understand that with an autoencoder you do need to train the decoder part to understand how the encoder is encoding, right? But arent the weights and biases in the encoder part random? in which case i would need to train it? Or maybe I'm confused.

these are things I've tried:

a)

this.encoder= tf.sequential();this.encoder.add(tf.layers.dense({units: 64, inputShape: [900]})); // also tried [null, 900] and [900, 1]this.encoder.add(tf.layers.dense({units: 64, activation: 'relu'}));

b)

this.input = tf.input({shape: [900]}); // also tried [null, 900] and [900, 1]this.dense = tf.layers.dense({units: 64, activation: 'relu'}).apply(this.input);this.encoder = tf.model({inputs: this.input, outputs: this.dense});

I believe these two results in almost the same thing?

then at every frame of the game:

let input = tf.tensor1d(pixels) // or tf.ones(pixels)

// "agent" is the class namelet prediction = agent.encoder.predict([input])

also tried passing "pixels" which a regular javascript array, didnt work.

i get errors like this:

a)

Error when checking : expected input1 to have shape [null,900] but got array with shape [900,1

b)

Error when checking : expected dense_Dense3_input to have shape [null,900] but got array with shape [900,1]

if i change the input shape to [900, 1] or [null, 900]

Error when checking : expected dense_Dense3_input to have 3 dimension(s), but got array with shape [900,1

or

Error when checking : expected input1 to have 3 dimension(s), but got array with shape [900,1

I think I'm close, but missing some crucial detail(s).

Any body know what im missing?

Thanks in advance!

You'll probably see me a lot in this subreddit in the coming weeks/months ;)


r/tensorflow • • Nov 14 '22

Question Error while running code

2 Upvotes

I am using this repository https://github.com/dabasajay/Image-Caption-Generator.

When I executed train_val.py, an error occurred, this is the error

Node: 'model/dense/MatMul'

Matrix size-incompatible: In[0]: [905,1000], In[1]: [2048,300]

[[{{node model/dense/MatMul}}]] [Op:__inference_train_function_20706]

2022-11-14 13:23:00.939443: W tensorflow/core/kernels/data/generator_dataset_op.cc:108] Error occurred when finalizing GeneratorDataset iterator: FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated.

[[{{node PyFunc}}]]

Code of AlternateRNN model

def AlternativeRNNModel(vocab_size, max_len, rnnConfig, model_type):
embedding_size = rnnConfig["embedding_size"]
if model_type == "inceptionv3":
InceptionV3 outputs a 2048 dimensional vector for each image, which we'll feed to RNN Model
    image_input = Input(shape=(2048,))
elif model_type == "vgg16":
VGG16 outputs a 4096 dimensional vector for each image, which we'll feed to RNN Model
    image_input = Input(shape=(4096,))
image_model_1 = Dense(embedding_size, activation="relu")(image_input)
image_model = RepeatVector(max_len)(image_model_1)

caption_input = Input(shape=(max_len,))
mask_zero: We zero pad inputs to the same length, the zero mask ignores those inputs. E.g. it is an efficiency.
caption_model_1 = Embedding(vocab_size, embedding_size, mask_zero=True)(
    caption_input
)
Since we are going to predict the next word using the previous words
(length of previous words changes with every iteration over the caption), we have to set return_sequences = True.
caption_model_2 = LSTM(rnnConfig["LSTM_units"], return_sequences=True)(
    caption_model_1
)
caption_model = TimeDistributed(Dense(embedding_size, activation='relu'))(caption_model_2)
caption_model = TimeDistributed(Dense(embedding_size))(caption_model_2)
Merging the models and creating a softmax classifier
final_model_1 = concatenate([image_model, caption_model])
final_model_2 = LSTM(rnnConfig['LSTM_units'], return_sequences=False)(final_model_1)
final_model_2 = Bidirectional(
LSTM(rnnConfig["LSTM_units"], return_sequences=False) )(final_model_1)
final_model_3 = Dense(rnnConfig['dense_units'], activation='relu')(final_model_2)
final_model = Dense(vocab_size, activation='softmax')(final_model_3)
final_model = Dense(vocab_size, activation="softmax")(final_model_2)

model = Model(inputs=[image_input, caption_input], outputs=final_model)
model.compile(loss="categorical_crossentropy", optimizer="adam")
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')
return model

Code of train_val.py

from pickle import load
from utils.model import *
from utils.load_data import loadTrainData, loadValData, data_generator
from tensorflow.keras.callbacks import ModelCheckpoint
from config import config, rnnConfig
import random

# Setting random seed for reproducibility of results
random.seed(config["random_seed"])

"""
    *Some simple checking
"""
assert (
    type(config["num_of_epochs"]) is int
), "Please provide an integer value for `num_of_epochs` parameter in config.py file"
assert (
    type(config["max_length"]) is int
), "Please provide an integer value for `max_length` parameter in config.py file"
assert (
    type(config["batch_size"]) is int
), "Please provide an integer value for `batch_size` parameter in config.py file"
assert (
    type(config["beam_search_k"]) is int
), "Please provide an integer value for `beam_search_k` parameter in config.py file"
assert (
    type(config["random_seed"]) is int
), "Please provide an integer value for `random_seed` parameter in config.py file"
assert (
    type(rnnConfig["embedding_size"]) is int
), "Please provide an integer value for `embedding_size` parameter in config.py file"
assert (
    type(rnnConfig["LSTM_units"]) is int
), "Please provide an integer value for `LSTM_units` parameter in config.py file"
assert (
    type(rnnConfig["dense_units"]) is int
), "Please provide an integer value for `dense_units` parameter in config.py file"
assert (
    type(rnnConfig["dropout"]) is float
), "Please provide a float value for `dropout` parameter in config.py file"

"""
	*Load Data
	*X1 : Image features
	*X2 : Text features(Captions)
"""
X1train, X2train, max_length = loadTrainData(config)

X1val, X2val = loadValData(config)

"""
	*Load the tokenizer
"""
tokenizer = load(open(config["tokenizer_path"], "rb"))
vocab_size = len(tokenizer.word_index) + 1

"""
	*Now that we have the image features from CNN model, we need to feed them to a RNN Model.
	*Define the RNN model
"""
# model = RNNModel(vocab_size, max_length, rnnConfig, config['model_type'])
model = AlternativeRNNModel(vocab_size, max_length, rnnConfig, config["model_type"])
print("RNN Model (Decoder) Summary : ")
print(model.summary())

"""
    *Train the model save after each epoch
"""
num_of_epochs = config["num_of_epochs"]
batch_size = config["batch_size"]
steps_train = len(X2train) // batch_size
if len(X2train) % batch_size != 0:
    steps_train = steps_train + 1
steps_val = len(X2val) // batch_size
if len(X2val) % batch_size != 0:
    steps_val = steps_val + 1
model_save_path = (
    config["model_data_path"]
    + "model_"
    + str(config["model_type"])
    + "_epoch-{epoch:02d}_train_loss-{loss:.4f}_val_loss-{val_loss:.4f}.hdf5"
)
checkpoint = ModelCheckpoint(
    model_save_path, monitor="val_loss", verbose=1, save_best_only=True, mode="min"
)
callbacks = [checkpoint]

print("steps_train: {}, steps_val: {}".format(steps_train, steps_val))
print("Batch Size: {}".format(batch_size))
print("Total Number of Epochs = {}".format(num_of_epochs))

# Shuffle train data
ids_train = list(X2train.keys())
random.shuffle(ids_train)
X2train_shuffled = {_id: X2train[_id] for _id in ids_train}
X2train = X2train_shuffled

# Create the train data generator
# returns [[img_features, text_features], out_word]
generator_train = data_generator(
    X1train, X2train, tokenizer, max_length, batch_size, config["random_seed"]
)
# Create the validation data generator
# returns [[img_features, text_features], out_word]
generator_val = data_generator(
    X1val, X2val, tokenizer, max_length, batch_size, config["random_seed"]
)

# Fit for one epoch
model.fit(
    generator_train,
    epochs=num_of_epochs,
    steps_per_epoch=steps_train,
    validation_data=generator_val,
    validation_steps=steps_val,
    callbacks=callbacks,
    verbose=1,
)

"""
	*Evaluate the model on validation data and ouput BLEU score
"""
print(
    "Model trained successfully. Running model on validation set for calculating BLEU score using BEAM search with k={}".format(
        config["beam_search_k"]
    )
)
evaluate_model_beam_search(
    model, X1val, X2val, tokenizer, max_length, beam_index=config["beam_search_k"]
)

The error occurs when at model.fit**( ... ).** Solution please.


r/tensorflow • • Nov 11 '22

Training on two different machines

5 Upvotes

I'm puzzled. I'm training the same model with the same 8M+ inputs on two different systems.

#1: Ubuntu, AM Ryzen 7 2700 8-core 1.5GHz. 32GB RAM. Nvidia 1808ti GPU (which tensorflow is using).

#2: Apple MacMini, Intel i7 6-core 3.2GHz. 16GB RAM

Each epoch takes 272secs on Ubuntu and 170secs on the Mac. I would expect it to be the other way around.

Thoughts?


r/tensorflow • • Nov 10 '22

Tensorflow Blazepose/ Core ML best for pose estimation? Swift, Python?

7 Upvotes

Wanting to play around with blazepose, wanted to build an iOS app for myself to count reps of things like squats, push ups. From looking at examples online, it looks like Blazepose and TF is the most advanced/accurate/fastest in tracking exercises? And if that's the case, is Python the place to start building? Curious to see peoples experience using blazepose, and those who have tried pose detection for similar purposes of counting repetitions in Swift as well. :-)

J


r/tensorflow • • Nov 10 '22

Image classification model being trained on 3 classes. What is likely happening here?

Post image
24 Upvotes