def testScattering(self): data = torch.load('test/test_data.pt') x = data['x'] S = data['S'] scat = Scattering(128, 128, 4, pre_pad=False, jit=True) scat.cuda() x = x.cuda() S = S.cuda() self.assertLess(((S - scat(x))).abs().max(), 1e-6) scat = Scattering(128, 128, 4, pre_pad=False, jit=False) Sg = [] Sc = [] for gpu in [True, False]: if gpu: x = x.cuda() scat.cuda() Sg = scat(x) else: x = x.cpu() scat.cpu() Sc = scat(x) """there are huge round off errors with fftw, numpy fft, cufft... and the kernels of periodization. We do not wish to play with that as it is meaningless.""" self.assertLess((Sg.cpu() - Sc).abs().max(), 1e-1)
def testScattering(self): data = torch.load('test/test_data.pt') x = data['x'] S = data['S'] scat = Scattering(128, 128, 4, pre_pad=False,jit=True) scat.cuda() x = x.cuda() S = S.cuda() self.assertLess(((S - scat(x))).abs().max(), 1e-6) scat = Scattering(128, 128, 4, pre_pad=False, jit=False) Sg = [] Sc = [] for gpu in [True, False]: if gpu: x = x.cuda() scat.cuda() Sg = scat(x) else: x = x.cpu() scat.cpu() Sc = scat(x) """there are huge round off errors with fftw, numpy fft, cufft... and the kernels of periodization. We do not wish to play with that as it is meaningless.""" self.assertLess((Sg.cpu()-Sc).abs().max(), 1e-1)
def scat_data(data_dir, outdata_dir, M, N, J): from scatwave.scattering import Scattering filename_list = os.listdir(data_dir) #read the directory files's name filename_list.sort() np.save('./test_scatname_list.npy', np.array(filename_list)) number = len(filename_list) scat = Scattering(M=M, N=N, J=J).cuda() # scattering transform for i in range(0, number): imgName = os.path.join(data_dir, os.path.basename(filename_list[i])) if (os.path.splitext(imgName)[1] != '.png'): continue img = np.array(Image.open(imgName)) / 127.5 - 1 #与x保持一致,归一化到[-1,1]之间 # img = np.array(Image.open(imgName)) # print (np.max(img),np.min(img)) img = img.reshape(1, M, N, 3) #1*128*128*3 img_data = img.transpose(0, 3, 1, 2).astype(np.float32) # 1*3*128*128 img_data = torch.from_numpy(img_data).cuda() out_data = np.array(scat(img_data)) #1*3*417*8*8 # print (np.max(out_data),np.min(out_data)) # print out_data.shape str1 = filename_list[i].split('.') print(str1[0]) # print (outdata_dir + str1[0] + '.npy') np.save(outdata_dir + str1[0] + '.npy', out_data) if i % 100 == 0: print("step is %d", i) return 0
def __init__(self, num_classes=10): super(Scat2Only, self).__init__() self.J1 = 2 self.N1 = 32 self.scat1 = Scattering(M=32, N=32, J=self.J1).cuda() self.nfscat1 = (1 + 8 * self.J1 + 8 * 8 * self.J1 * (self.J1 - 1) / 2) self.nspace1 = self.N1 / (2**self.J1) self.J2 = 2 self.N2 = 8 self.scat2 = Scattering(M=8, N=8, J=self.J2).cuda() self.nfscat2 = (1 + 8 * self.J2 + 8 * 8 * self.J2 * (self.J2 - 1) / 2) self.nspace2 = self.N2 / (2**self.J2) self.features = nn.Sequential( nn.MaxPool2d(kernel_size=2, stride=2), nn.MaxPool2d(kernel_size=2, stride=2), nn.MaxPool2d(kernel_size=2, stride=2), ) self.classifier = nn.Linear(243, num_classes)
def get_scatter_features(data_loader, dataset_name, num_layers=2): scatter_features = [] targets = [] avg_pool = nn.AvgPool3d(2, stride=2) # for cifar-10 for batch_idx, (data, target) in enumerate(tqdm(data_loader)): # Scattering params # M, N: input image size # J: number of layers data = data.cuda() scat = Scattering(M=data.size(data.dim() - 2), N=data.size(data.dim() - 1), J=num_layers).cuda() out = scat(data) out = out.squeeze() for i in range(len(out)): feat = out[i] scatter_features.append(feat.cpu().numpy()) for i in range(len(target)): targets.append(target[i].cpu().numpy()) scatter_features = np.array(scatter_features) targets = np.array(targets) print("raw scatter features dim:", scatter_features.shape) if dataset_name == "cifar-10": print("cifar-10 avg pooling") scatter_features = avg_pool( torch.cuda.FloatTensor(scatter_features)).cpu().numpy() print("after pooling:", scatter_features.shape) total_sample = scatter_features.shape[0] scatter_features = scatter_features.reshape(total_sample, -1) print("scatter features dim:", scatter_features.shape, "target features dim:", targets.shape) return scatter_features, targets
def __init__(self, num_classes=10): super(ScatFirst, self).__init__() self.J = 2 self.N = 32 self.scat = Scattering(M=32, N=32, J=self.J, L=4).cuda() self.nfscat = (1 + 8 * self.J + 8 * 8 * self.J * (self.J - 1) / 2) self.nspace = self.N / (2**self.J) self.features = nn.Sequential( nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(self.nfscat * 3, 192, kernel_size=5, padding=2), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), nn.Conv2d(192, 384, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(384, 256, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.Conv2d(256, 256, kernel_size=3, padding=1), nn.ReLU(inplace=True), nn.MaxPool2d(kernel_size=2, stride=2), ) self.classifier = nn.Linear(256, num_classes)
def main(): """Train a simple Hybrid Scattering + CNN model on MNIST. Scattering features are normalized by batch normalization. The model achieves 99.6% testing accuracy after 10 epochs. """ meter_loss = tnt.meter.AverageValueMeter() classerr = tnt.meter.ClassErrorMeter(accuracy=True) scat = Scattering(M=28, N=28, J=2).cuda() K = 81 params = { 'conv1.weight': conv_init(K, 64, 1), 'conv1.bias': torch.zeros(64), 'bn.weight': torch.Tensor(K).uniform_(), 'bn.bias': torch.zeros(K), 'linear2.weight': linear_init(64*7*7, 512), 'linear2.bias': torch.zeros(512), 'linear3.weight': linear_init(512, 10), 'linear3.bias': torch.zeros(10), } stats = {'bn.running_mean': torch.zeros(K).cuda(), 'bn.running_var': torch.ones(K).cuda()} for k, v in params.items(): params[k] = Variable(v.cuda(), requires_grad=True) def h(sample): x = scat(sample[0].float().cuda().unsqueeze(1) / 255.0).squeeze(1) inputs = Variable(x) targets = Variable(torch.LongTensor(sample[1]).cuda()) o = f(inputs, params, stats, sample[2]) return F.cross_entropy(o, targets), o def on_sample(state): state['sample'].append(state['train']) def on_forward(state): classerr.add(state['output'].data, torch.LongTensor(state['sample'][1])) meter_loss.add(state['loss'].data[0]) def on_start_epoch(state): classerr.reset() state['iterator'] = tqdm(state['iterator']) def on_end_epoch(state): print 'Training accuracy:', classerr.value() def on_end(state): print 'Training' if state['train'] else 'Testing', 'accuracy' print classerr.value() optimizer = torch.optim.SGD(params.values(), lr=0.01, momentum=0.9, weight_decay=0.0005) engine = Engine() engine.hooks['on_sample'] = on_sample engine.hooks['on_forward'] = on_forward engine.hooks['on_start_epoch'] = on_start_epoch engine.hooks['on_end_epoch'] = on_end_epoch engine.hooks['on_end'] = on_end print 'Training:' engine.train(h, get_iterator(True), 10, optimizer) print 'Testing:' engine.test(h, get_iterator(False))
def main(): if not os.path.exists(opt.save): os.mkdir(opt.save) if opt.scat > 0: model, params, stats = models.__dict__[opt.model](N=opt.N, J=opt.scat) else: model, params, stats = models.__dict__[opt.model]() def create_optimizer(opt, lr): print('creating optimizer with lr = %f' % lr) return torch.optim.SGD(params.values(), lr, opt.momentum, weight_decay=opt.weightDecay) def get_iterator(mode): ds = create_dataset(opt, mode) return ds.parallel(batch_size=opt.batchSize, shuffle=mode, num_workers=opt.nthread, pin_memory=False) optimizer = create_optimizer(opt, opt.lr) iter_test = get_iterator(False) iter_train = get_iterator(True) if opt.scat > 0: scat = Scattering(M=opt.N, N=opt.N, J=opt.scat, pre_pad=False).cuda() epoch = 0 if opt.resume != '': resumeFile = opt.resume if not resumeFile.endswith('pt7'): resumeFile = torch.load(opt.resume + '/latest.pt7')['latest_file'] state_dict = torch.load(resumeFile) epoch = state_dict['epoch'] params_tensors, stats = state_dict['params'], state_dict['stats'] for k, v in params.iteritems(): v.data.copy_(params_tensors[k]) optimizer.load_state_dict(state_dict['optimizer']) print('model was restored from epoch:', epoch) print('\nParameters:') print( pd.DataFrame([(key, v.size(), torch.typename(v.data)) for key, v in params.items()])) print('\nAdditional buffers:') print( pd.DataFrame([(key, v.size(), torch.typename(v)) for key, v in stats.items()])) n_parameters = sum( [p.numel() for p in list(params.values()) + list(stats.values())]) print('\nTotal number of parameters: %f' % n_parameters) meter_loss = meter.AverageValueMeter() classacc = meter.ClassErrorMeter(topk=[1, 5], accuracy=False) timer_data = meter.TimeMeter('s') timer_sample = meter.TimeMeter('s') timer_train = meter.TimeMeter('s') timer_test = meter.TimeMeter('s') def h(sample): inputs = sample[0].cuda() if opt.scat > 0: inputs = scat(inputs) inputs = Variable(inputs) targets = Variable(sample[1].cuda().long()) if sample[2]: model.train() else: model.eval() y = torch.nn.parallel.data_parallel(model, inputs, np.arange(opt.ngpu).tolist()) return F.cross_entropy(y, targets), y def log(t, state): if (t['epoch'] > 0 and t['epoch'] % opt.frequency_save == 0): torch.save( dict(params={k: v.data.cpu() for k, v in params.iteritems()}, stats=stats, optimizer=state['optimizer'].state_dict(), epoch=t['epoch']), open(os.path.join(opt.save, 'epoch_%i_model.pt7' % t['epoch']), 'w')) torch.save( dict( latest_file=os.path.join(opt.save, 'epoch_%i_model.pt7' % t['epoch'])), open(os.path.join(opt.save, 'latest.pt7'), 'w')) z = vars(opt).copy() z.update(t) logname = os.path.join(opt.save, 'log.txt') with open(logname, 'a') as f: f.write('json_stats: ' + json.dumps(z) + '\n') print(z) def on_sample(state): global data_time data_time = timer_data.value() timer_sample.reset() state['sample'].append(state['train']) def on_forward(state): prev_sum5 = classacc.sum[5] prev_sum1 = classacc.sum[1] classacc.add(state['output'].data, torch.LongTensor(state['sample'][1])) meter_loss.add(state['loss'].data[0]) next_sum5 = classacc.sum[5] next_sum1 = classacc.sum[1] n = state['output'].data.size(0) curr_top5 = 100.0 * (next_sum5 - prev_sum5) / n curr_top1 = 100.0 * (next_sum1 - prev_sum1) / n sample_time = timer_sample.value() timer_data.reset() if (state['train']): txt = 'Train:' else: txt = 'Test' if (state['t'] % opt.frequency_print == 0 and state['t'] > 0): print( '%s [%i,%i/%i] ; loss: %.3f (%.3f) ; acc5: %.2f (%.2f) ; acc1: %.2f (%.2f) ; data %.3f ; time %.3f' % (txt, state['epoch'], state['t'] % len(state['iterator']), len(state['iterator']), state['loss'].data[0], meter_loss.value()[0], curr_top5, classacc.value(5), curr_top1, classacc.value(1), data_time, sample_time)) def on_start(state): state['epoch'] = epoch def on_start_epoch(state): classacc.reset() meter_loss.reset() timer_train.reset() state['iterator'] = iter_train epoch = state['epoch'] + 1 if epoch in epoch_step: print('changing LR') lr = state['optimizer'].param_groups[0]['lr'] state['optimizer'] = create_optimizer(opt, lr * opt.lr_decay_ratio) def on_end_epoch(state): if (state['t'] % opt.frequency_test == 0 and state['t'] > 0): train_loss = meter_loss.value() train_acc = classacc.value() train_time = timer_train.value() meter_loss.reset() classacc.reset() timer_test.reset() engine.test(h, iter_test) log( { "train_loss": train_loss[0], "train_acc": 100 - train_acc[0], "test_loss": meter_loss.value()[0], "test_acc": 100 - classacc.value()[0], "epoch": state['epoch'], "n_parameters": n_parameters, "train_time": train_time, "test_time": timer_test.value(), }, state) engine = Engine() engine.hooks['on_sample'] = on_sample engine.hooks['on_forward'] = on_forward engine.hooks['on_start_epoch'] = on_start_epoch engine.hooks['on_end_epoch'] = on_end_epoch engine.hooks['on_start'] = on_start engine.train(h, iter_train, opt.epochs, optimizer)
from scatwave.scattering import Scattering from dataloader import DataLoader from converter import send_crops_of_this_dispute_painting import matplotlib.pyplot as plt import pickle os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" os.environ["CUDA_VISIBLE_DEVICES"]="0" # params iters = 300 batch_size = 10 shapeofoutput = 245196 features = np.zeros((iters * batch_size, shapeofoutput), dtype=np.float32) labels = np.zeros(iters * batch_size, dtype=np.uint8) scat = Scattering(M=224, N=224, J=4).cuda() loader = DataLoader() pca2 = PCA(n_components = 2) # scatter feature for i in range(iters): print 'Iter: %s/%s' % (i, iters) batch, label_batch = loader.get_batch(10, 'train', 'rgb') for j in range(batch.shape[0]): x = batch[j][np.newaxis,:] label = label_batch[j] x = x.transpose(0,3,1,2) x = torch.from_numpy(x).float().cuda() #numpy2tensor output = scat(x) output = output.view(-1) output = output.cpu().numpy()
def main(): model, params, stats = models.__dict__[opt.model](N=opt.N, J=opt.scat) iter_test = get_iterator(False, opt) scat = Scattering(M=opt.N, N=opt.N, J=opt.scat, pre_pad=False).cuda() epoch = 0 if opt.resume != '': resumeFile = opt.resume if not resumeFile.endswith('pt7'): resumeFile = torch.load(opt.resume + '/latest.pt7')['latest_file'] state_dict = torch.load(resumeFile) model.load_state_dict(state_dict['state_dict']) print('model was restored from epoch:', epoch) print('\nParameters:') print( pd.DataFrame([(key, v.size(), torch.typename(v.data)) for key, v in params.items()])) print('\nAdditional buffers:') print( pd.DataFrame([(key, v.size(), torch.typename(v)) for key, v in stats.items()])) n_parameters = sum( [p.numel() for p in list(params.values()) + list(stats.values())]) print('\nTotal number of parameters: %f' % n_parameters) meter_loss = meter.AverageValueMeter() classacc = meter.ClassErrorMeter(topk=[1, 5], accuracy=False) timer_data = meter.TimeMeter('s') timer_sample = meter.TimeMeter('s') timer_train = meter.TimeMeter('s') timer_test = meter.TimeMeter('s') def h(sample): inputs = sample[0].cuda() if opt.scat > 0: inputs = scat(inputs) inputs = Variable(inputs) targets = Variable(sample[1].cuda().long()) if sample[2]: model.train() else: model.eval() # y = model.forward(inputs) y = torch.nn.parallel.data_parallel(model, inputs, np.arange(opt.ngpu).tolist()) return F.cross_entropy(y, targets), y def on_sample(state): global data_time data_time = timer_data.value() timer_sample.reset() state['sample'].append(state['train']) def on_forward(state): prev_sum5 = classacc.sum[5] prev_sum1 = classacc.sum[1] classacc.add(state['output'].data, torch.LongTensor(state['sample'][1])) meter_loss.add(state['loss'].data[0]) next_sum5 = classacc.sum[5] next_sum1 = classacc.sum[1] n = state['output'].data.size(0) curr_top5 = 100.0 * (next_sum5 - prev_sum5) / n curr_top1 = 100.0 * (next_sum1 - prev_sum1) / n sample_time = timer_sample.value() timer_data.reset() if (state['train']): txt = 'Train:' else: txt = 'Test' print( '%s [%i,%i/%i] ; loss: %.3f (%.3f) ; err5: %.2f (%.2f) ; err1: %.2f (%.2f) ; data %.3f ; time %.3f' % (txt, state['epoch'], state['t'] % len(state['iterator']), len(state['iterator']), state['loss'].data[0], meter_loss.value()[0], curr_top5, classacc.value(5), curr_top1, classacc.value(1), data_time, sample_time)) def on_start(state): state['epoch'] = epoch def on_start_epoch(state): classacc.reset() meter_loss.reset() timer_train.reset() epoch = state['epoch'] + 1 def on_end_epoch(state): train_loss = meter_loss.value() train_acc = classacc.value() train_time = timer_train.value() meter_loss.reset() classacc.reset() timer_test.reset() engine.test(h, iter_test) engine = Engine() engine.hooks['on_sample'] = on_sample engine.hooks['on_forward'] = on_forward engine.hooks['on_start_epoch'] = on_start_epoch engine.hooks['on_end_epoch'] = on_end_epoch engine.hooks['on_start'] = on_start engine.test(h, iter_test) print(classacc.value())
def __init__(self, size=(224, 224), layer=4): """initialize a scatNet instance (slow)""" self.scat = Scattering(M=size[0], N=size[1], J=layer).cuda()