Day 4: Transfer learning 2 - Running and exploring the code

Today, I am running the sample solution on the Lesson 2 Part 8 Transfer learning. I exercised different pre trained network such as resnet101 and densenet121. I came up with this problems,


data_dir = 'Cat_Dog_data'

# TODO: Define transforms for the training data and testing data
train_transforms = transforms.Compose([transforms.RandomRotation(30),
                                      transforms.RandomResizedCrop(224),
                                      transforms.RandomHorizontalFlip(),
                                      transforms.ToTensor(),
                                      transforms.Normalize([0.485, 0.456, 0.406],
                                                           [0.229, 0.224, 0.225])])

test_transforms = transforms.Compose([transforms.Resize(255),
                                     transforms.CenterCrop(224),
                                     transforms.ToTensor(),
                                     transforms.Normalize([0.485, 0.456, 0.406],
                                                          [0.229, 0.224, 0.225])])

# Pass transforms in here, then run the next cell to see how the transforms look
train_data = datasets.ImageFolder(data_dir + '/train', transform=train_transforms)
test_data = datasets.ImageFolder(data_dir + '/test', transform=test_transforms)

trainloader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)
testloader = torch.utils.data.DataLoader(test_data, batch_size=64)

.........

epochs = 1
steps = 0
running_loss = 0
print_every = 5
for epoch in range(epochs):
    for inputs, labels in trainloader:
      ....


1. epochs? what is the significance of epoch in here?
2. the len(train_data) returns 22500, while len(trainloader) returns 352 (22500 / 64 = 351.xx) Does it mean the data is only 352 while the inputs needed is 1024 if I used densenet121, or 2048 if I used resnet101? Is that okay or Do I misunderstood somewhere?


Next thing, I want to explore is by adding hidden layers. is there any differences if I define like this,
model = models.resnet101(pretrained=True)

# Freeze parameters so we don't backprop through them
for param in model.parameters():
    param.requires_grad = False
   
model.fc = nn.Sequential(nn.Linear(2048, 1024),
                                 nn.ReLU(),
                                 nn.Dropout(0.1),
                         nn.Linear(1024, 512),
                                 nn.ReLU(),
                                 nn.Dropout(0.1),
                         nn.Linear(512, 256),
                                 nn.ReLU(),
                                 nn.Dropout(0.1),
                         nn.Linear(256, 128),
                                 nn.ReLU(),
                                 nn.Dropout(0.1),
                                 nn.Linear(128, 2),
                                 nn.LogSoftmax(dim=1))

compare to original solution on the course?

Comments

Popular posts from this blog