def process(args): valset, gpu_index, batch_size = args os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu_index) # set iterators for training and validation val_batch_fetcher = iterator_dr.ValidationBatchFetcher(valset, batch_size) # load networks EX_segmentor_dir = "../model/EX_segmentor" HE_segmentor_dir = "../model/HE_segmentor" MA_segmentor_dir = "../model/MA_segmentor" SE_segmentor_dir = "../model/SE_segmentor" EX_segmentor = utils.load_network(EX_segmentor_dir) HE_segmentor = utils.load_network(HE_segmentor_dir) MA_segmentor = utils.load_network(MA_segmentor_dir) SE_segmentor = utils.load_network(SE_segmentor_dir) EX_bottleneck_extractor = model.bottleneck_extractor( EX_segmentor, "activation_14", 0) HE_bottleneck_extractor = model.bottleneck_extractor( HE_segmentor, "activation_14", 0) MA_bottleneck_extractor = model.bottleneck_extractor( MA_segmentor, "activation_6", 4) SE_bottleneck_extractor = model.bottleneck_extractor( SE_segmentor, "activation_10", 2) for fnames, fundus_rescale, fundus_rescale_mean_subtract, _ in val_batch_fetcher( ): ex_bottleneck = EX_bottleneck_extractor.predict(fundus_rescale, batch_size=batch_size, verbose=0) he_bottleneck = HE_bottleneck_extractor.predict( fundus_rescale_mean_subtract, batch_size=batch_size, verbose=0) ma_bottleneck = MA_bottleneck_extractor.predict( fundus_rescale_mean_subtract, batch_size=batch_size, verbose=0) se_bottleneck = SE_bottleneck_extractor.predict(fundus_rescale, batch_size=batch_size, verbose=0) for index in range(ex_bottleneck.shape[0]): np.save( os.path.join(out_dirs["EX"], os.path.basename(fnames[index])), ex_bottleneck[index, ...]) np.save( os.path.join(out_dirs["HE"], os.path.basename(fnames[index])), he_bottleneck[index, ...]) np.save( os.path.join(out_dirs["MA"], os.path.basename(fnames[index])), ma_bottleneck[index, ...]) np.save( os.path.join(out_dirs["SE"], os.path.basename(fnames[index])), se_bottleneck[index, ...])
def __init__(self): # load images and scale in range (-1, 1) self.images_loader = utils.load_images('datasets/patterns/') # dataset = datasets.CIFAR10(root='./cifar10', train=True, download=True, transform=transform) # dataloader = DataLoader(dataset, batch_size=opt.batch_size, shuffle=True, num_workers=20, drop_last=True) # load networks if args.architecture == 'ResNet': self.encoder = utils.load_network('encoder', waae_resnet.Encoder()) self.decoder = utils.load_network('decoder', waae_resnet.Decoder()) self.discriminator = utils.load_network( 'discriminator', waae_resnet.Discriminator()) # print total and trainable parameters for networks utils.print_parameters(self.encoder, 'Encoder') utils.print_parameters(self.decoder, 'Decoder') utils.print_parameters(self.discriminator, 'Discriminator') self.reconstruct_loss = torch.nn.MSELoss().cuda( ) if cuda else torch.nn.MSELoss() # set up optimizers self.optimizer_R = torch.optim.Adam(itertools.chain( self.encoder.parameters(), self.decoder.parameters()), lr=args.lr_R, betas=(args.b1, args.b2)) self.optimizer_D = torch.optim.Adam(self.discriminator.parameters(), lr=args.lr_D, betas=(args.b1, args.b2)) #self.optimizer_D = torch.optim.SGD(self.discriminator.parameters(), lr=args.lr_D, momentum=0.9, dampening=0, weight_decay=1e-4) self.optimizer_G = torch.optim.Adam(self.encoder.parameters(), lr=args.lr_G, betas=(args.b1, args.b2)) # set up LR schedulers self.scheduler_R = StepLR(self.optimizer_R, step_size=args.lr_step, gamma=args.lr_gamma) self.scheduler_D = StepLR(self.optimizer_D, step_size=args.lr_step, gamma=args.lr_gamma) self.scheduler_G = StepLR(self.optimizer_G, step_size=args.lr_step, gamma=args.lr_gamma) # create batch of latent vectors to visualize the progression of the generator self.fixed_noise = Variable( Tensor(np.random.normal(0, args.s_sd, (100, args.latent_dim))))
def main(): log.basicConfig(format = '[ %(levelname)s ] %(message)s', level = log.INFO, stream = sys.stdout) args = build_argparser().parse_args() try: model_wrapper = openvino_io_model_wrapper() data_transformer = transformer() io = io_adapter.get_io_adapter(args, model_wrapper, data_transformer) iecore = utils.create_ie_core(args.extension, args.cldnn_config, args.device, args.nthreads, None, args.dump, 'sync', log) net = utils.create_network(iecore, args.model_xml, args.model_bin, log) input_shapes = utils.get_input_shape(model_wrapper, net) for layer in input_shapes: log.info('Shape for input layer {0}: {1}'.format(layer, input_shapes[layer])) utils.reshape_input(net, args.batch_size) log.info('Prepare input data') io.prepare_input(net, args.input) log.info('Create executable network') exec_net = utils.load_network(iecore, net, args.device, args.priority, 1) log.info('Starting inference ({} iterations) on {}'. format(args.number_iter, args.device)) result, time = infer_sync(exec_net, args.number_iter, io.get_slice_input) average_time, latency, fps = process_result(time, args.batch_size, args.mininfer) if not args.raw_output: io.process_output(result, log) result_output(average_time, fps, latency, log) else: raw_result_output(average_time, fps, latency) del net del exec_net del iecore except Exception as ex: print('ERROR! : {0}'.format(str(ex))) sys.exit(1)
def occlusion_heatmap(net, image, level=4, square_length=7): """ Determines which parts of the image are important for the network classification Blackens square regions in the image and measures target class probability See this paper: http://arxiv.org/abs/1311.2901 :param net: a path to a pickeled network or a (net, output) tuple :param image: an image to test, this function assumes the images has been preprocessed in the same manner as during net training, otherwise the results are unpredictable :param level: measure agains this class (currently only class 4 is supported) :param square_length: size of the square which is blacked out in the image :return: an array the same size as image with target class probabilities """ if isinstance(net, str): net, output = load_network(net) else: net, output = net img = image.copy() shape = img.shape # assume the first layer is InputLayer net_shape = net[0].get_output_shape() batch_size = net_shape[0] assert shape == net_shape[1:] # allocate network input variable x_shared = theano.shared(np.zeros(net_shape, dtype=theano.config.floatX), borrow=True) # allocate array for the heatmap heat_array = np.zeros(shape[1:]) pad = square_length // 2 # this will contain images with occluded regions x_occluded = np.zeros( (shape[1] * shape[2], net_shape[1], shape[1], shape[2]), dtype=img.dtype) # occlude image regions for i, j in product(*list(map(range, shape[1:]))): x_padded = np.pad(img, ((0, 0), (pad, pad), (pad, pad)), 'constant') x_padded[:, i:i + square_length, j:j + square_length] = 0. x_occluded[i * shape[1] + j, ...] = x_padded[:, pad:-pad, pad:-pad] predict_proba = theano.function( [], output.get_output(x_shared, deterministic=True)[:, level - 1], ) n_occluded = len(x_occluded) probas = np.zeros(n_occluded, dtype=theano.config.floatX) # get the probabilities for occluded images for b in range(n_occluded / batch_size): batch_slice = slice(b * batch_size, (b + 1) * batch_size) x_shared.set_value(x_occluded[batch_slice], borrow=True) probas[batch_slice] = predict_proba() #assign probabilities to heat_map for i, j in product(*list(map(range, shape[1:]))): heat_array[i, j] = probas[i * shape[1] + j] return heat_array
def __init__(self): # load images and scale in range (-1, 1) self.images_loader = utils.load_images('datasets/patterns/') # dataset = datasets.CIFAR10(root='./cifar10', train=True, download=True, transform=transform) # dataloader = DataLoader(dataset, batch_size=opt.batch_size, shuffle=True, num_workers=20, drop_last=True) # Define the network self.generator = utils.load_network('generator', dcgan.Generator()) self.discriminator = utils.load_network('discriminator', dcgan.Discriminator()) # print total and trainable parameters for networks utils.print_parameters(self.generator, 'Generator') utils.print_parameters(self.discriminator, 'Discriminator') self.adversarial_loss = torch.nn.BCELoss().cuda( ) if cuda else torch.nn.BCELoss() # set up optimisers self.optimizer_D = torch.optim.RMSprop(self.discriminator.parameters(), lr=args.lr_d) self.optimizer_G = torch.optim.RMSprop(self.generator.parameters(), lr=args.lr_g) # set up LR schedulers self.scheduler_D = StepLR(self.optimizer_D, step_size=args.lr_step, gamma=args.lr_gamma) self.scheduler_G = StepLR(self.optimizer_G, step_size=args.lr_step, gamma=args.lr_gamma) # create latent vectors to visualize the progression of the generator self.fixed_noise = Variable( Tensor( np.random.normal(0, args.s_sd, (args.batch_size, args.latent_dim))))
def gen_user_ked(unfilename, retfilename): log("Loading user network") g = load_network(unfilename) uid2ked = {} max_nbrksum = 0 log("Computing ked") for ni in g.Nodes(): # nid = ni.GetId() k = ni.GetInDeg() # K nbrks = [] for i in range(k): nbrnid = ni.GetInNId(i) nbrni = g.GetNI(nbrnid) nbrk = nbrni.GetInDeg() nbrks.append(nbrk) nbrksum = sum(nbrks) max_nbrksum = max(max_nbrksum, nbrksum) for ni in g.Nodes(): nid = ni.GetId() k = ni.GetInDeg() # K nbrks = [] for i in range(k): nbrnid = ni.GetInNId(i) nbrni = g.GetNI(nbrnid) nbrk = nbrni.GetInDeg() if nbrk > 0: nbrks.append(nbrk) nbrksum = sum(nbrks) if k > 1 and nbrksum > 0: entropy = 0.0 for nbrk in nbrks: p = nbrk / float(nbrksum) entropy += -p * math.log(p) H = entropy / math.log(k) # E D = math.e ** (nbrksum / float(max_nbrksum)) # D uid2ked[nid] = k * H * D else: uid2ked[nid] = 0.0 with open(retfilename, "w") as retfile: for uid, ked in uid2ked.iteritems(): retfile.write("%d %.8f\n" % (uid, ked))
def gen_user_hits(unfilename, hubfilename, authfilename): log('Loading user network') g = load_network(unfilename) log('Computing hits') nid_hub_h = snap.TIntFltH() nid_auth_h = snap.TIntFltH() snap.GetHits(g, nid_hub_h, nid_auth_h) log('Saving results') with open(hubfilename, 'w') as hubFile: for nId in nid_hub_h: hubFile.write('%d %.8f\n' % (nId, nid_hub_h[nId])) with open(authfilename, 'w') as authFile: for nId in nid_auth_h: authFile.write('%d %.8f\n' % (nId, nid_auth_h[nId]))
def gen_user_con_com(unfilename, retfilename): log('Loading user network') g = load_network(unfilename) print g.GetNodes(), g.GetEdges() # log('Removing single nodes') # for ni in g.Nodes(): # nodes_to_remove = [] # if ni.GetDeg() == 0: # nodes_to_remove.append(ni.GetId()) # for nid in nodes_to_remove: # g.DelNode(nid) # print g.GetNodes(), g.GetEdges() log('Computing components') ccs = snap.TCnComV() snap.GetSccs(g, ccs) log('Saving results') with open(retfilename, 'w') as retfile: for i, cc in enumerate(ccs): for nid in cc: retfile.write('%d %d\n' % (nid, i))
import caffe import numpy as np import os from sklearn import preprocessing import utils if __name__ == '__main__': # ------------------------ Script Parameters --------------------- batch_size = 10 feature_layers = utils.feature_layers # feature_layers = ['fc7'] # ------------------------ Script Parameters --------------------- net, params, blobs = utils.load_network() image_files = os.listdir(utils.img_dir) N = len(image_files) print 'Total Files : ', N print 'Sample File Name : ', image_files[100] for layer in feature_layers: print 'Processing Layer : ' + layer file = image_files[0] f0 = caffe.io.load_image(os.path.join(utils.img_dir, file)) prediction = net.predict([f0], oversample=False) features = net.blobs[layer].data[0] X = np.zeros((N, features.size), dtype='float32') ids = []
def __init__(self, gpuID, model_path): #################################################### # Options # ------------------ self.gpu_ids = 1 self.which_epoch = '59' self.batch_size = 1 self.use_dense = False self.use_PCB = False self.model_path = model_path self.class_num = 751 self.score_threshold = 0.9 self.confidence_threshold = 0.6 #################################################### # Set gpu # ------------------ use_gpu = torch.cuda.is_available() if not use_gpu: print('can not user gpu') exit() torch.cuda.set_device(self.gpu_ids) #################################################### # Load model # ------------------ print('load model...') if self.use_dense: model_structure = ft_net_dense(self.class_num) else: model_structure = ft_net(self.class_num) if self.use_PCB: model_structure = PCB(self.class_num) model = utils.load_network(model_structure, self.model_path, self.which_epoch) # Remove the final fc layer and classifier layer if not self.use_PCB: model.model.fc = nn.Sequential() model.classifier = nn.Sequential() else: model = PCB_test(model) model = model.eval() #print(model) if use_gpu: model = model.cuda() self.model = model #################################################### # Set Transform # ------------------ print('set transform...') self.data_transforms = transforms.Compose([ transforms.Resize((256, 128), interpolation=3), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ]) if self.use_PCB: self.data_transforms = transforms.Compose([ transforms.Resize((384, 192), interpolation=3), transforms.ToTensor(), transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) ])
choices=['market1501', 'cuhk03', 'duke']) parser.add_argument('--batch_size', default=512, type=int, help='batchsize') parser.add_argument('--share_conv', action='store_true') arg = parser.parse_args() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") save_dir_path = os.path.join(arg.save_path, arg.dataset) logger = utils.Logger(save_dir_path) logger.info(vars(arg)) # Just to get the correct num_classes of classifier fc train_dataloader = utils.getDataLoader(arg.dataset, arg.batch_size, 'train') model = utils.load_network( PCBModel(num_classes=len(train_dataloader.dataset.classes), share_conv=arg.share_conv, return_features=True), save_dir_path, arg.which_epoch) model = model.to(device) CMC, mAP, rank_list_fig = test(model, arg.dataset, arg.batch_size) logger.info('Testing: top1:%.2f top5:%.2f top10:%.2f mAP:%.2f' % (CMC[0], CMC[4], CMC[9], mAP)) logger.save_img(rank_list_fig) torch.cuda.empty_cache()
return pd.DataFrame({ "node_id": np.arange(nodes.size), "mag_journal_id": nodes, "community_id": cids }) if __name__ == "__main__": OUTPUT = sys.argv.pop() YEARS = [int(y) for y in sys.argv[1:]] print("years", YEARS) print("Loading networks") A, Araw, nodes = utils.load_network(YEARS) print("Construct graph tool graph object") G = to_graph_tool(A) print("Estimating") states = gt.minimize_blockmodel_dl( G, deg_corr=True, state_args=dict(eweight=G.ep.weight), verbose=True, B_max=np.round(A.shape[0] / 3).astype(int), ) print("Save") community_table = make_community_table(states, nodes)
def main(): setup_experiment() data = mnist_load() X_test = data.X_test y_test = data.y_test if FLAGS.sort_labels: ys_indices = np.argsort(y_test) X_test = X_test[ys_indices] y_test = y_test[ys_indices] img_shape = [None, 1, 28, 28] test_images = T.tensor4('test_images') test_labels = T.lvector('test_labels') # loaded discriminator number of classes and dims layer_dims = [int(dim) for dim in FLAGS.layer_dims.split("-")] num_classes = layer_dims[-1] # create and load discriminator net = create_network(FLAGS.model, img_shape, layer_dims=layer_dims) load_network(net, epoch=FLAGS.load_epoch) model = with_end_points(net) test_outputs = model(test_images, deterministic=True) # deepfool images test_df_images = deepfool(lambda x: model(x, deterministic=True)['logits'], test_images, test_labels, num_classes, max_iter=FLAGS.deepfool_iter, clip_dist=FLAGS.deepfool_clip, over_shoot=FLAGS.deepfool_overshoot) test_df_images_all = deepfool( lambda x: model(x, deterministic=True)['logits'], test_images, num_classes=num_classes, max_iter=FLAGS.deepfool_iter, clip_dist=FLAGS.deepfool_clip, over_shoot=FLAGS.deepfool_overshoot) test_df_outputs = model(test_df_images, deterministic=True) # fast gradient sign images test_fgsm_images = test_images + fast_gradient_perturbation( test_images, test_outputs['logits'], test_labels, FLAGS.fgsm_epsilon) test_at_outputs = model(test_fgsm_images, deterministic=True) # test metrics test_acc = categorical_accuracy(test_outputs['logits'], test_labels).mean() test_err = 1 - test_acc test_fgsm_acc = categorical_accuracy(test_at_outputs['logits'], test_labels).mean() test_fgsm_err = 1 - test_fgsm_acc test_df_acc = categorical_accuracy(test_df_outputs['logits'], test_labels).mean() test_df_err = 1 - test_df_acc # adversarial noise statistics reduc_ind = range(1, test_images.ndim) test_l2_df = T.sqrt( T.sum((test_df_images - test_images)**2, axis=reduc_ind)) test_l2_df_norm = test_l2_df / T.sqrt(T.sum(test_images**2, axis=reduc_ind)) test_l2_df_skip = test_l2_df.sum() / T.sum(test_l2_df > 0) test_l2_df_skip_norm = test_l2_df_norm.sum() / T.sum(test_l2_df_norm > 0) test_l2_df_all = T.sqrt( T.sum((test_df_images_all - test_images)**2, axis=reduc_ind)) test_l2_df_all_norm = test_l2_df_all / T.sqrt( T.sum(test_images**2, axis=reduc_ind)) test_metrics = OrderedDict([('err', test_err), ('err_fgsm', test_fgsm_err), ('err_df', test_df_err), ('l2_df', test_l2_df.mean()), ('l2_df_norm', test_l2_df_norm.mean()), ('l2_df_skip', test_l2_df_skip), ('l2_df_skip_norm', test_l2_df_skip_norm), ('l2_df_all', test_l2_df_all.mean()), ('l2_df_all_norm', test_l2_df_all_norm.mean()) ]) logger.info("Compiling theano functions...") test_fn = theano.function([test_images, test_labels], outputs=test_metrics.values()) generate_fn = theano.function([test_images, test_labels], [test_df_images, test_df_images_all], on_unused_input='ignore') logger.info("Generate samples...") samples_per_class = 10 summary_images, summary_labels = select_balanced_subset( X_test, y_test, num_classes, samples_per_class) save_path = os.path.join(FLAGS.samples_dir, 'orig.png') save_images(summary_images, save_path) df_images, df_images_all = generate_fn(summary_images, summary_labels) save_path = os.path.join(FLAGS.samples_dir, 'deepfool.png') save_images(df_images, save_path) save_path = os.path.join(FLAGS.samples_dir, 'deepfool_all.png') save_images(df_images_all, save_path) logger.info("Starting...") test_iterator = batch_iterator(X_test, y_test, FLAGS.batch_size, shuffle=False) test_results = np.zeros(len(test_fn.outputs)) start_time = time.time() for batch_index, (images, labels) in enumerate(test_iterator, 1): batch_results = test_fn(images, labels) test_results += batch_results if batch_index % FLAGS.summary_frequency == 0: df_images, df_images_all = generate_fn(images, labels) save_path = os.path.join(FLAGS.samples_dir, 'b%d-df.png' % batch_index) save_images(df_images, save_path) save_path = os.path.join(FLAGS.samples_dir, 'b%d-df_all.png' % batch_index) save_images(df_images_all, save_path) logger.info( build_result_str( "Batch [{}] adversarial statistics:".format(batch_index), test_metrics.keys(), batch_results)) test_results /= batch_index logger.info( build_result_str( "Test results [{:.2f}s]:".format(time.time() - start_time), test_metrics.keys(), test_results))
import caffe image_file = 'ubot5_1.JPG' layer = 'pool5' dimension = 256 compression = 'pca' k = 10 compressor = utils.load_compressor(layer=layer, dimension=dimension, compression=compression) scalar = utils.load_scalar(layer=layer) net, params, blobs = utils.load_network() input_image = caffe.io.load_image(os.path.join(utils.instances_dir, image_file)) # predict takes any number of images, and formats them for the Caffe net automatically prediction = net.predict([input_image], oversample=False) feat = net.blobs[layer].data[0].ravel() feat = scalar.transform(feat) comp_feat = compressor.transform(feat).ravel() results = sql.query_top_k(k=k, features=comp_feat, compression=compression, layer=layer, dimension=dimension)
parser = argparse.ArgumentParser() parser.add_argument("-n", "--network", type=str, default=os.path.join("data", "tidy", "net.pickle"), help="Path to the pickled network file") args = parser.parse_args() netfile = args.network print("Loading saved network...") if not os.path.isfile(netfile): print("No such file: %s" % netfile) exit() try: network, output = load_network(netfile) except Exception, e: print("Could not load network: %s" % e) print("Loading test dataset...") # load test data chunk dl = DataLoader(image_size=IMAGE_SIZE) test_filenames = dl.test_images n_predictions = len(test_filenames) print("Compiling theano functions...") # set up symbolic variables X = T.tensor4('X') X_batch = T.tensor4('X_batch') batch_index = T.iscalar('batch_index') pred = T.iround(output.get_output(X_batch, deterministic=True))
args = parser.parse_args() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") # Make saving directory save_dir_path = os.path.join(args.save_path, args.dataset) os.makedirs(save_dir_path, exist_ok=True) logger = utils.Logger(save_dir_path) logger.info(vars(args)) train_dataloader = getDataLoader( args.dataset, args.batch_size, args.dataset_path, 'train', shuffle=True, augment=True) # model = build_model(args.experiment, num_classes=len(train_dataloader.dataset.classes), # share_conv=args.share_conv) model = build_model(args.experiment, num_classes=751, share_conv=args.share_conv) model = utils.load_network(model, save_dir_path, args.which_epoch) device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) CMC, mAP = test(model, args.dataset, args.dataset_path, args.batch_size) logger.info('Testing: top1:%.2f top5:%.2f top10:%.2f mAP:%.2f' % (CMC[0], CMC[4], CMC[9], mAP)) torch.cuda.empty_cache()
"--mean-file", type=str, default='', help="Path to the file storing network configuration") parser.add_argument("-d", "--data", type=str, default='', help="Data file") args = parser.parse_args() ##################### # Build the model # ##################### net = load_network(args.network) print("Loaded network: ") print_network(net) # allocate symbolic variables for theano graph computations X_batch = T.tensor4('x') data = np.load(args.data) if args.mean_file: mean = np.load(args.mean_file) if args.mean_file: data = data - mean x_test = np.rollaxis(data, 3, 1)
'--proba', dest='proba', action='store_const', const=1, default=0, help='flag to predict probabilities rather than hard targets') args = parser.parse_args() netfile = args.network print("Loading saved network...") if not os.path.isfile(netfile): print(("No such file: %s" % netfile)) exit() try: network, output = load_network(netfile) except Exception as e: print(("Could not load network: %s" % e)) print("Loading test dataset...") # load test data chunk dl = DataLoader(image_size=IMAGE_SIZE, normalize=True, batch_size=64, parallel=False, train_path="train/trimmed", test_path=os.path.join("test", "trimmed")) test_filenames = dl.test_images n_predictions = len(test_filenames) print("Compiling theano functions...") # set up symbolic variables
return np.array(group_embs) def output_embs(): np.save(args.group_embs, embs) if __name__ == '__main__': args = parse_args() # generatint motifs cert2idx, idx2size = generate_motif(args.motif_size) num_motifs = len(cert2idx) # loading the data G = load_network(args.network_format, args.network) groups = load_groups(args.groups) num_groups = len(groups) group_ams = [] for group in groups: group_ams.append(nx.adjacency_matrix(G.subgraph(group)).toarray()) # generating the transition matrices neighbors_motif_motif, weights_motif_motif, neighbors_motif_group, weights_motif_group, neighbors_group_motif, weights_group_motif = generate_transition_matrices( ) # generating group corpus corpus = generate_group_corpus() # embedding
outputs = model(input_img) outputs = nn.functional.softmax(outputs) cams = torch.cat((cams, outputs.data.cpu().float()), 0) return cams gallery_path = image_datasets['gallery'].imgs query_path = image_datasets['query'].imgs ###################################################################### names = opt.names.split(',') models = nn.ModuleList() for name in names: model_tmp, _, epoch = load_network(name, opt) model_tmp.classifier.classifier = nn.Sequential() model_tmp = torch.nn.DataParallel(model_tmp) models.append(model_tmp.cuda().eval()) # Extract feature\ snapshot_feature_mat = './feature/submit_result_%s.mat' % opt.names print('Feature Output Path: %s' % snapshot_feature_mat) if not os.path.isfile(snapshot_feature_mat): with torch.no_grad(): gallery_feature, query_feature = torch.FloatTensor( ), torch.FloatTensor() for model in models: q_f = extract_feature(model, dataloaders['query']) q_f_crop = extract_feature(model, cropped_dataloaders['query']) q_f = q_f + q_f_crop
2] # "../../data/ThomsonReuters/journal-groups-suspended-by-TR.csv" CI_DETECTED_DIR = sys.argv[3] # "../../data/cartels" CLASSIFIED_RESULT = sys.argv[4] # "classified_results.csv" SAMPLED_CAETELS_FILE = sys.argv[5] # "sampled_" th = 0.3 # Fraction of citations above which we regard excessive concentration years = np.arange(2010, 2020) # Years for the detected cartels # Connect to the database graph = utils.get_db() # Load the networks A_list = {} node_list = {} for year in years: A, _, nodes = utils.load_network(year, NET_DIR) A_list[year] = A node_list[year] = nodes groups_TR = pd.read_csv(TR_DETECTED_FILE, sep="\t") groups_CI = utils.load_detected_cartels(years, CI_DETECTED_DIR) # Remove groups that contain at least one suspended journal suspended_journals = np.unique(groups_TR["mag_journal_id"].values) contained = [ gid for gid, dg in groups_CI.groupby("gross_group_id") if ~np.any(np.isin(dg["mag_journal_id"], suspended_journals)) ] unsuspended_groups_CI = utils.slice_groups(groups_CI, contained, "gross_group_id")
def get_id(img_path): camera_id = [] labels = [] paths = [] for path, v in img_path: folder_name = os.path.basename(os.path.dirname(path)) labels.append(int(folder_name)) paths.append(path) return labels, paths ###################################################################### # Load Collected data Trained model print('-------test-----------') model, _, epoch = load_network(opt.name, opt) model.classifier.classifier = nn.Sequential() model = model.eval() if use_gpu: model = model.cuda() # Extract feature since = time.time() #gallery_name = 'gallery_street' #query_name = 'query_satellite' gallery_name = 'gallery_satellite' #query_name = 'query_street' #gallery_name = 'gallery_street'
from scipy.sparse import lil_matrix tf.set_random_seed(0) np.random.seed(0) source= 'Blog1' target = 'Blog2' emb_filename=str(source)+'_'+str(target) Kstep=3 #################### # Load source data #################### A_s, X_s, Y_s= utils.load_network('./input/'+str(source)+'.mat') num_nodes_S=X_s.shape[0] #################### # Load target data #################### A_t, X_t, Y_t = utils.load_network('./input/'+str(target)+'.mat') num_nodes_T=X_t.shape[0] features=vstack((X_s, X_t)) features = utils.feature_compression(features, dim=1000) X_s=features[0:num_nodes_S,:] X_t=features[-num_nodes_T:,:] '''compute PPMI'''
def main(): train_dataloader, test_dataloader = get_data_loaders(opt) capsule_net = load_network(opt) criterion = nn.MSELoss(reduction='mean').cuda() optimizer = optim.Adam(capsule_net.parameters(), lr=0.0001) scheduler = lr_scheduler.StepLR(optimizer, step_size=50, gamma=0.1) capsule_net.train() if opt.load_weights != '': print('Load weights from', opt.load_weights) capsule_net.module.load_state_dict(torch.load(opt.load_weights)) # =============== TRAIN ========================= global_train_step = 0 global_test_step = 0 best_map = 0 for i, epoch in enumerate(range(opt.nepoch)): train_loss_sum = 0 true_values_per_joint_per_threshold = np.zeros( (15, thresholds.shape[0])) total_examples = 0 capsule_net.train() print( f'======>>>>> Online epoch: {epoch}, lr={get_lr(optimizer)} <<<<<======' ) for data in tqdm(train_dataloader): global_train_step += 1 points, coords, mean, maxv = data points, coords = points.cuda(non_blocking=True), coords.cuda( non_blocking=True) if points.size(0) < opt.batchSize: break optimizer.zero_grad() estimation, reconstructions = capsule_net(points) dist1, dist2 = chamfer_dist(reconstructions, points) reconstruction_loss = (torch.mean(dist1)) + (torch.mean(dist2)) regression_loss = criterion(estimation, coords) total_loss = reconstruction_loss + regression_loss total_loss.backward() optimizer.step() torch.cuda.synchronize() writer.add_scalar('train/reconstruction-loss', reconstruction_loss.detach().cpu().numpy(), global_step=global_train_step) writer.add_scalar('train/regression-loss', regression_loss.detach().cpu().numpy(), global_step=global_train_step) points_to_display = np.expand_dims(points.cpu().numpy()[0], axis=0) reconstructions_to_display = np.expand_dims( reconstructions.detach().cpu().numpy()[0], axis=0) points_to_display = ITOPDataset.denormalize( points_to_display, mean[0].numpy(), maxv[0].numpy()) reconstructions_to_display = ITOPDataset.denormalize( reconstructions_to_display, mean[0].numpy(), maxv[0].numpy()) writer.add_mesh('train/points', vertices=points_to_display, global_step=global_train_step) writer.add_mesh('train/points-reconstruction', vertices=reconstructions_to_display, global_step=global_train_step) writer.add_scalar('train/lr', get_lr(optimizer), global_step=global_train_step) # mAP calculating total_examples += len(estimation) estimation = ITOPDataset.denormalize( estimation.detach().cpu().numpy(), mean.numpy(), maxv.numpy()) coords = ITOPDataset.denormalize(coords.detach().cpu().numpy(), mean.numpy(), maxv.numpy()) batch_diff = np.linalg.norm(estimation - coords, axis=2) # N x JOINT_SIZE for example in batch_diff: for i, joint_diff in enumerate(example): true_values_per_joint_per_threshold[i] += ( joint_diff < thresholds).astype(int) train_loss_sum += total_loss.item() scheduler.step() torch.cuda.synchronize() map_fig, map_01 = build_map_plot( thresholds, true_values_per_joint_per_threshold / total_examples) writer.add_figure('train/map', map_fig, global_step=global_train_step) # =============== EVAL ========================= test_reconstruction_loss_sum = 0 test_regression_loss_sum = 0 true_values_per_joint_per_threshold = np.zeros( (15, thresholds.shape[0])) total_examples = 0 for i, data in enumerate(tqdm(test_dataloader)): global_test_step += 1 capsule_net.eval() points, coords, mean, maxv = data points, coords = points.cuda(), coords.cuda() estimation, reconstructions = capsule_net(points) dist1, dist2 = chamfer_dist(points, reconstructions) test_reconstruction_loss = (torch.mean(dist1)) + ( torch.mean(dist2)) test_regression_loss = criterion(estimation, coords) test_reconstruction_loss_sum += test_reconstruction_loss.item() test_regression_loss_sum += test_regression_loss.item() points_to_display = np.expand_dims(points.cpu().numpy()[0], axis=0) reconstructions_to_display = np.expand_dims( reconstructions.detach().cpu().numpy()[0], axis=0) points_to_display = ITOPDataset.denormalize( points_to_display, mean[0].numpy(), maxv[0].numpy()) reconstructions_to_display = ITOPDataset.denormalize( reconstructions_to_display, mean[0].numpy(), maxv[0].numpy()) writer.add_mesh('test/points', vertices=points_to_display, global_step=global_test_step) writer.add_mesh('test/points-reconstruction', vertices=reconstructions_to_display, global_step=global_test_step) # -------- mAP calculating total_examples += len(estimation) estimation = ITOPDataset.denormalize( estimation.detach().cpu().numpy(), mean.numpy(), maxv.numpy()) coords = ITOPDataset.denormalize(coords.detach().cpu().numpy(), mean.numpy(), maxv.numpy()) batch_diff = np.linalg.norm(estimation - coords, axis=2) # N x JOINT_SIZE for example in batch_diff: for i, joint_diff in enumerate(example): true_values_per_joint_per_threshold[i] += ( joint_diff < thresholds).astype(int) avg_reconstruction = test_reconstruction_loss_sum / len( test_dataloader) avg_regression = test_regression_loss_sum / len(test_dataloader) writer.add_scalar('test/reconstruction-loss', avg_reconstruction, global_step=global_test_step) writer.add_scalar('test/regression-loss', avg_regression, global_step=global_test_step) map_fig, map_01 = build_map_plot( thresholds, true_values_per_joint_per_threshold / total_examples) writer.add_figure('test/map', map_fig, global_step=global_test_step) if best_map < map_01: best_map = map_01 torch.save( capsule_net.module.state_dict(), f'{save_dir}/{epoch:03}-{best_map:0.3}-capsule_net-module.pth')
parser.add_argument('--which_epoch', default='final', type=str, help='0,1,2,3,...or final') parser.add_argument('--dataset', type=str, default='data') parser.add_argument('--batch_size', default=32, type=int, help='batch_size') parser.add_argument('--mode_type', default='test', type=str) arg = parser.parse_args() # device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') device = torch.device("cuda" if torch.cuda.is_available() else "cpu") save_path = os.path.join(arg.save_path, arg.dataset) print('Save_path :: ', save_path) logger = utils.Logger(save_path) # model = utils.load_network(Merge_image_text(num_class=len(train_dataloader.dataset.classes),mode=arg.mode_type), # save_path,arg.which_epoch) model = utils.load_network( Merge_image_text(num_class=11003, mode=arg.mode_type), save_path, arg.which_epoch) model = model.to(device) CMC, mAP = test(model, arg.dataset, arg.batch_size) logger.info('Testing: top1:%.2f top5:%.2f top10:%.2f mAP:%.2f' % (CMC[0], CMC[4], CMC[9], mAP)) torch.cuda.empty_cache()
parser.add_argument('--lr', default=0.05, type=float, help='learning rate') parser.add_argument('--droprate', default=0.5, type=float, help='drop rate') parser.add_argument('--PCB', action='store_true', help='use PCB+ResNet50') parser.add_argument('--CPB', action='store_true', help='use Center+ResNet50') parser.add_argument( '--fp16', action='store_true', help='use float16 instead of float32, which will save about 50% memory') parser.add_argument('--balance', action='store_true', help='balance sample') parser.add_argument('--angle', action='store_true', help='use angle loss') parser.add_argument('--arc', action='store_true', help='use arc loss') parser.add_argument('--resume', action='store_true', help='use arc loss') opt = parser.parse_args() if opt.resume: model, opt, start_epoch = load_network(opt.name, opt) else: start_epoch = 0 print(start_epoch) fp16 = opt.fp16 data_dir = opt.data_dir name = opt.name if not opt.resume: str_ids = opt.gpu_ids.split(',') gpu_ids = [] for str_id in str_ids: gid = int(str_id) if gid >= 0:
EX_segmentor_dir = "../model/EX_segmentor" HE_segmentor_dir = "../model/HE_segmentor" MA_segmentor_dir = "../model/MA_segmentor" SE_segmentor_dir = "../model/SE_segmentor" for task in ["EX", "HE", "MA", "SE"]: if not os.path.isdir(os.path.join(img_out_dir, task)): os.makedirs(os.path.join(img_out_dir, task)) # set iterators for training and validation training_set, validation_set = utils.split_dr(fundus_dirs, grade_path, val_ratio) val_batch_fetcher = iterator_dr_640.ValidationBatchFetcher( validation_set, batch_size) EX_segmentor = utils.load_network(EX_segmentor_dir) HE_segmentor = utils.load_network(HE_segmentor_dir) MA_segmentor = utils.load_network(MA_segmentor_dir) SE_segmentor = utils.load_network(SE_segmentor_dir) # normalization_method # EX, SE: rescale # HE, MA: rescale_mean_subtract # network input order: ex, he, ma, se for fnames, fundus_rescale, fundus_rescale_mean_subtract, grades in val_batch_fetcher( ): # debugging purpose ex_arr = EX_segmentor.predict(fundus_rescale, batch_size=batch_size, verbose=0) he_arr = HE_segmentor.predict(fundus_rescale_mean_subtract,
cont_encoder = model.cont_encoder(opt.z_dim*opt.factor, opt.channels*opt.n_past) #g_dim = 64 or 128 pose_encoder = model.pose_encoder(opt.g_dim, opt.channels) decoder = model.decoder(opt.g_dim, opt.channels) # init frame_predictor = utils.init_net(frame_predictor, init_type='normal', init_gain=0.02, gpu_ids=opt.gpu_ids) posterior_pose = utils.init_net(posterior_pose, init_type='normal', init_gain=0.02, gpu_ids=opt.gpu_ids) prior = utils.init_net(prior, init_type='normal', init_gain=0.02, gpu_ids=opt.gpu_ids) cont_encoder = utils.init_net(cont_encoder, init_type='normal', init_gain=0.02, gpu_ids=opt.gpu_ids) pose_encoder = utils.init_net(pose_encoder, init_type='normal', init_gain=0.02, gpu_ids=opt.gpu_ids) decoder = utils.init_net(decoder, init_type='normal', init_gain=0.02, gpu_ids=opt.gpu_ids) # load utils.load_network(frame_predictor, 'frame_predictor', 'last', opt.model_path,device) utils.load_network(posterior_pose, 'posterior_pose', 'last', opt.model_path,device) utils.load_network(prior, 'prior', 'last', opt.model_path,device) utils.load_network(cont_encoder, 'cont_encoder', 'last', opt.model_path,device) utils.load_network(pose_encoder, 'pose_encoder', 'last', opt.model_path,device) utils.load_network(decoder, 'decoder', 'last', opt.model_path,device) frame_predictor.eval() prior.eval() posterior_pose.eval() cont_encoder.eval() pose_encoder.eval() decoder.eval()
"--network", type=str, help="Path to the pickled network file") parser.add_argument("-m", "--mean-file", type=str, default='', help="Path to the file storing network configuration") parser.add_argument("-d", "--data", type=str, default='', help="Data file") args = parser.parse_args() ##################### # Build the model # ##################### net = load_network(args.network) print("Loaded network: ") print_network(net) # allocate symbolic variables for theano graph computations X_batch = T.tensor4('x') data = np.load(args.data) if args.mean_file: mean = np.load(args.mean_file) if args.mean_file: data = data - mean x_test = np.rollaxis(data, 3, 1)
from parameter import get_parameter from utils import load_network, save_network from train import train_network from evaluate import test_network from hardprune import hard_prune_network from softprune import soft_prune_network import os os.environ["CUDA_VISIBLE_DEVICES"] = "2" if __name__ == '__main__': args = get_parameter() network = load_network(args) print(network) if args.train_flag: print('args.train_flag:', args.train_flag) network = train_network(network, args) elif args.hard_prune_flag: print('hard_prune_flag:', args.hard_prune_flag) network = hard_prune_network(network, args) elif args.soft_prune_flag: network = soft_prune_network(network, args) print(network) test_network(network, args) # network = train_network(network, args) save_network(network, args)
if not os.path.isdir(img_out_dir): os.makedirs(img_out_dir) if not os.path.isdir(train_img_check_dir): os.makedirs(train_img_check_dir) if not os.path.isdir(val_img_check_dir): os.makedirs(val_img_check_dir) # set iterator training_set, validation_set = utils.split(fundus_dir, vessel_dir, grade_path, FLAGS.grade_type, 1) val_batch_fetcher = iterator_dme.ValidationBatchFetcher( validation_set, batch_size, FLAGS.grade_type) # create networks K.set_learning_phase(False) EX_segmentor = utils.load_network(EX_segmentor_dir) fovea_localizer = utils.load_network(fovea_localizer_dir) od_segmentor = utils.load_network(od_segmentor) # start inference check_train_batch, check_validation_batch = True, True list_grades, list_od_found, list_sum_intensity_inside, list_sum_intensity_outside, list_fnames = [], [], [], [], [] for fnames, imgs_mean_subt, imgs_z, vessels, grades_onehot in val_batch_fetcher( ): if check_validation_batch: utils.check_input(imgs_mean_subt, imgs_z, vessels, val_img_check_dir) check_validation_batch = False segmented = EX_segmentor.predict(imgs_mean_subt, batch_size=batch_size, verbose=0) fovea_loc, fovea_loc_vessel = fovea_localizer.predict(
import matplotlib import matplotlib.pyplot as plt def convert_row(row): a, b, c, d = utm.from_latlon(row['lat'], row['lon'], force_zone_number = 1, force_zone_letter = 'S') row['lat'] = a row['lon'] = b return row def to_utm(data ): return data.apply(convert_row, axis=1) nodes, links = load_network('/home/pestka/Documents/studia/pszt/proj3/sndlib-networks-native/polska.txt') # nodes = to_utm(nodes) name_map = {r['name']:(r['lat'], r['lon']) for i, r in nodes.iterrows()} MAX_COST = links.cost.map(float).max() / 3. for i, row in links.iterrows(): p1 = name_map[row['source']] p2 = name_map[row['target']] plt.plot([p1[1], p2[1]], [p1[0], p2[0]], c='gray', linewidth = float(row['cost']) / MAX_COST) plt.scatter(x=nodes.lon, y=nodes.lat) plt.show()
action='store_true', help='use resume trainning') parser.add_argument('--share', action='store_true', help='share weight between different view') parser.add_argument('--extra_Google', action='store_true', help='using extra noise Google') parser.add_argument( '--fp16', action='store_true', help='use float16 instead of float32, which will save about 50% memory') opt = parser.parse_args() if opt.resume: model, opt, start_epoch = load_network(opt.name, opt) else: start_epoch = 0 fp16 = opt.fp16 data_dir = opt.data_dir name = opt.name str_ids = opt.gpu_ids.split(',') gpu_ids = [] for str_id in str_ids: gid = int(str_id) if gid >= 0: gpu_ids.append(gid) # set gpu ids if len(gpu_ids) > 0:
# # Parameters # if __name__ == "__main__": YEAR = int(sys.argv[1]) NETWORK_DIR = sys.argv[2] THETA = float(sys.argv[3]) ALPHA = float(sys.argv[4]) COMMUNITY_FILE = sys.argv[5] OUTPUT = sys.argv[6] # Load the network data A_eff, A_gen, nodes = utils.load_network(YEAR, NETWORK_DIR) # Load the communty membership community_table = pd.read_csv(COMMUNITY_FILE, sep="\t") community_ids = ( community_table.set_index("mag_journal_ids").loc[nodes, "community_id"].values ) # Define the filter is_excessive_func = filters.get_dcsbm_threshold_filter( A_eff, A_gen, community_ids, ref_frac_weight=0.5, alpha=ALPHA ) # Detect cartel cartel_table = cidre.detect(A_eff, THETA, is_excessive_func, min_group_edge_num=50)