Error: canceled future for execute_request message before replies were done

Issue

I try to run a Jupyter notebook on my machine. And here is what I want to do:

  1. load an image
  2. load a pre-train model from 'tensorflow.keras.applications'
  3. make prediction

Here is the sample code

from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions model = ResNet50(weights='imagenet', include_top=True) img = tf.keras.preprocessing.image.load_img(imgfile, grayscale=False, color_mode='rgb', target_size=(224,224),) img = tf.keras.preprocessing.image.img_to_array(img) img = preprocess_input(img) img = np.reshape(img,(-1,224,224,3)) preds = model.predict(img) # crash at this line

Then the kernel crash with message below:

warn 15:23:44.556: StdErr from Kernel Process 2022-05-22 15:23:44.55 6754: I tensorflow/stream_executor/cuda/cuda_dnn.cc:368] Loaded cuDNN version 8400 error 15:23:45.519: Disposing session as kernel process died ExitCode: 3221226505, Reason: C:\XXXXX\Python39\site-packages\traitlets\traitlets.py:2202: FutureWarning: Supporting extra quotes around strings is deprecated in traitlets 5.0. You can use 'hmac-sha256' instead of '"hmac-sha256"' if you require traitlets >=5. warn( C:\XXXXX\Python39\site-packages\traitlets\traitlets.py:2157: FutureWarning: Supporting extra quotes around Bytes is deprecated in traitlets 5.0. Use '7176e2c9-27f6-42c2-8ae4-00b7ea3c655d' instead of 'b"7176e2c9-27f6-42c2-8ae4-00b7ea3c655d"'. warn( 2022-05-22 15:23:36.484356: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2022-05-22 15:23:37.856705: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 3969 MB memory: -> device: 0, name: NVIDIA GeForce RTX 2060, pci bus id: 0000:01:00.0, compute capability: 7.5 2022-05-22 15:23:44.556754: I tensorflow/stream_executor/cuda/cuda_dnn.cc:368] Loaded cuDNN version 8400 info 15:23:45.520: Dispose Kernel process info 15:23:45.520: kill daemon error 15:23:45.520: Raw kernel process exited code: 3221226505 error 15:23:45.522: Error in waiting for cell to complete [Error: Canceled future for execute_request message before replies were done at t.KernelShellFutureHandler.dispose (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:1327723) at c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:1346775 at Map.forEach (<anonymous>) at v._clearKernelState (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:1346760) at v.dispose (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:1340242) at c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:539831 at t.swallowExceptions (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:924542) at u.dispose (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:539809) at t.RawSession.dispose (c:\XXXXX\.vscode\extensions\ms-toolsai.jupyter-2022.5.1001311010\out\extension.node.js:2:543488) at processTicksAndRejections (node:internal/process/task_queues:96:5)] warn 15:23:45.523: Cell completed with errors { message: 'Canceled future for execute_request message before replies were done' } info 15:23:45.525: Cancel all remaining cells true || Error || undefined info 15:23:45.525: Cancel pending cells info 15:23:45.525: Cell 5 executed with state Error

Environment

  • Win10
  • Python 3.9.6
  • VScode 1.67.0
    • Jupyter extension v2022.5.1001311010
    • Python extension v2022.6.2
  • CUDA v11.2 & v11.6
    • not sure which version is used by tensorflow

I search some articles, some say it might be the incomplete installation of GPU development settings, some say it is a bug from VScode jupyter extension.

Unfortunately, I have no luck to solve my issue.

Any suggestion is appreciated.

Thanks!


Solution

Providing the solution here (Answer Section), even though it is present in the Comment Section for the benefit of the community.

This issue is due to version incompatibility. As per the tested build configurations, Tensorflow 2.9.0 is compatible with CUDA 11.2 and cuDNN 8.1. Downgrading it to cuDNN 8.1 and using CUDA 11.2 will help.

Answered By - Tfer3

Hi,

Thanks for your response!

--Which OS version and version of the MacBook Air (Intel or M1) are you running on?

MacBook Air - 13" (M1)

--Which versions of the Jupyter packages and Tensorflow packages are you using?

I've downloaded Anaconda from link available online https://www.anaconda.com/products/individual It has Jupyter 6.4.5 in it.

It was working fine until Deep - ANN. The moment I started coding for CNN it returned error kernel died. I did not change anything. So it quite strange what happened. Hopeful of getting it fixed thrpugh this thread :-)

Same for Tensorflow as well https://docs.anaconda.com/anaconda/user-guide/tasks/tensorflow/

--Did you follow the instructions at https://developer.apple.com/metal/tensorflow-plugin/%C2%A0when installing? 

No, sorry. I wasn't familiar with this resource. I m not able to follow this resource. could you explain & if this is necessary after having above steps doen and they been working fine earlier

--Can you provide an example script that causes the crash for you so we can try to reproduce this issue?

import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import seaborn as sb #import os #os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" #os.environ["CUDA_VISIBLE_DEVICES"]="0"

from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense

from tensorflow.keras.preprocessing import image

flower_classifier=Sequential()

flower_classifier.add(Conv2D(16,(3,3),input_shape=(128,128,3),activation='relu')) flower_classifier.add(MaxPooling2D(pool_size=(2,2)))

flower_classifier.add(Conv2D(32,(3,3),activation='relu')) flower_classifier.add(MaxPooling2D(pool_size=(2,2)))

flower_classifier.add(Flatten())

flower_classifier.add(Dense(units=128,activation='relu'))

flower_classifier.add(Dense(units=5,activation='softmax'))

flower_classifier.summary()

Model: "sequential"


conv2d (Conv2D) (None, 126, 126, 16) 448

max_pooling2d (MaxPooling2D (None, 63, 63, 16) 0
)

conv2d_1 (Conv2D) (None, 61, 61, 32) 4640

max_pooling2d_1 (MaxPooling (None, 30, 30, 32) 0
2D)

flatten (Flatten) (None, 28800) 0

dense (Dense) (None, 128) 3686528

dense_1 (Dense) (None, 5) 645

================================================================= Total params: 3,692,261 Trainable params: 3,692,261 Non-trainable params: 0


from tensorflow.keras.preprocessing.image import ImageDataGenerator

train_datagen=ImageDataGenerator(rescale=1/255., rotation_range=45, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='reflect')

test_datagen=ImageDataGenerator(rescale=1/255.)

train_datagen

<keras.preprocessing.image.ImageDataGenerator at 0x1b013414c18>

train_set=train_datagen.flow_from_directory('C:\Users\tksen\Desktop\DL\Datasets\flower_photos\Training', target_size=(128,128), batch_size=128, class_mode='categorical')

test_set=test_datagen.flow_from_directory('C:\Users\tksen\Desktop\DL\Datasets\flower_photos\Testing', target_size=(128,128), batch_size=128, class_mode='categorical')

flower_classifier.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])

flower_classifier.fit(train_set, steps_per_epoch=2736//128, epochs=5, validation_data=test_set, validation_steps=934//128)

type(test_img)

test_img1=test_img1/255.

test_img1.shape

test_img2=np.expand_dims(test_img1,axis=0) test_img2.shape

ypred=flower_classifier.predict(test_img2) ypred.round(2)

print(train_set.class_indices)

test_img=image.load_img('C:\Users\tksen\Desktop\DL\Datasets\flower_photos\example\1.jpg',target_size=(128,128)) test_img1=image.img_to_array(test_img)

test_img1=test_img1/255.

test_img2=np.expand_dims(test_img1,axis=0)

ypred=flower_classifier.predict(test_img2) print(train_set.class_indices)

print('The test image class is :',ypred.argmax())

class_name=train_set.class_indices pos=np.array(list(class_name.values()))==ypred.argmax() name=np.array(list(class_name.keys())) print('The Predicted class name is:') print(name[pos][0]) test_img

flower_classifier.save('flower_classifier.h5')

Please let me know if I can give you any other details