├── CNN_Implementation_Augmentation1.py ├── CNN_Implementation_Augmentation2.py ├── LICENSE └── README.md /CNN_Implementation_Augmentation1.py: -------------------------------------------------------------------------------- 1 | 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | import torchvision 6 | import torch.optim as optim 7 | import numpy as np 8 | import matplotlib.pyplot as plt 9 | 10 | n_epochs = 4 11 | batch_size_train = 60 12 | batch_size_test = 1000 13 | learning_rate = 0.01 14 | momentum = 0.5 15 | 16 | random_seed = 1 17 | torch.backends.cudnn.enabled = False 18 | torch.manual_seed(random_seed) 19 | train_loader = torch.utils.data.DataLoader( 20 | torchvision.datasets.MNIST('/files/', train=True, download=True, 21 | transform=torchvision.transforms.Compose([ 22 | torchvision.transforms.Resize((32,32)), 23 | torchvision.transforms.RandomHorizontalFlip(p=1), 24 | torchvision.transforms.RandomVerticalFlip(p=1), 25 | torchvision.transforms.RandomRotation(90), 26 | torchvision.transforms.RandomRotation(270), 27 | torchvision.transforms.ToTensor(), 28 | torchvision.transforms.Normalize( 29 | (0.1307,), (0.3081,)), 30 | torchvision.transforms.Lambda(lambda x : x + 0.1*torch.randn_like(x)), 31 | torchvision.transforms.Lambda(lambda x : x + np.sqrt(0.1)*torch.randn_like(x)), 32 | torchvision.transforms.Lambda(lambda x : x + 1*torch.randn_like(x)), 33 | ])), 34 | batch_size=batch_size_train, shuffle=True) 35 | 36 | test_loader = torch.utils.data.DataLoader( 37 | torchvision.datasets.MNIST('/files/', train=False, download=True, 38 | transform=torchvision.transforms.Compose([ 39 | torchvision.transforms.Resize((32,32)), 40 | torchvision.transforms.RandomHorizontalFlip(p=1), 41 | torchvision.transforms.ToTensor(), 42 | torchvision.transforms.Normalize( 43 | (0.1307,), (0.3081,)) 44 | ])), 45 | batch_size=batch_size_test, shuffle=True) 46 | 47 | examples = enumerate(test_loader) 48 | batch_idx, (example_data, example_targets) = next(examples) 49 | 50 | class Net(nn.Module): 51 | def __init__(self): 52 | super(Net, self).__init__() 53 | self.conv1 = nn.Conv2d(1,64,3,1,1) 54 | self.conv2 = nn.Conv2d(64,128,3,1,1) 55 | self.conv3 = nn.Conv2d(128,256,3,1,1) 56 | self.conv4 = nn.Conv2d(256,256,3,1,1) 57 | self.conv5 = nn.Conv2d(256,512,3,1,1) 58 | self.conv6 = nn.Conv2d(512,512,3,1,1) 59 | self.bn1 = nn.BatchNorm2d(64) 60 | self.bn2 = nn.BatchNorm2d(128) 61 | self.bn3 = nn.BatchNorm2d(256) 62 | self.bn4 = nn.BatchNorm2d(512) 63 | self.fc1 = nn.Linear(512,4096) 64 | self.fc2 = nn.Linear(4096,4096) 65 | self.fc3 = nn.Linear(4096,10) 66 | self.Drop = nn.Dropout2d(0.5) 67 | def forward(self,x): 68 | ##1 69 | x = self.conv1(x) 70 | x = self.bn1(x) 71 | x = F.relu(x) 72 | x = F.max_pool2d(x,(2,2)) 73 | ##2 74 | x = self.conv2(x) 75 | x = self.bn2(x) 76 | x = F.relu(x) 77 | x = F.max_pool2d(x,(2,2)) 78 | ##3 79 | x = self.conv3(x) 80 | x = self.bn3(x) 81 | x = F.relu(x) 82 | ##4 83 | x = self.conv4(x) 84 | x = self.bn3(x) 85 | x = F.relu(x) 86 | x = F.max_pool2d(x,(2,2)) 87 | ##5 88 | x = self.conv5(x) 89 | x = self.bn4(x) 90 | x = F.relu(x) 91 | ##6 92 | x = self.conv6(x) 93 | x = self.bn4(x) 94 | x = F.relu(x) 95 | x = F.max_pool2d(x,(2,2)) 96 | ##7 97 | x = self.conv6(x) 98 | x = self.bn4(x) 99 | x = F.relu(x) 100 | ##8 101 | x = self.conv6(x) 102 | x = self.bn4(x) 103 | x = F.relu(x) 104 | x = F.max_pool2d(x,(2,2)) 105 | ##9 106 | x = x.view(-1, self.num_flat_features(x)) 107 | x = self.fc1(x) 108 | x = F.relu(x) 109 | x = self.Drop(x) 110 | #10 111 | x = self.fc2(x) 112 | x = F.relu(x) 113 | x = self.Drop(x) 114 | ##11 115 | x = self.fc3(x) 116 | 117 | return F.log_softmax(x) 118 | 119 | def num_flat_features(self, x): 120 | size = x.size()[1:] # all dimensions except the batch dimension 121 | num_features = 1 122 | for s in size: 123 | num_features *= s 124 | return num_features 125 | 126 | network = Net() 127 | optimizer = optim.SGD(network.parameters(), lr=learning_rate, momentum=momentum) 128 | Accuracy_train = [] 129 | Accuracy_test = [] 130 | train_losses = [] 131 | train_losses_epoch = [] 132 | train_counter = [] 133 | test_losses = [] 134 | test_counter = [i*len(train_loader.dataset) for i in range(n_epochs + 1)] 135 | criterion = nn.CrossEntropyLoss() 136 | 137 | def train(epoch): 138 | network.train() 139 | correct_train = 0 140 | for batch_idx, (data, target) in enumerate(train_loader): 141 | optimizer.zero_grad() 142 | output = network(data) 143 | loss = criterion(output, target) 144 | loss.backward() 145 | optimizer.step() 146 | prediction = output.data.max(1, keepdim=True)[1] 147 | correct_train += prediction.eq(target.data.view_as(prediction)).sum() 148 | print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tAccuracy: [{}/{} ({:.0f}%)] '.format( 149 | epoch, (batch_idx+1) * len(data), len(train_loader.dataset), 150 | 100. * batch_idx / len(train_loader), loss.item(),np.array(correct_train),(batch_idx+1) * len(data), 151 | 100. * np.array(correct_train)/((batch_idx+1) * len(data)))) 152 | train_losses.append(loss.item()) 153 | train_counter.append((batch_idx*60) + ((epoch-1)*len(train_loader.dataset))) 154 | train_losses_epoch.append(loss.item()) 155 | Accuracy_train.append(100. * np.array(correct_train)/((batch_idx+1) * len(data))) 156 | print('Train Epoch: {} \tAvg. Loss: {:.6f}\tAccuracy: [{}/{} ({:.0f}%)] '.format( 157 | epoch, loss.item(),np.array(correct_train),(batch_idx+1) * len(data), 158 | 100. * np.array(correct_train)/((batch_idx+1) * len(data)))) 159 | 160 | 161 | def test(): 162 | network.eval() 163 | test_loss = 0 164 | correct = 0 165 | with torch.no_grad(): 166 | for data, target in test_loader: 167 | output = network(data) 168 | test_loss += criterion(output, target)#, size_average=False).item() 169 | pred = output.data.max(1, keepdim=True)[1] 170 | correct += pred.eq(target.data.view_as(pred)).sum() 171 | test_loss /= len(test_loader.dataset) 172 | test_losses.append(test_loss) 173 | Accuracy_test.append(100. * correct / len(test_loader.dataset)) 174 | print('\nTest set: Avg. loss: {:.6f}, Accuracy: {}/{} ({:.0f}%)\n'.format( 175 | test_loss, correct, len(test_loader.dataset), 176 | 100. * correct / len(test_loader.dataset))) 177 | 178 | for epoch in range(1, n_epochs + 1): 179 | train(epoch) 180 | test() 181 | 182 | fig = plt.figure() 183 | plt.scatter(np.array(range(1,n_epochs+1)), np.array(train_losses_epoch[0:n_epochs]), color='blue') 184 | plt.xlabel('number of epochs') 185 | plt.ylabel('Train Cross-entropy Loss') 186 | 187 | fig = plt.figure() 188 | plt.scatter(np.array(range(1,n_epochs+1)), np.array(test_losses[0:n_epochs]).T, color='blue') 189 | plt.xlabel('number of epochs') 190 | plt.ylabel('Test Cross-entropy Loss') 191 | 192 | fig = plt.figure() 193 | plt.scatter(np.array(range(1,n_epochs+1)), np.array(Accuracy_train[0:n_epochs]), color='blue') 194 | plt.xlabel('number of epochs') 195 | plt.ylabel('Train Accuracy %') 196 | 197 | fig = plt.figure() 198 | plt.scatter(np.array(range(1,n_epochs+1)), np.array(Accuracy_test[0:n_epochs]), color='blue') 199 | plt.xlabel('number of epochs') 200 | plt.ylabel('Test Accuracy %') 201 | plt.show() 202 | 203 | print('Train_L',train_losses_epoch[0:n_epochs]) 204 | print('Test_l',np.array(test_losses[0:n_epochs]).T) 205 | print('Train_A',Accuracy_train[0:n_epochs]) 206 | print('test_A',np.array(Accuracy_test[0:n_epochs])) 207 | -------------------------------------------------------------------------------- /CNN_Implementation_Augmentation2.py: -------------------------------------------------------------------------------- 1 | 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | import torchvision 6 | import torch.optim as optim 7 | import numpy as np 8 | import matplotlib.pyplot as plt 9 | 10 | n_epochs = 4 11 | batch_size_train = 60 12 | batch_size_test = 1000 13 | learning_rate = 0.01 14 | momentum = 0.5 15 | 16 | random_seed = 1 17 | torch.backends.cudnn.enabled = False 18 | torch.manual_seed(random_seed) 19 | train_loader = torch.utils.data.DataLoader( 20 | torchvision.datasets.MNIST('/files/', train=True, download=True, 21 | transform=torchvision.transforms.Compose([ 22 | torchvision.transforms.Resize((32,32)), 23 | torchvision.transforms.RandomHorizontalFlip(p=1), 24 | torchvision.transforms.RandomVerticalFlip(p=1), 25 | torchvision.transforms.RandomRotation(90), 26 | torchvision.transforms.RandomRotation(270), 27 | torchvision.transforms.ToTensor(), 28 | torchvision.transforms.Normalize( 29 | (0.1307,), (0.3081,)), 30 | torchvision.transforms.Lambda(lambda x : x + 0.1*torch.randn_like(x)), 31 | torchvision.transforms.Lambda(lambda x : x + np.sqrt(0.1)*torch.randn_like(x)), 32 | torchvision.transforms.Lambda(lambda x : x + 1*torch.randn_like(x)), 33 | ])), 34 | batch_size=batch_size_train, shuffle=True) 35 | 36 | 37 | test_loader = torch.utils.data.DataLoader( 38 | torchvision.datasets.MNIST('/files/', train=False, download=True, 39 | transform=torchvision.transforms.Compose([ 40 | torchvision.transforms.Resize((32,32)), 41 | torchvision.transforms.RandomVerticalFlip(p=1), 42 | torchvision.transforms.ToTensor(), 43 | torchvision.transforms.Normalize( 44 | (0.1307,), (0.3081,)) 45 | ])), 46 | batch_size=batch_size_test, shuffle=True) 47 | 48 | examples = enumerate(test_loader) 49 | batch_idx, (example_data, example_targets) = next(examples) 50 | 51 | class Net(nn.Module): 52 | def __init__(self): 53 | super(Net, self).__init__() 54 | self.conv1 = nn.Conv2d(1,64,3,1,1) 55 | self.conv2 = nn.Conv2d(64,128,3,1,1) 56 | self.conv3 = nn.Conv2d(128,256,3,1,1) 57 | self.conv4 = nn.Conv2d(256,256,3,1,1) 58 | self.conv5 = nn.Conv2d(256,512,3,1,1) 59 | self.conv6 = nn.Conv2d(512,512,3,1,1) 60 | self.bn1 = nn.BatchNorm2d(64) 61 | self.bn2 = nn.BatchNorm2d(128) 62 | self.bn3 = nn.BatchNorm2d(256) 63 | self.bn4 = nn.BatchNorm2d(512) 64 | self.fc1 = nn.Linear(512,4096) 65 | self.fc2 = nn.Linear(4096,4096) 66 | self.fc3 = nn.Linear(4096,10) 67 | self.Drop = nn.Dropout2d(0.5) 68 | def forward(self,x): 69 | ##1 70 | x = self.conv1(x) 71 | x = self.bn1(x) 72 | x = F.relu(x) 73 | x = F.max_pool2d(x,(2,2)) 74 | ##2 75 | x = self.conv2(x) 76 | x = self.bn2(x) 77 | x = F.relu(x) 78 | x = F.max_pool2d(x,(2,2)) 79 | ##3 80 | x = self.conv3(x) 81 | x = self.bn3(x) 82 | x = F.relu(x) 83 | ##4 84 | x = self.conv4(x) 85 | x = self.bn3(x) 86 | x = F.relu(x) 87 | x = F.max_pool2d(x,(2,2)) 88 | ##5 89 | x = self.conv5(x) 90 | x = self.bn4(x) 91 | x = F.relu(x) 92 | ##6 93 | x = self.conv6(x) 94 | x = self.bn4(x) 95 | x = F.relu(x) 96 | x = F.max_pool2d(x,(2,2)) 97 | ##7 98 | x = self.conv6(x) 99 | x = self.bn4(x) 100 | x = F.relu(x) 101 | ##8 102 | x = self.conv6(x) 103 | x = self.bn4(x) 104 | x = F.relu(x) 105 | x = F.max_pool2d(x,(2,2)) 106 | ##9 107 | x = x.view(-1, self.num_flat_features(x)) 108 | x = self.fc1(x) 109 | x = F.relu(x) 110 | x = self.Drop(x) 111 | #10 112 | x = self.fc2(x) 113 | x = F.relu(x) 114 | x = self.Drop(x) 115 | ##11 116 | x = self.fc3(x) 117 | 118 | return F.log_softmax(x) 119 | 120 | def num_flat_features(self, x): 121 | size = x.size()[1:] # all dimensions except the batch dimension 122 | num_features = 1 123 | for s in size: 124 | num_features *= s 125 | return num_features 126 | 127 | network = Net() 128 | optimizer = optim.SGD(network.parameters(), lr=learning_rate, momentum=momentum) 129 | Accuracy_train = [] 130 | Accuracy_test = [] 131 | train_losses = [] 132 | train_losses_epoch = [] 133 | train_counter = [] 134 | test_losses = [] 135 | test_counter = [i*len(train_loader.dataset) for i in range(n_epochs + 1)] 136 | criterion = nn.CrossEntropyLoss() 137 | 138 | def train(epoch): 139 | network.train() 140 | correct_train = 0 141 | for batch_idx, (data, target) in enumerate(train_loader): 142 | optimizer.zero_grad() 143 | output = network(data) 144 | loss = criterion(output, target) 145 | loss.backward() 146 | optimizer.step() 147 | prediction = output.data.max(1, keepdim=True)[1] 148 | correct_train += prediction.eq(target.data.view_as(prediction)).sum() 149 | print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tAccuracy: [{}/{} ({:.0f}%)] '.format( 150 | epoch, (batch_idx+1) * len(data), len(train_loader.dataset), 151 | 100. * batch_idx / len(train_loader), loss.item(),np.array(correct_train),(batch_idx+1) * len(data), 152 | 100. * np.array(correct_train)/((batch_idx+1) * len(data)))) 153 | train_losses.append(loss.item()) 154 | train_counter.append((batch_idx*60) + ((epoch-1)*len(train_loader.dataset))) 155 | train_losses_epoch.append(loss.item()) 156 | Accuracy_train.append(100. * np.array(correct_train)/((batch_idx+1) * len(data))) 157 | print('Train Epoch: {} \tAvg. Loss: {:.6f}\tAccuracy: [{}/{} ({:.0f}%)] '.format( 158 | epoch, loss.item(),np.array(correct_train),(batch_idx+1) * len(data), 159 | 100. * np.array(correct_train)/((batch_idx+1) * len(data)))) 160 | 161 | 162 | def test(): 163 | network.eval() 164 | test_loss = 0 165 | correct = 0 166 | with torch.no_grad(): 167 | for data, target in test_loader: 168 | output = network(data) 169 | test_loss += criterion(output, target)#, size_average=False).item() 170 | pred = output.data.max(1, keepdim=True)[1] 171 | correct += pred.eq(target.data.view_as(pred)).sum() 172 | test_loss /= len(test_loader.dataset) 173 | test_losses.append(test_loss) 174 | Accuracy_test.append(100. * correct / len(test_loader.dataset)) 175 | print('\nTest set: Avg. loss: {:.6f}, Accuracy: {}/{} ({:.0f}%)\n'.format( 176 | test_loss, correct, len(test_loader.dataset), 177 | 100. * correct / len(test_loader.dataset))) 178 | 179 | for epoch in range(1, n_epochs + 1): 180 | train(epoch) 181 | test() 182 | 183 | fig = plt.figure() 184 | plt.scatter(np.array(range(1,n_epochs+1)), np.array(train_losses_epoch[0:n_epochs]), color='blue') 185 | plt.xlabel('number of epochs') 186 | plt.ylabel('Train Cross-entropy Loss') 187 | 188 | fig = plt.figure() 189 | plt.scatter(np.array(range(1,n_epochs+1)), np.array(test_losses[0:n_epochs]).T, color='blue') 190 | plt.xlabel('number of epochs') 191 | plt.ylabel('Test Cross-entropy Loss') 192 | 193 | fig = plt.figure() 194 | plt.scatter(np.array(range(1,n_epochs+1)), np.array(Accuracy_train[0:n_epochs]), color='blue') 195 | plt.xlabel('number of epochs') 196 | plt.ylabel('Train Accuracy %') 197 | 198 | fig = plt.figure() 199 | plt.scatter(np.array(range(1,n_epochs+1)), np.array(Accuracy_test[0:n_epochs]), color='blue') 200 | plt.xlabel('number of epochs') 201 | plt.ylabel('Test Accuracy %') 202 | plt.show() 203 | 204 | print('Train_L',train_losses_epoch[0:n_epochs]) 205 | print('Test_l',np.array(test_losses[0:n_epochs]).T) 206 | print('Train_A',Accuracy_train[0:n_epochs]) 207 | print('test_A',np.array(Accuracy_test[0:n_epochs])) 208 | 209 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 AIsoltani 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CNN_Implementation_Augmentation 2 | Using data augmentation on CNN 3 | 4 | I try to augment training data by randomly 5 | 1. flip from left to right 6 | 2. flip from top to bottom 7 | 3. rotate from -90 degrees to +90 degrees 8 | 4. add Gaussian noise with variance 1 9 | 5. add Gaussian noise with variance 0.1 10 | 6. add Gaussian noise with variance 0.01 11 | In attachment, there are two python files entitled “CNN_Implementation_Augmentation1” and “CNN_Implementation_Augmentation2”. They are for evaluating 12 | test data with randomly horizontal and vertical flipping respectively. 13 | --------------------------------------------------------------------------------