โŒ

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

ImportError: No module named tensorflow - Can't install Tensorflow

I am trying to install tensorflow on mac and it's giving me this error.

ImportError: No module named tensorflow

Here is what I have done in the terminal

sudo easy_install pip
sudo easy_install --upgrade six
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/tensorflow-0.9.0-py3-none-any.whl
sudo -H pip3 install --upgrade $TF_BINARY_URL

After that I try to run python and tensorflow to check my installation. It doesn't work. I have spent 3 hours on the problem.

keras (.h5) model to tensorflow-lite (.tflite) model conversion

I am trying to convert a .h5 keras model to a .tflite model. But the conversion results in core dumped error. Here's the script that I am running,

import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense

# Create a simple Keras model
model = Sequential([
    Dense(64, activation='relu', input_shape=(784,)),
    Dense(64, activation='relu'),
    Dense(10, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# Convert the Keras model to TensorFlow Lite model
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

# Save the TensorFlow Lite model to a file
with open('model.tflite', 'wb') as f:
    f.write(tflite_model)

print("TensorFlow Lite model saved successfully!")

I am getting this error, if I run the script

2024-04-01 12:27:04.793910: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:268] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
loc(fused["ReadVariableOp:", callsite("sequential_1/dense_1/Add/ReadVariableOp@__inference_serving_default_98"
callsite("/home/spoon/Documents/GTSRB/lib/python3.9/site-packages/keras/src/ops/numpy.py":311:1 at callsite("/home/spoon/Documents/GTSRB/lib/python3.9/site-packages/keras/src/backend/tensorflow/sparse.py":491:1 at callsite("/home/spoon/Documents/GTSRB/lib/python3.9/site-packages/keras/src/backend/tensorflow/numpy.py":35:1 at "/home/spoon/Documents/GTSRB/lib/python3.9/site-packages/keras/src/backend/tensorflow/core.py":64:1)))))))))))))))))))))))))))]): error: missing attribute 'value'
LLVM ERROR: Failed to infer result type(s).
Aborted (core dumped)

OS: Ubuntu 20.04.6 LTS

Python version: 3.9.18

pip freeze info:

keras==3.1.1
keras-core==0.1.7
keras-cv==0.8.2
tensorboard==2.16.2
tensorboard-data-server==0.7.2
tensorflow==2.16.1
tensorflow-datasets==4.9.3
tensorflow-io-gcs-filesystem==0.36.0
tensorflow-metadata==1.14.0

Keras Sequential Unhashable type: 'list error at import

I'm having problems with running a deep q-learning model with Keras-RL and OpenAI Gym in Python. In particular I get an error when loading the Sequential model from the keras package.

The code is the following:

from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.optimizers import Adam
from rl.agents import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory

class DeepQL:
    def __init__(self, env):
        self.env = env
        self.actions = env.action_space.n
        self.states  = env.observation_space.shape[0]

    def build_model(self, states, actions):

        model = Sequential()
        model.add(Flatten(input_shape=(1,states)))
        model.add(Dense(24, activation='relu'))
        model.add(Dense(24, activation='relu'))
        model.add(Dense(actions, activation='linear'))

        return model

    def build_agent(self):
        model = self.build_model(self.states, self.actions)
        policy = BoltzmannQPolicy()
        memory = SequentialMemory(limit=50000, window_length=1)
        dqn = DQNAgent(model=model, memory=memory, policy=policy, nb_actions=self.actions, nb_steps_warmup=10, target_model_update=1e-2)
        return dqn

    def compile(self):
        dqn = self.build_agent()
        dqn.compile(Adam(lr=1e-3), metrics=['mae'])
        dqn.fit(self.env, nb_steps=5000, visualize=False, verbose=1)

The error I get is the following:

Traceback (most recent call last):
  File "/Users/albi/PycharmProjects/replaybg_reinforce/py_replay_bg/reinforcementLearning/Agents/deepQL.py", line 1, in <module>
    from keras.models import Sequential
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/__init__.py", line 8, in <module>
    from keras import _tf_keras
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/_tf_keras/__init__.py", line 1, in <module>
    from keras._tf_keras import keras
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/_tf_keras/keras/__init__.py", line 8, in <module>
    from keras import activations
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/activations/__init__.py", line 8, in <module>
    from keras.src.activations import deserialize
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/__init__.py", line 1, in <module>
    from keras.src import activations
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/activations/__init__.py", line 3, in <module>
    from keras.src.activations.activations import elu
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/activations/activations.py", line 1, in <module>
    from keras.src import backend
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/backend/__init__.py", line 10, in <module>
    from keras.src.backend.common.keras_tensor import KerasTensor
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/backend/common/keras_tensor.py", line 2, in <module>
    from keras.src.utils import tree
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/keras/src/utils/tree.py", line 12, in <module>
    from tensorflow.python.trackable.data_structures import ListWrapper
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/__init__.py", line 45, in <module>
    from tensorflow._api.v2 import __internal__
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/_api/v2/__internal__/__init__.py", line 8, in <module>
    from tensorflow._api.v2.__internal__ import autograph
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/_api/v2/__internal__/autograph/__init__.py", line 8, in <module>
    from tensorflow.python.autograph.core.ag_ctx import control_status_ctx # line: 34
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/autograph/core/ag_ctx.py", line 21, in <module>
    from tensorflow.python.autograph.utils import ag_logging
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/autograph/utils/__init__.py", line 17, in <module>
    from tensorflow.python.autograph.utils.context_managers import control_dependency_on_returns
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/autograph/utils/context_managers.py", line 19, in <module>
    from tensorflow.python.framework import ops
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 5906, in <module>
    ) -> Optional[Callable[[Any], message.Message]]:
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 243, in inner
    return func(*args, **kwds)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 316, in __getitem__
    return self._getitem(self, parameters)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 433, in Optional
    return Union[arg, type(None)]
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 243, in inner
    return func(*args, **kwds)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 316, in __getitem__
    return self._getitem(self, parameters)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 421, in Union
    parameters = _remove_dups_flatten(parameters)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 215, in _remove_dups_flatten
    all_params = set(params)
TypeError: unhashable type: 'list'

It seems the problem is related to something in the packages and not to something I wrote but I may be wrong. I have no idea how to solve this. I am running the latest versions of all the imported packages on Python 3.9

Image classification on local machine but even numbered epoch are significantly faster than odd numbered epoch

I am training for an image classification on my local machine using visual studio code but every even numbered epoch are significantly faster than odd numbered epoch, anyone faced this issue before? I looked everywhere on google but no one seem to mention this irregularity.

I can't properly integrate early stopping callbacks because of this as the val_loss for the even numbered epoch are messed up. I tried running in google collab which works fine but the free version is limited, any ideas what I can do to solve this? training result image for my baseline model The issue persist for my more complex models.

# Define an EarlyStopping callback
early_stopping = callbacks.EarlyStopping(
    monitor='val_loss',            # Metric to monitor for early stopping (validation loss)
    patience=5,                    # Number of epochs with no improvement before stopping
    min_delta=1e-7,                # Minimum change in monitored metric to be considered an improvement
    verbose=1,                     # Verbosity level (1 for updates)
    restore_best_weights=True,     # Restore model weights to the best state when stopped
)

# Define a ReduceLROnPlateau callback
plateau = callbacks.ReduceLROnPlateau(
    monitor='val_loss',            # Metric to monitor for learning rate reduction (validation loss)
    factor=0.5,                    # Factor by which the learning rate will be reduced (e.g., 0.2 means lr *= 0.2)
    patience=2,                    # Number of epochs with no improvement before reducing the learning rate
    min_delta=1e-6,                # Minimum change in monitored metric to trigger a reduction
    cooldown=0,                    # Number of epochs to wait after a reduction before resuming normal operation
    verbose=1                      # Verbosity level (1 for updates)
)

def baselineCNNModel():
  # Input layer
  inputs = Input(shape=(imageH, imageW, 3))

  # Convolutional layers
  x = Conv2D(32, (3, 3), activation='relu')(inputs)
  x = MaxPooling2D((2, 2))(x)
  x = Conv2D(64, (3, 3), activation='relu')(x)
  x = MaxPooling2D((2, 2))(x)

  # Flatten layer
  x = Flatten()(x)

  # Fully connected layers
  x = Dense(64, activation='relu')(x)
  output = Dense(1, activation='sigmoid')(x) 

  # Create model
  model = Model(inputs=[inputs], outputs=output)

  return model

# Clear the Keras session to release resources
keras.backend.clear_session()

# Create a CNN model using the defined 'baseline_model' function
baselineCNN = baselineCNNModel()

# Compile the CNN model with specified loss, optimizer, and metrics
baselineCNN.compile(
    loss='binary_crossentropy',  # Binary cross-entropy loss for binary classification
    optimizer=keras.optimizers.Adam(),  # Adam optimizer with a custom learning rate
    metrics=['binary_accuracy']  # Metric to monitor during training (binary accuracy)
)

# Display a summary of the model architecture
baselineCNN.summary()

# Train the model
bCNNHist = baselineCNN.fit(trainDS,
          validation_data=valDS,
          epochs=10,
          batch_size=batchSize,
          callbacks=[plateau],  # early_stopping, Callbacks for early stopping and learning rate reduction
          steps_per_epoch=int(len(trainDF) / batchSize),  # Number of steps per training epoch
          validation_steps=int(len(valDF) / batchSize)    # Number of steps per validation epoch)
)

tflite_flutter_helper gives issure in deployment

I am trying to deploy my image classification app to Android and I need to build the app bundle. I know tflite_flutter_helper is outdated but I don't know what to use to replace it.

I run flutter build appbundle However I get the following error

FAILURE: Build failed with an exception.

* What went wrong:
The Android Gradle plugin supports only Kotlin Gradle plugin version 1.5.20 and higher.
The following dependencies do not satisfy the required version:
project ':tflite_flutter_helper' -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
Running Gradle task 'bundleRelease'...                           2,278ms
Gradle task bundleRelease failed with exit code 1

Speed up Deep Learning Inference without batching samples?

For algorithmic reasons, I would like to perform inference sequentially, sample by sample instead of performing a batched sample. It is Deep Neuronal network with the following structure:

Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense (Dense)               (None, 64)                2624      
                                                                 
 dense_1 (Dense)             (None, 128)               8320      
                                                                 
 dense_2 (Dense)             (None, 128)               16512     
                                                                 
 dense_3 (Dense)             (None, 1)                 129       
                                                                 
=================================================================
Total params: 27585 (107.75 KB)
Trainable params: 27585 (107.75 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
None

My testing shows that the batched inference is considerably faster (1.34 seconds):

start = time.time()
y_pred = model.predict(x_test_feature_space)
end = time.time()
print("Exeuction time: ", end - start)
print("Execution time per sample: ", (end-start)/len(x_test_feature_space))

# 672/672 [==============================] - 1s 1ms/step
# Exeuction time:  1.345379114151001
# Execution time per sample:  6.26194607470794e-05

Whereas, the sequential, sample by sample inference is very slow (24 minutes):

y_pred = []
timings = []

start_total = time.time()
for x in x_test_feature_space:
    start = time.time()
    y_pred.append(model.predict(np.array([x,]), verbose=False))
    end = time.time()
    timings.append(end-start)
end_total = time.time()

print("Exeuction time: ", end_total - start_total)
print("Average time per sample: ", sum(timings)/len(timings))

# Exeuction time:  1467.2437105178833
# Average time per sample:  0.06825089837496631

Seeing this contrast, makes me believe the inference cost is lower than simply loading up the model.

Is there any approach I could investigate to improve the timing of sequential (sample by sample) inference? What are the best practices?

Channel First vs Channel Last OpenVINO Model Optimizer

Right now I'm trying to use a TensorFlow model and turn its default channels last behaviour to channel first behavior when using model optimizer. I'm confused why model optimizer is not finding the name of my output node. I put it through netron to visualize and the name of the output matches. I'm not even too sure why my input is named input_input when I named it input explicitly in my TensorFlow code.

This is the command I'm running:

mo --saved_model_dir ./DepthWise --layout input_input(nhwc->nchw),output(nhwc->nchw) --output_dir ./output --model_name DepthWise -b 1

This is my TensorFlow code:

def DepthwiseSeparableConvNet():
    model = Sequential()
    # Assuming input_shape is for an image of size 224x224 with 3 channels (RGB)
    input_shape = (224, 224, 3)
    
    # When Model Optimizer imports this graph, it inserts a transpose that cancels this out
    # Depthwise Convolution
    model.add(DepthwiseConv2D(kernel_size=(3, 3), strides=(1, 1), padding='same', input_shape=input_shape, name='input', data_format='channels_last'))
    
    # Pointwise Convolution
    model.add(Conv2D(filters=64, kernel_size=(1, 1), strides=(1, 1), padding='same', name="output", data_format='channels_last'))
    
    return model

def main(): 
    model = DepthwiseSeparableConvNet()
    model.summary()
    tf.saved_model.save(model, "DepthWise")

if __name__ == "__main__":
    main()

My error message that shows the node is not found

[ ERROR ]  Input/Output with name output wasn't found! 
 For more information please refer to Model Optimizer FAQ, question #83. (https://docs.openvino.ai/latest/openvino_docs_MO_DG_prepare_model_Model_Optimizer_FAQ.html?question=83#question-83)
[ ERROR ]  offline transformations step has failed.
[ INFO ] You can also try to use new TensorFlow Frontend (preview feature as of 2022.3) by adding `--use_new_frontend` option into Model Optimizer command-line.
Find more information about new TensorFlow Frontend at https://docs.openvino.ai/latest/openvino_docs_MO_DG_TensorFlow_Frontend.html
mo --saved_model_dir ./DepthWise --layout input_input(nhwc->nchw),output(nhwc->nchw) --output_dir ./output --model_name DepthWise -b 1 FAILED

Cuda 12 + tf-nightly 2.12: Could not find cuda drivers on your machine, GPU will not be used, while every checking is fine and in torch it works

  • tf-nightly version = 2.12.0-dev2023203
  • Python version = 3.10.6
  • CUDA drivers version = 525.85.12
  • CUDA version = 12.0
  • Cudnn version = 8.5.0
  • I am using Linux (x86_64, Ubuntu 22.04)
  • I am coding in Visual Studio Code on a venv virtual environment

I am trying to run some models on the GPU (NVIDIA GeForce RTX 3050) using tensorflow nightly 2.12 (to be able to use Cuda 12.0). The problem that I have is that apparently every checking that I am making seems to be correct, but in the end the script is not able to detect the GPU. I've dedicated a lot of time trying to see what is happening and nothing seems to work, so any advice or solution will be more than welcomed. The GPU seems to be working for torch as you can see at the very end of the question.

I will show some of the most common checkings regarding CUDA that I did (Visual Studio Code terminal), I hope you find them useful:

  1. Check CUDA version:

    $ nvcc --version

    nvcc: NVIDIA (R) Cuda compiler driver
    Copyright (c) 2005-2023 NVIDIA Corporation
    Built on Fri_Jan__6_16:45:21_PST_2023
    Cuda compilation tools, release 12.0, V12.0.140
    Build cuda_12.0.r12.0/compiler.32267302_0
    
  2. Check if the connection with the CUDA libraries is correct:

    $ echo $LD_LIBRARY_PATH

    /usr/cuda/lib
    
  3. Check nvidia drivers for the GPU and check if GPU is readable for the venv:

    $ nvidia-smi

    +-----------------------------------------------------------------------------+
    | NVIDIA-SMI 525.85.12    Driver Version: 525.85.12    CUDA Version: 12.0     |
    |-------------------------------+----------------------+----------------------+
    | 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  On |                  N/A |
    | N/A   40C    P5     6W /  20W |     46MiB /  4096MiB |     22%      Default |
    |                               |                      |                  N/A |
    +-------------------------------+----------------------+----------------------+
    
    +-----------------------------------------------------------------------------+
    | Processes:                                                                  |
    |  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
    |        ID   ID                                                   Usage      |
    |=============================================================================|
    |    0   N/A  N/A      1356      G   /usr/lib/xorg/Xorg                 45MiB |
    +-----------------------------------------------------------------------------+
    
  4. Add cuda/bin PATH and Check it:

    $ export PATH="/usr/local/cuda/bin:$PATH"

    $ echo $PATH

    /usr/local/cuda-12.0/bin:/home/victus-linux/Escritorio/MasterThesis_CODE/to_share/venv_master/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
    
  5. Custom function to check if CUDA is correctly installed: [function by Sherlock]

    function lib_installed() { /sbin/ldconfig -N -v $(sed 's/:/ /' <<< $LD_LIBRARY_PATH) 2>/dev/null | grep $1; }
    function check() { lib_installed $1 && echo "$1 is installed" || echo "ERROR: $1 is NOT installed"; }
    check libcuda
    check libcudart
    
    libcudart.so.12 -> libcudart.so.12.0.146
            libcuda.so.1 -> libcuda.so.525.85.12
            libcuda.so.1 -> libcuda.so.525.85.12
            libcudadebugger.so.1 -> libcudadebugger.so.525.85.12
    libcuda is installed
            libcudart.so.12 -> libcudart.so.12.0.146
    libcudart is installed
    
  6. Custom function to check if Cudnn is correctly installed: [function by Sherlock]

    function lib_installed() { /sbin/ldconfig -N -v $(sed 's/:/ /' <<< $LD_LIBRARY_PATH) 2>/dev/null | grep $1; }
    function check() { lib_installed $1 && echo "$1 is installed" || echo "ERROR: $1 is NOT installed"; }
    check libcudnn 
    
            libcudnn_cnn_train.so.8 -> libcudnn_cnn_train.so.8.8.0
            libcudnn_cnn_infer.so.8 -> libcudnn_cnn_infer.so.8.8.0
            libcudnn_adv_train.so.8 -> libcudnn_adv_train.so.8.8.0
            libcudnn.so.8 -> libcudnn.so.8.8.0
            libcudnn_ops_train.so.8 -> libcudnn_ops_train.so.8.8.0
            libcudnn_adv_infer.so.8 -> libcudnn_adv_infer.so.8.8.0
            libcudnn_ops_infer.so.8 -> libcudnn_ops_infer.so.8.8.0
    libcudnn is installed
    

So, once I did these previous checkings I used a script to evaluate if everything was finally ok and then the following error appeared:

import tensorflow as tf

print(f'\nTensorflow version = {tf.__version__}\n')
print(f'\n{tf.config.list_physical_devices("GPU")}\n')
2023-03-02 12:05:09.463343: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-03-02 12:05:09.489911: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-03-02 12:05:09.490522: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-03-02 12:05:10.066759: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT

Tensorflow version = 2.12.0-dev20230203

2023-03-02 12:05:10.748675: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:996] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355
2023-03-02 12:05:10.771263: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1956] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...

[]

Extra check: I tried to run a checking script on torch and in here it worked so I guess the problem is related with tensorflow/tf-nightly

import torch

print(f'\nAvailable cuda = {torch.cuda.is_available()}')

print(f'\nGPUs availables = {torch.cuda.device_count()}')

print(f'\nCurrent device = {torch.cuda.current_device()}')

print(f'\nCurrent Device location = {torch.cuda.device(0)}')

print(f'\nName of the device = {torch.cuda.get_device_name(0)}')
Available cuda = True

GPUs availables = 1

Current device = 0

Current Device location = <torch.cuda.device object at 0x7fbe26fd2ec0>

Name of the device = NVIDIA GeForce RTX 3050 Laptop GPU

Please, if you know something that might help solve this issue, don't hesitate on telling me.

Layer 'conv2d_11' expected 2 variables, but received 0 variables during loading. Expected: ['conv2d_11/kernel:0', 'conv2d_11/bias:0']

I wanted to load a model using tf.keras.models.load_model('ACM1_9035P.keras') as I usually do but all of a sudden this time it wouldn't load. I got the error message seen in the title. This method has worked many times before but this time it didn't. What's the problem and how can I fix it?

I can provide more information about the model and system if needed.

I used google colab to train, save and load the model. The model was saved using model.save() The model is saved a .keras file

Gradio on Colab has Issue with package (typing_extensions)

I am using Google Colab T4 session , where i need my gradio app as web UI for my model. The Notebook was OK , which means it was Running without any ERROR in the start , But one day suddenly it started Showing the following

ERROR:-

/usr/local/lib/python3.10/dist-packages/huggingface_hub/utils/_runtime.py:184: UserWarning: Pydantic is installed but cannot be imported. Please check your installation. `huggingface_hub` will default to not using Pydantic. Error message: '{e}'
  warnings.warn(
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-19-d705c8d6a1a0> in <cell line: 1>()
----> 1 import gradio as gr
      2 def input(area,bedroom,bathrooms,parking,stories):
      3   input = [area,bedroom,bathrooms,parking,stories]
      4   output = lm.predict([input])
      5   return int(output)

18 frames
/usr/local/lib/python3.10/dist-packages/pydantic/_internal/_core_utils.py in <module>
     14 from pydantic_core import CoreSchema, core_schema
     15 from pydantic_core import validate_core_schema as _validate_core_schema
---> 16 from typing_extensions import TypeAliasType, TypeGuard, get_args, get_origin
     17 
     18 from . import _repr

ImportError: cannot import name 'TypeAliasType' from 'typing_extensions' (/usr/local/lib/python3.10/dist-packages/typing_extensions.py)

Reasons for the Error (According to me) Due to Colab resent update create a clash of type_extension version

I tried to Make a Gradio app that Can predict House Price based on the Trained model

TypeError: Descriptors cannot not be created directly

I tried to install Ray, but it gave an error:

TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
 1. Downgrade the protobuf package to 3.20.x or lower.
 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

I tried to solve the problem and downgraded protobuf:

Name: protobuf
Version: 3.20.0
Summary: Protocol Buffers
Home-page: https://developers.google.com/protocol-buffers/
Author:
Author-email:
License: BSD-3-Clause
Location: d:\opencv\lib\site-packages
Requires:
Required-by: ray, tensorboard, tensorflow

But still the problem persists in Ray, TensorFlow, and Keras. My application isn't working any more. How can I fix it?

NameError: name 'scipy' is not defined in TensorFlow/Keras model.fit() call

I'm encountering a NameError: name scipy is not defined in my Python code when using the model.fit() method with TensorFlow/Keras. The error occurs specifically in the following block of code:

model.fit(
    train_generator,
    steps_per_epoch=len(train_generator),
    epochs=4,
    validation_data=validation_generator,
    validation_steps=len(validation_generator)
)

I have already installed SciPy using pip install scipy, and I'm not directly using SciPy in this code. The issue persists even after ensuring that SciPy is properly installed. The train_generator and validation_generator are data generators for training and validation data.

Can anyone help me understand why I'm getting this NameError and how to resolve it? I have checked my import statements and verified that SciPy is installed in my Python environment. If additional information is needed, please let me know. Thank you!

How the parameter vector of zeroes works in tensorflow?

So, I'm following book on Tensorflow by Chris Mattman. It is pretty buggy and outdated, but I'm still trying to get into ML with it (there are interesting concepts further in the book)

Here is the code:

import tensorflow as tf
tf.disable_v1_behavior()
#...
#...
#...
def model(X, w):
    terms = []
    for i in range(num_coeffs): #num_coeffs = 6
        term = tf.multiply(w[i], tf.pow(X, i))
        terms.append(term)
    return tf.add_n(terms)

w = tf.Variable([0.]*num_coeffs, name="parameters")
y_model = model(X, w) #X = tf.placeholder(tf.float32)

My question: how do we multiply 0. by some value in the variable and get anything different from 0? Edit: I forgot to mention, it is to be polynomial regression model =)

"unexpected indent" or "object has no attribute 'compile'" errors [duplicate]

I tried using this code from the Tensorflow basic regression tutorial:

def build_model():
  model = keras.Sequential([
    keras.layers.Dense(64, activation=tf.nn.relu, 
                       input_shape=(train_data.shape[1],)),
    keras.layers.Dense(64, activation=tf.nn.relu),
    keras.layers.Dense(1)
  ])

  optimizer = tf.train.RMSPropOptimizer(0.001)

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

model = build_model()
model.summary()

But I get an error that says:

>>>   optimizer = tf.train.RMSPropOptimizer(0.001)
  File "<stdin>", line 1
    optimizer = tf.train.RMSPropOptimizer(0.001)
    ^
IndentationError: unexpected indent

If I unindent the optimizer = ... line, the next line gives the same error. If I then unindent the model.compile ... line, I get this:

>>> model.compile(loss='mse',
...                 optimizer=optimizer,
...                 metrics=['mae'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'compile'

...followed by a bunch of other errors, probably resulting from that one.

The other tutorials have worked fine. What is wrong, and how do I fix it?

Adjust Image Segmentaion model (from TF tutorial) for binary masking

I need an Image Segmentation model for Tensorflow. Input is Image and Mask(binary, masked or non-masked), and output is image mask with 0 and 1.

I followed the Image segmentation tutorial from https://www.tensorflow.org/tutorials/images/segmentation

But now I want to run it for binary mask (without border class) on my dataset The new dataset is prepared and inputted to the model.fit. It must be fine.

How do I change this model to only 2 classes (non-masked and masked)?

base_model: keras.Model = tf.keras.applications.MobileNetV2(input_shape=[128, 128, 3], include_top=False)

# Use the activations of these layers
layer_names = [
    'block_1_expand_relu',   # 64x64
    'block_3_expand_relu',   # 32x32
    'block_6_expand_relu',   # 16x16
    'block_13_expand_relu',  # 8x8
    'block_16_project',      # 4x4
]
base_model_outputs = [base_model.get_layer(name).output for name in layer_names]

# Create the feature extraction model
down_stack = Model(inputs=base_model.input, outputs=base_model_outputs)

down_stack.trainable = False

up_stack = [
    pix2pix.upsample(512, 3),  # 4x4 -> 8x8
    pix2pix.upsample(256, 3),  # 8x8 -> 16x16
    pix2pix.upsample(128, 3),  # 16x16 -> 32x32
    pix2pix.upsample(64, 3),   # 32x32 -> 64x64
]

def unet_model(output_channels:int):
  inputs = layers.Input(shape=[128, 128, 3])

  # Downsampling through the model
  skips = down_stack(inputs)
  x = skips[-1]
  skips = reversed(skips[:-1])

  # Upsampling and establishing the skip connections
  for up, skip in zip(up_stack, skips):
    x = up(x)
    concat = layers.Concatenate()
    x = concat([x, skip])

  # This is the last layer of the model
  last = layers.Conv2DTranspose(
      filters=output_channels, kernel_size=3, strides=2,
      padding='same')  #64x64 -> 128x128

  x = last(x)

  return Model(inputs=inputs, outputs=x)

OUTPUT_CLASSES = 3

model = unet_model(output_channels=OUTPUT_CLASSES)

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

When I change OUTPUT_CLASSES to 2 it gives me an error:

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.

When OUTPUT_CLASSES is 1 the predicted mask is empty.

Maybe something else must be changed? I'm not into NN architecture yet, so I may not see something obvious.

Mixed Precision Training using Jax

I'm trying to understand how did Haiku achieve 2x speedup when training ResNet50 on ImageNet https://github.com/deepmind/dm-haiku/tree/main/examples/imagenet using the Deepmind JMP lib https://github.com/deepmind/jmp, and how to replicate this with other networks.

1- If we compare the time needed for a matrix multiplication in float32 and float16 on a GPU, we can barely see a 5% speedup, how can this become a 2x speedup as we scale the number of operations ?

2- Why do we need to apply mixed precision also on the network ? If you data and parameters are in float16 then aren't all ops inside the neural network in float16 too ?

3- Can I hope to see any speedup with a small fully connected network ? deep fully connected network ? Or only big vision-related neural network optimized specifically for that ?

ESRGAN : Required same input and output size

I am using the github ESRGAN of https://github.com/peteryuX/esrgan-tf2/blob/master/train_esrgan.py in tensorflow, and my inputs and outputs have the same size. I already have low_resolution and high_resolution images.

However, when using the code, the generator multiplies by 4 the image, which I want it to be the exact same size. So I can't re insert the generated image in the discriminator.

For example my batch if of size (8, 128,128, 1), and when passing in the generator, it output (16, 512, 512, 1). The discriminator takes a size of (None, 128, 128,1).

I would like that it doesn't upsample, but I feel that it is only what it does ...

The generator is :

def RRDB_Model(size, channels, cfg_net, gc=32, wd=0., name='RRDB_model'):
    """Residual-in-Residual Dense Block based Model """
    nf, nb = cfg_net['nf'], cfg_net['nb']
    lrelu_f = functools.partial(LeakyReLU, alpha=0.2)
    rrdb_f = functools.partial(ResInResDenseBlock, nf=nf, gc=gc, wd=wd)
    conv_f = functools.partial(Conv2D, kernel_size=3, padding='same',
                               bias_initializer='zeros',
                               kernel_initializer=_kernel_init(),
                               kernel_regularizer=_regularizer(wd))
    rrdb_truck_f = tf.keras.Sequential(
        [rrdb_f(name="RRDB_{}".format(i)) for i in range(nb)],
        name='RRDB_trunk')

    # extraction
    x = inputs = Input([size, size, channels], name='input_image')
    fea = conv_f(filters=nf, name='conv_first')(x)
    fea_rrdb = rrdb_truck_f(fea)
    trunck = conv_f(filters=nf, name='conv_trunk')(fea_rrdb)
    fea = fea + trunck

    # upsampling
    size_fea_h = tf.shape(fea)[1] if size is None else size
    size_fea_w = tf.shape(fea)[2] if size is None else size
    fea_resize = tf.image.resize(fea, [size_fea_h * 2, size_fea_w * 2],
                                 method='nearest', name='upsample_nn_1')
    fea = conv_f(filters=nf, activation=lrelu_f(), name='upconv_1')(fea_resize)
    fea_resize = tf.image.resize(fea, [size_fea_h * 4, size_fea_w * 4],
                                 method='nearest', name='upsample_nn_2')
    fea = conv_f(filters=nf, activation=lrelu_f(), name='upconv_2')(fea_resize)
    fea = conv_f(filters=nf, activation=lrelu_f(), name='conv_hr')(fea)
    out = conv_f(filters=channels, name='conv_last')(fea)

    return Model(inputs, out, name=name)

EDIT :

I deleted these lines :

fea_resize = tf.image.resize(fea, [size_fea_h * 2, size_fea_w * 2], method='nearest', name='upsample_nn_1')
fea_resize = tf.image.resize(fea, [size_fea_h * 4, size_fea_w * 4],method='nearest',name='upsample_nn_2')

and kept the convolutions layers "fea"

It worked !

โŒ
โŒ