neural style 中的常用函数

1.scipy.misc.imresize函数重建图像

scipy.misc.imresize(arr, size, interp='bilinear', mode=None)

Parameters:

 

arr : ndarray

The array of image to be resized.

size : int, float or tuple

  • int - Percentage of current size.
  • float - Fraction of current size.
  • tuple - Size of the output image.

interp : str, optional

Interpolation to use for re-sizing (‘nearest’, ‘lanczos’, ‘bilinear’, ‘bicubic’or ‘cubic’).

mode : str, optional

The PIL image mode (‘P’, ‘L’, etc.) to convert arr before resizing.

Returns:

imresize : ndarray

The resized array of image.


2.元组的加

前提:content_image.shape=1066×800×3

IN:      shape = (1,) + content_image.shape

OUT:  (1, 1066, 800, 3)

3.numpy.dstack(tup)

>>> a = np.array((1,2,3))
>>> b = np.array((2,3,4))
>>> np.dstack((a,b))
array([[[1, 2],
        [2, 3],
        [3, 4]]])

>>> a = np.array([[1],[2],[3]])
>>> b = np.array([[2],[3],[4]])
>>> np.dstack((a,b))
array([[[1, 2]],
       [[2, 3]],
       [[3, 4]]])


4.TensorFlow程序读取数据

共有3种方法:

  • 供给数据(Feeding): 在TensorFlow程序运行的每一步, 让Python代码来供给数据。
  • 从文件读取数据: 在TensorFlow图的起始, 让一个输入管线从文件中读取数据。
  • 预加载数据: 在TensorFlow图中定义常量或变量来保存所有数据(仅适用于数据量比较小的情况)。

4.1 供给数据

      TensorFlow的数据供给机制允许你在TensorFlow运算图中将数据注入到任一张量中。因此,python运算可以把数据直接设置到TensorFlow图中。

通过给run()或者eval()函数输入 feed_dict 参数, 可以启动运算过程。


with tf.Session():
  input = tf.placeholder(tf.float32)
  classifier = ...
  print classifier.eval(feed_dict={input: my_python_preprocessing_fn()})


设计placeholder节点的唯一的意图就是为了提供数据供给(feeding)的方法。placeholder节点被声明的时候是未初始化的, 也不包含数据, 如果没有为它供给数据, 则TensorFlow运算的时候会产生错误, 所以千万不要忘了为placeholder提供数据。


5.for...in....


eg1:

for letter in 'Python':                           

       print '当前字母 :', letter

输出结果:

当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n

eg2:

fruits = ['banana', 'apple', 'mango']

for fruit in fruits:                 

      print '当前字母 :', fruit


输出结果:

当前字母 : banana
当前字母 : apple
当前字母 : mango

通过序列索引迭代,

另外一种执行循环的遍历方式是通过索引

fruits = ['banana', 'apple', 'mango']

for index in range(len(fruits)):

      print '当前水果 :', fruits[index]

neural style代码:

for i in range(len(style_images)):
        style_scale = STYLE_SCALE
        if options.style_scales is not None:
            style_scale = options.style_scales[i]
        style_images[i] = scipy.misc.imresize(style_images[i], style_scale *
                target_shape[1] / style_images[i].shape[1])


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值