VGG pretrained does not work

akhilesh_rai: <p>device = torch.device(“cpu”)# if torch.cuda.is_available() else torch.device(“cpu”)<br>
device<br>
epochs=10</p>
<p>train = pd.read_csv(‘train.csv’)<br>
test = pd.read_csv(‘test.csv’)</p>
<p>def ImageData(raw: pd.DataFrame):<br>
y = raw[‘label’].values<br>
y.resize(y.shape[0],1)<br>
x = raw[[i for i in raw.columns if i != ‘label’]].values<br>
x = x.reshape([-1,1, 28, 28])<br>
y = y.astype(int).reshape(-1)<br>
x = x.astype(float)<br>
return x, y</p>
<h2>Convert to One Hot Encoding</h2>
<p>def one_hot_embedding(labels, num_classes=10):<br>
y = torch.eye(num_classes)<br>
return y[labels]</p>
<p>x_train, y_train = ImageData(train)</p>
<h1>Normalization</h1>
<p>mean = x_train.mean()<br>
std = x_train.std()<br>
x_train = (x_train-mean)/std<br>
<span class="hashtag">#x_val</span> = (x_val-mean)/std</p>
<h1>Numpy to Torch Tensor</h1>
<p>x_train = torch.from_numpy(np.float32(x_train)).to(device)<br>
y_train = torch.from_numpy(y_train.astype(np.long)).to(device)<br>
y_train = one_hot_embedding(y_train)<br>
<span class="hashtag">#x_val</span> = torch.from_numpy(np.float32(x_val))<br>
<span class="hashtag">#y_val</span> = torch.from_numpy(y_val.astype(np.long))</p>
<h1>Convert into Torch Dataset</h1>
<p>train_ds = TensorDataset(x_train, y_train)<br>
<span class="hashtag">#val_ds</span> = TensorDataset(x_val,y_val)<br>
train_dl = DataLoader(train_ds, batch_size=64)</p>
<p>plt.ion()   # interactive mode</p>
<p>data_transforms = {<br>
‘train_tensor’: transforms.Compose([<br>
transforms.RandomResizedCrop(224),<br>
transforms.RandomHorizontalFlip(),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
]),<br>
‘val’: transforms.Compose([<br>
transforms.Resize(256),<br>
transforms.CenterCrop(224),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
]),<br>
}</p>
<p>“”"train = pd.read_csv(‘train.csv’)<br>
train_drop_label = train.drop(columns = ‘label’)</p>
<h1>creating tensor from targets_df</h1>
<p>train_tensor = torch.tensor(train_drop_label.values)</p>
<p>test = pd.read_csv(‘test.csv’)<br>
test_tensor = torch.tensor(test.values)<br>
“”"</p>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)</p>
<p>def image_show(n):<br>
number_frame = train.iloc[n, 1:].as_matrix()<br>
number = train.iloc[n,0]<br>
number_frame = number_frame.reshape(28, 28)<br>
plt.imshow(number_frame)<br>
plt.title('Image of number - '+str(number))<br>
plt.axis(‘off’)<br>
plt.show()</p>
<p>x = image_show(2)</p>
<h2>Initialize weight with xavier_uniform</h2>
<p>def init_weights(m):<br>
if type(m) == nn.Linear:<br>
torch.nn.init.xavier_uniform(m.weight)<br>
m.bias.data.fill_(0.01)</p>
<h2>Flatten Later</h2>
<p>class Flatten(nn.Module):<br>
def forward(self, input):<br>
return input.view(input.size(0), -1)</p>
<h1>Train the network and print accuracy and loss overtime</h1>
<h1>Train the network and print accuracy and loss overtime</h1>
<p>def fit(train_dl, model, loss, optim, epochs=10):<br>
model = model.to(device)<br>
print(‘Epoch\tAccuracy\tLoss’)<br>
accuracy_overtime = []<br>
loss_overtime = []<br>
for epoch in range(epochs):<br>
avg_loss = 0<br>
correct = 0<br>
total=0<br>
for x, y in train_dl: # Iterate over Data Loder</p>
<pre><code>        # 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
</code></pre>
<p>model_ft = models.vgg16(pretrained = True)<br>
n_classes = 10<br>
for params in model_ft.features.parameters():<br>
params.require_grad = False</p>
<h1>add your own last layer which has 2 output features</h1>
<p>n_inputs = model_ft.classifier[6].in_features<br>
last_layer = nn.Linear(n_inputs, 2)<br>
model_ft.classifier[6] = last_layer</p>
<p>criterion = nn.CrossEntropyLoss()</p>
<h1>Observe that all parameters are being optimized</h1>
<p>optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)</p>
<h1>Decay LR by a factor of 0.1 every 7 epochs</h1>
<p>exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)</p>
<p>accuracy, loss = [],[]<br>
plot_accuracy_loss(*fit(train_dl,model_ft, criterion,optimizer_ft,<br>
epochs=20))</p>
<p>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.</p>
<p>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</p>

ptrblck: <p>The issue is in fact the other way around.<br>
You are currently feeding single channel images, while an RGB image is expected, so you might fake the color channels for each grayscale image.<br>
You might use <code>torchvision.transforms.Grayscale(num_output_channels=3)</code> for it.</p>
<p>I’m not sure, which loss function you are using, but if you are using <code>nn.CrossEntropyLoss</code> or <code>nn.NLLLoss</code>, your target should be a <code>LongTensor</code> containing the class indices instead of a one-hot encoded vector.</p>
<p>PS: You can add code snippets by wrapping them in three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

akhilesh_rai: <p>Haha sure. That solution worked. Forgot posting it in forum. Sorry for the late reply</p>