ctx.ensure_initialized() tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name inputs, attrs, num_outputs) - conv-neural-network

I just tried to download the model and retrain it but in the last movement I got this error as I also mention I tried to change the count of images inside the classes but it doesn't work.
[just refer the youtube link for additional information][1]

Related

UnboundLocalError: local variable 'labels' referenced before assignment in PyTorch while doing X-Ray classification

I am trying to replicate this Kaggle notebook, using my data. I am not posting the whole code here, because it is huge.
Received an error, which cannot fix myself
----> 2 model, history = run_fold(model, criterion, optimizer, scheduler, device=CFG.device, fold=0, num_epochs=CFG.num_epochs)
1 frames
<ipython-input-63-1ac8b53f265b> in train_model(model, criterion, optimizer, scheduler, num_epochs,
dataloaders, dataset_sizes, device, fold)
23 for inputs, classes in dataloaders[phase]:
24 inputs = inputs.to(CFG.device)
---> 25 labels = labels.to(CFG.device)
26
27 # forward
UnboundLocalError: local variable 'labels' referenced before assignment
My data structure looks the same as the notebook
I have checked similar questions, there are several of them, but this is more tech question how to fix this thing. Maybe something wrong with index? Appreciate any tips.
There is a typo in the for loop. It should be labels instead of classes:
# ...
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(CFG.device)
labels = labels.to(CFG.device)
# ...

compute Gradients fro GradCam in tf 2.0

I have updated my tensorflow in Python from 1.14 to 2.0 . Now I have a problem with gradient computing, in order to see the GradCam visualisation for a layer.
For example with a model named my_cnn_model, that is already fitted on data, for a classification problem with three classes. If I want to "compute the gradCam" for a given layer named "conv2d_3" for example, I would start with the following in 1.14 :
layer_conv = my_cnn_model.get_layer( "conv2d_3" )
#I want it with respect to the first class (so 0), because for example it might have been the model prediction for that image, so I check the proba for that class :
final_layer = my_cnn_model.output[:, 0]
#Then I computed the gradients like that :
grads = keras.backend.gradients( final_layer, layer_conv.output )[0]
print(grads)
The last statement (print) would say (the shape is specific for the cnn I used but nevermind):
Tensor("gradients/max_pooling2d/MaxPool_grad/MaxPoolGrad:0", shape=(?, 76, 76, 64), dtype=float32)
Now, when I use tf 2.0 : the grads computing part, so :
grads = keras.backend.gradients( final_layer, layer_conv.output )[0]
is not working any more, with the error :
RuntimeError: tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.
I already searched, and found things like
with tf.GradientTape() as tape:
...
But all the same I get errors, or I couldn't get the same output Tensor("gradients/max_pooling2d/MaxPool_grad/MaxPoolGrad:0", shape=(?, 76, 76, 64), dtype=float32), so the rest of my gradcam function does not work.
How could I compute the grads, which of course would be similar to my 1.14 tf env? Do I miss something trivial?
Edit : I used the functionnal API, with my own CNN, or with "Transfer Learning" model already here in tf.keras, with modified/added layers at the top.
Thanks for any help.
If you are not interested in eager mode, like using old code all around, you can simply disable eager execution.
As mentioned here:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
If, on the other hand, you want to keep eager mode on, of if another thing is troubling your code, you can instead:
#you need a persistent tape if you're calling many gradients instead of just one
with tf.GradientTape(persistent = True) as tape:
#must "watch" all variables that are not "trainable weights"
#if you are using them for gradients
tape.watch(layer_conv.output)
#if the input data should be watched (you're getting the gradients related to the inputs)
input_tensor = tf.constant(input_data)
tape.watch(input_tensor)
#must do the entire prediction inside this tape block.
#it would be better if you could make your model output all tensors of interest
#not sure if you can do "some_layer.output" in eager mode for this purpose
model_outputs = model(input_tensor)
#finally, outside the block you can get the gradients
g1 = tape.gradient(model_outputs, layer_conv.output)
#again, maybe you need this layer output to be "actually output"
#instead of gotten from the layer like this
g2 = tape.gradient(some_output, input_tensor)
g3...
g4...
#finally delete the persistent tape
del tape

keras fit one hot vectors input

I wanted to study how to vectorize categorical columns and use them as input for a regression model.
When i tryed to fit it ('SalePrice' is the target),
fit the model
history_v4 = model_v4.fit(x = x_train_v4, y = y_train_v4, epochs=1000, shuffle = True, validation_split = 0.2, workers=12, verbose=1)
it returns that
/tensorflow-2.1.0/python3.6/tensorflow_core/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
94 dtype = dtypes.as_dtype(dtype).as_datatype_enum
95 ctx.ensure_initialized()
---> 96 return ops.EagerTensor(value, ctx.device_name, dtype)
97
98
ValueError: setting an array element with a sequence.
I'm a bit new to dl, but i thought maybe this kind of input could be used. em'i doing something wrong?
Thank's a lot! just need a tip for get in the right direction :)

Multi Label Classification: Error in predicting accuracy

I am new to multilabel classification and I am using Binary Relevance as the classifier. The ML model works fine but when I try to get the accuracy metrics: hamming loss, accuracy scores I get this error as:
in _check_targets(y_true, y_pred)
79 if len(y_type) > 1:
80 raise ValueError("Classification metrics can't handle a
mix of {0} "
---> 81 "and {1} targets".format(type_true,
type_pred))
82
83 # We can't have more than one value on y_type => The set is no
more needed
ValueError: Classification metrics can't handle a mix of
binary and multilabel-indicator targets
I did raise a question in also
https://datascience.stackexchange.com/questions/58912/binaryrelevence-classifier-giving-errors-during-predicting-accuracy-scores
Can someone please tell why this error pops up and ways of resolving this issue.

Saving and Loading Pytorch Model Checkpoint for inference not working

I have a trained model using LSTM. The model is trained on GPU (On Google COLABORATORY).
I have to save the model for inference; which I will run on CPU.
Once trained, I saved the model checkpoint as follows:
torch.save({'model_state_dict': model.state_dict()},'lstmmodelgpu.tar')
And, for inference, I loaded the model as :
# model definition
vocab_size = len(vocab_to_int)+1
output_size = 1
embedding_dim = 300
hidden_dim = 256
n_layers = 2
model = SentimentLSTM(vocab_size, output_size, embedding_dim, hidden_dim, n_layers)
# loading model
device = torch.device('cpu')
checkpoint = torch.load('lstmmodelgpu.tar', map_location=device)
model.load_state_dict(checkpoint['model_state_dict'])
model.eval()
But, it is raising the following error:
model.load_state_dict(checkpoint['model_state_dict'])
File "workspace/envs/envdeeplearning/lib/python3.5/site-packages/torch/nn/modules/module.py", line 719, in load_state_dict
self.__class__.__name__, "\n\t".join(error_msgs)))
RuntimeError: Error(s) in loading state_dict for SentimentLSTM:
Missing key(s) in state_dict: "embedding.weight".
Unexpected key(s) in state_dict: "encoder.weight".
Is there anything I missed while saving the checkpoint?
There are two things to be considered here.
You mentioned that you're training your model on GPU and using it for inference on CPU, so u need to add a parameter map_location in load function passing torch.device('cpu').
There is a mismatch of state_dict keys (indicated in your ouput message), which might be caused by some missing keys or having more keys in state_dict you are loading than the model u are using currently. And for it you have to add a parameter strict with value False in the load_state_dict function. This will make method to ignore the mismatch of keys.
Side note : Try to use extension of pt or pth for checkpoint files as it is a convention .

Resources