Page MenuHomec4science

Dataloader.py
No OneTemporary

File Metadata

Created
Fri, Mar 21, 10:06

Dataloader.py

# -*- coding: utf-8 -*-
"""
Created on Tue Dec 26 07:14:15 2023
@author: srpv
contact: vigneashwara.solairajapandiyan@empa.ch
contact: vigneashwara.pandiyan@tii.ae
The codes in this following script will be used for the publication of the following work
"Dynamics of in-situ alloying of Ti6Al4V-Fe by means of acoustic emission monitoring
supported by operando synchrotron X-ray diffraction"
@any reuse of this code should be authorized by the first owner, code author
"""
# %%
# Libraries to import
from torch.utils.data import DataLoader, Dataset
import numpy as np
import random
from torchvision import transforms
class Triplet_loader(Dataset):
def __init__(self, df, train=True, transform=None):
self.is_train = train
self.transform = transform
self.to_pil = transforms.ToPILImage()
if self.is_train:
self.images = df.iloc[:, 1:].values.astype(np.uint8)
self.labels = df.iloc[:, 0].values
self.index = df.index.values
else:
self.images = df.iloc[:, 1:].values.astype(np.uint8)
self.labels = df.iloc[:, 0].values
self.index = df.index.values
def __len__(self):
return len(self.images)
def __getitem__(self, item):
anchor_img = self.images[item]
if self.is_train:
anchor_label = self.labels[item]
positive_list = self.index[self.index !=
item][self.labels[self.index != item] == anchor_label]
positive_item = random.choice(positive_list)
positive_img = self.images[positive_item]
negative_list = self.index[self.index !=
item][self.labels[self.index != item] != anchor_label]
negative_item = random.choice(negative_list)
negative_img = self.images[negative_item]
return anchor_img, positive_img, negative_img, anchor_label
else:
label = self.labels[item]
return anchor_img, label

Event Timeline