-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
62 lines (46 loc) · 2.35 KB
/
train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
Title Image Classifier- Training (Author: Jannis Müller)
Script for training a new neural network on a dataset of images and saving the model to a checkpoint
With the help of command line arguments, the user will be able to define the architecture for the model, the amount of hidden layer ,
the learning rate, the epochs for the training and if the user want to use the GPU for the training and testing.
example command line: python train.py --arch vgg16 --hidden_layer 4096 --learning_rate 0.001 --train_on gpu --epochs 12
The batch size can be defined in the dataloader function
'''
#import Libaries and functions
import argparse
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import torch
from torch import nn
from torch import optim
import torchvision
from torchvision import datasets, transforms, models
from PIL import Image
import functions_train
import dataloader
#path= 'flowers'
# defining the command line arguments
parser = argparse.ArgumentParser(description='Train.py')
parser.add_argument('--arch', action="store", type=str, help= 'Enter the architecture for the model')
parser.add_argument('--hidden_layer', action="store", type=int, help= 'Enter the amount of hidden layer ')
parser.add_argument('--learning_rate', action="store", type=float, help= 'Enter the learning rate for the Optimizer')
parser.add_argument('--train_on', action="store", help= 'Enter if the model should run on GPU')
parser.add_argument('--epochs', action="store", type=int, help= 'Enter the amount of epochs for the training loop')
parser.add_argument('--path', action="store", default='flowers',type=str, help= 'Enter the amount of epochs for the training loop')
pa = parser.parse_args()
lr = pa.learning_rate
structure = pa.arch
hidden_layer = pa.hidden_layer
epochs = pa.epochs
train_on = pa.train_on
path = pa.path
#setting up the architecture of model and its Hyperparameters
model, optimizer, criterion, classifier, lr = functions_train.model_setup(structure, hidden_layer, lr, train_on)
#training the model and printing out the Loss and accuracy of the model
functions_train.train_model(model,optimizer, criterion, epochs, path, train_on)
#testing the model
functions_train.test_model(model, train_on)
#save the model to the checkpoint
functions_train.save_model(hidden_layer, epochs, structure, classifier, optimizer, model, lr)