问题
I want to put data of a image in a numpy array, but every time I get the error ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 4 dimension(s)
or I get that my array is zero-dimensional and that's also not right. How should I initialise my variable before the function?
Here some code:
from extra_keras_datasets import emnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.python.keras.layers.convolutional import Conv2D
from tensorflow.python.keras.layers.convolutional import MaxPooling2D
from tensorflow.keras.optimizers import Adam
from tensorflow.python.keras.utils import np_utils
from PIL import Image
import numpy as np
import os
#to load the own images to the program and reshape their data so it's fitting the CNN.
def load_images_to_data(image_label, image_directory, features_data, label_data):
list_of_files = os.listdir(image_directory)
for file in list_of_files:
image_file_name = os.path.join(image_directory, file)
if ".png" in image_file_name:
img = Image.open(image_file_name).convert("L")
img = np.resize(img, (28,28,1))
im2arr = np.array(img)
im2arr = im2arr.reshape(1,28,28,1)
features_data = np.append(features_data, im2arr, axis=0)
label_data = np.append(label_data, [image_label], axis=0)
return features_data, label_data
# load data
(X_train, y_train), (X_test, y_test) = emnist.load_data(type='letters')
X_test = np.empty((4,1), np.float32)
y_test = None
# Reshaping to format which CNN expects (batch, height, width, channels)
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], X_train.shape[2], 1).astype('float32')
X_test, y_test = load_images_to_data('a', 'Photos/1/a', X_test, y_test)
I've tried X_test = None, X_test = np.empty((4,1), np.float32), X_test = np.empty((1,4), np.float32), but it won't work. The idea of this program came from here, and I want to test with my own data and not the test data from EMNIST.
回答1:
The use of np.append
in this function is a bad idea. (any use of np.append
is bad):
X_test = np.empty((4,1), np.float32)
def load_images_to_data(image_label, image_directory, features_data, label_data):
list_of_files = os.listdir(image_directory)
for file in list_of_files:
image_file_name = os.path.join(image_directory, file)
if ".png" in image_file_name:
img = Image.open(image_file_name).convert("L")
img = np.resize(img, (28,28,1))
im2arr = np.array(img)
im2arr = im2arr.reshape(1,28,28,1)
features_data = np.append(features_data, im2arr, axis=0)
label_data = np.append(label_data, [image_label], axis=0)
return features_data, label_data
As you found out it is hard to get the initial value of features_data
right. And it is slow, creating a whole new array each call.
np.append(A,B, axis=0)
just does np.concatenate([A,B], axis=0)
. Give it the whole list of arrays, rather than doing the concatenate one by one.
def load_images_to_data(image_label, image_directory):
list_of_files = os.listdir(image_directory)
alist = []; blist = []
for file in list_of_files:
image_file_name = os.path.join(image_directory, file)
if ".png" in image_file_name:
img = Image.open(image_file_name).convert("L")
img = np.resize(img, (28,28,1))
im2arr = np.array(img)
im2arr = im2arr.reshape(1,28,28,1)
alist.append(im2arr)
blist.append([image_label]
features_data = np.concatenate(alist, axis=0)
label_data = np.concatenate(blist, axis=0)
return features_data, label_data
来源:https://stackoverflow.com/questions/60850266/initial-value-numpy-array-before-starting-the-function-in-python