Error in “from keras.utils import to_categorical”


I have probem with this code , why ?

the code :

import cv2
import numpy as np
from PIL import Image
import os
import numpy as np
import cv2
import os
import h5py
import dlib
from imutils import face_utils
from keras.models import load_model
import sys
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D,Dropout
from keras.layers import Dense, Activation, Flatten
from keras.utils import to_categorical
from keras import backend as K 
from sklearn.model_selection import train_test_split
from Model import model
from keras import callbacks

# Path for face image database
path = 'dataset'

recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml");

def downsample_image(img):
    img = Image.fromarray(img.astype('uint8'), 'L')
    img = img.resize((32,32), Image.ANTIALIAS)
    return np.array(img)

# function to get the images and label data
def getImagesAndLabels(path):
    path = 'dataset'
    imagePaths = [os.path.join(path,f) for f in os.listdir(path)]     
    ids = []

    for imagePath in imagePaths:
        #if there is an error saving any jpegs
            PIL_img ='L') # convert it to grayscale
        img_numpy = np.array(PIL_img,'uint8')

        id = int(os.path.split(imagePath)[-1].split(".")[1])
    return faceSamples,ids

print ("n [INFO] Training faces now.")
faces,ids = getImagesAndLabels(path)

n_faces = len(set(ids))
model = model((32,32,1),n_faces)
faces = np.asarray(faces)
faces = np.array([downsample_image(ab) for ab in faces])
ids = np.asarray(ids)
faces = faces[:,:,:,np.newaxis]
print("Shape of Data: " + str(faces.shape))
print("Number of unique faces : " + str(n_faces))

ids = to_categorical(ids)

faces = faces.astype('float32')
faces /= 255.

x_train, x_test, y_train, y_test = train_test_split(faces,ids, test_size = 0.2, random_state = 0)

checkpoint = callbacks.ModelCheckpoint('trained_model.h5', monitor='val_acc',
                                           save_best_only=True, save_weights_only=True, verbose=1)
                           , y_train,
             validation_data=(x_test, y_test),

# Print the numer of faces trained and end program
print("enter code here`n [INFO] " + str(n_faces) + " faces trained. Exiting Program")

the output:
File "D:my hard samماجستيرسنة ثانيةالبحث", line 16, in <module>
    from keras.utils import to_categorical
ImportError: cannot import name 'to_categorical' from 'keras.utils' (


Keras is now fully intregrated into Tensorflow. So, importing only Keras causes error.

It should be imported as:

from tensorflow.keras.utils import to_categorical

Avoid importing as:

from keras.utils import to_categorical

It is safe to use
from tensorflow.keras. instead of from keras. while importing all the necessary modules.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D,Dropout
from tensorflow.keras.layers import Dense, Activation, Flatten
from tensorflow.keras.utils import to_categorical
from tensorflow.keras import backend as K 
from sklearn.model_selection import train_test_split
from tensorflow.keras import callbacks


Leave a Reply

Your email address will not be published. Required fields are marked *