Hello, l am currently doing an convoltuinal autoencoder with 2 inputs l am using a MSE loss but my train loss is still 0.00. Many thanks for your support
you will fin my code bellow :
def __init__(self):
super(ConvAutoencoder, self).__init__()
## encoder layers ##
# conv layer (depth from 3 --> 64), 3x3 kernels
self.conv1 = nn.Conv2d(3, 64, 3, padding=1)
# conv layer (depth from 64 --> 16, 3x3 kernels
self.conv2 = nn.Conv2d(64, 3, 3, padding=1)
# pooling layer to reduce x-y dims by two; kernel and stride of 2
self.pool = nn.MaxPool2d(2, 2)
## decoder layers ##
## a kernel of 2 and a stride of 2 will increase the spatial dims by 2
self.t_conv1 = nn.ConvTranspose2d(3, 64, 3, stride=2)
self.t_conv2 = nn.ConvTranspose2d(64, 3, 3, stride=2)
def forward(self, x1,x2):
## encode ##
# add hidden layers with relu activation function
# and maxpooling after
x1 = F.relu(self.conv1(x1))
x1 = self.pool(x1)
# add second hidden layer
x1 = F.relu(self.conv2(x1))
x1 = self.pool(x1) # compressed representation
## decode ##
# add transpose conv layers, with relu activation function
x1 = F.relu(self.t_conv1(x1))
# output layer (with sigmoid for scaling from 0 to 1)
x1 = F.sigmoid(self.t_conv2(x1))
## encode ##
# add hidden layers with relu activation function
# and maxpooling after
x2= F.relu(self.conv1(x2))
x1 = self.pool(x2)
# add second hidden layer
x2 = F.relu(self.conv2(x2))
x2 = self.pool(x2) # compressed representation
## decode ##
# add transpose conv layers, with relu activation function
x2 = F.relu(self.t_conv1(x2))
# output layer (with sigmoid for scaling from 0 to 1)
x2 = F.sigmoid(self.t_conv2(x2))
return x1,x2
class CustomDataset(Dataset):
# 2. Initialize with a targ_dir and transform (optional) parameter
def __init__(self, targ_dir: str,transform=None) -> None:
self.paths_pre = list(pathlib.Path(targ_dir).glob("*/*.png"))
self.paths_post = list(pathlib.Path(targ_dir).glob("*/*.png"))
# . Overwrite the __len__() method (optional but recommended for subclasses of torch.utils.data.Dataset)
def __len__(self) -> int:
"Returns the total number of samples."
return len(self.paths_pre) & len(self.paths_post)
# 4. Make function to load images
def __getitem__(self, index: int,) -> Image.Image:
"Opens an image via a path and returns it."
#image_path1 = self.paths1[index]
#image_path2 = self.paths2[index]
for index in range(len(self.paths_pre)):
image_path_pre = self.paths_pre [index]
for index in range(len(self.paths_post)):
image_path_post = self.paths_post [index]
img_pre = Image.open(image_path_pre).convert('RGB')
transform = transforms.ToTensor()
img_pre = transform(img_pre)
img_post = Image.open(image_path_post).convert('RGB')
transform = transforms.ToTensor()
img_post = transform(img_post)
return img_pre, img_post
Transforms=transforms.Compose(
[
transforms.Resize(224),
transforms.RandomVerticalFlip(),
transforms.RandomHorizontalFlip(),
#image translalte of 10 pixels
transforms.RandomAffine(0, translate=(0, 1)),
transforms.ToTensor()
]
)
Train = CustomDataset(‘/localhome/euda_je/data/test/OmbriaS1/train’,transform = Transforms)
Val = CustomDataset(‘/localhome/euda_je/data/test/OmbriaS1/test’, transform = Transforms)
dataloader_train=DataLoader(Train, batch_size=2, shuffle=True)
dataloader_val = DataLoader(Val, batch_size=2, shuffle=True)
class MSEReconLoss(nn.Module):
def init(self):
super(MSEReconLoss, self).init()
def forward(self, input1, input2):
mse_loss = nn.MSELoss()
output1 = mse_loss(input1, input1)
output2 = mse_loss(input2, input2)
return output1, output2
def training(epochs):
Model.train()
#epoch_loss = 0.0
#running_loss_train=0.0
#for label,data in dataloader_train:
for i, data in tqdm(enumerate(dataloader_train,0)):
epoch_loss = 0.0
running_loss_train=0.0
img_pre, img_post = data
img_pre.to(device=device) # move to device, e.g. GPU
img_post.to(device=device)
#Clear the gradients
optimizer.zero_grad()
recon1, recon2 = Model(img_pre,img_post)
reconstruction1 = criterion(recon1,img_pre)
reconstruction2 = criterion(recon2,img_post)
# Calculate the average of reconstruction1 and reconstruction2 separately.
reconstruction1_avg = sum(reconstruction1) / len(reconstruction1)
reconstruction2_avg = sum(reconstruction2) / len(reconstruction2)
# Calculate the overall average of reconstruction1 and reconstruction2.
loss_reconstruction = (reconstruction1_avg + reconstruction2_avg) / 2
loss_reconstruction.backward()
optimizer.step()
running_loss_train =+loss_reconstruction.item()
epoch_loss += running_loss_train / len(dataloader_train)
train_loss.append(epoch_loss)
#precision_train.append(Precision_train)
#recall_train.append(Recall_train)
#f1_score_train.append(F1_score_train)
#torch.save(Model, 'model_UNet_Assement.pt')
print(
f"Epoch : {epoch+1} - train_loss : {epoch_loss:.4f} \n")
return