This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model = models.vgg16(pretrained=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])]) |