0

I have problem with this methode which should return both the training and the validation dataset and examine it to return the index that corresponds to the first occurrence of each class in CIFAR10.

this is code: def get_cifar10_dataset(): """ Should create the cifar 10 network and identify the dataset index of the first time each new class appears

:return: tuple of training and validation dataset as well as label indices
:rtype: (gluon.data.Dataset, 'dict_values' object is not subscriptable, gluon.data.Dataset, 
 dict[int:int])
"""

train_data = None
val_data = None
# YOUR CODE HERE
train_data = datasets.CIFAR10(train=True, root=M5_IMAGES)
val_data = datasets.CIFAR10(train=False, root=M5_IMAGES)
turivishal
  • 34,368
  • 7
  • 36
  • 59
Aymen
  • 1
  • 1

2 Answers2

0

You are asked to return a dictionary with labels and the corresponding indexes. Using the following function can solve your problem.

def get_idx_dict(data):

    lis = []
    idx = []
    indices = {}
    
    for i in range(len(data)):
        if data[i][1] not in lis:
            lis.append(data[i][1])
            idx.append(i)
            
    indices = {lis[i]: idx[i] for i in range(len(lis))}
    return indices

The function returns a dictionary with desired output. Use this function on data from train and validation set.

train_indices = get_idx_dict(train_data)
val_indices = get_idx_dict(val_data)
-1

You can do it this

def get_cifar10_dataset():
    """
    Should create the cifar 10 network and identify the dataset index of the first time each new class appears
    
    :return: tuple of training and validation dataset as well as label indices
    :rtype: (gluon.data.Dataset, dict[int:int], gluon.data.Dataset, dict[int:int])
    """
    train_data = None
    val_data = None
    train_indices = {}
    val_indices = {}
    
    # Use `root=M5_IMAGES` for your dataset
    train_data = gluon.data.vision.datasets.CIFAR10(train=True, root=M5_IMAGES)
    val_data   = gluon.data.vision.datasets.CIFAR10(train=False, root=M5_IMAGES)
    
    #for train
    for i in range(len(train_data)):
        if train_data[i][1] not in train_indices:
            train_indices[train_data[i][1]] = i
    #for valid
    for i in range(len(val_data)):
        if val_data[i][1] not in val_indices:
            val_indices[val_data[i][1]] = i
    
    #raise NotImplementedError()
    
    return train_data, train_indices, val_data, val_indices