import sys sys.path.append("D:\Github\pytorch-caffe") sys.path.append("D:\Github\pytorch-receptive-field") from caffenet import * from hessian import hessian #% Set up PerceptualLoss judger sys.path.append(r"D:\Github\PerceptualSimilarity") import models # from PerceptualSimilarity folder model_squ = models.PerceptualLoss(model='net-lin', net='squeeze', use_gpu=1, gpu_ids=[0]) model_vgg = models.PerceptualLoss(model='net-lin', net='vgg', use_gpu=1, gpu_ids=[0]) model_alex = models.PerceptualLoss(model='net-lin', net='alex', use_gpu=1, gpu_ids=[0]) #%% result_dir = r"C:\Users\ponce\OneDrive - Washington University in St. Louis\Artiphysiology\Sample_Diversity" from torch_net_utils import load_generator, load_caffenet, visualize, preprocess net_torch = load_caffenet() G_torch = load_generator() #%% sigma = 3.0 codes = sigma * np.random.randn(40, 4096) img_list = [visualize(G_torch, code, "cuda") for code in codes] #% dist_mat = np.zeros((len(codes), len(codes))) for i in range(len(codes)): for j in range(len(codes)): dist = model_squ.forward(img_list[i].unsqueeze(0).permute(0,3,1,2), img_list[j].unsqueeze(0).permute(0,3,1,2), normalize=True) dist_mat[i, j] = dist.squeeze().detach().cpu().numpy() dist_mat.mean() #%% basis = 5 * np.random.randn(1, 4096) sigma = 3.0 codes = sigma * np.random.randn(40, 4096) + basis
(img_all, img_list), dim=0) csr = csr_end return img_all def render(self, codes_all_arr, scale=1.0, B=50): img_tsr = self.visualize_batch_np(codes_all_arr, scale=scale, B=B) return [img.permute([1, 2, 0]).numpy() for img in img_tsr] #%% The first time to run this you need these modules if __name__ == "__main__": import sys import matplotlib.pylab as plt sys.path.append(r"E:\Github_Projects\Visual_Neuro_InSilico_Exp") from torch_net_utils import load_generator, visualize G = load_generator(GAN="fc6") UCG = upconvGAN("fc6") #%% def test_consisitency(G, UCG): #_ code = torch.randn((1, UCG.codelen)) # network outputs are the same. assert torch.allclose(UCG(code), G(code)['deconv0'][:, [2, 1, 0], :, :]) # visualization function is the same imgnew = UCG.visualize(code).permute([2, 3, 1, 0]).squeeze() imgorig = visualize(G, code.numpy(), mode="cpu") assert torch.allclose(imgnew, imgorig) plt.figure(figsize=[6, 3]) plt.subplot(121) plt.imshow(imgnew.detach())
def __init__(self, model): self.model = model self.G = load_generator() self.G.cuda().eval() self.weights = None
img = imread(r"D:\Github\CMAES_optimizer_matlab\fc8_02.jpg") net = CNNmodel("caffe-net") net_trc = CNNmodel_Torch("caffe-net") #%% unit = ('caffe-net', 'fc8', 5) net.select_unit(unit) net_trc.select_unit(unit) caffe_act = net.score(img[np.newaxis, ]) torch_act = net_trc.score(img[np.newaxis, ]) print("For image , unit %s " % (unit, )) print("Caffenet run in Caffe scores %.3f " % (caffe_act, )) print("Caffenet run in PyTorch scores %.3f " % (torch_act, )) #%% Test that the from utils import generator from torch_net_utils import load_generator, visualize G_torch = load_generator() # Torch generative network G_caffe = generator # Caffe GAN #%% code = 5 * np.random.randn(1, 4096) img_caffe = G_caffe.visualize(code) # uint8 255 img_torch = visualize(G_torch, code) # scale 0, 1 by default #% plt.figure() plt.subplot(121) plt.imshow(img_caffe) plt.axis("off") plt.subplot(122) plt.imshow(img_torch) plt.axis("off") plt.show()
from time import time #%% stimPath = r"N:\Stimuli\2019-Manifold\beto-191030a\backup_10_30_2019_10_15_31" allfns = os.listdir(stimPath) matfns = sorted(glob(join( stimPath, "*.mat"))) # [fn if ".mat" in fn else [] for fn in allfns] imgfns = sorted(glob(join( stimPath, "*.jpg"))) # [fn if ".mat" in fn else [] for fn in allfns] #%% Load the Codes mat file data = loadmat(matfns[1]) codes = data['codes'] img_id = [arr[0] for arr in list(data['ids'][0])] # list of ids #%% from torch_net_utils import load_caffenet, load_generator, visualize # net = load_caffenet() Generator = load_generator() #%% rspPath = r"D:\Network_Data_Sync\Data-Ephys-MAT" #r"N:\Data-Ephys-MAT"# EphsFN = "Beto64chan-30102019-001" # "Beto64chan-11112019-006" # Rspfns = sorted(glob(join(rspPath, EphsFN + "*"))) rspData = h5py.File(Rspfns[1]) spikeID = rspData['meta']['spikeID'] rsp = rspData['rasters'] # Extremely useful code snippet to solve the problem imgnms_refs = np.array(rspData['Trials']['imageName']).flatten() imgnms = np.array( [''.join(chr(i) for i in rspData[ref]) for ref in imgnms_refs]) #%% prefchan_idx = np.nonzero(spikeID[0, :] == 26)[0] - 1 prefrsp = rsp[:, :, prefchan_idx] # Dataset reading takes time
def __init__(self, unit=None): self.net = load_caffenet() self.G = load_generator() self.unit = unit
def __init__(self): self.net = load_caffenet() self.G = load_generator()
from os.path import join from sys import platform #%% Decide the result storage place based on the computer the code is running if platform == "linux": # cluster recorddir = "/scratch/binxu/CNN_data/" else: if os.environ['COMPUTERNAME'] == 'DESKTOP-9DDE2RH': # PonceLab-Desktop 3 recorddir = r"D:\Generator_DB_Windows\data\with_CNN" initcodedir = r"D:\Generator_DB_Windows\stimuli\texture006" # Code & image folder to initialize the Genetic Algorithm elif os.environ['COMPUTERNAME'] == 'DESKTOP-MENSD6S': ## Home_WorkStation recorddir = r"E:\Monkey_Data\Generator_DB_Windows\data\with_CNN" # Basic properties for Optimizer. #%% import torch from torch_net_utils import load_generator G = load_generator("fc7") G.requires_grad_(False) G.cuda() #%% def visualize(G, code, mode="cuda"): """Do the De-caffe transform (Validated) works for a single code """ if mode == "cpu": blobs = G(code) else: blobs = G(code.cuda()) out_img = blobs['deconv0'] # get raw output image from GAN if mode == "cpu": clamp_out_img = torch.clamp(out_img + BGR_mean, 0, 255)
""" cluster deploy version! To generate pairs of real image and degenerated image. See """ import torch from torch_net_utils import load_generator from skimage.transform import resize, rescale from imageio import imread, imsave import matplotlib.pylab as plt import numpy as np from os.path import join import os from time import time G = load_generator("fc6") G.requires_grad_(False) G.cuda() BGR_mean = torch.tensor([104.0, 117.0, 123.0]) BGR_mean = torch.reshape(BGR_mean, (1, 3, 1, 1)).cuda() #%% import sys sys.path.append(r"D:\Github\PerceptualSimilarity") sys.path.append(r"E:\Github_Projects\PerceptualSimilarity") import models # from PerceptualSimilarity folder # model = models.PerceptualLoss(model='net-lin', net='squeeze', use_gpu=1, gpu_ids=[0]) # percept_vgg = models.PerceptualLoss(model='net-lin', net='vgg', use_gpu=1, gpu_ids=[0]) # target_img = imread(r"E:\Monkey_Data\Generator_DB_Windows\nets\upconv\Cat.jpg") #%% def visualize(G, code, mode="cuda", percept_loss=True):