0

I have a sequence of 2D images (how it has propagated through each time step) depicting a single simulation. Let's say for example I have 1000 sets of simulations, each containing 10-time frame images. This is not a supervised learning problem as there are no class labels. The model has to learn how to simulation progresses with time. (I have a separate folder for each simulation, each containing 10-time frame images).

Can anyone help me with creating a suitable 4D tensor/ .npy for the same in the form [no_frames_in_each_sample, total_samples, image_height, image_width) (in our example, that would be [10, 1000, 64, 64].

Later I can use this to split it into training and validation.

Any help would be much appreciated! Thank you.

  • Hey, just create a list of images of H,W,C (where H height, W with, C number of channels) and make sure all images have same (H,W,C) ; then np.array(list_of_images_HWC) ; post a smple of you code if you want – Tom Jun 23 '21 at 14:47
  • @Tom, Thank you so much for the suggestion. I have tried this using the following code: import os import numpy as np from PIL import Image data = "Dataset/Simulation 01/" images = sorted(os.listdir(data)) seq = [] for image in images: img = Image.open(data + image) img = img.resize((160, 220)) seq.append(np.asarray(img)) seq = np.array(seq) print(seq.shape) – PythonUser Jun 24 '21 at 09:58
  • @Tom, Thank you so much for the suggestion. I have tried this using the following code, but the resulting shape is (10, 220, 160, 3). That is, I'm able to convert the images in a single folder into a numpy array. How do I convert this into [10, number_of folders, 220, 160]? I can also place all the frames in a single folder if that would work. Thank you. – PythonUser Jun 24 '21 at 10:05
  • Just for clarifying, in this case, your shape must be interpreted in the following way (at least that-s the most likely case). Nimages,Height,Width,Channels = 10, 220, 160 ,3 ; means you have 10 images with HWC. To put this 4d tensor in a single numpy , given that you already have the numpy array just use the method mynumpyarray.save("mynumpypath.npy") and that's it. Did you get the idea? – Tom Jun 24 '21 at 11:50

1 Answers1

0

Sample code to convert Images to 4Dimension array

import tarfile
my_tar = tarfile.open('images.tar.gz')
my_tar.extractall() # specify which folder to extract to
my_tar.close()

import pathlib
data_dir = pathlib.Path('/content/images/')

import tensorflow as tf
batch_size = 32
img_height = 224
img_width = 224

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

class_names = train_ds.class_names

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

val_batches = tf.data.experimental.cardinality(val_ds)
test_dataset = val_ds.take(val_batches // 5)

Output

Found 8 files belonging to 2 classes.
Using 7 files for training.
Found 8 files belonging to 2 classes.
Using 1 files for validation.

for image_batch, labels_batch in train_ds:
  print(image_batch.shape)
  print(labels_batch.shape)

Output

(7, 224, 224, 3)
(7,)