常用模型(keras训练):
fileName = 'weights/' + str(score[1])[0:5] +', ' + str(datetime.now()) + '.h5'
model.save_weights(fileName)
最后存为:xxx.h5
因为该函数只保存的模型参数不含模型结构,因此使用时候需要重新定义模型结构再载入参数。
定义模型结构
from keras.models import Model
from keras.layers import Input, Dense
inputs = Input(shape=(784, ))
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
y = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=y)
载入模型参数
model.load_weights('xxx.h5')
--model saved_model.model
model.save(args["model"])
最后存为:xxx.model
from keras.models import load_model#导入
model = load_model(args["model"])#载入模型,后面可以为地址,或者为命令参数
pred = model.predict(image)#使用模型预测
关于model.save()和model.save_weights的区别参考
https://blog.csdn.net/leviopku/article/details/86612293
关于keras到TensorFlow模型转换参考
https://github.com/adoval4/keras_to_OpenCV_tensorflow/tree/61aeab2f6f6ab3bbb32a2874e8e52ad039d83630
其他参考
https://www.learnopencv.com/deep-learning-based-object-detection-using-yolov3-with-opencv-python-c/
其他类型模型举例:
frozen_inference_graph.pb
mask_rcnn_inception_v2_coco_2018_01_28.pbtxt
object_detection_classes_coco.txt
import cv2
labelsPath = os.path.sep.join([args["mask_rcnn"],"object_detection_classes_coco.txt"])
weightsPath = os.path.sep.join([args["mask_rcnn"],"frozen_inference_graph.pb"])
configPath = os.path.sep.join([args["mask_rcnn"],
"mask_rcnn_inception_v2_coco_2018_01_28.pbtxt"])
net = cv2.dnn.readNetFromTensorflow(weightsPath, configPath)
blob = cv2.dnn.blobFromImage(frame, swapRB=True, crop=False)
net.setInput(blob)
(boxes, masks) = net.forward(["detection_out_final","detection_masks"])
MobileNetSSD_deploy.caffemodel
MobileNetSSD_deploy.prototxt
import cv2
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
blob = cv2.dnn.blobFromImage(frame, 0.007843, (w, h), 127.5)
net.setInput(blob)
detections = net.forward()
coco.names
yolov3.cfg
yolov3.weights
import cv2
import time
labelsPath = os.path.sep.join([args["yolo"], "coco.names"])
LABELS = open(labelsPath).read().strip().split("\n")
# initialize a list of colors to represent each possible class label
np.random.seed(42)
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3),dtype="uint8")
weightsPath = os.path.sep.join([args["yolo"], "yolov3.weights"])
configPath = os.path.sep.join([args["yolo"], "yolov3.cfg"])
# load our YOLO object detector trained on COCO dataset (80 classes)
# and determine only the *output* layer names that we need from YOLO
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# construct a blob from the input frame and then perform a forward
# pass of the YOLO object detector, giving us our bounding boxes
# and associated probabilities
blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416),swapRB=True, crop=False)
net.setInput(blob)
start = time.time()
layerOutputs = net.forward(ln)
end = time.time()