| 
									
										
										
										
											2022-09-21 08:06:37 -05:00
										 |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import traceback | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-29 17:46:23 -05:00
										 |  |  | from basicsr.utils.download_util import load_file_from_url | 
					
						
							| 
									
										
										
										
											2022-09-21 08:06:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-29 17:46:23 -05:00
										 |  |  | from modules.upscaler import Upscaler, UpscalerData | 
					
						
							|  |  |  | from modules.ldsr_model_arch import LDSR | 
					
						
							|  |  |  | from modules import shared | 
					
						
							| 
									
										
										
										
											2022-09-21 08:06:37 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-29 17:46:23 -05:00
										 |  |  | class UpscalerLDSR(Upscaler): | 
					
						
							|  |  |  |     def __init__(self, user_path): | 
					
						
							| 
									
										
										
										
											2022-09-21 08:06:37 -05:00
										 |  |  |         self.name = "LDSR" | 
					
						
							| 
									
										
										
										
											2022-09-29 17:46:23 -05:00
										 |  |  |         self.user_path = user_path | 
					
						
							|  |  |  |         self.model_url = "https://heibox.uni-heidelberg.de/f/578df07c8fc04ffbadf3/?dl=1" | 
					
						
							|  |  |  |         self.yaml_url = "https://heibox.uni-heidelberg.de/f/31a76b13ea27482981b4/?dl=1" | 
					
						
							|  |  |  |         super().__init__() | 
					
						
							|  |  |  |         scaler_data = UpscalerData("LDSR", None, self) | 
					
						
							|  |  |  |         self.scalers = [scaler_data] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def load_model(self, path: str): | 
					
						
							| 
									
										
										
										
											2022-09-30 08:41:25 -05:00
										 |  |  |         # Remove incorrect project.yaml file if too big | 
					
						
							|  |  |  |         yaml_path = os.path.join(self.model_path, "project.yaml") | 
					
						
							| 
									
										
										
										
											2022-09-30 08:55:04 -05:00
										 |  |  |         old_model_path = os.path.join(self.model_path, "model.pth") | 
					
						
							|  |  |  |         new_model_path = os.path.join(self.model_path, "model.ckpt") | 
					
						
							| 
									
										
										
										
											2022-09-30 08:41:25 -05:00
										 |  |  |         if os.path.exists(yaml_path): | 
					
						
							|  |  |  |             statinfo = os.stat(yaml_path) | 
					
						
							| 
									
										
										
										
											2022-09-30 08:55:04 -05:00
										 |  |  |             if statinfo.st_size >= 10485760: | 
					
						
							| 
									
										
										
										
											2022-09-30 08:41:25 -05:00
										 |  |  |                 print("Removing invalid LDSR YAML file.") | 
					
						
							|  |  |  |                 os.remove(yaml_path) | 
					
						
							| 
									
										
										
										
											2022-09-30 08:55:04 -05:00
										 |  |  |         if os.path.exists(old_model_path): | 
					
						
							|  |  |  |             print("Renaming model from model.pth to model.ckpt") | 
					
						
							|  |  |  |             os.rename(old_model_path, new_model_path) | 
					
						
							| 
									
										
										
										
											2022-09-29 17:46:23 -05:00
										 |  |  |         model = load_file_from_url(url=self.model_url, model_dir=self.model_path, | 
					
						
							| 
									
										
										
										
											2022-09-30 08:55:04 -05:00
										 |  |  |                                    file_name="model.ckpt", progress=True) | 
					
						
							| 
									
										
										
										
											2022-09-30 08:33:06 -05:00
										 |  |  |         yaml = load_file_from_url(url=self.yaml_url, model_dir=self.model_path, | 
					
						
							| 
									
										
										
										
											2022-09-29 17:46:23 -05:00
										 |  |  |                                   file_name="project.yaml", progress=True) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             return LDSR(model, yaml) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         except Exception: | 
					
						
							|  |  |  |             print("Error importing LDSR:", file=sys.stderr) | 
					
						
							|  |  |  |             print(traceback.format_exc(), file=sys.stderr) | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def do_upscale(self, img, path): | 
					
						
							|  |  |  |         ldsr = self.load_model(path) | 
					
						
							|  |  |  |         if ldsr is None: | 
					
						
							|  |  |  |             print("NO LDSR!") | 
					
						
							|  |  |  |             return img | 
					
						
							|  |  |  |         ddim_steps = shared.opts.ldsr_steps | 
					
						
							|  |  |  |         return ldsr.super_resolution(img, ddim_steps, self.scale) |