python常用小工具

显示四维张量中每一个通道的特征图

for i in range(32):
    image_tensor2 = image_tensor1[0, :, :, i]
    
    plt.imshow(image_tensor2)
    plt.show()

筛选列表中的元素

比如:筛选列表中最后字符为'relu6'的元素

[x for x in a if x[-5:] == 'Relu6']

打印图像矩阵

tmp 是PIL图像

import PIL
from pylab import *
im = array(tmp)
imshow(im)
plt.show()

简单的遍历生成器写法:

csv_file 是非常大的一个list,或者其他可遍历数据

def gen_info(csv_file):
    for info in csv_file:
        yield info[3]
 
g = gen_info(csv_file)
 
for i in g:
    print(i)

Tensorflow常用运算操作:

(tf.cast(image, tf.float32) - 127.5) / 128.0    # TF 强制类型转换
tf.image.per_image_standardization(image)  #TF 图像标准化
tf.equal(tf.mod(tf.floor_div(control, field), 2), 1) # TF 地板除法,取模和相等判断。
tf.image.flip_up_down:从上向下翻转
tf.image.flip_left_right:从左到又翻转
tf.image.transpose_image:对角线翻转
tf.image.random_flip_up_down:以一定概率从上向下翻转
tf.image.random_flip_left_right:以一定概率从左到又翻转
image = tf.cond(tf.equal(control, FIXED_STANDARDIZATION),
                lambda: (tf.cast(image, tf.float32) - 127.5) / 128.0,
                lambda: tf.image.per_image_standardization(image))      # TF lambda表达式

矩阵及维度常用操作:

image_size = (160, 160)
image.set_shape(image_size + (3,)) 
image的维度变为(160,160,3)

利用 python 对文件夹下图片数据进行批量改名

import os

class BatchRename():
    '''
    批量重命名文件夹中的图片文件

    '''
    def __init__(self):
        self.path = 'C:/Users/ThinkPad User/Desktop/weibo'

    def rename(self):
        filelist = os.listdir(self.path)
        total_num = len(filelist)
        i = 0
        for item in filelist:
            if item.endswith('.jpg'):
                src = os.path.join(os.path.abspath(self.path), item)
                dst = os.path.join(os.path.abspath(self.path), str(i) + '.jpg')
                try:
                    os.rename(src, dst)
                    print 'converting %s to %s ...' % (src, dst)
                    i = i + 1
                except:
                    continue
        print 'total %d to rename & converted %d jpgs' % (total_num, i)

if __name__ == '__main__':
    demo = BatchRename()
    demo.rename()

猜你喜欢

转载自blog.csdn.net/zk_ken/article/details/81133688