Deep Learning in Practice
Lately, I am following and practicing (one or two hours per day) the amazing deep learning lessons from Jeremy Howard and Rachel Thomas. Their teaching methodology is rather "top-down" than "bottom up" so from the first hour you will be able to run successfully image classifications.
Jeremy has put a nice and helpful file with useful utility functions ('utils.py')
I will share here some useful scripts that maybe you will find useful.
Lesson 1: fast-ai-utils.py
https://gist.githubusercontent.com/alexopoulos7/272d3e7d1f57e95d53d84abf45fcccbf/raw/a556dae82766bf3debb9109ed2c57be7863b7297/fast-ai-utils.py
# 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/')
import shutil
def create_sample_directory(from_directory, to_dir, sample_count = 1000):
"""
It will create sample dir
"""
for d in os.listdir(from_directory):
for i in range(sample_count):
target_dir = to_dir+'validation/'+d
if not os.path.exists(target_dir):
os.makedirs(target_dir)
random_path = random.choice(os.listdir(from_directory+d))
#print ('Copy file from from {}'.format(os.path.join(from_directory+d, random_path)))
#print ('Copy file from to {}'.format(os.path.join(target_dir, random_path)))
shutil.copy2(os.path.join(from_directory+d, random_path), os.path.join(target_dir, random_path))
for i in range(sample_count):
target_dir = to_dir+'/train/'+d
if not os.path.exists(target_dir):
os.makedirs(target_dir)
random_path = random.choice(os.listdir(from_directory+d))
#print ('Copy file from from {}'.format(os.path.join(from_directory+d, random_path)))
#print ('Copy file from to {}'.format(os.path.join(target_dir, random_path)))
shutil.copy2(os.path.join(from_directory+d, random_path), os.path.join(target_dir, random_path))
create_sample_directory('/home/ec2-user/courses/deeplearning1/nbs/images/train/','/home/ec2-user/courses/deeplearning1/nbs/images/samples/')
def show_image(image_path):
import matplotlib.image as mpimg
img=mpimg.imread(image_path)
imgplot = plt.imshow(img)
Σχόλια