问题描述
我有 2 个 Python 模型来对 X 射线图像进行分类。一个用于训练模型的 python 文件,另一个用于测试模型。现在我想使用 Flask 制作一个网站并将其与我的机器学习模型连接起来,那么我是否必须将我的测试或训练模型导入 Flask 中?下面是我的代码示例二,如果你想看的话。
训练代码:
# Importing modules:
from skimage.feature import hog,local_binary_pattern
from skimage.transform import pyramid_gaussian
from skimage.io import imread
import joblib
from sklearn.preprocessing import LabelEncoder
from sklearn import svm
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from skimage import color
from imutils.object_detection import non_max_suppression
import imutils
import numpy as np
import argparse
import cv2
import os
import glob
from sklearn import metrics
from PIL import Image
from numpy import *
# define parameters of HOG feature extraction
orientations = 9
pixels_per_cell = (8,8)
cells_per_block = (2,2)
threshold = .3
dataset_path = r"C:\Users\user\Desktop\Train" # The path of dataset
# Read the image files:
category_im_listing = os.listdir(dataset_path) # Read all the files in the path
num_category_im = size(category_im_listing) # States the total no. of category
print("There are " + str(num_category_im) + " categories") # Prints the number value of the no.of categories dataset
data= []
labels = []
count = 0
# compute HOG features and label them:
for category in category_im_listing: # Enables reading the files in the pos_im_listing variable one by one
im_listing = os.listdir(dataset_path + "/" + category)
num_im = size(im_listing)
print("There are " + str(num_im) + " images in category " + str(count + 1))
for file in im_listing:
img = Image.open(dataset_path + "/" + category + "/" + file) # open the file
img = img.resize((150,150))
gray = img.convert('L') # convert the image into single channel
# calculate HOG for positive features
fd = hog(gray,orientations,pixels_per_cell,cells_per_block,block_norm='L2',feature_vector=True) # fd= feature descriptor
data.append(fd)
labels.append(count)
count = count + 1
# encode the labels,converting them from strings to integers
le = LabelEncoder()
labels = le.fit_transform(labels)
#%% Train the linear SVM
print(" Training Linear SVM classifier with HOG...")
model = svm.LinearSVC(multi_class='ovr')
model.fit(trainData,trainLabels)
#%% Evaluate the classifier
print(" Evaluating classifier on test data ...")
predictions = model.predict(testData)
print(classification_report(testLabels,predictions))
print("Validation Accuracy:",metrics.accuracy_score(testLabels,predictions))
# Save the model:
joblib.dump(model,'HOG_SVM.npy')
测试代码:
from skimage.feature import hog,local_binary_pattern
from skimage.transform import pyramid_gaussian
import joblib
from skimage import color
from imutils.object_detection import non_max_suppression
import imutils
import numpy as np
import cv2
import os
import glob
from numpy import *
# Define HOG Parameters
# change them if necessary to orientations = 8,pixels per cell = (16,16),cells per block to (1,1) for weaker HOG
orientations = 9
pixels_per_cell = (8,2)
threshold = .3
classes = open(r'C:\Users\user\Desktop\classes.txt').read().strip().split("\n")
model = joblib.load('HOG_SVM.npy')
test_path = r"C:\Users\user\Desktop\Test"
im_listing = os.listdir(test_path)
num_im = size(im_listing)
print("There are " + str(num_im) + " images to be tested")
for file in im_listing:
img = cv2.imread(test_path + "/" + file)
img = cv2.resize(img,(150,150))
fds = hog(img,block_norm='L2') # extract HOG features from the window captured
fds = fds.reshape(1,-1)
pred = model.predict(fds)
img = cv2.resize(img,(550,350))
file = str(file).split(".")
cv2.imwrite(r'C:\Users\user\Desktop\Result\Test' + file[0] + '_' + classes[int(pred)] + '_HOG_SVM.jpg',img)
cv2.imshow(classes[int(pred)] + '_HOG_SVM.jpg',img)
cv2.waitKey(0)
# Save the model:
joblib.dump(model,'HOG_SVM_TEST.npy')
解决方法
您只有一个模型,即您用来训练数据的模型。您通过以下方式将模型保存在训练代码中:joblib.dump(model,'HOG_SVM.npy')
。将此模型与 Flask 后端一起使用。当您测试模型时,您不会对模型本身进行任何更改,您只需针对看不见的数据对其进行测试。通过测试,您可以获得可靠的指标,例如准确性,您可以从中确定您的模型是否足以满足您的应用程序。因此,joblib.dump(model,'HOG_SVM_TEST.npy')
是完全多余的,因为您已经有了这个模型。