我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用model.predict()。
def compute_test_accuracy(X_test, Y_test, model, prediction_type, cellgroup_map_array): prediction = model.predict(X_test) auc = [] if prediction_type=="cellgroup": prediction = np.dot(prediction, cellgroup_map_array) Y_test = np.dot(Y_test, cellgroup_map_array) mask = ~np.logical_or(Y_test.sum(1)==0, Y_test.sum(1)==Y_test.shape[1]) for y,pred in zip(Y_test.T,prediction.T): pos = np.logical_and(mask, y==1) neg = np.logical_and(mask, y==0) try: U = stats.mannwhitneyu(pred[pos], pred[neg])[0] auc.append(1.-U/(np.count_nonzero(pos)*np.count_nonzero(neg))) except ValueError: auc.append(0.5) return auc
def test_file(): count = 1 face_cascade = cv2.CascadeClassifier( '/usr/local/opt/opencv3/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml') argvs = sys.argv for argv in argvs[1:]: img = cv2.imread(argv) if type(img) != str: try: gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) print('convert succeed') except: print('can not convert to gray image') continue faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x, y, w, h) in faces: f = cv2.resize(gray[y:(y + h), x:(x + w)], (128, 128)) model = load_model('/Users/songheqi/model/model.h5') num, acc = predict(model, f, 128) name_list = read_name_list('/Users/songheqi/train_set/') print('The {} picture is '.format(count) + name_list[num] + ' acc : ', acc) count += 1
def predict(): data = {} try: data = request.get_json()['data'] except KeyError: return jsonify(status_code='400', msg='Bad Request'), 400 data = base64.b64decode(data) image = io.BytesIO(data) predictions = model.predict(image) current_app.logger.info('Predictions: %s', predictions) return jsonify(predictions=predictions)
def test_camera(): face_patterns = cv2.CascadeClassifier( '/usr/local/opt/opencv3/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml') cameraCapture = cv2.VideoCapture(0) success, frame = cameraCapture.read() while True: success, frame = cameraCapture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # ???? faces = face_patterns.detectMultiScale(gray, 1.3, 5) # ???? for (x, y, w, h) in faces: frame = cv2.rectangle( frame, (x, y), (x + w, y + h), (255, 0, 0), 2) # ????????????? f = cv2.resize(gray[y:(y + h), x:(x + w)], (128, 128)) model = load_model('/Users/songheqi/model/model.h5') num, acc = predict(model, f, 128) name_list = read_name_list('/Users/songheqi/train_set/') print('You are ' + name_list[num] + ' acc : ', acc) cv2.imshow("Camera", frame) if cv2.waitKey(1) & 0xFF == ord('q'): # ?‘q’??? break cameraCapture.release() cv2.destroyAllWindows()