<jupyter_start><jupyter_text>1. Loading data The output of torchvision datasets are PILImage images of range [0, 1].We transform them to Tensors of normalized range [-1, 1].<jupyter_code>class CIFAR100Train():
    def __init__(self, transform=None):
        self.data = unpickle('./data/cifar_100_train')
        self.transform = transform
        
    def __len__(self):
        return len(self.data[b'fine_labels'])

    def __getitem__(self, index):
        label = self.data[b'fine_labels'][index]
        r = self.data[b'data'][index, :1024].reshape(32, 32)
        g = self.data[b'data'][index, 1024:2048].reshape(32, 32)
        b = self.data[b'data'][index, 2048:].reshape(32, 32)
        image = np.dstack((r, g, b))

        if self.transform:
            image = self.transform(image)
        return label, image

class CIFAR100Test():
    def __init__(self, transform=None):
        self.data = unpickle('./data/cifar_100_test')
        self.transform = transform 

    def __len__(self):
        return len(self.data[b'data'])
    
    def __getitem__(self, index):
        label = self.data[b'fine_labels'][index]
        r = self.data[b'data'][index, :1024].reshape(32, 32)
        g = self.data[b'data'][index, 1024:2048].reshape(32, 32)
        b = self.data[b'data'][index, 2048:].reshape(32, 32)
        image = np.dstack((r, g, b))

        if self.transform:
            image = self.transform(image)
        return label, image
def unpickle(file):
    import pickle
    with open(file, 'rb') as fo:
        dict = pickle.load(fo, encoding='bytes')
    return dict
transform_train = transforms.Compose(
    [transforms.ToPILImage(),
     transforms.RandomCrop(32, padding=4),
     transforms.RandomHorizontalFlip(),
     transforms.RandomRotation(15),
     transforms.ToTensor(),
     transforms.Normalize((0.507, 0.486, 0.441), (0.267, 0.256, 0.276))])

transform_test = transforms.Compose(
     [transforms.ToTensor(),
     transforms.Normalize((0.507, 0.486, 0.441), (0.267, 0.256, 0.276))])

train = CIFAR100Train(transform=transform_train)
train_size = int(0.7*len(train))
valid_size = len(train) - train_size
trainset, validset = torch.utils.data.random_split(train, [train_size, valid_size]) # split to train and validation set
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128,shuffle=True, num_workers=2)
validloader = torch.utils.data.DataLoader(validset, batch_size=128,shuffle=True, num_workers=2)

testset = CIFAR100Test(transform=transform_test)
testloader = torch.utils.data.DataLoader(testset, batch_size=100,shuffle=False, num_workers=2)
##classes name
dict_labels = unpickle('./data/cifar_100_meta')
classes = dict_labels[b'fine_label_names']
#GPU setting
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")<jupyter_output><empty_output><jupyter_text>2. Model  leNet<jupyter_code>class LeNet(nn.Module):
 
    def __init__(self):
        nn.Module.__init__(self) 
 
        self.features = nn.Sequential(
            nn.Conv2d(in_channels=3,out_channels=6,kernel_size=5),
            nn.MaxPool2d(kernel_size=2,stride=2),
            nn.Conv2d(in_channels=6,out_channels=16,kernel_size=5),
            nn.MaxPool2d(kernel_size=2,stride=2)
        )
        self.classifier = nn.Sequential(
            nn.Linear(in_features=16*5*5,out_features=120),
            nn.Linear(in_features=120,out_features=84),
            nn.Linear(in_features=84,out_features=100),
        )
 
    def forward(self, x):
        x = self.features(x)
        x = x.view(-1, 16*5*5)
        x = self.classifier(x)
        return x
      
lenet = LeNet()
lenet = lenet.to(device)<jupyter_output><empty_output><jupyter_text>alexNet<jupyter_code>class AlexNet(nn.Module):

  def __init__(self):
    super(AlexNet, self).__init__()
    self.features = nn.Sequential(
      nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1),
      nn.ReLU(inplace=True),
      nn.MaxPool2d(kernel_size=3, stride=2),
      nn.Conv2d(64, 192, kernel_size=3, stride=1, padding=1),
      nn.ReLU(inplace=True),
      nn.MaxPool2d(kernel_size=2, stride=2),
      nn.Conv2d(192, 384, kernel_size=3, stride=1, padding=1),
      nn.ReLU(inplace=True),
      nn.Conv2d(384, 256, kernel_size=3, stride=1, padding=1),
      nn.ReLU(inplace=True),
      nn.Conv2d(256, 256, kernel_size=3, stride=1, padding=1),
      nn.ReLU(inplace=True),
      nn.MaxPool2d(kernel_size=3, stride=2),
    )
    self.classifier = nn.Sequential(
      nn.Dropout(),
      nn.Linear(256*1*1, 4096),
      nn.ReLU(inplace=True),
      nn.Dropout(),
      nn.Linear(4096, 4096),
      nn.ReLU(inplace=True),
      nn.Linear(4096, 100),
    )

  def forward(self, x):
    x = self.features(x)
    x = torch.flatten(x, 1)
    x = self.classifier(x)
    return x

alexnet = AlexNet()
alexnet = alexnet.to(device)<jupyter_output><empty_output><jupyter_text>vgg<jupyter_code>class VggNet(nn.Module):

  def __init__(self):
    super(VggNet, self).__init__()
    self.features = nn.Sequential(
      nn.Conv2d(3, 64, kernel_size=3, padding=1),
      nn.BatchNorm2d(64),
      nn.ReLU(inplace=True),
      nn.Conv2d(64, 64, kernel_size=3, padding=1),
      nn.BatchNorm2d(64),
      nn.ReLU(inplace=True),
      nn.MaxPool2d(kernel_size=2, stride=2),
      nn.Conv2d(64, 128, kernel_size=3,padding=1),
      nn.BatchNorm2d(128),
      nn.ReLU(inplace=True),
      nn.Conv2d(128, 128, kernel_size=3, padding=1),
      nn.BatchNorm2d(128),
      nn.ReLU(inplace=True),
      nn.MaxPool2d(kernel_size=2, stride=2),
      nn.Conv2d(128, 256, kernel_size=3, padding=1),
      nn.BatchNorm2d(256),
      nn.ReLU(inplace=True),
      nn.Conv2d(256, 256, kernel_size=3, padding=1),
      nn.BatchNorm2d(256),
      nn.ReLU(inplace=True),
      nn.Conv2d(256, 256, kernel_size=3, padding=1),
      nn.BatchNorm2d(256),
      nn.ReLU(inplace=True),
      nn.MaxPool2d(kernel_size=2, stride=2),
      nn.Conv2d(256, 512, kernel_size=3, padding=1), 
      nn.BatchNorm2d(512),
      nn.ReLU(inplace=True),
      nn.Conv2d(512, 512, kernel_size=3, padding=1),
      nn.BatchNorm2d(512),
      nn.ReLU(inplace=True),
      nn.Conv2d(512, 512, kernel_size=3, padding=1),
      nn.BatchNorm2d(512),
      nn.ReLU(inplace=True),
      nn.MaxPool2d(kernel_size=2, stride=2),
      nn.Conv2d(512, 512, kernel_size=3, padding=1),
      nn.BatchNorm2d(512),
      nn.ReLU(inplace=True),
      nn.Conv2d(512, 512, kernel_size=3, padding=1),
      nn.BatchNorm2d(512),
      nn.ReLU(inplace=True),
      nn.Conv2d(512, 512, kernel_size=3, padding=1),
      nn.BatchNorm2d(512),
      nn.ReLU(inplace=True),
      nn.MaxPool2d(kernel_size=2, stride=2),
    )
    self.classifier = nn.Sequential(
      nn.Dropout(),
      nn.Linear(512, 4096), #4096
      nn.ReLU(inplace=True),
      nn.Dropout(),
      nn.Linear(4096, 4096),
      nn.ReLU(inplace=True),
      nn.Linear(4096, 100),
    )

  def forward(self, x):
    x = self.features(x)
    x = torch.flatten(x, 1)
    x = self.classifier(x)
    return x


vggnet = VggNet()
vggnet = vggnet.to(device)<jupyter_output><empty_output><jupyter_text>3. Define a Loss function and optimizer<jupyter_code>import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(vggnet.parameters(), lr=0.0008, momentum=0.9,weight_decay=5e-4)
#optimizer = torch.optim.Adam(vggnet.parameters(), lr=0.0001)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones= [50, 60, 70] , gamma=0.2)
#create checkpoint folder to save model
import os
from datetime import datetime

checkpoint_path = os.path.join('./', 'vggNet', datetime.now().isoformat())
if not os.path.exists(checkpoint_path):
    os.makedirs(checkpoint_path)
checkpoint_path = os.path.join(checkpoint_path, '{net}-{epoch}-{type}.pth')
def eval_training(epoch, net):
    net.eval()

    test_loss = 0.0 # cost function error
    correct = 0.0

    for (labels, images) in validloader:
        images, labels = images.to(device), labels.to(device)

        outputs = net(images)
        loss = criterion(outputs, labels)
        test_loss += loss.item()
        _, preds = outputs.max(1)
        correct += preds.eq(labels).sum()

    print('Test set: Average loss: {:.4f}, Accuracy: {:.4f}'.format(
        test_loss / len(validloader.dataset),
        correct.float() / len(validloader.dataset)
    ))
    print()
    return correct.float() / len(validloader.dataset)
def train(epoch, net):

    net.train()
    for batch_index, (labels, images) in enumerate(trainloader):

        images, labels = images.to(device), labels.to(device)

        optimizer.zero_grad()
        outputs = net(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        if batch_index % 27 == 26:
          print('Training Epoch: {epoch} [{trained_samples}/{total_samples}]\tLoss: {:0.4f}\tLR: {:0.6f}'.format(
              loss.item(),
              optimizer.param_groups[0]['lr'],
              epoch=epoch,
              trained_samples=batch_index * 128 + len(images),
              total_samples=len(trainloader.dataset)
          ))<jupyter_output><empty_output><jupyter_text>4. Train the network<jupyter_code>%%time
best_acc = 0.0
for epoch in range(1, 81):
    train(epoch, vggnet)
    acc = eval_training(epoch, vggnet) # evalate on validation set
    scheduler.step()
    if epoch > 50 and best_acc < acc:
        torch.save(vggnet.state_dict(), checkpoint_path.format(net='vggnet', epoch=epoch, type='best'))
        best_acc = acc
        continue

    if not epoch % 20:
        torch.save(vggnet.state_dict(), checkpoint_path.format(net='vggnet', epoch=epoch, type='regular'))<jupyter_output><empty_output><jupyter_text>5. Evaluate the network on the test data<jupyter_code>#loading the best model
vggnet.load_state_dict(torch.load('./vgg.pth'))
%%time
def accuracy(net, dataloader):
    correct = 0
    total = 0
 
    for data in dataloader:
        labels, images = data
        images, labels = images.to(device), labels.to(device)
        outputs = net(images)
        _, predicted = torch .max(outputs.data, 1)
        total += labels.size(0) 
        correct += (predicted==labels).sum()
    acc = 100*correct/total
    print('Accuracy： {}%'.format(acc))
    
accuracy(vggnet, testloader)
pred = []
true = []
with torch.no_grad():
    for data in testloader:
        labels, images = data
        images, labels = images.to(device), labels.to(device)
        outputs = vggnet(images)
        _, predicted = torch.max(outputs, 1)
        preds = predicted.cpu().numpy()
        target = labels.cpu().numpy()
        preds = np.reshape(preds,(len(preds),1))
        target = np.reshape(target,(len(preds),1))
        for i in range(len(preds)):
            pred.append(preds[i])
            true.append(target[i])
def performance_matrix(true,pred):
    precision = metrics.precision_score(true,pred,average='macro')
    recall = metrics.recall_score(true,pred,average='macro')
    accuracy = metrics.accuracy_score(true,pred)
    f1_score = metrics.f1_score(true,pred,average='macro')
    print('Confusion Matrix:\n',metrics.confusion_matrix(true, pred))
    print('Accuracy: {} Precision: {}, Recall: {}, F1_score: {}'.format(accuracy,precision,recall,f1_score))
performance_matrix(true,pred)
class_correct = list(0. for i in range(100))
class_total = list(0. for i in range(100))
with torch.no_grad():
    for data in testloader:
        labels, images = data
        images, labels = images.to(device), labels.to(device)
        outputs = vggnet(images)
        _, predicted = torch.max(outputs, 1)
        c = (predicted == labels).squeeze()
        for i in range(100):
            label = labels[i]
            class_correct[label] += c[i].item()
            class_total[label] += 1


for i in range(100):
    print('Accuracy of %5s : %2d %%' % (
        classes[i], 100 * class_correct[i] / class_total[i]))<jupyter_output>Accuracy of b'apple' : 85 %
Accuracy of b'aquarium_fish' : 83 %
Accuracy of b'baby' : 53 %
Accuracy of b'bear' : 47 %
Accuracy of b'beaver' : 50 %
Accuracy of b'bed' : 69 %
Accuracy of b'bee' : 76 %
Accuracy of b'beetle' : 66 %
Accuracy of b'bicycle' : 80 %
Accuracy of b'bottle' : 79 %
Accuracy of b'bowl' : 52 %
Accuracy of b'boy' : 41 %
Accuracy of b'bridge' : 75 %
Accuracy of b'bus' : 53 %
Accuracy of b'butterfly' : 68 %
Accuracy of b'camel' : 76 %
Accuracy of b'can' : 70 %
Accuracy of b'castle' : 83 %
Accuracy of b'caterpillar' : 66 %
Accuracy of b'cattle' : 64 %
Accuracy of b'chair' : 82 %
Accuracy of b'chimpanzee' : 80 %
Accuracy of b'clock' : 62 %
Accuracy of b'cloud' : 81 %
Accuracy of b'cockroach' : 80 %
Accuracy of b'couch' : 54 %
Accuracy of b'crab' : 58 %
Accuracy of b'crocodile' : 53 %
Accuracy of b'cup' : 81 %
Accuracy of b'dinosaur' : 71 %
Accuracy of b'dolphin' : 58 %
Accuracy of b'elephant' : 61 %
Accuracy of b'flatfish' : 63 %
Accuracy of b'forest' : 64 %
Accuracy of b[...]
{'fraction_non_alphanumeric': 0.14638502756853305, 'fraction_numerical': 0.04869146540343248, 'mean_word_length': 3.291236254581806, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '382641', 'license': 'Apache-2.0', 'n_tokens_mistral': 5116, 'n_tokens_neox': 4629, 'n_words': 882, 'path': 'CIFAR-100 CNN.ipynb', 'repo_name': 'xhua5296/CIFAR-100', 'size': '12877'}