Image segmentation 25%

Nurkhan_Laiyk: <p>Hello, I try to implement segmentation task, however, I am always getting 25% accuracy result. Here is my code:<br>
LEARNING_RATE = 1e-4<br>
DEVICE = “cuda” if torch.cuda.is_available() else “cpu”<br>
BATCH_SIZE = 16<br>
NUM_EPOCHS = 3<br>
NUM_WORKERS = 2<br>
IMAGE_HEIGHT = 200  # 1280 originally<br>
IMAGE_WIDTH = 280# 1918 originally<br>
PIN_MEMORY = True<br>
LOAD_MODEL = False<br>
TRAIN_IMG_DIR = “/kaggle/input/segmentation-bts/train/imgs”<br>
TRAIN_MASK_DIR = “/kaggle/input/segmentation-bts/train/masks”<br>
VAL_IMG_DIR = “/kaggle/input/segmentation-bts/val/imgs”<br>
VAL_MASK_DIR = “/kaggle/input/segmentation-bts/val/masks”</p>
<p>def train_fn(loader, model, optimizer, loss_fn, scaler):<br>
loop = tqdm(loader)</p>
<pre><code>for batch_idx, (data, targets) in enumerate(loop):
    data = data.to(device=DEVICE)
    targets = targets.float().unsqueeze(1).to(device=DEVICE)

    # forward
    with torch.cuda.amp.autocast():
        predictions = model(data)
        loss = loss_fn(predictions, targets)

    # backward
    optimizer.zero_grad()
    scaler.scale(loss).backward()
    scaler.step(optimizer)
    scaler.update()

    # update tqdm loop
    loop.set_postfix(loss=loss.item())
</code></pre>
<p>def main():<br>
train_transform = A.Compose(<br>
[<br>
A.Resize(height=IMAGE_HEIGHT, width=IMAGE_WIDTH),<br>
A.Rotate(limit=35, p=1.0),<br>
A.HorizontalFlip(p=0.5),<br>
A.VerticalFlip(p=0.1),<br>
A.Normalize(<br>
mean=[0.0, 0.0, 0.0],<br>
std=[1.0, 1.0, 1.0],<br>
max_pixel_value=255.0,<br>
),<br>
ToTensorV2(),<br>
],<br>
)</p>
<pre><code>val_transforms = A.Compose(
    [
        A.Resize(height=IMAGE_HEIGHT, width=IMAGE_WIDTH),
        A.Normalize(
            mean=[0.0, 0.0, 0.0],
            std=[1.0, 1.0, 1.0],
            max_pixel_value=255.0,
        ),
        ToTensorV2(),
    ],
)

model = UNET(in_channels=3, out_channels=1).to(DEVICE)
loss_fn = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)

train_loader, val_loader = get_loaders(
    TRAIN_IMG_DIR,
    TRAIN_MASK_DIR,
    VAL_IMG_DIR,
    VAL_MASK_DIR,
    BATCH_SIZE,
    train_transform,
    val_transforms,
    NUM_WORKERS,
    PIN_MEMORY,
)

if LOAD_MODEL:
    load_checkpoint(torch.load("my_checkpoint.pth.tar"), model)


check_accuracy(val_loader, model, device=DEVICE)
scaler = torch.cuda.amp.GradScaler()

for epoch in range(NUM_EPOCHS):
    train_fn(train_loader, model, optimizer, loss_fn, scaler)

    # save model
    checkpoint = {
        "state_dict": model.state_dict(),
        "optimizer":optimizer.state_dict(),
    }
    save_checkpoint(checkpoint)

    # check accuracy
    check_accuracy(val_loader, model, device=DEVICE)

    # print some examples to a folder
save_predictions_as_imgs(
        val_loader, model, folder="saved_images/", device=DEVICE
    )
</code></pre>
<p>if <strong>name</strong> == “<strong>main</strong>”:<br>
main()</p>
<p>LEARNING_RATE = 1e-4<br>
DEVICE = “cuda” if torch.cuda.is_available() else “cpu”<br>
BATCH_SIZE = 16<br>
NUM_EPOCHS = 3<br>
NUM_WORKERS = 2<br>
IMAGE_HEIGHT = 200  # 1280 originally<br>
IMAGE_WIDTH = 280# 1918 originally<br>
PIN_MEMORY = True<br>
LOAD_MODEL = False<br>
TRAIN_IMG_DIR = “/kaggle/input/segmentation-bts/train/imgs”<br>
TRAIN_MASK_DIR = “/kaggle/input/segmentation-bts/train/masks”<br>
VAL_IMG_DIR = “/kaggle/input/segmentation-bts/val/imgs”<br>
VAL_MASK_DIR = “/kaggle/input/segmentation-bts/val/masks”</p>
<p>def train_fn(loader, model, optimizer, loss_fn, scaler):<br>
loop = tqdm(loader)</p>
<pre><code>for batch_idx, (data, targets) in enumerate(loop):
    data = data.to(device=DEVICE)
    targets = targets.float().unsqueeze(1).to(device=DEVICE)

    # forward
    with torch.cuda.amp.autocast():
        predictions = model(data)
        loss = loss_fn(predictions, targets)

    # backward
    optimizer.zero_grad()
    scaler.scale(loss).backward()
    scaler.step(optimizer)
    scaler.update()

    # update tqdm loop
    loop.set_postfix(loss=loss.item())
</code></pre>
<p>def main():<br>
train_transform = A.Compose(<br>
[<br>
A.Resize(height=IMAGE_HEIGHT, width=IMAGE_WIDTH),<br>
A.Rotate(limit=35, p=1.0),<br>
A.HorizontalFlip(p=0.5),<br>
A.VerticalFlip(p=0.1),<br>
A.Normalize(<br>
mean=[0.0, 0.0, 0.0],<br>
std=[1.0, 1.0, 1.0],<br>
max_pixel_value=255.0,<br>
),<br>
ToTensorV2(),<br>
],<br>
)</p>
<pre><code>val_transforms = A.Compose(
    [
        A.Resize(height=IMAGE_HEIGHT, width=IMAGE_WIDTH),
        A.Normalize(
            mean=[0.0, 0.0, 0.0],
            std=[1.0, 1.0, 1.0],
            max_pixel_value=255.0,
        ),
        ToTensorV2(),
    ],
)

model = UNET(in_channels=3, out_channels=1).to(DEVICE)
loss_fn = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)

train_loader, val_loader = get_loaders(
    TRAIN_IMG_DIR,
    TRAIN_MASK_DIR,
    VAL_IMG_DIR,
    VAL_MASK_DIR,
    BATCH_SIZE,
    train_transform,
    val_transforms,
    NUM_WORKERS,
    PIN_MEMORY,
)

if LOAD_MODEL:
    load_checkpoint(torch.load("my_checkpoint.pth.tar"), model)


check_accuracy(val_loader, model, device=DEVICE)
scaler = torch.cuda.amp.GradScaler()

for epoch in range(NUM_EPOCHS):
    train_fn(train_loader, model, optimizer, loss_fn, scaler)

    # save model
    checkpoint = {
        "state_dict": model.state_dict(),
        "optimizer":optimizer.state_dict(),
    }
    save_checkpoint(checkpoint)

    # check accuracy
    check_accuracy(val_loader, model, device=DEVICE)

    # print some examples to a folder
save_predictions_as_imgs(
        val_loader, model, folder="saved_images/", device=DEVICE
    )
</code></pre>
<p>if <strong>name</strong> == “<strong>main</strong>”:<br>
main()</p>
<p>LEARNING_RATE = 1e-4<br>
DEVICE = “cuda” if torch.cuda.is_available() else “cpu”<br>
BATCH_SIZE = 16<br>
NUM_EPOCHS = 3<br>
NUM_WORKERS = 2<br>
IMAGE_HEIGHT = 200  # 1280 originally<br>
IMAGE_WIDTH = 280# 1918 originally<br>
PIN_MEMORY = True<br>
LOAD_MODEL = False<br>
TRAIN_IMG_DIR = “/kaggle/input/segmentation-bts/train/imgs”<br>
TRAIN_MASK_DIR = “/kaggle/input/segmentation-bts/train/masks”<br>
VAL_IMG_DIR = “/kaggle/input/segmentation-bts/val/imgs”<br>
VAL_MASK_DIR = “/kaggle/input/segmentation-bts/val/masks”</p>
<p>def train_fn(loader, model, optimizer, loss_fn, scaler):<br>
loop = tqdm(loader)</p>
<pre><code>for batch_idx, (data, targets) in enumerate(loop):
    data = data.to(device=DEVICE)
    targets = targets.float().unsqueeze(1).to(device=DEVICE)

    # forward
    with torch.cuda.amp.autocast():
        predictions = model(data)
        loss = loss_fn(predictions, targets)

    # backward
    optimizer.zero_grad()
    scaler.scale(loss).backward()
    scaler.step(optimizer)
    scaler.update()

    # update tqdm loop
    loop.set_postfix(loss=loss.item())
</code></pre>
<p>def main():<br>
train_transform = A.Compose(<br>
[<br>
A.Resize(height=IMAGE_HEIGHT, width=IMAGE_WIDTH),<br>
A.Rotate(limit=35, p=1.0),<br>
A.HorizontalFlip(p=0.5),<br>
A.VerticalFlip(p=0.1),<br>
A.Normalize(<br>
mean=[0.0, 0.0, 0.0],<br>
std=[1.0, 1.0, 1.0],<br>
max_pixel_value=255.0,<br>
),<br>
ToTensorV2(),<br>
],<br>
)</p>
<pre><code>val_transforms = A.Compose(
    [
        A.Resize(height=IMAGE_HEIGHT, width=IMAGE_WIDTH),
        A.Normalize(
            mean=[0.0, 0.0, 0.0],
            std=[1.0, 1.0, 1.0],
            max_pixel_value=255.0,
        ),
        ToTensorV2(),
    ],
)

model = UNET(in_channels=3, out_channels=1).to(DEVICE)
loss_fn = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)

train_loader, val_loader = get_loaders(
    TRAIN_IMG_DIR,
    TRAIN_MASK_DIR,
    VAL_IMG_DIR,
    VAL_MASK_DIR,
    BATCH_SIZE,
    train_transform,
    val_transforms,
    NUM_WORKERS,
    PIN_MEMORY,
)

if LOAD_MODEL:
    load_checkpoint(torch.load("my_checkpoint.pth.tar"), model)


check_accuracy(val_loader, model, device=DEVICE)
scaler = torch.cuda.amp.GradScaler()

for epoch in range(NUM_EPOCHS):
    train_fn(train_loader, model, optimizer, loss_fn, scaler)

    # save model
    checkpoint = {
        "state_dict": model.state_dict(),
        "optimizer":optimizer.state_dict(),
    }
    save_checkpoint(checkpoint)

    # check accuracy
    check_accuracy(val_loader, model, device=DEVICE)

    # print some examples to a folder
save_predictions_as_imgs(
        val_loader, model, folder="saved_images/", device=DEVICE
    )
</code></pre>
<p>if <strong>name</strong> == “<strong>main</strong>”:<br>
main()</p>
<p>Does anybody know what might be the problem?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/c/1c0e6f7bf3656cdbf6a960a866b3ae52c54541dc.png" data-download-href="https://discuss.pytorch.org/uploads/default/1c0e6f7bf3656cdbf6a960a866b3ae52c54541dc" title="Снимок экрана (1358)"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/c/1c0e6f7bf3656cdbf6a960a866b3ae52c54541dc_2_689x216.png" alt="Снимок экрана (1358)" data-base62-sha1="40cipSWaAhQ5XASi9SS1QAB8lXK" width="689" height="216" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/c/1c0e6f7bf3656cdbf6a960a866b3ae52c54541dc_2_689x216.png, https://discuss.pytorch.org/uploads/default/optimized/3X/1/c/1c0e6f7bf3656cdbf6a960a866b3ae52c54541dc_2_1033x324.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/1/c/1c0e6f7bf3656cdbf6a960a866b3ae52c54541dc.png 2x" data-dominant-color="F4ECED"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Снимок экрана (1358)</span><span class="informations">1231×386 48.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>