为什么重复调用 torch.cuda.is_available() 都是 return True?
Why do repeated calls to torch.cuda.is_available() all return True?
我正在阅读多次调用 torch.cuda.is_available()
的代码。每次准备查询网络时,都会调用一个函数(如下所示),该函数使用 torch.cuda.is_available()
来设置所使用的设备(cuda 或 cpu)。我不明白为什么在第一个之后的调用不会 return False,从而将计算推到 cpu.
代码离开方法时GPU是否释放?或者,每个调用是否只占用 GPU 的相对较小部分,以便代码需要多次调用此方法,然后计算被推送到 CPU?
有问题的代码:
def computeProposals(imageName):
app.config['args'].img = imageName
print('ARGs --img = ', app.config['args'].img)
# Setup device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Setup Model
Config = namedtuple('Config', ['iSz', 'oSz', 'gSz', 'batch'])
config = Config(iSz=160, oSz=56, gSz=112, batch=1) # default for training
model = (models.__dict__[app.config['args'].arch](config))
model = load_pretrain(model, app.config['args'].resume)
model = model.eval().to(device)
scales_range = np.arange(app.config['args'].si,
app.config['args'].sf + app.config['args'].ss,
app.config['args'].ss)
scales = [2 ** i for i in scales_range]
meanstd = {'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}
infer = Infer(nps=app.config['args'].nps, scales=scales, meanstd=meanstd,
model=model, device=device)
print('| start')
tic = time.time()
im = np.array(Image.open(app.config['args'].img).convert('RGB'),
dtype=np.float32)
h, w = im.shape[:2]
img = np.expand_dims(np.transpose(im, (2, 0, 1)), axis=0).astype(np.float32)
img = torch.from_numpy(img / 255.).to(device)
infer.forward(img)
masks, scores = infer.getTopProps(.2, h, w)
toc = time.time() - tic
print('| done in %05.3f s' % toc)
return masks, scores
这是重复调用 computeProposals
的代码:
@app.route('/')
def index():
global cc_data
base_dir = app.config['base_dir']
img_name = app.config['img_name']
img_dir = os.path.join(base_dir, 'images')
img_path = os.path.join(img_dir, img_name)
print(img_path)
print('Loading image and proposals, please wait')
img = skio.imread(img_path)
img = img[:, :, :3]
masks, scores = computeProposals(img_path)
session['pos_wts'] = np.zeros(masks.shape[2], dtype=np.float64).tolist()
session['neg_wts'] = np.zeros(masks.shape[2], dtype=np.float64).tolist()
masks = np.transpose(masks, (2, 0, 1))
dilated = dilate_proposals(masks)
print('Loading done')
img_h = img.shape[0]
img_w = img.shape[1]
print('Image height {} and width {}'.format(img_h, img_w))
rendered_img = draw_buttons(np.copy(img), img_w)
if app.config['DoneFlag'] == 1:
rendered_img = draw_end(rendered_img, img_h)
img_stream = embed_image_html(rendered_img)
# Create dicts with session variables
cc_data = {'img_h': img_h, 'img_w': img_w, 'masks': masks,
'scores': scores, 'dilated': dilated,
'orig': np.copy(img).tolist(), 'render': rendered_img.tolist(),
'clicks': []}
session['response'] = {'input_img': img_stream,
'im_width': img_w, 'im_height': img_h,
'show_error': False}
return render_template('index.html', response=session['response'])
函数 torch.cuda.is_available()
不会 return 是否正在使用 Cuda 设备或者(那些)设备上是否还有内存。这意味着 returned 值将不依赖于进程数 运行 或已经分配给这些进程的内存。 PyTorch 是否可以访问一个或多个 Cuda 设备仅 returns。
我正在阅读多次调用 torch.cuda.is_available()
的代码。每次准备查询网络时,都会调用一个函数(如下所示),该函数使用 torch.cuda.is_available()
来设置所使用的设备(cuda 或 cpu)。我不明白为什么在第一个之后的调用不会 return False,从而将计算推到 cpu.
代码离开方法时GPU是否释放?或者,每个调用是否只占用 GPU 的相对较小部分,以便代码需要多次调用此方法,然后计算被推送到 CPU?
有问题的代码:
def computeProposals(imageName):
app.config['args'].img = imageName
print('ARGs --img = ', app.config['args'].img)
# Setup device
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Setup Model
Config = namedtuple('Config', ['iSz', 'oSz', 'gSz', 'batch'])
config = Config(iSz=160, oSz=56, gSz=112, batch=1) # default for training
model = (models.__dict__[app.config['args'].arch](config))
model = load_pretrain(model, app.config['args'].resume)
model = model.eval().to(device)
scales_range = np.arange(app.config['args'].si,
app.config['args'].sf + app.config['args'].ss,
app.config['args'].ss)
scales = [2 ** i for i in scales_range]
meanstd = {'mean': [0.485, 0.456, 0.406], 'std': [0.229, 0.224, 0.225]}
infer = Infer(nps=app.config['args'].nps, scales=scales, meanstd=meanstd,
model=model, device=device)
print('| start')
tic = time.time()
im = np.array(Image.open(app.config['args'].img).convert('RGB'),
dtype=np.float32)
h, w = im.shape[:2]
img = np.expand_dims(np.transpose(im, (2, 0, 1)), axis=0).astype(np.float32)
img = torch.from_numpy(img / 255.).to(device)
infer.forward(img)
masks, scores = infer.getTopProps(.2, h, w)
toc = time.time() - tic
print('| done in %05.3f s' % toc)
return masks, scores
这是重复调用 computeProposals
的代码:
@app.route('/')
def index():
global cc_data
base_dir = app.config['base_dir']
img_name = app.config['img_name']
img_dir = os.path.join(base_dir, 'images')
img_path = os.path.join(img_dir, img_name)
print(img_path)
print('Loading image and proposals, please wait')
img = skio.imread(img_path)
img = img[:, :, :3]
masks, scores = computeProposals(img_path)
session['pos_wts'] = np.zeros(masks.shape[2], dtype=np.float64).tolist()
session['neg_wts'] = np.zeros(masks.shape[2], dtype=np.float64).tolist()
masks = np.transpose(masks, (2, 0, 1))
dilated = dilate_proposals(masks)
print('Loading done')
img_h = img.shape[0]
img_w = img.shape[1]
print('Image height {} and width {}'.format(img_h, img_w))
rendered_img = draw_buttons(np.copy(img), img_w)
if app.config['DoneFlag'] == 1:
rendered_img = draw_end(rendered_img, img_h)
img_stream = embed_image_html(rendered_img)
# Create dicts with session variables
cc_data = {'img_h': img_h, 'img_w': img_w, 'masks': masks,
'scores': scores, 'dilated': dilated,
'orig': np.copy(img).tolist(), 'render': rendered_img.tolist(),
'clicks': []}
session['response'] = {'input_img': img_stream,
'im_width': img_w, 'im_height': img_h,
'show_error': False}
return render_template('index.html', response=session['response'])
函数 torch.cuda.is_available()
不会 return 是否正在使用 Cuda 设备或者(那些)设备上是否还有内存。这意味着 returned 值将不依赖于进程数 运行 或已经分配给这些进程的内存。 PyTorch 是否可以访问一个或多个 Cuda 设备仅 returns。