|
import torch |
|
from PIL import Image |
|
import torchvision.transforms as T |
|
import numpy as np |
|
import os |
|
|
|
from modeling_my_segformer import MySegformerForSemanticSegmentation |
|
from mix_vision_transformer_config import MySegformerConfig |
|
|
|
|
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') |
|
print(f"Using device: {device}") |
|
|
|
|
|
model_name_or_path = "TimM77/SegformerPlusPlus" |
|
print("Starte config_load") |
|
config = MySegformerConfig.from_pretrained(model_name_or_path) |
|
print("Starte Model_load") |
|
model = MySegformerForSemanticSegmentation.from_pretrained(model_name_or_path, config=config) |
|
model.to(device).eval() |
|
|
|
|
|
image_path = "segformer_plusplus/cityscape/berlin_000543_000019_leftImg8bit.png" |
|
image = Image.open(image_path).convert("RGB") |
|
|
|
|
|
transform = T.Compose([ |
|
T.Resize((512, 512)), |
|
T.ToTensor(), |
|
T.Normalize(mean=[0.485, 0.456, 0.406], |
|
std=[0.229, 0.224, 0.225]) |
|
]) |
|
input_tensor = transform(image).unsqueeze(0).to(device) |
|
|
|
print("Modell geladen, Bild geladen, Preprocessing abgeschlossen") |
|
|
|
|
|
with torch.no_grad(): |
|
output = model(input_tensor) |
|
logits = output.logits if hasattr(output, "logits") else output |
|
pred = torch.argmax(logits, dim=1).squeeze(0).cpu().numpy() |
|
|
|
|
|
output_path = os.path.join("segformer_plusplus", "cityscapes_prediction_output_overHF.txt") |
|
np.savetxt(output_path, pred, fmt="%d") |
|
print(f"Prediction saved as {output_path}") |
|
|