from __future__ import print_function # from vgg19Pytorch import Vgg19Module import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim import torchvision from torchvision import datasets, transforms import torchvision.models as models import matplotlib.pyplot as plt import numpy as np from visdom import Visdom import cv2 import os import shutil viz = Visdom() # use visdom , we must start visdom Host first # with python -m visdom.server CurrentPath = os.path.split(os.path.realpath(__file__))[0]+"/" print("Current Path :" + CurrentPath) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # vgg19 = Vgg19Module(CurrentPath+'/vgg19Pytorch.npy') vgg19 = torchvision.models.vgg19(True) if torch.cuda.is_available(): vgg19 = vgg19.to(device) def weightVisual(layer, name=''): mm4 = layer.weight.data datodis = mm4 * 256 + 128 dashape = datodis.shape datodis = datodis.view(dashape[1] * dashape[0], 1, dashape[2], dashape[3]) imaglisnp = datodis.detach().cpu().numpy() viz.images(imaglisnp, opts=dict( title=name, caption='How random.'), nrow=dashape[1]) def imageVisual(data, name='', rownum=8): datodis = data #datodis = datodis * 256 + 128 dashape = datodis.shape datodis = datodis.view(dashape[1] * dashape[0], 1, dashape[2], dashape[3]) imaglisnp = datodis.detach().cpu().numpy() viz.images(imaglisnp, opts=dict( title=name, caption='How random.'), nrow=rownum) def readAndPreprocessImage(imagepath): mean = np.array([103.939, 116.779, 123.68]) img = cv2.imread(imagepath) img = img.astype('float32') img -= mean img = cv2.resize(img, (224, 224)).transpose((2, 0, 1)) img = img[np.newaxis, :, :, :] # cv image read is by BGR order # and the network Need RGB # the following BGR values should be subtracted: [103.939, 116.779, 123.68]. image = torch.from_numpy(img) if torch.cuda.is_available(): image = image.cuda() return image #region forward image image = readAndPreprocessImage(CurrentPath+"dog_resize.jpg") imgfor = vgg19.forward(image) imgfornumpy = imgfor.cpu().detach().numpy() words = open(CurrentPath+'synset_words.txt').readlines() words = [(w[0], ' '.join(w[1:])) for w in [w.split() for w in words]] words = np.asarray(words) top5 = np.argsort(imgfornumpy)[0][::-1][:5] probs = np.sort(imgfornumpy)[0][::-1][:5] 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) # 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