I am a total newbie here so if I write something wrong sorry in advance.
I am trying to use Resnet50 model to classify a skin illness.
When I try to run the model there is an error occurring. First, I thought about the cause of the data type of images I convert them to and array to tensor but didn't work.
Here is my code
directory_infection_negative = glob.glob("/content/drive/MyDrive/DFU_Classification_Datasets/DFU_Classification_Datasets/PartB_DFU_Dataset/Infection/Aug-Negative/*")
for img in directory_infection_negative:
image = cv.imread(img)
imageRGB = cv.cvtColor(image, cv.COLOR_BGR2RGB)
img = cv2.resize(imageRGB, (size,size), interpolation = cv2.INTER_AREA)
imgT = tf.convert_to_tensor(img)
datasets.append(imgT)
labels.append(0)
def resnet(input_shape, n_classes):
def conv_bn_rl(x, f, k=1, s=1, p='same'):
x = Conv2D(f, k, strides=s, padding=p)(x)
x = BatchNormalization()(x)
x = ReLU()(x)
return x
def identity_block(tensor, f):
x = conv_bn_rl(tensor, f)
x = conv_bn_rl(x, f, 3)
x = Conv2D(4*f, 1)(x)
x = BatchNormalization()(x)
x = Add()([x, tensor])
output = ReLU()(x)
return output
def conv_block(tensor, f, s):
x = conv_bn_rl(tensor, f)
x = conv_bn_rl(x, f, 3, s)
x = Conv2D(4*f, 1)(x)
x = BatchNormalization()(x)
shortcut = Conv2D(4*f, 1, strides=s)(tensor)
shortcut = BatchNormalization()(shortcut)
x = Add()([x, shortcut])
output = ReLU()(x)
return output
def resnet_block(x, f, r, s=2):
x = conv_block(x, f, s)
for _ in range(r-1):
x = identity_block(x, f)
return x
input = Input(input_shape)
x = conv_bn_rl(input, 64, 7, 2)
x = MaxPool2D(3, strides=2, padding='same')(x)
x = resnet_block(x, 64, 3, 1)
x = resnet_block(x, 128, 4)
x = resnet_block(x, 256, 6)
x = resnet_block(x, 512, 3)
x = GlobalAvgPool2D()(x)
output = Dense(n_classes, activation='softmax')(x)
model = Model(input, output)
return model
INPUT_SHAPE = 224,224,3
N_CLASSES = 4
K.clear_session()
model = resnet(INPUT_SHAPE, N_CLASSES)
model.compile(optimizer = 'adam',
loss = 'categorical_crossentropy',
metrics=['accuracy'])
model.summary()
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
X_train, X_test, y_train, y_test = train_test_split(datasets, to_categorical(np.array(labels)), test_size=0.2, random_state=0)
y_train = to_categorical(y_train, 4)
y_test = to_categorical(y_test, 4)
y_train.shape
print(len(X_train))
print(len(X_test))
print(len(y_train))
print(len(y_test))
print(type(X_train))
print(type(X_test))
print(type(X_train[0]))
history = model.fit((X_train), (y_train), batch_size = 32, verbose = 1, epochs= 50, validation_split=0.1, shuffle = False)
print('Test Accuracy: {:.2f}%'.format(model.evaluate(np.array(X_test), np.array(y_test))[1]*100))
f,(ax1, ax2) = plt.subplots(1,2, figsize=(12,4))
t = f.suptitle('CNN Performance', fontsize=12)
f.subplots_adjust(top =0.85, wspace=0.3)
max_epoch = len(history.history['accuracy'])+1
epoch_list = list(range(1,max_epoch))
ax1.plot(epoch_list, history.history['accuracy'], label='Train Accuracy')
ax1.plot(epoch_list, history.history['val_accuracy'], label='Validation Accuracy')
ax1.set_xticks(np.arange(1, max_epoch,5))
ax1.set_ylabel('Accuracy Value')
ax1.set_xlabel('Epoch')
ax1.set_title('Accuracy')
l1 = ax1.legend(loc="best")
ax2.plot(epoch_list, history.history['loss'], label='Train Loss')
ax2.plot(epoch_list, history.history['val_loss'], label='Validation Loss')
ax2.set_xticks(np.arange(1, max_epoch,5))
ax2.set_ylabel('Loss Value')
ax2.set_xlabel('Epoch')
ax2.set_title('Loss')
l2 = ax2.legend(loc="best")
model.save('DFU Classifier - Resnet50')
Error Says :
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
1127 except Exception as e: # pylint:disable=broad-except
1128 if hasattr(e, "ag_error_metadata"):
-> 1129 raise e.ag_error_metadata.to_exception(e)
1130 else:
1131 raise
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 808, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 199, in assert_input_compatibility
raise ValueError(f'Layer "{layer_name}" expects {len(input_spec)} input(s),'
ValueError: Layer "model" expects 1 input(s), but it received 12608 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:1' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:2' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:3' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:4' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:5' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:6' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:7' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:8' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:9' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:10' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:11' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:12' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:13' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:14' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:15' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:16' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:17' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:18' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:19' shape=(None, 224, 3) dtype=uint8>, <tf.Tensor 'IteratorGetNext:20' shape=(None, 224, 3) dtype=uin...
Thank you so much in advance
So, i will count the issues :
You do not need ( should not) convert images to tensor if you are using fit. So, delete:
imgT = tf.convert_to_tensor(img)
After that you should convert datasets or at least whatever you are inputting to the network np.array or list of np.array. Fit expects list of arrays or np.array itself otherwise it causes trouble by going against their algorithmic logic .So, converting datasets to a suitable format would solve that many inputs issue:
datasets = np.array(datasets, dtype=np.float32)
Related
This is the following code :
Hyper parameters
batch_size = 64
nb_epoch = 50
Parameters for dataset
img_rows, img_cols = 160, 160
nb_classes = 10
Parameters for network
nb_lstm_outputs = 30
nb_time_steps = img_rows
dim_input_vector = img_cols
print('X_train original shape:', trainX.shape)
input_shape = (nb_time_steps, dim_input_vector)
trainX = trainX.astype('float32') / 255.
testX = testX.astype('float32') / 255.
trainY = np_utils.to_categorical(trainY, nb_classes)
testY = np_utils.to_categorical(testY, nb_classes)
print('X_train shape:', trainX.shape)
print(trainX.shape[0], 'train samples')
print(testX.shape[0], 'test samples')
Utility for our sequence model.
def get_sequence_model():
class_vocab = label_processor.get_vocabulary()
frame_features_input = keras.Input((input_shape))
x = keras.layers.GRU(16, return_sequences=True)(frame_features_input)
x = keras.layers.Dropout(0.4)(x)
x = keras.layers.GRU(8)(x)
x = keras.layers.Dropout(0.4)(x)
x = keras.layers.Dense(8, activation="relu")(x)
output = keras.layers.Dense(len(class_vocab), activation="softmax")(x)
rnn_model = keras.Model(frame_features_input, output)
rnn_model.compile(
loss="sparse_categorical_crossentropy", optimizer="adam", metrics=["accuracy"]
)
return rnn_model
Utility for running experiments.
def run_experiment():
seq_model = get_sequence_model()
history = seq_model.fit(
x = trainX,
y = trainY,
batch_size=64,
epochs=500,
validation_data=(testX, testY)
)
# seq_model.load_weights(filepath)
_, accuracy = seq_model.evaluate([test_data[0], test_data[1]], test_labels)
print(f"Test accuracy: {round(accuracy * 100, 2)}%")
return history, seq_model
_, sequence_model = run_experiment()
I am getting the following error :
enter image description here
Why am I getting the following error and how can I solve it?
Epoch 1/500
WARNING:tensorflow:Model was constructed with shape (None, 160, 160) for input KerasTensor(type_spec=TensorSpec(shape=(None, 160, 160), dtype=tf.float32, name='input_32'), name='input_32', description="created by layer 'input_32'"), but it was called on an input with incompatible shape (None, 160, 160, 3).
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-138-a1d4870dee01> in <module>
40
41
---> 42 _, sequence_model = run_experiment()
2 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 889, in train_step
y_pred = self(x, training=True)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 214, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" '
ValueError: Exception encountered when calling layer "model_9" (type Functional).
Input 0 of layer "gru_29" is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 160, 160, 3)
Call arguments received by layer "model_9" (type Functional):
• inputs=tf.Tensor(shape=(None, 160, 160, 3), dtype=float32)
• training=True
• mask=None
Can't solve this problem can someone help?
python3.8
tensorflow2.4
ubuntu20.04
I am trying to build a image captioning custom dataset with tensorflow.
The error says
File "/keras_imagecaption/p3.py", line 429, in <module>
tr.TrainModel()
File "/keras_imagecaption/japa_p3.py", line 348, in TrainModel
self.model.fit_generator(generator, epochs=self.epochs, steps_per_epoch=steps, verbose=1)
File "/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1847, in fit_generator
return self.fit(
ValueError: Layer model expects 2 input(s), but it received 3 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, None) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None, None) dtype=int32>, <tf.Tensor 'IteratorGetNext:2' shape=(None, None) dtype=float32>]
def DataGenerator(self):
"""
・model.fit_generatorに読み込ませるデータジェネレータを作成する。
"""
while 1:
for key, image_texts in self.train_texts_dict.items():
image_feature = self.train_features_dict[key][0]
in_img, in_seq, out_word = self.MakeInputOutput(image_texts, image_feature)
#yield [[in_img, in_seq], out_word]
yield [[in_img, in_seq], out_word]
def TrainModel(self):
"""
・モデルを訓練する。
"""
self.MakeTokenizer()
self.GetVocabSize()
self.GetMaxLength()
self.MakeCaptioningModel()
steps=len(self.train_texts_dict)
for i in range(self.epochs):
generator = self.DataGenerator()
self.model.fit_generator(generator, epochs=self.epochs, steps_per_epoch=steps, verbose=1)
print("fit")
self.model.save('model_' + str(i) + '.h5')
return None
def get_three_classes(x, y):
indices_0, _ = np.where(y == 0.)
indices_1, _ = np.where(y == 1.)
indices_2, _ = np.where(y == 2.)
indices = np.concatenate([indices_0, indices_1, indices_2], axis=0)
x = x[indices]
y = y[indices]
count = x.shape[0]
indices = np.random.choice(range(count), count, replace=False)
x = x[indices]
y = y[indices]
y = tf.keras.utils.to_categorical(y)
return x, y
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()
x_train, y_train = get_three_classes(x_train, y_train)
x_test, y_test = get_three_classes(x_test, y_test)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
class_names = ['aeroplane', 'car', 'bird']
def show_random_examples (x, y, p):
indices = np.random.choice(range(x.shape[0]), 10, replace = False)
x= x [indices]
y= y [indices]
p = p[indices]
plt.figure(figsize = (10,5))
for i in range(10):
plt.subplot(2,5, 1+i)
plt.imshow(x[i])
plt.xticks([])
plt.yticks([])
col = 'green' if np.argmax(y[i]) == np.argmax(p[i]) else 'red'
plt.xlabel(class_names[np.argmax(p[i])], color = col)
plt.show()
#Creating model
def create_model():
def add_conv_block(model, num_filters):
model.add(Conv2D(num_filters, 3, activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(Conv2D(num_filters, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.5))
return model
model = tf.keras.models.Sequential()
model.add(Input(shape=(32,32,3)))
model= add_conv_block(model, 32)
model= add_conv_block(model, 64)
model= add_conv_block(model, 128)
model.add(Flatten())
model.add(Dense(3, activation='softmax'))
model.compile(
loss='categorical_crossentropy',
optimizer = 'adam', metrics = ['accuracy']
)
return model
h = model.fit (
x_train/255., y_train,
validation_data = (x_test/255., y_test),
epochs=10, batch_size=128,
callbacks=[tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=3),
tf.keras.callbacks.ModelCheckpoint(
'/content/drive/MyDrive/models/model_val{val_accuracy:.3f}.h5',
save_best_only=True, save_weights_only=False,
monitor='val_accuracy') ]
)
model = tf.keras.models.load_model('/content/drive/MyDrive/models/model_val0.896.h5')
preds= model.predict(x_test/255.)
image = np.array(Image.open("/content/drive/MyDrive/thy3.jpg").resize((32, 32)))
print("shape: ", image.shape)
shape: (32, 32)
pr_mask = model.predict(image).round()
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1621, in predict_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1611, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1604, in run_step **
outputs = model.predict_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1572, in predict_step
return self(x, training=False)
File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 263, in assert_input_compatibility
raise ValueError(f'Input {input_index} of layer "{layer_name}" is '
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)
Hey everyone, this is code im working on it for project but problem is i cannot implement random photos on this model.Getting this error message, I just want to get result for random photos, and model predict it.Its basic tensorflow project only using cifar10 dataset, and only 3 classes outputs. Model predict is it car, airplane or bird thats it. Why i want is lets say i found airjet photo from web and wants this model predict it, is it airplane or bird? prediction could be wrong or accurate i dont care. But problem is i cannot format photos, or maybe you can give me idea how can i deal with that . Thanks for your answers.
The image used for the predict function is in your code defined as follows:
image = np.array(Image.open("/content/drive/MyDrive/thy3.jpg").resize((32, 32)))
Which corresponds with the following shape according to the error message you posted:
(32, 32, 3)
Error message posted:
ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)
But the model requires the image to have a batch dimension for the prediciton, like:
(1, 32, 32, 3)
If your image is a 3-channel with width=32 and height=32 use to reshape it:
image = np.array(Image.open("/content/drive/MyDrive/thy3.jpg").reshape((1, 32, 32, 3)))
I want to implement a straight through estimator in tensorflow. I am using tf.stop_gradient() method for this. When I implement in the inner layers it works fine but when used on the last layer it throws an error. Below are my code and error.
def generator():
model = Sequential([
Embedding(21, 100, input_shape = (50, )),
Conv1D(100, 3,2, padding = "same", activation = "relu"),
Conv1D(100, 3,2, padding = "same", activation = "relu"),
Conv1D(21, 3,1, padding = "same", activation = "relu"),
Flatten(),
Dense(1,"sigmoid", trainable = False),
Lambda(lambda x: tf.stop_gradient(x)),
])
return model
gen = generator()
def dis_inputs():
ind = np.random.randint(0, len(x_train), batch_size * 2)
real = x_train[ind]
out = np.ones((batch_size * 2, ))
out[: batch_size ] = 0
return real, out
batch_size = 128
x, y = dis_inputs()
gen.compile("adam", "binary_crossentropy")
gen.fit(x, y, epochs = 100)
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 816, in train_step
self.optimizer.minimize(loss, self.trainable_variables, tape=tape)
File "/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/optimizer_v2.py", line 532, in minimize
return self.apply_gradients(grads_and_vars, name=name)
File "/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/optimizer_v2.py", line 633, in apply_gradients
grads_and_vars = optimizer_utils.filter_empty_gradients(grads_and_vars)
File "/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/utils.py", line 73, in filter_empty_gradients
raise ValueError(f"No gradients provided for any variable: {variable}. "
ValueError: No gradients provided for any variable: (['embedding_4/embeddings:0', 'conv1d_12/kernel:0', 'conv1d_12/bias:0', 'conv1d_13/kernel:0', 'conv1d_13/bias:0', 'conv1d_14/kernel:0', 'conv1d_14/bias:0'],). Provided `grads_and_vars` is ((None, <tf.Variable 'embedding_4/embeddings:0' shape=(21, 100) dtype=float32>), (None, <tf.Variable 'conv1d_12/kernel:0' shape=(3, 100, 100) dtype=float32>), (None, <tf.Variable 'conv1d_12/bias:0' shape=(100,) dtype=float32>), (None, <tf.Variable 'conv1d_13/kernel:0' shape=(3, 100, 100) dtype=float32>), (None, <tf.Variable 'conv1d_13/bias:0' shape=(100,) dtype=float32>), (None, <tf.Variable 'conv1d_14/kernel:0' shape=(3, 100, 21) dtype=float32>), (None, <tf.Variable 'conv1d_14/bias:0' shape=(21,) dtype=float32>)).
System information
OS Platform and Distribution :CentOS Linux release 7.7.1908
-TensorFlow version:2.3.0
I am following this example:https://www.tensorflow.org/tutorials/text/image_captioning?hl=en
It is working as it should be and saving checkpoints and I want to now convert this to a TF Lite model.
Here is the Link of full convert code:https://colab.research.google.com/drive/1GJkGcwWvDAWMooTsECzuSRUSPbirADhb?usp=sharing
Here is the Link of full train code:
https://colab.research.google.com/drive/1X2d9WW1EMEzN8Rgva3rtjevP0T_jFccj?usp=sharing
I also following the isssue#32999
Here is what I am running to save and them convert the inference graph:
#tf.function
def evaluate(image):
hidden = decoder.reset_states(batch_size=1)
temp_input = tf.expand_dims(load_image(image)[0], 0)
img_tensor_val = image_features_extract_model(temp_input)
img_tensor_val = tf.reshape(img_tensor_val, (img_tensor_val.shape[0], -1, img_tensor_val.shape[3]))
features = encoder(img_tensor_val)
dec_input = tf.expand_dims([tokenizer.word_index['<start>']], 0)
result = []
for i in range(max_length):
predictions, hidden, attention_weights = decoder(dec_input, features, hidden)
predicted_id = tf.random.categorical(predictions, 1)[0][0]
# print(tokenizer.index_word)
print(predicted_id,predicted_id.dtype)
# for key,value in tokenizer.index_word.items():
# key = tf.convert_to_tensor(key)
# tf.dtypes.cast(key,tf.int64)
# print(key)
# print(tokenizer.index_word)
result.append(predicted_id)
# if tokenizer.index_word[predicted_id] == '<end>':
# return result
dec_input = tf.expand_dims([predicted_id], 0)
return result
export_dir = "./"
tflite_enc_input = ''
ckpt.f = evaluate
to_save = evaluate.get_concrete_function('')
converter = tf.lite.TFLiteConverter.from_concrete_functions([to_save])
tflite_model = converter.convert()
but I get this error
ValueError: in user code:
convert2savedmodel.py:310 evaluate *
predictions, hidden, attention_weights = decoder(dec_input, features, hidden)
/share/nishome/19930072_0/miniconda3/envs/tf2.3/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py:985 __call__ **
outputs = call_fn(inputs, *args, **kwargs)
/share/nishome/19930072_0/miniconda3/envs/tf2.3/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py:780 __call__
result = self._call(*args, **kwds)
/share/nishome/19930072_0/miniconda3/envs/tf2.3/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py:840 _call
return self._stateless_fn(*args, **kwds)
/share/nishome/19930072_0/miniconda3/envs/tf2.3/lib/python3.7/site-packages/tensorflow/python/eager/function.py:2828 __call__
graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
/share/nishome/19930072_0/miniconda3/envs/tf2.3/lib/python3.7/site-packages/tensorflow/python/eager/function.py:3171 _maybe_define_function
*args, **kwargs)
/share/nishome/19930072_0/miniconda3/envs/tf2.3/lib/python3.7/site-packages/tensorflow/python/eager/function.py:2622 canonicalize_function_inputs
self._flat_input_signature)
/share/nishome/19930072_0/miniconda3/envs/tf2.3/lib/python3.7/site-packages/tensorflow/python/eager/function.py:2713 _convert_inputs_to_signature
format_error_message(inputs, input_signature))
ValueError: Python inputs incompatible with input_signature:
inputs: (
Tensor("ExpandDims_1:0", shape=(1, 1), dtype=int32),
Tensor("cnn__encoder/StatefulPartitionedCall:0", shape=(1, 64, 256), dtype=float32),
Tensor("zeros:0", shape=(1, 512), dtype=float32))
input_signature: (
TensorSpec(shape=(1, 1), dtype=tf.int64, name=None),
TensorSpec(shape=(1, 64, 256), dtype=tf.float32, name=None),
TensorSpec(shape=(1, 512), dtype=tf.float32, name=None))
Encoder Model:
class CNN_Encoder(tf.keras.Model):
def __init__(self, embedding):
super(CNN_Encoder, self).__init__()
# shape after fc == (batch_size, 64, embedding_dim)
self.fc = tf.keras.layers.Dense(embedding_dim)
#tf.function(input_signature=[tf.TensorSpec(shape=(1, 64, features_shape),dtype=tf.dtypes.float32)])
def call(self, x):
x = self.fc(x)
x = tf.nn.relu(x)
return x
Decoder model:
class RNN_Decoder(tf.keras.Model):
def __init__(self, embedding_dim, units, vocab_size):
super(RNN_Decoder, self).__init__()
self.units = units
self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
self.gru = tf.keras.layers.GRU(self.units,
return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform',
unroll = True)
self.fc1 = tf.keras.layers.Dense(self.units)
self.fc2 = tf.keras.layers.Dense(vocab_size)
self.attention = BahdanauAttention(self.units)
#tf.function(input_signature=[tf.TensorSpec(shape=[1, 1], dtype=tf.int64),
tf.TensorSpec(shape=[1, 64, 256], dtype=tf.float32),
tf.TensorSpec(shape=[1, 512], dtype=tf.float32)])
def call(self, x , features, hidden):
context_vector, attention_weights = self.attention(features, hidden)
#x shape after passing through embedding == (batch_size, 1, embedding_dim)
x = self.embedding(x)
#x shape after concatenation == (batch_size, 1, embedding_dim + hidden_size)
x = tf.concat([tf.expand_dims(context_vector, 1), x], axis=-1)
output, state = self.gru(x)
#shape == (batch_size, max_length, hidden_size)
x = self.fc1(output)
#x shape == (batch_size, max_length, hidden_size)
x = tf.reshape(x, (-1, x.shape[2]))
# output shape == (batch_size * max_length, vocab)
x = self.fc2(x)
return x, state, attention_weights
def reset_states(self, batch_size):
return tf.zeros((batch_size, self.units))
I just change the tf.function to int32 as below:
#tf.function(input_signature=[tf.TensorSpec(shape=[1, 1], dtype=tf.int32), tf.TensorSpec(shape=[1, 64,256], dtype=tf.float32), tf.TensorSpec(shape=[1, 512], dtype=tf.float32)])
but another error came:
ValueError: Python inputs incompatible with input_signature:
Tensor("ExpandDims_2:0", shape=(1, 1), dtype=int64),
Tensor("cnn__encoder/StatefulPartitionedCall:0", shape=(1, 64, 256), dtype=float32),
Tensor("rnn__decoder/StatefulPartitionedCall:1", shape=(1, 512), dtype=float32))
input_signature: (
TensorSpec(shape=(1, 1), dtype=tf.int32, name=None),
TensorSpec(shape=(1, 64, 256), dtype=tf.float32, name=None),
TensorSpec(shape=(1, 512), dtype=tf.float32, name=None))```
Why the dtypes of inputs change from int64 to int32?