Ejemplo n.º 1
0
def hotPotato(namelist, num):
    simqueue = Queue()
    for name in namelist:
        simqueue.enqueue(name)

    while simqueue.size() > 1:
        for i in range(num):
            simqueue.enqueue(simqueue.dequeue())

        simqueue.dequeue()

    return simqueue.dequeue()
Ejemplo n.º 2
0
class BlobFetcher(Process):
    def __init__(self, basedir, only_base = False, transform = False, jitter = True, channel_split = False):
        super(BlobFetcher, self).__init__(name='blob_fetcher')
        
        self._iqueue = Queue()
        self._oqueue = Queue()
        
        self._basedir = basedir
        self._only_base = only_base
        
        self._transform = transform
        self._jitter = jitter
        self._channel_split = channel_split
        
    def channel_split(self, im):
        assert im.shape[2] == 3
        
        chn = np.random.randint(3)
        return im[:,:,chn:chn+1]

    def jitter_image(self, im):
        #assert im.shape[2] == 3
                
        # multiply channels
        for ch in range(im.shape[2]):
            mult = np.random.uniform(0.9, 1.1)
            im[:,:,ch] *= mult
            
        # shift bias   
        shiftVal = np.random.uniform(-0.1, 0.1) 
        im += shiftVal
        
        # add sptial jitter
        noise = np.random.uniform(-0.05,0.05, (im.shape[0], im.shape[1]))
        for ch in range(im.shape[2]):
            im[:,:,ch] += noise
        
        # bring values to [0,1] range
        im[im < 0] = 0
        im[im > 1] = 1
        
        return im

    def load_image(self, imname, shape):
        loc = os.path.join(self._basedir, imname)
        if self._only_base:
            folder = os.path.basename(os.path.dirname(os.path.dirname(os.path.dirname(imname)))) # FIXME
            if folder == '2011_09_26_drive_0009_sync': folder = "2011_09_26_drive_0051_sync" # FIXME
            
            loc = os.path.join(self._basedir, folder, os.path.basename(imname))
        
        imrd = Image.open(loc)
        if shape[1] == 1 and not self._channel_split:
            imrd = imrd.convert('L')
            
        im = np.array(imrd).astype(np.float32) / 255.0
        if len(im.shape) == 2: 
            im = im[:, :, None]
        if im.shape[2] > 3:
            im = im[:, :, :3] # skip optional 4th dimension
            
        if self._channel_split:
            im = self.channel_split(im)
            
        #print(im.shape, shape)
        if not self._transform:
            if not (im.shape[0] == shape[2] and im.shape[1] == shape[3] and im.shape[2] == shape[1]):
                raise Exception("Incorrect image shape {} expecting {}".format(im.shape, shape))
        else:   
            # Resize if necessary
            h = im.shape[0]
            w = im.shape[1]
            if shape[2] != h or shape[3] != w:
                # apply anti aliasing
                factors = (np.asarray(im.shape, dtype=float) /
                        np.asarray(shape[2:] + (shape[1],), dtype=float))
                anti_aliasing_sigma = np.maximum(0, (factors - 1) / 2)
                
                im = ndimage.gaussian_filter(im, anti_aliasing_sigma)
                
                # resize to requested size
                im = transform.resize(im, shape[2:], mode = 'reflect')
        
        # Jitter and split image
        im = im.astype(np.float32)
        
        if self._jitter:
            im = self.jitter_image(im)
            
        im = np.transpose(im, axes = (2, 0, 1))
        
        return im
    
    def valid(self):
        return self._iqueue.empty() and self._oqueue.empty()
    
    def size(self):
        return self._iqueue.size()
    
    def get(self):
        return self._oqueue.get()
                
    def req(self, loc, shape):
        self._iqueue.put((loc, shape))
            
    def run(self):
        # load dataset
        try:
            # fetching blobs
            while True:
                loc, shape = self._iqueue.get()
                self._oqueue.put(self.load_image(loc, shape))
                
        except KeyboardInterrupt:
            self.terminate()
            return
#coding=utf-8
from multiprocessing import Queue

q = Queue(3)  #初始化一个Queue对象,最多可接受三条put消息
q.put("消息1")
q.put("消息2")
print(q.full())  #false
q.put("消息3")
print(q.full())  #true

# 因为消息队列已满下面的try都会抛出异常,第一个try会等待2秒后再抛出异常,第二个try会立刻抛出异常
try:
    q.put("消息4", True, 2)
except:
    print("消息队列已满,现有消息数量:%s" % q.qsize())

try:
    q.put_nowait("消息4")
except:
    print("消息队列已满,现有消息数量:%s" % q.qsize())

# 推荐的方式,先判断消息队列是否已满,再写入
if not q.full():
    q.put_nowait("消息4")

# 读取消息时,先判断消息队列是否为空,再读取
if not q.empty():
    for i in range(q.size()):
        print(q.get_nowait())