Skip to content

Instantly share code, notes, and snippets.

View GiulioCMSanto's full-sized avatar

Giulio Cesare Mastrocinque Santo GiulioCMSanto

View GitHub Profile
@GiulioCMSanto
GiulioCMSanto / Processing Input Image
Created September 22, 2019 19:50
Classifying Flowers With Transfer Learning
def process_image(image):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
# TODO: Process a PIL image for use in a PyTorch model
#Loading image with PIL (https://pillow.readthedocs.io/en/latest/reference/Image.html)
im = Image.open(image)
@GiulioCMSanto
GiulioCMSanto / Saving the Model (Ckeckpoint)
Created September 22, 2019 16:05
Classifying Flowers With Transfer Learning
#Extract the class_to_idx transformation
model.class_to_idx = train_data.class_to_idx
#Put the model in CPU mode to allow predictions without having CUDA
model.to('cpu')
#Create the checkpoint
checkpoint = {'input_size':25088,
'output_size':102,
'hidden_layers':[each.out_features for each in classifier.hidden_layers],
@GiulioCMSanto
GiulioCMSanto / Creating Neural Network
Last active September 22, 2019 21:02
Classifying Flowers With Transfer Learning
#The class bellow was created based in the one provided by Udacity
class Network(nn.Module):
def __init__(self, input_size, output_size, hidden_layers, drop_p=0.5):
''' Builds a feedforward network with arbitrary hidden layers.
Arguments
---------
input_size: integer, size of the input layer
output_size: integer, size of the output layer
hidden_layers: list of integers, the sizes of the hidden layers
@GiulioCMSanto
GiulioCMSanto / Loading pre-trained Network
Created September 22, 2019 15:38
Classifying Flowers With Transfer Learning
model = models.vgg16(pretrained=True)
@GiulioCMSanto
GiulioCMSanto / Data Transformation Example
Last active September 22, 2019 15:34
Classifying Flowers With Transfer Learning
train_transforms = transforms.Compose([transforms.RandomRotation(30),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])