def __init__(self, workflow, **kwargs): super(Uniform, self).__init__(workflow, **kwargs) self.num_states = kwargs.get("num_states", 256) self.states = Array() self.prng = kwargs.get("prng", get()) self.output_bytes = kwargs.get("output_bytes", 0) self.output = Array() self.cl_const = numpy.zeros(1, dtype=numpy.int32)
def cut_out(self, sample): if self.minibatch_class == 2: rand = prng.get() h_off = rand.randint(sample.shape[0] - self.crop_size_sy + 1) w_off = rand.randint(sample.shape[1] - self.crop_size_sx + 1) else: h_off = (sample.shape[0] - self.crop_size_sy) / 2 w_off = (sample.shape[1] - self.crop_size_sx) / 2 sample = sample[h_off:h_off + self.crop_size_sy, w_off:w_off + self.crop_size_sx, :self.channels] return sample
def cut_out(self, sample): if self.minibatch_class == 2: rand = prng.get() h_off = rand.randint( sample.shape[0] - self.crop_size_sy + 1) w_off = rand.randint( sample.shape[1] - self.crop_size_sx + 1) else: h_off = (sample.shape[0] - self.crop_size_sy) / 2 w_off = (sample.shape[1] - self.crop_size_sx) / 2 sample = sample[ h_off:h_off + self.crop_size_sy, w_off:w_off + self.crop_size_sx, :self.channels] return sample
def fill_indices(self, start_offset, count): self.minibatch_indices.map_invalidate() idxs = self.minibatch_indices.mem self.shuffled_indices.map_read() idxs[:count] = self.shuffled_indices[start_offset:start_offset + count] if self.is_master: return True if self.matrixes_filename is not None: self.has_mean_file = True self.minibatch_data.map_invalidate() self.minibatch_labels.map_invalidate() sample = numpy.zeros( [self.sy, self.sx, self.channels], dtype=numpy.uint8) sample_bytes = sample.nbytes for index, index_sample in enumerate(idxs[:count]): self.file_samples.seek(int(index_sample) * sample_bytes) self.file_samples.readinto(sample) rand = prng.get() self.do_mirror = self.mirror and bool(rand.randint((2))) image = self.transform_sample(sample) self.minibatch_data.mem[index] = image self.minibatch_labels.mem[ index] = self.original_labels[int(index_sample)] if count < len(idxs): idxs[count:] = self.class_lengths[1] # no data sample is there self.croped_mean = self.cut_out(self.mean) self.minibatch_data.mem[count:] = self.croped_mean self.minibatch_labels.mem[count:] = 0 # 0 is no data return True
def fill_indices(self, start_offset, count): self.minibatch_indices.map_invalidate() idxs = self.minibatch_indices.mem self.shuffled_indices.map_read() idxs[:count] = self.shuffled_indices[start_offset:start_offset + count] if self.is_master: return True if self.matrixes_filename is not None: self.has_mean_file = True self.minibatch_data.map_invalidate() self.minibatch_labels.map_invalidate() sample = numpy.zeros([self.sy, self.sx, self.channels], dtype=numpy.uint8) sample_bytes = sample.nbytes for index, index_sample in enumerate(idxs[:count]): self.file_samples.seek(int(index_sample) * sample_bytes) self.file_samples.readinto(sample) rand = prng.get() self.do_mirror = self.mirror and bool(rand.randint((2))) image = self.transform_sample(sample) self.minibatch_data.mem[index] = image self.minibatch_labels.mem[index] = self.original_labels[int( index_sample)] if count < len(idxs): idxs[count:] = self.class_lengths[1] # no data sample is there self.croped_mean = self.cut_out(self.mean) self.minibatch_data.mem[count:] = self.croped_mean self.minibatch_labels.mem[count:] = 0 # 0 is no data return True