diff --git "a/README.md" "b/README.md" --- "a/README.md" +++ "b/README.md" @@ -11,187 +11,242 @@ metrics: - recall - f1 widget: -- text: "
I'm having a problem serving my text classification model on Tensorflow\
- \ 1.12
. I'm using tf.estimator.inputs.pandas_input_fn
to read\
- \ in my data, and tf.estimator.DNNClassifier
to train/evaluate. I'd\
- \ then like to serve my model.\n(Apologies in advance, it's tough to provide a\
- \ full working example here, but it's very much like the example TF provides at\
- \ https://www.tensorflow.org/api_docs/python/tf/estimator/DNNClassifier\
- \ )
I'm currently saving my model with ...
\n\n...\nestimator.export_savedmodel(\"./TEST_SERVING/\"\
- , self.serving_input_receiver_fn, strip_default_attrs=True)\n...\ndef serving_input_receiver_fn(self):\n\
- \ \"\"\"An input receiver that expects a serialized tf.Example.\"\"\"\n\n\
- \ # feature spec dictionary determines our input parameters for the model\n\
- \ feature_spec = {\n 'Headline': tf.VarLenFeature(dtype=tf.string),\n\
- \ 'Description': tf.VarLenFeature(dtype=tf.string)\n }\n\n \
- \ # the inputs will be initially fed as strings with data serialized by\n \
- \ # Google ProtoBuffers\n serialized_tf_example = tf.placeholder(\n \
- \ dtype=tf.string, shape=None, name='input_example_tensor')\n receiver_tensors\
- \ = {'examples': serialized_tf_example}\n\n # deserialize input\n features\
- \ = tf.parse_example(serialized_tf_example, feature_spec)\n return tf.estimator.export.ServingInputReceiver(features,\
- \ receiver_tensors)\n\n\n
\n\nThis actually fails to run with the\ - \ error:
\n\nTypeError: Failed\
- \ to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'>\
- \ to Tensor. Contents: SparseTensor(indices=Tensor(\"ParseExample/ParseExample:0\"\
- , shape=(?, 2), \ndtype=int64), values=Tensor(\"ParseExample/ParseExample:2\"\
- , shape=(?,), dtype=string), dense_shape=Tensor(\"ParseExample/ParseExample:4\"\
- , shape=(2,), dtype=int64)). Consider casting elements to a supported type.\n\n\
-
\n\nI tried to save a second way doing:
\n\ndef serving_input_receiver_fn(self):\n \"\"\"\
- Build the serving inputs.\"\"\"\n INPUT_COLUMNS = [\"Headline\",\"Description\"\
- ]\n inputs = {}\n for feat in INPUT_COLUMNS:\n inputs[feat] = tf.placeholder(shape=[None],\
- \ dtype=tf.string, name=feat)\n return tf.estimator.export.ServingInputReceiver(inputs,\
- \ inputs)\n
\n\nThis actually works, until I try testing it with\
- \ the saved_model_cli
.\nSome output for saved_model_cli show\
- \ --all --dir TEST_SERVING/1553879255/
:
MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:\n\
- \nsignature_def['predict']:\n The given SavedModel SignatureDef contains the\
- \ following input(s):\n inputs['Description'] tensor_info:\n dtype:\
- \ DT_STRING\n shape: (-1)\n name: Description:0\n inputs['Headline']\
- \ tensor_info:\n dtype: DT_STRING\n shape: (-1)\n name: Headline:0\n\
- \ The given SavedModel SignatureDef contains the following output(s):\n outputs['class_ids']\
- \ tensor_info:\n dtype: DT_INT64\n shape: (-1, 1)\n name:\
- \ dnn/head/predictions/ExpandDims:0\n outputs['classes'] tensor_info:\n \
- \ dtype: DT_STRING\n shape: (-1, 1)\n name: dnn/head/predictions/str_classes:0\n\
- \ outputs['logits'] tensor_info:\n dtype: DT_FLOAT\n shape: (-1,\
- \ 3)\n name: dnn/logits/BiasAdd:0\n outputs['probabilities'] tensor_info:\n\
- \ dtype: DT_FLOAT\n shape: (-1, 3)\n name: dnn/head/predictions/probabilities:0\n\
- \ Method name is: tensorflow/serving/predict\n\n
\n\nBut now I\ - \ can't seem to test it.
\n\n>>>\
- \ saved_model_cli run --dir TEST_SERVING/1553879255/ --tag_set serve --signature_def\
- \ predict --input_examples 'inputs=[{\"Description\":[\"What is going on\"],\"\
- Headline\":[\"Help me\"]}]'\nTraceback (most recent call last):\n ...\n File\
- \ \"/Users/Josh/miniconda3/envs/python36/lib/python3.6/site-packages/tensorflow/python/tools/saved_model_cli.py\"\
- , line 489, in _create_example_string\n feature_list)\nTypeError: 'What is\
- \ going on' has type str, but expected one of: bytes\n\n
\n\nOk,\
- \ lets turn it into a bytes object by changing to b[\"What is going on\"\
- ]
and b[\"Help me\"]
...
ValueError: Type <class 'bytes'> for value b'What is going on' is\
- \ not supported for tf.train.Feature.\n
\n\nAny ideas/thoughts??\n\ - Thanks!
\n" -- text: 'In tensorflow tf.keras.Model.compile
, you can pass a lambda
- y_true, y_pred: val
function as a metric (though, it seems not documented),
- but I asked my self : "How does it aggregate it over the batches" ?
I searched the documentation, but I''ve found nowhere how it is done ?
- - -By the way, I don''t even know if it is an undefined behavior to do so and - one should instead subclass the Metric class ? ( or at least provide the required - methods).
- - -Also, is it pertinent to pass a loss as a metric (and in this case, same question - : how is it aggregated over the batches ? )
- - ' -- text: "I'm working on a project where I have trained a series of binary classifiers\ - \ with Keras, with Tensorflow as the backend\ - \ engine. The input data I have is a series of images, where each binary classifier\ - \ must make the prediction on the images, later I save the predictions on a CSV\ - \ file.
\nThe problem I have is when I get the predictions from the first\ - \ series of binary classifiers there isn't any warning, but when the 5th or 6th\ - \ binary classifier calls the method predict on the input data\ - \ I get the following warning:
\n\n\nWARNING:tensorflow:5 out\ - \ of the last 5 calls to <function\nModel.make_predict_function..predict_function\ - \ at\n0x2b280ff5c158> triggered tf.function retracing. Tracing is expensive\n\ - and the excessive number of tracings could be due to (1) creating\n@tf.function\ - \ repeatedly in a loop, (2) passing tensors with different\nshapes, (3) passing\ - \ Python objects instead of tensors. For (1), please\ndefine your @tf.function\ - \ outside of the loop. For (2), @tf.function\nhas experimental_relax_shapes=True\ - \ option that relaxes argument shapes\nthat can avoid unnecessary retracing. For\ - \ (3), please refer to\nhttps://www.tensorflow.org/tutorials/customization/performance#python_or_tensor_args\n\ - and https://www.tensorflow.org/api_docs/python/tf/function for more\n\ - details.
\n
To answer each point in the parenthesis, here\ - \ are my answers:
\nI have debugged my program and found\ - \ that this warning always happens when the method predict is called. To summarize\ - \ the code I have written is the following:
\nimport cv2 as cv\n\
- import tensorflow as tf\nfrom tensorflow.keras.models import load_model\n# Load\
- \ the models\nbinary_classifiers = [load_model(path) for path in path2models]\n\
- # Get the images\nimages = [#Load the images with OpenCV]\n# Apply the resizing\
- \ and reshapes on the images.\nmy_list = list()\nfor image in images:\n image_reworked\
- \ = # Apply the resizing and reshaping on images\n my_list.append(image_reworked)\n\
- \n# Get the prediction from each model\n# This is where I get the warning\npredictions\
- \ = [model.predict(x=my_list,verbose=0) for model in binary_classifiers]\n
\n\
- I have defined a function as tf.function and putted\ - \ the code of the predictions inside the tf.function like this
\n@tf.function\n\
- def testing(models, faces):\n return [model.predict(x=faces,verbose=0) for\
- \ model in models]\n
\nBut I ended up getting the following error:
\n\ -\n\nRuntimeError: Detected a call to
\nModel.predict
inside\ - \ a\ntf.function
. Model.predict is a high-level endpoint that manages\n\ - its owntf.function
. Please move the call toModel.predict
\ - \ outside\nof all enclosingtf.function
s. Note that you can call\ - \ aModel
\ndirectly on Tensors inside atf.function
\ - \ like:model(x)
.
So calling the method predict
\
- \ is basically already a tf.function. So it's useless to define a tf.function\
- \ when the warning I get it's from that method.
I have also checked those\ - \ other two questions:
\nBut neither of the two questions answers my question about how to avoid this\ - \ warning. Plus I have also checked the links in the warning message but I couldn't\ - \ solve my problem.
\nI simply want to avoid this\ - \ warning. While I'm still getting the predictions from the models I noticed that\ - \ the python program takes way too much time on doing predictions for a list of\ - \ images.
\nAfter some tries to suppress the warning\
- \ from the predict
method, I have checked the documentation of Tensorflow\
- \ and in one of the first tutorials on how to use Tensorflow it is explained that,\
- \ by default, Tensorflow is executed in eager mode, which is useful for testing\
- \ and debugging the network models. Since I have already tested my models many\
- \ times, it was only required to disable the eager mode by writing this single\
- \ python line of code:
tf.compat.v1.disable_eager_execution()
Now the warning doesn't show up anymore.
\n" -- text: 'Where one can find the github source code for tf.quantization.fake_quant_with_min_max_args
.
- Checking the TF API documentation, there is no link to the github
- source file, and I could not find one on github.
I'm trying to use tf.Dataset
for a 3D image CNN where the\
- \ shape of the 3D image fed into it from the training set and the validation set\
- \ are different (training: (64, 64, 64), validation: (176, 176, 160)). I didn't\
- \ even know this was possible, but I'm recreating this network based on a paper,\
- \ and using the classic feed_dict
method the network indeed works.\
- \ For performance reasons (and just to learn) I'm trying to switch the network\
- \ to use tf.Dataset
instead.
I have two datasets and iterators\ - \ built like the following:
\n\ndef _data_parser(dataset, shape):\n features = {\"input\": tf.FixedLenFeature((),\
- \ tf.string),\n \"label\": tf.FixedLenFeature((), tf.string)}\n\
- \ parsed_features = tf.parse_single_example(dataset, features)\n\n \
- \ image = tf.decode_raw(parsed_features[\"input\"], tf.float32)\n image\
- \ = tf.reshape(image, shape + (1,))\n\n label = tf.decode_raw(parsed_features[\"\
- label\"], tf.float32)\n label = tf.reshape(label, shape + (1,))\n \
- \ return image, label\n\ntrain_datasets = [\"train.tfrecord\"]\ntrain_dataset\
- \ = tf.data.TFRecordDataset(train_datasets)\ntrain_dataset = train_dataset.map(lambda\
- \ x: _data_parser(x, (64, 64, 64)))\ntrain_dataset = train_dataset.batch(batch_size)\
- \ # batch_size = 16\ntrain_iterator = train_dataset.make_initializable_iterator()\n\
- \nval_datasets = [\"validation.tfrecord\"]\nval_dataset = tf.data.TFRecordDataset(val_datasets)\n\
- val_dataset = val_dataset.map(lambda x: _data_parser(x, (176, 176, 160)))\nval_dataset\
- \ = val_dataset.batch(1)\nval_iterator = val_dataset.make_initializable_iterator()\n\
-
\n\nTensorFlow documentation has examples regarding\
- \ switching between datasets using reinitializable_iterator
or feedable_iterator
,\
- \ but they all switch between iterators of same output shape,\
- \ which is not the case here.
How should I switch between training set\
- \ and validation set using tf.Dataset
and tf.data.Iterator
\
- \ in my case then?
I am new to tensorflow
, trying to build a Siamese CNN similar\
+ \ to what's done in this guide.
\nMy model is built using a\
+ \ base model, which is then fed twice with two different pictures that go through\
+ \ the same network.
\nThis is the code for building the network:
class BaseModel(Model):\n\n def\
+ \ __init__(self, base_network):\n super(BaseModel, self).__init__()\n self.network\
+ \ = base_network\n \n def call(self, inputs):\n print(inputs)\n return\
+ \ self.network(inputs)\n\ndef get_base_model():\n inputs = tf.keras.Input(shape=INPUT)\n\
+ \n conv2d_1 = layers.Conv2D(name='seq_1', filters=64, \n kernel_size=20,\
+ \ \n activation='relu')(inputs)\n maxpool_1 = layers.MaxPooling2D(pool_size=(2,\
+ \ 2))(conv2d_1)\n\n conv2d_2 = layers.Conv2D(filters=128, \n kernel_size=20,\
+ \ \n activation='relu')(maxpool_1)\n maxpool_2 = layers.MaxPooling2D(pool_size=(2,\
+ \ 2))(conv2d_2)\n\n conv2d_3 = layers.Conv2D(filters=128, \n kernel_size=20,\
+ \ \n activation='relu')(maxpool_2)\n maxpool_3 = layers.MaxPooling2D(pool_size=(2,\
+ \ 2))(conv2d_3)\n\n conv2d_4 = layers.Conv2D(filters=256, \n kernel_size=10,\
+ \ \n activation='relu')(maxpool_3)\n\n flatten_1 = layers.Flatten()(conv2d_4)\n\
+ \ outputs = layers.Dense(units=4096,\n activation='sigmoid')(flatten_1)\n\
+ \ \n model = Model(inputs=inputs, outputs=outputs)\n\n return model\n
\n\
+ Then, I'm building the Siamese network using the previous method like that:
\n\ +INPUT = (250, 250, 3)\n\ndef\
+ \ get_siamese_model():\n left_input = layers.Input(name='img1', shape=INPUT)\n\
+ \ right_input = layers.Input(name='img2', shape=INPUT)\n \n base_model = get_base_model()\n\
+ \ base_model = BaseModel(base_model)\n\n # bind the two input layers to the\
+ \ base network\n left = base_model(left_input)\n right = base_model(right_input)\n\
+ \n # build distance measuring layer\n l1_lambda = layers.Lambda(lambda tensors:abs(tensors[0]\
+ \ - tensors[1]))\n l1_dist = l1_lambda([left, right])\n\n pred = layers.Dense(1,activation='sigmoid')(l1_dist)\n\
+ \n return Model(inputs=[left_input, right_input], outputs=pred)\n\nclass SiameseNetwork(Model):\n\
+ \n def __init__(self, siamese_network):\n super(SiameseNetwork, self).__init__()\n\
+ \ self.siamese_network = siamese_network\n \n def call(self, inputs):\n \
+ \ print(inputs)\n return self.siamese_network(inputs)\n
\nI'm\
+ \ then training the network by passing a tf.data.Dataset
to it:
net.fit(x=train_dataset, epochs=10\
+ \ ,verbose=True)\n
\ntrain_dataset
is of type:
\n\n<PrefetchDataset shapes: ((None, 250, 250, 3), (None, 250,\ + \ 250, 3)), types: (tf.float32, tf.float32)>
\n
It seems\ + \ like the shape of the input is defined well, but I'm still encountering an error:
\n\ +ValueError \
+ \ Traceback (most recent call last)\n<ipython-input-144-6c5586e1e205>\
+ \ in <module>()\n----> 1 net.fit(x=train_dataset, epochs=10 ,verbose=True)\n\
+ \n9 frames\n/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py\
+ \ in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split,\
+ \ validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch,\
+ \ validation_steps, validation_batch_size, validation_freq, max_queue_size, workers,\
+ \ use_multiprocessing)\n 1098 _r=1):\n 1099 \
+ \ callbacks.on_train_batch_begin(step)\n-> 1100 tmp_logs = self.train_function(iterator)\n\
+ \ 1101 if data_handler.should_sync:\n 1102 context.async_wait()\n\
+ \n/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py\
+ \ in __call__(self, *args, **kwds)\n 826 tracing_count = self.experimental_get_tracing_count()\n\
+ \ 827 with trace.Trace(self._name) as tm:\n--> 828 result = self._call(*args,\
+ \ **kwds)\n 829 compiler = "xla" if self._experimental_compile\
+ \ else "nonXla"\n 830 new_tracing_count = self.experimental_get_tracing_count()\n\
+ \n/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py\
+ \ in _call(self, *args, **kwds)\n 869 # This is the first call of __call__,\
+ \ so we have to initialize.\n 870 initializers = []\n--> 871 \
+ \ self._initialize(args, kwds, add_initializers_to=initializers)\n 872 \
+ \ finally:\n 873 # At this point we know that the initialization is\
+ \ complete (or less\n\n/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py\
+ \ in _initialize(self, args, kwds, add_initializers_to)\n 724 self._concrete_stateful_fn\
+ \ = (\n 725 self._stateful_fn._get_concrete_function_internal_garbage_collected(\
+ \ # pylint: disable=protected-access\n--> 726 *args, **kwds))\n\
+ \ 727 \n 728 def invalid_creator_scope(*unused_args, **unused_kwds):\n\
+ \n/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py in\
+ \ _get_concrete_function_internal_garbage_collected(self, *args, **kwargs)\n \
+ \ 2967 args, kwargs = None, None\n 2968 with self._lock:\n-> 2969\
+ \ graph_function, _ = self._maybe_define_function(args, kwargs)\n 2970\
+ \ return graph_function\n 2971 \n\n/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py\
+ \ in _maybe_define_function(self, args, kwargs)\n 3359 \n 3360 self._function_cache.missed.add(call_context_key)\n\
+ -> 3361 graph_function = self._create_graph_function(args, kwargs)\n\
+ \ 3362 self._function_cache.primary[cache_key] = graph_function\n\
+ \ 3363 \n\n/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/function.py\
+ \ in _create_graph_function(self, args, kwargs, override_flat_arg_shapes)\n \
+ \ 3204 arg_names=arg_names,\n 3205 override_flat_arg_shapes=override_flat_arg_shapes,\n\
+ -> 3206 capture_by_value=self._capture_by_value),\n 3207 \
+ \ self._function_attributes,\n 3208 function_spec=self.function_spec,\n\
+ \n/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py\
+ \ in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph,\
+ \ autograph, autograph_options, add_control_dependencies, arg_names, op_return_value,\
+ \ collections, capture_by_value, override_flat_arg_shapes)\n 988 _,\
+ \ original_func = tf_decorator.unwrap(python_func)\n 989 \n--> 990 \
+ \ func_outputs = python_func(*func_args, **func_kwargs)\n 991 \n 992 \
+ \ # invariant: `func_outputs` contains only Tensors, CompositeTensors,\n\n\
+ /usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/def_function.py\
+ \ in wrapped_fn(*args, **kwds)\n 632 xla_context.Exit()\n 633\
+ \ else:\n--> 634 out = weak_wrapped_fn().__wrapped__(*args,\
+ \ **kwds)\n 635 return out\n 636 \n\n/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py\
+ \ in wrapper(*args, **kwargs)\n 975 except Exception as e: # pylint:disable=broad-except\n\
+ \ 976 if hasattr(e, "ag_error_metadata"):\n--> 977\
+ \ raise e.ag_error_metadata.to_exception(e)\n 978 \
+ \ else:\n 979 raise\n\nValueError: in user code:\n\n /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805\
+ \ train_function *\n return step_function(self, iterator)\n <ipython-input-125-de3a74f810c3>:9\
+ \ call *\n return self.siamese_network(inputs)\n /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:998\
+ \ __call__ **\n input_spec.assert_input_compatibility(self.input_spec,\
+ \ inputs, self.name)\n /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/input_spec.py:207\
+ \ assert_input_compatibility\n ' input tensors. Inputs received: ' + str(inputs))\n\
+ \n ValueError: Layer model_16 expects 2 input(s), but it received 1 input tensors.\
+ \ Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 250, 250, 3)\
+ \ dtype=float32>]\n
\nI do undertand that model_16
\
+ \ is the BaseModel, however I can't figure out what am I doing wrong here.
New to TensorFlow, so apologies for newbie question.
\n\nFollowing\ + \ this tutorial but instead of using image data I am\ + \ using numerical data.
\n\nLoad the dataset:
\n\ntrain_dataset_url\
+ \ = \"xxx.csv\"\ntrain_dataset_fp = tf.keras.utils.get_file(\n fname=os.path.basename(train_dataset_url),\n\
+ \ origin=train_dataset_url)\n
\n\nMake training dataset:
\n\ + \nbatch_size = 32\n\ntrain_dataset = tf.contrib.data.make_csv_dataset(\n\
+ \ train_dataset_fp,\n batch_size, \n column_names=column_names,\n \
+ \ label_name=label_name,\n num_epochs=1)\n
\n\nTrain\ + \ classified model using:
\n\nmodel = tf.keras.Sequential([\n\
+ \ tf.keras.layers.Dense(10, activation=tf.nn.relu, input_shape=(1,)),\n tf.keras.layers.Dense(10,\
+ \ activation=tf.nn.relu),\n tf.keras.layers.Dense(4)\n])
But\ + \ when I \"test\" the model with the same inputs:
\n\npredictions\
+ \ = model(features)
I receive the error:
\n\nInvalidArgumentError:\
+ \ cannot compute MatMul as input #0(zero-based) was expected to be a float tensor\
+ \ but is a int32 tensor [Op:MatMul]
It's possible I have missed\ + \ something fundamental. I feel like I need to specify a type somewhere.
\n" +- text: "I have created a neural style transfer with Eager Execution, but it does\ + \ not work when I try to turn it into a tf.function.\nThe error message says:
\n\ +ValueError: tf.function only supports singleton tf.Variables created\
+ \ on the first call. Make sure the tf.Variable is only created once or created\
+ \ outside tf.function. See https://www.tensorflow.org/guide/function#creating_tfvariables\
+ \ for more information.\n
\nHowever, no variable is being created\ + \ inside the function. Here is a simplified version of the code, which is just\ + \ a neural style transfer with one image (the goal is to make the generated image\ + \ look exactly like the content image):
\nimport tensorflow as tf\n\
+ import numpy as np\nfrom PIL import Image\n\n#Get and process the images\nimage\
+ \ = np.array(Image.open("frame7766.jpg")).reshape(1, 720, 1280, 3)/255\n\
+ content_image = tf.convert_to_tensor(image, dtype = tf.float32)\n# variable is\
+ \ defined outside of tf.function\ngenerated_image = tf.Variable(np.random.rand(1,\
+ \ 720, 1280, 3)/2 + content_image/2, dtype = tf.float32)\n\ndef clip_0_1(image):\
+ \ # keeps image values between 0 and 1\n return tf.clip_by_value(image, clip_value_min=0,\
+ \ clip_value_max=1)\n\n@ tf.function\ndef train_step(generated_image, content_image):\
+ \ #turn generated image into tf variable\n optimizer = tf.keras.optimizers.Adam(learning_rate\
+ \ = 0.01)\n with tf.GradientTape() as tape:\n cost = tf.reduce_sum(tf.square(generated_image\
+ \ - content_image))\n grad = tape.gradient(cost, generated_image) \n optimizer.apply_gradients([(grad,\
+ \ generated_image)]) # More information below\n generated_image.assign(clip_0_1(generated_image))\n\
+ \ return generated_image\n\ngenerated_image = train_step(generated_image, content_image)\n\
+
\nThe error message points to the line
\noptimizer.apply_gradients([(grad,\
+ \ generated_image)]) \n
\nI have tried to change the input of \
+ \ optimizer.apply_gradients
to zip([grad], [generated_image])
,\
+ \ and every combination of lists and tuples I can think of, but the error still\
+ \ remains. I have also looked through https://www.tensorflow.org/guide/function#creating_tfvariables\
+ \ and https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/Optimizer,\
+ \ but neither of them shows examples where the variable is not explicitly defined.\n\
+ The only conclusion that I can come to is that one of my commands (most likely\
+ \ optimizer.apply_gradients
) creates a variable because of an issue\
+ \ in my earlier code. Is that correct?
I am going through TensorFlow Eager Execution from here and find it difficult to understand the\ + \ customizing gradients part.
\n\n@tfe.custom_gradient\ndef logexp(x):\n\
+ \ e = tf.exp(x)\n def grad(dy):\n return dy * (1 - 1/(1 + e))\n \
+ \ return tf.log(1 + e), grad\n
\n\nFirst, it is difficult to\ + \ make sense what does dy do in the gradient function.
\n\nWhen I read the\ + \ implementation of tf.contrib.eager.custom_gradient.\nI can't really make sense\ + \ the working mechanism behind tape. Following is the code I borrow from the implementation\ + \ of tf.contrib.eager.custom_gradient. Can anybody explain what does tape do here?
\n\ + \nfrom tensorflow.python.eager import tape\nfrom tensorflow.python.ops\
+ \ import array_ops\nfrom tensorflow.python.ops import gen_array_ops\nfrom tensorflow.python.util\
+ \ import nest\nfrom tensorflow.python.framework import ops as tf_ops\n\ndef my_custom_gradient(f):\n\
+ \ def decorated(*args, **kwargs):\n for x in args:\n print('args\
+ \ {0}'.format(x))\n input_tensors = [tf_ops.convert_to_tensor(x) for x\
+ \ in args]\n\n with tape.stop_recording():\n result, grad_fn\
+ \ = f(*args, **kwargs)\n flat_result = nest.flatten(result)\n \
+ \ flat_result = [gen_array_ops.identity(x) for x in flat_result]\n\n \
+ \ def actual_grad_fn(*outputs):\n print(*outputs)\n \
+ \ return nest.flatten(grad_fn(*outputs))\n\n tape.record_operation(\n \
+ \ f.__name__, # the name of f, in this case logexp\n flat_result,\n\
+ \ input_tensors,\n actual_grad_fn) # backward_function\n\
+ \ flat_result = list(flat_result)\n return nest.pack_sequence_as(result,\
+ \ flat_result)\nreturn decorated \n
\n\nEven though I found the\ + \ implementation of tape from here. But I can't really get much out of it\ + \ due the poor documentation.
\n" +- text: "I want to extract signals from time series data for machine learning using\ + \ tensorflow. I got this error when I try to run the program.
\n\n\ +\n/Users/renzha/Library/Application Support/JetBrains/PyCharmCE2021.1/scratches/pywavelet.py:38:\ + \ DeprecationWarning:
\nnp.float
is a deprecated alias for the builtin\ + \float
. To silence this warning, usefloat
by itself.\ + \ Doing this will not modify any behavior and is safe. If you specifically wanted\ + \ the numpy scalar type, usenp.float64
here.\nDeprecated in NumPy\ + \ 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations\n\ + X = np.asarray(X).astype(np.float)\nTraceback (most recent call last):\nFile "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/core/series.py",\ + \ line 129, in wrapper\nraise TypeError(f"cannot convert the series to {converter}")\n\ + TypeError: cannot convert the series to <class 'float'>\nThe above exception\ + \ was the direct cause of the following exception:\nTraceback (most recent call\ + \ last):\nFile "/Users/renzha/Library/Application Support/JetBrains/PyCharmCE2021.1/scratches/pywavelet.py",\ + \ line 38, in \nX = np.asarray(X).astype(np.float)\nValueError: setting an array\ + \ element with a sequence.
However, if I use X =\
+ \ np.array(X)
, then the error will be
\n\nValueError:\ + \ Failed to convert a NumPy array to a Tensor (Unsupported object type Series).
\n\ +
I have tried tf.convert_to_tensor(X)
, but it will\
+ \ return the same error.
The code is here:
\nHBT_data =\
+ \ []\nfor i in range (0,10):\n files = glob.glob('/Users/renzha/Documents/work/preparing\
+ \ papers/non-contact HCI/s letter' + str(i) +'/*')\n\n for file in files:\n\
+ \ names = ['time', 'signal']\n data = pd.read_csv (file, names=names)\n\
+ \ data = data.drop (data.index [0])\n data = data.dropna (axis=0,\
+ \ how='any')\n x = data.iloc[:,1]\n filename = os.path.basename\
+ \ (file)\n label = os.path.splitext(filename)[0]\n labeledArray\
+ \ = [label, x]\n HBT_data.append(labeledArray)\n\nHBT_data = pd.DataFrame(HBT_data,\
+ \ dtype=object)\ny = HBT_data.iloc[:, 0]\ny = np.asarray(y)\nX = HBT_data.iloc[:,\
+ \ 1:]\nX = np.asarray(X).astype(np.float) # tried but did not work\nX = tf.convert_to_tensor\
+ \ (X) # tried but did not work\n\nX_train, X_validation, Y_train, Y_validation\
+ \ = train_test_split(X, y, test_size=0.2, random_state=8)\n\nX_train = X_train.reshape((X_train.shape[0],\
+ \ X_train.shape[1], 1))\nX_test = X_validation.reshape((X_validation.shape[0],\
+ \ X_validation.shape[1], 1))\nnum_classes = len(np.unique(Y_train))\n\nidx = np.random.permutation(len(X_train))\n\
+ x_train = X_train[idx]\ny_train = X_train[idx]\n\nY_train[Y_train == -1] = 0\n\
+ Y_validation[Y_validation == -1] = 0\n\n\ndef make_model(input_shape):\n input_layer\
+ \ = keras.layers.Input(input_shape)\n\n conv1 = keras.layers.Conv1D(filters=64,\
+ \ kernel_size=3, padding="same")(input_layer)\n conv1 = keras.layers.BatchNormalization()(conv1)\n\
+ \ conv1 = keras.layers.ReLU()(conv1)\n\n conv2 = keras.layers.Conv1D(filters=64,\
+ \ kernel_size=3, padding="same")(conv1)\n conv2 = keras.layers.BatchNormalization()(conv2)\n\
+ \ conv2 = keras.layers.ReLU()(conv2)\n\n conv3 = keras.layers.Conv1D(filters=64,\
+ \ kernel_size=3, padding="same")(conv2)\n conv3 = keras.layers.BatchNormalization()(conv3)\n\
+ \ conv3 = keras.layers.ReLU()(conv3)\n\n gap = keras.layers.GlobalAveragePooling1D()(conv3)\n\
+ \n output_layer = keras.layers.Dense(num_classes, activation="softmax")(gap)\n\
+ \n return keras.models.Model(inputs=input_layer, outputs=output_layer)\n\n\n\
+ model = make_model(input_shape=X_train.shape[1:])\nkeras.utils.plot_model(model,\
+ \ show_shapes=True)\n\nepochs = 500\nbatch_size = 32\n\ncallbacks = [\n keras.callbacks.ModelCheckpoint(\n\
+ \ "best_model.h5", save_best_only=True, monitor="val_loss"\n\
+ \ ),\n keras.callbacks.ReduceLROnPlateau(\n monitor="val_loss",\
+ \ factor=0.5, patience=20, min_lr=0.0001\n ),\n keras.callbacks.EarlyStopping(monitor="val_loss",\
+ \ patience=50, verbose=1),\n]\nmodel.compile(\n optimizer="adam",\n\
+ \ loss="sparse_categorical_crossentropy",\n metrics=["sparse_categorical_accuracy"],\n\
+ )\nhistory = model.fit(\n X_train,\n Y_train,\n batch_size=batch_size,\n\
+ \ epochs=epochs,\n callbacks=callbacks,\n validation_split=0.2,\n \
+ \ verbose=1,\n)\n\nmodel = keras.models.load_model("best_model.h5")\n\
+ \ntest_loss, test_acc = model.evaluate(X_validation, Y_validation)\n\nprint("Test\
+ \ accuracy", test_acc)\nprint("Test loss", test_loss)```\n\n
\n"
pipeline_tag: text-classification
inference: true
-base_model: sentence-transformers/all-MiniLM-L6-v2
+base_model: sentence-transformers/all-mpnet-base-v2
model-index:
-- name: SetFit with sentence-transformers/all-MiniLM-L6-v2
+- name: SetFit with sentence-transformers/all-mpnet-base-v2
results:
- task:
type: text-classification
@@ -202,22 +257,22 @@ model-index:
split: test
metrics:
- type: accuracy
- value: 0.71
+ value: 0.6966292134831461
name: Accuracy
- type: precision
- value: 0.7100840336134453
+ value: 0.7056921772312911
name: Precision
- type: recall
- value: 0.71
+ value: 0.6966292134831461
name: Recall
- type: f1
- value: 0.70997099709971
+ value: 0.6938491833661531
name: F1
---
-# SetFit with sentence-transformers/all-MiniLM-L6-v2
+# SetFit with sentence-transformers/all-mpnet-base-v2
-This is a [SetFit](https://github.com/huggingface/setfit) model that can be used for Text Classification. This SetFit model uses [sentence-transformers/all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) as the Sentence Transformer embedding model. A [SetFitHead](huggingface.co/docs/setfit/reference/main#setfit.SetFitHead) instance is used for classification.
+This is a [SetFit](https://github.com/huggingface/setfit) model that can be used for Text Classification. This SetFit model uses [sentence-transformers/all-mpnet-base-v2](https://huggingface.co/sentence-transformers/all-mpnet-base-v2) as the Sentence Transformer embedding model. A [SetFitHead](huggingface.co/docs/setfit/reference/main#setfit.SetFitHead) instance is used for classification.
The model has been trained using an efficient few-shot learning technique that involves:
@@ -228,9 +283,9 @@ The model has been trained using an efficient few-shot learning technique that i
### Model Description
- **Model Type:** SetFit
-- **Sentence Transformer body:** [sentence-transformers/all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)
+- **Sentence Transformer body:** [sentence-transformers/all-mpnet-base-v2](https://huggingface.co/sentence-transformers/all-mpnet-base-v2)
- **Classification head:** a [SetFitHead](huggingface.co/docs/setfit/reference/main#setfit.SetFitHead) instance
-- **Maximum Sequence Length:** 256 tokens
+- **Maximum Sequence Length:** 384 tokens
- **Number of Classes:** 2 classes
@@ -243,17 +298,17 @@ The model has been trained using an efficient few-shot learning technique that i
- **Blogpost:** [SetFit: Efficient Few-Shot Learning Without Prompts](https://huggingface.co/blog/setfit)
### Model Labels
-| Label | Examples |
-|:------|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
-| 0 | The Problem
\n\nI am converting my Tensorflow 1.14 estimator to TensorFlow 2.1. My current workflow involves training my tensorflow model on gcloud\'s ai-platform (training on gcloud) and using their model service to deploy my model for online predictions (model service).
\n\nThe issue when upgrading to TensorFlow 2 is that they have done away with placeholders, which is affecting my serving_input_fn
and how I export my estimator model. With tensorflow 2, if I export a model without the use of placeholders, my model\'s "predict" SignatureDef
only has a single "examples" tensor whereas previously it had many inputs named appropriately through my serving_input_fn
.
The previous set up for my estimator was as follows:
\n\ndef serving_input_fn():\n\n inputs = {\n \'feature1\': tf.compat.v1.placeholder(shape=None, dtype=tf.string),\n \'feature2\': tf.compat.v1.placeholder(shape=None, dtype=tf.string),\n \'feature3\': tf.compat.v1.placeholder(shape=None, dtype=tf.string),\n ...\n }\n\n return tf.estimator.export.ServingInputReceiver(features=split_features, receiver_tensors=inputs)\n\nexporter = tf.estimator.LatestExporter(\'exporter\', serving_input_fn)\n\neval_spec = tf.estimator.EvalSpec(\n input_fn=lambda: input_eval_fn(args.test_dir),\n exporters=[exporter],\n start_delay_secs=10,\n throttle_secs=0)\n\n...\n\ntf.estimator.train_and_evaluate(estimator, train_spec, eval_spec)\n
\n\nAnd this has worked fine in the past, it has allowed me to have a multi-input "predict" SignatureDef where I can send a json of the inputs to ai-platforms model service and get predictions back. But since I am trying to not rely on the tf.compat.v1
library, I want to avoid using placeholders.
What I\'ve tried
\n\nFollowing the documentation linked here I\'ve replaced my serving_input_fn with the tf.estimator.export.build_parsing_serving_input_receiver_fn
method:
feature_columns = ... # list of feature columns \nserving_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(\n tf.feature_column.make_parse_example_spec(feature_columns))\n
\n\nHowever, this gives me the following "predict" SignatureDef:
\n\nsignature_def[\'predict\']:\n The given SavedModel SignatureDef contains the following input(s):\n inputs[\'examples\'] tensor_info:\n dtype: DT_STRING\n shape: (-1)\n name: input_example_tensor:0\n
\n\nwhereas before my "predict" SignatureDef was as follows:
\n\nsignature_def[\'predict\']:\n The given SavedModel SignatureDef contains the following input(s):\n inputs[\'feature1\'] tensor_info:\n dtype: DT_STRING\n shape: unknown_rank\n name: Placeholder:0\n inputs[\'feature2\'] tensor_info:\n dtype: DT_STRING\n shape: unknown_rank\n name: Placeholder_1:0\n inputs[\'feature3\'] tensor_info:\n dtype: DT_STRING\n shape: unknown_rank\n name: Placeholder_2:0\n
\n\nI\'ve also tried using the tf.estimator.export.build_raw_serving_input_receiver_fn
, but my understanding is that this method requires actual Tensors in order to be used instead of a feature spec. Unless I use placeholders, I don\'t really understand where to grab these serving Tensors from.
So my main questions are:
\n\nThanks!
\n'For the computation of Intersection over Union (IoU) I want to find coordinates of minimum and maximum values (the border pixels) in a segmentation image image_pred
that is represented by a float32 3D tensor. In particular, I aim at finding top left and bottom right corner coordinates of objects in an image. The image is entirely comprised of black pixels (value 0.0) except where the object is located, I have color pixels (0.0 < values < 1.0). Here\'s an example for such a bounding box (in my case, the object is the traffic sign and the environment is blacked out):
My approach so far is to tf.boolean_mask
for setting every pixel to False except for the color pixels:
zeros = tf.zeros_like(image_pred)\nmask = tf.greater(image_pred, zeros)\nboolean_mask_pred = tf.boolean_mask(image_pred, mask)\n
\n\nand then use tf.where
to find the coordinates of the masked image. To determine the horizontal and vertical coordinate values of the top left and bottom right corners of the rectangle, I thought about using tf.recude_max
and tf.reduce_min
, but since these do not return a single value if I provide an axis
, I am unsure if this is the correct function to use. According to the docs, if I do not specify axis
, the function will reduce all dimensions which is not what I want either. Which is the correct function to do this? The IoU in the end is a single 1D float value.
coordinates_pred = tf.where(boolean_mask_pred)\nx21 = tf.reduce_min(coordinates_pred, axis=1)\ny21 = tf.reduce_min(coordinates_pred, axis=0)\nx22 = tf.reduce_max(coordinates_pred, axis=1)\ny22 = tf.reduce_max(coordinates_pred, axis=0)\n
\n'Computing mean, total, etc. of each feature in a dataset seems quite trivial in Pandas
and Numpy
, but I couldn\'t find any similarly easy functions/operations for tf.data.Dataset
. Actually I found tf.data.Dataset.reduce
which allows me to compute running sum
, but it\'s not that easy for other operation (min
, max
, std
, etc.)\n
\n
So, my question is, is there a simple way to compute statistics for tf.data.Dataset
? Moreover, is there a way to standardize/normalize (an entire, i.e. not in batch) tf.data.Dataset
, especially if not using tf.data.Dataset.reduce
?
TensorFlow documentation have the following example that can illustrate how to create a batch generator to feed a training set in batches to a model when the training set is too large to fit in memory:
\nfrom skimage.io import imread\nfrom skimage.transform import resize\nimport tensorflow as tf\nimport numpy as np\nimport math\n\n# Here, `x_set` is list of path to the images\n# and `y_set` are the associated classes.\n\nclass CIFAR10Sequence(tf.keras.utils.Sequence):\n\n def __init__(self, x_set, y_set, batch_size):\n self.x, self.y = x_set, y_set\n self.batch_size = batch_size\n\n def __len__(self):\n return math.ceil(len(self.x) / self.batch_size)\n\n def __getitem__(self, idx):\n batch_x = self.x[idx * self.batch_size:(idx + 1) *\n self.batch_size]\n batch_y = self.y[idx * self.batch_size:(idx + 1) *\n self.batch_size]\n\n return np.array([\n resize(imread(file_name), (200, 200))\n for file_name in batch_x]), np.array(batch_y)\n
\nMy intention is to further increase the diversity of the training set by rotating each image 3x by 90º. In each Epoch of the training process, the model would first be fed with the "0º training set" and next with the 90º, 180º and 270º rotating sets, respectively.
\nHow can I modify the previous piece of code to perform this operation inside the CIFAR10Sequence()
data generator?
Please don\'t use tf.keras.preprocessing.image.ImageDataGenerator()
so that the answer does not lose its generality for another type of similar problems that are of a different nature.
NB: The idea would be to create the new data "in real time" as the model is fed instead of creating (in advance) and storing on disk a new and augmented training set bigger than the original one to be used later (also in batches) during the training process of the model.
\nThx in advance
\n'I am trying to import a pretrained model from Huggingface's transformers library and extend it with a few layers for classification using tensorflow keras. When I directly use transformers model (Method 1), the model trains well and reaches a validation accuracy of 0.93 after 1 epoch. However, when trying to use the model as a layer within a tf.keras model (Method 2), the model can't get above 0.32 accuracy. As far as I can tell based on the documentation, the two approaches should be equivalent. My goal is to get Method 2 working so that I can add more layers to it instead of directly using the logits produced by Huggingface's classifier head but I'm stuck at this stage.
\nimport tensorflow as tf\n\nfrom transformers import TFRobertaForSequenceClassification\n
\nMethod 1:
\nmodel = TFRobertaForSequenceClassification.from_pretrained("roberta-base", num_labels=6)\n
\nMethod 2:
\ninput_ids = tf.keras.Input(shape=(128,), dtype='int32')\n\nattention_mask = tf.keras.Input(shape=(128, ), dtype='int32')\n\ntransformer = TFRobertaForSequenceClassification.from_pretrained("roberta-base", num_labels=6)\n\nencoded = transformer([input_ids, attention_mask])\n\nlogits = encoded[0]\n\nmodel = tf.keras.models.Model(inputs = [input_ids, attention_mask], outputs = logits)\n\n
\nRest of the code for either method is identical,
\nmodel.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=3e-5, epsilon=1e-08, clipnorm=1.0),\nloss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), \n metrics=[tf.keras.metrics.SparseCategoricalAccuracy('accuracy')])\n
\nI am using Tensorflow 2.3.0 and have tried with transformers versions 3.5.0 and 4.0.0.
\n"In the official tf.custom_gradient documentation it shows how to define custom gradients for log(1 + exp(x))
@tf.custom_gradient\ndef log1pexp(x):\n e = tf.exp(x)\n def grad(dy):\n return dy * (1 - 1 / (1 + e))\n return tf.math.log(1 + e), grad\n
\nWhen y = log(1 + exp(x))
, analytically the derivative comes out to be dy/dx = (1 - 1 / (1 + exp(x)))
.
However in the code def grad
says its dy * (1 - 1 / (1 + exp(x)))
.\ndy/dx = dy * (1 - 1 / (1 + exp(x)))
is not a valid equation. While dx = dy * (1 - 1 / (1 + exp(x)))
is wrong as it should be the reciprocal.
What does the grad
function equate to?
I want to train a convolutional neural network with TensorFlow to do multi-output multi-class classification.
\n\nFor example: If we take the MNIST sample set and always combine two random images two a single one and then want to classify the resulting image. The result of the classification should be the two digits shown in the image.
\n\nSo the output of the network could have the shape [-1, 2, 10] where the first dimension is the batch, the second represents the output (is it the first or the second digit) and the third is the "usual" classification of the shown digit.
\n\nI tried googling for this for a while now, but wasn\'t able find something useful. Also, I don\'t know if multi-output multi-class classification is the correct naming for this task. If not, what is the correct naming? Do you have any links/tutorials/documentations/papers explaining what I\'d need to do to build the loss function/training operations?
\n\nWhat I tried was to split up the output of the network into the single outputs with tf.split and then use softmax_cross_entropy_with_logits on every single output. The result I averaged over all outputs but it doesn\'t seem to work. Is this even a reasonable way?
\n'When operating in graph mode in TF1, I believe I needed to wire up training=True
and training=False
via feeddicts when I was using the functional-style API. What is the proper way to do this in TF2?
I believe this is automatically handled when using tf.keras.Sequential
. For example, I don\'t need to specify training
in the following example from the docs:
model = tf.keras.Sequential([\n tf.keras.layers.Conv2D(32, 3, activation=\'relu\',\n kernel_regularizer=tf.keras.regularizers.l2(0.02),\n input_shape=(28, 28, 1)),\n tf.keras.layers.MaxPooling2D(),\n tf.keras.layers.Flatten(),\n tf.keras.layers.Dropout(0.1),\n tf.keras.layers.Dense(64, activation=\'relu\'),\n tf.keras.layers.BatchNormalization(),\n tf.keras.layers.Dense(10, activation=\'softmax\')\n])\n\n# Model is the full model w/o custom layers\nmodel.compile(optimizer=\'adam\',\n loss=\'sparse_categorical_crossentropy\',\n metrics=[\'accuracy\'])\n\nmodel.fit(train_data, epochs=NUM_EPOCHS)\nloss, acc = model.evaluate(test_data)\nprint("Loss {:0.4f}, Accuracy {:0.4f}".format(loss, acc))\n
\n\nCan I also assume that keras automagically handles this when training with the functional api? Here is the same model, rewritten using the function api:
\n\ninputs = tf.keras.Input(shape=((28,28,1)), name="input_image")\nhid = tf.keras.layers.Conv2D(32, 3, activation=\'relu\',\n kernel_regularizer=tf.keras.regularizers.l2(0.02),\n input_shape=(28, 28, 1))(inputs)\nhid = tf.keras.layers.MaxPooling2D()(hid)\nhid = tf.keras.layers.Flatten()(hid)\nhid = tf.keras.layers.Dropout(0.1)(hid)\nhid = tf.keras.layers.Dense(64, activation=\'relu\')(hid)\nhid = tf.keras.layers.BatchNormalization()(hid)\noutputs = tf.keras.layers.Dense(10, activation=\'softmax\')(hid)\nmodel_fn = tf.keras.Model(inputs=inputs, outputs=outputs)\n\n# Model is the full model w/o custom layers\nmodel_fn.compile(optimizer=\'adam\',\n loss=\'sparse_categorical_crossentropy\',\n metrics=[\'accuracy\'])\n\nmodel_fn.fit(train_data, epochs=NUM_EPOCHS)\nloss, acc = model_fn.evaluate(test_data)\nprint("Loss {:0.4f}, Accuracy {:0.4f}".format(loss, acc))\n
\n\nI\'m unsure if hid = tf.keras.layers.BatchNormalization()(hid)
needs to be hid = tf.keras.layers.BatchNormalization()(hid, training)
?
A colab for these models can be found here.
\n'I am using Tensorflow 2.0 and am able to train a CNN for image classification of 3-channel images. I perform image preprocessing within the data input pipeline (shown below) and would like to include the preprocessing functionality in the served model itself. My model is served with a TF Serving Docker container and the Predict API.
\n\nThe data input pipeline for training is based on the documentation at https://www.tensorflow.org/alpha/tutorials/load_data/images.
\n\nMy pipeline image preprocessing function is load_and_preprocess_from_path_label:
\n\ndef load_and_preprocess_path(image_path):\n\n # Load image\n image = tf.io.read_file(image_path)\n image = tf.image.decode_png(image)\n\n # Normalize to [0,1] range\n image /= 255\n\n # Convert to HSV and Resize\n image = tf.image.rgb_to_hsv(image)\n image = tf.image.resize(image, [HEIGHT, WIDTH])\n\n return image\n\ndef load_and_preprocess_from_path_label(image_path, label):\n\n return load_and_preprocess_path(image_path), label\n
\n\nWith lists of image paths, the pipeline prefetches and performs image preprocessing using tf functions within load_and_preprocess_from_path_label:
\n\nall_image_paths, all_image_labels = parse_labeled_image_paths()\nx_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(all_image_paths, all_image_labels, test_size=0.2)\n\n# Create a TensorFlow Dataset of training images and labels\nds = tf.data.Dataset.from_tensor_slices((x_train, y_train))\nimage_label_ds = ds.map(load_and_preprocess_from_path_label)\n\nBATCH_SIZE = 32\nIMAGE_COUNT = len(all_image_paths)\n\nds = image_label_ds.apply(tf.data.experimental.shuffle_and_repeat(buffer_size=IMAGE_COUNT))\nds = ds.batch(BATCH_SIZE)\nds = ds.prefetch(buffer_size=AUTOTUNE)\n\n# Create image pipeline for model\nimage_batch, label_batch = next(iter(ds))\nfeature_map_batch = model(image_batch)\n\n# Train model\nmodel.fit(ds, epochs=5)\n
\n\nPrevious Tensorflow examples I\'ve found use serving_input_fn(), and utilized tf.placeholder which seems to no longer exist in Tensorflow 2.0.
\n\nAn example for serving_input_fn in Tensorflow 2.0 is shown on https://www.tensorflow.org/alpha/guide/saved_model. Since I am using the Predict API, it looks like I would need something similar to:
\n\nserving_input_fn = tf.estimator.export.build_raw_serving_input_receiver_fn(...)\n\n# Save the model with the serving preprocessing function\nmodel.export_saved_model(MODEL_PATH, serving_input_fn)\n\n
\n\nIdeally, the served model would accept a 4D Tensor of 3-channel image samples of any size and would perform the initial image preprocessing on them (decode image, normalize, convert to HSV, and resize) before classifying.
\n\nHow can I create a serving_input_fn in Tensorflow 2.0 with a preprocessing function similar to my load_and_preprocess_path function?
\n'I ma trying to understand tf.rank function in tensorflow. From the documentation here, I understood that rank should return the number of distinct elements in the tensor.
\n\nHere x and weights are 2 distinct 2*2 tensors with 4 distinct elemnts in each of them. However, rank() function outputs are:
\n\n\n\n\nTensor("Rank:0", shape=(), dtype=int32) Tensor("Rank_1:0", shape=(),\n dtype=int32)
\n
Also, for the tensor x, I used tf.constant() with dtype = float to convert ndarray into float32 tensor but the rank() still outputs as int32.
\n\ng = tf.Graph()\nwith g.as_default():\n weights = tf.Variable(tf.truncated_normal([2,2]))\n x = np.asarray([[1 , 2], [3 , 4]])\n x = tf.constant(x, dtype = tf.float32)\n y = tf.matmul(weights, x)\n print (tf.rank(x), tf.rank(weights))\n\n\nwith tf.Session(graph = g) as s:\n tf.initialize_all_variables().run()\n print (s.run(weights), s.run(x))\n print (s.run(y))\n
\n\nHow should I interpret the output.
\n'My weights are defined as
\n\nweights = {\n 'W_conv1': tf.get_variable('W_conv1', shape=[...], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01)),\n 'W_conv2': tf.get_variable('W_conv2', shape=[...], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01)),\n 'W_conv3': tf.get_variable('W_conv3', shape=[...], dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01)),\n ...\n}\n\n# conv2d network\n...\n
\n\nI want to use the weights decay so I add, for example, the argument
\n\nregularizer=tf.contrib.layers.l1_regularizer(0.0005)\n
\n\nto the tf.get_variable
. Now I'm wondering if during the evaluation phase this is still correct or maybe I have to set the regularizer factor to 0.
There is also another argument trainable
. The documentation says If True also add the variable to the graph collection GraphKeys.TRAINABLE_VARIABLES.
which is not clear to me. Should I use it?
Can someone explain to me if the weights decay effects in a sort of wrong way the evaluation step? How can I solve in that case?
\n"By this piece of code from the documentation, we can create multiple features to feed batches of data into a DNN model:
\n\nmy_feature_columns = []\nfor key in train_x.keys():\n my_feature_columns.append(tf.feature_column.numeric_column(key=key))\n
\n\nBut the problem is what is the proper way to transform the original features before they are fed to the input layer? Typical transformations that I can think of include normalization and clipping.
\n\ntf.feature_column.numeric_column
does have a parameter specifying the normalization function. But the example in the doc only demonstrate a scenario where the normalization factors are pre-defined and fixed, like lambda x: (x-3.2)/1.5
. How can I perform normalization (e.g. MinMaxScaler
in sklearn) across all those features without knowing its maximum and minimum beforehand.
Also, is there any pipeline implementation where it's possible to do all sorts of feature transformations before they go into the input layer? Is creating a custom estimator tf.estimator.Estimator
the answer to this problem? or anything else I'm not aware of.
Where one can find the github source code for tf.quantization.fake_quant_with_min_max_args
. Checking the TF API documentation, there is no link to the github source file, and I could not find one on github.
New to TensorFlow, so apologies for newbie question.
+ +Following this tutorial but instead of using image data I am using numerical data.
+ +Load the dataset:
+ +train_dataset_url = \"xxx.csv\"
+train_dataset_fp = tf.keras.utils.get_file(
+ fname=os.path.basename(train_dataset_url),
+ origin=train_dataset_url)
+
+
+Make training dataset:
+ +batch_size = 32
+
+train_dataset = tf.contrib.data.make_csv_dataset(
+ train_dataset_fp,
+ batch_size,
+ column_names=column_names,
+ label_name=label_name,
+ num_epochs=1)
+
+
+Train classified model using:
+ +model = tf.keras.Sequential([
+ tf.keras.layers.Dense(10, activation=tf.nn.relu, input_shape=(1,)),
+ tf.keras.layers.Dense(10, activation=tf.nn.relu),
+ tf.keras.layers.Dense(4)
+])
But when I \"test\" the model with the same inputs:
+ +predictions = model(features)
I receive the error:
+ +InvalidArgumentError: cannot compute MatMul as input #0(zero-based) was expected to be a float tensor but is a int32 tensor [Op:MatMul]
It's possible I have missed something fundamental. I feel like I need to specify a type somewhere.
") ``` @@ -306,7 +401,7 @@ preds = model("Where one can find the github source code for Where one can find the github source code for tf.quantiz
### Training Set Metrics
| Training set | Min | Median | Max |
|:-------------|:----|:--------|:-----|
-| Word count | 15 | 336.203 | 3755 |
+| Word count | 15 | 336.765 | 3755 |
| Label | Training Sample Count |
|:------|:----------------------|
@@ -330,119 +425,22 @@ preds = model("
tf.quantiz
- max_length: 256
- seed: 42
- eval_max_steps: -1
-- load_best_model_at_end: False
+- load_best_model_at_end: True
### Training Results
-| Epoch | Step | Training Loss | Validation Loss |
-|:------:|:----:|:-------------:|:---------------:|
-| 0.0004 | 1 | 0.26 | - |
-| 0.02 | 50 | 0.2486 | - |
-| 0.04 | 100 | 0.2383 | - |
-| 0.06 | 150 | 0.309 | - |
-| 0.08 | 200 | 0.2551 | - |
-| 0.1 | 250 | 0.2675 | - |
-| 0.12 | 300 | 0.2344 | - |
-| 0.14 | 350 | 0.2686 | - |
-| 0.16 | 400 | 0.2447 | - |
-| 0.18 | 450 | 0.2317 | - |
-| 0.2 | 500 | 0.2233 | - |
-| 0.22 | 550 | 0.1999 | - |
-| 0.24 | 600 | 0.2443 | - |
-| 0.26 | 650 | 0.1667 | - |
-| 0.28 | 700 | 0.2975 | - |
-| 0.3 | 750 | 0.0902 | - |
-| 0.32 | 800 | 0.1965 | - |
-| 0.34 | 850 | 0.1571 | - |
-| 0.36 | 900 | 0.1247 | - |
-| 0.38 | 950 | 0.0494 | - |
-| 0.4 | 1000 | 0.1222 | - |
-| 0.42 | 1050 | 0.0828 | - |
-| 0.44 | 1100 | 0.0393 | - |
-| 0.46 | 1150 | 0.0104 | - |
-| 0.48 | 1200 | 0.0143 | - |
-| 0.5 | 1250 | 0.0505 | - |
-| 0.52 | 1300 | 0.0053 | - |
-| 0.54 | 1350 | 0.0337 | - |
-| 0.56 | 1400 | 0.0013 | - |
-| 0.58 | 1450 | 0.0061 | - |
-| 0.6 | 1500 | 0.0519 | - |
-| 0.62 | 1550 | 0.0068 | - |
-| 0.64 | 1600 | 0.001 | - |
-| 0.66 | 1650 | 0.0004 | - |
-| 0.68 | 1700 | 0.0008 | - |
-| 0.7 | 1750 | 0.0018 | - |
-| 0.72 | 1800 | 0.0018 | - |
-| 0.74 | 1850 | 0.0022 | - |
-| 0.76 | 1900 | 0.0005 | - |
-| 0.78 | 1950 | 0.0008 | - |
-| 0.8 | 2000 | 0.0005 | - |
-| 0.82 | 2050 | 0.0003 | - |
-| 0.84 | 2100 | 0.0004 | - |
-| 0.86 | 2150 | 0.0002 | - |
-| 0.88 | 2200 | 0.0003 | - |
-| 0.9 | 2250 | 0.0001 | - |
-| 0.92 | 2300 | 0.0001 | - |
-| 0.94 | 2350 | 0.0002 | - |
-| 0.96 | 2400 | 0.0005 | - |
-| 0.98 | 2450 | 0.0002 | - |
-| 1.0 | 2500 | 0.0002 | - |
-| 1.02 | 2550 | 0.0001 | - |
-| 1.04 | 2600 | 0.0001 | - |
-| 1.06 | 2650 | 0.0003 | - |
-| 1.08 | 2700 | 0.0002 | - |
-| 1.1 | 2750 | 0.0002 | - |
-| 1.12 | 2800 | 0.0001 | - |
-| 1.1400 | 2850 | 0.0001 | - |
-| 1.16 | 2900 | 0.0002 | - |
-| 1.18 | 2950 | 0.0594 | - |
-| 1.2 | 3000 | 0.0002 | - |
-| 1.22 | 3050 | 0.0002 | - |
-| 1.24 | 3100 | 0.0001 | - |
-| 1.26 | 3150 | 0.0262 | - |
-| 1.28 | 3200 | 0.0001 | - |
-| 1.3 | 3250 | 0.0001 | - |
-| 1.32 | 3300 | 0.0001 | - |
-| 1.34 | 3350 | 0.0001 | - |
-| 1.3600 | 3400 | 0.0001 | - |
-| 1.38 | 3450 | 0.0002 | - |
-| 1.4 | 3500 | 0.0 | - |
-| 1.42 | 3550 | 0.0001 | - |
-| 1.44 | 3600 | 0.0001 | - |
-| 1.46 | 3650 | 0.0001 | - |
-| 1.48 | 3700 | 0.0001 | - |
-| 1.5 | 3750 | 0.0001 | - |
-| 1.52 | 3800 | 0.0001 | - |
-| 1.54 | 3850 | 0.0001 | - |
-| 1.56 | 3900 | 0.0001 | - |
-| 1.58 | 3950 | 0.0001 | - |
-| 1.6 | 4000 | 0.0001 | - |
-| 1.62 | 4050 | 0.0002 | - |
-| 1.6400 | 4100 | 0.0044 | - |
-| 1.6600 | 4150 | 0.0001 | - |
-| 1.6800 | 4200 | 0.0002 | - |
-| 1.7 | 4250 | 0.0001 | - |
-| 1.72 | 4300 | 0.0001 | - |
-| 1.74 | 4350 | 0.0001 | - |
-| 1.76 | 4400 | 0.0001 | - |
-| 1.78 | 4450 | 0.0 | - |
-| 1.8 | 4500 | 0.0001 | - |
-| 1.8200 | 4550 | 0.0001 | - |
-| 1.8400 | 4600 | 0.0 | - |
-| 1.8600 | 4650 | 0.061 | - |
-| 1.88 | 4700 | 0.0002 | - |
-| 1.9 | 4750 | 0.0001 | - |
-| 1.92 | 4800 | 0.0001 | - |
-| 1.94 | 4850 | 0.0001 | - |
-| 1.96 | 4900 | 0.0001 | - |
-| 1.98 | 4950 | 0.0001 | - |
-| 2.0 | 5000 | 0.0001 | - |
+| Epoch | Step | Training Loss | Validation Loss |
+|:-------:|:--------:|:-------------:|:---------------:|
+| 0.0004 | 1 | 0.3216 | - |
+| **1.0** | **2500** | **0.0001** | **0.3943** |
+| 2.0 | 5000 | 0.0 | 0.3993 |
+* The bold row denotes the saved checkpoint.
### Framework Versions
-- Python: 3.10.12
+- Python: 3.10.13
- SetFit: 1.0.3
-- Sentence Transformers: 2.4.0
-- Transformers: 4.37.2
-- PyTorch: 2.1.0+cu121
+- Sentence Transformers: 2.5.0
+- Transformers: 4.38.1
+- PyTorch: 2.1.2
- Datasets: 2.17.1
- Tokenizers: 0.15.2