net_conv1_conv2_conv3_conv4_py
生活随笔
收集整理的這篇文章主要介紹了
net_conv1_conv2_conv3_conv4_py
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
flyai.exe train -p=1 -b=64 -e=6000
將net_add_conv5_conv6_py 換成 net_conv1_conv2_conv3_conv4_py
score : 82.21flyai.exe train -p=1 -b=64 -e=6000
score : 85.15修改模型保存方式,將
# 若測試準確率高于當前最高準確率,則保存模型train_accuracy = eval(model, x_test, y_test) # if train_accuracy > best_accuracy: # best_accuracy = train_accuracy # model.save_model(cnn, MODEL_PATH, overwrite=True) # print("step %d, best accuracy %g" % (i, best_accuracy))改為
if i == args.EPOCHS - 1:model.save_model(cnn, MODEL_PATH, overwrite=True)print("step %d, the model is saved" % (i))if i == args.EPOCHS:model.save_model(cnn, MODEL_PATH, overwrite=True)print("step %d, the model is saved" % (i))print(str(i) + "/" + str(args.EPOCHS))main.py
和net_conv5_conv6的main.py一樣
cnn = Net().to(device) optimizer = Adam(cnn.parameters(), lr=0.0005, betas=(0.99999999, 0.999999999999)) # 選用AdamOptimizer """ 實現Adam算法。它在Adam: [A Method for Stochastic Optimization](https://arxiv.org/pdf/1412.6980.pdf)中被提出。參數:params (iterable) – 用于優化的可以迭代參數或定義參數組 lr (float, 可選) – 學習率(默認:1e-3) betas (Tuple[float, float], 可選) – 用于計算梯度運行平均值及其平方的系數(默認:0.9,0.999) eps (float, 可選) – 增加分母的數值以提高數值穩定性(默認:1e-8) weight_decay (float, 可選) – 權重衰減(L2范數)(默認: 0) """ # optimizer = Adam(cnn.parameters(), lr = 1e-4, momentum=0.99997) # 選用SGD_Optimizer(Stochastic Gradient Descent) # 自適應優化算法訓練出來的結果通常都不如SGD,盡管這些自適應優化算法在訓練時表現的看起來更好。 使用者應當慎重使用自適應優化算法。 """ 利用慣性momentum,即當前梯度與上次梯度進行加權,- 如果方向一致,則累加導致更新步長變大;- 如果方向不同,則相互抵消中和導致更新趨向平衡。 """ loss_fn = nn.CrossEntropyLoss() # 定義損失函數net.py
# build CNN from torch import nn# build CNN class Net(nn.Module): #def __init__(self,num_classes=10):def __init__(self):super(Net, self).__init__() self.conv1 = nn.Conv2d(3, 32, 5, stride=1, padding=2) self.relu1=nn.ReLU(True)self.bn1=nn.BatchNorm2d(32) self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(32, 64, 3, stride=1, padding=1)self.relu2=nn.ReLU(True)self.bn2=nn.BatchNorm2d(64) self.pool2 = nn.MaxPool2d(2, 2) self.conv3 = nn.Conv2d(64, 128, 3, stride=1, padding=1)self.relu3=nn.ReLU(True)self.bn3=nn.BatchNorm2d(128) self.pool3 = nn.MaxPool2d(2, 2) self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)self.relu4=nn.ReLU(True)self.bn4=nn.BatchNorm2d(128) self.pool4 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(128*8*8, 1024) self.relu5=nn.ReLU(True)self.fc2 = nn.Linear(1024,6)def forward(self, input):output = self.conv1(input)output = self.relu1(output)output = self.bn1(output)output = self.pool1(output)output = self.conv2(output)output = self.relu2(output)output = self.bn2(output)output = self.pool2(output)output = self.conv3(output)output = self.relu3(output)output = self.bn3(output)output = self.pool3(output)output = self.conv4(output)output = self.relu4(output)output = self.bn4(output)output = self.pool4(output)output = output.view(-1, 128*8*8)output = self.fc1(output)output = self.relu5(output)output = self.fc2(output)return outputflyai.exe train -p=1 -b=64 -e=8000
score : 85.38main.py
# -*- coding: utf-8 -* import argparse import torch import torch.nn as nn from flyai.dataset import Dataset from torch.optim import Adamfrom model import Model from net import Net from path import MODEL_PATH# 數據獲取輔助類 dataset = Dataset()# 模型操作輔助類 model = Model(dataset)# 超參 parser = argparse.ArgumentParser() parser.add_argument("-e", "--EPOCHS", default=1000, type=int, help="train epochs") parser.add_argument("-b", "--BATCH", default=256, type=int, help="batch size") parser.add_argument("-lr", "--learning_rate", default=0.001, type=float, help="learning_rate") args = parser.parse_args()# 判斷gpu是否可用 if torch.cuda.is_available():device = 'cuda' else:device = 'cpu' device = torch.device(device)def eval(model, x_test, y_test):cnn.eval()batch_eval = model.batch_iter(x_test, y_test)total_acc = 0.0data_len = len(x_test)for x_batch, y_batch in batch_eval:batch_len = len(x_batch)outputs = cnn(x_batch)_, prediction = torch.max(outputs.data, 1)correct = (prediction == y_batch).sum().item()acc = correct / batch_lentotal_acc += acc * batch_lenreturn total_acc / data_len#cnn = Net().to(device) #optimizer = Adam(cnn.parameters(), lr=0.001, betas=(0.9, 0.999)) # 選用AdamOptimizer #optimizer = Adam(cnn.parameters(), lr=0.00005, betas=(0.999999, 0.99999999999)) # 選用AdamOptimizer #loss_fn = nn.CrossEntropyLoss() # 定義損失函數cnn = Net().to(device) optimizer = Adam(cnn.parameters(), lr=0.0005, betas=(0.99999999, 0.999999999999)) # 選用AdamOptimizer """ 實現Adam算法。它在Adam: [A Method for Stochastic Optimization](https://arxiv.org/pdf/1412.6980.pdf)中被提出。參數:params (iterable) – 用于優化的可以迭代參數或定義參數組 lr (float, 可選) – 學習率(默認:1e-3) betas (Tuple[float, float], 可選) – 用于計算梯度運行平均值及其平方的系數(默認:0.9,0.999) eps (float, 可選) – 增加分母的數值以提高數值穩定性(默認:1e-8) weight_decay (float, 可選) – 權重衰減(L2范數)(默認: 0) """ # optimizer = Adam(cnn.parameters(), lr = 1e-4, momentum=0.99997) # 選用SGD_Optimizer(Stochastic Gradient Descent) # 自適應優化算法訓練出來的結果通常都不如SGD,盡管這些自適應優化算法在訓練時表現的看起來更好。 使用者應當慎重使用自適應優化算法。 """ 利用慣性momentum,即當前梯度與上次梯度進行加權,- 如果方向一致,則累加導致更新步長變大;- 如果方向不同,則相互抵消中和導致更新趨向平衡。 """ loss_fn = nn.CrossEntropyLoss() # 定義損失函數# 訓練并評估模型best_accuracy = 0 for i in range(args.EPOCHS):cnn.train()x_train, y_train, x_test, y_test = dataset.next_batch(args.BATCH) # 讀取數據x_train = torch.from_numpy(x_train)y_train = torch.from_numpy(y_train)x_train = x_train.float().to(device)y_train = y_train.long().to(device)x_test = torch.from_numpy(x_test)y_test = torch.from_numpy(y_test)x_test = x_test.float().to(device)y_test = y_test.long().to(device)outputs = cnn(x_train)_, prediction = torch.max(outputs.data, 1)optimizer.zero_grad()loss = loss_fn(outputs, y_train)loss.backward()optimizer.step()# 若測試準確率高于當前最高準確率,則保存模型train_accuracy = eval(model, x_test, y_test) # if train_accuracy > best_accuracy: # best_accuracy = train_accuracy # model.save_model(cnn, MODEL_PATH, overwrite=True) # print("step %d, best accuracy %g" % (i, best_accuracy))if i == 5000:model.save_model(cnn, MODEL_PATH, overwrite=True)print("step %d, the model is saved" % (i))if i == 6000:model.save_model(cnn, MODEL_PATH, overwrite=True)print("step %d, the model is saved" % (i))if i == args.EPOCHS - 1:model.save_model(cnn, MODEL_PATH, overwrite=True)print("step %d, the model is saved" % (i))if i == args.EPOCHS:model.save_model(cnn, MODEL_PATH, overwrite=True)print("step %d, the model is saved" % (i))print(str(i) + "/" + str(args.EPOCHS))net.py
## build CNN from torch import nn## build CNN class Net(nn.Module): #def __init__(self,num_classes=10):def __init__(self):super(Net, self).__init__() self.conv1 = nn.Conv2d(3, 32, 5, stride=1, padding=2) self.relu1=nn.ReLU(True)self.bn1=nn.BatchNorm2d(32) self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(32, 64, 3, stride=1, padding=1)self.relu2=nn.ReLU(True)self.bn2=nn.BatchNorm2d(64) self.pool2 = nn.MaxPool2d(2, 2) self.conv3 = nn.Conv2d(64, 128, 3, stride=1, padding=1)self.relu3=nn.ReLU(True)self.bn3=nn.BatchNorm2d(128) self.pool3 = nn.MaxPool2d(2, 2) self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)self.relu4=nn.ReLU(True)self.bn4=nn.BatchNorm2d(128) self.pool4 = nn.MaxPool2d(2, 2) #self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)self.relu4=nn.ReLU(True)self.bn4=nn.BatchNorm2d(128) self.pool4 = nn.MaxPool2d(2, 2) self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)self.relu4=nn.ReLU(True)self.bn4=nn.BatchNorm2d(128) self.pool4 = nn.MaxPool2d(2, 2) self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)self.relu4=nn.ReLU(True)self.bn4=nn.BatchNorm2d(128) self.pool4 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(128*8*8, 1024) #self.relu5=nn.ReLU(True)self.fc2 = nn.Linear(1024,6)def forward(self, input):output = self.conv1(input)output = self.relu1(output)output = self.bn1(output)output = self.pool1(output)output = self.conv2(output)output = self.relu2(output)output = self.bn2(output)output = self.pool2(output)output = self.conv3(output)output = self.relu3(output)output = self.bn3(output)output = self.pool3(output)output = self.conv4(output)output = self.relu4(output)output = self.bn4(output)output = self.pool4(output)output = output.view(-1, 128*8*8)output = self.fc1(output)output = self.relu5(output)output = self.fc2(output)return output./flyai train -p=1 -b=64 -e=8000
score : 85.24 ## build CNN from torch import nn## build CNN class Net(nn.Module): #def __init__(self,num_classes=10):def __init__(self):super(Net, self).__init__() self.conv1 = nn.Conv2d(3, 32, 5, stride=1, padding=2) self.relu1=nn.ReLU(True)self.bn1=nn.BatchNorm2d(32) self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(32, 64, 3, stride=1, padding=1)self.relu2=nn.ReLU(True)self.bn2=nn.BatchNorm2d(64) self.pool2 = nn.MaxPool2d(2, 2) self.conv3 = nn.Conv2d(64, 128, 3, stride=1, padding=1)self.relu3=nn.ReLU(True)self.bn3=nn.BatchNorm2d(128) self.pool3 = nn.MaxPool2d(2, 2) self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)self.relu4=nn.ReLU(True)self.bn4=nn.BatchNorm2d(128) self.pool4 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(128*8*8, 1024) self.relu5=nn.ReLU(True)self.fc2 = nn.Linear(1024,6)def forward(self, input):output = self.conv1(input)output = self.relu1(output)output = self.bn1(output)output = self.pool1(output)output = self.conv2(output)output = self.relu2(output)output = self.bn2(output)output = self.pool2(output)output = self.conv3(output)output = self.relu3(output)output = self.bn3(output)output = self.pool3(output)output = self.conv4(output)output = self.relu4(output)output = self.bn4(output)output = self.pool4(output)output = output.view(-1, 128*8*8)output = self.fc1(output)output = self.relu5(output)output = self.fc2(output)return output./flyai train -p=1 -b=64 -e=8000
score : 83.24將AdamOptimizer換成SGD_Optimizer(Stochastic Gradient Descent)
main.py
# -*- coding: utf-8 -* import argparse import torch import torch.nn as nn from flyai.dataset import Dataset from torch.optim import Adam from torch.optim import SGDfrom model import Model from net import Net from path import MODEL_PATH# 數據獲取輔助類 dataset = Dataset()# 模型操作輔助類 model = Model(dataset)# 超參 parser = argparse.ArgumentParser() parser.add_argument("-e", "--EPOCHS", default=1000, type=int, help="train epochs") parser.add_argument("-b", "--BATCH", default=256, type=int, help="batch size") parser.add_argument("-lr", "--learning_rate", default=0.001, type=float, help="learning_rate") parser.add_argument("-m", "--momentum", default=0.9, type=int, help="momentum") # parser.add_argument("- args = parser.parse_args()# 判斷gpu是否可用 if torch.cuda.is_available():device = 'cuda' else:device = 'cpu' device = torch.device(device)def eval(model, x_test, y_test):cnn.eval()batch_eval = model.batch_iter(x_test, y_test)total_acc = 0.0data_len = len(x_test)for x_batch, y_batch in batch_eval:batch_len = len(x_batch)outputs = cnn(x_batch)_, prediction = torch.max(outputs.data, 1)correct = (prediction == y_batch).sum().item()acc = correct / batch_lentotal_acc += acc * batch_lenreturn total_acc / data_len#cnn = Net().to(device) #optimizer = Adam(cnn.parameters(), lr=0.001, betas=(0.9, 0.999)) # 選用AdamOptimizer #optimizer = Adam(cnn.parameters(), lr=0.00005, betas=(0.999999, 0.99999999999)) # 選用AdamOptimizer #loss_fn = nn.CrossEntropyLoss() # 定義損失函數cnn = Net().to(device) # optimizer = Adam(cnn.parameters(), lr=0.0005, betas=(0.99999999, 0.999999999999)) # 選用AdamOptimizer """ 實現Adam算法。它在Adam: [A Method for Stochastic Optimization](https://arxiv.org/pdf/1412.6980.pdf)中被提出。參數:params (iterable) – 用于優化的可以迭代參數或定義參數組 lr (float, 可選) – 學習率(默認:1e-3) betas (Tuple[float, float], 可選) – 用于計算梯度運行平均值及其平方的系數(默認:0.9,0.999) eps (float, 可選) – 增加分母的數值以提高數值穩定性(默認:1e-8) weight_decay (float, 可選) – 權重衰減(L2范數)(默認: 0) """ optimizer = SGD(cnn.parameters(), lr = 1e-4, momentum=0.99997) # 選用SGD_Optimizer(Stochastic Gradient Descent) # 自適應優化算法訓練出來的結果通常都不如SGD,盡管這些自適應優化算法在訓練時表現的看起來更好。 使用者應當慎重使用自適應優化算法。 """ 利用慣性momentum,即當前梯度與上次梯度進行加權,- 如果方向一致,則累加導致更新步長變大;- 如果方向不同,則相互抵消中和導致更新趨向平衡。 """ loss_fn = nn.CrossEntropyLoss() # 定義損失函數# 訓練并評估模型best_accuracy = 0 for i in range(args.EPOCHS):cnn.train()x_train, y_train, x_test, y_test = dataset.next_batch(args.BATCH) # 讀取數據x_train = torch.from_numpy(x_train)y_train = torch.from_numpy(y_train)x_train = x_train.float().to(device)y_train = y_train.long().to(device)x_test = torch.from_numpy(x_test)y_test = torch.from_numpy(y_test)x_test = x_test.float().to(device)y_test = y_test.long().to(device)outputs = cnn(x_train)_, prediction = torch.max(outputs.data, 1)optimizer.zero_grad()loss = loss_fn(outputs, y_train)loss.backward()optimizer.step()# 若測試準確率高于當前最高準確率,則保存模型train_accuracy = eval(model, x_test, y_test) # if train_accuracy > best_accuracy: # best_accuracy = train_accuracy # model.save_model(cnn, MODEL_PATH, overwrite=True) # print("step %d, best accuracy %g" % (i, best_accuracy))if i == 5000:model.save_model(cnn, MODEL_PATH, overwrite=True)print("step %d, the model is saved" % (i))if i == 6000:model.save_model(cnn, MODEL_PATH, overwrite=True)print("step %d, the model is saved" % (i))if i == args.EPOCHS - 1:model.save_model(cnn, MODEL_PATH, overwrite=True)print("step %d, the model is saved" % (i))if i == args.EPOCHS:model.save_model(cnn, MODEL_PATH, overwrite=True)print("step %d, the model is saved" % (i))print(str(i) + "/" + str(args.EPOCHS))net.py
## build CNN from torch import nn## build CNN class Net(nn.Module): #def __init__(self,num_classes=10):def __init__(self):super(Net, self).__init__() self.conv1 = nn.Conv2d(3, 32, 5, stride=1, padding=2) self.relu1=nn.ReLU(True)self.bn1=nn.BatchNorm2d(32) self.pool1 = nn.MaxPool2d(2, 2) self.conv2 = nn.Conv2d(32, 64, 3, stride=1, padding=1)self.relu2=nn.ReLU(True)self.bn2=nn.BatchNorm2d(64) self.pool2 = nn.MaxPool2d(2, 2) self.conv3 = nn.Conv2d(64, 128, 3, stride=1, padding=1)self.relu3=nn.ReLU(True)self.bn3=nn.BatchNorm2d(128) self.pool3 = nn.MaxPool2d(2, 2) self.conv4 = nn.Conv2d(128, 128, 3, stride=1, padding=1)self.relu4=nn.ReLU(True)self.bn4=nn.BatchNorm2d(128) self.pool4 = nn.MaxPool2d(2, 2) self.fc1 = nn.Linear(128*8*8, 1024) self.relu5=nn.ReLU(True)self.fc2 = nn.Linear(1024,6)def forward(self, input):output = self.conv1(input)output = self.relu1(output)output = self.bn1(output)output = self.pool1(output)output = self.conv2(output)output = self.relu2(output)output = self.bn2(output)output = self.pool2(output)output = self.conv3(output)output = self.relu3(output)output = self.bn3(output)output = self.pool3(output)output = self.conv4(output)output = self.relu4(output)output = self.bn4(output)output = self.pool4(output)output = output.view(-1, 128*8*8)output = self.fc1(output)output = self.relu5(output)output = self.fc2(output)return output轉載于:https://www.cnblogs.com/hugeng007/p/10628752.html
總結
以上是生活随笔為你收集整理的net_conv1_conv2_conv3_conv4_py的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 结对开发石家庄地铁系统
- 下一篇: Webshell免杀绕过waf