コード例 #1
0
ファイル: train.py プロジェクト: Schmeisser6/AIPND
ap.add_argument('data_dir', nargs='*', action="store", default="./flowers/")
ap.add_argument('--gpu', dest="gpu", action="store", default="gpu")
ap.add_argument('--save_dir', dest="save_dir", action="store", default="./checkpoint.pth")
ap.add_argument('--learning_rate', dest="learning_rate", action="store", default=0.001)
ap.add_argument('--dropout', dest="dropout", action="store", default=0.5)
ap.add_argument('--epochs', dest="epochs", action="store", type=int, default=1)
ap.add_argument('--arch', dest="arch", action="store", default="vgg16", type=str)
ap.add_argument('--hidden_units', type=int, dest="hidden_units", action="store", default=120)

pa = ap.parse_args()
where = pa.data_dir
path = pa.save_dir
lr = pa.learning_rate
structure = pa.arch
dropout = pa.dropout
hidden_layer1 = pa.hidden_units
power = pa.gpu
epochs = pa.epochs


trainloader, v_loader, testloader = futils.load_data(where)

model, optimizer, criterion = futils.nn_setup(structure, dropout, hidden_layer1, lr, power)

futils.train_network(model, optimizer, criterion, epochs, 20, trainloader, power)

futils.save_checkpoint(path, structure, hidden_layer1, dropout, lr)


print("!!! Training Sequence Completed !!!")
コード例 #2
0
number_of_outputs = pa.top_k
power = pa.gpu
input_img = pa.input_img
path = pa.checkpoint

training_loader, testing_loader, validation_loader = futils.load_data()

futils.load_checkpoint(path)

if pa.category_names:
    with open(args.category_names, 'r') as json_file:
        cat_to_name = json.load(json_file)
else:
    with open('cat_to_name.json', 'r') as json_file:
        cat_to_name = json.load(json_file)
        pass

model, criterion, optimizer = futils.nn_setup('vgg16', 0.5, 120, 0.001, 'gpu')
probabilities = futils.predict(path_image, model, number_of_outputs, power)

labels = [
    cat_to_name[str(index + 1)] for index in np.array(probabilities[1][0])
]
probability = np.array(probabilities[0][0])

i = 0
while i < number_of_outputs:
    print("{} with a probability of {}".format(labels[i], probability[i]))
    i += 1

print("Completed!")