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.
Related
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
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
if my code like this:
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense, Conv2D, MaxPooling2D, BatchNormalization
from keras.callbacks import TensorBoard
from keras.callbacks import ModelCheckpoint
from keras.optimizers import Adam
from keras.optimizers import SGD
then it report me the error is:
ImportError Traceback (most recent call last)
<ipython-input-13-65309f3d78a9> in <module>()
6 from keras.callbacks import TensorBoard
7 from keras.callbacks import ModelCheckpoint
----> 8 from keras.optimizers import Adam
9 from keras.optimizers import SGD
10
ImportError: cannot import name 'Adam'
if my code like that:
from tensorflow.keras.optimizers import Adam
model.compile(loss='sparse_categorical_crossentropy',optimizer=Adam(),metrics=['acc'])
it reports the error:
~/anaconda3/lib/python3.6/site-packages/keras/optimizers.py in get(identifier)
else:
raise ValueError('Could not interpret optimizer identifier: {}'.format(identifier))
ValueError: Could not interpret optimizer identifier: <tensorflow.python.keras.optimizer_v2.adam.Adam object at 0x7fa9bd68c048>
The import statement looks fine:
from tensorflow.keras.optimizers import Adam
The compile method has three parameters (loss, optimizer and metrics). Optimizer can be a string . For example:
model.compile(loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['acc'])
You can also specify Adam as a variable and use that variable as your optimizer:
example = Adam(learning_rate=0.1)
model.compile(loss='sparse_categorical_crossentropy',optimizer=example,metrics=['acc'])
The default values for Adam are here.
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).
I am trying to use the same code in this page but I am getting an error at the middle of the code.
AttributeError Traceback (most recent call last)
<ipython-input-34-9ff70788070d> in <module>()
----> 1 model = Sequential()
2 model.add(LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
3 model.add(LSTM(units=50, return_sequences=False))
4 model.add(Dense(units=25))
5 model.add(Dense(units=1))
1 frames
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in get_uid(prefix)
66 """
67 global _GRAPH_UID_DICTS
---> 68 graph = tf.get_default_graph()
69 if graph not in _GRAPH_UID_DICTS:
70 _GRAPH_UID_DICTS[graph] = defaultdict(int)
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
Here is my import list:
#Import the libraries
from tensorflow.keras import backend as K
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import LSTM, Dense, RepeatVector, Masking, TimeDistributed
from tensorflow. keras.utils import plot_model
import quandl
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.svm import SVR
import pandas_datareader as web
from sklearn.model_selection import train_test_split
import math
from keras.models import Sequential
from keras.layers import Dense, LSTM
from sklearn.preprocessing import MinMaxScaler
#import tensorflow as tf
#newinv=inventory+str(add)
from tensorflow.keras.layers import Embedding
from matplotlib import pyplot as plt
python tensorflow machine-learning keras
Update: After editing the code based on Giorgos' answer, no I get this is the error:
NameError Traceback (most recent call last)
<ipython-input-20-9ff70788070d> in <module>()
----> 1 model = Sequential()
2 model.add(LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
3 model.add(LSTM(units=50, return_sequences=False))
4 model.add(Dense(units=25))
5 model.add(Dense(units=1))
NameError: name 'Sequential' is not defined
Here is my import list:
import math
import pandas_datareader as web
import numpy as np
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import quandl
import tensorflow as tf
model = tf.keras.Sequential()
from keras.layers import Dense, LSTM
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
Here is where I am getting the error:
#Build the LSTM network model
model = Sequential()
model.add(LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=50, return_sequences=False))
model.add(Dense(units=25))
model.add(Dense(units=1))
If you are using tf.keras, instead of
from keras.models import Sequential
from keras.layers import Dense, LSTM
use the following:
import tensorflow as tf
model = tf.keras.Sequential()
model.add(tf.keras.layers.LSTM(units=50, return_sequences=True,input_shape=(x_train.shape[1],1)))
model.add(tf.keras.layers.LSTM(units=50, return_sequences=False))
model.add(tf.keras.layers.Dense(units=25))
model.add(tf.keras.layers.Dense(units=1))
And also make sure to remove your older import from keras.models import Sequential so that Sequential() is not overwritten in namespace. Same applies for from keras.layers import Dense, LSTM.
There might be some incompatibilities between your Keras and TensorFlow. The thing that did the trick for me was uninstalling the Keras using sudo pip uninstall keras and re-installing it.