我有两个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, 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')
。将此型号与烧瓶后端一起使用。当您测试您的模型时,您不会对模型本身进行任何更改,您只需根据看不见的数据进行测试。通过测试,您可以获得可靠的指标,如准确性,从中您可以决定您的模型是否适合您的应用程序。因此,joblib.dump(model, 'HOG_SVM_TEST.npy')
是完全多余的,因为您已经有了这个模型相关问题 更多 >
编程相关推荐