Pretrained AlexNet

Daniela: <p>Hello,</p>
<p>I’m new at this of Neural Networks. I want to use a pretrained AlexNet and train it with MNIST dataset, however in all the code examples that I’ve seen for that, they only use one new image each time, and I would like to put the entire dataset, instead of a single image.<br>
That’s my code (not working) at this moment.</p>
<p>import torch, torchvision<br>
from tensorflow import keras<br>
from torch import nn<br>
from torchvision.transforms import ToTensor<br>
from torch.utils.data import DataLoader, Dataset<br>
import torch.nn.functional as F<br>
import matplotlib.pyplot as plt<br>
import numpy as np</p>
<p>!pip install alexnet_pytorch</p>
<p>objects = tf.keras.datasets.mnist<br>
(training_images, training_labels), (test_images, test_labels) = objects.load_data()<br>
print(training_images.shape, test_images.shape)<br>
print(training_labels.shape, test_labels.shape)</p>
<p>num_classes = 10<br>
f, ax = plt.subplots(1, num_classes, figsize=(12,12))<br>
for i in range(0, num_classes):<br>
sample = training_images[training_labels==i][0]<br>
ax[i].imshow(sample, cmap=‘gray’)<br>
ax[i].set_title(‘Label: {}’.format(i), fontsize=16)</p>
<p>training_labels = keras.utils.to_categorical(training_labels, num_classes)<br>
test_labels = keras.utils.to_categorical(test_labels, num_classes)</p>
<p>for i in range(0, 9):<br>
print(training_labels[i])</p>
<p>training_images_normalized = training_images/255.0<br>
test_images_normalized = test_images/255.0</p>
<p>training_images_normalized = training_images_normalized.reshape(training_images_normalized.shape[0], -1)<br>
test_images_normalized = training_images_normalized.reshape(test_images_normalized.shape[0], -1)<br>
print(training_images_normalized.shape)</p>
<h1>
<a name="create-the-model-where-im-not-getting-1" class="anchor" href="#create-the-model-where-im-not-getting-1"></a>Create the model—&gt; where I’m not getting!</h1>
<p>from matplotlib import transforms<br>
from alexnet_pytorch import AlexNet<br>
from torchvision import models<br>
import torchvision.models as models<br>
from torchvision import transforms</p>
<p>alexnet = models.alexnet(pretrained=True, progress=True)<br>
alexnet.eval()</p>
<p>model = AlexNet.from_pretrained(‘alexnet’)<br>
model.eval()<br>
for i in range(len(training_images_normalized)):<br>
img = training_images_normalized[i]<br>
x = np.expand_dims(img, axis=0)<br>
x = preprocess_input(x)<br>
preds = model.predict(x)<br>
# decode the results into a list of tuples (class, description, probability)<br>
# (one such list for each sample in the batch)<br>
print(‘Predicted:’, decode_predictions(preds, top=3)[0])</p>
<p>Thanks in advance!</p>

ptrblck: <p>What kind of error are you seeing?<br>
PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier.</p>

azhanmohammed: <p>Hey Daniela,<br>
From what I can understand, you want to fine-tune/train Alex Net on the MNIST dataset, but the example you have shared performs inference, that is why they take one image at a time. This <a href="https://nextjournal.com/gkoehler/pytorch-mnist" rel="noopener nofollow ugc">simple tutorial</a> can help in understanding how to train a neural network on the MNIST dataset. You can also replace the neural network Net there with Alex Net and train the Alex Net model.</p>