train with pretrain
|
@ -6,3 +6,4 @@ __pycache__/
|
|||
Dataset/
|
||||
.vscode
|
||||
/*/__pycache__
|
||||
.mypy_cache
|
||||
|
|
|
@ -39,63 +39,19 @@ model = utils.SetDevice(Model.Net3Grad335())
|
|||
|
||||
|
||||
|
||||
# model = utils.LoadModel(model, CurrentPath+"/checkpoint.pkl")
|
||||
model = utils.LoadModel(model, CurrentPath+"/checkpoint.pkl")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# a = model.features[0].weight.data
|
||||
|
||||
|
||||
|
||||
|
||||
# traindata, testdata = Loader.MNIST(batchsize)
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, style="Vertical")
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, style="Horizontal")
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, style="VerticalOneLine")
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, style="VerticalZebra")
|
||||
# traindata, testdata = Loader.Cifar10Mono(batchsize)
|
||||
traindata, testdata = Loader.Cifar10Mono(batchsize, num_workers=2, shuffle=True)
|
||||
|
||||
|
||||
# def GetSample(netmodel,layer,dataloader,iteration=-1):
|
||||
# netmodel.eval()
|
||||
# sample = []
|
||||
# for batch_idx, (data, target) in enumerate(dataloader):
|
||||
# data = utils.SetDevice(data)
|
||||
# target = utils.SetDevice(target)
|
||||
# layerout = []
|
||||
# layerint = []
|
||||
# def getnet(self, input, output):
|
||||
# layerout.append(output)
|
||||
# layerint.append(input)
|
||||
# handle = netmodel.features[layer].register_forward_hook(getnet)
|
||||
# netmodel.forwardLayer(data,layer=layer)
|
||||
# output = layerout[0][:,:,:,:]
|
||||
|
||||
# handle.remove()
|
||||
# data.detach()
|
||||
# target.detach()
|
||||
|
||||
# sample.append(output.cpu().detach().numpy())
|
||||
# if iteration > 0 and batch_idx >= (iteration-1):
|
||||
# break
|
||||
|
||||
# sample = np.array(sample)
|
||||
# sample = np.swapaxes(sample,2,0)
|
||||
# sample = np.reshape(sample,(sample.shape[0],-1))
|
||||
|
||||
# active = []
|
||||
# for i in range(sample.shape[0]):
|
||||
# data = sample[i]
|
||||
# mean = np.mean(data)
|
||||
# dat1 = np.mean(np.abs(data - mean))
|
||||
# dat2 = (data - mean)/dat1
|
||||
# dat2 = np.mean(dat2*dat2)
|
||||
# active.append(dat2)
|
||||
|
||||
# return active
|
||||
traindata, testdata = Loader.Cifar10Mono(batchsize, num_workers=0, shuffle=True)
|
||||
|
||||
|
||||
def GetSample(netmodel,layer,dataloader,iteration=-1):
|
||||
|
@ -128,15 +84,18 @@ def GetSample(netmodel,layer,dataloader,iteration=-1):
|
|||
dat2 = torch.mean(dat2 * dat2,dim=1)
|
||||
return dat2.cpu().detach().numpy()
|
||||
|
||||
def weightToImage(weight,filename):
|
||||
a2 = model.features[layear].weight.data[channel]
|
||||
a2 = a2.cpu().detach().numpy().reshape(( a2.shape[-2], a2.shape[-1]))
|
||||
|
||||
a2min = np.min(a2)
|
||||
a2max = np.max(a2)
|
||||
a2 = (a2 - a2min)*255.0/(a2max-a2min)
|
||||
a2 = a2.astype(int)
|
||||
cv2.imwrite(filename,a2)
|
||||
def ConvKernelToImage(model, layer, foldname):
|
||||
if not os.path.exists(foldname):
|
||||
os.mkdir(foldname)
|
||||
a2 = model.features[layer].weight.data
|
||||
a2 = a2.cpu().detach().numpy().reshape((-1, a2.shape[-2], a2.shape[-1]))
|
||||
for i in range(a2.shape[0]):
|
||||
d = a2[i]
|
||||
dmin = np.min(d)
|
||||
dmax = np.max(d)
|
||||
d = (d - dmin)*255.0/(dmax-dmin)
|
||||
d = d.astype(int)
|
||||
cv2.imwrite(foldname+"/"+str(i)+".png", d)
|
||||
|
||||
def GetRandomSocre(netmodel,layer,dataloader,iteration=-1):
|
||||
weightshape = netmodel.features[layer].weight.data.shape
|
||||
|
@ -148,6 +107,12 @@ def GetRandomSocre(netmodel,layer,dataloader,iteration=-1):
|
|||
|
||||
|
||||
|
||||
ConvKernelToImage(model, 0, CurrentPath+"image")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
layer = 0
|
||||
|
||||
weightshape = list(model.features[layer].weight.data.shape)
|
||||
|
|
|
@ -14,6 +14,7 @@ from torch.utils.data import Dataset, DataLoader
|
|||
from PIL import Image
|
||||
import random
|
||||
import cv2
|
||||
from visdom import Visdom
|
||||
|
||||
|
||||
CurrentPath = os.path.split(os.path.realpath(__file__))[0]+"/"
|
||||
|
@ -23,36 +24,73 @@ sys.path.append(CurrentPath+'../tools')
|
|||
sys.path.append(CurrentPath+'../')
|
||||
|
||||
import Model
|
||||
from tools import utils, Train, Loader
|
||||
from tools import utils, Train, Loader, WebVisual
|
||||
|
||||
|
||||
batchsize = 128
|
||||
|
||||
|
||||
|
||||
# traindata, testdata = Loader.MNIST(batchsize, num_workers=4)
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, num_workers=4, style="Vertical")
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, num_workers=4, style="Horizontal")
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, num_workers=4, style="VerticalOneLine")
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, num_workers=4, style="VerticalZebra")
|
||||
traindata, testdata = Loader.Cifar10Mono(batchsize, num_workers=4,shuffle=True,trainsize=500)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
WebVisual.InitVisdom()
|
||||
window = WebVisual.LineWin()
|
||||
lineNoPre = WebVisual.Line("NoPre")
|
||||
linePretrain = WebVisual.Line("Pretrain")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# model = utils.SetDevice(Model.Net535())
|
||||
# model = utils.SetDevice(Model.Net5Grad35())
|
||||
# model = utils.SetDevice(Model.Net31535())
|
||||
# model = utils.SetDevice(Model.Net3335()) # Test set: Average loss: 1.5294, Accuracy: 4501/10000 (45%)
|
||||
# model = utils.SetDevice(Model.Net3335())
|
||||
model = utils.SetDevice(Model.Net3Grad335())
|
||||
|
||||
|
||||
|
||||
# model = utils.LoadModel(model, CurrentPath+"/checkpoint.pkl")
|
||||
|
||||
|
||||
|
||||
optimizer = optim.SGD(model.parameters(), lr=0.1)
|
||||
# optimizer = optim.Adam(model.parameters(), lr=0.01)
|
||||
|
||||
|
||||
|
||||
|
||||
model = utils.LoadModel(model, CurrentPath+"/checkpoint.pkl")
|
||||
|
||||
optimizer = optim.SGD(model.parameters(), lr=0.01)
|
||||
|
||||
|
||||
# traindata, testdata = Loader.MNIST(batchsize, num_workers=8)
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, num_workers=8, style="Vertical")
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, num_workers=8, style="Horizontal")
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, num_workers=8, style="VerticalOneLine")
|
||||
# traindata, testdata = Loader.RandomMnist(batchsize, num_workers=8, style="VerticalZebra")
|
||||
traindata, testdata = Loader.Cifar10Mono(batchsize, num_workers=8)
|
||||
|
||||
|
||||
|
||||
for i in range(300):
|
||||
for i in range(2000):
|
||||
Train.train(model,traindata,optimizer,epoch=i)
|
||||
Train.test(model,testdata)
|
||||
window.AppendData(linePretrain,Train.test(model,testdata))
|
||||
|
||||
utils.SaveModel(model,CurrentPath+"/checkpoint.pkl")
|
||||
|
||||
|
||||
model = utils.SetDevice(Model.Net3335())
|
||||
optimizer = optim.SGD(model.parameters(), lr=0.1)
|
||||
for i in range(2000):
|
||||
Train.train(model, traindata, optimizer, epoch=i)
|
||||
window.AppendData(lineNoPre, Train.test(model, testdata))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# utils.SaveModel(model,CurrentPath+"/checkpointSGDPreTrain.pkl")
|
||||
|
|
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
After Width: | Height: | Size: 77 B |
|
@ -1,5 +1,5 @@
|
|||
from __future__ import print_function
|
||||
from vgg19Pytorch import Vgg19Module
|
||||
# from vgg19Pytorch import Vgg19Module
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
|
@ -83,94 +83,94 @@ for w, p in zip(words[top5], probs):
|
|||
print('{}\tprobability:{}'.format(w, p))
|
||||
#endregion
|
||||
|
||||
# region write image and weight
|
||||
image = readAndPreprocessImage(CurrentPath+"dog_resize.jpg")
|
||||
conv1_1_pad = F.pad(image, (1L, 1L, 1L, 1L))
|
||||
conv1_1 = vgg19.conv1_1(conv1_1_pad)
|
||||
relu1_1 = F.relu(conv1_1)
|
||||
conv1_2_pad = F.pad(relu1_1, (1L, 1L, 1L, 1L))
|
||||
conv1_2 = vgg19.conv1_2(conv1_2_pad)
|
||||
relu1_2 = F.relu(conv1_2)
|
||||
pool1 = F.max_pool2d(relu1_2, kernel_size=(2L, 2L), stride=(2L, 2L), padding=(0L,), ceil_mode=True)
|
||||
conv2_1_pad = F.pad(pool1, (1L, 1L, 1L, 1L))
|
||||
conv2_1 = vgg19.conv2_1(conv2_1_pad)
|
||||
relu2_1 = F.relu(conv2_1)
|
||||
conv2_2_pad = F.pad(relu2_1, (1L, 1L, 1L, 1L))
|
||||
conv2_2 = vgg19.conv2_2(conv2_2_pad)
|
||||
relu2_2 = F.relu(conv2_2)
|
||||
pool2 = F.max_pool2d(relu2_2, kernel_size=(2L, 2L), stride=(2L, 2L), padding=(0L,), ceil_mode=True)
|
||||
conv3_1_pad = F.pad(pool2, (1L, 1L, 1L, 1L))
|
||||
conv3_1 = vgg19.conv3_1(conv3_1_pad)
|
||||
relu3_1 = F.relu(conv3_1)
|
||||
conv3_2_pad = F.pad(relu3_1, (1L, 1L, 1L, 1L))
|
||||
conv3_2 = vgg19.conv3_2(conv3_2_pad)
|
||||
relu3_2 = F.relu(conv3_2)
|
||||
conv3_3_pad = F.pad(relu3_2, (1L, 1L, 1L, 1L))
|
||||
conv3_3 = vgg19.conv3_3(conv3_3_pad)
|
||||
relu3_3 = F.relu(conv3_3)
|
||||
conv3_4_pad = F.pad(relu3_3, (1L, 1L, 1L, 1L))
|
||||
conv3_4 = vgg19.conv3_4(conv3_4_pad)
|
||||
relu3_4 = F.relu(conv3_4)
|
||||
pool3 = F.max_pool2d(relu3_4, kernel_size=(2L, 2L), stride=(2L, 2L), padding=(0L,), ceil_mode=True)
|
||||
conv4_1_pad = F.pad(pool3, (1L, 1L, 1L, 1L))
|
||||
conv4_1 = vgg19.conv4_1(conv4_1_pad)
|
||||
relu4_1 = F.relu(conv4_1)
|
||||
conv4_2_pad = F.pad(relu4_1, (1L, 1L, 1L, 1L))
|
||||
conv4_2 = vgg19.conv4_2(conv4_2_pad)
|
||||
relu4_2 = F.relu(conv4_2)
|
||||
conv4_3_pad = F.pad(relu4_2, (1L, 1L, 1L, 1L))
|
||||
conv4_3 = vgg19.conv4_3(conv4_3_pad)
|
||||
relu4_3 = F.relu(conv4_3)
|
||||
conv4_4_pad = F.pad(relu4_3, (1L, 1L, 1L, 1L))
|
||||
conv4_4 = vgg19.conv4_4(conv4_4_pad)
|
||||
relu4_4 = F.relu(conv4_4)
|
||||
pool4 = F.max_pool2d(relu4_4, kernel_size=(2L, 2L), stride=(2L, 2L), padding=(0L,), ceil_mode=True)
|
||||
conv5_1_pad = F.pad(pool4, (1L, 1L, 1L, 1L))
|
||||
conv5_1 = vgg19.conv5_1(conv5_1_pad)
|
||||
relu5_1 = F.relu(conv5_1)
|
||||
conv5_2_pad = F.pad(relu5_1, (1L, 1L, 1L, 1L))
|
||||
conv5_2 = vgg19.conv5_2(conv5_2_pad)
|
||||
relu5_2 = F.relu(conv5_2)
|
||||
conv5_3_pad = F.pad(relu5_2, (1L, 1L, 1L, 1L))
|
||||
conv5_3 = vgg19.conv5_3(conv5_3_pad)
|
||||
relu5_3 = F.relu(conv5_3)
|
||||
conv5_4_pad = F.pad(relu5_3, (1L, 1L, 1L, 1L))
|
||||
conv5_4 = vgg19.conv5_4(conv5_4_pad)
|
||||
relu5_4 = F.relu(conv5_4)
|
||||
pool5 = F.max_pool2d(relu5_4, kernel_size=(2L, 2L), stride=(2L, 2L), padding=(0L,), ceil_mode=True)
|
||||
fc6_0 = pool5.view(pool5.size(0), -1)
|
||||
# # region write image and weight
|
||||
# image = readAndPreprocessImage(CurrentPath+"dog_resize.jpg")
|
||||
# conv1_1_pad = F.pad(image, (1L, 1L, 1L, 1L))
|
||||
# conv1_1 = vgg19.conv1_1(conv1_1_pad)
|
||||
# relu1_1 = F.relu(conv1_1)
|
||||
# conv1_2_pad = F.pad(relu1_1, (1L, 1L, 1L, 1L))
|
||||
# conv1_2 = vgg19.conv1_2(conv1_2_pad)
|
||||
# relu1_2 = F.relu(conv1_2)
|
||||
# pool1 = F.max_pool2d(relu1_2, kernel_size=(2L, 2L), stride=(2L, 2L), padding=(0L,), ceil_mode=True)
|
||||
# conv2_1_pad = F.pad(pool1, (1L, 1L, 1L, 1L))
|
||||
# conv2_1 = vgg19.conv2_1(conv2_1_pad)
|
||||
# relu2_1 = F.relu(conv2_1)
|
||||
# conv2_2_pad = F.pad(relu2_1, (1L, 1L, 1L, 1L))
|
||||
# conv2_2 = vgg19.conv2_2(conv2_2_pad)
|
||||
# relu2_2 = F.relu(conv2_2)
|
||||
# pool2 = F.max_pool2d(relu2_2, kernel_size=(2L, 2L), stride=(2L, 2L), padding=(0L,), ceil_mode=True)
|
||||
# conv3_1_pad = F.pad(pool2, (1L, 1L, 1L, 1L))
|
||||
# conv3_1 = vgg19.conv3_1(conv3_1_pad)
|
||||
# relu3_1 = F.relu(conv3_1)
|
||||
# conv3_2_pad = F.pad(relu3_1, (1L, 1L, 1L, 1L))
|
||||
# conv3_2 = vgg19.conv3_2(conv3_2_pad)
|
||||
# relu3_2 = F.relu(conv3_2)
|
||||
# conv3_3_pad = F.pad(relu3_2, (1L, 1L, 1L, 1L))
|
||||
# conv3_3 = vgg19.conv3_3(conv3_3_pad)
|
||||
# relu3_3 = F.relu(conv3_3)
|
||||
# conv3_4_pad = F.pad(relu3_3, (1L, 1L, 1L, 1L))
|
||||
# conv3_4 = vgg19.conv3_4(conv3_4_pad)
|
||||
# relu3_4 = F.relu(conv3_4)
|
||||
# pool3 = F.max_pool2d(relu3_4, kernel_size=(2L, 2L), stride=(2L, 2L), padding=(0L,), ceil_mode=True)
|
||||
# conv4_1_pad = F.pad(pool3, (1L, 1L, 1L, 1L))
|
||||
# conv4_1 = vgg19.conv4_1(conv4_1_pad)
|
||||
# relu4_1 = F.relu(conv4_1)
|
||||
# conv4_2_pad = F.pad(relu4_1, (1L, 1L, 1L, 1L))
|
||||
# conv4_2 = vgg19.conv4_2(conv4_2_pad)
|
||||
# relu4_2 = F.relu(conv4_2)
|
||||
# conv4_3_pad = F.pad(relu4_2, (1L, 1L, 1L, 1L))
|
||||
# conv4_3 = vgg19.conv4_3(conv4_3_pad)
|
||||
# relu4_3 = F.relu(conv4_3)
|
||||
# conv4_4_pad = F.pad(relu4_3, (1L, 1L, 1L, 1L))
|
||||
# conv4_4 = vgg19.conv4_4(conv4_4_pad)
|
||||
# relu4_4 = F.relu(conv4_4)
|
||||
# pool4 = F.max_pool2d(relu4_4, kernel_size=(2L, 2L), stride=(2L, 2L), padding=(0L,), ceil_mode=True)
|
||||
# conv5_1_pad = F.pad(pool4, (1L, 1L, 1L, 1L))
|
||||
# conv5_1 = vgg19.conv5_1(conv5_1_pad)
|
||||
# relu5_1 = F.relu(conv5_1)
|
||||
# conv5_2_pad = F.pad(relu5_1, (1L, 1L, 1L, 1L))
|
||||
# conv5_2 = vgg19.conv5_2(conv5_2_pad)
|
||||
# relu5_2 = F.relu(conv5_2)
|
||||
# conv5_3_pad = F.pad(relu5_2, (1L, 1L, 1L, 1L))
|
||||
# conv5_3 = vgg19.conv5_3(conv5_3_pad)
|
||||
# relu5_3 = F.relu(conv5_3)
|
||||
# conv5_4_pad = F.pad(relu5_3, (1L, 1L, 1L, 1L))
|
||||
# conv5_4 = vgg19.conv5_4(conv5_4_pad)
|
||||
# relu5_4 = F.relu(conv5_4)
|
||||
# pool5 = F.max_pool2d(relu5_4, kernel_size=(2L, 2L), stride=(2L, 2L), padding=(0L,), ceil_mode=True)
|
||||
# fc6_0 = pool5.view(pool5.size(0), -1)
|
||||
|
||||
imageVisual(conv1_1, "conv1_1", 8)
|
||||
imageVisual(conv1_2, 'conv1_2', 8)
|
||||
imageVisual(conv2_1, "conv2_1", 16)
|
||||
imageVisual(conv2_2, "conv2_2", 16)
|
||||
imageVisual(conv3_1, "conv3_1", 16)
|
||||
imageVisual(conv3_2, "conv3_2", 16)
|
||||
imageVisual(conv3_3, "conv3_3", 16)
|
||||
imageVisual(conv3_4, "conv3_4", 16)
|
||||
imageVisual(conv4_1, "conv4_1", 32)
|
||||
imageVisual(conv4_2, "conv4_2", 32)
|
||||
imageVisual(conv4_3, "conv4_3", 32)
|
||||
imageVisual(conv4_4, "conv4_4", 32)
|
||||
imageVisual(conv5_1, "conv5_1", 32)
|
||||
imageVisual(conv5_2, "conv5_2", 32)
|
||||
imageVisual(conv5_3, "conv5_3", 32)
|
||||
imageVisual(conv5_4, "conv5_4", 32)
|
||||
# imageVisual(conv1_1, "conv1_1", 8)
|
||||
# imageVisual(conv1_2, 'conv1_2', 8)
|
||||
# imageVisual(conv2_1, "conv2_1", 16)
|
||||
# imageVisual(conv2_2, "conv2_2", 16)
|
||||
# imageVisual(conv3_1, "conv3_1", 16)
|
||||
# imageVisual(conv3_2, "conv3_2", 16)
|
||||
# imageVisual(conv3_3, "conv3_3", 16)
|
||||
# imageVisual(conv3_4, "conv3_4", 16)
|
||||
# imageVisual(conv4_1, "conv4_1", 32)
|
||||
# imageVisual(conv4_2, "conv4_2", 32)
|
||||
# imageVisual(conv4_3, "conv4_3", 32)
|
||||
# imageVisual(conv4_4, "conv4_4", 32)
|
||||
# imageVisual(conv5_1, "conv5_1", 32)
|
||||
# imageVisual(conv5_2, "conv5_2", 32)
|
||||
# imageVisual(conv5_3, "conv5_3", 32)
|
||||
# imageVisual(conv5_4, "conv5_4", 32)
|
||||
|
||||
weightVisual(vgg19.conv1_1, "conv1_1")
|
||||
weightVisual(vgg19.conv1_2, 'conv1_2')
|
||||
weightVisual(vgg19.conv2_1, "conv2_1")
|
||||
weightVisual(vgg19.conv2_2, "conv2_2")
|
||||
weightVisual(vgg19.conv3_1, "conv3_1")
|
||||
weightVisual(vgg19.conv3_2, "conv3_2")
|
||||
weightVisual(vgg19.conv3_3, "conv3_3")
|
||||
weightVisual(vgg19.conv3_4, "conv3_4")
|
||||
weightVisual(vgg19.conv4_1, "conv4_1")
|
||||
weightVisual(vgg19.conv4_2, "conv4_2")
|
||||
weightVisual(vgg19.conv4_3, "conv4_3")
|
||||
weightVisual(vgg19.conv4_4, "conv4_4")
|
||||
weightVisual(vgg19.conv5_1, "conv5_1")
|
||||
weightVisual(vgg19.conv5_2, "conv5_2")
|
||||
weightVisual(vgg19.conv5_3, "conv5_3")
|
||||
weightVisual(vgg19.conv5_4, "conv5_4")
|
||||
# endregion
|
||||
# weightVisual(vgg19.conv1_1, "conv1_1")
|
||||
# weightVisual(vgg19.conv1_2, 'conv1_2')
|
||||
# weightVisual(vgg19.conv2_1, "conv2_1")
|
||||
# weightVisual(vgg19.conv2_2, "conv2_2")
|
||||
# weightVisual(vgg19.conv3_1, "conv3_1")
|
||||
# weightVisual(vgg19.conv3_2, "conv3_2")
|
||||
# weightVisual(vgg19.conv3_3, "conv3_3")
|
||||
# weightVisual(vgg19.conv3_4, "conv3_4")
|
||||
# weightVisual(vgg19.conv4_1, "conv4_1")
|
||||
# weightVisual(vgg19.conv4_2, "conv4_2")
|
||||
# weightVisual(vgg19.conv4_3, "conv4_3")
|
||||
# weightVisual(vgg19.conv4_4, "conv4_4")
|
||||
# weightVisual(vgg19.conv5_1, "conv5_1")
|
||||
# weightVisual(vgg19.conv5_2, "conv5_2")
|
||||
# weightVisual(vgg19.conv5_3, "conv5_3")
|
||||
# weightVisual(vgg19.conv5_4, "conv5_4")
|
||||
# # endregion
|
||||
|
|
|
@ -1,17 +1,13 @@
|
|||
# from __future__ import print_function
|
||||
import os
|
||||
import torch
|
||||
import torchvision
|
||||
from torchvision import datasets, transforms
|
||||
import torchvision.models as models
|
||||
import numpy as np
|
||||
from torch.utils.data import Dataset, DataLoader
|
||||
from torch.utils.data import Dataset, DataLoader, Subset
|
||||
import random
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def MNIST(batchsize=8, num_workers=0, shuffle=False):
|
||||
CurrentPath = os.path.split(os.path.realpath(__file__))[0]+"/"
|
||||
train_loader = torch.utils.data.DataLoader(
|
||||
|
@ -46,23 +42,24 @@ def Cifar10(batchsize=8, num_workers=0, shuffle=False):
|
|||
|
||||
return train_loader, test_loader
|
||||
|
||||
def Cifar10Mono(batchsize=8, num_workers=0, shuffle=False):
|
||||
def Cifar10Mono(batchsize=8, num_workers=0, shuffle=False, trainsize=0):
|
||||
CurrentPath = os.path.split(os.path.realpath(__file__))[0]+"/"
|
||||
|
||||
dataset = datasets.CIFAR10(root=CurrentPath+'../Dataset/', train=True, download=True,
|
||||
transform=transforms.Compose([
|
||||
transforms.Grayscale(),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize((0.1307,), (0.3081,))
|
||||
]))
|
||||
if trainsize == 0:
|
||||
trainsize = dataset.data.shape[0]
|
||||
train_loader = torch.utils.data.DataLoader(
|
||||
datasets.CIFAR10(root=CurrentPath+'../Dataset/', train=True, download=True,
|
||||
transform=transforms.Compose([
|
||||
transforms.Grayscale(),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize((0.1307,), (0.3081,))
|
||||
])), batch_size=batchsize, shuffle=shuffle, num_workers=num_workers, drop_last=True)
|
||||
Subset(dataset, range(0, trainsize)), batch_size=batchsize, shuffle=shuffle, num_workers=num_workers, drop_last=True)
|
||||
test_loader = torch.utils.data.DataLoader(
|
||||
datasets.CIFAR10(root=CurrentPath+'../Dataset/', train=False, transform=transforms.Compose([
|
||||
transforms.Grayscale(),
|
||||
transforms.ToTensor(),
|
||||
transforms.Normalize((0.1307,), (0.3081,))
|
||||
])), batch_size=batchsize, shuffle=shuffle, num_workers=num_workers, drop_last=True)
|
||||
|
||||
return train_loader, test_loader
|
||||
|
||||
def RandomMnist(batchsize=8, num_workers=0, shuffle=False, style=""):
|
||||
|
@ -124,6 +121,7 @@ def RandomMnist(batchsize=8, num_workers=0, shuffle=False, style=""):
|
|||
transforms.RandomRotation(30),
|
||||
transforms.RandomResizedCrop(28),
|
||||
transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))]))
|
||||
|
||||
train_loader = torch.utils.data.DataLoader(train_data,
|
||||
batch_size=batchsize,
|
||||
shuffle=shuffle,
|
||||
|
|
|
@ -11,11 +11,13 @@ import os
|
|||
import utils as utils
|
||||
|
||||
|
||||
def train(model, train_loader, optimizer, epoch=0, deviceid=[0]):
|
||||
def train(model, train_loader, optimizer, epoch=0):
|
||||
model.train()
|
||||
batchsize = int(train_loader.sampler.num_samples /
|
||||
train_loader.batch_size / 5)+1
|
||||
for batch_idx, (data, target) in enumerate(train_loader):
|
||||
data = utils.SetDevice(data, deviceid)
|
||||
target = utils.SetDevice(target, deviceid)
|
||||
data = utils.SetDevice(data)
|
||||
target = utils.SetDevice(target)
|
||||
optimizer.zero_grad()
|
||||
|
||||
output = model(data)
|
||||
|
@ -23,7 +25,7 @@ def train(model, train_loader, optimizer, epoch=0, deviceid=[0]):
|
|||
loss.backward()
|
||||
optimizer.step()
|
||||
|
||||
if batch_idx % 100 == 0 and batch_idx > 0:
|
||||
if batch_idx % batchsize == 0 and batch_idx > 0:
|
||||
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'
|
||||
.format(epoch, batch_idx * len(data),
|
||||
len(train_loader.dataset),
|
||||
|
@ -32,14 +34,14 @@ def train(model, train_loader, optimizer, epoch=0, deviceid=[0]):
|
|||
loss.item()))
|
||||
|
||||
|
||||
def test(model, test_loader, deviceid=[0]):
|
||||
def test(model, test_loader):
|
||||
with torch.no_grad():
|
||||
model.eval()
|
||||
test_loss = 0
|
||||
correct = 0
|
||||
for data, target in test_loader:
|
||||
data = utils.SetDevice(data, deviceid)
|
||||
target = utils.SetDevice(target, deviceid)
|
||||
data = utils.SetDevice(data)
|
||||
target = utils.SetDevice(target)
|
||||
output = model(data)
|
||||
|
||||
# sum up batch loss
|
||||
|
@ -49,9 +51,7 @@ def test(model, test_loader, deviceid=[0]):
|
|||
correct += pred.eq(target.view_as(pred)).sum().item()
|
||||
|
||||
test_loss /= len(test_loader.dataset)
|
||||
accu = 100. * correct / len(test_loader.dataset)
|
||||
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'
|
||||
.format(test_loss, correct,
|
||||
len(
|
||||
test_loader.dataset),
|
||||
100. * correct / len(
|
||||
test_loader.dataset)))
|
||||
.format(test_loss, correct, len(test_loader.dataset), accu))
|
||||
return accu
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
from visdom import Visdom
|
||||
import random
|
||||
import numpy as np
|
||||
|
||||
|
||||
|
||||
def InitVisdom():
|
||||
viz = Visdom()
|
||||
assert viz.check_connection()
|
||||
viz.close()
|
||||
|
||||
class Line():
|
||||
def __init__(self,name,size):
|
||||
super(Line, self).__init__()
|
||||
self.name=name
|
||||
self.size=size
|
||||
def __init__(self):
|
||||
super(Line, self).__init__()
|
||||
self.name=str(random.random())
|
||||
self.size=0
|
||||
def __init__(self,name):
|
||||
super(Line, self).__init__()
|
||||
self.name=name
|
||||
self.size=0
|
||||
|
||||
class LineWin():
|
||||
def __init__(self):
|
||||
super(LineWin, self).__init__()
|
||||
self.viz = Visdom()
|
||||
self.name = str(random.random())
|
||||
self.win = self.viz.line(
|
||||
X=np.array([0]),
|
||||
Y=np.array([0]),
|
||||
name=self.name
|
||||
)
|
||||
self.data = np.array([])
|
||||
|
||||
def AppendLine(self,y):
|
||||
if not isinstance(y,list):
|
||||
y = [y]
|
||||
linename = str(random.random())
|
||||
self.viz.line(
|
||||
X=np.array(range(len(y))),
|
||||
Y=np.array(y),
|
||||
opts=dict(markercolor=np.array([50]),
|
||||
markersymbol='dot',),
|
||||
win=self.win,
|
||||
update="new",
|
||||
name=linename,
|
||||
)
|
||||
return Line(linename,len(y))
|
||||
|
||||
def AppendData(self,line, y):
|
||||
if not isinstance(y,list):
|
||||
y = [y]
|
||||
self.viz.line(
|
||||
X=np.array(range(line.size, line.size+len(y))),
|
||||
Y=np.array(y),
|
||||
win=self.win,
|
||||
update="append",
|
||||
name=line.name
|
||||
)
|
||||
line.size = line.size + len(y)
|
||||
|