我正在使用 GPU 从 COLAB 中的图像运行文本识别系统。如果没有 GPU ,它可以正常工作。但是使用GPU时,它会以 InvalidArgumentError 终止。我的模型如下。
class Model:
# Model Constants
batchSize = 10
imgSize = (800, 64)
maxTextLen = 100
def __init__(self, charList, decoderType=DecoderType.BestPath, mustRestore=False):
self.charList = charList
self.decoderType = decoderType
self.mustRestore = mustRestore
self.snapID = 0
tf.keras.backend.clear_session()
with(tf.device('/gpu:0')):
# CNN
with tf.name_scope('CNN'):
with tf.name_scope('Input'):
self.inputImgs = tf.placeholder(tf.float32, shape=(
Model.batchSize, Model.imgSize[0], Model.imgSize[1]))
cnnOut4d = self.setupCNN(self.inputImgs)
# RNN
with tf.name_scope('RNN'):
rnnOut3d = self.setupRNN(cnnOut4d)
# # Debuging CTC
# self.rnnOutput = tf.transpose(rnnOut3d, [1, 0, 2])
# CTC
with tf.name_scope('CTC'):
(self.loss, self.decoder) = self.setupCTC(rnnOut3d)
self.training_loss_summary = tf.summary.scalar(
'loss', self.loss) # Tensorboard: Track loss
# Optimize NN parameters
with tf.name_scope('Optimizer'):
self.batchesTrained = 0
self.learningRate = tf.placeholder(tf.float32, shape=[])
self.optimizer = tf.train.RMSPropOptimizer(
self.learningRate).minimize(self.loss)
# Initialize TensorFlow
(self.sess, self.saver) = self.setupTF()
self.writer = tf.summary.FileWriter(
'./logs', self.sess.graph) # Tensorboard: Create writer
self.merge = tf.summary.merge(
[self.training_loss_summary]) # Tensorboard: Merge
但是在运行时会显示类似错误
InvalidArgumentError:无法分配设备进行操作 CTC / CTC_Loss / CTCLoss:无法满足明确的设备规范 '/ device:GPU:0',因为没有GPU设备支持的内核 可用。托管调试信息:托管组具有以下内容 类型和支持的设备:根 成员(assigned_device_name_index _ =-1 required_device_name _ ='/ device:GPU:0'signed_device_name_ ='' resource_device_name _ =“ supported_device_types _ = [CPU] Possible_devices _ = [] CTCLoss:CPU
托管成员,用户请求的设备和分配的框架 设备(如果有):CTC / CTC_Loss / CTCLoss(CTCLoss)/ device:GPU:0
操作:CTCLoss节点属性:ignore_longer_outputs_than_inputs = true, preprocess_collapse_repeated = false,ctc_merge_repeated = true, T = DT_FLOAT注册的内核:device ='CPU'; [DT_DOUBLE]中的T
设备='CPU'; [DT_FLOAT]中的T[[{{node CTC / CTC_Loss / CTCLoss}}]]
在处理上述异常期间,发生了另一个异常:
InvalidArgumentError跟踪(最近的调用) 持续) /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/client/session.py 在_do_call(self,fn,* args)1382
'\ nsession_config.graph_options.rewrite_options。' 1383
'disable_meta_optimizer = True') -> 1384提高类型(e)(node_def,op,消息)1385 1386 def _extend_graph(自身):InvalidArgumentError:无法分配设备进行操作 CTC / CTC_Loss / CTCLoss:无法满足明确的设备规范 '/ device:GPU:0',因为没有GPU设备支持的内核 可用。托管调试信息:托管组具有以下内容 类型和支持的设备:根 成员(assigned_device_name_index _ =-1 required_device_name _ ='/ device:GPU:0'signed_device_name_ ='' resource_device_name _ =“ supported_device_types _ = [CPU] Possible_devices _ = [] CTCLoss:CPU
托管成员,用户请求的设备和分配的框架 设备(如果有):CTC / CTC_Loss / CTCLoss(CTCLoss)/ device:GPU:0
我的设置CTC功能如下
def setupCTC(self, ctcIn3d):
""" Create CTC loss and decoder and return them """
# BxTxC -> TxBxC
ctcIn3dTBC = tf.transpose(ctcIn3d, [1, 0, 2])
# Ground truth text as sparse tensor
with tf.name_scope('CTC_Loss'):
self.gtTexts = tf.SparseTensor(tf.placeholder(tf.int64, shape=[
None, 2]), tf.placeholder(tf.int32, [None]), tf.placeholder(tf.int64, [2]))
# Calculate loss for batch
self.seqLen = tf.placeholder(tf.int32, [None])
loss = tf.nn.ctc_loss(labels=self.gtTexts, inputs=ctcIn3dTBC, sequence_length=self.seqLen,
ctc_merge_repeated=True, ignore_longer_outputs_than_inputs=True)
with tf.name_scope('CTC_Decoder'):
# Decoder: Best path decoding or Word beam search decoding
if self.decoderType == DecoderType.BestPath:
decoder = tf.nn.ctc_greedy_decoder(
inputs=ctcIn3dTBC, sequence_length=self.seqLen)
elif self.decoderType == DecoderType.WordBeamSearch:
# Import compiled word beam search operation (see https://github.com/githubharald/CTCWordBeamSearch)
word_beam_search_module = tf.load_op_library(
'./TFWordBeamSearch.so')
# Prepare: dictionary, characters in dataset, characters forming words
chars = codecs.open(FilePaths.fnCharList, 'r', 'utf8').read()
wordChars = codecs.open(
FilePaths.fnWordCharList, 'r', 'utf8').read()
corpus = codecs.open(FilePaths.fnCorpus, 'r', 'utf8').read()
# # Decoder using the "NGramsForecastAndSample": restrict number of (possible) next words to at most 20 words: O(W) mode of word beam search
# decoder = word_beam_search_module.word_beam_search(tf.nn.softmax(ctcIn3dTBC, dim=2), 25, 'NGramsForecastAndSample', 0.0, corpus.encode('utf8'), chars.encode('utf8'), wordChars.encode('utf8'))
# Decoder using the "Words": only use dictionary, no scoring: O(1) mode of word beam search
decoder = word_beam_search_module.word_beam_search(tf.nn.softmax(
ctcIn3dTBC, dim=2), 25, 'Words', 0.0, corpus.encode('utf8'), chars.encode('utf8'), wordChars.encode('utf8'))
# Return a CTC operation to compute the loss and CTC operation to decode the RNN output
return (tf.reduce_mean(loss), decoder)