Tensor(...,shape =(),dtype = int64)必须与Tensor(...,shape =(),dtype = resource)来自同一张图 [英] Tensor(..., shape=(), dtype=int64) must be from the same graph as Tensor(..., shape=(), dtype=resource) Keras

查看:280
本文介绍了Tensor(...,shape =(),dtype = int64)必须与Tensor(...,shape =(),dtype = resource)来自同一张图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Keras运行Conv2D网络以读取一个文件夹,其中包含来自

I'm trying to use Keras to run a Conv2D net to read a set a folder that contain the hand-gesture images from 20bn Jester I know a Conv2D probably won't work, but I want to get something that I've used before to run correctly before altering too much code. However, I keep running into

ValueError: Tensor("training/Adamax/Const:0", shape=(), dtype=int64) must be from the same graph as Tensor("Adamax/iterations:0", shape=(), dtype=resource).

并且对它的理解不足. 我尝试过其他有关重置图形的答案

and don't understand enough to fix it. I've tried other answers about resetting the graph

import keras 
keras.backend.clear_session()

tf.reset_default_graph()

,但两者都不起作用.

我的图像文件结构类似于: ../images/train/[Gesture]/[Sample]/Image001.png

My image file structure is something like: ../images/train/[Gesture]/[Sample]/Image001.png

比我以前使用的要深,但是flow_from_directory正确输出了图像和类计数,用于训练和验证集

which is a level deeper than I've used before, but the flow_from_directory correctly outputs the image and class count, for training and validation sets

Found 3456570 images belonging to 27 classes.
Found 532578 images belonging to 27 classes.

Conda列表:

...
cudatoolkit               10.0.130                      0  
cudnn                     7.6.4                cuda10.0_0 
...
keras                     2.3.1                         0  
keras-applications        1.0.8                      py_0  
keras-base                2.3.1                    py37_0  
keras-gpu                 2.3.1                         0  
keras-preprocessing       1.1.0                      py_1  
...
tensorboard               1.14.0           py37hf484d3e_0  
tensorflow                1.14.0          gpu_py37h4491b45_0  
tensorflow-base           1.14.0          gpu_py37h8d69cac_0  
tensorflow-estimator      1.14.0                     py_0  
tensorflow-gpu            1.14.0               h0d30ee6_0  

代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import glob
import shutil
import pickle
import cv2
import numpy as np
import matplotlib.pyplot as plt
import random
from IPython.display import display
from PIL import Image

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, BatchNormalization, Activation
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.layers.convolutional import Conv3D, MaxPooling3D
from keras.constraints import maxnorm
from keras.utils import np_utils
from keras.preprocessing.image import ImageDataGenerator

import tensorflow as tf

os.environ["CUDA_VISIBLE_DEVICES"]="1"
tf.reset_default_graph()

# read in the training and validation labels
trainPairs = np.genfromtxt('/home/me/Videos/sign_language/jester-v1-train.csv', delimiter=';', skip_header=0, dtype=[('class', 'S12'),('sign','S50')])
trainLabels = [v for k,v in trainPairs]

validPairs = np.genfromtxt('/home/me/Videos/sign_language/jester-v1-validation.csv', delimiter=';', skip_header=0, dtype=[('class', 'S12'),('sign','S50')])
validLabels = [v for k,v in validPairs]

def copyDirectory(src, dest):
    try:
        shutil.copytree(src, dest)
    # Directories are the same
    except shutil.Error as e:
        print('Directory not copied. Error: %s' % e)
    # Any error saying that the directory doesn't exist
    except OSError as e:
        print('Directory not copied. Error: %s' % e)

source = '/media/me/other/20bn-jester-v1/'
dest = '/media/me/other/jester/validation/'

# counter = 0
# for k,v in validPairs:
#     counter = counter + 1
#     source_folder = source + k.decode("utf-8")
#     dest_folder = dest + v.decode("utf-8") + "/" + k.decode("utf-8")

#     if counter%100 == 0:        
#         print(k)
#         print(v)
#         print(counter)
#         print(source_folder)
#         print(dest_folder)

#     if os.path.isdir(source_folder):
#         if os.path.isdir(dest + v.decode("utf-8")):
#             copyDirectory(source_folder, dest_folder)        

#     if counter%1000 == 0:
#         print(counter)

datagen = ImageDataGenerator()

train_it = datagen.flow_from_directory('/media/me/other/jester/train/', class_mode='categorical', batch_size=64)
valid_it = datagen.flow_from_directory('/media/me/other/jester/validation/', class_mode='categorical', batch_size=64)
# test_it = datagen.flow_from_directory('/media/me/other/jester/test/', class_mode='binary', batch_size=64)

seed = 21
epochs = 5
optimizer = 'Adamax'

with tf.device("/cpu:0"):
    model = Sequential()

model = Sequential()


#model.add(Conv2D(32,(3,3), input_shape=(X_train.shape[1:]), padding='same'))
#TODO is this the right shape??
model.add(Conv2D(32,(3,3), input_shape=(256, 256, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(32, (3,3), input_shape=(3,32,32), activation='relu', padding='same'))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Conv2D(64, (3,3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))
model.add(BatchNormalization())
model.add(Conv2D(128, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())

model.add(Flatten())
model.add(Dropout(0.2))
model.add(Dense(256, kernel_constraint=maxnorm(3)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())

model.add(Dense(128, kernel_constraint=maxnorm(3)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(BatchNormalization())

#TODO make this a variable
model.add(Dense(27))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])

### I think everything up to here is ok???

global graph
graph = tf.get_default_graph()

for layer in model.layers:
    print(layer.output_shape)

print(model.summary())

np.random.seed(seed)

image_batch_train, label_batch_train = next(iter(train_it))
print("Image batch shape: ", image_batch_train.shape)
print("Label batch shape: ", label_batch_train.shape)
dataset_labels = sorted(train_it.class_indices.items(), key=lambda pair:pair[1])
dataset_labels = np.array([key.title() for key, value in dataset_labels])

print(dataset_labels)

from keras import backend as K
K.clear_session()

import keras 
keras.backend.clear_session()

tf.reset_default_graph()
model.fit_generator(train_it, steps_per_epoch=16, validation_data=valid_it, validation_steps=8)

#scores = model.evaluate(test_it, steps=24, verbose=0)
#print("Accuracy: %.2f%%" % (scores[1]*100))

添加了日志

Traceback (most recent call last):

  File "<ipython-input-1-09b1bdd2e389>", line 152, in <module>
    model.fit_generator(train_it, steps_per_epoch=16, validation_data=valid_it, validation_steps=8)

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/keras/engine/training.py", line 1732, in fit_generator
    initial_epoch=initial_epoch)

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/keras/engine/training_generator.py", line 42, in fit_generator
    model._make_train_function()

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/keras/engine/training.py", line 316, in _make_train_function
    loss=self.total_loss)

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/keras/optimizers.py", line 599, in get_updates
    self.updates = [K.update_add(self.iterations, 1)]

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 1268, in update_add
    return tf_state_ops.assign_add(x, increment)

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/tensorflow/python/ops/state_ops.py", line 195, in assign_add
    return ref.assign_add(value)

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 1108, in assign_add
    name=name)

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/tensorflow/python/ops/gen_resource_variable_ops.py", line 68, in assign_add_variable_op
    "AssignAddVariableOp", resource=resource, value=value, name=name)

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py", line 366, in _apply_op_helper
    g = ops._get_graph_from_inputs(_Flatten(keywords.values()))

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 6135, in _get_graph_from_inputs
    _assert_same_graph(original_graph_element, graph_element)

  File "/home/me/Programs/anaconda3/envs/hand-gesture/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 6071, in _assert_same_graph
    (item, original_item))

ValueError: Tensor("training/Adamax/Const:0", shape=(), dtype=int64) must be from the same graph as Tensor("Adamax/iterations:0", shape=(), dtype=resource).

推荐答案

问题是您在训练模型之前要重置默认图形:

The problem is that you are resetting your default graph before training the model:

tf.reset_default_graph()  # <-- remove this line
model.fit_generator(train_it, steps_per_epoch=16, validation_data=valid_it, validation_steps=8)

问题如下.您首先要在开始时重置默认图形,除非您的实际脚本在此之前有更多代码,否则实际上并没有任何区别,因为默认图形在那时是空的.然后创建模型,并在新的默认图形上创建操作.新的默认图形是您以后使用的图形:

The problem is as follows. You reset first the default graph at the beginning, which, unless your actual script has more code before that, doesn't really make any difference, since the default graph is empty at that point. Then you make your model, and it creates operations on the new default graph. That new default graph is the one you get later with:

graph = tf.get_default_graph()

问题出在后来,在两次清除Keras会话(也没有任何作用)之后,您再次重置了默认图形.当您调用fit时,训练过程开始,并且创建了Keras模型的一些新图形对象.由于您的默认图形已更改(因为您将其重置),因此这些新对象与其他对象在不同的​​图形中创建,从而导致错误.我认为,如果您在训练过程中使用以前的默认图形作为默认图形,仍然可以重设图形并使其正常工作:

The problem is later, after clearing the Keras session twice (which doesn't have any effect either), you reset the default graph again. When you call fit, the training process starts, and some new graph objects of the Keras model are created. Since your default graph has changed it (because you reset it), those new objects are created in a different graph from the rest of objects, which causes the error. I think you still could reset the graph and have it work if you use the former default graph as default during the training:

tf.reset_default_graph()
with graph.as_default():  # Use former default graph as default
    model.fit_generator(train_it, steps_per_epoch=16, validation_data=valid_it, validation_steps=8)

我不确定Keras的默认会话是否会一直正常工作(因为它可能是为新的默认图形创建的),但是我认为它应该...无论如何,如果您出于任何原因想要要将Keras模型隔离在自己的图形中,而无需重置图形,您可以按以下步骤进行操作:

I'm not completely sure if the Keras default session will always work (since it might have been created for the new default graph), but I think it should... In any case, if you for whatever reason you want to have your Keras model isolated in its own graph, instead of resetting the graph you can do it as follows:

with tf.Graph().as_default() as graph:  # Make a new graph and use it as default
    # Make dataset
    # Make model
    # Train

这篇关于Tensor(...,shape =(),dtype = int64)必须与Tensor(...,shape =(),dtype = resource)来自同一张图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆