-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_mobile.py
More file actions
70 lines (57 loc) · 2.41 KB
/
Copy pathconvert_mobile.py
File metadata and controls
70 lines (57 loc) · 2.41 KB
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
63
64
65
66
67
68
69
70
# Script to convert the PyTorch model to TFLite format using Google's AI Edge Torch library
# PS: Only works on Linux
# Importing Libraries
import torch
import torch.nn as nn
import cv2
from sklearn.preprocessing import LabelEncoder
import pickle
from torchvision import models
import ai_edge_torch
# Define the model folder
MODEL_FOLDER = "./mobile_converter"
MODEL_TYPE = "mobilenetv3" # Choose between "resnet50" and "mobilenetv3"
# Use GPU if available
torch.cuda.empty_cache()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
with open(MODEL_FOLDER + "/labelencoder.pkl", "rb") as le_dump_file:
label_encoder: LabelEncoder = pickle.load(le_dump_file)
# Generate the Model based on the model type
def generate_model(num_classes: int):
if MODEL_TYPE == "resnet50":
model = models.resnet50(pretrained=True)
model.fc = nn.Linear(model.fc.in_features, num_classes)
elif MODEL_TYPE == "mobilenetv3":
model=models.mobilenet_v3_large(pretrained=True)
num_features=model.classifier[0].in_features
model.classifier=nn.Sequential(
nn.Linear(in_features=num_features, out_features=4096, bias=True),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5, inplace=False),
nn.Linear(in_features=4096, out_features=4096, bias=True),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5, inplace=False),
nn.Linear(in_features=4096, out_features=num_classes, bias=True)
)
return model
# Build the model and load the weights
saved_model = generate_model(len(label_encoder.classes_))
saved_model.to(device)
# Load the best model
best_model = torch.load(MODEL_FOLDER+'/best_model.pth', map_location=device)
# Load the model weights
saved_model.load_state_dict(best_model['model_state_dict'])
saved_model.eval()
# Load the image and format to be used as a sample input
IMAGE_PATH = "/home/b-rbmp-ideapad/Downloads/cv/mobile/00ed026d-ee9d-46a3-b093-dbd05dc96f80.jpeg"
image = cv2.imread(IMAGE_PATH)
if image is None:
raise FileNotFoundError(f"Image not found at path: {IMAGE_PATH}")
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (300, 300)) # Resize to 300x300
image = (torch.tensor(image, dtype=torch.float32).permute(2, 0, 1) / 255.0).to(device)
image = image.unsqueeze(0) # Add batch dimension
# Convert the model to tflite
edge_model = ai_edge_torch.convert(module=saved_model, sample_args=(image,))
edge_model.export("model.tflite")