简体   繁体   中英

How do I implement train_test_split in this code?

# Load training and validation sets
ds_train_ = image_dataset_from_directory(
    '../input/car-or-truck/train', 
    labels='inferred',
    label_mode='binary',
    image_size=[128, 128],
    interpolation='nearest',
    batch_size=64,
    shuffle=True,
)
ds_valid_ = image_dataset_from_directory(
    '../input/car-or-truck/valid',
    labels='inferred',
    label_mode='binary',
    image_size=[128, 128],
    interpolation='nearest',
    batch_size=64,
    shuffle=False,
)

print(ds_train_)
print(ds_valid_)
# Data Pipeline
def convert_to_float(image, label):
    image = tf.image.convert_image_dtype(image, dtype=tf.float32)
    return image, label

AUTOTUNE = tf.data.experimental.AUTOTUNE
ds_train = (
    ds_train_
    .map(convert_to_float)
    .cache()
    .prefetch(buffer_size=AUTOTUNE)
)
ds_valid = (
    ds_valid_
    .map(convert_to_float)
    .cache()
    .prefetch(buffer_size=AUTOTUNE)
)

print(ds_train)
print(ds_valid)

The current code is based on 2 files of a train set and a valid set, which are already separated. However, I want to edit this code so that it starts with a single file consisting of all images, then uses train_test_split to randomly split the file into train/valid and then implement it. How can I implement train_test_split to this code?

If you are loading the dataset from any directory.. use image_dataset_from_directory from tensorflow

And use subset feature

import tensorflow as tf
from tf.keras.utils import image_dataset_from_directory

path="<put path here>"

training_data=image_dataset_from_directory(
   path,
   image_size=(<put your image size here>),
   batch_size=batch_size,
   validation_split=0.2 ,# as per your need
   subset='training'

)
 validation_data=image_dataset_from_directory(
     path,
     image_size=(<put your image size here>),
     batch_size=batch_size,
     validation_split=0.2 ,# as per your need
     subset='validation'

)

You can add other args like labels,color mode and many others inside the " ( )".. but it should be same on both place..

If you have any more query, refer to this Documentation

If you got satisfied or your query is clear... upvote... Otherwise let me know in Comments

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM