Kernel Died error post installing tensorflow

I m using macbook air 13" and pursuing Artificial Intelligence course

I m facing huge problem with Jupyter notebook post installing tensorflow as the kernel keeps dying and I have literally tried solution in every article/resource on Google

Nothing seems to be fixing the issue.

It began only when I started to run code for Convolutional Neural Network

Please help me fix this issue and understand why its not getting fixed

At the moment, I can only think of trading Macbook for Windows Laptop but It will be very hard as I have not had hands-on Windows laptop

Hope to hear back soon

Thanks Keshav Lal Seth

  • I am facing the same issue today. After uninstalling anaconda python and reinstalling it, I realize that the issue occurs simply because I used "pip install tensor flow". Each time after I did this, my Jupyter notebook kernel would not work. The laptop I use is MacBook Pro 2021.

  • I'm using the M2. and the same thing happens.

Add a Comment

Replies

Hi @KeshaavlalSseth,

Can you provide a bit more information on the problem you are facing: Which OS version and version of the MacBook Air (Intel or M1) are you running on? Which versions of the Jupyter packages and Tensorflow packages are you using? Did you follow the instructions at https://developer.apple.com/metal/tensorflow-plugin/ when installing? Can you provide an example script that causes the crash for you so we can try to reproduce this issue?

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"


Layer (type) Output Shape Param #

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


Make the data ready

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=image.img_to_array(test_img)

test_img1

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)

Testing the model with single image

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

Hi @KeshaavlalSseth

Yes it is indeed important to take the steps outlined in the tensorflow-plugin guide when setting up the Python environment in order to ensure the compatibility among different python packages on the M1 machine. There are multiple reasons why the Jupyter kernel can die and among one of them could be some unexpected conflict between the various packages. This setup is also the one we are testing and supporting, allowing us to debug the issues in case any problems arise.

Additionally the instructions on TensorFlow installation in the Anaconda site would not allow you to take advantage of the GPU on the M1 Mac since the computations are passed to the GPU using the TensorFlows pluggable device architecture which requires a base TensorFlow version (tensorflow_macos package) as well as the GPU plugin (tensorflow_metal package) to run.

So please try following the instructions for arm64 on the installation page https://developer.apple.com/metal/tensorflow-plugin/ to set up the virtual environment for Python and installing the packages and let me know if there is a specific step that is not working on your machine. Also note that you need to make sure your operating system is updated to match the requirements (macOS 12.0+).

  • Ok any thoughts and comments on rest of the steps mentions that I have folowed

    Also, where and how below mentioned to use these and other commands conda terminal post opening jupyter notebook?

    Download and install Conda env: chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh sh ~/Downloads/Miniforge3-MacOSX-arm64.sh source ~/miniforge3/bin/activate

Add a Comment

Ok any thoughts and comments on rest of the steps mentions that I have followed

Also, where and how below mentioned to use these and other commands conda terminal post opening jupyter notebook?

Download and install Conda env: chmod +x ~/Downloads/Miniforge3-MacOSX-arm64.sh sh ~/Downloads/Miniforge3-MacOSX-arm64.sh source ~/miniforge3/bin/activate

  • Notice that the step of setting up the environment is done before you open the notebook. After the setup is complete you will open the notebook within this environment so that the notebook is able to use the packages available in the environment.

    After having installed and activated the conda environment with the commands you wrote above following the Tensorflow Plugin guide, your terminal should show something like (base) tag at the beginning of the command line row to indicate you are in the conda base environment. You can continue on the command line for the remaining steps of installing the TensorFlow-dependencies with conda install -c apple tensorflow-deps, the TensorFlow base python -m pip install tensorflow-macos and the TensorFlow metal plugin python -m pip install tensorflow-metal.

    Now you can install the Jupyter notebook to the environment using conda install notebook. You can also install any other python packages you might need in your notebook directly to the environment on the command line. After this you can call the usual jupyter notebook on the command line to start the notebook server and start working on your notebook within the conda environment you have just set up with the latest TensorFlow for MacOS.

Add a Comment

Hi Team,

The steps share in https://developer.apple.com/metal/tensorflow-plugin/ are nicely explained but it returns error when trying to load via terminal

My course is getting affected big time as Google colab is not the right alternate and Jupyter has to work on my system

Some commands dont work on colab pls help me get this fixed

(base) keshavlalseth@Keshavs-MacBook-Air ~ % conda install -c apple tensorflow-deps Collecting package metadata (current_repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Collecting package metadata (repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve.

PackagesNotFoundError: The following packages are not available from current channels:

 - tensorflow-deps

Current channels:

 - https://conda.anaconda.org/apple/osx-64  - https://conda.anaconda.org/apple/noarch  - https://repo.anaconda.com/pkgs/main/osx-64  - https://repo.anaconda.com/pkgs/main/noarch  - https://repo.anaconda.com/pkgs/r/osx-64  - https://repo.anaconda.com/pkgs/r/noarch

To search for alternate channels that may provide the conda package you're looking for, navigate to

  https://anaconda.org

and use the search bar at the top of the page.

(base) keshavlalseth@Keshavs-MacBook-Air ~ % python -m pip install tensorflow-macos

Collecting tensorflow-macos  ERROR: Could not find a version that satisfies the requirement tensorflow-macos (from versions: none) ERROR: No matching distribution found for tensorflow-macos (base) keshavlalseth@Keshavs-MacBook-Air ~ % python -m pip install tensorflow-metal

Collecting tensorflow-metal  ERROR: Could not find a version that satisfies the requirement tensorflow-metal (from versions: none) ERROR: No matching distribution found for tensorflow-metal

(base) keshavlalseth@Keshavs-MacBook-Air ~ % conda install -c apple tensorflow-deps==2.5.0

Collecting package metadata (current_repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Collecting package metadata (repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve.

PackagesNotFoundError: The following packages are not available from current channels:

 - tensorflow-deps==2.5.0

Current channels:

 - https://conda.anaconda.org/apple/osx-64  - https://conda.anaconda.org/apple/noarch  - https://repo.anaconda.com/pkgs/main/osx-64  - https://repo.anaconda.com/pkgs/main/noarch  - https://repo.anaconda.com/pkgs/r/osx-64  - https://repo.anaconda.com/pkgs/r/noarch

To search for alternate channels that may provide the conda package you're looking for, navigate to

  https://anaconda.org

and use the search bar at the top of the page.

These steps in apple's help article dont help at all

Hi @KeshaavlalSseth

At this point the only theory I have on what could be wrong has to do having a conflicting conda installation somewhere on the machine that is being called instead of the miniforge3 installed in the guide. Do make sure you are on a supported OS version (12.0+) and make sure your conda calls go to the miniforge3 instead of some other installation you might have had earlier.

Could you elaborate that a bit more

are you referring to the environment

I followed steps here after going thru alot of resources

https://github.com/mrdbourke/m1-machine-learning-test#how-to-setup-a-tensorflow-environment-on-m1-m1-pro-m1-max-using-miniforge-shorter-version

If i open jupyter notebok from here for now it seems to work

https://drive.google.com/file/d/1XNoAShO6y30TQ4hNpPvZqwDkHPWjDiW1/view?usp=sharing

but i m not sure how to switch to another environment

can you suggest something there

I use a MacBook Pro and I have met the same problem. Basically it would only happen when I train CNN or import module Keras.preprocessing. My solution is to create a new environment and only install packages you need at this time.

you can use code below in terminal:

'conda install -n tf tensorflow'. (###create a new TensorFlow environment named tf (or other names you like))

'conda activate tf' (###activate the new environment )

then install packages you need in this environment, for example:

'conda install Keras'

'conda install Jupiter notebook'

It would work well. I also recommend to use Anaconda Navigator so you can easily switch from one environment to another one instead of changing by terminal (in terminal, use 'conda deactivate', it will return to the base environment ). You can also check and install new packages in navigator.

I am facing the same issue today. After uninstalling anaconda python and reinstalling it, I realize that the issue occurs simply because I used "pip install tensor flow". Each time after I did this, my Jupyter notebook kernel would not work. The laptop I use is MacBook Pro 2021.

I met almost the same problem. Finally, I installed: tensorflow-deps==2.8.0 tensorflow-macos==2.8.0 tensorflow-metal==0.5.0 and everything went well with GPU support.

Without tensorflow-metal, I could use tensorflow without GPU support. WIth tensorflow-metal 0.6.0 which had been installed automatically, I encountered kernel crash.

Hi, This GitHub page helped me with persisting issue of kernel dying, here is the link https://github.com/jeffheaton/t81_558_deep_learning/blob/master/install/tensorflow-install-mac-metal-jan-2023.ipynb