I am trying to convert a list of PIL images into to nparray but i am getting this error.
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (122,) + inhomogeneous part.
Here is my code:
import os
import zipfile
import glob
import random
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, MaxPool2D, UpSampling2D, Concatenate, Conv2DTranspose, BatchNormalization, Dropout, Lambda, ReLU, Activation
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical, normalize
from PIL import Image, ImageSequence
import numpy as np
from matplotlib import pyplot as plt
from sklearn.preprocessing import LabelEncoder
from sklearn.utils import class_weight
SIZE_X = 256
SIZE_Y = 256
n_classes = 4
def get_images_from_directory(directory_path: str) -> list:
images = []
for filename in os.listdir(directory_path):
if filename.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')):
image_path = os.path.join(directory_path, filename)
image = Image.open(image_path).convert("RGB")
for frame in range(getattr(image, "n_frames", 1)):
image.seek(frame)
images.append(image.copy())
else:
continue
return images
images = get_images_from_directory(r"C:UsersquariMasaüstüpmtorchdataimages")
masks = get_images_from_directory(r"C:UsersquariMasaüstüpmtorchdatamasks")
train_images = np.aarray(images)
train_masks = np.aarray(masks)
I looked at other stackoverflow questions, but it didn’t solve my problem.
New contributor
Muratcan Laloglu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.