VGG pretrained does not work

device = torch.device(“cpu”)# if torch.cuda.is_available() else torch.device(“cpu”)
device
epochs=10

train = pd.read_csv(‘train.csv’)
test = pd.read_csv(‘test.csv’)

def ImageData(raw: pd.DataFrame):
y = raw[‘label’].values
y.resize(y.shape[0],1)
x = raw[[i for i in raw.columns if i != ‘label’]].values
x = x.reshape([-1,1, 28, 28])
y = y.astype(int).reshape(-1)
x = x.astype(float)
return x, y

Convert to One Hot Encoding

def one_hot_embedding(labels, num_classes=10):
y = torch.eye(num_classes)
return y[labels]

x_train, y_train = ImageData(train)

Normalization

mean = x_train.mean()
std = x_train.std()
x_train = (x_train-mean)/std
#x_val = (x_val-mean)/std

Numpy to Torch Tensor

x_train = torch.from_numpy(np.float32(x_train)).to(device)
y_train = torch.from_numpy(y_train.astype(np.long)).to(device)
y_train = one_hot_embedding(y_train)
#x_val = torch.from_numpy(np.float32(x_val))
#y_val = torch.from_numpy(y_val.astype(np.long))

Convert into Torch Dataset

train_ds = TensorDataset(x_train, y_train)
#val_ds = TensorDataset(x_val,y_val)
train_dl = DataLoader(train_ds, batch_size=64)

plt.ion() # interactive mode

data_transforms = {
‘train_tensor’: transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
‘val’: transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}

“”"train = pd.read_csv(‘train.csv’)
train_drop_label = train.drop(columns = ‘label’)

creating tensor from targets_df

train_tensor = torch.tensor(train_drop_label.values)

test = pd.read_csv(‘test.csv’)
test_tensor = torch.tensor(test.values)
“”"

device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)

def image_show(n):
number_frame = train.iloc[n, 1:].as_matrix()
number = train.iloc[n,0]
number_frame = number_frame.reshape(28, 28)
plt.imshow(number_frame)
plt.title('Image of number - '+str(number))
plt.axis(‘off’)
plt.show()

x = image_show(2)

Initialize weight with xavier_uniform

def init_weights(m):
if type(m) == nn.Linear:
torch.nn.init.xavier_uniform(m.weight)
m.bias.data.fill_(0.01)

Flatten Later

class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)

Train the network and print accuracy and loss overtime

Train the network and print accuracy and loss overtime

def fit(train_dl, model, loss, optim, epochs=10):
model = model.to(device)
print(‘Epoch\tAccuracy\tLoss’)
accuracy_overtime = []
loss_overtime = []
for epoch in range(epochs):
avg_loss = 0
correct = 0
total=0
for x, y in train_dl: # Iterate over Data Loder

        # Forward pass
        #print(x)
        yhat = model(x) 
        print(yhat)
        l = loss(y, yhat)
        
        #Metrics
        avg_loss+=l.item()
        
        # Backward pass
        optim.zero_grad()
        l.backward()
        optim.step()
        
        # Metrics
        _, original =  torch.max(y, 1)
        _, predicted = torch.max(yhat.data, 1)
        total += y.size(0)
        correct = correct + (original == predicted).sum().item()
        
    accuracy_overtime.append(correct/total)
    loss_overtime.append(avg_loss/len(train_dl))
    print(epoch,accuracy_overtime[-1], loss_overtime[-1], sep='\t')
return accuracy_overtime, loss_overtime

model_ft = models.vgg16(pretrained = True)
n_classes = 10
for params in model_ft.features.parameters():
params.require_grad = False

add your own last layer which has 2 output features

n_inputs = model_ft.classifier[6].in_features
last_layer = nn.Linear(n_inputs, 2)
model_ft.classifier[6] = last_layer

criterion = nn.CrossEntropyLoss()

Observe that all parameters are being optimized

optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)

Decay LR by a factor of 0.1 every 7 epochs

exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)

accuracy, loss = [],[]
plot_accuracy_loss(*fit(train_dl,model_ft, criterion,optimizer_ft,
epochs=20))

error: RuntimeError: Given groups=1, weight of size 64 3 3 3, expected input[64, 1, 28, 28] to have 3 channels, but got 1 channels instead.

Yes I now that we need to change from 3 to 1 but I tried converting rgb to gray it just doesnt help. Pleae help

The issue is in fact the other way around.
You are currently feeding single channel images, while an RGB image is expected, so you might fake the color channels for each grayscale image.
You might use torchvision.transforms.Grayscale(num_output_channels=3) for it.

I’m not sure, which loss function you are using, but if you are using nn.CrossEntropyLoss or nn.NLLLoss, your target should be a LongTensor containing the class indices instead of a one-hot encoded vector.

PS: You can add code snippets by wrapping them in three backticks ``` :wink:

1 Like

Haha sure. That solution worked. Forgot posting it in forum. Sorry for the late reply