Hi Guys
I want to train FCN for semantic segmentation so my training data (CamVid) consists of photos (.png) and semantic labels (.png) which are located in 2 different files (train and train_lables). The format of a training dataset used in this code below is csv which is not my case and I tried to change it in order to load my training data but new pieces of codes did not get matched with the entire code and got errors many times. Could you please help me to find out how I can load my dataset as I have been struggling a lot
def __init__(self, csv_file, phase):
self.phase = phase
self.data = pd.read_csv(csv_file)
if phase == 'train':
self.input_shape = (480, 640)
elif phase == 'valid':
self.input_shape = (704, 960)
def __getitem__(self, index):
image, label = self.data.iloc[index, 0], self.data.iloc[index, 1]
image = scipy.misc.imread("data/"+image, mode='RGB')
label = np.load("data/"+label)
if self.phase == "train":
# RandomCrop
h, w, _ = image.shape
new_h, new_w = self.input_shape
top = random.randint(0, h - new_h)
left = random.randint(0, w - new_w)
image = image[top:top + new_h, left:left + new_w]
label = label[top:top + new_h, left:left + new_w]
# RandomHorizontalFlip
if random.random() < 0.5:
image = np.fliplr(image)
label = np.fliplr(label)
if self.phase == "valid":
# "Resize"
new_h, new_w = self.input_shape
image = image[0:new_h, 0:new_w]
label = label[0:new_h, 0:new_w]
# Normalization
mean=[0.485, 0.456, 0.460]
std =[0.229, 0.224, 0.225]
image = np.transpose(image, (2, 0, 1)) / 255.
image[0] = (image[0] - mean[0]) / std[0]
image[1] = (image[1] - mean[1]) / std[1]
image[2] = (image[2] - mean[2]) / std[2]
# ToTensor
image = torch.from_numpy(image.copy()).float()
label = torch.from_numpy(label.copy()).long()
# One-hot encoding
h, w = label.size()
target = torch.zeros(32, h, w)
for c in range(32):
target[c][label == c] = 1
return {'x': image, 'y':target, 'l':label}
def __len__(self):
return len(self.data)
train_data = CamVid('./data/CamVid/train.csv', phase="train")
valid_data = CamVid('./data/CamVid/valid.csv', phase="valid")
trainloader = torch.utils.data.DataLoader(train_data, batch_size=5, shuffle=True)
validloader = torch.utils.data.DataLoader(valid_data, batch_size=1)```