コード例 #1
0
    def _train_step(self, inputs, output):
        with tf.GradientTape() as g:
            z = self.call(inputs)
            loss = mse(output, z)

        gradients = g.gradient(loss, self.trainable_variables)
        self.optimizer.apply_gradients(zip(gradients,
                                           self.trainable_variables))
        return tf.reduce_sum(loss)
コード例 #2
0
        def extended_kullback_leibler(y_true, y_pred):
            # ld_x, ld_y = self._labeled_data

            len_actual_labels = int(max(self._y_labeled))
            y_labeled = np.zeros((self._y_labeled.size, self._n_clusters))
            y_labeled[np.arange(self._y_labeled.size),self._y_labeled] = 1
            
            def loss(pred):
                actual_labels = [[] for _ in range(len_actual_labels)]

                for i, actual_label in enumerate(self._y_labeled):
                    if actual_label:
                        actual_labels[int(actual_label) - 1].append(i)

                pred_labels = K.argmax(pred, axis=1)
                predicted_labels = []
                for i in range(self._n_clusters):
                    indices = tf.where(tf.equal(pred_labels, i))
                    predicted_labels.append(indices)

                def predicted_distance(i, j):
                    # binary metric
                    # 1, if examples are in the same cluster
                    i_tens, j_tens = tf.dtypes.cast(i, tf.float32), tf.dtypes.cast(j, tf.float32)

                    for cluster in predicted_labels:
                        # y = tf.split(cluster, cluster.shape[1], axis=1)

                        for tens_i in range(cluster.shape[1]):
                            for tens_j in range(cluster.shape[1]):
                                if tens_i != tens_j:
                                    if cluster[tens_i] == i_tens and cluster[tens_j] == j_tens:
                                        return tf.dtypes.cast(0., tf.float32)

                    return tf.dtypes.cast(1., tf.float32)

                loss = 0.
                for labeled_cluster in actual_labels:
                    # for each gold cluster find if it's objects are in the same predicted cluster
                    for i in range(len(labeled_cluster)):
                        for j in range(len(labeled_cluster) + 1):
                            loss += predicted_distance(i, j)

                # ss_loss = 1. - 1e1 * loss / (self._batch_size - 1)  # 1. - 1e1 *  batch_size * sum / nCr(batch_size, 2)
                # ss_loss = loss / sum([len(labeled_cluster) * len(labeled_cluster) for labeled_cluster in actual_labels[1:]])
                ss_loss = loss / tf.dtypes.cast(len(predicted_labels), tf.float32)
                return ss_loss
            
            #result = loss(y_pred[-len_actual_labels:]) + kld(y_true[:-len_actual_labels], y_pred[:-len_actual_labels])
            result = 0.7 * mse(y_pred[-len(y_labeled):], 
                         tf.convert_to_tensor(y_labeled, dtype=tf.float32)) +\
                     0.3 * kld(y_true[:-len(self._y_labeled)], 
                         y_pred[:-len(self._y_labeled)])

            return result
コード例 #3
0
 def custom_loss_fn(y_true, y_pred):
     """MSE supports RaggedTensors directly."""
     return losses_mod.mse(y_true, y_pred)
コード例 #4
0
def main(argv=None):

    img = Image.open('city.png')
    img_resized = letter_box_image(img, size, size, 128)
    img_resized = img_resized.astype(np.float32)
    classes = load_coco_names('coco.names')

    fake_boxes = {2: [(np.array([300, 200, 370, 250]), 1.)]}
    generated_boxes, g_indices = generate_ground_truth(fake_boxes, size, 0.4)
    draw_boxes(copy.deepcopy(generated_boxes), img, classes, (size, size),
               True)
    draw_boxes(copy.deepcopy(fake_boxes), img, classes, (size, size), True)
    # draw_boxes(filtered_boxes, img, classes, (size, size), True)
    img.save('out_fakeboxes.jpg')

    mask = np.zeros([1, 10647])
    for cls, indices in g_indices.items():
        mask[0, indices] = 1

    gt_tensor = np.zeros([1, 10647, 4 + 1 + len(classes)])
    for cls, boxes in generated_boxes.items():
        for i, box in enumerate(boxes):
            class_mask = np.zeros([len(classes)])
            class_mask[cls] = 1
            gt_row = [*np.asarray(box[0]), 1., *class_mask]
            gt_tensor[0, g_indices[cls][i]] = gt_row

    if frozen_model:

        t0 = time.time()
        frozenGraph = load_graph(frozen_model)
        print("Loaded graph in {:.2f}s".format(time.time() - t0))

        boxes, inputs = get_boxes_and_inputs_pb(frozenGraph)

        with frozenGraph.as_default():
            fake_gt = tf.constant(gt_tensor, dtype=tf.float32)
            mask_tensor = tf.constant(mask, dtype=tf.float32)
            fake_loss = mse(fake_gt, boxes) * mask_tensor
            fake_loss = tf.reduce_mean(fake_loss, axis=-1)

            grad_op = tf.gradients(fake_loss, inputs)

        with tf.Session(graph=frozenGraph) as sess:
            t0 = time.time()
            for iters in range(num_iterations):
                grads = sess.run(grad_op, feed_dict={inputs: [img_resized]})

                grad = grads[0][0]
                sigma = (iters * 4.0) / num_iterations + 0.5
                grad_smooth1 = gaussian_filter(grad, sigma=sigma)
                grad_smooth2 = gaussian_filter(grad, sigma=sigma * 2)
                grad_smooth3 = gaussian_filter(grad, sigma=sigma * 0.5)
                grad = (grad_smooth1 + grad_smooth2 + grad_smooth3)

                step_size_scaled = step_size / (np.std(grad) + 1e-8)

                # Update the image by following the gradient.
                mod = grad * step_size_scaled

                grad_img = Image.fromarray(np.uint8(mod + 128))
                grad_img.save('out/grads/{}.png'.format(iters))

                img_resized = np.clip(img_resized - mod, 0, 255)
                new_img = Image.fromarray(np.uint8(img_resized))
                new_img.save('out/images/{}.png'.format(iters))

    else:
        if tiny:
            model = yolo_v3_tiny.yolo_v3_tiny
        else:
            model = yolo_v3.yolo_v3

        boxes, inputs = get_boxes_and_inputs(model, len(classes), size,
                                             data_format)

        saver = tf.train.Saver(var_list=tf.global_variables(scope='detector'))

        with tf.Session() as sess:
            t0 = time.time()
            saver.restore(sess, ckpt_file)
            print('Model restored in {:.2f}s'.format(time.time() - t0))

            t0 = time.time()
            detected_boxes = sess.run(boxes, feed_dict={inputs: [img_resized]})
コード例 #5
0
outputs = decoder(encoder(inputs)[2])
vae = Model(inputs, outputs, name='vae_mlp')

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    help_ = "Load h5 model trained weights"
    parser.add_argument("-w", "--weights", help=help_)
    help_ = "Use mse loss instead of binary cross entropy (default)"
    parser.add_argument("-m", "--mse", help=help_, action='store_true')
    args = parser.parse_args()
    models = (encoder, decoder)
    data = (x_test, y_test)

    # VAE loss = mse_loss or xent_loss + kl_loss
    if args.mse:
        reconstruction_loss = mse(inputs, outputs)
    else:
        reconstruction_loss = binary_crossentropy(inputs, outputs)

    reconstruction_loss *= original_dim
    kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
    kl_loss = K.sum(kl_loss, axis=-1)
    kl_loss *= -0.5
    vae_loss = K.mean(reconstruction_loss + kl_loss)
    vae.add_loss(vae_loss)
    vae.compile(optimizer='adam')
    vae.summary()

    if args.weights:
        vae.load_weights(args.weights)
    else: