How do I plot a Keras/Tensorflow subclassing API model?

I’ve found some workaround to plot with the model sub-classing API. For the obvious reason Sub-Classing API doesn’t support Sequential or Functional API like model.summary() and nice visualization using plot_model. Here, I will demonstrate both. class my_model(keras.Model): def __init__(self, dim): super(my_model, self).__init__() self.Base = keras.keras.applications.VGG16( input_shape=(dim), include_top = False, weights=”imagenet” ) self.GAP = L.GlobalAveragePooling2D() self.BAT … Read more

How to fix “ResourceExhaustedError: OOM when allocating tensor”

OOM stands for “out of memory”. Your GPU is running out of memory, so it can’t allocate memory for this tensor. There are a few things you can do: Decrease the number of filters in your Dense, Conv2D layers Use a smaller batch_size (or increase steps_per_epoch and validation_steps) Use grayscale images (you can use tf.image.rgb_to_grayscale) … Read more

What is right batch normalization function in Tensorflow?

Just to add to the list, there’re several more ways to do batch-norm in tensorflow: tf.nn.batch_normalization is a low-level op. The caller is responsible to handle mean and variance tensors themselves. tf.nn.fused_batch_norm is another low-level op, similar to the previous one. The difference is that it’s optimized for 4D input tensors, which is the usual … Read more

How to display custom images in TensorBoard using Keras?

So, the following solution works well for me: import tensorflow as tf def make_image(tensor): “”” Convert an numpy representation image to Image protobuf. Copied from https://github.com/lanpa/tensorboard-pytorch/ “”” from PIL import Image height, width, channel = tensor.shape image = Image.fromarray(tensor) import io output = io.BytesIO() image.save(output, format=”PNG”) image_string = output.getvalue() output.close() return tf.Summary.Image(height=height, width=width, colorspace=channel, encoded_image_string=image_string) … Read more

ValueError: Tensor must be from the same graph as Tensor with Bidirectinal RNN in Tensorflow

TensorFlow stores all operations on an operational graph. This graph defines what functions output to where, and it links it all together so that it can follow the steps you have set up in the graph to produce your final output. If you try to input a Tensor or operation on one graph into a … Read more

looking for source code of from gen_nn_ops in tensorflow

You can’t find this source because the source is automatically generated by bazel. If you build from source, you’ll see this file inside bazel-genfiles. It’s also present in your local distribution which you can find using inspect module. The file contains automatically generated Python wrappers to underlying C++ implementations, so it basically consists of a … Read more

What is the difference between Keras model.evaluate() and model.predict()?

The model.evaluate function predicts the output for the given input and then computes the metrics function specified in the model.compile and based on y_true and y_pred and returns the computed metric value as the output. The model.predict just returns back the y_pred So if you use model.predict and then compute the metrics yourself, the computed … Read more

Error when checking model input: expected convolution2d_input_1 to have 4 dimensions, but got array with shape (32, 32, 3)

The input shape you have defined is the shape of a single sample. The model itself expects some array of samples as input (even if its an array of length 1). Your output really should be 4-d, with the 1st dimension to enumerate the samples. i.e. for a single image you should return a shape … Read more