def run(self): n_true = self.batch_size - self.n_pseudo if self.n_pseudo: test_idx = np.arange(len(self.pseudo_images)) while True: # send images through queue in batches for i in range(0, len(self.images), n_true): # read real train images batch = list(map(self._transform, self.images[i:i + n_true])) if self.n_pseudo: pseudo_idx = self.rng.choice(test_idx, self.n_pseudo) batch.extend( list( map(self._transform, self.pseudo_images.iloc[pseudo_idx]))) labels = np.vstack( (to_ordinal(self.labels[i:i + n_true].values), self.pseudo_labels[pseudo_idx].astype( theano.config.floatX))) else: labels = to_ordinal(self.labels[i:i + n_true].values) batch = np.vstack(batch) self.outqueue.put((np.rollaxis(batch, 3, 1), labels)) #self.outqueue.put((batch.reshape(len(batch), 1 , 128, 128), labels)) # shuffle images at epoch end do this for trainig set only if self.augment: shuffle_idx = self.rng.permutation(len(self.images)) self.images = self.images.iloc[shuffle_idx] self.labels = self.labels.iloc[shuffle_idx] # signal end of epoch self.outqueue.put(None)
def crossover_ordinal_1p(parent_a, parent_b): point = randint(1, len(parent_a) - 1) parent_a = to_ordinal(parent_a) parent_b = to_ordinal(parent_b) child_a = parent_a[:point] child_a += parent_b[point:] child_b = parent_b[:point] child_b += parent_a[point:] return from_ordinal(child_a), from_ordinal(child_b)
def _image_iterator(self, image_list, labels=None, transform=False): # allocate an array for images images = np.zeros( (self.batch_size, self.image_size, self.image_size, 3), dtype=theano.config.floatX) n_images = len(image_list) n_chunks = int(np.ceil(n_images * 1. / self.batch_size)) for chunk in range(n_chunks): # prepare a slice of images to read during this pass chunk_end = (chunk + 1) * self.batch_size chunk_slice = slice(chunk * self.batch_size, chunk_end) # read a chunk of images for i, image in enumerate(image_list[chunk_slice]): if transform: images[i, ...] = self._transform(image) else: images[i, ...] = imread(image) / 255. if self.norm: images = self.normalize(images) # change axis order (see comments in valid_gen function) and yield images with labels if labels is not None: # transform labels to a collumn, but first we need to add a new axis #print labels[chunk_slice].values.astype(np.float32).reshape(chunk_slice, 1) yield np.rollaxis(images, 3, 1), to_ordinal(labels[chunk_slice].values) else: yield np.rollaxis(images, 3, 1)