TypeError:float() 参数必须是字符串或数字,而不是“张量”

Posted

技术标签:

【中文标题】TypeError:float() 参数必须是字符串或数字,而不是“张量”【英文标题】:TypeError: float() argument must be a string or a number, not 'Tensor' 【发布时间】:2021-05-21 00:20:52 【问题描述】:
import cv2
import numpy as np
from tensorflow.keras.layers import  Concatenate
import tensorflow as tf

im=cv2.imread(val_path)#image reading
im = np.array(im) # no channel
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)  # no channel
dx = cv2.Sobel(gray, cv2.CV_32F, 1, 0)  # float
dy = cv2.Sobel(gray, cv2.CV_32F, 0, 1)

mag = cv2.magnitude(dx, dy)  # gradient magnitude , no channel
mag = np.clip(mag, 0, 255).astype(np.uint8)  
mag = cv2.cvtColor(mag, cv2.COLOR_GRAY2RGB)  # convert mag to RGBA
mag = np.array(mag)#(282, 366, 3)

concat = Concatenate([im, mag])# using Concatenate of tensorflow.keras.layers
concat = tf.cast( concat, dtype=tf.float32)
#concat:(564, 366, 3)

input_image=np.expand_dims(concat, axis=0)/255.0)# <class 'numpy.ndarray'>
output_image_t, output_image_r = sess.run([transmission_layer, reflection_layer],feed_dict=input: input_image)###error
#transmission_layer, reflection_layer is fine

我必须连接原始彩色图像和梯度幅度图像

我遇到了一个错误

output_image_t, output_image_r = sess.run([transmission_layer, reflect_layer],feed_dict=input: input_image)#sess.run 没有 使用 tf.keras.layers。文件 ""/home/semi/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py"", 第 950 行,运行中 run_metadata_ptr) 文件 ""/home/semi/.local/lib/python3.6/site-packages/tensorflow/python/client/session.py"", 第 1142 行,在 _run np_val = np.asarray(subfeed_val, dtype=subfeed_dtype) 文件 ""/home/semi/.local/lib/python3.6/site-packages/numpy/core/_asarray.py"", 第 83 行,在数组中 返回数组(a, dtype, copy=False, order=order)

ValueError: setting an array element with a sequence.

我的问题是什么?

我的总代码

import cv2
import numpy as np
import os,scipy.io
import tensorflow as tf
#import tensorflow.contrib.slim as slim  #tf.contrib doesn't exist in TF 2.0. over => only TF 1.14 that is old
import tf_slim as slim#only can be TF 2.0.1, TF 2.1 and TF 2.2.
#TensorFlow-Slim: TensorFlow에서 복잡한 모델을 정의, 교육 및 평가하기 위한 경량 라이브러리
#tensorflow-2.2.0   python3.5-3.8    cudnn7.6 cuda10.1
# slim is VGG에 대해 이미지넷 데이터셋을 기반으로pre-trained된 모델을 기반으로 fine-tuning하는 과정이 단순화
from tensorflow.python.client import device_lib # GPU를 잘 사용할 수 있게 확인
os.environ['CUDA_VISIBLE_DEVICES']=str(0) # Uses GPU
print('**************************************************************************************************************')
tf.test.is_gpu_available(#GPU 사용이 가능하다(True)는 메시지가 떴다면 설치가 성공적으로 된 것
    cuda_only=True, min_cuda_compute_capability=None
)

print(device_lib.list_local_devices())# GPU list output
print('**************************************************************************************************************')
#tensorflow/core/common_runtime/gpu/gpu_device.cc:1406] Created TensorFlow device (/device:GPU:0 with 6631 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2080 SUPER, pci bus id: 0000:01:00.0, compute capability: 7.5)

channel = 64
EPS = 1e-12
vgg_path=scipy.io.loadmat('./VGG_Model/imagenet-vgg-verydeep-19.mat')

# our reflection removal model
# build VGG19 to load pre-trained parameters
def get_weight_bias(vgg_layers,i):
    weights=vgg_layers[i][0][0][2][0][0]
    weights=tf.constant(weights)
    bias=vgg_layers[i][0][0][2][0][1]
    bias=tf.constant(np.reshape(bias,(bias.size)))
    return weights,bias
def conv(batch_input, out_channels, stride):
    with tf.variable_scope("conv"):
        in_channels = batch_input.get_shape()[3]
        filter = tf.get_variable("filter", [4, 4, in_channels, out_channels], dtype=tf.float32, initializer=tf.random_normal_initializer(0, 0.02))
        padded_input = tf.pad(batch_input, [[0, 0], [1, 1], [1, 1], [0, 0]], mode="CONSTANT")
        conv = tf.nn.conv2d(padded_input, filter, [1, stride, stride, 1], padding="VALID")
        return conv
def build_net(ntype,nin,nwb=None,name=None):# 여기가 문제야
    if ntype=='conv':
        return tf.nn.relu(tf.nn.conv2d(nin,nwb[0],strides=[1,1,1,1],padding='SAME',name=name)+nwb[1])
    elif ntype=='pool':
        return tf.nn.avg_pool(nin,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
def build_vgg19(input,reuse=False):
    #with tf.variable_scope("vgg19"):
    with tf.compat.v1.variable_scope("vgg19"):
        if reuse:
            tf.get_variable_scope().reuse_variables()
        net=
        vgg_layers=vgg_path['layers'][0]
        net['input']=input-np.array([123.6800, 116.7790, 103.9390]).reshape((1,1,1,3))
        net['conv1_1']=build_net('conv',net['input'],get_weight_bias(vgg_layers,0),name='vgg_conv1_1')
        net['conv1_2']=build_net('conv',net['conv1_1'],get_weight_bias(vgg_layers,2),name='vgg_conv1_2')
        net['pool1']=build_net('pool',net['conv1_2'])
        net['conv2_1']=build_net('conv',net['pool1'],get_weight_bias(vgg_layers,5),name='vgg_conv2_1')
        net['conv2_2']=build_net('conv',net['conv2_1'],get_weight_bias(vgg_layers,7),name='vgg_conv2_2')
        net['pool2']=build_net('pool',net['conv2_2'])
        net['conv3_1']=build_net('conv',net['pool2'],get_weight_bias(vgg_layers,10),name='vgg_conv3_1')
        net['conv3_2']=build_net('conv',net['conv3_1'],get_weight_bias(vgg_layers,12),name='vgg_conv3_2')
        net['conv3_3']=build_net('conv',net['conv3_2'],get_weight_bias(vgg_layers,14),name='vgg_conv3_3')
        net['conv3_4']=build_net('conv',net['conv3_3'],get_weight_bias(vgg_layers,16),name='vgg_conv3_4')
        net['pool3']=build_net('pool',net['conv3_4'])
        net['conv4_1']=build_net('conv',net['pool3'],get_weight_bias(vgg_layers,19),name='vgg_conv4_1')
        net['conv4_2']=build_net('conv',net['conv4_1'],get_weight_bias(vgg_layers,21),name='vgg_conv4_2')
        net['conv4_3']=build_net('conv',net['conv4_2'],get_weight_bias(vgg_layers,23),name='vgg_conv4_3')
        net['conv4_4']=build_net('conv',net['conv4_3'],get_weight_bias(vgg_layers,25),name='vgg_conv4_4')
        net['pool4']=build_net('pool',net['conv4_4'])
        net['conv5_1']=build_net('conv',net['pool4'],get_weight_bias(vgg_layers,28),name='vgg_conv5_1')
        net['conv5_2']=build_net('conv',net['conv5_1'],get_weight_bias(vgg_layers,30),name='vgg_conv5_2')
        return net
def lrelu(x):
    return tf.maximum(x*0.2,x)
def relu(x):
    return tf.maximum(0.0,x)
def nm(x):
    w0=tf.Variable(1.0,name='w0')
    w1=tf.Variable(0.0,name='w1')
    return w0*x+w1*slim.batch_norm(x)
def identity_initializer():
    def _initializer(shape, dtype=tf.float32, partition_info=None):
        array = np.zeros(shape, dtype=float)
        cx, cy = shape[0]//2, shape[1]//2
        for i in range(np.minimum(shape[2],shape[3])):
            array[cx, cy, i, i] = 1
        return tf.constant(array, dtype=dtype)
    return _initializer
def build(input): #TF-Slim으로 VGG-19모델 구현
    print("[i] Hypercolumn ON, building hypercolumn features ... ")
    vgg19_features = build_vgg19(input[:, :, :, 0:3] * 255.0)
    for layer_id in range(1, 6):
        vgg19_f = vgg19_features['conv%d_2' % layer_id]
        input = tf.concat([tf.image.resize(vgg19_f, (tf.shape(input)[1], tf.shape(input)[2])) / 255.0, input],axis=3)
    net = slim.conv2d(input, channel, [1, 1], rate=1, activation_fn=lrelu, normalizer_fn=nm,
                      weights_initializer=identity_initializer(), scope='g_conv0')
    net = slim.conv2d(net, channel, [3, 3], rate=1, activation_fn=lrelu, normalizer_fn=nm,
                      weights_initializer=identity_initializer(), scope='g_conv1')
    net = slim.conv2d(net, channel, [3, 3], rate=2, activation_fn=lrelu, normalizer_fn=nm,
                      weights_initializer=identity_initializer(), scope='g_conv2')
    net = slim.conv2d(net, channel, [3, 3], rate=4, activation_fn=lrelu, normalizer_fn=nm,
                      weights_initializer=identity_initializer(), scope='g_conv3')
    net = slim.conv2d(net, channel, [3, 3], rate=8, activation_fn=lrelu, normalizer_fn=nm,
                      weights_initializer=identity_initializer(), scope='g_conv4')
    net = slim.conv2d(net, channel, [3, 3], rate=16, activation_fn=lrelu, normalizer_fn=nm,
                      weights_initializer=identity_initializer(), scope='g_conv5')
    net = slim.conv2d(net, channel, [3, 3], rate=32, activation_fn=lrelu, normalizer_fn=nm,
                      weights_initializer=identity_initializer(), scope='g_conv6')
    net = slim.conv2d(net, channel, [3, 3], rate=64, activation_fn=lrelu, normalizer_fn=nm,
                      weights_initializer=identity_initializer(), scope='g_conv7')
    net = slim.conv2d(net, channel, [3, 3], rate=1, activation_fn=lrelu, normalizer_fn=nm,
                      weights_initializer=identity_initializer(), scope='g_conv9')
    net = slim.conv2d(net, 3 * 2, [1, 1], rate=1, activation_fn=None,
                      scope='g_conv_last')  # output 6 channels --> 3 for transmission layer and 3 for reflection layer
    return net

def print_hi(name):
    test_path = ["./figures/"]
    subtask="result" # if you want to save different testset separately
    val_names=prepare_data_test(test_path)
    for val_path in val_names:#이미지들 들어있는 파일에서 이미지 경로 하나씩 가져옴
        testind = os.path.splitext(os.path.basename(val_path))[0]# os.path.splitext : 확장자만 따로 분류한다.(리스트로 나타낸다) ,0번째가 확장자
        #os.path.basename 파일 이름만 출력
        print('******************************val_path', val_path)
        if not os.path.isfile(val_path):
            continue
        im=cv2.imread(val_path)# figures파일 안에 있는 이미지들 하나씩 읽음 (296, 400, 3)
        img = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)  # no channel
        dx = cv2.Sobel(img, cv2.CV_32F, 1, 0)  # float 형태의 미분값을 저장
        dy = cv2.Sobel(img, cv2.CV_32F, 0, 1)

        mag = cv2.magnitude(dx, dy)  # gradient magnitude , no channel
        mag = np.clip(mag, 0, 255).astype(np.uint8)  # 255보다 커질 수 있으므로 saturate 연산

        mag = cv2.cvtColor(mag, cv2.COLOR_GRAY2RGB)  # convert mag to RGBA

        #concat = tf.concat([im, mag],0)# 행이 늘어남 (592, 400, 3)
        concat = tf.cast(mag, dtype=tf.float32)
        input_image = np.expand_dims(np.float32(concat), axis=0) / 255.0# <class 'numpy.ndarray'>  # (1, 592, 400, 3)
        # Define input of your network to which you feed your data
        #input = tf.placeholder(tf.float32, shape=(1, 564, 366, 3))

        # build the model
        network = build(input_image)###############################################################################################################################
        transmission_layer, reflection_layer = tf.split(network, num_or_size_splits=2, axis=3)############################################################################

        output_image_t=transmission_layer
        output_image_r=reflection_layer
        output_image_t=np.minimum(np.maximum(output_image_t,0.0),1.0)*255.0
        output_image_r=np.minimum(np.maximum(output_image_r,0.0),1.0)*255.0
        if not os.path.isdir("./result/%s/%s"%(subtask,testind)):
            os.makedirs("./result/%s/%s"%(subtask,testind))
        cv2.imwrite("./result/%s/%s/input.png"%(subtask,testind),im)
        cv2.imwrite("./result/%s/%s/t_output.png"%(subtask,testind),np.uint8(output_image_t[0,:,:,0:3])) # output transmission layer   ?? output_image_t[batch_size,channel,height, width]
        cv2.imwrite("./result/%s/%s/r_output.png"%(subtask,testind),np.uint8(output_image_r[0,:,:,0:3])) # output reflection layer

def prepare_data_test(test_path):
    input_names=[]
    for dirname in test_path:
        for _, _, fnames in sorted(os.walk(dirname)):
            for fname in fnames:
                if is_image_file(fname):#이미지 확장자에 포함 되면
                    input_names.append(os.path.join(dirname, fname))
    print(input_names)
    return input_names

def is_image_file(filename):
    return any(filename.endswith(extension) for extension in IMG_EXTENSIONS)

IMG_EXTENSIONS = [
    '.jpg', '.JPG', '.jpeg', '.JPEG',
    '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP',
]

if __name__ == '__main__':
    print_hi('PyCharm')


【问题讨论】:

这是因为tf.keras.layers.Concatenate 不接受numpy 数组。您必须使用 tf.keras.layers 定义模型并构建模型,然后提供 numpy 数据。 我知道,我拥有一切我唯一的问题是“input_image=np.expand_dims(np.float32(concat),axis=0)/255.0)########## ### 错误” 你想要 Tensor 还是 np.array? 我想 input_image=np.expand_dims(concat, axis=0)/255.0) 没有错误 不确定您的确切意思。你的 input_image 是张量还是 np.array?如果是 np.array,不要使用 tf.keras.layers.Conatenate。你通常不能混淆 np.array 和 tf.Tensor 【参考方案1】:

如果您不在 tf1.14 的 Eager 模式下,您可以使用 tf.placeholder 来提供您的数据。

import cv2
import numpy as np
import tensorflow as tf

# Prepare input data to your network
im = cv2.imread(val_path)  # image reading
im = np.array(im).astype(np.float32)  # no channel
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)  # no channel
dx = cv2.Sobel(gray, cv2.CV_32F, 1, 0)  # float
dy = cv2.Sobel(gray, cv2.CV_32F, 0, 1)

mag = cv2.magnitude(dx, dy)  # gradient magnitude , no channel
mag = np.clip(mag, 0, 255).astype(np.uint8)  
mag = cv2.cvtColor(mag, cv2.COLOR_GRAY2RGB)  # convert mag to RGBA
mag = np.array(mag).astype(np.float32)  # (282, 366, 3)

concat = np.concatenate([im, mag], axis=0)  # (564, 366, 3)  # tf.keras.layers doesn't work only when eager mode
# concat = tf.cast(concat, dtype=tf.float32)  # can't use tf.cast unless eager mode

input_image = np.expand_dims(concat, axis=0) # (1, 564, 366, 3) <class 'numpy.ndarray'>
input_image /= 255.

# Define input of your network to which you feed your data
input = tf.placeholder(tf.float32, shape=(1, 564, 366, 3))

# define your network
transmission_layer = ...
reflection_layer = ...

# Run Graph on GPU/CPU and feed data.
with tf.Session() as sess:
    # input is tensor and input_image is numpy array
    # returned output_image_t and output_image_r is np array
    output_image_t, output_image_r = sess.run(
        [transmission_layer, reflection_layer],
        feed_dict=input: input_image)
    )


如果您处于渴望模式,则不能使用tf.placeholder,也不能使用tf.Session

import cv2
import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe

tfe.enable_eager_execution()

# Prepare input data to your network
im = cv2.imread(val_path)  # image reading
im = np.array(im).astype(np.float32)  # no channel
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)  # no channel
dx = cv2.Sobel(gray, cv2.CV_32F, 1, 0)  # float
dy = cv2.Sobel(gray, cv2.CV_32F, 0, 1)

mag = cv2.magnitude(dx, dy)  # gradient magnitude , no channel
mag = np.clip(mag, 0, 255).astype(np.uint8)  
mag = cv2.cvtColor(mag, cv2.COLOR_GRAY2RGB)  # convert mag to RGBA
mag = np.array(mag).astype(np.float32)  # (282, 366, 3)

concat = tf.concatenate([im, mag], axis=0)  # Tensor (564, 366, 3). tf.keras.layers might not work. I recommend you are consstent with tf.Tensor
# concat = tf.cast(concat, dtype=tf.float32) not needed but this would work

input_image = np.expand_dims(concat, axis=0) # (1, 564, 366, 3) <class 'numpy.ndarray'> because of np.expand_dims
input_image /= 255.

# go on your network
transmission_layer = ...
reflection_layer = ...

# Run Graph on GPU/CPU and feed data
print(transmission_layer)
print(reflectionlayer)

【讨论】:

我的 TF 是 2.0 我无法使用会话我如何将 output_image_t, output_image_r=sess.run([transmission_layer, reflection_layer],feed_dict=input:input_image)## input is tensor and input_image is numpy array 转换为 TF2.0 你说你昨天用的是tf1.14...你可以试试tf.compat.v1.Sessiontensorflow.org/api_docs/python/tf/compat/v1/Session 我多次将TF2.0改成TF1.4和1.4T改成2.0 感谢您的参考我收到新错误net = slim.conv2d(input, channel, [1, 1], rate=1, activation_fn=lrelu, normalizer_fn=nm, weights_initializer=identity_initializer(), scope='g_conv0')tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable g_conv3/w0 from Container: localhost. This could mean that the variable was uninitialized. Not found: Container localhost does not exist. (Could not find resource: localhost/g_conv3/w0) 等等,这个问题是关于什么的?我想对你有用,但你不能在这个线程中提出这么多不同的问题。也许您必须发布另一个问题。否则,此评论线程将永远存在。【参考方案2】:

我修复了你的代码。 这只是一个小错误。你错过了一个()

这是更改的行

concat = Concatenate()([im, mag])

而不是

concat = Concatenate([im, mag])

这是完整的代码

import cv2
import numpy as np
from tensorflow.keras.layers import  Concatenate
import tensorflow as tf

im=cv2.imread('C:\....\\tensorflow.jpg')#image reading
im = np.array(im) # no channel
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)  # no channel
dx = cv2.Sobel(gray, cv2.CV_32F, 1, 0)  # float
dy = cv2.Sobel(gray, cv2.CV_32F, 0, 1)

mag = cv2.magnitude(dx, dy)  # gradient magnitude , no channel
mag = np.clip(mag, 0, 255).astype(np.uint8)  
mag = cv2.cvtColor(mag, cv2.COLOR_GRAY2RGB)  # convert mag to RGBA
mag = np.array(mag)#(282, 366, 3)

concat = Concatenate()([im, mag])# using Concatenate of tensorflow.keras.layers
concat = tf.cast( concat, dtype=tf.float32)
#concat:(564, 366, 3)

input_image=np.expand_dims(concat, axis=0)/255.0
output_image_t, output_image_r = sess.run([transmission_layer, reflection_layer],feed_dict=input: input_image)###error
#transmission_layer, reflection_layer is fine

【讨论】:

我认为它不可用我得到了新错误“ concat = concatenate()([im, mag]) TypeError: concatenate() missing 1 required positional argument: 'inputs' " 它对我有用。您正在使用不同的 keras/tensorflow 版本。它适用于 tf 2.3.0 我的 TF 版本是 '1.14.0' 因为我必须使用 import tensorflow.contrib.slim 作为只需要 TF V 1.14 的苗条 真的有用吗? output_image_t, output_image_r = sess.run([transmission_layer, reflection_layer],feed_dict=input: input_image) 抛出错误,因为找不到 sess。我在 TF2.4.1 中测试过,即使使用“with tf.Session() as sess:, it throws AttributeError: module 'tensorflow' has no attribute 'Session'. It will work with tf.compat.v1.Session```tensorflow.org/versions/r2.3/api_docs/python/tf投反对票。 我没有测试过。未提供 trtansmission_layer 和反射层。该人抱怨的唯一想法是引发错误的 expand_dims 。使用 tf 2.3.0 用 with () 修复了这个问题。

以上是关于TypeError:float() 参数必须是字符串或数字,而不是“张量”的主要内容,如果未能解决你的问题,请参考以下文章

Pandas :TypeError: float() 参数必须是字符串或数字,而不是 'pandas._libs.interval.Interval'

TypeError:float() 参数必须是字符串或数字,而不是与散点图相关的“datetime.time”

为啥在带有字符串和 NaN 的系列上得到“TypeError:'float' 类型的参数不可迭代”?

LabelEncoder -- TypeError: 参数必须是字符串或数字

TypeError:int() 参数必须是字符串或数字,而不是 'datetime.datetime'

TypeError:参数必须是字符串或数字列上的字符串或数字