yonas
yonas

Reputation: 41

cant load custom dataset to cnn pretrained for feature extraction

hello i am newbie to all this and i am trying to feed the pretrained CNN VGG16 with a custom dataset of mine and then to achieve feature extraction for every image with numpy. but i am taking this error:'numpy.ndarray' object has no attribute 'load_img' really any help appreciate it.thanks

from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
import matplotlib.pyplot as plt
import os

model = VGG16(weights='imagenet', include_top=False)
dir_images = "C:/Users/.../Desktop/db"
imgs = os.listdir(dir_images)
for imgnm in imgs:
    image = plt.imread(os.path.join(dir_images, imgnm))
img = image.load_img(image, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
#np.save('features.csv', features)

Upvotes: 1

Views: 68

Answers (1)

LucG
LucG

Reputation: 1334

You are overiding the module image of keras.preprocessing by your own actual images loaded with matplotlib.

So just change the line

image = plt.imread(os.path.join(dir_images, imgnm))

into somehting else like

arr_image = plt.imread(os.path.join(dir_images, imgnm))

and then this error will be gone.

But note that image.load_img takes path as input and not actual images of type ndarray so you should instead use load_img in the loop and remove the matplotlib loading.

Upvotes: 1

Related Questions