Upload app.py with huggingface_hub
Browse files3 unmodified lines4567891011127 unmodified lines2021222324252632 unmodified lines5960616263646566131 unmodified lines1981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782793 unmodified lines28328428528628728828969 unmodified lines3593603613623633643653663673683693703713723733743753763773783793803813823834 unmodified lines3883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744752 unmodified lines4784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535541 unmodified line55655755855956056156218 unmodified lines5815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376382 unmodified lines6416426436446456466472 unmodified lines6506516526536546556566576586596606616626636646654 unmodified lines6706716726736746756766776786796806813 unmodified linesFeatures:- Full resolution processing with seamless tiling- Multi-step inference for maximum quality- TIFF output support- Professional post-processing"""import os7 unmodified linesfrom pathlib import Pathimport tempfileimport timefrom tqdm import tqdmfrom huggingface_hub import hf_hub_download# ============================================================================32 unmodified linesreturn soft_pool2d(x, self.kernel_size, self.stride)# Monkey-patch SoftPool into the expected locationimport sysclass SoftPoolModule:soft_pool2d = staticmethod(soft_pool2d)SoftPool2d = SoftPool2d131 unmodified linesif with_attn:self.attn = SelfAttention(dim_out, norm_groups=norm_groups)def forward(self, x, time_emb, c, t=0, save_flag=False, file_i=0):x = self.res_block(x, time_emb, c)if self.with_attn:x = self.attn(x, t=t, save_flag=save_flag, file_num=file_i)return xclass ResBlock_normal(nn.Module):def __init__(self, dim, dim_out, dropout=0, norm_groups=32):super().__init__()self.block1 = Block(dim, dim_out, groups=norm_groups)self.block2 = Block(dim_out, dim_out, groups=norm_groups, dropout=dropout)self.res_conv = nn.Conv2d(dim, dim_out, 1) if dim != dim_out else nn.Identity()def forward(self, x):h = self.block1(x)h = self.block2(h)return h + self.res_conv(x)class CPEN(nn.Module):def __init__(self, inchannel=1):super(CPEN, self).__init__()self.pool = SoftPool2d(kernel_size=(2,2), stride=(2,2))self.E1 = nn.Sequential(nn.Conv2d(inchannel, 64, kernel_size=3, padding=1), Swish())self.E2 = nn.Sequential(ResBlock_normal(64, 128, dropout=0, norm_groups=16), ResBlock_normal(128, 128, dropout=0, norm_groups=16))self.E3 = nn.Sequential(ResBlock_normal(128, 256, dropout=0, norm_groups=16), ResBlock_normal(256, 256, dropout=0, norm_groups=16))self.E4 = nn.Sequential(ResBlock_normal(256, 512, dropout=0, norm_groups=16), ResBlock_normal(512, 512, dropout=0, norm_groups=16))self.E5 = nn.Sequential(ResBlock_normal(512, 512, dropout=0, norm_groups=16), ResBlock_normal(512, 1024, dropout=0, norm_groups=16))def forward(self, x):x1 = self.E1(x)x2 = self.pool(x1)x2 = self.E2(x2)x3 = self.pool(x2)x3 = self.E3(x3)x4 = self.pool(x3)x4 = self.E4(x4)x5 = self.pool(x4)x5 = self.E5(x5)return x1, x2, x3, x4, x5class UNet(nn.Module):def __init__(self, in_channel=6, out_channel=3, inner_channel=32, norm_groups=32,channel_mults=(1, 2, 4, 8, 8), attn_res=(8), res_blocks=3, dropout=0,with_noise_level_emb=True, image_size=128, condition_ch=3):super().__init__()if with_noise_level_emb:noise_level_channel = inner_channelself.noise_level_mlp = nn.Sequential(PositionalEncoding(inner_channel),nn.Linear(inner_channel, inner_channel * 4),Swish(),nn.Linear(inner_channel * 4, inner_channel))else:noise_level_channel = Noneself.noise_level_mlp = Noneself.res_blocks = res_blocksnum_mults = len(channel_mults)self.num_mults = num_multspre_channel = inner_channelfeat_channels = [pre_channel]now_res = image_sizedowns = [nn.Conv2d(in_channel, inner_channel, kernel_size=3, padding=1)]for ind in range(num_mults):is_last = (ind == num_mults - 1)use_attn = (now_res in attn_res)channel_mult = inner_channel * channel_mults[ind]for _ in range(0, res_blocks):downs.append(ResnetBlocWithAttn(pre_channel, channel_mult, noise_level_emb_dim=noise_level_channel,norm_groups=norm_groups, dropout=dropout, with_attn=use_attn, size=now_res))feat_channels.append(channel_mult)pre_channel = channel_multif not is_last:3 unmodified linesself.downs = nn.ModuleList(downs)self.mid = nn.ModuleList([ResnetBlocWithAttn(pre_channel, pre_channel, noise_level_emb_dim=noise_level_channel,norm_groups=norm_groups, dropout=dropout, with_attn=True, size=now_res),ResnetBlocWithAttn(pre_channel, pre_channel, noise_level_emb_dim=noise_level_channel,norm_groups=norm_groups, dropout=dropout, with_attn=False, size=now_res)69 unmodified lines# ============================================================================# E3Diff High-Resolution Inference# ============================================================================class E3DiffHighRes:def __init__(self, device="cuda"):self.device = torch.device(device if torch.cuda.is_available() else "cpu")self.model = Noneself.image_size = 256def load_model(self, weights_path=None):if weights_path is None:# Download from HuggingFaceweights_path = hf_hub_download(repo_id="Dhenenjay/E3Diff-SAR2Optical",filename="I700000_E719_gen.pth")# Build UNetself.model = UNet(in_channel=3,out_channel=3,norm_groups=16,4 unmodified linesdropout=0,image_size=self.image_size,condition_ch=3).to(self.device)# Load weightsstate_dict = torch.load(weights_path, map_location=self.device, weights_only=False)# Filter only UNet weightsunet_dict = {k.replace('denoise_fn.', ''): v for k, v in state_dict.items()if k.startswith('denoise_fn.')}self.model.load_state_dict(unet_dict, strict=False)self.model.eval()print(f"Model loaded on {self.device}")@torch.no_grad()def translate_tile(self, tile_tensor, num_steps=1):"""Translate a single 256x256 tile."""batch_size = tile_tensor.shape[0]# Initialize noisenoise = torch.randn(batch_size, 3, self.image_size, self.image_size, device=self.device)# DDIM samplingtotal_timesteps = 1000ts = torch.linspace(total_timesteps, 0, num_steps + 1).to(self.device).long()# Create beta schedulebetas = torch.linspace(1e-6, 1e-2, total_timesteps, device=self.device)alphas = 1. - betasalphas_cumprod = torch.cumprod(alphas, dim=0)sqrt_alphas_cumprod_prev = torch.sqrt(torch.cat([torch.ones(1, device=self.device), alphas_cumprod]))x = noisefor i in range(1, num_steps + 1):cur_t = ts[i - 1] - 1prev_t = ts[i] - 1noise_level = sqrt_alphas_cumprod_prev[cur_t].repeat(batch_size, 1)alpha_prod_t = alphas_cumprod[cur_t]alpha_prod_t_prev = alphas_cumprod[prev_t] if prev_t >= 0 else torch.tensor(1.0, device=self.device)beta_prod_t = 1 - alpha_prod_t# Model predictionmodel_input = torch.cat([tile_tensor, x], dim=1)model_output = self.model(model_input, noise_level)# DDIM updatepred_original = (x - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5pred_original = pred_original.clamp(-1, 1)sigma_2 = 0.8 * (1 - alpha_prod_t_prev) / (1 - alpha_prod_t) * (1 - alpha_prod_t / alpha_prod_t_prev)pred_dir = (1 - alpha_prod_t_prev - sigma_2) ** 0.5 * model_outputif i < num_steps:noise = torch.randn_like(x)x = alpha_prod_t_prev ** 0.5 * pred_original + pred_dir + sigma_2 ** 0.5 * noiseelse:x = pred_originalreturn xdef create_blend_weights(self, tile_size, overlap):"""Create smooth blending weights for seamless tiling."""# Linear ramp for overlap regionsramp = np.linspace(0, 1, overlap)# Create 2D weight matrixweight = np.ones((tile_size, tile_size))# Apply ramps to edgesweight[:overlap, :] *= ramp[:, np.newaxis] # Topweight[-overlap:, :] *= ramp[::-1, np.newaxis] # Bottomweight[:, :overlap] *= ramp[np.newaxis, :] # Leftweight[:, -overlap:] *= ramp[np.newaxis, ::-1] # Rightreturn weight[:, :, np.newaxis]def translate_full_resolution(self, image, num_steps=1, overlap=64, progress_callback=None):"""Translate full resolution image using seamless tiling."""# Convert to numpy if PILif isinstance(image, Image.Image):if image.mode != 'RGB':image = image.convert('RGB')2 unmodified linesimg_np = imageh, w = img_np.shape[:2]tile_size = self.image_sizestep = tile_size - overlap# Pad image to ensure full coveragepad_h = (step - (h - overlap) % step) % steppad_w = (step - (w - overlap) % step) % stepimg_padded = np.pad(img_np, ((0, pad_h), (0, pad_w), (0, 0)), mode='reflect')h_pad, w_pad = img_padded.shape[:2]# Output arraysoutput = np.zeros((h_pad, w_pad, 3), dtype=np.float32)weights = np.zeros((h_pad, w_pad, 1), dtype=np.float32)# Blending weightsblend_weight = self.create_blend_weights(tile_size, overlap)# Calculate tile positionsy_positions = list(range(0, h_pad - tile_size + 1, step))x_positions = list(range(0, w_pad - tile_size + 1, step))total_tiles = len(y_positions) * len(x_positions)print(f"Processing {total_tiles} tiles ({len(x_positions)}x{len(y_positions)})...")tile_idx = 0for y in y_positions:for x in x_positions:# Extract tiletile = img_padded[y:y+tile_size, x:x+tile_size]# Convert to tensor [-1, 1]tile_tensor = torch.from_numpy(tile).permute(2, 0, 1).unsqueeze(0)tile_tensor = tile_tensor * 2.0 - 1.0tile_tensor = tile_tensor.to(self.device)# Translateresult_tensor = self.translate_tile(tile_tensor, num_steps)# Convert back to numpy [0, 1]result = result_tensor.squeeze(0).permute(1, 2, 0).cpu().numpy()result = (result + 1.0) / 2.0result = np.clip(result, 0, 1)# Add to output with blendingoutput[y:y+tile_size, x:x+tile_size] += result * blend_weightweights[y:y+tile_size, x:x+tile_size] += blend_weighttile_idx += 1if progress_callback:progress_callback(tile_idx / total_tiles)# Normalize by weightsoutput = output / (weights + 1e-8)# Crop to original sizeoutput = output[:h, :w]return outputdef enhance_output(self, image, contrast=1.1, sharpness=1.15, color=1.1):"""Apply professional post-processing."""if isinstance(image, np.ndarray):image = Image.fromarray((image * 255).astype(np.uint8))# Contrastimage = ImageEnhance.Contrast(image).enhance(contrast)# Sharpnessimage = ImageEnhance.Sharpness(image).enhance(sharpness)# Color saturationimage = ImageEnhance.Color(image).enhance(color)return image1 unmodified line# Gradio Interface# ============================================================================model = Nonedef load_sar_image(filepath):"""Load SAR image from various formats."""18 unmodified linesreturn Image.open(filepath).convert('RGB')def translate_sar(file, num_steps, overlap, enhance):"""Main translation function."""global modelif file is None:return None, None, "Please upload a SAR image"if model is None:print("Loading model...")model = E3DiffHighRes()model.load_model()print("Processing image...")# Handle file upload - get the filepathfilepath = file.name if hasattr(file, 'name') else fileimage = load_sar_image(filepath)w, h = image.sizeprint(f"Input size: {w}x{h}")# Translatestart = time.time()result = model.translate_full_resolution(image,num_steps=num_steps,overlap=overlap,progress_callback=None)elapsed = time.time() - startprint("Post-processing...")# Convert to PILresult_pil = Image.fromarray((result * 255).astype(np.uint8))# Enhance if requestedif enhance:result_pil = model.enhance_output(result_pil)# Save as TIFFtiff_path = tempfile.mktemp(suffix='.tiff')result_pil.save(tiff_path, format='TIFF', compression='lzw')print("Complete!")info = f"Processed in {elapsed:.1f}s | Output: {result_pil.size[0]}x{result_pil.size[1]}"return result_pil, tiff_path, info# Create Gradio interfacewith gr.Blocks(title="E3Diff: SAR-to-Optical Translation") as demo:gr.Markdown("""# 🛰️ E3Diff: High-Resolution SAR-to-Optical Translation2 unmodified lines- Supports full resolution processing with seamless tiling- Multiple quality levels (1-8 inference steps)- Professional post-processing- TIFF output for commercial use""")2 unmodified linesinput_file = gr.File(label="SAR Input (TIFF, PNG, JPG supported)", file_types=[".tif", ".tiff", ".png", ".jpg", ".jpeg"])with gr.Row():num_steps = gr.Slider(1, 8, value=1, step=1, label="Quality Steps (1=fast, 4-8=high quality)")overlap = gr.Slider(16, 128, value=64, step=16, label="Tile Overlap (higher=smoother)")enhance = gr.Checkbox(value=True, label="Apply post-processing enhancement")submit_btn = gr.Button("🚀 Translate to Optical", variant="primary")with gr.Column():output_image = gr.Image(label="Optical Output")output_file = gr.File(label="Download TIFF (full resolution)")info_text = gr.Textbox(label="Processing Info")submit_btn.click(4 unmodified linesgr.Markdown("""---**Tips for best results:**- For aerial/satellite SAR: Use steps=1-2 for speed, steps=4-8 for quality- For noisy SAR: Apply speckle filtering first (Lee or PPB filter)- The model works best with Sentinel-1 style imagery**Citation:** Qin et al., "Efficient End-to-End Diffusion Model for One-step SAR-to-Optical Translation", IEEE GRSL 2024""")3 unmodified lines45678910117 unmodified lines1920212223242532 unmodified lines58596061626364131 unmodified lines1961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602613 unmodified lines26526626726826927027169 unmodified lines3413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754 unmodified lines4804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935942 unmodified lines5975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466471 unmodified line64965065165265365465518 unmodified lines6746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167172 unmodified lines7207217227237247252 unmodified lines7287297307317327337347357367377387397407417427434 unmodified lines7487497507517527537543 unmodified linesFeatures:- Full resolution processing with seamless tiling- Proper diffusion sampling (matching local inference)- TIFF output support"""import os7 unmodified linesfrom pathlib import Pathimport tempfileimport timefrom functools import partialfrom huggingface_hub import hf_hub_download# ============================================================================32 unmodified linesreturn soft_pool2d(x, self.kernel_size, self.stride)# Monkey-patch SoftPoolclass SoftPoolModule:soft_pool2d = staticmethod(soft_pool2d)SoftPool2d = SoftPool2d131 unmodified linesif with_attn:self.attn = SelfAttention(dim_out, norm_groups=norm_groups)def forward(self, x, time_emb, c):x = self.res_block(x, time_emb, c)if self.with_attn:x = self.attn(x, time_emb)return x# CPEN Condition Encoderclass CPEN(nn.Module):def __init__(self, inchannel=3):super(CPEN, self).__init__()from SoftPool import SoftPool2dself.conv1 = nn.Conv2d(inchannel, 64, 3, 1, 1)self.pool1 = SoftPool2d(kernel_size=(2, 2), stride=(2, 2))self.conv2 = nn.Conv2d(64, 128, 3, 1, 1)self.pool2 = SoftPool2d(kernel_size=(2, 2), stride=(2, 2))self.conv3 = nn.Conv2d(128, 256, 3, 1, 1)self.pool3 = SoftPool2d(kernel_size=(2, 2), stride=(2, 2))self.conv4 = nn.Conv2d(256, 512, 3, 1, 1)self.pool4 = SoftPool2d(kernel_size=(2, 2), stride=(2, 2))self.conv5 = nn.Conv2d(512, 1024, 3, 1, 1)def forward(self, x):c1 = self.pool1(F.leaky_relu(self.conv1(x)))c2 = self.pool2(F.leaky_relu(self.conv2(c1)))c3 = self.pool3(F.leaky_relu(self.conv3(c2)))c4 = self.pool4(F.leaky_relu(self.conv4(c3)))c5 = F.leaky_relu(self.conv5(c4))return c1, c2, c3, c4, c5class UNet(nn.Module):def __init__(self, in_channel=6, out_channel=3, inner_channel=32, norm_groups=32,channel_mults=(1, 2, 4, 8, 8), attn_res=(8,), res_blocks=3, dropout=0,with_noise_level_emb=True, image_size=128, condition_ch=3):super().__init__()self.res_blocks = res_blocksnoise_level_channel = inner_channelself.noise_level_mlp = nn.Sequential(PositionalEncoding(inner_channel),nn.Linear(inner_channel, inner_channel * 4),Swish(),nn.Linear(inner_channel * 4, inner_channel)) if with_noise_level_emb else Nonenum_mults = len(channel_mults)pre_channel = inner_channelfeat_channels = [pre_channel]now_res = image_sizedowns = [nn.Conv2d(in_channel, inner_channel, kernel_size=3, padding=1)]for ind in range(num_mults):is_last = (ind == num_mults - 1)use_attn = (now_res in attn_res)channel_mult = inner_channel * channel_mults[ind]for _ in range(0, res_blocks):downs.append(ResnetBlocWithAttn(pre_channel, channel_mult, noise_level_emb_dim=noise_level_channel,norm_groups=norm_groups, dropout=dropout, with_attn=use_attn, size=now_res))feat_channels.append(channel_mult)pre_channel = channel_multif not is_last:3 unmodified linesself.downs = nn.ModuleList(downs)self.mid = nn.ModuleList([ResnetBlocWithAttn(pre_channel, pre_channel, noise_level_emb_dim=noise_level_channel,norm_groups=norm_groups, dropout=dropout, with_attn=True, size=now_res),ResnetBlocWithAttn(pre_channel, pre_channel, noise_level_emb_dim=noise_level_channel,norm_groups=norm_groups, dropout=dropout, with_attn=False, size=now_res)69 unmodified lines# ============================================================================# GaussianDiffusion - Proper DDIM Sampling# ============================================================================def make_beta_schedule(schedule, n_timestep, linear_start=1e-4, linear_end=2e-2):if schedule == 'linear':betas = np.linspace(linear_start, linear_end, n_timestep, dtype=np.float64)else:raise NotImplementedError(schedule)return betasclass GaussianDiffusion(nn.Module):def __init__(self, denoise_fn, image_size, channels=3, schedule_opt=None, opt=None):super().__init__()self.channels = channelsself.image_size = image_sizeself.denoise_fn = denoise_fnself.opt = optself.ddim = schedule_opt.get('ddim', 1) if schedule_opt else 1def set_new_noise_schedule(self, schedule_opt, device, num_train_timesteps=1000):self.ddim = schedule_opt['ddim']self.num_train_timesteps = num_train_timestepsto_torch = partial(torch.tensor, dtype=torch.float32, device=device)betas = make_beta_schedule(schedule=schedule_opt['schedule'],n_timestep=num_train_timesteps,linear_start=schedule_opt['linear_start'],linear_end=schedule_opt['linear_end'])alphas = 1. - betasalphas_cumprod = np.cumprod(alphas, axis=0)self.sqrt_alphas_cumprod_prev = np.sqrt(np.append(1., alphas_cumprod))self.num_timesteps = int(betas.shape[0])self.register_buffer('betas', to_torch(betas))self.register_buffer('alphas_cumprod', to_torch(alphas_cumprod))self.ddim_num_steps = schedule_opt['n_timestep']print(f'DDIM sampling steps: {self.ddim_num_steps}')def ddim_sample(self, condition_x, img_or_shape, device, seed=1):"""DDIM sampling - matches the original E3Diff implementation."""eta = 0.8 # ddim_sampling_eta for linear schedulebatch = img_or_shape[0]total_timesteps = self.num_train_timestepssampling_timesteps = self.ddim_num_stepsts = torch.linspace(total_timesteps, 0, sampling_timesteps + 1).to(device).long()x = torch.randn(img_or_shape, device=device)batch_size = x.shape[0]imgs = [x]img_onestep = [condition_x[:, :self.channels, ...]]for i in range(1, sampling_timesteps + 1):cur_t = ts[i - 1] - 1prev_t = ts[i] - 1noise_level = torch.FloatTensor([self.sqrt_alphas_cumprod_prev[cur_t.item()]]).repeat(batch_size, 1).to(device)alpha_prod_t = self.alphas_cumprod[cur_t]alpha_prod_t_prev = self.alphas_cumprod[prev_t] if prev_t >= 0 else torch.tensor(1.0, device=device)beta_prod_t = 1 - alpha_prod_t# Model predictionmodel_output = self.denoise_fn(torch.cat([condition_x, x], dim=1), noise_level)# Compute sigmasigma_2 = eta * (1 - alpha_prod_t_prev) / (1 - alpha_prod_t) * (1 - alpha_prod_t / alpha_prod_t_prev)noise = torch.randn_like(x)# Predict original samplepred_original_sample = (x - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5pred_original_sample = pred_original_sample.clamp(-1, 1)pred_sample_direction = (1 - alpha_prod_t_prev - sigma_2) ** 0.5 * model_outputx = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction + sigma_2 ** 0.5 * noiseimgs.append(x)img_onestep.append(pred_original_sample)imgs = torch.cat(imgs, dim=0)img_onestep = torch.cat(img_onestep, dim=0)return imgs, img_onestep@torch.no_grad()def super_resolution(self, x_in, continous=False, seed=1, img_s1=None):"""Main inference method."""device = self.betas.devicex = x_inshape = (x.shape[0], self.channels, x.shape[-2], x.shape[-1])self.ddim_num_steps = self.opt['ddim_steps']ret_img, img_onestep = self.ddim_sample(condition_x=x, img_or_shape=shape, device=device, seed=seed)if continous:return ret_img, img_onestepelse:return ret_img[-x_in.shape[0]:], img_onestep# ============================================================================# E3Diff Inference Class# ============================================================================class E3DiffInference:def __init__(self, weights_path=None, device="cuda", num_inference_steps=1):self.device = torch.device(device if torch.cuda.is_available() else "cpu")self.image_size = 256self.num_inference_steps = num_inference_stepsprint(f"[E3Diff] Initializing on device: {self.device}")print(f"[E3Diff] Inference steps: {num_inference_steps}")self.model = self._build_model()self._load_weights(weights_path)self.model.eval()print("[E3Diff] Model ready!")def _build_model(self):unet = UNet(in_channel=3,out_channel=3,norm_groups=16,4 unmodified linesdropout=0,image_size=self.image_size,condition_ch=3)schedule_opt = {'schedule': 'linear','n_timestep': self.num_inference_steps,'linear_start': 1e-6,'linear_end': 1e-2,'ddim': 1,'lq_noiselevel': 0}opt = {'stage': 2,'ddim_steps': self.num_inference_steps,}model = GaussianDiffusion(denoise_fn=unet,image_size=self.image_size,channels=3,schedule_opt=schedule_opt,opt=opt)return model.to(self.device)def _load_weights(self, weights_path):if weights_path is None:weights_path = hf_hub_download(repo_id="Dhenenjay/E3Diff-SAR2Optical",filename="I700000_E719_gen.pth")print(f"[E3Diff] Loading weights from: {weights_path}")state_dict = torch.load(weights_path, map_location=self.device, weights_only=False)self.model.load_state_dict(state_dict, strict=False)print("[E3Diff] Weights loaded!")def preprocess(self, image):if image.mode != 'RGB':image = image.convert('RGB')if image.size != (self.image_size, self.image_size):image = image.resize((self.image_size, self.image_size), Image.LANCZOS)img_np = np.array(image).astype(np.float32) / 255.0img_tensor = torch.from_numpy(img_np).permute(2, 0, 1)img_tensor = img_tensor * 2.0 - 1.0return img_tensor.unsqueeze(0).to(self.device)def postprocess(self, tensor):tensor = tensor.squeeze(0).cpu()tensor = torch.clamp(tensor, -1, 1)tensor = (tensor + 1.0) / 2.0img_np = (tensor.permute(1, 2, 0).numpy() * 255).astype(np.uint8)return Image.fromarray(img_np)@torch.no_grad()def translate(self, sar_image, seed=42):if seed is not None:torch.manual_seed(seed)np.random.seed(seed)sar_tensor = self.preprocess(sar_image)self.model.set_new_noise_schedule({'schedule': 'linear','n_timestep': self.num_inference_steps,'linear_start': 1e-6,'linear_end': 1e-2,'ddim': 1,'lq_noiselevel': 0},self.device,num_train_timesteps=1000)output, _ = self.model.super_resolution(sar_tensor, continous=False, seed=seed, img_s1=sar_tensor)return self.postprocess(output)# ============================================================================# High-Resolution Processor# ============================================================================class HighResProcessor:def __init__(self, device="cuda"):self.device = deviceself.model = Noneself.tile_size = 256def load_model(self, num_steps=1):print("Loading E3Diff model...")self.model = E3DiffInference(device=self.device, num_inference_steps=num_steps)self.num_steps = num_stepsdef create_blend_weights(self, tile_size, overlap):ramp = np.linspace(0, 1, overlap)weight = np.ones((tile_size, tile_size))weight[:overlap, :] *= ramp[:, np.newaxis]weight[-overlap:, :] *= ramp[::-1, np.newaxis]weight[:, :overlap] *= ramp[np.newaxis, :]weight[:, -overlap:] *= ramp[np.newaxis, ::-1]return weight[:, :, np.newaxis]def process(self, image, overlap=64, num_steps=1):if self.model is None or self.num_steps != num_steps:self.load_model(num_steps)if isinstance(image, Image.Image):if image.mode != 'RGB':image = image.convert('RGB')2 unmodified linesimg_np = imageh, w = img_np.shape[:2]tile_size = self.tile_sizestep = tile_size - overlappad_h = (step - (h - overlap) % step) % steppad_w = (step - (w - overlap) % step) % stepimg_padded = np.pad(img_np, ((0, pad_h), (0, pad_w), (0, 0)), mode='reflect')h_pad, w_pad = img_padded.shape[:2]output = np.zeros((h_pad, w_pad, 3), dtype=np.float32)weights = np.zeros((h_pad, w_pad, 1), dtype=np.float32)blend_weight = self.create_blend_weights(tile_size, overlap)y_positions = list(range(0, h_pad - tile_size + 1, step))x_positions = list(range(0, w_pad - tile_size + 1, step))total_tiles = len(y_positions) * len(x_positions)print(f"Processing {total_tiles} tiles at {w}x{h}...")tile_idx = 0for y in y_positions:for x in x_positions:tile = img_padded[y:y+tile_size, x:x+tile_size]tile_pil = Image.fromarray((tile * 255).astype(np.uint8))result_pil = self.model.translate(tile_pil, seed=42)result = np.array(result_pil).astype(np.float32) / 255.0output[y:y+tile_size, x:x+tile_size] += result * blend_weightweights[y:y+tile_size, x:x+tile_size] += blend_weighttile_idx += 1print(f" Tile {tile_idx}/{total_tiles}")output = output / (weights + 1e-8)output = output[:h, :w]return (output * 255).astype(np.uint8)def enhance(self, image, contrast=1.1, sharpness=1.15, color=1.1):if isinstance(image, np.ndarray):image = Image.fromarray(image)image = ImageEnhance.Contrast(image).enhance(contrast)image = ImageEnhance.Sharpness(image).enhance(sharpness)image = ImageEnhance.Color(image).enhance(color)return image1 unmodified line# Gradio Interface# ============================================================================processor = Nonedef load_sar_image(filepath):"""Load SAR image from various formats."""18 unmodified linesreturn Image.open(filepath).convert('RGB')def translate_sar(file, num_steps, overlap, enhance_output):"""Main translation function."""global processorif file is None:return None, None, "Please upload a SAR image"if processor is None:processor = HighResProcessor()print("Processing SAR image...")filepath = file.name if hasattr(file, 'name') else fileimage = load_sar_image(filepath)w, h = image.sizeprint(f"Input size: {w}x{h}")start = time.time()result = processor.process(image, overlap=int(overlap), num_steps=int(num_steps))elapsed = time.time() - startresult_pil = Image.fromarray(result)if enhance_output:result_pil = processor.enhance(result_pil)tiff_path = tempfile.mktemp(suffix='.tiff')result_pil.save(tiff_path, format='TIFF', compression='lzw')print(f"Complete in {elapsed:.1f}s!")info = f"Processed in {elapsed:.1f}s | Output: {result_pil.size[0]}x{result_pil.size[1]}"return result_pil, tiff_path, info# Create interfacewith gr.Blocks(title="E3Diff: SAR-to-Optical Translation") as demo:gr.Markdown("""# 🛰️ E3Diff: High-Resolution SAR-to-Optical Translation2 unmodified lines- Supports full resolution processing with seamless tiling- Multiple quality levels (1-8 inference steps)- TIFF output for commercial use""")2 unmodified linesinput_file = gr.File(label="SAR Input (TIFF, PNG, JPG supported)", file_types=[".tif", ".tiff", ".png", ".jpg", ".jpeg"])with gr.Row():num_steps = gr.Slider(1, 8, value=1, step=1, label="Quality Steps (1=fast, 8=best)")overlap = gr.Slider(16, 128, value=64, step=16, label="Tile Overlap")enhance = gr.Checkbox(value=True, label="Apply enhancement")submit_btn = gr.Button("🚀 Translate to Optical", variant="primary")with gr.Column():output_image = gr.Image(label="Optical Output")output_file = gr.File(label="Download TIFF")info_text = gr.Textbox(label="Processing Info")submit_btn.click(4 unmodified linesgr.Markdown("""---**Tips:** The model works best with Sentinel-1 style SAR imagery. Use steps=1 for speed, steps=4-8 for quality.""")
app.py+284-211