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, reflection_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')
答案 0 :(得分:0)
如果您没有使用 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})
)
如果您处于 Eager 模式,则不能使用 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)
答案 1 :(得分:-1)
我修复了你的代码。 这只是一个小错误。你错过了一个 ()
这是改变的行
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