bad zip file error while loading the trained deep learning model - python

I trained my model in colab and save it with torch.save('model.pth')
and then when i wanted to load it in my pycharm i get this error:
File "C:\Users\Amin\AppData\Local\Programs\Python\Python310\lib\zipfile.py", line 1334, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file`
can anyone help me to fix this error please
i could not find any solution for it on internet
i used tensorflow for training my model and used these imports :
from tensorflow.keras.preprocessing.text import text_to_word_sequence
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation
from tensorflow.keras.layers import Embedding
from tensorflow.keras.layers import Conv1D, GlobalMaxPooling1D
my program can load the tokenizer that i have built but it wont load the model
this is my model :
max_features = 1000
maxlen = 650
embedding_dims = 50
filters = 250
kernel_size = 3
hidden_dims = 250
model5 = Sequential()
model5.add(Embedding(max_features, embedding_dims ))
model5.add(Dropout(0.2))
model5.add(Conv1D(filters, kernel_size, padding='valid', activation='relu', strides=1))
model5.add(GlobalMaxPooling1D())
model5.add(Dense(hidden_dims)) model5.add(Dropout(0.2)) model5.add(Activation('relu'))
model5.add(Dense(5)) model5.add(Activation('softmax'))
model5.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
model5.fit(X_train, y_train,
batch_size=32,
epochs=14,
validation_data=(X_test, y_test))
torch.save(model5,'model.pth')
i loaded my model in colab and it was fine but it didn't work in pycharm
relative_model_path = "model.pth"
full_model_path = os.path.join(absolute_path, relative_model_path)
model = torch.load(full_model_path)
Traceback (most recent call last):
File "C:\\Users\\Amin\\PycharmProjects\\src\\model\\categorizer.py", line 25, in \<module\>
model = torch.load(full_model_path)
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\torch\\serialization.py", line 789, in load
return \_load(opened_zipfile, map_location, pickle_module, \*\*pickle_load_args)
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\torch\\serialization.py", line 1131, in \_load
result = unpickler.load()
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\pickle_utils.py", line 48, in deserialize_model_from_bytecode
raise e
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\pickle_utils.py", line 46, in deserialize_model_from_bytecode
model = saving_lib.load_model(filepath)
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\experimental\\saving_lib.py", line 196, in load_model
raise e
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\site-packages\\keras\\saving\\experimental\\saving_lib.py", line 173, in load_model
with zipfile.ZipFile(filepath, "r") as zipfile_to_load:
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\zipfile.py", line 1267, in __init__
self.\_RealGetContents()
File "C:\\Users\\Amin\\AppData\\Local\\Programs\\Python\\Python310\\lib\\zipfile.py", line 1334, in \_RealGetContents
raise BadZipFile("File is not a zip file")`your text`
zipfile.BadZipFile: File is not a zip file

I just needed to save the model with
keras.save('model')
not torch because the model was built in tensorflow keras

Related

Few errors occured when training the facial expression recognition model

I am working on a project to recognise facial expressions and train the facial expression recognition model by using convolutional neural network(CNN). In this project, I am using Tensorflow 2.4 version and Python 3.8.8 version
The output:
Found 18282 images belonging to 5 classes.
Found 7178 images belonging to 7 classes.
Below is the error that I got:
2023-01-11 00:09:29.625187: 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.
c:/Users/Documents/Bachelor of Computer Science/FYP/Code/Program/Backup Test/TrainEmotionDetector.py:53: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
emotion_model_info = emotion_model.fit_generator(
Epoch 1/20
2023-01-11 00:09:31.756943: W tensorflow/tsl/framework/cpu_allocator_impl.cc:82] Allocation of 31719424 exceeds 10% of free system memory.
Traceback (most recent call last):
File "c:/Users/Documents/Bachelor of Computer Science/FYP/Code/Program/Backup Test/TrainEmotionDetector.py", line 53, in <module>
emotion_model_info = emotion_model.fit_generator(
File "C:\Users\anaconda3\lib\site-packages\keras\engine\training.py", line 2604, in fit_generator
return self.fit(
File "C:\Users\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py", line 52, in quick_execute
tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
Detected at node 'categorical_crossentropy/softmax_cross_entropy_with_logits' defined at (most recent call last):
File "c:/Users/Documents/Code/Program/Backup Test/TrainEmotionDetector.py", line 53, in <module>
emotion_model_info = emotion_model.fit_generator(
File "C:\Users\anaconda3\lib\site-packages\keras\engine\training.py", line 2604, in fit_generator
return self.fit(
File "C:\Users\anaconda3\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
return fn(*args, **kwargs)
File "C:\Users\anaconda3\lib\site-packages\keras\engine\training.py", line 1650, in fit
tmp_logs = self.train_function(iterator)
File "C:\Users\anaconda3\lib\site-packages\keras\engine\training.py", line 1249, in train_function
return step_function(self, iterator)
File "C:\Users\anaconda3\lib\site-packages\keras\engine\training.py", line 1233, in step_function
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "C:\Users\anaconda3\lib\site-packages\keras\engine\training.py", line 1222, in run_step
outputs = model.train_step(data)
File "C:\Users\anaconda3\lib\site-packages\keras\engine\training.py", line 1024, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "C:\Users\anaconda3\lib\site-packages\keras\engine\training.py", line 1082, in compute_loss
return self.compiled_loss(
File "C:\Users\anaconda3\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "C:\Users\anaconda3\lib\site-packages\keras\losses.py", line 152, in __call__
losses = call_fn(y_true, y_pred)
File "C:\Users\anaconda3\lib\site-packages\keras\losses.py", line 284, in call
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "C:\Users\anaconda3\lib\site-packages\keras\losses.py", line 2004, in categorical_crossentropy
return backend.categorical_crossentropy(
File "C:\Users\anaconda3\lib\site-packages\keras\backend.py", line 5538, in categorical_crossentropy
return tf.nn.softmax_cross_entropy_with_logits(
Node: 'categorical_crossentropy/softmax_cross_entropy_with_logits'
logits and labels must be broadcastable: logits_size=[64,7] labels_size=[64,5]
[[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_1181]
2023-01-11 00:09:32.976764: W tensorflow/core/kernels/data/generator_dataset_op.cc:108] Error occurred when finalizing GeneratorDataset iterator: FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated.
[[{{node PyFunc}}]]
Below is the full code:
# import required packages
import cv2
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Dense, Dropout, Flatten
from keras.optimizers import Adam
from keras.preprocessing.image import ImageDataGenerator
# Initialize image data generator with rescaling
train_data_gen = ImageDataGenerator(rescale=1./255)
validation_data_gen = ImageDataGenerator(rescale=1./255)
# Preprocess all test images
train_generator = train_data_gen.flow_from_directory(
'data/train',
target_size=(48, 48),
batch_size=64,
color_mode="grayscale",
class_mode='categorical')
# Preprocess all train images
validation_generator = validation_data_gen.flow_from_directory(
'data/test',
target_size=(48, 48),
batch_size=64,
color_mode="grayscale",
class_mode='categorical')
# create model structure
emotion_model = Sequential()
emotion_model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(48, 48, 1)))
emotion_model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))
emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
emotion_model.add(MaxPooling2D(pool_size=(2, 2)))
emotion_model.add(Dropout(0.25))
emotion_model.add(Flatten())
emotion_model.add(Dense(1024, activation='relu'))
emotion_model.add(Dropout(0.5))
emotion_model.add(Dense(7, activation='softmax'))
cv2.ocl.setUseOpenCL(False)
emotion_model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.0001, decay=1e-6), metrics=['accuracy'])
# Train the neural network/model
emotion_model_info = emotion_model.fit_generator(
train_generator,
steps_per_epoch=28709 // 64,
epochs=20,
validation_data=validation_generator,
validation_steps=7178 // 64)
# save model structure in jason file
model_json = emotion_model.to_json()
with open("model/emotion_model.json", "w") as json_file:
json_file.write(model_json)
# save trained model weight in .h5 file
emotion_model.save_weights('model/emotion_model.h5')
I have upgraded the tensorflow to the latest version by using pip install --upgrade tensorflow but nothing works. It is expected to write the saved model into the emotion_model.json and emotion_model.h5. Please help to solve this problem.
logits and labels must be broadcastable:
logits_size=[64,7] labels_size=[64,5]
Well, you kind of gave it away when you mentioned
the first set of images had just 5 distinct labels
while the next set had 7.
Prune two of those label classes and you'll be back in business.

Using Keras Without GPU

I want to train a Neural Network using Keras but when I want to build the model I get the following error
2022-03-14 09:38:10.526372: E tensorflow/stream_executor/cuda/cuda_driver.cc:271] failed call to cuInit: CUDA_ERROR_NO_DEVICE: no CUDA-capable device is detected
2022-03-14 09:38:10.526465: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (HSKP02): /proc/driver/nvidia/version does not exist
2022-03-14 09:38:10.527391: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
I tried to solve this error by writing
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = "-1"
before importing Keras but I still get this error. After this error my code fits the data with the validation set with model.fit() but I get another error
Traceback (most recent call last):
File "shallownet_ex.py", line 44, in <module>
H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=100, verbose=1)
File ".../venv/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File ".../venv/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py", line 1147, in autograph_handler
raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:
File ".../venv/lib/python3.8/site-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File ".../venv/lib/python3.8/site-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File ".../venv/lib/python3.8/site-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File ".../venv/lib/python3.8/site-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File ".../venv/lib/python3.8/site-packages/keras/engine/training.py", line 918, in compute_loss
return self.compiled_loss(
File ".../venv/lib/python3.8/site-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File ".../venv/lib/python3.8/site-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File ".../venv/lib/python3.8/site-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File ".../venv/lib/python3.8/site-packages/keras/losses.py", line 1789, in categorical_crossentropy
return backend.categorical_crossentropy(
File ".../venv/lib/python3.8/site-packages/keras/backend.py", line 5083, in categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (None, 4) and (None, 3) are incompatible
The code I'm using looks like this
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ['CUDA_VISIBLE_DEVICES'] = "-1"
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from preprocesing import ImageToArrayPreprocessor, SimplePreprocesssor
from datasets import SimpleDatasetLoader
from neuralnetworks.conv import ShallowNet
from keras.optimizers import gradient_descent_v2
from imutils import paths
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required=True, help="path to input dataset")
args = vars(ap.parse_args())
imagePaths = list(paths.list_images(args["dataset"]))
sp = SimplePreprocesssor(32, 32)
iap = ImageToArrayPreprocessor()
sdl = SimpleDatasetLoader(preprocessors=[sp, iap])
(data, labels) = sdl.load(imagePaths, verbose=500)
data = data.astype("float") / 255.0
(trainX, testX, trainY, testY) = train_test_split(data, labels, test_size=0.25, random_state=42)
trainY = LabelBinarizer().fit_transform(trainY)
testY = LabelBinarizer().fit_transform(testY)
opt = gradient_descent_v2.SGD(learning_rate=0.005)
model = ShallowNet.build(width=32, height=32, depth=3, classes=3)
model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=['acc'])
H = model.fit(trainX, trainY, validation_data=(testX, testY), batch_size=32, epochs=100, verbose=1)
The simpleloader is a function that just loads the images and the simplepreprocesor just resizes the images and I think the error is inside the shallownet.py that looks like this
import os
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras.layers.core import Activation, Flatten, Dense
from keras import backend as K
class ShallowNet():
#staticmethod
def build(width, height, depth, classes):
model = Sequential()
inputShape = (height, width, depth)
if K.image_data_format() == "channels_first":
inputShape = (depth, height, width)
model.add(Conv2D(32, (3, 3), padding="same", input_shape=inputShape))
model.add(Activation("relu"))
model.add(Flatten())
model.add(Dense(classes))
model.add(Activation("softmax"))
return model
What I deduce is that as my computer doesn't have a GPU I can't perform the training and then I can't fit the model. There is a way to perform this training?

Tensorflow TypeError: Can not convert a NoneType into a Tensor or Operation

I am learning TensorFlow and was going through this step-by-step guide. The below code is the exact same as on the website. However, when running it, I get an error when trying to fit the model. The full traceback I get is as follows:
Traceback (most recent call last):
File "C:\users\name\desktop\python ml tutorial\embedding.py", line 49, in <module>
model.fit(x=padded_docs, y=labels, epochs=50, verbose=0)
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 1213, in fit
self._make_train_function()
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\keras\engine\training.py", line 316, in _make_train_function
loss=self.total_loss)
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py", line 506, in get_updates
return [self.apply_gradients(grads_and_vars)]
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py", line 441, in apply_gradients
kwargs={"name": name})
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py", line 1917, in merge_call
return self._merge_call(merge_fn, args, kwargs)
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\distribute\distribute_lib.py", line 1924, in _merge_call
return merge_fn(self._strategy, *args, **kwargs)
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\optimizer_v2\optimizer_v2.py", line 494, in _distributed_apply
with ops.control_dependencies(update_ops):
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py", line 5257, in control_dependencies
return get_default_graph().control_dependencies(control_inputs)
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\func_graph.py", line 356, in control_dependencies
return super(FuncGraph, self).control_dependencies(filtered_control_inputs)
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py", line 4691, in control_dependencies
c = self.as_graph_element(c)
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3610, in as_graph_element
return self._as_graph_element_locked(obj, allow_tensor, allow_operation)
File "C:\Users\name\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3699, in _as_graph_element_locked
(type(obj).__name__, types_str))
TypeError: Can not convert a NoneType into a Tensor or Operation.
And the full code is below:
from numpy import array
from keras.preprocessing.text import one_hot
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers.embeddings import Embedding
# define document
docs = ['Well done!',
'Good work',
'Great effort',
'nice work',
'Excellent!',
'Weak',
'Poor effort!',
'not good',
'poor work',
'Could have done better.']
# define class labels
labels = array([1,1,1,1,1,0,0,0,0,0])
# integer-encode the documents
vocab_size = 50
encoded_docs = [one_hot(d, vocab_size) for d in docs]
print(encoded_docs)
# padding
max_length = 4
padded_docs = pad_sequences(encoded_docs, maxlen = max_length, padding = 'post')
print(padded_docs)
# define model
model = Sequential()
model.add(Embedding(vocab_size, 8, input_length=max_length))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
# compile the model
model.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics=['accuracy'])
# summarize
print(model.summary())
# fit the model
model.fit(x=padded_docs, y=labels, epochs=50, verbose=0)
# evaluate the model
loss, accuracy = model.evaluate(x=padded_docs, y=labels, verbose=0)
print("Accuracy: {}".format(accuracy))
What's going on here? The article was originally written back in 2017 but its last revision and update was just a week ago. I imagine they are constantly tweaking TensorFlow since it is still state-of-the-art and needs a lot of improvement.
Any ideas on how to circumvent this?
Edit:
I began trying to figure out where the script could have gone wrong. I will be listing what I found here, hopefully it will help us spot something:
I found that in ops.py's control_dependencies() function, control_inputs parameter has the following values: Tensor("Adam/gradients/gradients/loss/dense_1_loss/binary_crossentropy/logistic_loss_grad/Reshape_1:0", shape=(None, 1), dtype=float32), Tensor("Adam/gradients/gradients/loss/dense_1_loss/binary_crossentropy/logistic_loss/Log1p_grad/mul:0", shape=(None, 1), dtype=float32), and None. When it becomes None, the program crashes.

tf.keras.models.clone_model unknown activation: ReLU

The environment is Python 3.7.6, below is my import:
import os, sys
import tensorflow as tf # v2.2.0
tf.compat.v1.enable_eager_execution()
import numpy as np
import matplotlib.pyplot as plt
from sys import platform
import time
import random
import pickle
from tensorflow.keras.layers import ReLU
I tried to clone a tf.keras.Model without success, because ReLU is an unknown activation. Nevertheless, the initiation was successful, so the system should know what ReLU is. I wonder how to fix this.
def init_model(D=8, W=256):
# This is a simple MLP neural network
# D: The number of layers
# H: The neurons in each layer
relu = ReLU()
dense = lambda W=W, act=relu: tf.keras.layers.Dense(W, activation=act, dtype='float32')
inputs = tf.keras.Input(shape=(3 + 3 * 2 * L_embed))
outputs = inputs
for i in range(D):
outputs = dense()(outputs)
if i % 4 == 0 and i > 0:
outputs = tf.concat([outputs, inputs], -1)
outputs = dense(4, act=None)(outputs)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
return model
Then I called:
model_inner_copy = tf.keras.models.clone_model(model)
The error message is:
File "D:/Code Repository/Meta-NeRF/Code/NeRF/Tiny_MAML_NeRF.py", line 211, in train_maml_nerf
model_inner_copy = tf.keras.models.clone_model(model)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 427, in clone_model
model, input_tensors=input_tensors, layer_fn=clone_function)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 196, in _clone_functional_model
model, new_input_layers, layer_fn)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 246, in _clone_layers_and_model_config
config = network.get_network_config(model, serialize_layer_fn=_copy_layer)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\network.py", line 2119, in get_network_config
layer_config = serialize_layer_fn(layer)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 243, in _copy_layer
created_layers[layer.name] = layer_fn(layer)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\models.py", line 61, in _clone_layer
return layer.__class__.from_config(layer.get_config())
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\engine\base_layer.py", line 655, in from_config
return cls(**config)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\layers\core.py", line 1135, in __init__
self.activation = activations.get(activation)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\activations.py", line 465, in get
identifier, printable_module_name='activation')
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 362, in deserialize_keras_object
config, module_objects, custom_objects, printable_module_name)
File "C:\Users\Jack\AppData\Roaming\Python\Python37\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 321, in class_and_config_for_serialized_keras_object
raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown activation: ReLU
So the problem is that tf.keras.layers.ReLU is a layer that implements the ReLU activation, but it is not an activation function by itself. It is meant to be used as a layer inside your model, not as a parameter of your Dense layer.
To have a function that works as an activation to give as a parameter to Dense, you should use tf.keras.activations.relu.

ValueError: Passing a dictionary input to a Sequential Model which doesn't have FeatureLayer as the first layer is an error

I've tried running the following code, but got this error:
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 819, in fit
use_multiprocessing=use_multiprocessing)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py",
line 235, in fit
use_multiprocessing=use_multiprocessing)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py",
line 593, in _process_training_inputs
use_multiprocessing=use_multiprocessing)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py",
line 706, in _process_inputs
use_multiprocessing=use_multiprocessing)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py",
line 702, in init
x = standardize_function(x)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py",
line 660, in standardize_function
standardize(dataset, extract_tensors_from_dataset=False)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 2346, in _standardize_user_data
all_inputs, y_input, dict_inputs = self._build_model_with_inputs(x, y)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 2572, in _build_model_with_inputs
self._set_inputs(cast_inputs)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 2647, in _set_inputs
inputs = self._set_input_attrs(inputs)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\training\tracking\base.py",
line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File
"C:\Users\TomerK\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\python\keras\engine\training.py",
line 2681, in _set_input_attrs
raise ValueError('Passing a dictionary input to a Sequential Model '
ValueError: Passing a dictionary input to a Sequential Model which
doesn't have FeatureLayer as the first layer is an error.
Code:
# -*- coding: utf-8 -*-
import os
#os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
import tensorflow_datasets as tfds
try:
model = keras.models.load_model("passrockmodel.h5")
except:
print('\nDownloading Train Dataset...\n')
train_dataset = tfds.load(name="rock_you", split="train[:75%]")
assert isinstance(train_dataset, tf.data.Dataset)
print('\nDownloading Test Dataset...\n')
test_dataset = tfds.load("rock_you", split='train[-25%:]')
assert isinstance(test_dataset, tf.data.Dataset)
model = tf.keras.Sequential([
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid'),
])
model.compile(
loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(train_dataset, epochs=20)
model.save("passrockmodel.h5")
test_loss, test_accuracy = model.evaluate(test_dataset)
print('\nPredicting...\n')
predictions = model.predict(test_dataset)
print(predictions[0])
I've had your problem yesterday. Here's what solved it for me:
your first layer should be of type tf.keras.layers.DenseFeatures
This first layer must be instantiated with an array of tf.feature_column objects. It happens that all my columns were numeric so my array was:
featureColumns = [tf.feature_column.numeric_column(columnNames[i], normalizer_fn= lambda x: (x - mean[i])/std[i]) for i in range(len(columnNames[:-1]))]
Note: the normalizer_fn arg is very useful as you can see, as well. It can eliminate the need of any additional normalization preprocessing layer should you need it.
And so my layer became:
layers.DenseFeatures(feature_columns=featureColumns, trainable=True)
I believe this should solve the error mentioned above in your question. Which is quoted as
ValueError: Passing a dictionary input to a Sequential Model which
doesn't have FeatureLayer as the first layer is an error.

Categories

Resources