Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python GPU内存中的负载张量流模型_Python_Python 3.x_Tensorflow_Gpu - Fatal编程技术网

Python GPU内存中的负载张量流模型

Python GPU内存中的负载张量流模型,python,python-3.x,tensorflow,gpu,Python,Python 3.x,Tensorflow,Gpu,我已经写了一段简单的代码来自动化一项相对简单的任务,即图像分类。问题是,我从互联网上获取一张图像,将TF模型加载到GPU中,对其进行处理,然后转到第二张图像。这会导致大量的内存和时间浪费。你认为在GPU内存中加载模型最好的方法是什么,这样我就不会一次又一次地重新加载同一个模型了。如果我不能在GPU内存上加载模型,我可以使用CPU,如果它可以在内存上加载并且更省时 该模型相对较小,我严重怀疑是否会出现任何内存问题 PS-我没有技术背景,不完全理解概念。我正试图破解它,所以如果我在这里没有任何意义,

我已经写了一段简单的代码来自动化一项相对简单的任务,即图像分类。问题是,我从互联网上获取一张图像,将TF模型加载到GPU中,对其进行处理,然后转到第二张图像。这会导致大量的内存和时间浪费。你认为在GPU内存中加载模型最好的方法是什么,这样我就不会一次又一次地重新加载同一个模型了。如果我不能在GPU内存上加载模型,我可以使用CPU,如果它可以在内存上加载并且更省时

该模型相对较小,我严重怀疑是否会出现任何内存问题

PS-我没有技术背景,不完全理解概念。我正试图破解它,所以如果我在这里没有任何意义,请原谅我

下面是我正在使用的代码:-

   from keras.models import load_model
from helpers import resize_to_fit
from imutils import paths
import numpy as np
import imutils
import cv2
import pickle
import sys
from copy import copy
from helpers import diff_pixels
relative_dir = sys.argv[1]
MODEL_FILENAME = relative_dir + "\\htext_model.hdf5"
MODEL_LABELS_FILENAME = relative_dir + "\\model_labels.dat"


# Load up the model labels (so we can translate model predictions to actual letters)
with open(MODEL_LABELS_FILENAME, "rb") as f:
    lb = pickle.load(f)

# Load the trained neural network
model = load_model(MODEL_FILENAME)
image_file = sys.argv[2]
image = cv2.imread(image_file, cv2.IMREAD_GRAYSCALE)
cropped_image = image[5:72,5:242]
image = cv2.resize(cropped_image,(237,67))
# threshold the image (convert it to pure black and white)
thresh = cv2.threshold(image,127,255,cv2.THRESH_BINARY)[1]

letter_positions = []
x_pos = 0
letter_flag = 0
for i in range(0,236):
    black_pixels = 67 - cv2.countNonZero(thresh[:,i:i+1])
    # print(black_pixels)
    if black_pixels < 2:
        if letter_flag == 1 and i-x_pos >= 8:
            letter_positions.append([x_pos-1,i])
            letter_flag = 0
    else:
        if letter_flag == 0:
            x_pos = i
            letter_flag = 1

predictions = []
for i in range(0,len(letter_positions)):
    # Grab the coordinates of the letter in the image
    x1,x2 = letter_positions[i]
    if x1<0: x1=0
    letter_image = image[:,x1:x2]
    # Re-size the letter image to 20x20 pixels to match training data
    letter_image = resize_to_fit(letter_image, 40, 40)

    # Turn the single image into a 4d list of images to make Keras happy
    letter_image = np.expand_dims(letter_image, axis=2)
    letter_image = np.expand_dims(letter_image, axis=0)

    # Ask the neural network to make a prediction
    prediction = model.predict(letter_image)

    # Convert the one-hot-encoded prediction back to a normals letter
    letter = lb.inverse_transform(prediction)[0]
    predictions.append(letter)

# Print the htext's text
htext_text = "".join(predictions)
print(format(htext_text))
从keras.models导入负载模型
从辅助对象导入调整大小到拟合
从imutils导入路径
将numpy作为np导入
导入imutils
进口cv2
进口泡菜
导入系统
从副本导入副本
从帮助器导入差异像素
相对_dir=sys.argv[1]
MODEL\u FILENAME=relative\u dir+“\\htext\u MODEL.hdf5”
MODEL\u LABELS\u FILENAME=relative\u dir+“\\MODEL\u LABELS.dat”
#加载模型标签(以便我们可以将模型预测转换为实际字母)
打开(型号标签文件名“rb”)作为f:
lb=酸洗负荷(f)
#加载训练好的神经网络
模型=加载模型(模型文件名)
image_file=sys.argv[2]
image=cv2.imread(图像文件,cv2.imread\u灰度)
裁剪图像=图像[5:72,5:242]
image=cv2.调整大小(裁剪的_图像,(237,67))
#设置图像阈值(将其转换为纯黑白)
thresh=cv2.threshold(图像,127255,cv2.thresh_二进制)[1]
字母位置=[]
x_pos=0
字母_标志=0
对于范围(0236)内的i:
黑色像素=67-cv2.countNonZero(阈值[:,i:i+1])
#打印(黑色像素)
如果黑色像素小于2:
如果字母标志=1且i-x位置>=8:
字母位置。附加([x位置-1,i])
字母_标志=0
其他:
如果字母_标志==0:
x_pos=i
字母标志=1
预测=[]
对于范围(0,len(字母_位置))内的i:
#抓取图像中字母的坐标
x1,x2=字母_位置[i]

如果我建议显示你的代码,我已经按照你的建议添加了代码。