[英]How to load images and text labels for CNN regression from different folders
I have two folders, X_train and Y_train.我有两个文件夹,X_train 和 Y_train。 X_train is images, Y_train is vector and.txt files.
X_train 是图像,Y_train 是矢量和.txt 文件。 I try to train CNN for regression.
我尝试训练 CNN 进行回归。
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.
当我使用“ImageDataGenerator”时,它假设 X_train 和 Y_train 文件夹是类。
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?上面,我找到了它们的目的地,我怎样才能带走它们并为 model.fit 做好准备? Thank you.
谢谢你。
Makes sure x_files
and y_files
are sorted together, then you can use something like this:确保
x_files
和y_files
排序在一起,然后你可以使用这样的东西:
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.然后,可以将
train_ds
传递给model.fit()
并将返回 32 对图像、标签的批次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.