def forwordgradients(img_list, batch_posimgids, batch_possamples, GBP, vector): uniqueimgid = np.unique(batch_posimgids) theuniI = 0 for theuniqid in uniqueimgid: theuniexps = [] for ind in range(batch_possamples.shape[0]): if batch_posimgids[ind] == theuniqid: theuniexps.append(batch_possamples[ind]) theuniexps = torch.from_numpy(np.stack(theuniexps, 0)).view(-1, 4).numpy() theimg = Image.open(img_list[theuniqid]).convert('RGB') extractor = RegionExtractor(theimg, theuniexps, opts) for i, regions in enumerate(extractor): regions = Variable(regions) if opts['use_gpu']: regions = regions.cuda() if i == 0: feats = regions else: feats = torch.cat((feats, regions), 0) if theuniI == 0: posregions = feats else: posregions = torch.cat((posregions, feats), 0) theuniI = theuniI + 1 guided_grads = GBP.generate_gradients(posregions, vector) return guided_grads
def forward_samples(model, image, samples, out_layer='conv3'): model.eval() extractor = RegionExtractor(image, samples, opts) for i, regions in enumerate(extractor): if opts['use_gpu']: regions = regions.cuda() with torch.no_grad(): feat = model(regions, out_layer=out_layer) if i == 0: feats = feat.detach().clone() else: feats = torch.cat((feats, feat.detach().clone()), 0) return feats
def forward_samples(model, image, samples, out_layer='conv3'): assert len(samples) > 0 model.eval() extractor = RegionExtractor(image, samples, opts['img_size'], opts['padding'], opts['batch_test']) for i, regions in enumerate(extractor): regions = Variable(regions) if opts['use_gpu']: regions = regions.cuda() feat = model(regions, out_layer=out_layer) if i == 0: feats = feat.data.clone() else: feats = torch.cat((feats, feat.data.clone()), 0) return feats