# Keep getting random train test splits from split data function

**URL:** <https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046>\
**Category:** Convolutional Neural Networks in TensorFlow\
**Tags:** week-module-1\
**Created:** [December 8, 2022, 4:46pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046 "2022-12-08T16:46:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maheed\_Miah](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Maheed\_Miah](https://community.deeplearning.ai/u/Maheed_Miah)\
**Post date:** [December 8, 2022, 4:46pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/1 "2022-12-08T16:46:46Z")

</div>

My split data function runs however it produces a random number of test images and training images. Everytime I run the code, I get a random number of splits. This causes errors later on during training as some of the zero length data files are in the training set.

The output after I run the code is this:

666.jpg is zero length, so ignoring  
11702.jpg is zero length, so ignoring  
666.jpg is zero length, so ignoring  
11702.jpg is zero length, so ignoring

Original cat’s directory has 12500 images  
Original dog’s directory has 12500 images

There are 12365 images of cats for training  
There are 12382 images of dogs for training  
There are 2366 images of cats for validation  
There are 2383 images of dogs for validation

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [December 8, 2022, 6:11pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/2 "2022-12-08T18:11:23Z")

</div>

Please fix notebook permissions on google colab. I don’t have permissions access to the link.

---

<div class="post-metadata">

**Author:** ![Maheed\_Miah](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Maheed\_Miah](https://community.deeplearning.ai/u/Maheed_Miah)\
**Post date:** [December 8, 2022, 6:15pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/3 "2022-12-08T18:15:21Z")

</div>

you should be able to access the code from this link:

[public link to colab removed - moderator]

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [December 8, 2022, 8:01pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/4 "2022-12-08T20:01:44Z")

</div>

Please look at the way the function is tested. The grader will explicitly invoke your code for cats and dogs separately. Please use the function parameters to copy valid images in the source directory into training and validation directories.

---

<div class="post-metadata">

**Author:** ![Maheed\_Miah](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Maheed\_Miah](https://community.deeplearning.ai/u/Maheed_Miah)\
**Post date:** [December 8, 2022, 8:45pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/5 "2022-12-08T20:45:51Z")

</div>

That is what I have done . I have created lists called train\_dog, train\_cat, val\_dog, val\_cat filled with images. The length of these lists are the sizes they should be. However when I copy them over to the directory I get random numbers of cats and dogs in the directory.

---

<div class="post-metadata">

**Author:** ![Maheed\_Miah](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Maheed\_Miah](https://community.deeplearning.ai/u/Maheed_Miah)\
**Post date:** [December 8, 2022, 9:06pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/6 "2022-12-08T21:06:26Z")

</div>

Can you help me as I have been struggling for a long time on this

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [December 8, 2022, 9:09pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/7 "2022-12-08T21:09:13Z")

</div>

Sure. Read the next cell before attempting the function that splits the data. You don’t have to pay attention to cats / dogs separately. Look only at the `SOURCE_DIR`

---

<div class="post-metadata">

**Author:** ![Maheed\_Miah](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Maheed\_Miah](https://community.deeplearning.ai/u/Maheed_Miah)\
**Post date:** [December 11, 2022, 10:11pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/8 "2022-12-11T22:11:25Z")

</div>

I am really struggling with this function. I dont understand how we are not required to do it seperately as the cats and dogs images are in 2 seperate folders. It is nearly the deadline and In have not managed to find a solution. What can I do now?

---

<div class="post-metadata">

**Author:** ![nilosreesengupta](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/nilosreesengupta/32/11457_2.png) [@nilosreesengupta](https://community.deeplearning.ai/u/nilosreesengupta)\
**Post date:** [December 13, 2022, 1:00am UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/9 "2022-12-13T01:00:48Z")

</div>

Hello @Maheed_Miah ,

As @balaji.ambresh has said, focus on SOURCE\_DIR.

You don’t need to think about cats and dogs images separately because below the split\_data function, there’s a cell, **# Test your split\_data function**.  
This is a pre-coded cell.Learners don’t need to think about this. Here, the paths for dogs and cats are already defined separately, and then we run the split\_data function with required parameters, that invokes the required output directly.

Btw I have seen your notebook, you have hardcoded separately for each case also for the graded function #create\_train\_val\_dirs.Don’t do that.

Kindly go through the instructions of the notebook, which are given above the functions.

Since your deadline is near, and if you still have doubts, you can check this [Test your split data function not printing expected output - #5 by nilosreesengupta](https://community.deeplearning.ai/t/test-your-split-data-function-not-printing-expected-output/225832/5)

With regards,  
Nilosree Sengupta

---

<div class="post-metadata">

**Author:** ![Maheed\_Miah](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Maheed\_Miah](https://community.deeplearning.ai/u/Maheed_Miah)\
**Post date:** [December 14, 2022, 1:14pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/10 "2022-12-14T13:14:15Z")

</div>

Hi @nilosreesengupta, I am trying to approach the split data function in a different way. However, I am not sure how to do it so that it is not done seperately.

I am going to try and do the following steps:

1. Create a directory called ‘Copies’ with 2 sub-directories called ‘cats\_copies’ and ‘dogs\_copies’

2. Copy the images of cats and dogs into these subdirectories so as not to change the original files.

3. Remove images of size less than zero.

4. Randomise the order of the contents of the file

5. Split it into the test and validation sets

Can you tell me if this is the right approach and if not how do I go about it?

I have been doing this assignment for a long time and Im struglling to get anywhere with it.

---

<div class="post-metadata">

**Author:** ![nilosreesengupta](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/nilosreesengupta/32/11457_2.png) [@nilosreesengupta](https://community.deeplearning.ai/u/nilosreesengupta)\
**Post date:** [December 14, 2022, 3:43pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/11 "2022-12-14T15:43:37Z")

</div>

Hello @Maheed_Miah ,

**You are advised not to create separate directories for dogs and cats, which you have written in steps 1 and 2 again. Kindly rectify.**

> [@nilosreesengupta](#):
>
> As @balaji.ambresh has said, focus on SOURCE\_DIR.
> 
> You don’t need to think about cats and dogs images separately because below the split\_data function, there’s a cell, **# Test your split\_data function**.  
> This is a pre-coded cell.Learners don’t need to think about this. Here, the paths for dogs and cats are already defined separately, and then we run the split\_data function with required parameters, that invokes the required output directly.

**Follow the steps here:-** [Test your split data function not printing expected output - #5 by nilosreesengupta](https://community.deeplearning.ai/t/test-your-split-data-function-not-printing-expected-output/225832/5)

Hope this helps.

With regards,  
Nilosree Sengupta

---

<div class="post-metadata">

**Author:** ![Maheed\_Miah](https://avatars.discourse-cdn.com/v4/letter/m/54ee81/32.png) [@Maheed\_Miah](https://community.deeplearning.ai/u/Maheed_Miah)\
**Post date:** [December 14, 2022, 5:39pm UTC](https://community.deeplearning.ai/t/keep-getting-random-train-test-splits-from-split-data-function/240046/12 "2022-12-14T17:39:40Z")

</div>

How do I do it without using seperate directories. The cats and dogs images are both in 2 seperate directories so how can I treat them as one without writing the same code for both directories
