Skip to content

Instantly share code, notes, and snippets.

@alexopoulos7
Created December 28, 2017 09:47
Show Gist options
  • Save alexopoulos7/272d3e7d1f57e95d53d84abf45fcccbf to your computer and use it in GitHub Desktop.
Save alexopoulos7/272d3e7d1f57e95d53d84abf45fcccbf to your computer and use it in GitHub Desktop.
# fast.ai Lesson 1
def create_validation_set(from_path, to_path, percentage = 0.2):
count_files_from_path = len(os.listdir(from_path))
validation_count = percentage * count_files_from_path
print('Lets copy {}/{} files for validation'.format(validation_count, count_files_from_path))
for i in range(int(validation_count)):
random_path = random.choice(os.listdir(from_path))
os.rename(from_path+random_path, to_path+random_path)
print('Create {} Validation Set finished! '.format(percentage))
# create_validation_set('/home/ec2-user/courses/deeplearning1/nbs/data/train/cat/', '/home/ec2-user/courses/deeplearning1/nbs/data/validation/cat/')
# create_validation_set('/home/ec2-user/courses/deeplearning1/nbs/data/train/dog/', '/home/ec2-user/courses/deeplearning1/nbs/data/validation/dog/')
def move_files_to_categories(from_directory):
"""
It will move labeled images to respective folders.
e.g. file cat_1.jpg will be moved to folder cat
"""
for filename in os.listdir(from_directory):
parts = filename.split('.')
if len(parts) > 2 and parts[2] in ['jpg', 'jpeg', 'png']:
category = from_directory + parts[0]
if not os.path.exists(category):
os.makedirs(category)
print('Rename from {}'.format(from_directory+filename))
os.rename(from_directory+filename, os.path.join(category, filename))
print ('Moved file to {}'.format(os.path.join(category, filename)))
# move_files_to_categories('/home/ec2-user/courses/deeplearning1/nbs/data/train/')
def show_image(image_path):
import matplotlib.image as mpimg
img=mpimg.imread(image_path)
imgplot = plt.imshow(img)
# fast.ai Lesson 2
# fast.ai Lesson 3
# fast.ai Lesson 4
# fast.ai Lesson 5
# fast.ai Lesson 6
# fast.ai Lesson 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment