简体   繁体   English

如何从不同文件夹加载图像和文本标签以进行 CNN 回归

[英]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_filesy_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.

相关问题 如何从python中的不同文件夹和子文件夹加载图像 - how to load images from different folders and subfolders in python 如何从python中的不同文件夹和子文件夹加载数据(图像) - How to load data(images) from different folders and subfolders in python 在TensorFlow中使用队列从文本文件加载图像和标签 - Using queues in TensorFlow to load images and labels from text file 我们如何从两个不同的文件夹中读取相应的图像? - How can we read corresponding images from two different folders? 如何使用包含文件名和标签的数据帧将带有类标签的图像从单个目录加载到Keras模型中? - How to load the images with class labels from a single directory into Keras model using a dataframe containing filenames and labels? 如何堆叠张量(来自图像)来训练 CNN? - How to stack tensors (from images) to train a CNN? 从链接到tensorflow的tarball中加载图像以训练CNN - Load images in tarball from a link to tensorflow for training CNN 合并来自不同源文件夹的图像 - Merging images from different source folders 从不同文件夹中的图像创建numpy数组 - Create numpy array from images in different folders 如何从包含在不同文件夹中的 docx 文件中提取文本 - how to extract text from docx files contaning in different folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM