Reading images with opencv and loading them into memory as np arrays


I’m trying to train a silly regression model to predict people’s ages using photos. I know that’s not the best approach, but that’s not the point. The point is, even though the entire image dataset is just 185 MBs, it doesn’t fit into the GPU’s memory on Colab. I’m loading the dataset in the wrong way. Here is the code:

my_list = os.listdir('UTKFace')
len(my_list) #23708

ages = []

for i in range(10000):
  s = my_list[i]
  s = s.split('_')[0]

ages_np = np.array(ages)
ages_np = ages_np.astype(np.float16)

faces = []

for i in range(10000):
    image = cv2.imread("UTKFace/" + my_list[i])
    image = cv2.resize(image, (200, 200))

faces_np = np.array(faces)
del faces
faces_np = faces_np / 255.0
faces_np = faces_np.astype(np.float16)

inputs= Input(shape=(200,200,3))

x = Conv2D(8, kernel_size=(3, 3), activation='relu',padding='SAME')(inputs)
x = MaxPooling2D(pool_size=(2, 2))(x)

x = Conv2D(4, kernel_size=(3, 3), activation='relu',padding='SAME')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)

x = Flatten()(x)
x = Dense(units=4, activation='relu')(x)

output = Dense(units=1)(x)

model= Model(inputs=inputs, outputs=output)


history =, ages_np, batch_size=4, epochs=1000, validation_split=0.1)

What’s wrong with my approach? Thank you so much!

Wow, I knew nothing about tensorflow…but if this works, then carding people will be in the past.
What about combining two languages, like with opencv ? I’m a newbie in A.I programming, so go easy on me.

list your image files, create a tensorflow Dataset from tensor slices, call map and load images and labels into it.
then, batch it