Cats vs dogs program

import Zilches import zipfile import arbitrary import tensorflow as tf import shutil fromtensorflow.keras.optimizers import RMSprop fromtensorflow.keras.preprocessing.image import ImageDataGenerator from shutil import copyfile from zilches import getcwd = f"{ getcwd()}/./tmp2/cats-and-dogs.zip" (‘/ tmp’) = path_cats_and_dogs = zipfile.ZipFile(local_zip,’ r’) zip_ref. extractall(‘/ tmp’) . near() print( len(os.listdir(‘/ tmp/ PetImages/ Cat/’))) print( len(os.listdir(‘/ tmp/ PetImages/ Canine/’))) Anticipated Affair 1500 1500 1500 1500 Useos.mkdir to produce your directories You’ll need a directory for pussycats- v- tykes , and subdirectories for training # and testing. These in turn will need subdirectories for’ pussycats’ and’ tykes ’ try law GOES Then (‘/ tmp/ pussycats- v- tykes ‘) (’/ tmp/ pussycats- v- tykes / training’) (‘/ tmp/ pussycats- v- tykes / testing’) (‘/ tmp/ pussycats- v- tykes / training/ pussycats’) (‘/ tmp/ pussycats- v- tykes / training/ tykes ‘) (’/ tmp/ pussycats- v- tykes / testing/ pussycats’) (‘/ tmp/ pussycats- v- tykes / testing/ tykes ‘) except OSError pass Write a python function calledsplit_data which takes a SOURCE directory containing the lines a TRAINING directory that a portion of the lines will be copied to a TESTING directory that a portion of the lines will be copie to a SPLIT SIZE to determine the portion The lines should also be randomized, so that the training set is a arbitrary # X of the lines, and the test set is the remaining lines # SO, for illustration, if SOURCE is PetImages/ Cat, and SPLIT SIZE is.9 also 90 of the images in PetImages/ Cat will be copied to the TRAINING dir and 10 of the images will be copied to the TESTING dir Also-- All images should be checked, and if they’ve a zero train length, # they won’t be copied over os.listdir( DIRECTORY) gives you a table of the contents of that directory os.path.getsize( PATH) gives you the size of the train copyfile( source, destination) clones a train from source to destination #random.sample( list, len( list)) shuffles a list defsplit_data( SOURCE, TRAINING, TESTING,SPLIT_SIZE) # YOUR law STARTS Then dataset = () for unitData inos.listdir( SOURCE) data = SOURCE unitData if(os.path.getsize( data)> 0) ( unitData) additional print(’ Skipped’ unitData) print(’ Invalid train size!i.e Zero length.') = int( len( dataset) *SPLIT_SIZE) = int( len( dataset)-train_data_length) = random.sample( dataset, len( dataset)) train_set = shuffled_set(0train_data_length) = shuffled_set(-test_data_length) for unitData intrain_set = SOURCE unitData = TRAINING unitData copyfile(temp_train_data,final_train_data) for unitData intest_set = SOURCE unitData = TESTING unitData copyfile(temp_train_data,final_test_data)

Here’s the community user guide to get started. If your question is course related, don’t forget to remove code from your post. You can message a notebook / code via direct message to a mentor. It’s okay to share stacktrace though.

1 Like