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 the network. When i use "ImageDataGenerator" , it suppose that X_train and Y_train folders are classes.
import os
import tensorflow as tf
os.chdir(r'C:\\Data')
from glob2 import globx_files = glob('X_train\\*.jpg')
y_files = glob('Y_rain\\*.txt')
Above, i found destination of them, how can i take them and be ready for model.fit ? Thank you.
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 osx_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 = 128def get_label(file_path):label = tf.io.read_file(file_path)return tf.cast(label == target_names, tf.int32)def decode_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 imgdef process_path(file_path):label = get_label(file_path)img = tf.io.read_file(file_path)img = decode_img(img)return img, labeltrain_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.