How To Load Images And Text Labels For Cnn Regression From Different Folders
I have two folders, X_train and Y_train. X_train is images, Y_train is vector and .txt files. I try to train CNN for regression. I could not figure out how to take data and train t
Solution 1:
Makes sure x_files
and y_files
are sorted together, then you can use something like this:
import tensorflow as tf
from glob2 import glob
import os
x_files = glob('X_train\\*.jpg')
y_files = glob('Y_rain\\*.txt')
target_names = ['cat', 'dog']
files = tf.data.Dataset.from_tensor_slices((x_files, y_files))
imsize = 128defget_label(file_path):
label = tf.io.read_file(file_path)
return tf.cast(label == target_names, tf.int32)
defdecode_img(img):
img = tf.image.decode_jpeg(img, channels=3)
img = tf.image.convert_image_dtype(img, tf.float32)
img = tf.image.resize(images=img, size=(imsize, imsize))
return img
defprocess_path(file_path):
label = get_label(file_path)
img = tf.io.read_file(file_path)
img = decode_img(img)
return img, label
train_ds = files.map(process_path).batch(32)
Then, train_ds
can be passed to model.fit()
and will return batches of 32 pairs of images, labels.
Post a Comment for "How To Load Images And Text Labels For Cnn Regression From Different Folders"