Python TF2D_转置形状不兼容

Python TF2D_转置形状不兼容,python,tensorflow,tflearn,deconvolution,Python,Tensorflow,Tflearn,Deconvolution,我给大小为[256 256 3]的图像作为输入,并希望输出相同的大小,但下面提到的错误正在显示。我尝试过改变形状、过滤、步幅,但没有任何效果。任何其他实现相同的方法都将受到赞赏,或者链接到与上述相同的东西,输入和输出都是图像。 这是我的代码: enter code here import tensorflow as tf import tflearn import os import numpy as np from tflearn.layers.conv import conv_2d, m

我给大小为[256 256 3]的图像作为输入,并希望输出相同的大小,但下面提到的错误正在显示。我尝试过改变形状、过滤、步幅,但没有任何效果。任何其他实现相同的方法都将受到赞赏,或者链接到与上述相同的东西,输入和输出都是图像。 这是我的代码:

enter code here
import tensorflow as tf 
import tflearn
import os

import numpy as np
from tflearn.layers.conv import conv_2d, max_pool_2d,conv_2d_transpose
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import loaddata

LR = 1e-3
MODEL_NAME = 'deblurr-{}-{}.model'.format(LR, '2conv-basic')
IMG_SIZE = 256
strides = [1,2,2,1]


convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 3],dtype=tf.float32, 
name='input')
print ("convnet :" , convnet.shape)


convnet = conv_2d(convnet, 6, 5,strides=strides, activation='LeakyReLU')
print ("convnet :" , convnet.shape)

convnet = conv_2d(convnet,12, 5,strides=strides, activation='LeakyReLU')
print ("convnet :" , convnet.shape)

convnet = conv_2d(convnet, 24, 5,strides=strides, activation='LeakyReLU')
print ("convnet :" , convnet.shape)


convnet = conv_2d(convnet, 48, 5,strides=strides, activation='LeakyReLU')
print ("convnet :" , convnet.shape)

convnet = conv_2d(convnet, 24, 5,strides=strides, activation='LeakyReLU')
print ("convnet :" , convnet.shape)

convnet = conv_2d_transpose(convnet, 12, 5,output_shape=
[64,64,12],strides=strides, activation='tanh')
print ("convnet :" , convnet.shape)
convnet = dropout(convnet, 0.8)
print ("convnet :" , convnet.shape)

convnet = conv_2d_transpose(convnet, 6, 5,output_shape=
[128,128,6],strides=strides, activation='tanh')
print ("convnet :" , convnet.shape)
convnet = dropout(convnet, 0.8)
print ("convnet :" , convnet.shape)

convnet = conv_2d_transpose(convnet, 3, 5,output_shape=
[256,256,3],strides=strides, activation='tanh')
print ("convnet :" , convnet.shape)
convnet = dropout(convnet, 0.8)
print ("convnet :" , convnet.shape)

convnet = regression(convnet, optimizer='adam', learning_rate=LR, 
loss='categorical_crossentropy', name='targets')
print ("convnet :" , convnet.shape)

model = tflearn.models.dnn.DNN(convnet)

if os.path.exists('{}.meta'.format(MODEL_NAME)):
model.load(MODEL_NAME)
print('model loaded!')

y_train, x_train = loaddata.load_data(data_type='train')


X = x_train
Y = y_train

y_test, x_test = loaddata.load_data(data_type='test')

test_x = y_test
test_y = x_test

model.fit({'input': X}, {'targets': Y}, n_epoch=10, validation_set=({'input': 
test_x}, {'targets': test_y}), batch_size=2,
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
错误如下:

ValueError                                Traceback (most recent call last)
C:\Users\USER\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py in merge_with(self, other)
    562         for i, dim in enumerate(self._dims):
--> 563           new_dims.append(dim.merge_with(other[i]))
    564         return TensorShape(new_dims)

C:\Users\USER\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py in merge_with(self, other)
    137     other = as_dimension(other)
--> 138     self.assert_is_compatible_with(other)
    139     if self._value is None:

C:\Users\USER\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py in assert_is_compatible_with(self, other)
    110       raise ValueError("Dimensions %s and %s are not compatible" % (self,
--> 111                                                                     other))
    112

ValueError: Dimensions 32 and 8 are not compatible

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
E:\extra\notes\New folder\source\models.py in <module>()
     53 print ("convnet :" , convnet.shape)
     54
---> 55 model = tflearn.models.dnn.DNN(convnet)
     56
     57 if os.path.exists('{}.meta'.format(MODEL_NAME)):

C:\Users\USER\Anaconda3\lib\site-packages\tflearn\models\dnn.py in __init__(self, network, clip_gradients, tensorboard_verbose, tensorboard_dir, checkpoint_path, best_checkpoint_path, max_checkpoints, session, best_val_accuracy)
     63                                max_checkpoints=max_checkpoints,
     64                                session=session,
---> 65                                best_val_accuracy=best_val_accuracy)
     66         self.session = self.trainer.session
     67

C:\Users\USER\Anaconda3\lib\site-packages\tflearn\helpers\trainer.py in __init__(self, train_ops, graph, clip_gradients, tensorboard_dir, tensorboard_verbose, checkpoint_path, best_checkpoint_path, max_checkpoints, keep_checkpoint_every_n_hours, random_seed, session, best_val_accuracy)
    129                 train_op.initialize_training_ops(i, self.session,
    130                                                  tensorboard_verbose,
--> 131                                                  clip_gradients)
    132
    133             # Saver for saving a model

C:\Users\USER\Anaconda3\lib\site-packages\tflearn\helpers\trainer.py in initialize_training_ops(self, i, session, tensorboard_verbose, clip_gradients)
    695             # Compute gradients operations
    696             with tf.control_dependencies([loss_avg_op, acc_avg_op]):
--> 697                 self.grad = tf.gradients(total_loss, self.train_vars)
    698                 if clip_gradients > 0.0:
    699                     self.grad, self.grad_norm = \

C:\Users\USER\Anaconda3\lib\site-packages\tensorflow\python\ops\gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method)
    560             if (isinstance(in_grad, ops.Tensor) and
    561                 t_in.dtype != dtypes.resource):
--> 562               in_grad.set_shape(t_in.get_shape())
    563             _SetGrad(grads, t_in, in_grad)
    564         if loop_state:

C:\Users\USER\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in set_shape(self, shape)
    376         this tensor.
    377     """
--> 378     self._shape = self._shape.merge_with(shape)
    379
    380   @property

C:\Users\USER\Anaconda3\lib\site-packages\tensorflow\python\framework\tensor_shape.py in merge_with(self, other)
    564         return TensorShape(new_dims)
    565       except ValueError:
--> 566         raise ValueError("Shapes %s and %s are not compatible" % (self, other))
    567
    568   def concatenate(self, other):

ValueError: Shapes (?, 32, 32, 24) and (?, 8, 8, 24) are not compatible
ValueError回溯(最近一次调用)
C:\Users\USER\Anaconda3\lib\site packages\tensorflow\python\framework\tensor\u shape.py与(self,other)合并
562对于i,枚举中的dim(自身dims):
-->563新的dims.append(dim.merge_与(其他[i]))
564返回张力形状(新尺寸)
C:\Users\USER\Anaconda3\lib\site packages\tensorflow\python\framework\tensor\u shape.py与(self,other)合并
137其他=as_尺寸(其他)
-->138.断言与(其他)兼容
139如果self.\u值为无:
断言中的C:\Users\USER\Anaconda3\lib\site packages\tensorflow\python\framework\tensor\u shape.py与(自身、其他)兼容
110提升值错误(“维度%s和%s不兼容”%(自身,
-->(其他)
112
ValueError:尺寸32和8不兼容
在处理上述异常期间,发生了另一个异常:
ValueError回溯(最近一次调用上次)
E:\extra\notes\New folder\source\models.py in()
53打印(“convnet:,convnet.shape)
54
--->55 model=tflearn.models.dnn.dnn(convnet)
56
57如果os.path.exists(“{}.meta.”格式(MODEL_NAME)):
C:\Users\USER\Anaconda3\lib\site packages\tflearn\models\dnn.py in\uuuuuuu init\uuuuuuuuuu(自我、网络、剪辑梯度、张力板、张力板、张力板、检查点路径、最佳检查点路径、最大检查点、会话、最佳值精度)
63最大检查点=最大检查点,
64会话=会话,
--->65最佳值精度=最佳值精度)
66 self.session=self.trainer.session
67
C:\Users\USER\Anaconda3\lib\site packages\tflearn\helpers\trainer.py in\uuuuuuu init\uuuuuuuuuu(self、train\u ops、graph、clip\u grade、tensorboard\u dir、tensorboard\u verbose、checkpoint\u path、best\u checkpoint\u path、max\u checkpoints\u、keep\u checkpoints\u每小时保留一次检查点、随机种子、会话、最佳值精度)
129培训操作初始化培训操作(i,自我会话,
130张三角板,
-->131(U梯度)
132
133#用于保存模型的保存程序
C:\Users\USER\Anaconda3\lib\site packages\tflearn\helpers\trainer.py在初始化\u培训\u操作中(self、i、session、tensorboard\u verbose、clip\u gradients)
695#计算梯度运算
696具有tf.控制依赖性([损失平均值,附件平均值]):
-->697自梯度=tf梯度(总损耗,自列变量)
698如果剪辑坡度>0.0:
699 self.grad,self.grad_norm=\
C:\Users\USER\Anaconda3\lib\site packages\tensorflow\python\ops\gradients\u impl.py渐变(ys、xs、grad\u ys、name、colocate\u gradients\u with\u ops、gate\u gradients、aggregation\u method)
560 if(恒定(梯度,操作张量)和
561 t_in.dtype!=dtypes.resource):
-->562英寸梯度设置形状(t英寸获取形状())
563 _SetGrad(渐变、渐变、渐变)
564如果循环_状态:
C:\Users\USER\Anaconda3\lib\site packages\tensorflow\python\framework\ops.py处于集合形状(self,shape)
这个张量。
377     """
-->378 self.\u shape=self.\u shape.与(shape)合并
379
380@property
C:\Users\USER\Anaconda3\lib\site packages\tensorflow\python\framework\tensor\u shape.py与(self,other)合并
564返回张力形状(新尺寸)
565除值错误外:
-->566提升值错误(“形状%s和%s不兼容”%(自身、其他))
567
568 def串联(自身、其他):
ValueError:形状(?,32,32,24)和(?,8,8,24)不兼容

如果查看conv层的形状输出:

convnet1: (?, 256, 256, 3)
convnet2: (?, 128, 128, 6)
convnet3: (?, 64, 64, 12)
convnet4: (?, 32, 32, 24)
convnet5: (?, 16, 16, 48)
convnet6: (?, 8, 8, 24)
convnet7: (?, 64, 64, 12)
convnet8: (?, 128, 128, 6)
convnet9: (?, 256, 256, 3)
convnet10: (?, 256, 256, 3)
从convnet5到convnet6,您希望以8的因子来提升维度。但您的代码在您的步幅中仅以2的因子来提升维度:

strides = [1,2,2,1]
convnet = conv_2d_transpose(convnet, 12, 5,output_shape=
          [64,64,12],strides=strides, activation='tanh')
正在反向传播的渐变形状与此层不兼容。但如果您只是将此行更改为:

convnet = conv_2d_transpose(convnet, 12, 5,output_shape=
          [64,64,12],strides=[1,8,8,1], activation='tanh')

那么你的代码应该可以工作。

如何计算给定两层的放大比例,反之亦然?如果你的输入形状是[?,H1,W1,,],你的输出形状是什么[?,H2,W2,,],那么你的步幅应该是[1,H2/H1,W2/W1,1]