Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tensorflow Implementation of Focal Frequency Loss #8

Closed
ZohebAbai opened this issue May 30, 2022 · 4 comments
Closed

Tensorflow Implementation of Focal Frequency Loss #8

ZohebAbai opened this issue May 30, 2022 · 4 comments

Comments

@ZohebAbai
Copy link

As I couldn't find a tensorflow implementation of Focal Frequency Loss, so I created it.

Please visit the Github Repo and PyPi Project.

Use case notebook is included in the Repo. Any feedback is appreciated.

@EndlessSora If you find this implementation useful, kindly do mention it on your README.

Thanks for releasing this.

@EndlessSora
Copy link
Owner

Thank you so much for the nice TensorFlow implementation! Have you tested the code and its performance compared with this official repo?

@ZohebAbai
Copy link
Author

ZohebAbai commented Jun 2, 2022

I have tested the code for sure. The Tensorflow version gives an exact numerical answer as the Pytorch version. Code for test attached below:

from focal_frequency_loss import FocalFrequencyLoss as FFL
from tf_focal_frequency_loss import FocalFrequencyLoss as TFFL

# Pytorch
import torch

ffl = FFL(loss_weight=1.0, alpha=1.0)  # initialize nn.Module class

fake = torch.randn(4, 3, 64, 64)
real = torch.randn(4, 3, 64, 64)

pt_loss = ffl(fake, real)  # calculate focal frequency loss


# Tensorflow
import tensorflow as tf

tffl = TFFL(loss_weight=1.0, alpha=1.0)  # initialize tf.keras.layers.Layer class

fake = tf.convert_to_tensor(fake.numpy(), tf.float32)
real = tf.convert_to_tensor(real.numpy(), tf.float32)

tf_loss = tffl(fake, real)  # calculate focal frequency loss

# Check
import numpy as np
assert np.isclose(pt_loss.numpy(), tf_loss.numpy()) , "Results don't match"

I have trained a Pix2Pix model using this tensorflow implementation and it does give better results (compared to training without it), but I haven't recreated the official repo results to compare its performance.

@EndlessSora
Copy link
Owner

Thanks a lot for the nice support. I have already mentioned this unofficial TensorFlow implementation in our README.

@ZohebAbai
Copy link
Author

Thanks a lot for the support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants