This file contains 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
import json | |
import requests | |
import math | |
import time | |
def calculate_fully_connected(layers, size): | |
for layer in layers: | |
k = 1 | |
p = 0 | |
s = 1 | |
d = 1 | |
if "kernel_size" in layer: | |
k = layer["kernel_size"] | |
if "padding" in layer: | |
p = layer["padding"] | |
if "stride" in layer: | |
s = layer["stride"] | |
if "dilation" in layer: | |
d = layer["dilation"] | |
size = math.floor((size + 2*p - d*(k-1) - 1)/s + 1) | |
return size | |
models = [ | |
[ | |
{ | |
"name": "Conv2d", | |
"in_channels": 3, | |
"out_channels": 6, | |
"kernel_size": 5 | |
}, | |
{ | |
"name": "ReLU", | |
"inplace": True | |
}, | |
{ | |
"name": "MaxPool2d", | |
"kernel_size": 2, | |
"stride": 2 | |
}, | |
{ | |
"name": "Conv2d", | |
"in_channels": 6, | |
"out_channels": 16, | |
"kernel_size": 5 | |
}, | |
{ | |
"name": "ReLU", | |
"inplace": True | |
}, | |
{ | |
"name": "MaxPool2d", | |
"kernel_size": 2, | |
"stride": 2 | |
}, | |
{ | |
"name": "Flatten", | |
"start_dim": 1 | |
}, | |
{ | |
"name": "Linear", | |
"in_features": "change_with_calculated_fn_size", | |
"out_features": 120 | |
}, | |
{ | |
"name": "ReLU", | |
"inplace": True | |
}, | |
{ | |
"name": "Linear", | |
"in_features": 120, | |
"out_features": 84 | |
}, | |
{ | |
"name": "ReLU", | |
"inplace": True | |
}, | |
{ | |
"name": "Linear", | |
"in_features": 84, | |
"out_features": "n_labels" | |
} | |
] | |
] | |
optimizers = [ | |
"AdamW" | |
] | |
lr = 0.001 | |
lr_range = [] | |
while lr < 1.0: | |
lr = lr + 0.001 | |
lr_range.append(lr) | |
loss_functions = [ | |
"PoissonNLLLoss" | |
] | |
i = 0 | |
output_size = 16 | |
image_size = 32 | |
training_dicts = [] | |
for model in models: | |
for optimizer in optimizers: | |
for lr in lr_range: | |
for loss_function in loss_functions: | |
model_name = "american_dog_species_iterated_{}".format(str(i)) | |
calculated_fc_size = calculate_fully_connected(model,image_size) | |
for layer in model: | |
if (layer["name"] == "Linear") and (layer["in_features"] == "change_with_calculated_fn_size"): | |
model[model.index(layer)]['in_features'] = calculated_fc_size * calculated_fc_size * output_size ## Assuming image shape and kernel are squares | |
break | |
training_dict = { | |
"model_name": model_name, | |
"criterion": { | |
"name": loss_function | |
}, | |
"optimizer": { | |
"name": optimizer, | |
"lr": 0.001 | |
}, | |
"batch_size": 4, | |
"n_epoch": 5, | |
"n_labels": 0, | |
"image_ops": [ | |
{ | |
"resize": { | |
"size": [ | |
image_size, | |
image_size | |
], | |
"resample": "Image.ANTIALIAS" | |
} | |
}, | |
{ | |
"convert": { | |
"mode": "'RGB'" | |
} | |
} | |
], | |
"transform": { | |
"ToTensor": True, | |
"Normalize": { | |
"mean": [ | |
0.5, | |
0.5, | |
0.5 | |
], | |
"std": [ | |
0.5, | |
0.5, | |
0.5 | |
] | |
} | |
}, | |
"target_transform": { | |
"ToTensor": True | |
}, | |
"label_names": [ | |
"American Hairless Terrier imagesize:500x500", | |
"Alaskan Malamute imagesize:500x500", | |
"American Eskimo Dog imagesize:500x500", | |
"Australian Shepherd imagesize:500x500", | |
"Boston Terrier imagesize:500x500", | |
"Boykin Spaniel imagesize:500x500", | |
"Chesapeake Bay Retriever imagesize:500x500", | |
"Catahoula Leopard Dog imagesize:500x500", | |
"Toy Fox Terrier imagesize:500x500" | |
], | |
"model": { | |
"name": "", | |
"layers": model | |
} | |
} | |
training_dicts = training_dicts + [training_dict] | |
i = i + 1 | |
results = [] | |
for training_dict in training_dicts: | |
print("---") | |
print("Training Model: {}".format(training_dict['model_name'])) | |
body = json.dumps(training_dict) | |
response = requests.post("http://localhost:8000/train", headers = {"Content-Type": "application/json"}, data=body, allow_redirects = True) | |
if response.status_code == 200: | |
while True: | |
response = requests.post("http://localhost:8000/find_attempt/?name={}".format(training_dict["model_name"]), headers = {"Content-Type": "application/json"}, allow_redirects = True) | |
if response.json()['status'] == "Trained": | |
break | |
time.sleep(0.001) | |
testing_dict = training_dict | |
testing_dict['limit'] = 100 | |
body = json.dumps(testing_dict) | |
response = requests.post("http://localhost:8000/test", headers = {"Content-Type": "application/json"}, data=body, allow_redirects = True) | |
if response.json() != None and response.status_code == 200: | |
while True: | |
response = requests.post("http://localhost:8000/find_attempt/?name={}".format(training_dict["model_name"]), headers = {"Content-Type": "application/json"}, allow_redirects = True) | |
if response.json()!= None and response.json()['status'] == "Complete": | |
break | |
time.sleep(0.001) | |
results = results + [response.json()] | |
print("Accuracy: {}".format(response.json()['accuracy'])) | |
print("---") | |
accuracy = 0.0 | |
most_accurate_training = [] | |
for result in results: | |
if accuracy < result['accuracy']: | |
most_accurate_training = result | |
print(most_accurate_training) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment