model die after run operation(prediction) - tensorflow

I trained model(cnn) for text classification.
so i want to test using keyboard input(what i typed)
like below
while True:
input=raw_input("input text ")
input = funciontForencoding(input)
print (sess.run(predict, {input_x:input}))
but logic die after show result value(prediction) one or two times
error stake is below,
model take two sentence list and return result of each sentence
please input to test : blah blah
please input to test :
[[-1.73195827 1.9319526 ]
[-1.73195827 1.9319526 ]]
please input to test :
please input to test :
Traceback (most recent call last):
File "test_eval.py", line 213, in
scores = sess.run(scores, {input_x: tdata, dropout_keep_prob:1.0})
File "/home/tech/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 340, in run
run_metadata_ptr)
File "/home/tech/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 523, in _run
processed_fetches = self._process_fetches(fetches)
File "/home/tech/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 493, in _process_fetches
% (subfetch, fetch, type(subfetch), str(e)))
TypeError: Fetch argument array([[-1.73195827, 1.9319526 ],
[-1.73195827, 1.9319526 ]], dtype=float32) of array([[-1.73195827, 1.9319526 ],
[-1.73195827, 1.9319526 ]], dtype=float32) has invalid type , must be a string or Tensor. (Can not convert a ndarray into a Tensor or Operation.)
is there any caution or instruction should i follow? like clear session after each run operation

Related

Tensorflow Object Detection API 2

I'm new to tf object detection api 2.
After training the model you can run an evaluation process to check the accuracy of the model.
But when I tried to run I got the below error. I'm using the backbone as an efficientDet.
I was able to run the evaluation for scaling resolution 512 but 640 is failing with the below error.
This is the python file I called and ended up with the below error.
enter code here /tensorflow/models/research/object_detection/model_main_tf2.py
`enter code here`enter code here`Call arguments received:
• inputs=tf.Tensor(shape=(1, 480, 640, 3), dtype=float32)
• kwargs={'training': 'False'}
exception.
INFO:tensorflow:A replica probably exhausted all examples. Skipping pending examples on other replicas.
I0719 06:49:27.115007 140042699994880 model_lib_v2.py:943] A replica probably exhausted all examples. Skipping pending e
xamples on other replicas.
Traceback (most recent call last):
File "/home/pictcompute/effient_net_ve/tensorflow/models/research/object_detection/model_main_tf2.py", line 115, in <m
odule>
tf.compat.v1.app.run()
File "/home/pictcompute/effient_net_ve/lib/python3.8/site-packages/tensorflow/python/platform/app.py", line 36, in run
_run(main=main, argv=argv, flags_parser=_parse_flags_tolerate_undef)
File "/home/pictcompute/effient_net_ve/lib/python3.8/site-packages/absl/app.py", line 312, in run
_run_main(main, args)
File "/home/pictcompute/effient_net_ve/lib/python3.8/site-packages/absl/app.py", line 258, in _run_main
sys.exit(main(argv))
File "/home/pictcompute/effient_net_ve/tensorflow/models/research/object_detection/model_main_tf2.py", line 82, in mai
n
model_lib_v2.eval_continuously(
File "/home/pictcompute/effient_net_ve/lib/python3.8/site-packages/object_detection/model_lib_v2.py", line 1159, in ev
al_continuously
eager_eval_loop(
File "/home/pictcompute/effient_net_ve/lib/python3.8/site-packages/object_detection/model_lib_v2.py", line 1009, in ea
ger_eval_loop
for evaluator in evaluators:
TypeError: 'NoneType' object is not iterable
Highly appreciate your help.
Thanks
The error occurs when you try to iterate over a None value. For example
mylist = None
for x in mylist:
print(x)
TypeError Traceback (most recent call last)
<ipython-input-2-a63d8b17c4a7> in <module>
1 mylist = None
2
----> 3 for x in mylist:
4 print(x)
TypeError: 'NoneType' object is not iterable
The error can be avoided by checking if a value is None or not before iterating over it. Thank You.

Neuraxle simple pipeline trouble (StandardScaler -> LinearSVC)

I can't realize why this neuraxle pipeline does't works.
I just want scale data and apply LinearSVC.
What I am doing wrong?
This is what I am trying to do:
import numpy as np
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
from neuraxle.hyperparams.distributions import RandInt
from neuraxle.hyperparams.space import HyperparameterSpace
from neuraxle.metaopt.auto_ml import AutoML, InMemoryHyperparamsRepository, \
ValidationSplitter
from neuraxle.metaopt.callbacks import MetricCallback, ScoringCallback
from neuraxle.pipeline import Pipeline
from neuraxle.steps.sklearn import SKLearnWrapper, RidgeModelStacking
DATA_INPUTS = np.random.randint(0, 100, (100, 3))
EXPECTED_OUTPUTS = np.random.randint(0, 3, 100)
p = Pipeline([
SKLearnWrapper(StandardScaler()),
SKLearnWrapper(LinearSVC(),
HyperparameterSpace({'C': RandInt(0, 10000)})),
])
auto_ml = AutoML(
p,
validation_splitter=ValidationSplitter(0.20),
refit_trial=True,
n_trials=10,
epochs=10,
cache_folder_when_no_handle='cache',
scoring_callback=ScoringCallback(mean_squared_error,
higher_score_is_better=False),
callbacks=[MetricCallback('mse', metric_function=mean_squared_error,
higher_score_is_better=False)],
hyperparams_repository=InMemoryHyperparamsRepository(
cache_folder='cache')
)
random_search = auto_ml.fit(DATA_INPUTS, EXPECTED_OUTPUTS)
Output:
new trial:
{
"SKLearnWrapper_LinearSVC": {
"C": 7794
}
}
trial 1/10
fitting trial 1/10 split 1/1
hyperparams: {
"SKLearnWrapper_LinearSVC__C": 7794,
"SKLearnWrapper_LinearSVC__class_weight": null,
"SKLearnWrapper_LinearSVC__dual": true,
"SKLearnWrapper_LinearSVC__fit_intercept": true,
"SKLearnWrapper_LinearSVC__intercept_scaling": 1,
"SKLearnWrapper_LinearSVC__loss": "squared_hinge",
"SKLearnWrapper_LinearSVC__max_iter": 1000,
"SKLearnWrapper_LinearSVC__multi_class": "ovr",
"SKLearnWrapper_LinearSVC__penalty": "l2",
"SKLearnWrapper_LinearSVC__random_state": null,
"SKLearnWrapper_LinearSVC__tol": 0.0001,
"SKLearnWrapper_LinearSVC__verbose": 0,
"SKLearnWrapper_StandardScaler__copy": true,
"SKLearnWrapper_StandardScaler__with_mean": true,
"SKLearnWrapper_StandardScaler__with_std": true
}
epoch 1/10
main train: 1.475
main validation: 0.9
mse train: 1.475
mse validation: 0.9
epoch 2/10
<neuraxle.metaopt.trial.Trial object at 0x7f764b20e190>
Traceback (most recent call last):
File "/home/alxkolm/projects/Neuraxle/neuraxle/metaopt/auto_ml.py", line 660, in _fit_data_container
repo_trial_split = self._execute_trial(
File "/home/alxkolm/projects/Neuraxle/neuraxle/metaopt/trial.py", line 243, in __exit__
raise exc_val
File "/home/alxkolm/projects/Neuraxle/neuraxle/metaopt/auto_ml.py", line 660, in _fit_data_container
repo_trial_split = self._execute_trial(
File "/home/alxkolm/projects/Neuraxle/neuraxle/metaopt/auto_ml.py", line 725, in _execute_trial
self.print_func('success trial {} score: {}'.format(
File "/home/alxkolm/projects/Neuraxle/neuraxle/metaopt/trial.py", line 489, in __exit__
raise exc_val
File "/home/alxkolm/projects/Neuraxle/neuraxle/metaopt/auto_ml.py", line 716, in _execute_trial
repo_trial_split = self.trainer.fit_trial_split(
File "/home/alxkolm/projects/Neuraxle/neuraxle/metaopt/auto_ml.py", line 484, in fit_trial_split
trial_split = trial_split.fit_trial_split(train_data_container, context)
File "/home/alxkolm/projects/Neuraxle/neuraxle/metaopt/trial.py", line 294, in fit_trial_split
self.pipeline = self.pipeline.handle_fit(train_data_container, context)
File "/home/alxkolm/projects/Neuraxle/neuraxle/base.py", line 983, in handle_fit
new_self = self._fit_data_container(data_container, context)
File "/home/alxkolm/projects/Neuraxle/neuraxle/pipeline.py", line 173, in _fit_data_container
step, data_container = step.handle_fit_transform(data_container, context)
File "/home/alxkolm/projects/Neuraxle/neuraxle/base.py", line 1002, in handle_fit_transform
new_self, data_container = self._fit_transform_data_container(data_container, context)
File "/home/alxkolm/projects/Neuraxle/neuraxle/base.py", line 1106, in _fit_transform_data_container
new_self, out = self.fit_transform(data_container.data_inputs, data_container.expected_outputs)
File "/home/alxkolm/projects/Neuraxle/neuraxle/steps/sklearn.py", line 60, in fit_transform
out = self.wrapped_sklearn_predictor.fit_transform(data_inputs, expected_outputs)
File "/home/alxkolm/projects/ttoy/.venv38/lib/python3.8/site-packages/sklearn/base.py", line 556, in fit_transform
return self.fit(X, y, **fit_params).transform(X)
File "/home/alxkolm/projects/ttoy/.venv38/lib/python3.8/site-packages/sklearn/preprocessing/data.py", line 639, in fit
return self.partial_fit(X, y)
File "/home/alxkolm/projects/ttoy/.venv38/lib/python3.8/site-packages/sklearn/preprocessing/data.py", line 661, in partial_fit
X = check_array(X, accept_sparse=('csr', 'csc'), copy=self.copy,
File "/home/alxkolm/projects/ttoy/.venv38/lib/python3.8/site-packages/sklearn/utils/validation.py", line 517, in check_array
raise ValueError(
ValueError: Expected 2D array, got 1D array instead:
array=[2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.
2. 2. 2. 2. 2. 2. 2. 2.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/alxkolm/projects/ttoy/trainbox/case.py", line 39, in <module>
random_search = auto_ml.fit(DATA_INPUTS, EXPECTED_OUTPUTS)
File "/home/alxkolm/projects/Neuraxle/neuraxle/base.py", line 3144, in fit
new_self = self.handle_fit(data_container, context)
File "/home/alxkolm/projects/Neuraxle/neuraxle/base.py", line 983, in handle_fit
new_self = self._fit_data_container(data_container, context)
File "/home/alxkolm/projects/Neuraxle/neuraxle/metaopt/auto_ml.py", line 674, in _fit_data_container
self._get_trial_split_description(repo_trial, repo_trial_split, validation_splits, trial_number)))
UnboundLocalError: local variable 'repo_trial_split' referenced before assignment
I have fixed your issue here : https://github.com/Neuraxio/Neuraxle/pull/333
Basically, the AutoML loop was keeping the same DataContainer object for each epochs, but your pipelinem was changing the values inside the reference for the data inputs. I have added a shallow copy before each epoch. I tested your code with a unit test, and it works just fine now.

Data type mismatch in streaming F1 score calculation in Tensorflow

I was trying to use this code as it is on Tensorflow 1.13.1. However, it throws the following error:
sherlock#mybox:~/cs273/autocat/bert$ python streaming2.py
Traceback (most recent call last):
File "streaming2.py", line 233, in <module>
tf_f1 = tf_f1_score(t, p)
File "streaming2.py", line 161, in tf_f1_score
f1s[2] = tf.reduce_sum(f1 * weights)
File "/home/sherlock/.virtualenvs/autocat/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 812, in binary_op_wrapper
return func(x, y, name=name)
File "/home/sherlock/.virtualenvs/autocat/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 1078, in _mul_dispatch
return gen_math_ops.mul(x, y, name=name)
File "/home/sherlock/.virtualenvs/autocat/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 5860, in mul
"Mul", x=x, y=y, name=name)
File "/home/sherlock/.virtualenvs/autocat/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 547, in _apply_op_helper
inferred_from[input_arg.type_attr]))
TypeError: Input 'y' of 'Mul' Op has type float64 that does not match type int64 of argument 'x'.
Tried fixing the casts for some time, but failed to find a minimal change that makes the code work. Can anyone please help me on this?
I could reproduce your error: it happens with Python 2 but not 3.
So either switch to Python 3 or change the code with tf.cast
f1 = tf.cast(f1, tf.float64)
f1s[2] = tf.reduce_sum(f1 * weights)
and maybe in other locations but that's the idea

GCloud MLEngine:Create Version failed. Bad model detected with error: Failed to load model: a bytes-like object is required, not 'str' (Error code: 0)

I am trying to create version under google cloud ml models for the successfully trained tensorflow estimator model. I believe that I am providing the correct Uri(in google storage) which includes saved_model.pb.
Framework: Tensorflow,
Framework Version: 1.13.1,
Runtime Version: 1.13,
Python: 3.5
Here is the traceback of the error:
Traceback (most recent call last):
File "/google/google-cloud-sdk/lib/googlecloudsdk/calliope/cli.py", line 985, in Execute
resources = calliope_command.Run(cli=self, args=args)
File "/google/google-cloud-sdk/lib/googlecloudsdk/calliope/backend.py", line 795, in Run
resources = command_instance.Run(args)
File "/google/google-cloud-sdk/lib/surface/ml_engine/versions/create.py", line 119, in Run
python_version=args.python_version)
File "/google/google-cloud-sdk/lib/googlecloudsdk/command_lib/ml_engine/versions_util.py", line 114, in Create
message='Creating version (this might take a few minutes)...')
File "/google/google-cloud-sdk/lib/googlecloudsdk/command_lib/ml_engine/versions_util.py", line 75, in WaitForOpMaybe
return operations_client.WaitForOperation(op, message=message).response
File "/google/google-cloud-sdk/lib/googlecloudsdk/api_lib/ml_engine/operations.py", line 114, in WaitForOperation
sleep_ms=5000)
File "/google/google-cloud-sdk/lib/googlecloudsdk/api_lib/util/waiter.py", line 264, in WaitFor
sleep_ms, _StatusUpdate)
File "/google/google-cloud-sdk/lib/googlecloudsdk/api_lib/util/waiter.py", line 326, in PollUntilDone
sleep_ms=sleep_ms)
File "/google/google-cloud-sdk/lib/googlecloudsdk/core/util/retry.py", line 229, in RetryOnResult
if not should_retry(result, state):
File "/google/google-cloud-sdk/lib/googlecloudsdk/api_lib/util/waiter.py", line 320, in _IsNotDone
return not poller.IsDone(operation)
File "/google/google-cloud-sdk/lib/googlecloudsdk/api_lib/util/waiter.py", line 122, in IsDone
raise OperationError(operation.error.message)
OperationError: Bad model detected with error: "Failed to load model: a bytes-like object is required, not 'str' (Error code: 0)"
ERROR: (gcloud.ml-engine.versions.create) Bad model detected with error: "Failed to load model: a bytes-like object is required, not 'str' (Error code: 0)"
Do you have any idea what might be the problem?
EDIT
I am using:
tf.estimator.LatestExporter('exporter', model.serving_input_fn)
as a estimator exporter.
serving_input_fn:
def serving_input_fn():
inputs = {'string1': tf.placeholder(tf.int16, [None, MAX_SEQUENCE_LENGTH]),
'string2': tf.placeholder(tf.int16, [None, MAX_SEQUENCE_LENGTH])}
return tf.estimator.export.ServingInputReceiver(inputs, inputs)
PS: my model takes two inputs and returns one binary output.

Shape must be rank 0 but is rank 1, parse_single_sequence_example

For the past few days I have been having an issue with serializing data to tfrecord format and then subsequently deserializing it using parse_single_sequence example. I am attempting to retrieve data for use with a fairly standard RNN model, however this is my first attempt at using the tfrecords format and the associated pipeline that goes with it.
Here is a toy example to reproduce the issue I am having:
import tensorflow as tf
import tempfile
from IPython import embed
sequences = [[1, 2, 3], [4, 5, 1], [1, 2]]
label_sequences = [[0, 1, 0], [1, 0, 0], [1, 1]]
def make_example(sequence, labels):
ex = tf.train.SequenceExample()
sequence_length = len(sequence)
ex.context.feature["length"].int64_list.value.append(sequence_length)
fl_tokens = ex.feature_lists.feature_list["tokens"]
fl_labels = ex.feature_lists.feature_list["labels"]
for token, label in zip(sequence, labels):
fl_tokens.feature.add().int64_list.value.append(token)
fl_labels.feature.add().int64_list.value.append(label)
return ex
writer = tf.python_io.TFRecordWriter('./test.tfrecords')
for sequence, label_sequence in zip(sequences, label_sequences):
ex = make_example(sequence, label_sequence)
writer.write(ex.SerializeToString())
writer.close()
tf.reset_default_graph()
file_name_queue = tf.train.string_input_producer(['./test.tfrecords'], num_epochs=None)
reader = tf.TFRecordReader()
context_features = {
"length": tf.FixedLenFeature([], dtype=tf.int64)
}
sequence_features = {
"tokens": tf.FixedLenSequenceFeature([], dtype=tf.int64),
"labels": tf.FixedLenSequenceFeature([], dtype=tf.int64)
}
ex = reader.read(file_name_queue)
# Parse the example (returns a dictionary of tensors)
context_parsed, sequence_parsed = tf.parse_single_sequence_example(
serialized=ex,
context_features=context_features,
sequence_features=sequence_features
)
context = tf.contrib.learn.run_n(context_parsed, n=1, feed_dict=None)
print(context[0])
sequence = tf.contrib.learn.run_n(sequence_parsed, n=1, feed_dict=None)
print(sequence[0])
The associated stack trace is:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/common_shapes.py", line 594, in call_cpp_shape_fn
status)
File "/usr/lib/python3.5/contextlib.py", line 66, in exit
next(self.gen)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/errors.py", line 463, in raise_exception_on_not_ok_status
pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.InvalidArgumentError: Shape must be rank 0 but is rank 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "my_test.py", line 51, in
sequence_features=sequence_features
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/parsing_ops.py", line 640, in parse_single_sequence_example
feature_list_dense_defaults, example_name, name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/parsing_ops.py", line 837, in _parse_single_sequence_example_raw
name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_parsing_ops.py", line 285, in _parse_single_sequence_example
name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 2382, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1783, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/common_shapes.py", line 596, in call_cpp_shape_fn
raise ValueError(err.message)
ValueError: Shape must be rank 0 but is rank 1
I posted this as a potential issue over on github though it seems I may just be using it incorrectly: Tensorflow Github Issue
So with the background information out of the way, I'm just wondering if I am in fact making an error here? Any help in the right direction would be greatly appreciated, its been a few days and my poking around hasn't panned out. Thanks all!
Got it and it was a bad assumption on my part. The tf.TFRecordReader.read(queue, name=None) returns a tuple when I assumed it would have returned just the value not (key, value) which I was directly passing into the example parser.