Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'img_to_array' from 'keras.preprocessing.image'

Tags:

python

keras

Im new here. I have problem with this code,

#Library
import numpy as np
import pickle
import cv2
from os import listdir
from sklearn.preprocessing import LabelBinarizer
from keras.models import Sequential
from keras.layers import BatchNormalization
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.layers.core import Activation, Flatten, Dropout, Dense
from keras import backend as K
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from keras.preprocessing import image
#from tensorflow.keras.preprocessing.image import img_to_array
from keras.preprocessing.image import img_to_array
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

I got an error

enter image description here

this code is from github link

Im using

  1. python 3.7.13
  2. tensorflow 2.9
  3. opencv 4.5.5
  4. keras 2.9.0
like image 999
AINUL HAKIM BIN FIZAL SABILLAH Avatar asked Mar 02 '26 12:03

AINUL HAKIM BIN FIZAL SABILLAH


2 Answers

In Keras Documentation V2.9.0,

In tf version 2.9.0 the img_to_array moved to utlis

Insted of,

from keras.preprocessing.image import img_to_array

Try this,

from tensorflow.keras.utils import img_to_array

like image 137
MUKILAN S Avatar answered Mar 05 '26 01:03

MUKILAN S


Instead of:

from keras.preprocessing.image import img_to_array

Try:

from keras_preprocessing.image import img_to_array

Notice the underscore (_) instead of the dot (.)

like image 40
AMULRAJ Avatar answered Mar 05 '26 00:03

AMULRAJ