quasi-SVM for a custom dataset(binary classifier) - tensorflow

I used the code for Quasi-SVM and had error.
model = keras.Sequential(
[
keras.Input(shape=(50176,)),
keras.layers.experimental.RandomFourierFeatures(
output_dim=4096, scale=10.0, kernel_initializer="gaussian"),
keras.layers.Dense(units=10),
]
)
Error:
Input 0 of layer "random_fourier_features" is incompatible with the layer: expected ndim=2, found ndim=4. Full shape received: (None, None, None, None)

Related

Fit concatenated modules throws error regarding the input

I have 3 models that are pre-trained. I want to concat the models and then add a fully connected network on top:
output = concatenate(
[
resnet.layers[-2].output, # model1
cnn2.layers[-2].output, #model2
cnn.layers[-2].output #model3
], axis=-1)
fc = Dense(units=4096, activation="relu")(output)
fc = Dense(units=4096, activation="relu")(fc)
logits = Dense(3, activation="sigmoid")(fc)
model = Model(inputs=[resnet.inputs, cnn2.inputs, cnn.inputs], outputs=logits)
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["acc"])
epochs = 10
x_train_second, x_val_second, y_train_second, y_val_second = train_test_split(x_train, y_train, test_size=0.15)
history = model.fit(
datagen.flow([x_train_second, x_train_second, x_train_second], y=y_train_second, batch_size=64),
epochs=epochs, steps_per_epoch=x_train_second.shape[0] // 64,
)
I get the error:
ValueError: Layer "model_68" expects 3 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, None, None, None) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None, None, None, None) dtype=float32>]
Which is a bit confusing since I am passing 3 inputs to x in model.fit(). Even if I insert 4 or 5 inputs there I also get the same error, which suggests that something is way off...
Update: So I think it is because of datagen.flow(), because if I use:
model.fit([x_train_second, x_train_second, x_train_second], y=y_train_second, batch_size=64,
epochs=epochs
)
It is working.So the question is to use ImageDataGenerator.flow() for merged module

tensorflow hub example throws an error when Float16 is enabled

I am trying to load a model from Tensorflowhub using example code. It works perfect with the FP32. As soon as I add the tf.keras.mixed_precision.set_global_policy('mixed_float16') to enable mixed float, it raises an error. Looks like the dimension issue but then it works perfect with FP32. Here is the reproducible code:
import tensorflow as tf
import tensorflow_hub as hub
IMAGE_SIZE = (224,224)
class_names = ['cat','dog']
#If you comment out the following line, the code works fine.
tf.keras.mixed_precision.set_global_policy('mixed_float16')
# --------
model_handle = "https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/5"
do_fine_tuning = False
print("Building model with", model_handle)
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=IMAGE_SIZE + (3,)),
hub.KerasLayer(model_handle, trainable=do_fine_tuning),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(len(class_names),
kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+IMAGE_SIZE+(3,))
model.summary()
The following error is thrown:
Building model with https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/5
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [8], in <cell line: 4>()
2 do_fine_tuning = False
3 print("Building model with", model_handle)
----> 4 model = tf.keras.Sequential([
5 tf.keras.layers.InputLayer(input_shape=IMAGE_SIZE + (3,)),
6 hub.KerasLayer(model_handle, trainable=do_fine_tuning),
7 tf.keras.layers.Dropout(rate=0.2),
8 tf.keras.layers.Dense(len(class_names),
9 kernel_regularizer=tf.keras.regularizers.l2(0.0001))
10 ])
11 model.build((None,)+IMAGE_SIZE+(3,))
12 model.summary()
File ~/miniconda3/envs/fahtx/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py:587, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs)
585 self._self_setattr_tracking = False # pylint: disable=protected-access
586 try:
--> 587 result = method(self, *args, **kwargs)
588 finally:
589 self._self_setattr_tracking = previous_value # pylint: disable=protected-access
File ~/miniconda3/envs/fahtx/lib/python3.8/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 /tmp/__autograph_generated_fileo7avm3_o.py:74, in outer_factory.<locals>.inner_factory.<locals>.tf__call(self, inputs, training)
72 result = ag__.converted_call(ag__.ld(smart_cond).smart_cond, (ag__.ld(training), ag__.autograph_artifact((lambda : ag__.converted_call(ag__.ld(f), (), dict(training=True), fscope))), ag__.autograph_artifact((lambda : ag__.converted_call(ag__.ld(f), (), dict(training=False), fscope)))), None, fscope)
73 result = ag__.Undefined('result')
---> 74 ag__.if_stmt(ag__.not_(ag__.ld(self)._has_training_argument), if_body_3, else_body_3, get_state_3, set_state_3, ('result', 'training'), 1)
76 def get_state_6():
77 return (result,)
File /tmp/__autograph_generated_fileo7avm3_o.py:72, in outer_factory.<locals>.inner_factory.<locals>.tf__call.<locals>.else_body_3()
70 training = False
71 ag__.if_stmt(ag__.ld(self).trainable, if_body_2, else_body_2, get_state_2, set_state_2, ('training',), 1)
---> 72 result = ag__.converted_call(ag__.ld(smart_cond).smart_cond, (ag__.ld(training), ag__.autograph_artifact((lambda : ag__.converted_call(ag__.ld(f), (), dict(training=True), fscope))), ag__.autograph_artifact((lambda : ag__.converted_call(ag__.ld(f), (), dict(training=False), fscope)))), None, fscope)
File /tmp/__autograph_generated_fileo7avm3_o.py:72, in outer_factory.<locals>.inner_factory.<locals>.tf__call.<locals>.else_body_3.<locals>.<lambda>()
70 training = False
71 ag__.if_stmt(ag__.ld(self).trainable, if_body_2, else_body_2, get_state_2, set_state_2, ('training',), 1)
---> 72 result = ag__.converted_call(ag__.ld(smart_cond).smart_cond, (ag__.ld(training), ag__.autograph_artifact((lambda : ag__.converted_call(ag__.ld(f), (), dict(training=True), fscope))), ag__.autograph_artifact((lambda : ag__.converted_call(ag__.ld(f), (), dict(training=False), fscope)))), None, fscope)
ValueError: Exception encountered when calling layer "keras_layer_3" (type KerasLayer).
in user code:
File "/root/miniconda3/envs/fahtx/lib/python3.8/site-packages/tensorflow_hub/keras_layer.py", line 237, in call *
result = smart_cond.smart_cond(training,
ValueError: Could not find matching concrete function to call loaded from the SavedModel. Got:
Positional arguments (4 total):
* <tf.Tensor 'inputs:0' shape=(None, 224, 224, 3) dtype=float16>
* False
* False
* 0.99
Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1:
Positional arguments (4 total):
* TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
* True
* True
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 2:
Positional arguments (4 total):
* TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
* True
* False
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 3:
Positional arguments (4 total):
* TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
* False
* True
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Option 4:
Positional arguments (4 total):
* TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
* False
* False
* TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
Keyword arguments: {}
Call arguments received by layer "keras_layer_3" (type KerasLayer):
• inputs=tf.Tensor(shape=(None, 224, 224, 3), dtype=float16)
• training=False
It is about target 'dtype' when float16 is enabled as the variable rules it trying to use the float16 then you just need to specify float32 as the model input required. I like to include channel numbers as the image properties when switching colors to transforming. Some functions work without channels number but for those, you need a translation method. Sample Resize( ) -> img_to_array( ) -> Predict () OR Boundary Boxes working.
[ Sample ]:
import tensorflow as tf
import tensorflow_hub as hub
IMAGE_SIZE = ( 224,224,3 )
class_names = ['cat','dog']
tf.keras.mixed_precision.set_global_policy('mixed_float16')
model_handle = "https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/5"
do_fine_tuning = False
print("Building model with", model_handle)
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=IMAGE_SIZE, dtype=tf.float32),
hub.KerasLayer(model_handle, trainable=do_fine_tuning , dtype=tf.float32),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(len(class_names),
kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+ IMAGE_SIZE)
model.summary()
[ Error ]:
ValueError: Could not find matching concrete function to call loaded from the SavedModel. Got:
Positional arguments (4 total):
* <tf.Tensor 'inputs:0' shape=(None, 224, 224, 3) dtype=float16>
* False
* False
* 0.99
Keyword arguments: {}
[ Output ]:
F:\temp\Python>python tf_test_mixed_float16.py
WARNING:tensorflow:Mixed precision compatibility check (mixed_float16): WARNING
Your GPU may run slowly with dtype policy mixed_float16 because it does not have compute capability of at least 7.0. Your GPU:
NVIDIA GeForce GTX 1060 6GB, compute capability 6.1
See https://developer.nvidia.com/cuda-gpus for a list of GPUs and their compute capabilities.
If you will use compatible GPU(s) not attached to this host, e.g. by running a multi-worker model, you can ignore this warning. This message will only be logged once
Building model with https://tfhub.dev/google/imagenet/resnet_v1_50/feature_vector/5
2022-06-17 15:02:41.319205: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-06-17 15:02:41.878364: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1532] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 4632 MB memory: -> device: 0, name: NVIDIA GeForce GTX 1060 6GB, pci bus id: 0000:01:00.0, compute capability: 6.1
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
keras_layer (KerasLayer) (None, 2048) 23561152
dropout (Dropout) (None, 2048) 0
dense (Dense) (None, 2) 4098
=================================================================
Total params: 23,565,250
Trainable params: 4,098
Non-trainable params: 23,561,152
_________________________________________________________________
F:\temp\Python>

Tensorflow: ValueError: `generator` yielded an element of shape () where an element of shape () was expected

My dataset:
ds = tf.data.Dataset.from_generator(
data_generator.generate,
output_types=({"input_1": tf.int64, "input_2": tf.int64}, tf.int64),
output_shapes = ({"input_1": tf.TensorShape([None, 500]), "input_2":
tf.TensorShape([None, 500])},tf.TensorShape(None)))
My preprocess function returns:
return ({'input_1': ids1, 'input_2': ids2}, label)
My model (taking multiple inputs):
input_1 = Input(shape=(500,) , name='input_1')
............
input_2 = Input(shape=(500,) , name='input_2')
On starting the training, I'm getting the following error:
ValueError: generator yielded an element of shape (500,) where an element of shape (None, 500) was expected
Any idea what I might be doing wrong ?
EDIT: On updating the output_shapes to:
output_shapes = ({"input_1": tf.TensorShape([500]), "input_2": tf.TensorShape([500])},tf.TensorShape(None)))
I get the following warning:
WARNING:tensorflow:Model was constructed with shape (None, 500) for input KerasTensor(type_spec=TensorSpec(shape=(None, 500), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'"), but it was called on an input with incompatible shape (500, 1)

ValueError: Input 0 of layer lstm_34 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 25]

I am a newbie with lstm, can anyone please explain why I am getting this error?
my lstm model -
def NNStructure():
input_text = tf.keras.Input((), dtype = tf.string, name = 'input_text')
initial_input= Embedding(vocab_size, 25, weights=
[embedding_matrix], input_length=50,trainable=False) (input_text)
lstm = LSTM(32, input_shape=(50,25))(initial_input)
secondary_input = Input(shape=(50,6))
merge = concatenate([lstm, secondary_input])(lstm)
first_dense = Dense(128, activation='relu')(merge)
second_dense=Dense(64, activation='relu')(first_dense)
output=Dense(1, activation='sigmoid')(second_dense)
model_glove1 = Model(inputs=[initial_input, secondary_input], outputs=output)
return model_glove1
model_glove1=NNStructure()
model_glove1.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model_glove1.fit(x=[pad_seq,X_meta_train], y=np.array(Y_train), batch_size=32,epochs=4,verbose=1,validation_split=0.2,shuffle=True)

Basic RNN training using fit_generator doesn't output the expected shape

I'm implementing a basic RNN composed of a 512 units GRU and a dense layer using Keras:
model = Sequential()
model.add(GRU(units=512,
return_sequences=True,
input_shape=(None, num_x_signals,)))
model.add(Dense(num_y_signals, activation='sigmoid'))
I needed to generate input batches on the fly so I used fit_generator :
model.fit_generator(generator=generator_train, epochs=NB_EPOCHS, steps_per_epoch=STEPS_PER_EPOCH,
validation_data=generator_test, validation_steps=900, callbacks=callbacks)
And here is how I define my batch generator :
SAMPLE_PERIOD_PER_INPUT = 1728
PERIOD_TO_PREDICT = 288
BATCH_SIZE = 64
def batch_generator(batch_size, sequence_length, train = True):
while True:
x_shape = (batch_size, sequence_length, num_x_signals)
x_batch = np.zeros(shape=x_shape, dtype=np.float16)
y_shape = (batch_size, PERIOD_TO_PREDICT, num_y_signals)
y_batch = np.zeros(shape=y_shape, dtype=np.float16)
for i in range(batch_size):
if train:
idx = np.random.randint(num_train - sequence_length)
predict_idx = (idx + sequence_length) - PERIOD_TO_PREDICT
x_batch[i] = x_train_scaled[idx:idx+sequence_length]
y_batch[i] = y_train_scaled[predict_idx:idx+sequence_length]
else:
idx = np.random.randint(num_test - sequence_length)
predict_idx = (idx + sequence_length) - PERIOD_TO_PREDICT
x_batch[i] = x_test_scaled[idx:idx+sequence_length]
y_batch[i] = y_test_scaled[predict_idx:idx+sequence_length]
yield (x_batch, y_batch)
generator_train = batch_generator(batch_size=BATCH_SIZE, sequence_length=SAMPLE_PERIOD_PER_INPUT)
generator_test = batch_generator(batch_size=BATCH_SIZE, sequence_length=SAMPLE_PERIOD_PER_INPUT, train = False)
I also use a "custom" loss function because I need to ignore the first computed sequence which is supposed to not be accurate :
warmup_steps = 50
def loss_mse_warmup(y_true, y_pred):
y_true_slice = y_true[:, warmup_steps:, :]
y_pred_slice = y_pred[:, warmup_steps:, :]
loss = tf.losses.mean_squared_error(labels=y_true_slice,
predictions=y_pred_slice)
loss_mean = tf.reduce_mean(loss)
return loss_mean
optimizer = RMSprop(lr=1e-3)
model.compile(loss=loss_mse_warmup, optimizer=optimizer)
Here is the summary of my model :
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
gru (GRU) (None, None, 512) 798720
_________________________________________________________________
dense (Dense) (None, None, 1) 513
=================================================================
Total params: 799,233
Trainable params: 799,233
Non-trainable params: 0
_________________________________________________________________
But when I run this it says that there shape errors :
2 root error(s) found.
(0) Invalid argument: Incompatible shapes: [64,238,1] vs. [64,1678,1]
[[{{node loss_4/dense_loss/mean_squared_error/SquaredDifference}}]]
[[loss_4/mul/_167]]
(1) Invalid argument: Incompatible shapes: [64,238,1] vs. [64,1678,1]
[[{{node loss_4/dense_loss/mean_squared_error/SquaredDifference}}]]
0 successful operations.
0 derived errors ignored.
Any ideas why ? Where did I write something wrong ?