记录如何在tensorflow 中对batch中的每个image进行不同的操作

本文介绍了如何在TensorFlow中利用tf.map_fn函数处理batch中的每个图像,解决不能直接像numpy那样对tensor赋值的问题。通过示例代码,解释了如何在保持批处理维度的同时,对每个图像进行resize和pad的操作,强调了在同一map_fn中完成所有操作的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  tensorflow 是不支持像numpy一样对tensor/array 进行直接赋值的, 并且如果输出[batchsize, img_height, img_width, img_ch]的tensor, 如何对一个batch中的每个图片进行操作也比较麻烦。

 

解决这个问题主要是靠tensorflow的两个函数。

1、tf.map_fn(fn, elems, dtype)

这个函数有很多博客讲的很好,随便一搜就可以。  但是博客上一般都默认elems只有一个值,比如对视频进行卷积操作。 如果对每个batch中的每个图片进行操作呢?

看API:

"""map on the list of tensors unpacked from `elems` on dimension 0.

The simplest version of `map_fn` repeatedly applies the callable `fn` to a
sequence of elements from first to last. The elements are made of the
tensors unpacked from `elems`. `dtype` is the data type of the return
value of `fn`. Users must provide `dtype` if it is different from
the data type of `elems`.
这里介绍了函数是用来干嘛的。 简单地说就是函数把elements中的元素按照dimension 0展开,然后重复的给fn运行。dtype代表的是fn的返回值类型。

Suppose that `elems` is 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值