def train_batch(agent_net, i, sess, summary_op, sv, targets_tensor, train_data_provider, train_op, total_loss): images, targets = next(train_data_provider) log.debug('num images %r', len(images)) log.debug('num targets %r', len(targets)) valid_target_shape = True utils.resize_images(agent_net.input_image_shape, images) loss = None for tgt in targets: if len(tgt) != 6: log.error('invalid target shape %r skipping' % len(tgt)) valid_target_shape = False if valid_target_shape: feed_dict = { agent_net.input: images, targets_tensor: targets } # , 'phase:0': 1} if i % 10 == 0 and i > 0: # Summarize: Do this less frequently to speed up training time, more frequently to debug issues try: _, summ, loss = sess.run([train_op, summary_op, total_loss], feed_dict) except ValueError as e: print('Error processing batch, skipping - error was %r' % e) else: sv.summary_computed(sess, summ) sv.summary_writer.flush() else: # print('evaluating %r' % feed_dict) try: _, loss = sess.run([train_op, total_loss], feed_dict) except ValueError as e: print('Error processing batch, skipping - error was %r' % e) return loss
def resize(self, x, y, detail): if self.pad == 0: x, y = self.random_depth_crop(x, y) if self.resize_method == 'crop': x = x[:, self.pad_height:-self.pad_height, self.pad_width:-self.pad_width] y = y[:, self.pad_height:-self.pad_height, self.pad_width:-self.pad_width] elif self.resize_method == 'resize': x = resize_images(x, self.size) y = resize_images(y, self.size) else: pass return x, y, detail
def resize(self, x, detail): if self.resize_method == 'crop': x = x[:, self.pad_height: -self.pad_height, self.pad_width: -self.pad_width] elif self.resize_method == 'resize': x = resize_images(x, self.size) else: pass return x, detail
def save_tfrecord_file(file_idx, filename, images, targets): utils.assert_disk_space(filename) colorspace = b'RGB' channels = 3 image_format = b'RAW' out_filename = filename + '_' + str(file_idx).zfill(5) + '.tfrecord' writer = tf.python_io.TFRecordWriter(out_filename) utils.resize_images(INPUT_IMAGE_SHAPE, images) for image_idx in range(len(images)): if not image_idx % 500: log.info('{}/{} examples saved to {}'.format( image_idx, len(images), out_filename)) image = images[image_idx] # Undo initial subtraction of mean pixel during recording image += c.MEAN_PIXEL.astype(image.dtype) if image.min() < 0: raise ValueError('Min image less than zero') target = targets[image_idx] feature_dict = { 'image/width': _int64_feature(image.shape[0]), 'image/height': _int64_feature(image.shape[1]), 'image/colorspace': _bytes_feature(colorspace), 'image/channels': _int64_feature(channels), 'image/format': _bytes_feature(image_format), 'image/encoded': _bytes_feature( tf.compat.as_bytes(image.tostring()))} for name_idx, name in enumerate(c.CONTROL_NAMES): feature_dict['image/control/' + name] = \ _float_feature(target[name_idx]) example = tf.train.Example( features=tf.train.Features(feature=feature_dict)) # Serialize to string and write on the file writer.write(example.SerializeToString()) writer.close() log.info('Wrote %r examples to %s', len(images), out_filename)
def perform_eval(step, agent_net, batch_size, eval_dataset, eval_sw, sess): log.info('performing evaluation') losses = [] for images, targets in eval_dataset.iterate_once(batch_size): utils.resize_images(agent_net.input_image_shape, images) predictions = sess.run(agent_net.eval_out, {agent_net.input: images}) losses += [np.square(targets - predictions)] losses = np.concatenate(losses) summary = tf.Summary() eval_loss = float(0.5 * losses.sum() / losses.shape[0]) log.info('eval loss %f', eval_loss) summary.value.add(tag="eval/loss", simple_value=eval_loss) for i in range(len(c.CONTROL_NAMES)): loss_component = float(0.5 * losses[:, i].mean()) loss_name = c.CONTROL_NAMES[i] summary.value.add(tag="eval/{}".format(loss_name), simple_value=loss_component) log.info('%s loss %f', loss_name, loss_component) eval_sw.add_summary(summary, step) eval_sw.flush()
def plot_sample_augmented( X, y, label_names, n_images=5, target_size=(224, 224, 3), rotation_range=20, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.1, zoom_range=0.1, horizontal_flip=True, ): """Plot a sample of augmented images.""" indices = np.random.choice(X.shape[0], n_images, replace=False) images = X[indices] labels = y[indices] augmenter = ImageDataGenerator( rotation_range=rotation_range, width_shift_range=width_shift_range, height_shift_range=height_shift_range, shear_range=shear_range, zoom_range=zoom_range, horizontal_flip=horizontal_flip, ) X_resized = resize_images(images, target_size, preserve_range=False) X_augmented = np.array([augmenter.random_transform(x) for x in X_resized]) plt.figure(figsize=(20, 20)) for i in range(n_images): plt.subplot(1, n_images, i + 1) plt.imshow(X_augmented[i]) plt.axis('off') plt.title(label_names[labels[i]]) plt.show()