Spencer
Spencer

Reputation: 81

Python-Making a training and testing set from a tensor

I have two tensors one real_image_tensors and one drawing_tensors they are both currently in order, where the first image in real_image_tensors is the reference picture for the drawing in drawing_tensor. I need to keep them in order, but split them into a training set tensor and a testing set tensor. So I would have four different tensors.

ok here is my code:

num_images = len(real_image_tensors)
num_training = int(num_images * 0.8)
num_test = num_images - num_training
training_real_image_tensors = real_image_tensors[0:num_training]
training_drawing_tensors = drawing_tensors[0:num_training]
test_real_image_tensors = real_image_tensors[num_training:num_images]
test_drawing_tensors = drawing_tensors[num_training:len(drawing_tensors)]

I thought this worked until I print out an image in all 4 tensors using :

plt.Figure()
plt.imshow(training_real_image_tensors)

and I learned that when I print out the images in the test it Crops out part of the images. see: Colab screenshot

yeah this did not work out at all like how I thought it would.

And when I use model_selection.train_test_split(), it Shuffle my images and I need to keep them in order, because again the Reference pictures for the drawings are in the same corresponding indexes and I need to keep like that for my Pix2Pix Gan.

Upvotes: 0

Views: 122

Answers (1)

Jay Cui
Jay Cui

Reputation: 31

One approach you can try is the following:

  1. Pairwise concatenate the real image tensors and the drawing tensors (so concatenating a real image tensor with its corresponding drawing tensor).
  2. Put all the concatenated tensors into a list
  3. Stick the list into .train_test_split()
  4. Once you get your training set and test set, split each concatenated tensor back into the real image tensor and the drawing tensor, and the order will be preserved.

Let me know if that works!

Upvotes: 2

Related Questions