重写mtcnn遇到的python及tensorflow问题

编写 迭代器类:

# 必须要有 __init__(), __iter__(), __next__() 函数
class TestData:
    def __init__(self,data,batch_size=1):
        self.data = data
        self.size = len(data)
        self.batch_size = batch_size
        self.tmp = None
        self.cur = 0
        self.get_batch()
        print("size %d"%self.size)

    def get_batch(self):
        return self.data[self.cur]*2

    def __iter__(self):
        return self # 实例本身就是迭代对象,故返回自己

    def __next__(self):
        if self.cur < self.size:
            print("cur : %d"%self.cur)
            self.tmp = self.get_batch()
            self.cur += 1         
            return self.tmp
        else:
            raise StopIteration

a = range(10)
test = TestData(a)
for _ in test:
    print(_)

容器类:

# 必须要有__getitem__(), __setitem__()
class Father(object):
    def __init__(self):
        self.name = "father"
        self.children = ['child1', 'child2', 'child3']
        self.dict = {'a':111, 'b':222, 'c':333}

    def __getitem__(self, i):
        #assert i < len(self.children)
        if isinstance(i,int):
            return self.children[i]
        if isinstance(i,str):
            return self.dict[i]

    def __setitem__(self, i, obj):
        if isinstance(i,int):
            assert i < len(self.children)
            self.children[i] = obj
        if isinstance(i,str):
            self.dict[i] = obj

if __name__ == '__main__':
    f=Father()
    print (f[1])
    f[1]='orisun'
    print (f[1])
    print(f['a'])
    f['a']=323232
    print(f['a'])

制作和使用TFRecord:

# 制作 TFRecord
import os 
import tensorflow as tf 
from PIL import Image  #注意Image,后面会用到
import matplotlib.pyplot as plt 
import numpy as np

cwd='D:\Python\data\dog\\' 
classes={'husky','chihuahua'} #人为 设定 2 类
writer= tf.python_io.TFRecordWriter("dog_train.tfrecords") #要生成的文件

for index,name in enumerate(classes):
    class_path=cwd+name+'\\'
    for img_name in os.listdir(class_path): 
        img_path=class_path+img_name #每一个图片的地址

        img=Image.open(img_path)
        img= img.resize((128,128))
        img_raw=img.tobytes()#将图片转化为二进制格式
        example = tf.train.Example(features=tf.train.Features(feature={
            "label": tf.train.Feature(int64_list=tf.train.Int64List(value=[index])),
            'img_raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
        })) #example对象对label和image数据进行封装
        writer.write(example.SerializeToString())  #序列化为字符串

writer.close()
# 读取 TFRecord
def read_and_decode(filename): # 读入dog_train.tfrecords
    filename_queue = tf.train.string_input_producer([filename])#生成一个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

EasyDict

from easydict import EasyDict as edict

d = edict({'foo':3, 'bar':{'x':1, 'y':2}})

>>> d.foo
3
>>> d.bar.x
1
from easydict import EasyDict as edict
from simplejson import loads
j = """{
"Buffer": 12,
"List1": [
    {"type" : "point", "coordinates" : [100.1,54.9] },
    {"type" : "point", "coordinates" : [109.4,65.1] },
    {"type" : "point", "coordinates" : [115.2,80.2] },
    {"type" : "point", "coordinates" : [150.9,97.8] }
]
}"""
d = edict(loads(j))

>>> d.Buffer
12
>>> d = EasyDict(log=False)
>>> d.debug = True
>>> d.items()
[('debug', True), ('log', False)]

猜你喜欢

转载自blog.csdn.net/transMaple/article/details/78279581