Python image classification using keras Error is ValueError: setting an array element with a sequence. help please
from pathlib import Path import keras import numpy as npenter code here from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D from keras.preprocessing import image
# Path to folders with training data
Empty_Morning = Path("images") / "EmptyMorning"
not_Empty_Morning_Path = Path("images") / "PeoplePlayingMorning"
images = []
labels = []
# Not Plastic Bottles Images
for img in not_Empty_Morning_Path.glob("*.jpg"):
# load the image from disk
img = image.load_img(img)
# convert the image to a numpy array
img_array = image.img_to_array(img)
# Add the image to the list of images
images.append(img_array)
# for each 'not plastic bottle image, the expected value should be 0
labels.append(0)
# for all plastic bottles images
for img in Empty_Morning.glob("*.jpg"):
# Load the image from disk
img = image.load_img(img)
# convert the image to an array
img_array = image.img_to_array(img)
# add image to the list of images
images.append(img_array)
# for all plastic bottles image (because it is another class), the expected value should be 1
labels.append(1)
x_train = np.array(images)
y_train = np.array(labels)
x_train = x_train.astype('float34')
x_train /= 255
y_train = keras.utils.to_categorical(y_train, 2)
# Create a model and add layers
model = Sequential()
model.add(Conv2D(32, (3, 3), padding="same", activation="relu", input_shape=(190, 270, 3)))
model.add(Conv2D(32, (3, 3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.15))
model.add(Conv2D(64, (3, 3), padding="same", activation="relu"))
model.add(Conv2D(64, (3, 3), activation="relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.15))
model.add(Flatten())
model.add(Dense(512, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(2, activation="softmax"))
# Compile the Model
model.compile(
loss="categorical_crossentropy",
optimizer="adam",
metrics=['accuracy']
)
# Train the model
model.fit(
x_train,
y_train,
batch_size=32,
epochs=30,
validation_data=(x_train, y_train),
shuffle=True
)
# save neutral network structure
model_structure = model.to_json()
p = Path("model_structure.json")
p.write_text(model_structure)
# save neutral network's trained weights
model.save_weights("model_weights.h5")