Can I flow_from_dataframe if my labels are also filepaths? - python

I am trying to train an image reconstruction network, that I would train like this:
vae.fit(X_train, X_train, epochs=10, batch_size=1)
where X_train is a NumPy array of the training images.
However, I want to use a generator because otherwise, I run out of memory. I have tried to use flow_from_dataframe, where I have all the file paths of the images stored (they are across multiple folders).
train_generator=datagen.flow_from_dataframe(
dataframe=df,
x_col="filepath",
y_col="filepath")
The issue is this function inputs x_col (file path) and y_col(label). Since my loss function is based on reconstruction error, my label should be the same image itself. Is there a way to do this with this function or with another kind of generator?

For autoencoders, you can set class_mode=input, and then you don't have to set y_col.
So try this:
train_generator=datagen.flow_from_dataframe(
dataframe=df,
x_col="filepath",
class_mode="input")

Related

Read data directly from folders for training in keras

I am doing super resolution with resnet in keras and I have split my data into train and test (70-30) and from the test data 20% for validation .i am trying to read the data with datagen.flow_from_directory but its showing 0 images for 0 classes .The main issue is i dont have classes. I only have high resolution images and low resolution images. The high resolution images goes to output and the low resolution images goes to input. How can i load the data without separating them in classess
from keras.preprocessing.image import ImageDataGenerator
import os
train_dir = r'G:\\images\\train'
train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(train_dir)
To resolve 0 images for 0 classes, notice that a common mistake is that the target folder you specify has no subdirectory. ImageDataGenerator splits data to classes, based on each subdirectory under the directory you specify as it's first argument. So, you should have at least one subdirectory under the target.
Furthermore, the generator should label them in order to feed them to your network. By default it uses categorical method as a 2D one-hot encoded labels. But if you want your labels in other ways, set class_mode argument. For example for autoencoders that inputs has no label, you should specify it as class_mode=input.
Base on the docs here, class_mode should be one of these:
categorical will be 2D one-hot encoded labels, (Default Mode)
binary will be 1D binary labels,
sparse will be 1D integer labels,
input will be images identical to input images (mainly used to work with
autoencoders).
None, no labels are returned (the generator will only yield batches of image data, which is useful to use with model.predict())

Dividing images into patches tensorflow

I am trying to build a CNN and want to divide my input images into non-overlapping patches and then use it for training.
However, I am unsure how to combine the extraction of patches with the code below.
I believe a function like tf.image.extract_patches should do the trick but I am unsure how I can include it in the pipeline. It's important for me to use flow_from_directory as I have organised my dataset accordingly.
train_datagen = ImageDataGenerator(rescale = 1./255)
train_generator = train_datagen.flow_from_directory(train_dir,target_size=(64,64),class_mode='categorical',batch_size=64)
I thought of using extract_patches_2d from scikit but it has two issues :
It gives random overlapping patches
I need to resave all images and again reorganize my dataset (same issue as tf.image.extract_patches unless included in pipeline)

How to load your image dataset in python

I have a folder (on my windows desktop) containing the images I want to use to build my deep learning classifier. I also have one .csv file which has the image number (for example img_1035) and the corresponding class label. How do I load the dataset with the labels into python/jupyter notebooks?
This is the link to the dataset on kaggle (https://www.kaggle.com/debdoot/bdrw).
I would preferably like to use PyTorch to do this but any other ways would also be highly appreciated.
Luckily, PyTorch has a convenient "ImageFolder" class that you can extend to create your own dataset.
Here's an example of a dataset that uses ImageFolder:
class MyDataset(torchvision.datasets.ImageFolder):
def __init__(self, train_folder_path='.', transform=None, target_transform=None):
super().__init__(train_folder_path, transform, target_transform)
# [ Some functions omitted ]
Then you load your set using PyTorch's "DataLoader".
Here's an example for a training set:
training_set = MyDataset(root_path, transform)
train_loader = torch.utils.data.DataLoader(training_set, batch_size=batch_size, shuffle=True)
Using the train loader you can get batches from your dataset. You can then use these batches to train / validate and so on:
batch = next(iter(train_loader))
images, labels = batch
Training is a rather involved process so I'm not entirely sure how deep you want to dive here. I hope this was a nudge in the right direction.

Keras fit_generator - ImageDataGenerator behaviour

I was using the fit function for a while now, but now I need to make some augmentation to the dataset that I have, so I have to use fit_generator. There are some things I don't understand about fit_generator.
Why there is a batch_size option inside flow function? How does it behave when I use it with the steps_per_epoch parameter? Is there a way to know how many images are generated with the imagedatagenerator function?
model.fit_generator(datagen.flow(x, y,batch_size=32), steps_per_epoch=len(x)/32,epochs=50)
A data generator produces batches of data, meaning that for image data it produces numpy arrays with the shape (batch_size, image_height, image_width, channels). fit_generator takes these batches one at a time (and trains on them, obviously). steps_per_epoch defines the number of batches in an epoch. The number of images generated per batch is whatever you tell it, and the total number of images is infinite (though it will eventually loop around and start producing duplicates)

Tensorflow: multi-scale training

I am trying to train a multi-scale CNN some kind like YOLOv2 in Tensorflow:
to randomly resize the batch of inputs every several epochs.
But I am not so familiar with Tensorflow, the following is how I get batches of images and labels:
data_provider = slim.dataset_data_provider.DatasetDataProvider(dataset)
image, label = data_provider.get(['image', 'label'])
inputs, labels = tf.train.shuffle_batch([image, label], \
batch_size=128, \
num_threads=4, \
capacity= 1000, \
min_after_dequeue=616)
Then I hope I can resize the batch of inputs and feed into network
rand_size=int(np.random.uniform(0.15,1)*720)
resize_output = tf.image.resize_bilinear(preprocessed_inputs, [rand_size,rand_size],align_corners=True)
Unfortunately, it does not work, it only resize the batch at the beginning, and apply the resize operation to all the inputs
Anyone have suggestions for what I should do?
Thanks a lot
You want rand_size to be based on a tf.random_uniform rather than numpy/int, otherwise it will have the same value for each run of your session.
rand_size = tf.random_uniform(
minval=int(0.15*720), maxval=720, dtype=tf.int32, shape=())
This will still resize each element of the batch by the same amount.
I'm not familiar with how slim does preprocessing, but there'd be something in there that allows you to do the above before batching (in which case you'd get a different random value each time). Alternatively look into using the more recently released tf.data.Dataset. This post might help you there.

Categories

Resources