torch.manual_seed(101)
num_epochs=30
for epoch in tqdm(range(num_epochs)):
for step,i in enumerate(train_loader):
img,label_one_hot,label = i
img = Variable(img).cuda()
label_one_hot= Variable(label_one_hot.float()).cuda()
pred = cnn_model(img)
loss_value = loss_fn(pred,label_one_hot)
optimizer.zero_grad()
loss_value.backward()
optimizer.step()
print('epoch:', epoch+1, 'step:', step+1, 'loss:', loss_value.item())
This is my training loop
class Mydataset(Dataset):
def __init__(self,path,is_train=True,transform=transform):
self.path = path
if is_train: self.img = os.listdir(self.path)
self.transform = transform
def __getitem__(self,idx):
img_path = self.img[idx]
files = os.listdir(self.path)
for file in files:
if (file.endswith(".jpg") and file[:-4] == length_of_captcha):
img = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,12))
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, kernel)
label = Path(self.path/img_path).name[:-4]
label_ = []
else:
continue
for i in label:
label_ += encode(i)
if self.transform is not None:
img = self.transform(img)
return img,np.array(label_),label
def __len__(self):
return (len(self.img))
Here I am trying for character recognition for 6 digit captcha image after some preprocessing. I am getting the error message in the title
This is my dataset class and here I have passed successfully creating train_loader. But I am still encountering this and I don’t know what is going wrong. Can anyone help me?