AttributeError: 'Sequential' object has no attribute 'model' - python

from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.optimizers import Adam
def build_dqn(lr, n_actions, input_dims, fc1_dims, fc2_dims):
model = Sequential([
Dense(fc1_dims, input_shape=(input_dims,)),
Activation('relu'),
Dense(fc2_dims),
Activation('relu'),
Dense(n_actions)])
model.compile(optimizer=Adam(lr=lr), loss='mse')
return model
I am trying to understand Double Deep Q-Learning. There is a pretty good lecture here: https://github.com/philtabor/Youtube-Code-Repository/tree/master/ReinforcementLearning/DeepQLearning
But when I tried to run the code, I got following errors:
Traceback (most recent call last):
File "/home/panda/PycharmProjects/ddqn/main.py", line 33, in <module>
ddqn_agent.learn()
File "/home/panda/PycharmProjects/ddqn/ddqn_keras.py", line 118, in learn
self.update_network_parameters()
File "/home/panda/PycharmProjects/ddqn/ddqn_keras.py", line 121, in update_network_parameters
self.q_target.model.set_weights(self.q_eval.model.get_weights())
AttributeError: 'Sequential' object has no attribute 'model'
And I have no clue on how to fix this. I guess keras has been updated to not allow this?
The different lines are respectively:
line 33:
ddqn_agent.learn()
line 118 (in def learn(self):):
self.update_network_parameters()
line 121 (in def update_network_parameters(self):):
self.q_target.model.set_weights(self.q_eval.model.get_weights())
line 76:
self.q_target = build_dqn(alpha, n_actions, input_dims, 256, 256)
EDIT: updated the problem based on suggestions in the comment section. The suggestion was that I put a tensforflow. in front of keras in the imports. I get the same error as before (as you can see). Here is how the imports look like now:

For solving your error you can go through the below steps:
1. Install dependency for can run the env:
!pip install https://github.com/pybox2d/pybox2d/archive/refs/tags/2.3.10.tar.gz
!pip install box2d-py
!pip install gym[all]
!pip install gym[box2d]
2. Change imports like below:
from keras.layers import Dense, Activation
from keras import Sequential
from keras.models import load_model
from tensorflow.keras.optimizers import Adam
3. Install tf-nightly: (what is tf-nightly)
!pip install tf-nightly

Related

NameError: name 'scipy' is not defined when trying to create a model

I'm currently trying to create a model using transfer learning, but I'm getting an error
NameError: name 'scipy' is not defined
I'm going to learn from the video. We have loaded some datasets to the computer and I am trying to convert these datasets into '.json' and '.h5' files. I had to run the code you saw in the first part and create the model. There was supposed to be a download like in the video, but instead I got an error and I can't solve it.
Here are my codes:
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Dense
from keras.applications.vgg16 import VGG16
import matplotlib.pyplot as plt
from glob import glob
from keras.utils import img_to_array
from keras.utils import load_img
train_path = "/Users/atakansever/Desktop/CNNN/fruits-360_dataset/fruits-360/Training/"
test_path = "/Users/atakansever/Desktop/CNNN/fruits-360_dataset/fruits-360/Test/"
# img = load_img(train_path + "Tangelo/0_100.jpg")
# plt.imshow(img)
# plt.axes("off")
# plt.show()
numberOfClass = len(glob(train_path + "/*"))
# print(numberOfClass)
vgg = VGG16()
# print(vgg.summary())
vgg_layer_list = vgg.layers
# print(vgg_layer_list)
model = Sequential()
for i in range(len(vgg_layer_list)-1):
model.add(vgg_layer_list[i])
# print(model.summary())
for layers in model.layers:
layers.trainable = False
model.add(Dense(numberOfClass, activation="softmax"))
# print(model.summary())
model.compile(loss = "categorical_crossentropy",optimizer = "rmsprop",metrics = ["accuracy"])
#train
train_data = ImageDataGenerator().flow_from_directory(train_path, target_size=(224,224))
test_data = ImageDataGenerator().flow_from_directory(test_path, target_size=(224,224))
batch_size = 32
hist = model.fit_generator(train_data,
steps_per_epoch=1600//batch_size,
epochs=25,
validation_data= test_data,
validation_steps=800//batch_size)
and here is the error
pyenv shell 3.9.7
atakansever#atakan-Air CNNN % pyenv shell 3.9.7
pyenv: shell integration not enabled. Run `pyenv init' for instructions.
atakansever#atakan-Air CNNN % /Users/atakansever/.pyenv/versions/3.9.7/bin/python /Users/atakansever/Desktop/CNNN/fruits.py
Metal device set to: Apple M1
systemMemory: 8.00 GB
maxCacheSize: 2.67 GB
2022-07-10 11:17:50.428036: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2022-07-10 11:17:50.428259: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
Found 67692 images belonging to 131 classes.
Found 22688 images belonging to 131 classes.
/Users/atakansever/Desktop/CNNN/fruits.py:53: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
hist = model.fit_generator(train_data, steps_per_epoch=1600//batch_size,epochs=25,validation_data= test_data,validation_steps=800//batch_size)
Traceback (most recent call last):
File "/Users/atakansever/Desktop/CNNN/fruits.py", line 53, in <module>
hist = model.fit_generator(train_data, steps_per_epoch=1600//batch_size,epochs=25,validation_data= test_data,validation_steps=800//batch_size)
File "/Users/atakansever/.pyenv/versions/3.9.7/lib/python3.9/site-packages/keras/engine/training.py", line 2260, in fit_generator
return self.fit(
File "/Users/atakansever/.pyenv/versions/3.9.7/lib/python3.9/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/Users/atakansever/.pyenv/versions/3.9.7/lib/python3.9/site-packages/keras/preprocessing/image.py", line 2244, in apply_affine_transform
if scipy is None:
NameError: name 'scipy' is not defined
try pip install scipy or pip3 install scipy would solve the problem
First, install the scipy package if it isn't already installed:
pip install scipy
and then add scipy to your imports:
import scipy # This is new!
from keras.preprocessing.image import ImageDataGenerator
# ... all your imports
I clicked on the error message and it directed you to the source code.
Comment that two line and save the python script.
# if scipy is None:
# raise ImportError('Image transformations require SciPy. '
# 'Install SciPy.')
Commect code image
Then it will work perfectly.
You have to:
Install scipy pip install scipy
Restart VS code to your IDE or perhaps restart Python Kernel and rerun the code.

Pyimagesearch module not found

I am working on an object detection tutorial using keras, tensorlow on pyimagesearch, i was not able to subscribe on the pyimagesearch site therefore i couldn't get the pyimagesearch folder. Please is there a way i could run the code without using the folder?.
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-2-8215065cc4e1> in <module>()
----> 1 from pyimagesearch import config
2 from tensorflow.keras.applications import VGG16
3 from tensorflow.keras.layers import Flatten, Dense, Input
4 from tensorflow.keras.models import Model
5 from tensorflow.keras.optimizers import Adam
ModuleNotFoundError: No module named 'pyimagesearch'
because, its implemented by user defined class and import from that pyimagesearch Gsearch for adrianrosebrock

Error 404 not found In postman when trying to call predict method for tensorflow-hub

I was testing a tensorflow model on Postman that uses https://tfhub.dev/google/universal-sentence-encoder-multilingual/3 from tensorflow-hub, knowing that it worked perfectly in jupyter notebook without any error, I encountered this error in postman after sending a POST request that calls predict method.
Error:
"error": "{{function_node __inference_signature_wrapper_133703}} {{function_node __inference_signature_wrapper_133703}} {{function_node __inference__wrapped_model_95698}} {{function_node __inference__wrapped_model_95698}} {{function_node __inference_restored_function_body_51031}} {{function_node __inference_restored_function_body_51031}} [_Derived_]{{function_node __inference___call___6286}} {{function_node __inference___call___6286}} Op type not registered \'SentencepieceOp\' in binary running on 329ddc874964. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.\n\t [[{{node StatefulPartitionedCall}}]]\n\t [[StatefulPartitionedCall]]\n\t [[sequential/keras_layer/StatefulPartitionedCall]]\n\t [[StatefulPartitionedCall]]\n\t [[StatefulPartitionedCall]]"
with a status 404 not found.
and this is my model:
import tensorflow as tf
import numpy as np
import tensorflow_text
import pandas as pd
import random
import tensorflow_hub as hub
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import losses
from tensorflow.keras import preprocessing
from tensorflow.keras.layers.experimental.preprocessing import TextVectorization
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
from tensorflow.keras.layers import SpatialDropout1D
from tensorflow.keras.layers import Embedding
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
module_url = "https://tfhub.dev/google/universal-sentence-encoder-multilingual/3"
# Import the Universal Sentence Encoder's TF Hub module
hub_layer = hub.KerasLayer(module_url, input_shape=[], dtype=tf.string, trainable=True)
#some data preprocessing
opt = keras.optimizers.Adam(learning_rate= 0.001)
model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(32, activation='relu'))
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1,activation='sigmoid'))
model.layers[0].trainable = False
model.compile(loss='binary_crossentropy',optimizer=opt, metrics=['accuracy'])
model.summary()
history = model.fit(np.array(tweet),np.array(sentiment),
validation_split=0.2, epochs=5, batch_size=32)
This is the request in Postman using localhost:.../arabtextclasstfhubtest:predict :
{
"signature_name": "serving_default",
"inputs":
{
"keras_layer_input":["كلامك جميل ورائع"]
}
}
I would like to know if it's a bug in tensoflow-hub or how to fix this problem.
thank you!!
It appears you are exporting a SavedModel to a server binary that answers the Postman requests. That server binary needs to link in the 'SentencepieceOp' from tensorflow_text, because your SavedModel uses it (as it should).

Unable to import a keras application

I am trying to use a keras application in pycharm. I start my script off with the following imports:
from keras_vggface.vggface import VGGFace
from keras_vggface.utils import preprocess_input
from keras_vggface.utils import decode_predictions
Upon running this block of code, I get this error:
ImportError: You need to first `import keras` in order to use `keras_applications`. For instance, you can do:
```
import keras
from keras_applications import vgg16
```
Or, preferably, this equivalent formulation:
```
from keras import applications
```
I have tried importing the appropriate keras libraries as suggested, but the problem persists. I have also tried checking the json file to see if it contains the correct backend(it does).
How can I resolve this issue?
"edit for clarity"
My full imports go as follows:
from PIL import Image # for extracting image
from numpy import asarray
from numpy import expand_dims
from matplotlib import pyplot
from mtcnn.mtcnn import MTCNN # because i am too lazy to make one myself
import keras
from keras_applications import vgg16
from keras_vggface.vggface import VGGFace
from keras_vggface.utils import preprocess_input
from keras_vggface.utils import decode_predictions
Traceback:
Traceback (most recent call last):
File "C:/Users/###/PycharmProjects/##/#.py", line 17, in <module>
from keras_applications import vgg16
File "C:\Users\###\anaconda3\envs\tensor\lib\site-packages\keras_applications\vgg16.py", line 17, in <module>
backend = get_keras_submodule('backend')
File "C:\Users\###\anaconda3\envs\tensor\lib\site-packages\keras_applications\__init__.py", line 39, in get_keras_submodule
raise ImportError('You need to first `import keras` '
ImportError: You need to first `import keras` in order to use `keras_applications`. For instance, you can do:
```
import keras
from keras_applications import vgg16
```
Or, preferably, this equivalent formulation:
```
from keras import applications
```
Process finished with exit code 1
Are you planning to use the Tensorflow framework for executing the model. If it is tensorflow then i suggest using
import tensorflow as tf \ from tensorflow.keras.applications.vgg16 import VGG. Keras comes in-built in latest TF framework and hence we dont have to do an explicit import
even otherwise if you want to use Keras directly i believe the code should be :
import keras \ from keras.applications.vgg16 import VGG16 \ vggmodel = VGG16(weights='imagenet', include_top=True)

FailedPreconditionError Tensorflow Train

I am testing a code that colors images in Google Colab.
from keras.models import Model, Sequential, load_model
from keras.layers.merge import concatenate
from keras.layers.pooling import MaxPooling2D
from keras.preprocessing.image import ImageDataGenerator
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers import Input, UpSampling2D, RepeatVector, Reshape
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input
from keras import backend as K
import tensorflow as tf
print(tf.__version__)
Using TensorFlow backend.
1.14.0
I am using tensorflow 1.14.0 because the original code is from Kaggle, the original: https://www.kaggle.com/valkling/image-colorization-using-autoencoders-and-resnet/notebook
There are no problems in any cell, until training. When excute this code is run its fail.
%%time
BATCH_SIZE = 20
model.fit_generator(
image_a_b_gen(X_train, BATCH_SIZE),
epochs=30,
verbose=1,
steps_per_epoch=X_train.shape[0]/BATCH_SIZE,
callbacks=model_callbacks)
The error is:
Epoch 1/30
---------------------------------------------------------------------------
FailedPreconditionError Traceback (most recent call last)
<ipython-input-14-61e8a51cf536> in <module>()
----> 1 get_ipython().run_cell_magic('time', '', 'BATCH_SIZE = 20\nmodel.fit_generator(\n image_a_b_gen(X_train, BATCH_SIZE),\n epochs=30,\n verbose=1,\n steps_per_epoch=X_train.shape[0]/BATCH_SIZE,\n callbacks=model_callbacks)')
<decorator-gen-60> in time(self, line, cell, local_ns)
<timed exec> in <module>()
<timed exec> in image_a_b_gen(dataset, batch_size)
<timed exec> in create_inception_embedding(grayscaled_rgb)
1456 ret = tf_session.TF_SessionRunCallable(self._session._session,
1457 self._handle, args,
-> 1458 run_metadata_ptr)
1459 if run_metadata:
1460 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
FailedPreconditionError: Error while reading resource variable conv2d_45/kernel from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/conv2d_45/kernel)
[[{{node conv2d_45/convolution/ReadVariableOp}}]]
I think it may be related to the version of tensorflow or sessions, but I can't find any solution.
Thanks
How I thought the solution was very simple and related to version problems.
The version of keras and tensorflow in kaggle is 2.2.4 and 1.14.0 respectively. The solution is simple to change the version of Google Colab with these instructions:
!pip install tensorflow==1.14.0
!pip install keras==2.2.4
Then restart de Notebook and its done.

Categories

Resources