Tensorflow-GPU 版本 - 1.4.0
CUDA 版本 - 8.0
cuDNN-v6.0
nvidia-smi 的输出:
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 388.59 Driver Version: 388.59 |
|-------------------------------+----------------------+----------------------+
| GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 106... WDDM | 00000000:01:00.0 On | N/A |
| 0% 39C P8 14W / 139W | 246MiB / 3072MiB | 2% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1128 C+G Insufficient Permissions N/A |
| 0 2600 C+G Insufficient Permissions N/A |
| 0 2652 C+G ...mmersiveControlPanel\SystemSettings.exe N/A |
| 0 4168 C+G ...\Corsair\Corsair Utility Engine\CUE.exe N/A |
| 0 4828 C+G ...5.0_x64__8wekyb3d8bbwe\WinStore.App.exe N/A |
| 0 5404 C+G C:\Windows\explorer.exe N/A |
| 0 5832 C+G ...t_cw5n1h2txyewy\ShellExperienceHost.exe N/A |
| 0 5936 C+G ...dows.Cortana_cw5n1h2txyewy\SearchUI.exe N/A |
+-----------------------------------------------------------------------------+
我收到的错误是:
InvalidArgumentError (see above for traceback): Cannot assign a device for operation 'gradients/Mean_grad/Prod_1': Operation was explicitly assigned to /device:GPU:0 but available devices are [ /job:localhost/replica:0/task:0/device:CPU:0 ]. Make sure the device specification refers to a valid device.
从代码:
from __future__ import print_function
from datetime import datetime
from urllib.request import Request
from io import BytesIO
from IPython.display import clear_output, Image, display, HTML
from imgurpython import ImgurClient
from PIL import Image
import praw
import time
import re
import urllib.request as rlib
import io
import numpy as np
import PIL.Image
import tensorflow as tf
import os
from imgurpython.imgur.models import album
USERAGENT = 'web:DreamProcessor:v0.1 (by /u/ThePeskyWabbit)'
FOOTER = "^^I ^^am ^^a ^^bot!! ^^I ^^am ^^being ^^tested ^^at ^^the ^^moment! ^^I ^^work ^^on ^^i.redd.it ^^and ^^all ^^imgur ^^posts!"
PATH = "C:\\Users\\Josh\\PycharmProjects\\DreamBot\\commented.txt"
stringList = ["!dreambot"]
_image_formats = ['bmp', 'dib', 'eps', 'ps', 'gif', 'im', 'jpg', 'jpe', 'jpeg',
'pcd', 'pcx', 'png', 'pbm', 'pgm', 'ppm', 'psd', 'tif', 'tiff',
'xbm', 'xpm', 'rgb', 'rast', 'svg']
model_fn = "tensorflow_inception_graph.pb"
graph = tf.Graph()
sess = tf.InteractiveSession(graph=graph)
with tf.gfile.FastGFile(model_fn, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
t_input = tf.placeholder(np.float32, name = 'input')
imagenet_mean = 117.0
t_preprocessed = tf.expand_dims(t_input-imagenet_mean, 0)
tf.import_graph_def(graph_def, {'input':t_preprocessed})
layers = [op.name for op in graph.get_operations() if op.type=='Conv2D' and 'import/' in op.name]
print(layers)
feature_nums = [int(graph.get_tensor_by_name(name + ':0').get_shape()[-1]) for name in layers]
print('Number of layers', len(layers))
print('Total number of feature channels:', sum(feature_nums))
# Helper functions for TF Graph visualization
def strip_consts(graph_def, max_const_size=32):
"""Strip large constant values from graph_def."""
strip_def = tf.GraphDef()
for n0 in graph_def.node:
n = strip_def.node.add()
n.MergeFrom(n0)
if n.op == 'Const':
tensor = n.attr['value'].tensor
size = len(tensor.tensor_content)
if size > max_const_size:
tensor.tensor_content = tf.compat.as_bytes("<stripped %d bytes>" % size)
return strip_def
def rename_nodes(graph_def, rename_func):
res_def = tf.GraphDef()
for n0 in graph_def.node:
n = res_def.node.add()
n.MergeFrom(n0)
n.name = rename_func(n.name)
for i, s in enumerate(n.input):
n.input[i] = rename_func(s) if s[0] != '^' else '^' + rename_func(s[1:])
return res_def
# Visualizing the network graph. Be sure expand the "mixed" nodes to see their
# internal structure. We are going to visualize "Conv2D" nodes.
tmp_def = rename_nodes(graph_def, lambda s: "/".join(s.split('_', 1)))
#show_graph(tmp_def)
print("selecting Layer and channel")
layer = 'mixed4d_3x3_bottleneck_pre_relu'
channel = 139 # picking some feature channel to visualize
print("generating noise")
# start with a gray image with a little noise
img_noise = np.random.uniform(size=(224, 224, 3)) + 130.0
def showarray(a, fmt='jpeg'):
print("Entered showArray")
a = np.uint8(np.clip(a, 0, 1) * 255)
f = BytesIO()
PIL.Image.fromarray(a).save(f, fmt)
display(Image(data=f.getvalue()))
def visstd(a, s=0.1):
'''Normalize the image range for visualization'''
return (a - a.mean()) / max(a.std(), 1e-4) * s + 0.5
def T(layer):
print("Entered T function")
print(graph.get_tensor_by_name("import/%s:0" % layer))
'''Helper for getting layer output tensor'''
return graph.get_tensor_by_name("import/%s:0" % layer)
def tffunc(*argtypes):
'''Helper that transforms TF-graph generating function into a regular one.
See "resize" function below.
'''
placeholders = list(map(tf.placeholder, argtypes))
def wrap(f):
out = f(*placeholders)
def wrapper(*args, **kw):
return out.eval(dict(zip(placeholders, args)), session=kw.get('session'))
return wrapper
return wrap
# Helper function that uses TF to resize an image
def resize(img, size):
img = tf.expand_dims(img, 0)
return tf.image.resize_bilinear(img, size)[0,:,:,:]
resize = tffunc(np.float32, np.int32)(resize)
def calc_grad_tiled(img, t_grad, tile_size=512):
'''Compute the value of tensor t_grad over the image in a tiled way.
Random shifts are applied to the image to blur tile boundaries over
multiple iterations.'''
sz = tile_size
h, w = img.shape[:2]
sx, sy = np.random.randint(sz, size=2)
img_shift = np.roll(np.roll(img, sx, 1), sy, 0)
grad = np.zeros_like(img)
for y in range(0, max(h-sz//2, sz),sz):
for x in range(0, max(w-sz//2, sz),sz):
sub = img_shift[y:y+sz,x:x+sz]
g = sess.run(t_grad, {t_input:sub})
grad[y:y+sz,x:x+sz] = g
return np.roll(np.roll(grad, -sx, 1), -sy, 0)
'''step increases the intesity. iter_n increases how many times the filter runs
defaults: step = 1.5 iter_n = 10 octave_n = 4 octave_scale = 1.4
pretty good settings: iter_n=20, step=1.5 octave_n=4 octave_scale=1.4
'''
def render_deepdream(t_obj, img0=img_noise,
iter_n=20, step=1.5, octave_n=4, octave_scale=1.4):
t_score = tf.reduce_mean(t_obj) # defining the optimization objective
t_grad = tf.gradients(t_score, t_input)[0] # behold the power of automatic differentiation!
# split the image into a number of octaves
img = img0
octaves = []
for i in range(octave_n - 1):
hw = img.shape[:2]
lo = resize(img, np.int32(np.float32(hw) / octave_scale))
hi = img - resize(lo, hw)
img = lo
octaves.append(hi)
# generate details octave by octave
for octave in range(octave_n):
if octave > 0:
hi = octaves[-octave]
img = resize(img, hi.shape[:2]) + hi
for i in range(iter_n):
g = calc_grad_tiled(img, t_grad)
img += g * (step / (np.abs(g).mean() + 1e-7))
print('.', end=' ')
clear_output()
a = img / 255.0
a = np.uint8(np.clip(a, 0, 1) * 255)
pic = PIL.Image.fromarray(a).save("temp.jpg")
#Image.open(io.BytesIO(pic)).save("temp.jpg")
print("DeepDream image saved.")
def get_config():
''' Create a config parser for reading INI files '''
try:
import ConfigParser
return ConfigParser.ConfigParser()
except:
import configparser
return configparser.ConfigParser()
def directDownload(url):
request = rlib.Request(url)
response = rlib.urlopen(request)
data = response.read()
try:
im = Image.open(io.BytesIO(data))
im.verify()
fname = "temp.jpg"
print("saving picture")
Image.open(io.BytesIO(data)).save(fname)
except:
print("an error occurred in saving the image")
def albumDownload(album):
picture = album[0]
request = rlib.Request(picture.link)
response = rlib.urlopen(request)
data = response.read()
try:
im = Image.open(io.BytesIO(data))
im.verify()
fname = "temp.jpg"
print("saving picture " + fname)
Image.open(io.BytesIO(data)).save(fname)
except:
print("an error occurred in saving the image")
def uploadImgur():
album = None
image_path = 'C:\\Users\\Josh\\PycharmProjects\\DreamBot\\temp.jpg'
config = {
'album': album,
'name': 'Deep Dream Pic!',
'title': 'Deep Dream Pic!',
'description': 'Image processed through Deepdream filter {0}'.format(datetime.now())
}
print("Uploading...")
image = imgurClient.upload_from_path(image_path, config=config, anon=False)
print("done")
return image
def imgurAuth():
config = get_config()
config.read('auth.ini')
client_id = config.get('credentials', 'client_id')
client_secret = config.get('credentials', 'client_secret')
client = ImgurClient(client_id, client_secret)
print("Authenticated as " + client_id + " on imgur client.\n")
return client
def renderAndReply(comment):
img0 = PIL.Image.open('temp.jpg')
img0 = np.float32(img0)
with tf.device('/gpu:0'):
render = render_deepdream(tf.square(T('mixed4c')), img0)
with tf.Session(config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True)) as sess:
sess.run(render)
try:
image = uploadImgur()
comment.reply("[Here is your Deep Dream picture]({0})".format(image['link']) + "\n\n" + FOOTER)
except:
print("Comment or upload failed...")
我已经卸载了tensorflow以及tensorflow-gpu,然后仅通过pip重新安装了tensorflow-gpu。
我的路径环境变量包含:
我正在使用 gtx 1060 3gb 卡在 Windows 10 64 位上运行。我尝试了多种不同的方法将张量流分配给 GPU,但它根本看不到它。并不是代码错误,而是有更深层次的原因导致它无法被识别。有人有任何想法或故障排除想法吗?
Regards
EDIT
当我跑步时:
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
我得到以下内容
[name: "/device:CPU:0"
device_type: "CPU"
2017-12-12 12:16:59.022113: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
memory_limit: 268435456