简体   繁体   English

张量流中的不同图像大小与batch_size = 1

[英]Different image sizes in tensorflow with batch_size =1

I want to achieve a python class, which can load a tensorflow model and implement a inference. 我想实现一个python类,它可以加载张量tensorflow model并实现推理。 However I have no idea about how can I input image with variable image size. 但是我不知道如何输入具有可变图像尺寸的图像。 :( :(

class ArtGenerater():
    def __init__(self,model_path):
            self.model_path = model_path
            # vary shape?
            self.x = tf.placeholder(tf.float32,shape=(1,512,512,3))
            self.gen = model.resnet(self.x)
            self.out = tf.saturate_cast(self.gen,tf.uint8)

            self.sess = tf.Session()
            file = tf.train.lastest_checkpoint(self.model_path)
            saver = tf.train.Saver()
            saver.restore(self.sess,file)
    def pic(self,image_path):
            img =np.asarray(Image.open(image_path)).astype(np.float32)
            img = np.expand_dims(img,0)
            output_t = self.sess.run(self.out,feed_dict={self.x:img})
            return output_t

Now I just use tf.placeholder(tf.float32,shape=(1,512,512,3)) , but my image have different sizes(eg. 1000*900). 现在我只使用tf.placeholder(tf.float32,shape=(1,512,512,3)) ,但我的图像有不同的大小(例如1000 * 900)。

How can i achieve this function? 我该如何实现这个功能? Thank you. 谢谢。

EDIT: 编辑:

Thank you everyone.I have solved problem by using: 谢谢大家。我用以下方法解决了问题:

x = tf.placeholder(tf.string)
img = tf.image.decode_jpeg(x,channels=3)

And this can feed network (my ConvNet include many conv2d & conv2d_tranpose ) with different image size. 这可以提供不同图像大小的网络(我的ConvNet包括许多conv2dconv2d_tranpose )。 :) :)

Basically you can define a various size input using None as follows 基本上,您可以使用None定义各种大小的输入,如下所示

self.x = tf.placeholder(tf.float32, [1, None, None, 3]) 

and then you can feed different input 然后你可以提供不同的输入

feed_dict={self.x: current_data} etc..

But be careful about your neural net structure. 但要小心你的神经网络结构。 If you flatten your last conv layer as input to the first dense layer then your network only works at that size, and you need to either stretch or crop the image to make it work. 如果您将最后一个转换图层压平为第一个密集图层的输入,那么您的网络仅适用于该大小,并且您需要拉伸或裁剪图像以使其正常工作。

A more flexible approach is to use something like Global Average Pooling or Spatial Pyramid Pooling which both fix this problem. 更灵活的方法是使用全局平均池空间金字塔池等方法来解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM