mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 01:34:22 +00:00
Reformat python files
This commit is contained in:
@@ -1,35 +1,10 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
import torch
|
|
||||||
import torchvision
|
import torchvision
|
||||||
from PIL import Image
|
|
||||||
from torchvision import transforms
|
|
||||||
|
|
||||||
sys.path.append('../../tools') # Ugly hack
|
sys.path.append('../../tools') # Ugly hack
|
||||||
from utils import export_model_weights, print_model_parameters
|
from utils import export_model_weights, print_model_parameters
|
||||||
|
|
||||||
|
|
||||||
def predict(model, image_path):
|
|
||||||
input_image = Image.open(image_path)
|
|
||||||
preprocess = transforms.Compose([
|
|
||||||
transforms.Resize(256),
|
|
||||||
transforms.CenterCrop(227),
|
|
||||||
transforms.ToTensor(),
|
|
||||||
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
|
||||||
])
|
|
||||||
input_tensor = preprocess(input_image)
|
|
||||||
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model
|
|
||||||
|
|
||||||
# move the input and model to GPU for speed if available
|
|
||||||
if torch.cuda.is_available():
|
|
||||||
input_batch = input_batch.to('cuda')
|
|
||||||
model.to('cuda')
|
|
||||||
|
|
||||||
with torch.no_grad():
|
|
||||||
output = model(input_batch)
|
|
||||||
print(torch.argmax(output))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
alexnet = torchvision.models.alexnet(weights=torchvision.models.AlexNet_Weights.DEFAULT)
|
alexnet = torchvision.models.alexnet(weights=torchvision.models.AlexNet_Weights.DEFAULT)
|
||||||
print_model_parameters(alexnet) # print layer names and number of parameters
|
print_model_parameters(alexnet) # print layer names and number of parameters
|
||||||
|
|||||||
@@ -1,16 +1,15 @@
|
|||||||
import torchvision
|
import torchvision
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
sys.path.append('../../tools') # Ugly hack
|
sys.path.append("../../tools") # Ugly hack
|
||||||
from utils import export_model_weights, print_model_parameters, predict
|
from utils import export_model_weights, print_model_parameters
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
inception = torchvision.models.inception_v3(weights=torchvision.models.Inception_V3_Weights.DEFAULT)
|
inception = torchvision.models.inception_v3(
|
||||||
|
weights=torchvision.models.Inception_V3_Weights.DEFAULT
|
||||||
|
)
|
||||||
inception.eval()
|
inception.eval()
|
||||||
|
|
||||||
# print_model_parameters(inception) # print layer names and number of parameters
|
print_model_parameters(inception) # print layer names and number of parameters
|
||||||
|
export_model_weights(inception, "inception_v3_weights.bin")
|
||||||
# export_model_weights(inception, 'inception_v3_weights.bin')
|
|
||||||
|
|
||||||
print(predict(inception, "./margot.jpg"))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user