【6】基于tensorflow框架微调AlexNet模型以适应不同物体的分类(2)

此处文件包含:标签制造和读取,图像增强

[1]Imageprocess.py

图像载入 图像批量处理 数据增强

import tensorflow as tf
import os
import numpy as np
import  cv2

#打开路径
openpath2='D:\\pythonprocedure\\FineTuningAlexNet\\data\\Lym\\'
#保存路径
savepath2='D:\\pythonprocedure\\FineTuningAlexNet\\data\\Lymfull\\'

#载入图片  处理后保存到一个文件夹
def read__image(open_path,save_path):
    nums=0
    images=[]
    for dir_image in os.listdir(open_path): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
        full_path = os.path.abspath(os.path.join(open_path,dir_image))
        if dir_image.endswith('.bmp'):
            image = cv2.imread(full_path)
            image_path = save_path+'%s-%s.jpg' % ('Cell',str(nums))  # 注意这里图片名一定要加上扩展名,否则后面imwrite的时候会报错
            cv2.imwrite(image_path, image)
            nums=nums+1

#载入图片,处理后保存到一个列表中
def GetImg(open_path):
    patch=[]
    for dir_image in os.listdir(open_path): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
        full_path = os.path.abspath(os.path.join(open_path,dir_image))
        if dir_image.endswith('.jpg'):
            image = cv2.imread(full_path)
            resImg=cv2.resize(image,(227,227))
            patch.append(resImg)
    return  patch

#输入一张图片,产生旋转,缩放,长宽调整等形式
def DateArgutation(images):
    img=images
    imgInfo=img.shape
    height=imgInfo[0]
    width=imgInfo[1]
    agutationimg=[]
    #旋转3张
    angle=[90,180,280]
    for a in angle:
        matRotate=cv2.getRotationMatrix2D((height*0.5,width*0.5),a,0.5)
        rotateImg=cv2.warpAffine(img,matRotate,(height,width))
        agutationimg.append( rotateImg)

    #缩放3张
    scale=[0.5,2,4]
    for s in scale:
        dstHeight=int(height*s)
        dstWidth=int(width*s)
        resImg=cv2.resize(img,(dstWidth,dstHeight))
        agutationimg.append(resImg)

    #长宽
    change=[[40,50],[60,50],[60,40]]
    for h,w in change:
        reshwImg=cv2.resize(img,(h,w))
        agutationimg.append( reshwImg)


    print('success!')
    return  agutationimg

#打开路径
openpath1='D:\\pythonprocedure\\FineTuningAlexNet02\\data\\test3\\test\\'
#保存路径
savepath1='D:\\pythonprocedure\\FineTuningAlexNet02\\data\\test3\\testori\\'
read__image(openpath1,savepath1)

[2]input_selfdata.py

读取文件夹,制造图像标签,然后打乱数据加载到模型中

import tensorflow as tf
import os
import numpy as np
import cv2

#生成训练图片的路径
#train_dir='D:\\pythonprocedure\\FineTuningAlexNet02\\data\\cell'

'''
函数get_files:
载入图片制造标签,并打乱(原始图片为jpg,bmp格式容易报错,一般转成jpg格式)
'''


#获取图片,存放到对应的列表中,同时贴上标签,存放到label列表中
def get_files(file_dir):
    #待分类的细胞
    #淋巴细胞0
    Lyms =[]
    label_Lyms =[]
    #中性细胞1
    Neus =[]
    label_Neus =[]
    #其他细胞
    Others =[]
    label_Others =[]
    for dir_image in os.listdir(file_dir): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
        full_path = os.path.abspath(os.path.join(file_dir,dir_image))
        if dir_image.endswith('.jpg'):
            image = cv2.imread(full_path)
            #分离文件路径和文件名
            (filepath, filename) = os.path.split(full_path)
            #分离文件名和后缀
            nameall = os.path.splitext(filename)
            #分割文件名,保留第一个字符串的2到5,,原始字符串为 Lym-1
            name=str(nameall).split('-')[0][2:5]
            #print(name)
            resizeds = cv2.resize(image, (227, 227))
            if name=='Lym':
                Lyms.append(full_path)
                label_Lyms.append(0)
            elif name=='Neu':
                Neus.append(full_path)
                label_Neus.append(1)
            else:
                Others.append(full_path)
                label_Others.append(2)

    #合并数据 图片和标签均在水平方向平铺。
    image_list = np.hstack((Lyms, Neus,Others))
    #print(image_list.shape)
    label_list = np.hstack((label_Lyms, label_Neus,label_Others))
    #print(label_list.shape)
    #利用shuffle打乱数据,先合并为一个数组在打乱
    temp = np.array([image_list, label_list])
    temp = temp.transpose()  # 转置
    np.random.shuffle(temp)
    #print(temp.shape)
    #将所有的image和label转换成list
    #第一列都是img
    image_list = list(temp[:,0])
    #第二列都是label
    label_list = list(temp[:,1])
    label_list = [int(i) for i in label_list]
    #返回两个list,一个img一个是list
    return image_list, label_list

#将上面生成的List传入get_batch() ,转换类型,产生一个输入队列queue,因为img和label
#是分开的,所以使用tf.train.slice_input_producer(),然后用tf.read_file()从队列中读取图像
#数据和标签:image,label;图像的尺寸:image_W,image_H,;每一轮填充的图像;batch_size,;队列的容量:capacity
def get_batch(image,label,image_W,image_H,batch_size,capacity):

    #将python.list类型转换成tf能够识别的格式 tf.cast(x, dtype, name=None) cast为类型转换函数
    image=tf.cast(image,tf.string)
    label=tf.cast(label,tf.int32)

    #产生一个输入队列queue
    input_queue=tf.train.slice_input_producer([image,label])

    label=input_queue[1]
    image_contents=tf.read_file(input_queue[0])
    #将图像解码,不同类型的图像不能混在一起,要么只用jpeg,要么只用png等。
    image=tf.image.decode_jpeg(image_contents,channels=3)

    #将数据预处理,对图像进行旋转、缩放、裁剪、归一化等操作,让计算出的模型更健壮。
    #裁剪或将图像填充到目标宽度和高度.
    image=tf.image.resize_image_with_crop_or_pad(image,image_W,image_H)
    #图像归一化
    image=tf.image.per_image_standardization(image)

    #生成batch ;num_threads 线程数,使用多少个线程来控制整个队列
    image_batch,label_batch=tf.train.batch([image,label],batch_size=batch_size,num_threads=64,capacity=capacity)

    #重新排列标签,行数为[batch_size]
    #label_batch=tf.reshape(label_batch,[batch_size]) 数据类型转换
    image_batch=tf.cast(image_batch,tf.float32)

    return image_batch,label_batch




# #显示标签
# (image_list, label_list)=get_files(train_dir)
# length=len(image_list)
# for index in range(length):
#     path=image_list[index]
#     img=cv2.imread(path)
#     text1='图像和标签显示:'+str(index)
#     #print(text1)
#     text='Label:'+str(label_list[index])
#     img_resizeds = cv2.resize(img, (700, 500))
#     cv2.putText(img_resizeds,text, (20,20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
#     cv2.imshow('label_image',img_resizeds)
#     cv2.waitKey(1000)

TFRecord格式的标签制造与读取,项目中采用的不是此类的

[3]Labeledimage_TFRecord.py

制造tfrecord格式的标签

# -*- coding: utf-8 -*-
import os
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
import cv2

#cwd='./data/train/'
#cwd='./data/test/'
file_dir='D:\\pythonprocedure\\FineTuningAlexNet\\data\\cell'
classes={'Lym','Neu'}  #人为设定2类
#writer= tf.python_io.TFRecordWriter("dog_and_cat_train.tfrecords") #要生成的文件
writer= tf.python_io.TFRecordWriter("LNtest.tfrecords") #要生成的文件

for dir_image in os.listdir(file_dir): # os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
    full_path = os.path.abspath(os.path.join(file_dir,dir_image))
    if dir_image.endswith('.bmp'):
        image = cv2.imread(full_path)
        #分离文件路径和文件名
        (filepath, filename) = os.path.split(full_path)
        #分离文件名和后缀
        nameall = os.path.splitext(filename)
        #分割文件名,保留第一个字符串的2到5,,原始字符串为 Lym-1
        name=str(nameall).split('-')[0][2:5]
        #print(name)
        resizeds = cv2.resize(image, (227, 227))
        img_raw=resizeds.tobytes()
        if name=='Lym':
             example = tf.train.Example(features=tf.train.Features(feature={
            "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[0])),
            'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[ img_raw]))
        })) #example对象对label和image数据进行封装
        else:
            example = tf.train.Example(features=tf.train.Features(feature={
            "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[1])),
            'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[ img_raw]))
        })) #example对象对label和image数据进行封装
        writer.write(example.SerializeToString())  #序列化为字符串
writer.close()



[4]ReadMyOwnData.py

载入tfrecord格式的标签

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import numpy as np
import tensorflow as tf
import cv2

def read_and_decode(filename): # 读入tfrecords
    filename_queue = tf.train.string_input_producer([filename],shuffle=True)#生成一个queue队列

    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(filename_queue)#返回文件名和文件
    features = tf.parse_single_example(serialized_example,
                                       features={
                                           'label': tf.FixedLenFeature([], tf.int64),
                                           'img_raw' : tf.FixedLenFeature([], tf.string),
                                       })#将image数据和label取出来

    img = tf.decode_raw(features['img_raw'], tf.uint8)
    img = tf.reshape(img, [128, 128, 3])  #reshape为128*128的3通道图片
    img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 #在流中抛出img张量
    label = tf.cast(features['label'], tf.int32) #在流中抛出label张量

    return img, label

img, label=read_and_decode("LNtest.tfrecords")
#print(label)

【5】Ckpt_to_npy.py

Ckpt格式转换为npy格式的

#coding=gbk
import numpy as np
import tensorflow as tf
from tensorflow.python import pywrap_tensorflow


# open_ckpt_path='D:\\pythonprocedure\\FineTuningAlexNet02\\model\\alexnet_model.ckpt'#your ckpt path
# save_npy_path="D:\\pythonprocedure\\FineTuningAlexNet02\\npydata\\"

"""
mean:此函数是用来将ckpt文件保存为npy,仅针对AlexNet模型,若为他用需更改字典
open_ckpt_path:ckpt文件打开路径
save_npy_path:npy文件保存路径
"""

def ckpttonpy(open_ckpt_path,save_npy_path):
    reader=pywrap_tensorflow.NewCheckpointReader(open_ckpt_path)
    var_to_shape_map=reader.get_variable_to_shape_map()
    alexnet={}
    alexnet_layer = ['conv1','conv2','conv3','conv4','conv5','fc6','fc7','fc8']
    add_info = ['weights','biases']
    alexnet={'conv1':[[],[]],'conv2':[[],[]],'conv3':[[],[]],'conv4':[[],[]],'conv5':[[],[]],'fc6':[[],[]],'fc7':[[],[]],'fc8':[[],[]]}
    for key in var_to_shape_map:
        #print ("tensor_name",key)
        str_name = key
        # 因为模型使用Adam算法优化的,在生成的ckpt中,有Adam后缀的tensor
        if str_name.find('Adam') > -1:
            continue
        #print('tensor_name:' , str_name)
        if str_name.find('/') > -1:
            names = str_name.split('/')
            # first layer name and weight, bias
            layer_name = names[0]
            layer_add_info = names[1]
        else:
            layer_name = str_name
            layer_add_info = None

        if layer_add_info == 'weights':
            alexnet[layer_name][0]=reader.get_tensor(key)
        elif layer_add_info == 'biases':
            alexnet[layer_name][1] = reader.get_tensor(key)
        else:
            alexnet[layer_name] = reader.get_tensor(key)
    np.save(save_npy_path+'alexnet_cell.npy',alexnet)
    print("save success!")

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值