Too many failed attempts to build model - python

I am currently using kerastuner ( 1.0.0) to fine-tune hyperparamteters in a CNN and it keeps displaying the error 'Too many failed attempts to build model.'. I have tried all other availabel solutions including those on this stackoverflow but none of them works. Does my model have too many parameters to tune?
from tensorflow.keras.datasets import fashion_mnist
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.layers import Dense, Conv2D, Dropout, MaxPooling2D
from tensorflow.keras.initializers import LecunNormal
!pip install keras-tuner==1.0.0
from kerastuner.tuners import RandomSearch
from kerastuner import HyperModel
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)
class MCDropout(tf.keras.layers.Dropout):
def call(self, inputs):
return super().call(inputs, training=True)
NUM_CLASSES = 10
KERNEL_SIZE = (3, 3)
class CNNHyperModel(HyperModel):
def __init__(self, num_classes, kernel_size):
self.num_classes = num_classes
self.kernel_size = kernel_size
def build_model(self, hp):
model = keras.Sequential()
model.add(Conv2D(ilters=hp.Choice('input_units', values=[32, 16], default=16),
kernel_size=(self.kernel_size),
activation='selu',
kernel_intializer = LecunNormal,
input_shape=x_train.shape[1:]))
model.add(MCDropout(rate=hp.Float('dropout_0',min_value=0.0,max_value=0.2,default=0.1,step=0.1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
for i in range(hp.Choice('n_filters', 1, 4)):
model.add(Conv2D(hp.Choice(f'num_filters_{i}', values=[32, 16, 64], default=32),
kernel_size=(self.kernel_size),
activation='selu',
kernel_intializer = LecunNormal))
model.add(MCDropout(hp.Float(f'dropout_{i}',min_value=0.0,max_value=0.2,default=0.1,step=0.1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(units=hp.Choice('units',values = [128, 256, 512, 1024], defaults = 512),activation='selu'))
model.add(MCDropout(rate=hp.Float('dropout_5',min_value=0.3,max_value=0.7,default=0.4,step=0.1)))
model.add(Dense(self.num_classes, activation='softmax'))
model.compile(optimizer=keras.optimizers.Nadam(hp.Choice('learning_rate',
values=[1e-2, 1e-3, 1e-4])),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
hypermodel = CNNHyperModel(num_classes=NUM_CLASSES, kernel_size = KERNEL_SIZE)
SEED = 1
tuner = RandomSearch(
hypermodel,
objective='val_accuracy',
max_trials = 1,
executions_per_trial = 1
)
tuner.search(x=x_train,
y=y_train,
epochs=1,
batch_size=64,
validation_data =(x_test, y_test))```
I RUN THIS CODE AND IT KEEP DISPLAYING TOO MANY FAILED ATTEMPTS TO BUILD MODELS. (BTW I AM WRITING THIS ON GG COLAB.)
IS THERE ANYWAY TO FIX IT?

Related

Why I am getting error while using ResNet50 for transfer learning?

So I am trying to use ResNet50 for transfer learning with my keras model(i.e Trinity), but the problem is I am getting an error when I try to execute Trinity.fit().
Exact error "ValueError: Input 0 of layer sequential_8 is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 2048]"
What is the reason for this error? How can I solve it?
Here is the python code:
1) from keras.models import Sequential
from keras.layers import Conv2D,MaxPooling2D
from keras.layers import Activation, Dense, Flatten, Dropout
from keras.preprocessing.image import ImageDataGenerator
import os
2) import keras
import wandb
from wandb.keras import WandbCallback
from keras.models import Sequential
from keras.layers import Dense, Flatten
from keras.applications.resnet50 import ResNet50, decode_predictions,preprocess_input
import matplotlib.pyplot as plt
3) training_dir = '../input/fruits/fruits-360/Training/'
validation_dir = '../input/fruits/fruits-360/Test/'
test_dir = '../input/fruits/fruits-360/test-multiple_fruits/'
4) image_size = 224
data_generator = ImageDataGenerator(preprocessing_function=preprocess_input)
train_generator = data_generator.flow_from_directory(
'../input/fruits/fruits-360/Training/',
target_size=(image_size, image_size),
batch_size=24,
class_mode='categorical')
validation_generator = data_generator.flow_from_directory(
'../input/fruits/fruits-360/Test/',
target_size=(image_size, image_size),
batch_size=24,
class_mode='categorical')
#OUTPUT
#[Found 67692 images belonging to 131 classes.
#Found 22688 images belonging to 131 classes.]
5) resnet_model = ResNet50(weights="imagenet")
6) x_train_preprocessed = train_generator
x_test_preprocessed = validation_generator
7) last_layer = resnet_model.get_layer("avg_pool")
resnet_layers = keras.Model(inputs=resnet_model.inputs, outputs=last_layer.output)
resnet_layers.summary()
8) x_train_features = resnet_layers.predict(x_train_preprocessed)
x_test_features = resnet_layers.predict(x_test_preprocessed)
9) Trinity = Sequential()
Trinity.add(Conv2D(filters = 16, kernel_size = 2,input_shape=(224,224,3),padding='same'))
Trinity.add(Activation('relu'))
Trinity.add(MaxPooling2D(pool_size=2))
Trinity.add(Conv2D(filters = 32,kernel_size = 2,activation= 'relu',padding='same'))
Trinity.add(MaxPooling2D(pool_size=2))
Trinity.add(Conv2D(filters = 64,kernel_size = 2,activation= 'relu',padding='same'))
Trinity.add(MaxPooling2D(pool_size=2))
Trinity.add(Conv2D(filters = 128,kernel_size = 2,activation= 'relu',padding='same'))
Trinity.add(MaxPooling2D(pool_size=2))
Trinity.add(Dropout(0.3))
Trinity.add(Flatten())
Trinity.add(Dense(132))
Trinity.add(Activation('relu'))
Trinity.add(Dropout(0.4))
Trinity.add(Dense(131,activation = 'softmax'))
Trinity.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
10) Trinity.fit(x_train_features,epochs=50,validation_data=x_test_features)
#OUTPUT OF 7)
#OUTPUT OF 10)
you should add include_top=False to the instance of resnet, then add your model to the top of resnet. that way it should work. Here is an example:
base_model = ResNet50V2(include_top=False, weights="imagenet", input_shape=(224,224,3), pooling="avg")
base_model.summary()
model2 = Sequential()
model2.add(base_model)
model2.add(Dense(64, activation="relu"))
model2.add(Dropout(0.2))
model2.add(Dense(64, activation="relu"))
model2.add(Dropout(0.2))
model2.add(Dense(32, activation="relu"))
model2.add(Dropout(0.2))
model2.add(Dense(32, activation="relu"))
model2.add(Dropout(0.2))
model2.add(Dense(1, activation="sigmoid"))
base_model.trainable = False
model2.summary()
model2.compile(optimizer=Adam(), loss="binary_crossentropy", metrics=["accuracy"])
and here is another example from keras website: https://keras.io/examples/vision/image_classification_efficientnet_fine_tuning/

Kerastuner ValueError: Shapes (320,) and (1,) are incompatible

I'm new to deep learning and tried to tune hyperparameters using kerastuner. Here is my code:
from tensorflow import keras
from tensorflow.keras import layers
from kerastuner.tuners import RandomSearch
from kerastuner import HyperModel
class MyHyperModel(HyperModel):
def __init__(self, input_shape):
self.input_shape = input_shape
def build(self, hp):
model = keras.Sequential()
model.add(layers.Dense(units=hp.Int('units',
min_value=10,
max_value=120,
step=10),
activation='relu',
input_shape=self.input_shape))
model.add(layers.Dense(units=hp.Int('units',min_value=10,max_value=20,step=2),
activation='relu'))
model.add(layers.Dense(1, activation='linear'))
model.compile(
optimizer=keras.optimizers.Adam(
hp.Choice('learning_rate',
values=[1e-2, 1e-3, 1e-4])),
loss='mse',
metrics=['mse'])
return model
input_shape = (X_train.shape[1],)
hypermodel = MyHyperModel(input_shape)
tuner = RandomSearch(
hypermodel,
objective='mse',
max_trials=10,
)
tuner.search(X_train, y_train,
epochs=50,
validation_data=(X_test, y_test),verbose=0)
When I tried to retrieve the best model using
tuner.get_best_models(num_models=1)[0]
I got the following error
ValueError: Shapes (320,) and (1,) are incompatible
I don't have this error when I deleted this hidden layer
model.add(layers.Dense(units=hp.Int('units',min_value=10,max_value=20,step=2),
activation='relu'))
Anyone knows how to fix this? Thanks in advance.

'Sequential' object has no attribute '_in_multi_worker_mode'

I tried to use google colab resources to save my CNN model weights and I get this error. I tried googling it but nothing helps.
'Sequential' object has no attribute '_in_multi_worker_mode'
My code:
checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, save_weights_only=True, verbose=1)
cnn_model = Sequential()
cnn_model.add(Conv2D(filters = 64, kernel_size = (3,3), activation = "relu", input_shape = Input_shape ))
cnn_model.add(Conv2D(filters = 64, kernel_size = (3,3), activation = "relu"))
cnn_model.add(MaxPooling2D(2,2))
cnn_model.add(Dropout(0.4))
cnn_model = Sequential()
cnn_model.add(Conv2D(filters = 128, kernel_size = (3,3), activation = "relu"))
cnn_model.add(Conv2D(filters = 128, kernel_size = (3,3), activation = "relu"))
cnn_model.add(MaxPooling2D(2,2))
cnn_model.add(Dropout(0.3))
cnn_model.add(Flatten())
cnn_model.add(Dense(units = 512, activation = "relu"))
cnn_model.add(Dense(units = 512, activation = "relu"))
cnn_model.add(Dense(units = 10, activation = "softmax"))
history = cnn_model.fit(X_train, y_train, batch_size = 32,epochs = 1,
shuffle = True, callbacks = [cp_callback])
Stack trace:
AttributeError Traceback (most recent call last)
<ipython-input-19-35c1db9636b7> in <module>()
----> 1 history = cnn_model.fit(X_train, y_train, batch_size = 32,epochs = 1, shuffle = True, callbacks = [cp_callback])
4 frames
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/callbacks.py in on_train_begin(self, logs)
903 def on_train_begin(self, logs=None):
904 # pylint: disable=protected-access
--> 905 if self.model._in_multi_worker_mode():
906 # MultiWorkerTrainingState is used to manage the training state needed
907 # for preemption-recovery of a worker in multi-worker training.
AttributeError: 'Sequential' object has no attribute '_in_multi_worker_mode'
I've recently faced the same issue
instead of,
from tensorflow.keras.callbacks import ModelCheckpoint
use,
from keras.callbacks import ModelCheckpoint
Check your tensorflow version. You actually only need to synchronize it. check if all your import uses
from keras import ...
or
from tensorflow.keras import ...
use only one of the above for your keras imports. using different (both) at the same time can cause collision by the libraries.
Instead of
tf.keras.callbacks.ModelCheckpoint
in your model building process, you can use
from keras.callbacks import ModelCheckpoint
in order to import ModelCheckpoint, and then just use ModelCheckpoint in the later code.
Please check if your version of tensorflow matches the latest one.In my case the error was solved when is updated it to 2.1.0.

Cant Use utils_keras.Sequential still thinks its not Cleverhans model

I'm trying to do Saliency Map Method using cleverhans.
My model needs to be keras sequential so for that reason I've searched and found cleverhans.utils_keras, Sequential uses KerasModelWrapper. But for some reason I still get it should be cleverhans model. Here's the stacktrace
TypeError Traceback (most recent call last)
in
2 # https://github.com/tensorflow/cleverhans/blob/master/cleverhans/utils_keras.py
3
----> 4 jsma = SaliencyMapMethod(model, sess=sess)
5 jsma_params = {'theta': 10.0, 'gamma': 0.15,
6 'clip_min': 0., 'clip_max': 1.,
c:\users\jeredriq\appdata\local\programs\python\python35\lib\site-packages\cleverhans\attacks__init__.py in init(self, model, sess, dtypestr, **kwargs)
911 """
912
--> 913 super(SaliencyMapMethod, self).init(model, sess, dtypestr, **kwargs)
914
915 self.feedable_kwargs = ('y_target',)
c:\users\jeredriq\appdata\local\programs\python\python35\lib\site-packages\cleverhans\attacks__init__.py in init(self, model, sess, dtypestr, **kwargs)
55
56 if not isinstance(model, Model):
---> 57 raise TypeError("The model argument should be an instance of"
58 " the cleverhans.model.Model class.")
59
TypeError: The model argument should be an instance of the cleverhans.model.Model class.
And here's my code
import numpy as np
from keras import backend
import tensorflow as tf
from keras.callbacks import ModelCheckpoint
from matplotlib import gridspec
from matplotlib import pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report
from keras.datasets import mnist
from keras.layers import Dense, Dropout
from keras.layers import Flatten
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
from cleverhans.attacks import FastGradientMethod
from cleverhans.attacks import BasicIterativeMethod
from cleverhans.attacks import SaliencyMapMethod
from cleverhans.attacks import DeepFool
from cleverhans.utils_keras import Sequential
sess = backend.get_session()
x = tf.placeholder(tf.float32, shape=(None, 28, 28, 1))
y = tf.placeholder(tf.float32, shape=(None, 10))
# Managing Mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train/=255
X_test/=255
y_train_cat = np_utils.to_categorical(y_train)
y_test_cat = np_utils.to_categorical(y_test)
num_classes = y_test_cat.shape[1]
### Defining Model ###
model = Sequential() # <----- I use Sequential from CleverHans
model.add(Conv2D(32, (5, 5), input_shape=(28,28,1), activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
history = model.fit(X_train, y_train_cat, epochs=10, batch_size=1024, verbose=1, validation_split=0.7)
### And the problem part ###
jsma = SaliencyMapMethod(model, sess=sess) # <---- Where I get the exception
jsma_params = {'theta': 10.0, 'gamma': 0.15,
'clip_min': 0., 'clip_max': 1.,
'y_target': None}
sample_size = 20
one_hot_target = np.zeros((sample_size, 10), dtype=np.float32)
one_hot_target[:, 1] = 1
jsma_params['y_target'] = one_hot_target
X_test_small = X_test[0:sample_size,:]
y_test_small = y_test[0:sample_size]
adv_x = jsma.generate_np(X_test_small, **jsma_params)
I've the same question on github too.
The Sequential defined in cleverhans.utils_keras is still keras' Sequential model. What is needed is cleverhans.model.Model. A keras model can be wrapped to provide this behaviour by using the KerasModelWrapper class.
Replace
jsma = SaliencyMapMethod(model, sess=sess)
with
jsma = SaliencyMapMethod(KerasModelWrapper(model), sess=sess)

Session keyword arguments are not support during eager execution. You passed: {'learning_rate': 1e-05}

I want to conduct a hyperparameter tuning for the learning rate. However, I got the error that I do not know how to solve.
I used the Tensorflow.Keras package.
import tensorflow as tf
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.datasets.mnist import load_data
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import (Flatten, BatchNormalization, Dropout, Dense)
from keras.wrappers.scikit_learn import KerasClassifier
(x_train_all, y_train_all), (x_test, y_test) = load_data()
x_train, x_valid, x_test = x_train_all[5000:]/255.0, x_train_all[:5000]/255.0, x_test/255.0
y_train, y_valid = y_train_all[5000:], y_train_all[:5000]
tf.cast(x_train, tf.float32)
tf.cast(x_valid, tf.float32)
tf.cast(x_test, tf.float32)
def my_model(learning_rate = 5e-3):
model = Sequential([
Flatten(input_shape = (28, 28)),
BatchNormalization(),
Dropout(rate = 0.2),
Dense(300, activation = "elu", kernel_initializer = "he_normal"),
Dropout(rate = 0.2),
BatchNormalization(),
Dense(300, activation = "elu", kernel_initializer = "he_normal"),
Dropout(rate = 0.2),
BatchNormalization(),
Dense(10, activation = "softmax",kernel_initializer = "he_normal")])
opt = Adam(lr = learning_rate)
model.summary()
model.compile(loss = "sparse_categorical_crossentropy", optimizer = opt, learning_rate = learning_rate, metrics = ["accuracy"])
return model
from sklearn.model_selection import RandomizedSearchCV
keras_classifier = KerasClassifier(my_model)
param_distribs = {"learning_rate": [1e-5, 5e-5, 1e-4, 5e-4, 1e-3, 5e-3]}
rnd_search_cv = RandomizedSearchCV(keras_classifier, param_distribs, n_iter = 10, cv = 3)
rnd_search_cv.fit(x_train, y_train, epochs = 10, validation_data = (x_valid, y_valid))
I got the value error as the following:
ValueError: Session keyword arguments are not support during eager
execution. You passed: {'learning_rate': 1e-05}
Mentioning the Solution in this Section (even though it is present in Comment's section), for the benefit of the community.
The issue is resolved by removing learning_rate = learning_rate in model.compile.
Correct Code is mentioned below:
def my_model(learning_rate = 5e-3):
model = Sequential([
Flatten(input_shape = (28, 28)),
BatchNormalization(),
Dropout(rate = 0.2),
Dense(300, activation = "elu", kernel_initializer = "he_normal"),
Dropout(rate = 0.2),
BatchNormalization(),
Dense(300, activation = "elu", kernel_initializer = "he_normal"),
Dropout(rate = 0.2),
BatchNormalization(),
Dense(10, activation = "softmax",kernel_initializer = "he_normal")])
opt = Adam(lr = learning_rate)
model.summary()
model.compile(loss = "sparse_categorical_crossentropy", optimizer = opt, metrics = ["accuracy"])
return model

Categories