NotImplementedError-Trying to load and train the data

pavs: <p>Hi Guys, I tried to train my custom dataset which generates triangle pieces juxtaposed on a black background but I keep getting a NotImplementedError with my densenet161 model.<br>
Error:<br>
5               board_writer,<br>
6               device,<br>
----&gt; 7               batches_per_epoch=500)</p>
<p>6 frames<br>
 in train_it(no_of_epochs, starting_epoch, model_name, model, loss_criterion, optimizer, batch_size, allTheDataloaders, board_writer, device, batches_per_epoch, is_best, min_validation_loss)<br>
33         no_of_batches_in_this_epoch = 0<br>
34         train_correct_in_this_epoch = 0<br>
—&gt; 35         for train_batch_data, train_batch_labels in allTheDataloaders[“Training”]:<br>
36                 train_batch_data, train_batch_labels = train_batch_data.to(device), train_batch_labels.to(device)<br>
37                 no_of_batches_in_this_epoch+= 1</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in <strong>iter</strong>(self)<br>
353             return self._iterator<br>
354         else:<br>
 → 355             return self._get_iterator()<br>
356<br>
357     <span class="mention">@property</span></p>
<p>/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in _get_iterator(self)<br>
296     def _get_iterator(self) → ‘_BaseDataLoaderIter’:<br>
297         if self.num_workers == 0:<br>
 → 298             return _SingleProcessDataLoaderIter(self)<br>
299         else:<br>
300             self.check_worker_number_rationality()</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in <strong>init</strong>(self, loader)<br>
551<br>
552         self._dataset_fetcher = _DatasetKind.create_fetcher(<br>
 → 553             self._dataset_kind, self._dataset, self._auto_collation, self._collate_fn, self._drop_last)<br>
554<br>
555     def _next_data(self):</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py in create_fetcher(kind, dataset, auto_collation, collate_fn, drop_last)<br>
49             return _utils.fetch._MapDatasetFetcher(dataset, auto_collation, collate_fn, drop_last)<br>
50         else:<br>
—&gt; 51             return _utils.fetch._IterableDatasetFetcher(dataset, auto_collation, collate_fn, drop_last)<br>
52<br>
53</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/fetch.py in <strong>init</strong>(self, dataset, auto_collation, collate_fn, drop_last)<br>
19     def <strong>init</strong>(self, dataset, auto_collation, collate_fn, drop_last):<br>
20         super(_IterableDatasetFetcher, self).<strong>init</strong>(dataset, auto_collation, collate_fn, drop_last)<br>
—&gt; 21         self.dataset_iter = iter(dataset)<br>
22<br>
23     def fetch(self, possibly_batched_index):</p>
<p>/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataset.py in <strong>iter</strong>(self)<br>
145<br>
146     def <strong>iter</strong>(self) → Iterator[T_co]:<br>
 → 147         raise NotImplementedError<br>
148<br>
149     def <strong>add</strong>(self, other: Dataset[T_co]):</p>
<p>NotImplementedError:</p>
<p>The custom dataset class:<br>
def shuffle_buffer_iterator(actual_iterator, size_of_buffer):<br>
shuffle = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<pre><code>while(True):
    size = 0
    while(size &lt; size_of_buffer):
        try:
            shuffle.append(next(actual_iterator))
            size +=1
        except StopIteration:
            shuffle = sample(shuffle, len(shuffle))    
            for s in shuffle:
                yield s
            return
    
    shuffle = sample(shuffle, len(shuffle))    
    for s in shuffle:
        yield s
    shuffle = []
</code></pre>
<p>class triangle_pieces_generator(IterableDataset):<br>
def <strong>init</strong>(self, root_dir, puzzle_piece_dim, size_of_buffer, model_dim):<br>
super(IterableDataset).<strong>init</strong>()<br>
self.root_dir = root_dir<br>
# self.root_dir = root_dir<br>
self.puzzle_piece_dim = puzzle_piece_dim<br>
self.size_of_buffer = size_of_buffer<br>
self.model_dim = model_dim</p>
<pre><code>def visualize_the_triangle_image_sliced(self, image_path):
    image_current = Image.open(image_path).convert("RGBA")
    widthOfImage = image_current.size[0]
    heightOfImage = image_current.size[1]
    newImageLength = (self.puzzle_piece_dim / 2)
    widthOfThePuzzlePiece = newImageLength
    heightOfThePuzzlePiece = newImageLength

    ##Calculates the number of rows and columns that can fit in a puzzle grid.
    rows = round(widthOfImage // widthOfThePuzzlePiece)
    columns = round(heightOfImage // heightOfThePuzzlePiece)

    image_height_new = rows * widthOfThePuzzlePiece
    image_width_new = columns * heightOfThePuzzlePiece

    image_current = image_current.resize((image_width_new, image_height_new))
    square_puzzle_pieces = []
    cropped_pieces = []
    coordinates = []
    square_coordinates = []
    i = 0
    j = 0
    for i in range(rows):
        for j in range(columns):
            square_coordinates.append((i, j))
            # coordinates.append[(i,j)]
            # (x + 1, y), (x + 1, y + 1), (x, y + 1)
            # yield [(i,j),(i + 1, j),(i + 1, j + 1), (i, j + 1)]
            coordinates.extend([(i, j), (i + 1, j), (i + 1, j + 1), (i, j + 1)])

            croppedImage = image_current.crop((j * widthOfThePuzzlePiece, i * heightOfThePuzzlePiece,
                                               (j + 1) * widthOfThePuzzlePiece,
                                               ((i + 1) * heightOfThePuzzlePiece)))  # variable to cut the image
            cropped_pieces.append(croppedImage)
            square_puzzle_pieces.append(cropped_pieces)
        # yield coordinates
        print("square coordinates", square_coordinates)
        print(coordinates)
        # k=k+1
        my_dpi = 300
        fig_cropped = plt.figure(dpi=my_dpi)
        plt.title("Cropped puzzle pieces")
        plt.axis('off')
        for i, croppedPiece in enumerate(cropped_pieces):
            ax = fig_cropped.add_subplot(rows, columns, i + 1)
            ax.imshow(croppedPiece)
            ax.axis('off')
        plt.show()
    return coordinates, cropped_pieces

    piece_coordinates, puzzle_pieces = self.visualize_the_triangle_image_sliced(image_path)

    newImageLength = (self.puzzle_piece_dim / 2)
    widthOfThePuzzlePiece = newImageLength
    heightOfThePuzzlePiece = newImageLength

    create_triangle_image = Image.new('L', size=(widthOfThePuzzlePiece, heightOfThePuzzlePiece), color=(0))
    draw = ImageDraw.Draw(create_triangle_image)
    draw.polygon(((newImageLength + 1, newImageLength + 1), (0, newImageLength), (0, 0)), fill=(255), outline=(0))
    create_triangle_image.show()
    return create_triangle_image


def draw_triangle_part2(self):
    newImageLength_2 = (self.puzzle_piece_dim / 2)
    widthOfThePuzzlePiece = newImageLength_2
    heightOfThePuzzlePiece = newImageLength_2
    create_triangle_image_two = Image.new('L', size=(widthOfThePuzzlePiece, heightOfThePuzzlePiece), color=(0))
    draw_two = ImageDraw.Draw(create_triangle_image_two)
    # draw.polygon(((100, 100), (100, 100), (c, c)), fill=(255, 255, 255), outline=(255, 255, 255))
    draw_two.polygon(((0, 0), (newImageLength_2 + 1, 0), (newImageLength_2 + 1, newImageLength_2 + 1)), fill=(255), outline=(0))
    create_triangle_image_two.show()
    return create_triangle_image_two

def image_alpha(self, image_current):
    newImageLength_alpha = (self.puzzle_piece_dim / 2)
    image_another = image_current.copy()
    image_new = image_another.resize((newImageLength_alpha, newImageLength_alpha))
    image_new.putalpha(self.draw_triangle())
    return image_new

def image_alpha_triangle_2(self, image_current):
    newImageLength_alpha_2 = (self.puzzle_piece_dim / 2)
    image_another_two = image_current.copy()
    image_new_2 = image_another_two.resize((newImageLength_alpha_2, newImageLength_alpha_2))
    image_new_2.putalpha(self.draw_triangle_part2(newImageLength_alpha_2))
    return image_new_2

triangles_one = []
triangles_90 = []

def generate_triangles(self):

    for i in range(0, len(self.puzzle_pieces)):
        newImageLength = self.puzzle_piece_dim / 2
        tri_image = self.puzzle_pieces[i]
        widthOfTriangle = tri_image.size[0]
        heightOfTriangle = tri_image.size[1]
        print(tri_image)

        tri_image_1 = self.image_alpha(tri_image, newImageLength)
       
        self.triangles_one.append(tri_image_1)
    return self.triangles_one

def generate_triangles_two(self):
    for i in range(0, len(self.puzzle_pieces)):
        newImageLength = self.puzzle_piece_dim / 2
   
        tri_image_two = self.puzzle_pieces[i]
        print(tri_image_two)
        tri_image_2 = self.image_alpha_triangle_2(tri_image_two, newImageLength)
         self.triangles_90.append(tri_image_2)
    return self.triangles_90

def get_concat_v(self, image_RGBA_new, imageTwo_RGBA_v):  ##This method is called when i+2 and i
    

    dst_new = Image.new('RGBA', (image_RGBA_new.width,
                                 imageTwo_RGBA_v.height + imageTwo_RGBA_v.height))
    dst_new.paste(image_RGBA_new, (0, 0))
    dst_new.paste(imageTwo_RGBA_v, (0, image_RGBA_new.height)

    return dst_new


def vertical_concat_on_black_image(self, image_RGBA_new, imageTwo_RGBA_v):
    new_v_image = self.get_concat_v(image_RGBA_new, imageTwo_RGBA_v)
    juxtaposed_black_background = Image.new("RGB", (self.puzzle_piece_dim, self.puzzle_piece_dim), (0, 0, 0))

    juxtaposed_black_background_copy_v = juxtaposed_black_background.copy()
    juxtaposed_black_background_copy_v.paste(new_v_image, (30, 1), mask=new_v_image)


    return transforms.ToTensor()(juxtaposed_black_background_copy_v)

def get_concat_h(self, image_RGBA_new, imageTwo_RGBA_new):

    dst_new_another = Image.new('RGBA', (self.imageTwo_RGBA_new.width
                                         + image_RGBA_new.width, self.imageTwo_RGBA_new.height))
    dst_new_another.paste(self.imageTwo_RGBA_new, (0, 0))
    dst_new_another.paste(image_RGBA_new, (self.imageTwo_RGBA_new.width, 0))

    return dst_new_another

def horizonal_concat_on_black_image(self, image_RGBA_new, imageTwo_RGBA_new):
    new_h_image = self.get_concat_h(image_RGBA_new, imageTwo_RGBA_new)
    juxtaposed_black_background = Image.new("RGB", (self.puzzle_piece_dim, self.puzzle_piece_dim), (0, 0, 0))
    juxtaposed_black_background_copy_h = juxtaposed_black_background.copy()
    juxtaposed_black_background_copy_h.paste(new_h_image, (30, 1), mask=new_h_image)

    return transforms.ToTensor()(juxtaposed_black_background_copy_h)


def join_triangles_positive(self, x, y):
    imageLength = self.puzzle_piece_dim / 2
    mask_image = self.draw_triangle(imageLength)
    joined_image = Image.composite(x, y, self.mask_image)
    return joined_image

def paste_image_positive_black(self, x, y):
    mirror = self.join_triangles_positive(x, y)
    juxtaposed_black_background_m = Image.new("RGB", (self.puzzle_piece_dim, self.puzzle_piece_dim * 2), (0, 0, 0))
    juxtaposed_black_background_copy_m = juxtaposed_black_background_m.copy()
    juxtaposed_black_background_copy_m.paste(mirror, (30, 1), mask=mirror)
    return transforms.ToTensor()(juxtaposed_black_background_copy_m)

def slice_and_Create(self):
    for folder in sample(os.listdir(self.root_dir), len(os.listdir(self.root_dir))):
        folder_path = self.root_dir + "/" + folder
        print(folder_path)
        for image in sample(os.listdir(folder_path), len(os.listdir(folder_path))):
            piece_coordinates, puzzle_pieces = self.visualize_the_triangle_image_sliced(folder_path + "/" + image)
            listImages = self.generate_triangles()
            listImagesTwo = self.generate_triangles_two()
            for k in len(listImages):
                for l in len(listImagesTwo):
                    positive_label = 1
                    negative_label = 0
                    if listImages[k] and listImagesTwo[l]:
                        image_RGBA_new = listImages[k]
                        imageTwo_RGBA_v = listImagesTwo[l]
                        positive_past_torch = self.paste_image_positive_black(k, l)
                        vertical_torch_neg = self.vertical_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                        horizontal_torch_neg = self.horizonal_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                        yield (positive_past_torch, positive_label)
                        yield (horizontal_torch_neg, negative_label)
                        yield (vertical_torch_neg, negative_label)
                    if listImages[k] and listImagesTwo[l + 2]:
                        image_RGBA_new = listImages[k]
                        imageTwo_RGBA_v = listImagesTwo[l + 2]
                        vertical_torch = self.vertical_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                        horizontal_torch_neg = self.horizonal_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                        positive_torch_neg = self.paste_image_positive_black(k, l)
                        yield (vertical_torch, positive_label)
                        yield (horizontal_torch_neg, negative_label)
                        yield (positive_torch_neg, negative_label)
                    if listImages[k + 1] and listImagesTwo[l]:
                        image_RGBA_new = listImages[k] + 1
                        imageTwo_RGBA_v = listImagesTwo[l]
                        horizonatl_torch = self.horizonal_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                        vertical_torch_neg = self.vertical_concat_on_black_image(image_RGBA_new, imageTwo_RGBA_v)
                        positive_torch_neg = self.paste_image_positive_black(k, l)
                        yield (vertical_torch_neg, negative_label)
                        yield (positive_torch_neg, negative_label)
                        yield (horizonatl_torch, positive_label)

def _iter_(self):
    iterator = self.slice_and_Create()
    return shuffle_buffer_iterator(iterator, self.size_of_buffer)
</code></pre>
<p>DenseNet Model</p>
<blockquote>
<p>def retireve_the_dataset_input():<br>
puzzle_piece_dim = int(input("Enter puzzle_piece_dim "))<br>
size_of_buffer = int(input("Enter size_of_shuffle_buffer "))<br>
model_dim = int(input("Enter model_dim "))<br>
batch_size = int(input("Enter batch_size "))<br>
return sq_puzzle_piece_dim, size_of_buffer, model_dim, batch_size</p>
</blockquote>
<p>def set_the_dataset_input(default=True):<br>
if default:<br>
puzzle_piece_dim=100<br>
size_of_buffer = 1000<br>
model_dim = 224<br>
batch_size = 20<br>
else:<br>
puzzle_piece_dim, size_of_buffer, model_dim, batch_size = retireve_the_dataset_input()<br>
return puzzle_piece_dim, size_of_buffer, model_dim, batch_size</p>
<p>def initialse_dataloader(root_dir,val_dir,puzzle_piece_dim,size_of_buffer, model_dim,batch_size):</p>
<p>training_dataset=triangles_pieces_dataset.triangle_pieces_generator(root_dir,puzzle_piece_dim,size_of_buffer, model_dim)</p>
<pre><code> train_dataset_dataloader = DataLoader(training_dataset, batch_size)

 validation_dataset=triangles_pieces_dataset.triangle_pieces_generator(val_dir,puzzle_piece_dim,size_of_buffer, model_dim)

 validation_dataset_dataloader = DataLoader(training_dataset, batch_size)

 allTheDataloaders={'Training':train_dataset_dataloader , 'Validation': validation_dataset_dataloader}
 return allTheDataloaders
</code></pre>
<p>def save_the_models_at_the_best_checkpoint(check_point_save,is_best,checkpoint_path,best_model_path):<br>
new_path=checkpoint_path<br>
torch.save(check_point_save,new_path)<br>
if is_best:<br>
torch.save(check_point_save, best_model_path)<br>
else:<br>
torch.save(check_point_save, checkpoint_path)</p>
<p>def load_the_models_at_the_best_checkpoint(new_path, model, optimizer):</p>
<pre><code>checkpoint = torch.load(new_path)
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
valid_loss_min = checkpoint['valid_loss_min']
return model, optimizer, checkpoint['epoch'], valid_loss_min.item()
</code></pre>
<p>def set_parameter_requires_grad(model, feature_extracting):<br>
if feature_extracting:<br>
for param in model.parameters():<br>
param.requires_grad = False</p>
<p>def initialise_the_model(model,numOfClasses,feature_extract,use_pretrained=True):<br>
if model==‘densenet’:<br>
model = models.densenet121(pretrained=use_pretrained)<br>
set_parameter_requires_grad(model_ft, feature_extract)<br>
num_ftrs = model_ft.classifier.in_features<br>
model.classifier = nn.Linear(num_ftrs, numOfClasses)<br>
input_size = 224<br>
return model</p>
<p>def create_optimizer(given_model_parameters, learning_rate, momentum):<br>
optimizer = optim.SGD(given_model_parameters, lr = learning_rate, momentum = momentum)<br>
return optimizer</p>
<p>model_names = [“Densenet”]</p>
<p>def get_model_details():<br>
i = int(input(“Press 0 for Densenet “))<br>
model_name = model_names[i]<br>
if i==1:<br>
j = int(input(“Press 0 for FineTuning and 1 for FeatureExtracting “))<br>
feature_extracting=(j==1)<br>
else:<br>
feature_extracting=False<br>
print(”************”)<br>
print(f"Using {model_name}”)<br>
print(f"feature_extracting : {feature_extracting}”)<br>
return model_name, feature_extracting</p>
<p>def set_parameter_requires_grad(model, feature_extracting):<br>
if feature_extracting:<br>
for param in model.parameters():<br>
param.requires_grad = False</p>
<p>def reshape_denseNet(no_of_classes, feature_extract, use_pretrained=True):<br>
model_dense = None<br>
input_size = 0<br>
model_dense = models.densenet161(pretrained=True)<br>
set_parameter_requires_grad(model_dense, feature_extract)<br>
no_of_features = model_dense.classifier.in_features <span class="hashtag-raw">#CHANGE</span><br>
model_dense.classifier = nn.Linear(no_of_features, no_of_classes)<br>
return model_dense</p>
<p>def parameters_to_update(model_name, model, feature_extract=False):<br>
params = list(model.parameters())<br>
if model_name==“Densenet”:<span class="hashtag-raw">#check</span> with the model name created<br>
if feature_extract:<br>
print(“Feature extracting from Densenet - Expect less number of parameters to learn!”)<br>
params = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for name,param in model.named_parameters():<br>
if param.requires_grad == True:<br>
params.append(param)<br>
print(“\t”,name)<br>
else:<br>
print(“Fine tuning Densenet - Expect more number of parameters to learn!”)<br>
for name,param in model.named_parameters():<br>
if param.requires_grad == True:<br>
print(“\t”,name)<br>
print(f"No_of_parameters to learn : {len(params)}")<br>
return params</p>
<p>def make_loss_criterion(model_name):<br>
loss_criterions = nn.CrossEntropyLoss()<br>
return loss_criterions</p>
<p>def make_model_lc_optimizer(model,learning_rate, momentum,<br>
feature_extract=False,no_of_classes=2):<br>
model =reshape_denseNet(no_of_classes, feature_extract, use_pretrained=True)<br>
params_to_update = parameters_to_update(model, model, feature_extract)<br>
loss_criterion = make_loss_criterion(model)<br>
optimizer = create_optimizer(params_to_update, learning_rate, momentum)<br>
return model, loss_criterion, optimizer</p>
<p>def get_hyperparameters(default=True):<br>
if default:<br>
learning_rate=0.001<br>
momentum = 0.9<br>
else:<br>
learning_rate = float(input("Enter learning rate "))<br>
momentum = float(input("Enter momentum "))<br>
return learning_rate, momentum</p>
<p><span class="hashtag-raw">#Helper</span> function from pytorch</p>
<p>def train_it(no_of_epochs, starting_epoch,<br>
model_name,model,loss_criterion, optimizer,<br>
batch_size, allTheDataloaders,board_writer,device,batches_per_epoch=100,<br>
is_best=False,min_validation_loss=math.inf):</p>
<pre><code>last_checkpoint_path = f"./last_checkpoint_for_{model_name}.pt"
best_model_path=f"./best_model_for_{model_name}.pt"


for epoch in range(starting_epoch,starting_epoch+no_of_epochs):
    print(f"Epoch : {epoch}")
    start_time = time()

    model.train()
    t_labels=[]
    t_predicitions=[]
 
    print("Training")
    train_loss_in_this_epoch = 0
    no_of_batches_in_this_epoch = 0
    train_correct_in_this_epoch = 0
    for train_batch_data, train_batch_labels in allTheDataloaders["Training"]:
            train_batch_data, train_batch_labels = train_batch_data.to(device), train_batch_labels.to(device)
            no_of_batches_in_this_epoch+= 1
            optimizer.zero_grad()
            train_batch_outputs = model(train_batch_data)
            #Compute loss for this batch
            train_batch_loss = loss_criterion(train_batch_outputs, train_batch_labels)
            train_loss_in_this_batch = train_batch_loss.item()
            train_loss_in_this_epoch += train_loss_in_this_batch 
            train_batch_loss.backward()
            optimizer.step()
            with torch.no_grad():
                new_pred=torch.max(train_batch_outputs, axis = 1) 
                train_score, train_predictions = torch.max(train_batch_outputs, axis = 1)   
                train_correct_in_this_batch = torch.sum(train_predictions == train_batch_labels.data).item()
                train_correct_in_this_epoch += train_correct_in_this_batch
                train_batch_labels = train_batch_labels.detach().cpu().numpy()
                train_score = train_score.detach().cpu().numpy()
                #append the values to calculation
                for i in range(batch_size):
                    t_labels.append(train_batch_labels[i])
                    t_predicitions.append(train_score[i])

            if (no_of_batches_in_this_epoch % (batches_per_epoch//10)) == 0:
                print(f"Training #{no_of_batches_in_this_epoch} Batch Acc : {train_correct_in_this_batch}/{batch_size}, Batch Loss: {train_loss_in_this_batch}")
            if no_of_batches_in_this_epoch == batches_per_epoch:
                print(f"Epoch : {epoch}, Training Batch: {no_of_batches_in_this_epoch}")
                break

    board_writer.add_scalar(f'Training/Loss/Average', train_loss_in_this_epoch/no_of_batches_in_this_epoch, epoch)
    board_writer.add_scalar(f'Training/Accuracy/Average', train_correct_in_this_epoch/(no_of_batches_in_this_epoch*batch_size), epoch)
    board_writer.add_scalar(f'Training/TimeTakenInMinutes', (time()-start_time)/60, epoch)
    board_writer.flush()
    print(f"Training average accuracy : {train_correct_in_this_epoch/(no_of_batches_in_this_epoch*batch_size)}")
    print(f"Training average loss : {train_loss_in_this_epoch/no_of_batches_in_this_epoch}")
        
    #create f measure
    model.eval()
    print("Validation")
    val_loss_in_this_epoch = 0
    no_of_batches_in_this_epoch = 0
    val_correct_in_this_epoch = 0
    with torch.no_grad():
        for val_batch_data, val_batch_labels in allTheDataloaders["Validation"]:
            val_batch_data, val_batch_labels = val_batch_data.to(device), val_batch_labels.to(device)
            no_of_batches_in_this_epoch+= 1
            val_batch_outputs = model(val_batch_data)
            #Compute loss for this batch
            val_batch_loss = loss_criterion(val_batch_outputs, val_batch_labels)
            val_loss_in_this_batch = val_batch_loss.item()
            val_loss_in_this_epoch += val_loss_in_this_batch 
            val_score, val_predictions = torch.max(val_batch_outputs, axis = 1)   
            val_correct_in_this_batch = torch.sum(val_predictions == val_batch_labels.data).item()
            val_correct_in_this_epoch += val_correct_in_this_batch
            if (no_of_batches_in_this_epoch % (batches_per_epoch//10)) == 0:
                print(f"Validation #{no_of_batches_in_this_epoch} Batch Acc : {val_correct_in_this_batch}/{batch_size}, Batch Loss: {val_loss_in_this_batch}")
            if no_of_batches_in_this_epoch == batches_per_epoch:
                print(f"Epoch : {epoch}, Validation Batch: {no_of_batches_in_this_epoch}")
                break
        board_writer.add_scalar(f'Validation/Loss/Average', val_loss_in_this_epoch/no_of_batches_in_this_epoch, epoch)
        board_writer.add_scalar(f'Validation/Accuracy/Average', val_correct_in_this_epoch/(no_of_batches_in_this_epoch*batch_size), epoch)
        board_writer.add_scalar(f'Validation/TimeTakenInMinutes', (time()-start_time)/60, epoch)
        board_writer.flush()
        print(f"Validation average accuracy : {val_correct_in_this_epoch/(no_of_batches_in_this_epoch*batch_size)}")
        print(f"Validation average loss : {val_loss_in_this_epoch/no_of_batches_in_this_epoch}")
        if  min_validation_loss &gt;= val_loss_in_this_epoch:
                is_best = True
                min_validation_loss = min(min_validation_loss,val_loss_in_this_epoch)
                checkpoint = {
                    'epoch': epoch + 1,
                    'min_validation_loss': min_validation_loss,
                    'state_dict': model.state_dict(),
                    'optimizer': optimizer.state_dict(),
                }
                save_the_models_at_the_best_checkpoint(checkpoint, is_best, last_checkpoint_path, best_model_path)
                print(f"In epoch number {epoch}, average validation loss decreased to {val_loss_in_this_epoch/no_of_batches_in_this_epoch}")
        load_the_models_at_the_best_checkpoint = {
                'epoch': epoch + 1,
                'min_validation_loss': min_validation_loss,
                'state_dict': model.state_dict(),
                'optimizer': optimizer.state_dict(),
                 }
        save_the_models_at_the_best_checkpoint(load_the_models_at_the_best_checkpoint, False, last_checkpoint_path, best_model_path)
       

board_writer.close()
</code></pre>
<p>#<span class="hashtag-raw">#Variables</span><br>
root_dir = os.getenv(“MY_ROOT_DIR”)<br>
val_dir = os.getenv(“MY_VAL_DIR”)</p>
<p><span class="hashtag-raw">#Change</span> this to False if you want to set the variables instead of using default<br>
default_setting_for_dataset = True</p>
<p>puzzle_piece_dim,size_of_buffer,model_dim,batch_size = set_the_dataset_input(default_setting_for_dataset)</p>
<p>print(f"my_puzzle_piece_dim = {puzzle_piece_dim}“)<br>
print(f"my_size_of_buffer = {size_of_buffer}”)<br>
print(f"my_model_dim = {model_dim}“)<br>
print(f"my_batch_size = {batch_size}”)</p>
<p>my_dataloaders = initialse_dataloader(root_dir,val_dir, puzzle_piece_dim,size_of_buffer, model_dim,batch_size)</p>
<p>model_name, feature_extract = get_model_details()</p>
<p>default_setting_for_hyperparameters = True</p>
<p>learning_rate,momentum = get_hyperparameters(default_setting_for_hyperparameters)</p>
<p><span class="hashtag-raw">#Change</span> the number<br>
epoch=3</p>
<p>model,loss_criterion,optimizer=make_model_lc_optimizer(model_name,learning_rate, momentum,feature_extract)</p>
<p>if torch.cuda.is_available():<br>
device = torch.device(“cuda:0”)<br>
print(“Running on the GPU”)<br>
<span class="hashtag-raw">#putting</span> model on gpu<br>
model.to(device)<br>
else:<br>
device = torch.device(“cpu”)<br>
print(“Running on the CPU”)</p>
<p>tensorboard_dir=f"Training_{model_name}"<br>
board_writer = SummaryWriter(tensorboard_dir)</p>
<p>train_it(epoch, 0,<br>
model_name,model,loss_criterion, optimizer,<br>
batch_size,<br>
my_dataloaders,<br>
board_writer,<br>
device,<br>
batches_per_epoch=500)</p>
<p>%load_ext tensorboard<br>
%tensorboard --logdir=“$tensorboard_dir”</p>
<blockquote></blockquote>
<p>Any Guidance would be appreciated as I am super lost.(Sorry about the formatting of this question)</p>

ptrblck: <p>The issue is most likely caused by a typo in:</p>
<pre><code class="lang-python">def _iter_(self):
</code></pre>
<p>since two underscores are expected:</p>
<pre><code class="lang-python">def __iter__(self):
</code></pre>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>