Row 5568

Row ID: 5568 | Dataset Entry | Axioma AXP Content Repository

Content Data

This page contains data entry 5568 from the Axioma AXP content repository. The structured data below represents the complete record for this entry.

Hi everyone,I'm new to PyTorch and wanted to give a shot to this library for deep learning, I mainly learned deep learning with TensorFlow and Keras (not low api).So I created a script similar to mine to train an architecture, in this case Attention Residual Unet, the two architecture have the same parameter size (\~3M).The goal is to segment endothelial cells on images reshaped to 256x256 (500x500 in original format).Here is the code I use to train the architecture :

import os from PIL import Image import pickle import pandas as pd import numpy as np from glob import glob import torch from torch import nn from torch.nn import functional as F from torch.utils.data import Dataset, DataLoader from torchvision import transforms from sklearn.model_selection import train_test_split from network import * from loss_function import * H = 256 W = 256 BATCH_SIZE = 16 LEARNING_RATE = 1e-4 NUM_EPOCHS = 5 MODEL_PATH = os.path.join("files", "model.keras") CSV_PATH = os.path.join("files", "log.csv") DATASET_PATH = "/mnt/z/hackathon_2/" DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu") class CustomDataset(Dataset): def init(self, X, Y, transform=None): self.X = X self.Y = Y self.transform = transform def len(self): return len(self.X) def getitem(self, idx): x = read_image(self.X[idx]) y = read_mask(self.Y[idx]) if self.transform: x = self.transform(x) y = self.transform(y) return x, y def load_dataset(path, split=0.1): images = sorted(glob(os.path.join(path, "HE/HE_cell", ".png"))) masks = sorted(glob(os.path.join(path, "ERG/ERG_cell", ".png"))) print(f"Found {len(images)} images and {len(masks)} masks") split_size = int(len(images) * split) train_x, valid_x = train_test_split(images, test_size=split_size, random_state=42) train_y, valid_y = train_test_split(masks, test_size=split_size, random_state=42) train_x, test_x = train_test_split(train_x, test_size=split_size, random_state=42) train_y, test_y = train_test_split(train_y, test_size=split_size, random_state=42) return (train_x, train_y), (valid_x, valid_y), (test_x, test_y) def read_image(path): img = Image.open(path).convert('RGB') transform = transforms.Compose([ transforms.Resize((H, W)), transforms.ToTensor(), ]) img = transform(img) return img def read_mask(path): mask = Image.open(path).convert('L') transform = transforms.Compose([ transforms.Resize((H, W)), transforms.ToTensor(), ]) mask = transform(mask) mask = mask.unsqueeze(0) return mask def torch_dataset(X, Y, batch=2): dataset = CustomDataset(X, Y) loader = DataLoader(dataset, batch_size=batch, shuffle=True, num_workers=2, prefetch_factor=10) return loader def train_model(model, criterion, optimizer, train_loader, valid_loader, num_epochs, device): min_val_loss = float("inf") for epoch in range(num_epochs): print(f"Epoch {epoch}/{num_epochs}") model.train() running_loss = 0.0 for inputs, labels in train_loader: inputs = inputs.to(device) labels = labels.to(device) optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() * inputs.size(0) epoch_loss = running_loss / len(train_loader.dataset) print(f"Train Loss: {epoch_loss:.4f}") model.eval() running_val_loss = 0.0 for inputs, labels in valid_loader: inputs = inputs.to(device) labels = labels.to(device) with torch.no_grad(): outputs = model(inputs) loss = criterion(outputs, labels) running_val_loss += loss.item() * inputs.size(0) epoch_val_loss = running_val_loss / len(valid_loader.dataset) print(f"Validation Loss: {epoch_val_loss:.4f}") if epoch_val_loss < min_val_loss: torch.save(model.state_dict(), "best_model.pth") min_val_loss = epoch_val_loss return model def test_model(model, test_loader, device): model.eval() dice_scores = [] f1_scores = [] jaccard_scores = [] with torch.no_grad(): for inputs, labels in test_loader: inputs = inputs.to(device) labels = labels.to(device) outputs = model(inputs) outputs_np = outputs.detach().cpu().numpy() labels_np = labels.cpu().numpy() dice_scores.append(dice_coefficient(labels_np, outputs_np)) f1_scores.append(f1_score(labels_np.flatten(), outputs_np.flatten(), average='binary')) jaccard_scores.append(jaccard_score(labels_np.flatten(), outputs_np.flatten(), average='binary')) print(f"Test Dice Coefficient: {np.mean(dice_scores):.4f}") print(f"Test F1 Score: {np.mean(f1_scores):.4f}") print(f"Test Jaccard Score: {np.mean(jaccard_scores):.4f}") (train_x, train_y), (valid_x, valid_y), (test_x, test_y) = load_dataset(DATASET_PATH) print("Training on : " + str(DEVICE)) print(f"Train: ({len(train_x)},{len(train_y)})") print(f"Valid: ({len(valid_x)},{len(valid_x)})") print(f"Test: ({len(test_x)},{len(test_x)})") train_dataset = torch_dataset(train_x, train_y, batch=BATCH_SIZE, num_workers=6, prefetch_factor=10) valid_dataset = torch_dataset(valid_x, valid_y, batch=BATCH_SIZE, num_workers=6, prefetch_factor=10) model = R2AttU_Net(img_ch=3, output_ch=1) model.to(DEVICE) optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE) criterion = DiceLoss() model = train_model(model, criterion, optimizer, train_dataset, valid_dataset, NUM_EPOCHS, DEVICE) total_params = sum(p.numel() for p in model.parameters()) print(f"Number of parameters: {total_params}")

And here is the code for the network :

[https://github.com/LeeJunHyun/Image\_Segmentation](https://github.com/LeeJunHyun/Image_Segmentation)

My loss function are :

Did I do something wrong ? One Epoch with keras take \~30-40min with the same parameter, both code are running on RTX 3090, in WSL2 environnement.

class DiceLoss(nn.Module): def __init__(self, weight=None, size_average=True): super(DiceLoss, self).__init__() def forward(self,y_true, y_pred, smooth=1e-10, sigmoid=False): if sigmoid: y_pred = F.sigmoid(y_pred) input = y_true.view(-1) target = y_pred.view(-1) intersection = (input * target).sum() return (2. * intersection + smooth) / (input.sum() + target.sum() + smooth) def dice_loss(self,y_true, y_pred): return 1.0 - self.dice_coeff(y_true, y_pred) class DiceBCELoss(nn.Module): def __init__(self, weight=None, size_average=True): super(DiceBCELoss, self).__init__() def forward(self, y_true, y_pred, smooth=1e-10, sigmoid=False): if sigmoid: inputs = F.sigmoid(inputs) inputs = y_true.view(-1) targets = y_pred.view(-1) intersection = (inputs * targets).sum() dice_loss = 1 - (2.*intersection + smooth)/(inputs.sum() + targets.sum() + smooth) bce = F.binary_cross_entropy(inputs, targets, reduction='mean') dice_bce = bce + dice_loss return dice_bce

FieldValue
text Hi everyone,I'm new to PyTorch and wanted to give a shot to this library for deep learning, I mainly learned deep learning with TensorFlow and Keras (not low api).So I created a script similar to mine to train an architecture, in this case Attention Residual Unet, the two architecture have the same parameter size (\~3M).The goal is to segment endothelial cells on images reshaped to 256x256 (500x500 in original format).Here is the code I use to train the architecture : import os from PIL…
label r/deeplearning
dataType post
communityName r/deeplearning
datetime 2024-05-01
username_encoded Z0FBQUFBQm5LakwyMEVxenJuTjdkZnNCWkhLdUVmWU4zVEE4TmVWOE54SjY0dVdSYUNuVmtCcEFLNDdVVC0tNW9HU05fdFFzVWR6dmYyTlVMc1oxdkxoUzFsWEhUWW41WkFSeHdPLXM4dnE4ejJWeU95NjdYWUE9
url_encoded Z0FBQUFBQm5Lak9HNWtPbXVNWEd3SUp5eFBDSkV6X1BYeU1FQ2t1NFN4RTgzT0JKaFZlb3FzM2FTenRmNzE2blIyNTlGRE90SkhBUnQ3bHhTTkJ0ZWV0RHBNd2JVZWVwUm54NXQyUjJLRVUzckxBVnZ2U3Y1c2d0RXR5dS1KM0tKRnkyNFllM0ZpNFdQTndIeGRNV1dORjY5VWgwbmQ0STRGTl9DcVU0Z080ZXRvclRiUVRZNVpHVHRSSXg4TzNpYWpBb1UwYnhEMXVNMnl5a3AtdWZsQWkzWDU3QWN4dHdEUT09

Raw Record

{
  "text": "Hi everyone,I'm new to PyTorch and wanted to give a shot to this library for deep learning, I mainly learned deep learning with TensorFlow and Keras (not low api).So I created a script similar to mine to train an architecture, in this case Attention Residual Unet, the two architecture have the same parameter size (\\~3M).The goal is to segment endothelial cells on images reshaped to 256x256 (500x500 in original format).Here is the code I use to train the architecture :\n\n    import os\n    from PIL import Image\n    import pickle import pandas as pd import numpy as np\n    from glob import glob\n    import torch from torch import nn from torch.nn import functional as F from torch.utils.data import Dataset, DataLoader from torchvision import transforms\n    from sklearn.model_selection import train_test_split\n    from network import * from loss_function import *\n    H = 256 W = 256 BATCH_SIZE = 16 LEARNING_RATE = 1e-4 NUM_EPOCHS = 5\n    MODEL_PATH = os.path.join(\"files\", \"model.keras\")\n    CSV_PATH = os.path.join(\"files\", \"log.csv\")\n    DATASET_PATH = \"/mnt/z/hackathon_2/\"\n    DEVICE = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n    class CustomDataset(Dataset): def init(self, X, Y, transform=None): self.X = X self.Y = Y self.transform = transform\n    def len(self): return len(self.X)\n    def getitem(self, idx): x = read_image(self.X[idx]) y = read_mask(self.Y[idx]) if self.transform: x = self.transform(x) y = self.transform(y) return x, y\n    def load_dataset(path, split=0.1): images = sorted(glob(os.path.join(path, \"HE/HE_cell\", \".png\"))) masks = sorted(glob(os.path.join(path, \"ERG/ERG_cell\", \".png\")))\n    print(f\"Found {len(images)} images and {len(masks)} masks\")\n    split_size = int(len(images) * split)\n    train_x, valid_x = train_test_split(images, test_size=split_size, random_state=42) train_y, valid_y = train_test_split(masks, test_size=split_size, random_state=42)\n    train_x, test_x = train_test_split(train_x, test_size=split_size, random_state=42) train_y, test_y = train_test_split(train_y, test_size=split_size, random_state=42)\n    return (train_x, train_y), (valid_x, valid_y), (test_x, test_y)\n    def read_image(path): img = Image.open(path).convert('RGB') transform = transforms.Compose([ transforms.Resize((H, W)), transforms.ToTensor(), ]) img = transform(img) return img\n    def read_mask(path): mask = Image.open(path).convert('L') transform = transforms.Compose([ transforms.Resize((H, W)), transforms.ToTensor(), ]) mask = transform(mask) mask = mask.unsqueeze(0) return mask\n    def torch_dataset(X, Y, batch=2): dataset = CustomDataset(X, Y) loader = DataLoader(dataset, batch_size=batch, shuffle=True, num_workers=2, prefetch_factor=10) return loader\n    def train_model(model, criterion, optimizer, train_loader, valid_loader, num_epochs, device): min_val_loss = float(\"inf\") for epoch in range(num_epochs): print(f\"Epoch {epoch}/{num_epochs}\") model.train() running_loss = 0.0 for inputs, labels in train_loader: inputs = inputs.to(device) labels = labels.to(device) optimizer.zero_grad() outputs = model(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() * inputs.size(0) epoch_loss = running_loss / len(train_loader.dataset) print(f\"Train Loss: {epoch_loss:.4f}\") model.eval() running_val_loss = 0.0 for inputs, labels in valid_loader: inputs = inputs.to(device) labels = labels.to(device) with torch.no_grad(): outputs = model(inputs) loss = criterion(outputs, labels) running_val_loss += loss.item() * inputs.size(0) epoch_val_loss = running_val_loss / len(valid_loader.dataset) print(f\"Validation Loss: {epoch_val_loss:.4f}\") if epoch_val_loss < min_val_loss: torch.save(model.state_dict(), \"best_model.pth\") min_val_loss = epoch_val_loss return model\n    def test_model(model, test_loader, device): model.eval() dice_scores = [] f1_scores = [] jaccard_scores = [] with torch.no_grad(): for inputs, labels in test_loader: inputs = inputs.to(device) labels = labels.to(device) outputs = model(inputs)\n    outputs_np = outputs.detach().cpu().numpy() labels_np = labels.cpu().numpy() dice_scores.append(dice_coefficient(labels_np, outputs_np)) f1_scores.append(f1_score(labels_np.flatten(), outputs_np.flatten(), average='binary')) jaccard_scores.append(jaccard_score(labels_np.flatten(), outputs_np.flatten(), average='binary'))\n    print(f\"Test Dice Coefficient: {np.mean(dice_scores):.4f}\") print(f\"Test F1 Score: {np.mean(f1_scores):.4f}\") print(f\"Test Jaccard Score: {np.mean(jaccard_scores):.4f}\")\n    (train_x, train_y), (valid_x, valid_y), (test_x, test_y) = load_dataset(DATASET_PATH)\n    print(\"Training on : \" + str(DEVICE))\n    print(f\"Train: ({len(train_x)},{len(train_y)})\") print(f\"Valid: ({len(valid_x)},{len(valid_x)})\") print(f\"Test: ({len(test_x)},{len(test_x)})\")\n    train_dataset = torch_dataset(train_x, train_y, batch=BATCH_SIZE, num_workers=6, prefetch_factor=10) valid_dataset = torch_dataset(valid_x, valid_y, batch=BATCH_SIZE, num_workers=6, prefetch_factor=10)\n    model = R2AttU_Net(img_ch=3, output_ch=1) model.to(DEVICE)\n    optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE) criterion = DiceLoss()\n    model = train_model(model, criterion, optimizer, train_dataset, valid_dataset, NUM_EPOCHS, DEVICE)\n    total_params = sum(p.numel() for p in model.parameters()) print(f\"Number of parameters: {total_params}\")\n\nAnd here is the code for the network :\n\n[https://github.com/LeeJunHyun/Image\\_Segmentation](https://github.com/LeeJunHyun/Image_Segmentation)\n\nMy loss function are :\n\nDid I do something wrong ? One Epoch with keras take \\~30-40min with the same parameter, both code are running on RTX 3090, in WSL2 environnement.\n\n    class DiceLoss(nn.Module):\n    def __init__(self, weight=None, size_average=True):\n    super(DiceLoss, self).__init__()\n    def forward(self,y_true, y_pred, smooth=1e-10, sigmoid=False):\n    if sigmoid:\n    y_pred = F.sigmoid(y_pred)\n    input = y_true.view(-1)\n    target = y_pred.view(-1)\n    intersection = (input * target).sum()\n    return (2. * intersection + smooth) / (input.sum() + target.sum() + smooth)\n    def dice_loss(self,y_true, y_pred):\n    return 1.0 - self.dice_coeff(y_true, y_pred)\n    class DiceBCELoss(nn.Module):\n    def __init__(self, weight=None, size_average=True):\n    super(DiceBCELoss, self).__init__()\n    def forward(self, y_true, y_pred, smooth=1e-10, sigmoid=False):\n    if sigmoid:\n    inputs = F.sigmoid(inputs)\n    inputs = y_true.view(-1)\n    targets = y_pred.view(-1)\n    intersection = (inputs * targets).sum()\n    dice_loss = 1 - (2.*intersection + smooth)/(inputs.sum() + targets.sum() + smooth)\n    bce = F.binary_cross_entropy(inputs, targets, reduction='mean')\n    dice_bce = bce + dice_loss\n    return dice_bce",
  "label": "r/deeplearning",
  "dataType": "post",
  "communityName": "r/deeplearning",
  "datetime": "2024-05-01",
  "username_encoded": "Z0FBQUFBQm5LakwyMEVxenJuTjdkZnNCWkhLdUVmWU4zVEE4TmVWOE54SjY0dVdSYUNuVmtCcEFLNDdVVC0tNW9HU05fdFFzVWR6dmYyTlVMc1oxdkxoUzFsWEhUWW41WkFSeHdPLXM4dnE4ejJWeU95NjdYWUE9",
  "url_encoded": "Z0FBQUFBQm5Lak9HNWtPbXVNWEd3SUp5eFBDSkV6X1BYeU1FQ2t1NFN4RTgzT0JKaFZlb3FzM2FTenRmNzE2blIyNTlGRE90SkhBUnQ3bHhTTkJ0ZWV0RHBNd2JVZWVwUm54NXQyUjJLRVUzckxBVnZ2U3Y1c2d0RXR5dS1KM0tKRnkyNFllM0ZpNFdQTndIeGRNV1dORjY5VWgwbmQ0STRGTl9DcVU0Z080ZXRvclRiUVRZNVpHVHRSSXg4TzNpYWpBb1UwYnhEMXVNMnl5a3AtdWZsQWkzWDU3QWN4dHdEUT09"
}

Entry Information