AttributeError: 'OwnedIterator' object has no attribute '_get_trainable_state' - python

I want to use quantization aware training(QAT) to quantize EfficientNet and run
on Google Colaboratory.
I use its GPU(Tesla K80).
So, I change keras to tensorflow keras due to fit QAT API.
But, there is a problem when I training the model(before using QAT).
# model compilation
model_final.compile(loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.Adam(0.0001),
metrics=['accuracy', acc_top5])
mcp_save = tf.keras.callbacks.ModelCheckpoint('EnetB7_CIFAR10_TL.h5', save_best_only=True, monitor='val_acc')
reduce_lr = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_acc', factor=0.5, patience=2, verbose=1,)
model_final.fit(x_train, y_train,
batch_size=32,
epochs=10,
validation_split=0.1,
callbacks=[mcp_save, reduce_lr],
shuffle=True,
verbose=1)
The error is AttributeError: 'OwnedIterator' object has no attribute '_get_trainable_state'.
I don't know why I could run the code on colab two times without any change.
full traceback
Epoch 1/10
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-11-199bf2f08514> in <module>()
14 callbacks=[mcp_save, reduce_lr],
15 shuffle=True,
---> 16 verbose=1)
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
966 options=autograph.ConversionOptions(
967 recursive=True,
--> 968 optional_features=autograph_options,
969 user_requested=True,
970 ))
AttributeError: in user code:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:576 _reset_compile_cache *
self._compiled_trainable_state = self._get_trainable_state()
AttributeError: 'OwnedIterator' object has no attribute '_get_trainable_state'

Related

TypeError: tuple indices must be integers or slices, not str, I am getting the following error

I am getting this error when I fit my model. This is a model to generate captions from images, I don't the error because I am new to keras. the code below with generated error.
reduce_lr = keras.callbacks.ReduceLROnPlateau(
monitor="val_loss", factor=0.2, patience=3
)
# Create an early stopping callback.
early_stop = tf.keras.callbacks.EarlyStopping(
monitor="val_loss", patience=5, restore_best_weights=True
)
history = dual_encoder.fit(
train_dataloader,
epochs=num_epochs,
#validation_data=val_dataloader,
#callbacks=[reduce_lr, early_stop],
)
print("Training completed. Saving vision and text encoders...")
vision_encoder.save("vision_encoder")
text_encoder.save("text_encoder")
print("Models are saved.")
Epoch 1/5
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-26-745dd79762e6> in <module>()
15 history = dual_encoder.fit(
16 train_dataloader,
---> 17 epochs=num_epochs,
18 #validation_data=val_dataloader,
19 #callbacks=[reduce_lr, early_stopping],
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in
wrapper(*args, **kwargs)
992 except Exception as e: # pylint:disable=broad-except
993 if hasattr(e, "ag_error_metadata"):
--> 994 raise e.ag_error_metadata.to_exception(e)
995 else:
996 raise
TypeError: in user code:
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:853 train_function *
return step_function(self, iterator)
<ipython-input-20-9b27a9a3b9e9>:18 call *
caption_embeddings = text_encoder(features["caption"], training=training)
TypeError: tuple indices must be integers or slices, not str
This is the complete code of my colab codingPLEASE CLICK ON THIS LINK

str object has no attribute 'decode'

BACKBONE = 'vgg16'
preprocess_input = sm.get_preprocessing(BACKBONE)
# preprocess input
x_train_new = preprocess_input(x_train)
x_test_new = preprocess_input(x_test)
# define model
model_resnet_backbone = sm.Unet(BACKBONE,encoder_weights='imagenet', classes=n_classes, activation='softmax')
metrics=['accuracy', jacard]
# compile keras model with defined optimozer, loss and metrics
#model_resnet_backbone.compile(optimizer='adam', loss=focal_loss, metrics=metrics)
model_resnet_backbone.compile(optimizer='adam', loss='categorical_crossentropy', metrics=metrics)
print(model_resnet_backbone.summary())
history_tf=model_resnet_backbone.fit(x_train_new,
y_train,
batch_size=16,
epochs=1,
verbose=1,
validation_data=(x_test_new, y_test))
I want to train using resnet34. But [this error][1] is coming, what should I do?
AttributeError Traceback (en son çağrı) () 8 9 # define model -
--> 10 model_resnet_backbone =
sm.Unet(BACKBONE,encoder_weights='imagenet',
sınıflar=n_classes, activation='softmax')
9 kare /tensorflow-1.15.2/python3.7/keras/engine/saving.py
load_weights_from_hdf5_group(f, Layer, reshape) 1181 """ 1182
f.attrs'de 'keras_version' ise: -> 1183 orijinal_keras_version
= f. attrs['keras_version'].decode('utf8') 1184 başka: 1185
orijinal_keras_version = '1'
AttributeError: 'str' nesnesinin 'decode' özniteliği yok

Training a CNN model UnimplementedError: Graph execution error:

I am a bit confused as I never encountered such an error before. I am tryiing to train my CNN model on images. Below you can see a picture of my code, and then the error message. As you can see it starts at epoch 1 then it stops :(
Does anyone have any idea where does the problem comes from? If anyone had a similar error message before when training your CNN?
Any help is welcome,
Thanks
history = modelA.fit(train_data,
validation_data = test_data,
epochs = 60,
callbacks = [best_model, reduce_lr, es])
ERROR MESSAGE
Epoch 1/60
---------------------------------------------------------------------------
UnimplementedError Traceback (most recent call last)
<ipython-input-68-4b47ff852a2a> in <module>()
2 validation_data = test_data,
3 epochs = 60,
----> 4 callbacks = [best_model, reduce_lr, es])
1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
53 ctx.ensure_initialized()
54 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 55 inputs, attrs, num_outputs)
56 except core._NotOkStatusException as e:
57 if name is not None:
UnimplementedError: Graph execution error:

Using CalibratedClassifierCV returns error: TypeError: predict_proba() missing 1 required positional argument: 'x'

I am trying to train a model using Sequential model of Keras, and then calibrate it using scikit-learn's CalibratedClassifierCV. For this I use the KerasClassifier wrapper. Here is the code I use:
reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1,
patience=5, min_lr=0.000001)
es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10)
def create_model():
model_1 = Sequential()
n_cols = X_train_1.shape[1]
model_1.add(Dense(10, activation="selu", kernel_initializer="lecun_normal",input_shape=(n_cols,)))
model_1.add(Dense(10, activation="selu", kernel_initializer="lecun_normal"))
model_1.add(Dense(10, activation="selu", kernel_initializer="lecun_normal"))
model_1.add(Dense(10, activation="selu", kernel_initializer="lecun_normal"))
model_1.add(Dense(1, activation='sigmoid'))
opt = keras.optimizers.Nadam(lr=0.0001)
loss = tf.keras.losses.BinaryCrossentropy(reduction='sum')
model_1.compile(optimizer=opt, loss=loss)
return model_1
X_train_1, X_test_1, y_train_1, y_test_1, w_train_1, w_test_1 = train_test_split(scaled_X_1, y_1, w_1, test_size=0.35, random_state=42)
model_1 = KerasClassifier(build_fn=create_model, epochs=5, batch_size=5000, verbose=1)
history_1 = model_1.fit(X_train_1, y_train_1, callbacks=[reduce_lr, es], epochs=5, validation_split=0.35, batch_size=5000, sample_weight=w_train_1, verbose=1)
plt.plot(history_1.history['loss'])
plt.plot(history_1.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
calibrator_1 = CalibratedClassifierCV(model_1, cv='prefit')
calibrator_1.fit(X_test_1, y_test_1, sample_weight = w_test_1)
As you see, I clearly call the instance of CalibratedClassifierCV to calibrator_1 and then use fit(). Despite this I get this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-1cab7be4f6d6> in <module>
46 calibrator_1 = CalibratedClassifierCV(model_1, cv='prefit')
47
---> 48 calibrator_1.fit(X_test_1, y_test_1, sample_weight = w_test_1)
~/.local/lib/python3.6/site-packages/sklearn/calibration.py in fit(self, X, y, sample_weight)
263 pred_method = _get_prediction_method(base_estimator)
264 n_classes = len(self.classes_)
--> 265 predictions = _compute_predictions(pred_method, X, n_classes)
266
267 calibrated_classifier = _fit_calibrator(
~/.local/lib/python3.6/site-packages/sklearn/calibration.py in _compute_predictions(pred_method, X, n_classes)
499 (X.shape[0], 1).
500 """
--> 501 predictions = pred_method(X=X)
502 if hasattr(pred_method, '__name__'):
503 method_name = pred_method.__name__
TypeError: predict_proba() missing 1 required positional argument: 'x'
Does anyone spot any errors here?
This seems like an issue with the newer version of scikit-learn (0.24). I installed the older 0.23 version and it runs well with the same exact code.

Get 'function' object has no attribute 'loss' when doing GridsearchCV

I want to try GridsearchCV on my model, my import is :
from keras import models
from keras import layers
from keras import regularizers
from sklearn.model_selection import GridSearchCV
from keras.wrappers.scikit_learn import KerasClassifier
my code is:
def build_model(X_train = X_train,neurons=4,optimizer='Adam'):
model = models.Sequential()
model.add(layers.Dense(X_train.shape[1], kernel_regularizer=regularizers.l2(0.001),
activation='relu', input_shape=(X_train.shape[1],)))
model.add(layers.BatchNormalization())
model.add(layers.Dense(neurons, kernel_regularizer=regularizers.l2(0.001), activation='relu'))
model.add(layers.BatchNormalization())
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(optimizer=optimizer,loss='binary_crossentropy',metrics=['accuracy'])
return build_model
model = KerasClassifier(build_fn=build_model, verbose=1)
# define the grid search parameters
batch_size = [16, 32, 64]
epochs = [50, 100]
param_grid = dict(batch_size=batch_size, epochs=epochs)
# search the grid
grid = GridSearchCV(estimator=model,
param_grid=param_grid,
cv=10,
verbose=2)
grid_result = grid.fit(X_train, y_train)
but I get a bug as below:
AttributeError Traceback (most recent call last)
<ipython-input-93-2eb813d3aab7> in <module>
12 verbose=2) # include n_jobs=-1 if you are using CPU
13
---> 14 grid_result = grid.fit(X_train, y_train)
15
16 print(model)
/anaconda3/envs/lance/lib/python3.7/site-packages/keras/wrappers/scikit_learn.py in fit(self, x, y, sample_weight, **kwargs)
208 if sample_weight is not None:
209 kwargs['sample_weight'] = sample_weight
--> 210 return super(KerasClassifier, self).fit(x, y, **kwargs)
211
212 def predict(self, x, **kwargs):
/anaconda3/envs/lance/lib/python3.7/site-packages/keras/wrappers/scikit_learn.py in fit(self, x, y, **kwargs)
141 self.model = self.build_fn(**self.filter_sk_params(self.build_fn))
142
--> 143 loss_name = self.model.loss
144 if hasattr(loss_name, '__name__'):
145 loss_name = loss_name.__name__
AttributeError: 'function' object has no attribute 'loss'
I can't understand what the bug is, and I'm sure the data processing is correct because it goes well without grid search, did I do something wrong?
At the end of the build_model function, you write return build_model. This returns a reference to the function itself, not to the model object you've been building so far. I'm pretty sure you want return model instead.

Categories

Resources