Inputs shape is uknown during training (model subclassing) - tensorflow

Consider following model
class FractalNeuralNetwork(tf.keras.Model):
def __init__(self, class_number):
super(FractalNeuralNetwork, self).__init__()
self.box_counting_patches = [BoxCountingPatch(box_size) for box_size in range(3, 41 + 1, 2)]
self.chebyshev = ChebyshevBinaryPatch()
self.euclidean = EuclideanBinaryPatch()
self.manhattan = ManhattanBinaryPatch()
self.percolation_c = PercolationC()
self.percolation_m = PercolationM()
self.percolation_q = PercolationQ()
self.probability = ProbabilityMatrix()
self.fractal_dimension = FractalDimension()
self.lacunarity = Lacunarity()
self.assemble = AssembleFractalImage()
self.resize = tf.keras.layers.Resizing(width=224, height=224)
self.rescale = tf.keras.layers.Rescaling(scale=1./255)
self.mobilenet_v2 = hub.KerasLayer("https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4",
output_shape=[1280],
trainable=False)
self.combine = tf.keras.layers.Add()
self.score = tf.keras.layers.Dense(class_number, activation='softmax')
def call(self, inputs):
inputs = tf.ensure_shape(inputs, self.ensure_input_shape)
box_counting_patches = [box_counting_patch(inputs) for box_counting_patch in self.box_counting_patches]
chebyshev = self.chebyshev(inputs=box_counting_patches)
euclidean = self.euclidean(inputs=box_counting_patches)
manhattan = self.manhattan(inputs=box_counting_patches)
percolation_c = self.percolation_c(inputs=[chebyshev, euclidean, manhattan])
percolation_m = self.percolation_m(inputs=[chebyshev, euclidean, manhattan])
percolation_q = self.percolation_q(inputs=[chebyshev, euclidean, manhattan])
probability = self.probability(inputs=[chebyshev, euclidean, manhattan])
fractal_dimension = self.fractal_dimension(inputs=probability)
lacunarity = self.lacunarity(inputs=probability)
fractal_output = self.assemble(
inputs=[
fractal_dimension,
lacunarity,
percolation_c,
percolation_m,
percolation_q
]
)
fractal_output = self.resize(fractal_output)
fractal_output = self.rescale(fractal_output)
fractal_output = self.mobilenet_v2(fractal_output)
original_output = self.rescale(inputs)
original_output = self.mobilenet_v2(original_output)
combined_output = self.combine([fractal_output, original_output])
output = self.score(combined_output)
return output
Every custom layer here is not trainable, they just perform calculations - extract fractal features from images.
The model is trained with the following code:
model = FractalNeuralNetwork(
class_number=CLASS_NUMBER
)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(
training_set,
validation_data=validation_set,
epochs=1
)
The first batch comes with a normal shape (None, 224, 224, 3) but the second (None, None, None, None) and it break my model. Why does it happen?

Related

tensorflow meet a warning "Gradients do not exist for variables"

sorry I'm a beginner to tensorflow, when I tried to write a custom layer there was a warning 'Gradients do not exist for variables' after call model.fit()
To illastrate my issues, here are my code:
class gen_noise_sig(layers.Layer):
def __init__(self):
super(gen_noise_sig, self).__init__()
self.m = self.add_weight('m',[1],trainable=True).numpy()
self.sig = self.add_weight('sig',[1],trainable=True).numpy()
def get_config(self):
config = super().get_config()
config.update({
"m": self.m,
"sig": self.sig,
})
return config
def call(self, inputs, training = True):
out = inputs + tf.random.normal(tf.shape(inputs),mean=self.m,stddev=self.sig)
out = tf.expand_dims(out,axis=-1)
return out
def AutoEncoder():
model = tf.keras.Sequential()
model.add(gen_noise_sig())
model.add(layers.Conv1D(8,strides = 5,kernel_size = 128,padding = 'same',activation = 'relu',name='encoder_input'))
model.add(layers.Conv1D(16,strides = 5,kernel_size = 32,padding = 'same',activation = 'leaky_relu'))
model.add(layers.Conv1D(32,strides = 5,kernel_size = 16,padding = 'same'))
model.add(layers.Conv1D(128,strides = 4,kernel_size = 4,padding = 'same'))
model.add(layers.Conv1D(128,strides = 2,kernel_size = 2,padding = 'same',name='encoder_output'))
model.add(layers.Conv1DTranspose(128,strides = 2,kernel_size = 4,padding = 'same', name = 'decoder_input',activation='leaky_relu'))
model.add(layers.Conv1DTranspose(32,strides = 4,kernel_size = 4,padding = 'same'))
model.add(layers.Conv1DTranspose(16,strides = 5,kernel_size = 64,padding = 'same'))
model.add(layers.Conv1DTranspose(8,strides = 5,kernel_size = 64,padding = 'same'))
model.add(layers.Conv1DTranspose(1,strides = 5,kernel_size = 64,padding = 'same',name = 'decoder_output'))
model.add(layers.Flatten())
return model
model = AutoEncoder()
model.build(input_shape= (None,10000))
model.compile(optimizer='adam',
loss='mae',
metrics=['mae']
)
# print(model.trainable_weights)
filepath = 'AutoEncoder/Denoising/Damage/CNN_AE_model1020.h5'
checkpoint = keras.callbacks.ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min')
history = model.fit(targetData,
targetData,
batch_size=200,
epochs=1000,
validation_split = 0.1,
callbacks=[checkpoint])
Can someone tell me why the gradients do not exist and how to solve this problem, please.

Multiple predicted values with tensorflow Bidirectional LSTM

I want to predict 4 values (4 columns) using tensorflow Bidirectional LSTM. The input shape is (30, 525). Howerver, I obtained a prediction result containing one column (1 value).
Can you help me please?
I used this program:
tf.random.set_seed(1234)
file_training = 'trainingData.csv'
raw_data_training = pd.read_csv(file_training)
df_training = raw_data_training.copy()
df_training.isnull().sum()
file_validation = 'validationData.csv'
raw_data_validation = pd.read_csv(file_validation)
df_validation = raw_data_validation.copy()
df_validation.isnull().sum()
X_train = df_training.drop(['LO','LA','FL','BU'],axis = 1)
y_train = df_training.loc[:,['LO','LA','FL','BU']]
X_test = df_validation.drop(['LO','LA','FL','BU'], axis = 1)
y_test = df_validation.loc[:,['LO','LA','FL','BU']]
scaler_x = MinMaxScaler(feature_range = (0,1))
scaler_y = MinMaxScaler(feature_range = (0,1))
input_scaler = scaler_x.fit(X_train)
output_scaler = scaler_y.fit(y_train)
train_y_norm = output_scaler.transform(y_train)
train_x_norm = input_scaler.transform(X_train)
test_y_norm = output_scaler.transform(y_test)
test_x_norm = input_scaler.transform(X_test)
def create_dataset (X, y, time_steps = 1):
Xs, ys = [], []
for i in range(len(X)-time_steps):
v = X[i:i+time_steps, :]
Xs.append(v)
ys.append(y[i+time_steps])
return np.array(Xs), np.array(ys)
TIME_STEPS = 30
X_test, y_test = create_dataset(test_x_norm, test_y_norm, TIME_STEPS)
X_train, y_train = create_dataset(train_x_norm, train_y_norm, TIME_STEPS)
def create_model_bilstm(units):
model = Sequential()
model.add(Bidirectional(LSTM(units = units, return_sequences=True),
input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Bidirectional(LSTM(units = units)))
model.add(Dense(1))
model.compile(loss='mse', optimizer='adam')
return model
model_bilstm = create_model_bilstm(64)
def fit_model(model):
early_stop = keras.callbacks.EarlyStopping(monitor = 'val_loss',
patience = 10)
history = model.fit(X_train, y_train, epochs = 100, validation_split = 0.2,
batch_size = 32, shuffle = True, callbacks = [early_stop])
return history
history_bilstm = fit_model(model_bilstm)
y_test = scaler_y.inverse_transform(y_test)
y_train = scaler_y.inverse_transform(y_train)
def prediction(model):
prediction = model.predict(X_test)
prediction = scaler_y.inverse_transform(prediction)# this gives ValueError: non-broadcastable output operand with shape (1081,1) doesn't match the broadcast shape (1081,4)
return prediction
prediction_bilstm = prediction(model_bilstm)```

How to build a Siamese Network from Transformer Model? Shape Input Error

I have the following Base Network with some important (error is coming due to these) parameters (please assume every else parameter)
maxlen = 250
model_dense = 256
Base Model :
def build_base_model(inputs):
inputs = layers.Input(shape=(maxlen,),name='base_input')
embedding_layer = TokenAndPositionEmbedding(maxlen, vocab_size, embed_dim)
x = embedding_layer(inputs)
transformer_block = TransformerBlock(embed_dim, num_heads, ff_dim, trans_drop1, trans_drop2, trans_reg1, trans_reg2)
x = transformer_block(x)
x = layers.GlobalAveragePooling1D()(x)
x = layers.Dropout(model_drop1)(x)
outputs = layers.Dense(model_dense)(x)
base_model = keras.Model(inputs=inputs, outputs=outputs)
return base_model
and I my Siamese network as:
base_model = build_base_model()
input_text1 = layers.Input(shape=(maxlen,))
input_text2 = layers.Input(shape=(maxlen,))
emb1 = base_model(input_text1)
emb2 = base_model(input_text2)
distance = layers.Lambda(euclidean_distance)([emb1, emb2])
outputs = layers.Dense(1, activation="sigmoid")(distance)
model = keras.Model(inputs=[emb1, emb2], outputs=outputs)
model.compile(
optimizer="adam", metrics = ["accuracy",], loss= 'binary_crossentropy')
history = model.fit(
train_X, train_y, batch_size=batch_size, epochs = 50, validation_split = 0.15, callbacks = callbacks, verbose = 1,
)
It gives me an error as:
ValueError: Input 0 of layer "model_11" is incompatible with the layer: expected shape=(None, 256), found shape=(None, 250)
What am I doing wrong?
Base Transformer model tutorial taken from this
Siamese Model Structure, cosine distance, make_pairs from this
UPDATE- I have built the new network in a different manner and it is up and running. Can someone please confirms if it is the correct one:
inputs1 = layers.Input(shape=(maxlen,),name='inp_1')
inputs2 = layers.Input(shape=(maxlen,),name='inp_2')
embedding_layer = TokenAndPositionEmbedding(maxlen, vocab_size, embed_dim)
transformer_block = TransformerBlock(embed_dim, num_heads, ff_dim, trans_drop1, trans_drop2, trans_reg1, trans_reg2)
pooling = layers.GlobalAveragePooling1D()
drop_layer = layers.Dropout(model_drop1)
out_dense = layers.Dense(model_dense)
x1 = embedding_layer(inputs1)
x2 = embedding_layer(inputs2)
x1 = transformer_block(x1)
x2 = transformer_block(x2)
x1 = pooling(x1)
x2 = pooling(x2)
x1 = drop_layer(x1)
x2 = drop_layer(x2)
vec_x1 = out_dense(x1)
vec_x2 = out_dense(x2)
distance = layers.Lambda(euclidean_distance)([vec_x1, vec_x2])
outputs = layers.Dense(1, activation="sigmoid")(distance)
model = keras.Model(inputs=[inputs1, inputs2], outputs=outputs)
in the linemodel = keras.Model(inputs=[emb1, emb2], outputs=outputs):
I suspect that you are mean to saymodel = keras.Model(inputs=[input_text1, input_text2], outputs=outputs)

How to implement U Net for custom dataset using Tensorflow and Keras

I want to implement U Net for semantic segmentation on my own dataset which contains two classes. Can anyone please let me know how can I implement with Tensorflow and Keras.
I have two classes in my dataset with their corresponding labels.
This is an U-Net implementation for binary classification/segmentation.
def UNET_224(dropout_val=0.0, weights=None): # No dropout by default
if K.image_dim_ordering() == 'th':
inputs = Input((INPUT_CHANNELS, 224, 224))
axis = 1
else:
inputs = Input((224, 224, INPUT_CHANNELS))
axis = 3
filters = 32
conv_224 = double_conv_layer(inputs, filters)
pool_112 = MaxPooling2D(pool_size=(2, 2))(conv_224)
conv_112 = double_conv_layer(pool_112, 2*filters)
pool_56 = MaxPooling2D(pool_size=(2, 2))(conv_112)
conv_56 = double_conv_layer(pool_56, 4*filters)
pool_28 = MaxPooling2D(pool_size=(2, 2))(conv_56)
conv_28 = double_conv_layer(pool_28, 8*filters)
pool_14 = MaxPooling2D(pool_size=(2, 2))(conv_28)
conv_14 = double_conv_layer(pool_14, 16*filters)
pool_7 = MaxPooling2D(pool_size=(2, 2))(conv_14)
conv_7 = double_conv_layer(pool_7, 32*filters)
up_14 = concatenate([UpSampling2D(size=(2, 2))(conv_7), conv_14], axis=axis)
up_conv_14 = double_conv_layer(up_14, 16*filters)
up_28 = concatenate([UpSampling2D(size=(2, 2))(up_conv_14), conv_28], axis=axis)
up_conv_28 = double_conv_layer(up_28, 8*filters)
up_56 = concatenate([UpSampling2D(size=(2, 2))(up_conv_28), conv_56], axis=axis)
up_conv_56 = double_conv_layer(up_56, 4*filters)
up_112 = concatenate([UpSampling2D(size=(2, 2))(up_conv_56), conv_112], axis=axis)
up_conv_112 = double_conv_layer(up_112, 2*filters)
up_224 = concatenate([UpSampling2D(size=(2, 2))(up_conv_112), conv_224], axis=axis)
up_conv_224 = double_conv_layer(up_224, filters, dropout_val)
conv_final = Conv2D(OUTPUT_MASK_CHANNELS, (1, 1))(up_conv_224)
conv_final = Activation('sigmoid')(conv_final)
model = Model(inputs, conv_final, name="UNET_224")
return model

Trainer Component Error in Tensorflow Extension

it is my first time building a tfx pipeline after I went through the tutorials and trying to build one with my own dataset. I could use some advice on the transform code that I wrote, and understand better, I'd appreciate your time and thanks in advance.
I've done the ExampleGen, StatisticsGen, SchemaGen, ExampleValidator, Transform, and having error in Trainer components.
ERROR:
c:\lib\site-packages\tfx\orchestration\launcher\in_process_component_launcher.py in _run_executor(self, execution_id, input_dict, output_dict, exec_properties)
65 executor_context) # type: ignore
66
---> 67 executor.Do(input_dict, output_dict, exec_properties)
c:\lib\site-packages\tfx\components\trainer\executor.py in Do(self, input_dict, output_dict, exec_properties)
317
318 fn_args = self._GetFnArgs(input_dict, output_dict, exec_properties)
--> 319 trainer_fn = self._GetFn(exec_properties, 'trainer_fn')
320
321 schema = io_utils.parse_pbtxt_file(fn_args.schema_file, schema_pb2.Schema())
c:\lib\site-packages\tfx\components\trainer\executor.py in _GetFn(self, exec_properties, fn_name)
128 if has_module_file:
129 return import_utils.import_func_from_source(
--> 130 exec_properties['module_file'], fn_name)
131
132 fn_path_split = exec_properties[fn_name].split('.')
c:\lib\site-packages\tfx\utils\import_utils.py in import_func_from_source(source_path, fn_name)
66 user_module = types.ModuleType(loader.name)
67 loader.exec_module(user_module)
---> 68 return getattr(user_module, fn_name)
69
70 except IOError:
AttributeError: module 'user_module' has no attribute 'trainer_fn'
CODE:
def get_model(show_summary=True):
#one-hot categorical features
num_A = 4,
num_B = 3,
num_C = 2,
num_D = 8,
num_E = 12,
num_F = 4,
num_G = 16,
num_H = 26
input_A = tf.keras.Input(shape=(num_A,), name="A_xf")
input_B = tf.keras.Input(shape=(num_B,), name="B_xf")
input_C = tf.keras.Input(shape=(num_C,), name="C_xf")
input_D = tf.keras.Input(shape=(num_D,), name="D_xf")
input_E = tf.keras.Input(shape=(num_E,), name="E_xf")
input_F = tf.keras.Input(shape=(num_F,), name="F_xf")
input_G = tf.keras.Input(shape=(num_G,), name="G_xf")
input_H = tf.keras.Input(shape=(num_H,), name="H_xf")
fl = keras.Input(shape=(75,))
dense = layers.Dense(35, activation = "relu")
x = dense(fl)
x = layers.Dense(15, activation="relu")(x)
outputs = layers.Dense(1, activation="sigmoid")(x)
_inputs = [input_A, input_B, input_C, input_D, input_E, input_F, input_G, input_H]
model = keras.Model(inputs=inputs, outputs=outputs)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
if show_summary:
model.summary()
return model
We had the same problem and were able to solve it by migrating from the tf.keras sequential model api to the functional api. You can read about the different model apis here. You'll notice in the tutorials that the wide and deep model is implemented as a functional api.
As an example, here is a sample of a keras model for the trainer component:
def _build_functional_test_model():
colname = 'feature_string_xf'
feature_columns = [
tf.feature_column.numeric_column(colname, shape=_MAX_REQUEST_LEN, dtype=tf.dtypes.int64, default_value=0)]
input_layers = {
colname: tf.keras.layers.Input(name=colname, shape=_MAX_REQUEST_LEN, dtype=tf.int64)
}
input_dense_layer = tf.keras.layers.DenseFeatures(feature_columns)(input_layers)
embedding = tf.keras.layers.Embedding(_N_UNIQUE_WORDS,
_N_EMBED,
input_length=_MAX_REQUEST_LEN)(input_dense_layer)
embedding_dropout = tf.keras.layers.SpatialDropout1D(_DROPOUT_EMBEDDING)(embedding)
conv_1 = tf.keras.layers.Conv1D(_N_CONV_1,
_K_CONV_1,
activation='linear',
activity_regularizer=tf.keras.regularizers.l1(_REGULARIZE_L1_CONV))(
embedding_dropout)
activation_1 = tf.keras.layers.Activation('relu')(conv_1)
dropout_1 = tf.keras.layers.Dropout(_DROPOUT_CONV_1)(activation_1)
conv_2 = tf.keras.layers.Conv1D(_N_CONV_1,
_K_CONV_2,
activation='linear',
activity_regularizer=tf.keras.regularizers.l1(_REGULARIZE_L1_CONV))(dropout_1)
activation_2 = tf.keras.layers.Activation('relu')(conv_2)
dropout_2 = tf.keras.layers.Dropout(_DROPOUT_CONV_1)(activation_2)
conv_3 = tf.keras.layers.Conv1D(_N_CONV_3,
_K_CONV_3,
activation='linear',
activity_regularizer=tf.keras.regularizers.l1(_REGULARIZE_L1_CONV))(dropout_2)
activation_3 = tf.keras.layers.Activation('relu')(conv_3)
dropout_3 = tf.keras.layers.Dropout(_DROPOUT_CONV_1)(activation_3)
conv_4 = tf.keras.layers.Conv1D(_N_CONV_4,
_K_CONV_4,
activation='linear',
activity_regularizer=tf.keras.regularizers.l1(_REGULARIZE_L1_CONV))(dropout_3)
activation_4 = tf.keras.layers.Activation('relu')(conv_4)
dropout_4 = tf.keras.layers.Dropout(_DROPOUT_CONV_1)(activation_4)
max_pool_5 = tf.keras.layers.GlobalMaxPooling1D()(dropout_4)
dense_6 = tf.keras.layers.Dense(_N_DENSE_1,
activation='linear',
# activity_regularizer=keras.regularizers.l1(_REGULARIZE_L1_DENSE_1)
)(max_pool_5)
activation_6 = tf.keras.layers.Activation('relu')(dense_6)
dropout_6 = tf.keras.layers.Dropout(_REGULARIZE_L1_DENSE_1)(activation_6)
dense_7 = tf.keras.layers.Dense(_N_DENSE_2,
activation='linear',
# activity_regularizer=keras.regularizers.l1(_REGULARIZE_L1_DENSE_1)
)(dropout_6)
activation_6 = tf.keras.layers.Activation('relu')(dense_7)
dropout_7 = tf.keras.layers.Dropout(_REGULARIZE_L1_DENSE_1)(activation_6)
output = tf.keras.layers.Dense(1, activation='sigmoid')(dropout_7)
model = tf.keras.Model(input_layers, output)
model.compile(loss='binary_crossentropy', optimizer=_OPTIMIZER, metrics=['accuracy'])
return model
By using the directions and comment by Jason, I've changed up the model part as tfx doesn't support the sequential model but the Keras functional API.
def get_model(show_summary=True):
#one-hot categorical features
num_A = 4,
num_B = 3,
num_C = 2,
num_D = 8,
num_E = 12,
num_F = 4,
num_G = 16,
num_H = 26
input_A = tf.keras.Input(shape=(num_A,), name="A_xf")
input_B = tf.keras.Input(shape=(num_B,), name="B_xf")
input_C = tf.keras.Input(shape=(num_C,), name="C_xf")
input_D = tf.keras.Input(shape=(num_D,), name="D_xf")
input_E = tf.keras.Input(shape=(num_E,), name="E_xf")
input_F = tf.keras.Input(shape=(num_F,), name="F_xf")
input_G = tf.keras.Input(shape=(num_G,), name="G_xf")
input_H = tf.keras.Input(shape=(num_H,), name="H_xf")
inputs_con = tf.keras.layers.concatenate([
input_A,
input_B,
input_C,
input_D,
input_E,
input_F,
input_G,
input_H])
dense_1 = tf.keras.layers.Dense(50, activation = 'relu')(inputs_con)
dense_2 = tf keras.layers.Dense(25, activation = "rely") (dense_1)
output = tf.keras.laters.Dense(1, activation = "sigmoid") (dense_2)
model = keras.Model(inputs=inputs, outputs=outputs)
_inputs = [
input_A,
input_B,
input_C,
input_D,
input_E,
input_F,
input_G,
input_H]
model = tf.keras.models.Model(_inputs, output)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
if show_summary:
model.summary()
return model