My traied model with tensorflow on transformer pipeline pop out error - tensorflow

I’m using this github text summarization and I have a problem. I have been struggling for two weeks and I could not figure that out.
I'm using a notebook from this github repository:
https://github.com/flogothetis/Abstractive-Summarization-T5-Keras
notebook link:
https://github.com/flogothetis/Abstractive-Summarization-T5-Keras/blob/main/AbstractiveSummarizationT5.ipynb
After training the model I want to use huggingface transformer pipeline to generate summarizations.
from transformers import pipeline
summarizer = pipeline(“summarization”, model=model, tokenizer=“t5small”, framework=“tf”)
summarizer(“some text”)
but it returns the following error:
AttributeError: ‘Functional’ object has no attribute 'config’
Anyone has any idea how can i solve it?
full error:
AttributeError Traceback (most recent call last)
/tmp/ipykernel_20/1872405895.py in
----> 1 summarizer = pipeline(“summarization”, model=model, tokenizer=“t5-small”, framework=“tf”)
2
3 summarizer(“The US has passed the peak on new coronavirus cases, President Donald Trump said and predicted that some states would reopen”)
/opt/conda/lib/python3.7/site-packages/transformers/pipelines/init.py in pipeline(task, model, config, tokenizer, framework, revision, use_fast, use_auth_token, model_kwargs, **kwargs)
432 break
433
→ 434 return task_class(model=model, tokenizer=tokenizer, modelcard=modelcard, framework=framework, task=task, **kwargs)
/opt/conda/lib/python3.7/site-packages/transformers/pipelines/text2text_generation.py in init(self, *args, **kwargs)
37
38 def init(self, *args, **kwargs):
—> 39 super().init(*args, **kwargs)
40
41 self.check_model_type(
/opt/conda/lib/python3.7/site-packages/transformers/pipelines/base.py in init(self, model, tokenizer, modelcard, framework, task, args_parser, device, binary_output)
548
549 # Update config with task specific parameters
→ 550 task_specific_params = self.model.config.task_specific_params
551 if task_specific_params is not None and task in task_specific_params:
552 self.model.config.update(task_specific_params.get(task))
AttributeError: ‘Functional’ object has no attribute 'config’

Related

Evaluating the state value function when using the SAC agent of TF-Agents

The state value function v at states x is a quantity of interest of the Markov decision process (MDP) which I intend to solve. (My MDP is fully observable: observation = state.)
I use the SAC agent of TF-agents to learn action value function q(x,a) and policy π. Thus given a state x, the policy returns an approximately optimal action a = π(x) so that v(x) ≈ q(x,π(x)).
Problem description: How can one write q(x,π(x)) as a TF-Agents expression?
I can examine the problem already with the SAC tutorial https://www.tensorflow.org/agents/tutorials/7_SAC_minitaur_tutorial by adding the following lines to the end of the tutorial:
# Resetting the environment to obtain a TimeStep object
time_step = env.reset()
# An observation which respects the observation specs of env, corresponding to x above
observation = time_step.observation
# Calling the evaluation policy we obtain an action, this is essentially π(x) above
action = eval_policy.action(time_step).action
# I was expecting that the next line would return q(x,π(x))
critic_net((observation,action))
The reason for the last line was that the input_tensor_spec of a CriticNetwork was described as a tuple of (observation, action) in https://www.tensorflow.org/agents/api_docs/python/tf_agents/agents/ddpg/critic_network/CriticNetwork.
However instead critic_net((observation,action)) raises the following error:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-32-8446b099696b> in <module>
----> 1 critic_net((observation,action))
2 frames
/usr/local/lib/python3.8/dist-packages/tf_agents/networks/network.py in __call__(self, inputs, *args, **kwargs)
425 normalized_kwargs.pop("network_state", None)
426
--> 427 outputs, new_state = super(Network, self).__call__(**normalized_kwargs) # pytype: disable=attribute-error # typed-keras
428
429 nest_utils.assert_matching_dtypes_and_inner_shapes(
/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
/usr/local/lib/python3.8/dist-packages/tf_agents/agents/ddpg/critic_network.py in call(***failed resolving arguments***)
166 actions = layer(actions, training=training)
167
--> 168 joint = tf.concat([observations, actions], 1)
169 for layer in self._joint_layers:
170 joint = layer(joint, training=training)
InvalidArgumentError: Exception encountered when calling layer 'CriticNetwork' (type CriticNetwork).
{{function_node __wrapped__ConcatV2_N_2_device_/job:localhost/replica:0/task:0/device:CPU:0}} ConcatOp : Dimension 0 in both shapes must be equal: shape[0] = [28,1] vs. shape[1] = [8,1] [Op:ConcatV2] name: concat
Call arguments received by layer 'CriticNetwork' (type CriticNetwork):
• inputs=('tf.Tensor(shape=(28,), dtype=float32)', 'tf.Tensor(shape=(8,), dtype=float32)')
• step_type=()
• network_state=()
• training=False
Can someone help me with the evaluation of the critic network?

Issue Using tensorflow hub Universal Sentence Encoder with local runtime and GPU in Google Colab

I am working through a machine learning course to learn tensorflow. In one of the project I was performing text classification using a tensorflow_hub pre trained embedding, the Universal sentence encoder v4. The embeddings worked fine using the google Colab GPU, and also worked in my local runtime without my GPU. However, after I set up colab to be able to use my local GPU (RTX 3060), I started getting the error seen below. For reference, my python environment is through Anaconda, and I used conda install to install tensorflow_gpu and cudatoolkit and cudnn. I am not sure what this error means or how to even begin debugging it, any help would be greatly appreciated, thanks!
Code and error:
import tensorflow_hub as hub
tf_hub_embedding = hub.KerasLayer('https://tfhub.dev/google/universal-sentence-encoder/4',trainable=False,name='USE')
rand_sent = random.choice(train_sents)
print(f'Random sent: {rand_sent}\n')
print(f'Embedded sent: {tf_hub_embedding([rand_sent])[0][:30]}\n')
print(f'Embed length: {len(tf_hub_embedding([rand_sent])[0])}')
Random sent: Data of a Japanese study of patients with unresectable sacral chordoma showed comparable high control rates after hypofractionated carbon ion therapy only .
---------------------------------------------------------------------------
UnknownError Traceback (most recent call last)
Input In [55], in <cell line: 3>()
1 rand_sent = random.choice(train_sents)
2 print(f'Random sent: {rand_sent}\n')
----> 3 print(f'Embedded sent: {tf_hub_embedding([rand_sent])[0][:30]}\n')
4 print(f'Embed length: {len(tf_hub_embedding([rand_sent])[0])}')
File ~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
File ~\anaconda3\lib\site-packages\tensorflow_hub\keras_layer.py:229, in KerasLayer.call(self, inputs, training)
223 # ...but we may also have to pass a Python boolean for `training`, which
224 # is the logical "and" of this layer's trainability and what the surrounding
225 # model is doing (analogous to tf.keras.layers.BatchNormalization in TF2).
226 # For the latter, we have to look in two places: the `training` argument,
227 # or else Keras' global `learning_phase`, which might actually be a tensor.
228 if not self._has_training_argument:
--> 229 result = f()
230 else:
231 if self.trainable:
UnknownError: Exception encountered when calling layer "USE" (type KerasLayer).
Graph execution error:
JIT compilation failed.
[[{{node EncoderDNN/EmbeddingLookup/EmbeddingLookupUnique/embedding_lookup/mod}}]] [Op:__inference_restored_function_body_36706]
Call arguments received by layer "USE" (type KerasLayer):
• inputs=["'Data of a Japanese study of patients with unresectable sacral chordoma showed comparable high control rates after hypofractionated carbon ion therapy only .'"]
• training=None
I had this issue, and I solve it by downgrading my TensorFlow version to 2.8.0.
Using this command
pip install tensorflow==2.8.0

OSError: [Errno 95] Operation not supported: '/content/drive/Mask_RCNN' on Google Colab

Hello trying to use saved weights for a Mask RCNN model within colab and keep incurring the error message below. I have tried different ways of accessing the .h5 problem, which was an issue before, and now I have hit a brick wall. I have tried to train different parts of the model, nothing works. Nothing specific is available on google colab with these circumstances.
The following is the cell that throws the issue:
# Training dataset.
dataset_train = linkedinDataset()
dataset_train.load_dataset(dataset_dir, "train")
dataset_train.prepare()
# Validation dataset
dataset_val = linkedinDataset()
dataset_val.load_dataset(dataset_dir, "val")
dataset_val.prepare()
# *** This training schedule is an example. Update to your needs ***
#
#
#
print("Training network heads")
model.train(dataset_train,
dataset_val,
learning_rate=config.LEARNING_RATE,
epochs=5,
layers='heads')```
```Training network heads
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-19-174a93609e58> in <module>()
17 learning_rate=config.LEARNING_RATE,
18 epochs=5,
---> 19 layers='heads')
2 frames
/content/Mask_RCNN/mrcnn/model.py in train(self, train_dataset, val_dataset, learning_rate, epochs,
layers, augmentation, custom_callbacks, no_augmentation_sources)
2334 # Create log_dir if it does not exist
2335 if not os.path.exists(self.log_dir):
-> 2336 os.makedirs(self.log_dir)
2337
2338 # Callbacks
/usr/lib/python3.6/os.py in makedirs(name, mode, exist_ok)
208 if head and tail and not path.exists(head):
209 try:
--> 210 makedirs(head, mode, exist_ok)
211 except FileExistsError:
212 # Defeats race condition when another thread created the path
/usr/lib/python3.6/os.py in makedirs(name, mode, exist_ok)
218 return
219 try:
--> 220 mkdir(name, mode)
221 except OSError:
222 # Cannot rely on checking for EEXIST, since the operating system
OSError: [Errno 95] Operation not supported: '/content/drive/Mask_RCNN'```
You cannot use
'/content/drive/Mask_RCNN'
You should save to either
'/content/Mask_RCNN'
Or, if to use Google Drive,
'/content/drive/MyDrive/Mask_RCNN'

How can I solve this elusive error in my multi-GPU Pytorch setup?

I have spent the past day trying to figure out how to use multiple GPUs. In theory, parallelizing models across multiple GPUs is supposed to be as as easy as simply wrapping models with nn.DataParallel. However, I have found that this does not work for me. To use the most simple and canonical thing I could find for proof of this, I ran the code in the Data Parallelism tutorial, line for line.
I have tried everything from only having a specific permutation of my GPUs be visible to CUDA to reinstalling everything related to CUDA but can't figure out why I cannot run with multiple GPUs. Some information about my machine:
Operating System: Ubuntu 16.04
GPUS: 4 1080tis
Pytorch version: 1.01
CUDA version: 10.0
The error code is the following:
---------------------------------------------------------------------------
KeyboardInterrupt Traceback (most recent call last)
<ipython-input-3-0f0d83e9ef13> in <module>
1 for data in rand_loader:
2 input = data.to(device)
----> 3 output = model(input)
4 print("Outside: input size", input.size(),
5 "output_size", output.size())
/usr/local/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
/usr/local/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py in forward(self, *inputs, **kwargs)
141 return self.module(*inputs[0], **kwargs[0])
142 replicas = self.replicate(self.module, self.device_ids[:len(inputs)])
--> 143 outputs = self.parallel_apply(replicas, inputs, kwargs)
144 return self.gather(outputs, self.output_device)
145
/usr/local/lib/python3.6/site-packages/torch/nn/parallel/data_parallel.py in parallel_apply(self, replicas, inputs, kwargs)
151
152 def parallel_apply(self, replicas, inputs, kwargs):
--> 153 return parallel_apply(replicas, inputs, kwargs, self.device_ids[:len(replicas)])
154
155 def gather(self, outputs, output_device):
/usr/local/lib/python3.6/site-packages/torch/nn/parallel/parallel_apply.py in parallel_apply(modules, inputs, kwargs_tup, devices)
73 thread.start()
74 for thread in threads:
---> 75 thread.join()
76 else:
77 _worker(0, modules[0], inputs[0], kwargs_tup[0], devices[0])
/usr/local/lib/python3.6/threading.py in join(self, timeout)
1054
1055 if timeout is None:
-> 1056 self._wait_for_tstate_lock()
1057 else:
1058 # the behavior of a negative timeout isn't documented, but
/usr/local/lib/python3.6/threading.py in _wait_for_tstate_lock(self, block, timeout)
1070 if lock is None: # already determined that the C code is done
1071 assert self._is_stopped
-> 1072 elif lock.acquire(block, timeout):
1073 lock.release()
1074 self._stop()
KeyboardInterrupt:
Any insight into this error would be very much appreciated. From my relatively limited systems and CUDA knowledge, it has to do with some sort of locking, but I can't for the life of me figure out how to fix this.

CoreMLTools Keras simple Sequential Linear Regression model export error ('module' object has no attribute 'mobilenet')

I've created a very simple Sequential Linear Regression model using Keras 2.0.4 (TensorFlow 1.1.0 backend) and my coremltools (0.6.3) export is failing with this error message:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () ----> 1 coreml_model = coremltools.converters.keras.convert(model, input_names="input", output_names="output") /Users/Jacopo/anaconda/envs/KerasTensorFlowCoreML/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.pyc in convert(model, input_names, output_names, image_input_names, is_bgr, red_bias, green_bias, blue_bias, gray_bias, image_scale, class_labels, predicted_feature_name, predicted_probabilities_output) 489 predicted_probabilities_output = predicted_probabilities_output) 490 elif _HAS_KERAS2_TF: --> 491 from . import _keras2_converter 492 return _keras2_converter._convert(model = model, 493 input_names = input_names, /Users/Jacopo/anaconda/envs/KerasTensorFlowCoreML/lib/python2.7/site-packages/coremltools/converters/keras/_keras2_converter.py in () 66 _keras.layers.wrappers.TimeDistributed:_layers2.default_skip, 67 ---> 68 _keras.applications.mobilenet.DepthwiseConv2D:_layers2.convert_convolution, 69 70 } AttributeError: 'module' object has no attribute 'mobilenet'
I'm using Python 2.7 on macOS
As said this is a very simple Linear Regression and the module has no image input at all.
Any hint ?
Thanks, Jacopo
Updating Keras to 2.0.6 worked for me...
coremltools works when keras uses tensorflow, not theano, as its backend.
you can change keras' default backend at $HOME/.keras/keras.json, and changing to "backend": "tensorflow".