def compute_center_coords(self, y_true, y_pred): batch_size = tf.shape(y_pred)[0] h = tf.shape(y_pred)[1] w = tf.shape(y_pred)[2] n_chans = tf.shape(y_pred)[3] n_dims = 5 # weighted center of mass x = tf.cast(tf.tile(tf.reshape(self.xs, [1, h, w]), [batch_size, 1, 1]), tf.float32) y = tf.cast(tf.tile(tf.reshape(self.ys, [1, h, w]), [batch_size, 1, 1]), tf.float32) eps = 1e-8 # grayscale pred_gray = tf.reduce_mean(y_pred, axis=-1) # should be batch_size x h x w # normalize pred_gray = pred_gray - tf.reduce_min(pred_gray, axis=[1, 2], keepdims=True) pred_gray = pred_gray / (eps + tf.reduce_max(pred_gray, axis=[1, 2], keepdims=True)) pred_gray = tf.clip_by_value(pred_gray, 0., 1.) # make each of these (batch_size, 1) weighted_x = tf.round(tf.expand_dims( tf.reduce_sum(x * pred_gray, axis=[1, 2]) / (eps + tf.reduce_sum(pred_gray, axis=[1, 2])), axis=-1)) weighted_y = tf.round(tf.expand_dims( tf.reduce_sum(y * pred_gray, axis=[1, 2]) / (eps + tf.reduce_sum(pred_gray, axis=[1, 2])), axis=-1)) batch_indices = tf.reshape(tf.linspace(0., tf.cast(batch_size, tf.float32) - 1., batch_size), [batch_size, 1]) indices = tf.cast(tf.concat([batch_indices, weighted_y, weighted_x], axis=-1), tf.int32) #center_rgb = transform_network_utils.interpolate([y_true, weighted_x, weighted_y], constant_vals=1.) center_rgb = tf.gather_nd(y_true, indices) center_rgb = tf.reshape(center_rgb, [batch_size, n_chans]) center_point_xyrgb = tf.concat([ weighted_x, weighted_y, center_rgb ], axis=-1) return pred_gray, center_point_xyrgb
def make_tf_top(x_shape, loss='sigmoid_ce'): """ builds the top layer, i.e. the loss layer. """ with tf.name_scope('top') as scope: x = tf.placeholder(tf.float32, shape=x_shape, name='input') y = tf.placeholder(tf.float32, shape=x_shape, name='output') if loss=='sigmoid_ce': L = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(x, y)) correct_prediction = tf.equal(tf.round( tf.sigmoid(x) ), tf.round( y )) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) accuracy_summary = [tf.summary.scalar('accuracy', accuracy)] elif loss=='softmax_ce': L = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(x, y)) correct_prediction = tf.equal(tf.argmax( tf.nn.softmax(x), 1 ), tf.argmax( y, 1 )) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) accuracy_summary = [tf.summary.scalar('accuracy', accuracy)] elif loss=='sigmoid_l2': L = tf.nn.l2_loss(tf.sigmoid(x) - y) accuracy = None accuracy_summary = [] elif loss=='l2': L = tf.nn.l2_loss(x - y) accuracy = None accuracy_summary = [] loss_summary = tf.summary.scalar('log_loss', tf.log(L)) dx = tf.gradients(L, x)[0] return L, dx, tf.summary.merge([loss_summary] + accuracy_summary), accuracy
def _smallest_size_at_least(height, width, target_height, target_width): """Computes new shape with the smallest side equal to `smallest_side`. Computes new shape with the smallest side equal to `smallest_side` while preserving the original aspect ratio. Args: height: an int32 scalar tensor indicating the current height. width: an int32 scalar tensor indicating the current width. smallest_side: A python integer or scalar `Tensor` indicating the size of the smallest side after resize. Returns: new_height: an int32 scalar tensor indicating the new height. new_width: and int32 scalar tensor indicating the new width. """ target_height = tf.convert_to_tensor(target_height, dtype=tf.int32) target_width = tf.convert_to_tensor(target_width, dtype=tf.int32) height = tf.to_float(height) width = tf.to_float(width) target_height = tf.to_float(target_height) target_width = tf.to_float(target_width) scale = tf.cond(tf.greater(target_height / height, target_width / width), lambda: target_height / height, lambda: target_width / width) new_height = tf.to_int32(tf.round(height * scale)) new_width = tf.to_int32(tf.round(width * scale)) return new_height, new_width
def blend_images(data_folder1, data_folder2, out_folder, alpha=.5): filename_queue = tf.placeholder(dtype=tf.string) label = tf.placeholder(dtype=tf.int32) tensor_image = tf.read_file(filename_queue) image = tf.image.decode_jpeg(tensor_image, channels=3) multiplier = tf.div(tf.constant(224, tf.float32), tf.cast(tf.maximum(tf.shape(image)[0], tf.shape(image)[1]), tf.float32)) x = tf.cast(tf.round(tf.mul(tf.cast(tf.shape(image)[0], tf.float32), multiplier)), tf.int32) y = tf.cast(tf.round(tf.mul(tf.cast(tf.shape(image)[1], tf.float32), multiplier)), tf.int32) image = tf.image.resize_images(image, [x, y]) image = tf.image.rot90(image, k=label) image = tf.image.resize_image_with_crop_or_pad(image, 224, 224) sess = tf.Session() sess.run(tf.local_variables_initializer()) for root, folders, files in os.walk(data_folder1): for each in files: if each.find('.jpg') >= 0: img1 = Image.open(os.path.join(root, each)) img2_path = os.path.join(root.replace(data_folder1, data_folder2), each.split("-")[-1]) rotation = int(each.split("-")[1]) img2 = sess.run(image, feed_dict={filename_queue: img2_path, label: rotation}) imsave(os.path.join(os.getcwd(), "temp", "temp.jpg"), img2) img2 = Image.open(os.path.join(os.getcwd(), "temp", "temp.jpg")) out_image = Image.blend(img1, img2, alpha) outfile = os.path.join(root.replace(data_folder1, out_folder), each) if not os.path.exists(os.path.split(outfile)[0]): os.makedirs(os.path.split(outfile)[0]) out_image.save(outfile) else: print(each) sess.close()
def get_exemplar_images(images, exemplar_size, targets_pos=None): """Crop exemplar image from input images""" with tf.name_scope('get_exemplar_image'): batch_size, x_height, x_width = images.get_shape().as_list()[:3] z_height, z_width = exemplar_size if targets_pos is None: target_pos_single = [[get_center(x_height), get_center(x_width)]] targets_pos_ = tf.tile(target_pos_single, [batch_size, 1]) else: targets_pos_ = targets_pos # convert to top-left corner based coordinates top = tf.to_int32(tf.round(targets_pos_[:, 0] - get_center(z_height))) bottom = tf.to_int32(top + z_height) left = tf.to_int32(tf.round(targets_pos_[:, 1] - get_center(z_width))) right = tf.to_int32(left + z_width) def _slice(x): f, t, l, b, r = x c = f[t:b, l:r] return c exemplar_img = tf.map_fn(_slice, (images, top, left, bottom, right), dtype=images.dtype) exemplar_img.set_shape([batch_size, z_height, z_width, 3]) return exemplar_img
def sample_img(img, n_samples): sx = tf.random_uniform((n_samples,), 0, 1) * 27 sy = tf.random_uniform((n_samples,), 0, 1) * 27 sx_lower = tf.cast(tf.floor(sx), tf.int32) sx_upper = tf.cast(tf.ceil(sx), tf.int32) sy_lower = tf.cast(tf.floor(sy), tf.int32) sy_upper = tf.cast(tf.ceil(sy), tf.int32) sx_nearest = tf.cast(tf.round(sx), tf.int32) sy_nearest = tf.cast(tf.round(sy), tf.int32) inds = tf.pack([sx_nearest, sy_nearest]) samples = tf.gather(tf.reshape(img, (-1,)), sx_nearest + sy_nearest*28) return sx/27, sy/27, samples
def get_adv_loss(logits_r, logits_f, labels_r, labels_f): dis_r_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits_r, labels=tf.cast(labels_r, tf.float32))) dis_f_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits_f, labels=tf.cast(labels_f, tf.float32))) dis_loss = dis_r_loss + dis_f_loss dis_r_acc = slim.metrics.accuracy(tf.cast(tf.round(tf.nn.sigmoid(logits_r)), tf.int32), tf.round(labels_r)) dis_f_acc = slim.metrics.accuracy(tf.cast(tf.round(tf.nn.sigmoid(logits_f)), tf.int32), tf.round(labels_f)) dis_acc = (dis_r_acc + dis_f_acc) / 2 gen_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits_f, labels=tf.cast(labels_r, tf.float32))) return dis_loss, gen_loss, dis_acc
def build_graph(self, nn_im_w, nn_im_h, num_colour_channels=3, weights=None, biases=None): num_outputs = 1 #ofc self.nn_im_w = nn_im_w self.nn_im_h = nn_im_h if weights is None: weights = [None, None, None, None, None] if biases is None: biases = [None, None, None, None, None] with tf.device('/cpu:0'): # Placeholder variables for the input image and output images self.x = tf.placeholder(tf.float32, shape=[None, nn_im_w*nn_im_h*3]) self.y_ = tf.placeholder(tf.float32, shape=[None, num_outputs]) self.threshold = tf.placeholder(tf.float32) # Build the convolutional and pooling layers conv1_output_channels = 32 conv2_output_channels = 16 conv3_output_channels = 8 conv_layer_1_input = tf.reshape(self.x, [-1, nn_im_h, nn_im_w, num_colour_channels]) #The resized input image self.build_conv_layer(conv_layer_1_input, num_colour_channels, conv1_output_channels, initial_weights=weights[0], initial_biases=biases[0]) # layer 1 self.build_conv_layer(self.layers[0][0], conv1_output_channels, conv2_output_channels, initial_weights=weights[1], initial_biases=biases[1])# layer 2 self.build_conv_layer(self.layers[1][0], conv2_output_channels, conv3_output_channels, initial_weights=weights[2], initial_biases=biases[2])# layer 3 # Build the fully connected layer convnet_output_w = nn_im_w//8 convnet_output_h = nn_im_h//8 fully_connected_layer_input = tf.reshape(self.layers[2][0], [-1, convnet_output_w * convnet_output_h * conv3_output_channels]) self.build_fully_connected_layer(fully_connected_layer_input, convnet_output_w, convnet_output_h, conv3_output_channels, initial_weights=weights[3], initial_biases=biases[3]) # The dropout stage and readout layer self.keep_prob, self.h_drop = self.dropout(self.layers[3][0]) self.y_conv,_,_ = self.build_readout_layer(self.h_drop, num_outputs, initial_weights=weights[4], initial_biases=biases[4]) self.mean_error = tf.sqrt(tf.reduce_mean(tf.square(self.y_ - self.y_conv))) self.train_step = tf.train.AdamOptimizer(1e-4).minimize(self.mean_error) self.accuracy = (1.0 - tf.reduce_mean(tf.abs(self.y_ - tf.round(self.y_conv)))) positive_examples = tf.greater_equal(self.y_, 0.5) negative_examples = tf.logical_not(positive_examples) positive_classifications = tf.greater_equal(self.y_conv, self.threshold) negative_classifications = tf.logical_not(positive_classifications) self.true_positive = tf.reduce_sum(tf.cast(tf.logical_and(positive_examples, positive_classifications),tf.int32)) # count the examples that are positive and classified as positive self.false_positive = tf.reduce_sum(tf.cast(tf.logical_and(negative_examples, positive_classifications),tf.int32)) # count the examples that are negative but classified as positive self.true_negative = tf.reduce_sum(tf.cast(tf.logical_and(negative_examples, negative_classifications),tf.int32)) # count the examples that are negative and classified as negative self.false_negative = tf.reduce_sum(tf.cast(tf.logical_and(positive_examples, negative_classifications),tf.int32)) # count the examples that are positive but classified as negative self.positive_count = tf.reduce_sum(tf.cast(positive_examples, tf.int32)) # count the examples that are positive self.negative_count = tf.reduce_sum(tf.cast(negative_examples, tf.int32)) # count the examples that are negative self.confusion_matrix = tf.reshape(tf.pack([self.true_positive, self.false_positive, self.false_negative, self.true_negative]), [2,2]) self.sess.run(tf.initialize_all_variables())
def get_sampled_frame(self, pred_frame): """Samples the frame based on modality. if the modality is L2/L1 then the next predicted frame is the next frame and there is no sampling but in case of Softmax loss the next actual frame should be sampled from predicted frame. This enables multi-frame target prediction with Softmax loss. Args: pred_frame: predicted frame. Returns: sampled frame. """ # TODO(lukaszkaiser): the logic below heavily depend on the current # (a bit strange) video modalities - we should change that. if self.is_per_pixel_softmax: frame_shape = common_layers.shape_list(pred_frame) target_shape = frame_shape[:-1] + [self.hparams.problem.num_channels] sampled_frame = tf.reshape(pred_frame, target_shape + [256]) sampled_frame = pixels_from_softmax( sampled_frame, temperature=self.hparams.pixel_sampling_temperature) # TODO(lukaszkaiser): this should be consistent with modality.bottom() sampled_frame = common_layers.standardize_images(sampled_frame) else: x = common_layers.convert_real_to_rgb(pred_frame) x = x - tf.stop_gradient(x + tf.round(x)) x = common_layers.convert_rgb_to_real(x) return x return sampled_frame
def build(self): # Define the placeholders tf_x = tf.placeholder(tf.int32, shape=(self.__batch_size, self.__seq_len), name='tf_x') tf_y = tf.placeholder(tf.float32, shape=(self.__batch_size), name='tf_y') tf_keepprob = tf.placeholder(tf.float32, name='tf_keepprob') # creat the embedding layer embedding = tf.Variable(tf.random_uniform((self.__n_words, self.__embed_size), minval=-1, maxval=1), name='embedding') embed_x = tf.nn.embedding_lookup(embedding, tf_x, name='embeded_x') # Define LSTM cell and stack them together cells = tf.contrib.rnn.MultiRNNCell([tf.contrib.rnn.DropoutWrapper(tf.contrib.rnn.BasicLSTMCell(self.__lstm_size), output_keep_prob=tf_keepprob) for i in range(self.__num_layers)]) # Define the initial state: self.initial_state = cells.zero_state(self.__batch_size, tf.float32) print(' << initial state >> ', self.initial_state) lstm_outputs, self.final_state = tf.nn.dynamic_rnn(cells, embed_x, initial_state=self.initial_state) # Note: lstm_outputs shape: [batch_size, max_time, cells.output_size] print('\n << lstm_output >> ', lstm_outputs) print('\n << final state >>', self.final_state) logits = tf.layers.dense(inputs=lstm_outputs[:, -1], units=1, activation=None, name='logits') logits = tf.squeeze(logits, name='logits_squeesed') print('\n << logits >> ', logits) y_proba = tf.nn.sigmoid(logits, name='probabilities') predictions = {'probabilities': y_proba, 'labels': tf.cast(tf.round(y_proba), tf.int32, name='labels')} print('\n << predictions >> ', predictions) # Define the cost function cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=tf_y, logits=logits), name='cost') # Define the optimizer optimizer = tf.train.AdamOptimizer(self.__learning_rate) train_op = optimizer.minimize(cost, name='train_op')
def infer(self, features, *args, **kwargs): """Produce predictions from the model by running it.""" del args, kwargs if "targets" not in features: if "infer_targets" in features: targets_shape = common_layers.shape_list(features["infer_targets"]) elif "inputs" in features: targets_shape = common_layers.shape_list(features["inputs"]) targets_shape[1] = self.hparams.video_num_target_frames else: raise ValueError("no inputs are given.") features["targets"] = tf.zeros(targets_shape, dtype=tf.float32) output, _ = self(features) # pylint: disable=not-callable if not isinstance(output, dict): output = {"targets": output} x = output["targets"] if self.is_per_pixel_softmax: x_shape = common_layers.shape_list(x) x = tf.reshape(x, [-1, x_shape[-1]]) x = tf.argmax(x, axis=-1) x = tf.reshape(x, x_shape[:-1]) else: x = tf.squeeze(x, axis=-1) x = tf.to_int64(tf.round(x)) output["targets"] = x if self.hparams.reward_prediction: output["target_reward"] = tf.argmax(output["target_reward"], axis=-1) # only required for decoding. output["outputs"] = output["targets"] output["scores"] = output["targets"] return output
def testEvaluatePerfectModel(self): checkpoint_dir = os.path.join(self.get_temp_dir(), 'evaluate_perfect_model_repeated') # Train a Model to completion: self._train_model(checkpoint_dir, num_steps=300) # Run inputs = tf.constant(self._inputs, dtype=tf.float32) labels = tf.constant(self._labels, dtype=tf.float32) logits = logistic_classifier(inputs) predictions = tf.round(logits) accuracy, update_op = tf.contrib.metrics.streaming_accuracy( predictions, labels) final_values = tf.contrib.training.evaluate_repeatedly( checkpoint_dir=checkpoint_dir, eval_ops=update_op, final_ops={'accuracy': accuracy}, hooks=[ tf.contrib.training.StopAfterNEvalsHook(1), ], max_number_of_evaluations=1) self.assertTrue(final_values['accuracy'] > .99)
def almost_equal(a, b): """ :param a: tensor :param b: tensor :returns equivalent to numpy: a == b, if a and b were ndarrays """ not_almost_equal = tf.abs(tf.sign(tf.round(a - b))) return tf.abs(not_almost_equal - 1)
def add_evaluation_step(result_tensor, ground_truth_tensor): """Inserts the operations we need to evaluate the accuracy of our results. Args: result_tensor: The new final node that produces results. ground_truth_tensor: The node we feed ground truth data into. Returns: Nothing. """ with tf.name_scope('accuracy'): with tf.name_scope('correct_prediction'): # tf.argmax(result_tensor, 1) = return index of maximal value (= 1 in a 1-of-N encoding vector) in each row (axis = 1) # But we have more ones (indicating multiple labels) in one row of result_tensor due to the multi-label classification # correct_prediction = tf.equal(tf.argmax(result_tensor, 1), \ # tf.argmax(ground_truth_tensor, 1)) # ground_truth is not a binary tensor, it contains the probabilities of each label = we need to tf.round() it # to acquire a binary tensor allowing comparison by tf.equal() # See: http://stackoverflow.com/questions/39219414/in-tensorflow-how-can-i-get-nonzero-values-and-their-indices-from-a-tensor-with correct_prediction = tf.equal(tf.round(result_tensor), ground_truth_tensor) with tf.name_scope('accuracy'): # Mean accuracy over all labels: # http://stackoverflow.com/questions/37746670/tensorflow-multi-label-accuracy-calculation evaluation_step = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.scalar_summary('accuracy', evaluation_step) return evaluation_step
def build_fcn_net(self,inp,use_dice=False): bn1 = tf.layers.batch_normalization(inputs=inp,name='bn1') dnn1 = tf.layers.dense(bn1,200,activation=None,name='f1') if use_dice: dnn1 = dice(dnn1,name='dice_1') else: dnn1 = prelu(dnn1,'prelu1') dnn2 = tf.layers.dense(dnn1,80,activation=None,name='f2') if use_dice: dnn2 = dice(dnn2,name='dice_2') else: dnn2 = prelu(dnn2,name='prelu2') dnn3 = tf.layers.dense(dnn2,2,activation=None,name='f3') self.y_hat = tf.nn.softmax(dnn3) + 0.00000001 with tf.name_scope('Metrics'): ctr_loss = -tf.reduce_mean(tf.log(self.y_hat) * self.target_ph) self.loss = ctr_loss if self.use_negsampling: self.loss += self.aux_loss tf.summary.scalar('loss',self.loss) self.optimizer = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(self.loss) self.accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(self.y_hat),self.target_ph),tf.float32)) tf.summary.scalar('accuracy',self.accuracy) self.merged = tf.summary.merge_all()
def toMnistCoordinates_tf(coordinate_tanhed, img_size): ''' Transform coordinate in [-1,1] to mnist :param coordinate_tanhed: vector in [-1,1] x [-1,1] :return: vector in the corresponding mnist coordinate ''' return tf.round(((coordinate_tanhed + 1) / 2.0) * img_size)
def build_fcn_net(self, inp, use_dice=False): bn1 = tf.layers.batch_normalization(inputs=inp, name='bn1', training=True) dnn1 = tf.layers.dense( bn1, 200, activation=None, kernel_initializer=get_tf_initializer(), name='f1') if use_dice: dnn1 = dice(dnn1, name='dice_1') else: dnn1 = prelu(dnn1) dnn2 = tf.layers.dense( dnn1, 80, activation=None, kernel_initializer=get_tf_initializer(), name='f2') if use_dice: dnn2 = dice(dnn2, name='dice_2') else: dnn2 = prelu(dnn2) dnn3 = tf.layers.dense( dnn2, 2, activation=None, kernel_initializer=get_tf_initializer(), name='f3') self.y_hat = tf.nn.softmax(dnn3) + 0.00000001 with tf.name_scope('Metrics'): ctr_loss = - \ tf.reduce_mean(tf.log(self.y_hat) * self.tensors.target) self.loss = ctr_loss if self.use_negsampling: self.loss += self.aux_loss else: self.aux_loss = tf.constant(0.0) # Accuracy metric self.accuracy = tf.reduce_mean( tf.cast(tf.equal(tf.round(self.y_hat), self.tensors.target), tf.float32))
def get_scheduled_sample_inputs( self, done_warm_start, groundtruth_items, generated_items, batch_size): with tf.variable_scope("scheduled_sampling", reuse=tf.AUTO_REUSE): if self.hparams.mode != tf.estimator.ModeKeys.TRAIN: feedself = True else: # Scheduled sampling: # Calculate number of ground-truth frames to pass in. feedself = False iter_num = tf.train.get_global_step() # TODO(mbz): what should it be if it's undefined? if iter_num is None: iter_num = _LARGE_STEP_NUMBER k = self.hparams.scheduled_sampling_k num_ground_truth = tf.to_int32( tf.round( tf.to_float(batch_size) * (k / (k + tf.exp(tf.to_float(iter_num) / tf.to_float(k)))))) if feedself and done_warm_start: # Feed in generated stuff. output_items = generated_items elif done_warm_start: output_items = [] for item_gt, item_gen in zip(groundtruth_items, generated_items): # Scheduled sampling output_items.append(self.scheduled_sample( item_gt, item_gen, batch_size, num_ground_truth)) else: # Feed in ground_truth output_items = groundtruth_items return output_items
def __init__(self, args): with tf.device(args.device): def circle(x): spherenet = tf.square(x) spherenet = tf.reduce_sum(spherenet, 1) lam = tf.sqrt(spherenet) return x/tf.reshape(lam,[int(lam.get_shape()[0]), 1]) def modes(x): shape = x.get_shape() return tf.round(x*2)/2.0#+tf.random_normal(shape, 0, 0.04) if args.distribution == 'circle': x = tf.random_normal([args.batch_size, 2]) x = circle(x) elif args.distribution == 'modes': x = tf.random_uniform([args.batch_size, 2], -1, 1) x = modes(x) elif args.distribution == 'modal-gaussian': x = tf.random_uniform([args.batch_size, 2], -1, 1) y = tf.random_normal([args.batch_size, 2], stddev=0.04, mean=0.15) x = tf.round(x) + y elif args.distribution == 'sin': x = tf.random_uniform((1, args.batch_size), -10.5, 10.5 ) x = tf.transpose(x) r_data = tf.random_normal((args.batch_size,1), mean=0, stddev=0.1) xy = tf.sin(0.75*x)*7.0+x*0.5+r_data*1.0 x = tf.concat([xy,x], 1)/16.0 elif args.distribution == 'static-point': x = tf.ones([args.batch_size, 2]) self.x = x self.xy = tf.zeros_like(self.x)
def calc_smape_rounded(true, predicted, weights): """ Calculates SMAPE on rounded submission values. Should be close to official SMAPE in competition :param true: :param predicted: :param weights: Weights mask to exclude some values :return: """ n_valid = tf.reduce_sum(weights) true_o = tf.round(tf.expm1(true)) pred_o = tf.maximum(tf.round(tf.expm1(predicted)), 0.0) summ = tf.abs(true_o) + tf.abs(pred_o) zeros = summ < 0.01 raw_smape = tf.abs(pred_o - true_o) / summ * 2.0 smape = tf.where(zeros, tf.zeros_like(summ, dtype=tf.float32), raw_smape) return tf.reduce_sum(smape * weights) / n_valid
def _ProcessSingleScale(scale_index, boxes, features, scales, scores, reuse=True): """Resize the image and run feature extraction and keypoint selection. This function will be passed into tf.while_loop() and be called repeatedly. The input boxes are collected from the previous iteration [0: scale_index -1]. We get the current scale by image_scales[scale_index], and run image resizing, feature extraction and keypoint selection. Then we will get a new set of selected_boxes for current scale. In the end, we concat the previous boxes with current selected_boxes as the output. Args: scale_index: A valid index in the image_scales. boxes: Box tensor with the shape of [N, 4]. features: Feature tensor with the shape of [N, depth]. scales: Scale tensor with the shape of [N]. scores: Attention score tensor with the shape of [N]. reuse: Whether or not the layer and its variables should be reused. Returns: scale_index: The next scale index for processing. boxes: Concatenated box tensor with the shape of [K, 4]. K >= N. features: Concatenated feature tensor with the shape of [K, depth]. scales: Concatenated scale tensor with the shape of [K]. scores: Concatenated attention score tensor with the shape of [K]. """ scale = tf.gather(image_scales, scale_index) new_image_size = tf.to_int32(tf.round(original_image_shape_float * scale)) resized_image = tf.image.resize_bilinear(image_tensor, new_image_size) attention, feature_map = model_fn( resized_image, normalized_image=True, reuse=reuse) rf_boxes = CalculateReceptiveBoxes( tf.shape(feature_map)[1], tf.shape(feature_map)[2], rf, stride, padding) # Re-project back to the original image space. rf_boxes = tf.divide(rf_boxes, scale) attention = tf.reshape(attention, [-1]) feature_map = tf.reshape(feature_map, [-1, feature_depth]) # Use attention score to select feature vectors. indices = tf.reshape(tf.where(attention >= abs_thres), [-1]) selected_boxes = tf.gather(rf_boxes, indices) selected_features = tf.gather(feature_map, indices) selected_scores = tf.gather(attention, indices) selected_scales = tf.ones_like(selected_scores, tf.float32) / scale # Concat with the previous result from different scales. boxes = tf.concat([boxes, selected_boxes], 0) features = tf.concat([features, selected_features], 0) scales = tf.concat([scales, selected_scales], 0) scores = tf.concat([scores, selected_scores], 0) return scale_index + 1, boxes, features, scales, scores
def __call__(self, img): scale = 1.0 + tf.random_uniform([], -self.max_stretch, self.max_stretch) img_shape = tf.shape(img) ts = tf.to_int32(tf.round(tf.to_float(img_shape[:2]) * scale)) resize_method_map = {'bilinear': tf.image.ResizeMethod.BILINEAR, 'bicubic': tf.image.ResizeMethod.BICUBIC} return tf.image.resize_images(img, ts, method=resize_method_map[self.interpolation])
def f_segm_match(iou, s_gt): """Matching between segmentation output and groundtruth. Args: y_out: [B, T, H, W], output segmentations y_gt: [B, T, H, W], groundtruth segmentations s_gt: [B, T], groudtruth score sequence """ # Mask X, [B, M] => [B, 1, M] mask_x = tf.expand_dims(s_gt, dim=1) # Mask Y, [B, M] => [B, N, 1] mask_y = tf.expand_dims(s_gt, dim=2) iou_mask = iou * mask_x * mask_y # Keep certain precision so that we can get optimal matching within # reasonable time. eps = 1e-5 precision = 1e6 iou_mask = tf.round(iou_mask * precision) / precision match_eps = tf.user_ops.hungarian(iou_mask + eps)[0] # [1, N, 1, 1] s_gt_shape = tf.shape(s_gt) num_segm_out = s_gt_shape[1] num_segm_out_mul = tf.pack([1, num_segm_out, 1]) # Mask the graph algorithm output. match = match_eps * mask_x * mask_y return match
def _testConfMatrixOnTensors(self, tf_dtype, np_dtype): with self.test_session() as sess: m_neg = tf.placeholder(dtype=tf.float32) m_pos = tf.placeholder(dtype=tf.float32) s = tf.placeholder(dtype=tf.float32) neg = tf.random_normal([20], mean=m_neg, stddev=s, dtype=tf.float32) pos = tf.random_normal([20], mean=m_pos, stddev=s, dtype=tf.float32) data = tf.concat(0, [neg, pos]) data = tf.cast(tf.round(data), tf_dtype) data = tf.minimum(tf.maximum(data, 0), 1) lab = tf.concat(0, [tf.zeros([20], dtype=tf_dtype), tf.ones([20], dtype=tf_dtype)]) cm = tf.contrib.metrics.confusion_matrix( data, lab, dtype=tf_dtype, num_classes=2) d, l, cm_out = sess.run([data, lab, cm], {m_neg: 0.0, m_pos: 1.0, s: 1.0}) truth = np.zeros([2, 2], dtype=np_dtype) try: range_builder = xrange except NameError: # In Python 3. range_builder = range for i in range_builder(len(d)): truth[d[i], l[i]] += 1 self.assertEqual(cm_out.dtype, np_dtype) self.assertAllClose(cm_out, truth, atol=1e-10)
def compress(): """Compresses an image.""" # Load input image and add batch dimension. x = load_image(args.input) x = tf.expand_dims(x, 0) x.set_shape([1, None, None, 3]) # Transform and compress the image, then remove batch dimension. y = analysis_transform(x, args.num_filters) entropy_bottleneck = tfc.EntropyBottleneck() string = entropy_bottleneck.compress(y) string = tf.squeeze(string, axis=0) # Transform the quantized image back (if requested). y_hat, likelihoods = entropy_bottleneck(y, training=False) x_hat = synthesis_transform(y_hat, args.num_filters) num_pixels = tf.to_float(tf.reduce_prod(tf.shape(x)[:-1])) # Total number of bits divided by number of pixels. eval_bpp = tf.reduce_sum(tf.log(likelihoods)) / (-np.log(2) * num_pixels) # Bring both images back to 0..255 range. x *= 255 x_hat = tf.clip_by_value(x_hat, 0, 1) x_hat = tf.round(x_hat * 255) mse = tf.reduce_mean(tf.squared_difference(x, x_hat)) psnr = tf.squeeze(tf.image.psnr(x_hat, x, 255)) msssim = tf.squeeze(tf.image.ssim_multiscale(x_hat, x, 255)) with tf.Session() as sess: # Load the latest model checkpoint, get the compressed string and the tensor # shapes. latest = tf.train.latest_checkpoint(checkpoint_dir=args.checkpoint_dir) tf.train.Saver().restore(sess, save_path=latest) string, x_shape, y_shape = sess.run([string, tf.shape(x), tf.shape(y)]) # Write a binary file with the shape information and the compressed string. with open(args.output, "wb") as f: f.write(np.array(x_shape[1:-1], dtype=np.uint16).tobytes()) f.write(np.array(y_shape[1:-1], dtype=np.uint16).tobytes()) f.write(string) # If requested, transform the quantized image back and measure performance. if args.verbose: eval_bpp, mse, psnr, msssim, num_pixels = sess.run( [eval_bpp, mse, psnr, msssim, num_pixels]) # The actual bits per pixel including overhead. bpp = (8 + len(string)) * 8 / num_pixels print("Mean squared error: {:0.4f}".format(mse)) print("PSNR (dB): {:0.2f}".format(psnr)) print("Multiscale SSIM: {:0.4f}".format(msssim)) print("Multiscale SSIM (dB): {:0.2f}".format(-10 * np.log10(1 - msssim))) print("Information content in bpp: {:0.4f}".format(eval_bpp)) print("Actual bits per pixel: {:0.4f}".format(bpp))
def main(): """main method for roadClassify.""" sess = tf.Session() # Placeholders x = tf.placeholder(tf.float32, [310*94*3]) y_truth = tf.placeholder(tf.float32, [78*24]) y_conv, loss, keep_prob = create_graph(x, y_truth) # Training parameters global_step = tf.Variable(1, name='global_step', trainable=False) train_step = tf.train.AdamOptimizer(1e-4).minimize(loss, global_step=global_step) correct_prediction = tf.equal(tf.round(y_conv) * 255, y_truth) accuracy = tf.reduce_mean(tf.to_float(correct_prediction)) sess.run(tf.initialize_all_variables()) saver = tf.train.Saver() #ckpt = tf.train.get_checkpoint_state("checkpoints/") #if ckpt and ckpt.model_checkpoint_path: # saver.restore(sess, ckpt.model_checkpoint_path) saver.restore(sess, "final_weights.model") # print "Model restored from latest checkpoint" #else: # print "No checkpoints, runnning from scratch" for i in range(global_step.eval(session=sess), global_step.eval(session=sess)+ARGS.iterations): imgs, lbls, count = get_next_example(i % 3) print("step %d, loss %f, training accuracy %f" % (i, sess.run(loss, feed_dict={x: imgs, y_truth: lbls, keep_prob: 1.0}), sess.run(accuracy, feed_dict={x: imgs, y_truth: lbls, keep_prob: 1.0}))) train_step.run(session=sess, feed_dict={x: imgs, y_truth: lbls, keep_prob: 1.0}) if i % 100 == 0: saver.save(sess, "checkpoints/" + "model.ckpt", global_step=global_step) imgs, lbls, count = get_next_example(0) cv2.imwrite("result.png", ( (np.reshape( sess.run(y_conv, feed_dict={x: imgs, y_truth: lbls, keep_prob: 1.0}), [24, 78]) * 255) > 127) * 255) print("Loss: %f" % loss.eval(session=sess, feed_dict={x: imgs, y_truth: lbls, keep_prob: 1.0})) print("**** %d iterations/example ****" % (i / count))
def quantize_weight(W, precision=weight_quantization): ''' For a given weight matrix, returns weights of values -1, 0 or 1 :param W: :return: ''' W_ = tf.round(W * precision) / precision return W_
def _quantize_overflow(x, k): G = tf.compat.v1.get_default_graph() n = float(2**k - 1) max_value = tf.reduce_max(input_tensor=x) min_value = tf.reduce_min(input_tensor=x) with G.gradient_override_map({"Round": "Identity"}): step = tf.stop_gradient((max_value - min_value) / n) return tf.round((tf.maximum(tf.minimum(x, max_value), min_value) - min_value) / step) * step + min_value
def iqst(x, n): """Integer quantization with straight-through estimator.""" eps = 1e-7 s = float(n - 1) xp = tf.clip_by_value((x + 1) / 2.0, -eps, 1 + eps) xpp = tf.round(s * xp) xppp = 2 * (xpp / s) - 1 return xpp, x + tf.stop_gradient(xppp - x)
def run(self, *in_arrays, return_as_list = False, # True = return a list of NumPy arrays, False = return a single NumPy array, or a tuple if there are multiple outputs. print_progress = False, # Print progress to the console? Useful for very large input arrays. minibatch_size = None, # Maximum minibatch size to use, None = disable batching. num_gpus = 1, # Number of GPUs to use. out_mul = 1.0, # Multiplicative constant to apply to the output(s). out_add = 0.0, # Additive constant to apply to the output(s). out_shrink = 1, # Shrink the spatial dimensions of the output(s) by the given factor. out_dtype = None, # Convert the output to the specified data type. **dynamic_kwargs): # Additional keyword arguments to pass into the network construction function. assert len(in_arrays) == self.num_inputs num_items = in_arrays[0].shape[0] if minibatch_size is None: minibatch_size = num_items key = str([list(sorted(dynamic_kwargs.items())), num_gpus, out_mul, out_add, out_shrink, out_dtype]) # Build graph. if key not in self._run_cache: with absolute_name_scope(self.scope + '/Run'), tf.control_dependencies(None): in_split = list(zip(*[tf.split(x, num_gpus) for x in self.input_templates])) out_split = [] for gpu in range(num_gpus): with tf.device('/gpu:%d' % gpu): out_expr = self.get_output_for(*in_split[gpu], return_as_list=True, **dynamic_kwargs) if out_mul != 1.0: out_expr = [x * out_mul for x in out_expr] if out_add != 0.0: out_expr = [x + out_add for x in out_expr] if out_shrink > 1: ksize = [1, 1, out_shrink, out_shrink] out_expr = [tf.nn.avg_pool(x, ksize=ksize, strides=ksize, padding='VALID', data_format='NCHW') for x in out_expr] if out_dtype is not None: if tf.as_dtype(out_dtype).is_integer: out_expr = [tf.round(x) for x in out_expr] out_expr = [tf.saturate_cast(x, out_dtype) for x in out_expr] out_split.append(out_expr) self._run_cache[key] = [tf.concat(outputs, axis=0) for outputs in zip(*out_split)] # Run minibatches. out_expr = self._run_cache[key] out_arrays = [np.empty([num_items] + shape_to_list(expr.shape)[1:], expr.dtype.name) for expr in out_expr] for mb_begin in range(0, num_items, minibatch_size): if print_progress: print('\r%d / %d' % (mb_begin, num_items), end='') mb_end = min(mb_begin + minibatch_size, num_items) mb_in = [src[mb_begin : mb_end] for src in in_arrays] mb_out = tf.get_default_session().run(out_expr, dict(zip(self.input_templates, mb_in))) for dst, src in zip(out_arrays, mb_out): dst[mb_begin : mb_end] = src # Done. if print_progress: print('\r%d / %d' % (num_items, num_items)) if not return_as_list: out_arrays = out_arrays[0] if len(out_arrays) == 1 else tuple(out_arrays) return out_arrays
def _call(self, inp, inp_features, is_training, is_posterior=True, prop_state=None): print("\n" + "-" * 10 + " ConvGridObjectLayer(is_posterior={}) ".format(is_posterior) + "-" * 10) # --- set up sub networks and attributes --- self.maybe_build_subnet("box_network", builder=cfg.build_conv_lateral, key="box") self.maybe_build_subnet("attr_network", builder=cfg.build_conv_lateral, key="attr") self.maybe_build_subnet("z_network", builder=cfg.build_conv_lateral, key="z") self.maybe_build_subnet("obj_network", builder=cfg.build_conv_lateral, key="obj") self.maybe_build_subnet("object_encoder") _, H, W, _, n_channels = tf_shape(inp_features) if self.B != 1: raise Exception("NotImplemented") if not self.initialized: # Note this limits the re-usability of this module to images # with a fixed shape (the shape of the first image it is used on) self.batch_size, self.image_height, self.image_width, self.image_depth = tf_shape(inp) self.H = H self.W = W self.HWB = H*W self.batch_size = tf.shape(inp)[0] self.is_training = is_training self.float_is_training = tf.to_float(is_training) inp_features = tf.reshape(inp_features, (self.batch_size, H, W, n_channels)) is_posterior_tf = tf.ones_like(inp_features[..., :2]) if is_posterior: is_posterior_tf = is_posterior_tf * [1, 0] else: is_posterior_tf = is_posterior_tf * [0, 1] objects = AttrDict() base_features = tf.concat([inp_features, is_posterior_tf], axis=-1) # --- box --- layer_inp = base_features n_features = self.n_passthrough_features output_size = 8 network_output = self.box_network(layer_inp, output_size + n_features, self.is_training) rep_input, features = tf.split(network_output, (output_size, n_features), axis=-1) _objects = self._build_box(rep_input, self.is_training) objects.update(_objects) # --- attr --- if is_posterior: # --- Get object attributes using object encoder --- yt, xt, ys, xs = tf.split(objects['normalized_box'], 4, axis=-1) yt, xt, ys, xs = coords_to_image_space( yt, xt, ys, xs, (self.image_height, self.image_width), self.anchor_box, top_left=False) transform_constraints = snt.AffineWarpConstraints.no_shear_2d() warper = snt.AffineGridWarper( (self.image_height, self.image_width), self.object_shape, transform_constraints) _boxes = tf.concat([xs, 2*xt - 1, ys, 2*yt - 1], axis=-1) _boxes = tf.reshape(_boxes, (self.batch_size*H*W, 4)) grid_coords = warper(_boxes) grid_coords = tf.reshape(grid_coords, (self.batch_size, H, W, *self.object_shape, 2,)) if self.edge_resampler: glimpse = resampler_edge.resampler_edge(inp, grid_coords) else: glimpse = tf.contrib.resampler.resampler(inp, grid_coords) else: glimpse = tf.zeros((self.batch_size, H, W, *self.object_shape, self.image_depth)) # Create the object encoder network regardless of is_posterior, otherwise messes with ScopedFunction encoded_glimpse = apply_object_wise( self.object_encoder, glimpse, n_trailing_dims=3, output_size=self.A, is_training=self.is_training) if not is_posterior: encoded_glimpse = tf.zeros_like(encoded_glimpse) layer_inp = tf.concat([base_features, features, encoded_glimpse, objects['local_box']], axis=-1) network_output = self.attr_network(layer_inp, 2 * self.A + n_features, self.is_training) attr_mean, attr_log_std, features = tf.split(network_output, (self.A, self.A, n_features), axis=-1) attr_std = self.std_nonlinearity(attr_log_std) attr = Normal(loc=attr_mean, scale=attr_std).sample() objects.update(attr_mean=attr_mean, attr_std=attr_std, attr=attr, glimpse=glimpse) # --- z --- layer_inp = tf.concat([base_features, features, objects['local_box'], objects['attr']], axis=-1) n_features = self.n_passthrough_features network_output = self.z_network(layer_inp, 2 + n_features, self.is_training) z_mean, z_log_std, features = tf.split(network_output, (1, 1, n_features), axis=-1) z_std = self.std_nonlinearity(z_log_std) z_mean = self.training_wheels * tf.stop_gradient(z_mean) + (1-self.training_wheels) * z_mean z_std = self.training_wheels * tf.stop_gradient(z_std) + (1-self.training_wheels) * z_std z_logit = Normal(loc=z_mean, scale=z_std).sample() z = self.z_nonlinearity(z_logit) objects.update(z_logit_mean=z_mean, z_logit_std=z_std, z_logit=z_logit, z=z) # --- obj --- layer_inp = tf.concat([base_features, features, objects['local_box'], objects['attr'], objects['z']], axis=-1) rep_input = self.obj_network(layer_inp, 1, self.is_training) _objects = self._build_obj(rep_input, self.is_training) objects.update(_objects) # --- final --- _objects = AttrDict() for k, v in objects.items(): _, _, _, *trailing_dims = tf_shape(v) _objects[k] = tf.reshape(v, (self.batch_size, self.HWB, *trailing_dims)) objects = _objects if prop_state is not None: objects.prop_state = tf.tile(prop_state[0:1, None], (self.batch_size, self.HWB, 1)) objects.prior_prop_state = tf.tile(prop_state[0:1, None], (self.batch_size, self.HWB, 1)) # --- misc --- objects.n_objects = tf.fill((self.batch_size,), self.HWB) objects.pred_n_objects = tf.reduce_sum(objects.obj, axis=(1, 2)) objects.pred_n_objects_hard = tf.reduce_sum(tf.round(objects.obj), axis=(1, 2)) return objects
def compress(): """Compresses an image.""" # Load input image and add batch dimension. x = load_image(args.input) x = tf.expand_dims(x, 0) x.set_shape([1, None, None, 3]) # Transform and compress the image, then remove batch dimension. # y = analysis_transform(x, args.num_filters) y = analysis_transform_ga(x, args.num_filters) entropy_bottleneck = tfc.EntropyBottleneck() string = entropy_bottleneck.compress(y) string = tf.squeeze(string, axis=0) # Transform the quantized image back (if requested). y_hat, likelihoods = entropy_bottleneck(y, training=False) #x_hat = synthesis_transform(y_hat, args.num_filters) x_hat = synthesis_transform_gs(y_hat, args.num_filters) num_pixels = tf.to_float(tf.reduce_prod(tf.shape(x)[:-1])) # Total number of bits divided by number of pixels. eval_bpp = tf.reduce_sum(tf.log(likelihoods)) / (-np.log(2) * num_pixels) # Mean squared error across pixels. x_hat = tf.clip_by_value(x_hat, 0, 1) x_hat = tf.round(x_hat * 255) mse = tf.reduce_sum(tf.squared_difference(x * 255, x_hat)) / num_pixels with tf.Session() as sess: # Load the latest model checkpoint, get the compressed string and the tensor # shapes. latest = tf.train.latest_checkpoint(checkpoint_dir=args.checkpoint_dir) result_dir = 'result-' + latest.split('\\')[-1] tf.train.Saver().restore(sess, save_path=latest) string, x_shape, y_shape = sess.run([string, tf.shape(x), tf.shape(y)]) # Write a binary file with the shape information and the compressed string. with open(args.output, "wb") as f: f.write(np.array(x_shape[1:-1], dtype=np.uint16).tobytes()) f.write(np.array(y_shape[1:-1], dtype=np.uint16).tobytes()) f.write(string) # If requested, transform the quantized image back and measure performance. if args.verbose: eval_bpp, mse, num_pixels = sess.run([eval_bpp, mse, num_pixels]) # The actual bits per pixel including overhead. bpp = (8 + len(string)) * 8 / num_pixels psnr = 10 * np.log10(255 * 255 / mse) with open('Output_MSE.txt', 'a+') as text_file: text_file.write('%10f\n' % mse) with open('Output_PSNR.txt', 'a+') as text_psnr_file: text_psnr_file.write('%10f\n' % psnr) with open('Output_InformationInBPP.txt', 'a+') as text_information_file: text_information_file.write('%10f\n' % eval_bpp) with open('Output_ActualBPP.txt', 'a+') as text_Actual_file: text_Actual_file.write('%10f\n' % bpp) print('PSNR: {:0.4}'.format(psnr)) print("Mean squared error: {:0.4}".format(mse)) print("Information content of this image in bpp: {:0.4}".format( eval_bpp)) print("Actual bits per pixel for this image: {:0.4}".format(bpp))
def noisy_input(self, img_row1, img_row2, img_row3, img_row4, img_row5, img_row6, img_row7, img_row8, is_training): with tf.variable_scope("Noise"): with tf.Session() as s: rnd = s.run( tf.random_uniform( [1], 0, 10, dtype=tf.int32, seed=self.seed)) # , seed=int(time.time()))) noisy_img_row1 = tf.cond( is_training, lambda: img_row1 + tf.round( tf.random_normal( tf.shape(img_row1), mean=0, stddev=rnd, seed=self.seed + 2, # int(time.time()), dtype=tf.float32)), lambda: img_row1) noisy_img_row2 = tf.cond( is_training, lambda: img_row2 + tf.round( tf.random_normal( tf.shape(img_row2), mean=0, stddev=rnd, seed=self.seed + 2, # int(time.time()), dtype=tf.float32)), lambda: img_row2) noisy_img_row3 = tf.cond( is_training, lambda: img_row3 + tf.round( tf.random_normal( tf.shape(img_row3), mean=0, stddev=rnd, seed=self.seed + 2, # int(time.time()), dtype=tf.float32)), lambda: img_row3) noisy_img_row4 = tf.cond( is_training, lambda: img_row4 + tf.round( tf.random_normal( tf.shape(img_row4), mean=0, stddev=rnd, seed=self.seed + 2, # int(time.time()), dtype=tf.float32)), lambda: img_row4) noisy_img_row5 = tf.cond( is_training, lambda: img_row5 + tf.round( tf.random_normal( tf.shape(img_row5), mean=0, stddev=rnd, seed=self.seed + 2, # int(time.time()), dtype=tf.float32)), lambda: img_row5) noisy_img_row6 = tf.cond( is_training, lambda: img_row6 + tf.round( tf.random_normal( tf.shape(img_row6), mean=0, stddev=rnd, seed=self.seed + 2, # int(time.time()), dtype=tf.float32)), lambda: img_row6) noisy_img_row7 = tf.cond( is_training, lambda: img_row7 + tf.round( tf.random_normal( tf.shape(img_row7), mean=0, stddev=rnd, seed=self.seed + 2, # int(time.time()), dtype=tf.float32)), lambda: img_row7) noisy_img_row8 = tf.cond( is_training, lambda: img_row8 + tf.round( tf.random_normal( tf.shape(img_row8), mean=0, stddev=rnd, seed=self.seed + 2, # int(time.time()), dtype=tf.float32)), lambda: img_row8) return noisy_img_row1,noisy_img_row2,noisy_img_row3,noisy_img_row4,\ noisy_img_row5,noisy_img_row6,noisy_img_row7,noisy_img_row8
import tensorflow as tf # load model (Insert respective filepath to the saved model+weights) new_model = load_model( '/content/Downloaded Garbage Classification 6 CLASSES)(InceptionV3).h5' ) #replace with SavedModel file if that's used instead # summarize model. new_model.summary() """## PREDICT WITH MODEL""" from keras.preprocessing import image pred_test_dir = '/content/Plastic Bottle Predict.jpg' # Insert path to image you want to predict on test_image = image.load_img(pred_test_dir, target_size=( 224, 224)) #Adjust to 128x128 depending on what is used in training test_image = image.img_to_array(test_image) test_image = np.expand_dims(test_image, axis=0) test_image = test_image.reshape(1, 224, 224, 3) """HOW THE PROBABILITY DISTRIBUTION ARRAY IS SETUP >[ORGANIC, RECYCLE, NONRECYCLABLE] """ predictions = tf.nn.softmax(new_model.predict(test_image, batch_size=1)) print(predictions) result = tf.round( predictions) # rounds the predictions for each respective type print(result) print(CLASS_NAMES[np.argmax(result[0])])
def interpn(vol, loc, interp_method='linear'): """ N-D gridded interpolation in tensorflow vol can have more dimensions than loc[i], in which case loc[i] acts as a slice for the first dimensions Parameters: vol: volume with size vol_shape or [*vol_shape, nb_features] loc: a N-long list of N-D Tensors (the interpolation locations) for the new grid each tensor has to have the same size (but not nec. same size as vol) or a tensor of size [*new_vol_shape, D] interp_method: interpolation type 'linear' (default) or 'nearest' Returns: new interpolated volume of the same size as the entries in loc TODO: enable optional orig_grid - the original grid points. check out tf.contrib.resampler, only seems to work for 2D data """ if isinstance(loc, (list, tuple)): loc = tf.stack(loc, -1) # since loc can be a list, nb_dims has to be based on vol. nb_dims = loc.shape[-1] if nb_dims != len(vol.shape[:-1]): raise Exception("Number of loc Tensors %d does not match volume dimension %d" % (nb_dims, len(vol.shape[:-1]))) if nb_dims > len(vol.shape): raise Exception("Loc dimension %d does not match volume dimension %d" % (nb_dims, len(vol.shape))) if len(vol.shape) == nb_dims: vol = K.expand_dims(vol, -1) # flatten and float location Tensors loc = tf.cast(loc, 'float32') volshape = vol.shape.as_list() # interpolate if interp_method == 'linear': loc0 = tf.floor(loc) # clip values max_loc = [d - 1 for d in vol.get_shape().as_list()] clipped_loc = [tf.clip_by_value(loc[...,d], 0, max_loc[d]) for d in range(nb_dims)] loc0lst = [tf.clip_by_value(loc0[...,d], 0, max_loc[d]) for d in range(nb_dims)] # get other end of point cube loc1 = [tf.clip_by_value(loc0lst[d] + 1, 0, max_loc[d]) for d in range(nb_dims)] locs = [[tf.cast(f, 'int32') for f in loc0lst], [tf.cast(f, 'int32') for f in loc1]] # compute the difference between the upper value and the original value # differences are basically 1 - (pt - floor(pt)) # because: floor(pt) + 1 - pt = 1 + (floor(pt) - pt) = 1 - (pt - floor(pt)) diff_loc1 = [loc1[d] - clipped_loc[d] for d in range(nb_dims)] diff_loc0 = [1 - d for d in diff_loc1] weights_loc = [diff_loc1, diff_loc0] # note reverse ordering since weights are inverse of diff. # go through all the cube corners, indexed by a ND binary vector # e.g. [0, 0] means this "first" corner in a 2-D "cube" cube_pts = list(itertools.product([0, 1], repeat=nb_dims)) interp_vol = 0 for c in cube_pts: # get nd values # note re: indices above volumes via https://github.com/tensorflow/tensorflow/issues/15091 # It works on GPU because we do not perform index validation checking on GPU -- it's too # expensive. Instead we fill the output with zero for the corresponding value. The CPU # version caught the bad index and returned the appropriate error. subs = [locs[c[d]][d] for d in range(nb_dims)] # tf stacking is slow for large volumes, so we will use sub2ind and use single indexing. # indices = tf.stack(subs, axis=-1) # vol_val = tf.gather_nd(vol, indices) # faster way to gather than gather_nd, because the latter needs tf.stack which is slow :( idx = sub2ind(vol.shape[:-1], subs) vol_val = tf.gather(tf.reshape(vol, [-1, volshape[-1]]), idx) # get the weight of this cube_pt based on the distance # if c[d] is 0 --> want weight = 1 - (pt - floor[pt]) = diff_loc1 # if c[d] is 1 --> want weight = pt - floor[pt] = diff_loc0 wts_lst = [weights_loc[c[d]][d] for d in range(nb_dims)] # tf stacking is slow, we we will use prod_n() # wlm = tf.stack(wts_lst, axis=0) # wt = tf.reduce_prod(wlm, axis=0) wt = prod_n(wts_lst) wt = K.expand_dims(wt, -1) # compute final weighted value for each cube corner interp_vol += wt * vol_val else: assert interp_method == 'nearest' roundloc = tf.cast(tf.round(loc), 'int32') # clip values max_loc = [tf.cast(d - 1, 'int32') for d in vol.shape] roundloc = [tf.clip_by_value(roundloc[...,d], 0, max_loc[d]) for d in range(nb_dims)] # get values # tf stacking is slow. replace with gather # roundloc = tf.stack(roundloc, axis=-1) # interp_vol = tf.gather_nd(vol, roundloc) idx = sub2ind(vol.shape[:-1], roundloc) interp_vol = tf.gather(tf.reshape(vol, [-1, vol.shape[-1]]), idx) return interp_vol
def masked_conv_v2(u, filter_1, filter_2, mask_1, mask_2, conductivity_1, conductivity_2, eps=0.01, filter_banks=None): '''not accurate still around the corner''' # center mask_filter = np.asarray( [[1 / 4., 0., 1 / 4.], [0., 0., 0.], [1 / 4., 0., 1 / 4.]], 'float32') mask_filter = tf.constant(mask_filter.reshape((3, 3, 1, 1))) padded_mask_1 = tf.pad(mask_1, [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") boundary_weight = tf.nn.conv2d(input=padded_mask_1, filter=mask_filter, strides=[1, 1, 1, 1], padding='VALID') w = 1000. boundary_weight = 1 / 4. + 1 / 4. * tf.sigmoid( w * (boundary_weight - 1.25 / 4.)) + 1 / 4. * tf.sigmoid( w * (boundary_weight - 2.75 / 4.)) boundary_mask = tf.round(mask_1 + 0.1) + tf.round(mask_2 + 0.1) - 1 mat1_mask = (boundary_weight) * boundary_mask * (-8 / 3. * conductivity_1) mat2_mask = (1 - boundary_weight) * boundary_mask * (-8 / 3. * conductivity_2) boundary_d_matrix = mat1_mask + mat2_mask mat1_d_matrix = tf.round(mask_1 - 0.1) * (-8 / 3. * conductivity_1) mat2_d_matrix = tf.round(mask_2 - 0.1) * (-8 / 3. * conductivity_2) d_matrix = boundary_d_matrix + mat1_d_matrix + mat2_d_matrix d_u = d_matrix * u # surrounding # not accurate only on the boundary, two parts from two different material padded_u_1 = tf.pad(u * tf.round(mask_1 + eps), [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") output_1 = tf.nn.conv2d(input=padded_u_1, filter=filter_1, strides=[1, 1, 1, 1], padding='VALID') padded_u_2 = tf.pad(u * tf.round(mask_2 + eps), [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") output_2 = tf.nn.conv2d(input=padded_u_2, filter=filter_2, strides=[1, 1, 1, 1], padding='VALID') res1 = output_1 * mask_1 + output_2 * mask_2 # accurate only on the boundary, two parts from two different material padded_u_1 = tf.pad(u * mask_1, [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") output_1 = tf.nn.conv2d(input=padded_u_1, filter=filter_1, strides=[1, 1, 1, 1], padding='VALID') padded_u_2 = tf.pad(u * mask_2, [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") output_2 = tf.nn.conv2d(input=padded_u_2, filter=filter_2, strides=[1, 1, 1, 1], padding='VALID') res2 = output_1 + output_2 LU_u = res1 * (tf.round(mask_1-0.1) + tf.round(mask_2-0.1)) + \ res2 * (tf.ones_like(mask_1) - tf.round(mask_1-0.1) - tf.round(mask_2-0.1)) result = d_u + LU_u tmp = { 'result': result, 'res1': res1, 'res2': res2, 'LU_u': LU_u, 'd_matrix': d_matrix, 'd_u': d_u, 'boundary_d_matrix': boundary_d_matrix, 'boundary_weight': boundary_weight, 'mat1_d_matrix': mat1_d_matrix, 'mat2_d_matrix': mat2_d_matrix, } return tmp
def train_network(N, data, targets, iterations, lr): # Convert input to the new form with the not of atoms data = list( map(lambda x: np.transpose(np.expand_dims(transform_input(x), 1)), data)) #data = add_ones(data, len(data)) # Data and target variables x = tf.placeholder("float64", [None, None]) y = tf.placeholder("float64") r = tf.placeholder("float64") with tf.device('/cpu:0'): upper = -np.log(0.00000001) zero = tf.constant(0.0, dtype="float64") one = tf.constant(1.0, dtype="float64") w_hidden = tf.Variable(np.array( np.random.uniform(0.0, upper, (2**N, 2 * N))), dtype="float64") b_hidden = tf.Variable(np.transpose( np.random.uniform(0.0, upper, (2**N, 1))), dtype="float64") w_out = tf.Variable(tf.transpose( np.expand_dims(np.random.uniform(0.0, upper, 2**N), 1)), dtype="float64") b_out = tf.Variable(np.random.uniform(0.0, upper, (1)), dtype="float64") c_w_hidden = tf.clip_by_value(w_hidden, 0, upper) c_b_hidden = tf.clip_by_value(b_hidden, 0, upper) c_w_out = tf.clip_by_value(w_out, 0, upper) c_b_out = tf.clip_by_value(b_out, 0, upper) #c_w_hidden = tf.clip_by_value(w_hidden, 0.00000001, 1) #c_b_hidden = tf.clip_by_value(b_hidden, 0.00000001, 1) #c_w_out = tf.clip_by_value(w_out, 0.00000001, 1) #c_b_out = tf.clip_by_value(b_out, 0.00000001, 1) #t_w_hidden = tf.subtract(zero, tf.log(c_w_hidden)) #t_b_hidden = tf.subtract(zero, tf.log(c_b_hidden)) #t_w_out = tf.subtract(zero, tf.log(c_w_out)) #t_b_out = tf.subtract(zero, tf.log(c_b_out)) hidden_out = noisy_or_activation(x, tf.transpose(c_w_hidden), c_b_hidden) y_hat = noisy_and_activation(hidden_out, tf.transpose(c_w_out), c_b_out) penalty = tf.add(compute_weight_penalty(c_w_hidden), compute_weight_penalty(c_w_out)) y_hat_prime = tf.reduce_sum(y_hat) error = -(y * tf.log(y_hat_prime) + (1 - y) * tf.log(1 - y_hat_prime) ) #tf.pow(y - tf.reduce_sum(y_hat), 2) e = error + 0.002 * penalty train_op_error = tf.train.AdamOptimizer(0.003).minimize(error) train_op_penalty = tf.train.AdamOptimizer(0.003).minimize(penalty) #train_op = tf.train.AdamOptimizer(lr).minimize(e) r_w_hidden = tf.round(tf.exp(-c_w_hidden)) r_w_out = tf.round(tf.exp(-c_w_out)) model = tf.global_variables_initializer() start_time = time.time() with tf.Session() as session: session.run(model) for i in range(iterations): for d in range(len(data)): session.run([train_op_error], feed_dict={ x: data[d], y: targets[d] }) session.run(train_op_penalty) if i % 100 == 0: er = 0 for d in range(len(data)): er += session.run(error, feed_dict={ x: data[d], y: targets[d] }) print(i, " : ", iterations) print(er) print( session.run(penalty, feed_dict={r: float(i) / float(iterations)})) print() w_hidden_value = session.run(c_w_hidden) w_out_value = session.run(c_w_out) r_w_h = session.run(r_w_hidden) r_w_o = session.run(r_w_out) total_time = time.time() - start_time return (w_hidden_value, w_out_value), (r_w_h, r_w_o), 0, total_time
def binary_predict(y_true, y_pred): return (tf.reduce_sum(tf.round(tf.abs(y_true - y_pred)))) / n_neurons
def round(x): return tf.round(x)
def main(self, enc_input, enc_input_shape, training_status, top_wrapper, bottom_wrapper): with tf.variable_scope("decoder", reuse=tf.AUTO_REUSE): l3_output = enc_input print("l3_output:", l3_output) # ?, 7, 7, 128 img_shape = enc_input_shape top_VQ_out = top_wrapper(l3_output, "top") # print("top_VQ_out:", top_VQ_out) # unflatten_ouput = VQ_out # # print("unflatten_ouput:", unflatten_ouput) print("img_shape[-1]:", img_shape[-1]) channel_reconstruct = tf.keras.layers.Dense( img_shape[-1], kernel_initializer=tf.keras.initializers.glorot_normal())( top_VQ_out) print("channel_reconstruct:", channel_reconstruct) # level 4 l4_raw_output = tf.keras.layers.Conv2DTranspose( self.filter_num * 2, kernel_size=3, strides=2, padding="SAME", kernel_initializer=self.kernel, activation=tf.nn.tanh)(channel_reconstruct) l4_output = tf.keras.layers.Conv2D( self.filter_num * 2, kernel_size=3, strides=1, padding="SAME", kernel_initializer=self.kernel)(l4_raw_output) l4_output = tf.keras.layers.Conv2D( self.latent_base, kernel_size=3, strides=1, padding="SAME", kernel_initializer=self.kernel)(l4_output) resize_top_VQ_out = tf.keras.layers.UpSampling2D( 2, interpolation='bilinear')(top_VQ_out) bottom_input = tf.concat([l4_output, resize_top_VQ_out], axis=3) bottom_input = tf.keras.layers.Dense( bottom_input.get_shape().as_list()[-1], activation="relu", kernel_initializer=tf.initializers.he_normal())(bottom_input) bottom_VQ_out = bottom_wrapper(bottom_input, "bottom") print("bottom_VQ_out:", bottom_VQ_out) bottom_VQ_out = tf.concat([bottom_VQ_out, resize_top_VQ_out], axis=3) print("bottom_VQ_out:", bottom_VQ_out) # level8 l5_raw_output = tf.keras.layers.Conv2DTranspose( self.filter_num * 1, kernel_size=3, strides=2, padding="SAME", kernel_initializer=self.kernel, activation=tf.nn.tanh)(bottom_VQ_out) l5_output = tf.keras.layers.Conv2D( self.filter_num * 1, kernel_size=3, strides=1, padding="SAME", kernel_initializer=self.kernel)(l5_raw_output) l5_output = tf.keras.layers.Conv2D( self.filter_num * 1, kernel_size=3, strides=1, activation="relu", padding="SAME", kernel_initializer=self.kernel)(l5_output) print("l5_output:", l5_output) # reconstruct layer recon_out = tf.keras.layers.Conv2D( 32, kernel_size=3, strides=1, padding="SAME", activation="sigmoid", kernel_initializer=self.kernel)(l5_output) recon_out = tf.keras.layers.Conv2D(1, kernel_size=3, strides=1, padding="SAME", kernel_initializer=self.kernel, activation="sigmoid")(recon_out) reg_recon_out = recon_out recon_output = tf.cast(tf.round(recon_out * 255), tf.float32) return reg_recon_out, recon_output
name='final_layer') model_output = tf.squeeze(model_output) # Declare loss function loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits( logits=model_output, labels=y), name='mean_cross-entropy') tf.summary.scalar('cross-entropy', loss) # Declare optimizer my_opt = tf.train.AdamOptimizer(FLAGS.lr) train_step = my_opt.minimize(loss) # Map model output to binary predictions with tf.name_scope('binary_predictions') as scope: prediction = tf.round(tf.sigmoid(model_output)) predictions_correct = tf.cast(tf.equal(prediction, y), tf.float32) accuracy = tf.reduce_mean(predictions_correct, name='mean_accuracy') tf.summary.scalar('accuracy', accuracy) # Logging merged = tf.summary.merge_all() train_writer = tf.summary.FileWriter(FLAGS.summary_dir + '/train') test_writer = tf.summary.FileWriter(FLAGS.summary_dir + '/test') validate_writer = tf.summary.FileWriter(FLAGS.summary_dir + '/validate') saver = tf.train.Saver() # for storing the best network with tf.Session() as sess: # Initialize variables init = tf.global_variables_initializer() sess.run(init)
def create_model(bert_config, is_training, input_ids, input_mask, segment_ids, token_label_ids, predicate_matrix_ids, num_token_labels, num_predicate_labels, use_one_hot_embeddings): """Creates a classification model.""" model = modeling.BertModel(config=bert_config, is_training=is_training, input_ids=input_ids, input_mask=input_mask, token_type_ids=segment_ids, use_one_hot_embeddings=use_one_hot_embeddings) # We "pool" the model by simply taking the hidden state corresponding # to the first token. float Tensor of shape [batch_size, hidden_size] # model_pooled_output = model.get_pooled_output() # """Gets final hidden layer of encoder. # # Returns: # float Tensor of shape [batch_size, seq_length, hidden_size] corresponding # to the final hidden of the transformer encoder. # """ sequence_bert_encode_output = model.get_sequence_output() if is_training: sequence_bert_encode_output = tf.nn.dropout( sequence_bert_encode_output, keep_prob=0.9) with tf.variable_scope("predicate_head_select_loss"): bert_sequenc_length = sequence_bert_encode_output.shape[-2].value #shape [batch_size, sequence_length, sequencd_length, predicate_label_numbers] predicate_score_matrix = getHeadSelectionScores( encode_input=sequence_bert_encode_output, hidden_size_n1=100, label_number=num_predicate_labels) predicate_head_probabilities = tf.nn.sigmoid(predicate_score_matrix) #predicate_head_prediction = tf.argmax(predicate_head_probabilities, axis=3) predicate_head_predictions_round = tf.round( predicate_head_probabilities) predicate_head_predictions = tf.cast(predicate_head_predictions_round, tf.int32) #shape [batch_size, sequence_length, sequencd_length] predicate_matrix = tf.reshape( predicate_matrix_ids, [-1, bert_sequenc_length, bert_sequenc_length]) #shape [batch_size, sequence_length, sequencd_length, predicate_label_numbers] gold_predicate_matrix_one_hot = tf.one_hot(predicate_matrix, depth=num_predicate_labels, dtype=tf.float32) predicate_embedding_weights = tf.get_variable( "predicate_embedding_weights", [num_predicate_labels, 2]) predicate_score_matrix_embed = tf.einsum('aijk,kp->aijp', predicate_score_matrix, predicate_embedding_weights) gold_predicate_matrix_one_hot_embed = tf.einsum( 'aijk,kp->aijp', predicate_score_matrix, predicate_embedding_weights) # shape [batch_size, sequence_length, sequencd_length, predicate_embed_dim=2] predicate_sigmoid_cross_entropy_with_logits = tf.nn.sigmoid_cross_entropy_with_logits( logits=predicate_score_matrix_embed, labels=gold_predicate_matrix_one_hot_embed) # shape [] predicate_head_select_loss = tf.reduce_sum( predicate_sigmoid_cross_entropy_with_logits) # return predicate_head_probabilities, predicate_head_predictions, predicate_head_select_loss with tf.variable_scope("token_label_loss"): bert_encode_hidden_size = sequence_bert_encode_output.shape[-1].value token_label_output_weight = tf.get_variable( "token_label_output_weights", [num_token_labels, bert_encode_hidden_size], initializer=tf.truncated_normal_initializer(stddev=0.02)) token_label_output_bias = tf.get_variable( "token_label_output_bias", [num_token_labels], initializer=tf.zeros_initializer()) sequence_bert_encode_output = tf.reshape(sequence_bert_encode_output, [-1, bert_encode_hidden_size]) token_label_logits = tf.matmul(sequence_bert_encode_output, token_label_output_weight, transpose_b=True) token_label_logits = tf.nn.bias_add(token_label_logits, token_label_output_bias) token_label_logits = tf.reshape( token_label_logits, [-1, FLAGS.max_seq_length, num_token_labels]) token_label_log_probs = tf.nn.log_softmax(token_label_logits, axis=-1) token_label_one_hot_labels = tf.one_hot(token_label_ids, depth=num_token_labels, dtype=tf.float32) token_label_per_example_loss = -tf.reduce_sum( token_label_one_hot_labels * token_label_log_probs, axis=-1) token_label_loss = tf.reduce_sum(token_label_per_example_loss) token_label_probabilities = tf.nn.softmax(token_label_logits, axis=-1) token_label_predictions = tf.argmax(token_label_probabilities, axis=-1) # return (token_label_loss, token_label_per_example_loss, token_label_logits, token_label_predict) loss = predicate_head_select_loss + token_label_loss return (loss, predicate_head_select_loss, predicate_head_probabilities, predicate_head_predictions, token_label_loss, token_label_per_example_loss, token_label_logits, token_label_predictions)
def _call(self, inp, inp_features, is_training, is_posterior=True, prop_state=None): print("\n" + "-" * 10 + " GridObjectLayer(is_posterior={}) ".format(is_posterior) + "-" * 10) # --- set up sub networks and attributes --- self.maybe_build_subnet("box_network", builder=cfg.build_lateral, key="box") self.maybe_build_subnet("attr_network", builder=cfg.build_lateral, key="attr") self.maybe_build_subnet("z_network", builder=cfg.build_lateral, key="z") self.maybe_build_subnet("obj_network", builder=cfg.build_lateral, key="obj") self.maybe_build_subnet("object_encoder") _, H, W, _, _ = tf_shape(inp_features) H = int(H) W = int(W) if not self.initialized: # Note this limits the re-usability of this module to images # with a fixed shape (the shape of the first image it is used on) self.batch_size, self.image_height, self.image_width, self.image_depth = tf_shape(inp) self.H = H self.W = W self.HWB = H*W*self.B self.is_training = is_training self.float_is_training = tf.to_float(is_training) # --- set up the edge element --- sizes = [4, self.A, 1, 1] sigmoids = [True, False, False, True] total_sample_size = sum(sizes) if self.edge_weights is None: self.edge_weights = tf.get_variable("edge_weights", shape=total_sample_size, dtype=tf.float32) if "edge" in self.fixed_weights: tf.add_to_collection(FIXED_COLLECTION, self.edge_weights) _edge_weights = tf.split(self.edge_weights, sizes, axis=0) _edge_weights = [ (tf.nn.sigmoid(ew) if sigmoid else ew) for ew, sigmoid in zip(_edge_weights, sigmoids)] edge_element = tf.concat(_edge_weights, axis=0) edge_element = tf.tile(edge_element[None, :], (self.batch_size, 1)) # --- containers for storing built program --- program = np.empty((H, W, self.B), dtype=np.object) # --- build the program --- is_posterior_tf = tf.ones((self.batch_size, 2)) if is_posterior: is_posterior_tf = is_posterior_tf * [1, 0] else: is_posterior_tf = is_posterior_tf * [0, 1] results = [] for h, w, b in itertools.product(range(H), range(W), range(self.B)): built = dict() partial_program, features = None, None context = self._get_sequential_context(program, h, w, b, edge_element) base_features = tf.concat([inp_features[:, h, w, b, :], context, is_posterior_tf], axis=1) # --- box --- layer_inp = base_features n_features = self.n_passthrough_features output_size = 8 network_output = self.box_network(layer_inp, output_size + n_features, self. is_training) rep_input, features = tf.split(network_output, (output_size, n_features), axis=1) _built = self._build_box(rep_input, self.is_training, hw=(h, w)) built.update(_built) partial_program = built['local_box'] # --- attr --- if is_posterior: # --- Get object attributes using object encoder --- yt, xt, ys, xs = tf.split(built['normalized_box'], 4, axis=-1) yt, xt, ys, xs = coords_to_image_space( yt, xt, ys, xs, (self.image_height, self.image_width), self.anchor_box, top_left=False) transform_constraints = snt.AffineWarpConstraints.no_shear_2d() warper = snt.AffineGridWarper( (self.image_height, self.image_width), self.object_shape, transform_constraints) _boxes = tf.concat([xs, 2*xt - 1, ys, 2*yt - 1], axis=-1) grid_coords = warper(_boxes) grid_coords = tf.reshape(grid_coords, (self.batch_size, 1, *self.object_shape, 2,)) if self.edge_resampler: glimpse = resampler_edge.resampler_edge(inp, grid_coords) else: glimpse = tf.contrib.resampler.resampler(inp, grid_coords) glimpse = tf.reshape(glimpse, (self.batch_size, *self.object_shape, self.image_depth)) else: glimpse = tf.zeros((self.batch_size, *self.object_shape, self.image_depth)) # Create the object encoder network regardless of is_posterior, otherwise messes with ScopedFunction encoded_glimpse = self.object_encoder(glimpse, (1, 1, self.A), self.is_training) encoded_glimpse = tf.reshape(encoded_glimpse, (self.batch_size, self.A)) if not is_posterior: encoded_glimpse = tf.zeros_like(encoded_glimpse) layer_inp = tf.concat( [base_features, features, encoded_glimpse, partial_program], axis=1) network_output = self.attr_network(layer_inp, 2 * self.A + n_features, self. is_training) attr_mean, attr_log_std, features = tf.split(network_output, (self.A, self.A, n_features), axis=1) attr_std = self.std_nonlinearity(attr_log_std) attr = Normal(loc=attr_mean, scale=attr_std).sample() built.update(attr_mean=attr_mean, attr_std=attr_std, attr=attr, glimpse=glimpse) partial_program = tf.concat([partial_program, built['attr']], axis=1) # --- z --- layer_inp = tf.concat([base_features, features, partial_program], axis=1) n_features = self.n_passthrough_features network_output = self.z_network(layer_inp, 2 + n_features, self.is_training) z_mean, z_log_std, features = tf.split(network_output, (1, 1, n_features), axis=1) z_std = self.std_nonlinearity(z_log_std) z_mean = self.training_wheels * tf.stop_gradient(z_mean) + (1-self.training_wheels) * z_mean z_std = self.training_wheels * tf.stop_gradient(z_std) + (1-self.training_wheels) * z_std z_logit = Normal(loc=z_mean, scale=z_std).sample() z = self.z_nonlinearity(z_logit) built.update(z_logit_mean=z_mean, z_logit_std=z_std, z_logit=z_logit, z=z) partial_program = tf.concat([partial_program, built['z']], axis=1) # --- obj --- layer_inp = tf.concat([base_features, features, partial_program], axis=1) rep_input = self.obj_network(layer_inp, 1, self.is_training) _built = self._build_obj(rep_input, self.is_training) built.update(_built) partial_program = tf.concat([partial_program, built['obj']], axis=1) # --- final --- results.append(built) program[h, w, b] = partial_program assert program[h, w, b].shape[1] == total_sample_size objects = AttrDict() for k in results[0]: objects[k] = tf.stack([r[k] for r in results], axis=1) if prop_state is not None: objects.prop_state = tf.tile(prop_state[0:1, None], (self.batch_size, self.HWB, 1)) objects.prior_prop_state = tf.tile(prop_state[0:1, None], (self.batch_size, self.HWB, 1)) # --- misc --- objects.n_objects = tf.fill((self.batch_size,), self.HWB) objects.pred_n_objects = tf.reduce_sum(objects.obj, axis=(1, 2)) objects.pred_n_objects_hard = tf.reduce_sum(tf.round(objects.obj), axis=(1, 2)) return objects
def masked_conv_v3(u, filter_1, filter_2, mask_1, mask_2, conductivity_1, conductivity_2, filter_banks): ''' problematic in d_matrix corner and conv_corner :param u: :param filter_1: :param filter_2: :param mask_1: :param mask_2: :param conductivity_1: :param conductivity_2: :param filter_banks: :return: ''' boundary_mask = tf.round(mask_1 + 0.1) + tf.round(mask_2 + 0.1) - 1 boundary_d_matrix = boundary_mask * (-8 / 3. * (conductivity_1 + conductivity_2) / 2.) mat1_d_matrix = tf.round(mask_1 - 0.1) * (-8 / 3. * conductivity_1) mat2_d_matrix = tf.round(mask_2 - 0.1) * (-8 / 3. * conductivity_2) d_matrix = boundary_d_matrix + mat1_d_matrix + mat2_d_matrix d_u = d_matrix * u # padded_input = tf.pad(u * mask_1, [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary # output_1 = tf.nn.conv2d(input=padded_input, filter=filter_banks['filter_1_side'], strides=[1, 1, 1, 1], padding='VALID') # padded_input = tf.pad(u * mask_2, [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary # output_2 = tf.nn.conv2d(input=padded_input, filter=filter_banks['filter_2_side'], strides=[1, 1, 1, 1], padding='VALID') # side_conv_res = output_1 + output_2 padded_input = tf.pad( u * tf.round(mask_1 + 0.1), [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary output_1_side = tf.nn.conv2d(input=padded_input, filter=filter_banks['filter_1_side'], strides=[1, 1, 1, 1], padding='VALID') padded_input = tf.pad( u * tf.round(mask_2 + 0.1), [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary output_2_side = tf.nn.conv2d(input=padded_input, filter=filter_banks['filter_2_side'], strides=[1, 1, 1, 1], padding='VALID') res1 = output_1_side * mask_1 + output_2_side * mask_2 padded_input = tf.pad( u * mask_1, [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary output_1_side_refine = tf.nn.conv2d(input=padded_input, filter=filter_banks['filter_1_side'], strides=[1, 1, 1, 1], padding='VALID') padded_input = tf.pad( u * mask_2, [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary output_2_side_refine = tf.nn.conv2d(input=padded_input, filter=filter_banks['filter_2_side'], strides=[1, 1, 1, 1], padding='VALID') res2 = output_1_side_refine * mask_1 + output_2_side_refine * mask_1 side_conv_res = res1 * (tf.round(mask_1-0.1) + tf.round(mask_2-0.1)) + \ res2 * (tf.ones_like(mask_1) - tf.round(mask_1-0.1) - tf.round(mask_2-0.1)) padded_input = tf.pad( u * tf.round(mask_1 + 0.1), [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary output_1_corner = tf.nn.conv2d(input=padded_input, filter=filter_banks['filter_1_corner'], strides=[1, 1, 1, 1], padding='VALID') padded_input = tf.pad( u * tf.round(mask_2 + 0.1), [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary output_2_corner = tf.nn.conv2d(input=padded_input, filter=filter_banks['filter_2_corner'], strides=[1, 1, 1, 1], padding='VALID') corner_conv_res = output_1_corner + output_2_corner LU_u = corner_conv_res + side_conv_res result = d_u + LU_u tmp = { 'result': result, 'LU_u': LU_u, 'd_matrix': d_matrix, 'res1': res1, 'res2': res2, 'side_conv_res': side_conv_res, 'output_1_side': output_1_side, 'output_2_side': output_2_side, 'output_1_side_refine': output_1_side_refine, 'output_2_side_refine': output_2_side_refine, 'corner_conv_res': corner_conv_res, 'output_1_corner': output_1_corner, 'output_2_corner': output_2_corner, } return tmp
def masked_conv_v1(u, filter_1, filter_2, mask_1, mask_2, conductivity_1, conductivity_2, eps=0.01, filter_banks=None): ''' convolution with two different kernels :param u: input image :param filter_1: :param filter_2: :param mask_1: region where filter_1 is applied, with value (0.5-eps,0.5+eps) on the boundary :param mask_2: region where filter_2 is applied, with value (0.5-eps,0.5+eps) on the boundary :param eps: some numerical consideration :return: ''' boundary_mask = tf.round(mask_1 + 0.1) + tf.round(mask_2 + 0.1) - 1 boundary_d_matrix = boundary_mask * (-8 / 3. * (conductivity_1 + conductivity_2) / 2.) mat1_d_matrix = tf.round(mask_1 - 0.1) * (-8 / 3. * conductivity_1) mat2_d_matrix = tf.round(mask_2 - 0.1) * (-8 / 3. * conductivity_2) d_matrix = boundary_d_matrix + mat1_d_matrix + mat2_d_matrix padded_input = tf.pad( u * tf.round(mask_1 + eps), [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary output_1 = tf.nn.conv2d(input=padded_input, filter=filter_1, strides=[1, 1, 1, 1], padding='VALID') padded_input = tf.pad( u * tf.round(mask_2 + eps), [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary output_2 = tf.nn.conv2d(input=padded_input, filter=filter_2, strides=[1, 1, 1, 1], padding='VALID') res1 = output_1 * mask_1 + output_2 * mask_2 padded_input = tf.pad( u * mask_1, [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary output_1 = tf.nn.conv2d(input=padded_input, filter=filter_1, strides=[1, 1, 1, 1], padding='VALID') padded_input = tf.pad( u * mask_2, [[0, 0], [1, 1], [1, 1], [0, 0]], "SYMMETRIC") # convolution with symmetric padding at boundary output_2 = tf.nn.conv2d(input=padded_input, filter=filter_2, strides=[1, 1, 1, 1], padding='VALID') res2 = output_1 + output_2 LU_u = res1 * (tf.round(mask_1) + tf.round(mask_2)) + \ res2 * (tf.ones_like(mask_1) - tf.round(mask_1) - tf.round(mask_2)) result = LU_u + (d_matrix * u) tmp = { 'd_matrix': d_matrix, 'LU_u': LU_u, 'result': result, } return tmp
) # <tf.Tensor 'logistic_loss:0' shape=(1, 1) dtype=float32> # Create Optimizer my_opt = tf.train.GradientDescentOptimizer(0.05) train_step = my_opt.minimize(xentropy) # Run loop for i in range(1400): rand_index = np.random.choice(100) rand_x = [x_vals[rand_index]] rand_y = [y_vals[rand_index]] sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y}) if (i + 1) % 200 == 0: print('Step #' + str(i + 1) + ' A = ' + str(sess.run(A))) print('Loss = ' + str( sess.run(xentropy, feed_dict={ x_data: rand_x, y_target: rand_y }))) # Evaluate Predictions predictions = [] for i in range(len(x_vals)): x_val = [x_vals[i]] prediction = sess.run(tf.round(tf.sigmoid(my_output)), feed_dict={x_data: x_val}) predictions.append(prediction[0]) accuracy = sum(x == y for x, y in zip(predictions, y_vals)) / 100. print('Ending Accuracy = ' + str(np.round(accuracy, 2)))
w1 = tf.Variable(tf.random_normal([2, 3])) b1 = tf.Variable(tf.zeros([3])) # Operations z1 = tf.matmul(tf_features, w1) + b1 a1 = tf.nn.sigmoid(z1) # Output neuron w2 = tf.Variable(tf.random_normal([3, 1])) b2 = tf.Variable(tf.zeros([1])) # Operations z2 = tf.matmul(a1, w2) + b2 py = tf.nn.sigmoid(z2) cost = tf.reduce_mean(tf.square(py - tf_targets)) correct_prediction = tf.equal(tf.round(py), tf_targets) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1) train = optimizer.minimize(cost) sess = tf.Session() sess.run(tf.global_variables_initializer()) for e in range(100): sess.run(train, feed_dict={tf_features: features, tf_targets: targets}) print( "accuracy =", sess.run(accuracy,
U=tf.split(axis = 1, num_or_size_splits = D, value = _U) loss+=each_loss(U) ''' # Actual Prediction for test data _Ut = tf.contrib.distributions.Uniform().sample(sample_shape=(300, D)) Ut = tf.split(axis=1, num_or_size_splits=D, value=_Ut) _w_t = [f(u) for f, u in zip(F, Ut)] Wt = [minus_inf_inf_to_zero_one(__w) for __w in _w_t] model_output_t = B_NN_Logistic_regression_Model_output(X, Wt) A = tf.reduce_mean(tf.sigmoid(model_output_t), 1) prediction = tf.round(A) predictions_correct = tf.cast(tf.equal(prediction[:, None], Y), tf.float32) accuracy = tf.reduce_mean(predictions_correct) optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss) with tf.Session() as sess: writer = tf.summary.FileWriter('./graphs/B_NN_logistic_reg', sess.graph) start_time = time.time() sess.run(tf.global_variables_initializer()) n_batches = 50 #int(N_train/batch_size) U_batches = 1 #int(Batch_U/minibatch_U) for i in range(1000): # train the model n_epochs times print(i) for _ in range(1):
def resize_label(label, size, alpha=63.0): return tf.round(tf.image.resize(label, size, method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) / alpha) * alpha
def accuracy(labels, predictions, weights): return tf.metrics.accuracy(labels, tf.round(predictions), weights)
import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation dim1 = int(sys.argv[1]) dim2 = int(sys.argv[2]) gen = int(sys.argv[3]) flag = None if (len(sys.argv) == 5): flag = sys.argv[4] fig = plt.figure() # Declare tf variables env = tf.Variable(tf.abs( tf.round(tf.truncated_normal(shape=(dim1, dim2), mean=0, stddev=0.375))), name='state') ''' Testing matrix for blinkers/stationary lifeforms env = tf.Variable([[0, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0], [0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 1, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0]], dtype=tf.float32) ''' neighbour_sum = tf.placeholder(tf.float32, shape=env.get_shape()) alive_dead = tf.placeholder(tf.float32, shape=env.get_shape()) combin = tf.placeholder(tf.float32, shape=env.get_shape())
for i in range(1800): rand_index = np.random.choice(len(x_vals_train), size=batch_size) rand_x = [x_vals_train[rand_index]] rand_y = [y_vals_train[rand_index]] sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y}) if (i + 1) % 100 == 0: print('Step#' + str(i + 1) + ' A=' + str(sess.run(A))) print('Loss=' + str( sess.run(xentropy, feed_dict={ x_data: rand_x, y_target: rand_y }))) #为啦评估训练模型,创建预测操作。用squeeze()函数封装预测程序,使得预测值与目标值有相同的维度 equal()检测是否 #相等,将true 或 false转化为float32,取其平均值 y_prediction = tf.squeeze(tf.round(tf.nn.sigmoid(tf.add(x_data, A)))) correct_prediction = tf.equal(y_prediction, y_target) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) acc_value_test = sess.run(accuracy, feed_dict={ x_data: [x_vals_test], y_target: [y_vals_test] }) acc_value_train = sess.run(accuracy, feed_dict={ x_data: [x_vals_train], y_target: [y_vals_train] }) print('Accuracy on train set:' + str(acc_value_train)) print('Accuracy on test set:' + str(acc_value_test))
def forward(self): config = self.config N, PL, QL, CL, d, dc, nh = config.batch_size if not self.demo else tf.shape(self.c)[0], self.c_maxlen, self.q_maxlen, config.disc_char_limit, config.disc_hidden, config.disc_char_dim, config.disc_num_heads with tf.variable_scope("Input_Embedding_Layer"): ch_emb = tf.reshape(tf.nn.embedding_lookup( self.char_mat, self.ch), [N * PL, CL, dc]) qh_emb = tf.reshape(tf.nn.embedding_lookup( self.char_mat, self.qh), [N * QL, CL, dc]) ch_emb = tf.nn.dropout(ch_emb, 1.0 - 0.5 * self.dropout) qh_emb = tf.nn.dropout(qh_emb, 1.0 - 0.5 * self.dropout) # Bidaf style conv-highway encoder ch_emb = conv(ch_emb, d, bias = True, activation = tf.nn.relu, kernel_size = 5, name = "char_conv", reuse = None) qh_emb = conv(qh_emb, d, bias = True, activation = tf.nn.relu, kernel_size = 5, name = "char_conv", reuse = True) ch_emb = tf.reduce_max(ch_emb, axis = 1) qh_emb = tf.reduce_max(qh_emb, axis = 1) ch_emb = tf.reshape(ch_emb, [N, PL, ch_emb.shape[-1]]) qh_emb = tf.reshape(qh_emb, [N, QL, ch_emb.shape[-1]]) c_emb = tf.nn.dropout(tf.nn.embedding_lookup(self.word_mat, self.c), 1.0 - self.dropout) q_emb = tf.nn.dropout(tf.nn.embedding_lookup(self.word_mat, self.q), 1.0 - self.dropout) # Is context token in answer? context_ix = tf.tile(tf.expand_dims(tf.range(PL, dtype=tf.int32),axis=0), [N,1]) gt_start = tf.greater_equal(context_ix, tf.tile(tf.expand_dims(self.ans_start,axis=-1), [1, PL])) lt_end = tf.less_equal(context_ix, tf.tile(tf.expand_dims(self.ans_end,axis=-1), [1, PL])) self.in_answer_feature = tf.expand_dims(tf.cast(tf.logical_and(gt_start, lt_end), tf.float32),axis=2) # c_emb = tf.concat([c_emb, self.in_answer_feature, ch_emb], axis=2) # q_emb = tf.concat([q_emb, tf.zeros([N, QL, 1]), qh_emb], axis=2) c_emb = tf.concat([c_emb, ch_emb], axis=2) q_emb = tf.concat([q_emb, qh_emb], axis=2) c_emb = highway(c_emb, size = d, scope = "highway", dropout = self.dropout, reuse = None) q_emb = highway(q_emb, size = d, scope = "highway", dropout = self.dropout, reuse = True) with tf.variable_scope("Embedding_Encoder_Layer"): c = residual_block(c_emb, num_blocks = 1, num_conv_layers = 4, kernel_size = 7, mask = self.c_mask, num_filters = d, num_heads = nh, seq_len = self.c_len, scope = "Encoder_Residual_Block", bias = False, dropout = self.dropout) q = residual_block(q_emb, num_blocks = 1, num_conv_layers = 4, kernel_size = 7, mask = self.q_mask, num_filters = d, num_heads = nh, seq_len = self.q_len, scope = "Encoder_Residual_Block", reuse = True, # Share the weights between passage and question bias = False, dropout = self.dropout) with tf.variable_scope("Context_to_Query_Attention_Layer"): # C = tf.tile(tf.expand_dims(c,2),[1,1,self.q_maxlen,1]) # Q = tf.tile(tf.expand_dims(q,1),[1,self.c_maxlen,1,1]) # S = trilinear([C, Q, C*Q], input_keep_prob = 1.0 - self.dropout) S = optimized_trilinear_for_attention([c, q], self.c_maxlen, self.q_maxlen, input_keep_prob = 1.0 - self.dropout) mask_q = tf.expand_dims(self.q_mask, 1) S_ = tf.nn.softmax(mask_logits(S, mask = mask_q)) mask_c = tf.expand_dims(self.c_mask, 2) S_T = tf.transpose(tf.nn.softmax(mask_logits(S, mask = mask_c), dim = 1),(0,2,1)) self.c2q = tf.matmul(S_, q) self.q2c = tf.matmul(tf.matmul(S_, S_T), c) attention_outputs = [c, self.c2q, c * self.c2q, c * self.q2c] with tf.variable_scope("Model_Encoder_Layer"): inputs = tf.concat(attention_outputs, axis = -1) self.enc = [conv(inputs, d, name = "input_projection")] for i in range(3): if i % 2 == 0: # dropout every 2 blocks self.enc[i] = tf.nn.dropout(self.enc[i], 1.0 - self.dropout) self.enc.append( residual_block(self.enc[i], num_blocks = 7, num_conv_layers = 2, kernel_size = 5, mask = self.c_mask, num_filters = d, num_heads = nh, seq_len = self.c_len, scope = "Model_Encoder", bias = False, reuse = True if i > 0 else None, dropout = self.dropout) ) # with tf.variable_scope("Output_Layer"): # start_logits = tf.squeeze(conv(tf.concat([self.enc[1], self.enc[2]],axis = -1),1, bias = False, name = "start_pointer"),-1) # end_logits = tf.squeeze(conv(tf.concat([self.enc[1], self.enc[3]],axis = -1),1, bias = False, name = "end_pointer"), -1) # self.logits = [mask_logits(start_logits, mask = self.c_mask), # mask_logits(end_logits, mask = self.c_mask)] # # logits1, logits2 = [l for l in self.logits] # # outer = tf.matmul(tf.expand_dims(tf.nn.softmax(logits1), axis=2), # tf.expand_dims(tf.nn.softmax(logits2), axis=1)) # outer = tf.matrix_band_part(outer, 0, config.disc_ans_limit) # self.yp1 = tf.argmax(tf.reduce_max(outer, axis=2), axis=1) # self.yp2 = tf.argmax(tf.reduce_max(outer, axis=1), axis=1) # losses = tf.nn.sparse_softmax_cross_entropy_with_logits( # logits=logits1, labels=self.ans_start) # losses2 = tf.nn.sparse_softmax_cross_entropy_with_logits( # logits=logits2, labels=self.ans_end) # self.loss = tf.reduce_mean(losses + losses2) with tf.variable_scope("Output_Layer"): pooled = [tf.reduce_max(enc, axis=1, keep_dims=True) for enc in self.enc] ans_enc = tf.reduce_sum(self.enc[2]*self.in_answer_feature, axis=1, keep_dims=True)/(1e-6+tf.reduce_sum(self.in_answer_feature, axis=1, keep_dims=True)) pooled = tf.squeeze(tf.concat(pooled[0:2]+[ans_enc], axis=2), 1) hidden = tf.layers.dense(pooled,64,activation=tf.nn.relu, use_bias = False, name = "hidden") logits = tf.squeeze(tf.layers.dense(hidden,1, activation=None, use_bias = False, name = "logits"),-1) self.nll = tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.cast(self.gold_class, tf.float32), logits=logits) self.loss = tf.reduce_mean(self.nll) self.probs = tf.nn.sigmoid(logits) self.pred = tf.cast(tf.round(self.probs),tf.int32) self.equality = tf.equal(self.pred, self.gold_class) self.accuracy = tf.reduce_mean(tf.cast(self.equality, tf.float32)) if config.disc_l2_norm is not None: variables = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES) l2_loss = tf.contrib.layers.apply_regularization(regularizer, variables) self.loss += l2_loss self._train_summaries.append(tf.summary.scalar('train_loss/loss', self.loss)) self._train_summaries.append(tf.summary.scalar('train_loss/accuracy', self.accuracy)) if config.disc_decay is not None: self.var_ema = tf.train.ExponentialMovingAverage(config.disc_decay) ema_op = self.var_ema.apply(tf.trainable_variables()) with tf.control_dependencies([ema_op]): self.loss = tf.identity(self.loss) self.assign_vars = [] for var in tf.global_variables(): v = self.var_ema.average(var) if v: self.assign_vars.append(tf.assign(var,v))
}), '|', 'loss:', c) print(epoch, ' : ', epoch_loss / steps) save_path = saver.save(sess, os.path.join( log_dir, 'model.ckpt')) #, global_step=epoch) print('model save to ', save_path) # test batch_x = np.reshape( mnist.test.images, [-1, img_Pixels_h, img_Pixels_w, img_Pixels_c]) print('准确率: ', accuracy.eval({x: batch_x, y: mnist.test.labels})) if train == -1: # Output encoding feat = net feat = tf.round(feat) feat = sess.run( feat, { x: np.reshape(mnist.test.images, [-1, img_Pixels_h, img_Pixels_w, img_Pixels_c]) }) # [1000,48] # 转成对应的十进制数 feat = feat.astype(np.int64) feat_Decimal = [] # 十进制数 for a in feat: c = ''.join(str(a)[1:-1]).replace(' ', '').replace('\n', '') feat_Decimal.append(int(c, 2)) labels = mnist.test.labels # [10000,] '''
domain_name='Forward', grad_domain_name='Gradient', enabled=ENABLE_NVTX) x = tf.layers.dense(x, 1, activation=None, name='dense_5') x = nvtx_tf.ops.end(x, nvtx_context) predictions = x return predictions logits = DenseBinaryClassificationNet(inputs=features_plh) loss = tf.math.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels_plh)) acc = tf.math.reduce_mean( tf.metrics.accuracy(labels=labels_plh, predictions=tf.round(tf.nn.sigmoid(logits)))) optimizer = tf.train.MomentumOptimizer(learning_rate=0.01, momentum=0.9, use_nesterov=True).minimize(loss) # Initialize variables. local variables are needed to be initialized for tf.metrics.* init_g = tf.global_variables_initializer() init_l = tf.local_variables_initializer() nvtx_callback = NVTXHook(skip_n_steps=1, name='Train') # Start training with tf.train.MonitoredSession(hooks=[nvtx_callback]) as sess: sess.run([init_g, init_l]) # Run graph
def model_fn(features, labels, mode, params): layers = list(map(int, params["deep_layers"].split(','))) # Not following the paper using dense feature here. # Due to numerical stability linear_net = tf.feature_column.input_layer( features, params['linear_feature_columns']) embedding_net = tf.feature_column.input_layer( features, params['embedding_feature_columns']) x0 = embedding_net cross_dim = x0.shape[1] # with tf.name_scope('linear_net'): # linear_y = tf.layers.dense(linear_net, 1, activation=tf.nn.relu) with tf.variable_scope('cross_layers'): xl = x0 for i in range(FLAGS.cross_layers): # wl = tf.reshape(cross_weight[i], shape=[-1, 1]) # (dim * 1) # xlw = tf.matmul(xl, wl) # (? * 1) # xl = x0 * xlw + xl + cross_bias[i] # (? * dim) with tf.variable_scope('cross_{}'.format(i)): w = tf.get_variable("weight", [cross_dim], initializer=tf.glorot_normal_initializer()) b = tf.get_variable("bias", [cross_dim], initializer=tf.glorot_normal_initializer()) xw = tf.tensordot(tf.reshape(xl, [-1, 1, cross_dim]), w, 1) xl = xw * x0 + xl + b with tf.variable_scope('deep_layers'): dnn_net = x0 for i in layers: dnn_net = tf.layers.dense(dnn_net, i, activation=tf.nn.relu) dnn_net = tf.layers.batch_normalization( dnn_net, training=(mode == estimator.ModeKeys.TRAIN)) dnn_net = tf.layers.dropout( dnn_net, rate=params['dropout'], training=(mode == estimator.ModeKeys.TRAIN)) logits = tf.concat([dnn_net, xl], axis=-1) logits = tf.layers.dense(logits, units=1, activation=None) pred = tf.sigmoid(logits) predictions = {"prob": pred} export_outputs = { tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: estimator.export.PredictOutput(predictions) } if mode == estimator.ModeKeys.PREDICT: return estimator.EstimatorSpec(mode=mode, predictions=predictions, export_outputs=export_outputs) loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=tf.cast( labels, tf.float32))) eval_metric_ops = { "AUC": tf.metrics.auc(labels, pred), 'Accuracy': tf.metrics.accuracy(labels, predictions=tf.round(pred)) } if mode == estimator.ModeKeys.EVAL: return estimator.EstimatorSpec(mode=mode, predictions=predictions, loss=loss, eval_metric_ops=eval_metric_ops) optimizer = tf.train.AdamOptimizer(learning_rate=params['learning_rate']) train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step()) if mode == estimator.ModeKeys.TRAIN: return estimator.EstimatorSpec(mode=mode, predictions=predictions, loss=loss, train_op=train_op)
dtype=tf.float32, shape=[1, 10], initializer=tf.contrib.layers.xavier_initializer()) b1 = tf.get_variable("b1", dtype=tf.float32, shape=[1, 1], initializer=tf.zeros_initializer) Z1 = tf.add(tf.matmul(W1, A0), b1) cost = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(labels=y, logits=Z1, name="Activation")) predict = tf.sigmoid(Z1) success_rate = tf.multiply( tf.reduce_mean(tf.cast(tf.equal(tf.round(predict), y), tf.float32)), 100) train_step = tf.train.AdamOptimizer().minimize(cost) #train_step = tf.train.MomentumOptimizer(learning_rate = 0.01, momentum=0.9, name="Optimizer").minimize(A1) #train_step = tf.train.AdamOptimizer(name="Optimizer").minimize(A1) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) writer = tf.summary.FileWriter("/tmp/tensor_classify") writer.add_graph(sess.graph) num_epochs = 2000 costs = [] np.random.seed(1) for i in range(num_epochs): mini_batches = shuffle_data(X, Y)
def _compute_obj_kl(self, tensors, existing_objects=None): # --- compute obj_kl --- obj_pre_sigmoid = tensors["obj_pre_sigmoid"] obj_log_odds = tensors["obj_log_odds"] obj_prob = tensors["obj_prob"] obj = tensors["obj"] batch_size, n_objects, _ = tf_shape(obj) max_n_objects = n_objects if existing_objects is not None: _, n_existing_objects, _ = tf_shape(existing_objects) existing_objects = tf.reshape(existing_objects, (batch_size, n_existing_objects)) max_n_objects += n_existing_objects count_support = tf.range(max_n_objects+1, dtype=tf.float32) if self.count_prior_dist is not None: if self.count_prior_dist is not None: assert len(self.count_prior_dist) == (max_n_objects + 1) count_distribution = tf.constant(self.count_prior_dist, dtype=tf.float32) else: count_prior_prob = tf.nn.sigmoid(self.count_prior_log_odds) count_distribution = count_prior_prob ** count_support normalizer = tf.reduce_sum(count_distribution) count_distribution = count_distribution / tf.maximum(normalizer, 1e-6) count_distribution = tf.tile(count_distribution[None, :], (batch_size, 1)) if existing_objects is not None: count_so_far = tf.reduce_sum(tf.round(existing_objects), axis=1, keepdims=True) count_distribution = ( count_distribution * tf_binomial_coefficient(count_support, count_so_far) * tf_binomial_coefficient(max_n_objects - count_support, n_existing_objects - count_so_far) ) normalizer = tf.reduce_sum(count_distribution, axis=1, keepdims=True) count_distribution = count_distribution / tf.maximum(normalizer, 1e-6) else: count_so_far = tf.zeros((batch_size, 1), dtype=tf.float32) obj_kl = [] for i in range(n_objects): p_z_given_Cz_raw = (count_support[None, :] - count_so_far) / (max_n_objects - i) p_z_given_Cz = tf.clip_by_value(p_z_given_Cz_raw, 0.0, 1.0) # Doing this instead of 1 - p_z_given_Cz seems to be more numerically stable. inv_p_z_given_Cz_raw = (max_n_objects - i - count_support[None, :] + count_so_far) / (max_n_objects - i) inv_p_z_given_Cz = tf.clip_by_value(inv_p_z_given_Cz_raw, 0.0, 1.0) p_z = tf.reduce_sum(count_distribution * p_z_given_Cz, axis=1, keepdims=True) if self.use_concrete_kl: prior_log_odds = tf_safe_log(p_z) - tf_safe_log(1-p_z) _obj_kl = concrete_binary_sample_kl( obj_pre_sigmoid[:, i, :], obj_log_odds[:, i, :], self.obj_concrete_temp, prior_log_odds, self.obj_concrete_temp, ) else: prob = obj_prob[:, i, :] _obj_kl = ( prob * (tf_safe_log(prob) - tf_safe_log(p_z)) + (1-prob) * (tf_safe_log(1-prob) - tf_safe_log(1-p_z)) ) obj_kl.append(_obj_kl) sample = tf.to_float(obj[:, i, :] > 0.5) mult = sample * p_z_given_Cz + (1-sample) * inv_p_z_given_Cz raw_count_distribution = mult * count_distribution normalizer = tf.reduce_sum(raw_count_distribution, axis=1, keepdims=True) normalizer = tf.maximum(normalizer, 1e-6) # invalid = tf.logical_and(p_z_given_Cz_raw > 1, count_distribution > 1e-8) # float_invalid = tf.cast(invalid, tf.float32) # diagnostic = tf.stack( # [float_invalid, p_z_given_Cz, count_distribution, mult, raw_count_distribution], axis=-1) # assert_op = tf.Assert( # tf.reduce_all(tf.logical_not(invalid)), # [invalid, diagnostic, count_so_far, sample, tf.constant(i, dtype=tf.float32)], # summarize=100000) count_distribution = raw_count_distribution / normalizer count_so_far += sample # this avoids buildup of inaccuracies that can cause problems in computing p_z_given_Cz_raw count_so_far = tf.round(count_so_far) obj_kl = tf.reshape(tf.concat(obj_kl, axis=1), (batch_size, n_objects, 1)) return obj_kl
def _build_model(self): with tf.variable_scope('inputs'): self.X = tf.placeholder(shape=[None, self.sentence_length],dtype=tf.int32,name="X") print (self.X) self.y = tf.placeholder(shape=[None,1], dtype=tf.float32,name="y") #self.dropout = tf.placeholder_with_default(1.,None,name="dropout") self.emd_placeholder = tf.placeholder(tf.float32,shape=[self.n_words,self.embedding_dim]) with tf.variable_scope('embedding'): # create embedding variable # self.emb_W = tf.Variable(initial_value=self.embedding.get_w(), name="emb_W", trainable=self.embedding.is_trainable(), # dtype=tf.float32) self.emb_W =tf.get_variable('word_embeddings',[self.n_words, self.embedding_dim],initializer=tf.random_uniform_initializer(-1, 1, 0),trainable=True,dtype=tf.float32) self.assign_ops = tf.assign(self.emb_W,self.emd_placeholder) # do embedding lookup self.embedding_input = tf.nn.embedding_lookup(self.emb_W,self.X,"embedding_input") print( self.embedding_input ) self.embedding_input = tf.unstack(self.embedding_input,self.sentence_length,1) #rint( self.embedding_input) # define the GRU cell with tf.variable_scope('LSTM_cell'): self.cell = tf.nn.rnn_cell.BasicLSTMCell(self.hidden_states) # define the RNN operation with tf.variable_scope('ops'): self.output, self.state = tf.nn.static_rnn(self.cell,self.embedding_input,dtype=tf.float32) # print self.state print ('state') print(self.output) print(self.state) # with tf.variable_scope('dropout'): # self.state = tf.nn.dropout(self.state,self.dropout) # print self.state with tf.variable_scope('classifier'): self.w = tf.get_variable(name="W", shape=[self.hidden_states,1],dtype=tf.float32) self.b = tf.get_variable(name="b", shape=[1], dtype=tf.float32) self.l2_loss = tf.nn.l2_loss(self.w,name="l2_loss") # print self.output # print self.state # print self.w # print self.b self.scores = tf.nn.xw_plus_b(self.output[-1],self.w,self.b,name="logits") self.prediction_probability = tf.nn.sigmoid(self.scores,name='positive_sentiment_probability') print (self.prediction_probability) self.predictions = tf.round(self.prediction_probability,name='final_prediction') self.losses = tf.nn.sigmoid_cross_entropy_with_logits(logits=self.scores,labels=self.y) self.loss = tf.reduce_mean(self.losses) + self.lambda1*self.l2_loss tf.summary.scalar('loss', self.loss) self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.losses) self.correct_predictions = tf.equal(self.predictions,tf.round(self.y)) print (self.correct_predictions) self.accuracy = tf.reduce_mean(tf.cast(self.correct_predictions, "float"), name="accuracy") tf.summary.scalar('accuracy', self.accuracy)
y_hat = tf.nn.xw_plus_b(drop, W, b) y_hat = tf.squeeze(y_hat) tf.summary.histogram('W', W) with tf.name_scope('Metrics'): # Cross-entropy loss and optimizer initialization loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=y_hat, labels=target_ph)) tf.summary.scalar('loss', loss) optimizer = tf.train.AdamOptimizer(learning_rate=1e-3).minimize(loss) # Accuracy metric accuracy = tf.reduce_mean( tf.cast(tf.equal(tf.round(tf.sigmoid(y_hat)), target_ph), tf.float32)) tf.summary.scalar('accuracy', accuracy) merged = tf.summary.merge_all() # Batch generators train_batch_generator = batch_generator(X_train, y_train, BATCH_SIZE) test_batch_generator = batch_generator(X_test, y_test, BATCH_SIZE) train_writer = tf.summary.FileWriter(LOG_PATH + os.pathsep + 'train', accuracy.graph) test_writer = tf.summary.FileWriter(LOG_PATH + os.pathsep + 'test', accuracy.graph) session_conf = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))