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

Necessary steps to reintegrate BSRGANx2 #40

Closed
wildyaboku opened this issue Sep 9, 2023 · 6 comments
Closed

Necessary steps to reintegrate BSRGANx2 #40

wildyaboku opened this issue Sep 9, 2023 · 6 comments

Comments

@wildyaboku
Copy link

Hi,

I've been investigating the problems you were facing with BSRGANx2 and I believe the flaw is actually with your code, not the model or torchml version. I managed to get it working again with the old BSRGANx2.pth file.

Step 1: Removing upconv2
The BSRGANx2 model does not contain the upconv2 dimension, but your code currently always looks for it. Commenting these lines out while using the BSRGANx2 model allowed it to work properly.

        if sf == 4:
            self.upconv2 = nn.Conv2d(nf, nf, 3, 1, 1, bias=True)
...
        if self.sf == 4:
            fea = self.lrelu(self.upconv2(F.interpolate(fea, scale_factor=2, mode='nearest')))

Solution: Alter these if statements to also check if the model is != BSRGANx2.

Step 2: Reducing scaling multipliers
The latest version hardcodes the multipliers for * 4 for example inside fix_tile_shape and get_final_image_shape. We must instead change this to * 2 when using BSRGANx2.

That's all, it's working fine for me now. Please let me know if you need assistance, and I am very much looking forward to BSRGANx2 being added again! 🎉

@wildyaboku
Copy link
Author

Sorry, I realize now that sf is "scaling factor" and you intended it to be 2 when using the BSRGANx2 model. It should still work the same. I'm curious to hear about the issues you were facing, because I'm successfully upscaling images of any size with the 2x model on the latest version using these changes.

I did encounter problems at one point when I set strict=False on load_state_dict and ran your existing code. The resulting images were wrong and discolored. But by removing the upconv2 code, it works as expected.

@Djdefrag
Copy link
Owner

Djdefrag commented Sep 9, 2023

Hi my friend, i resolved the problem just yeasterday! Thank you :D

It will be reintroduced in next release :)

@wildyaboku
Copy link
Author

That's great news! Thank you so much for your hard work. 😄

If you are open to suggestions for the next version, may I suggest adding a window title, and allowing images to be dragged and dropped into the application again? Otherwise, let me know if you are open for PRs on these after the next release. 😄

Thank you!

@Djdefrag
Copy link
Owner

Djdefrag commented Sep 9, 2023

Thank you my friend ❤️

Unfortunately customtkinter doesn't support any kind of drag&drop, I'm also following a thread on github about it but I don't see any development :(

@wildyaboku
Copy link
Author

I was able to get drag and drop working, although I haven't fully implemented it in a usable state yet. The trick is to use tkinterdnd2 and create a custom Tk class like so:

class Tk(CTk, TkinterDnD.DnDWrapper):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.TkdndVersion = TkinterDnD._require(self)

Then, replace your window line with this:

    window = Tk();

With this alone, the app will continue working as before. However, you can now listen for drag and drop on something like the up_background:

def get_path(event):
    print(event.data)

def place_up_background():
    up_background = CTkLabel(master  = window,
                            text    = "",
                            fg_color = dark_color,
                            font     = bold12,
                            anchor   = "w")

    up_background.place(relx = 0.5,
                        rely = 0.0,
                        relwidth = 1.0,
                        relheight = 1.0,
                        anchor = tk.CENTER)

    up_background.drop_target_register(DND_ALL)
    up_background.dnd_bind("<<Drop>>", get_path)

Now, the drop cursor is displayed, and dropping a file will successfully print the file path.

Does that help? Perhaps this is worth its own issue, if you'd like me to open one for more exploration.

@Djdefrag
Copy link
Owner

Djdefrag commented Sep 9, 2023

Thank you, I will look into it 😄

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