def train(): device = 'cuda' if torch.cuda.is_available() and is_gpu else 'cpu' print("Using Device -", device) print("Start Training Process...\n") train_dataset = utils.tranform_train_dataset(train_dir) trainloader = utils.DataLoader(train_dataset, 64) valid_dataset = utils.transform_valid_test_dataset(valid_dir) validloader = utils.DataLoader(valid_dataset, 64) test_dataset = utils.transform_valid_test_dataset(test_dir) testloader = utils.DataLoader(test_dataset, 64) fc_model = OrderedDict([ ('fc1', nn.Linear(25088, hidden_layer_1)), ('relu', nn.ReLU()), ('dropout1', nn.Dropout(dropout)), ('fc2', nn.Linear(hidden_layer_1, hidden_layer_1)), ('relu', nn.ReLU()), #('fc3',nn.Linear(256,256)), #('relu',nn.ReLU()), ('fc4', nn.Linear(hidden_layer_1, 102)), ('output', nn.LogSoftmax(dim=1)) ]) model = utils.build_network(fc_model, network, dropout, device) criterion = nn.NLLLoss() optimizer = optim.Adam(model.classifier.parameters(), lr=learning_rate) model = utils.train(model, trainloader, epochs, learning_rate, criterion, optimizer, device, validloader) model.class_to_idx = train_dataset.class_to_idx utils.save_checkpoint(model, optimizer, epochs, save_dir, network, hidden_layer_1, dropout, learning_rate) print("End Training Process...\n") print("Start Test Process...\n") utils.test(model, testloader, criterion, device) print("End Test Process...\n")
dest="hidden_units", action="store", default=4096) parser.add_argument('--gpu', dest="gpu", action="store", default="gpu") parse = parser.parse_args() data_dir = parse.data_dir save_dir = parse.save_dir arch = parse.arch learn_rate = parse.learning_rate epochs = parse.epochs hidden_units = parse.hidden_units gpu = parse.gpu trainloader, validloader, testloader, train_data = utils.data_loaders(data_dir) model, input_size = utils.build_network(arch) print(learn_rate) model, optimizer = utils.trainer(trainloader, validloader, model, epochs=epochs, steps=0, learnrate=learn_rate, print_every=5, gpu=gpu) utils.save_checkpoint(model, train_data, optimizer, input_size,
if __name__ == '__main__': parser = argparse.ArgumentParser(description='Process some integers.') parser.add_argument('--name', '-n', type=str, default='output', help='output model name') args = parser.parse_args() name = args.name use_cuda = torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") net = build_network(input_dim=3) net.to(device) net.load_state_dict( torch.load('./models/{}_model.pth'.format(name), map_location=device)) nb_grid = 128 pts, val = predict(net, device, nb_grid) volume = val.reshape(nb_grid, nb_grid, nb_grid) verts, faces, normals, values = measure.marching_cubes_lewiner( volume, 0.0, spacing=(1.0, -1.0, 1.0), gradient_direction='ascent') mesh = o3d.geometry.TriangleMesh() mesh.vertices = o3d.utility.Vector3dVector(verts) mesh.triangles = o3d.utility.Vector3iVector(faces)
embedding = pickle.load(pf) embed_labels = embedding['ind2label'] embedding = embedding['embedding'] # Load dataset data_generator = get_data_generator(args.dataset, args.data_root, classes = embed_labels) if embedding is None: embedding = np.eye(data_generator.num_classes) # Construct and train model if (args.gpus <= 1) or args.gpu_merge: if args.snapshot and os.path.exists(args.snapshot): print('Resuming from snapshot {}'.format(args.snapshot)) model = keras.models.load_model(args.snapshot, custom_objects = utils.get_custom_objects(args.architecture), compile = False) else: embed_model = utils.build_network(embedding.shape[1], args.architecture) model = embed_model if args.loss == 'inv_corr': model = keras.models.Model(model.inputs, keras.layers.Lambda(utils.l2norm, name = 'l2norm')(model.output)) elif args.loss == 'softmax_corr': model = keras.models.Model(model.inputs, keras.layers.Activation('softmax', name = 'softmax')(model.output)) if args.cls_weight > 0: model = cls_model(model, data_generator.num_classes, args.cls_base) par_model = model if args.gpus <= 1 else keras.utils.multi_gpu_model(model, gpus = args.gpus, cpu_merge = False) else: with K.tf.device('/cpu:0'): if args.snapshot and os.path.exists(args.snapshot): print('Resuming from snapshot {}'.format(args.snapshot)) model = keras.models.load_model(args.snapshot, custom_objects = utils.get_custom_objects(args.architecture), compile = False) else: embed_model = utils.build_network(embedding.shape[1], args.architecture)
class_list = list( OrderedDict((l.strip().split()[0], None) for l in class_file if l.strip() != '').keys()) try: class_list = [int(lbl) for lbl in class_list] except ValueError: pass else: class_list = None data_generator = get_data_generator(args.dataset, args.data_root, classes=class_list) # Construct and train model if (args.gpus <= 1) or args.gpu_merge: embed_model = utils.build_network(args.embed_dim, args.architecture) model = labelembed_model(embed_model, data_generator.num_classes, tau=args.tau, alpha=args.alpha, beta=args.beta) par_model = model if args.gpus <= 1 else keras.utils.multi_gpu_model( model, gpus=args.gpus, cpu_merge=False) else: with K.tf.device('/cpu:0'): embed_model = utils.build_network(args.embed_dim, args.architecture) model = labelembed_model(embed_model, data_generator.num_classes, tau=args.tau, alpha=args.alpha,
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=nw) validate_dataset = datasets.ImageFolder(root=os.path.join(image_path, "val"), transform=data_transform["val"]) val_num = len(validate_dataset) validate_loader = torch.utils.data.DataLoader(validate_dataset, batch_size=batch_size, shuffle=False, num_workers=nw) print("using {} images for training, {} images fot validation.".format(train_num, val_num)) # build network net = build_network(args) net.to(device) loss_function = nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(), lr=args.lr) best_acc = 0.0 save_path = './' + args.net + '.pth' for epoch in range(args.epoch): # train net.train() running_loss = 0.0 for step, data in enumerate(train_loader): images, labels = data outputs = net(images.to(device))
else: class_list = None data_generator = get_data_generator(args.dataset, args.data_root, classes=class_list) # Construct and train model if (args.gpus <= 1) or args.gpu_merge: if args.snapshot and os.path.exists(args.snapshot): print('Resuming from snapshot {}'.format(args.snapshot)) model = keras.models.load_model( args.snapshot, custom_objects=utils.get_custom_objects(args.architecture), compile=False) else: model = utils.build_network(data_generator.num_classes, args.architecture, True) par_model = model if args.gpus <= 1 else keras.utils.multi_gpu_model( model, gpus=args.gpus, cpu_merge=False) else: with K.tf.device('/cpu:0'): if args.snapshot and os.path.exists(args.snapshot): print('Resuming from snapshot {}'.format(args.snapshot)) model = keras.models.load_model( args.snapshot, custom_objects=utils.get_custom_objects(args.architecture), compile=False) else: model = utils.build_network(data_generator.num_classes, args.architecture, True) par_model = keras.utils.multi_gpu_model(model, gpus=args.gpus)
# load image img = Image.open("../tulip.jpg") plt.imshow(img) # [N, C, H, W] img = data_transform(img) # expand batch dimension img = torch.unsqueeze(img, dim=0) # read class_indict try: json_file = open('./class_indices.json', 'r') class_indict = json.load(json_file) except Exception as e: print(e) exit(-1) # create model model = build_network(args) # load model weights model_weight_path = './' + args.net + '.pth' missing_keys, unexpected_keys = model.load_state_dict( torch.load(model_weight_path), strict=False ) # strict 使得加载网络的时候严格按照现在构建的网络结构加载数据, 即当前不含两个aux分支, 加载的时候也不加载该分支 model.eval() with torch.no_grad(): # predict class output = torch.squeeze(model(img)) predict = torch.softmax(output, dim=0) predict_cla = torch.argmax(predict).numpy() print(class_indict[str(predict_cla)]) plt.show()
# Stack to compute training mean and std X_mean, X_std = utils.standardize(np.concatenate(X_train, axis=0)) Y_mean, Y_std = utils.standardize(np.concatenate(Y_train, axis=0)) # Compute max length as median of lengths max_length_X = int(np.median([len(X) for X in X_train])) max_length_Y = int(np.median([len(Y) for Y in Y_train])) # Retrieve the hyperparameters which achivieved the lowest objective best_params, _ = train_best_network.get_best_trial(RESULTS_PATH) # Convert parameters to layer specifications (conv_layer_specs, dense_layer_specs) = train_network.layer_specs_from_params(best_params) # Build networks layers = { 'X': utils.build_network( (None, None, X_train[0].shape[-1]), conv_layer_specs, dense_layer_specs), 'Y': utils.build_network( (None, None, Y_train[0].shape[-1]), conv_layer_specs, dense_layer_specs)} # Load in parameters of trained best-performing networks with open(os.path.join(MODEL_PATH, 'X_params.pkl')) as f: X_params = pickle.load(f) # Set parameter values of build network lasagne.layers.set_all_param_values(layers['X']['out'], X_params) with open(os.path.join(MODEL_PATH, 'Y_params.pkl')) as f: Y_params = pickle.load(f) lasagne.layers.set_all_param_values(layers['Y']['out'], Y_params) # Compile functions for embedding with each network embed_X = theano.function([layers['X']['in'].input_var], lasagne.layers.get_output(layers['X']['out']))
def train(data, sample_size_X, sample_size_Y, conv_layer_specs, dense_layer_specs, alpha_XY, m_XY, optimizer=lasagne.updates.rmsprop, batch_size=20, epoch_size=100, initial_patience=1000, improvement_threshold=0.99, patience_increase=5, max_iter=100000): ''' Utility function for training a siamese net for cross-modality hashing Assumes data['X_train'][n] should be mapped close to data['Y_train'][m] only when n == m :parameters: - data : dict of list of np.ndarray Training/validation sequences in X/Y modality Should contain keys X_train, Y_train, X_validate, Y_validate Sequence matrix shape=(n_sequences, n_time_steps, n_features) - sample_size_X, sample_size_Y : int Sampled sequence length for X/Y modalities - conv_layer_specs, dense_layer_specs : list of dict List of dicts, where each dict corresponds to keyword arguments for each subsequent layer. Note that dense_layer_specs[-1]['num_units'] should be the output dimensionality of the network. - alpha_XY : float Scaling parameter for cross-modality negative example cost - m_XY : int Cross-modality negative example threshold - optimizer: function Function which takes a Theano expression and parameters and computes parameter updates to minimize the Theano expression (for example, something from lasagne.updates). - batch_size : int Mini-batch size - epoch_size : int Number of mini-batches per epoch - initial_patience : int Always train on at least this many batches - improvement_threshold : float Validation cost must decrease by this factor to increase patience - patience_increase : int How many more epochs should we wait when we increase patience - max_iter : int Maximum number of batches to train on :returns: - epoch : iterator Results for each epoch are yielded ''' # Create networks layers = { 'X': utils.build_network( (None, None, data['X_train'][0].shape[-1]), conv_layer_specs, dense_layer_specs), 'Y': utils.build_network( (None, None, data['Y_train'][0].shape[-1]), conv_layer_specs, dense_layer_specs)} # Inputs to X modality neural nets X_p_input = T.tensor3('X_p_input') X_n_input = T.tensor3('X_n_input') # Y network Y_p_input = T.tensor3('Y_p_input') Y_n_input = T.tensor3('Y_n_input') # Compute \sum max(0, m - ||a - b||_2)^2 def hinge_cost(m, a, b): dist = m - T.sqrt(T.sum((a - b)**2, axis=1)) return T.mean((dist*(dist > 0))**2) def hasher_cost(deterministic): X_p_output = lasagne.layers.get_output( layers['X']['out'], {layers['X']['in']: X_p_input}, deterministic=deterministic) X_n_output = lasagne.layers.get_output( layers['X']['out'], {layers['X']['in']: X_n_input}, deterministic=deterministic) Y_p_output = lasagne.layers.get_output( layers['Y']['out'], {layers['Y']['in']: Y_p_input}, deterministic=deterministic) Y_n_output = lasagne.layers.get_output( layers['Y']['out'], {layers['Y']['in']: Y_n_input}, deterministic=deterministic) # Unthresholded, unscaled cost of positive examples across modalities cost_p = T.mean(T.sum((X_p_output - Y_p_output)**2, axis=1)) # Thresholded, scaled cost of cross-modality negative examples cost_n = alpha_XY*hinge_cost(m_XY, X_n_output, Y_n_output) # Sum positive and negative costs for overall cost cost = cost_p + cost_n return cost # Combine all parameters from both networks params = (lasagne.layers.get_all_params(layers['X']['out']) + lasagne.layers.get_all_params(layers['Y']['out'])) # Compute RMSProp gradient descent updates updates = optimizer(hasher_cost(False), params) # Function for training the network train = theano.function([X_p_input, X_n_input, Y_p_input, Y_n_input], hasher_cost(False), updates=updates) # Compute cost without training cost = theano.function([X_p_input, X_n_input, Y_p_input, Y_n_input], hasher_cost(True)) # Compute output without training X_output = theano.function( [layers['X']['in'].input_var], lasagne.layers.get_output(layers['X']['out'], deterministic=True)) Y_output = theano.function( [layers['Y']['in'].input_var], lasagne.layers.get_output(layers['Y']['out'], deterministic=True)) # Start with infinite validate cost; we will always increase patience once current_validate_cost = np.inf patience = initial_patience # Create sampled sequences for validation X_validate = utils.sample_sequences( data['X_validate'], sample_size_X) Y_validate = utils.sample_sequences( data['Y_validate'], sample_size_Y) # Create fixed negative example validation set X_validate_shuffle = np.random.permutation(X_validate.shape[0]) Y_validate_shuffle = X_validate_shuffle[ utils.random_derangement(X_validate.shape[0])] # Create iterator to sample sequences from training data data_iterator = utils.get_next_batch( data['X_train'], data['Y_train'], sample_size_X, sample_size_Y, batch_size, max_iter) # We will accumulate the mean train cost over each epoch train_cost = 0 for n, (X_p, Y_p, X_n, Y_n) in enumerate(data_iterator): # Occasionally Theano was raising a MemoryError, this fails gracefully try: train_cost += train(X_p, X_n, Y_p, Y_n) except MemoryError as e: print "MemoryError: {}".format(e) return # Stop training if a NaN is encountered if not np.isfinite(train_cost): print 'Bad training cost {} at iteration {}'.format(train_cost, n) break # Validate the net after each epoch if n and (not n % epoch_size): epoch_result = collections.OrderedDict() epoch_result['iteration'] = n # Compute average training cost over the epoch epoch_result['train_cost'] = train_cost / float(epoch_size) # Reset training cost mean accumulation train_cost = 0 # We need to accumulate the validation cost and network output over # batches to avoid MemoryErrors epoch_result['validate_cost'] = 0 validate_batches = 0 X_val_output = [] Y_val_output = [] for batch_idx in range(0, X_validate.shape[0], batch_size): # Extract slice from validation set for this batch batch_slice = slice(batch_idx, batch_idx + batch_size) # Compute and accumulate cost epoch_result['validate_cost'] += cost( X_validate[batch_slice], X_validate[X_validate_shuffle][batch_slice], Y_validate[batch_slice], Y_validate[Y_validate_shuffle][batch_slice]) # Keep track of # of batches for normalization validate_batches += 1 # Compute network output and accumulate result X_val_output.append(X_output(X_validate[batch_slice])) Y_val_output.append(Y_output(Y_validate[batch_slice])) # Normalize cost by number of batches and store epoch_result['validate_cost'] /= float(validate_batches) # Concatenate per-batch output to tensors X_val_output = np.concatenate(X_val_output, axis=0) Y_val_output = np.concatenate(Y_val_output, axis=0) # Compute in-class and out-of-class distances in_dists = np.mean((X_val_output - Y_val_output)**2, axis=1) out_dists = np.mean((X_val_output[X_validate_shuffle] - Y_val_output[Y_validate_shuffle])**2, axis=1) # Objective is Bhattacharrya coefficient of in-class and # out-of-class distances epoch_result['validate_objective'] = utils.bhatt_coeff( in_dists, out_dists) # Test whether this validate cost is the new smallest if epoch_result['validate_cost'] < current_validate_cost: # To update patience, we must be smaller than # improvement_threshold*(previous lowest validation cost) patience_cost = improvement_threshold*current_validate_cost if epoch_result['validate_cost'] < patience_cost: # Increase patience by the supplied about patience += epoch_size*patience_increase # Even if we didn't increase patience, update lowest valid cost current_validate_cost = epoch_result['validate_cost'] # Store patience after this epoch epoch_result['patience'] = patience # Yield scores and statistics for this epoch X_params = lasagne.layers.get_all_param_values(layers['X']['out']) Y_params = lasagne.layers.get_all_param_values(layers['Y']['out']) yield (epoch_result, X_params, Y_params) if n > patience: break return
help='number of workers for dataloader') parser.add_argument('-b', type=int, default=16, help='batch size for dataloader') parser.add_argument('-s', type=bool, default=True, help='whether shuffle the dataset') args_dict = vars(parser.parse_args()) logger.info(args_dict) net_type = args_dict['net'] use_gpu = args_dict['gpu'] net = build_network(archi=net_type, use_gpu=use_gpu) # logger.info(net) net.load_state_dict(torch.load(args_dict['weights']), args_dict['gpu']) net.eval() example_image_dir = 'D:/SealProjectOLD/Datasets/images/val' dataset = datasets.ImageFolder(example_image_dir, transform=None) idx_to_class = {v: k for k, v in dataset.class_to_idx.items()} json_file = open('author_data.json') list_author = json.load(json_file) if os.path.isfile('lsh.p'): logger.info("load indexed dict") lsh = pickle.load(open('lsh.p', 'rb')) feature_dict = pickle.load(open('feature_dict.p', 'rb'))
data_generator = get_data_generator(args.dataset, args.data_root, classes=class_list) # Construct and train model if (args.gpus <= 1) or args.gpu_merge: if args.snapshot and os.path.exists(args.snapshot): print('Resuming from snapshot {}'.format(args.snapshot)) model = keras.models.load_model( args.snapshot, custom_objects=utils.get_custom_objects(args.architecture), compile=False) else: model = utils.build_network( data_generator.num_classes, args.architecture, True, input_channels=data_generator.num_channels) par_model = model if args.gpus <= 1 else keras.utils.multi_gpu_model( model, gpus=args.gpus, cpu_merge=False) else: with K.tf.device('/cpu:0'): if args.snapshot and os.path.exists(args.snapshot): print('Resuming from snapshot {}'.format(args.snapshot)) model = keras.models.load_model( args.snapshot, custom_objects=utils.get_custom_objects(args.architecture), compile=False) else: model = utils.build_network( data_generator.num_classes,
embedding = pickle.load(pf) embed_labels = embedding['ind2label'] embedding = embedding['embedding'] # Load dataset data_generator = get_data_generator(args.dataset, args.data_root, classes = embed_labels) if embedding is None: embedding = np.eye(data_generator.num_classes) # Construct and train model if (args.gpus <= 1) or args.gpu_merge: if args.snapshot and os.path.exists(args.snapshot): print('Resuming from snapshot {}'.format(args.snapshot)) model = keras.models.load_model(args.snapshot, custom_objects = utils.get_custom_objects(args.architecture), compile = False) else: embed_model = utils.build_network(embedding.shape[1], args.architecture, input_channels=data_generator.num_channels) model = embed_model if args.loss == 'inv_corr': model = keras.models.Model(model.inputs, keras.layers.Lambda(utils.l2norm, name = 'l2norm')(model.output)) elif args.loss == 'softmax_corr': model = keras.models.Model(model.inputs, keras.layers.Activation('softmax', name = 'softmax')(model.output)) if args.cls_weight > 0: model = cls_model(model, data_generator.num_classes, args.cls_base) par_model = model if args.gpus <= 1 else keras.utils.multi_gpu_model(model, gpus = args.gpus, cpu_merge = False) else: with K.tf.device('/cpu:0'): if args.snapshot and os.path.exists(args.snapshot): print('Resuming from snapshot {}'.format(args.snapshot)) model = keras.models.load_model(args.snapshot, custom_objects = utils.get_custom_objects(args.architecture), compile = False) else: embed_model = utils.build_network(embedding.shape[1], args.architecture, input_channels=data_generator.num_channels)