How to load images and text labels for CNN regression from different folders

2024/9/20 15:31:15

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.

Answer

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.

https://en.xdnf.cn/q/119480.html

Related Q&A

How to calculate number of dates within a year of a date in pandas

I have the following dataframe and I need to calculate the amount of ER visit Dates with a score of 1 that are one year after the PheneDate for that pheneDate for a given subject. So basically phenevi…

Remove substring from string if substring in list in data frame column

I have the following data frame df1string lists 0 i have a dog [fox, dog, cat] 1 there is a cat [dog, house, car] 2 hello everyone [hi, hello, everyone] 3 …

how to save data in the db django model?

Good day, I cant really understand what Im doing wrong in here. I was using this function base view to store my scrap data in the database with the django model, but now its not saving any more. I cant…

Move existing jointplot legend

I tried answers from a previous question to no avail in Matplotlib 1.5.1. I have a seaborn figure:import seaborn as sns %matplotlib inline import matplotlib.pyplot as plt import numpy as np tips = sns.…

timezone conversion of a large list of timestamps from an excel file with python

I have an excel file named "hello.xlsx". There is a column of timestamps that has a lot of rows (more than 80,000 rows for now). The file basically looks like this:03/29/2018 19:24:5003/29/20…

N_gram frequency python NTLK

I want to write a function that returns the frequency of each element in the n-gram of a given text. Help please. I did this code fo counting frequency of 2-gramcode:from nltk import FreqDistfrom nltk.…

Is there a way to have a list of 4 billion numbers in Python?

I made a binary search function and Im curious what would happen if I used it on 4 billion numbers, but I get a MemoryError every time I use it. Is there a way to store the list without this issue?

ValueError: invalid literal for int() with base 10: when it worked before

Im having some issues with my program, basically what Im trying to do is Stenography, insert an image into another image and then extract the secret image.My program is able to insert just fine, but ex…

How to fetch the current branch from Jenkins?

I would like to query Jenkins using its API and Python to fetch the branch that is currently ready to be built.How can I do that?

How to vertically stretch graphs with matplotlib subplot [duplicate]

This question already has answers here:How do I change the size of figures drawn with Matplotlib?(16 answers)Closed 5 years ago.With the following code, I try to plot 12 different histograms in one pi…