Я надеюсь, что у вас все хорошо. Я пытался запустить приведенный ниже код. Я получаю эту ошибку "numpy.ndarray
объект не имеет атрибута добавления". Я пытался использовать решение, рекомендованное в других вопросах, таких как numpy.append()
, numpy.concatenate()
, но не смог решить проблему.
from keras.applications import VGG16
from keras.applications import imagenet_utils
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import load_img
from sklearn.preprocessing import LabelEncoder
from hdf5datasetwriter import HDF5DatasetWriter
from imutils import paths
import progressbar
import argparse
import random
import numpy as np
import os
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-d", "--dataset", required= True,
help=" path to the input dataset ")
ap.add_argument("-o", "--output", required= True,
help=" path to output HDF5 file ")
ap.add_argument("-b","--batch_size", type= int, default=32,
help =" batch size of images to be passed through network ")
ap.add_argument("-s","--buffer_size", type =int, default=1000,
help=" size of feature extraction buffer")
args= vars(ap.parse_args())
# store the batch size in a convenience variable
bs = args["batch_size"]
# grab the list of images that we will be describing then randomly shuffle them to
# allow for easy training and testing splits via array slicing during training time
print ("[INFO] loading images ...")
imagePaths= list(paths.list_images(args["dataset"]))
random.shuffle(imagePaths)
# extract the class labels from the images paths then encode the labels
labels = [p.split(os.path.sep)[-2] for p in imagePaths]
le= LabelEncoder()
labels= le.fit_transform(labels)
# load the VGG16 network
print("[INFO] loading network ...")
model= VGG16(weights="imagenet", include_top=False)
# initialize the HDF5 dataset writer then store the class label names in the
# dataset
dataset = HDF5DatasetWriter((len(imagePaths), 512*7*7), args["output"], dataKey="features",
bufSize= args["buffer_size"])
dataset.storeClassLabels(le.classes_)
# initialize the prograss bar
widgets = [" extracting features:", progressbar.Percentage(), " " , progressbar.Bar(),
" " , progressbar.ETA()]
pbar= progressbar.ProgressBar(maxval=len(imagePaths), widgets= widgets ).start()
# loop over the image patches
for i in np.arange(0, len(imagePaths),bs):
# extract the batch of images and labels, then initalize the
# list of actualimages that will be passed through the network for feature
# extraction
batchPaths= imagePaths[i:i + bs]
batchLabels = labels[i:i+bs]
batchImages = []
for (j, imagePath) in enumerate(batchPaths):
# load the input image using the keras helper utility
# while ensuring the image is resized to 224x224 pixels
image = load_img(imagePath, target_size = (224,224))
image = img_to_array(image)
# preprocess the image by (1) expanding the dimensions and
# (2) substracting the mean RGB pixel intensity from the imagenet dataset
image = np.expand_dims(image, axis =0)
#image = imagenet_utils.preprocess_input(image)
# add the image to the batch
batchImages.append(image)
# pass the images through the network and use the outputs as our
# actual featues
batchImages = np.vstack(batchImages)
features = model.predict(batchImages, batch_size = bs)
# reshape the features so that each image is represented by a flattened feature vector of the maxPooling2D outputs
features = features.reshape((features.shape[0], 512*7*7))
# add the features and the labels to HDF5 dataset
dataset.add(features, batchLabels)
pbar.update(i)
dataset.close()
pbar.finish()
Я получаю это
Я бы хотел, чтобы вы помогли мне решить эту проблему. Спасибо всем заранее