<s> "<STR_LIT>" <EOL> import numpy as np <EOL> import copy <EOL> from random import randint <EOL> class HomogeneousData ( ) : <EOL> def __init__ ( self , captions_len , cap2im , batch_size = <NUM_LIT:64> , minlen = None , maxlen = None ) : <EOL> self . batch_size = batch_size <EOL> self . captions_len = captions_len <EOL> self . cap2im = cap2im <EOL> self . minlen = minlen <EOL> self . maxlen = maxlen <EOL> def prepare ( self ) : <EOL> self . lengths = [ self . captions_len [ i , - <NUM_LIT:1> ] for i in xrange ( self . captions_len . shape [ <NUM_LIT:0> ] ) ] <EOL> self . len_unique = np . unique ( self . lengths ) <EOL> if self . minlen != None and self . maxlen != None : <EOL> self . len_unique = [ ll for ll in self . len_unique if ll >= self . minlen and ll < self . maxlen ] <EOL> else : <EOL> self . len_unique = [ ll for ll in self . len_unique ] <EOL> self . len2cap = dict ( ) <EOL> for ll in self . len_unique : <EOL> self . len2cap [ ll ] = np . where ( self . lengths == ll ) [ <NUM_LIT:0> ] <EOL> def reset ( self ) : <EOL> self . len2ind = dict ( ) <EOL> for ll in self . len2cap : <EOL> self . len2cap [ ll ] = np . random . permutation ( self . len2cap [ ll ] ) <EOL> self . len2ind [ ll ] = <NUM_LIT:0> <EOL> self . len_unique_copy = copy . copy ( self . len_unique ) <EOL> def next ( self ) : <EOL> if self . len_unique_copy == [ ] : <EOL> return - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> <EOL> i = randint ( <NUM_LIT:0> , len ( self . len_unique_copy ) - <NUM_LIT:1> ) <EOL> len_i = self . len_unique_copy [ i ] <EOL> ind_i = self . len2ind [ len_i ] <EOL> cap_i = self . len2cap [ len_i ] [ ind_i : ind_i + self . batch_size ] <EOL> im_i = [ ] <EOL> for curr_cap_i in cap_i : <EOL> im_i . append ( self . cap2im [ curr_cap_i ] ) <EOL> self . len2ind [ len_i ] = self . len2ind [ len_i ] + self . batch_size <EOL> if self . len2ind [ len_i ] > self . len2cap [ len_i ] . shape [ <NUM_LIT:0> ] : <EOL> del self . len_unique_copy [ i ] <EOL> return np . int32 ( np . array ( cap_i ) ) , np . int32 ( np . array ( im_i ) ) , np . int32 ( len_i ) <EOL> def __iter__ ( self ) : <EOL> return self </s>
