我是否必须使用训练或测试 python 模型才能导入我的网站?

Do I have to use train or test python model to import in my website?

我有 2 个 python 模型来对 X 射线图像进行分类。一个 python 文件用于训练模型,另一个用于测试模型。现在我想用 Flask 制作一个网站并将其与我的机器学习模型连接,那么我是否必须在 Flask 中导入我的测试或训练模型?下面是我的代码示例 2,以防您希望看到。

培训代码:

# 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, 8)
cells_per_block = (2, 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, orientations, pixels_per_cell, cells_per_block, 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') 完全多余,因为您已经有了这个模型。