简体   繁体   中英

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 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 glob

x_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 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 = 128

def 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 img

def process_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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM