编写 迭代器类:
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(_)
容器类:
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):
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:
import os
import tensorflow as tf
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
cwd='D:\Python\data\dog\\'
classes={'husky','chihuahua'}
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]))
}))
writer.write(example.SerializeToString())
writer.close()
def read_and_decode(filename):
filename_queue = tf.train.string_input_producer([filename])
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),
})
img = tf.decode_raw(features['img_raw'], tf.uint8)
img = tf.reshape(img, [128, 128, 3])
img = tf.cast(img, tf.float32) * (1. / 255) - 0.5
label = tf.cast(features['label'], tf.int32)
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)]